1 ;;; org-babel-tangle.el --- Extract source code from org-mode files
3 ;; Copyright (C) 2009 Dan Davison, Eric Schulte
5 ;; Author: Dan Davison, Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
12 ;; This program is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; Extract the code from source blocks out into raw source-code files.
34 (defvar org-babel-tangle-langs nil
35 "Association list matching source-block languages. The car of
36 each element should be a string indicating the source block
37 language, and the cdr should be a list containing the extension
38 and shebang(#!) line to use when writing out the language to
41 (defun org-babel-load-file (file)
42 "Load the contents of the Emacs Lisp source code blocks in the
43 org-mode formatted FILE. This function will first export the
44 source code using `org-babel-tangle' and then load the resulting
45 file using `load-file'."
46 (let ((loadable-file (first (org-babel-tangle-file file
"emacs-lisp"))))
47 (message "loading %s" loadable-file
)
48 (unless (file-exists-p loadable-file
)
49 (error "can't load file that doesn't exist"))
50 (load-file loadable-file
)
51 (message "loaded %s" loadable-file
)))
53 (defun org-babel-tangle-file (file &optional lang
)
54 "Extract the bodies of all source code blocks in FILE with
55 `org-babel-tangle'. Optional argument LANG can be used to limit
56 the exported source code blocks by language."
59 (time-subtract (current-time)
60 (sixth (file-attributes file
))))))
61 (let ((target-file (concat (file-name-sans-extension file
) "."
62 (second (assoc lang org-babel-tangle-langs
)))))
63 (if (and lang
(file-exists-p target-file
) (> (age file
) (age target-file
)))
65 (save-window-excursion (find-file file
) (org-babel-tangle lang
))))))
67 (defun org-babel-tangle (&optional lang
)
68 "Extract the bodies of all source code blocks from the current
69 file into their own source-specific files. Optional argument
70 LANG can be used to limit the exported source code blocks by
74 (let ((base-name (file-name-sans-extension (buffer-file-name)))
77 (mapc ;; for every language create a file
79 (let* ((lang (car by-lang
))
80 (lang-f (intern (concat lang
"-mode")))
81 (lang-specs (cdr (assoc lang org-babel-tangle-langs
)))
82 (ext (first lang-specs
))
83 (she-bang (second lang-specs
))
84 (by-session (cdr by-lang
)))
85 (flet ((to-file (filename specs
)
86 (add-to-list 'path-collector filename
)
87 (with-temp-file filename
89 (when she-bang
(insert (concat she-bang
"\n")))
91 (point) (progn (insert "generated by org-babel-tangle") (point)))
92 (mapc #'org-babel-spec-to-string
(reverse specs
)))))
93 ;; if there are multiple sessions then break out by session
94 (if (> (length by-session
) 1)
95 (mapc (lambda (session-pair)
96 (setq block-counter
(+ block-counter
(length (cdr session-pair
))))
98 "%s-%s.%s" base-name
(car session-pair
) ext
) (cdr session-pair
)))
100 (setq block-counter
(+ block-counter
(length (cdr (car by-session
)))))
101 (to-file (format "%s.%s" base-name ext
) (cdr (car by-session
)))))))
102 (org-babel-collect-blocks lang
))
103 (message "tangled %d source-code blocks" block-counter
)
106 (defun org-babel-collect-blocks (&optional lang
)
107 "Collect all source blocks in the current org-mode file.
108 Return two nested association lists, first grouped by language,
109 then by session, the contents will be source-code block
110 specifications of the form used by `org-babel-spec-to-string'.
111 Optional argument LANG can be used to limit the collected source
112 code blocks by language."
113 (let ((block-counter 0) blocks
)
114 (org-babel-map-source-blocks (buffer-file-name)
115 (setq block-counter
(+ 1 block-counter
))
116 (let* ((link (progn (call-interactively 'org-store-link
)
117 (org-babel-clean-text-properties (car (pop org-stored-links
)))))
118 (source-name (intern (or (org-babel-get-src-block-name)
119 (format "block-%d" block-counter
))))
120 (info (org-babel-get-src-block-info))
121 (src-lang (first info
))
123 (params (third info
))
124 (spec (list link source-name params body
))
125 (session (cdr (assoc :session params
)))
127 (unless (and lang
(not (string= lang src-lang
))) ;; maybe limit by language
128 ;; add the spec for this block to blocks under it's language and session
129 (setq by-lang
(cdr (assoc src-lang blocks
)))
130 (setq blocks
(delq (assoc src-lang blocks
) blocks
))
131 (setq by-session
(cdr (assoc session by-lang
)))
132 (setq by-lang
(delq (assoc session by-lang
) by-lang
))
133 (setq blocks
(cons ;; by-language
134 (cons src-lang
(cons ;; by-session
135 (cons session
(cons spec by-session
)) by-lang
))
137 ;; blocks should contain all source-blocks organized by language and session
138 ;; (message "blocks=%S" blocks) ;; debugging
141 (defun org-babel-spec-to-string (spec)
142 "Insert the source-code specified by SPEC into the current
143 source code file. This function uses `comment-region' which
144 assumes that the appropriate major-mode is set. SPEC has the
147 (link source-name params body)"
148 (let ((link (first spec
))
149 (source-name (second spec
))
151 (comment-padding "* "))
152 (flet ((insert-comment (text)
153 (comment-region (point) (progn (insert text
) (point)))))
155 (insert-comment (format "* [[%s][%s]]" (org-link-escape link
) source-name
))
156 (insert (format "\n%s\n" (org-babel-chomp body
)))
157 (insert-comment (format "%s ends here" source-name
))
160 (provide 'org-babel-tangle
)
161 ;;; org-babel-tangle.el ends here