If you've used macros in Emacs, sooner or later you'd need to automatically increment as expressed in this thread. If you read the thread, you'd see a number of suggestions but the simplest is to use the builtin Emacs counters. The Emacs manual too mentions how to use them.
The Emacswiki too mentions how to use them and some tricks in getting it to do it the way you want.
Learn it, it saves a tremendous amount of drudgery typing
Postings on living in an Emacs world. Posts will be mostly on using Emacs, related functions and tools.
Ads by Google
Wednesday, October 7, 2009
Saturday, October 3, 2009
Passing AWK variables to shell commands for evaluation
For some reason, I never seem to get how to use the shell from within GNU AWK to manipulate the AWK variable and return it. Spending a lot of time with the system and getline command, I still couldn't figure out how to pass a variable to a shell command, evaluate it and then return the value.
After much trial and error and googling, this seems to work.
a=20090601
cat ../data/C_100119.dat | \
awk -v compdate="$a" '{
while ("date --date="$4 " +%Y%m%d"|getline aa)
{
if(aa >= compdate)
print $0
else
exit
}
}'
where I'm extracting records whose 4th column date value is greater than a.
Hope this is useful to others.
Update: Scratch the above code. I got some feedback about the correct ways to use getline.
a=20090601
awk -v compdate="$a" '
{
if (("date --date="$4 " +%Y%m%d"|getline aa) >0){
if(aa >= compdate) {
print
next
}
}
exit
}' ../data/C_100119.dat
is a better way to do it.

After much trial and error and googling, this seems to work.
a=20090601
cat ../data/C_100119.dat | \
awk -v compdate="$a" '{
while ("date --date="$4 " +%Y%m%d"|getline aa)
{
if(aa >= compdate)
print $0
else
exit
}
}'
where I'm extracting records whose 4th column date value is greater than a.
Hope this is useful to others.
Update: Scratch the above code. I got some feedback about the correct ways to use getline.
a=20090601
awk -v compdate="$a" '
{
if (("date --date="$4 " +%Y%m%d"|getline aa) >0){
if(aa >= compdate) {
next
}
}
exit
}' ../data/C_100119.dat
is a better way to do it.
Saturday, September 19, 2009
Friday, September 11, 2009
An improved or better Quicksort?
Via reddit, this is an interesting post on using 2 pivots to do quicksort with an attendant savings in swaps and speed. I wonder how soon this will get into other tools.

Sunday, September 6, 2009
Getting Slide or Frame numbers in Beamer Warsaw Theme
While preparing a presentation using Beamer, I decided to get the slide numbers(frame numbers in Beamer parlance) into the presentation just to pace my slides. And it turns out that it's not easy as it sounds. I tried a variety of setbeamertemplate options and even considered editing the template. I believe beamerouterthemesplit.sty is the file to change though I have no idea what I have to do.
A bit of googling got to me to this page and I have no idea why the suggestion works. But it does. I have broken it into 2 lines with a extraneous \ at the end of the first line.
A bit of googling got to me to this page and I have no idea why the suggestion works. But it does. I have broken it into 2 lines with a extraneous \ at the end of the first line.
\title[Short Title \hspace{4em} \insertframenumber\ \
of \inserttotalframenumber]{Full Title}
Friday, September 4, 2009
An Emacs keybindings help tip
In Emacs, C-h k or C-h m will show you the help regarding what function is bound to the key or what keys are bound in the buffer you're currently working on. If it's a drag to scroll through the entire list or if you want to narrow it down, you can press the prefix keys and then C-h to get only those keybindings that start with the prefix key.
For example, pressing the prefix C-x r and then C-h (C-x r C-h) will only show the help regarding the key bindings starting with the prefix C-x r. This is useful when you enable a minor mode that has a prefix key combination that you use infrequently. In the normal case, you'd have to invoke C-h m and then scroll down to the respective section in the help buffer. If you knew the prefix, then pressing the prefix followed by C-h will be "context" sensitive help.
Based on the thread here on gnu.emacs.help.

