ob-lob.el 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. ;;; ob-lob.el --- functions supporting the Library of Babel
  2. ;; Copyright (C) 2009-2011 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte, Dan Davison
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Code:
  18. (eval-when-compile
  19. (require 'cl))
  20. (require 'ob)
  21. (require 'ob-table)
  22. (defvar org-babel-library-of-babel nil
  23. "Library of source-code blocks.
  24. This is an association list. Populate the library by adding
  25. files to `org-babel-lob-files'.")
  26. (defcustom org-babel-lob-files '()
  27. "Files used to populate the `org-babel-library-of-babel'.
  28. To add files to this list use the `org-babel-lob-ingest' command."
  29. :group 'org-babel
  30. :type 'list)
  31. (defvar org-babel-default-lob-header-args '((:exports . "results"))
  32. "Default header arguments to use when exporting #+lob/call lines.")
  33. ;;;###autoload
  34. (defun org-babel-lob-ingest (&optional file)
  35. "Add all named source-blocks defined in FILE to
  36. `org-babel-library-of-babel'."
  37. (interactive "fFile: ")
  38. (let ((lob-ingest-count 0))
  39. (org-babel-map-src-blocks file
  40. (let* ((info (org-babel-get-src-block-info 'light))
  41. (source-name (nth 4 info)))
  42. (when source-name
  43. (setq source-name (intern source-name)
  44. org-babel-library-of-babel
  45. (cons (cons source-name info)
  46. (assq-delete-all source-name org-babel-library-of-babel))
  47. lob-ingest-count (1+ lob-ingest-count)))))
  48. (message "%d src block%s added to Library of Babel"
  49. lob-ingest-count (if (> lob-ingest-count 1) "s" ""))
  50. lob-ingest-count))
  51. (defconst org-babel-lob-call-aliases '("lob" "call")
  52. "Aliases to call a source block function.
  53. If you change the value of this variable then your files may
  54. become unusable by other org-babel users, and vice versa.")
  55. (defconst org-babel-block-lob-one-liner-regexp
  56. (concat
  57. "^\\([ \t]*\\)#\\+\\(?:"
  58. (mapconcat #'regexp-quote org-babel-lob-call-aliases "\\|")
  59. "\\):[ \t]+\\([^\(\)\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)"
  60. "\(\\([^\n]*\\)\)\\(\\[.+\\]\\|\\)[ \t]*\\(\\([^\n]*\\)\\)?")
  61. "Regexp to match non-inline calls to predefined source block functions.")
  62. (defconst org-babel-inline-lob-one-liner-regexp
  63. (concat
  64. "\\([^\n]*\\)\\(?:"
  65. (mapconcat #'regexp-quote org-babel-lob-call-aliases "\\|")
  66. "\\)_\\([^\(\)\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)"
  67. "\(\\([^\n]*\\)\)\\(\\[\\(.*?\\)\\]\\)?")
  68. "Regexp to match inline calls to predefined source block functions.")
  69. (defconst org-babel-lob-one-liner-regexp
  70. (concat "\\(" org-babel-block-lob-one-liner-regexp
  71. "\\|" org-babel-inline-lob-one-liner-regexp "\\)")
  72. "Regexp to match calls to predefined source block functions.")
  73. ;; functions for executing lob one-liners
  74. ;;;###autoload
  75. (defun org-babel-lob-execute-maybe ()
  76. "Execute a Library of Babel source block, if appropriate.
  77. Detect if this is context for a Library Of Babel source block and
  78. if so then run the appropriate source block from the Library."
  79. (interactive)
  80. (let ((info (org-babel-lob-get-info)))
  81. (if (nth 0 info) (progn (org-babel-lob-execute info) t) nil)))
  82. ;;;###autoload
  83. (defun org-babel-lob-get-info ()
  84. "Return a Library of Babel function call as a string."
  85. (flet ((nonempty (a b)
  86. (let ((it (match-string a)))
  87. (if (= (length it) 0) (match-string b) it))))
  88. (let ((case-fold-search t))
  89. (save-excursion
  90. (beginning-of-line 1)
  91. (when (looking-at org-babel-lob-one-liner-regexp)
  92. (append
  93. (mapcar #'org-babel-clean-text-properties
  94. (list
  95. (format "%s%s(%s)%s"
  96. (nonempty 3 12)
  97. (if (not (= 0 (length (nonempty 5 13))))
  98. (concat "[" (nonempty 5 13) "]") "")
  99. (or (nonempty 7 16) "")
  100. (or (nonempty 8 19) ""))
  101. (nonempty 9 18)))
  102. (list (length (if (= (length (match-string 12)) 0)
  103. (match-string 2) (match-string 11))))))))))
  104. (defun org-babel-lob-execute (info)
  105. "Execute the lob call specified by INFO."
  106. (let ((params (org-babel-process-params
  107. (org-babel-merge-params
  108. org-babel-default-header-args
  109. (org-babel-params-from-buffer)
  110. (org-babel-params-from-properties)
  111. (org-babel-parse-header-arguments
  112. (org-babel-clean-text-properties
  113. (concat ":var results="
  114. (mapconcat #'identity (butlast info) " "))))))))
  115. (org-babel-execute-src-block
  116. nil (list "emacs-lisp" "results" params nil nil (nth 2 info)))))
  117. (provide 'ob-lob)
  118. ;;; ob-lob.el ends here