Robert Melton's Writings

Emacs Lisp Sorcery: Taming the Chaos of Scratch Files

Listen up, code jockeys. If you’re drowning in a sea of disorganized notes and half-baked ideas, I’m about to throw you a lifeline. Today, we’re diving into some Emacs Lisp magic that’ll make your scratch file workflow so smooth, you’ll wonder how you ever lived without it.

Now, I know what you’re thinking. “Oh great, another Emacs evangelism post.” But stick with me here. This isn’t about preaching the gospel of GNU. This is about solving real problems with code. So buckle up, because we’re about to turn your digital notebook chaos into a finely-tuned machine.

Here’s the spell book:

(defvar my-scratch (expand-file-name "~/projects/robertmeta/scratch/"))

(defun auto-commit-and-push-directory (directory-path)
  (interactive "DDirectory: ")
  (let ((default-directory directory-path))
    (when (file-directory-p directory-path)
      (message "Making a push for directory: %s" directory-path)
      (magit-stage-file directory-path)
      (let ((commit-message (format "Automatic commit at %s" (current-time-string))))
        (magit-commit-create (list "-m" commit-message)))
      (message "Pushing changes")
      (magit-push-implicitly "origin"))))

(defun auto-pull-directory (directory-path)
  (interactive "Directory: ")
  (let ((default-directory directory-path))
    (when (file-directory-p directory-path)
      (magit-pull-from-pushremote nil))))

(defun open-scratch-file-with-date ()
  "Open or create a file with the format in the directory specified by `my-scratch` and position the cursor at the bottom."
  (let ((file-name (format-time-string "")))
    (find-file (expand-file-name file-name my-scratch))
    (goto-char (point-max))))

These three little functions? They’re the digital equivalent of finally organizing that junk drawer in your kitchen. Except instead of old batteries and takeout menus, we’re talking about your brilliant (and not-so-brilliant) ideas, meeting notes, and all the random crap you need to remember.

Here’s the deal: I’m a scratch file junkie. Code idea? Scratch file. Meeting? Scratch file. Random conversation that might be useful someday? You guessed it, scratch file. But having a million text files scattered across your system is about as useful as a chocolate teapot.

That’s where this setup comes in. It keeps all your scratch files in one Git repo, synced between computers faster than you can say “where did I put that note?” And with everything in one place, you can unleash the full power of tools like deadgrep and Magit. It’s like giving your brain a search engine and version control.

The best part? Creating a new scratch file is now easier than ordering takeout. One command, and boom - you’ve got a fresh .org file, named by date, ready for your stream of consciousness.

Is it perfect? No. Am I a “hardcore single org-mode file” user yet? Also no. But it’s a damn sight better than the digital landfill most of us are working with.

So there you have it. A simple setup that’s been more life-changing than any productivity app or self-help book. It’s not going to solve world hunger or bring about world peace, but it might just save your sanity. And in this industry, that’s worth its weight in gold.

Now if you’ll excuse me, I’ve got some scratch files to write.