This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Emacs equivalent of Vim's foldmethod = indent
JEdit has a mode (specifically, folding mode: indent, in Options) that lets you fold code blocks based purely on indentation. It does not require any additional configuration, or knowledge of the language you are using.
That is, if you have code like this:
foo
bar
blah
oof
and the cursor is on the second or third line, and you tell JEdit to fold, those two lines will be hidden.
I have read similar SO questions, but I haven't found anything in emacs that "just works", for any buffer, like JEdit's code folding does.
I have specifically tried Fold Dwim. It doesn't work for me. It folds right to the end of the buffer, for some reason, which is utterly useless.
Take a look at minor mode folding-mode
OutlineMinorMode works for me, though I've only used it with LaTeX. AFAIK it won't fold based on indentation (someone correct me if I'm wrong please) but on keywords etc that you've configured as marking a foldable section.
Edit: This looks relevant.
Related
Just FYI, I am new to the .emacs file.
I would lik to set up my .emacs file to auto-indent and auto-pair a certain way to make writing code a little faster. I have found some info as to how to do these things independently but I'm not sure how to put it all together for the emacs version that I have. Ultimately, I would like to set up these definitions specific to which ever language I am coding in. Just to get me started I will use java as an example.
Obviously auto-pairing for ", (, ' are pretty straigforward. I would just like it to auto insert a closing ", ), ' and place the cursor in the middle.
For {, I would like it auto insert two newlines and the closing } whith the cursor in the middle.
Example
while (true) {
<--- cursor would be here with auto-indent of 2 spaces
}
I would also like this to work for nested curly braces which the appropriate indentation.
Example
while(true) {
if (...) {
}
}
Here is what I have so far in my .emacs file:
(defun java-autoindent ()
(when (and (eq major-mode 'java-mode) (looking-back "[{;]"))
(newline-and-indent)))
(add-hook 'post-self-insert-hook 'java-autoindent)
Obviously this just inserts a line and auto indents, but I also want the closed } to be included on the line below. I also tried using electric-pair but that didn't work.
My wish list may be a little unrealistic. I'm not even sure that this is possible, but I would be happy with the closest that I could get.
Any help to get me going in the right direction would be greatly appreciated.
Emacs defines modes for each type of language you code in. Some modes are derived from others and there is a mode called prog-mode which most programming modes are derived from.
The mode for a language is where things like indentation are defined because these tend to be language specific. The rules for indentation can be quite complicated, which is why people often use a mode with similar indentation style as the parent and derive a new mode from that.
Have a look at modes and derived mode in the emacs elisp manual.
With respect to adding matching/closing delimiters, have a look at electric-pair-mode (I think it was in emacs 24.4 - I'm running 25 and forget when it was introduced).
With respect to your requirement to enter some code, some newlines and position the cursor in a specific place, you probably want to look at one of emacs' template solutions. yasnippet is a popular choice and it is easy to define new templates in it. There are also many existing packaged yasnippet templates you can download/install. If you don't like yasnippet, google emacs template and have a look there are quitre a few frameworks.
in gedit it's possible to define so-called "snippets" for simpler input.
For example, there is a snippet while. This means: If you type while -> (-> stands for tab key). And gedit automatically converts it to the following (including correct indentation):
while (condition){
}
In vim (in conjunction with latex-suite) I saw the following: If you type (, vim inserts just a (. If you type ( a second time, vim automatically converts it to \left( \right).
I found abbrev-mode but this mode doesn't place the cursor properly (i.e. between parentheses or inside the while loop).
I managed to create custom emacs keybindings/macros that do just the same (without having to press the tab key), so I know it's possible.
However, is there already and package where you can define such "snippets" without much effort? Or are there even any serious reasons not to use such things?
See yasnippet. It provides snippets for most major languages, and it is easy to add new ones or modify the old ones.
Yes, yasnippet is probably the way to go. But make sure you learn the major mode you're using for your editing - when writing in LaTeX, learn auctex. Major modes can contain functionality that makes some snippets pointless, and do the same thing even better. So instead of using a begin/end-snippet in a LaTeX buffer, try C-c C-e in auctex. Etc :)
Don't forget abbrev-mode.
I would like rainbow parens for editing Clojure in Emacs and since VI does this I assume that in Emacs it should be something like M-x butterfly or something :)
This is an old question now, but I recently wrote RainbowDelimiters mode for this. It's a 'rainbow parens'-type mode which colors all parens, brackets and braces, made with Clojure programming in mind.
It highlights the whole buffer, not just the parens surrounding point.
The most important thing is that it's FAST - all the other rainbow paren modes I've tried slow down editing (especially scrolling) quite a lot. I put significant effort into profiling and optimizing it so doesn't have any noticeable impact on scrolling/editing speed.
You can find info about it at the EmacsWiki page, and the mode itself is at rainbow-delimiters.el.
I'm using highlight-parentheses-mode from the script mquander mentioned. It doesn't provide much of a rainbow effect out of the box, but it is customisable:
(setq hl-paren-colors
'(;"#8f8f8f" ; this comes from Zenburn
; and I guess I'll try to make the far-outer parens look like this
"orange1" "yellow1" "greenyellow" "green1"
"springgreen1" "cyan1" "slateblue1" "magenta1" "purple"))
I believe I've lifted the actual colours from Vimclojure. Note that Vimclojure highlights all parentheses in the file, whereas with highlight-parentheses-mode only the parens which actually contain the point will be highlighted (and only a limited number of levels). I happen to find this behaviour useful, but it is perhaps a bit lacking in the prettiness area in comparison with the Vimclojure way.
I now notice I've never gotten 'round to fixing those outer paren colours actually... Maybe I will now that you've reminded me about it.
Here's a mode for that which I have used in the past briefly. Here's another one which I haven't tried.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to achieve code folding effects in emacs
An excellent feature of Dreamweaver is code folding of any lines of text -- recursively!
It's fantastic, I can fold any text or code, regardless of language. I work with existing systems; I don't edit well-written code or code in one language etc. mostly HTML mixed with god-knows-what. Folding lines makes understanding a lot easier and quicker. Sadly, this is the only feature I like in Dreamweaver.
Is there any code folding for Emacs in a similar aim?
There's folding mode, a minor mode. Unfortunately it's intrusive: you have to manually annotate the folds with specialized comments, which clutter the code when you aren't using the mode (or when sharing code with others who don't use it). A better mode would not change your code to work.
Let me add: there's a duplicate of this that's worth a look: How to achieve code folding effects in Emacs.
hide-show (hs-minor-mode) is a minor mode that will do something like this...
The default key-binding to trigger the folding is C-c # C-c which I find pretty cumbersome. But then I don't use it much, either.
You might want to look up the function set-selective-display and the variable selective-display. Not exactly what you want but it lets you hide lines based on indentation level.
I use fold-dwim.el. From the emacs wiki:
fold-dwim.el is a unified user interface for Emacs folding/outlining modes. It supports folding.el, hideshow.el, outline.el, TeX-fold.el, and nxml-outln.el
You can get it here:
http://www.emacswiki.org/emacs/FoldDwim
I have this in my .emacs:
(require 'fold-dwim)
(global-set-key [(C kp-4)] 'fold-dwim-hide-all)
(global-set-key [(C kp-5)] 'fold-dwim-toggle)
(global-set-key [(C kp-6)] 'fold-dwim-show-all)
Keep in mind that you still need to activate hs-minor-mode, folding-mode, etc. but I find it easier to use them this way.
Something else you might look into is nxhtml-mode; it doesn't fold code, but it does highlight mixed code (i.e. HTML and PHP) differently depending on its type. That gives you a similar gain in comprehensibility without the awkwardness of folding-mode. I think that approach is more suited to Emacs anyway, first because code-folding seems like a mouse-oriented idea that doesn't adapt well to the basically keyboard-centric Emacs interface, and second because Emacs eases navigating a large file to an extent that code can stay visible without getting in your way.
If you actually need "something to hide a given region rather than it trying to understand the syntax" (unlike hideshow and other solutions based on parsing) and you "don't want to have to edit [your] code" (unlike folding), then, I assume, you mean you don't want the regions to be persistent between different editing sessions. Then you might use http://www.emacswiki.org/emacs/HideRegion to hide user-selected regions...
(But that's strange. The folding minor mode with persistent marks seems to be a far more convenient solution.)
Emacs reindents the current line whenever I type certain things, like ";" or "//". This is pretty annoying, since there are a whole lot of places where it isn't smart enough to indent correctly.
How do I disable this feature? I still want to be able to indent the line with TAB, but I don't want any source code I type to cause it to reindent.
(I'm using Dylan Moonfire's C# mode, but this probably applies to any cc-mode.)
Try running c-toggle-electric-state to turn off the electric action of these characters.
You can do this as part of a c-mode-common-hook, or toggle the state manually by hitting C-c C-l.
most likely caused by the inline-and-indent 'feature' of c-mode and derivatives. emacswiki has several solutions.