From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: "F. Jason Park" Date: Tue, 19 Oct 2021 22:53:03 -0700 Subject: [PATCH 09/29] Add eventual replacement for erc-default-recipients * lisp/erc/erc.el (erc--target, erc--target-channel, erc--target-channel-local): Add new structs to hold info on a buffer's target; stored in a local variable of the same name. (erc-open): Create above items in non server buffers. * lisp/erc/erc-backend.el (erc-server-NICK): Recreate `erc--target' when necessary. --- lisp/erc/erc-backend.el | 4 ++-- lisp/erc/erc.el | 30 ++++++++++++++++++++++++++++++ test/lisp/erc/erc-tests.el | 12 ++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/lisp/erc/erc-backend.el b/lisp/erc/erc-backend.el index 9e95f47d6f..1ed0a24f94 100644 --- a/lisp/erc/erc-backend.el +++ b/lisp/erc/erc-backend.el @@ -1387,8 +1387,8 @@ define-erc-response-handler (erc-buffer-filter (lambda () (when (equal (erc-default-target) nick) - (setq erc-default-recipients - (cons nn (cdr erc-default-recipients))) + (setq erc-default-recipients (cons nn (cdr erc-default-recipients)) + erc--target (erc--target-from-string nn)) (rename-buffer nn t) ; bug#12002 (erc-update-mode-line) (cl-pushnew (current-buffer) bufs)))) diff --git a/lisp/erc/erc.el b/lisp/erc/erc.el index af4a6477a1..9b5493951b 100644 --- a/lisp/erc/erc.el +++ b/lisp/erc/erc.el @@ -1350,6 +1350,35 @@ define-erc-module (put ',enable 'definition-name ',name) (put ',disable 'definition-name ',name)))) +;; The rationale for favoring inheritance here (nicer dispatch) is +;; kinda flimsy since there aren't yet any actual methods. + +(cl-defstruct erc--target + (string "" :type string :documentation "Received name of target.") + (symbol nil :type symbol :documentation "Case-mapped name as symbol.")) + +(cl-defstruct (erc--target-channel (:include erc--target))) + +(cl-defstruct (erc--target-channel-local (:include erc--target-channel))) + +(defun erc--target-from-string (string) + "Construct an `erc--target' variant from STRING." + (funcall (if (erc-channel-p string) + (if (erc-valid-local-channel-p string) + #'make-erc--target-channel-local + #'make-erc--target-channel) + #'make-erc--target) + :string string :symbol (intern (erc-downcase string)))) + +(defvar-local erc--target nil + "Info about a buffer's target, if any.") + +;; Temporary internal getter to ease transition to `erc--target' everywhere. +(defun erc--default-target () + "Return target string or nil." + (when erc--target + (erc--target-string erc--target))) + (defun erc-once-with-server-event (event f) "Run function F the next time EVENT occurs in the `current-buffer'. @@ -2057,6 +2086,7 @@ erc-open (set-marker erc-insert-marker (point)) ;; stack of default recipients (setq erc-default-recipients tgt-list) + (setq erc--target (and channel (erc--target-from-string channel))) (setq erc-server-current-nick nil) ;; Initialize erc-server-users and erc-channel-users (if connect diff --git a/test/lisp/erc/erc-tests.el b/test/lisp/erc/erc-tests.el index bf70a4b1cb..42f346b201 100644 --- a/test/lisp/erc/erc-tests.el +++ b/test/lisp/erc/erc-tests.el @@ -258,6 +258,18 @@ erc-local-channel-p (should-not (erc-valid-local-channel-p "#chan")) (should (erc-valid-local-channel-p "&local"))))) +(ert-deftest erc--target-from-string () + (should (equal (erc--target-from-string "#chan") + #s(erc--target-channel "#chan" \#chan))) + + (should (equal (erc--target-from-string "Bob") + #s(erc--target "Bob" bob))) + + (let ((erc--isupport-params (make-hash-table))) + (puthash 'CHANTYPES '("&#") erc--isupport-params) + (should (equal (erc--target-from-string "&Bitlbee") + #s(erc--target-channel-local "&Bitlbee" &bitlbee))))) + (ert-deftest erc-ring-previous-command-base-case () (ert-info ("Create ring when nonexistent and do nothing") (let (erc-input-ring -- 2.35.1