passerby51
passerby51

Reputation: 955

Emacs predictive mode and cycling with TAB

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

Upvotes: 2

Views: 540

Answers (1)

Toby Cubitt
Toby Cubitt

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

Related Questions