4

I have an org table with has a NAME label like:

#+NAME: TBL
| Foo | Bar |

In the past I have always been able to expand and collapse the whole table by cycling using TAB on the #+NAME line.

However in the latest version of emacs (26.1), this doesn't seem to work.

If I replace #+NAME: TBL with #+RESULTS: TBL it works fine. Pressing TAB on the RESULTS line hides the table, pressing it again reveals the table once more.

Is there an org setting to define which which blocks can be expanded/folded, or some other requirement I'm missing?

Thanks

  • Would you consider using drawers and keeping your tables inside them? – dangom Dec 2 '18 at 11:34
1

I don't know if the foldable elements are documented elsewhere, but the "folding" is done in the functions org-hide-block-toggle-maybe and org-hide-block-toggle (the former just calls the latter and ignores errors). The list is as follows:

(center-block comment-block dynamic-block example-block
             export-block quote-block special-block
             src-block verse-block)

Instead of using #+RESULT for this (which is specifically meant for results of source blocks), I would use an example block:

#+BEGIN_EXAMPLE
#+NAME: TBL
| Foo | Bar |
#+END_EXAMPLE

I tried to find an org version that actually folds #+NAME (or #+TABLE) elements and I couldn't come up with one, although I went back five years to version 8.1 (of course, I could have made a mistake). Are you sure that this was ever the case? If so, what version of org-mode (M-x org-version will tell) was that?

UPDATE: org-hide-block-toggle is used for blocks, but not for results. Results are handled through the org-tab-first-hook variable: ob-core.el adds org-babel-hide-result-toggle-maybe to the hook and that is what folds/unfolds results. AFAICT however, that is not used for #+NAME folding - still looking.

| improve this answer | |
  • Thanks for this - strangely the version it works on is "Org-mode version 8.2.10 (release_8.2.10 @ /usr/share/emacs/24.5/lisp/org/)" - I suspect it's the default Org for Ubuntu 16.04. Which suggests that there is some setting being pulled in from elsewhere. I'll double check all my settings again. – Phil Oct 2 '18 at 8:39
  • Interestingly on my version of 8.2 M-x org-hide-block-toggle returns Not looking at a source block for both NAME and RESULTS. In both cases TAB expands and folds. – Phil Oct 2 '18 at 8:53
  • Final test - If I run as emacs -q and thus load no plugins or config, the feature still exists in Ubuntu 16.04's build of emacs 24.5.1/Org 8.2.10, but not in my source build of 26.1/Org 9.1.9. So it looks like it was a core change to emacs or org rather than config or settings? – Phil Oct 2 '18 at 9:04
  • ... or I made a mistake. I'll check again when I have some time. – NickD Oct 2 '18 at 12:15
  • "Interestingly on my version of 8.2 M-x org-hide-block-toggle returns Not looking at a source block for both NAME and RESULTS. In both cases TAB expands and folds" - you are right: see UPDATE. – NickD Oct 2 '18 at 16:17
0

I am also concerned with the change of the behaviour. I don't remember the emacs & org versions but I used to set a header like #+TBLNAME which was able to expand and collapse. It was terrificly useful as long as I was learning with tables and using them a lot.

Following your tests I have figured out something like #+begin_table and it seems to work pretty good, you can expand and collapse over #+begin. You can even set a header #+name: and it also works expand&collapse over #+name with the table above it.

#+name: number-prototypes
#+begin_table
 |-------------------+------+--------|
 | Propose           | Date | Number |
 |-------------------+------+--------|
 | Researcher        | 2011 |      5 |
 | Researcher 2 & 3  | 2019 |     13 |
 |-------------------+------+--------|
#+end_table

UPDATE: By the way it doesn't work for literate programming :-/

Feedback is welcome ;-)

| improve this answer | |
0

The following Elisp code lets you toggle the visibility of the element following the #+NAME: line with the TAB-key. Note that a table is a special element. Therefore, the code also works for tables.

(defun org+-at-keyword-line-p (name)
  "Return non-nil if point is in a line with #+NAME: keyword.
Therefore, NAME stands for the string argument NAME, not for the Org keyword.
The return value is actually the first non-space sequence after #+NAME:"
  (save-excursion
    (goto-char (line-beginning-position))
    (and (looking-at (concat "^[[:blank:]]*#\\+" name ":[[:blank:]]*\\([^[:space:]]+\\)?"))
     (or (match-string 1) ""))))

(defun org+-hide-named-paragraph-toggle (&optional force)
  "Toggle the visibility of named paragraphs.
If FORCE is 'off make paragraph visible.
If FORCE is otherwise non-nil make paragraph invisible.
Otherwise toggle the visibility."
  (interactive "P")
  (when (org+-at-keyword-line-p "NAME")
    (save-excursion
      (forward-line)
      (let* ((par (org-element-at-point))
         (start (org-element-property :contents-begin par))
         (end (org-element-property :contents-end par))
         (post (org-element-property :post-affiliated par)))
    (cond ((eq force 'off)
           (org-flag-region start end nil 'org-hide-block))
          (force
           (org-flag-region start end t 'org-hide-block))
          ((eq (get-char-property start 'invisible) 'org-hide-block)
           (org-flag-region start end nil 'org-hide-block))
          (t
           (org-flag-region start end t 'org-hide-block)))
    ;; When the block is hidden away, make sure point is left in
    ;; a visible part of the buffer.
    (when (invisible-p (max (1- (point)) (point-min)))
      (goto-char post))
    ;; Signal success.
    t))))

(add-hook 'org-tab-after-check-for-cycling-hook #'org+-hide-named-paragraph-toggle)

Tested with Emacs 26.3 and org-version 9.2.6.

| improve this answer | |

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.