branch: externals/exwm
commit f4416a10e3f87ceddc70ae02f065f14c5e6dc3fd
Author: Chris Feng <chris.w.f...@gmail.com>
Commit: Chris Feng <chris.w.f...@gmail.com>

    Fix potential naming conflicts
    
    Buffers may share a same name (without the possible leading space) when 
created
    in different workspaces.
---
 exwm-workspace.el |   21 +++++++++++++++++----
 1 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/exwm-workspace.el b/exwm-workspace.el
index bab2612..b46b2e8 100644
--- a/exwm-workspace.el
+++ b/exwm-workspace.el
@@ -119,9 +119,9 @@ The optional FORCE option is for internal use only "
     (dolist (i exwm--id-buffer-alist)
       (with-current-buffer (cdr i)
         (let ((name (replace-regexp-in-string "^\\s-*" "" (buffer-name))))
-          (rename-buffer (if (eq (selected-frame) exwm--frame)
-                             name
-                           (concat " " name))))))
+          (exwm-workspace-rename-buffer (if (eq (selected-frame) exwm--frame)
+                                            name
+                                          (concat " " name))))))
     ;; Update demands attention flag
     (set-frame-parameter (selected-frame) 'exwm--urgency nil)
     ;; Update switch workspace history
@@ -151,7 +151,7 @@ The optional FORCE option is for internal use only "
     (let ((frame (elt exwm-workspace--list index)))
       (with-current-buffer (exwm--id->buffer id)
         (setq exwm--frame frame)
-        (rename-buffer
+        (exwm-workspace-rename-buffer
          (concat " " (replace-regexp-in-string "^\\s-*" "" (buffer-name))))
         (if exwm--floating-frame
             ;; Move the floating frame is enough
@@ -172,6 +172,19 @@ The optional FORCE option is for internal use only "
     (xcb:flush exwm--connection)
     (exwm-workspace--update-switch-history)))
 
+(defun exwm-workspace-rename-buffer (newname)
+  "Rename a buffer."
+  (if (/= ?  (aref newname 0))
+      (rename-buffer newname t)
+    ;; If a buffer name is prefixed with a space, Emacs append a random
+    ;; number before renaming it. This is not desired behavior.
+    (let ((name (replace-regexp-in-string "<[0-9]+>$" "" newname))
+          (counter 1))
+      (while (and (get-buffer newname)
+                  (not (eq (get-buffer newname) (current-buffer))))
+        (setq newname (format "%s<%d>" name (cl-incf counter)))))
+    (rename-buffer newname)))
+
 (defun exwm-workspace--init ()
   "Initialize workspace module."
   (cl-assert (and (< 0 exwm-workspace-number) (>= 10 exwm-workspace-number)))

Reply via email to