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