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 (file-attributes file
))))))
52 (let* ((base-name (file-name-sans-extension file
))
53 (exported-file (concat base-name
".el")))
54 ;; tangle if the org-mode file is newer than the elisp file
55 (unless (and (file-exists-p exported-file
) (> (age file
) (age exported-file
)))
56 (org-babel-tangle-file file base-name
"emacs-lisp"))
57 (load-file exported-file
)
58 (message "loaded %s" exported-file
))))
60 (defun org-babel-tangle-file (file &optional target-file lang
)
61 "Extract the bodies of all source code blocks in FILE with
62 `org-babel-tangle'. Optional argument TARGET-FILE can be used to
63 specify a default export file for all source blocks. Optional
64 argument LANG can be used to limit the exported source code
66 (interactive "fFile to tangle: \nP")
67 (save-window-excursion (find-file file
) (org-babel-tangle target-file lang
)))
69 (defun org-babel-tangle (&optional target-file lang
)
70 "Extract the bodies of all source code blocks from the current
71 file into their own source-specific files. Optional argument
72 TARGET-FILE can be used to specify a default export file for all
73 source blocks. Optional argument LANG can be used to limit the
74 exported source code blocks by language."
78 (let ((block-counter 0)
80 (mapc ;; map over all languages
82 (let* ((lang (car by-lang
))
84 (lang-f (intern (concat
85 (or (and (cdr (assoc lang org-src-lang-modes
))
87 (cdr (assoc lang org-src-lang-modes
))))
90 (lang-specs (cdr (assoc lang org-babel-tangle-langs
)))
91 (ext (first lang-specs
))
92 (she-bang (second lang-specs
))
93 (commentable (not (third lang-specs
)))
97 (let* ((tangle (cdr (assoc :tangle
(third spec
))))
99 ((string= "yes" tangle
)
100 (file-name-sans-extension (buffer-file-name)))
101 ((string= "no" tangle
) nil
)
102 ((> (length tangle
) 0) tangle
))
104 (file-name (when base-name
105 (if (string= base-name
106 (file-name-sans-extension base-name
))
107 (concat base-name
"." ext
) base-name
))))
109 ;; (message "tangle=%S base-name=%S file-name=%S"
110 ;; tangle base-name file-name)
112 ;; delete any old versions of file
113 (when (and (file-exists-p file-name
)
114 (not (member file-name path-collector
)))
115 (delete-file file-name
))
116 ;; drop source-block to file
119 (when (and she-bang
(not (member file-name she-banged
)))
120 (insert (concat she-bang
"\n"))
121 (setq she-banged
(cons file-name she-banged
)))
124 (point) (progn (insert "generated by org-babel-tangle") (point)))
125 (move-end-of-line nil
))
126 (org-babel-spec-to-string spec
)
127 (append-to-file nil nil file-name
))
129 (setq block-counter
(+ 1 block-counter
))
130 (add-to-list 'path-collector file-name
))))
132 (org-babel-tangle-collect-blocks lang
))
133 (message "tangled %d code block%s" block-counter
134 (if (= block-counter
1) "" "s"))
137 (defun org-babel-tangle-clean ()
138 "Call this function inside of a source-code file generated by
139 `org-babel-tangle' to remove all comments inserted automatically
140 by `org-babel-tangle'. Warning, this comment removes any lines
141 containing constructs which resemble org-mode file links or noweb
144 (goto-char (point-min))
145 (while (or (re-search-forward "\\[\\[file:.*\\]\\[.*\\]\\]" nil t
)
146 (re-search-forward "<<[^[:space:]]*>>" nil t
))
147 (delete-region (save-excursion (move-beginning-of-line 1) (point))
148 (save-excursion (move-end-of-line 1) (forward-char 1) (point)))))
150 (defun org-babel-tangle-collect-blocks (&optional lang
)
151 "Collect all source blocks in the current org-mode file.
152 Return an association list of source-code block specifications of
153 the form used by `org-babel-spec-to-string' grouped by language.
154 Optional argument LANG can be used to limit the collected source
155 code blocks by language."
156 (let ((block-counter 0) blocks
)
157 (org-babel-map-source-blocks (buffer-file-name)
158 (setq block-counter
(+ 1 block-counter
))
159 (let* ((link (progn (call-interactively 'org-store-link
)
160 (org-babel-clean-text-properties (car (pop org-stored-links
)))))
161 (info (org-babel-get-src-block-info))
162 (source-name (intern (or (fifth info
)
163 (format "block-%d" block-counter
))))
164 (src-lang (first info
))
165 (body (org-babel-expand-noweb-references info
))
166 (params (third info
))
167 (spec (list link source-name params body
(third (cdr (assoc src-lang org-babel-tangle-langs
)))))
169 (unless (string= (cdr (assoc :tangle params
)) "no") ;; maybe skip
170 (unless (and lang
(not (string= lang src-lang
))) ;; maybe limit by language
171 ;; add the spec for this block to blocks under it's language
172 (setq by-lang
(cdr (assoc src-lang blocks
)))
173 (setq blocks
(delq (assoc src-lang blocks
) blocks
))
174 (setq blocks
(cons (cons src-lang
(cons spec by-lang
)) blocks
))))))
175 ;; ensure blocks in the correct order
177 (mapcar (lambda (by-lang) (cons (car by-lang
) (reverse (cdr by-lang
)))) blocks
))
178 ;; blocks should contain all source-blocks organized by language
179 ;; (message "blocks=%S" blocks) ;; debugging
182 (defun org-babel-spec-to-string (spec)
183 "Insert the source-code specified by SPEC into the current
184 source code file. This function uses `comment-region' which
185 assumes that the appropriate major-mode is set. SPEC has the
188 (link source-name params body)"
189 (flet ((insert-comment (text)
192 (comment-region (point) (progn (insert text
) (point)))
193 (move-end-of-line nil
)
195 (let ((link (first spec
))
196 (source-name (second spec
))
198 (commentable (not (fifth spec
))))
199 (insert-comment (format "[[%s][%s]]" (org-link-escape link
) source-name
))
200 (insert (format "%s" (org-babel-chomp body
)))
201 (insert-comment (format "%s ends here" source-name
)))))
203 (provide 'org-babel-tangle
)
204 ;;; org-babel-tangle.el ends here