Archives September 2026

Location-Based Themes in Emacs

There are lots of Emacs themes out there. People mostly use them to change colors. That’s great, but you can do more. As the documentation says:

Custom themes are collections of settings that can be enabled or disabled as a unit.

That can be any setting, not just colors.

I use Emacs in multiple situations. I use it at home, I use it in the office, I use it when I’m working from home, and so on. And there are settings that change depending on how and where I’m using Emacs, the most obvious of which being user-mail-address, which I want to set to user@home.org when I’m sending personal mail from home, and user@work.com when I’m sending professional mail from work. I have other settings that change between locations, like the directory where org files go, because reasons. Your location-dependent settings will be different.

So it seems the natural way to deal with this is to put all of the location-dependent settings in a theme, and load whichever theme is appropriate for what you’re doing. Since themes are groups of settings that can be turned on or off as a group, you can even switch themes in the middle of an Emacs session. For instance, if you’re a consultant working with two clients, you can enable theme client1 in the morning; and then, in the afternoon, disable theme client1 and enable theme client2 to update your settings.

To start with, let’s set up a couple of themes called personal and work, for personal and professional stuff, respectively. Check the value of custom-theme-directory. By default, Emacs looks for themes in the same directory as init.el, but I like to put them in a separate subdirectory, ~/.emacs.d/themes. Customize this to your taste. At any rate, that’s the directory where you’ll be creating theme files. For the rest of this post, I’ll assume that you’re using ~/.emacs.d/themes.

Theme files should go in a file named <theme-name>-theme.el. So create the file ~/.emacs.d/themes/personal-theme.el to hold the personal theme:

(deftheme personal
  "Settings when working on personal projects."
  :family "location")

(custom-theme-set-variables 'personal
 '(user-mail-address "bob@home.org")
 ;; Add additional variables here.
 )

(provide-theme 'loc-personal)

You can now run M-x load-theme personal to load the theme. Theme files contain Emacs Lisp code, which can be a security risk, so the first time you load it, or any time you make any changes, Emacs will ask you to confirm whether to load the theme, and whether to mark it as safe in the future. If you say yes, the next time it’ll just load the theme asking for confirmation.

Follow the same steps to create a work theme.

Once you have something that works reasonably well, you’ll probably want something that loads the right theme when Emacs starts. For this example, we’ll assume that if the hostname is my-laptop, then you’re working on personal things and want to load the personal theme, while if the hostname is office-workstation, then you’re at work and want to load the work theme.

Add something like the following to your ~/.emacs.d/init.el:

(add-hook 'emacs-startup-hook
  (lambda nil
    ;; Figure out which theme to load, depending on which machine this
    ;; is running on, and which options were specified.
    (let ((loc-theme 'unknon)
          )
      (cond
       ;; Running on personal laptop.
       ((string= system-name "my-laptop")
        (setq loc-theme 'personal)
        )

       ;; Running on work machine.
       ((string= system-name "office-workstation")
        (setq loc-theme 'work))

       ;; Add any other locations/work modes here.
       ))

      ;; Check whether the theme exists. If it does, load it.
      (if (member loc-theme (custom-available-themes))
          (progn
            (load-theme loc-theme)
            )
        (warn "Can't find location theme \"%s\"" loc-theme)
        ))))

Here, we’re just looking at the hostname, but obviously the condition can be arbitrarily complex. You might pick different themes depending on the day of the week, or whether you’re ssh-ed in from another host, or whatever makes sense in your situation. Add or remove themes as necessary.

One advantage of doing things this way is separation of information. Maybe you want to make your Emacs setup publicly visible on github, but your work setup includes hostnames or other information that shouldn’t leave company premises. In this case, you can store your work-theme.el file on your company machine, perhaps in a separate directory. You can add an entry to custom-theme-load-path, a directory outside of ~/.emacs.d, so that your work theme doesn’t accidentally get added to the git repo that has your init.el and your publicly-visible themes.

There’s one problem that I haven’t found a good solution to: child themes. Let’s say you have your work theme, with a hundred work-related settings. But of those 100 settings, there are three that change depending on whether you’re logged in to host1 or host2.It would be nice to have a host1 theme that automatically includes everything from the work theme, plus the three settings that are specific to host1. I don’t see a good way of doing this. It might make sense to use literate programming to generate multiple *-theme.el files from one source .org file.

Customizing Pasted Strings in Emacs

(A version of this post was posted earlier, but disappeared. Sorry for any inconvenience.)

The Problem

At my day job, we use the Jira ticketing system, so it’s very useful to link to tickets in Org-mode files, to the point where I’ve defined a shortcut, [[jira:ABC-12345]], and an org capture template for tickets. But sometimes, I’ll copy a link from an email message or something, and C-y to paste it in Emacs, and then I get an ugly

 

https://jira.example.com/ticket/ABC-12345

That got me to wondering if I could rewrite the text before pasting it, and turn that URL into

The Transform Function

We’re going to need a function to do the transformation, so let’s get that out of the way. Here’s a function that takes a string to be yanked from the kill buffer, and returns the string that we actually want. The only thing to note is that I used cond instead of if because I’ll probably want to expand it later, as I find new patterns I want to transform.

(defun my-org-convert-links (str)
  "Given a string to yank, try to convert it to org format."
  ;; Use `cond' rather than `if' to make it easier to add other
  ;; patterns.
  (cond
   ;; Example Jira URL. Extract the ticket number.
   ((string-match
     "^https://jira.example.com/ticket/\\([A-Z]+-[0-9]+\\)$"
     str)
    (let ((ticket (substring str
                             (match-beginning 1)
                             (match-end 1))))
      (format "[[jira:%s][%s]]" ticket ticket)
      ))

   ;; Insert other transformations here. Whatever's useful for you.

   ;; Nothing special. Just return the string.
   (t str))
  )

Hooking into yank

In an ordinary buffer, the Emacs function for pasting is yank, as you can confirm with M-x describe-key C-y, or whatever you’ve bound it to. And if you look up its documentation, you’ll see that it can be customized using yank-transform-functions. This is a list of functions that are called with the string to be yanked. We’ve already written a transform function above, so all we need to do is add it to yank-transform-functions:

(add-to-list 'yank-transform-functions
             #'my-org-convert-links)

In this case, the syntax [[jira:ABC-123456][ABC-123456]] doesn’t make sense outside of Org-Mode buffers, so let’s make it specific to org-mode buffers: we’ll make yank-transform-functions buffer-local, and only add our function to it in Org-Mode buffers:

(add-hook 'org-mode-hook
  (lambda nil
    (make-variable-buffer-local 'yank-transform-functions)
    (add-to-list 'yank-transform-functions
                 #'my-org-convert-links)
    ))

Now, the more astute among you will have noticed that in Org-Mode, C-y is bound to org-yank, which performs other org-specific magic before pasting. Thankfully, it winds up calling the built-in yank, so the customization above still works.

And that’s about it! I can now have Jira links look pretty when I paste them into my Org-Mode notes, and a structure where I can easily add more transformations if and when I want them.

I’ll note that yanking is different from drag-and-drop, and is handled differently from Emacs. I want to set up something similar for that, but that’ll be a story for a different day.