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