Little Elisp Workflow Enhancements

Published November 12, 2024 · 315 words · 2 minute read

Every week at work, I type a line in my org notes like so:

*** 2024-W46 [2024-11-11 Mon]--[2024-11-15 Fri]:

Clicking this date range opens the five-day agenda view for the week, and allows me to visualize all the meetings and date-stamped events I’ve recorded for the week in question.

Engineering and computer science types of people love to spend hours automating away minor inconveniences. Living in a LISP interpreter enables these automations to be written quickly, re-used, and improved over time without the crazy hassle of writing personal extensions in other popular text editors.

Now, rather than typing the line above, I can type M-x r/new-week and have this line appear with no effort at all, saving twenty or so keystrokes a week.

Here’s the full source code:

;;; work.el --- Work related Elisp    -*- lexical-binding: t; -*-
;;; Commentary:
;; Some simple functions to hack on for work at IBM.
;;; Code:

;; Libraries
(require 'cl-lib)
(require 'org)
(require 's)

(defun get-day (time day-of-week)
  "Given the current TIME and DAY-OF-WEEK, find this time on the last existing Monday."
  (let ((weekday (decoded-time-weekday (decode-time time))))
    (message "Weekday: %s" weekday)
    (cond
     ((= weekday day-of-week) time)
     ((> weekday day-of-week) (time-subtract time (days-to-time (- weekday day-of-week))))
     ((< weekday day-of-week) (time-add time (days-to-time (- day-of-week weekday))))
     (t (error "Error! Imaginary time")))))

(defun get-monday (time)
  "Given the current TIME, return the same time on Monday."
  (get-day time 1))

(defun get-friday (time)
  "Given the current TIME, return the same time on Friday."
  (get-day time 5))

(defun r/new-week ()
  "Prints a header for the current week's notes to the org file."
  (interactive)
  (org-insert-heading)

  ;; Insert the current-week heading
  (let ((now (current-time)))
    (insert (format-time-string "%Y-W%W " now))
    ;; Org Timestamp Format: [2024-11-12 Tue 10:00]
    (insert (format-time-string "[%Y-%m-%d %a]--" (get-monday now)))
    (insert (format-time-string "[%Y-%m-%d %a]:\n" (get-friday now))))

  ;; Optionally add a blank child heading:
  ;; (org-insert-heading)
  ;; (org-metaright)
  )

(provide 'work)
;;; work.el ends here

Comments