Categories

Author

Quick and dirty code folding

January 16, 2007 — jao

Code folding, the ability to hide code or text blocks in source or text files, is a feature present in most editors nowadays. Emacs has several modes providing this functionality with various degrees of sophistication: outline-mode, allout, hide-show or folding-mode come to mind. One can even use Emacs narrowing capabilities to cook up custom folding.

Over the years, i’ve used several of these packages (and perhaps i’ll share some tips on them in future postings). But i’ve noticed that, at the end of the day, i have usually little use for folding. I strive for short functions (whenever a function body extends a half-page, i feel a strong refactoring urge), and as a consequence i seldom need to hide their bodies, nor to mention blocks within them.

There is however one folding trick that i use all the time. I often want to hide all function bodies in a file, to get a quick summary of it contents. As it happens, you don’t need any package or extra configuration for that. Just go to any source file, type

We can use Emacs’ help system to discover what’s going on: C-h k C-x $ tells us that the above key combination is calling set-selective-display, a function that takes one numerical argument (the M-1 prefix passes 1 as the value of that argument) and, unsurprisingly, sets the variable selective-display to the value of that argument. I’ll let you read about the details in Emacs help itself, although i’m sure that by now you get the idea.

If you find yourself setting and unsetting selective-display as often as i do, next thing will be defining a handy keyboard shortcut, right?

Like this:

Related

28 Responses to “Quick and dirty code folding”

Excellent post! I don’t know how to use folding but this is one useful trick! Thanks also for listing the various “folding” modes. I keep forgetting that everybody brings their own “rock” to the “soup”.

Thanks; this is really useful! I have played with hs-minor-mode in the past, but it was pretty complicated; this is exactly what I would use.

There is one thing that hs-minor-mode can do that this can’t though; reveal-mode. This will temporarily expand a given hidden block when the point enters the “…” markers, and it will hide it again when the point leaves the function. Do you know if this works with C-x $?

Anyway, this blog is a gold mine; I love it. Makes me feel bad about not posting my own useful snippets.

One thing that I misses is a hide comment mode.
With all these hiding modes out there it is strange that it does not exist.
When reading code with two much comments it becomes difficult to read the code.

Great tip. However, my mission in life is to get my emacs to work like CodeWright. I like emacs, but there are a few features of CodeWrite that I just can’t emulate. One of them is the ability do display all lines containing the word at point. I’ve found an “occur-at-point” which does what I want, but leaves me in the occur buffer. I never considered that CodeWright might be performing some kind of selective display or folding instead of “occur”.

Set-selective-display works on indentation, right? Is there some way to fold on a regexp?

But i’ve noticed that, at the end of the day, i have usually little use for folding. I strive for short functions (whenever a function body extends a half-page, i feel a strong refactoring urge), and as a consequence i seldom need to hide their bodies, nor to mention blocks within them.

well aren’t you mr. lucky ducky. i have to work with people who include 20-line function bodies WITH the declaration IN a header file. i don’t think they’ve heard of the ‘inline’ keyword.

Seriously, thanks for the tips. it will help me and my ADD work with other people’s code.

Being an Emacs-Lisp newbie, I tried to modified the function so that it will fold based on the column at point, and came up with the following:
(defun jao-selective-display ()
"Activate selective display based on the column at point"
(interactive)
(set-selective-display
(if selective-display
nil
(+ 1 (current-column)))))
(global-set-key [f1] 'jao-selective-display)