babel: enhanced code block movement functions
[org-mode.git] / lisp / ob-lob.el
blob9f0245166aea3ed98a80dd058af7dde4fccca4d2
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: 0.01
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 ;;; Commentary:
27 ;; See the online documentation for more information
28 ;;
29 ;; http://orgmode.org/worg/org-contrib/babel/
31 ;;; Code:
32 (require 'ob)
33 (require 'ob-table)
35 (defvar org-babel-library-of-babel nil
36 "Library of source-code blocks. This is an association list.
37 Populate the library by adding files to `org-babel-lob-files'.")
39 (defcustom org-babel-lob-files '()
40 "Files used to populate the `org-babel-library-of-babel'. To
41 add files to this list use the `org-babel-lob-ingest' command."
42 :group 'org-babel
43 :type 'list)
45 ;;;###autoload
46 (defun org-babel-lob-ingest (&optional file)
47 "Add all source-blocks defined in FILE to `org-babel-library-of-babel'."
48 (interactive "f")
49 (org-babel-map-src-blocks file
50 (let* ((info (org-babel-get-src-block-info))
51 (source-name (intern (nth 4 info))))
52 (when source-name
53 (setq org-babel-library-of-babel
54 (cons (cons source-name info)
55 (assq-delete-all source-name org-babel-library-of-babel)))))))
57 (defconst org-babel-lob-call-aliases '("lob" "call")
58 "These can be used interchangeably to call a source block
59 function. If you change the value of this variable then your
60 files may become unusable by other org-babel users, and vice
61 versa.")
63 (defconst org-babel-lob-one-liner-regexp
64 (concat "^\\([ \t]*\\)#\\+\\(?:"
65 (mapconcat #'regexp-quote org-babel-lob-call-aliases "\\|")
66 "\\):[ \t]+\\([^\(\)\n]+\\)\(\\([^\n]*\\)\)[ \t]*\\([^\n]*\\)")
67 "Regexp to match calls to predefined source block functions")
69 ;; functions for executing lob one-liners
70 ;;;###autoload
71 (defun org-babel-lob-execute-maybe ()
72 "Detect if this is context for a org-babel Library Of Babel
73 src-block and if so then run the appropriate source block from
74 the Library."
75 (interactive)
76 (let ((info (org-babel-lob-get-info)))
77 (if (nth 0 info) (progn (org-babel-lob-execute info) t) nil)))
79 (add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-lob-execute-maybe)
81 ;;;###autoload
82 (defun org-babel-lob-get-info ()
83 "Return the function call supplied on the current Library of
84 Babel line as a string.
86 This function is analogous to org-babel-get-src-block-name. For
87 both functions, after they are called, (match-string 1) matches
88 the function name, and (match-string 2) matches the function
89 arguments inside the parentheses. I think perhaps these functions
90 should be renamed to bring out this similarity, perhaps involving
91 the word 'call'."
92 (let ((case-fold-search t))
93 (save-excursion
94 (beginning-of-line 1)
95 (if (looking-at org-babel-lob-one-liner-regexp)
96 (append (mapcar #'org-babel-clean-text-properties
97 (list (format "%s(%s)" (match-string 2) (match-string 3))
98 (match-string 4)))
99 (list (length (match-string 1))))))))
101 (defun org-babel-lob-execute (info)
102 "Execute the lob call specified by INFO."
103 (let ((params (org-babel-merge-params
104 org-babel-default-header-args
105 (org-babel-params-from-buffer)
106 (org-babel-params-from-properties)
107 (org-babel-parse-header-arguments
108 (org-babel-clean-text-properties
109 (concat ":var results=" (mapconcat #'identity (butlast info) " ")))))))
110 (org-babel-execute-src-block
111 nil (list "emacs-lisp" "results" params nil nil (nth 2 info)))))
113 (provide 'ob-lob)
115 ;; arch-tag: ce0712c9-2147-4019-ba3f-42341b8b474b
117 ;;; ob-lob.el ends here