hack on match-paren

Posted in Emacs on February 5, 2010 by zhangda

I used to use a short code "match-paren" (http://grok2.tripod.com/) when I program, especially in Lisp where parentheses are everywhere. I like this piece of code, for its simplicity and usefulness: if you bind this code to something like M-[, and when you press M-[ on a "(", the cursor goes to the matching ")" automatically. This also works when mark is activated, so you can highlight the region between two matching "(" and ")" very easily. The original code is as follows:

(defun match-paren (arg)
"Go to the matching paren if on a paren."
(interactive "p")
(cond ((looking-at "\\s\(") (forward-list 1) (backward-char 1))
((looking-at "\\s\)") (forward-char 1) (backward-list 1))))

Then sometimes I found that the code does not always work intuitively, especially when I want to highlight a region with the matching "(" and ")", so I did the following hack:
(1) when you keep hitting the key-binding, e.g., M-[, the cursor jump back and forth between its original locations, not like the original code
(2) when the mark is active, the cursor jump to the matching parenthesis and move forward one step after reaching ")" or backward one step after reaching "(", so the highlighted region contains everything between (and including) the matching "(" and ")". I found this especially useful when you want to cut a list out when programming in Lisp.

Here is my hack.

(defun da-match-paren (arg)
"Go to the matching paren if on a paren."
(interactive "p")
(cond ((and mark-active (looking-at "\\s\(")) (forward-list 1))
((and mark-active (looking-back "\\s\)")) (backward-list 1))
((looking-at "\\s\(") (forward-list 1) (backward-char 1))
((looking-at "\\s\)") (forward-char 1) (backward-list 1))
))
(global-set-key (kbd "M-[") ‘da-match-paren)

another way to track literature

Posted in Research on February 4, 2010 by zhangda

I found using NIH or NSF grant number, such as NIH RO1-EB002123, is another good way to track a series of papers from a particular research group. Actually, the corresponding authors usually pay attention to which grants a paper should mention at the Acknowledgment part, so the papers having the same grant number are often automatically and carefully classified according to the big project they are related to. I think this is worth mentioning in my web note.

open a windows explorer at the path of the current buffer

Posted in Emacs with tags on February 3, 2010 by zhangda

I use emacs in Windows. As I edit some text based files in Emacs, sometimes I need to open a window at the path of the current buffer to visit other files. It would be nice if I can just press a key in Emacs, and have this window open. Here is my simple solution for this task in Emacs with Windows OS.

(defun open-buffer-path ()
“Run explorer on the directory of the current buffer.”
(interactive)
(shell-command (concat “explorer ” (replace-regexp-in-string “/” “\\\\” (file-name-directory (buffer-file-name)) t t))))

I bound this function to a key-binding:
(global-set-key [M-f9] ‘open-buffer-path)

Notes: the key points in the line
(shell-command (concat “explorer ” (replace-regexp-in-string “/” “\\\\” (file-name-directory (buffer-file-name)) t t)))
(1) shell-command call a command specified by a string from shell
(2) replace-regexp-in-string “/” “\\\\” replace the Unix-style path (using /) to Windows-style paht (using \), the optional arguments “t t” let replace-regexp-in-string replace literally.

how to configure NIST TNT into cygwin gcc environment

Posted in Medical Imaging, Research on November 27, 2009 by zhangda

NIST TNT and JAMA are very useful libraries designed for complex matrix based computation. The libraries are purely headers containing template based classes and subroutines. To configure the TNT into a cygwin/gcc programming environment, the following can be done:

1. download TNT and JAMA from nist.gov (http://math.nist.gov/tnt/download.html)

2. extract the header files into /usr/local/include/tnt

3. test a cpp file:

#include <tnt/tnt.h>
#include <iostream>
using namespace TNT;
using namespace std;
int main(void)
{
int M = 3;
int N = 3;
Array2D< double > A(M,N, 0.0); /* create MxN array; all zeros */

for (int i=0; i < M; i++)
for (int j=0; j < N; j++)
A[i][j] = i*10+j; /* initalize array values */

Array2D< double > B = A.copy(); /* create a new copy */

Array2D< double > C(B); /* create a new view of B */
/* Both arrays (B & C) share data */
for (int i=0; i<M; i++)
for (int j=0; j<N; j++)
cout<<"A["<<i<<"]["<<j<<"] = "<<A[i][j]<<endl;
return 0;
}

4. write a makefile

CPP = g++
OFLAG = -Wall -o
LFLAG = -l
IFLAG = -I
LIBFLAG = -L
LIBDIR = /usr/local/lib/
INCLUDEDIR = /usr/local/include/TNT/
DEBUGF = -g -D DEBUG
DEBUG = no

.SUFFIXES: .exe .cpp
.cpp.exe:
$(CPP) $(OFLAG) $@ $<
$@

try.exe: try.cpp
$(CPP) $(IFLAG) $(INCLUDEDIR) $(OFLAG) try.exe try.cpp

5. build, and run the try.exe. The result shows:

A[0][0] = 0
A[0][1] = 1
A[0][2] = 2
A[1][0] = 10
A[1][1] = 11
A[1][2] = 12
A[2][0] = 20
A[2][1] = 21
A[2][2] = 22

how to set up fftw for cygwin in Win XP

Posted in Medical Imaging, Research on November 27, 2009 by zhangda

FFTW is essential for image processing, reconstruction, and other imaging related projects.
How to integrate FFTW into cygwin-Emacs-gcc programming chain:
1. instruction to follow: the INSTALL file in the package

2. 3 steps to build: ./configure, make, make install

3. the files will be placed into
/usr/local/include
/usr/local/bin
/usr/local/lib

4. test a cpp file:

#include <fftw/fftw3.h>
#include <iostream>
using namespace std;
int main()
{
cout << "fftw3.h is included" << endl;
fftw_complex *in, *out;
fftw_plan p;
int N = 64;//size of the fft
in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
p = fftw_plan_dft_1d(N, in , out, FFTW_FORWARD, FFTW_ESTIMATE);
//now start initialize in
for(int i=0; i<N; i++)
{
in[i][0] = 1;
in[i][1] = 0;
}

fftw_execute(p);//actually run the fftw routine

for(int i=0; i<N; i++)
{
cout << "out[" << i << "] = " << out[i][0] << "+" << out[i][1] << "j" << endl;
}
fftw_destroy_plan(p);//
fftw_free(in);
fftw_free(out);
}

5. write the makefile correctly:

CPP = g++
OFLAG = -Wall -o
LFLAG = -l
IFLAG = -I
LIBFLAG = -L
LIBDIR = /usr/local/lib/
INCLUDEDIR = /usr/local/include/
DEBUGF = -g -D DEBUG
DEBUG = no

.SUFFIXES: .exe .cpp
.cpp.exe:
$(CPP) $(OFLAG) $@ $<
$@

main.exe: main.cpp
$(CPP) $(IFLAG) $(INCLUDEDIR) $(OFLAG) main.exe main.cpp $(LIBDIR)libfftw3.a

6. build

7. test the program main.exe

How to setup boost for cygwin in Win XP

Posted in Medical Imaging, Research on November 27, 2009 by zhangda

Boost is a super powerful and multi-platform C++ library. I always want to make use of this powerful tool. Here is the experience I had on how to install it for a Win XP box with cygwin:

1. the right instruction page to follow: Getting Started on Unix Variants (http://www.boost.org/doc/libs/1_41_0/more/getting_started/unix-variants.html#prepare-to-use-a-boost-library-binary)
2. which package to download: the source code package, e.g., from sourceforge.net (http://sourceforge.net/projects/boost/files/boost/), the version I downloaded was: boost_1_41_0.zip

3. how to build: follow Section 5.1 of the instruction page “Getting Started on Unix Variants”

3.1 cd path/to/boost_1_41_0
3.2 ./bootstrap.sh –prefix=/path/to/installation
3.3 ./bjam
3.4 ./bjam install

4. in /path/to/installation there will be 3 folders generated: bin, include, lib

5. copy these 3 folders to /usr/local/ (this is the local folder for the user installed packages instead of the standard cygwin packages)

6. build the test c++ program with linking to boost library regex
6.1 sample cpp file:
boost.cpp

#include
#include
#include

int main()
{
std::string line;
boost::regex pat( “^Subject: (Re: |Aw: )*(.*)” );

while (std::cin)
{
std::getline(std::cin, line);
boost::smatch matches;
if (boost::regex_match(line, matches, pat))
std::cout << matches[2] << std::endl;
}
}

6.2 sample makefile
makefile

CPP = g++
OFLAG = -Wall -o
LFLAG = -l
IFLAG = -I
LIBFLAG = -L
LIBDIR = /usr/local/lib/
INCLUDEDIR = /usr/local/include/
DEBUGF = -g -D DEBUG
DEBUG = no

.SUFFIXES: .exe .cpp
.cpp.exe:
    $(CPP) $(OFLAG) $@ $<
    $@

main.exe: boost.cpp
    $(CPP) $(IFLAG) $(INCLUDEDIR) $(OFLAG) boost.exe boost.cpp $(LIBFLAG)$(LIBDIR) $(LFLAG)libboost_regex

7. build the cpp file

8. create the test file to test the regexp functionality used in the above boost.cpp: save the following to test.txt

To: George Shmidlap
From: Rita Marlowe
Subject: Will Success Spoil Rock Hunter?

See subject.

8. test the boost.exe by

test.txt > ./boost.exe

9. the results shows successfully:  Will Success Spoil Rock Hunter?

Key point:

1. find the good instruction to follow

2. copy the include, bin, lib folders into the right path: /usr/local/

3. write the makefile correctly (setup good parameters for the compiler gcc)

how to get ImageMagick work in Cygwin under Win Xp

Posted in Medical Imaging, Research on November 27, 2009 by zhangda

I tried to 3 methods, only one works well (without too much trouble) with Cygwin:

1. install the binary windows version directly ( ftp://ftp.imagemagick.org/pub/ImageMagick/binaries/). This works in Windows, but not in the cygwin flavor: the display.exe does not work under mintty or other bash shells. Even after I setup export DISPLAY=localhost:0.0, it still complains about: “display.exe: unable to open X server `(null)’ @ display.”

2. build from source code using the ImageMagick’s package (ftp://ftp.imagemagick.org/pub/ImageMagick/), but it seemed that there are some packages, such as QLR, are missing. And after configuration the “make” step failed.

3. searched if cygwin contains a ImageMagick package from the online cygwin package list (http://www.cygwin.com/packages/), it turn out to be in the “graphics” category. After installing it, display.exe was still not working. Then I started the X-Server, and included

export DISPLAY=localhost:0.0

into ~/.bashrc. Then it works.

Using Emacs and org mode for quick generating of presentation slices

Posted in Emacs on October 23, 2009 by zhangda

Following emacs-fu’s post on how to generate pdf slices using Emacs and org-mode,  I found some tricks to write down:

(1)  when include an image in the sub folder, the formula is:

#+LaTeX:\includegraphics[height=0.45\paperheight]{img//Scan_Type_dialog.jpg}

or use the latex code directly:

\begin{figure}[H]
\centerline{\includegraphics[height=0.45\paperheight]{img/Scan_Type_dialog.jpg}}
\caption[]{Scan Type Selection Dialog \label{fig:scan_type_dialog} }
\end{figure}

(2) structure

* big section

** slice title

*** sub item

**** sub sub item

italic font in org-mode

Posted in Emacs on October 18, 2009 by zhangda

I am trying to follow Emacs-fu’s wonderful post on how he use org-mode in various tasks, including note taking and presentation preparation. He mentioned in his post that /word/ in org mode can change the display font into italic (/word/, which seems quite cool. But this did not work for me at the beginning. I did a little research on this issue and found the following problem:
(1) in my own switch-color-theme-matlab-latex.el, which setups several themes I daily use and switch among, the italic was not defined properly: I put too many entries in that face, but ignore the most fundamental one: :slant italic.
(2) the font I am using (Andale Mono) for windows does not work very well with Emacs’s italic display. I changed my default to Bitstream Vera Sans Mono, which is both available in Windows and Linux.

After this changes, the /italic/ works well, and the font actually saves me more display area.

Excel break external links

Posted in Uncategorized on October 7, 2009 by zhangda

From the MS Excel help I found this useful information:

Important When you break a link to a source, all formulas that that use the source are converted to their current value. For example, the link =SUM([Budget.xls]Annual!C10:C25) would be converted to =45. Because this action cannot be undone, you may want to save a version of the file before you start.

On the Edit menu, click Links.

In the Source list, click the link you want to break.

To select multiple linked objects, hold down CTRL and click each linked object.

To select all links, press CTRL+A.

Click Break Link.

If the link used a defined name, the name is not automatically removed. You may want to delete the name as well.

How?

On the Insert menu, point to Name, and then click Define.

In the Names in workbook list, click the name you want to change.

Do one of the following:

Change the name
Type the new name for the reference, and then click Add.

Click the original name, and then click Delete.

Change the cell, formula, or constant represented by a name
Change it in the Refers to box.

Delete the name
Click Delete.