branch: externals/frog-menu commit 94c99cfd647c71e8e46c82de8dd8a282d44c9efc Author: Clemens Radermacher <clem...@posteo.net> Commit: Clemens Radermacher <clem...@posteo.net>
Add support for all collection formats of completing-read --- frog-menu.el | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 51 insertions(+), 10 deletions(-) diff --git a/frog-menu.el b/frog-menu.el index a8e38b6..512dd64 100644 --- a/frog-menu.el +++ b/frog-menu.el @@ -641,27 +641,68 @@ COLLECTION are the arguments from `frog-menu-read'." (replace-regexp-in-string "\\(: ?\\)?\\'" ": " prompt)) collection args)) -(defun frog-menu-completing-read-function (prompt collection &rest _) +(defun frog-menu-completing-read-function (prompt collection predicate &rest _) "Can be used as `completing-read-function'. -For now all arguments other than PROMPT and COLLECTION are -ignored. COLLECTION has to use a format `frog-menu-read' can -understand." - (frog-menu-read prompt collection)) +PROMPT, COLLECTION and PREDICATE are of format as specified by +`completing-read'." + (let ((strings (frog-menu--collection-to-strings collection predicate))) + (frog-menu-read prompt strings))) ;;;###autoload (defun frog-menu-call (cmds &optional prompt) "Read a command from CMDS and execute it. -CMDS is a list of command symbols to choose from. If PROMPT is -given it should be a string with prompt information for the -user." - (let ((cmd (intern-soft (frog-menu-read (or prompt "") - (mapcar #'symbol-name cmds))))) +CMDS is of format as specified by `completing-read' +collections. If PROMPT is given it should be a string with prompt +information for the user." + (let ((cmd (intern-soft (frog-menu-read + (or prompt "") + (frog-menu--collection-to-strings cmds))))) (command-execute cmd))) +(defun frog-menu--collection-to-strings (collection &optional predicate) + "Return list of strings representing COLLECTION. +COLLECTION and PREDICATE should have the format as specified by +`completing-read'." + (cond ((functionp collection) + (let ((cands (funcall collection "" predicate t))) + (if (stringp (car-safe cands)) + (copy-sequence cands) + (mapcar #'symbol-name cands)))) + ((listp collection) + (let ((strings ())) + (dolist (el collection (nreverse strings)) + (unless (and predicate + (funcall predicate el)) + (let ((cand (or (car-safe el) el))) + (push (if (symbolp cand) + (symbol-name cand) + cand) + strings)))))) + ((hash-table-p collection) + (let ((strings ())) + (maphash + (lambda (key val) + (unless (and predicate + (funcall predicate key val)) + (push (if (symbolp el) + (symbol-name el) + el) + strings)))) + (nreverse strings))) + ((vectorp collection) + (let ((strings ())) + (mapatoms + (lambda (el) + (unless (and predicate + (funcall predicate el)) + (push (symbol-name el) strings)))) + (nreverse strings))))) + + ;;;###autoload (defun frog-menu-read (prompt collection &optional actions) "Read from a menu of variable `frog-menu-type'.