0

I have a bunch of scripts for managing files/directories that I use on a regular basis. Would it be possible to assign a key binding such that whenever I hit this key binding I get a dialogue box that lists my scripts within emacs and I can choose one to execute on the fly?

This may be fairly obvious but I couldn't find anything about this online possibly because I do not know what the term to search for is.

1

You can use easy-menu-define for defining a popup menu and a menu in the menu bar. If you need to extend the menu later on you can use easy-menu-add-item.

Example:

(defun foo ()
  "Just an example script."
  (interactive)
  (message "*foo*"))

(defun bar ()
  "Just another example script."
  (interactive)
  (message "*bar*"))

(easy-menu-define
  my-script-menu
  global-map
  "My Scripts"
  '("Scripts" ["Call foo" foo t])) ;; List of all menu items which you know when you create the menu.

(easy-menu-add-item ;; For adding menu items later on.
 global-map
 '(menu-bar Scripts)
 ["Call bar" bar :keys ""])

(defun my-open-script-menu ()
  "Open a popup menu at mouse location with my scripts."
  (interactive)
  (let* ((key-list (x-popup-menu t my-script-menu)) ;; x-popup-menu returns a list of events.
     (key-sequence (vconcat key-list)) ;; ... but key sequences are vectors
     (command (lookup-key my-script-menu key-sequence))) ;; now looking up the key sequence in the keymap
    (when (commandp command) ;; when successful call the command
      (call-interactively command))))

(global-set-key (kbd "C-c c") #'my-open-script-menu)
| improve this answer | |

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.