org-babel-tangle.el 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. (mapc #'load-file (org-babel-tangle-file "emacs-lisp")))
  38. (defun org-babel-tangle-file (file &optional lang)
  39. "Extract the bodies of all source code blocks in FILE with
  40. `org-babel-tangle'. Optional argument LANG can be used to limit
  41. the exported source code blocks by language."
  42. (save-window-excursion (find-file file) (org-babel-tangle lang)))
  43. (defun org-babel-tangle (&optional lang)
  44. "Extract the bodies of all source code blocks from the current
  45. file into their own source-specific files. Optional argument
  46. LANG can be used to limit the exported source code blocks by
  47. language."
  48. (interactive)
  49. (save-excursion
  50. (let ((base-name (file-name-sans-extension (buffer-file-name)))
  51. (block-counter 0)
  52. path-collector)
  53. (mapc ;; for every language create a file
  54. (lambda (by-lang)
  55. (let* ((lang (car by-lang))
  56. (lang-f (intern (concat lang "-mode")))
  57. (lang-specs (cdr (assoc lang org-babel-tangle-langs)))
  58. (ext (first lang-specs))
  59. (she-bang (second lang-specs))
  60. (by-session (cdr by-lang)))
  61. (flet ((to-file (filename specs)
  62. (add-to-list 'path-collector filename)
  63. (with-temp-file filename
  64. (funcall lang-f)
  65. (when she-bang (insert (concat she-bang "\n")))
  66. (comment-region
  67. (point) (progn (insert "generated by org-babel-tangle") (point)))
  68. (mapc #'org-babel-spec-to-string (reverse specs)))))
  69. ;; if there are multiple sessions then break out by session
  70. (if (> (length by-session) 1)
  71. (mapc (lambda (session-pair)
  72. (setq block-counter (+ block-counter (length (cdr session-pair))))
  73. (to-file (format
  74. "%s-%s.%s" base-name (car session-pair) ext) (cdr session-pair)))
  75. by-session)
  76. (setq block-counter (+ block-counter (length (cdr (car by-session)))))
  77. (to-file (format "%s.%s" base-name ext) (cdr (car by-session)))))))
  78. (org-babel-collect-blocks lang))
  79. (message "tangled %d source-code blocks" block-counter)
  80. path-collector)))
  81. (defun org-babel-collect-blocks (&optional lang)
  82. "Collect all source blocks in the current org-mode file.
  83. Return two nested association lists, first grouped by language,
  84. then by session, the contents will be source-code block
  85. specifications of the form used by `org-babel-spec-to-string'.
  86. Optional argument LANG can be used to limit the collected source
  87. code blocks by language."
  88. (let ((block-counter 0) blocks)
  89. (org-babel-map-source-blocks (buffer-file-name)
  90. (setq block-counter (+ 1 block-counter))
  91. (let* ((link (progn (call-interactively 'org-store-link)
  92. (org-babel-clean-text-properties (car (pop org-stored-links)))))
  93. (source-name (intern (or (org-babel-get-src-block-name)
  94. (format "block-%d" block-counter))))
  95. (info (org-babel-get-src-block-info))
  96. (src-lang (first info))
  97. (body (second info))
  98. (params (third info))
  99. (spec (list link source-name params body))
  100. (session (cdr (assoc :session params)))
  101. by-lang by-session)
  102. (unless (and lang (not (string= lang src-lang))) ;; maybe limit by language
  103. ;; add the spec for this block to blocks under it's language and session
  104. (setq by-lang (cdr (assoc src-lang blocks)))
  105. (setq blocks (delq (assoc src-lang blocks) blocks))
  106. (setq by-session (cdr (assoc session by-lang)))
  107. (setq by-lang (delq (assoc session by-lang) by-lang))
  108. (setq blocks (cons ;; by-language
  109. (cons src-lang (cons ;; by-session
  110. (cons session (cons spec by-session)) by-lang))
  111. blocks)))))
  112. ;; blocks should contain all source-blocks organized by language and session
  113. ;; (message "blocks=%S" blocks) ;; debugging
  114. blocks))
  115. (defun org-babel-spec-to-string (spec)
  116. "Insert the source-code specified by SPEC into the current
  117. source code file. This function uses `comment-region' which
  118. assumes that the appropriate major-mode is set. SPEC has the
  119. form
  120. (link source-name params body)"
  121. (flet ((insert-comment (text)
  122. (comment-region (point) (progn (insert text) (point)))))
  123. (let ((link (first spec))
  124. (source-name (second spec))
  125. (body (fourth spec)))
  126. (insert "\n\n")
  127. (insert-comment (format "[[%s][%s]]" (org-link-escape link) source-name))
  128. (insert (format "\n%s\n" (org-babel-chomp body)))
  129. (insert-comment (format "%s ends here" source-name))
  130. (insert "\n"))))
  131. (provide 'org-babel-tangle)
  132. ;;; org-babel-tangle.el ends here