ob-lob.el 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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: 7.7
  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. ;;; Code:
  19. (eval-when-compile
  20. (require 'cl))
  21. (require 'ob)
  22. (require 'ob-table)
  23. (defvar org-babel-library-of-babel nil
  24. "Library of source-code blocks.
  25. This is an association list. Populate the library by adding
  26. files to `org-babel-lob-files'.")
  27. (defcustom org-babel-lob-files '()
  28. "Files used to populate the `org-babel-library-of-babel'.
  29. To add files to this list use the `org-babel-lob-ingest' command."
  30. :group 'org-babel
  31. :type 'list)
  32. (defvar org-babel-default-lob-header-args '((:exports . "results"))
  33. "Default header arguments to use when exporting #+lob/call lines.")
  34. ;;;###autoload
  35. (defun org-babel-lob-ingest (&optional file)
  36. "Add all named source-blocks defined in FILE to
  37. `org-babel-library-of-babel'."
  38. (interactive "fFile: ")
  39. (let ((lob-ingest-count 0))
  40. (org-babel-map-src-blocks file
  41. (let* ((info (org-babel-get-src-block-info 'light))
  42. (source-name (nth 4 info)))
  43. (when source-name
  44. (setq source-name (intern source-name)
  45. org-babel-library-of-babel
  46. (cons (cons source-name info)
  47. (assq-delete-all source-name org-babel-library-of-babel))
  48. lob-ingest-count (1+ lob-ingest-count)))))
  49. (message "%d src block%s added to Library of Babel"
  50. lob-ingest-count (if (> lob-ingest-count 1) "s" ""))
  51. lob-ingest-count))
  52. (defconst org-babel-lob-call-aliases '("lob" "call")
  53. "Aliases to call a source block function.
  54. If you change the value of this variable then your files may
  55. become unusable by other org-babel users, and vice versa.")
  56. (defconst org-babel-block-lob-one-liner-regexp
  57. (concat
  58. "^\\([ \t]*\\)#\\+\\(?:"
  59. (mapconcat #'regexp-quote org-babel-lob-call-aliases "\\|")
  60. "\\):[ \t]+\\([^\(\)\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)"
  61. "\(\\([^\n]*\\)\)\\(\\[.+\\]\\|\\)[ \t]*\\(\\([^\n]*\\)\\)?")
  62. "Regexp to match non-inline calls to predefined source block functions.")
  63. (defconst org-babel-inline-lob-one-liner-regexp
  64. (concat
  65. "\\([^\n]*\\)\\(?:"
  66. (mapconcat #'regexp-quote org-babel-lob-call-aliases "\\|")
  67. "\\)_\\([^\(\)\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)"
  68. "\(\\([^\n]*\\)\)\\(\\[\\(.*?\\)\\]\\)?")
  69. "Regexp to match inline calls to predefined source block functions.")
  70. (defconst org-babel-lob-one-liner-regexp
  71. (concat "\\(" org-babel-block-lob-one-liner-regexp
  72. "\\|" org-babel-inline-lob-one-liner-regexp "\\)")
  73. "Regexp to match calls to predefined source block functions.")
  74. ;;;###autoload
  75. (defmacro org-babel-map-call-lines (file &rest body)
  76. "Evaluate BODY forms on each #+call line in FILE.
  77. If FILE is nil evaluate BODY forms on source blocks in current
  78. buffer."
  79. (declare (indent 1))
  80. `(org-babel-map-regexp ,org-babel-block-lob-one-liner-regexp ,file ,@body))
  81. ;;;###autoload
  82. (defun org-babel-lob-execute-maybe ()
  83. "Execute a Library of Babel source block, if appropriate.
  84. Detect if this is context for a Library Of Babel source block and
  85. if so then run the appropriate source block from the Library."
  86. (interactive)
  87. (let ((info (org-babel-lob-get-info)))
  88. (if (nth 0 info) (progn (org-babel-lob-execute info) t) nil)))
  89. ;;;###autoload
  90. (defun org-babel-lob-get-info ()
  91. "Return a Library of Babel function call as a string."
  92. (flet ((nonempty (a b)
  93. (let ((it (match-string a)))
  94. (if (= (length it) 0) (match-string b) it))))
  95. (let ((case-fold-search t))
  96. (save-excursion
  97. (beginning-of-line 1)
  98. (when (looking-at org-babel-lob-one-liner-regexp)
  99. (append
  100. (mapcar #'org-babel-clean-text-properties
  101. (list
  102. (format "%s%s(%s)%s"
  103. (nonempty 3 12)
  104. (if (not (= 0 (length (nonempty 5 13))))
  105. (concat "[" (nonempty 5 13) "]") "")
  106. (or (nonempty 7 16) "")
  107. (or (nonempty 8 19) ""))
  108. (nonempty 9 18)))
  109. (list (length (if (= (length (match-string 12)) 0)
  110. (match-string 2) (match-string 11))))))))))
  111. (defun org-babel-lob-execute (info)
  112. "Execute the lob call specified by INFO."
  113. (let ((params (org-babel-process-params
  114. (org-babel-merge-params
  115. org-babel-default-header-args
  116. (org-babel-params-from-buffer)
  117. (org-babel-params-from-properties)
  118. (org-babel-parse-header-arguments
  119. (org-babel-clean-text-properties
  120. (concat ":var results="
  121. (mapconcat #'identity (butlast info) " "))))))))
  122. (org-babel-execute-src-block
  123. nil (list "emacs-lisp" "results" params nil nil (nth 2 info)))))
  124. (provide 'ob-lob)
  125. ;; arch-tag: ce0712c9-2147-4019-ba3f-42341b8b474b
  126. ;;; ob-lob.el ends here