Created
December 16, 2025 18:44
-
-
Save redblobgames/a2d43b822fa95665c60f546a35f11a84 to your computer and use it in GitHub Desktop.
Emacs: use font-lock to add unit conversion to temperatures
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; -*- 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