2009-06-14 21:30:29 -04: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 18:25:46 -04: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-07-14 21:29:28 -04:00
|
|
|
(defun org-babel-load-file (file)
|
2009-07-14 21:37:47 -04:00
|
|
|
"Load the contents of the Emacs Lisp source code blocks in the
|
|
|
|
org-mode formatted FILE. This function will first export the
|
|
|
|
source code using `org-babel-tangle' and then load the resulting
|
|
|
|
file using `load-file'."
|
2009-07-14 23:16:51 -04:00
|
|
|
(let ((loadable-file (first (org-babel-tangle-file file "emacs-lisp"))))
|
|
|
|
(unless (file-exists-p loadable-file)
|
|
|
|
(error "can't load file that doesn't exist"))
|
|
|
|
(load-file loadable-file)
|
|
|
|
(message "loaded %s" loadable-file)))
|
2009-07-14 21:29:28 -04:00
|
|
|
|
2009-07-14 21:26:15 -04:00
|
|
|
(defun org-babel-tangle-file (file &optional lang)
|
|
|
|
"Extract the bodies of all source code blocks in FILE with
|
|
|
|
`org-babel-tangle'. Optional argument LANG can be used to limit
|
|
|
|
the exported source code blocks by language."
|
2009-07-14 23:16:51 -04:00
|
|
|
(flet ((age (file)
|
|
|
|
(time-to-seconds
|
|
|
|
(time-subtract (current-time)
|
|
|
|
(sixth (file-attributes file))))))
|
|
|
|
(let ((target-file (concat (file-name-sans-extension file) "."
|
|
|
|
(second (assoc lang org-babel-tangle-langs)))))
|
|
|
|
(if (and lang (file-exists-p target-file) (> (age file) (age target-file)))
|
|
|
|
(list target-file)
|
|
|
|
(save-window-excursion (find-file file) (org-babel-tangle lang))))))
|
2009-07-14 21:26:15 -04:00
|
|
|
|
|
|
|
(defun org-babel-tangle (&optional lang)
|
2009-07-01 19:16:30 -04:00
|
|
|
"Extract the bodies of all source code blocks from the current
|
2009-07-14 21:26:15 -04:00
|
|
|
file into their own source-specific files. Optional argument
|
|
|
|
LANG can be used to limit the exported source code blocks by
|
|
|
|
language."
|
2009-06-15 18:35:11 -04:00
|
|
|
(interactive)
|
2009-06-27 14:58:13 -04:00
|
|
|
(save-excursion
|
|
|
|
(let ((base-name (file-name-sans-extension (buffer-file-name)))
|
2009-07-14 21:37:47 -04:00
|
|
|
(block-counter 0)
|
|
|
|
path-collector)
|
2009-06-27 14:58:13 -04:00
|
|
|
(mapc ;; for every language create a file
|
|
|
|
(lambda (by-lang)
|
|
|
|
(let* ((lang (car by-lang))
|
|
|
|
(lang-f (intern (concat lang "-mode")))
|
|
|
|
(lang-specs (cdr (assoc lang org-babel-tangle-langs)))
|
|
|
|
(ext (first lang-specs))
|
|
|
|
(she-bang (second lang-specs))
|
|
|
|
(by-session (cdr by-lang)))
|
|
|
|
(flet ((to-file (filename specs)
|
2009-07-14 21:37:47 -04:00
|
|
|
(add-to-list 'path-collector filename)
|
2009-06-27 14:58:13 -04:00
|
|
|
(with-temp-file filename
|
|
|
|
(funcall lang-f)
|
2009-06-27 15:44:12 -04:00
|
|
|
(when she-bang (insert (concat she-bang "\n")))
|
2009-07-01 19:16:30 -04:00
|
|
|
(comment-region
|
|
|
|
(point) (progn (insert "generated by org-babel-tangle") (point)))
|
2009-06-27 16:13:37 -04:00
|
|
|
(mapc #'org-babel-spec-to-string (reverse specs)))))
|
2009-06-27 14:58:13 -04:00
|
|
|
;; if there are multiple sessions then break out by session
|
|
|
|
(if (> (length by-session) 1)
|
|
|
|
(mapc (lambda (session-pair)
|
2009-06-27 20:25:01 -04:00
|
|
|
(setq block-counter (+ block-counter (length (cdr session-pair))))
|
2009-07-01 19:16:30 -04:00
|
|
|
(to-file (format
|
|
|
|
"%s-%s.%s" base-name (car session-pair) ext) (cdr session-pair)))
|
2009-06-27 14:58:13 -04:00
|
|
|
by-session)
|
2009-06-27 20:25:01 -04:00
|
|
|
(setq block-counter (+ block-counter (length (cdr (car by-session)))))
|
2009-06-27 14:58:13 -04:00
|
|
|
(to-file (format "%s.%s" base-name ext) (cdr (car by-session)))))))
|
2009-07-14 21:26:15 -04:00
|
|
|
(org-babel-collect-blocks lang))
|
2009-07-14 21:37:47 -04:00
|
|
|
(message "tangled %d source-code blocks" block-counter)
|
|
|
|
path-collector)))
|
2009-06-26 14:43:50 -04:00
|
|
|
|
2009-07-14 21:26:15 -04:00
|
|
|
(defun org-babel-collect-blocks (&optional lang)
|
2009-06-27 20:25:01 -04:00
|
|
|
"Collect all source blocks in the current org-mode file.
|
|
|
|
Return two nested association lists, first grouped by language,
|
|
|
|
then by session, the contents will be source-code block
|
2009-07-14 21:26:15 -04:00
|
|
|
specifications of the form used by `org-babel-spec-to-string'.
|
|
|
|
Optional argument LANG can be used to limit the collected source
|
|
|
|
code blocks by language."
|
2009-06-27 20:25:01 -04:00
|
|
|
(let ((block-counter 0) blocks)
|
|
|
|
(org-babel-map-source-blocks (buffer-file-name)
|
|
|
|
(setq block-counter (+ 1 block-counter))
|
|
|
|
(let* ((link (progn (call-interactively 'org-store-link)
|
|
|
|
(org-babel-clean-text-properties (car (pop org-stored-links)))))
|
|
|
|
(source-name (intern (or (org-babel-get-src-block-name)
|
|
|
|
(format "block-%d" block-counter))))
|
|
|
|
(info (org-babel-get-src-block-info))
|
2009-07-14 21:26:15 -04:00
|
|
|
(src-lang (first info))
|
2009-06-27 20:25:01 -04:00
|
|
|
(body (second info))
|
|
|
|
(params (third info))
|
|
|
|
(spec (list link source-name params body))
|
|
|
|
(session (cdr (assoc :session params)))
|
|
|
|
by-lang by-session)
|
2009-07-14 21:26:15 -04:00
|
|
|
(unless (and lang (not (string= lang src-lang))) ;; maybe limit by language
|
|
|
|
;; add the spec for this block to blocks under it's language and session
|
|
|
|
(setq by-lang (cdr (assoc src-lang blocks)))
|
|
|
|
(setq blocks (delq (assoc src-lang blocks) blocks))
|
|
|
|
(setq by-session (cdr (assoc session by-lang)))
|
|
|
|
(setq by-lang (delq (assoc session by-lang) by-lang))
|
|
|
|
(setq blocks (cons ;; by-language
|
|
|
|
(cons src-lang (cons ;; by-session
|
|
|
|
(cons session (cons spec by-session)) by-lang))
|
|
|
|
blocks)))))
|
2009-06-27 20:25:01 -04:00
|
|
|
;; blocks should contain all source-blocks organized by language and session
|
|
|
|
;; (message "blocks=%S" blocks) ;; debugging
|
|
|
|
blocks))
|
|
|
|
|
2009-06-25 23:17:21 -04:00
|
|
|
(defun org-babel-spec-to-string (spec)
|
2009-06-27 14:58:13 -04:00
|
|
|
"Insert the source-code specified by SPEC into the current
|
|
|
|
source code file. This function uses `comment-region' which
|
|
|
|
assumes that the appropriate major-mode is set. SPEC has the
|
|
|
|
form
|
2009-06-26 15:12:23 -04:00
|
|
|
|
|
|
|
(link source-name params body)"
|
|
|
|
(flet ((insert-comment (text)
|
|
|
|
(comment-region (point) (progn (insert text) (point)))))
|
2009-06-27 14:58:13 -04:00
|
|
|
(let ((link (first spec))
|
|
|
|
(source-name (second spec))
|
|
|
|
(body (fourth spec)))
|
|
|
|
(insert "\n\n")
|
|
|
|
(insert-comment (format "[[%s][%s]]" (org-link-escape link) source-name))
|
|
|
|
(insert (format "\n%s\n" (org-babel-chomp body)))
|
|
|
|
(insert-comment (format "%s ends here" source-name))
|
|
|
|
(insert "\n"))))
|
2009-06-15 18:35:11 -04:00
|
|
|
|
2009-06-14 21:30:29 -04:00
|
|
|
(provide 'org-babel-tangle)
|
|
|
|
;;; org-babel-tangle.el ends here
|