org-babel-tangle.el 5.5 KB

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