Make org-babel-tangle comment with ;;*.
[org-mode.git] / lisp / org-babel-tangle.el
blob6fadaa6b4848a0ae18684805b8e52dedda402c37
1 ;;; org-babel-tangle.el --- Extract source code from org-mode files
3 ;; Copyright (C) 2009 Dan Davison, Eric Schulte
5 ;; Author: Dan Davison, Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
8 ;; Version: 0.01
10 ;;; License:
12 ;; This program is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; any later version.
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;; Commentary:
29 ;; Extract the code from source blocks out into raw source-code files.
31 ;;; Code:
32 (require 'org-babel)
34 (defvar org-babel-tangle-langs nil
35 "Association list matching source-block languages. The car of
36 each element should be a string indicating the source block
37 language, and the cdr should be a list containing the extension
38 and shebang(#!) line to use when writing out the language to
39 file.")
41 (defun org-babel-load-file (file)
42 "Load the contents of the Emacs Lisp source code blocks in the
43 org-mode formatted FILE. This function will first export the
44 source code using `org-babel-tangle' and then load the resulting
45 file using `load-file'."
46 (let ((loadable-file (first (org-babel-tangle-file file "emacs-lisp"))))
47 (message "loading %s" loadable-file)
48 (unless (file-exists-p loadable-file)
49 (error "can't load file that doesn't exist"))
50 (load-file loadable-file)
51 (message "loaded %s" loadable-file)))
53 (defun org-babel-tangle-file (file &optional lang)
54 "Extract the bodies of all source code blocks in FILE with
55 `org-babel-tangle'. Optional argument LANG can be used to limit
56 the exported source code blocks by language."
57 (flet ((age (file)
58 (time-to-seconds
59 (time-subtract (current-time)
60 (sixth (file-attributes file))))))
61 (let ((target-file (concat (file-name-sans-extension file) "."
62 (second (assoc lang org-babel-tangle-langs)))))
63 (if (and lang (file-exists-p target-file) (> (age file) (age target-file)))
64 (list target-file)
65 (save-window-excursion (find-file file) (org-babel-tangle lang))))))
67 (defun org-babel-tangle (&optional lang)
68 "Extract the bodies of all source code blocks from the current
69 file into their own source-specific files. Optional argument
70 LANG can be used to limit the exported source code blocks by
71 language."
72 (interactive)
73 (save-excursion
74 (let ((base-name (file-name-sans-extension (buffer-file-name)))
75 (block-counter 0)
76 path-collector)
77 (mapc ;; for every language create a file
78 (lambda (by-lang)
79 (let* ((lang (car by-lang))
80 (lang-f (intern (concat lang "-mode")))
81 (lang-specs (cdr (assoc lang org-babel-tangle-langs)))
82 (ext (first lang-specs))
83 (she-bang (second lang-specs))
84 (by-session (cdr by-lang)))
85 (flet ((to-file (filename specs)
86 (add-to-list 'path-collector filename)
87 (with-temp-file filename
88 (funcall lang-f)
89 (when she-bang (insert (concat she-bang "\n")))
90 (comment-region
91 (point) (progn (insert "generated by org-babel-tangle") (point)))
92 (mapc #'org-babel-spec-to-string (reverse specs)))))
93 ;; if there are multiple sessions then break out by session
94 (if (> (length by-session) 1)
95 (mapc (lambda (session-pair)
96 (setq block-counter (+ block-counter (length (cdr session-pair))))
97 (to-file (format
98 "%s-%s.%s" base-name (car session-pair) ext) (cdr session-pair)))
99 by-session)
100 (setq block-counter (+ block-counter (length (cdr (car by-session)))))
101 (to-file (format "%s.%s" base-name ext) (cdr (car by-session)))))))
102 (org-babel-collect-blocks lang))
103 (message "tangled %d source-code blocks" block-counter)
104 path-collector)))
106 (defun org-babel-collect-blocks (&optional lang)
107 "Collect all source blocks in the current org-mode file.
108 Return two nested association lists, first grouped by language,
109 then by session, the contents will be source-code block
110 specifications of the form used by `org-babel-spec-to-string'.
111 Optional argument LANG can be used to limit the collected source
112 code blocks by language."
113 (let ((block-counter 0) blocks)
114 (org-babel-map-source-blocks (buffer-file-name)
115 (setq block-counter (+ 1 block-counter))
116 (let* ((link (progn (call-interactively 'org-store-link)
117 (org-babel-clean-text-properties (car (pop org-stored-links)))))
118 (source-name (intern (or (org-babel-get-src-block-name)
119 (format "block-%d" block-counter))))
120 (info (org-babel-get-src-block-info))
121 (src-lang (first info))
122 (body (second info))
123 (params (third info))
124 (spec (list link source-name params body))
125 (session (cdr (assoc :session params)))
126 by-lang by-session)
127 (unless (and lang (not (string= lang src-lang))) ;; maybe limit by language
128 ;; add the spec for this block to blocks under it's language and session
129 (setq by-lang (cdr (assoc src-lang blocks)))
130 (setq blocks (delq (assoc src-lang blocks) blocks))
131 (setq by-session (cdr (assoc session by-lang)))
132 (setq by-lang (delq (assoc session by-lang) by-lang))
133 (setq blocks (cons ;; by-language
134 (cons src-lang (cons ;; by-session
135 (cons session (cons spec by-session)) by-lang))
136 blocks)))))
137 ;; blocks should contain all source-blocks organized by language and session
138 ;; (message "blocks=%S" blocks) ;; debugging
139 blocks))
141 (defun org-babel-spec-to-string (spec)
142 "Insert the source-code specified by SPEC into the current
143 source code file. This function uses `comment-region' which
144 assumes that the appropriate major-mode is set. SPEC has the
145 form
147 (link source-name params body)"
148 (let ((link (first spec))
149 (source-name (second spec))
150 (body (fourth spec))
151 (comment-padding "* "))
152 (flet ((insert-comment (text)
153 (comment-region (point) (progn (insert text) (point)))))
154 (insert "\n\n")
155 (insert-comment (format "* [[%s][%s]]" (org-link-escape link) source-name))
156 (insert (format "\n%s\n" (org-babel-chomp body)))
157 (insert-comment (format "%s ends here" source-name))
158 (insert "\n"))))
160 (provide 'org-babel-tangle)
161 ;;; org-babel-tangle.el ends here