-
-
Notifications
You must be signed in to change notification settings - Fork 337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
multi-level menu with ivy? #1640
Comments
Ivy is single-level. You can add multiple levels by calling |
As I showed in the gif. the first level menu contains some categories and the second level contains some synonyms of the chosen category to replace word at point. When I choose the wrong category and want to go back, I hit escape or backspace to go back to the previous menu. Such actions can be repeated infinitely. I found it hard to implement such feature with ivy API especially when the list structure is complex. (defun stardict-thesaurus--do-replace (replacement)
(if (use-region-p)
(delete-region (region-beginning) (region-end))
(let ((bounds (bounds-of-thing-at-point 'word)))
(delete-region (car bounds) (cdr bounds))))
(insert replacement))
(defun stardict-thesaurus--pick-synonym (synonyms)
(let ((key (stardict-thesaurus--pick-lvl-1 synonyms)))
(stardict-thesaurus--pick-lvl-2 key synonyms)))
(defun stardict-thesaurus--pick-lvl-1 (synonyms)
(let ((keys (mapcar #'car synonyms)))
(completing-read "Choose a key: " keys)))
(defun stardict-thesaurus--pick-lvl-2 (key synonyms)
(let* ((repick (lambda ()
(interactive)
(ivy-quit-and-run
(stardict-thesaurus--pick-synonym synonyms))))
(ivy-on-del-error-function repick)
(kmap (make-sparse-keymap)))
(define-key kmap (kbd "<escape>") repick)
(ivy-read "Choose a synonym: "
(assoc-default key synonyms)
:require-match t
:keymap kmap
:action #'stardict-thesaurus--do-replace
:caller 'stardict-thesaurus--pick-lvl-2)))
Here I play with |
I think you went with a good approach. Re-using Your code looks good enough for me, you are using the API correctly to implement something more advanced than what the API was originally intended to do.
|
Thanks for your suggestions! Any plan to extend the API to support such feature? Maybe a keyword argument such as |
There's no plan to do it. There's no need to complicate the API when the existing one can be used to create the same functionality. |
Hi,
How to implement a multi-level menu with ivy? I tried on a simple two-level case:
This is accomplished by manipulating
ivy-on-del-error-function
andivy-quit-and-run
which is very cumbersome. Is there a canonical way to do this kind of job?The text was updated successfully, but these errors were encountered: