2010-06-25 16:20:39 +00:00
|
|
|
;;; ob-tangle.el --- extract source code from org-mode files
|
2009-06-15 01:30:29 +00:00
|
|
|
|
2010-06-25 16:20:39 +00:00
|
|
|
;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
|
2009-06-15 01:30:29 +00:00
|
|
|
|
2009-10-29 23:03:43 +00:00
|
|
|
;; Author: Eric Schulte
|
2009-06-15 01:30:29 +00:00
|
|
|
;; Keywords: literate programming, reproducible research
|
|
|
|
;; Homepage: http://orgmode.org
|
2010-07-19 06:33:24 +00:00
|
|
|
;; Version: 7.01trans
|
2009-06-15 01:30:29 +00:00
|
|
|
|
2010-06-25 16:20:39 +00:00
|
|
|
;; This file is part of GNU Emacs.
|
2009-06-15 01:30:29 +00:00
|
|
|
|
2010-06-25 16:20:39 +00:00
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
2009-06-15 01:30:29 +00:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2010-06-25 16:20:39 +00:00
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
2009-06-15 01:30:29 +00:00
|
|
|
;; 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.
|
2010-06-25 16:20:39 +00:00
|
|
|
|
2009-06-15 01:30:29 +00:00
|
|
|
;; You should have received a copy of the GNU General Public License
|
2010-06-25 16:20:39 +00:00
|
|
|
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
2009-06-15 01:30:29 +00:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; Extract the code from source blocks out into raw source-code files.
|
|
|
|
|
|
|
|
;;; Code:
|
2010-06-11 23:02:42 +00:00
|
|
|
(require 'ob)
|
2010-06-23 18:24:33 +00:00
|
|
|
(require 'org-src)
|
2010-06-13 01:32:23 +00:00
|
|
|
(eval-when-compile
|
|
|
|
(require 'cl))
|
2009-06-15 01:30:29 +00:00
|
|
|
|
2010-06-23 18:24:33 +00:00
|
|
|
(declare-function org-link-escape "org" (text &optional table))
|
2010-07-12 23:48:05 +00:00
|
|
|
(declare-function org-heading-components "org" ())
|
2010-06-23 18:24:33 +00:00
|
|
|
|
2010-06-17 16:01:24 +00:00
|
|
|
(defcustom org-babel-tangle-lang-exts
|
|
|
|
'(("emacs-lisp" . "el"))
|
|
|
|
"Alist mapping languages to their file extensions.
|
|
|
|
The key is the language name, the value is the string that should
|
|
|
|
be inserted as the extension commonly used to identify files
|
|
|
|
written in this language. If no entry is found in this list,
|
|
|
|
then the name of the language is used."
|
|
|
|
:group 'org-babel-tangle
|
|
|
|
:type '(repeat
|
|
|
|
(cons
|
|
|
|
(string "Language name")
|
|
|
|
(string "File Extension"))))
|
2010-05-27 22:32:10 +00:00
|
|
|
|
2010-07-08 18:53:51 +00:00
|
|
|
(defcustom org-babel-post-tangle-hook nil
|
|
|
|
"Hook run in code files tangled by `org-babel-tangle'."
|
|
|
|
:group 'org-babel
|
|
|
|
:type 'hook)
|
|
|
|
|
2010-07-16 22:55:40 +00:00
|
|
|
(defmacro org-babel-with-temp-filebuffer (file &rest body)
|
|
|
|
"Open FILE into a temporary buffer execute BODY there like
|
|
|
|
`progn', then kill the FILE buffer returning the result of
|
|
|
|
evaluating BODY."
|
|
|
|
(declare (indent 1))
|
|
|
|
(let ((temp-result (make-symbol "temp-result"))
|
|
|
|
(temp-file (make-symbol "temp-file")))
|
|
|
|
`(let (,temp-result ,temp-file)
|
2010-07-21 16:46:55 +00:00
|
|
|
(find-file-noselect ,file)
|
2010-07-16 22:55:40 +00:00
|
|
|
(setf ,temp-file (current-buffer))
|
|
|
|
(setf ,temp-result (progn ,@body))
|
|
|
|
(kill-buffer ,temp-file)
|
|
|
|
,temp-result)))
|
|
|
|
|
2010-06-23 18:24:33 +00:00
|
|
|
;;;###autoload
|
2009-07-15 01:29:28 +00:00
|
|
|
(defun org-babel-load-file (file)
|
2010-07-13 23:20:08 +00:00
|
|
|
"Load Emacs Lisp source code blocks in the Org-mode FILE.
|
|
|
|
This function exports the source code using
|
|
|
|
`org-babel-tangle' and then loads the resulting file using
|
|
|
|
`load-file'."
|
2009-07-15 03:16:51 +00:00
|
|
|
(flet ((age (file)
|
2010-06-13 01:50:05 +00:00
|
|
|
(float-time
|
2009-07-15 03:16:51 +00:00
|
|
|
(time-subtract (current-time)
|
2010-06-13 01:26:08 +00:00
|
|
|
(nth 5 (or (file-attributes (file-truename file))
|
2010-02-08 20:17:06 +00:00
|
|
|
(file-attributes file)))))))
|
2009-07-30 14:57:26 +00:00
|
|
|
(let* ((base-name (file-name-sans-extension file))
|
|
|
|
(exported-file (concat base-name ".el")))
|
|
|
|
;; tangle if the org-mode file is newer than the elisp file
|
2010-06-02 12:52:30 +00:00
|
|
|
(unless (and (file-exists-p exported-file)
|
|
|
|
(> (age file) (age exported-file)))
|
2010-03-18 18:30:54 +00:00
|
|
|
(org-babel-tangle-file file exported-file "emacs-lisp"))
|
2009-07-30 14:57:26 +00:00
|
|
|
(load-file exported-file)
|
|
|
|
(message "loaded %s" exported-file))))
|
|
|
|
|
2010-06-23 18:24:33 +00:00
|
|
|
;;;###autoload
|
2009-07-30 14:57:26 +00:00
|
|
|
(defun org-babel-tangle-file (file &optional target-file lang)
|
2010-07-13 23:20:08 +00:00
|
|
|
"Extract the bodies of source code blocks in FILE.
|
|
|
|
Source code blocks are extracted with `org-babel-tangle'.
|
|
|
|
Optional argument TARGET-FILE can be used to specify a default
|
|
|
|
export file for all source blocks. Optional argument LANG can be
|
|
|
|
used to limit the exported source code blocks by language."
|
2009-10-20 14:17:42 +00:00
|
|
|
(interactive "fFile to tangle: \nP")
|
2010-06-02 13:18:36 +00:00
|
|
|
(let ((visited-p (get-file-buffer (expand-file-name file)))
|
|
|
|
to-be-removed)
|
|
|
|
(save-window-excursion
|
|
|
|
(find-file file)
|
|
|
|
(setq to-be-removed (current-buffer))
|
|
|
|
(org-babel-tangle target-file lang))
|
|
|
|
(unless visited-p
|
|
|
|
(kill-buffer to-be-removed))))
|
2009-07-15 01:26:15 +00:00
|
|
|
|
2010-04-12 05:54:31 +00:00
|
|
|
(defun org-babel-tangle-publish (_ filename pub-dir)
|
|
|
|
"Tangle FILENAME and place the results in PUB-DIR."
|
|
|
|
(mapc (lambda (el) (copy-file el pub-dir t)) (org-babel-tangle-file filename)))
|
|
|
|
|
2010-06-23 18:24:33 +00:00
|
|
|
;;;###autoload
|
2009-07-30 14:57:26 +00:00
|
|
|
(defun org-babel-tangle (&optional target-file lang)
|
2010-07-13 23:20:08 +00:00
|
|
|
"Write code blocks to source-specific files.
|
|
|
|
Extract the bodies of all source code blocks from the current
|
2009-07-15 01:26:15 +00:00
|
|
|
file into their own source-specific files. Optional argument
|
2009-07-30 14:57:26 +00:00
|
|
|
TARGET-FILE can be used to specify a default export file for all
|
|
|
|
source blocks. Optional argument LANG can be used to limit the
|
|
|
|
exported source code blocks by language."
|
2009-06-15 22:35:11 +00:00
|
|
|
(interactive)
|
2009-11-09 01:25:15 +00:00
|
|
|
(save-buffer)
|
2009-06-27 18:58:13 +00:00
|
|
|
(save-excursion
|
2009-07-31 02:14:35 +00:00
|
|
|
(let ((block-counter 0)
|
2010-07-09 16:43:30 +00:00
|
|
|
(org-babel-default-header-args
|
|
|
|
(if target-file
|
|
|
|
(org-babel-merge-params org-babel-default-header-args
|
|
|
|
(list (cons :tangle target-file)))
|
|
|
|
org-babel-default-header-args))
|
2009-07-15 01:37:47 +00:00
|
|
|
path-collector)
|
2009-07-31 22:53:02 +00:00
|
|
|
(mapc ;; map over all languages
|
2009-06-27 18:58:13 +00:00
|
|
|
(lambda (by-lang)
|
|
|
|
(let* ((lang (car by-lang))
|
2009-07-30 14:57:26 +00:00
|
|
|
(specs (cdr by-lang))
|
2010-06-17 16:01:24 +00:00
|
|
|
(ext (or (cdr (assoc lang org-babel-tangle-lang-exts)) lang))
|
2010-06-02 12:52:30 +00:00
|
|
|
(lang-f (intern
|
|
|
|
(concat
|
|
|
|
(or (and (cdr (assoc lang org-src-lang-modes))
|
|
|
|
(symbol-name
|
|
|
|
(cdr (assoc lang org-src-lang-modes))))
|
|
|
|
lang)
|
|
|
|
"-mode")))
|
2009-11-04 22:18:50 +00:00
|
|
|
she-banged)
|
2009-07-30 14:57:26 +00:00
|
|
|
(mapc
|
|
|
|
(lambda (spec)
|
2010-02-06 02:04:50 +00:00
|
|
|
(flet ((get-spec (name)
|
2010-06-13 01:26:08 +00:00
|
|
|
(cdr (assoc name (nth 2 spec)))))
|
2010-02-06 02:04:50 +00:00
|
|
|
(let* ((tangle (get-spec :tangle))
|
2010-06-17 16:01:24 +00:00
|
|
|
(she-bang ((lambda (sheb) (when (> (length sheb) 0) sheb))
|
|
|
|
(get-spec :shebang)))
|
2010-07-09 16:43:30 +00:00
|
|
|
(base-name (cond
|
|
|
|
((string= "yes" tangle)
|
|
|
|
(file-name-sans-extension
|
|
|
|
(buffer-file-name)))
|
|
|
|
((string= "no" tangle) nil)
|
|
|
|
((> (length tangle) 0) tangle)))
|
2010-02-06 02:04:50 +00:00
|
|
|
(file-name (when base-name
|
2010-02-07 18:36:52 +00:00
|
|
|
;; decide if we want to add ext to base-name
|
2010-03-18 00:09:06 +00:00
|
|
|
(if (and ext (string= "yes" tangle))
|
2010-02-06 02:04:50 +00:00
|
|
|
(concat base-name "." ext) base-name))))
|
|
|
|
(when file-name
|
|
|
|
;; delete any old versions of file
|
|
|
|
(when (and (file-exists-p file-name)
|
|
|
|
(not (member file-name path-collector)))
|
|
|
|
(delete-file file-name))
|
|
|
|
;; drop source-block to file
|
|
|
|
(with-temp-buffer
|
2010-07-08 18:53:51 +00:00
|
|
|
(when (fboundp lang-f) (funcall lang-f))
|
2010-02-06 02:04:50 +00:00
|
|
|
(when (and she-bang (not (member file-name she-banged)))
|
|
|
|
(insert (concat she-bang "\n"))
|
|
|
|
(setq she-banged (cons file-name she-banged)))
|
|
|
|
(org-babel-spec-to-string spec)
|
2010-03-18 01:03:21 +00:00
|
|
|
;; We avoid append-to-file as it does not work with tramp.
|
|
|
|
(let ((content (buffer-string)))
|
|
|
|
(with-temp-buffer
|
|
|
|
(if (file-exists-p file-name)
|
|
|
|
(insert-file-contents file-name))
|
|
|
|
(goto-char (point-max))
|
|
|
|
(insert content)
|
|
|
|
(write-region nil nil file-name))))
|
2010-05-26 02:01:20 +00:00
|
|
|
;; if files contain she-bangs, then make the executable
|
|
|
|
(when she-bang (set-file-modes file-name ?\755))
|
2010-02-06 02:04:50 +00:00
|
|
|
;; update counter
|
|
|
|
(setq block-counter (+ 1 block-counter))
|
|
|
|
(add-to-list 'path-collector file-name)))))
|
2009-07-30 14:57:26 +00:00
|
|
|
specs)))
|
2009-07-23 00:30:30 +00:00
|
|
|
(org-babel-tangle-collect-blocks lang))
|
2009-09-02 19:20:20 +00:00
|
|
|
(message "tangled %d code block%s" block-counter
|
|
|
|
(if (= block-counter 1) "" "s"))
|
2010-07-08 18:53:51 +00:00
|
|
|
;; run `org-babel-post-tangle-hook' in all tangled files
|
|
|
|
(when org-babel-post-tangle-hook
|
|
|
|
(mapc
|
|
|
|
(lambda (file)
|
2010-07-16 22:55:40 +00:00
|
|
|
(org-babel-with-temp-filebuffer file
|
2010-07-08 18:53:51 +00:00
|
|
|
(run-hooks 'org-babel-post-tangle-hook)))
|
|
|
|
path-collector))
|
2009-07-15 01:37:47 +00:00
|
|
|
path-collector)))
|
2009-06-26 18:43:50 +00:00
|
|
|
|
2009-08-20 22:32:57 +00:00
|
|
|
(defun org-babel-tangle-clean ()
|
2010-07-13 23:20:08 +00:00
|
|
|
"Remove comments inserted by `org-babel-tangle'.
|
|
|
|
Call this function inside of a source-code file generated by
|
2009-08-20 22:32:57 +00:00
|
|
|
`org-babel-tangle' to remove all comments inserted automatically
|
|
|
|
by `org-babel-tangle'. Warning, this comment removes any lines
|
|
|
|
containing constructs which resemble org-mode file links or noweb
|
|
|
|
references."
|
|
|
|
(interactive)
|
|
|
|
(goto-char (point-min))
|
|
|
|
(while (or (re-search-forward "\\[\\[file:.*\\]\\[.*\\]\\]" nil t)
|
|
|
|
(re-search-forward "<<[^[:space:]]*>>" nil t))
|
Xemacs incompatibilities
Carsten Dominik <carsten.dominik@gmail.com> writes:
> On May 17, 2010, at 4:39 PM, Michael Sperber wrote:
>
>> In particular, fixing the require won't be enough: org-babel-python.el
>> uses `run-python' and interacts with the inferior Python, whereas
>> python-mode.el defines `py-shell'.
>>
>> Should I try to abstract over the differences?
>
> Yes, this would be much appreciated. (I think, Eric or Dan?)
OK, I've attached a patch that makes `org-babel-python' work on XEmacs.
Most of the issues are pure XEmacs issues. Notes:
- XEmacs doesn't have [[:digit:]] - I hope to rectify this in the
future, but it seems there's no downside in this particular case to
replacing by [0-9].
- XEmacs doesn't have `move-end-of-line', but does have `end-of-line'.
I don't understand the intent of having both of these, but the code
seems fine with `end-of-line'.
- It seems there are way too few `require's throughout org-babel. I
don't know if it's OK to add the ones I needed.
- `org-babel-python-evaluate' looked broken as-is: It doesn't use the
`body' argument properly, the result is (I think) processed in the
wrong order and not properly split into lines. I've fixed all these,
but a review is probably in order.
2010-05-24 19:22:50 +00:00
|
|
|
(delete-region (save-excursion (beginning-of-line 1) (point))
|
|
|
|
(save-excursion (end-of-line 1) (forward-char 1) (point)))))
|
2009-08-20 22:32:57 +00:00
|
|
|
|
2010-06-23 18:24:33 +00:00
|
|
|
(defvar org-stored-links)
|
2009-07-23 00:30:30 +00:00
|
|
|
(defun org-babel-tangle-collect-blocks (&optional lang)
|
2010-07-13 23:20:08 +00:00
|
|
|
"Collect source blocks in the current Org-mode file.
|
2009-07-30 14:57:26 +00:00
|
|
|
Return an association list of source-code block specifications of
|
|
|
|
the form used by `org-babel-spec-to-string' grouped by language.
|
2009-07-15 01:26:15 +00:00
|
|
|
Optional argument LANG can be used to limit the collected source
|
|
|
|
code blocks by language."
|
2010-07-12 23:48:05 +00:00
|
|
|
(let ((block-counter 1) (current-heading "") blocks)
|
2010-07-12 04:35:58 +00:00
|
|
|
(org-babel-map-src-blocks (buffer-file-name)
|
2010-07-12 23:48:05 +00:00
|
|
|
((lambda (new-heading)
|
|
|
|
(if (not (string= new-heading current-heading))
|
|
|
|
(progn
|
|
|
|
(setq block-counter 1)
|
|
|
|
(setq current-heading new-heading))
|
|
|
|
(setq block-counter (+ 1 block-counter))))
|
|
|
|
(replace-regexp-in-string "[ \t]" "-"
|
|
|
|
(nth 4 (org-heading-components))))
|
2009-06-28 00:25:01 +00:00
|
|
|
(let* ((link (progn (call-interactively 'org-store-link)
|
2010-06-02 12:52:30 +00:00
|
|
|
(org-babel-clean-text-properties
|
|
|
|
(car (pop org-stored-links)))))
|
2009-06-28 00:25:01 +00:00
|
|
|
(info (org-babel-get-src-block-info))
|
2010-06-13 01:26:08 +00:00
|
|
|
(source-name (intern (or (nth 4 info)
|
2010-07-12 23:48:05 +00:00
|
|
|
(format "%s:%d"
|
|
|
|
current-heading block-counter))))
|
2010-06-13 01:26:08 +00:00
|
|
|
(src-lang (nth 0 info))
|
2010-04-27 19:02:55 +00:00
|
|
|
(expand-cmd (intern (concat "org-babel-expand-body:" src-lang)))
|
2010-06-13 01:26:08 +00:00
|
|
|
(params (nth 2 info))
|
2009-07-30 14:57:26 +00:00
|
|
|
by-lang)
|
2010-06-02 12:52:30 +00:00
|
|
|
(unless (string= (cdr (assoc :tangle params)) "no") ;; skip
|
|
|
|
(unless (and lang (not (string= lang src-lang))) ;; limit by language
|
2009-07-30 14:57:26 +00:00
|
|
|
;; add the spec for this block to blocks under it's language
|
2009-07-23 00:30:30 +00:00
|
|
|
(setq by-lang (cdr (assoc src-lang blocks)))
|
|
|
|
(setq blocks (delq (assoc src-lang blocks) blocks))
|
2010-04-20 05:40:58 +00:00
|
|
|
(setq blocks
|
|
|
|
(cons
|
|
|
|
(cons src-lang
|
|
|
|
(cons (list link source-name params
|
2010-04-24 15:08:27 +00:00
|
|
|
((lambda (body)
|
|
|
|
(if (assoc :no-expand params)
|
|
|
|
body
|
|
|
|
(funcall
|
2010-06-02 12:52:30 +00:00
|
|
|
(if (fboundp expand-cmd)
|
|
|
|
expand-cmd
|
|
|
|
'org-babel-expand-body:generic)
|
2010-04-24 15:08:27 +00:00
|
|
|
body
|
|
|
|
params)))
|
2010-04-20 05:40:58 +00:00
|
|
|
(if (and (cdr (assoc :noweb params))
|
2010-06-02 12:52:30 +00:00
|
|
|
(string=
|
|
|
|
"yes"
|
|
|
|
(cdr (assoc :noweb params))))
|
|
|
|
(org-babel-expand-noweb-references
|
|
|
|
info)
|
2010-06-17 16:01:24 +00:00
|
|
|
(nth 1 info))))
|
2010-04-20 05:40:58 +00:00
|
|
|
by-lang)) blocks))))))
|
2009-08-03 17:00:42 +00:00
|
|
|
;; ensure blocks in the correct order
|
|
|
|
(setq blocks
|
2010-06-02 12:52:30 +00:00
|
|
|
(mapcar
|
|
|
|
(lambda (by-lang) (cons (car by-lang) (reverse (cdr by-lang))))
|
|
|
|
blocks))
|
2009-06-28 00:25:01 +00:00
|
|
|
blocks))
|
|
|
|
|
2009-06-26 03:17:21 +00:00
|
|
|
(defun org-babel-spec-to-string (spec)
|
2010-07-13 23:20:08 +00:00
|
|
|
"Insert SPEC into the current file.
|
|
|
|
Insert the source-code specified by SPEC into the current
|
2009-06-27 18:58:13 +00:00
|
|
|
source code file. This function uses `comment-region' which
|
|
|
|
assumes that the appropriate major-mode is set. SPEC has the
|
|
|
|
form
|
2009-06-26 19:12:23 +00:00
|
|
|
|
|
|
|
(link source-name params body)"
|
2010-06-15 00:50:28 +00:00
|
|
|
(let ((link (nth 0 spec))
|
|
|
|
(source-name (nth 1 spec))
|
|
|
|
(body (nth 3 spec))
|
2010-06-17 16:01:24 +00:00
|
|
|
(commentable (string= (cdr (assoc :comments (nth 2 spec))) "yes")))
|
2010-06-15 00:50:28 +00:00
|
|
|
(flet ((insert-comment (text)
|
2010-07-05 17:25:31 +00:00
|
|
|
(when commentable
|
2010-06-15 00:50:28 +00:00
|
|
|
(insert "\n")
|
|
|
|
(comment-region (point)
|
|
|
|
(progn (insert text) (point)))
|
|
|
|
(end-of-line nil)
|
|
|
|
(insert "\n"))))
|
2009-06-27 18:58:13 +00:00
|
|
|
(insert-comment (format "[[%s][%s]]" (org-link-escape link) source-name))
|
2010-06-07 18:46:11 +00:00
|
|
|
(insert (format "\n%s\n" (replace-regexp-in-string
|
|
|
|
"^," "" (org-babel-chomp body))))
|
2009-11-04 22:14:45 +00:00
|
|
|
(insert-comment (format "%s ends here" source-name)))))
|
2009-06-15 22:35:11 +00:00
|
|
|
|
2010-06-11 23:02:42 +00:00
|
|
|
(provide 'ob-tangle)
|
2010-06-25 16:20:39 +00:00
|
|
|
|
|
|
|
;; arch-tag: 413ced93-48f5-4216-86e4-3fc5df8c8f24
|
|
|
|
|
2010-06-11 23:02:42 +00:00
|
|
|
;;; ob-tangle.el ends here
|