1 ;;; ob-tangle.el --- extract source code from org-mode files
3 ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs 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 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs 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. If not, see <http://www.gnu.org/licenses/>.
27 ;; Extract the code from source blocks out into raw source-code files.
35 (declare-function org-link-escape
"org" (text &optional table
))
36 (declare-function org-heading-components
"org" ())
38 (defcustom org-babel-tangle-lang-exts
39 '(("emacs-lisp" .
"el"))
40 "Alist mapping languages to their file extensions.
41 The key is the language name, the value is the string that should
42 be inserted as the extension commonly used to identify files
43 written in this language. If no entry is found in this list,
44 then the name of the language is used."
45 :group
'org-babel-tangle
48 (string "Language name")
49 (string "File Extension"))))
51 (defcustom org-babel-post-tangle-hook nil
52 "Hook run in code files tangled by `org-babel-tangle'."
56 (defmacro org-babel-with-temp-filebuffer
(file &rest body
)
57 "Open FILE into a temporary buffer execute BODY there like
58 `progn', then kill the FILE buffer returning the result of
61 (let ((temp-result (make-symbol "temp-result"))
62 (temp-file (make-symbol "temp-file")))
63 `(let (,temp-result
,temp-file
)
65 (setf ,temp-file
(current-buffer))
66 (setf ,temp-result
(progn ,@body
))
67 (kill-buffer ,temp-file
)
71 (defun org-babel-load-file (file)
72 "Load Emacs Lisp source code blocks in the Org-mode FILE.
73 This function exports the source code using
74 `org-babel-tangle' and then loads the resulting file using
78 (time-subtract (current-time)
79 (nth 5 (or (file-attributes (file-truename file
))
80 (file-attributes file
)))))))
81 (let* ((base-name (file-name-sans-extension file
))
82 (exported-file (concat base-name
".el")))
83 ;; tangle if the org-mode file is newer than the elisp file
84 (unless (and (file-exists-p exported-file
)
85 (> (age file
) (age exported-file
)))
86 (org-babel-tangle-file file exported-file
"emacs-lisp"))
87 (load-file exported-file
)
88 (message "loaded %s" exported-file
))))
91 (defun org-babel-tangle-file (file &optional target-file lang
)
92 "Extract the bodies of source code blocks in FILE.
93 Source code blocks are extracted with `org-babel-tangle'.
94 Optional argument TARGET-FILE can be used to specify a default
95 export file for all source blocks. Optional argument LANG can be
96 used to limit the exported source code blocks by language."
97 (interactive "fFile to tangle: \nP")
98 (let ((visited-p (get-file-buffer (expand-file-name file
)))
100 (save-window-excursion
102 (setq to-be-removed
(current-buffer))
103 (org-babel-tangle target-file lang
))
105 (kill-buffer to-be-removed
))))
107 (defun org-babel-tangle-publish (_ filename pub-dir
)
108 "Tangle FILENAME and place the results in PUB-DIR."
109 (mapc (lambda (el) (copy-file el pub-dir t
)) (org-babel-tangle-file filename
)))
112 (defun org-babel-tangle (&optional target-file lang
)
113 "Write code blocks to source-specific files.
114 Extract the bodies of all source code blocks from the current
115 file into their own source-specific files. Optional argument
116 TARGET-FILE can be used to specify a default export file for all
117 source blocks. Optional argument LANG can be used to limit the
118 exported source code blocks by language."
122 (let ((block-counter 0)
123 (org-babel-default-header-args
125 (org-babel-merge-params org-babel-default-header-args
126 (list (cons :tangle target-file
)))
127 org-babel-default-header-args
))
129 (mapc ;; map over all languages
131 (let* ((lang (car by-lang
))
132 (specs (cdr by-lang
))
133 (ext (or (cdr (assoc lang org-babel-tangle-lang-exts
)) lang
))
136 (or (and (cdr (assoc lang org-src-lang-modes
))
138 (cdr (assoc lang org-src-lang-modes
))))
144 (flet ((get-spec (name)
145 (cdr (assoc name
(nth 2 spec
)))))
146 (let* ((tangle (get-spec :tangle
))
147 (she-bang ((lambda (sheb) (when (> (length sheb
) 0) sheb
))
148 (get-spec :shebang
)))
150 ((string= "yes" tangle
)
151 (file-name-sans-extension
153 ((string= "no" tangle
) nil
)
154 ((> (length tangle
) 0) tangle
)))
155 (file-name (when base-name
156 ;; decide if we want to add ext to base-name
157 (if (and ext
(string= "yes" tangle
))
158 (concat base-name
"." ext
) base-name
))))
160 ;; delete any old versions of file
161 (when (and (file-exists-p file-name
)
162 (not (member file-name path-collector
)))
163 (delete-file file-name
))
164 ;; drop source-block to file
166 (when (fboundp lang-f
) (funcall lang-f
))
167 (when (and she-bang
(not (member file-name she-banged
)))
168 (insert (concat she-bang
"\n"))
169 (setq she-banged
(cons file-name she-banged
)))
170 (org-babel-spec-to-string spec
)
171 ;; We avoid append-to-file as it does not work with tramp.
172 (let ((content (buffer-string)))
174 (if (file-exists-p file-name
)
175 (insert-file-contents file-name
))
176 (goto-char (point-max))
178 (write-region nil nil file-name
))))
179 ;; if files contain she-bangs, then make the executable
180 (when she-bang
(set-file-modes file-name ?
\755))
182 (setq block-counter
(+ 1 block-counter
))
183 (add-to-list 'path-collector file-name
)))))
185 (org-babel-tangle-collect-blocks lang
))
186 (message "tangled %d code block%s" block-counter
187 (if (= block-counter
1) "" "s"))
188 ;; run `org-babel-post-tangle-hook' in all tangled files
189 (when org-babel-post-tangle-hook
192 (org-babel-with-temp-filebuffer file
193 (run-hooks 'org-babel-post-tangle-hook
)))
197 (defun org-babel-tangle-clean ()
198 "Remove comments inserted by `org-babel-tangle'.
199 Call this function inside of a source-code file generated by
200 `org-babel-tangle' to remove all comments inserted automatically
201 by `org-babel-tangle'. Warning, this comment removes any lines
202 containing constructs which resemble org-mode file links or noweb
205 (goto-char (point-min))
206 (while (or (re-search-forward "\\[\\[file:.*\\]\\[.*\\]\\]" nil t
)
207 (re-search-forward "<<[^[:space:]]*>>" nil t
))
208 (delete-region (save-excursion (beginning-of-line 1) (point))
209 (save-excursion (end-of-line 1) (forward-char 1) (point)))))
211 (defvar org-stored-links
)
212 (defun org-babel-tangle-collect-blocks (&optional lang
)
213 "Collect source blocks in the current Org-mode file.
214 Return an association list of source-code block specifications of
215 the form used by `org-babel-spec-to-string' grouped by language.
216 Optional argument LANG can be used to limit the collected source
217 code blocks by language."
218 (let ((block-counter 1) (current-heading "") blocks
)
219 (org-babel-map-src-blocks (buffer-file-name)
220 ((lambda (new-heading)
221 (if (not (string= new-heading current-heading
))
223 (setq block-counter
1)
224 (setq current-heading new-heading
))
225 (setq block-counter
(+ 1 block-counter
))))
226 (replace-regexp-in-string "[ \t]" "-"
227 (nth 4 (org-heading-components))))
228 (let* ((link (progn (call-interactively 'org-store-link
)
229 (org-babel-clean-text-properties
230 (car (pop org-stored-links
)))))
231 (info (org-babel-get-src-block-info))
232 (source-name (intern (or (nth 4 info
)
234 current-heading block-counter
))))
235 (src-lang (nth 0 info
))
236 (expand-cmd (intern (concat "org-babel-expand-body:" src-lang
)))
237 (params (nth 2 info
))
239 (unless (string= (cdr (assoc :tangle params
)) "no") ;; skip
240 (unless (and lang
(not (string= lang src-lang
))) ;; limit by language
241 ;; add the spec for this block to blocks under it's language
242 (setq by-lang
(cdr (assoc src-lang blocks
)))
243 (setq blocks
(delq (assoc src-lang blocks
) blocks
))
247 (cons (list link source-name params
249 (if (assoc :no-expand params
)
252 (if (fboundp expand-cmd
)
254 'org-babel-expand-body
:generic
)
257 (if (and (cdr (assoc :noweb params
))
260 (cdr (assoc :noweb params
))))
261 (org-babel-expand-noweb-references
264 by-lang
)) blocks
))))))
265 ;; ensure blocks in the correct order
268 (lambda (by-lang) (cons (car by-lang
) (reverse (cdr by-lang
))))
272 (defun org-babel-spec-to-string (spec)
273 "Insert SPEC into the current file.
274 Insert the source-code specified by SPEC into the current
275 source code file. This function uses `comment-region' which
276 assumes that the appropriate major-mode is set. SPEC has the
279 (link source-name params body)"
280 (let ((link (nth 0 spec
))
281 (source-name (nth 1 spec
))
283 (commentable (string= (cdr (assoc :comments
(nth 2 spec
))) "yes")))
284 (flet ((insert-comment (text)
287 (comment-region (point)
288 (progn (insert text
) (point)))
291 (insert-comment (format "[[%s][%s]]" (org-link-escape link
) source-name
))
292 (insert (format "\n%s\n" (replace-regexp-in-string
293 "^," "" (org-babel-chomp body
))))
294 (insert-comment (format "%s ends here" source-name
)))))
298 ;; arch-tag: 413ced93-48f5-4216-86e4-3fc5df8c8f24
300 ;;; ob-tangle.el ends here