ob-lob.el 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. ;;; ob-lob.el --- functions supporting the Library of Babel
  2. ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte, Dan Davison
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.01
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs 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 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; See the online documentation for more information
  20. ;;
  21. ;; http://orgmode.org/worg/org-contrib/babel/
  22. ;;; Code:
  23. (require 'ob)
  24. (require 'ob-table)
  25. (defvar org-babel-library-of-babel nil
  26. "Library of source-code blocks. This is an association list.
  27. Populate the library by adding files to `org-babel-lob-files'.")
  28. (defcustom org-babel-lob-files '()
  29. "Files used to populate the `org-babel-library-of-babel'. To
  30. add files to this list use the `org-babel-lob-ingest' command."
  31. :group 'org-babel
  32. :type 'list)
  33. ;;;###autoload
  34. (defun org-babel-lob-ingest (&optional file)
  35. "Add all source-blocks defined in FILE to `org-babel-library-of-babel'."
  36. (interactive "f")
  37. (org-babel-map-src-blocks file
  38. (let* ((info (org-babel-get-src-block-info))
  39. (source-name (intern (nth 4 info))))
  40. (when source-name
  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. (defconst org-babel-lob-call-aliases '("lob" "call")
  45. "These can be used interchangeably to call a source block
  46. function. If you change the value of this variable then your
  47. files may become unusable by other org-babel users, and vice
  48. versa.")
  49. (defconst org-babel-lob-one-liner-regexp
  50. (concat "^\\([ \t]*\\)#\\+\\(?:"
  51. (mapconcat #'regexp-quote org-babel-lob-call-aliases "\\|")
  52. "\\):[ \t]+\\([^\(\)\n]+\\)\(\\([^\n]*\\)\)[ \t]*\\([^\n]*\\)")
  53. "Regexp to match calls to predefined source block functions")
  54. ;; functions for executing lob one-liners
  55. ;;;###autoload
  56. (defun org-babel-lob-execute-maybe ()
  57. "Detect if this is context for a org-babel Library Of Babel
  58. src-block and if so then run the appropriate source block from
  59. the Library."
  60. (interactive)
  61. (let ((info (org-babel-lob-get-info)))
  62. (if (nth 0 info) (progn (org-babel-lob-execute info) t) nil)))
  63. (add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-lob-execute-maybe)
  64. ;;;###autoload
  65. (defun org-babel-lob-get-info ()
  66. "Return the function call supplied on the current Library of
  67. Babel line as a string.
  68. This function is analogous to org-babel-get-src-block-name. For
  69. both functions, after they are called, (match-string 1) matches
  70. the function name, and (match-string 2) matches the function
  71. arguments inside the parentheses. I think perhaps these functions
  72. should be renamed to bring out this similarity, perhaps involving
  73. the word 'call'."
  74. (let ((case-fold-search t))
  75. (save-excursion
  76. (beginning-of-line 1)
  77. (if (looking-at org-babel-lob-one-liner-regexp)
  78. (append (mapcar #'org-babel-clean-text-properties
  79. (list (format "%s(%s)" (match-string 2) (match-string 3))
  80. (match-string 4)))
  81. (list (length (match-string 1))))))))
  82. (defun org-babel-lob-execute (info)
  83. "Execute the lob call specified by INFO."
  84. (let ((params (org-babel-merge-params
  85. org-babel-default-header-args
  86. (org-babel-params-from-buffer)
  87. (org-babel-params-from-properties)
  88. (org-babel-parse-header-arguments
  89. (org-babel-clean-text-properties
  90. (concat ":var results=" (mapconcat #'identity (butlast info) " ")))))))
  91. (org-babel-execute-src-block
  92. nil (list "emacs-lisp" "results" params nil nil (nth 2 info)))))
  93. (provide 'ob-lob)
  94. ;; arch-tag: ce0712c9-2147-4019-ba3f-42341b8b474b
  95. ;;; ob-lob.el ends here