Function: org-babel-ref-resolve

org-babel-ref-resolve is a byte-compiled function defined in ob-ref.el.gz.

Signature

(org-babel-ref-resolve REF)

Documentation

Resolve the reference REF and return its value.

Source Code

;; Defined in /usr/src/emacs/lisp/org/ob-ref.el.gz
(defun org-babel-ref-resolve (ref)
  "Resolve the reference REF and return its value."
  (save-window-excursion
    (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)
	  ;; if ref is indexed grab the indices -- beware nested indices
	  (when (and (string-match "\\[\\([^\\[]*\\)\\]$" ref)
		     (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))
	    (setq ref (substring ref 0 (match-beginning 0))))
	  ;; assign any arguments to pass to source block
	  (when (string-match
		 "^\\(.+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)(\\(.*\\))$" ref)
	    (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
				    new-header-args)
				   args)))
	      (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)))))))))