Skip to main content
added 133 characters in body
Source Link
ideasman42
  • 9.2k
  • 1
  • 35
  • 123
(defun custom-popup (my-prompt default-index my-content)
  "Pop up menu
Takes args: my-prompt, default-index, my-content).
Where the content is any number of (string, function) pairs,
each representing a menu item."
  (cond
    ;; Ivy (optional)
    ((fboundp 'ivy-read)
      (ivy-read 
 my-prompt my-content
   prompt content
     :preselect
     (cond
   (if   ((= default-index -1)
       nil)
      (t
       default-index))
        :require-match t
        :action
        (lambda (x)
          (pcase-let ((`(,_text . ,action) x))
            (funcall action)))
        :caller #'custom-popup))
  
   ;; Fallback to completing read.
    (t
      (let ((choice
           (funcall completing-read-function
                     my-prompt 
 my-                   content
                    nil
                    t 
 nil                   nil
                    nil
                    (nth default-index my-content))))
        (pcase-let ((`(,_text . ,action) (assoc choice my-content)))
          (funcall action))))))
(defvar
  my-global-utility-menu-def
  ;; (content, prompt, default_index)
  '(("Emacs REPL" . ielm)
    ("Spell Check" . ispell-buffer)
    ("Delete Trailing Space In Buffer" . delete-trailing-whitespace)
    ("Emacs Edit Init" . (lambda () (find-file user-init-file)))))

(defun my-global-utility-popup ()
  "Pop up my menu. Hit RET immediately to select the default item."
  (interactive)
  (custom-popup "Select: " 1 my-global-utility-menu-def "Select: ", 1))

(global-set-key (kbd "<f12>") 'my-global-utility-popup)
(defun custom-popup (my-prompt default-index my-content)
  "Pop up menu
Takes args: my-prompt, default-index, my-content).
Where the content is any number of (string, function) pairs,
each representing a menu item."
  (cond
    ;; Ivy (optional)
    ((fboundp 'ivy-read)
      (ivy-read my-prompt my-content
        :preselect
        (if (= default-index -1) nil default-index)
        :require-match t
        :action
        (lambda (x)
          (pcase-let ((`(,_text . ,action) x))
            (funcall action)))
        :caller #'custom-popup))
    ;; Fallback to completing read.
    (t
      (let ((choice (completing-read
                     my-prompt my-content nil t nil nil
                     (nth default-index my-content))))
        (pcase-let ((`(,_text . ,action) (assoc choice my-content)))
          (funcall action))))))
(defvar
  my-global-utility-menu-def
  ;; (content, prompt, default_index)
  '(("Emacs REPL" . ielm)
    ("Spell Check" . ispell-buffer)
    ("Delete Trailing Space In Buffer" . delete-trailing-whitespace)
    ("Emacs Edit Init" . (lambda () (find-file user-init-file))))

(defun my-global-utility-popup ()
  "Pop up my menu. Hit RET immediately to select the default item."
  (interactive)
  (custom-popup my-global-utility-menu-def "Select: ", 1))

(global-set-key (kbd "<f12>") 'my-global-utility-popup)
(defun custom-popup (prompt default-index content)
  "Pop up menu
Takes args: prompt, default-index, content).
Where the content is any number of (string, function) pairs,
each representing a menu item."
  (cond
   ;; Ivy (optional)
   ((fboundp 'ivy-read)
    (ivy-read 
     prompt content
     :preselect
     (cond
      ((= default-index -1)
       nil)
      (t
       default-index))
     :require-match t
     :action
     (lambda (x)
       (pcase-let ((`(,_text . ,action) x))
         (funcall action)))
     :caller #'custom-popup))
 
   ;; Fallback to completing read.
   (t
    (let ((choice
           (funcall completing-read-function
                    prompt 
                    content
                    nil
                    t 
                    nil
                    nil
                    (nth default-index content))))
      (pcase-let ((`(,_text . ,action) (assoc choice content)))
        (funcall action))))))
(defvar my-global-utility-menu-def
  ;; (content, prompt, default_index)
  '(("Emacs REPL" . ielm)
    ("Spell Check" . ispell-buffer)
    ("Delete Trailing Space In Buffer" . delete-trailing-whitespace)
    ("Emacs Edit Init" . (lambda () (find-file user-init-file)))))

(defun my-global-utility-popup ()
  "Pop up my menu. Hit RET immediately to select the default item."
  (interactive)
  (custom-popup "Select: " 1 my-global-utility-menu-def))

(global-set-key (kbd "<f12>") 'my-global-utility-popup)
deleted 275 characters in body
Source Link
ideasman42
  • 9.2k
  • 1
  • 35
  • 123

This optionally uses ivy (when available), which has more advanced features such as being able to activate items while keeping the menu open.

Generic popup.

(defun custom-popup (menumy-prompt menudefault-index menumy-content) "
Pop  "Pop up menu
Takes args: menumy-prompt, default-index, menumy-content).
Where the content is any number of (string, function) pairs,  
each representing a menu item."
User can hit(cond
 just the first char;; ofIvy a(optional)
 menu item to choose((fboundp it.'ivy-read)
Or click it with `mouse-1' or `mouse(ivy-2' toread selectmy-prompt it.my-content
Or hit RET immediately to select the default item.
":preselect
  (interactive)
  (let*
    (
     if (icicle-Completions-max-columns             = default-index -1)
     nil (icicle-show-Completions-initiallydefault-flag       tindex)
      (icicle-incremental-completion-delay  :require-match t
        0.01):action
      (icicle-top-level-when-sole-completion-flag  (lambda t(x)
      (icicle-top-level-when-sole-completion-delay  0.01)
      (icicle-defaultpcase-value        let ((`(,_text . ,action) x))
            (funcall taction)))
      (icicle-show-Completions-help-flag   :caller #'custom-popup))
    ;; Fallback to completing nil)read.
      (choicet
      (let ((choice (completing-read
          ;; menu prompt & content
          menumy-prompt menumy-content nil t nil nil
          ;; default index
          (nth menudefault-index menumy-content))))
      (action  (cdrpcase-let ((`(,_text . ,action) (assoc choice menumy-content))))
          (funcall action))))))

Generic popup.

(defun custom-popup (menu-prompt menu-index menu-content) "
Pop up menu
Takes args: menu-prompt, default-index, menu-content).
Where the content is any number of (string, function) pairs, each representing a menu item.
User can hit just the first char of a menu item to choose it.
Or click it with `mouse-1' or `mouse-2' to select it.
Or hit RET immediately to select the default item.
"
  (interactive)
  (let*
    (
      (icicle-Completions-max-columns               1)
      (icicle-show-Completions-initially-flag       t)
      (icicle-incremental-completion-delay          0.01)
      (icicle-top-level-when-sole-completion-flag   t)
      (icicle-top-level-when-sole-completion-delay  0.01)
      (icicle-default-value                         t)
      (icicle-show-Completions-help-flag            nil)
      (choice
        (completing-read
          ;; menu prompt & content
          menu-prompt menu-content nil t nil nil
          ;; default index
          (nth menu-index menu-content)))
      (action  (cdr (assoc choice menu-content))))
    (funcall action)))

This optionally uses ivy (when available), which has more advanced features such as being able to activate items while keeping the menu open.

Generic popup.

(defun custom-popup (my-prompt default-index my-content)
  "Pop up menu
Takes args: my-prompt, default-index, my-content).
Where the content is any number of (string, function) pairs, 
each representing a menu item."
  (cond
    ;; Ivy (optional)
    ((fboundp 'ivy-read)
      (ivy-read my-prompt my-content
        :preselect
        (if (= default-index -1) nil default-index)
        :require-match t
        :action
        (lambda (x)
          (pcase-let ((`(,_text . ,action) x))
            (funcall action)))
        :caller #'custom-popup))
    ;; Fallback to completing read.
    (t
      (let ((choice (completing-read
                     my-prompt my-content nil t nil nil
                     (nth default-index my-content))))
        (pcase-let ((`(,_text . ,action) (assoc choice my-content)))
          (funcall action))))))
Use 3 args
Source Link
ideasman42
  • 9.2k
  • 1
  • 35
  • 123
(defun custom-popup (menu-def)
prompt menu-index "Popmenu-content) up"
Pop myup menu
Takes 3 items (content,args: menu-prompt, default-index, menu-content).
Where the content is any number of (string, function) pairs, each representing a menu item.
User can hit just the first char of a menu item to choose it.
Or click it with `mouse-1' or `mouse-2' to select it.
Or hit RET immediately to select the default item. 
"
  (interactive)
  (let* ((menu-content (nth 0 menu-def))
         (menu-prompt (nth 1 menu-def))
         (menu-index (nth 2 menu-def))
         (icicle-Completions-max-columns               1)
         (icicle-show-Completions-initially-flag       t)
         (icicle-incremental-completion-delay          0.01)
         (icicle-top-level-when-sole-completion-flag   t)
         (icicle-top-level-when-sole-completion-delay  0.01)
         (icicle-default-value                         t)
         (icicle-show-Completions-help-flag            nil)
         (choice
          (completing-read
           ;; menu prompt & content
           menu-prompt menu-content nil t nil nil
           ;; default index
           (nth menu-index menu-content)))
         (action  (cdr (assoc choice menu-content))))
    (funcall action)))
(defvar
  my-global-utility-menu-def
  ;; (content, prompt, default_index)
  '((("Emacs REPL" . ielm)
     ("Spell Check" . ispell-buffer)
     ("Delete Trailing Space In Buffer" . delete-trailing-whitespace)
     ("Emacs Edit Init" . (lambda () (find-file user-init-file)
     )) "Select:" 1))

(defun my-global-utility-popup ()
  "Pop up my menu. Hit RET immediately to select the default item."
  (interactive)
  (custom-popup my-global-utility-menu-def "Select: ", 1))

(global-set-key (kbd "<f12>") 'my-global-utility-popup)
(defun custom-popup (menu-def)
  "Pop up my menu
Takes 3 items (content, prompt, default-index).
Where the content is any number of (string, function) pairs, each representing a menu item.
User can hit just the first char of a menu item to choose it.
Or click it with `mouse-1' or `mouse-2' to select it.
Or hit RET immediately to select the default item."
  (interactive)
  (let* ((menu-content (nth 0 menu-def))
         (menu-prompt (nth 1 menu-def))
         (menu-index (nth 2 menu-def))
         (icicle-Completions-max-columns               1)
         (icicle-show-Completions-initially-flag       t)
         (icicle-incremental-completion-delay          0.01)
         (icicle-top-level-when-sole-completion-flag   t)
         (icicle-top-level-when-sole-completion-delay  0.01)
         (icicle-default-value                         t)
         (icicle-show-Completions-help-flag            nil)
         (choice
          (completing-read
           ;; menu prompt & content
           menu-prompt menu-content nil t nil nil
           ;; default index
           (nth menu-index menu-content)))
         (action  (cdr (assoc choice menu-content))))
    (funcall action)))
(defvar
  my-global-utility-menu-def
  ;; (content, prompt, default_index)
  '((("Emacs REPL" . ielm)
     ("Spell Check" . ispell-buffer)
     ("Delete Trailing Space In Buffer" . delete-trailing-whitespace)
     ("Emacs Edit Init" . (lambda () (find-file user-init-file)
     )) "Select:" 1))

(defun my-global-utility-popup ()
  "Pop up my menu. Hit RET immediately to select the default item."
  (interactive)
  (custom-popup my-global-utility-menu-def))

(global-set-key (kbd "<f12>") 'my-global-utility-popup)
(defun custom-popup (menu-prompt menu-index menu-content) "
Pop up menu
Takes args: menu-prompt, default-index, menu-content).
Where the content is any number of (string, function) pairs, each representing a menu item.
User can hit just the first char of a menu item to choose it.
Or click it with `mouse-1' or `mouse-2' to select it.
Or hit RET immediately to select the default item. 
"
  (interactive)
  (let*
    (
      (icicle-Completions-max-columns               1)
      (icicle-show-Completions-initially-flag       t)
      (icicle-incremental-completion-delay          0.01)
      (icicle-top-level-when-sole-completion-flag   t)
      (icicle-top-level-when-sole-completion-delay  0.01)
      (icicle-default-value                         t)
      (icicle-show-Completions-help-flag            nil)
      (choice
        (completing-read
          ;; menu prompt & content
          menu-prompt menu-content nil t nil nil
          ;; default index
          (nth menu-index menu-content)))
      (action  (cdr (assoc choice menu-content))))
    (funcall action)))
(defvar
  my-global-utility-menu-def
  ;; (content, prompt, default_index)
  '(("Emacs REPL" . ielm)
    ("Spell Check" . ispell-buffer)
    ("Delete Trailing Space In Buffer" . delete-trailing-whitespace)
    ("Emacs Edit Init" . (lambda () (find-file user-init-file))))

(defun my-global-utility-popup ()
  "Pop up my menu. Hit RET immediately to select the default item."
  (interactive)
  (custom-popup my-global-utility-menu-def "Select: ", 1))

(global-set-key (kbd "<f12>") 'my-global-utility-popup)
added 111 characters in body
Source Link
ideasman42
  • 9.2k
  • 1
  • 35
  • 123
Loading
added 1 character in body
Source Link
ideasman42
  • 9.2k
  • 1
  • 35
  • 123
Loading
Source Link
ideasman42
  • 9.2k
  • 1
  • 35
  • 123
Loading