[emacs-berlin] Criticize this little elisp snippet

jman emacs-berlin at city17.xyz
Sun Feb 18 23:30:45 UTC 2024


I wrote a simple function to create a stub for my blog posts.

It should respect the following:
- check if `unidecode-sanitize` if loaded (used to slugify the title)
- ask for the blog post title: if it's empty, show error message and return
- create a new buffer with the post frontmatter
- save the buffer in a specific location. Filename is today's date + slugified title. Asks to overwrite if file is existing.

I am a lisp newbie, how does it look? How can I improve it? Feel free to nitpick! :-)


(defconst save-location "~/somewhere")
(defun jman/create-blog-stub ()
  "Create a blog post stub. Enter a post title.
A filename with today's date and a slugified version of the title
will be created in SAVE-LOCATION."
  (unless (fboundp 'unidecode-sanitize) (error "Please install/load unidecode package"))
  (let ((post-title (read-from-minibuffer "Insert post title: "))
        (today (format-time-string "%Y-%m-%d" (current-time))))
    (if (string= "" post-title)
        (error "Please provide a title")
      (let ((filename (format "%s-%s.md" today (unidecode-sanitize post-title))))
        (switch-to-buffer filename)
        (insert (format "+++\ntitle = %s\n date = %s\ndraft = true\n+++\n\n"
                        (format-time-string "%Y-%m-%d %H:%M:%S%:z")))
        (write-file (format "%s/%s" save-location filename) t)))))

More information about the emacs-berlin mailing list