org-babel-lob.el 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. (when source-name
  42. ;; remove :var elements from params
  43. ;; (once we have a better way of combining parameter lists then we won't have to do this)
  44. (setf (third info) (assq-delete-all :var (third info)))
  45. (setq org-babel-library-of-babel
  46. (cons (cons source-name info)
  47. (assq-delete-all source-name org-babel-library-of-babel)))))))
  48. (org-babel-lob-ingest ;; actually add the source-blocks defined in library-of-babel.org
  49. (expand-file-name "library-of-babel.org"
  50. (expand-file-name ".." (file-name-directory (or load-file-name buffer-file-name)))))
  51. ;; functions for executing lob one-liners
  52. (defvar org-babel-lob-one-liner-regexp "#\\+lob:[ \t]+\\([^\n]+\\)\n")
  53. (defun org-babel-lob-execute-maybe ()
  54. "Detect if this is context for a org-babel Library Of Babel
  55. src-block and if so then run the appropriate source block from
  56. the Library."
  57. (interactive)
  58. (let ((info (org-babel-lob-get-info)))
  59. (if info (progn (org-babel-lob-execute info) t) nil)))
  60. (add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-lob-execute-maybe)
  61. (defun org-babel-lob-get-info ()
  62. "Return the information of the current Library of Babel line as
  63. a list of the following form.
  64. (source-block-name header-arguments-alist)"
  65. (let ((case-fold-search t))
  66. (save-excursion
  67. (move-beginning-of-line 1)
  68. (if (looking-at org-babel-lob-one-liner-regexp)
  69. (org-babel-clean-text-properties (match-string 1))))))
  70. (defun org-babel-lob-execute (info)
  71. (let ((params (org-babel-parse-header-arguments (concat ":var results=" info))))
  72. (org-babel-execute-src-block t (list "emacs-lisp" "results" params))))
  73. (provide 'org-babel-lob)
  74. ;;; org-babel-lob.el ends here