~:: kalyan ::~

January 29, 2009

emacs tweak

Filed under: emacs, Linux, Script — Tags: — skalyanasundaram @ 5:34 pm

Backspace
While programming in emacs have you ever felt, you have a tab character (it is “\t” and not 8 contiguous space) and when you press backspace, instead of deleting the “\t” character, it deletes the characters one by one. It happens atleast in c, c++ mode and but not in python-mode. So you press a tab character to undo that press backspace 8 times. Isnt that tedious?

In the background the backspace character is attached to a function called “backward-delete-char-untabify”. This first converts the tab to chars and delete one character.

Lets fix it,

put this in your .emacs

(gloabl-set-key [backspace] ‘backward-delete-char)

This function does only one job, just delete the preceding character, including “\t”.

This gets applied globally everywhere in emacs. But we want to do this only when do programming. Lets do this only when we load the c-mode by doing the following

(add-hook ‘c-mode-hook ‘(lambda ()
(local-set-key [backspace] ‘backward-delete-char)))
Add this to the specific mode’s hook where ever you want to fix.

Update

Ofcourse, the above was a poor man’s solution. I did not mention it fully. When c-mode gets loaded the backspace is bound to c-electric-backspace. This take two direction based on the inputs and a variable.

first it checks the variable c-hungry-delete-key, if its true, it calls c-hungry-delete-backward which delete all the whitespace including the new line. By default this is set to nil. If you want this do M-x c-toggle-hungry-state.

Otherwise it calls the function pointed by the variable c-backspace-function. By default it points to backward-delete-char-untabify. This is where we reach if you have not modified any variable.
Now we have two solution,

1. set the variable “c-backspace-function” to “backward-delete-char”
2. The backward-delete-char-untabify again operate based on the variable “backward-delete-char-untabify-method” by default its untabify. It can take 4 values,
“untabify” – convert tab to space and delete one char
“hungry” – delete all the tabs, space backward
“all” – delete all the tabs, space and newline backward
nil – delete only one character whatever it is.

Our expectation is to delete one tab at a time. So set the variable backward-delete-char-untabify-method to nil. That should do the job

Advertisements

January 27, 2009

emacs tweak

Filed under: emacs, Linux, Script — Tags: — skalyanasundaram @ 8:36 pm

If you are working on a perl file and using emacs then emacs can really help. By default the perl-mode is loaded. Thats bit old I guess. Instead use cperl-mode which is much more improved than the perl-mode. With the cperl-mode you can get

  • Electric mode, its when you type if it automatically generate if () {}…
  • Better font-lock mode for perl
  • Help. Keep your cursor on some keyword for example use after a few seconds the minibuffer will show the help of use operator.

So make sure to replace the perl-mode by cperl-mode by doing the following in your .emacs

(defalias 'perl-mode 'cperl-mode)
and also load all the functionality by,
(setq cperl-hairy t)

Apart from this I also use flymake-mode which compiles your program as you type and mark it when there
is an error found. Load the file,
(load "~/.emacs.d/flymake.el") and enable the flymake mode.

I also see this flymake can work for c,c++,java, but haven't tried that. I also see some php extensions.

January 23, 2009

emacs tweak

Filed under: emacs, open-source, Script — Tags: — skalyanasundaram @ 5:58 pm

In my old project there used to be many #if 0 .. #endif which does not fall in to the comment-face of the c-mode. This always confuses me. It would be good if that block is colored like a comment. So that i can just ignore that place.

