branch: externals/fontaine commit 748d4ed863b31d6a9ad9a048171ea783b8628197 Author: Protesilaos Stavrou <i...@protesilaos.com> Commit: Protesilaos Stavrou <i...@protesilaos.com>
Define the fontaine-toggle-preset command --- README.org | 7 +++++++ fontaine.el | 47 ++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/README.org b/README.org index ff464a2f8e..286cea522b 100644 --- a/README.org +++ b/README.org @@ -203,6 +203,13 @@ the =.emacs.d= directory). Saving is done by the function function ~fontaine-restore-latest-preset~ (such as by adding it to their init file). +#+findex: fontaine-toggle-preset +The command ~fontaine-toggle-preset~ can toggle between the last two +valid presets, as set by ~fontaine-set-preset~. If it cannot find two +different presets, then it prompts using minibuffer completion. As a +final step, it calls the ~fontaine-set-preset-hook~. [ The +~fontaine-toggle-preset~ is part of {{{development-version}}}. ] + For users of the =no-littering= package, ~fontaine-latest-state-file~ is not stored in their =.emacs.d=, but in a standard directory instead: https://github.com/emacscollective/no-littering. diff --git a/fontaine.el b/fontaine.el index 6cf6b62392..81ad188041 100644 --- a/fontaine.el +++ b/fontaine.el @@ -402,12 +402,24 @@ If FRAME is nil, apply the effect to all frames." symbol)) fontaine-presets))) -(defun fontaine--set-fonts-prompt () - "Prompt for font set (used by `fontaine-set-fonts')." +(defun fontaine--get-preset-symbols () + "Return list of the `car' of each element in `fontain-presets'." + (delq nil + (mapcar + (lambda (element) + (when-let ((first (car element)) + (_ (not (eq first t)))) + first)) + fontaine-presets))) + +(defun fontaine--set-fonts-prompt (&optional prompt) + "Prompt for font set (used by `fontaine-set-fonts'). +If optional PROMPT string, use it for the prompt, else use one that asks +for a preset among `fontaine-presets'." (let* ((def (nth 1 fontaine--font-display-hist)) - (prompt (if def - (format "Apply font configurations from PRESET [%s]: " def) - "Apply font configurations from PRESET: "))) + (prompt (if prompt + (format-prompt prompt nil) + (format-prompt "Apply font configurations from PRESET" def)))) (intern (completing-read prompt @@ -474,6 +486,31 @@ which this function ignores" (fontaine-set-preset current) (user-error "The `fontaine-current-preset' is not among `fontaine-presets'"))) +(defun fontaine--get-first-non-current-preset (history) + "Return the first element of HISTORY which is not `fontaine-current-preset'. +Only consider elements that are still part of the `fontaine-presets', +per `fontaine--get-preset-symbols'." + (catch 'first + (dolist (element history) + (when (stringp element) + (setq element (intern element))) + (when (and (not (eq element fontaine-current-preset)) + (member element (fontaine--get-preset-symbols))) + (throw 'first element))))) + +;;;###autoload +(defun fontaine-toggle-preset () + "Toggle between the last two known Fontaine presets. +These are the presets that were set with `fontaine-set-preset'. If +there are no two selected presets, then prompt the user to set a preset. + +As a final step, call the `fontaine-set-preset-hook'." + (interactive) + (fontaine-set-preset + (if-let ((previous (fontaine--get-first-non-current-preset fontaine--font-display-hist))) + previous + (fontaine--set-fonts-prompt "No previous preset to toggle; select PRESET")))) + ;;;; Store and restore preset (defvar fontaine--preset-history '()