The following code (mostly coming from here) creates a company-mode back-end and binds TAB
to it:
(require 'company)
(defconst sample-completions
'("procedural" "functional" "high-level" "low-level" "statically-scoped" "dynamically-scoped"))
(defun company-sample-backend (command &optional arg &rest ignored)
(interactive (list 'interactive))
(let ((my-completions (copy-sequence sample-completions)))
(case command
(interactive (company-begin-backend 'company-sample-backend))
(prefix (and (eq major-mode 'fundamental-mode)
(company-grab-symbol)))
(candidates
(remove-if-not
(lambda (c) (string-prefix-p arg c))
my-completions)))))
(add-to-list 'company-backends 'company-sample-backend)
(define-key company-mode-map (kbd "TAB") 'company-sample-backend)
I have checked that the above works. That is, when I put a regular buffer (such as *scratch*
) in company
mode, I can invoke completion by pressing TAB
and the sample-completions
collection is used for completion.
Now I am trying to read a string from mini-buffer with auto-completion. To make sure that the mini-buffer is in company
mode, I evaluate:
(defun my-minibuffer-mode ()
(company-mode 1))
(add-hook 'minibuffer-setup-hook 'my-minibuffer-mode)
Now I try:
(read-from-minibuffer "Prompt: ")
However, what I get is completion from the English dictionary and not from my sample-completions
collection. How do I fix this?
major-mode
incompany-sample-backend
. How about rewriting like(prefix (company-grab-symbol))
?(memq major-mode '(fundamental-mode minibuffer-inactive-mode))
. The result seems to work as expected.(eq major-mode 'fundamental-mode)
. Is an active mini-buffer in a special mini-buffer mode that is different from the fundamental mode?t
makes it work!