0

If my cursor is on <<globals>>, how can I open the org-src block similar to org-open-at-point.

#+begin_src shell :noweb yes :tangle ~/file.txt
<<globals>>
<<aliases>>
#+end_src
2
  • Note that more than one block can be given the same noweb-ref if you want it to only be one block the use the NAME property
    – mmmmmm
    Commented Sep 27, 2022 at 11:56
  • emacs.stackexchange.com/tags/elisp/info
    – Drew
    Commented Sep 27, 2022 at 19:07

2 Answers 2

2

I would recommend that you name your source blocks with #+name: foo e.g.:

#+name: globals
#+begin_src elisp
globals
#+end_src

#+name: aliases
#+begin_src elisp
aliases
#+end_src

#+name: bar
#+begin_src shell :noweb yes :tangle ~/file.txt
<<globals>>
<<aliases>>
#+end_src

You can then use org-babel-goto-named-source-block, perhaps in a wrapper to implement the -at-point functionality, although that seems to work fine out of the box: the default choice is the name that you are currently on.

1
  • Thank you for pointing out org-babel-goto-named-source-block.
    – anakha
    Commented Sep 27, 2022 at 15:11
1

@NickD's answer is more standards compliant given that it utilizes orgmode's internal mechanism. Meanwhile, I had also put together a function that will visit the noweb-ref without the necessity of naming the src block.

(defun mb/try-open-noweb-at-point ()
  "Try to visit the noweb-ref source at point."
  (interactive)
  (let ((thing (thing-at-point 'symbol 't))
        (found nil))
    (catch 'foo
      (org-babel-map-src-blocks nil
        (if-let* ((args (nth 2 (org-babel-get-src-block-info t)))
                  (noweb-ref (alist-get :noweb-ref args)))
            (when (equal thing noweb-ref)
              (org-show-set-visibility 'minimal)
              (with-current-buffer (buffer-name)
                (goto-char beg-body)
                (throw 'foo (setq found t)))
              t))))
    (if (equal found nil)
        (message "not found"))))

2
  • Your solution assumes that each relevant code block is basically named with a :noweb-ref. That's not the purpose of the :noweb-ref header (see Noweb reference syntax for an example of its usage). Nevertheless, +1 for the use of org-babel-map-src-blocks and in particular for beg-body which I had forgotten about and had to look for in the doc string of the function :-)
    – NickD
    Commented Sep 27, 2022 at 19:53
  • I believe you can avoid calling org-babel-get-src-block-info: the header args are available inside org-babel-map-src-blocks as the value of header-args (untested).
    – NickD
    Commented Sep 27, 2022 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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