26

I have some code looking like this:

142  \newcommand*\john[2]{
143    blah blah blah blah
144    blah blah
145    blah blah blah
146    blah
147  }
148
149  \newcommand*\peter[3]{
150    blah blah
151  }

It would be great if I could double-click (or ctrl-click, or whatever) on the { on line 142, or the } on line 147, to make the definition of \john "fold up", like so:

142  \newcommand*\john[2]{ ... }
148
149  \newcommand*\peter[3]{
150    blah blah
151  }

Does (or could) this feature exist? It would be rather handy when working with large files.

3

2 Answers 2

25
+50

This should work (requires AUCTeX and you first need to enable TeX-fold-mode with C-c C-o C-f or M-x TeX-fold-mode)

(defun mg-TeX-fold-brace ()
  "Hide the group in which point currently is located with \"{...}\"."
  (interactive)
  (let ((opening-brace (TeX-find-opening-brace))
    (closing-brace (TeX-find-closing-brace))
    priority ov)
    (if (and opening-brace closing-brace)
    (progn
      (setq priority (TeX-overlay-prioritize opening-brace closing-brace))
      (setq ov (make-overlay opening-brace closing-brace
                 (current-buffer) t nil))
      (overlay-put ov 'category 'TeX-fold)
      (overlay-put ov 'priority priority)
      (overlay-put ov 'evaporate t)
      (overlay-put ov 'TeX-fold-display-string-spec "{...}")
      (TeX-fold-hide-item ov))
      (message "No group found"))))

;; Bind the function to C-c C-o p
(eval-after-load "tex-fold"
  '(define-key TeX-fold-keymap "p" 'mg-TeX-fold-brace))

enter image description here

Point must be placed between braces, braces excluded. I've almost copy-pasted TeX-fold-make-overlay defined in tex-fold.el. You can call this function with M-x mg-TeX-fold-brace or bind it to your favorite key binding. I used C-c C-o p in the example, TeX-fold-mode automatically prefixes the defined key with C-c C-o.

To automatically unfold braces move point between them, instead to permanently show the braces use C-c C-o i or M-x TeX-fold-clearout-item.

Starting from this function it's possible to write a function for folding whatever you want. What you need is to find a way for searching the points where folding starts and ends. In this case I used the AUCTeX functions TeX-find-{opening,closing}-brace for finding the two braces. In the line

(overlay-put ov 'TeX-fold-display-string-spec "{...}")

you can set the string with which the folded region will be replaced.

7
  • +1. Can you please elaborate on -- You can bind this function to your favorite key binding ? Also, how do we unfold things?
    – user11232
    Jun 1, 2013 at 0:38
  • @Harish I'm on mobile, but describe-function local-set-key. Jun 1, 2013 at 3:00
  • @HarishKumar I think something like (global-set-key (kbd "C-c p") 'mg-TeX-fold-brace) would do the trick. Jun 1, 2013 at 5:27
  • @HarishKumar I've added instructions on how to bind that function to a key binding in TeX-mode and how to unfold braces. @JohnWickerson I prefer to bind this function to the AUCTeX specific key map ;-).
    – giordano
    Jun 1, 2013 at 8:36
  • This works really well. Thanks so much giordano! Jun 3, 2013 at 9:18
2

Giordano solution works perfectly but the fold will be gone after the buffer killed. Each time you want to fold, you need to run the function again.

I have another solution to automatically fold everything you want. I use this function with comments. Example:

% Why I write this paragraph in latex \begin{fold}
% comment
% another comment
% \end{fold}

or You want to fold paragraph or other

% Paragraph that explains about A \begin{fold}
A is a letter. Another sentence. End of paragraph.
% \end{fold}

When nyou run this function. M-x latex-fold-foo It will fold everythig that \begin{fold} \end{fold}

The result like this

% Why I write this paragraph in latex [fold]
% Paragraph that explains about A [fold]

Each time you move your cursor to the folded paragraph, It will open it temporarily. If you want to remove the fold, use C-c C-o i

(defun latex-fold-foo ()
  (interactive)
    (save-excursion
      (goto-char (point-min))
      (while (search-forward (format "begin{fold}") nil t)
        (TeX-fold-env))))

My workflow is

  1. Open the buffer
  2. Run the function to automatically fold everything
  3. C-c C-o i to delete the fold if u want to edit it and see it permanently

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .