Dump Your Currently Set Faces in Emacs

Posted on 26th of May 2024 | 639 words

Every once in a while I like to play around with Emacs themes. Of course, mainly due to yak-shaving and procrastination reasons and nothing actually valid. But it’s fun nonetheless, although occasionally very tedious. Tedious in a sense that Emacs tend to come with a lot of different faces, especially if you happen to use more than a few third-party packages.

Defining a custom theme in Emacs

Emacs comes with couple relatively straight forward way to set custom themes in the function custom-theme-set where you define the name of the theme and then the faces you want to modify:

(deftheme fancy-theme)

(custom-set-faces
 'fancy-theme
 '(default ((t (:foreground "black" :background "white"))))

 ;; [... and so on]
 )

You’re also able to control on what sort of displays your custom theme should support. For example, if you want to use theme in only terminals that support minimum number of 16 different colors, you’re able to define that like:

'(default ((((class color) (min-colors 16)) (:foreground "black" :background "white"))))

Of course that can be slightly verbose, so often you might see custom themes to be set like:

(deftheme fancy-theme)

(let ((class '((class color) (min-colors 16))))
  (custom-set-faces
   'fancy-theme
   `(default ((,class (:foreground "black" :background "white"))))

   ;; [... and so on]
   ))

But it still tedious since there is a lot of faces and there can be lot of variance between the faces so often if you want to write your own full fledged theme, it’s better to explicitly set all the faces that you plan to use. Mainly since, if the face is not set in the theme it’ll use that lastly set face for that face in question, so if you session has used multiple different faces, it can lead into some funky color combinations.

Currently in my Emacs session – which is relatively light package-wise – amount of currently set faces equals to 869.

Simple tool written for aspiring theme developers

This is why, I decided to write a simple Emacs Lisp helper function to dump all the currently set faces to a buffer where you can copy them to your own theme at will. I also made it so you can simply just dump the faces from a give package if you only care, e.g. about Magit’s faces.

(require 'cl-lib)

(defconst theme-dump--buffer-name "*theme-dump*")
(defconst theme-dump--default-format-string "%s %s ")
(defconst theme-dump--string-format-string "%s \"%s\" ")

(defun theme-dump-current-faces (pkg)
  (interactive (list (read-string "Dump faces from package (leave empty for all packages): ")))
  (let ((faces '()))
    (dolist (f (theme-dump--filter-faces-by-package pkg))
      (let ((attr-str ""))
        (dolist (a (face-all-attributes f))
          (setq attr-str
                (concat attr-str (theme-dump--format-attr-str f a))))
        (push (if (not (string-equal attr-str ""))
                  (format "`(%s ((,class (%s))))\n" f (string-trim-right attr-str))
                (format "`(%s (( )))\n" f))
              faces)))
    (let ((buf (get-buffer-create theme-dump--buffer-name)))
      (with-current-buffer buf
        (erase-buffer)
        (insert (mapconcat 'identity faces))
        (local-set-key (kbd "q") 'kill-buffer-and-window))
      (split-window-right)
      (other-window 1)
      (switch-to-buffer buf))))

(defun theme-dump--filter-faces-by-package (pkg)
  (cl-flet ((pkg-face-p (face)
              (cl-search pkg (symbol-name face))))
    (if (not (string-equal pkg ""))
        (cl-remove-if-not #'pkg-face-p (face-list))
      (face-list))))

(defun theme-dump--format-attr-str (face attr)
  (unless (equal (face-attribute face (car attr))
                 'unspecified)
    (cl-typecase (face-attribute face (car attr))
      (string (format theme-dump--string-format-string
                      (symbol-name (car attr))
                      (face-attribute face (car attr))))
      (t (format theme-dump--default-format-string
                 (symbol-name (car attr))
                 (face-attribute face (car attr)))))))

Essentially, this just gets all the faces with face-list and either returns all of the or filters them by package name if you want. Function prints the faces into a new buffer in a format that is already suitable for new custom themes:

`(magit-section-highlight ((,class (:extend t :background "grey95"))))
`(magit-section-heading ((,class (:weight bold :extend t))))
`(magit-section-secondary-heading ((,class (:weight bold :extend t))))
`(magit-section-heading-selection ((,class (:extend t :foreground "salmon4"))))
`(magit-section-child-count (( )))

;; and so on...

The printed faces can then easily be copied to custom-set-faces for your possible new theme. Faces are also backticked by default, in case you want to change faces to use variables instead of hardcoded names.

Code is in public domain and also available in sourcehut.

RIP Steve Albini

Posted on 7th of May 2024 | 15 words

This one hurts. Steve Albini has his hands in so many of my favourite albums.

Youtube video

Sunsetting Mastodon

Posted on 4th of May 2024 | 390 words

Some months ago I decided to join back to social media in the form of registering to Mastodon. I thought that maybe this could be the platform to fill the void – which I’ve later realised to be non-existent – of social medias in my life. But now I’ve decided to leave it as well. Not necessarily due to same reasons why I left social medias like Facebook, Instagram or Twitter. But still, partly due to same reasons.

One of the main reasons why I left mainstream social media platforms was their draconian behaviour behind the scenes. Addictive design principles behind their applications, algorithms promoting content that you didn’t ask for, data mining and profiling, and so on. Fortunately, in Mastodon, these sort of issues weren’t present, which was one of the reasons why it felt so welcoming. It had this weird old internet type of aura in it. Like being part of this weird digital clique.

Also, just from the pure technological point of view, the federated elements of Mastodon – or ActivityPub in general – seemed quite interesting. Finally, a platform competing with the centralized behemoths where users were able to retain control over the data and interaction without being riddled with profit-driven algorithms. If I would want to be part of some social media, it still would be Mastodon, which might sound slightly odd considering the title of this post.

So why did I then decide to leave it? One of the main reasons why I didn’t enjoy using e.g. other mainstream social medias, was the fact how they started affect my own behaviour. The constant urge to check what’s going on. Picking up your phone, opening the app, or just going to the site itself out of habit without necessarily even having a reason for it. It is addicting. It is meant to be addicting. Although, in Mastodon’s case, probably not maliciously.

So while I thought investing time and energy into building connections and contributing to various discussions in Mastodon would be beneficial, and no doubt, they can be. For me, it just started feel like sacrificing my mental well-being for digital engagement, again.

Mastodon is by no means a bad platform. It just isn’t suitable for me who wants to navigate digital landscape mindfully and intentionally without being ensnared into various addictive grasps once more.

What I Read in April 2024

Posted on 30th of April 2024 | 498 words

Haven’t written one of these in couple of months since I’ve mainly spent my time reading various papers - mainly computer science related - and not so much literature, but I finally got back to the saddle so here’s the latest reviews.

William Strunk Jr.: The Elements of Style (reread)

English is not my native language – even though I mainly write in it for this site – so grammatical and other style rulings when it comes to writing is something that I always struggle. While I would consider these rules to be relatively easy, I tend to always forgot them since they’re still so different from the rules of my native language, Finnish.

The Elements of Style is a short book is comprised of these sort of elementary rules for writing English. Due to the length of this book, I have read this many times and every once in a while, I tend to come back to it. It was quite a useful read for me since I have had a few month break from writing altogether so it was beneficial for waking the old habit.

Kimmo Svinhufvud: Kokonaisvaltainen kirjoittaminen (reread)

On similar theme as above, this focuses mainly on writing style in Finnish.

Stephen King: On Writing: A Memoir of the Craft

This book has been on my reading list for some time just out of sheer curiosity. I’ve always enjoyed Stephen King’s writing and I know how proficient he is at writing. Especially considering how much he publishes yearly. On Writing: A Memoir of the Craft is a wonderful book that gives a glimpse to the life of writer and how one becomes one.

Writing has always interested me, and especially lives of remarkable writers. What sort of life events might’ve affected their style and stories. So it’s wonderful to hear such life stories written by the writer themselves.

Andrew Appel: Modern Compiler Implementation in ML (reread)

If you’ve followed my blog, I have spent some of my free time hacking my own programming language and compiler. Appel’s book on implementing compiler has been a standard that every once in a while I tend to go back to refresh some memories. Wonderful book.

George Saunders: Lincoln in the Bardo

I’ve been a big fan of George Saunders’ works for long time, especially his short stories. Lincoln in the Bardo was his first novel released in 2017 and I have had it on my reading list for a long time and I finally got into reading it. Thankfully I did so since it was such a pleasurable read.

Novel tells the story of aftermath of passing of Lincoln’s side and it was written in quite weird style. The style the novel was written is might definitely not so easily approachable for every reader and it started like that for me as well. Once you get into it, it start to open up. Definitely well written – even despite the unusual style – and worth a read.