Fix parser wrt to defcustom syntax related changes
[org-mode/org-tableheadings.git] / lisp / ob-tangle.el
blob1c159a577e8ac11dbea9b95472244d03cd7fd868
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-mode 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 ((block-counter 1) (current-heading "") blocks by-lang)
395 (org-babel-map-src-blocks (buffer-file-name)
396 ((lambda (new-heading)
397 (if (not (string= new-heading current-heading))
398 (progn
399 (setq block-counter 1)
400 (setq current-heading new-heading))
401 (setq block-counter (+ 1 block-counter))))
402 (replace-regexp-in-string "[ \t]" "-"
403 (condition-case nil
404 (or (nth 4 (org-heading-components))
405 "(dummy for heading without text)")
406 (error (buffer-file-name)))))
407 (let* ((info (org-babel-get-src-block-info 'light))
408 (src-lang (nth 0 info))
409 (src-tfile (cdr (assoc :tangle (nth 2 info)))))
410 (unless (or (org-in-commented-heading-p)
411 (string= (cdr (assoc :tangle (nth 2 info))) "no")
412 (and tangle-file (not (equal tangle-file src-tfile))))
413 (unless (and language (not (string= language src-lang)))
414 ;; Add the spec for this block to blocks under it's language
415 (setq by-lang (cdr (assoc src-lang blocks)))
416 (setq blocks (delq (assoc src-lang blocks) blocks))
417 (setq blocks (cons
418 (cons src-lang
419 (cons
420 (org-babel-tangle-single-block
421 block-counter)
422 by-lang)) blocks))))))
423 ;; Ensure blocks are in the correct order
424 (setq blocks
425 (mapcar
426 (lambda (by-lang) (cons (car by-lang) (reverse (cdr by-lang))))
427 blocks))
428 blocks))
430 (defun org-babel-tangle-single-block
431 (block-counter &optional only-this-block)
432 "Collect the tangled source for current block.
433 Return the list of block attributes needed by
434 `org-babel-tangle-collect-blocks'.
435 When ONLY-THIS-BLOCK is non-nil, return the full association
436 list to be used by `org-babel-tangle' directly."
437 (let* ((info (org-babel-get-src-block-info))
438 (start-line
439 (save-restriction (widen)
440 (+ 1 (line-number-at-pos (point)))))
441 (file (buffer-file-name))
442 (src-lang (nth 0 info))
443 (params (nth 2 info))
444 (extra (nth 3 info))
445 (cref-fmt (or (and (string-match "-l \"\\(.+\\)\"" extra)
446 (match-string 1 extra))
447 org-coderef-label-format))
448 (link (let ((link (org-no-properties
449 (org-store-link nil))))
450 (and (string-match org-bracket-link-regexp link)
451 (match-string 1 link))))
452 (source-name
453 (intern (or (nth 4 info)
454 (format "%s:%d"
455 (or (ignore-errors (nth 4 (org-heading-components)))
456 "No heading")
457 block-counter))))
458 (expand-cmd
459 (intern (concat "org-babel-expand-body:" src-lang)))
460 (assignments-cmd
461 (intern (concat "org-babel-variable-assignments:" src-lang)))
462 (body
463 ;; Run the tangle-body-hook.
464 (let* ((body ;; Expand the body in language specific manner.
465 (if (org-babel-noweb-p params :tangle)
466 (org-babel-expand-noweb-references info)
467 (nth 1 info)))
468 (body
469 (if (assoc :no-expand params)
470 body
471 (if (fboundp expand-cmd)
472 (funcall expand-cmd body params)
473 (org-babel-expand-body:generic
474 body params
475 (and (fboundp assignments-cmd)
476 (funcall assignments-cmd params)))))))
477 (with-temp-buffer
478 (insert body)
479 (when (string-match "-r" extra)
480 (goto-char (point-min))
481 (while (re-search-forward
482 (replace-regexp-in-string "%s" ".+" cref-fmt) nil t)
483 (replace-match "")))
484 (run-hooks 'org-babel-tangle-body-hook)
485 (buffer-string))))
486 (comment
487 (when (or (string= "both" (cdr (assoc :comments params)))
488 (string= "org" (cdr (assoc :comments params))))
489 ;; From the previous heading or code-block end
490 (funcall
491 org-babel-process-comment-text
492 (buffer-substring
493 (max (condition-case nil
494 (save-excursion
495 (org-back-to-heading t) ; Sets match data
496 (match-end 0))
497 (error (point-min)))
498 (save-excursion
499 (if (re-search-backward
500 org-babel-src-block-regexp nil t)
501 (match-end 0)
502 (point-min))))
503 (point)))))
504 (result
505 (list start-line file link source-name params body comment)))
506 (if only-this-block
507 (list (cons src-lang (list result)))
508 result)))
510 (defun org-babel-tangle-comment-links ( &optional info)
511 "Return a list of begin and end link comments for the code block at point."
512 (let* ((start-line (org-babel-where-is-src-block-head))
513 (file (buffer-file-name))
514 (link (org-link-escape (progn (call-interactively 'org-store-link)
515 (org-no-properties
516 (car (pop org-stored-links))))))
517 (source-name (nth 4 (or info (org-babel-get-src-block-info 'light))))
518 (link-data (mapcar (lambda (el)
519 (cons (symbol-name el)
520 (let ((le (eval el)))
521 (if (stringp le) le (format "%S" le)))))
522 '(start-line file link source-name))))
523 (list (org-fill-template org-babel-tangle-comment-format-beg link-data)
524 (org-fill-template org-babel-tangle-comment-format-end link-data))))
526 ;; de-tangling functions
527 (defvar org-bracket-link-analytic-regexp)
528 (defun org-babel-detangle (&optional source-code-file)
529 "Propagate changes in source file back original to Org-mode file.
530 This requires that code blocks were tangled with link comments
531 which enable the original code blocks to be found."
532 (interactive)
533 (save-excursion
534 (when source-code-file (find-file source-code-file))
535 (goto-char (point-min))
536 (let ((counter 0) new-body end)
537 (while (re-search-forward org-bracket-link-analytic-regexp nil t)
538 (when (re-search-forward
539 (concat " " (regexp-quote (match-string 5)) " ends here"))
540 (setq end (match-end 0))
541 (forward-line -1)
542 (save-excursion
543 (when (setq new-body (org-babel-tangle-jump-to-org))
544 (org-babel-update-block-body new-body)))
545 (setq counter (+ 1 counter)))
546 (goto-char end))
547 (prog1 counter (message "Detangled %d code blocks" counter)))))
549 (defun org-babel-tangle-jump-to-org ()
550 "Jump from a tangled code file to the related Org-mode file."
551 (interactive)
552 (let ((mid (point))
553 start body-start end done
554 target-buffer target-char link path block-name body)
555 (save-window-excursion
556 (save-excursion
557 (while (and (re-search-backward org-bracket-link-analytic-regexp nil t)
558 (not ; ever wider searches until matching block comments
559 (and (setq start (point-at-eol))
560 (setq body-start (save-excursion
561 (forward-line 2) (point-at-bol)))
562 (setq link (match-string 0))
563 (setq path (match-string 3))
564 (setq block-name (match-string 5))
565 (save-excursion
566 (save-match-data
567 (re-search-forward
568 (concat " " (regexp-quote block-name)
569 " ends here") nil t)
570 (setq end (point-at-bol))))))))
571 (unless (and start (< start mid) (< mid end))
572 (error "Not in tangled code"))
573 (setq body (org-babel-trim (buffer-substring start end))))
574 (when (string-match "::" path)
575 (setq path (substring path 0 (match-beginning 0))))
576 (find-file path) (setq target-buffer (current-buffer))
577 (goto-char start) (org-open-link-from-string link)
578 (if (string-match "[^ \t\n\r]:\\([[:digit:]]+\\)" block-name)
579 (org-babel-next-src-block
580 (string-to-number (match-string 1 block-name)))
581 (org-babel-goto-named-src-block block-name))
582 ;; position at the beginning of the code block body
583 (goto-char (org-babel-where-is-src-block-head))
584 (forward-line 1)
585 ;; Use org-edit-special to isolate the code.
586 (org-edit-special)
587 ;; Then move forward the correct number of characters in the
588 ;; code buffer.
589 (forward-char (- mid body-start))
590 ;; And return to the Org-mode buffer with the point in the right
591 ;; place.
592 (org-edit-src-exit)
593 (setq target-char (point)))
594 (org-src-switch-to-buffer target-buffer t)
595 (prog1 body (goto-char target-char))))
597 (provide 'ob-tangle)
599 ;; Local variables:
600 ;; generated-autoload-file: "org-loaddefs.el"
601 ;; End:
603 ;;; ob-tangle.el ends here