ob-lob.el 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.01trans
  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.
  27. This is an association list. Populate the library by adding
  28. files to `org-babel-lob-files'.")
  29. (defcustom org-babel-lob-files '()
  30. "Files used to populate the `org-babel-library-of-babel'.
  31. To add files to this list use the `org-babel-lob-ingest' command."
  32. :group 'org-babel
  33. :type 'list)
  34. ;;;###autoload
  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-src-blocks file
  39. (let* ((info (org-babel-get-src-block-info))
  40. (source-name (intern (nth 4 info))))
  41. (when source-name
  42. (setq org-babel-library-of-babel
  43. (cons (cons source-name info)
  44. (assq-delete-all source-name org-babel-library-of-babel)))))))
  45. (defconst org-babel-lob-call-aliases '("lob" "call")
  46. "Aliases to call a source block function.
  47. If you change the value of this variable then your files may
  48. become unusable by other org-babel users, and vice versa.")
  49. (defconst org-babel-lob-one-liner-regexp
  50. (concat
  51. "^\\([ \t]*\\)#\\+\\(?:"
  52. (mapconcat #'regexp-quote org-babel-lob-call-aliases "\\|")
  53. "\\):[ \t]+\\([^\(\)\n]+\\)\(\\([^\n]*\\)\)\\(\\[.+\\]\\|\\)[ \t]*\\([^\n]*\\)")
  54. "Regexp to match calls to predefined source block functions.")
  55. ;; functions for executing lob one-liners
  56. ;;;###autoload
  57. (defun org-babel-lob-execute-maybe ()
  58. "Execute a Library of Babel source block, if appropriate.
  59. Detect if this is context for a Library Of Babel source block and
  60. if so then run the appropriate source block from the Library."
  61. (interactive)
  62. (let ((info (org-babel-lob-get-info)))
  63. (if (nth 0 info) (progn (org-babel-lob-execute info) t) nil)))
  64. ;;;###autoload
  65. (defun org-babel-lob-get-info ()
  66. "Return a Library of Babel function call as a string.
  67. This function is analogous to org-babel-get-src-block-name. For
  68. both functions, after they are called, (match-string 1) matches
  69. the function name, and (match-string 2) matches the function
  70. arguments inside the parentheses. I think perhaps these functions
  71. should be renamed to bring out this similarity, perhaps involving
  72. the word 'call'."
  73. (let ((case-fold-search t))
  74. (save-excursion
  75. (beginning-of-line 1)
  76. (if (looking-at org-babel-lob-one-liner-regexp)
  77. (append
  78. (mapcar #'org-babel-clean-text-properties
  79. (list
  80. (format "%s(%s)%s"
  81. (match-string 2) (match-string 3) (match-string 4))
  82. (match-string 5)))
  83. (list (length (match-string 1))))))))
  84. (defun org-babel-lob-execute (info)
  85. "Execute the lob call specified by INFO."
  86. (let ((params (org-babel-merge-params
  87. org-babel-default-header-args
  88. (org-babel-params-from-buffer)
  89. (org-babel-params-from-properties)
  90. (org-babel-parse-header-arguments
  91. (org-babel-clean-text-properties
  92. (concat ":var results="
  93. (mapconcat #'identity (butlast info) " ")))))))
  94. (org-babel-execute-src-block
  95. nil (list "emacs-lisp" "results" params nil nil (nth 2 info)))))
  96. (provide 'ob-lob)
  97. ;; arch-tag: ce0712c9-2147-4019-ba3f-42341b8b474b
  98. ;;; ob-lob.el ends here