Skip to content

Instantly share code, notes, and snippets.

@redblobgames
Created December 16, 2025 18:44
Show Gist options
  • Select an option

  • Save redblobgames/a2d43b822fa95665c60f546a35f11a84 to your computer and use it in GitHub Desktop.

Select an option

Save redblobgames/a2d43b822fa95665c60f546a35f11a84 to your computer and use it in GitHub Desktop.
Emacs: use font-lock to add unit conversion to temperatures
;; -*- lexical-binding: t; -*-
(defun add-C-to-F-highlighting ()
"demo: add highlighting to current buffer"
(interactive)
(font-lock-add-keywords
nil
`((,(rx (group (seq bow (+ digit) (optional (or "." "/")) (* digit))) (optional "°") (group "C"))
(0 'bold)
(2
(let* ((temperature-C (* 1.0 (string-to-number (match-string 1))))
(temperature-F (+ 32 (* 9 (/ temperature-C 5)))))
(list
'face nil
'display
(concat "C"
(propertize (format " (%.1f°F)" temperature-F) 'face 'italic)))))))))
;; need to repeat that for F to C conversion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment