~:: 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 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.

April 4, 2007

syslog-ng with dhcp

Filed under: dhcp, Linux, open-source, Script, SuSE — skalyanasundaram @ 10:52 am

It is been long time i looked at the log files methods. I had written an article earlier for logging the dhcp messages at separate location. Suddenly i felt syslog daemon itself missing!. I am not updated 😦 Well, now they comes up with syslog-ng. It is really cool, has all the flexibility. The abstract way of doing things, reg exp support and TCP support. I was trying out for logging the dhcp to log in seperate file just for the learning purpose.

You will have to add the following lines in /etc/syslog-ng/syslog-ng.conf [may be at the bottom]

filter f_daemon     { facility(daemon); };
filter f_dhcpd       { match(“dhcpd”); };
destination dhcpmessages { file(“var/log/dhcp.log”); };
log { source(src); filter(f_daemon);  filter(f_dhcpd); destination(dhcpmessages); }; 

By default the dhcp sends out in daemon facility , so i just used it. And /var/log/dhcp.log have to be touched. It may ask some time to have a 640 permission. But what ever works for me. and need to do a syslog restart. Now everything goes fine :).

But if you use pipes instead of file in the destination atleast in SuSE you will have to make sure the apparmor [SELinux in redhat] has the permission to write on the ports. I did not try to configure the profile. I just simply unloaded the apparmor and tried it that works.

October 18, 2006

Dancing Images on the page you are seeing!

Filed under: Script — skalyanasundaram @ 1:16 pm

Some nice script my friend send me. Just copy and paste it in your address bar of your browser. All the images which are exist in that page will start dancing. Logic is very simple. But a good kind…..

javascript:R=0; x1=.1; y1=.05; x2=.25; y2=.24; x3=1.6; y3=.24; x4=300; y4=200; x5=300; y5=200; DI=document.images; DIL=DI.length; function A(){for(i=0; i<DIL; i++){DIS=DI[ i ].style; DIS.position='absolute'; DIS.left=Math.sin(R*x1+i*x2+x3)*x4+x5; DIS.top=Math.cos(R*y1+i*y2+y3)*y4+y5}R++}setInterval('A()',5); void(0)

Let see the proper intended one.

javascript:

R=0;

x1=.1; y1=.05; // initial varaiables

x2=.25; y2=.24;

x3=1.6; y3=.24;

x4=300; y4=200;

x5=300; y5=200;

DI=document.images; //get list of images from DOM

DIL=DI.length; // count hoe many images

function A() {

for(i=0; i<DIL; i++){ //repeat for all images

DIS=DI[ i ].style; //get reference to style attribuet of each image

DIS.position=’absolute’; //change the img attribute position to absolute so that we can set our own, make dance

DIS.left=Math.sin(R*x1+i*x2+x3)*x4+x5; // chaneg position and make dance using sin,cos logic

DIS.top=Math.cos(R*y1+i*y2+y3)*y4+y5;

}

R++;

}

setInterval(‘A()’,5); //repeat the same for ever with a gap of 5 sec

void(0);

Is it a nice one? I Hope so…

Blog at WordPress.com.