Starting Gemlog Entries Faster Part 2 (publ. 2024-02-14)

For indexing the post quickly, I came up with this function:

(defun index-gemlog-post (index-path index-header-string &optional time)
  (let* ((post-file-name (buffer-file-name))
         (post-nondirectory (file-name-nondirectory post-file-name))
         (post-directory (file-name-directory post-file-name))
         (index-directory (file-name-directory index-path))
         (time (if time time (current-time)))
         (datestring (format-time-string "%Y-%m-%d" time)))
    (if (not (equal post-directory index-directory))
        (error "File %S does not appear to be in the same directory as index %S"
               post-file-name index-path))
    (let ((entry-string
           (concat
            "=> " post-nondirectory " " datestring " ")))
      (find-file index-path) ;; enough error handling?
      (beginning-of-buffer)
      (search-forward index-header-string)
      (forward-char 2)
      (insert entry-string ?\n))))
</pre>

<p>Currently it does not figure out the title on its own. But that shouldn't be too hard, as in principle I just need to pull it from the first line of the post, after figuring out whether or not to scrub header markup. Anyway, I just have to define a convenience function for this particular gemlog:</p>

<pre>
(defvar starlog-index-path (concat gemlog-directory "starlog/index.gmi"))

(defun index-starlog-post ()
  (interactive)
  (index-gemlog-post starlog-index-path "# Transmissions"))
</pre>

<p>One thing I am little nervous about is the use of find-file, as it does not necessarily do what I would want — throw a fatal error — under some exceptional cases. E.g., I found that if the file doesn't exist, then find-file will go ahead and create a non-writable empty buffer. But there are several convenient things about find-file, namely that (1) it opens the file in a buffer, or switches to that buffer, and (2) it can process Tramp compatible file names, so that the files can be files we are working with remotely through Tramp and SSH.</p>

<h2>Copyright</h2>

<p>This work © 2024 by Christopher Howard is licensed under Attribution-ShareAlike 4.0 International.</p>
<div><a href="https://creativecommons.org/licenses/by-sa/4.0/">CC BY-SA 4.0 Deed</a></div>

</main>
</body>

</html>