Autocomplete with a dictionary and hippie-expand
I use company-mode most of the time. Sometimes I use Hippie Expand to autocomplete the English words from a dictionary.
I surely can do this in company-mode too. But I prefer hippie-expand because I choose to make company-mode focus on programming stuff and hippie-expand on writing.
Solution
This solution works in any environment.
- Step1 (OPTIONAL), download english-words.txt and place it under "~/.emacs.d/misc/".
- Step 2, Copy below setup into ~/.emacs and use key binding "M-/" to complete the word:
(global-set-key (kbd "M-/") 'hippie-expand)
;; The actual expansion function
(defun try-expand-by-dict (old)
;; old is true if we have already attempted an expansion
(unless (bound-and-true-p ispell-minor-mode)
(ispell-minor-mode 1))
;; english-words.txt is the fallback dicitonary
(if (not ispell-alternate-dictionary)
(setq ispell-alternate-dictionary (file-truename "~/.emacs.d/misc/english-words.txt")))
(let ((lookup-func (if (fboundp 'ispell-lookup-words)
'ispell-lookup-words
'lookup-words)))
(unless old
(he-init-string (he-lisp-symbol-beg) (point))
(if (not (he-string-member he-search-string he-tried-table))
(setq he-tried-table (cons he-search-string he-tried-table)))
(setq he-expand-list
(and (not (equal he-search-string ""))
(funcall lookup-func (concat (buffer-substring-no-properties (he-lisp-symbol-beg) (point)) "*")))))
(if (null he-expand-list)
(if old (he-reset-string))
(he-substitute-string (car he-expand-list))
(setq he-expand-list (cdr he-expand-list))
t)
))
(setq hippie-expand-try-functions-list
'(;; try-expand-dabbrev
;; try-expand-dabbrev-all-buffers
try-expand-by-dict))
Technical details
- based on ac-ispell
- lazy load of ispell-mode to speed Emacs startup
- add a fallback dictionary "english-words.txt" so autocompletion never fails
- `ispell-lookup-words` or `lookup-words` simply does grep thing, so english-words.txt is just a plain text file.