1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-15 10:30:25 -08:00

Restore module var toggles in ERC's Custom buffers

* lisp/erc/erc-common.el (erc--neuter-custom-variable-state): Remove
function.  ERC famously toggles global minor-mode vars during normal
operations, which adds noise to its customization buffers because
`customize-variable-state' always sees an activated module's mode
variable as having "CHANGED".  To suppress this annoyance, a
workaround was employed that used a dishonest `:get' function to
simply return the "saved value," when present.  While this improved
the Customize experience, it also misled users, which likely wasn't
justified.
(erc--make-show-me-widget): Add helper to avoid forward declarations.
(erc--prepare-custom-module-type): Don't deprive users of a working
minor-mode toggle.
(erc--find-feature): New function to guess the feature of a module's
containing library.
(define-erc-module): Remove `:get' keyword.  Specify `:require'
instead, whose value may be nil.  Users who currently have mode vars
in their `custom-file' won't be impacted by this addition because
those `custom-set-variables' entries will still lack a REQUEST list
and hence won't incur a startup penalty.  And new users intent on
using the toggle will hopefully do so with the knowledge they're
opting in to requiring ERC on startup, which is not the case if they
follow the recommended practice of using `erc-modules' instead.
(erc-with-server-buffer): Inline `erc-server-buffer'.
* test/lisp/erc/erc-tests.el (erc-process-input-line): Use helper.
(define-erc-module--global): Change expected expansion.  (Bug#60935)
This commit is contained in:
F. Jason Park 2023-04-14 00:07:31 -07:00
parent 2e18ba6302
commit b0d761be0f
2 changed files with 49 additions and 34 deletions

View file

@ -32,6 +32,7 @@
(defvar erc-dbuf)
(defvar erc-log-p)
(defvar erc-modules)
(defvar erc-server-process)
(defvar erc-server-users)
(defvar erc-session-server)
@ -40,6 +41,9 @@
(declare-function erc-server-buffer "erc" nil)
(declare-function widget-apply-action "wid-edit" (widget &optional event))
(declare-function widget-at "wid-edit" (&optional pos))
(declare-function widget-create-child-and-convert "wid-edit"
(parent type &rest args))
(declare-function widget-default-format-handler "wid-edit" (widget escape))
(declare-function widget-get-sibling "wid-edit" (widget))
(declare-function widget-move "wid-edit" (arg &optional suppress-echo))
(declare-function widget-type "wid-edit" (widget))
@ -195,16 +199,6 @@ instead of a `set' state, which precludes any actual saving."
(throw 'found found)))
'erc))
(defun erc--neuter-custom-variable-state (variable)
"Lie to Customize about VARIABLE's true state.
Do so by always returning its standard value, namely nil."
;; Make a module's global minor-mode toggle blind to Customize, so
;; that `customize-variable-state' never sees it as "changed",
;; regardless of its value. This snippet is
;; `custom--standard-value' from Emacs 28+.
(cl-assert (null (eval (car (get variable 'standard-value)) t)))
nil)
;; This exists as a separate, top-level function to prevent the byte
;; compiler from warning about widget-related dependencies not being
;; loaded at runtime.
@ -230,25 +224,42 @@ Do so by always returning its standard value, namely nil."
(substitute-command-keys "\\[Custom-set]")
(substitute-command-keys "\\[Custom-save]"))))
;; This stands apart to avoid needing forward declarations for
;; `wid-edit' functions in every file requiring `erc-common'.
(defun erc--make-show-me-widget (widget escape &rest plist)
(if (eq escape ?i)
(apply #'widget-create-child-and-convert widget 'push-button plist)
(widget-default-format-handler widget escape)))
(defun erc--prepare-custom-module-type (name)
`(let* ((name (erc--normalize-module-symbol ',name))
(fmtd (format " `%s' " name)))
`(boolean
:button-face '(custom-variable-obsolete custom-button)
:format "%{%t%}: %[Deprecated Toggle%] \n%h\n"
:format "%{%t%}: %i %[Deprecated Toggle%] %v \n%h\n"
:format-handler
,(lambda (widget escape)
(erc--make-show-me-widget
widget escape
:button-face '(custom-variable-obsolete custom-button)
:tag "Show Me"
:action (apply-partially #'erc--tick-module-checkbox name)
:help-echo (lambda (_)
(let ((hasp (memq name erc-modules)))
(concat (if hasp "Remove" "Add") fmtd
(if hasp "from" "to")
" `erc-modules'.")))))
:action widget-toggle-action
:documentation-property
,(lambda (_)
(let ((hasp (memq name erc-modules)))
(concat "Setting a module's minor-mode variable is "
(propertize "ineffective" 'face 'error)
".\nPlease " (if hasp "remove" "add") fmtd
(if hasp "from" "to") " `erc-modules' directly instead.\n"
"You can do so now by clicking the scary button above.")))
:help-echo ,(lambda (_)
(let ((hasp (memq name erc-modules)))
(concat (if hasp "Remove" "Add") fmtd
(if hasp "from" "to") " `erc-modules'.")))
:action ,(apply-partially #'erc--tick-module-checkbox name))))
(concat
"Setting a module's minor-mode variable is "
(propertize "ineffective" 'face 'error)
".\nPlease " (if hasp "remove" "add") fmtd
(if hasp "from" "to") " `erc-modules' directly instead.\n"
"You can do so now by clicking "
(propertize "Show Me" 'face 'custom-variable-obsolete)
" above."))))))
(defun erc--fill-module-docstring (&rest strings)
(with-temp-buffer
@ -264,6 +275,12 @@ Do so by always returning its standard value, namely nil."
(goto-char (point-min))
(nth 3 (read (current-buffer)))))
(defmacro erc--find-feature (name alias)
`(pcase (erc--find-group ',name ,(and alias (list 'quote alias)))
('erc (and-let* ((file (or (macroexp-file-name) buffer-file-name)))
(intern (file-name-base file))))
(v v)))
(defmacro define-erc-module (name alias doc enable-body disable-body
&optional local-p)
"Define a new minor mode using ERC conventions.
@ -310,7 +327,7 @@ if ARG is omitted or nil.
\n%s" name name doc))
:global ,(not local-p)
:group (erc--find-group ',name ,(and alias (list 'quote alias)))
,@(unless local-p '(:get #'erc--neuter-custom-variable-state))
,@(unless local-p `(:require ',(erc--find-feature name alias)))
,@(unless local-p `(:type ,(erc--prepare-custom-module-type name)))
(if ,mode
(,enable)
@ -371,12 +388,13 @@ If no server buffer exists, return nil."
(not (cdr body))
(special-variable-p (car body))))
(buffer (make-symbol "buffer")))
`(let ((,buffer (erc-server-buffer)))
(when (buffer-live-p ,buffer)
,(if varp
`(buffer-local-value ',(car body) ,buffer)
`(with-current-buffer ,buffer
,@body))))))
`(when-let* (((processp erc-server-process))
(,buffer (process-buffer erc-server-process))
((buffer-live-p ,buffer)))
,(if varp
`(buffer-local-value ',(car body) ,buffer)
`(with-current-buffer ,buffer
,@body)))))
(defmacro erc-with-all-buffers-of-server (process pred &rest forms)
"Execute FORMS in all buffers which have same process as this server.