diff options
-rw-r--r-- | lisp/erc/erc-common.el | 76 | ||||
-rw-r--r-- | test/lisp/erc/erc-tests.el | 7 |
2 files changed, 49 insertions, 34 deletions
diff --git a/lisp/erc/erc-common.el b/lisp/erc/erc-common.el index 6c015c71ff9..708cdb0c422 100644 --- a/lisp/erc/erc-common.el +++ b/lisp/erc/erc-common.el @@ -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. diff --git a/test/lisp/erc/erc-tests.el b/test/lisp/erc/erc-tests.el index 5aaf7e499e3..bafe418f0cd 100644 --- a/test/lisp/erc/erc-tests.el +++ b/test/lisp/erc/erc-tests.el @@ -1289,15 +1289,12 @@ (erc-default-recipients '("#chan")) calls) (with-temp-buffer + (erc-tests--set-fake-server-process "sleep" "1") (cl-letf (((symbol-function 'erc-cmd-MSG) (lambda (line) (push line calls) (should erc--called-as-input-p) (funcall orig-erc-cmd-MSG line))) - ((symbol-function 'erc-server-buffer) - (lambda () (current-buffer))) - ((symbol-function 'erc-server-process-alive) - (lambda () t)) ((symbol-function 'erc-server-send-queue) #'ignore)) @@ -2018,7 +2015,7 @@ ARG is omitted or nil. Some docstring." :global t :group (erc--find-group 'mname 'malias) - :get #'erc--neuter-custom-variable-state + :require 'nil :type "mname" (if erc-mname-mode (erc-mname-enable) |