Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
2  
The feature exists (see gnu.org/software/auctex/manual/auctex/Folding.html ), but it seems to require some tweaking in order to work on braces (instead of the macro itself). –  T. Verron May 31 '13 at 13:26
1  
Thanks @T.Verron, yes I read a little about that. As you say, the machinery is all in place, we just needs an emacs wiz to swoop in... –  John Wickerson May 31 '13 at 13:41
1  
Also, if your use-case is specifically a file with lots of \newcommands, you may find documentation about emacs tags interesting: gnu.org/software/emacs/manual/html_node/emacs/Tags.html –  T. Verron May 31 '13 at 14:00
add comment

1 Answer 1

up vote 23 down vote accepted
+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.

share|improve this answer
    
+1. Can you please elaborate on -- You can bind this function to your favorite key binding ? Also, how do we unfold things? –  Harish Kumar Jun 1 '13 at 0:38
    
@Harish I'm on mobile, but describe-function local-set-key. –  Sean Allred Jun 1 '13 at 3:00
    
@HarishKumar I think something like (global-set-key (kbd "C-c p") 'mg-TeX-fold-brace) would do the trick. –  John Wickerson Jun 1 '13 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 '13 at 8:36
    
Thanks, giordano –  Harish Kumar Jun 1 '13 at 12:56
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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