ob-lob.el 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. ;;; ob-lob.el --- The Library of Babel: off-the-shelf functions for data analysis and plotting using org-babel
  2. ;; Copyright (C) 2009 Eric Schulte, Dan Davison
  3. ;; Author: Eric Schulte, Dan Davison
  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 'ob)
  26. (require 'ob-table)
  27. (require 'ob-exp)
  28. (declare-function org-babel-get-src-block-info "ob" (&optional header-vars-only))
  29. (defvar org-babel-library-of-babel nil
  30. "Library of source-code blocks. This is an association list.
  31. Populate the library by adding files to `org-babel-lob-files'.")
  32. (defcustom org-babel-lob-files '()
  33. "Files used to populate the `org-babel-library-of-babel'. To
  34. add files to this list use the `org-babel-lob-ingest' command."
  35. :group 'org-babel
  36. :type 'list)
  37. (defun org-babel-lob-ingest (&optional file)
  38. "Add all source-blocks defined in FILE to `org-babel-library-of-babel'."
  39. (interactive "f")
  40. (org-babel-map-source-blocks file
  41. (let* ((info (org-babel-get-src-block-info))
  42. (source-name (intern (nth 4 info))))
  43. (when source-name
  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. (defconst org-babel-lob-call-aliases '("lob" "call")
  48. "These can be used interchangeably to call a source block
  49. function. If you change the value of this variable then your
  50. files may become unusable by other org-babel users, and vice
  51. versa.")
  52. (defconst org-babel-lob-one-liner-regexp
  53. (concat "^\\([ \t]*\\)#\\+\\(?:"
  54. (mapconcat #'regexp-quote org-babel-lob-call-aliases "\\|")
  55. "\\):[ \t]+\\([^\(\)\n]+\\)\(\\([^\n]*\\)\)[ \t]*\\([^\n]*\\)")
  56. "Regexp to match calls to predefined source block functions")
  57. ;; functions for executing lob one-liners
  58. (defun org-babel-lob-execute-maybe ()
  59. "Detect if this is context for a org-babel Library Of Babel
  60. src-block and if so then run the appropriate source block from
  61. the Library."
  62. (interactive)
  63. (let ((info (org-babel-lob-get-info)))
  64. (if (nth 0 info) (progn (org-babel-lob-execute info) t) nil)))
  65. (add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-lob-execute-maybe)
  66. (defun org-babel-lob-get-info ()
  67. "Return the function call supplied on the current Library of
  68. Babel line as a string.
  69. This function is analogous to org-babel-get-src-block-name. For
  70. both functions, after they are called, (match-string 1) matches
  71. the function name, and (match-string 2) matches the function
  72. arguments inside the parentheses. I think perhaps these functions
  73. should be renamed to bring out this similarity, perhaps involving
  74. the word 'call'."
  75. (let ((case-fold-search t))
  76. (save-excursion
  77. (beginning-of-line 1)
  78. (if (looking-at org-babel-lob-one-liner-regexp)
  79. (append (mapcar #'org-babel-clean-text-properties
  80. (list (format "%s(%s)" (match-string 2) (match-string 3))
  81. (match-string 4)))
  82. (list (length (match-string 1))))))))
  83. (defun org-babel-lob-execute (info)
  84. "Execute the lob call specified by INFO."
  85. (let ((params (org-babel-merge-params
  86. org-babel-default-header-args
  87. (org-babel-params-from-buffer)
  88. (org-babel-params-from-properties)
  89. (org-babel-parse-header-arguments
  90. (org-babel-clean-text-properties
  91. (concat ":var results=" (mapconcat #'identity (butlast info) " ")))))))
  92. (org-babel-execute-src-block
  93. nil (list "emacs-lisp" "results" params nil nil (nth 2 info)))))
  94. (provide 'ob-lob)
  95. ;;; ob-lob.el ends here