2015-10-25 20:56:00 -04:00
|
|
|
;;; ob-ref.el --- Babel Functions for Referencing External Data -*- lexical-binding: t; -*-
|
2009-03-23 19:29:31 -04:00
|
|
|
|
2023-01-01 05:31:12 -05:00
|
|
|
;; Copyright (C) 2009-2023 Free Software Foundation, Inc.
|
2009-03-23 19:29:31 -04:00
|
|
|
|
2012-04-01 18:53:28 -04:00
|
|
|
;; Authors: Eric Schulte
|
|
|
|
;; Dan Davison
|
2009-03-23 19:29:31 -04:00
|
|
|
;; Keywords: literate programming, reproducible research
|
2021-09-26 03:44:29 -04:00
|
|
|
;; URL: https://orgmode.org
|
2009-03-23 19:29:31 -04:00
|
|
|
|
2010-06-25 12:20:39 -04:00
|
|
|
;; This file is part of GNU Emacs.
|
2009-03-23 19:29:31 -04:00
|
|
|
|
2010-06-25 12:20:39 -04:00
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
2009-03-23 19:29:31 -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-03-23 19:29:31 -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-03-23 19:29:31 -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-03-23 19:29:31 -04:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; Functions for referencing data from the header arguments of a
|
2009-05-24 16:38:01 -04:00
|
|
|
;; org-babel block. The syntax of such a reference should be
|
2010-06-25 12:20:39 -04:00
|
|
|
|
2009-03-23 20:01:11 -04:00
|
|
|
;; #+VAR: variable-name=file:resource-id
|
2010-06-25 12:20:39 -04:00
|
|
|
|
2009-03-23 19:29:31 -04:00
|
|
|
;; - variable-name :: the name of the variable to which the value
|
|
|
|
;; will be assigned
|
2010-06-25 12:20:39 -04:00
|
|
|
|
2009-03-23 19:29:31 -04:00
|
|
|
;; - file :: path to the file containing the resource, or omitted if
|
|
|
|
;; resource is in the current file
|
2010-06-25 12:20:39 -04:00
|
|
|
|
2009-05-09 21:28:08 -04:00
|
|
|
;; - resource-id :: the id or name of the resource
|
2010-06-25 12:20:39 -04:00
|
|
|
|
2018-09-20 05:23:49 -04:00
|
|
|
;; So an example of a simple source block referencing table data in
|
|
|
|
;; the same file would be
|
2010-06-25 12:20:39 -04:00
|
|
|
|
2013-04-09 16:09:00 -04:00
|
|
|
;; #+NAME: sandbox
|
2010-07-14 20:59:43 -04:00
|
|
|
;; | 1 | 2 | 3 |
|
2009-05-24 16:38:01 -04:00
|
|
|
;; | 4 | org-babel | 6 |
|
2009-05-09 21:28:08 -04:00
|
|
|
;;
|
|
|
|
;; #+begin_src emacs-lisp :var table=sandbox
|
2010-06-25 12:20:39 -04:00
|
|
|
;; (message table)
|
2009-05-09 21:28:08 -04:00
|
|
|
;; #+end_src
|
2009-03-23 19:29:31 -04:00
|
|
|
|
|
|
|
;;; Code:
|
2022-08-04 09:53:05 -04:00
|
|
|
|
|
|
|
(require 'org-macs)
|
|
|
|
(org-assert-version)
|
|
|
|
|
2012-12-11 18:58:16 -05:00
|
|
|
(require 'ob-core)
|
2018-05-09 20:04:12 -04:00
|
|
|
(require 'org-macs)
|
2015-11-06 07:04:20 -05:00
|
|
|
(require 'cl-lib)
|
2009-03-23 19:29:31 -04:00
|
|
|
|
2022-10-18 11:27:54 -04:00
|
|
|
(declare-function org-babel-lob-get-info "ob-lob" (&optional datum no-eval))
|
2022-01-06 12:39:05 -05:00
|
|
|
(declare-function org-element-at-point "org-element" (&optional pom cached-only))
|
2023-05-16 06:41:53 -04:00
|
|
|
(declare-function org-element-property "org-element-ast" (property node))
|
2023-05-03 10:26:41 -04:00
|
|
|
(declare-function org-element-post-affiliated "org-element" (node))
|
2023-05-16 06:41:53 -04:00
|
|
|
(declare-function org-element-type "org-element-ast" (node &optional anonymous))
|
2015-03-31 15:50:43 -04:00
|
|
|
(declare-function org-end-of-meta-data "org" (&optional full))
|
|
|
|
(declare-function org-find-property "org" (property &optional value))
|
2012-10-02 05:19:29 -04:00
|
|
|
(declare-function org-id-find-id-file "org-id" (id))
|
2015-11-05 09:44:22 -05:00
|
|
|
(declare-function org-id-find-id-in-file "org-id" (id file &optional markerp))
|
|
|
|
(declare-function org-in-commented-heading-p "org" (&optional no-inheritance))
|
2021-10-16 12:01:56 -04:00
|
|
|
(declare-function org-narrow-to-subtree "org" (&optional element))
|
2022-01-16 02:07:25 -05:00
|
|
|
(declare-function org-fold-show-context "org-fold" (&optional key))
|
2010-06-23 14:24:33 -04:00
|
|
|
|
2011-12-12 12:04:15 -05:00
|
|
|
(defvar org-babel-update-intermediate nil
|
2011-11-20 15:19:01 -05:00
|
|
|
"Update the in-buffer results of code blocks executed to resolve references.")
|
|
|
|
|
2010-10-16 14:14:20 -04:00
|
|
|
(defun org-babel-ref-parse (assignment)
|
2010-07-13 19:20:08 -04:00
|
|
|
"Parse a variable ASSIGNMENT in a header argument.
|
2017-12-14 17:57:50 -05:00
|
|
|
|
2010-07-13 19:20:08 -04:00
|
|
|
If the right hand side of the assignment has a literal value
|
2017-12-14 17:57:50 -05:00
|
|
|
return that value, otherwise interpret it as a reference to an
|
|
|
|
external resource and find its value using `org-babel-ref-resolve'.
|
|
|
|
|
|
|
|
Return a list with two elements: the name of the variable, and an
|
|
|
|
Emacs Lisp representation of the value of the variable."
|
|
|
|
(when (string-match "\\(.+?\\)=" assignment)
|
|
|
|
(let ((var (org-trim (match-string 1 assignment)))
|
|
|
|
(ref (org-trim (substring assignment (match-end 0)))))
|
2010-10-16 14:14:20 -04:00
|
|
|
(cons (intern var)
|
2013-06-25 09:55:32 -04:00
|
|
|
(let ((out (save-excursion
|
|
|
|
(when org-babel-current-src-block-location
|
2013-11-12 15:55:53 -05:00
|
|
|
(goto-char (if (markerp org-babel-current-src-block-location)
|
|
|
|
(marker-position org-babel-current-src-block-location)
|
|
|
|
org-babel-current-src-block-location)))
|
2013-06-25 09:55:32 -04:00
|
|
|
(org-babel-read ref))))
|
2010-11-07 09:39:05 -05:00
|
|
|
(if (equal out ref)
|
2016-08-24 11:05:54 -04:00
|
|
|
(if (and (string-prefix-p "\"" ref)
|
|
|
|
(string-suffix-p "\"" ref))
|
2010-11-07 09:39:05 -05:00
|
|
|
(read ref)
|
|
|
|
(org-babel-ref-resolve ref))
|
|
|
|
out))))))
|
2009-05-09 20:44:52 -04:00
|
|
|
|
2011-06-28 16:02:01 -04:00
|
|
|
(defun org-babel-ref-goto-headline-id (id)
|
2015-03-31 15:50:43 -04:00
|
|
|
(or (let ((h (org-find-property "CUSTOM_ID" id)))
|
|
|
|
(when h (goto-char h)))
|
|
|
|
(let* ((file (org-id-find-id-file id))
|
|
|
|
(m (when file (org-id-find-id-in-file id file 'marker))))
|
|
|
|
(when (and file m)
|
|
|
|
(message "file:%S" file)
|
2016-06-23 04:00:00 -04:00
|
|
|
(pop-to-buffer-same-window (marker-buffer m))
|
2015-03-31 15:50:43 -04:00
|
|
|
(goto-char m)
|
|
|
|
(move-marker m nil)
|
2022-01-16 02:07:25 -05:00
|
|
|
(org-fold-show-context)
|
2015-03-31 15:50:43 -04:00
|
|
|
t))))
|
2011-06-28 16:02:01 -04:00
|
|
|
|
|
|
|
(defun org-babel-ref-headline-body ()
|
|
|
|
(save-restriction
|
|
|
|
(org-narrow-to-subtree)
|
|
|
|
(buffer-substring
|
|
|
|
(save-excursion (goto-char (point-min))
|
2015-03-31 15:50:43 -04:00
|
|
|
(org-end-of-meta-data)
|
2011-06-28 16:02:01 -04:00
|
|
|
(point))
|
|
|
|
(point-max))))
|
|
|
|
|
2010-06-12 21:49:02 -04:00
|
|
|
(defvar org-babel-library-of-babel)
|
2010-10-16 14:14:20 -04:00
|
|
|
(defun org-babel-ref-resolve (ref)
|
2010-06-11 20:12:15 -04:00
|
|
|
"Resolve the reference REF and return its value."
|
2011-01-31 14:31:17 -05:00
|
|
|
(save-window-excursion
|
2014-03-14 10:27:32 -04:00
|
|
|
(with-current-buffer (or org-babel-exp-reference-buffer (current-buffer))
|
|
|
|
(save-excursion
|
|
|
|
(let ((case-fold-search t)
|
2015-11-05 09:44:22 -05:00
|
|
|
args new-refere new-header-args new-referent split-file split-ref
|
2022-08-13 14:49:27 -04:00
|
|
|
index contents)
|
2014-03-14 10:27:32 -04:00
|
|
|
;; if ref is indexed grab the indices -- beware nested indices
|
2022-08-13 14:49:27 -04:00
|
|
|
(when (and (string-match "\\[\\([^\\[]*\\)\\]$" ref)
|
2014-03-14 10:27:32 -04:00
|
|
|
(let ((str (substring ref 0 (match-beginning 0))))
|
2017-05-06 13:16:49 -04:00
|
|
|
(= (cl-count ?\( str) (cl-count ?\) str))))
|
2022-08-13 14:49:27 -04:00
|
|
|
(if (> (length (match-string 1 ref)) 0)
|
|
|
|
(setq index (match-string 1 ref))
|
|
|
|
(setq contents t))
|
2014-03-14 10:27:32 -04:00
|
|
|
(setq ref (substring ref 0 (match-beginning 0))))
|
|
|
|
;; assign any arguments to pass to source block
|
|
|
|
(when (string-match
|
2015-09-17 19:08:20 -04:00
|
|
|
"^\\(.+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)(\\(.*\\))$" ref)
|
2014-03-14 10:27:32 -04:00
|
|
|
(setq new-refere (match-string 1 ref))
|
|
|
|
(setq new-header-args (match-string 3 ref))
|
|
|
|
(setq new-referent (match-string 5 ref))
|
|
|
|
(when (> (length new-refere) 0)
|
|
|
|
(when (> (length new-referent) 0)
|
|
|
|
(setq args (mapcar (lambda (ref) (cons :var ref))
|
|
|
|
(org-babel-ref-split-args new-referent))))
|
|
|
|
(when (> (length new-header-args) 0)
|
|
|
|
(setq args (append (org-babel-parse-header-arguments
|
2020-02-18 16:57:37 -05:00
|
|
|
new-header-args)
|
|
|
|
args)))
|
2014-03-14 10:27:32 -04:00
|
|
|
(setq ref new-refere)))
|
|
|
|
(when (string-match "^\\(.+\\):\\(.+\\)$" ref)
|
|
|
|
(setq split-file (match-string 1 ref))
|
|
|
|
(setq split-ref (match-string 2 ref))
|
2023-10-21 06:08:55 -04:00
|
|
|
(when (file-exists-p split-file)
|
|
|
|
(find-file split-file)
|
|
|
|
(setq ref split-ref)))
|
2015-11-05 09:44:22 -05:00
|
|
|
(org-with-wide-buffer
|
|
|
|
(goto-char (point-min))
|
2022-07-21 06:54:30 -04:00
|
|
|
(let* ((params (append args '((:results . "none"))))
|
2015-11-05 09:44:22 -05:00
|
|
|
(regexp (org-babel-named-data-regexp-for-name ref))
|
|
|
|
(result
|
2015-11-07 15:09:34 -05:00
|
|
|
(catch :found
|
|
|
|
;; Check for code blocks or named data.
|
|
|
|
(while (re-search-forward regexp nil t)
|
|
|
|
;; Ignore COMMENTed headings and orphaned
|
|
|
|
;; affiliated keywords.
|
|
|
|
(unless (org-in-commented-heading-p)
|
|
|
|
(let ((e (org-element-at-point)))
|
|
|
|
(when (equal (org-element-property :name e) ref)
|
|
|
|
(goto-char
|
2023-05-03 10:26:41 -04:00
|
|
|
(org-element-post-affiliated e))
|
2015-11-07 15:09:34 -05:00
|
|
|
(pcase (org-element-type e)
|
|
|
|
(`babel-call
|
|
|
|
(throw :found
|
2016-06-16 17:32:58 -04:00
|
|
|
(org-babel-execute-src-block
|
2016-06-16 17:44:53 -04:00
|
|
|
nil (org-babel-lob-get-info e) params)))
|
2022-08-13 14:49:27 -04:00
|
|
|
((and `src-block (guard (not contents)))
|
2015-11-07 15:09:34 -05:00
|
|
|
(throw :found
|
|
|
|
(org-babel-execute-src-block
|
|
|
|
nil nil
|
|
|
|
(and
|
|
|
|
(not org-babel-update-intermediate)
|
|
|
|
params))))
|
|
|
|
((and (let v (org-babel-read-element e))
|
|
|
|
(guard v))
|
|
|
|
(throw :found v))
|
|
|
|
(_ (error "Reference not found")))))))
|
|
|
|
;; Check for local or global headlines by ID.
|
|
|
|
(when (org-babel-ref-goto-headline-id ref)
|
|
|
|
(throw :found (org-babel-ref-headline-body)))
|
|
|
|
;; Check the Library of Babel.
|
2015-11-07 17:06:21 -05:00
|
|
|
(let ((info (cdr (assq (intern ref)
|
|
|
|
org-babel-library-of-babel))))
|
|
|
|
(when info
|
|
|
|
(throw :found
|
|
|
|
(org-babel-execute-src-block nil info params))))
|
|
|
|
(error "Reference `%s' not found in this buffer" ref))))
|
2015-11-05 09:44:22 -05:00
|
|
|
(cond
|
2022-10-06 07:11:06 -04:00
|
|
|
((and result (symbolp result)) (format "%S" result))
|
2015-11-05 09:44:22 -05:00
|
|
|
((and index (listp result))
|
|
|
|
(org-babel-ref-index-list index result))
|
|
|
|
(t result)))))))))
|
2009-11-13 22:26:44 -05:00
|
|
|
|
|
|
|
(defun org-babel-ref-index-list (index lis)
|
2010-07-13 19:20:08 -04:00
|
|
|
"Return the subset of LIS indexed by INDEX.
|
2010-07-14 14:01:57 -04:00
|
|
|
|
|
|
|
Indices are 0 based and negative indices count from the end of
|
|
|
|
LIS, so 0 references the first element of LIS and -1 references
|
|
|
|
the last. If INDEX is separated by \",\"s then each \"portion\"
|
|
|
|
is assumed to index into the next deepest nesting or dimension.
|
|
|
|
|
|
|
|
A valid \"portion\" can consist of either an integer index, two
|
|
|
|
integers separated by a \":\" in which case the entire range is
|
|
|
|
returned, or an empty string or \"*\" both of which are
|
|
|
|
interpreted to mean the entire range and as such are equivalent
|
|
|
|
to \"0:-1\"."
|
|
|
|
(if (and (> (length index) 0) (string-match "^\\([^,]*\\),?" index))
|
2015-09-17 19:08:20 -04:00
|
|
|
(let* ((ind-re "\\(\\([-[:digit:]]+\\):\\([-[:digit:]]+\\)\\|\\*\\)")
|
2012-08-09 10:13:18 -04:00
|
|
|
(lgth (length lis))
|
|
|
|
(portion (match-string 1 index))
|
|
|
|
(remainder (substring index (match-end 0)))
|
|
|
|
(wrap (lambda (num) (if (< num 0) (+ lgth num) num)))
|
|
|
|
(open (lambda (ls) (if (and (listp ls) (= (length ls) 1)) (car ls) ls))))
|
|
|
|
(funcall
|
|
|
|
open
|
|
|
|
(mapcar
|
|
|
|
(lambda (sub-lis)
|
|
|
|
(if (listp sub-lis)
|
|
|
|
(org-babel-ref-index-list remainder sub-lis)
|
|
|
|
sub-lis))
|
|
|
|
(if (or (= 0 (length portion)) (string-match ind-re portion))
|
|
|
|
(mapcar
|
|
|
|
(lambda (n) (nth n lis))
|
|
|
|
(apply 'org-number-sequence
|
|
|
|
(if (and (> (length portion) 0) (match-string 2 portion))
|
|
|
|
(list
|
|
|
|
(funcall wrap (string-to-number (match-string 2 portion)))
|
|
|
|
(funcall wrap (string-to-number (match-string 3 portion))))
|
|
|
|
(list (funcall wrap 0) (funcall wrap -1)))))
|
2012-08-15 18:27:35 -04:00
|
|
|
(list (nth (funcall wrap (string-to-number portion)) lis))))))
|
|
|
|
lis))
|
2009-03-23 20:01:11 -04:00
|
|
|
|
2009-07-22 18:04:19 -04:00
|
|
|
(defun org-babel-ref-split-args (arg-string)
|
|
|
|
"Split ARG-STRING into top-level arguments of balanced parenthesis."
|
2016-06-21 09:24:06 -04:00
|
|
|
(mapcar #'org-trim (org-babel-balanced-split arg-string 44)))
|
2009-07-22 18:04:19 -04:00
|
|
|
|
2010-06-11 19:02:42 -04:00
|
|
|
(provide 'ob-ref)
|
2010-06-25 12:20:39 -04:00
|
|
|
|
2010-06-11 19:02:42 -04:00
|
|
|
;;; ob-ref.el ends here
|