diff options
| author | Craig Jennings <c@cjennings.net> | 2025-10-20 13:39:23 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2025-10-20 13:39:23 -0500 |
| commit | e3b008757fe4ff438433b507f192c80c517549ef (patch) | |
| tree | 207eddce75a8641824198cc6138c598183958071 /modules | |
| parent | cc3c93d342a122215c59e9a9865314268213b2ad (diff) | |
refactor: ui-theme: improve theme switching and persistence
Remove unnecessary comments and whitespace, streamline theme
switching logic, and update keybinding function for theme switching.
Add eval-when-compile for org-dir variable. Improve error messaging
and conditional checks for theme persistence.
Diffstat (limited to 'modules')
| -rw-r--r-- | modules/ui-theme.el | 27 |
1 files changed, 10 insertions, 17 deletions
diff --git a/modules/ui-theme.el b/modules/ui-theme.el index d6eaf404..1575257a 100644 --- a/modules/ui-theme.el +++ b/modules/ui-theme.el @@ -17,7 +17,9 @@ ;;; Code: -(require 'user-constants) ;; For org-dir +(require 'user-constants) + +(eval-when-compile (defvar org-dir)) ;; ----------------------------------- Themes ---------------------------------- ;; theme choices and settings @@ -32,35 +34,30 @@ (defun cj/switch-themes () "Function to switch themes and save chosen theme name for persistence. - Unloads any other applied themes before applying the chosen theme." (interactive) - (let ((chosentheme "")) - (setq chosentheme - (completing-read "Load custom theme: " - (mapcar #'symbol-name - (custom-available-themes)))) + (let ((chosentheme (completing-read "Load custom theme: " + (mapcar #'symbol-name + (custom-available-themes))))) (mapc #'disable-theme custom-enabled-themes) (load-theme (intern chosentheme) t)) (cj/save-theme-to-file)) -(global-set-key (kbd "M-L") 'cj/switch-themes) +(keymap-global-set "M-L" #'cj/switch-themes) ;; ----------------------------- Theme Persistence ----------------------------- ;; persistence utility functions used by switch themes. (defvar theme-file (concat org-dir "emacs-theme.persist") "The location of the file to persist the theme name. - If you want your theme change to persist across instances, put this in a directory that is sync'd across machines with this configuration.") (defvar fallback-theme-name "modus-vivendi" "The name of the theme to fallback on. - -This is used then there's no file, or the theme name doesn't match +This is used when there's no file, or the theme name doesn't match any of the installed themes. This should be a built-in theme. If theme name is -'nil', there will be no theme.") +`nil', there will be no theme.") (defun cj/read-file-contents (filename) "Read FILENAME and return its content as a string. @@ -73,7 +70,6 @@ If FILENAME isn't readable, return nil." (defun cj/write-file-contents (content filename) "Write CONTENT to FILENAME. - If FILENAME isn't writeable, return nil. If successful, return t." (when (file-writable-p filename) (condition-case err @@ -88,7 +84,6 @@ If FILENAME isn't writeable, return nil. If successful, return t." (defun cj/get-active-theme-name () "Return the name of the active UI theme as a string. - Returns fallback-theme-name if no theme is active." (if custom-enabled-themes (symbol-name (car custom-enabled-themes)) @@ -96,20 +91,18 @@ Returns fallback-theme-name if no theme is active." (defun cj/save-theme-to-file () "Save the string representing the current theme to the theme-file." - (if (equal (cj/write-file-contents (cj/get-active-theme-name) theme-file) nil) + (if (not (cj/write-file-contents (cj/get-active-theme-name) theme-file)) (message "Cannot save theme: %s is unwriteable" theme-file) (message "%s theme saved to %s" (cj/get-active-theme-name) theme-file))) (defun cj/load-fallback-theme (msg) "Display MSG and load ui-theme fallback-theme-name. - Used to handle errors with loading persisted theme." (message "%s Loading fallback theme %s" msg fallback-theme-name) (load-theme (intern fallback-theme-name) t)) (defun cj/load-theme-from-file () "Apply the theme name contained in theme-file as the active UI theme. - If the theme is nil, it disables all current themes. If an error occurs loading the file name, the fallback-theme-name is applied and saved." (let ((theme-name (cj/read-file-contents theme-file))) |
