2008-12-26

showing and hiding blocks of code

When I'm writing some code, I find it often useful to hide most function bodies, and only see the one I am working on. This way, I can keep the overview, and concentrate on the part I am working on. Emacs has support for showing/hiding blocks of code, using the HideShow-package. HideShow works with C/C++, Lisp, Scheme, Java, Perl, PHP, TCL, VHDL and Fortran. It also seems to work with Javascript (at least JS2).

You can enable Hide-Show for C/C++ code by adding the following to your .emacs:

(add-hook 'c-mode-common-hook
  (lambda()
    (local-set-key (kbd "C-c <right>") 'hs-show-block)
    (local-set-key (kbd "C-c <left>")  'hs-hide-block)
    (local-set-key (kbd "C-c <up>")    'hs-hide-all)
    (local-set-key (kbd "C-c <down>")  'hs-show-all)
    (hs-minor-mode t)))
You can hide or show either the current block, or the whole file. To show the former, consider a C-function:
int my_function (int a, int b)
{
 return a + b;
}
Now, when calling hs-hide-block, we get:
int my_function (int a, int b)...
HideShow has one problem: the truly bizarre default key bindings, such as 'C-c @ ESC C-s' for hs-show-all. This is the reason I am adding these alternative key bindings in the add-hook above - 'C-c' and the arrow keys; alternatively, you could use the 'Super'-key instead of C-c.

If you'd like to hide everything by default when you open a file, you can do so by adding

(hs-hide-all)
to the above hook function.

Side-note: Even though there are many keys on your keyboard, there is a finite number of easy combinations, especially when the arrow-keys are involved. They are popular, and not just by emacs -- for example, window managers often use Ctrl-Alt-<arrow-key> for switching between virtual desktops. So, it's good to think a little bit where to 'spend' your easiest key bindings.

8 comments:

Anonymous said...

Thanks for the great tip.

I introduced this great tip to Korean emacs user in myblog.
( http://tkhwang.tistory.com/35 )

It seems that you didn't open trackback. T_T;

djcb said...

@tkhwang: nice -- and i've enabled the trackback support.

Terrence Brannon said...

How would you take a few lines of variable declarations and turn that into a hideable block?

djcb said...

@Terrence Brannon: well, hide-show is for hiding *blocks*; for most languages, it would not make much sense to put the variables in a separate block, so hide-show is not a good solution for that.

Instead, you could look at e.g, folding mode, but that requires specially formatted comments.

Terrence Brannon said...

yeah, I tried to put my comments in a "block":

# variable decls {

my $x = 12;
my $y = 33;
my $z =33;

# }

but the mode tries to interpret comments as a block and expects a certain number of lines for the comments

yes, i may have to goto folding mode, but I really liked this.

Rajarshi Tiwari said...

Isn't this something similar to what we see in org-mode. The headings are shown and hidden in cyclic manner?

I wonder if we could adapt your solution to a case where we could do the same in f90-mode or c-mode etc.

Anonymous said...

Thanks for the tip!

I am wonder if it's easy or not to customize the color of the 3 dots ? I think that it would be better to view something that differs from three real dots, no?

It should be cool also if we can start the buffer in hs-hide-all except for the comments of the code which are sometimes more important than the function definition itself, it is easy to customize ?

djcb said...

@Anonymous: you can customize how it looks, see the documentation for the variable hs-set-up-overlay. It does require some elisp though.