For example, pressing the prefix C-x r and then C-h (C-x r C-h) will only show the help regarding the key bindings starting with the prefix C-x r. This is useful when you enable a minor mode that has a prefix key combination that you use infrequently. In the normal case, you'd have to invoke C-h m and then scroll down to the respective section in the help buffer. If you knew the prefix, then pressing the prefix followed by C-h will be "context" sensitive help.
Based on the thread here on gnu.emacs.help.
Sunday, August 30, 2009
A Lisp book incident
The herd effect in programming is very powerful. Whatever is the programming flavour this year, that's where the crowd is going to be. Lots of Java, C#, VB etc. If you ever wondered on the uptake of Lisp and its ilk....
A few years ago, I happened on a copy of Winston Horn's Lisp book in a book store. The book seemed untouched, was dusty and was jammed in one corner of a bookshelf. When I wanted to buy it, the owner seemed happy to get rid of it. He even gave me a good discount over the list price saying "That's the last copy I have and you're the first person in years to buy this book"
hmmm....
Lugging it around my workplace, a few weeks later, I misplaced it. Searching all over the usual places, I couldn't locate it. Resigned to the fact that I lost the book within a few weeks of purchase, I made a half hearted call to security, whether someone returned the book there.
To my delight, he said, that the book was found by one of the security personnel in the cafeteria and I could come and collect it.
When I went to get it, the head of security said "What is this Lisp? I've not heard of it. If this had been a Java, C# or ASP book, you'd not have got the book back. It would have been taken by someone."
Guess that's the bright side of getting your book back. OTOH, Lisp popularity....

A few years ago, I happened on a copy of Winston Horn's Lisp book in a book store. The book seemed untouched, was dusty and was jammed in one corner of a bookshelf. When I wanted to buy it, the owner seemed happy to get rid of it. He even gave me a good discount over the list price saying "That's the last copy I have and you're the first person in years to buy this book"
hmmm....
Lugging it around my workplace, a few weeks later, I misplaced it. Searching all over the usual places, I couldn't locate it. Resigned to the fact that I lost the book within a few weeks of purchase, I made a half hearted call to security, whether someone returned the book there.
To my delight, he said, that the book was found by one of the security personnel in the cafeteria and I could come and collect it.
When I went to get it, the head of security said "What is this Lisp? I've not heard of it. If this had been a Java, C# or ASP book, you'd not have got the book back. It would have been taken by someone."
Guess that's the bright side of getting your book back. OTOH, Lisp popularity....
Sunday, August 23, 2009
WoMan: An Emacs interface to read man pages
If you've worked with terminals and launched man in it, it's a bit weird to suddenly use vi commands to navigate the man pages. And it really is a problem to navigate on older *nixes with the way the screen moves or refreshes itself. Well, Emacs has a really good interface to read manpages call WoMan (Without Man). Nicely formatted, easier navigation and ability to jump from section to section is among the features I like in it.
It can be launched as M-x woman and will prompt for a command you want the manpage on. Now, if you have cygwin installed, it should work without a hitch, otherwise you might have to customise woman-manpath and woman-path to point to where the manpages lie. Reading the variable documentation on both using C-h v , which should tell you how to set it.
Note that, it launches a new frame (Window in Microsoft terminology) to display the manpage and would probably be in a different font. You can close the frame by pressing C-x 5 0.

It can be launched as M-x woman and will prompt for a command you want the manpage on. Now, if you have cygwin installed, it should work without a hitch, otherwise you might have to customise woman-manpath and woman-path to point to where the manpages lie. Reading the variable documentation on both using C-h v , which should tell you how to set it.
Note that, it launches a new frame (Window in Microsoft terminology) to display the manpage and would probably be in a different font. You can close the frame by pressing C-x 5 0.
Saturday, August 15, 2009
Getting DocView to work on Windows Emacs
Emacs 23 has a new document viewer called docview. From the manual
As a first step, M-x getenv RET PATH should show something like this.
C:\MiKTeX2.7\miktex\bin;C:\cygwin\bin;C:\cygwin\usr\bin;C:\gnu\bin\;....
Next configure all doc-view-*-program to point to the correct binaries.
Upto this point, docview will work to convert the DVI or PDF file to a PNG file. In order to view the files, you need to have Emacs with Image support. On Windows, the following DLLs are required to make it work.
glut32.dll
jpeg62.dll
libimage.dll
libpng12.dll
libpng13.dll
libpng3.dll
librle3.dll
libtiff3.dll
zlib1.dll
which can be had from downloading the following from the gnuwin32 site.
libpng-1.2.37-bin.zip
libpng-1.2.37-dep.zip
tiff-3.8.2-1-bin.zip
jpeg-6b-4-bin.zip
tiff-3.8.2-1-dep.zip
jpeg-6b-4-dep.zip
Note that the actual version numbers may differ. Just take only the DLLs and dump them in the Emacs bin directory. Or you could put them somewhere in the search path, so that you don't have to do this step every time a new Emacs version is released.
Docview should now work without a hitch. The added bonus is that other image formats can also be viewed within Emacs with all the other DLLs plugged in, in Image mode.
In my set up, docview program variables point to a mix of MiKTeX and Cygwin binaries and that has not created any issues so far.