I wrote the following elisp code which adds the #if 0 section to the comment section keywords of the c-mode.
(font-lock-add-keywords
'c-mode
'(("\s-*#\s-*if\s-*0\s-*n\(.*n\)*?\s-*#\s-*endif\s-*" 0 font-lock-comment-face append)))

I have left \s-* in many places as it is because even using back reference does not improve any readability.

December 27, 2008

Christmas Holiday

Filed under: General, life — Tags: — skalyanasundaram @ 7:02 pm
  • Solved Rubix cube
  • Could solve the rubic cube. Dont know what method its. Right now taking lot of time to solve. 
    I guess i will end up breaking my cube some time, still need to practice for the nice movement.
    Look at my solved cubes, :)
    rubix
  • Finished reading Veronika Decided To Die
  • After reading The Alchemist, johnny suggest this one. This is indeed good. I liked it.
    The whole story was moving inside villete. "Every one is mad, the one whos not mad is
    whos not showing others that he/she is mad" is nice. Inspiring book. So, What next?
    Rich dad Poor dad? or Seven Ancient Wonders?
  • Watching TV a lot
  • Children of Heaven movie is cute, slow movie though.

And Advanced Happy new Year.

December 25, 2008

LOC counter

Filed under: open-source, Script — Tags: — skalyanasundaram @ 6:19 pm

I moved to a new project at work. I thought i would count the loc. AWK is cool for this job.

BEGIN {x=0}
{
if ($0 ~ /^[[:blank:]]*/*/ )  {x=x+1}
if(x > 0 && $0 ~ /.**/[[:blank:]]*$/) {x=x-1}
else if(x==0 && $0 !~ /^[[:blank:]]*/// && $0 !~ /^$/) {
print $0
}
}

There are lot of free loc counters available. May be i am re-inventing the wheel. Anyway it is my version. Comments are welcome. This does not count the single line comment and multiline nested comments and all blank lines are ignored.

You need to call this explicitly like, > find . -name *.c -or -name *.h | awk -f ./abovefile.awk | wc -l

November 2, 2008

AWK After Diwali

Filed under: General, Script — Tags: — skalyanasundaram @ 6:55 am

Took a long break for Diwali, six days stay at home. After Diwali settled down at home had nothing to do other than sleeping and eating. TV was boaring. So sat with awk manual. I have had problem in managing my music library. The provider always add his name to the song name like “Sitename – Songname.mp3”. The meta data like album,composer can be managed all together with iTunes, but the name has to be renamed one by one manually. I was struggling to write a shell script. I knew there is some way with awk. And finally i found a way with awk script. Here it is.

ls -l | grep -v "total*" | awk '{val=($(NF-2)" "$(NF-1)" "$(NF)); printf("mv "%s" "%s" n",val, $NF)|"sh"} END{close("sh")}'

I have started using mutt, ppl are moving from text based application to desktop application and now to browser application. Johnny says i am going in the opposite direction. True! anyway this is just fun. I was configuring the addressbook for mutt. Though there are multiple options like lbdb, abook, I still wanted to use the simple mutt alias. But the command add-alias is interactive. I wanted to write a macro which just takes the from field and add to the alias without any interaction. Here is the macro which i wrote

