5

I know the thread about having folds for LaTex. However, I want folds for C++/Java when I code.

How can you have either automatic or manual folds in Emacs for C++/Java?

Community
  • 1
  • 1
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697

6 Answers6

11

hs-minor-mode is what you want.

  • Arkadiy - can you say more about it, like how to enable it or turn it on? – jww Sep 22 '15 at 04:53
  • Like any other mode, `esc x hs-minor-mode` toggles it. On minor modes in general, see https://www.gnu.org/software/emacs/manual/html_node/emacs/Minor-Modes.html –  Sep 22 '15 at 11:40
8

My customization for hs-minor-mode is as follows

(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)))
Amol Gawai
  • 3,220
  • 4
  • 23
  • 25
  • I did not get the code to work. I put the code to my .emacs and started Emacs in terminal. I pressed CTRL-C and then arrow key left, but nothing happens. I also tested your code in hs-minor-mode directly by going there first by M-hs-minor-mode. – Léo Léopold Hertz 준영 Apr 27 '09 at 12:09
  • Can you give me your system, emacs spec? The functions in above code are documented in hideshow.el. They have their own shortcut keys but I have mapped them for convenience. – Amol Gawai Apr 27 '09 at 12:43
  • 1
    @Masi: I don't have experience on OS X. Please have a look at http://www.emacswiki.org/emacs/HideShow and http://gnufool.blogspot.com/2009/03/make-hideshow-behave-more-like-org-mode.html The second link shows hide show working on OS X and has implementation for org-mode like hide show i.e. using tab button to smartely perform various operations including hide show – Amol Gawai Apr 28 '09 at 04:08
  • It seems now that the problem is in my .emacs, not in your code. I cannot even import external lips files. My .emacs is at http://dpaste.com/38766/ . My .emacs file's permissions are -rw-r--r--, so they should not be the problem. – Léo Léopold Hertz 준영 Apr 28 '09 at 13:35
  • 1
    Quick check of your .emacs shows strange characters at line 81. Also check *Messages* buffer after starting emacs to see errors, warnings etc. – Amol Gawai Apr 29 '09 at 05:03
5

You can use CEDET to do this. This package provides global-semantic-tag-folding-mode, that allows to fold functions, classes/structures, comments, namespaces, etc. It works more properly than other packages, as it has all syntactic information about code.

There is introduction article about CEDET, that allows to quickly start work with it

Alex Ott
  • 80,552
  • 8
  • 87
  • 132
  • Wow this CEDET things is really nifty. It does not seem to fold on control structures (like "if" and "for"), but it does an admirable job on classes and functions. – Diogenes Creosote Sep 22 '10 at 13:56
3

Make sure you have folding-mode.el. Then, insert

// {{{

// }}}

Around your code. Reload your buffer, and voila! You'll have folds.

Ben Collins
  • 20,538
  • 18
  • 127
  • 187
  • @Ben: Can you have a folding mode without those signs? – Léo Léopold Hertz 준영 Apr 26 '09 at 20:35
  • 1
    There might be another folding-mode script out there, but this is how you use the most commonly distributed one. Of course, as will anything in Emacs, you can always roll your own. Most of the time when some customization in emacs has a minor irritant (like these superfluous comments) I just "stop worrying and learn to love" it. – Ben Collins Apr 26 '09 at 21:02
  • I put the folding.el file to my lisp folder which is run at the startup. I start emacs by $ emacs ~/.vimrc. I have comment signs as "{{{ --- "}}}. I do not see any folds. Should I change those fold marks //{{{ --- //}}} ? – Léo Léopold Hertz 준영 Apr 30 '09 at 21:05
  • 1
    I must accept this answer, since it is increases portability between Vim, Emacs and other editors. – Léo Léopold Hertz 준영 Apr 30 '09 at 21:06
  • The folding delimiters for any particular file depends on the major mode. It's usually paired with a comment symbol for whatever type of file it is. In C, it's /* {{{ */. In C++, it's // {{{, etc. There may be a generic symbol for plain text, but I don't know offhand what it is. – Ben Collins May 01 '09 at 03:03
2

You could experiment with selective-display. It's more of a quick folding of all your code according to its indentation level. It's great for getting class/function summaries or for moving around quickly.

But if folding up blocks of code is what you want, then HideShow, like Arkadiy pointed out, is probably more suitable.

Silfheed
  • 11,585
  • 11
  • 55
  • 67
1

For Java, use JDEE. For C/C++ see the other answer about CEDET.

Laurynas Biveinis
  • 10,547
  • 4
  • 53
  • 66