2

I'm trying to create a binding to kill windows (but not buffers) when they're being pressed by a middle mouse click in their header lines. (Just to note, all windows in my setup have header lines.)

So far I've had some limited success killing the active window which is being under focus via this binding:

(global-set-key 
    (kbd "<header-line> <mouse-2>") 
    'kill-buffer-and-window)

The two issues I'm having with my current binding:

  • When trying to kill an inactive window, it kills the active window instead of the window that is actually being pressed. (by "active" I mean under focus with the caret pointer in it)
  • It kills both the window and the buffer, how can I kill just the window but not the buffer? The only interactive function that I've found was kill-buffer-and-window.
  • do you mean you just want to delete-window C-x 0 ? – manandearth Nov 2 '18 at 7:16
2

The function you are looking for is delete-window (I hadn't thought about it before, but apparently windows are "deleted" rather than "killed" ¯\_(ツ)_/¯).

In order to examine the target of click, you need to examine the mouse position event associated with the interactive command (see "Accessing Mouse" in the elisp manual). The following should include all the necessary implementation details you're looking for:

(defun my/delete-on-click (event)
  "Delete the window targeted by mouse click EVENT."
  (interactive "e")
  (delete-window
   (posn-window (event-start event))))

(define-key global-map (kbd "<header-line> <mouse-2>") #'my/delete-on-click)

TIL: You can use <header-line> in key bindings! :-)

  • Thank you! It does exactly what I wanted! I will look deeper into accessing the mouse in Emacs too. – David Tamar Nov 2 '18 at 4:39
  • FWIW: mouse-remove-window in frame-cmds.el does that, but it first calls (mouse-minibuffer-check event). See that function in mouse.el. – Drew Nov 2 '18 at 14:58

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.