Thursday, January 8, 2009

Emacs - customizing code indentation

In code editors with automatic indenting, we can normally customize the indentation by setting up variables. Say, "indent-space" which is an integer which determines how many spaces should be inserted from current indentation level for a nested block. It usually takes a long time to find out the name of the appropriate variable (which involves searching through a list of 1000 variables). Emacs takes a different, highly interactive approach.

The code indentation engine in Emacs works with "syntactic symbols". When you type code in emacs type "C-c C-s". The minibuffer will show the syntactic symbol near point. Now type "C-c C-o" this will ask for a syntactic symbol that you want to customize (By default it shows the one near point). Here you can select the number of spaces Emacs should use to indent this particular syntactic symbol.

For ex:-

main()
{ /* type C-c C-s here, minibuffer will show defun-open, typing C-c C-o now
will allow you to set the number of spaces Emacs should insert for a brace
starting a function definition */


PS:- A point means the position at which the cursor is in.


No comments: