Using Quickrun in Emacs

Today I want to talk about Quickrun, an Emacs package by Syohei Yoshida. (Who has made a number of packages worth your time.) I hope to show you how Quickrun is useful, simple, and an effective tool in Emacs. You will need Emacs 24.1 or later for this to work, but that shouldn’t be much of a problem.

What it Does

Quickrun executes the contents of a buffer, nothing more.

The fundamental command is quickrun. Because the package binds no keys we need to do so ourself. My personal preference is to prefix all Quickrun-related keys with C-c q, and so I have the quickrun command bound to C-c q q.

With that in place, consider the following, simple C file:

#include <stdio.h>

int main(void)
{
        printf("Hello world!\n");
        return 0;
}

If we run C-c q q while in this buffer then another will appear showing us the program’s result output. Note that this program requires compilation. If you need to pass arguments to the compiler or interpreter than either use the universal C-u prefix or the quickrun-with-arg command. Using the prefix C-u C-u, or running the quickrun-compile-only command, will build and execute your program but will display no output.

Quickrun supports a large number of languages besides C. It not only executes buffers of code using such dynamic languages as Perl or Python, but many others. There are samples of every format with Quickrun can handle on the official site.

If you do not wish to execute the entire buffer then you can restrict Quickrun to a region, not surprisingly with the quickrun-region command. You select a region of code as you normally world and then run the command, possibly using the same prefixes as listed above.

Note: At this time of writing, quickrun-region does not work with Java.

A slight variation of this command is quickrun-replace-region. Instead of showing the output in another buffer, it replaces the selected region with the output of the code. I have found this useful for generating data sets, i.e. lookup tables of values kept in memory. Instead of writing out each number by hand, and potentially screwing up, I will write a small chunk of code in Perl or Python that will calculate and print those numbers for me. Then I run quickrun-replace-region on that code and get my data, less work and far less margin for error.

If your code reads from standard input then you may want to use quickrun-shell. It executes the code in Emacs’ Eshell, which you can use to interact with your code (e.g. answer prompts). When you are testing some code which requires human intervention, quickrun-shell is often your best bet.

Custom Behavior

Even though Quickrun understands a ton of programming and markup languages you can still extend it with your own behavior. Here’s an example:

I like using jrnl to maintain a personal journal of ideas. I write these in a text file and then feed it into jrnl via standard input. However, I use Quickrun to automate this process. First, the code:

(quickrun-add-command "jrnl"
    '((:command . "jrnl")
      (:exec . ("%c < %s"))
      (:default-directory . "/tmp")))

(add-to-list 'quickrun-file-alist '("\\.jrnl.txt$" . "jrnl"))

This tells Quickrun that if we use the jrnl command it should run the program jrnl and redirect standard input, i.e. our journal entry, into the program. The :exec part of the definition dictates this. Quickrun replaces %c with the name of what we give to :command while %s is the absolute path to the file containing our writing. For the sake of sanity we do all of those in the /tmp directory, but that’s not mandatory.

The final line, affecting quickrun-file-alist, tells Quickrun should run the jrnl rule whenever we run the quickrun command on a file ending with *.jrnl.txt. This is the primary way to ensure that certain rules only work on certain types of files.

Now let’s look at something slightly different.

(quickrun-add-command "markdown/doku"
           '((:command . "pandoc")
             (:exec . "%c --from=markdown --to=dokuwiki %o %s %a")
             (:default-directory . "/tmp"))
        :mode 'markdown-mode)

(quickrun-set-default "markdown" "markdown/doku")

This creates a rule for converting Markdown into DokuWiki syntax by using Pandoc. In most ways this is similar to our jrnl command; we name the command, Pandoc in this case, set a default directory for any temporary files, and write the actual command to execute as part of the :exec section. A big difference, however, is the inclusion of :mode 'markdown-mode at the end. This tells Quickrun the markdown/doku rule is only applicable to Markdown buffers. We cannot use it elsewhere.

We run into a problem though: Quickrun already has a built-in rule for handling Markdown documents. Using quickrun-add-command will not work here.

(quickrun-set-default "markdown" "markdown/doku")

This command tells Quickrun that our new markdown/doku should be the default for all Markdown documents. We can use this approach to override existing rules, e.g. we want to use something besides GCC or Clang for our C software.

Bonuses

Do you use the Helm or Anything packages? Because if so, Quickrun comes with support for them. So if you like either of those two systems you’re going to have an easy time adjusting to Quickrun.

Conclusion

Hopefully I’ve given you some ideas about what you can do with Quickrun and why it’s useful. Let me know if you have any questions, and happy hacking!

Advertisements

One thought on “Using Quickrun in Emacs

Add Your Thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s