org-babel-lob.el 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. ;;; org-babel-lob.el --- The Library of Babel: off-the-shelf functions for data analysis and plotting using org-babel
  2. ;; Copyright (C) 2009 Dan Davison, Eric Schulte
  3. ;; Author: Dan Davison, Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.01
  7. ;;; License:
  8. ;; This program is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 3, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;; Commentary:
  23. ;; See org-babel.org in the parent directory for more information
  24. ;;; Code:
  25. (require 'org-babel)
  26. (require 'org-babel-table)
  27. (defvar org-babel-library-of-babel nil
  28. "Library of source-code blocks. This is an association list.
  29. Populate the library by adding files to `org-babel-lob-files'.")
  30. (defcustom org-babel-lob-files '()
  31. "Files used to populate the `org-babel-library-of-babel'. To
  32. add files to this list use the `org-babel-lob-ingest' command."
  33. :group 'org-babel
  34. :type 'list)
  35. (defun org-babel-lob-ingest (&optional file)
  36. "Add all source-blocks defined in FILE to `org-babel-library-of-babel'."
  37. (interactive "f")
  38. (org-babel-map-source-blocks file
  39. (let ((source-name (intern (org-babel-get-src-block-name)))
  40. (info (org-babel-get-src-block-info)))
  41. ;; remove :var elements from params
  42. ;; (once we have a better way of combining parameter lists then we won't have to do this)
  43. (setf (third info) (assq-delete-all :var (third info)))
  44. (setq org-babel-library-of-babel
  45. (cons (cons source-name info)
  46. (assq-delete-all source-name org-babel-library-of-babel))))))
  47. (org-babel-lob-ingest ;; actually add the source-blocks defined in library-of-babel.org
  48. (expand-file-name "library-of-babel.org"
  49. (expand-file-name ".." (file-name-directory (or load-file-name buffer-file-name)))))
  50. ;; functions for executing lob one-liners
  51. (defvar org-babel-lob-one-liner-regexp "#\\+lob:[ \t]+\\([^\n]+\\)\n")
  52. (defun org-babel-lob-execute-maybe ()
  53. "Detect if this is context for a org-babel Library Of Babel
  54. src-block and if so then run the appropriate source block from
  55. the Library."
  56. (interactive)
  57. (let ((info (org-babel-lob-get-info)))
  58. (if info (progn (org-babel-lob-execute info) t) nil)))
  59. (add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-lob-execute-maybe)
  60. (defun org-babel-lob-get-info ()
  61. "Return the information of the current Library of Babel line as
  62. a list of the following form.
  63. (source-block-name header-arguments-alist)"
  64. (let ((case-fold-search t))
  65. (save-excursion
  66. (move-beginning-of-line 1)
  67. (if (looking-at org-babel-lob-one-liner-regexp)
  68. (org-babel-clean-text-properties (match-string 1))))))
  69. (defun org-babel-lob-execute (info)
  70. (let ((params (org-babel-parse-header-arguments (concat ":var results=" info))))
  71. (org-babel-execute-src-block t (list "emacs-lisp" "results" params))))
  72. (provide 'org-babel-lob)
  73. ;;; org-babel-lob.el ends here