org-babel-tangle.el 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 form the current
  34. file into their own source-specific files."
  35. (interactive)
  36. (let (blocks)
  37. ;; blocks will be two nested association lists, first grouped by
  38. ;; language, then by session, the contents of the second a-list
  39. ;; will be source-code blocks
  40. (org-babel-map-source-blocks (buffer-file-name)
  41. (let* ((link (progn (org-store-link nil) (pop org-stored-links)))
  42. (source-name (intern (org-babel-get-src-block-name)))
  43. (info (org-babel-get-src-block-info))
  44. (lang (first info))
  45. (body (second info))
  46. (params (third info))
  47. (spec (list link source-name params body))
  48. (session (cdr (assoc :session params)))
  49. by-lang by-session)
  50. ;; add the spec for this block to blocks under it's lang and session
  51. (setq by-lang (org-babel-alist-pop lang blocks))
  52. (setq by-session (org-babel-alist-pop session by-lang))
  53. (setq blocks (cons ;; by-language
  54. (cons lang (cons ;; by-session
  55. (cons session (cons spec by-session)) by-lang))
  56. blocks))))
  57. ;; blocks should contain all source-blocks organized by language
  58. ;; and session
  59. (message "block = %S" blocks)
  60. (mapc ;; for every language create a file
  61. (lambda (by-lang)
  62. (let ((lang (car by-lang))
  63. (by-session (cdr by-lang)))
  64. (if (> (length by-session) 1)
  65. )
  66. ))
  67. )
  68. ))
  69. (defun org-babel-tangle-specs-to-file (filename specs)
  70. "Take a list of source-block specifications in SPECS and write
  71. it out to FILENAME."
  72. (with-temp-file filename
  73. (insert (mapconcat #'org-babel-spec-to-string specs "\n"))))
  74. (defun org-babel-spec-to-string (spec)
  75. "Return the string version of spec suitable for inclusion in a
  76. source code file."
  77. )
  78. (provide 'org-babel-tangle)
  79. ;;; org-babel-tangle.el ends here