Send Emacs Buffer to jrnl

Lately I’ve been using the program jrnl to keep a journal of personal information. You can write entries completely from the command-line but I dislike that approach because editing text is a pain. Since I am a GNU Emacs junkie I wanted to use that program for all of my writing. Today I want to share an extremely simple function I wrote for that purpose.

You can add a journal entry by feeding it in via standard input, e.g.

$ jrnl < /tmp/entry.txt

It does not take that much effort to write such a file in Emacs and then run the command above. But I am lazy, like most programmers. Instead of taking the minuscule time to save a file and enter one shell command I wrote the following:

(defun ejmr/send-buffer-to-jrnl ()
  "Sends the content of the current buffer to jrnl."
  (interactive)
  (call-process-region (point-min) (point-max) "jrnl")
  (message "Saved buffer contents in journal"))

This illustrates some very basic Emacs Lisp.

First, we have to get the entire contents of the buffer. The functions point-min and point-max give us the buffer’s full length. We could also get the buffer contents by calling buffer-string, but that would not be useful here, the reason being…

Second, we have to give a region to call-process-region. The first two arguments indicate the starting and ending position of the region we want to use. This is why we use the point-* functions instead of buffer-string.

The third and final argument is simply the name of the program which will receive the region via standard input. The result is the same as if we’d executed jrnl < entry.txt where the file entry.txt contains everything in the current buffer. After that all I had to do was bind the command to a key.

As I said, this is rudimentary Emacs Lisp, but I hope it demonstrates how simply you can pass the contents of a buffer onto another program.

Advertisements

2 thoughts on “Send Emacs Buffer to jrnl

  1. Nice post!

    You can also use isend (I use it to send code to irb, python, erlang-shell, etc), see section 2 of http://wenshanren.org/?p=351.

    BTW, do you know how to get the output of a process? For example, if I have irb running, after sending “puts 1+2” to it, I’d like to know the result (which is 3) so I can show it in the mini-buffer.

  2. I’m happy to hearyou like the post :) And thank you for the suggestion of isend.

    BTW, do you know how to get the outut of a process?

    One way to do this is by using ‘process buffers’. First you create a process for the program you wish to run, e.g. (start-process “irb-process” “irb” “irb”). This creates and returns a process called ‘irb-process’ that has an associated process buffer called ‘irb“. You can then access that buffer by calling (process-buffer) on the process you crated with (start-process). And from there you can yank text out ofhe buffer lik you would any other. I don’ do this very often though so there may be more simple approach. But I hope that helps!

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