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