ob-tangle.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. ;;; ob-tangle.el --- extract source code from org-mode files
  2. ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 7.01trans
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Extract the code from source blocks out into raw source-code files.
  20. ;;; Code:
  21. (require 'ob)
  22. (require 'org-src)
  23. (eval-when-compile
  24. (require 'cl))
  25. (declare-function org-link-escape "org" (text &optional table))
  26. (declare-function org-heading-components "org" ())
  27. (defcustom org-babel-tangle-lang-exts
  28. '(("emacs-lisp" . "el"))
  29. "Alist mapping languages to their file extensions.
  30. The key is the language name, the value is the string that should
  31. be inserted as the extension commonly used to identify files
  32. written in this language. If no entry is found in this list,
  33. then the name of the language is used."
  34. :group 'org-babel-tangle
  35. :type '(repeat
  36. (cons
  37. (string "Language name")
  38. (string "File Extension"))))
  39. (defcustom org-babel-post-tangle-hook nil
  40. "Hook run in code files tangled by `org-babel-tangle'."
  41. :group 'org-babel
  42. :type 'hook)
  43. (defcustom org-babel-pre-tangle-hook '(save-buffer)
  44. "Hook run at the beginning of `org-babel-tangle'."
  45. :group 'org-babel
  46. :type 'hook)
  47. (defun org-babel-find-file-noselect-refresh (file)
  48. "Find file ensuring that the latest changes on disk are
  49. represented in the file."
  50. (find-file-noselect file)
  51. (with-current-buffer (get-file-buffer file)
  52. (revert-buffer t t t)))
  53. (defmacro org-babel-with-temp-filebuffer (file &rest body)
  54. "Open FILE into a temporary buffer execute BODY there like
  55. `progn', then kill the FILE buffer returning the result of
  56. evaluating BODY."
  57. (declare (indent 1))
  58. (let ((temp-result (make-symbol "temp-result"))
  59. (temp-file (make-symbol "temp-file"))
  60. (visited-p (make-symbol "visited-p")))
  61. `(let (,temp-result ,temp-file
  62. (,visited-p (get-file-buffer ,file)))
  63. (org-babel-find-file-noselect-refresh ,file)
  64. (setf ,temp-file (get-file-buffer ,file))
  65. (with-current-buffer ,temp-file
  66. (setf ,temp-result (progn ,@body)))
  67. (unless ,visited-p (kill-buffer ,temp-file))
  68. ,temp-result)))
  69. ;;;###autoload
  70. (defun org-babel-load-file (file)
  71. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  72. This function exports the source code using
  73. `org-babel-tangle' and then loads the resulting file using
  74. `load-file'."
  75. (flet ((age (file)
  76. (float-time
  77. (time-subtract (current-time)
  78. (nth 5 (or (file-attributes (file-truename file))
  79. (file-attributes file)))))))
  80. (let* ((base-name (file-name-sans-extension file))
  81. (exported-file (concat base-name ".el")))
  82. ;; tangle if the org-mode file is newer than the elisp file
  83. (unless (and (file-exists-p exported-file)
  84. (> (age file) (age exported-file)))
  85. (org-babel-tangle-file file exported-file "emacs-lisp"))
  86. (load-file exported-file)
  87. (message "loaded %s" exported-file))))
  88. ;;;###autoload
  89. (defun org-babel-tangle-file (file &optional target-file lang)
  90. "Extract the bodies of source code blocks in FILE.
  91. Source code blocks are extracted with `org-babel-tangle'.
  92. Optional argument TARGET-FILE can be used to specify a default
  93. export file for all source blocks. Optional argument LANG can be
  94. used to limit the exported source code blocks by language."
  95. (interactive "fFile to tangle: \nP")
  96. (let ((visited-p (get-file-buffer (expand-file-name file)))
  97. to-be-removed)
  98. (save-window-excursion
  99. (find-file file)
  100. (setq to-be-removed (current-buffer))
  101. (org-babel-tangle target-file lang))
  102. (unless visited-p
  103. (kill-buffer to-be-removed))))
  104. (defun org-babel-tangle-publish (_ filename pub-dir)
  105. "Tangle FILENAME and place the results in PUB-DIR."
  106. (mapc (lambda (el) (copy-file el pub-dir t)) (org-babel-tangle-file filename)))
  107. ;;;###autoload
  108. (defun org-babel-tangle (&optional target-file lang)
  109. "Write code blocks to source-specific files.
  110. Extract the bodies of all source code blocks from the current
  111. file into their own source-specific files. Optional argument
  112. TARGET-FILE can be used to specify a default export file for all
  113. source blocks. Optional argument LANG can be used to limit the
  114. exported source code blocks by language."
  115. (interactive)
  116. (run-hooks 'org-babel-pre-tangle-hook)
  117. (save-excursion
  118. (let ((block-counter 0)
  119. (org-babel-default-header-args
  120. (if target-file
  121. (org-babel-merge-params org-babel-default-header-args
  122. (list (cons :tangle target-file)))
  123. org-babel-default-header-args))
  124. path-collector)
  125. (mapc ;; map over all languages
  126. (lambda (by-lang)
  127. (let* ((lang (car by-lang))
  128. (specs (cdr by-lang))
  129. (ext (or (cdr (assoc lang org-babel-tangle-lang-exts)) lang))
  130. (lang-f (intern
  131. (concat
  132. (or (and (cdr (assoc lang org-src-lang-modes))
  133. (symbol-name
  134. (cdr (assoc lang org-src-lang-modes))))
  135. lang)
  136. "-mode")))
  137. she-banged)
  138. (mapc
  139. (lambda (spec)
  140. (flet ((get-spec (name)
  141. (cdr (assoc name (nth 2 spec)))))
  142. (let* ((tangle (get-spec :tangle))
  143. (she-bang ((lambda (sheb) (when (> (length sheb) 0) sheb))
  144. (get-spec :shebang)))
  145. (base-name (cond
  146. ((string= "yes" tangle)
  147. (file-name-sans-extension
  148. (buffer-file-name)))
  149. ((string= "no" tangle) nil)
  150. ((> (length tangle) 0) tangle)))
  151. (file-name (when base-name
  152. ;; decide if we want to add ext to base-name
  153. (if (and ext (string= "yes" tangle))
  154. (concat base-name "." ext) base-name))))
  155. (when file-name
  156. ;; delete any old versions of file
  157. (when (and (file-exists-p file-name)
  158. (not (member file-name path-collector)))
  159. (delete-file file-name))
  160. ;; drop source-block to file
  161. (with-temp-buffer
  162. (when (fboundp lang-f) (funcall lang-f))
  163. (when (and she-bang (not (member file-name she-banged)))
  164. (insert (concat she-bang "\n"))
  165. (setq she-banged (cons file-name she-banged)))
  166. (org-babel-spec-to-string spec)
  167. ;; We avoid append-to-file as it does not work with tramp.
  168. (let ((content (buffer-string)))
  169. (with-temp-buffer
  170. (if (file-exists-p file-name)
  171. (insert-file-contents file-name))
  172. (goto-char (point-max))
  173. (insert content)
  174. (write-region nil nil file-name))))
  175. ;; if files contain she-bangs, then make the executable
  176. (when she-bang (set-file-modes file-name #o755))
  177. ;; update counter
  178. (setq block-counter (+ 1 block-counter))
  179. (add-to-list 'path-collector file-name)))))
  180. specs)))
  181. (org-babel-tangle-collect-blocks lang))
  182. (message "tangled %d code block%s" block-counter
  183. (if (= block-counter 1) "" "s"))
  184. ;; run `org-babel-post-tangle-hook' in all tangled files
  185. (when org-babel-post-tangle-hook
  186. (mapc
  187. (lambda (file)
  188. (org-babel-with-temp-filebuffer file
  189. (run-hooks 'org-babel-post-tangle-hook)))
  190. path-collector))
  191. path-collector)))
  192. (defun org-babel-tangle-clean ()
  193. "Remove comments inserted by `org-babel-tangle'.
  194. Call this function inside of a source-code file generated by
  195. `org-babel-tangle' to remove all comments inserted automatically
  196. by `org-babel-tangle'. Warning, this comment removes any lines
  197. containing constructs which resemble org-mode file links or noweb
  198. references."
  199. (interactive)
  200. (goto-char (point-min))
  201. (while (or (re-search-forward "\\[\\[file:.*\\]\\[.*\\]\\]" nil t)
  202. (re-search-forward "<<[^[:space:]]*>>" nil t))
  203. (delete-region (save-excursion (beginning-of-line 1) (point))
  204. (save-excursion (end-of-line 1) (forward-char 1) (point)))))
  205. (defvar org-stored-links)
  206. (defun org-babel-tangle-collect-blocks (&optional lang)
  207. "Collect source blocks in the current Org-mode file.
  208. Return an association list of source-code block specifications of
  209. the form used by `org-babel-spec-to-string' grouped by language.
  210. Optional argument LANG can be used to limit the collected source
  211. code blocks by language."
  212. (let ((block-counter 1) (current-heading "") blocks)
  213. (org-babel-map-src-blocks (buffer-file-name)
  214. ((lambda (new-heading)
  215. (if (not (string= new-heading current-heading))
  216. (progn
  217. (setq block-counter 1)
  218. (setq current-heading new-heading))
  219. (setq block-counter (+ 1 block-counter))))
  220. (replace-regexp-in-string "[ \t]" "-"
  221. (condition-case nil
  222. (nth 4 (org-heading-components))
  223. (error (buffer-file-name)))))
  224. (let* ((link (progn (call-interactively 'org-store-link)
  225. (org-babel-clean-text-properties
  226. (car (pop org-stored-links)))))
  227. (info (org-babel-get-src-block-info))
  228. (source-name (intern (or (nth 4 info)
  229. (format "%s:%d"
  230. current-heading block-counter))))
  231. (src-lang (nth 0 info))
  232. (expand-cmd (intern (concat "org-babel-expand-body:" src-lang)))
  233. (params (nth 2 info))
  234. by-lang)
  235. (unless (string= (cdr (assoc :tangle params)) "no") ;; skip
  236. (unless (and lang (not (string= lang src-lang))) ;; limit by language
  237. ;; add the spec for this block to blocks under it's language
  238. (setq by-lang (cdr (assoc src-lang blocks)))
  239. (setq blocks (delq (assoc src-lang blocks) blocks))
  240. (setq blocks
  241. (cons
  242. (cons src-lang
  243. (cons (list link source-name params
  244. ((lambda (body)
  245. (if (assoc :no-expand params)
  246. body
  247. (funcall
  248. (if (fboundp expand-cmd)
  249. expand-cmd
  250. 'org-babel-expand-body:generic)
  251. body
  252. params)))
  253. (if (and (cdr (assoc :noweb params))
  254. (string=
  255. "yes"
  256. (cdr (assoc :noweb params))))
  257. (org-babel-expand-noweb-references
  258. info)
  259. (nth 1 info))))
  260. by-lang)) blocks))))))
  261. ;; ensure blocks in the correct order
  262. (setq blocks
  263. (mapcar
  264. (lambda (by-lang) (cons (car by-lang) (reverse (cdr by-lang))))
  265. blocks))
  266. blocks))
  267. (defun org-babel-spec-to-string (spec)
  268. "Insert SPEC into the current file.
  269. Insert the source-code specified by SPEC into the current
  270. source code file. This function uses `comment-region' which
  271. assumes that the appropriate major-mode is set. SPEC has the
  272. form
  273. (link source-name params body)"
  274. (let ((link (nth 0 spec))
  275. (source-name (nth 1 spec))
  276. (body (nth 3 spec))
  277. (commentable (string= (cdr (assoc :comments (nth 2 spec))) "yes")))
  278. (flet ((insert-comment (text)
  279. (when commentable
  280. (insert "\n")
  281. (comment-region (point)
  282. (progn (insert text) (point)))
  283. (end-of-line nil)
  284. (insert "\n"))))
  285. (insert-comment (format "[[%s][%s]]" (org-link-escape link) source-name))
  286. (insert (format "\n%s\n" (replace-regexp-in-string
  287. "^," "" (org-babel-chomp body))))
  288. (insert-comment (format "%s ends here" source-name)))))
  289. (provide 'ob-tangle)
  290. ;; arch-tag: 413ced93-48f5-4216-86e4-3fc5df8c8f24
  291. ;;; ob-tangle.el ends here