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-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
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.