Amazing Emacs!

When was the last time your programming editor amazed you? Well Emacs never stops amazing me! I have used Emacs for almost 6 years now and never realized that it has an “artist-mode” that lets you draw ascii images using mouse. This has many uses. Think how easy it will be to add visual cues to comments that describe a data structure or an algorithm? This screencast shows one such use of the artist-mode.

Emacsclient on windows

You can set Emacs as the default editor for files of a particular format (for instance, ".cpp" files). 
This will enable you to open files in Emacs by double-clicking them.
The first step is to have the "emacs edit server" loaded on Emacs startup. 
Add this to your C:\.emacs file:

(server-start)

Now, emacs can 'listen' for external edit requests and act accordingly.  
We can use the 'emacsclient' or 'emacsclientw' commands to connect to the existing Emacs 
process and tell it to visit a file. The following simple batch script will do the job for us:

@echo off
emacsclientw.exe %1

Save this as 'emacs.bat' or something in the emacs bin folder (might be: c:\program files\emacs\bin). 
 (Make sure the bin folder is in your PATH). The rest, as you know, is easy. Right-click the file, select 
Open with -> Choose Program ... and select 'emacs.bat'.

File IO in Elisp

Elisp is tightly integrated with Emacs. Still, it is a complete programming language suitable for all types of programming jobs. If you are an Emacs user, you might have already used Elisp functions that manipulate text in a buffer. Elisp can also be used to write directly to disk files in the background (i.e, without actually displaying the associated buffer). The following code snippet creates a buffer, associates it with a file called “data” and writes a string to that buffer. Then the buffer is written back to the disk file.

(setq buffer (find-file-noselect "data"))

(save-current-buffer
  (set-buffer (get-buffer-create buffer))
  (insert "hello, world")
  (save-buffer))

The save-current-buffer function makes saves the current buffer and executes its body like progn. In the body of save-current-buffer we set the current buffer to our new “data” buffer, updates and writes it to disk. When the evaluation of save-current-buffer is finished, the original buffer is restored.

Source template hook for Emacs

Click here to download:
tmpl-gen.el (5 KB)

 

Started of with a simple header comment generation function for emacs and ended up with a complete template generation hook for the common languages I work with. (C. C++, Java, Lisp and Python). A sample C header file template generated by the hook looks like this:
 
// Interface to print the 'hello, world' message.
// Copyright (C) 2010 Some Company (http://www.someco.com)
// Author: C Coder <ccoder@someco.com>
// Date: 05-May-2010
 
// This program is free software: you can redistribute 
// it and/or modify it under the terms of the GNU General
// Public License 
// as published by the Free Software Foundation, either version 3 of 
// the License, or (at your option) any later version.
 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
 
// You should have received a copy of the GNU General Public License
// along with this program.  

#ifndef _HELLO_H_
#define _HELLO_H_

#endif
 
The complete elisp code of the hook is attached for you to copy/paste to your .emacs file and customize as you wish! User can input the file description interactively. Basic customization is done by modifying the 5 variables at the beginning of the script. New file-types can be easily supported by adding a check in the source-filep function.