Merge branch 'maint'
[org-mode.git] / lisp / ob-tangle.el
bloba835915b7c3b6412a67f3dee8eda419b2d576c85
1 ;;; ob-tangle.el --- extract source code from org-mode files
3 ;; Copyright (C) 2009-2013 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Extract the code from source blocks out into raw source-code files.
28 ;;; Code:
29 (require 'org-src)
30 (eval-when-compile
31 (require 'cl))
33 (declare-function org-link-escape "org" (text &optional table))
34 (declare-function org-heading-components "org" ())
35 (declare-function org-back-to-heading "org" (invisible-ok))
36 (declare-function org-fill-template "org" (template alist))
37 (declare-function org-babel-update-block-body "org" (new-body))
38 (declare-function make-directory "files" (dir &optional parents))
40 (defcustom org-babel-tangle-lang-exts
41 '(("emacs-lisp" . "el"))
42 "Alist mapping languages to their file extensions.
43 The key is the language name, the value is the string that should
44 be inserted as the extension commonly used to identify files
45 written in this language. If no entry is found in this list,
46 then the name of the language is used."
47 :group 'org-babel-tangle
48 :version "24.1"
49 :type '(repeat
50 (cons
51 (string "Language name")
52 (string "File Extension"))))
54 (defcustom org-babel-post-tangle-hook nil
55 "Hook run in code files tangled by `org-babel-tangle'."
56 :group 'org-babel
57 :version "24.1"
58 :type 'hook)
60 (defcustom org-babel-pre-tangle-hook '(save-buffer)
61 "Hook run at the beginning of `org-babel-tangle'."
62 :group 'org-babel
63 :version "24.1"
64 :type 'hook)
66 (defcustom org-babel-tangle-body-hook nil
67 "Hook run over the contents of each code block body."
68 :group 'org-babel
69 :version "24.1"
70 :type 'hook)
72 (defcustom org-babel-tangle-comment-format-beg "[[%link][%source-name]]"
73 "Format of inserted comments in tangled code files.
74 The following format strings can be used to insert special
75 information into the output using `org-fill-template'.
76 %start-line --- the line number at the start of the code block
77 %file --------- the file from which the code block was tangled
78 %link --------- Org-mode style link to the code block
79 %source-name -- name of the code block
81 Whether or not comments are inserted during tangling is
82 controlled by the :comments header argument."
83 :group 'org-babel
84 :version "24.1"
85 :type 'string)
87 (defcustom org-babel-tangle-comment-format-end "%source-name ends here"
88 "Format of inserted comments in tangled code files.
89 The following format strings can be used to insert special
90 information into the output using `org-fill-template'.
91 %start-line --- the line number at the start of the code block
92 %file --------- the file from which the code block was tangled
93 %link --------- Org-mode style link to the code block
94 %source-name -- name of the code block
96 Whether or not comments are inserted during tangling is
97 controlled by the :comments header argument."
98 :group 'org-babel
99 :version "24.1"
100 :type 'string)
102 (defcustom org-babel-process-comment-text #'org-babel-trim
103 "Function called to process raw Org-mode text collected to be
104 inserted as comments in tangled source-code files. The function
105 should take a single string argument and return a string
106 result. The default value is `org-babel-trim'."
107 :group 'org-babel
108 :version "24.1"
109 :type 'function)
111 (defun org-babel-find-file-noselect-refresh (file)
112 "Find file ensuring that the latest changes on disk are
113 represented in the file."
114 (find-file-noselect file)
115 (with-current-buffer (get-file-buffer file)
116 (revert-buffer t t t)))
118 (defmacro org-babel-with-temp-filebuffer (file &rest body)
119 "Open FILE into a temporary buffer execute BODY there like
120 `progn', then kill the FILE buffer returning the result of
121 evaluating BODY."
122 (declare (indent 1))
123 (let ((temp-path (make-symbol "temp-path"))
124 (temp-result (make-symbol "temp-result"))
125 (temp-file (make-symbol "temp-file"))
126 (visited-p (make-symbol "visited-p")))
127 `(let* ((,temp-path ,file)
128 (,visited-p (get-file-buffer ,temp-path))
129 ,temp-result ,temp-file)
130 (org-babel-find-file-noselect-refresh ,temp-path)
131 (setf ,temp-file (get-file-buffer ,temp-path))
132 (with-current-buffer ,temp-file
133 (setf ,temp-result (progn ,@body)))
134 (unless ,visited-p (kill-buffer ,temp-file))
135 ,temp-result)))
136 (def-edebug-spec org-babel-with-temp-filebuffer (form body))
138 ;;;###autoload
139 (defun org-babel-load-file (file &optional compile)
140 "Load Emacs Lisp source code blocks in the Org-mode FILE.
141 This function exports the source code using `org-babel-tangle'
142 and then loads the resulting file using `load-file'. With prefix
143 arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
144 file to byte-code before it is loaded."
145 (interactive "fFile to load: \nP")
146 (let* ((age (lambda (file)
147 (float-time
148 (time-subtract (current-time)
149 (nth 5 (or (file-attributes (file-truename file))
150 (file-attributes file)))))))
151 (base-name (file-name-sans-extension file))
152 (exported-file (concat base-name ".el")))
153 ;; tangle if the org-mode file is newer than the elisp file
154 (unless (and (file-exists-p exported-file)
155 (> (funcall age file) (funcall age exported-file)))
156 (org-babel-tangle-file file exported-file "emacs-lisp"))
157 (message "%s %s"
158 (if compile
159 (progn (byte-compile-file exported-file 'load)
160 "Compiled and loaded")
161 (progn (load-file exported-file) "Loaded"))
162 exported-file)))
164 ;;;###autoload
165 (defun org-babel-tangle-file (file &optional target-file lang)
166 "Extract the bodies of source code blocks in FILE.
167 Source code blocks are extracted with `org-babel-tangle'.
168 Optional argument TARGET-FILE can be used to specify a default
169 export file for all source blocks. Optional argument LANG can be
170 used to limit the exported source code blocks by language."
171 (interactive "fFile to tangle: \nP")
172 (let ((visited-p (get-file-buffer (expand-file-name file)))
173 to-be-removed)
174 (save-window-excursion
175 (find-file file)
176 (setq to-be-removed (current-buffer))
177 (org-babel-tangle nil target-file lang))
178 (unless visited-p
179 (kill-buffer to-be-removed))))
181 (defun org-babel-tangle-publish (_ filename pub-dir)
182 "Tangle FILENAME and place the results in PUB-DIR."
183 (mapc (lambda (el) (copy-file el pub-dir t)) (org-babel-tangle-file filename)))
185 ;;;###autoload
186 (defun org-babel-tangle (&optional only-this-block target-file lang)
187 "Write code blocks to source-specific files.
188 Extract the bodies of all source code blocks from the current
189 file into their own source-specific files. Optional argument
190 TARGET-FILE can be used to specify a default export file for all
191 source blocks. Optional argument LANG can be used to limit the
192 exported source code blocks by language."
193 (interactive "P")
194 (run-hooks 'org-babel-pre-tangle-hook)
195 ;; possibly restrict the buffer to the current code block
196 (save-restriction
197 (when only-this-block
198 (unless (org-babel-where-is-src-block-head)
199 (error "Point is not currently inside of a code block"))
200 (save-match-data
201 (unless (or (cdr (assoc :tangle (nth 2 (org-babel-get-src-block-info))))
202 target-file)
203 (setq target-file
204 (read-from-minibuffer "Tangle to: " (buffer-file-name)))))
205 (narrow-to-region
206 (save-match-data
207 (save-excursion
208 (goto-char (org-babel-where-is-src-block-head))
209 (while (and (forward-line -1)
210 (looking-at org-babel-multi-line-header-regexp)))
211 (point)))
212 (match-end 0)))
213 (save-excursion
214 (let ((block-counter 0)
215 (org-babel-default-header-args
216 (if target-file
217 (org-babel-merge-params org-babel-default-header-args
218 (list (cons :tangle target-file)))
219 org-babel-default-header-args))
220 path-collector)
221 (mapc ;; map over all languages
222 (lambda (by-lang)
223 (let* ((lang (car by-lang))
224 (specs (cdr by-lang))
225 (ext (or (cdr (assoc lang org-babel-tangle-lang-exts)) lang))
226 (lang-f (intern
227 (concat
228 (or (and (cdr (assoc lang org-src-lang-modes))
229 (symbol-name
230 (cdr (assoc lang org-src-lang-modes))))
231 lang)
232 "-mode")))
233 she-banged)
234 (mapc
235 (lambda (spec)
236 (let ((get-spec (lambda (name) (cdr (assoc name (nth 4 spec))))))
237 (let* ((tangle (funcall get-spec :tangle))
238 (she-bang ((lambda (sheb) (when (> (length sheb) 0) sheb))
239 (funcall get-spec :shebang)))
240 (base-name (cond
241 ((string= "yes" tangle)
242 (file-name-sans-extension
243 (buffer-file-name)))
244 ((string= "no" tangle) nil)
245 ((> (length tangle) 0) tangle)))
246 (file-name (when base-name
247 ;; decide if we want to add ext to base-name
248 (if (and ext (string= "yes" tangle))
249 (concat base-name "." ext) base-name))))
250 (when file-name
251 ;; possibly create the parent directories for file
252 (when ((lambda (m) (and m (not (string= m "no"))))
253 (funcall get-spec :mkdirp))
254 (make-directory (file-name-directory file-name) 'parents))
255 ;; delete any old versions of file
256 (when (and (file-exists-p file-name)
257 (not (member file-name path-collector)))
258 (delete-file file-name))
259 ;; drop source-block to file
260 (with-temp-buffer
261 (when (fboundp lang-f) (ignore-errors (funcall lang-f)))
262 (when (and she-bang (not (member file-name she-banged)))
263 (insert (concat she-bang "\n"))
264 (setq she-banged (cons file-name she-banged)))
265 (org-babel-spec-to-string spec)
266 ;; We avoid append-to-file as it does not work with tramp.
267 (let ((content (buffer-string)))
268 (with-temp-buffer
269 (if (file-exists-p file-name)
270 (insert-file-contents file-name))
271 (goto-char (point-max))
272 (insert content)
273 (write-region nil nil file-name))))
274 ;; if files contain she-bangs, then make the executable
275 (when she-bang (set-file-modes file-name #o755))
276 ;; update counter
277 (setq block-counter (+ 1 block-counter))
278 (add-to-list 'path-collector file-name)))))
279 specs)))
280 (org-babel-tangle-collect-blocks lang))
281 (message "Tangled %d code block%s from %s" block-counter
282 (if (= block-counter 1) "" "s")
283 (file-name-nondirectory
284 (buffer-file-name (or (buffer-base-buffer) (current-buffer)))))
285 ;; run `org-babel-post-tangle-hook' in all tangled files
286 (when org-babel-post-tangle-hook
287 (mapc
288 (lambda (file)
289 (org-babel-with-temp-filebuffer file
290 (run-hooks 'org-babel-post-tangle-hook)))
291 path-collector))
292 path-collector))))
294 (defun org-babel-tangle-clean ()
295 "Remove comments inserted by `org-babel-tangle'.
296 Call this function inside of a source-code file generated by
297 `org-babel-tangle' to remove all comments inserted automatically
298 by `org-babel-tangle'. Warning, this comment removes any lines
299 containing constructs which resemble org-mode file links or noweb
300 references."
301 (interactive)
302 (goto-char (point-min))
303 (while (or (re-search-forward "\\[\\[file:.*\\]\\[.*\\]\\]" nil t)
304 (re-search-forward (org-babel-noweb-wrap) nil t))
305 (delete-region (save-excursion (beginning-of-line 1) (point))
306 (save-excursion (end-of-line 1) (forward-char 1) (point)))))
308 (defvar org-stored-links)
309 (defvar org-bracket-link-regexp)
310 (defun org-babel-spec-to-string (spec)
311 "Insert SPEC into the current file.
312 Insert the source-code specified by SPEC into the current
313 source code file. This function uses `comment-region' which
314 assumes that the appropriate major-mode is set. SPEC has the
315 form
317 (start-line file link source-name params body comment)"
318 (let* ((start-line (nth 0 spec))
319 (file (nth 1 spec))
320 (link (nth 2 spec))
321 (source-name (nth 3 spec))
322 (body (nth 5 spec))
323 (comment (nth 6 spec))
324 (comments (cdr (assoc :comments (nth 4 spec))))
325 (padline (not (string= "no" (cdr (assoc :padline (nth 4 spec))))))
326 (link-p (or (string= comments "both") (string= comments "link")
327 (string= comments "yes") (string= comments "noweb")))
328 (link-data (mapcar (lambda (el)
329 (cons (symbol-name el)
330 ((lambda (le)
331 (if (stringp le) le (format "%S" le)))
332 (eval el))))
333 '(start-line file link source-name)))
334 (insert-comment (lambda (text)
335 (when (and comments (not (string= comments "no"))
336 (> (length text) 0))
337 (when padline (insert "\n"))
338 (comment-region (point) (progn (insert text) (point)))
339 (end-of-line nil) (insert "\n")))))
340 (when comment (funcall insert-comment comment))
341 (when link-p
342 (funcall
343 insert-comment
344 (org-fill-template org-babel-tangle-comment-format-beg link-data)))
345 (when padline (insert "\n"))
346 (insert
347 (format
348 "%s\n"
349 (replace-regexp-in-string
350 "^," ""
351 (org-babel-trim body (if org-src-preserve-indentation "[\f\n\r\v]")))))
352 (when link-p
353 (funcall
354 insert-comment
355 (org-fill-template org-babel-tangle-comment-format-end link-data)))))
357 (defvar org-comment-string) ;; Defined in org.el
358 (defun org-babel-tangle-collect-blocks (&optional language)
359 "Collect source blocks in the current Org-mode file.
360 Return an association list of source-code block specifications of
361 the form used by `org-babel-spec-to-string' grouped by language.
362 Optional argument LANG can be used to limit the collected source
363 code blocks by language."
364 (let ((block-counter 1) (current-heading "") blocks)
365 (org-babel-map-src-blocks (buffer-file-name)
366 ((lambda (new-heading)
367 (if (not (string= new-heading current-heading))
368 (progn
369 (setq block-counter 1)
370 (setq current-heading new-heading))
371 (setq block-counter (+ 1 block-counter))))
372 (replace-regexp-in-string "[ \t]" "-"
373 (condition-case nil
374 (or (nth 4 (org-heading-components))
375 "(dummy for heading without text)")
376 (error (buffer-file-name)))))
377 (let* ((start-line
378 (save-restriction (widen) (+ 1 (line-number-at-pos (point)))))
379 (file (buffer-file-name))
380 (info (org-babel-get-src-block-info 'light))
381 (src-lang (nth 0 info)))
382 (unless (or (string-match (concat "^" org-comment-string) current-heading)
383 (string= (cdr (assoc :tangle (nth 2 info))) "no"))
384 (unless (and language (not (string= language src-lang)))
385 (let* ((info (org-babel-get-src-block-info))
386 (params (nth 2 info))
387 (extra (nth 3 info))
388 (cref-fmt (or (and (string-match "-l \"\\(.+\\)\"" extra)
389 (match-string 1 extra))
390 org-coderef-label-format))
391 (link ((lambda (link)
392 (and (string-match org-bracket-link-regexp link)
393 (match-string 1 link)))
394 (org-no-properties
395 (org-store-link nil))))
396 (source-name
397 (intern (or (nth 4 info)
398 (format "%s:%d"
399 current-heading block-counter))))
400 (expand-cmd
401 (intern (concat "org-babel-expand-body:" src-lang)))
402 (assignments-cmd
403 (intern (concat "org-babel-variable-assignments:" src-lang)))
404 (body
405 ((lambda (body) ;; run the tangle-body-hook
406 (with-temp-buffer
407 (insert body)
408 (when (string-match "-r" extra)
409 (goto-char (point-min))
410 (while (re-search-forward
411 (replace-regexp-in-string "%s" ".+" cref-fmt) nil t)
412 (replace-match "")))
413 (run-hooks 'org-babel-tangle-body-hook)
414 (buffer-string)))
415 ((lambda (body) ;; expand the body in language specific manner
416 (if (assoc :no-expand params)
417 body
418 (if (fboundp expand-cmd)
419 (funcall expand-cmd body params)
420 (org-babel-expand-body:generic
421 body params
422 (and (fboundp assignments-cmd)
423 (funcall assignments-cmd params))))))
424 (if (org-babel-noweb-p params :tangle)
425 (org-babel-expand-noweb-references info)
426 (nth 1 info)))))
427 (comment
428 (when (or (string= "both" (cdr (assoc :comments params)))
429 (string= "org" (cdr (assoc :comments params))))
430 ;; from the previous heading or code-block end
431 (funcall
432 org-babel-process-comment-text
433 (buffer-substring
434 (max (condition-case nil
435 (save-excursion
436 (org-back-to-heading t) ; sets match data
437 (match-end 0))
438 (error (point-min)))
439 (save-excursion
440 (if (re-search-backward
441 org-babel-src-block-regexp nil t)
442 (match-end 0)
443 (point-min))))
444 (point)))))
445 by-lang)
446 ;; add the spec for this block to blocks under it's language
447 (setq by-lang (cdr (assoc src-lang blocks)))
448 (setq blocks (delq (assoc src-lang blocks) blocks))
449 (setq blocks (cons
450 (cons src-lang
451 (cons (list start-line file link
452 source-name params body comment)
453 by-lang)) blocks)))))))
454 ;; ensure blocks in the correct order
455 (setq blocks
456 (mapcar
457 (lambda (by-lang) (cons (car by-lang) (reverse (cdr by-lang))))
458 blocks))
459 blocks))
461 (defun org-babel-tangle-comment-links ( &optional info)
462 "Return a list of begin and end link comments for the code block at point."
463 (let* ((start-line (org-babel-where-is-src-block-head))
464 (file (buffer-file-name))
465 (link (org-link-escape (progn (call-interactively 'org-store-link)
466 (org-no-properties
467 (car (pop org-stored-links))))))
468 (source-name (nth 4 (or info (org-babel-get-src-block-info 'light))))
469 (link-data (mapcar (lambda (el)
470 (cons (symbol-name el)
471 ((lambda (le)
472 (if (stringp le) le (format "%S" le)))
473 (eval el))))
474 '(start-line file link source-name))))
475 (list (org-fill-template org-babel-tangle-comment-format-beg link-data)
476 (org-fill-template org-babel-tangle-comment-format-end link-data))))
478 ;; de-tangling functions
479 (defvar org-bracket-link-analytic-regexp)
480 (defun org-babel-detangle (&optional source-code-file)
481 "Propagate changes in source file back original to Org-mode file.
482 This requires that code blocks were tangled with link comments
483 which enable the original code blocks to be found."
484 (interactive)
485 (save-excursion
486 (when source-code-file (find-file source-code-file))
487 (goto-char (point-min))
488 (let ((counter 0) new-body end)
489 (while (re-search-forward org-bracket-link-analytic-regexp nil t)
490 (when (re-search-forward
491 (concat " " (regexp-quote (match-string 5)) " ends here"))
492 (setq end (match-end 0))
493 (forward-line -1)
494 (save-excursion
495 (when (setq new-body (org-babel-tangle-jump-to-org))
496 (org-babel-update-block-body new-body)))
497 (setq counter (+ 1 counter)))
498 (goto-char end))
499 (prog1 counter (message "Detangled %d code blocks" counter)))))
501 (defun org-babel-tangle-jump-to-org ()
502 "Jump from a tangled code file to the related Org-mode file."
503 (interactive)
504 (let ((mid (point))
505 start end done
506 target-buffer target-char link path block-name body)
507 (save-window-excursion
508 (save-excursion
509 (while (and (re-search-backward org-bracket-link-analytic-regexp nil t)
510 (not ; ever wider searches until matching block comments
511 (and (setq start (point-at-eol))
512 (setq link (match-string 0))
513 (setq path (match-string 3))
514 (setq block-name (match-string 5))
515 (save-excursion
516 (save-match-data
517 (re-search-forward
518 (concat " " (regexp-quote block-name)
519 " ends here") nil t)
520 (setq end (point-at-bol))))))))
521 (unless (and start (< start mid) (< mid end))
522 (error "Not in tangled code"))
523 (setq body (org-babel-trim (buffer-substring start end))))
524 (when (string-match "::" path)
525 (setq path (substring path 0 (match-beginning 0))))
526 (find-file path) (setq target-buffer (current-buffer))
527 (goto-char start) (org-open-link-from-string link)
528 (if (string-match "[^ \t\n\r]:\\([[:digit:]]+\\)" block-name)
529 (org-babel-next-src-block
530 (string-to-number (match-string 1 block-name)))
531 (org-babel-goto-named-src-block block-name))
532 (setq target-char (point)))
533 (pop-to-buffer target-buffer)
534 (prog1 body (goto-char target-char))))
536 (provide 'ob-tangle)
538 ;; Local variables:
539 ;; generated-autoload-file: "org-loaddefs.el"
540 ;; End:
542 ;;; ob-tangle.el ends here