Template Mode for Emacs

If you write a lot of R-, Stata-, Gams and/or LaTeX-files templates can make your life more relaxed. Emacs doesn’t have a template system (well there is auto-insert, but I did not find enough information on how to use if for templates). I used to have some templates in a directory and usually I would start with them (and forget that I should save it under another name…). The disadvantage is that you have to jump around in the file to fill out the information you want to have at the top of the file (like project name, topic, file name etc.). Today I discovered templatemode (see http://emacs-template.sourceforge.net/details.html). It works like a breeze and makes my writing life much easier.
I have templates for the above mentioned file types. The Gams template looks like this:
Now if I start a new file, TemplateMode asks me if I want to use the Gams-template, it inserts all the information between >>> and <<< or asks for information to insert. For example DIR, DATE will grab the directory and the date. At the end of the template file you see pairs of words. These words define the interactive part. Templatemode asks me for the project name with Project:, the job with Job: and the Gams title with Title:.
The line with the Version Control Information grabs information from my verison control system as soon as I commit the file (and is not part of templatemode).
This is how a new Gams file would look like:
Installation is easy and you can find lots of examples in the directory.

Emacs and Stata

Although I am not a great fan of multi-tasking I often use a lot of programs simultaneously for one task: For example, when I am writing a report in Word or with Emacs, I take notes in Emacs, search the internet and have my model results in the gdx-viewer.  Additionally my mail client is somewhere in the background and nowadays I have a messenger program open to talk to my collaborators. I don”t like this jumping from one program to another, but it is often necessary. This is also one of the reasons I switched to Emacs, because I can do my main work in Emacs (modelling, note taking, statistics and report writing).
For my statistical work I use R, SPSS, Eviews and Stata. Emacs has a nice mode for working with R (ESS: Emacs-Speaks-Statistics). You can edit your scripts and  run it  in R without leaving Emacs. ESS has highlighting and lots of other features. ESS supports Stata too, but you can”t run your Stata commands in Emacs. So you have to copy-paste from Emacs to Stata. The editor of Stata (I use version 10) is  a plain text editor without syntax highlighting and almost no other features (version 11 has a much better editor but the update is too expensive).
So what can you do, if you still want to use Emacs (or any other text editor other than the Stata editor)? Use a lot of keyboard strokes switching back and fort from one program to another is one option, but here you run into other problems: if you have multi line commands copy-paste doesn’t work for Stata as you have to strip off the linefeeds. OK, you can write your commands on one line, but then you have to adjust your output if you want to document your scripts nicely. Another more practical way is using autohotkey, a free, open-source utility for Windows
(there are similar programs for Unix or Mac). Here are the great features of Autohotkey. With Autohotkey, you can (taken from the manual):
  • Automate almost anything by sending keystrokes and mouse clicks. You can write macros by hand or use the macro recorder.
  • Create hotkeys for keyboard, joystick, and mouse. Virtually any key, button, or combination can become a hotkey.
  • Expand abbreviations as you type them. For example, typing “btw” can automatically produce “by the way”.
  • Create custom data-entry forms, user interfaces, and menu bars. See GUI for details.
  • Remap keys and buttons on your keyboard, joystick, and mouse.
  • Respond to signals from hand-held remote controls via the WinLIRC client script.
  • Convert any script into an EXE file that can be run on computers that don”t have AutoHotkey installed.
For the problem described above I wrote a small script and linked it to the key combination Windows+s.
The script looks like this:

#s::                               ; the short cut (Windows key + s
clipboard =                      ; Empty the clipboard.
Send !w                          ; Copy the highlighted region in Emacs (Alt-w)
; Replace /// (necessary in Stata for multiline commands) and line feeds:
StringReplace, clipboard, clipboard, ///rn,  , All
WinActivate, Stata/SE       ; Activate the Stata window
Send ^v                           ; Copy the clipboard (Ctrl-v)
Send {Enter}                     ; Execute the commands
; Jump back to Emacs (Renger”s Emacs Mode is my Emacs window title)
WinActivate, Renger”s Emacs Mode      

If I mark a region in my Stata script in Emacs and hit Windows+s, the line is copied to the clipboard, all line end feeds (only in multi line commands) are stripped off and the edited region is copied to Stata and executed. After the execution the cursor jumps back to Emacs. This with just one key stroke!
I use Autohotkey for lot of other tasks (one recently was taking a sample from a telephone directory and copying the addresses to an Excel sheet). Another nice feature I implemented is automating copy-paste from a pdf-file I am reading to a text file with my notes. Once again: one click and the text is in my notes.