Emacs Micro Lesson #9: Start a Process From Emacs Lisp

That’s right. Let’s take a common scenario. You’re doing some work in Emacs. Maybe you’re programming. Maybe you’re writing a novel. It doesn’t matter what you’re doing. You want to start playing some music or run a backup of your files in the background. You can open up a different app or your terminal emulator and run a command.

What if you could run that background process from Emacs with a simple function? One and done. That’s how we like to do things in the Emacs world. The best way to get this done is to use a function called (you guessed it already), start-process.

In the example below, I want to start the Audacious media player. But specifically, I’d like it to open a particular playlist. For example, I have a playlist for the Outrun video game soundtrack (Sega Genesis version). This bit of Emacs lisp will start up Audacious and provide a file path to the program as an argument:

(defun outrun-play ()
"Startup Audacious music player, playing Outrun playlist."
(interactive)
(start-process "Outrun soundtrack" nil "audacious" "/home/user/Music/outrun.m3u" ))

Let’s break with this function down:

defun
the function to define the function
“Startup…”
an optional documentation string, to briefly explain what the function does
(interactive)
sets this function as an interactive function, able to be called as a command in the Emacs minibuffer
(start-process…)
The actual meat of the function, what the function is doing

The start-process function accepts a series of arguments:

(start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS)
  • NAME: The name of the process, which you can use to identify it. If you try to close Emacs while a process is running, you’ll get a warning, and you’ll be asked if you want to stop the process. The name you put here will appear in the warning test.
  • BUFFER: nil because we don’t use this argument in the function we’re writing here.
  • PROGRAM: The name of the program you are invoking with this function. In our example, audacious.
  • PROGRAM-ARGS: Arguments you want to provide to the program. In our example, we are merely providing a file path.

Be the first to comment

Leave a Reply

Your email address will not be published.


*