Function: org-babel-where-is-src-block-result

org-babel-where-is-src-block-result is a byte-compiled function defined in ob-core.el.gz.

Signature

(org-babel-where-is-src-block-result &optional INSERT INFO HASH)

Documentation

Find where the current source block results begin.

Return the point at the beginning of the result of the current source block, specifically at the beginning of the results line.

If no result exists for this block return nil, unless optional argument INSERT is non-nil. In this case, create a results line following the source block and return the position at its beginning. In the case of inline code, remove the results part instead.

If optional argument HASH is a string, remove contents related to RESULTS keyword if its hash is different. Then update the latter to HASH.

Source Code

;; Defined in /usr/src/emacs/lisp/org/ob-core.el.gz
(defun org-babel-where-is-src-block-result (&optional insert _info hash)
  "Find where the current source block results begin.

Return the point at the beginning of the result of the current
source block, specifically at the beginning of the results line.

If no result exists for this block return nil, unless optional
argument INSERT is non-nil.  In this case, create a results line
following the source block and return the position at its
beginning.  In the case of inline code, remove the results part
instead.

If optional argument HASH is a string, remove contents related to
RESULTS keyword if its hash is different.  Then update the latter
to HASH."
  (let ((context (org-element-context)))
    (catch :found
      (org-with-wide-buffer
       (pcase (org-element-type context)
	 ((or `inline-babel-call `inline-src-block)
	  ;; Results for inline objects are located right after them.
	  ;; There is no RESULTS line to insert either.
	  (let ((limit (or (org-element-contents-end (org-element-parent context))
                           (org-element-end (org-element-parent context)))))
	    (goto-char (org-element-end context))
	    (skip-chars-forward " \t\n" limit)
	    (throw :found
		   (and
		    (< (point) limit)
		    (let ((result (org-element-context)))
		      (and (org-element-type-p result 'macro)
			   (string= (org-element-property :key result)
				    "results")
			   (if (not insert) (point)
			     (delete-region
			      (point)
			      (progn
				(goto-char (org-element-end result))
				(skip-chars-backward " \t")
				(point)))
			     (point))))))))
	 ((or `babel-call `src-block)
	  (let* ((name (org-element-property :name context))
		 (named-results (and name (org-babel-find-named-result name))))
	    (goto-char (or named-results (org-element-end context)))
	    (cond
	     ;; Existing results named after the current source.
	     (named-results
	      (when (org-babel--clear-results-maybe hash)
		(org-babel--insert-results-keyword name hash))
	      (throw :found (point)))
	     ;; Named results expect but none to be found.
	     (name)
	     ;; No possible anonymous results at the very end of
	     ;; buffer or outside CONTEXT parent.
	     ((eq (point)
		  (or (pcase (org-element-type (org-element-parent context))
                        ((or `section `org-data)
                         (org-element-end (org-element-parent context)))
                        (_ (org-element-contents-end
                            (org-element-parent context))))
		      (point-max))))
	     ;; Check if next element is an anonymous result below
	     ;; the current block.
	     ((let* ((next (org-element-at-point))
		     (end (save-excursion
			    (goto-char
			     (org-element-post-affiliated next))
			    (line-end-position)))
		     (empty-result-re (concat org-babel-result-regexp "$"))
		     (case-fold-search t))
		(re-search-forward empty-result-re end t))
	      (forward-line 0)
	      (when (org-babel--clear-results-maybe hash)
		(org-babel--insert-results-keyword nil hash))
	      (throw :found (point))))))
	 ;; Ignore other elements.
	 (_ (throw :found nil))))
      ;; No result found.  Insert a RESULTS keyword below element, if
      ;; appropriate.  In this case, ensure there is an empty line
      ;; after the previous element.
      (when insert
	(save-excursion
	  (goto-char (min (org-element-end context) (point-max)))
	  (skip-chars-backward " \t\n")
	  (forward-line)
	  (unless (bolp) (insert "\n"))
	  (insert "\n")
	  (org-babel--insert-results-keyword
	   (org-element-property :name context) hash)
	  (point))))))