org-babel-tangle.el 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. ;;; org-babel-tangle.el --- Extract source code from org-mode files
  2. ;; Copyright (C) 2009 Dan Davison, Eric Schulte
  3. ;; Author: Dan Davison, 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. "Association list matching source-block languages. The car of
  28. each element should be a string indicating the source block
  29. language, and the cdr should be a list containing the extension
  30. and shebang(#!) line to use when writing out the language to
  31. file.")
  32. (defun org-babel-load-file (file)
  33. "Load the contents of the Emacs Lisp source code blocks in the
  34. org-mode formatted FILE. This function will first export the
  35. source code using `org-babel-tangle' and then load the resulting
  36. file using `load-file'."
  37. (flet ((age (file)
  38. (time-to-seconds
  39. (time-subtract (current-time)
  40. (sixth (file-attributes file))))))
  41. (let* ((base-name (file-name-sans-extension file))
  42. (exported-file (concat base-name ".el")))
  43. ;; tangle if the org-mode file is newer than the elisp file
  44. (unless (and (file-exists-p exported-file) (> (age file) (age exported-file)))
  45. (org-babel-tangle-file file base-name "emacs-lisp"))
  46. (load-file exported-file)
  47. (message "loaded %s" exported-file))))
  48. (defun org-babel-tangle-file (file &optional target-file lang)
  49. "Extract the bodies of all source code blocks in FILE with
  50. `org-babel-tangle'. Optional argument TARGET-FILE can be used to
  51. specify a default export file for all source blocks. Optional
  52. argument LANG can be used to limit the exported source code
  53. blocks by language."
  54. (save-window-excursion (find-file file) (org-babel-tangle target-file lang)))
  55. (defun org-babel-tangle (&optional target-file lang)
  56. "Extract the bodies of all source code blocks from the current
  57. file into their own source-specific files. Optional argument
  58. TARGET-FILE can be used to specify a default export file for all
  59. source blocks. Optional argument LANG can be used to limit the
  60. exported source code blocks by language."
  61. (interactive)
  62. (save-excursion
  63. (let ((base-name (file-name-sans-extension (buffer-file-name)))
  64. (block-counter 0)
  65. path-collector)
  66. (mapc ;; for every language create a file
  67. (lambda (by-lang)
  68. (let* ((lang (car by-lang))
  69. (specs (cdr by-lang))
  70. (lang-f (intern (concat lang "-mode")))
  71. (lang-specs (cdr (assoc lang org-babel-tangle-langs)))
  72. (ext (first lang-specs))
  73. (she-bang (second lang-specs)))
  74. (mapc
  75. (lambda (spec)
  76. (let* ((tangle (cdr (assoc :tangle params)))
  77. (base-name (or (when (not (or (string= tangle "yes")
  78. (string= tangle "no")))
  79. tangle)
  80. target-file))
  81. (file-name (when base-name
  82. (concat base-name "." ext))))
  83. (when file-name
  84. ;; delete any old versions of file
  85. (unless (member file-name path-collector)
  86. (delete-file file-name))
  87. ;; drop source-block to file
  88. (with-temp-buffer
  89. (funcall lang-f)
  90. (when she-bang (insert (concat she-bang "\n")))
  91. (comment-region
  92. (point) (progn (insert "generated by org-babel-tangle") (point)))
  93. (org-babel-spec-to-string spec)
  94. (append-to-file nil nil file-name))
  95. (add-to-list 'path-collector file-name))))
  96. specs)))
  97. (org-babel-tangle-collect-blocks lang))
  98. (message "tangled %d source-code blocks" block-counter)
  99. path-collector)))
  100. (defun org-babel-tangle-collect-blocks (&optional lang)
  101. "Collect all source blocks in the current org-mode file.
  102. Return an association list of source-code block specifications of
  103. the form used by `org-babel-spec-to-string' grouped by language.
  104. Optional argument LANG can be used to limit the collected source
  105. code blocks by language."
  106. (let ((block-counter 0) blocks)
  107. (org-babel-map-source-blocks (buffer-file-name)
  108. (setq block-counter (+ 1 block-counter))
  109. (let* ((link (progn (call-interactively 'org-store-link)
  110. (org-babel-clean-text-properties (car (pop org-stored-links)))))
  111. (source-name (intern (or (org-babel-get-src-block-name)
  112. (format "block-%d" block-counter))))
  113. (info (org-babel-get-src-block-info))
  114. (src-lang (first info))
  115. (body (second info))
  116. (params (third info))
  117. (spec (list link source-name params body))
  118. by-lang)
  119. (unless (string= (cdr (assoc :tangle params)) "no") ;; maybe skip
  120. (unless (and lang (not (string= lang src-lang))) ;; maybe limit by language
  121. ;; add the spec for this block to blocks under it's language
  122. (setq by-lang (cdr (assoc src-lang blocks)))
  123. (setq blocks (delq (assoc src-lang blocks) blocks))
  124. (setq blocks (cons (cons src-lang (cons spec by-lang)) blocks))))))
  125. ;; blocks should contain all source-blocks organized by language
  126. ;; (message "blocks=%S" blocks) ;; debugging
  127. blocks))
  128. (defun org-babel-spec-to-string (spec)
  129. "Insert the source-code specified by SPEC into the current
  130. source code file. This function uses `comment-region' which
  131. assumes that the appropriate major-mode is set. SPEC has the
  132. form
  133. (link source-name params body)"
  134. (flet ((insert-comment (text)
  135. (comment-region (point) (progn (insert text) (point)))))
  136. (let ((link (first spec))
  137. (source-name (second spec))
  138. (body (fourth spec)))
  139. (insert "\n\n")
  140. (insert-comment (format "[[%s][%s]]" (org-link-escape link) source-name))
  141. (insert (format "\n%s\n" (org-babel-chomp body)))
  142. (insert-comment (format "%s ends here" source-name))
  143. (insert "\n"))))
  144. (provide 'org-babel-tangle)
  145. ;;; org-babel-tangle.el ends here