Notes on C-x s bug with new org-src-mode patch
[rgr-org-mode.git] / lisp / org-babel-tangle.el
blob7da4c2e88bba9e69027c29e5eb2d744d35f44976
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
8 ;; Version: 0.01
10 ;;; License:
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)
15 ;; any later version.
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.
27 ;;; Commentary:
29 ;; Extract the code from source blocks out into raw source-code files.
31 ;;; Code:
32 (require 'org-babel)
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
40 commentable.")
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'."
47 (flet ((age (file)
48 (time-to-seconds
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
64 blocks by language."
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."
73 (interactive)
74 (save-excursion
75 (let ((block-counter 0)
76 path-collector)
77 (mapc ;; map over all languages
78 (lambda (by-lang)
79 (let* ((lang (car by-lang))
80 (specs (cdr by-lang))
81 (lang-f (intern (concat
82 (or (and (cdr (assoc lang org-src-lang-modes))
83 (symbol-name
84 (cdr (assoc lang org-src-lang-modes))))
85 lang)
86 "-mode")))
87 (lang-specs (cdr (assoc lang org-babel-tangle-langs)))
88 (ext (first lang-specs))
89 (she-bang (second lang-specs))
90 (commentable (not (third lang-specs))))
91 (mapc
92 (lambda (spec)
93 (let* ((tangle (cdr (assoc :tangle (third spec))))
94 (base-name (or (cond
95 ((string= "yes" tangle)
96 (file-name-sans-extension (buffer-file-name)))
97 ((string= "no" tangle) nil)
98 ((> (length tangle) 0) tangle))
99 target-file))
100 (file-name (when base-name
101 (if (string= base-name
102 (file-name-sans-extension base-name))
103 (concat base-name "." ext) base-name))))
104 ;; ;; debugging
105 ;; (message "tangle=%S base-name=%S file-name=%S"
106 ;; tangle base-name file-name)
107 (when file-name
108 ;; delete any old versions of file
109 (when (and (file-exists-p file-name)
110 (not (member file-name path-collector)))
111 (delete-file file-name))
112 ;; drop source-block to file
113 (with-temp-buffer
114 (funcall lang-f)
115 (when she-bang (insert (concat she-bang "\n")))
116 (when commentable
117 (comment-region
118 (point) (progn (insert "generated by org-babel-tangle") (point)))
119 (move-end-of-line nil))
120 (org-babel-spec-to-string spec)
121 (append-to-file nil nil file-name))
122 ;; update counter
123 (setq block-counter (+ 1 block-counter))
124 (add-to-list 'path-collector file-name))))
125 specs)))
126 (org-babel-tangle-collect-blocks lang))
127 (message "tangled %d code block%s" block-counter
128 (if (= block-counter 1) "" "s"))
129 path-collector)))
131 (defun org-babel-tangle-clean ()
132 "Call this function inside of a source-code file generated by
133 `org-babel-tangle' to remove all comments inserted automatically
134 by `org-babel-tangle'. Warning, this comment removes any lines
135 containing constructs which resemble org-mode file links or noweb
136 references."
137 (interactive)
138 (goto-char (point-min))
139 (while (or (re-search-forward "\\[\\[file:.*\\]\\[.*\\]\\]" nil t)
140 (re-search-forward "<<[^[:space:]]*>>" nil t))
141 (delete-region (save-excursion (move-beginning-of-line 1) (point))
142 (save-excursion (move-end-of-line 1) (forward-char 1) (point)))))
144 (defun org-babel-tangle-collect-blocks (&optional lang)
145 "Collect all source blocks in the current org-mode file.
146 Return an association list of source-code block specifications of
147 the form used by `org-babel-spec-to-string' grouped by language.
148 Optional argument LANG can be used to limit the collected source
149 code blocks by language."
150 (let ((block-counter 0) blocks)
151 (org-babel-map-source-blocks (buffer-file-name)
152 (setq block-counter (+ 1 block-counter))
153 (let* ((link (progn (call-interactively 'org-store-link)
154 (org-babel-clean-text-properties (car (pop org-stored-links)))))
155 (source-name (intern (or (org-babel-get-src-block-name)
156 (format "block-%d" block-counter))))
157 (info (org-babel-get-src-block-info))
158 (src-lang (first info))
159 (body (org-babel-expand-noweb-references info))
160 (params (third info))
161 (spec (list link source-name params body (third (cdr (assoc src-lang org-babel-tangle-langs)))))
162 by-lang)
163 (unless (string= (cdr (assoc :tangle params)) "no") ;; maybe skip
164 (unless (and lang (not (string= lang src-lang))) ;; maybe limit by language
165 ;; add the spec for this block to blocks under it's language
166 (setq by-lang (cdr (assoc src-lang blocks)))
167 (setq blocks (delq (assoc src-lang blocks) blocks))
168 (setq blocks (cons (cons src-lang (cons spec by-lang)) blocks))))))
169 ;; ensure blocks in the correct order
170 (setq blocks
171 (mapcar (lambda (by-lang) (cons (car by-lang) (reverse (cdr by-lang)))) blocks))
172 ;; blocks should contain all source-blocks organized by language
173 ;; (message "blocks=%S" blocks) ;; debugging
174 blocks))
176 (defun org-babel-spec-to-string (spec)
177 "Insert the source-code specified by SPEC into the current
178 source code file. This function uses `comment-region' which
179 assumes that the appropriate major-mode is set. SPEC has the
180 form
182 (link source-name params body)"
183 (flet ((insert-comment (text)
184 (when commentable
185 (comment-region (point) (progn (insert text) (point)))
186 (move-end-of-line nil))))
187 (let ((link (first spec))
188 (source-name (second spec))
189 (body (fourth spec))
190 (commentable (not (fifth spec))))
191 (insert "\n\n")
192 (insert-comment (format "[[%s][%s]]" (org-link-escape link) source-name))
193 (insert (format "\n%s\n" (org-babel-chomp body)))
194 (insert-comment (format "%s ends here" source-name))
195 (insert "\n"))))
197 (defun org-babel-expand-noweb-references (&optional info parent-buffer)
198 "This function expands Noweb style references in the body of
199 the current source-code block. For example the following
200 reference would be replaced with the body of the source-code
201 block named 'example-block' (assuming the '#' character starts a
202 comment) .
204 # <<example-block>>
206 This function must be called from inside of the buffer containing
207 the source-code block which holds BODY."
208 (let* ((parent-buffer (or parent-buffer (current-buffer)))
209 (info (or info (org-babel-get-src-block-info)))
210 (lang (first info))
211 (body (second info))
212 (new-body "") index source-name)
213 (flet ((nb-add (text)
214 (setq new-body (concat new-body text))))
215 (with-temp-buffer
216 (insert body) (goto-char (point-min))
217 (funcall (intern (concat (or (and (cdr (assoc lang org-src-lang-modes))
218 (symbol-name
219 (cdr (assoc lang org-src-lang-modes))))
220 lang) "-mode")))
221 (setq index (point))
222 (while (and (re-search-forward "<<\\(.+\\)>>" nil t))
223 (save-match-data (setf source-name (match-string 1)))
224 ;; add interval to new-body
225 (goto-char (match-end 0)) (move-end-of-line nil)
226 (nb-add (buffer-substring index (point)))
227 (setq index (point))
228 ;; if found, add body of referenced source-block
229 (nb-add (save-excursion
230 (set-buffer parent-buffer)
231 (let ((point (org-babel-find-named-block source-name)))
232 (if point
233 (save-excursion
234 (goto-char point)
235 (concat "\n" (org-babel-expand-noweb-references
236 (org-babel-get-src-block-info))))
237 "")))))
238 (nb-add (buffer-substring index (point-max)))))
239 new-body))
241 (provide 'org-babel-tangle)
242 ;;; org-babel-tangle.el ends here