is a viewer for DVI, Postscript (PS),and PDF documents. It provides features such as slicing, zooming, and searching inside documents. It works by converting the document to a set of images using the `gs' (GhostScript) command, and displaying those images.To get it to work on Windows, you'd at least need to have a working LaTeX system like MiKTeX. And also have ghostscript installed. Now, if you read the Docview info, this is all you need to get it work but on Windows you need to jump through additional hoops to view it.
As a first step, M-x getenv RET PATH should show something like this.
C:\MiKTeX2.7\miktex\bin;C:\cygwin\bin;C:\cygwin\usr\bin;C:\gnu\bin\;....
Next configure all doc-view-*-program to point to the correct binaries.
Upto this point, docview will work to convert the DVI or PDF file to a PNG file. In order to view the files, you need to have Emacs with Image support. On Windows, the following DLLs are required to make it work.
glut32.dll
jpeg62.dll
libimage.dll
libpng12.dll
libpng13.dll
libpng3.dll
librle3.dll
libtiff3.dll
zlib1.dll
which can be had from downloading the following from the gnuwin32 site.
libpng-1.2.37-bin.zip
libpng-1.2.37-dep.zip
tiff-3.8.2-1-bin.zip
jpeg-6b-4-bin.zip
tiff-3.8.2-1-dep.zip
jpeg-6b-4-dep.zip
Note that the actual version numbers may differ. Just take only the DLLs and dump them in the Emacs bin directory. Or you could put them somewhere in the search path, so that you don't have to do this step every time a new Emacs version is released.
Docview should now work without a hitch. The added bonus is that other image formats can also be viewed within Emacs with all the other DLLs plugged in, in Image mode.
In my set up, docview program variables point to a mix of MiKTeX and Cygwin binaries and that has not created any issues so far.
Thursday, August 13, 2009
Sending Emails from the commandline Part 2
Previously I had posted about my challenges in getting attachments sent and being seen reliably as one in most MUAs. Spending a few more days pottering around and testing, I finally settled on mpack. Using mutt, I could not get it to work consistently in an cygwin environment as it kept dumping core.
I downloaded mpack, ran make and then installed it. Based on the manpage and a few examples on the web, I rejigged my shell script use mpack to generate a MIME encoded mail message, wrapped with msmtp needed headers and mailed it out. Works flawlessly.
So far.
The code snippet below is the way I used it.
function send_it () {
/usr/sbin/msmtp -t < $1
echo "mail send status" $?
if [ $? -ne 0 ]; then
echo "Fail: $1 file not deleted"
else
echo "Success: Removing $1 file"
rm $1
fi
}
function mpack_it () {
# outmimefile, zip file name, tmpfile split_part
mpack -s "Generated Output $4 : `date` " -o $1 $2
#change inline to attachment, insert msmtp needed headers and delete message-ID
sed 's/inline/attachment/' $1 | sed "/^Subject:/i\
To: \nFrom: xxxxxx@gmail.com \nBcc: $LIST"|sed '1,1d' > $3
}
The attachment itself is base64 encoded.
This solution, I'm reasonably satisfied with, though I'm pretty sure when I try an all Win32 solution, it might flame out spectacularly.

I downloaded mpack, ran make and then installed it. Based on the manpage and a few examples on the web, I rejigged my shell script use mpack to generate a MIME encoded mail message, wrapped with msmtp needed headers and mailed it out. Works flawlessly.
So far.
The code snippet below is the way I used it.
function send_it () {
/usr/sbin/msmtp -t < $1
echo "mail send status" $?
if [ $? -ne 0 ]; then
echo "Fail: $1 file not deleted"
else
echo "Success: Removing $1 file"
rm $1
fi
}
function mpack_it () {
# outmimefile, zip file name, tmpfile split_part
mpack -s "Generated Output $4 : `date` " -o $1 $2
#change inline to attachment, insert msmtp needed headers and delete message-ID
sed 's/inline/attachment/' $1 | sed "/^Subject:/i\
To: \nFrom: xxxxxx@gmail.com \nBcc: $LIST"|sed '1,1d' > $3
}
The attachment itself is base64 encoded.
This solution, I'm reasonably satisfied with, though I'm pretty sure when I try an all Win32 solution, it might flame out spectacularly.
Subscribe to:
Posts (Atom)