org-babel-tangle.el 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. ;;; org-babel-tangle.el --- Extract source code from org-mode files
  2. ;; Copyright (C) 2009 Eric Schulte
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.01
  7. ;;; License:
  8. ;; This program 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, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;; Commentary:
  23. ;; Extract the code from source blocks out into raw source-code files.
  24. ;;; Code:
  25. (require 'org-babel)
  26. (defvar org-babel-tangle-langs nil
  27. "List of languages supported by `org-babel-tangle'. The first
  28. element of each language's list is a string indicating the name
  29. of the language, the second element should be the file extension
  30. of the language, an optional third element the shebang(#!) line
  31. to use when writing out the language to file, and an optional
  32. fourth element is a flag which when true indicates that the
  33. language does not support comments.")
  34. (defun org-babel-load-file (file)
  35. "Load the contents of the Emacs Lisp source code blocks in the
  36. org-mode formatted FILE. This function will first export the
  37. source code using `org-babel-tangle' and then load the resulting
  38. file using `load-file'."
  39. (flet ((age (file)
  40. (time-to-seconds
  41. (time-subtract (current-time)
  42. (sixth (file-attributes
  43. (file-truename file)))))))
  44. (let* ((base-name (file-name-sans-extension file))
  45. (exported-file (concat base-name ".el")))
  46. ;; tangle if the org-mode file is newer than the elisp file
  47. (unless (and (file-exists-p exported-file) (> (age file) (age exported-file)))
  48. (org-babel-tangle-file file base-name "emacs-lisp"))
  49. (load-file exported-file)
  50. (message "loaded %s" exported-file))))
  51. (defun org-babel-tangle-file (file &optional target-file lang)
  52. "Extract the bodies of all source code blocks in FILE with
  53. `org-babel-tangle'. Optional argument TARGET-FILE can be used to
  54. specify a default export file for all source blocks. Optional
  55. argument LANG can be used to limit the exported source code
  56. blocks by language."
  57. (interactive "fFile to tangle: \nP")
  58. (save-window-excursion (find-file file) (org-babel-tangle target-file lang)))
  59. (defun org-babel-tangle (&optional target-file lang)
  60. "Extract the bodies of all source code blocks from the current
  61. file into their own source-specific files. Optional argument
  62. TARGET-FILE can be used to specify a default export file for all
  63. source blocks. Optional argument LANG can be used to limit the
  64. exported source code blocks by language."
  65. (interactive)
  66. (save-buffer)
  67. (save-excursion
  68. (let ((block-counter 0)
  69. path-collector)
  70. (mapc ;; map over all languages
  71. (lambda (by-lang)
  72. (let* ((lang (car by-lang))
  73. (specs (cdr by-lang))
  74. (lang-f (intern (concat
  75. (or (and (cdr (assoc lang org-src-lang-modes))
  76. (symbol-name
  77. (cdr (assoc lang org-src-lang-modes))))
  78. lang)
  79. "-mode")))
  80. (lang-specs (cdr (assoc lang org-babel-tangle-langs)))
  81. (ext (first lang-specs))
  82. (she-bang (second lang-specs))
  83. (commentable (not (third lang-specs)))
  84. she-banged)
  85. (mapc
  86. (lambda (spec)
  87. (let* ((tangle (cdr (assoc :tangle (third spec))))
  88. (base-name (or (cond
  89. ((string= "yes" tangle)
  90. (file-name-sans-extension (buffer-file-name)))
  91. ((string= "no" tangle) nil)
  92. ((> (length tangle) 0) tangle))
  93. target-file))
  94. (file-name (when base-name
  95. (if (and ext
  96. (string= base-name
  97. (file-name-sans-extension base-name)))
  98. (concat base-name "." ext) base-name))))
  99. ;; ;; debugging
  100. ;; (message "tangle=%S base-name=%S file-name=%S"
  101. ;; tangle base-name file-name)
  102. (when file-name
  103. ;; delete any old versions of file
  104. (when (and (file-exists-p file-name)
  105. (not (member file-name path-collector)))
  106. (delete-file file-name))
  107. ;; drop source-block to file
  108. (with-temp-buffer
  109. (funcall lang-f)
  110. (when (and she-bang (not (member file-name she-banged)))
  111. (insert (concat she-bang "\n"))
  112. (setq she-banged (cons file-name she-banged)))
  113. (when commentable
  114. (comment-region
  115. (point) (progn (insert "generated by org-babel-tangle") (point)))
  116. (move-end-of-line nil))
  117. (org-babel-spec-to-string spec)
  118. (append-to-file nil nil file-name))
  119. ;; update counter
  120. (setq block-counter (+ 1 block-counter))
  121. (add-to-list 'path-collector file-name))))
  122. specs)))
  123. (org-babel-tangle-collect-blocks lang))
  124. (message "tangled %d code block%s" block-counter
  125. (if (= block-counter 1) "" "s"))
  126. path-collector)))
  127. (defun org-babel-tangle-clean ()
  128. "Call this function inside of a source-code file generated by
  129. `org-babel-tangle' to remove all comments inserted automatically
  130. by `org-babel-tangle'. Warning, this comment removes any lines
  131. containing constructs which resemble org-mode file links or noweb
  132. references."
  133. (interactive)
  134. (goto-char (point-min))
  135. (while (or (re-search-forward "\\[\\[file:.*\\]\\[.*\\]\\]" nil t)
  136. (re-search-forward "<<[^[:space:]]*>>" nil t))
  137. (delete-region (save-excursion (move-beginning-of-line 1) (point))
  138. (save-excursion (move-end-of-line 1) (forward-char 1) (point)))))
  139. (defun org-babel-tangle-collect-blocks (&optional lang)
  140. "Collect all source blocks in the current org-mode file.
  141. Return an association list of source-code block specifications of
  142. the form used by `org-babel-spec-to-string' grouped by language.
  143. Optional argument LANG can be used to limit the collected source
  144. code blocks by language."
  145. (let ((block-counter 0) blocks)
  146. (org-babel-map-source-blocks (buffer-file-name)
  147. (setq block-counter (+ 1 block-counter))
  148. (let* ((link (progn (call-interactively 'org-store-link)
  149. (org-babel-clean-text-properties (car (pop org-stored-links)))))
  150. (info (org-babel-get-src-block-info))
  151. (source-name (intern (or (fifth info)
  152. (format "block-%d" block-counter))))
  153. (src-lang (first info))
  154. (body (org-babel-expand-noweb-references info))
  155. (params (third info))
  156. (spec (list link source-name params body (third (cdr (assoc src-lang org-babel-tangle-langs)))))
  157. by-lang)
  158. (unless (string= (cdr (assoc :tangle params)) "no") ;; maybe skip
  159. (unless (and lang (not (string= lang src-lang))) ;; maybe limit by language
  160. ;; add the spec for this block to blocks under it's language
  161. (setq by-lang (cdr (assoc src-lang blocks)))
  162. (setq blocks (delq (assoc src-lang blocks) blocks))
  163. (setq blocks (cons (cons src-lang (cons spec by-lang)) blocks))))))
  164. ;; ensure blocks in the correct order
  165. (setq blocks
  166. (mapcar (lambda (by-lang) (cons (car by-lang) (reverse (cdr by-lang)))) blocks))
  167. ;; blocks should contain all source-blocks organized by language
  168. ;; (message "blocks=%S" blocks) ;; debugging
  169. blocks))
  170. (defun org-babel-spec-to-string (spec)
  171. "Insert the source-code specified by SPEC into the current
  172. source code file. This function uses `comment-region' which
  173. assumes that the appropriate major-mode is set. SPEC has the
  174. form
  175. (link source-name params body)"
  176. (flet ((insert-comment (text)
  177. (when commentable
  178. (insert "\n")
  179. (comment-region (point) (progn (insert text) (point)))
  180. (move-end-of-line nil)
  181. (insert "\n"))))
  182. (let ((link (first spec))
  183. (source-name (second spec))
  184. (body (fourth spec))
  185. (commentable (not (fifth spec))))
  186. (insert-comment (format "[[%s][%s]]" (org-link-escape link) source-name))
  187. (insert (format "%s" (org-babel-chomp body)))
  188. (insert-comment (format "%s ends here" source-name)))))
  189. (provide 'org-babel-tangle)
  190. ;;; org-babel-tangle.el ends here