>From 93233064285554d24df1a3137194bae4edd1ec06 Mon Sep 17 00:00:00 2001 From: "F. Jason Park" Date: Wed, 29 Jun 2022 04:05:52 -0700 Subject: [PATCH 2/3] [SQUASH] Avoid mutating default value of erc-server-last-peers * lisp/erc/erc-backend.el (erc-server-last-peers): Define as nil instead of a quoted constant. (erc-server-connect): Initialize `erc-server-last-peers' to a new value local to a server buffer. (erc--message-shortcut): New function to store and recall senders and receivers for the current connection when processing "." and "," shortcuts. (erc-message): Delegate to `erc--message-shortcut' to handle shortcuts. (erc-server-PRIVMSG): Create a new `erc-server-last-peers' for every message received to make for easier testing. --- lisp/erc/erc-backend.el | 31 +++++++++++++++-------------- test/lisp/erc/erc-tests.el | 40 ++++++++++++++++---------------------- 2 files changed, 33 insertions(+), 38 deletions(-) diff --git a/lisp/erc/erc-backend.el b/lisp/erc/erc-backend.el index bc7a7d14dc..ea1891da0b 100644 --- a/lisp/erc/erc-backend.el +++ b/lisp/erc/erc-backend.el @@ -230,7 +230,7 @@ erc-server-error-occurred (defvar-local erc-server-lines-sent nil "Line counter.") -(defvar-local erc-server-last-peers '(nil . nil) +(defvar-local erc-server-last-peers nil "Last peers used, both sender and receiver. Those are used for /MSG destination shortcuts.") @@ -562,7 +562,7 @@ erc-server-connect (setq erc-server-last-received-time time)) (setq erc-server-lines-sent 0) ;; last peers (sender and receiver) - (setq erc-server-last-peers '(nil . nil))) + (setq erc-server-last-peers (cons nil nil))) ;; we do our own encoding and decoding (when (fboundp 'set-process-coding-system) (set-process-coding-system process 'raw-text)) @@ -931,29 +931,30 @@ erc-server-send-queue (run-at-time (+ 0.2 erc-server-flood-penalty) nil #'erc-server-send-queue buffer))))))) +(defun erc--message-shortcut (arg &optional commit) + (let* ((buffer (erc-server-buffer)) + (peers (buffer-local-value 'erc-server-last-peers buffer)) + (tgt (pcase arg ("," (car peers)) ("." (cdr peers)) (_ arg)))) + (when tgt + (when commit + (with-current-buffer buffer + (setq erc-server-last-peers (cons (car peers) tgt)))) + tgt))) + (defun erc-message (message-command line &optional force) "Send LINE to the server as a privmsg or a notice. MESSAGE-COMMAND should be either \"PRIVMSG\" or \"NOTICE\". If the target is \",\", the last person you've got a message from will be used. If the target is \".\", the last person you've sent a message -to will be used." +to will be used. Note that as of ERC 5.6, this syntax is considered +deprecated. See the pcomplete module for similar functionality." (cond ((string-match "^\\s-*\\(\\S-+\\) ?\\(.*\\)" line) - (let ((tgt (match-string 1 line)) + (let ((tgt (erc--message-shortcut (match-string 1 line) 'commit)) (s (match-string 2 line))) (erc-log (format "cmd: MSG(%s): [%s] %s" message-command tgt s)) - (cond - ((string= tgt ",") - (if (car erc-server-last-peers) - (setq tgt (car erc-server-last-peers)) - (setq tgt nil))) - ((string= tgt ".") - (if (cdr erc-server-last-peers) - (setq tgt (cdr erc-server-last-peers)) - (setq tgt nil)))) (cond (tgt - (setcdr erc-server-last-peers tgt) (erc-server-send (format "%s %s :%s" message-command tgt s) force)) (t @@ -1550,7 +1551,7 @@ define-erc-response-handler (erc-process-ctcp-reply proc parsed nick login host (match-string 1 msg))))) (t - (setcar erc-server-last-peers nick) + (setq erc-server-last-peers (cons nick (cdr erc-server-last-peers))) (setq s (erc-format-privmessage (or fnick nick) msg ;; If buffer is a query buffer, diff --git a/test/lisp/erc/erc-tests.el b/test/lisp/erc/erc-tests.el index 373f5f16b0..0f222edacf 100644 --- a/test/lisp/erc/erc-tests.el +++ b/test/lisp/erc/erc-tests.el @@ -896,8 +896,7 @@ erc-process-input-line ;; Note: if adding an erc-backend-tests.el, please relocate this there. (ert-deftest erc-message () - (unless (equal erc-server-last-peers '(nil . nil)) - (setq erc-server-last-peers (cons nil nil))) + (should-not erc-server-last-peers) (let (server-proc calls erc-kill-channel-hook erc-kill-server-hook erc-kill-buffer-hook) @@ -938,19 +937,19 @@ erc-message (should (string-match "" (pop calls)))) (with-current-buffer "#chan" - (ert-info ("Shortcuts unusable in target buffers") + (ert-info ("Shortcuts usable in target buffers") (should-not (local-variable-p 'erc-server-last-peers)) - (should (equal erc-server-last-peers '(nil . nil))) + (should-not erc-server-last-peers) (erc-message "PRIVMSG" ". hi") - (should (equal erc-server-last-peers '(nil . nil))) + (should-not erc-server-last-peers) (should (eq 'no-target (pop calls))) (erc-message "PRIVMSG" ", hi") - (should (equal erc-server-last-peers '(nil . nil))) - (should (eq 'no-target (pop calls))))) + (should-not erc-server-last-peers) + (should (string-match "alice :hi" (pop calls))))) (with-current-buffer "ExampleNet" (ert-info ("Shortcuts local in server bufs") - (should (equal erc-server-last-peers '("alice"))) + (should (equal erc-server-last-peers '("alice" . "alice"))) (erc-message "PRIVMSG" ", hi") (should (equal erc-server-last-peers '("alice" . "alice"))) (should (string-match "PRIVMSG alice :hi" (pop calls))) @@ -960,23 +959,18 @@ erc-message (should (string-match "PRIVMSG bob :hi" (pop calls))))) (with-current-buffer "#chan" - (ert-info ("Non-shortcuts affect global default") + (ert-info ("Non-shortcuts are local to server buffer") (should-not (local-variable-p 'erc-server-last-peers)) - (should (equal erc-server-last-peers '(nil . nil))) + (should-not erc-server-last-peers) (erc-message "PRIVMSG" "#chan hola") - (should (equal erc-server-last-peers '(nil . "#chan"))) - (should (equal (default-value 'erc-server-last-peers) - '(nil . "#chan"))) - (should (string-match "hola" (pop calls)))) - - (ert-info ("Dot shortcut now usable in target buffers") - (erc-message "PRIVMSG" ". how") - (should (equal erc-server-last-peers '(nil . "#chan"))) - (should (string-match "how" (pop calls)))))) - - (should (equal (default-value 'erc-server-last-peers) '(nil . "#chan"))) - (setq erc-server-last-peers (cons nil nil)) - (should (equal erc-server-last-peers '(nil . nil))) + (should-not erc-server-last-peers) + (should-not (default-value 'erc-server-last-peers)) + (should (equal (buffer-local-value 'erc-server-last-peers + (get-buffer "ExampleNet")) + '("alice" . "#chan"))) + (should (string-match "hola" (pop calls)))))) + + (should-not erc-server-last-peers) (should-not calls) (kill-buffer "ExampleNet") (kill-buffer "#chan"))) -- 2.36.1