Automatic operator formatting mode in Emacs

Emacs has a nice new mode called electric-operator developed by David Spepherd. It helps when you write code by formatting all operators in a predefined way. For example, in R it adds spaces around the operator signs, when you write 1+1, this is automatically converted into 1 + 1.

You can define your own way of formatting for other modes. For gams-mode you add the following to your .emacs file

First you set gams-mode as a mode on which electric operator can work.Then you add some rules. After that you set a hook so electric-operator is automatically started when you use Gams (or ESS).

A special formatting is necessary for the comments in GAMS as they start with the same symbol as the multiplication. With the help of David, I could add a function gams-mode-* that changes the “*” used as a comment in “* “ and otherwise in “ * “.  It checks if the “*” symbol is at the beginning of the line and in that case will not add a space in front of it.

It probably needs some more tweaking, and I will keep you posted on improvements.

Column guide in Emacs

Many professional editors have a column guide. For example, below you see in the Powershell editor a veritcal line at column 80.

image

This marker comes in handy, if you want to print your files. In Emacs I usually check for the column 80 in the status line.

image

However, I usually forget to check, and when I print out stuff or publish, I got line breaks where I don’t want them. Passing the 80th column for comments is not a problem, as a simple Ctrl-q forces Emacs to break the lines nicely and add comment symbols at the beginning.

image

But after hitting Ctrl-q, Emacs produces this:

image

There is, however, a package, that mimics column guide seen in other editors. The package is called “fill-column-indicator”. Installing this package will do the trick. After installing, you only have to add the following lines to your .emacs file:

image

I added two hooks, so the column rule appears automatically in Gams and ess.

More information can be found here: http://www.emacswiki.org/emacs/FillColumnIndicator

Using file templates in Emacs

A good practice in modeling is to place information related to the project your are working on, the subject, the data and your personal information like E-mail address at the top of your file. In Emacs you can define skeleton functions that will prompt for the information and put it in the file. Below is an example for a file with as comment symbol “**: (the $Id: $ is for my version control system, that automatically will add the information on the last commit in this line).

image

