2009-06-15 01:30:29 +00:00
|
|
|
;;; org-babel-tangle.el --- Extract source code from org-mode files
|
|
|
|
|
|
|
|
;; Copyright (C) 2009 Dan Davison, Eric Schulte
|
|
|
|
|
|
|
|
;; Author: Dan Davison, Eric Schulte
|
|
|
|
;; Keywords: literate programming, reproducible research
|
|
|
|
;; Homepage: http://orgmode.org
|
|
|
|
;; Version: 0.01
|
|
|
|
|
|
|
|
;;; License:
|
|
|
|
|
|
|
|
;; This program is free software; you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation; either version 3, or (at your option)
|
|
|
|
;; any later version.
|
|
|
|
;;
|
|
|
|
;; This program is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
;;
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
|
|
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
;; Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; Extract the code from source blocks out into raw source-code files.
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
(require 'org-babel)
|
|
|
|
|
2009-06-15 22:25:46 +00:00
|
|
|
(defvar org-babel-tangle-langs nil
|
|
|
|
"Association list matching source-block languages. The car of
|
|
|
|
each element should be a string indicating the source block
|
|
|
|
language, and the cdr should be a list containing the extension
|
|
|
|
and shebang(#!) line to use when writing out the language to
|
|
|
|
file.")
|
|
|
|
|
2009-06-26 03:12:12 +00:00
|
|
|
(defun org-babel-tangle ()
|
2009-06-15 22:35:11 +00:00
|
|
|
"Extract the bodies of all source code blocks form the current
|
|
|
|
file into their own source-specific files."
|
|
|
|
(interactive)
|
2009-06-26 18:43:50 +00:00
|
|
|
(let ((base-name (file-name-sans-extension (buffer-file-name)))
|
|
|
|
blocks)
|
2009-06-26 03:12:12 +00:00
|
|
|
;; blocks will be two nested association lists, first grouped by
|
|
|
|
;; language, then by session, the contents of the second a-list
|
|
|
|
;; will be source-code blocks
|
2009-06-15 22:35:11 +00:00
|
|
|
(org-babel-map-source-blocks (buffer-file-name)
|
2009-06-26 03:12:12 +00:00
|
|
|
(let* ((link (progn (org-store-link nil) (pop org-stored-links)))
|
|
|
|
(source-name (intern (org-babel-get-src-block-name)))
|
|
|
|
(info (org-babel-get-src-block-info))
|
|
|
|
(lang (first info))
|
|
|
|
(body (second info))
|
|
|
|
(params (third info))
|
|
|
|
(spec (list link source-name params body))
|
|
|
|
(session (cdr (assoc :session params)))
|
|
|
|
by-lang by-session)
|
|
|
|
;; add the spec for this block to blocks under it's lang and session
|
|
|
|
(setq by-lang (org-babel-alist-pop lang blocks))
|
|
|
|
(setq by-session (org-babel-alist-pop session by-lang))
|
|
|
|
(setq blocks (cons ;; by-language
|
|
|
|
(cons lang (cons ;; by-session
|
|
|
|
(cons session (cons spec by-session)) by-lang))
|
|
|
|
blocks))))
|
|
|
|
;; blocks should contain all source-blocks organized by language
|
|
|
|
;; and session
|
2009-06-26 19:12:23 +00:00
|
|
|
;; (message "block = %S" blocks) ;; debugging
|
2009-06-26 03:17:21 +00:00
|
|
|
(mapc ;; for every language create a file
|
|
|
|
(lambda (by-lang)
|
2009-06-26 17:25:31 +00:00
|
|
|
(let* ((lang (car by-lang))
|
2009-06-26 19:12:23 +00:00
|
|
|
(lang-f (intern (concat lang "-mode")))
|
2009-06-26 17:25:31 +00:00
|
|
|
(lang-specs (cdr (assoc lang org-babel-tangle-langs)))
|
|
|
|
(ext (first lang-specs))
|
|
|
|
(she-bang (second lang-specs))
|
|
|
|
(by-session (cdr by-lang)))
|
2009-06-26 19:12:23 +00:00
|
|
|
(flet ((to-file (filename specs)
|
|
|
|
(with-temp-file filename
|
|
|
|
(funcall lang-f)
|
|
|
|
(when she-bang (insert she-bang))
|
|
|
|
(comment-region (point) (progn (insert "generated by org-babel-tangle") (point)))
|
|
|
|
(message "specs=%s" specs)
|
|
|
|
(mapc #'org-babel-spec-to-string specs))))
|
|
|
|
;; (message "lang=%s lang-specs=%s ext=%s she-bang=%s by-session=%s" lang lang-specs ext she-bang by-session) ;; debugging
|
|
|
|
;; if there are multiple sessions then break out by session
|
|
|
|
(if (> (length by-session) 1)
|
|
|
|
(mapc (lambda (session-pair)
|
|
|
|
(to-file (format "%s-%s.%s" base-name (car session-pair) ext) (cdr session-pair)))
|
|
|
|
by-session)
|
|
|
|
(message "by-session=%s" by-session)
|
|
|
|
(to-file (format "%s.%s" base-name ext) (cdr (car by-session)))))))
|
2009-06-26 18:43:50 +00:00
|
|
|
blocks)))
|
|
|
|
|
2009-06-26 03:17:21 +00:00
|
|
|
(defun org-babel-spec-to-string (spec)
|
|
|
|
"Return the string version of spec suitable for inclusion in a
|
2009-06-26 19:12:23 +00:00
|
|
|
source code file. SPEC has the form
|
|
|
|
|
|
|
|
(link source-name params body)"
|
|
|
|
(flet ((insert-comment (text)
|
|
|
|
(comment-region (point) (progn (insert text) (point)))))
|
|
|
|
(insert-comment (format "\n\nlink: %s" (first spec)))
|
|
|
|
(insert-comment (format "\nsource-name: %s" (second spec)))
|
|
|
|
(insert (format "\n%s\n" (fourth spec)))))
|
2009-06-15 22:35:11 +00:00
|
|
|
|
2009-06-15 01:30:29 +00:00
|
|
|
(provide 'org-babel-tangle)
|
|
|
|
;;; org-babel-tangle.el ends here
|