Reputation: 955
I am trying to use predictive mode in Emacs for auto-completion in LaTeX documents. When TAB is pressed I want it to do the following
complete the word if there is only one possibility. (This usually does not happen. The rest of the word is highlighted in blue and I have to press [Ctrl]-Enter to complete.)
cycle through alternatives if there are many. (I have no idea how to make it do this.)
Upvotes: 2
Views: 540
Reputation: 453
You need to check how many completion candidates there are, and call completion-accept
or completion-cycle
accordingly.
The follow should do the trick:
(defun completion-accept-or-cycle (&optional n)
"Accept current completion if there's only one possible candidate.
Otherwise, cycle the completion candidates. A numerical prefix argument
N specifies the number of candidates to cycle forwards (or backwards if
N is negative)."
(interactive)
(let ((overlay (completion-ui-overlay-at-point)))
(when overlay
(if (= (length (overlay-get overlay 'completions)) 1)
(completion-accept)
(completion-cycle n)))))
Now bind TAB to this new completion-accept-or-cycle
command in the completion-overlay-map
keymap in your .emacs:
(define-key completion-overlay-map "\t" 'completion-accept-or-cycle)
Upvotes: 3