From e898612de9e2df42c6a769fa88eb02eaeb8b0b78 Mon Sep 17 00:00:00 2001 From: "F. Jason Park" Date: Wed, 21 Jul 2021 00:26:38 -0700 Subject: [PATCH 2/2] Fix a couple byte-compiler warnings in erc.el MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * lisp/erc/erc.el (erc-lurker-maybe-trim): Prevent warning from showing up in third-party code re library not loaded by autoloading rx.el when needed. Also make function do what it claims to. It now only removes trailing chars typically appended automatically for uniquifying purposes when a desired nick is already taken. Special thanks to Mattias EngdegÄrd for making this more respectable. Bug#50005. (erc-with-all-buffers-of-server): Mute byte compiler warning saying return value unused. Leave possible optimizations for some future person. --- lisp/erc/erc.el | 25 ++++-------- test/lisp/erc/erc-tests.el | 79 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 17 deletions(-) diff --git a/lisp/erc/erc.el b/lisp/erc/erc.el index 73202016ba..ec3ed354b6 100644 --- a/lisp/erc/erc.el +++ b/lisp/erc/erc.el @@ -1732,20 +1732,11 @@ erc-with-all-buffers-of-server where PRED matches or in all buffers of the server process if PRED is nil." (declare (indent 1) (debug (form form body))) - ;; Make the evaluation have the correct order - (let ((pre (make-symbol "pre")) - (pro (make-symbol "pro"))) - `(let* ((,pro ,process) - (,pre ,pred) - (res (mapcar (lambda (buffer) - (with-current-buffer buffer - ,@forms)) - (erc-buffer-list ,pre - ,pro)))) - ;; Silence the byte-compiler by binding the result of mapcar to - ;; a variable. - (ignore res) - res))) + (macroexp-let2 nil pred pred + `(erc-buffer-filter (lambda () + (when (or (not ,pred) (funcall ,pred)) + ,@forms)) + ,process))) (define-obsolete-function-alias 'erc-iswitchb #'erc-switch-to-buffer "25.1") (defun erc--switch-to-buffer (&optional arg) @@ -2583,9 +2574,9 @@ erc-lurker-maybe-trim Returns NICK unmodified unless `erc-lurker-trim-nicks' is non-nil." (if erc-lurker-trim-nicks - (replace-regexp-in-string - (regexp-opt-charset (string-to-list erc-lurker-ignore-chars)) - "" nick) + (string-trim-right + nick (rx-to-string `(+ (in ,@(string-to-list + erc-lurker-ignore-chars))))) nick)) (defcustom erc-lurker-hide-list nil diff --git a/test/lisp/erc/erc-tests.el b/test/lisp/erc/erc-tests.el index 9efcf4a703..d1a1405999 100644 --- a/test/lisp/erc/erc-tests.el +++ b/test/lisp/erc/erc-tests.el @@ -47,6 +47,85 @@ erc--read-time-period (cl-letf (((symbol-function 'read-string) (lambda (&rest _) "1d"))) (should (equal (erc--read-time-period "foo: ") 86400)))) +(ert-deftest erc-with-all-buffers-of-server () + (let (proc-exnet + proc-onet + erc-kill-channel-hook erc-kill-server-hook erc-kill-buffer-hook) + + (with-current-buffer (get-buffer-create "OtherNet") + (erc-mode) + (setq proc-onet (start-process "sleep" (current-buffer) "sleep" "1") + erc-server-process proc-onet + erc-network 'OtherNet) + (set-process-query-on-exit-flag erc-server-process nil)) + + (with-current-buffer (get-buffer-create "ExampleNet") + (erc-mode) + (setq proc-exnet (start-process "sleep" (current-buffer) "sleep" "1") + erc-server-process proc-exnet + erc-network 'ExampleNet) + (set-process-query-on-exit-flag erc-server-process nil)) + + (with-current-buffer (get-buffer-create "#foo") + (erc-mode) + (setq erc-server-process proc-exnet) + (setq erc-default-recipients '("#foo"))) + + (with-current-buffer (get-buffer-create "#spam") + (erc-mode) + (setq erc-server-process proc-onet) + (setq erc-default-recipients '("#spam"))) + + (with-current-buffer (get-buffer-create "#bar") + (erc-mode) + (setq erc-server-process proc-onet) + (setq erc-default-recipients '("#bar"))) + + (with-current-buffer (get-buffer-create "#baz") + (erc-mode) + (setq erc-server-process proc-exnet) + (setq erc-default-recipients '("#baz"))) + + (should (eq (get-buffer-process "ExampleNet") proc-exnet)) + (erc-with-all-buffers-of-server (get-buffer-process "ExampleNet") + nil + (kill-buffer)) + + (should-not (get-buffer "ExampleNet")) + (should-not (get-buffer "#foo")) + (should-not (get-buffer "#baz")) + (should (get-buffer "OtherNet")) + (should (get-buffer "#bar")) + (should (get-buffer "#spam")) + + (let* ((test (lambda () (not (string= (buffer-name) "#spam")))) + (calls 0) + (get-test (lambda () (cl-incf calls) test))) + + (erc-with-all-buffers-of-server proc-onet + (funcall get-test) + (kill-buffer)) + + (should (= calls 1))) + + (should-not (get-buffer "OtherNet")) + (should-not (get-buffer "#bar")) + (should (get-buffer "#spam")) + (kill-buffer "#spam"))) + +(ert-deftest erc-lurker-maybe-trim () + (let (erc-lurker-trim-nicks + (erc-lurker-ignore-chars "_`")) + + (should (string= "nick`" (erc-lurker-maybe-trim "nick`"))) + + (setq erc-lurker-trim-nicks t) + (should (string= "nick" (erc-lurker-maybe-trim "nick`"))) + (should (string= "ni`_ck" (erc-lurker-maybe-trim "ni`_ck__``"))) + + (setq erc-lurker-ignore-chars "_-`") ; set of chars, not character alts + (should (string= "nick" (erc-lurker-maybe-trim "nick-_`"))))) + (ert-deftest erc-ring-previous-command-base-case () (ert-info ("Create ring when nonexistent and do nothing") (let (erc-input-ring -- 2.31.1