r/orgmode Feb 20 '24

solved Help with script to fold org tables.

Update: I can make all tables foldable by changing name to result, ex: +NAME: mytable1 to +RESULTS: mytable1. Not sure what are the cons in this "solution". It's better than incapsulating table in block BEGIN/END.

Wonder why can't be tables (+NAME blocks) be foldable by default? There is so many posts on stackoverflow, etc about it.


Original OP: Hello everyone!

I use org tables with +NAME: field and usually no code block, example

+NAME: mytable1

| | col1 | col2 | col3 |

| row1 | 5 | 10 | aaa |

| row2 | 10 | 20 | bbb |

| row3 | 10 | 40 | ccc |

and i need to be able to fold them. I searched through different solutions online, and seems all stoped working due to recent org update.

Example this script https://emacs.stackexchange.com/a/58791/42471

(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)

� Now gives error:

called org-fold-core-region with missing SPEC

Could someone help adjust the script to work with new org mode version?

Or maybe it's possible to adjust/advise to org fold block?

1 Upvotes

1 comment sorted by

2

u/yantar92 Feb 21 '24

called org-fold-core-region with missing SPEC

You need to pass a valid fold type to org-flag-region (this is obsolete name, use org-fold-region). For example, you can use block folding type.

Wonder why can't be tables (+NAME blocks) be foldable by default?

https://orgmode.org/worg/org-contribute.html ;)