Created
October 16, 2018 11:10
-
-
Save JohnLunzer/7c6d72a14c76c0a3057535e4f6148ef8 to your computer and use it in GitHub Desktop.
Hippie expansion menu using ivy (fallback to ido)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; * IDO/IVY completion menu | |
;; This is originally from https://www.emacswiki.org/emacs/HippieExpand#toc10 | |
;; | |
;; There are a few improvements: | |
;; - It is undo friendly with the addition of save-excursion | |
;; - It will use ivy for the expansion menu if ivy is available, falls back to ido | |
;; - Automatically expands if there is only one possible expansion | |
(defun my-hippie-expand-completions (&optional hippie-expand-function) | |
"Return list of completions generated by `hippie-expand'." | |
(save-excursion | |
(let ((this-command 'my-hippie-expand-completions) | |
(last-command last-command) | |
(hippie-expand-function (or hippie-expand-function 'hippie-expand))) | |
(while (progn | |
(funcall hippie-expand-function nil) | |
(setq last-command 'my-hippie-expand-completions) | |
(not (equal he-num -1)))) | |
;; Provide the options in the order in which they are normally generated. | |
(delete he-search-string (reverse he-tried-table))))) | |
(defun my-ido-or-ivy-hippie-expand-with (hippie-expand-function) | |
"Offer ido or ivy based completion using the specified hippie-expand function." | |
(let* ((options (my-hippie-expand-completions hippie-expand-function))) | |
(if options | |
(progn | |
(if (> (safe-length options) 1) | |
(if (require 'ivy nil t) | |
(setq selection (ivy-read "Completions: " options)) | |
(setq selection (ido-completing-read "Completions: " options))) | |
(setq selection (car options))) | |
(if selection | |
(he-substitute-string selection t))) | |
(message "No expansion found")))) | |
(defun my-ido-or-ivy-hippie-expand () | |
"Offer ido or ivy based completion for the word at point." | |
(interactive) | |
(my-ido-or-ivy-hippie-expand-with 'hippie-expand)) | |
;; I like to use C-v as a prefix because it's right next to X and C, just seems natural and adds tons more shortcut options | |
(define-prefix-command 'c-v-prefix) | |
(global-set-key (kbd "C-v") 'c-v-prefix) | |
(global-set-key (kbd "C-v C-v") 'my-ido-or-ivy-hippie-expand) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As I make my first tentative steps into the world of emacs...from vim...this has been fantastic. Thanks very much for (4 years ago!) adapting the wiki code to ivy. Incredibly useful.
Note: if using evil and binding to C-n with
(setq evil-complete-next-func 'my-ido-or-ivy-hippie-expand)
I had to change the finaldefun
to(defun my-ido-or-ivy-hippie-expand (arg)