Posts Tagged ‘Coding Practices’

An Emacs Diff (Ediff) tutorial

December 12, 2008

Since Jitesh is currently writing an extremely interesting series of posts on diff and patch (1,2), I decided to pitch in with my contribution. Ediff is one of those things which make Emacs so much more than just an editor. It is an extremely convenient way of viewing differences in files / buffers / directories.. You get the point.

Following the teaching tradition I prefer, lets get our hands down and dirty, and figure out the theory along the way:

Step 1:
Write two test files for our purposes.

Step 2: (more…)

Advertisements

Quick-Post: Coding Style

November 25, 2008

Here is a document specifying Google’s C++ Style Guide: Google C++ Style Guide.
I have not read it yet, but it promises to be good. I’m hoping it will help readers develop a uniform coding style.

P.S : Linux kernel’s document on coding guidelines can be found in the kernel code at Documentation/CodingStyle.

Emacs Tip of the Day # Are you a fan of Tab Completion? #

October 16, 2008

Tab Completion is a great, great feature. It saves me a lot of time and effort, in bash as well as in the Emacs minibuffer.
I don’t know if you are aware of this, but Emacs also comes with a built-in feature called dabbrev-expand (M-/ in the minibuffer). Here is what it does. If you type out the first few letters of your word and hit the key-combo, it auto-completes the word after searching the current buffer and the other open buffers for completion. You can cycle through all possible completions by repeatedly reinvoking the function. This is very, very cool. The only problem is that M-/ is not conducive to speed.
So can we do something super cool like re-bind the function to the TAB key? Without destroying other tab related functions like indentation? Turns out we can!

(global-set-key [(tab)] ‘smart-tab)
(defun smart-tab ()
“This smart tab is minibuffer compliant: it acts as usual in
the minibuffer. Else, if mark is active, indents region. Else if
point is at the end of a symbol, expands it. Else indents the
current line.”
(interactive)
(if (minibufferp)
(unless (minibuffer-complete)
(dabbrev-expand nil))
(if mark-active
(indent-region (region-beginning)
(region-end))
(if (looking-at “\\_>”)
(dabbrev-expand nil)
(indent-for-tab-command)))))

Add this to your .emacs file, and start using TAB in everything you type. You will find that your typing speed triples!

[hat-tip]THE EMACS WIKI

Emacs Tip of the Day #Jumping to close-paren and back#

October 10, 2008

Some code browsing tips:

C-M-f – Jump to closing parenthesis
C-M-b – Jump to opening parenthesis

Pretty much self-explanatory me thinks.

Emacs Tip of the Day #Indenting A Region of Code#

October 8, 2008

We often have to work with code written by other people, and though it would be nice if people did their indentation on their own, many people just aren’t bothered. Life is too short to waste time re-indenting somebody’s badly written code. Let Emacs come to your rescue.
Select the offending piece of code, and run the command

M-x indent-region
alternatively,
C-M-\

Voila!

*Update*

Came across something a bit more useful. Here is a function to re-indent the entire buffer (without having to select it first)

(defun iwb ()
“indent whole buffer”
(interactive)
(delete-trailing-whitespace)
(indent-region (point-min) (point-max) nil)
(untabify (point-min) (point-max)))

Just add it to your .emacs file. Next time you open a buffer, execute M-x iwb, and get properly indented code.

[hat-tip]: M-x all-things-emacs