2009-06-14 21:30:29 -04:00
|
|
|
;;; org-babel-tangle.el --- Extract source code from org-mode files
|
|
|
|
|
2009-10-29 19:03:43 -04:00
|
|
|
;; Copyright (C) 2009 Eric Schulte
|
2009-06-14 21:30:29 -04:00
|
|
|
|
2009-10-29 19:03:43 -04:00
|
|
|
;; Author: Eric Schulte
|
2009-06-14 21:30:29 -04:00
|
|
|
;; Keywords: literate programming, reproducible research
|
|
|
|
;; Homepage: http://orgmode.org
|
|
|
|
;; Version: 0.01
|
|
|
|
|
|
|
|
;;; License:
|
|
|
|
|
|
|
|
;; This program is free software; you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation; either version 3, or (at your option)
|
|
|
|
;; any later version.
|
|
|
|
;;
|
|
|
|
;; This program is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
;;
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
|
|
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
;; Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; Extract the code from source blocks out into raw source-code files.
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
(require 'org-babel)
|
|
|
|
|
2009-06-15 18:25:46 -04:00
|
|
|
(defvar org-babel-tangle-langs nil
|
2009-11-20 11:12:19 -05:00
|
|
|
"List of languages supported by `org-babel-tangle'. The first
|
|
|
|
element of each language's list is a string indicating the name
|
|
|
|
of the language, the second element should be the file extension
|
|
|
|
of the language, an optional third element the shebang(#!) line
|
|
|
|
to use when writing out the language to file, and an optional
|
|
|
|
fourth element is a flag which when true indicates that the
|
|
|
|
language does not support comments.")
|
2009-06-15 18:25:46 -04:00
|
|
|
|
2009-07-14 21:29:28 -04:00
|
|
|
(defun org-babel-load-file (file)
|
2009-07-14 21:37:47 -04:00
|
|
|
"Load the contents of the Emacs Lisp source code blocks in the
|
|
|
|
org-mode formatted FILE. This function will first export the
|
|
|
|
source code using `org-babel-tangle' and then load the resulting
|
|
|
|
file using `load-file'."
|
2009-07-14 23:16:51 -04:00
|
|
|
(flet ((age (file)
|
|
|
|
(time-to-seconds
|
|
|
|
(time-subtract (current-time)
|
2010-02-08 15:17:06 -05:00
|
|
|
(sixth (or (file-attributes (file-truename file))
|
|
|
|
(file-attributes file)))))))
|
2009-07-30 10:57:26 -04:00
|
|
|
(let* ((base-name (file-name-sans-extension file))
|
|
|
|
(exported-file (concat base-name ".el")))
|
|
|
|
;; tangle if the org-mode file is newer than the elisp file
|
|
|
|
(unless (and (file-exists-p exported-file) (> (age file) (age exported-file)))
|
2010-03-18 14:30:54 -04:00
|
|
|
(org-babel-tangle-file file exported-file "emacs-lisp"))
|
2009-07-30 10:57:26 -04:00
|
|
|
(load-file exported-file)
|
|
|
|
(message "loaded %s" exported-file))))
|
|
|
|
|
|
|
|
(defun org-babel-tangle-file (file &optional target-file lang)
|
|
|
|
"Extract the bodies of all source code blocks in FILE with
|
|
|
|
`org-babel-tangle'. Optional argument TARGET-FILE can be used to
|
|
|
|
specify a default export file for all source blocks. Optional
|
|
|
|
argument LANG can be used to limit the exported source code
|
|
|
|
blocks by language."
|
2009-10-20 10:17:42 -04:00
|
|
|
(interactive "fFile to tangle: \nP")
|
2009-07-30 10:57:26 -04:00
|
|
|
(save-window-excursion (find-file file) (org-babel-tangle target-file lang)))
|
2009-07-14 21:26:15 -04:00
|
|
|
|
2009-07-30 10:57:26 -04:00
|
|
|
(defun org-babel-tangle (&optional target-file lang)
|
2009-07-01 19:16:30 -04:00
|
|
|
"Extract the bodies of all source code blocks from the current
|
2009-07-14 21:26:15 -04:00
|
|
|
file into their own source-specific files. Optional argument
|
2009-07-30 10:57:26 -04:00
|
|
|
TARGET-FILE can be used to specify a default export file for all
|
|
|
|
source blocks. Optional argument LANG can be used to limit the
|
|
|
|
exported source code blocks by language."
|
2009-06-15 18:35:11 -04:00
|
|
|
(interactive)
|
2009-11-08 20:25:15 -05:00
|
|
|
(save-buffer)
|
2009-06-27 14:58:13 -04:00
|
|
|
(save-excursion
|
2009-07-30 22:14:35 -04:00
|
|
|
(let ((block-counter 0)
|
2009-07-14 21:37:47 -04:00
|
|
|
path-collector)
|
2009-07-31 18:53:02 -04:00
|
|
|
(mapc ;; map over all languages
|
2009-06-27 14:58:13 -04:00
|
|
|
(lambda (by-lang)
|
|
|
|
(let* ((lang (car by-lang))
|
2009-07-30 10:57:26 -04:00
|
|
|
(specs (cdr by-lang))
|
2009-08-16 23:14:22 -04:00
|
|
|
(lang-f (intern (concat
|
2009-08-25 10:34:49 -04:00
|
|
|
(or (and (cdr (assoc lang org-src-lang-modes))
|
|
|
|
(symbol-name
|
|
|
|
(cdr (assoc lang org-src-lang-modes))))
|
2009-08-16 23:14:22 -04:00
|
|
|
lang)
|
|
|
|
"-mode")))
|
2009-06-27 14:58:13 -04:00
|
|
|
(lang-specs (cdr (assoc lang org-babel-tangle-langs)))
|
|
|
|
(ext (first lang-specs))
|
2009-08-04 15:23:23 -04:00
|
|
|
(she-bang (second lang-specs))
|
2010-03-27 18:29:25 -04:00
|
|
|
(commentable (and (fboundp lang-f) (not (third lang-specs))))
|
2009-11-04 17:18:50 -05:00
|
|
|
she-banged)
|
2009-07-30 10:57:26 -04:00
|
|
|
(mapc
|
|
|
|
(lambda (spec)
|
2010-02-05 21:04:50 -05:00
|
|
|
(flet ((get-spec (name)
|
|
|
|
(cdr (assoc name (third spec)))))
|
|
|
|
(let* ((tangle (get-spec :tangle))
|
|
|
|
(she-bang (if (> (length (get-spec :shebang)) 0)
|
|
|
|
(get-spec :shebang)
|
|
|
|
she-bang))
|
|
|
|
(base-name (or (cond
|
|
|
|
((string= "yes" tangle)
|
|
|
|
(file-name-sans-extension (buffer-file-name)))
|
|
|
|
((string= "no" tangle) nil)
|
|
|
|
((> (length tangle) 0) tangle))
|
|
|
|
target-file))
|
|
|
|
(file-name (when base-name
|
2010-02-07 13:36:52 -05:00
|
|
|
;; decide if we want to add ext to base-name
|
2010-03-17 20:09:06 -04:00
|
|
|
(if (and ext (string= "yes" tangle))
|
2010-02-05 21:04:50 -05:00
|
|
|
(concat base-name "." ext) base-name))))
|
|
|
|
;; ;; debugging
|
|
|
|
;; (message
|
|
|
|
;; "tangle=%S base-name=%S file-name=%S she-bang=%S commentable=%s"
|
|
|
|
;; tangle base-name file-name she-bang commentable)
|
|
|
|
(when file-name
|
|
|
|
;; delete any old versions of file
|
|
|
|
(when (and (file-exists-p file-name)
|
|
|
|
(not (member file-name path-collector)))
|
|
|
|
(delete-file file-name))
|
|
|
|
;; drop source-block to file
|
|
|
|
(with-temp-buffer
|
2010-03-27 18:29:25 -04:00
|
|
|
(if (fboundp lang-f) (funcall lang-f))
|
2010-02-05 21:04:50 -05:00
|
|
|
(when (and she-bang (not (member file-name she-banged)))
|
|
|
|
(insert (concat she-bang "\n"))
|
|
|
|
(setq she-banged (cons file-name she-banged)))
|
|
|
|
(org-babel-spec-to-string spec)
|
2010-03-17 21:03:21 -04:00
|
|
|
;; We avoid append-to-file as it does not work with tramp.
|
|
|
|
(let ((content (buffer-string)))
|
|
|
|
(with-temp-buffer
|
|
|
|
(if (file-exists-p file-name)
|
|
|
|
(insert-file-contents file-name))
|
|
|
|
(goto-char (point-max))
|
|
|
|
(insert content)
|
|
|
|
(write-region nil nil file-name))))
|
2010-02-05 21:04:50 -05:00
|
|
|
;; update counter
|
|
|
|
(setq block-counter (+ 1 block-counter))
|
|
|
|
(add-to-list 'path-collector file-name)))))
|
2009-07-30 10:57:26 -04:00
|
|
|
specs)))
|
2009-07-22 20:30:30 -04:00
|
|
|
(org-babel-tangle-collect-blocks lang))
|
2009-09-02 15:20:20 -04:00
|
|
|
(message "tangled %d code block%s" block-counter
|
|
|
|
(if (= block-counter 1) "" "s"))
|
2009-07-14 21:37:47 -04:00
|
|
|
path-collector)))
|
2009-06-26 14:43:50 -04:00
|
|
|
|
2009-08-20 18:32:57 -04:00
|
|
|
(defun org-babel-tangle-clean ()
|
|
|
|
"Call this function inside of a source-code file generated by
|
|
|
|
`org-babel-tangle' to remove all comments inserted automatically
|
|
|
|
by `org-babel-tangle'. Warning, this comment removes any lines
|
|
|
|
containing constructs which resemble org-mode file links or noweb
|
|
|
|
references."
|
|
|
|
(interactive)
|
|
|
|
(goto-char (point-min))
|
|
|
|
(while (or (re-search-forward "\\[\\[file:.*\\]\\[.*\\]\\]" nil t)
|
|
|
|
(re-search-forward "<<[^[:space:]]*>>" nil t))
|
|
|
|
(delete-region (save-excursion (move-beginning-of-line 1) (point))
|
|
|
|
(save-excursion (move-end-of-line 1) (forward-char 1) (point)))))
|
|
|
|
|
2009-07-22 20:30:30 -04:00
|
|
|
(defun org-babel-tangle-collect-blocks (&optional lang)
|
2009-06-27 20:25:01 -04:00
|
|
|
"Collect all source blocks in the current org-mode file.
|
2009-07-30 10:57:26 -04:00
|
|
|
Return an association list of source-code block specifications of
|
|
|
|
the form used by `org-babel-spec-to-string' grouped by language.
|
2009-07-14 21:26:15 -04:00
|
|
|
Optional argument LANG can be used to limit the collected source
|
|
|
|
code blocks by language."
|
2009-06-27 20:25:01 -04:00
|
|
|
(let ((block-counter 0) blocks)
|
|
|
|
(org-babel-map-source-blocks (buffer-file-name)
|
|
|
|
(setq block-counter (+ 1 block-counter))
|
|
|
|
(let* ((link (progn (call-interactively 'org-store-link)
|
|
|
|
(org-babel-clean-text-properties (car (pop org-stored-links)))))
|
|
|
|
(info (org-babel-get-src-block-info))
|
2009-10-31 11:36:25 -04:00
|
|
|
(source-name (intern (or (fifth info)
|
|
|
|
(format "block-%d" block-counter))))
|
2009-07-14 21:26:15 -04:00
|
|
|
(src-lang (first info))
|
2009-07-31 19:01:40 -04:00
|
|
|
(body (org-babel-expand-noweb-references info))
|
2009-06-27 20:25:01 -04:00
|
|
|
(params (third info))
|
2009-08-04 15:23:23 -04:00
|
|
|
(spec (list link source-name params body (third (cdr (assoc src-lang org-babel-tangle-langs)))))
|
2009-07-30 10:57:26 -04:00
|
|
|
by-lang)
|
2009-07-22 20:30:30 -04:00
|
|
|
(unless (string= (cdr (assoc :tangle params)) "no") ;; maybe skip
|
|
|
|
(unless (and lang (not (string= lang src-lang))) ;; maybe limit by language
|
2009-07-30 10:57:26 -04:00
|
|
|
;; add the spec for this block to blocks under it's language
|
2009-07-22 20:30:30 -04:00
|
|
|
(setq by-lang (cdr (assoc src-lang blocks)))
|
|
|
|
(setq blocks (delq (assoc src-lang blocks) blocks))
|
2009-07-30 10:57:26 -04:00
|
|
|
(setq blocks (cons (cons src-lang (cons spec by-lang)) blocks))))))
|
2009-08-03 13:00:42 -04:00
|
|
|
;; ensure blocks in the correct order
|
|
|
|
(setq blocks
|
|
|
|
(mapcar (lambda (by-lang) (cons (car by-lang) (reverse (cdr by-lang)))) blocks))
|
2009-07-30 10:57:26 -04:00
|
|
|
;; blocks should contain all source-blocks organized by language
|
2009-08-04 15:23:23 -04:00
|
|
|
;; (message "blocks=%S" blocks) ;; debugging
|
2009-06-27 20:25:01 -04:00
|
|
|
blocks))
|
|
|
|
|
2009-06-25 23:17:21 -04:00
|
|
|
(defun org-babel-spec-to-string (spec)
|
2009-06-27 14:58:13 -04:00
|
|
|
"Insert the source-code specified by SPEC into the current
|
|
|
|
source code file. This function uses `comment-region' which
|
|
|
|
assumes that the appropriate major-mode is set. SPEC has the
|
|
|
|
form
|
2009-06-26 15:12:23 -04:00
|
|
|
|
|
|
|
(link source-name params body)"
|
|
|
|
(flet ((insert-comment (text)
|
2009-08-04 15:23:23 -04:00
|
|
|
(when commentable
|
2009-11-04 17:14:45 -05:00
|
|
|
(insert "\n")
|
2009-08-16 23:14:22 -04:00
|
|
|
(comment-region (point) (progn (insert text) (point)))
|
2009-11-04 17:14:45 -05:00
|
|
|
(move-end-of-line nil)
|
|
|
|
(insert "\n"))))
|
2009-06-27 14:58:13 -04:00
|
|
|
(let ((link (first spec))
|
|
|
|
(source-name (second spec))
|
2009-08-04 15:23:23 -04:00
|
|
|
(body (fourth spec))
|
2010-02-05 21:04:50 -05:00
|
|
|
(commentable (not (if (> (length (cdr (assoc :comments (third spec)))) 0)
|
|
|
|
(string= (cdr (assoc :comments (third spec))) "no")
|
|
|
|
(fifth spec)))))
|
2009-06-27 14:58:13 -04:00
|
|
|
(insert-comment (format "[[%s][%s]]" (org-link-escape link) source-name))
|
2009-11-04 17:14:45 -05:00
|
|
|
(insert (format "%s" (org-babel-chomp body)))
|
|
|
|
(insert-comment (format "%s ends here" source-name)))))
|
2009-06-15 18:35:11 -04:00
|
|
|
|
2009-06-14 21:30:29 -04:00
|
|
|
(provide 'org-babel-tangle)
|
|
|
|
;;; org-babel-tangle.el ends here
|