org-babel-lob.el 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. (setq org-babel-library-of-babel
  42. (cons (cons source-name info)
  43. (assq-delete-all source-name org-babel-library-of-babel))))))
  44. (org-babel-lob-ingest ;; actually add the source-blocks defined in library-of-babel.org
  45. (expand-file-name "library-of-babel.org"
  46. (expand-file-name ".." (file-name-directory (or load-file-name buffer-file-name)))))
  47. ;; functions for executing lob one-liners
  48. (defvar org-babel-lob-one-liner-regexp
  49. "#\\+lob:\\([^ \t\n\r]+\\)\\([ \t]+\\([^\n]+\\)\\)?\n")
  50. (defun org-babel-lob-execute-maybe ()
  51. "Detect if this is context for a org-babel Library Of Babel
  52. src-block and if so then run the appropriate source block from
  53. the Library."
  54. (interactive)
  55. (let ((info (org-babel-lob-get-info)))
  56. (if info (progn (org-babel-lob-execute info) t) nil)))
  57. (add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-lob-execute-maybe)
  58. (defun org-babel-lob-get-info ()
  59. "Return the information of the current Library of Babel line as
  60. a list of the following form.
  61. (source-block-name header-arguments-alist)"
  62. (let ((case-fold-search t))
  63. (save-excursion
  64. (move-beginning-of-line 1)
  65. (if (looking-at org-babel-lob-one-liner-regexp)
  66. (cons (org-babel-clean-text-properties (match-string 1))
  67. (delq nil (mapcar (lambda (assignment)
  68. (save-match-data
  69. (if (string-match "\\(.+\\)=\\(.+\\)" assignment)
  70. (list (org-babel-clean-text-properties (match-string 1 assignment))
  71. (org-babel-clean-text-properties (match-string 2 assignment)))
  72. nil)))
  73. (split-string (match-string 3)))))))))
  74. (defun org-babel-lob-execute (info)
  75. (let ((params (org-babel-parse-header-arguments
  76. (concat ":var results="
  77. (car info)
  78. "("
  79. (mapconcat (lambda (var-spec)
  80. (format "%s=%s" (first var-spec) (second var-spec)))
  81. (cdr info) ", ")
  82. ")"))))
  83. (org-babel-execute-src-block t (list "emacs-lisp" "results" params))))
  84. (provide 'org-babel-lob)
  85. ;;; org-babel-lob.el ends here