1 ;;; org-babel-lob.el --- The Library of Babel: off-the-shelf functions for data analysis and plotting using org-babel
3 ;; Copyright (C) 2009 Eric Schulte, Dan Davison
5 ;; Author: Eric Schulte, Dan Davison
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
12 ;; This program 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, or (at your option)
17 ;; This program 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; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; See org-babel.org in the parent directory for more information
33 (require 'org-babel-table
)
34 (require 'org-babel-exp
)
36 (defvar org-babel-library-of-babel nil
37 "Library of source-code blocks. This is an association list.
38 Populate the library by adding files to `org-babel-lob-files'.")
40 (defcustom org-babel-lob-files
'()
41 "Files used to populate the `org-babel-library-of-babel'. To
42 add files to this list use the `org-babel-lob-ingest' command."
46 (defun org-babel-lob-ingest (&optional file
)
47 "Add all source-blocks defined in FILE to `org-babel-library-of-babel'."
49 (org-babel-map-source-blocks file
50 (let* ((info (org-babel-get-src-block-info))
51 (source-name (intern (fifth info
))))
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
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
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
76 (let ((info (org-babel-lob-get-info)))
77 (if (first info
) (progn (org-babel-lob-execute info
) t
) nil
)))
79 (add-hook 'org-ctrl-c-ctrl-c-hook
'org-babel-lob-execute-maybe
)
81 (defun org-babel-lob-get-info ()
82 "Return the function call supplied on the current Library of
83 Babel line as a string.
85 This function is analogous to org-babel-get-src-block-name. For
86 both functions, after they are called, (match-string 1) matches
87 the function name, and (match-string 2) matches the function
88 arguments inside the parentheses. I think perhaps these functions
89 should be renamed to bring out this similarity, perhaps involving
91 (let ((case-fold-search t
))
93 (move-beginning-of-line 1)
94 (if (looking-at org-babel-lob-one-liner-regexp
)
95 (mapcar #'org-babel-clean-text-properties
96 (list (format "%s(%s)" (match-string 1) (match-string 2))
97 (match-string 3)))))))
99 (defun org-babel-lob-execute (info)
100 (let ((params (org-babel-merge-params
101 org-babel-default-header-args
102 (org-babel-parse-header-arguments
103 (org-babel-clean-text-properties
104 (concat ":var results=" (mapconcat #'identity info
" ")))))))
105 (org-babel-execute-src-block nil
(list "emacs-lisp" "results" params
))))
107 (define-generic-mode org-babel-lob-mode
108 '("#") (list org-babel-function-def-export-keyword
) nil nil nil
109 "Major mode for fontification of library of babel lines on export")
111 (provide 'org-babel-lob
)
112 ;;; org-babel-lob.el ends here