ielm – the interactive Emacs Lisp REPL

It is a wonderful thing to learn more about one’s tools.

I recently was trying to debug some Elisp code (specifically, I was writing a custom Org Mode capture function), and the behaviour was sufficiently non-obvious that I said, “I sure wish there was a REPL so I could run some test cases for my function interactively.”

Upon searching for it, I discovered that this was a built-in feature, ielm, and, to a certain bit of personal disappointment, I had probably been oblivious of this feature for 20 years or so.

Emacs is more than large enough to contain mountains of “little features” like this unnoticed.

By the way, here’s the code that took some debugging…

(defun before-gtalug (date)
  (let* ((now (decode-time)) ;;; take today
         (day (elt now 3)) ;;; extract month, year, dst, zone
         (month (elt now 4))
         (year (elt now 5))
         (dst (elt now 7))
         (zone (elt now 8))
;;; construct the 8th of the month, which is the earliest possible GTALUG date
         (eighthday (encode-time 0 0 0 8 month year zone))
;;; decode it…
         (eighth-decoded (decode-time eighthday))
;;; Grab the day of the week…
         (eighth-dow (elt eighth-decoded 6))
;;; How many days until the next tuesday???
         (days-to-tuesday
          (mod (- (+ 9 eighth-dow)) 6))
         (gtalug-tuesday (+ (elt eighth-decoded 3) days-to-tuesday)))
  (<= day gtalug-tuesday)))
(defun gtalug-month-label ()
  (let* ((now (decode-time)) ;;; take today
         (month (elt now 4))
         (year (elt now 5))
         (is-before-gtalug (before-gtalug now)))
      (if is-before-gtalug
        (format-time-string "GTALUG Q&A Topics for %B %Y"
                            (encode-time 0 0 0 1 month year))
        (if (= month 12) ;;; if december, then shift to next year
            (format-time-string "GTALUG Q&A Topics for %B %Y"
                                (encode-time 0 0 0 1 1 (+ year 1)))
(setf gtalug-month-label "")
(defun set-gtalug-month-label ()
   (setf gtalug-month-label (gtalug-month-label))
   (setq org-capture-templates
      `(("a" "Afilias" entry (file+headline "~/GitConfig/OrgDiary/Afilias.org" "Afilias")
         "* %?\nEntered on %U\n %i\n WhereWasI:%a")
        ("g" "GTALUG" entry (file+headline "~/GitConfig/OrgDiary/notes.org"
             ,(gtalug-month-label)
        )
      "* %?\nEntered on %U")
      ("n" "Note" entry (file+olp+datetree "~/GitConfig/OrgDiary/notes.org")
       "* %?\nEntered on %U\n %i\n WhereWasI:%a"))))

;;; Set a timer to re-evaluate the template about once a day
(run-at-time "1 sec" (* 3600 24) #'set-gtalug-month-label)

Designing SQL Schemas in Lisp

I have, as a task, designing a fairly rich DBMS schema for some future systems.  I’m not keen on doing a whole lot of by-hand reconciliation, so am taking a somewhat different approach.  I am analytically describing the relationships between tables, and then generating SQL DDL from the results.

I am doing some of the intermediary work in Lisp; probably a little bit dubious, but the translation is making sure that everything that I do have is  pretty strictly defined.  I created Lisp “primitive” functions such as “make-table”, “add-column” which do a little bit of data validation, and do any SQL generation in one spot.  The more interesting part is then to characterize the creation of various sorts of tables:

  • first-class-object indicates a table that sits on its own.
  • make-subobject indicates a table that depends on one of the “first class” objects.
  • make-association-table indicates a table that links two “first class” objects together.
  • make-transaction-stream indicates a table that contains transactions/activities
  • make-queue sets up a table that will contain work that is to be processed (and, presumably, cleared out upon completion)

The more structure that may be characterized in a fashion that makes it amenable to automation, the better 🙂