Searching for Blog Posts by Title

Last year I wrote about how I search for a blog post’s org file by the post’s title. That involved bringing up a dired buffer for the directory containing the source org files and doing a dired-do-search for a regular expression that looks like

TITLE:[ ]+Blog-Post-Title

That works well but it’s a little fussy because you have to mark all the org files in the dired buffer and the regular expression is tedious to enter. I finally decided it was time to truly automate this chore.

The following code prompts for the title (ignoring case) and then pops up the relevant file in a new buffer.

 1: (defun jcs-search-by-title (title)
 2:   "Search for a bog post source by title."
 3:   (interactive "sTitle: ")
 4:   (dolist (f (directory-files "~/org/blog" t ".*\\.org"))
 5:     (with-temp-buffer
 6:       (insert-file-contents f)
 7:       (let ((case-fold-search t))
 8:         (if (search-forward-regexp (concat "#\\+TITLE:\\s-*" title) nil t)
 9:           (progn
10:             (find-file f)
11:             (return t)))))))

The directory-files on line 4 returns at list of all the .org files in the directory that contains blog posts source files. Each of those files is placed in a temporary buffer on line 6 and a regular expression search is performed for the title line. If the search succeeds, the file is loaded into a new buffer ready for editing.

It doesn’t seem like much of an improvement over the old method but it makes it enough easier that I use it instead of just trying to guess the file’s name. I just love the way that Emacs’ extensibility allows me to optimize my work flow.

This entry was posted in Programming and tagged , . Bookmark the permalink.