org-table: use HH:MM:SS as the standard display of durations.
[org-mode.git] / lisp / ob-lob.el
blob9188769e0a0120071508a1b2fcae98d4639b9ca6
1 ;;; ob-lob.el --- functions supporting the Library of Babel
3 ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte, Dan Davison
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
8 ;; Version: 7.6
10 ;; This file is part of GNU Emacs.
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.
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.
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/>.
25 ;;; Code:
26 (eval-when-compile
27 (require 'cl))
28 (require 'ob)
29 (require 'ob-table)
31 (defvar org-babel-library-of-babel nil
32 "Library of source-code blocks.
33 This is an association list. Populate the library by adding
34 files to `org-babel-lob-files'.")
36 (defcustom org-babel-lob-files '()
37 "Files used to populate the `org-babel-library-of-babel'.
38 To add files to this list use the `org-babel-lob-ingest' command."
39 :group 'org-babel
40 :type 'list)
42 (defvar org-babel-default-lob-header-args '((:exports . "results"))
43 "Default header arguments to use when exporting #+lob/call lines.")
45 ;;;###autoload
46 (defun org-babel-lob-ingest (&optional file)
47 "Add all named source-blocks defined in FILE to
48 `org-babel-library-of-babel'."
49 (interactive "fFile: ")
50 (let ((lob-ingest-count 0))
51 (org-babel-map-src-blocks file
52 (let* ((info (org-babel-get-src-block-info 'light))
53 (source-name (nth 4 info)))
54 (when source-name
55 (setq source-name (intern source-name)
56 org-babel-library-of-babel
57 (cons (cons source-name info)
58 (assq-delete-all source-name org-babel-library-of-babel))
59 lob-ingest-count (1+ lob-ingest-count)))))
60 (message "%d src block%s added to Library of Babel"
61 lob-ingest-count (if (> lob-ingest-count 1) "s" ""))
62 lob-ingest-count))
64 (defconst org-babel-lob-call-aliases '("lob" "call")
65 "Aliases to call a source block function.
66 If you change the value of this variable then your files may
67 become unusable by other org-babel users, and vice versa.")
69 (defconst org-babel-block-lob-one-liner-regexp
70 (concat
71 "^\\([ \t]*\\)#\\+\\(?:"
72 (mapconcat #'regexp-quote org-babel-lob-call-aliases "\\|")
73 "\\):[ \t]+\\([^\(\)\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)"
74 "\(\\([^\n]*\\)\)\\(\\[.+\\]\\|\\)[ \t]*\\(\\([^\n]*\\)\\)?")
75 "Regexp to match non-inline calls to predefined source block functions.")
77 (defconst org-babel-inline-lob-one-liner-regexp
78 (concat
79 "\\([^\n]*\\)\\(?:"
80 (mapconcat #'regexp-quote org-babel-lob-call-aliases "\\|")
81 "\\)_\\([^\(\)\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)"
82 "\(\\([^\n]*\\)\)\\(\\[\\(.*?\\)\\]\\)?")
83 "Regexp to match inline calls to predefined source block functions.")
85 (defconst org-babel-lob-one-liner-regexp
86 (concat "\\(" org-babel-block-lob-one-liner-regexp
87 "\\|" org-babel-inline-lob-one-liner-regexp "\\)")
88 "Regexp to match calls to predefined source block functions.")
90 ;; functions for executing lob one-liners
91 ;;;###autoload
92 (defun org-babel-lob-execute-maybe ()
93 "Execute a Library of Babel source block, if appropriate.
94 Detect if this is context for a Library Of Babel source block and
95 if so then run the appropriate source block from the Library."
96 (interactive)
97 (let ((info (org-babel-lob-get-info)))
98 (if (nth 0 info) (progn (org-babel-lob-execute info) t) nil)))
100 ;;;###autoload
101 (defun org-babel-lob-get-info ()
102 "Return a Library of Babel function call as a string."
103 (flet ((nonempty (a b)
104 (let ((it (match-string a)))
105 (if (= (length it) 0) (match-string b) it))))
106 (let ((case-fold-search t))
107 (save-excursion
108 (beginning-of-line 1)
109 (when (looking-at org-babel-lob-one-liner-regexp)
110 (append
111 (mapcar #'org-babel-clean-text-properties
112 (list
113 (format "%s%s(%s)%s"
114 (nonempty 3 12)
115 (if (not (= 0 (length (nonempty 5 13))))
116 (concat "[" (nonempty 5 13) "]") "")
117 (or (nonempty 7 16) "")
118 (or (nonempty 8 19) ""))
119 (nonempty 9 18)))
120 (list (length (if (= (length (match-string 12)) 0)
121 (match-string 2) (match-string 11))))))))))
123 (defun org-babel-lob-execute (info)
124 "Execute the lob call specified by INFO."
125 (let ((params (org-babel-process-params
126 (org-babel-merge-params
127 org-babel-default-header-args
128 (org-babel-params-from-buffer)
129 (org-babel-params-from-properties)
130 (org-babel-parse-header-arguments
131 (org-babel-clean-text-properties
132 (concat ":var results="
133 (mapconcat #'identity (butlast info) " "))))))))
134 (org-babel-execute-src-block
135 nil (list "emacs-lisp" "results" params nil nil (nth 2 info)))))
137 (provide 'ob-lob)
139 ;; arch-tag: ce0712c9-2147-4019-ba3f-42341b8b474b
141 ;;; ob-lob.el ends here