Below you see how to make a function using skeleton. As I don’t want to repeat myself, the function asks as an input the comment symbol(s) that should be used. I now can use the function for different programming languages (R uses “#” , C++ uses two forward slashes, GAMS “*”, etc.). You can add the time and date by using the command “current-time-string”. All other inputs that have skeleton-read in front of it, are also prompted for. If you need the template for your whole project team, you could just make the author and contact field also input-dependent.

image 

More information can be found at: SkeletonMode. (note that this is part of Emacs since version 22).

PS. The function puts an extra comment symbol at the end (not shown). This is a bug in my function. Any ideas how to get rid of this are welcome.

Using Org-mode to keep track of your project files

For every project I work on I have a org-file. Every file has the following structure:

  • Tasks
  • Repeat
  • Wait
  • Notes
  • Calls
  • Reading

I use remember-mode to capture tasks, notes and calls. Under the heading “Repeat” are tasks that are repeated on a regularly base. Under “Wait” are reminders for things I wait for (like a reply on an e-mail, an order, a call, etc.). These waits are also captured using org-remember (I have written about that in an earlier post). Under the heading “Reading” I have links to papers I am currently reading (using Bibtex; see an earlier post on that).

Sometimes it is nice to have information on the files in one of the project directories, if possible with links. This can be done with org-fstree. Just add (require ‘org-fstree) in your .emacs-file after installing. You now can add the following line somewhere in your org-project file:

#+BEGIN_FSTREE: d:/inbox/Gams

This will give me all the files in the directory d:/inbox/Gams with a direct link (this is my folder for answering questions in the Gams forums).

image

You can update this by putting the cursor in this line and hitting C-c C-c. The documentation gives you information on the options:

“#+BEGIN_FSTREE: <dir> :<optionname1> <optionvalue1> :<optionname2> <optionvalue2> …

Options are:

  • :non-recursive t , to suppress recursion into directories
  • :exclude-regexp-name <list of regexp strings> , exclude file/directory names matching either of the given regexp expressions
    • :exclude-regexp-name (“.*\\.pdf$” “.*\\.zip$”), excludes files/directories ending with either “.pdf” or “.zip”
    • :exclude-regexp-name (“^\\.git$“) , excludes files/directories named “.git”
    • – :exclude-regexp-fullpath <list of regexp strings>, same as :exclude-regexp-name but matches absolute path to file/directory
  • :relative-links t , generates relative instead of absolute links
  • :show-only-matches t , only files that are being linked to show up
  • :only-directories t , only directories are listed
  • :only-regular-files t , only regular files are listed
  • :dynamic-update t , [EXPERIMENTAL] dynamically update a subtree on visibility cycling.
  • :links-as-properties t, sets the links as properties Link1, Link2,… for use in column view [Does not work with dynamic-update!]
  • :no-annotations t, suppresses the search and display of file annotations

Following up on Outlook E-Mails in Org-Mode

The combination of Org-Mode and Remember-Mode helps me to organize my projects. One special task category is “WAIT”, if I have to wait for some input from somebody else or waiting for a delivery to arrive. An example is ordering a book at Amazon. As soon as I make the order, I generate a “WAIT” which would look like this

** WAIT [#C] [2014-09-16 Tu] Book on Modeling by Morgan

In this case, I ordered the book on September 9th and it has not a high priority. Once a week I check this category in a customized agenda view which sorts the WAITs according to their priority. If necessary, I take action (for example resending an E-Mail). 

One nuisance is that I use Outlook for my E-Mails and Org-Mode for my tasks.If I send an E-Mail and are waiting for an answer, or if I receive an E-Mail with information on my order, I can’t generate a direct link between the E-Mail and the WAIT-task and I loose time looking for the original E-Mail.

I googled around and found the following solution by John Hilliard here (there is org-outlook, but I did not find a good explanation, how that works). He suggests to write a macro in Outlook that looks like this

The macro gets the unique id for the email message and writes an Org Mode link to that message to the clip board.

Note that in order to write a macro, you have to activate the “Developer” tab in Outook, image

which can be done by right-clicking on the tabs and choosing “Customizing the Ribbon”. After defining the macro, you can add it to the quick access toolbar (same procedure as adding the developer tab), and choose a nice icon

image 

There might be an error, when running the macro, but you John nicely describes how to get rid of that one:

“When I first ran the macro, I actually got an error saying “Compile error: User-defined type not defined.” That was a little mysterious. In order to fix that error:

Click “Tools” then “References” in the menu.

Check “Microsoft Forms 2.0 Object Library” in the list of available references. 

  • This option wasn’t available to me at first so I clicked “Browse”
  • Then I opened “c:\windows\system32\FM20.DLL”

Then I clicked “OK”

I finally added the code for opening Outlook in my .emacs file.

(defcustom org-outlook-location (w32-short-file-name “c:/Program Files/Microsoft Office 15/root/office15/OUTLOOK.exe”)
  “* Microsoft Outlook 2013 location.”
  :type ‘string
  :group ‘org-outlook)

Now, I can select a message, click on the macro symbol which copies the link, jump to Emacs (I have a script for that) and insert the link. The WAIIT-Task would look like this: image

If I click on the Message-link, Outlook opens the message.

Using Preview for LaTeX documents in Emacs

Editing LaTeX documents with lots of equations can be sometimes hard if you want to refer to the equations in the text. You can use, if you have a big monitor, put the compiled file to the right of your editor, but on a notebook this is not a good option. If you use AucTex in Emacs, you can use the preview mode.

image

Here is the text without preview in Emacs And here it is with preview.

image

You can set the display options in the Option Group “AucTex” under “Preview Appearance”. One more thing: I always compile to pdf (not dvi). In order to generate the preview, you have to set this option back (C-c C-t C-p).

Removing extra blank lines in Emacs

If you want to get rid of extra blank lines in your text files, you can use the search and replace commands with regular expressions. However, for whatever reason, this does not work properly on my Windows machine.

Fortunately, I found a nice function definition at stackoverflow which does the job:

http://stackoverflow.com/questions/4419576/delete-extra-blank-lines-in-emacs

Get rid of tabs in Emacs

If you work with other on text files, you often run into the problem that one of the programmers is using tabs set to 4, another to 8 spaces. For this reason, I always save my files without tabs. In Emacs there is a command to do this “untabify-buffer”, but I usually forget to do this.

Searching the internet I found the following code at stackoverflow.com, to do it automatically as soon as you save the file.

You can define your own hooks for other modes.

If you would use the untabify-buffer as a common hook, you can run into problems, because every file will be untabified.

Using org-mode in outline-mode

I have written about using org-mode in gams-mode in an earlier post. Gams-mode lets you structure your gams file using headings and subheadings. These headings can be (partially) collapsed. The structure of your file could look like this:

image

You can now cycle through the structure using Shift-Tab. Tab opens a section:

image

This is a great help in complex files.

Gams-mode has another great outline feature for the listing file. Instead of putting a normal comment with a “*” in front, one writes a display statement that starts with a “@” (see the code above). We now see a nice heading in our listing file.

image

However, what is really fantastic, is the display of the actual loop state in the outline mode (in the listing file you can easily search for the loop element, but in the outline mode this is not possible). Now you can see the loop status in the above code after the “LOO” . Hitting “space” the listing will be shown in another window.

Managing papers with Org-Mode and Reftex

Writing papers means reading lots of papers, and I often lose track of all the papers I am reading: Where did I file a certain paper? Which paper did I start reading? Which paper might be interesting?

A reading list would be nice, but such a list should be easy to handle. I have all my papers catalogued in JabRef and use AucTex and RefTex for writing and Org-Mode for my tasks.The solution should therefore combine the strength of all three. I found a nice post on how to this (http://tincman.wordpress.com/2011/01/04/research-paper-management-with-emacs-org-mode-and-reftex/) and adopted it to my needs.

Here is how my solution looks like (for managing tasks with Org-Mode see one of my previous posts)

image

  1. For every paper I am writing I have an org-file with my tasks, reading list, notes, and calls.
  2. I have four lists for my reading:
    1. Actual reading
    2. Should read: papers I glanced through and that look promising
    3. Not yet read: lower priority papers to be read
    4. Already read
  3. I insert new papers by using C-c ) (as in LaTeX/AucTex/RefTex). More on that later. Automatically, a link to this paper and the title are inserted (I save all papers as pdf-file and named with the first author name and the year of publication. They are all saved in the same dropbox directory). With “C-c C-c” I can tag the entries for easy searching later on.
  4. If I want to work on my paper, I open the org-file and, if I want to read a paper, I just click on its link and start reading.
  5. If necessary I can make short notes directly under the link.

Org-Mode makes it possible to use RefTex. For this it is necessary to put the information on where the bib-file is (third line in the next figure)

image 

The .emacs file has to be adjusted as follows:

Note: as I can make the reading to a TODO or a MIT (most important task), it will also show up in my daily agenda and I can also start reading from here by clicking on the link:

image