1 ;;; org-babel-tangle.el --- Extract source code from org-mode files
3 ;; Copyright (C) 2009 Eric Schulte
5 ;; Author: 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 "List of languages supported by `org-babel-tangle'. The first
36 element of each language's list is a string indicating the name
37 of the language, the second element should be the file extension
38 of the language, an optional third element the shebang(#!) line
39 to use when writing out the language to file, and an optional
40 fourth element is a flag which when true indicates that the
41 language does not support comments.")
43 (defun org-babel-load-file (file)
44 "Load the contents of the Emacs Lisp source code blocks in the
45 org-mode formatted FILE. This function will first export the
46 source code using `org-babel-tangle' and then load the resulting
47 file using `load-file'."
50 (time-subtract (current-time)
51 (sixth (or (file-attributes (file-truename file
))
52 (file-attributes file
)))))))
53 (let* ((base-name (file-name-sans-extension file
))
54 (exported-file (concat base-name
".el")))
55 ;; tangle if the org-mode file is newer than the elisp file
56 (unless (and (file-exists-p exported-file
) (> (age file
) (age exported-file
)))
57 (org-babel-tangle-file file base-name
"emacs-lisp"))
58 (load-file exported-file
)
59 (message "loaded %s" exported-file
))))
61 (defun org-babel-tangle-file (file &optional target-file lang
)
62 "Extract the bodies of all source code blocks in FILE with
63 `org-babel-tangle'. Optional argument TARGET-FILE can be used to
64 specify a default export file for all source blocks. Optional
65 argument LANG can be used to limit the exported source code
67 (interactive "fFile to tangle: \nP")
68 (save-window-excursion (find-file file
) (org-babel-tangle target-file lang
)))
70 (defun org-babel-tangle (&optional target-file lang
)
71 "Extract the bodies of all source code blocks from the current
72 file into their own source-specific files. Optional argument
73 TARGET-FILE can be used to specify a default export file for all
74 source blocks. Optional argument LANG can be used to limit the
75 exported source code blocks by language."
79 (let ((block-counter 0)
81 (mapc ;; map over all languages
83 (let* ((lang (car by-lang
))
85 (lang-f (intern (concat
86 (or (and (cdr (assoc lang org-src-lang-modes
))
88 (cdr (assoc lang org-src-lang-modes
))))
91 (lang-specs (cdr (assoc lang org-babel-tangle-langs
)))
92 (ext (first lang-specs
))
93 (she-bang (second lang-specs
))
94 (commentable (not (third lang-specs
)))
98 (flet ((get-spec (name)
99 (cdr (assoc name
(third spec
)))))
100 (let* ((tangle (get-spec :tangle
))
101 (she-bang (if (> (length (get-spec :shebang
)) 0)
105 ((string= "yes" tangle
)
106 (file-name-sans-extension (buffer-file-name)))
107 ((string= "no" tangle
) nil
)
108 ((> (length tangle
) 0) tangle
))
110 (file-name (when base-name
111 ;; decide if we want to add ext to base-name
112 (if (and ext
(not (string= (file-name-extension base-name
) ext
)))
113 (concat base-name
"." ext
) base-name
))))
116 ;; "tangle=%S base-name=%S file-name=%S she-bang=%S commentable=%s"
117 ;; tangle base-name file-name she-bang commentable)
119 ;; delete any old versions of file
120 (when (and (file-exists-p file-name
)
121 (not (member file-name path-collector
)))
122 (delete-file file-name
))
123 ;; drop source-block to file
126 (when (and she-bang
(not (member file-name she-banged
)))
127 (insert (concat she-bang
"\n"))
128 (setq she-banged
(cons file-name she-banged
)))
129 (org-babel-spec-to-string spec
)
130 (append-to-file nil nil file-name
))
132 (setq block-counter
(+ 1 block-counter
))
133 (add-to-list 'path-collector file-name
)))))
135 (org-babel-tangle-collect-blocks lang
))
136 (message "tangled %d code block%s" block-counter
137 (if (= block-counter
1) "" "s"))
140 (defun org-babel-tangle-clean ()
141 "Call this function inside of a source-code file generated by
142 `org-babel-tangle' to remove all comments inserted automatically
143 by `org-babel-tangle'. Warning, this comment removes any lines
144 containing constructs which resemble org-mode file links or noweb
147 (goto-char (point-min))
148 (while (or (re-search-forward "\\[\\[file:.*\\]\\[.*\\]\\]" nil t
)
149 (re-search-forward "<<[^[:space:]]*>>" nil t
))
150 (delete-region (save-excursion (move-beginning-of-line 1) (point))
151 (save-excursion (move-end-of-line 1) (forward-char 1) (point)))))
153 (defun org-babel-tangle-collect-blocks (&optional lang
)
154 "Collect all source blocks in the current org-mode file.
155 Return an association list of source-code block specifications of
156 the form used by `org-babel-spec-to-string' grouped by language.
157 Optional argument LANG can be used to limit the collected source
158 code blocks by language."
159 (let ((block-counter 0) blocks
)
160 (org-babel-map-source-blocks (buffer-file-name)
161 (setq block-counter
(+ 1 block-counter
))
162 (let* ((link (progn (call-interactively 'org-store-link
)
163 (org-babel-clean-text-properties (car (pop org-stored-links
)))))
164 (info (org-babel-get-src-block-info))
165 (source-name (intern (or (fifth info
)
166 (format "block-%d" block-counter
))))
167 (src-lang (first info
))
168 (body (org-babel-expand-noweb-references info
))
169 (params (third info
))
170 (spec (list link source-name params body
(third (cdr (assoc src-lang org-babel-tangle-langs
)))))
172 (unless (string= (cdr (assoc :tangle params
)) "no") ;; maybe skip
173 (unless (and lang
(not (string= lang src-lang
))) ;; maybe limit by language
174 ;; add the spec for this block to blocks under it's language
175 (setq by-lang
(cdr (assoc src-lang blocks
)))
176 (setq blocks
(delq (assoc src-lang blocks
) blocks
))
177 (setq blocks
(cons (cons src-lang
(cons spec by-lang
)) blocks
))))))
178 ;; ensure blocks in the correct order
180 (mapcar (lambda (by-lang) (cons (car by-lang
) (reverse (cdr by-lang
)))) blocks
))
181 ;; blocks should contain all source-blocks organized by language
182 ;; (message "blocks=%S" blocks) ;; debugging
185 (defun org-babel-spec-to-string (spec)
186 "Insert the source-code specified by SPEC into the current
187 source code file. This function uses `comment-region' which
188 assumes that the appropriate major-mode is set. SPEC has the
191 (link source-name params body)"
192 (flet ((insert-comment (text)
195 (comment-region (point) (progn (insert text
) (point)))
196 (move-end-of-line nil
)
198 (let ((link (first spec
))
199 (source-name (second spec
))
201 (commentable (not (if (> (length (cdr (assoc :comments
(third spec
)))) 0)
202 (string= (cdr (assoc :comments
(third spec
))) "no")
204 (insert-comment (format "[[%s][%s]]" (org-link-escape link
) source-name
))
205 (insert (format "%s" (org-babel-chomp body
)))
206 (insert-comment (format "%s ends here" source-name
)))))
208 (provide 'org-babel-tangle
)
209 ;;; org-babel-tangle.el ends here