tags:

views:

660

answers:

6

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?

+2  A: 

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

// {{{

// }}}

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

Ben Collins
@Ben: Can you have a folding mode without those signs?
Masi
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
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 //{{{ --- //}}} ?
Masi
I must accept this answer, since it is increases portability between Vim, Emacs and other editors.
Masi
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
+7  A: 

hs-minor-mode is what you want.

Arkadiy
+3  A: 

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
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.
Andrew Cone
+1  A: 

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

Laurynas Biveinis
+4  A: 

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
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.
Masi
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
@Amol: I use OS X Leopard.
Masi
@Masi: I don't have experience on OS X. Please have a look at http://www.emacswiki.org/emacs/HideShowandhttp://gnufool.blogspot.com/2009/03/make-hideshow-behave-more-like-org-mode.htmlThe 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
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.
Masi
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
+1  A: 

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