0

Code:

(let ((parsetree (org-element-parse-buffer 'element)))
  ; find the appropriate src element and jump to it
  (org-element-map parsetree 'src-block
    (lambda (elem)
      (let ((name (org-element-property :name elem)))
        ; str is the name of the element, we are searching for
        (when (equal name str)
          (goto-char (org-element-property :begin elem))
          (org-reveal)))))) ; <- doesn't work

It jumps to the src element in the current buffer with name equal to str (used for literate programming). Unfortunately, sometimes the element is closed, so I have to open it manually. (org-reveal) doesn't work. Is there any function to open the src element elem?

1
  • 1
    Can you add to your question the version of Org mode you are using? M-x org-version will report that.
    – NickD
    Commented Apr 18, 2023 at 14:42

2 Answers 2

1

Here's a version of the function to unfold the source block at point that should work for Org mode versions before 9.5.4 (including 9.1.9):

#+begin_src elisp
  (defun my/org-show-block ()
    (let* ((e (org-element-at-point))
           (type (org-element-type e)))
      (when (eq type 'src-block)
        (org-hide-block-toggle 'off nil e))))
#+end_src

It should behave identically to the function below (which only works for version 9.5.4 and above).


Original answer

[Original answer starts here: it uses org-fold.el so it requires a fairly recent version of Org mode: 9.5.4 and above.]

Here's a little function to unfold the current element-at-point, given that it is a src-block:

#+begin_src elisp
  (defun my/org-show-block ()
    (let* ((e (org-element-at-point))
           (type (org-element-type e))
           (beg (org-element-property :begin e))
           (end (org-element-property :end e)))
      (when (eq type 'src-block)
          (org-fold-region beg end nil 'block))))
#+end_src

It's a no-op if the block is already unfolded.

EDIT: here's another version of this function that is very similar to the one at the top of the answer. It uses an org-fold function so that it will work in version 9.5.4 or later of Org mode:

 #+begin_src elisp
  (defun my/org-show-block ()
    (let* ((e (org-element-at-point))
           (type (org-element-type e)))
      (when (eq type 'src-block)
        (org-fold-hide-block-toggle 'off nil e))))
#+end_src

As you can see the only difference is the function name.

EDIT: It may be that you are using an old version of Org mode that does not have org-fold.el. org-fold is available in Org mode 9.5.4 and later. Emacs 28.2 currently comes with Org mode 9.6 I think, so that should work, but earlier version probably will not.

7
  • Symbol’s function definition is void: org-fold-region. What is it? I have fresh Emacs.
    – user4035
    Commented Apr 18, 2023 at 9:52
  • At home I have org mode 9.1.9. Will tell you the version at work as soon as I get there.
    – user4035
    Commented Apr 18, 2023 at 17:12
  • OK - that's too old : org-fold.el was added early last year. What version of emacs are you running? M-x emacs-version will tell.
    – NickD
    Commented Apr 18, 2023 at 17:14
  • GNU Emacs 26.3 of 2019-08-29. Trying to upgrade org-mode.
    – user4035
    Commented Apr 18, 2023 at 18:37
  • 1
    I installed fresh org-mode from the git repository. Your code works now. Thanks!
    – user4035
    Commented Apr 19, 2023 at 7:39
1

I guess something like this is what you want. This checks if there is invisible text at the end of the src line, and if so toggles the hiding.

(require 'ov)

(let ((parsetree (org-element-parse-buffer 'element)))
                    ; find the appropriate src element and jump to it
  (org-element-map parsetree 'src-block
    (lambda (elem)
      (let ((name (org-element-property :name elem)))
                    ; str is the name of the element, we are searching for
        (when (equal name "name")
          (goto-char (org-element-property :begin elem))
      (save-excursion
        (forward-line 1)
        (end-of-line)       
        (when (and (ov-at) (overlay-get (ov-at) 'invisible))
          (org-hide-block-toggle))))))))

You might try org-cycle instead, but it is less specific to src blocks.

2
  • Symbol’s function definition is void: ov-at. org-cycle may close an opened block, which shouldn't be done, so I decided not to use it.
    – user4035
    Commented Apr 17, 2023 at 11:55
  • you have to (require 'ov) I added that to the solution above. Commented Apr 18, 2023 at 0:34

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.