emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
blob 2bcee3947a06b81932222232f8fe8615930b6629 6514 bytes (raw)
name: lisp/ob-lob.el 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
 
;;; ob-lob.el --- Functions Supporting the Library of Babel -*- lexical-binding: t; -*-

;; Copyright (C) 2009-2023 Free Software Foundation, Inc.

;; Authors: Eric Schulte
;;	 Dan Davison
;; Keywords: literate programming, reproducible research
;; URL: https://orgmode.org

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; 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,
;; 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.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Code:

(require 'org-macs)
(unless (version< emacs-version "29")
  (org-assert-version))

(require 'cl-lib)
(require 'ob-core)
(require 'ob-table)

(declare-function org-babel-ref-split-args "ob-ref" (arg-string))
(declare-function org-element-at-point "org-element" (&optional pom cached-only))
(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))

(defvar org-babel-library-of-babel nil
  "Library of source-code blocks.
This is an association list.  Populate the library by calling
`org-babel-lob-ingest' on files containing source blocks.")

(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.")

(defun org-babel-lob-ingest (&optional file)
  "Add all named source blocks defined in FILE to `org-babel-library-of-babel'."
  (interactive "fFile: ")
  (let ((lob-ingest-count 0))
    (org-babel-map-src-blocks file
      (let* ((info (org-babel-get-src-block-info 'no-eval))
	     (source-name (nth 4 info)))
	(when source-name
	  (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))))
    (message "%d source block%s added to Library of Babel"
	     lob-ingest-count (if (> lob-ingest-count 1) "s" ""))
    lob-ingest-count))

;; Functions for executing lob one-liners.

;;;###autoload
(defun org-babel-lob-execute-maybe ()
  "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."
  (interactive)
  (let* ((datum (org-element-context))
         (info (org-babel-lob-get-info datum)))
    (when info
      (org-babel-execute-src-block nil info nil (org-element-type datum))
      t)))

(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
for a Babel call or source block.  If none is found, it looks
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))))))))

;;;###autoload
(defun org-babel-lob-get-info (&optional datum no-eval)
  "Return internal representation for Library of Babel function call.

Consider DATUM, when provided, or element at point otherwise.

When optional argument NO-EVAL is non-nil, Babel does not resolve
remote variable references; a process which could likely result
in the execution of other code blocks, and do not evaluate Lisp
values in parameters.

Return nil when not on an appropriate location.  Otherwise return
a list compatible with `org-babel-get-src-block-info', which
see."
  (let* ((context (or datum (org-element-context)))
	 (type (org-element-type context))
	 (reference (org-element-property :call context)))
    (when (memq type '(babel-call inline-babel-call))
      (pcase (org-babel-lob--src-info reference)
	(`(,language ,body ,header ,_ ,_ ,_ ,coderef)
	 (let ((begin (org-element-property (if (eq type 'inline-babel-call)
						:begin
					      :post-affiliated)
					    context)))
	   (list language
		 body
		 (apply #'org-babel-merge-params
			header
			org-babel-default-lob-header-args
			(append
			 (org-with-point-at begin
			   (org-babel-params-from-properties language no-eval))
			 (list
			  (org-babel-parse-header-arguments
			   (org-element-property :inside-header context) no-eval)
			  (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) no-eval))))
		 nil
		 (org-element-property :name context)
		 begin
		 coderef)))
	(_ nil)))))

(provide 'ob-lob)

;; Local variables:
;; generated-autoload-file: "org-loaddefs.el"
;; End:

;;; ob-lob.el ends here

debug log:

solving 2bcee3947 ...
found 2bcee3947 in https://yhetil.org/orgmode/87o7obn0dp.fsf@localhost/
found f77bda73d in https://git.savannah.gnu.org/cgit/emacs/org-mode.git
preparing index
index prepared:
100644 f77bda73dd62fb8812eef132f8d4ef71b4753620	lisp/ob-lob.el

applying [1/1] https://yhetil.org/orgmode/87o7obn0dp.fsf@localhost/
diff --git a/lisp/ob-lob.el b/lisp/ob-lob.el
index f77bda73d..2bcee3947 100644

Checking patch lisp/ob-lob.el...
Applied patch lisp/ob-lob.el cleanly.

index at:
100644 2bcee3947a06b81932222232f8fe8615930b6629	lisp/ob-lob.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).