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 shebang(#!) line to use when writing out the language to file,
39 and an optional flag indicating that the language is not
42 (defun org-babel-load-file (file)
43 "Load the contents of the Emacs Lisp source code blocks in the
44 org-mode formatted FILE. This function will first export the
45 source code using `org-babel-tangle' and then load the resulting
46 file using `load-file'."
49 (time-subtract (current-time)
50 (sixth (file-attributes file
))))))
51 (let* ((base-name (file-name-sans-extension file
))
52 (exported-file (concat base-name
".el")))
53 ;; tangle if the org-mode file is newer than the elisp file
54 (unless (and (file-exists-p exported-file
) (> (age file
) (age exported-file
)))
55 (org-babel-tangle-file file base-name
"emacs-lisp"))
56 (load-file exported-file
)
57 (message "loaded %s" exported-file
))))
59 (defun org-babel-tangle-file (file &optional target-file lang
)
60 "Extract the bodies of all source code blocks in FILE with
61 `org-babel-tangle'. Optional argument TARGET-FILE can be used to
62 specify a default export file for all source blocks. Optional
63 argument LANG can be used to limit the exported source code
65 (save-window-excursion (find-file file
) (org-babel-tangle target-file lang
)))
67 (defun org-babel-tangle (&optional target-file lang
)
68 "Extract the bodies of all source code blocks from the current
69 file into their own source-specific files. Optional argument
70 TARGET-FILE can be used to specify a default export file for all
71 source blocks. Optional argument LANG can be used to limit the
72 exported source code blocks by language."
75 (let ((block-counter 0)
77 (mapc ;; map over all languages
79 (let* ((lang (car by-lang
))
81 (lang-f (intern (concat
83 (cdr (assoc lang org-src-lang-modes
)))
86 (lang-specs (cdr (assoc lang org-babel-tangle-langs
)))
87 (ext (first lang-specs
))
88 (she-bang (second lang-specs
))
89 (commentable (not (third lang-specs
))))
92 (let* ((tangle (cdr (assoc :tangle
(third spec
))))
94 ((string= "yes" tangle
)
95 (file-name-sans-extension (buffer-file-name)))
96 ((string= "no" tangle
) nil
)
97 ((> (length tangle
) 0) tangle
))
99 (file-name (when base-name
100 (if (string= base-name
101 (file-name-sans-extension base-name
))
102 (concat base-name
"." ext
) base-name
))))
104 ;; (message "tangle=%S base-name=%S file-name=%S"
105 ;; tangle base-name file-name)
107 ;; delete any old versions of file
108 (when (and (file-exists-p file-name
)
109 (not (member file-name path-collector
)))
110 (delete-file file-name
))
111 ;; drop source-block to file
114 (when she-bang
(insert (concat she-bang
"\n")))
117 (point) (progn (insert "generated by org-babel-tangle") (point)))
118 (move-end-of-line nil
))
119 (org-babel-spec-to-string spec
)
120 (append-to-file nil nil file-name
))
122 (setq block-counter
(+ 1 block-counter
))
123 (add-to-list 'path-collector file-name
))))
125 (org-babel-tangle-collect-blocks lang
))
126 (message "tangled %d source-code block%s" block-counter
127 (if (> block-counter
1) "s" ""))
130 (defun org-babel-tangle-clean ()
131 "Call this function inside of a source-code file generated by
132 `org-babel-tangle' to remove all comments inserted automatically
133 by `org-babel-tangle'. Warning, this comment removes any lines
134 containing constructs which resemble org-mode file links or noweb
137 (goto-char (point-min))
138 (while (or (re-search-forward "\\[\\[file:.*\\]\\[.*\\]\\]" nil t
)
139 (re-search-forward "<<[^[:space:]]*>>" nil t
))
140 (delete-region (save-excursion (move-beginning-of-line 1) (point))
141 (save-excursion (move-end-of-line 1) (forward-char 1) (point)))))
143 (defun org-babel-tangle-collect-blocks (&optional lang
)
144 "Collect all source blocks in the current org-mode file.
145 Return an association list of source-code block specifications of
146 the form used by `org-babel-spec-to-string' grouped by language.
147 Optional argument LANG can be used to limit the collected source
148 code blocks by language."
149 (let ((block-counter 0) blocks
)
150 (org-babel-map-source-blocks (buffer-file-name)
151 (setq block-counter
(+ 1 block-counter
))
152 (let* ((link (progn (call-interactively 'org-store-link
)
153 (org-babel-clean-text-properties (car (pop org-stored-links
)))))
154 (source-name (intern (or (org-babel-get-src-block-name)
155 (format "block-%d" block-counter
))))
156 (info (org-babel-get-src-block-info))
157 (src-lang (first info
))
158 (body (org-babel-expand-noweb-references info
))
159 (params (third info
))
160 (spec (list link source-name params body
(third (cdr (assoc src-lang org-babel-tangle-langs
)))))
162 (unless (string= (cdr (assoc :tangle params
)) "no") ;; maybe skip
163 (unless (and lang
(not (string= lang src-lang
))) ;; maybe limit by language
164 ;; add the spec for this block to blocks under it's language
165 (setq by-lang
(cdr (assoc src-lang blocks
)))
166 (setq blocks
(delq (assoc src-lang blocks
) blocks
))
167 (setq blocks
(cons (cons src-lang
(cons spec by-lang
)) blocks
))))))
168 ;; ensure blocks in the correct order
170 (mapcar (lambda (by-lang) (cons (car by-lang
) (reverse (cdr by-lang
)))) blocks
))
171 ;; blocks should contain all source-blocks organized by language
172 ;; (message "blocks=%S" blocks) ;; debugging
175 (defun org-babel-spec-to-string (spec)
176 "Insert the source-code specified by SPEC into the current
177 source code file. This function uses `comment-region' which
178 assumes that the appropriate major-mode is set. SPEC has the
181 (link source-name params body)"
182 (flet ((insert-comment (text)
184 (comment-region (point) (progn (insert text
) (point)))
185 (move-end-of-line nil
))))
186 (let ((link (first spec
))
187 (source-name (second spec
))
189 (commentable (not (fifth spec
))))
191 (insert-comment (format "[[%s][%s]]" (org-link-escape link
) source-name
))
192 (insert (format "\n%s\n" (org-babel-chomp body
)))
193 (insert-comment (format "%s ends here" source-name
))
196 (defun org-babel-expand-noweb-references (&optional info parent-buffer
)
197 "This function expands Noweb style references in the body of
198 the current source-code block. For example the following
199 reference would be replaced with the body of the source-code
200 block named 'example-block' (assuming the '#' character starts a
205 This function must be called from inside of the buffer containing
206 the source-code block which holds BODY."
207 (let* ((parent-buffer (or parent-buffer
(current-buffer)))
208 (info (or info
(org-babel-get-src-block-info)))
211 (new-body "") index source-name
)
212 (flet ((nb-add (text)
213 (setq new-body
(concat new-body text
))))
215 (insert body
) (goto-char (point-min))
216 (funcall (intern (concat (or (symbol-name
217 (cdr (assoc lang org-src-lang-modes
)))
220 (while (and (re-search-forward "<<\\(.+\\)>>" nil t
))
221 (save-match-data (setf source-name
(match-string 1)))
222 ;; add interval to new-body
223 (goto-char (match-end 0)) (move-end-of-line nil
)
224 (nb-add (buffer-substring index
(point)))
226 ;; if found, add body of referenced source-block
227 (nb-add (save-excursion
228 (set-buffer parent-buffer
)
229 (let ((point (org-babel-find-named-block source-name
)))
233 (concat "\n" (org-babel-expand-noweb-references
234 (org-babel-get-src-block-info))))
236 (nb-add (buffer-substring index
(point-max)))))
239 (provide 'org-babel-tangle
)
240 ;;; org-babel-tangle.el ends here