1

Is there a way to fold the first comment of a file on load?

While I don't like code folding in general, it can be handy in this one instance when every file has a boiler plate header which isn't useful to see each time.

Is there a way to setup emacs to do this?

I'm not looking for an advanced code-folding feature, just the most simple code folding, with a way to see the first comment if it's needed.

1

I use elide-head.el (which comes with Emacs) specifically for this purpose.

Setup

Add the following to your init.el.

;; enable elide-head to hide boilerplate
(require 'elide-head)
;; automatically enable it for CC-mode's languages
(add-hook 'c-mode-common-hook 'elide-head)

Result

By default elide-head understands GNU, BSD, and X11 preambles. So, with the above initialization, whenever you load a C/C++ file, the display of your code could go from something like this:

/**
 *  $Id: main.cc 2993 2011-11-11 01:11:11Z you $
 *
 *  Copyright (C) 1969
 *  you <you@yousjob.cc>
 *  Some "Other" Author
 *  Some "Dude Who Didn't Really" Help
 *  and Bunch of Fools <http://yousjob.cc/fools/>
 *
 *  This file is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

To:

/**
 *  $Id: main.cc 2993 2011-11-11 01:11:11Z you $
 *
 *  Copyright (C) 1969
 *  you <you@yousjob.cc>
 *  Some "Other" Author
 *  Some "Dude Who Didn't Really" Help
 *  and Bunch of Fools <http://yousjob.cc/fools/>
 *
 *  This file is free software; you can redistribute it and/or modify...
 *
 */

Customization

The above is enough for me as I often want to cursepraise who was there before me, but you could tighten things up if you wanted. Continuing with our example, if you changed your elide-head setup to:

;; enable elide-head to hide boilerplate
(require 'elide-head)
(add-to-list 'elide-head-headers-to-hide 
             '("Copyright (C)" . "If not, see <http://www\\.gnu\\.org/licenses/>\\.?"))
(add-hook 'c-mode-common-hook 'elide-head)

whenever you loaded a C/C++ file your code would look like:

/**
 *  $Id: main.cc 2993 2011-11-11 01:11:11Z you $
 *
 *  Copyright (C) 1969...
 *
 */

elide-head-headers-to-hide is an alist of regexp pairs that mark the start and end of the area that you want to elide. Read the source for complete details. elide-head.el is less than 120 lines.

Another option

The comments in elide-head.el note that hs-minor-mode can act similarly, but is not as selective, which maybe something that you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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