Skip to content

Instantly share code, notes, and snippets.

@rjl6789
Forked from JohnLunzer/hippie.el
Created March 22, 2022 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjl6789/2693b1a2c050ab7f097eb6baf6532156 to your computer and use it in GitHub Desktop.
Save rjl6789/2693b1a2c050ab7f097eb6baf6532156 to your computer and use it in GitHub Desktop.
Hippie expansion menu using ivy (fallback to ido)
;; * 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