org-mode/lisp/ob-ref.el

255 lines
9.4 KiB
EmacsLisp
Raw Normal View History

Activate lexical binding in some libraries * lisp/ob-C.el (org-babel-prep-session:C): (org-babel-load-session:C): * lisp/ob-J.el: (org-babel-expand-body:J): (org-babel-execute:J): * lisp/ob-R.el: (org-babel-expand-body:R): * lisp/ob-abc.el: (org-babel-execute:abc): (org-babel-prep-session:abc): * lisp/ob-asymptote.el: (org-babel-execute:asymptote): (org-babel-prep-session:asymptote): * lisp/ob-awk.el: (org-babel-expand-body:awk): * lisp/ob-calc.el: (org-babel-expand-body:calc): * lisp/ob-clojure.el: * lisp/ob-comint.el: (org-babel-comint-in-buffer): (org-babel-comint-with-output): (org-babel-comint-eval-invisibly-and-wait-for-file): * lisp/ob-coq.el: * lisp/ob-css.el: (org-babel-execute:css): (org-babel-prep-session:css): * lisp/ob-ditaa.el: (org-babel-execute:ditaa): (org-babel-prep-session:ditaa): * lisp/ob-dot.el: (org-babel-execute:dot): (org-babel-prep-session:dot): * lisp/ob-ebnf.el: * lisp/ob-emacs-lisp.el: * lisp/ob-eval.el: * lisp/ob-forth.el: * lisp/ob-fortran.el: (org-babel-execute:fortran): (org-babel-prep-session:fortran): (org-babel-load-session:fortran): * lisp/ob-gnuplot.el: (org-babel-expand-body:gnuplot): (org-babel-prep-session:gnuplot): (org-babel-gnuplot-initiate-session): * lisp/ob-groovy.el: (org-babel-prep-session:groovy): (org-babel-groovy-initiate-session): * lisp/ob-haskell.el: (org-babel-haskell-initiate-session): * lisp/ob-io.el: (org-babel-prep-session:io): (org-babel-io-initiate-session): * lisp/ob-java.el: (org-babel-execute:java): * lisp/ob-js.el: * lisp/ob-keys.el: * lisp/ob-latex.el: (org-babel-prep-session:latex): * lisp/ob-ledger.el: (org-babel-execute:ledger): (org-babel-prep-session:ledger): * lisp/ob-lilypond.el: (org-babel-lilypond-commands): (org-babel-lilypond-process-basic): (org-babel-prep-session:lilypond): (org-babel-lilypond-parse-line-num): * lisp/ob-lisp.el: * lisp/ob-makefile.el: (org-babel-execute:makefile): (org-babel-prep-session:makefile): * lisp/ob-matlab.el: * lisp/ob-maxima.el: (org-babel-prep-session:maxima): * lisp/ob-mscgen.el: (org-babel-prep-session:mscgen): * lisp/ob-ocaml.el: (org-babel-execute:ocaml): (org-babel-prep-session:ocaml): * lisp/ob-octave.el: (org-babel-execute:octave): (org-babel-octave-initiate-session): * lisp/ob-org.el: (org-babel-prep-session:org): * lisp/ob-perl.el: (org-babel-prep-session:perl): (org-babel-perl--var-to-perl): (org-babel-perl-initiate-session): * lisp/ob-picolisp.el: (org-babel-expand-body:picolisp): (org-babel-execute:picolisp): * lisp/ob-plantuml.el: (org-babel-execute:plantuml): (org-babel-prep-session:plantuml): * lisp/ob-processing.el: (org-babel-prep-session:processing): * lisp/ob-python.el: (org-babel-python-initiate-session): * lisp/ob-ref.el: (org-babel-ref-resolve): * lisp/ob-ruby.el: (org-babel-ruby-initiate-session): * lisp/ob-sass.el: (org-babel-execute:sass): (org-babel-prep-session:sass): * lisp/ob-scala.el: (org-babel-execute:scala): (org-babel-prep-session:scala): (org-babel-scala-initiate-session): * lisp/ob-scheme.el: * lisp/ob-screen.el: (org-babel-prep-session:screen): (org-babel-screen-session-write-temp-file): (org-babel-screen-test): * lisp/ob.el: * lisp/org-colview.el: (org-columns-todo): (org-columns-set-tags-or-toggle): (org-columns-new): (org-columns-uncompile-format): (org-agenda-colview-summarize): * lisp/org-footnote.el: (electric-indent-mode): * lisp/org-indent.el: (org-indent-refresh-maybe): * lisp/org-list.el: * lisp/org-macro.el: (org-macro--collect-macros): * lisp/org-src.el: * lisp/org-table.el: (sort-fold-case): (org-table-create): (org-table-field-info): (org-table-transpose-table-at-point): (org-table-remove-rectangle-highlight): (orgtbl-create-or-convert-from-region): (org-define-lookup-function): * lisp/ox-ascii.el: (org-ascii-format-drawer-function): (org-ascii--has-caption-p): (org-ascii-bold): (org-ascii-center-block): (org-ascii-clock): (org-ascii-code): (org-ascii-dynamic-block): (org-ascii-entity): (org-ascii-example-block): (org-ascii-export-snippet): (org-ascii-export-block): (org-ascii-fixed-width): (org-ascii-footnote-reference): (org-ascii-horizontal-rule): (org-ascii-inline-src-block): (org-ascii-format-inlinetask-default): (org-ascii-italic): (org-ascii-keyword): (org-ascii-latex-environment): (org-ascii-latex-fragment): (org-ascii-line-break): (org-ascii-node-property): (org-ascii-planning): (org-ascii-quote-block): (org-ascii-radio-target): (org-ascii-special-block): (org-ascii-src-block): (org-ascii-statistics-cookie): (org-ascii-subscript): (org-ascii-superscript): (org-ascii-strike-through): (org-ascii-timestamp): (org-ascii-underline): (org-ascii-verbatim): (org-ascii-verse-block): (org-ascii-filter-headline-blank-lines): (org-ascii-filter-paragraph-spacing): (org-ascii-filter-comment-spacing): Use lexical binding.
2015-10-25 20:56:00 -04:00
;;; ob-ref.el --- Babel Functions for Referencing External Data -*- lexical-binding: t; -*-
;; Copyright (C) 2009-2023 Free Software Foundation, Inc.
2012-04-01 18:53:28 -04:00
;; 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/>.
;;; 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
;; #+VAR: variable-name=file:resource-id
;; - variable-name :: the name of the variable to which the value
;; will be assigned
;; - file :: path to the file containing the resource, or omitted if
;; resource is in the current file
2009-05-09 21:28:08 -04:00
;; - resource-id :: the id or name of the resource
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
;; #+NAME: sandbox
;; | 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
;; (message table)
2009-05-09 21:28:08 -04:00
;; #+end_src
;;; Code:
(require 'org-macs)
(org-assert-version)
2012-12-11 18:58:16 -05:00
(require 'ob-core)
(require 'org-macs)
(require 'cl-lib)
(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))
(declare-function org-element-property "org-element-ast" (property node))
(declare-function org-element-post-affiliated "org-element" (node))
(declare-function org-element-type "org-element-ast" (node &optional anonymous))
(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))
(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))
(declare-function org-narrow-to-subtree "org" (&optional element))
(declare-function org-fold-show-context "org-fold" (&optional key))
2010-06-23 14:24:33 -04:00
(defvar org-babel-update-intermediate nil
"Update the in-buffer results of code blocks executed to resolve references.")
(defun org-babel-ref-parse (assignment)
2010-07-13 19:20:08 -04:00
"Parse a variable ASSIGNMENT in a header argument.
2010-07-13 19:20:08 -04:00
If the right hand side of the assignment has a literal value
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)))))
(cons (intern var)
(let ((out (save-excursion
(when org-babel-current-src-block-location
(goto-char (if (markerp org-babel-current-src-block-location)
(marker-position org-babel-current-src-block-location)
org-babel-current-src-block-location)))
(org-babel-read ref))))
(if (equal out ref)
(if (and (string-prefix-p "\"" ref)
(string-suffix-p "\"" ref))
(read ref)
(org-babel-ref-resolve ref))
out))))))
(defun org-babel-ref-goto-headline-id (id)
(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)
(pop-to-buffer-same-window (marker-buffer m))
(goto-char m)
(move-marker m nil)
(org-fold-show-context)
t))))
(defun org-babel-ref-headline-body ()
(save-restriction
(org-narrow-to-subtree)
(buffer-substring
(save-excursion (goto-char (point-min))
(org-end-of-meta-data)
(point))
(point-max))))
(defvar org-babel-library-of-babel)
(defun org-babel-ref-resolve (ref)
"Resolve the reference REF and return its value."
(save-window-excursion
ob-ref: Properly resolve references in ":post" arguments * lisp/ob-core.el (org-babel-exp-reference-buffer): New variable, as a replacement for `org-current-export-file'. (org-babel-check-confirm-evaluate): Use new variable. * lisp/ob-exp.el (org-babel-exp-in-export-file): Use new variable. (org-babel-exp-get-export-buffer): Remove function. (org-babel-exp-process-buffer): Change signature. * lisp/ob-ref.el (org-babel-ref-resolve): Use new variable during export in order to properly resolve references. * lisp/ox.el (org-export-execute-babel-code): Use new variable. * contrib/lisp/org-wikinodes.el (org-wikinodes-process-links-for-export): Remove a cond branch as it is always false (`org-current-export-file' couldn't be a string). * testing/lisp/test-ob-lob.el (test-ob-lob/export-lob-lines): Update test. * testing/lisp/test-ob.el (test-ob/eval-header-argument): Update test. * testing/lisp/test-ob-exp.el (ob-export/reference-in-post-header): New test. During export, Babel executes sequentially all blocks in the buffer being exported. This can lead to modifications preventing some references from being resolved. As a workaround, Babel stores a pristine copy of the buffer in a variable so it can always find needed references. Before this patch, the variable storing this copy was `org-current-export-file' and was dynamically bound in "ox.el". It was used to resolve noweb references (`org-babel-expand-noweb-references') but not regular references (`org-babel-ref-resolve'). Now, the variable is `org-babel-exp-reference-buffer' and it is bound from `org-babel-exp-process-buffer'. It is used to resolve all references. In particular, this allows to use references in :post header. Thanks to Jarmo Hurri for reporting it.
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)
args new-refere new-header-args new-referent split-file split-ref
index contents)
ob-ref: Properly resolve references in ":post" arguments * lisp/ob-core.el (org-babel-exp-reference-buffer): New variable, as a replacement for `org-current-export-file'. (org-babel-check-confirm-evaluate): Use new variable. * lisp/ob-exp.el (org-babel-exp-in-export-file): Use new variable. (org-babel-exp-get-export-buffer): Remove function. (org-babel-exp-process-buffer): Change signature. * lisp/ob-ref.el (org-babel-ref-resolve): Use new variable during export in order to properly resolve references. * lisp/ox.el (org-export-execute-babel-code): Use new variable. * contrib/lisp/org-wikinodes.el (org-wikinodes-process-links-for-export): Remove a cond branch as it is always false (`org-current-export-file' couldn't be a string). * testing/lisp/test-ob-lob.el (test-ob-lob/export-lob-lines): Update test. * testing/lisp/test-ob.el (test-ob/eval-header-argument): Update test. * testing/lisp/test-ob-exp.el (ob-export/reference-in-post-header): New test. During export, Babel executes sequentially all blocks in the buffer being exported. This can lead to modifications preventing some references from being resolved. As a workaround, Babel stores a pristine copy of the buffer in a variable so it can always find needed references. Before this patch, the variable storing this copy was `org-current-export-file' and was dynamically bound in "ox.el". It was used to resolve noweb references (`org-babel-expand-noweb-references') but not regular references (`org-babel-ref-resolve'). Now, the variable is `org-babel-exp-reference-buffer' and it is bound from `org-babel-exp-process-buffer'. It is used to resolve all references. In particular, this allows to use references in :post header. Thanks to Jarmo Hurri for reporting it.
2014-03-14 10:27:32 -04:00
;; if ref is indexed grab the indices -- beware nested indices
(when (and (string-match "\\[\\([^\\[]*\\)\\]$" ref)
ob-ref: Properly resolve references in ":post" arguments * lisp/ob-core.el (org-babel-exp-reference-buffer): New variable, as a replacement for `org-current-export-file'. (org-babel-check-confirm-evaluate): Use new variable. * lisp/ob-exp.el (org-babel-exp-in-export-file): Use new variable. (org-babel-exp-get-export-buffer): Remove function. (org-babel-exp-process-buffer): Change signature. * lisp/ob-ref.el (org-babel-ref-resolve): Use new variable during export in order to properly resolve references. * lisp/ox.el (org-export-execute-babel-code): Use new variable. * contrib/lisp/org-wikinodes.el (org-wikinodes-process-links-for-export): Remove a cond branch as it is always false (`org-current-export-file' couldn't be a string). * testing/lisp/test-ob-lob.el (test-ob-lob/export-lob-lines): Update test. * testing/lisp/test-ob.el (test-ob/eval-header-argument): Update test. * testing/lisp/test-ob-exp.el (ob-export/reference-in-post-header): New test. During export, Babel executes sequentially all blocks in the buffer being exported. This can lead to modifications preventing some references from being resolved. As a workaround, Babel stores a pristine copy of the buffer in a variable so it can always find needed references. Before this patch, the variable storing this copy was `org-current-export-file' and was dynamically bound in "ox.el". It was used to resolve noweb references (`org-babel-expand-noweb-references') but not regular references (`org-babel-ref-resolve'). Now, the variable is `org-babel-exp-reference-buffer' and it is bound from `org-babel-exp-process-buffer'. It is used to resolve all references. In particular, this allows to use references in :post header. Thanks to Jarmo Hurri for reporting it.
2014-03-14 10:27:32 -04:00
(let ((str (substring ref 0 (match-beginning 0))))
(= (cl-count ?\( str) (cl-count ?\) str))))
(if (> (length (match-string 1 ref)) 0)
(setq index (match-string 1 ref))
(setq contents t))
ob-ref: Properly resolve references in ":post" arguments * lisp/ob-core.el (org-babel-exp-reference-buffer): New variable, as a replacement for `org-current-export-file'. (org-babel-check-confirm-evaluate): Use new variable. * lisp/ob-exp.el (org-babel-exp-in-export-file): Use new variable. (org-babel-exp-get-export-buffer): Remove function. (org-babel-exp-process-buffer): Change signature. * lisp/ob-ref.el (org-babel-ref-resolve): Use new variable during export in order to properly resolve references. * lisp/ox.el (org-export-execute-babel-code): Use new variable. * contrib/lisp/org-wikinodes.el (org-wikinodes-process-links-for-export): Remove a cond branch as it is always false (`org-current-export-file' couldn't be a string). * testing/lisp/test-ob-lob.el (test-ob-lob/export-lob-lines): Update test. * testing/lisp/test-ob.el (test-ob/eval-header-argument): Update test. * testing/lisp/test-ob-exp.el (ob-export/reference-in-post-header): New test. During export, Babel executes sequentially all blocks in the buffer being exported. This can lead to modifications preventing some references from being resolved. As a workaround, Babel stores a pristine copy of the buffer in a variable so it can always find needed references. Before this patch, the variable storing this copy was `org-current-export-file' and was dynamically bound in "ox.el". It was used to resolve noweb references (`org-babel-expand-noweb-references') but not regular references (`org-babel-ref-resolve'). Now, the variable is `org-babel-exp-reference-buffer' and it is bound from `org-babel-exp-process-buffer'. It is used to resolve all references. In particular, this allows to use references in :post header. Thanks to Jarmo Hurri for reporting it.
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
"^\\(.+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)(\\(.*\\))$" ref)
ob-ref: Properly resolve references in ":post" arguments * lisp/ob-core.el (org-babel-exp-reference-buffer): New variable, as a replacement for `org-current-export-file'. (org-babel-check-confirm-evaluate): Use new variable. * lisp/ob-exp.el (org-babel-exp-in-export-file): Use new variable. (org-babel-exp-get-export-buffer): Remove function. (org-babel-exp-process-buffer): Change signature. * lisp/ob-ref.el (org-babel-ref-resolve): Use new variable during export in order to properly resolve references. * lisp/ox.el (org-export-execute-babel-code): Use new variable. * contrib/lisp/org-wikinodes.el (org-wikinodes-process-links-for-export): Remove a cond branch as it is always false (`org-current-export-file' couldn't be a string). * testing/lisp/test-ob-lob.el (test-ob-lob/export-lob-lines): Update test. * testing/lisp/test-ob.el (test-ob/eval-header-argument): Update test. * testing/lisp/test-ob-exp.el (ob-export/reference-in-post-header): New test. During export, Babel executes sequentially all blocks in the buffer being exported. This can lead to modifications preventing some references from being resolved. As a workaround, Babel stores a pristine copy of the buffer in a variable so it can always find needed references. Before this patch, the variable storing this copy was `org-current-export-file' and was dynamically bound in "ox.el". It was used to resolve noweb references (`org-babel-expand-noweb-references') but not regular references (`org-babel-ref-resolve'). Now, the variable is `org-babel-exp-reference-buffer' and it is bound from `org-babel-exp-process-buffer'. It is used to resolve all references. In particular, this allows to use references in :post header. Thanks to Jarmo Hurri for reporting it.
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
Tiny formatting fixes * lisp/ox.el (org-export-table-dimensions): * lisp/ox-texinfo.el (org-texinfo-template): * lisp/ox-md.el (org-md-link): * lisp/ox-icalendar.el (org-icalendar-use-UTC-date-time-p): * lisp/ox-ascii.el (org-ascii-fixed-width): * lisp/org.el (org-context): * lisp/org-table.el (org-table-eval-formula) (org-table-export): * lisp/org-refile.el: * lisp/org-plot.el (org-plot/gnuplot-to-grid-data): * lisp/org-num.el (org-num): * lisp/org-mouse.el (org-mouse-popup-global-menu) (org-mouse-context-menu): * lisp/org-macro.el (org-macro): * lisp/org-lint.el (org-lint): * lisp/org-keys.el (org-keys): * lisp/org-duration.el: * lisp/org-clock.el (org-clock-get-last-clock-out-time) (org-clock-update-mode-line, org-find-open-clocks): * lisp/org-agenda.el (org-diary) (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item) (org-agenda-highlight-todo, org-cmp-alpha) (org-agenda-filter-by-category): * lisp/ol.el (org-link-expand-abbrev, ol): * lisp/ol-docview.el (ol-docview): * lisp/ol-bibtex.el (org-execute-file-search-in-bibtex) (org-bibtex, org-bibtex-read): * lisp/ol-bbdb.el (org-bbdb-anniversary-description): * lisp/ob-tangle.el (org-babel-tangle-jump-to-org): * lisp/ob-table.el (org-babel-table-truncate-at-newline): * lisp/ob-stan.el: * lisp/ob-sqlite.el (org-babel-sqlite-table-or-scalar): * lisp/ob-sql.el: * lisp/ob-shen.el: * lisp/ob-shell.el (org-babel-prep-session:shell) (org-babel-prep-session:shell): * lisp/ob-sed.el (org-babel-execute:sed) (org-babel-execute:sed): * lisp/ob-screen.el: * lisp/ob-sass.el: * lisp/ob-ruby.el (org-babel-prep-session:ruby) (org-babel-prep-session:ruby): * lisp/ob-ref.el (org-babel-ref-resolve, ob-ref): * lisp/ob-python.el (org-babel-prep-session:python) (org-babel-prep-session:python): * lisp/ob-plantuml.el: * lisp/ob-picolisp.el: * lisp/ob-perl.el: * lisp/ob-org.el: * lisp/ob-octave.el (org-babel-prep-session:octave) (org-babel-prep-session:octave) (org-babel-octave-evaluate-session): * lisp/ob-ocaml.el: * lisp/ob-mscgen.el (org-babel-execute:mscgen) (org-babel-execute:mscgen): * lisp/ob-maxima.el: (ob-maxima): * lisp/ob-matlab.el: * lisp/ob-makefile.el: * lisp/ob-lua.el (org-babel-prep-session:lua) (org-babel-prep-session:lua): * lisp/ob-lisp.el: * lisp/ob-ledger.el: * lisp/ob-latex.el (org-babel-expand-body:latex) (org-babel-expand-body:latex, ob-latex): * lisp/ob-js.el: * lisp/ob-java.el: * lisp/ob-io.el (org-babel-prep-session:io) (org-babel-prep-session:io): * lisp/ob-hledger.el: * lisp/ob-haskell.el: * lisp/ob-groovy.el (org-babel-groovy-wrapper-method) (org-babel-groovy-evaluate): * lisp/ob-gnuplot.el: * lisp/ob-fortran.el (org-babel-expand-body:fortran) (org-babel-expand-body:fortran): * lisp/ob-forth.el (org-babel-forth-session-execute): * lisp/ob-exp.el (ob-exp): * lisp/ob-eval.el: * lisp/ob-emacs-lisp.el: * lisp/ob-ebnf.el: * lisp/ob-dot.el: * lisp/ob-ditaa.el: * lisp/ob-css.el: * lisp/ob-core.el (org-babel-put-rownames): * lisp/ob-coq.el: * lisp/ob-comint.el: * lisp/ob-calc.el: * lisp/ob-awk.el: * lisp/ob-asymptote.el: * lisp/ob-abc.el: * lisp/ob-R.el (org-babel-prep-session:R): Formatting fixes.
2020-02-18 16:57:37 -05:00
new-header-args)
args)))
ob-ref: Properly resolve references in ":post" arguments * lisp/ob-core.el (org-babel-exp-reference-buffer): New variable, as a replacement for `org-current-export-file'. (org-babel-check-confirm-evaluate): Use new variable. * lisp/ob-exp.el (org-babel-exp-in-export-file): Use new variable. (org-babel-exp-get-export-buffer): Remove function. (org-babel-exp-process-buffer): Change signature. * lisp/ob-ref.el (org-babel-ref-resolve): Use new variable during export in order to properly resolve references. * lisp/ox.el (org-export-execute-babel-code): Use new variable. * contrib/lisp/org-wikinodes.el (org-wikinodes-process-links-for-export): Remove a cond branch as it is always false (`org-current-export-file' couldn't be a string). * testing/lisp/test-ob-lob.el (test-ob-lob/export-lob-lines): Update test. * testing/lisp/test-ob.el (test-ob/eval-header-argument): Update test. * testing/lisp/test-ob-exp.el (ob-export/reference-in-post-header): New test. During export, Babel executes sequentially all blocks in the buffer being exported. This can lead to modifications preventing some references from being resolved. As a workaround, Babel stores a pristine copy of the buffer in a variable so it can always find needed references. Before this patch, the variable storing this copy was `org-current-export-file' and was dynamically bound in "ox.el". It was used to resolve noweb references (`org-babel-expand-noweb-references') but not regular references (`org-babel-ref-resolve'). Now, the variable is `org-babel-exp-reference-buffer' and it is bound from `org-babel-exp-process-buffer'. It is used to resolve all references. In particular, this allows to use references in :post header. Thanks to Jarmo Hurri for reporting it.
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))
(when (file-exists-p split-file)
(find-file split-file)
(setq ref split-ref)))
(org-with-wide-buffer
(goto-char (point-min))
(let* ((params (append args '((:results . "none"))))
(regexp (org-babel-named-data-regexp-for-name ref))
(result
(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
(org-element-post-affiliated e))
(pcase (org-element-type e)
(`babel-call
(throw :found
(org-babel-execute-src-block
nil (org-babel-lob-get-info e) params)))
((and `src-block (guard (not contents)))
(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.
(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))))
(cond
((and result (symbolp result)) (format "%S" result))
((and index (listp result))
(org-babel-ref-index-list index result))
(t result)))))))))
(defun org-babel-ref-index-list (index lis)
2010-07-13 19:20:08 -04:00
"Return the subset of LIS indexed by INDEX.
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))
(let* ((ind-re "\\(\\([-[:digit:]]+\\):\\([-[:digit:]]+\\)\\|\\*\\)")
(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)))))
(list (nth (funcall wrap (string-to-number portion)) lis))))))
lis))
(defun org-babel-ref-split-args (arg-string)
"Split ARG-STRING into top-level arguments of balanced parenthesis."
(mapcar #'org-trim (org-babel-balanced-split arg-string 44)))
(provide 'ob-ref)
;;; ob-ref.el ends here