Search code examples
emacselispmodeline

Add button with dynamic menu to emacs's modeline?


I'm making my own minor mode for emacs. Now I want to add button to modeline. Click on this button must сause pop-up menu appear. The items of this menu depend on user's actions. I know that there is a way to add a function button to modeline with `minor-mode-alist', but I have no idea how to make dynamic menu.


Solution

  • I found more proper way: When you define minor mode, you can specify :lighter param

    (define-minor-mode my-minor-mode
      "docstring"
      nil
      :lighter (:eval (format "%s%.5s" "#" "some code if you want dynamic label"))
      ;or just string :lighter "static string"
      :keymap my-minor-mode-map
    ... ... ... rest of code ....
    

    then you can use easymenu:

    (require 'easymenu)
    
    (easy-menu-define my-minor-mode-menu
      my-minor-mode-map
      "Menu for my-minor-mode"
       '("some text"
          "-")) ;separator
    
    ; and then add menu items with easy-menu-add-item and remove with easy-menu-remove-item
    ; it's nothing hard. Read the docs ;)
    

    This menu will be added to global menu-bar and it will pop up if you click on auto added(cause you specified :lighter param) minor-mode button on modeline.