From c07f359d648a447ebeaaa3a89a69a3a25c3d0366 Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Thu, 12 Sep 2024 23:18:08 -0400 Subject: [PATCH] fix(ligatures): activate prettify-symbols-mode conditionally With +extra enabled, this module would activate `prettify-symbols-mode` in any buffer where `prettify-symbols-alist` is non-nil, whether or not `+ligatures-extra-alist` has an entry for the current major mode (or a parent thereof). This behavior is poor UX, since the user may be expecting that a empty entry for some `X-mode` in `+ligatures-extra-alist` should mean *no` prettify-symbols-mode` at all in `X-mode`. With this, `+ligatures-extra-alist` is now the authority. An empty entry for `X-mode` will result in `prettify-symbols-mode` *not* being activated there. If that entry *isn't* empty, it will be combined only with the global default value of `prettify-symbols-alist`, not any pre-existing buffer-local value, to make the end result deterministic, because some modes have their own defaults for it (like `lisp-prettify-symbols-alist`, `js--prettify-symbols-alist`, and `rust-prettify-symbols-alist`). Fix: #7440 --- modules/lang/rust/config.el | 3 --- modules/ui/ligatures/config.el | 36 +++++++++++++++++----------------- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/modules/lang/rust/config.el b/modules/lang/rust/config.el index 5d07f11a8..89b4f1cd7 100644 --- a/modules/lang/rust/config.el +++ b/modules/lang/rust/config.el @@ -42,9 +42,6 @@ (setq rustic-indent-method-chain t) - ;; Conflicts with (and is redundant with) :ui ligatures - (setq rust-prettify-symbols-alist nil) - ;; Leave automatic reformatting to the :editor format module. (setq rustic-babel-format-src-block nil rustic-format-trigger nil) diff --git a/modules/ui/ligatures/config.el b/modules/ui/ligatures/config.el index e3236227b..1de06f345 100644 --- a/modules/ui/ligatures/config.el +++ b/modules/ui/ligatures/config.el @@ -99,19 +99,24 @@ efficient to remove the `+extra' flag from the :ui ligatures module instead).") (defun +ligatures-init-extra-symbols-h () "Set up `prettify-symbols-mode' for the current buffer. -Extra ligatures are mode-specific substituions, defined in -`+ligatures-extra-symbols', assigned with `set-ligatures!', and made possible -with `prettify-symbols-mode'. This variable controls where these are enabled. -See `+ligatures-extras-in-modes' to control what major modes this function can -and cannot run in." - (when (and after-init-time (+ligatures--enable-p +ligatures-extras-in-modes)) - (prependq! prettify-symbols-alist - (or (alist-get major-mode +ligatures-extra-alist) - (cl-loop for (mode . symbols) in +ligatures-extra-alist - if (derived-mode-p mode) - return symbols))) - (when prettify-symbols-alist - (when prettify-symbols-mode +Overwrites `prettify-symbols-alist' and activates `prettify-symbols-mode' if +(and only if) there is an associated entry for the current major mode (or a +parent mode) in `+ligatures-extra-alist' AND the current mode (or a parent mode) +isn't disabled in `+ligatures-extras-in-modes'." + (when after-init-time + (when-let* + (((+ligatures--enable-p +ligatures-extras-in-modes)) + (symbols + (if-let ((symbols (assq major-mode +ligatures-extra-alist))) + symbols + (cl-loop for (mode . symbols) in +ligatures-extra-alist + if (derived-mode-p mode) + return symbols)))) + (setq prettify-symbols-alist + (append symbols + ;; Don't overwrite global defaults + (default-value 'prettify-symbols-alist))) + (when (bound-and-true-p prettify-symbols-mode) (prettify-symbols-mode -1)) (prettify-symbols-mode +1)))) @@ -124,11 +129,6 @@ and cannot run in." (setq prettify-symbols-unprettify-at-point 'right-edge) (when (modulep! +extra) - ;; Lisp modes offer their own defaults for `prettify-symbols-mode' (just a - ;; lambda symbol substitution), but this might be unexpected if the user - ;; enables +extra but has unset `+ligatures-extra-symbols'. - (setq lisp-prettify-symbols-alist nil) - (add-hook 'after-change-major-mode-hook #'+ligatures-init-extra-symbols-h)) (cond