Subject: Re: bug#41343: tab-bar-mode: Close tab on mouse-2 click

From: Juri Linkov <[email-masked]>
Date: Wed, 04 Aug 2021 01:33:45 +0300

> Please consider adding a binding to close a tab in tab-bar-mode by
> clicking on it using the middle mouse button (mouse-2).

Here's the patch that implements mouse-2 tab closing:

diff --git a/lisp/tab-bar.el b/lisp/tab-bar.el
index 7660b217d2..ce4d18a0fa 100644
--- a/lisp/tab-bar.el
+++ b/lisp/tab-bar.el
@@ -229,7 +229,8 @@ tab-bar-handle-mouse
 This command is used when you click the mouse in the tab bar
 on a console which has no window system but does have a mouse."
   (interactive "e")
-  (let* ((x-position (car (posn-x-y (event-start event))))
+  (let* ((button (event-basic-type event))
+         (x-position (car (posn-x-y (event-start event))))
          (keymap (lookup-key (cons 'keymap (nreverse (current-active-maps))) [tab-bar]))
          (column 0))
     (when x-position
@@ -238,7 +239,9 @@ tab-bar-handle-mouse
                  (lambda (key binding)
                    (when (eq (car-safe binding) 'menu-item)
                      (when (> (+ column (length (nth 1 binding))) x-position)
-                       (if (get-text-property (- x-position column) 'close-tab (nth 1 binding))
+                       (if (or (eq button 'mouse-2)
+                               (get-text-property
+                                (- x-position column) 'close-tab (nth 1 binding)))
                            (let* ((close-key (vector (intern (format "C-%s" key))))
                                   (close-def (lookup-key keymap close-key)))
                              (when close-def
@@ -768,7 +771,8 @@ tab-bar-format-list
 (defun tab-bar-make-keymap-1 ()
   "Generate an actual keymap from `tab-bar-map', without caching."
   (append
-   '(keymap (mouse-1 . tab-bar-handle-mouse))
+   '(keymap (mouse-1 . tab-bar-handle-mouse)
+            (mouse-2 . tab-bar-handle-mouse))
    (tab-bar-format-list tab-bar-format)))
 
 
diff --git a/src/dispextern.h b/src/dispextern.h
index 33fcaa4c07..26112de0a9 100644
--- a/src/dispextern.h
+++ b/src/dispextern.h
@@ -3416,7 +3416,7 @@ #define TTY_CAP_STRIKE_THROUGH	0x20
 extern Lisp_Object find_hot_spot (Lisp_Object, int, int);
 
 extern void handle_tab_bar_click (struct frame *,
-                                   int, int, bool, int);
+                                   int, int, bool, int, int);
 extern void handle_tool_bar_click (struct frame *,
                                    int, int, bool, int);
 
diff --git a/src/w32term.c b/src/w32term.c
index ad4d1a3282..f53114a857 100644
--- a/src/w32term.c
+++ b/src/w32term.c
@@ -3691,10 +3691,11 @@ w32_handle_tab_bar_click (struct frame *f, struct input_event *button_event)
   int y = XFIXNAT (button_event->y);
 
   if (button_event->modifiers & down_modifier)
-    handle_tab_bar_click (f, x, y, 1, 0);
+    handle_tab_bar_click (f, x, y, 1, 0, button_event->code + 1);
   else
     handle_tab_bar_click (f, x, y, 0,
-                          button_event->modifiers & ~up_modifier);
+                          button_event->modifiers & ~up_modifier,
+                          button_event->code + 1);
 }

diff --git a/src/xdisp.c b/src/xdisp.c
index 70d15aee68..9ca6234264 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -13747,7 +13747,7 @@ get_tab_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
 
 void
 handle_tab_bar_click (struct frame *f, int x, int y, bool down_p,
-		      int modifiers)
+		      int modifiers, int button)
 {
   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
   struct window *w = XWINDOW (f->tab_bar_window);
@@ -13793,7 +13793,7 @@ handle_tab_bar_click (struct frame *f, int x, int y, bool down_p,
       event.kind = TAB_BAR_EVENT;
       event.frame_or_window = frame;
       event.arg = key;
-      event.modifiers = close_p ? ctrl_modifier | modifiers : modifiers;
+      event.modifiers = (close_p || button == 2) ? ctrl_modifier | modifiers : modifiers;
       kbd_buffer_store_event (&event);
       f->last_tab_bar_item = -1;
     }
@@ -13962,6 +13962,8 @@ tty_handle_tab_bar_click (struct frame *f, int x, int y, bool down_p,
       bool close_p = false;
       if ((x == clen - 1 || (clen > 1 && x == clen - 2)) && lastc == 'x')
 	close_p = true;
+      if (event->code == 1) /* mouse-2 */
+	close_p = true;
 
       event->code = 0;
       XSETFRAME (frame, f);
diff --git a/src/xterm.c b/src/xterm.c
index 1887c3255d..5a22bc31d4 100644
--- a/src/xterm.c
+++ b/src/xterm.c
@@ -9217,7 +9217,8 @@ handle_one_xevent (struct x_display_info *dpyinfo,
                 if (tab_bar_p && event->xbutton.button < 4)
 		  handle_tab_bar_click
 		    (f, x, y, event->xbutton.type == ButtonPress,
-		     x_x_to_emacs_modifiers (dpyinfo, event->xbutton.state));
+		     x_x_to_emacs_modifiers (dpyinfo, event->xbutton.state),
+		     event->xbutton.button);
               }
 
 #if ! defined (USE_GTK)