1 ;;; ob-tangle.el --- extract source code from org-mode files
3 ;; Copyright (C) 2009-2016 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/>.
26 ;; Extract the code from source blocks out into raw source-code files.
31 (declare-function make-directory
"files" (dir &optional parents
))
32 (declare-function org-at-heading-p
"org" (&optional ignored
))
33 (declare-function org-babel-update-block-body
"org" (new-body))
34 (declare-function org-back-to-heading
"org" (invisible-ok))
35 (declare-function org-before-first-heading-p
"org" ())
36 (declare-function org-edit-special
"org" (&optional arg
))
37 (declare-function org-element-at-point
"org-element" ())
38 (declare-function org-element-type
"org-element" (element))
39 (declare-function org-fill-template
"org" (template alist
))
40 (declare-function org-heading-components
"org" ())
41 (declare-function org-in-commented-heading-p
"org" (&optional no-inheritance
))
42 (declare-function org-link-escape
"org" (text &optional table
))
43 (declare-function org-open-link-from-string
"org" (s &optional arg reference-buffer
))
44 (declare-function org-store-link
"org" (arg))
45 (declare-function org-string-nw-p
"org" (s))
46 (declare-function outline-previous-heading
"outline" ())
48 (defvar org-link-types-re
)
50 (defcustom org-babel-tangle-lang-exts
51 '(("emacs-lisp" .
"el")
53 "Alist mapping languages to their file extensions.
54 The key is the language name, the value is the string that should
55 be inserted as the extension commonly used to identify files
56 written in this language. If no entry is found in this list,
57 then the name of the language is used."
58 :group
'org-babel-tangle
62 (string "Language name")
63 (string "File Extension"))))
65 (defcustom org-babel-tangle-use-relative-file-links t
66 "Use relative path names in links from tangled source back the Org-mode file."
67 :group
'org-babel-tangle
70 (defcustom org-babel-post-tangle-hook nil
71 "Hook run in code files tangled by `org-babel-tangle'."
76 (defcustom org-babel-pre-tangle-hook
'(save-buffer)
77 "Hook run at the beginning of `org-babel-tangle'."
82 (defcustom org-babel-tangle-body-hook nil
83 "Hook run over the contents of each code block body."
88 (defcustom org-babel-tangle-comment-format-beg
"[[%link][%source-name]]"
89 "Format of inserted comments in tangled code files.
90 The following format strings can be used to insert special
91 information into the output using `org-fill-template'.
92 %start-line --- the line number at the start of the code block
93 %file --------- the file from which the code block was tangled
94 %link --------- Org-mode style link to the code block
95 %source-name -- name of the code block
97 Upon insertion the formatted comment will be commented out, and
98 followed by a newline. To inhibit this post-insertion processing
99 set the `org-babel-tangle-uncomment-comments' variable to a
102 Whether or not comments are inserted during tangling is
103 controlled by the :comments header argument."
108 (defcustom org-babel-tangle-comment-format-end
"%source-name ends here"
109 "Format of inserted comments in tangled code files.
110 The following format strings can be used to insert special
111 information into the output using `org-fill-template'.
112 %start-line --- the line number at the start of the code block
113 %file --------- the file from which the code block was tangled
114 %link --------- Org-mode style link to the code block
115 %source-name -- name of the code block
117 Upon insertion the formatted comment will be commented out, and
118 followed by a newline. To inhibit this post-insertion processing
119 set the `org-babel-tangle-uncomment-comments' variable to a
122 Whether or not comments are inserted during tangling is
123 controlled by the :comments header argument."
128 (defcustom org-babel-tangle-uncomment-comments nil
129 "Inhibits automatic commenting and addition of trailing newline
130 of tangle comments. Use `org-babel-tangle-comment-format-beg'
131 and `org-babel-tangle-comment-format-end' to customize the format
132 of tangled comments."
136 (defcustom org-babel-process-comment-text
#'org-remove-indentation
137 "Function called to process raw Org-mode text collected to be
138 inserted as comments in tangled source-code files. The function
139 should take a single string argument and return a string
140 result. The default value is `org-remove-indentation'."
145 (defun org-babel-find-file-noselect-refresh (file)
146 "Find file ensuring that the latest changes on disk are
147 represented in the file."
148 (find-file-noselect file
'nowarn
)
149 (with-current-buffer (get-file-buffer file
)
150 (revert-buffer t t t
)))
152 (defmacro org-babel-with-temp-filebuffer
(file &rest body
)
153 "Open FILE into a temporary buffer execute BODY there like
154 `progn', then kill the FILE buffer returning the result of
157 (let ((temp-path (make-symbol "temp-path"))
158 (temp-result (make-symbol "temp-result"))
159 (temp-file (make-symbol "temp-file"))
160 (visited-p (make-symbol "visited-p")))
161 `(let* ((,temp-path
,file
)
162 (,visited-p
(get-file-buffer ,temp-path
))
163 ,temp-result
,temp-file
)
164 (org-babel-find-file-noselect-refresh ,temp-path
)
165 (setf ,temp-file
(get-file-buffer ,temp-path
))
166 (with-current-buffer ,temp-file
167 (setf ,temp-result
(progn ,@body
)))
168 (unless ,visited-p
(kill-buffer ,temp-file
))
170 (def-edebug-spec org-babel-with-temp-filebuffer
(form body
))
173 (defun org-babel-tangle-file (file &optional target-file lang
)
174 "Extract the bodies of source code blocks in FILE.
175 Source code blocks are extracted with `org-babel-tangle'.
176 Optional argument TARGET-FILE can be used to specify a default
177 export file for all source blocks. Optional argument LANG can be
178 used to limit the exported source code blocks by language.
179 Return a list whose CAR is the tangled file name."
180 (interactive "fFile to tangle: \nP")
181 (let ((visited-p (get-file-buffer (expand-file-name file
)))
184 (save-window-excursion
186 (setq to-be-removed
(current-buffer))
187 (mapcar #'expand-file-name
(org-babel-tangle nil target-file lang
)))
189 (kill-buffer to-be-removed
)))))
191 (defun org-babel-tangle-publish (_ filename pub-dir
)
192 "Tangle FILENAME and place the results in PUB-DIR."
193 (unless (file-exists-p pub-dir
)
194 (make-directory pub-dir t
))
195 (mapc (lambda (el) (copy-file el pub-dir t
)) (org-babel-tangle-file filename
)))
198 (defun org-babel-tangle (&optional arg target-file lang
)
199 "Write code blocks to source-specific files.
200 Extract the bodies of all source code blocks from the current
201 file into their own source-specific files.
202 With one universal prefix argument, only tangle the block at point.
203 When two universal prefix arguments, only tangle blocks for the
204 tangle file of the block at point.
205 Optional argument TARGET-FILE can be used to specify a default
206 export file for all source blocks. Optional argument LANG can be
207 used to limit the exported source code blocks by language."
209 (run-hooks 'org-babel-pre-tangle-hook
)
210 ;; Possibly Restrict the buffer to the current code block
213 (when (equal arg
'(4))
214 (let ((head (org-babel-where-is-src-block-head)))
217 (user-error "Point is not in a source code block"))))
218 (let ((block-counter 0)
219 (org-babel-default-header-args
221 (org-babel-merge-params org-babel-default-header-args
222 (list (cons :tangle target-file
)))
223 org-babel-default-header-args
))
225 (when (equal arg
'(16))
226 (or (cdr (assoc :tangle
(nth 2 (org-babel-get-src-block-info 'light
))))
227 (user-error "Point is not in a source code block"))))
229 (mapc ;; map over all languages
231 (let* ((lang (car by-lang
))
232 (specs (cdr by-lang
))
233 (ext (or (cdr (assoc lang org-babel-tangle-lang-exts
)) lang
))
236 (or (and (cdr (assoc lang org-src-lang-modes
))
238 (cdr (assoc lang org-src-lang-modes
))))
244 (let ((get-spec (lambda (name) (cdr (assoc name
(nth 4 spec
))))))
245 (let* ((tangle (funcall get-spec
:tangle
))
246 (she-bang (let ((sheb (funcall get-spec
:shebang
)))
247 (when (> (length sheb
) 0) sheb
)))
248 (tangle-mode (funcall get-spec
:tangle-mode
))
250 ((string= "yes" tangle
)
251 (file-name-sans-extension
253 ((string= "no" tangle
) nil
)
254 ((> (length tangle
) 0) tangle
)))
255 (file-name (when base-name
256 ;; decide if we want to add ext to base-name
257 (if (and ext
(string= "yes" tangle
))
258 (concat base-name
"." ext
) base-name
))))
260 ;; Possibly create the parent directories for file.
261 (let ((m (funcall get-spec
:mkdirp
))
262 (fnd (file-name-directory file-name
)))
263 (and m fnd
(not (string= m
"no"))
264 (make-directory fnd
'parents
)))
265 ;; delete any old versions of file
266 (and (file-exists-p file-name
)
267 (not (member file-name
(mapcar #'car path-collector
)))
268 (delete-file file-name
))
269 ;; drop source-block to file
271 (when (fboundp lang-f
) (ignore-errors (funcall lang-f
)))
272 (when (and she-bang
(not (member file-name she-banged
)))
273 (insert (concat she-bang
"\n"))
274 (setq she-banged
(cons file-name she-banged
)))
275 (org-babel-spec-to-string spec
)
276 ;; We avoid append-to-file as it does not work with tramp.
277 (let ((content (buffer-string)))
279 (if (file-exists-p file-name
)
280 (insert-file-contents file-name
))
281 (goto-char (point-max))
282 ;; Handle :padlines unless first line in file
283 (unless (or (string= "no" (cdr (assoc :padline
(nth 4 spec
))))
284 (= (point) (point-min)))
287 (write-region nil nil file-name
))))
288 ;; if files contain she-bangs, then make the executable
290 (unless tangle-mode
(setq tangle-mode
#o755
)))
292 (setq block-counter
(+ 1 block-counter
))
293 (add-to-list 'path-collector
294 (cons file-name tangle-mode
)
296 (lambda (a b
) (equal (car a
) (car b
))))))))
299 (org-babel-tangle-single-block 1 t
)
300 (org-babel-tangle-collect-blocks lang tangle-file
)))
301 (message "Tangled %d code block%s from %s" block-counter
302 (if (= block-counter
1) "" "s")
303 (file-name-nondirectory
305 (or (buffer-base-buffer) (current-buffer)))))
306 ;; run `org-babel-post-tangle-hook' in all tangled files
307 (when org-babel-post-tangle-hook
310 (org-babel-with-temp-filebuffer file
311 (run-hooks 'org-babel-post-tangle-hook
)))
312 (mapcar #'car path-collector
)))
313 ;; set permissions on tangled files
315 (when (cdr pair
) (set-file-modes (car pair
) (cdr pair
))))
317 (mapcar #'car path-collector
)))))
319 (defun org-babel-tangle-clean ()
320 "Remove comments inserted by `org-babel-tangle'.
321 Call this function inside of a source-code file generated by
322 `org-babel-tangle' to remove all comments inserted automatically
323 by `org-babel-tangle'. Warning, this comment removes any lines
324 containing constructs which resemble org-mode file links or noweb
327 (goto-char (point-min))
328 (while (or (re-search-forward "\\[\\[file:.*\\]\\[.*\\]\\]" nil t
)
329 (re-search-forward (org-babel-noweb-wrap) nil t
))
330 (delete-region (save-excursion (beginning-of-line 1) (point))
331 (save-excursion (end-of-line 1) (forward-char 1) (point)))))
333 (defvar org-stored-links
)
334 (defvar org-bracket-link-regexp
)
335 (defun org-babel-spec-to-string (spec)
336 "Insert SPEC into the current file.
338 Insert the source-code specified by SPEC into the current source
339 code file. This function uses `comment-region' which assumes
340 that the appropriate major-mode is set. SPEC has the form:
342 (start-line file link source-name params body comment)"
343 (let* ((start-line (nth 0 spec
))
345 (file (if org-babel-tangle-use-relative-file-links
346 (file-relative-name (nth 1 spec
))
348 (link (let ((link (nth 2 spec
)))
349 (if org-babel-tangle-use-relative-file-links
350 (when (string-match org-link-types-re link
)
351 (let ((type (match-string 0 link
))
352 (link (substring link
(match-end 0))))
357 (file-name-directory (cdr (assq :tangle info
)))))))
359 (source-name (nth 3 spec
))
361 (comment (nth 6 spec
))
362 (comments (cdr (assq :comments info
)))
363 (link-p (or (string= comments
"both") (string= comments
"link")
364 (string= comments
"yes") (string= comments
"noweb")))
365 (link-data (mapcar (lambda (el)
366 (cons (symbol-name el
)
367 (let ((le (eval el
)))
368 (if (stringp le
) le
(format "%S" le
)))))
369 '(start-line file link source-name
)))
370 (insert-comment (lambda (text)
372 (not (string= comments
"no"))
373 (org-string-nw-p text
))
374 (if org-babel-tangle-uncomment-comments
375 ;; Plain comments: no processing.
377 ;; Ensure comments are made to be
378 ;; comments, and add a trailing
379 ;; newline. Also ignore invisible
380 ;; characters when commenting.
383 (progn (insert (org-no-properties text
))
387 (when comment
(funcall insert-comment comment
))
391 (org-fill-template org-babel-tangle-comment-format-beg link-data
)))
395 (org-unescape-code-in-string
396 (org-babel-trim body
(if org-src-preserve-indentation
"[\f\n\r\v]")))))
400 (org-fill-template org-babel-tangle-comment-format-end link-data
)))))
402 (defun org-babel-tangle-collect-blocks (&optional language tangle-file
)
403 "Collect source blocks in the current Org file.
404 Return an association list of source-code block specifications of
405 the form used by `org-babel-spec-to-string' grouped by language.
406 Optional argument LANGUAGE can be used to limit the collected
407 source code blocks by language. Optional argument TANGLE-FILE
408 can be used to limit the collected code blocks by target file."
409 (let ((counter 0) last-heading-pos blocks
)
410 (org-babel-map-src-blocks (buffer-file-name)
411 (let ((current-heading-pos
412 (org-with-wide-buffer
413 (org-with-limited-levels (outline-previous-heading)))))
414 (if (eq last-heading-pos current-heading-pos
) (incf counter
)
416 (setq last-heading-pos current-heading-pos
)))
417 (unless (org-in-commented-heading-p)
418 (let* ((info (org-babel-get-src-block-info 'light
))
419 (src-lang (nth 0 info
))
420 (src-tfile (cdr (assq :tangle
(nth 2 info
)))))
421 (unless (or (string= src-tfile
"no")
422 (and tangle-file
(not (equal tangle-file src-tfile
)))
423 (and language
(not (string= language src-lang
))))
424 ;; Add the spec for this block to blocks under its
426 (let ((by-lang (assoc src-lang blocks
))
427 (block (org-babel-tangle-single-block counter
)))
428 (if by-lang
(setcdr by-lang
(cons block
(cdr by-lang
)))
429 (push (cons src-lang
(list block
)) blocks
)))))))
430 ;; Ensure blocks are in the correct order.
431 (mapcar (lambda (b) (cons (car b
) (nreverse (cdr b
)))) blocks
)))
433 (defun org-babel-tangle-single-block
434 (block-counter &optional only-this-block
)
435 "Collect the tangled source for current block.
436 Return the list of block attributes needed by
437 `org-babel-tangle-collect-blocks'.
438 When ONLY-THIS-BLOCK is non-nil, return the full association
439 list to be used by `org-babel-tangle' directly."
440 (let* ((info (org-babel-get-src-block-info))
442 (save-restriction (widen)
443 (+ 1 (line-number-at-pos (point)))))
444 (file (buffer-file-name (buffer-base-buffer)))
445 (src-lang (nth 0 info
))
446 (params (nth 2 info
))
448 (cref-fmt (or (and (string-match "-l \"\\(.+\\)\"" extra
)
449 (match-string 1 extra
))
450 org-coderef-label-format
))
451 (link (let ((link (org-no-properties
452 (org-store-link nil
))))
453 (and (string-match org-bracket-link-regexp link
)
454 (match-string 1 link
))))
456 (intern (or (nth 4 info
)
458 (or (ignore-errors (nth 4 (org-heading-components)))
462 (intern (concat "org-babel-expand-body:" src-lang
)))
464 (intern (concat "org-babel-variable-assignments:" src-lang
)))
466 ;; Run the tangle-body-hook.
467 (let* ((body ;; Expand the body in language specific manner.
468 (if (org-babel-noweb-p params
:tangle
)
469 (org-babel-expand-noweb-references info
)
472 (if (assoc :no-expand params
)
474 (if (fboundp expand-cmd
)
475 (funcall expand-cmd body params
)
476 (org-babel-expand-body:generic
478 (and (fboundp assignments-cmd
)
479 (funcall assignments-cmd params
)))))))
482 (when (string-match "-r" extra
)
483 (goto-char (point-min))
484 (while (re-search-forward
485 (replace-regexp-in-string "%s" ".+" cref-fmt
) nil t
)
487 (run-hooks 'org-babel-tangle-body-hook
)
490 (when (or (string= "both" (cdr (assoc :comments params
)))
491 (string= "org" (cdr (assoc :comments params
))))
492 ;; From the previous heading or code-block end
494 org-babel-process-comment-text
496 (max (condition-case nil
498 (org-back-to-heading t
) ; Sets match data
502 (if (re-search-backward
503 org-babel-src-block-regexp nil t
)
508 (list start-line file link source-name params body comment
)))
510 (list (cons src-lang
(list result
)))
513 (defun org-babel-tangle-comment-links ( &optional info
)
514 "Return a list of begin and end link comments for the code block at point."
515 (let* ((start-line (org-babel-where-is-src-block-head))
516 (file (buffer-file-name))
517 (link (org-link-escape (progn (call-interactively 'org-store-link
)
519 (car (pop org-stored-links
))))))
520 (source-name (nth 4 (or info
(org-babel-get-src-block-info 'light
))))
521 (link-data (mapcar (lambda (el)
522 (cons (symbol-name el
)
523 (let ((le (eval el
)))
524 (if (stringp le
) le
(format "%S" le
)))))
525 '(start-line file link source-name
))))
526 (list (org-fill-template org-babel-tangle-comment-format-beg link-data
)
527 (org-fill-template org-babel-tangle-comment-format-end link-data
))))
529 ;; de-tangling functions
530 (defvar org-bracket-link-analytic-regexp
)
531 (defun org-babel-detangle (&optional source-code-file
)
532 "Propagate changes in source file back original to Org-mode file.
533 This requires that code blocks were tangled with link comments
534 which enable the original code blocks to be found."
537 (when source-code-file
(find-file source-code-file
))
538 (goto-char (point-min))
539 (let ((counter 0) new-body end
)
540 (while (re-search-forward org-bracket-link-analytic-regexp nil t
)
541 (when (re-search-forward
542 (concat " " (regexp-quote (match-string 5)) " ends here"))
543 (setq end
(match-end 0))
546 (when (setq new-body
(org-babel-tangle-jump-to-org))
547 (org-babel-update-block-body new-body
)))
548 (setq counter
(+ 1 counter
)))
550 (prog1 counter
(message "Detangled %d code blocks" counter
)))))
552 (defun org-babel-tangle-jump-to-org ()
553 "Jump from a tangled code file to the related Org mode file."
556 start body-start end done
557 target-buffer target-char link path block-name body
)
558 (save-window-excursion
560 (while (and (re-search-backward org-bracket-link-analytic-regexp nil t
)
561 (not ; ever wider searches until matching block comments
562 (and (setq start
(line-beginning-position))
563 (setq body-start
(line-beginning-position 2))
564 (setq link
(match-string 0))
565 (setq path
(match-string 3))
566 (setq block-name
(match-string 5))
570 (concat " " (regexp-quote block-name
)
572 (setq end
(line-beginning-position))))))))
573 (unless (and start
(< start mid
) (< mid end
))
574 (error "Not in tangled code"))
575 (setq body
(buffer-substring body-start end
)))
576 (when (string-match "::" path
)
577 (setq path
(substring path
0 (match-beginning 0))))
579 (setq target-buffer
(current-buffer))
580 ;; Go to the beginning of the relative block in Org file.
581 (org-open-link-from-string link
)
582 (if (string-match "[^ \t\n\r]:\\([[:digit:]]+\\)" block-name
)
583 (let ((n (string-to-number (match-string 1 block-name
))))
584 (if (org-before-first-heading-p) (goto-char (point-min))
585 (org-back-to-heading t
))
586 ;; Do not skip the first block if it begins at point min.
587 (cond ((or (org-at-heading-p)
588 (not (eq (org-element-type (org-element-at-point))
590 (org-babel-next-src-block n
))
592 (t (org-babel-next-src-block (1- n
)))))
593 (org-babel-goto-named-src-block block-name
))
594 (goto-char (org-babel-where-is-src-block-head))
595 ;; Preserve location of point within the source code in tangled
598 (forward-char (- mid body-start
))
599 (setq target-char
(point)))
600 (org-src-switch-to-buffer target-buffer t
)
601 (prog1 body
(goto-char target-char
))))
606 ;; generated-autoload-file: "org-loaddefs.el"
609 ;;; ob-tangle.el ends here