Merge changes from emacs-23 branch
[bpt/emacs.git] / lisp / org / ob-lob.el
CommitLineData
86fbb8ca
CD
1;;; ob-lob.el --- functions supporting the Library of Babel
2
3;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
4
acedf35c 5;; Author: Eric Schulte, Dan Davison
86fbb8ca
CD
6;; Keywords: literate programming, reproducible research
7;; Homepage: http://orgmode.org
acedf35c 8;; Version: 7.4
86fbb8ca
CD
9
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs is free software: you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25;;; Commentary:
26
27;; See the online documentation for more information
acedf35c 28;;
86fbb8ca
CD
29;; http://orgmode.org/worg/org-contrib/babel/
30
31;;; Code:
32(require 'ob)
33(require 'ob-table)
34
35(defvar org-babel-library-of-babel nil
36 "Library of source-code blocks.
37This is an association list. Populate the library by adding
38files to `org-babel-lob-files'.")
39
40(defcustom org-babel-lob-files '()
41 "Files used to populate the `org-babel-library-of-babel'.
42To add files to this list use the `org-babel-lob-ingest' command."
43 :group 'org-babel
44 :type 'list)
45
46;;;###autoload
47(defun org-babel-lob-ingest (&optional file)
afe98dfa
CD
48 "Add all named source-blocks defined in FILE to
49`org-babel-library-of-babel'."
86fbb8ca 50 (interactive "f")
afe98dfa
CD
51 (let ((lob-ingest-count 0))
52 (org-babel-map-src-blocks file
53 (let* ((info (org-babel-get-src-block-info 'light))
54 (source-name (nth 4 info)))
55 (when source-name
56 (setq source-name (intern source-name)
57 org-babel-library-of-babel
58 (cons (cons source-name info)
59 (assq-delete-all source-name org-babel-library-of-babel))
60 lob-ingest-count (1+ lob-ingest-count)))))
61 (message "%d src block%s added to Library of Babel"
62 lob-ingest-count (if (> lob-ingest-count 1) "s" ""))
63 lob-ingest-count))
86fbb8ca
CD
64
65(defconst org-babel-lob-call-aliases '("lob" "call")
66 "Aliases to call a source block function.
67If you change the value of this variable then your files may
68 become unusable by other org-babel users, and vice versa.")
69
70(defconst org-babel-lob-one-liner-regexp
afe98dfa
CD
71 (concat
72 "^\\([ \t]*\\)#\\+\\(?:"
73 (mapconcat #'regexp-quote org-babel-lob-call-aliases "\\|")
acedf35c
CD
74 "\\):[ \t]+\\([^\(\)\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)"
75 "\(\\([^\n]*\\)\)\\(\\[.+\\]\\|\\)[ \t]*\\([^\n]*\\)")
86fbb8ca
CD
76 "Regexp to match calls to predefined source block functions.")
77
78;; functions for executing lob one-liners
79;;;###autoload
80(defun org-babel-lob-execute-maybe ()
81 "Execute a Library of Babel source block, if appropriate.
82Detect if this is context for a Library Of Babel source block and
83if so then run the appropriate source block from the Library."
84 (interactive)
85 (let ((info (org-babel-lob-get-info)))
86 (if (nth 0 info) (progn (org-babel-lob-execute info) t) nil)))
87
86fbb8ca
CD
88;;;###autoload
89(defun org-babel-lob-get-info ()
afe98dfa 90 "Return a Library of Babel function call as a string."
86fbb8ca
CD
91 (let ((case-fold-search t))
92 (save-excursion
93 (beginning-of-line 1)
94 (if (looking-at org-babel-lob-one-liner-regexp)
afe98dfa 95 (append
acedf35c 96 (mapcar #'org-babel-clean-text-properties
afe98dfa 97 (list
acedf35c
CD
98 (format "%s%s(%s)%s"
99 (match-string 2)
100 (if (match-string 4)
101 (concat "[" (match-string 4) "]") "")
102 (or (match-string 6) "") (match-string 7))
103 (match-string 8)))
afe98dfa 104 (list (length (match-string 1))))))))
acedf35c 105
86fbb8ca
CD
106(defun org-babel-lob-execute (info)
107 "Execute the lob call specified by INFO."
afe98dfa
CD
108 (let ((params (org-babel-process-params
109 (org-babel-merge-params
110 org-babel-default-header-args
111 (org-babel-params-from-buffer)
112 (org-babel-params-from-properties)
113 (org-babel-parse-header-arguments
114 (org-babel-clean-text-properties
115 (concat ":var results="
116 (mapconcat #'identity (butlast info) " "))))))))
86fbb8ca
CD
117 (org-babel-execute-src-block
118 nil (list "emacs-lisp" "results" params nil nil (nth 2 info)))))
119
120(provide 'ob-lob)
121
122;; arch-tag: ce0712c9-2147-4019-ba3f-42341b8b474b
123
124;;; ob-lob.el ends here