macro index,pager <Esc>a "<enter-command>unset wait_key<enter><pipe-message> awk '{if($1=="From:") { sub(/^"/, "", $2); sub(/"$/, "", $(NF-1)); name=tolower($2) tolower($(NF-1)); print "alias", name, name, $NF >> "/home/tree/.mutt/Mutt-alias" }}' <enter> <shell-escape> awk '{lst[$2] = $0} END{ for(x in lst) print lst[x]  >> "/home/tree/.mutt/Mutt-alias.temp" ;}' /home/tree/.mutt/Mutt-alias;  mv /home/tree/.mutt/Mutt-alias{.temp,} <enter> <enter-command>set wait_key<enter> <enter-command>source /home/tree/.mutt/Mutt-alias<enter>" "Mutt Add address to alias file with no intraction"

This macro takes the first name and last name (middle name is ignored) and strips the any double quote at the beginning and in the ending and add the alias to the alias file and also the alias will be available immediatly. The problem was duplicate entries. The second awk script removes the duplicate entry. It just works fine for me. awk is cool.

update: The above script did not work when song name contain spaces too. So the new one looks like this,

ls -l Sitename* | grep -v "total*" | awk -F "Sitename.Com - " '{val=("Sitename.Com - "$(NF)); printf("mv "%s" "%s" n",val, $NF)|"sh"} END{close("sh")}'

The “sitename.com – “is the string i wanted to strip.

June 9, 2008

Emacs project

Filed under: emacs, General, Linux, open-source — Tags: — skalyanasundaram @ 3:46 pm

I had faced several configuration issue while using emacs as my primary development environment. All is configurable,. but it is just to put together.

I use desktop.el, cscope+.el, etags and each one of them annoyed by conflicting desktop files, having cscope key bindings while using erc or gnus, conflicting etgas between multiple projects correspondingly…

I had tried few things like, ecb, cedet but nothing suited for my requirement. So this is the initiative to make it nice.

I had kept my epr file here and here

May 16, 2008

emacs save all modified files

Filed under: emacs, open-source — Tags: — skalyanasundaram @ 10:43 pm

Few time I got bugged up with the situation where I forgot to save the buffer and trying to blame make, automake tools. It would be nice if there is some function which saves all modified buffers like “Save All” from any IDEs.

I was trying something like this

(setq var (buffer-list))
(unless (null var)
(string-match “.+.[ch].*” (buffer-name (car var)))
(save-buffer (car var))
(setq var (cdr var)))

ofcourse did not work with the last argument of string-match. Latter found save-buffer only saves current buffer and does not take buffer as argument. But which leads me to find save-some-buffers, and this what I was trying to write my own. :-/

save-some-buffers C-x s , saves all modified buffers which are all associated with files. It also ask for many questions. The beauty is if you dont remember what changes have been made in the buffer you can make a diff between the file and the buffer like the below. Review each hunk with M-n

***************
*** 19,25 ****

#include <config.h>

! #include <ctype.h>
#include <stdlib.h>
#include <string.h>
//Test
— 19,25 —-

#include <config.h>

! #kalyaninclude <ctype.h>
#include <stdlib.h>
#include <string.h>
//Test

Diff finished. Fri Jan 18 10:25:55 2008
There is an another option C-r which takes us to the modified buffer too.

Yet another nice feature 🙂

NetBeans supports emacs key bindings!!

Filed under: emacs, open-source — Tags: — skalyanasundaram @ 8:21 pm

I am working on DHCP JavaConsole recently. I though will give a try in NetBeans and I need to work on jdk1.4. Finally got NetBeans 5.0 which is compatible with jdk1.4. Surprisingly NetBeans supports the emacs key bindings. Tools=>Options in the option dialog the keybinding option need to be selected. It is satisfactory. But sometime I am getting confused with the key bindings like M-C-n M-C-p.. It would be good if NetBeans could understand .el files. So that I can customize for my own need. 😉

March 23, 2008

Brainshare week

Filed under: dhcp, General, life, Linux, open-source — Tags: — skalyanasundaram @ 6:18 am

Made my first trip to Brainshare, in SLC. Things went well. Customers partners and issues, requests, satisfication, queries and many more. I had given a talk in BrainShare on DNS/DHCP on OES2 linux, the new features and migration. There were two sessions. I guess in the first session many ppl find it difficult in my indian accent. I could correct that to some extend in my second sessions but still I guess it was difficult.

Had a chance to attend the Hack week demo from the middle. Srini, calvin, Brady’s,.. demo was amazing. The sharing of tasks, notes using giver and evolution meeting setup and openoffice animations all seems to me great stuff. The session was started before I go, so dont know how much i missed it.

Hope this week I get chance to work with NCP and DST folks closely and get some brain food.

Apart from the work could not get much time. We tried to go Antelope island and ofcourse nothing to see much and the to SLC temple, again we cant enter but we could get into assembly hall and other buildings. I met sister low in the assembly hall and the surprising thing is she welcomed me in Tamil. She is basically Malasian and grown up in a tamil family. She showed us the pin drop where with out a mike and speakers you could hear a pin drop sound. Basically to explain the tallent of the mormons civil architects in the earlier.

Moved to provo today or yesterday. Lets see about this week.

(Photos latter)

Create a free website or blog at WordPress.com.