3

I hide the window title bar, so I can't use the close window button in the top right corner.

I want to add a menu item with no drop-down menu on the far right side of menu-bar, which can only be clicked as close window button.



A more detailed explanation of the problem added after Drew’s answer:

Close button should be close to the right edge of the window, There should be a space separator in the middle to isolate the close button from other menu items.

Tobias
  • 32,569
  • 1
  • 34
  • 75
dongli si
  • 63
  • 8

1 Answers1

1

C-h v menu-bar-final-items:

menu-bar-final-items is a variable defined in C source code.

Its value is (help-menu)

Documentation:

List of menu bar items to move to the end of the menu bar.

The elements of the list are event types that may have menu bar bindings.

Create a "menu" that acts like a button to do what you want, and add that menu after help-menu in the list value of menu-bar-final-items.

For example, supposing your command to close the window is close-the-gui-window:

(define-key global-map [menu-bar close-gui-window]
  '(menu-item
    "Close Window" close-the-gui-window
    :help "Close GUI window"))

(defun close-the-gui-window (&rest _args)  ; Just an example.
  "Delete selected frame by clicking a menu item bound to this command."
  (interactive)
  (delete-frame))

(add-to-list 'menu-bar-final-items 'close-gui-window 'append)

I use this approach, for example, in library ToolBar+ (tool-bar+.el.

Whenever tool-bar-pop-up-mode is enabled and the tool bar is not shown (tool-bar-mode is not active) the menu-bar shows a "menu" named Buttons, which has no menu items.

Clicking Buttons pops up the tool bar, and removes Buttons, for the duration of one command (either a tool-bar use or any other action). Then Buttons is shown again.

Drew
  • 75,699
  • 9
  • 109
  • 225
  • Hi Drew, if it was that simple I would not have upvoted the question. At least it does not work with emacs-26.2 under WSL Ubuntu-linux. Meanwhile I think integrating command calling menu items into the menu-bar is not possible. The menu-bar only takes keymaps for other menus and the `#'undefined' function for hiding menu items from global-map. **Please** prove me wrong;-). (I did even dig into the C-sources to look what is going on but currently I do not have the time for a thorough analysis.) – Tobias Sep 24 '19 at 14:41
  • @Drew, I want the close button to be in the top right corner of the screen (I also hide the gnome panel), which is the easiest position for the mouse to move to. – dongli si Sep 24 '19 at 15:12
  • @Drew I added a definition for `close-guid-window` to make the example self-contained. – Tobias Sep 24 '19 at 15:33
  • @donglisi I added a definition for `close-guid-window`. You can copy-paste the code now into your `*scratch*` buffer. It would be very nice if you could test whether the frame is deleted when you click on "Close Window" in the menu bar. It would be very interesting if that works for you. – Tobias Sep 24 '19 at 15:40
  • @tobias: Yes, it should work fine. You used the wrong command name (missing `-the`). The menu item name need not be the same as the command name. – Drew Sep 24 '19 at 15:56
  • @donglisi: I can't speak to use of Gnome etc. You asked for a menu name in the menu-bar, but with no menu, so that when you click it you perform some action (e.g. close the GUI window). And you asked for this to be at the right of the menu-bar. That's what this answer provides. It is variable `menu-bar-final-items` that puts the "button" at the right of the menu-bar. If it's not far enough to the right for you then you'll need to add some padding - phony menus with blank names might work. But be aware that the menu-bar menus change with different modes. – Drew Sep 24 '19 at 16:00
  • @Drew Ups. The `-the` was just a typo. I did many other tests before (including `easy-menu` stuff and `define-key` stuff). Does not work. Did you test it yourself? – Tobias Sep 24 '19 at 16:00
  • @Tobias: Yes. It works fine for me. I'm using MS Windows, but that shouldn't make a difference. Of course, `delete-window` won't delete the last (sole) GUI window. If you want to do that then use an action that kills Emacs. – Drew Sep 24 '19 at 16:02
  • @Drew Ah, I just tested with cygwin. It is working there but it is not working on Ubuntu WLS (`GNU Emacs 26.2 (build 2, x86_64-pc-linux-gnu, GTK+ Version 3.22.30) of 2019-04-12`). If I click `Close Window` there it looks like the menu item becomes an empty sub-menu. So it seems to be system dependent. My cygwin emacs is:`GNU Emacs 26.1 (build 1, x86_64-unknown-cygwin) of 2018-05-28` if that is relevant. **Interesting!** Because, I was about to post a similar answer as you did but it didn't work for me. – Tobias Sep 24 '19 at 16:06
  • @Drew FYI: My observations from [the last comment](https://emacs.stackexchange.com/questions/52807/how-to-add-a-right-aligned-menu-item-to-menu-bar#comment82484_52813) are also reproduceable with `GNU Emacs 27.0.50 (build 2, x86_64-pc-linux-gnu, GTK+ Version 3.22.30) of 2019-08-16`. – Tobias Sep 24 '19 at 16:14
  • @tobias: Sounds like a (platform-dependent) bug. Please consider reporting it. AFAIK there's nothing odd or special about the Lisp code I use. But is the problem about closing the window or is it about the behavior of the menu-bar menu? IOW, if you use a different action (e.g. just display a message) do you still see a problem? – Drew Sep 24 '19 at 17:13