2

I want to call (recode-region) on sh code block results in :post header attribute. So it needs START and END values. Is there a way to define of RESULTS block content boundaries for it ?

2

You can use org-element-parse-buffer to obtain an abstract syntax tree (AST) of your org buffer. Search that AST for the node representing the results of interest by org-element-map. You get the beginning and the end of the region with the results of interest from the node by (org-element-property :contents-begin node) and (org-element-property :contents-end node). All addressed functions are well documented and also described in the worg wiki.

I demonstrate the outlined procedure by a documented example org-mode file below:

* The source code block with the results of interest
  #+NAME: resultsOfInterest
  #+BEGIN_SRC emacs-lisp
'((1 2 3) (4 5 6))
  #+END_SRC

  #+RESULTS: resultsOfInterest
  | 1 | 2 | 3 |
  | 4 | 5 | 6 |

* The source code block for the analysis
  #+BEGIN_SRC emacs-lisp :results drawer
;; (org-src-debug) ;;< Remove first comment starters to use the debugging code from https://emacs.stackexchange.com/questions/13244/edebug-orgmode-source-code-blocks-with-input-variables
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; The following wrapper enables us to run the code
;; - by evaluating the source code block within the org-file and also
;; - by evaluating the form in the source edit buffer.
(with-current-buffer (or (and (org-src-edit-buffer-p) (org-src--source-buffer))
             (current-buffer))
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;; The actual code starts here:
  (when-let ((ast (org-element-parse-buffer)) ;; built up the abstract syntax tree of the org buffer
         (node (org-element-map
               ast
               'table ;;< We assume here that the results of interest are generated as table. Adapt to your case.
             (lambda (node)
               "Predicate for identifying the result of interest in the ast"
               (when (equal (org-element-property :results node) '("resultsOfInterest")) ;; Results of interest are marked by #+RESULTS: "resultsOfInterest".
             node))
             nil
             'first-match ;;< With 'first-match `org-element-map' works similar to `cl-find-if'.
             ))
         (beg (org-element-property :contents-begin node)) ;< Beginning of region with results of interest 
         (end (org-element-property :contents-end node))) ;< End of region with results of interest
    ;; Action on the results of interest:
    ;; We just output the region as result of this source block.
    (buffer-substring beg end)))
  #+END_SRC

  #+RESULTS:
  :RESULTS:
    | 1 | 2 | 3 |
    | 4 | 5 | 6 |
  :END:
0

With point in a source block, the following snippet of code will return the start and end of the results as (RESULTS_START . RESTULTS_END):

(unless (org-in-src-block-p)
    (error "Not in a source block"))
(let* ((src (org-element-context))
       (results-start (org-babel-where-is-src-block-result))
       (results-end
        (when results-start
          (save-excursion
            (goto-char results-start)
            (goto-char (org-babel-result-end))
            (point)))))
  ;; return the region as (RESULTS_START . RESULTS_END)
  `(,results-start . ,results_end))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.