[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] erc: fix erc-reuse-buffers behavior

From: Mingde (Matthew) Zeng
Subject: [PATCH] erc: fix erc-reuse-buffers behavior
Date: Tue, 07 Jul 2020 03:20:03 -0400
User-agent: mu4e 1.4.10; emacs 27.0.91

By definition of erc-reuse-buffers, if non-nil it should create a
new buffer when joining channels with same names on different
servers. The current behavior of erc-reuse-buffers is:
1. when non-nil, it will always reuse the same channel buffer,
resulting in server A's channel gets reconnected to the channel with
the same name of server B.
2. when nil, the buffer-name of the joined channel is
'#channel/server'. However if one tries to '/join #channel' from the
server buffer, it creates a new empty buffer with buffer-name
'#channel', instead of opening the already-joined channel buffer.

* lisp/erc/erc.el (erc-generate-new-buffer-name): since `target' is
always non-nil, buffer-name is non-nil, erc-reuse-buffers is t, it
will always use the first candidate, resulting in behavior 1. This is
now fixed, and both channel buffers with the same name will be formated
'#channel/serverA' and '#channel/serverB' for the sake of consistency.

* lisp/erc/erc.el (erc-cmd-JOIN): with some refactoring, it ensures
the joined channel buffer is selected and thereby fixes behavior 2.

Signed-off-by: Mingde (Matthew) Zeng <matthewzmd@gmail.com>
 lisp/erc/erc.el | 38 +++++++++++++++++++++++---------------
 1 file changed, 23 insertions(+), 15 deletions(-)

diff --git a/lisp/erc/erc.el b/lisp/erc/erc.el
index 3880778794..f0ace765d9 100644
--- a/lisp/erc/erc.el
+++ b/lisp/erc/erc.el
@@ -1622,14 +1622,20 @@ symbol, it may have these values:
       (if (and (not buffer-name)
                (or (not (get-buffer candidate))
-                   (or target
-                       (with-current-buffer (get-buffer candidate)
-                         (and (erc-server-buffer-p)
-                              (not (erc-server-process-alive)))))
+                   (with-current-buffer (get-buffer candidate)
+                     (and (erc-server-buffer-p)
+                          (not (erc-server-process-alive))))
                    (with-current-buffer (get-buffer candidate)
                      (and (string= erc-session-server server)
                           (erc-port-equal erc-session-port port)))))
-          (setq buffer-name candidate)))
+          (setq buffer-name candidate)
+        (when (and erc-reuse-buffers (get-buffer buf-name))
+          ;; a new buffer will be created with the name buf-name/server, rename
+          ;; the existing name-duplicated buffer with the same format as well
+          (with-current-buffer (get-buffer buf-name)
+            (when (derived-mode-p 'erc-mode) ; ensure it's an erc buffer
+              (rename-buffer
+               (concat buf-name "/" (or erc-session-server 
     ;; if buffer-name is unset, neither candidate worked out for us,
     ;; fallback to the old <N> uniquification method:
     (or buffer-name (generate-new-buffer-name (concat buf-name "/" server)))))
@@ -3097,16 +3103,18 @@ were most recently invited.  See also `invitation'."
       (setq chnl (erc-ensure-channel-name channel)))
     (when chnl
       ;; Prevent double joining of same channel on same server.
-      (let ((joined-channels
-             (mapcar #'(lambda (chanbuf)
-                         (with-current-buffer chanbuf (erc-default-target)))
-                     (erc-channel-list erc-server-process))))
-        (if (erc-member-ignore-case chnl joined-channels)
-            (switch-to-buffer (car (erc-member-ignore-case chnl
-                                                           joined-channels)))
-         (let ((server (with-current-buffer (process-buffer erc-server-process)
-                         (or erc-session-server erc-server-announced-name))))
-           (erc-server-join-channel server chnl key))))))
+      (let* ((joined-channels
+              (mapcar #'(lambda (chanbuf)
+                          (with-current-buffer chanbuf (erc-default-target)))
+                      (erc-channel-list erc-server-process)))
+             (server (with-current-buffer (process-buffer erc-server-process)
+                      (or erc-session-server erc-server-announced-name)))
+             (chnl-name (car (erc-member-ignore-case chnl joined-channels))))
+        (if chnl-name
+            (switch-to-buffer (if (get-buffer chnl-name)
+                                  chnl-name
+                                (concat chnl-name "/" server)))
+         (erc-server-join-channel server chnl key)))))

 (defalias 'erc-cmd-CHANNEL 'erc-cmd-JOIN)
Mingde (Matthew) Zeng

reply via email to

[Prev in Thread] Current Thread [Next in Thread]