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)
        (lambda (c) (string-prefix-p arg c))

(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?

  • You seems to limit the major-mode in company-sample-backend. How about rewriting like (prefix (company-grab-symbol))?
    – lurdan
    Aug 2 '17 at 9:14
  • Or try this major-mode check instead: (memq major-mode '(fundamental-mode minibuffer-inactive-mode)). The result seems to work as expected.
    – Dmitry
    Aug 2 '17 at 9:42
  • Or indeed drop this comparison if this backend is only going to be used in the minibuffer.
    – Dmitry
    Aug 2 '17 at 9:43
  • @Dmitry To tell you the truth, I do not really understand what this code is doing. I took it verbatim. I think you are talking about the check (eq major-mode 'fundamental-mode). Is an active mini-buffer in a special mini-buffer mode that is different from the fundamental mode? Aug 2 '17 at 20:46
  • 2
    @Dmitry You are right. Changing this check for t makes it work! Aug 2 '17 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.