2016-06-15 06:36:26 -04:00
|
|
|
;;; ob-lob.el --- Functions Supporting the Library of Babel -*- lexical-binding: t; -*-
|
2009-06-12 20:26:28 -04:00
|
|
|
|
2022-01-01 15:10:55 -05:00
|
|
|
;; Copyright (C) 2009-2022 Free Software Foundation, Inc.
|
2009-06-12 20:26:28 -04:00
|
|
|
|
2012-04-01 18:53:28 -04:00
|
|
|
;; Authors: Eric Schulte
|
|
|
|
;; Dan Davison
|
2009-06-12 20:26:28 -04:00
|
|
|
;; Keywords: literate programming, reproducible research
|
2021-09-26 03:44:29 -04:00
|
|
|
;; URL: https://orgmode.org
|
2009-06-12 20:26:28 -04:00
|
|
|
|
2010-06-25 12:20:39 -04:00
|
|
|
;; This file is part of GNU Emacs.
|
2009-06-12 20:26:28 -04:00
|
|
|
|
2010-06-25 12:20:39 -04:00
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
2009-06-12 20:26:28 -04:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2010-06-25 12:20:39 -04:00
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
2009-06-12 20:26:28 -04:00
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
2010-06-25 12:20:39 -04:00
|
|
|
|
2009-06-12 20:26:28 -04:00
|
|
|
;; You should have received a copy of the GNU General Public License
|
2017-09-13 18:52:52 -04:00
|
|
|
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
2009-06-12 20:26:28 -04:00
|
|
|
|
|
|
|
;;; Code:
|
2016-06-15 06:35:19 -04:00
|
|
|
(require 'cl-lib)
|
2012-12-11 18:58:16 -05:00
|
|
|
(require 'ob-core)
|
2010-06-11 19:02:42 -04:00
|
|
|
(require 'ob-table)
|
2009-06-14 10:54:55 -04:00
|
|
|
|
2016-06-16 18:24:12 -04:00
|
|
|
(declare-function org-babel-ref-split-args "ob-ref" (arg-string))
|
2022-01-06 12:39:05 -05:00
|
|
|
(declare-function org-element-at-point "org-element" (&optional pom cached-only))
|
2015-11-26 09:13:22 -05:00
|
|
|
(declare-function org-element-context "org-element" (&optional element))
|
|
|
|
(declare-function org-element-property "org-element" (property element))
|
|
|
|
(declare-function org-element-type "org-element" (element))
|
2011-11-15 10:38:41 -05:00
|
|
|
|
2009-06-14 17:10:04 -04:00
|
|
|
(defvar org-babel-library-of-babel nil
|
2010-07-13 19:20:08 -04:00
|
|
|
"Library of source-code blocks.
|
2016-06-20 08:44:44 -04:00
|
|
|
This is an association list. Populate the library by calling
|
|
|
|
`org-babel-lob-ingest' on files containing source blocks.")
|
2009-06-14 17:10:04 -04:00
|
|
|
|
2016-07-24 16:59:41 -04:00
|
|
|
(defvar org-babel-default-lob-header-args '((:exports . "results"))
|
|
|
|
"Default header arguments to use when exporting Babel calls.
|
|
|
|
By default, a Babel call inherits its arguments from the source
|
|
|
|
block being called. Header arguments defined in this variable
|
|
|
|
take precedence over these. It is useful for properties that
|
|
|
|
should not be inherited from a source block.")
|
2011-06-24 16:53:25 -04:00
|
|
|
|
2009-06-14 17:10:04 -04:00
|
|
|
(defun org-babel-lob-ingest (&optional file)
|
2014-01-03 11:15:34 -05:00
|
|
|
"Add all named source blocks defined in FILE to `org-babel-library-of-babel'."
|
2011-01-31 14:33:09 -05:00
|
|
|
(interactive "fFile: ")
|
2010-10-12 06:52:29 -04:00
|
|
|
(let ((lob-ingest-count 0))
|
|
|
|
(org-babel-map-src-blocks file
|
2016-07-24 17:26:43 -04:00
|
|
|
(let* ((info (org-babel-get-src-block-info 'light))
|
2010-10-12 06:52:29 -04:00
|
|
|
(source-name (nth 4 info)))
|
|
|
|
(when source-name
|
2017-09-28 17:04:54 -04:00
|
|
|
(setf (nth 1 info)
|
|
|
|
(if (org-babel-noweb-p (nth 2 info) :eval)
|
|
|
|
(org-babel-expand-noweb-references info)
|
|
|
|
(nth 1 info)))
|
|
|
|
(let ((source (intern source-name)))
|
|
|
|
(setq org-babel-library-of-babel
|
|
|
|
(cons (cons source info)
|
|
|
|
(assq-delete-all source org-babel-library-of-babel))))
|
|
|
|
(cl-incf lob-ingest-count))))
|
2018-09-20 05:23:49 -04:00
|
|
|
(message "%d source block%s added to Library of Babel"
|
2010-10-15 20:42:26 -04:00
|
|
|
lob-ingest-count (if (> lob-ingest-count 1) "s" ""))
|
|
|
|
lob-ingest-count))
|
2009-06-14 17:10:04 -04:00
|
|
|
|
2016-02-10 16:47:09 -05:00
|
|
|
;; Functions for executing lob one-liners.
|
2011-11-09 16:35:04 -05:00
|
|
|
|
2010-06-23 14:24:33 -04:00
|
|
|
;;;###autoload
|
2009-06-14 10:54:55 -04:00
|
|
|
(defun org-babel-lob-execute-maybe ()
|
2010-07-13 19:20:08 -04:00
|
|
|
"Execute a Library of Babel source block, if appropriate.
|
|
|
|
Detect if this is context for a Library Of Babel source block and
|
|
|
|
if so then run the appropriate source block from the Library."
|
2009-06-14 10:54:55 -04:00
|
|
|
(interactive)
|
|
|
|
(let ((info (org-babel-lob-get-info)))
|
2016-01-28 05:18:50 -05:00
|
|
|
(when info
|
2016-06-16 17:32:58 -04:00
|
|
|
(org-babel-execute-src-block nil info)
|
2016-01-28 05:18:50 -05:00
|
|
|
t)))
|
2009-06-14 10:54:55 -04:00
|
|
|
|
2018-01-22 13:27:13 -05:00
|
|
|
(defun org-babel-lob--src-info (ref)
|
|
|
|
"Return internal representation for Babel data referenced as REF.
|
|
|
|
REF is a string. This function looks into the current document
|
2016-06-16 16:16:41 -04:00
|
|
|
for a Babel call or source block. If none is found, it looks
|
2018-01-22 13:27:13 -05:00
|
|
|
after REF in the Library of Babel."
|
|
|
|
(let ((name ref)
|
|
|
|
(file nil))
|
|
|
|
;; Extract the remote file, if specified in the reference.
|
|
|
|
(when (string-match "\\`\\(.+\\):\\(.+\\)\\'" ref)
|
|
|
|
(setq file (match-string 1 ref))
|
|
|
|
(setq name (match-string 2 ref)))
|
|
|
|
;; During export, look into the pristine copy of the document
|
|
|
|
;; being exported instead of the current one, which could miss
|
|
|
|
;; some data.
|
|
|
|
(with-current-buffer (cond (file (find-file-noselect file t))
|
|
|
|
(org-babel-exp-reference-buffer)
|
|
|
|
(t (current-buffer)))
|
|
|
|
(org-with-point-at 1
|
|
|
|
(catch :found
|
|
|
|
(let ((case-fold-search t)
|
|
|
|
(regexp (org-babel-named-data-regexp-for-name name)))
|
|
|
|
(while (re-search-forward regexp nil t)
|
|
|
|
(let ((element (org-element-at-point)))
|
|
|
|
(when (equal name (org-element-property :name element))
|
|
|
|
(throw :found
|
|
|
|
(pcase (org-element-type element)
|
|
|
|
(`src-block (org-babel-get-src-block-info t element))
|
|
|
|
(`babel-call (org-babel-lob-get-info element))
|
|
|
|
;; Non-executable data found. Since names
|
|
|
|
;; are supposed to be unique throughout
|
|
|
|
;; a document, bail out.
|
|
|
|
(_ nil))))))
|
|
|
|
(cdr (assoc-string ref org-babel-library-of-babel))))))))
|
2016-06-16 16:16:41 -04:00
|
|
|
|
2010-06-23 14:24:33 -04:00
|
|
|
;;;###autoload
|
2016-01-28 05:18:50 -05:00
|
|
|
(defun org-babel-lob-get-info (&optional datum)
|
2016-06-16 16:16:41 -04:00
|
|
|
"Return internal representation for Library of Babel function call.
|
2018-01-22 13:27:13 -05:00
|
|
|
|
|
|
|
Consider DATUM, when provided, or element at point otherwise.
|
|
|
|
|
|
|
|
Return nil when not on an appropriate location. Otherwise return
|
|
|
|
a list compatible with `org-babel-get-src-block-info', which
|
|
|
|
see."
|
2016-02-10 18:32:07 -05:00
|
|
|
(let* ((context (or datum (org-element-context)))
|
2018-01-22 13:27:13 -05:00
|
|
|
(type (org-element-type context))
|
|
|
|
(reference (org-element-property :call context)))
|
2016-02-10 18:32:07 -05:00
|
|
|
(when (memq type '(babel-call inline-babel-call))
|
2018-01-22 13:27:13 -05:00
|
|
|
(pcase (org-babel-lob--src-info reference)
|
2016-08-28 05:45:39 -04:00
|
|
|
(`(,language ,body ,header ,_ ,_ ,_ ,coderef)
|
2016-06-16 16:16:41 -04:00
|
|
|
(let ((begin (org-element-property (if (eq type 'inline-babel-call)
|
|
|
|
:begin
|
|
|
|
:post-affiliated)
|
|
|
|
context)))
|
|
|
|
(list language
|
|
|
|
body
|
|
|
|
(apply #'org-babel-merge-params
|
|
|
|
header
|
2016-07-24 16:59:41 -04:00
|
|
|
org-babel-default-lob-header-args
|
2016-06-16 16:16:41 -04:00
|
|
|
(append
|
2017-12-23 09:56:53 -05:00
|
|
|
(org-with-point-at begin
|
|
|
|
(org-babel-params-from-properties language))
|
2016-06-16 16:16:41 -04:00
|
|
|
(list
|
|
|
|
(org-babel-parse-header-arguments
|
|
|
|
(org-element-property :inside-header context))
|
|
|
|
(let ((args (org-element-property :arguments context)))
|
|
|
|
(and args
|
|
|
|
(mapcar (lambda (ref) (cons :var ref))
|
|
|
|
(org-babel-ref-split-args args))))
|
|
|
|
(org-babel-parse-header-arguments
|
|
|
|
(org-element-property :end-header context)))))
|
|
|
|
nil
|
|
|
|
(org-element-property :name context)
|
2016-08-28 05:45:39 -04:00
|
|
|
begin
|
|
|
|
coderef)))
|
2016-06-16 16:16:41 -04:00
|
|
|
(_ nil)))))
|
|
|
|
|
2010-06-11 19:02:42 -04:00
|
|
|
(provide 'ob-lob)
|
2010-06-25 12:20:39 -04:00
|
|
|
|
2012-10-02 02:50:46 -04:00
|
|
|
;; Local variables:
|
|
|
|
;; generated-autoload-file: "org-loaddefs.el"
|
|
|
|
;; End:
|
2010-06-25 12:20:39 -04:00
|
|
|
|
2010-06-11 19:02:42 -04:00
|
|
|
;;; ob-lob.el ends here
|