Emacs: font-lock explanation - emacs

I'm using Emacs since a long time and I've customized it quite a lot. I've even written simple ELisp functions and now I'm starting to use macros more and more. I'm not anywhere near a knowledgable Emacs user but I'm not either a complete beginner.
I'm using nxhtml, mumamo, dired, ido, custom keybindings and so many things I added throughout the years that I don't remember all of them ; )
But there's something I really never understood: what is the "font-lock" mode and why should I care?
For example, I've read that nxhtml and nxml, if I recall correctly, do not use font-locking. Maybe I didn't understand that part well but in either way: what does it change for me, as a user?
Or take for example this description about WhiteSpace:
Note that when WhiteSpace is turned on, WhiteSpace saves the
font-lock state, that is, if font-lock is on or off. And WhiteSpace
restores the font-lock state when it is turned off. So, if WhiteSpace
is turned on and font-lock is off, WhiteSpace also turns on the
font-lock to highlight blanks, but the font-lock will be turned off
when WhiteSpace is turned off. Thus, turn on font-lock before
WhiteSpace is on, if you want that font-lock continues on after
WhiteSpace is turned off.
OK, fine. I understand that. But what does it change if font-lock continues after or not?
Basically I just don't "get it", no matter how much I read about the subject.
Any example/explanation as to what "font-lock" is under Emacs and why it concerns me would be most welcome!

font-lock-mode == colorful syntax highligthing. global-font-lock-mode enables font-locking for each and every mode in Emacs. Older Emacs version required you to enable it manually, newer version (23+ I think) enable this by default. I can't imagine that many people would want to turn off this feature, since it makes some type of content (like source code) immensely more readable. Every mode that uses font-lock relies on a certain set of faces - a combination of font properties like size, bold, underline, italic and color. The basic faces that most modes use are:
font-lock-builtin-face
font-lock-comment-face
font-lock-comment-delimiter-face
font-lock-constant-face
font-lock-doc-face
font-lock-doc-string-face
font-lock-function-name-face
font-lock-keyword-face
font-lock-negation-char-face
font-lock-preprocessor-face
font-lock-string-face
font-lock-type-face
font-lock-variable-name-face
font-lock-warning-face
You can easily override their values if you'd like them (or use a different color-theme). Some modes happen to define additional faces as well.

It displays your code with all the different colors (red for comments, green for class names, etc). If it's off, you get black-and-white code instead. Open up some code and execute M-x font-lock-mode a couple times to watch it switch on and off.

Related

Code folding for LaTeX in Emacs

Is there an Emacs minor-mode (or piece of elisp code) that lets you selectively hide/show environments while in LaTeX mode? For instance, I would like to move to the beginning of a long \begin{figure} block, hit a keystroke, and have the contents of that figure environment hidden from view. Similarly with \begin{proof} and so on, and ideally even with \subsections.
Is this possible? I just tried hs-minor-mode, allout-mode, and outline-minor-mode, but most of them don't recognize LaTeX's environments, e.g. hs-minor-mode fails with "scan error: unbalanced parentheses". I would prefer not to have to enter explicit folding marks like {{{ as in folding-mode.
[Ideally it would be great if the folding were persistent, but I see that that question doesn't have an accepted answer yet.]
AUCTeX does folding: http://www.gnu.org/software/auctex/manual/auctex.html#Folding
A popular complaint about markup languages like TeX and LaTeX is that there is too much clutter in the source text and that one cannot focus well on the content. There are macros where you are only interested in the content they are enclosing, like font specifiers where the content might already be fontified in a special way by font locking. Or macros the content of which you only want to see when actually editing it, like footnotes or citations. Similarly you might find certain environments or comments distracting when trying to concentrate on the body of your document.
With AUCTeX’s folding functionality you can collapse those items and replace them by a fixed string, the content of one of their arguments, or a mixture of both. If you want to make the original text visible again in order to view or edit it, move point sideways onto the placeholder (also called display string) or left-click with the mouse pointer on it. (The latter is currently only supported on Emacs.) The macro or environment will unfold automatically, stay open as long as point is inside of it and collapse again once you move point out of it. (Note that folding of environments currently does not work in every AUCTeX mode.)
In order to use this feature, you have to activate TeX-fold-mode which will activate the auto-reveal feature and the necessary commands to hide and show macros and environments. You can activate the mode in a certain buffer by typing the command M-x TeX-fold-mode RET or using the keyboard shortcut C-c C-o C-f. If you want to use it every time you edit a LaTeX document, add it to a hook:
(add-hook 'LaTeX-mode-hook (lambda ()
(TeX-fold-mode 1)))
If it should be activated in all AUCTeX modes, use TeX-mode-hook instead of LaTeX-mode-hook.
Once the mode is active there are several commands available to hide and show macros, environments and comments...

How do I get "rainbow parentheses" in emacs?

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.

Why isn't return bound to newline-and-indent by default on emacs

I have tried emacs on and off for a while now and every time I start emacs, I go through the same routine. Customizing. The first one is binding return to newline-and-indent. (g)Vim does this by default. Showing matching parenthesis is also done by default on (g)Vim. It is grea that I can customize emacs to my heart's content but why doesn't emacs have nice and easy defaults? For reference, I am now using Emacs 23 on a RHEL5 box.
Probably because RMS didn't want it, that and because changing long-standing defaults is just an issue of politics. Like vi, Emacs has a hard-core following and basic changes like these are minefields.
Note: if you saved your customizations, then you wouldn't have to re-do them every time...
To have those nice and easy defaults, install Emacs Starter Kit. It enables by default a bunch of useful and convenient features make even the advanced Emacs users more productive.
Otherwise, as TJ pointed out, Emacs Customization Mode (type M-x customize) allows you to save permanently any of the settings. You can even store them in a separate file from your dotemacs―(setq custom-file "~/.emacs-custom.el")―so you can use it in every computer you work on.
The title of your question doesn't really reflect what your question is (and has been answered by Trey and Torok), but I'll tell you why I like it being bound to just newline: useless whitespace. Say you are nested inside a conditional in a function etc. and hit return a couple times to leave a blank line. The blank line now has a bunch of space chars on it. Yes, you can (and I do) remove trailing whitespace before saving, but I also have visual whitespace mode on and I can see it there taunting me.

Code folding in Emacs [duplicate]

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.)

Make Emacs less aggressive about indentation

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.