ob-tangle: Small refactoring
[org-mode/org-tableheadings.git] / lisp / ob-tangle.el
blob12d8092eff9120835726c0769bc137b91c39b501
1 ;;; ob-tangle.el --- extract source code from org-mode files
3 ;; Copyright (C) 2009-2014 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)
31 (declare-function org-edit-special "org" (&optional arg))
32 (declare-function org-link-escape "org" (text &optional table))
33 (declare-function org-store-link "org" (arg))
34 (declare-function org-open-link-from-string "org" (s &optional arg reference-buffer))
35 (declare-function org-heading-components "org" ())
36 (declare-function org-back-to-heading "org" (invisible-ok))
37 (declare-function org-fill-template "org" (template alist))
38 (declare-function org-babel-update-block-body "org" (new-body))
39 (declare-function org-up-heading-safe "org" ())
40 (declare-function org-in-commented-heading-p "org" (&optional no-inheritance))
41 (declare-function make-directory "files" (dir &optional parents))
42 (declare-function org-before-first-heading-p "org" ())
44 (defcustom org-babel-tangle-lang-exts
45 '(("emacs-lisp" . "el")
46 ("elisp" . "el"))
47 "Alist mapping languages to their file extensions.
48 The key is the language name, the value is the string that should
49 be inserted as the extension commonly used to identify files
50 written in this language. If no entry is found in this list,
51 then the name of the language is used."
52 :group 'org-babel-tangle
53 :version "24.1"
54 :type '(repeat
55 (cons
56 (string "Language name")
57 (string "File Extension"))))
59 (defcustom org-babel-tangle-use-relative-file-links t
60 "Use relative path names in links from tangled source back the Org-mode file."
61 :group 'org-babel-tangle
62 :type 'boolean)
64 (defcustom org-babel-post-tangle-hook nil
65 "Hook run in code files tangled by `org-babel-tangle'."
66 :group 'org-babel
67 :version "24.1"
68 :type 'hook)
70 (defcustom org-babel-pre-tangle-hook '(save-buffer)
71 "Hook run at the beginning of `org-babel-tangle'."
72 :group 'org-babel
73 :version "24.1"
74 :type 'hook)
76 (defcustom org-babel-tangle-body-hook nil
77 "Hook run over the contents of each code block body."
78 :group 'org-babel
79 :version "24.1"
80 :type 'hook)
82 (defcustom org-babel-tangle-comment-format-beg "[[%link][%source-name]]"
83 "Format of inserted comments in tangled code files.
84 The following format strings can be used to insert special
85 information into the output using `org-fill-template'.
86 %start-line --- the line number at the start of the code block
87 %file --------- the file from which the code block was tangled
88 %link --------- Org-mode style link to the code block
89 %source-name -- name of the code block
91 Upon insertion the formatted comment will be commented out, and
92 followed by a newline. To inhibit this post-insertion processing
93 set the `org-babel-tangle-uncomment-comments' variable to a
94 non-nil value.
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-tangle-comment-format-end "%source-name ends here"
103 "Format of inserted comments in tangled code files.
104 The following format strings can be used to insert special
105 information into the output using `org-fill-template'.
106 %start-line --- the line number at the start of the code block
107 %file --------- the file from which the code block was tangled
108 %link --------- Org-mode style link to the code block
109 %source-name -- name of the code block
111 Upon insertion the formatted comment will be commented out, and
112 followed by a newline. To inhibit this post-insertion processing
113 set the `org-babel-tangle-uncomment-comments' variable to a
114 non-nil value.
116 Whether or not comments are inserted during tangling is
117 controlled by the :comments header argument."
118 :group 'org-babel
119 :version "24.1"
120 :type 'string)
122 (defcustom org-babel-tangle-uncomment-comments nil
123 "Inhibits automatic commenting and addition of trailing newline
124 of tangle comments. Use `org-babel-tangle-comment-format-beg'
125 and `org-babel-tangle-comment-format-end' to customize the format
126 of tangled comments."
127 :group 'org-babel
128 :type 'boolean)
130 (defcustom org-babel-process-comment-text #'org-remove-indentation
131 "Function called to process raw Org-mode text collected to be
132 inserted as comments in tangled source-code files. The function
133 should take a single string argument and return a string
134 result. The default value is `org-remove-indentation'."
135 :group 'org-babel
136 :version "24.1"
137 :type 'function)
139 (defun org-babel-find-file-noselect-refresh (file)
140 "Find file ensuring that the latest changes on disk are
141 represented in the file."
142 (find-file-noselect file 'nowarn)
143 (with-current-buffer (get-file-buffer file)
144 (revert-buffer t t t)))
146 (defmacro org-babel-with-temp-filebuffer (file &rest body)
147 "Open FILE into a temporary buffer execute BODY there like
148 `progn', then kill the FILE buffer returning the result of
149 evaluating BODY."
150 (declare (indent 1))
151 (let ((temp-path (make-symbol "temp-path"))
152 (temp-result (make-symbol "temp-result"))
153 (temp-file (make-symbol "temp-file"))
154 (visited-p (make-symbol "visited-p")))
155 `(let* ((,temp-path ,file)
156 (,visited-p (get-file-buffer ,temp-path))
157 ,temp-result ,temp-file)
158 (org-babel-find-file-noselect-refresh ,temp-path)
159 (setf ,temp-file (get-file-buffer ,temp-path))
160 (with-current-buffer ,temp-file
161 (setf ,temp-result (progn ,@body)))
162 (unless ,visited-p (kill-buffer ,temp-file))
163 ,temp-result)))
164 (def-edebug-spec org-babel-with-temp-filebuffer (form body))
166 ;;;###autoload
167 (defun org-babel-tangle-file (file &optional target-file lang)
168 "Extract the bodies of source code blocks in FILE.
169 Source code blocks are extracted with `org-babel-tangle'.
170 Optional argument TARGET-FILE can be used to specify a default
171 export file for all source blocks. Optional argument LANG can be
172 used to limit the exported source code blocks by language.
173 Return a list whose CAR is the tangled file name."
174 (interactive "fFile to tangle: \nP")
175 (let ((visited-p (get-file-buffer (expand-file-name file)))
176 to-be-removed)
177 (prog1
178 (save-window-excursion
179 (find-file file)
180 (setq to-be-removed (current-buffer))
181 (org-babel-tangle nil target-file lang))
182 (unless visited-p
183 (kill-buffer to-be-removed)))))
185 (defun org-babel-tangle-publish (_ filename pub-dir)
186 "Tangle FILENAME and place the results in PUB-DIR."
187 (mapc (lambda (el) (copy-file el pub-dir t)) (org-babel-tangle-file filename)))
189 ;;;###autoload
190 (defun org-babel-tangle (&optional arg target-file lang)
191 "Write code blocks to source-specific files.
192 Extract the bodies of all source code blocks from the current
193 file into their own source-specific files.
194 With one universal prefix argument, only tangle the block at point.
195 When two universal prefix arguments, only tangle blocks for the
196 tangle file of the block at point.
197 Optional argument TARGET-FILE can be used to specify a default
198 export file for all source blocks. Optional argument LANG can be
199 used to limit the exported source code blocks by language."
200 (interactive "P")
201 (run-hooks 'org-babel-pre-tangle-hook)
202 ;; Possibly Restrict the buffer to the current code block
203 (save-restriction
204 (save-excursion
205 (when (equal arg '(4))
206 (let ((head (org-babel-where-is-src-block-head)))
207 (if head
208 (goto-char head)
209 (user-error "Point is not in a source code block"))))
210 (let ((block-counter 0)
211 (org-babel-default-header-args
212 (if target-file
213 (org-babel-merge-params org-babel-default-header-args
214 (list (cons :tangle target-file)))
215 org-babel-default-header-args))
216 (tangle-file
217 (when (equal arg '(16))
218 (or (cdr (assoc :tangle (nth 2 (org-babel-get-src-block-info 'light))))
219 (user-error "Point is not in a source code block"))))
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 (let ((sheb (funcall get-spec :shebang)))
239 (when (> (length sheb) 0) sheb)))
240 (tangle-mode (funcall get-spec :tangle-mode))
241 (base-name (cond
242 ((string= "yes" tangle)
243 (file-name-sans-extension
244 (buffer-file-name)))
245 ((string= "no" tangle) nil)
246 ((> (length tangle) 0) tangle)))
247 (file-name (when base-name
248 ;; decide if we want to add ext to base-name
249 (if (and ext (string= "yes" tangle))
250 (concat base-name "." ext) base-name))))
251 (when file-name
252 ;; Possibly create the parent directories for file.
253 (let ((m (funcall get-spec :mkdirp))
254 (fnd (file-name-directory file-name)))
255 (and m fnd (not (string= m "no"))
256 (make-directory fnd 'parents)))
257 ;; delete any old versions of file
258 (and (file-exists-p file-name)
259 (not (member file-name (mapcar #'car path-collector)))
260 (delete-file file-name))
261 ;; drop source-block to file
262 (with-temp-buffer
263 (when (fboundp lang-f) (ignore-errors (funcall lang-f)))
264 (when (and she-bang (not (member file-name she-banged)))
265 (insert (concat she-bang "\n"))
266 (setq she-banged (cons file-name she-banged)))
267 (org-babel-spec-to-string spec)
268 ;; We avoid append-to-file as it does not work with tramp.
269 (let ((content (buffer-string)))
270 (with-temp-buffer
271 (if (file-exists-p file-name)
272 (insert-file-contents file-name))
273 (goto-char (point-max))
274 ;; Handle :padlines unless first line in file
275 (unless (or (string= "no" (cdr (assoc :padline (nth 4 spec))))
276 (= (point) (point-min)))
277 (insert "\n"))
278 (insert content)
279 (write-region nil nil file-name))))
280 ;; if files contain she-bangs, then make the executable
281 (when she-bang
282 (unless tangle-mode (setq tangle-mode #o755)))
283 ;; update counter
284 (setq block-counter (+ 1 block-counter))
285 (add-to-list 'path-collector
286 (cons file-name tangle-mode)
288 (lambda (a b) (equal (car a) (car b))))))))
289 specs)))
290 (if (equal arg '(4))
291 (org-babel-tangle-single-block 1 t)
292 (org-babel-tangle-collect-blocks lang tangle-file)))
293 (message "Tangled %d code block%s from %s" block-counter
294 (if (= block-counter 1) "" "s")
295 (file-name-nondirectory
296 (buffer-file-name
297 (or (buffer-base-buffer) (current-buffer)))))
298 ;; run `org-babel-post-tangle-hook' in all tangled files
299 (when org-babel-post-tangle-hook
300 (mapc
301 (lambda (file)
302 (org-babel-with-temp-filebuffer file
303 (run-hooks 'org-babel-post-tangle-hook)))
304 (mapcar #'car path-collector)))
305 ;; set permissions on tangled files
306 (mapc (lambda (pair)
307 (when (cdr pair) (set-file-modes (car pair) (cdr pair))))
308 path-collector)
309 (mapcar #'car path-collector)))))
311 (defun org-babel-tangle-clean ()
312 "Remove comments inserted by `org-babel-tangle'.
313 Call this function inside of a source-code file generated by
314 `org-babel-tangle' to remove all comments inserted automatically
315 by `org-babel-tangle'. Warning, this comment removes any lines
316 containing constructs which resemble org-mode file links or noweb
317 references."
318 (interactive)
319 (goto-char (point-min))
320 (while (or (re-search-forward "\\[\\[file:.*\\]\\[.*\\]\\]" nil t)
321 (re-search-forward (org-babel-noweb-wrap) nil t))
322 (delete-region (save-excursion (beginning-of-line 1) (point))
323 (save-excursion (end-of-line 1) (forward-char 1) (point)))))
325 (defvar org-stored-links)
326 (defvar org-bracket-link-regexp)
327 (defun org-babel-spec-to-string (spec)
328 "Insert SPEC into the current file.
330 Insert the source-code specified by SPEC into the current source
331 code file. This function uses `comment-region' which assumes
332 that the appropriate major-mode is set. SPEC has the form:
334 \(start-line file link source-name params body comment)"
335 (let* ((start-line (nth 0 spec))
336 (file (if org-babel-tangle-use-relative-file-links
337 (file-relative-name (nth 1 spec))
338 (nth 1 spec)))
339 (link (let ((link (nth 2 spec)))
340 (if org-babel-tangle-use-relative-file-links
341 (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
342 (let* ((type (match-string 1 link))
343 (path (match-string 2 link))
344 (origpath path)
345 (case-fold-search nil))
346 (setq path (file-relative-name path))
347 (concat type path)))
348 link)))
349 (source-name (nth 3 spec))
350 (body (nth 5 spec))
351 (comment (nth 6 spec))
352 (comments (cdr (assoc :comments (nth 4 spec))))
353 (link-p (or (string= comments "both") (string= comments "link")
354 (string= comments "yes") (string= comments "noweb")))
355 (link-data (mapcar (lambda (el)
356 (cons (symbol-name el)
357 (let ((le (eval el)))
358 (if (stringp le) le (format "%S" le)))))
359 '(start-line file link source-name)))
360 (insert-comment (lambda (text)
361 (when (and comments (not (string= comments "no"))
362 (> (length text) 0))
363 (if org-babel-tangle-uncomment-comments
364 ;; just plain comments with no processing
365 (insert text)
366 ;; ensure comments are made to be
367 ;; comments, and add a trailing newline
368 (comment-region
369 (point) (progn (insert text) (point)))
370 (end-of-line nil)
371 (insert "\n"))))))
372 (when comment (funcall insert-comment comment))
373 (when link-p
374 (funcall
375 insert-comment
376 (org-fill-template org-babel-tangle-comment-format-beg link-data)))
377 (insert
378 (format
379 "%s\n"
380 (org-unescape-code-in-string
381 (org-babel-trim body (if org-src-preserve-indentation "[\f\n\r\v]")))))
382 (when link-p
383 (funcall
384 insert-comment
385 (org-fill-template org-babel-tangle-comment-format-end link-data)))))
387 (defun org-babel-tangle-collect-blocks (&optional language tangle-file)
388 "Collect source blocks in the current Org file.
389 Return an association list of source-code block specifications of
390 the form used by `org-babel-spec-to-string' grouped by language.
391 Optional argument LANGUAGE can be used to limit the collected
392 source code blocks by language. Optional argument TANGLE-FILE
393 can be used to limit the collected code blocks by target file."
394 (let ((counter 0) last-heading-pos blocks)
395 (org-babel-map-src-blocks (buffer-file-name)
396 (let ((current-heading-pos
397 (org-with-wide-buffer
398 (org-with-limited-levels (outline-previous-heading)))))
399 (cond ((eq last-heading-pos current-heading-pos) (incf counter))
400 ((= counter 1))
401 (t (setq counter 1))))
402 (unless (org-in-commented-heading-p)
403 (let* ((info (org-babel-get-src-block-info 'light))
404 (src-lang (nth 0 info))
405 (src-tfile (cdr (assq :tangle (nth 2 info)))))
406 (unless (or (string= (cdr (assq :tangle (nth 2 info))) "no")
407 (and tangle-file (not (equal tangle-file src-tfile)))
408 (and language (not (string= language src-lang))))
409 ;; Add the spec for this block to blocks under its
410 ;; language.
411 (let ((by-lang (assoc src-lang blocks))
412 (block (org-babel-tangle-single-block counter)))
413 (if by-lang (setcdr by-lang (cons block (cdr by-lang)))
414 (push (cons src-lang (list block)) blocks)))))))
415 ;; Ensure blocks are in the correct order.
416 (mapcar (lambda (b) (cons (car b) (nreverse (cdr b)))) blocks)))
418 (defun org-babel-tangle-single-block
419 (block-counter &optional only-this-block)
420 "Collect the tangled source for current block.
421 Return the list of block attributes needed by
422 `org-babel-tangle-collect-blocks'.
423 When ONLY-THIS-BLOCK is non-nil, return the full association
424 list to be used by `org-babel-tangle' directly."
425 (let* ((info (org-babel-get-src-block-info))
426 (start-line
427 (save-restriction (widen)
428 (+ 1 (line-number-at-pos (point)))))
429 (file (buffer-file-name))
430 (src-lang (nth 0 info))
431 (params (nth 2 info))
432 (extra (nth 3 info))
433 (cref-fmt (or (and (string-match "-l \"\\(.+\\)\"" extra)
434 (match-string 1 extra))
435 org-coderef-label-format))
436 (link (let ((link (org-no-properties
437 (org-store-link nil))))
438 (and (string-match org-bracket-link-regexp link)
439 (match-string 1 link))))
440 (source-name
441 (intern (or (nth 4 info)
442 (format "%s:%d"
443 (or (ignore-errors (nth 4 (org-heading-components)))
444 "No heading")
445 block-counter))))
446 (expand-cmd
447 (intern (concat "org-babel-expand-body:" src-lang)))
448 (assignments-cmd
449 (intern (concat "org-babel-variable-assignments:" src-lang)))
450 (body
451 ;; Run the tangle-body-hook.
452 (let* ((body ;; Expand the body in language specific manner.
453 (if (org-babel-noweb-p params :tangle)
454 (org-babel-expand-noweb-references info)
455 (nth 1 info)))
456 (body
457 (if (assoc :no-expand params)
458 body
459 (if (fboundp expand-cmd)
460 (funcall expand-cmd body params)
461 (org-babel-expand-body:generic
462 body params
463 (and (fboundp assignments-cmd)
464 (funcall assignments-cmd params)))))))
465 (with-temp-buffer
466 (insert body)
467 (when (string-match "-r" extra)
468 (goto-char (point-min))
469 (while (re-search-forward
470 (replace-regexp-in-string "%s" ".+" cref-fmt) nil t)
471 (replace-match "")))
472 (run-hooks 'org-babel-tangle-body-hook)
473 (buffer-string))))
474 (comment
475 (when (or (string= "both" (cdr (assoc :comments params)))
476 (string= "org" (cdr (assoc :comments params))))
477 ;; From the previous heading or code-block end
478 (funcall
479 org-babel-process-comment-text
480 (buffer-substring
481 (max (condition-case nil
482 (save-excursion
483 (org-back-to-heading t) ; Sets match data
484 (match-end 0))
485 (error (point-min)))
486 (save-excursion
487 (if (re-search-backward
488 org-babel-src-block-regexp nil t)
489 (match-end 0)
490 (point-min))))
491 (point)))))
492 (result
493 (list start-line file link source-name params body comment)))
494 (if only-this-block
495 (list (cons src-lang (list result)))
496 result)))
498 (defun org-babel-tangle-comment-links ( &optional info)
499 "Return a list of begin and end link comments for the code block at point."
500 (let* ((start-line (org-babel-where-is-src-block-head))
501 (file (buffer-file-name))
502 (link (org-link-escape (progn (call-interactively 'org-store-link)
503 (org-no-properties
504 (car (pop org-stored-links))))))
505 (source-name (nth 4 (or info (org-babel-get-src-block-info 'light))))
506 (link-data (mapcar (lambda (el)
507 (cons (symbol-name el)
508 (let ((le (eval el)))
509 (if (stringp le) le (format "%S" le)))))
510 '(start-line file link source-name))))
511 (list (org-fill-template org-babel-tangle-comment-format-beg link-data)
512 (org-fill-template org-babel-tangle-comment-format-end link-data))))
514 ;; de-tangling functions
515 (defvar org-bracket-link-analytic-regexp)
516 (defun org-babel-detangle (&optional source-code-file)
517 "Propagate changes in source file back original to Org-mode file.
518 This requires that code blocks were tangled with link comments
519 which enable the original code blocks to be found."
520 (interactive)
521 (save-excursion
522 (when source-code-file (find-file source-code-file))
523 (goto-char (point-min))
524 (let ((counter 0) new-body end)
525 (while (re-search-forward org-bracket-link-analytic-regexp nil t)
526 (when (re-search-forward
527 (concat " " (regexp-quote (match-string 5)) " ends here"))
528 (setq end (match-end 0))
529 (forward-line -1)
530 (save-excursion
531 (when (setq new-body (org-babel-tangle-jump-to-org))
532 (org-babel-update-block-body new-body)))
533 (setq counter (+ 1 counter)))
534 (goto-char end))
535 (prog1 counter (message "Detangled %d code blocks" counter)))))
537 (defun org-babel-tangle-jump-to-org ()
538 "Jump from a tangled code file to the related Org-mode file."
539 (interactive)
540 (let ((mid (point))
541 start body-start end done
542 target-buffer target-char link path block-name body)
543 (save-window-excursion
544 (save-excursion
545 (while (and (re-search-backward org-bracket-link-analytic-regexp nil t)
546 (not ; ever wider searches until matching block comments
547 (and (setq start (point-at-eol))
548 (setq body-start (save-excursion
549 (forward-line 2) (point-at-bol)))
550 (setq link (match-string 0))
551 (setq path (match-string 3))
552 (setq block-name (match-string 5))
553 (save-excursion
554 (save-match-data
555 (re-search-forward
556 (concat " " (regexp-quote block-name)
557 " ends here") nil t)
558 (setq end (point-at-bol))))))))
559 (unless (and start (< start mid) (< mid end))
560 (error "Not in tangled code"))
561 (setq body (org-babel-trim (buffer-substring start end))))
562 (when (string-match "::" path)
563 (setq path (substring path 0 (match-beginning 0))))
564 (find-file path) (setq target-buffer (current-buffer))
565 (goto-char start) (org-open-link-from-string link)
566 (if (string-match "[^ \t\n\r]:\\([[:digit:]]+\\)" block-name)
567 (org-babel-next-src-block
568 (string-to-number (match-string 1 block-name)))
569 (org-babel-goto-named-src-block block-name))
570 ;; position at the beginning of the code block body
571 (goto-char (org-babel-where-is-src-block-head))
572 (forward-line 1)
573 ;; Use org-edit-special to isolate the code.
574 (org-edit-special)
575 ;; Then move forward the correct number of characters in the
576 ;; code buffer.
577 (forward-char (- mid body-start))
578 ;; And return to the Org-mode buffer with the point in the right
579 ;; place.
580 (org-edit-src-exit)
581 (setq target-char (point)))
582 (org-src-switch-to-buffer target-buffer t)
583 (prog1 body (goto-char target-char))))
585 (provide 'ob-tangle)
587 ;; Local variables:
588 ;; generated-autoload-file: "org-loaddefs.el"
589 ;; End:
591 ;;; ob-tangle.el ends here