CompanyMode

Company stands for “complete anything” and is a modular in-buffer completion mechanism.

http://company-mode.github.io/

http://company-mode.github.io/images/company-elisp.png

Backends

Backends are single functions, they look like this:

  (defun company-my-backend (command &optional arg &rest ignored)
    (interactive (list 'interactive))
    (case command
      (interactive (company-begin-backend 'company-my-backend))
      (prefix (when (looking-back "foo\\>")
                (match-string 0)))
      (candidates (when (equal arg "foo")
                    (list "foobar" "foobaz" "foobarbaz")))
      (meta (format "This value is named %s" arg))))

They can be installed simply by adding them to company-backends. If it is in a file named like the function (e.g. company-my-backend.el), it will be required automatically.

A list of user-written backends: CompanyModeBackends

Frontends

A visualization (frontend) is also a single function. It looks like this:

  (defun company-my-frontend (command)
    (case command
      (pre-command (echo "%s" (car company-candidates)))
      (post-command (echo "%s" (car company-candidates)))
      (hide (echo ""))))

It can be installed by adding it to company-frontends.

Related packages other than backends

helm-company provides a helm interface for company-mode.

company-quickhelp provides popup documentation for completion candidates, as here.

Issues & questions

Why is `company-backends' initialized with `company-css', `company-elisp', etc? Shouldn't we add those symbols to `company-backends' when their respective libraries are required?

No need to: iterating through backends that don’t apply to the current buffer is pretty fast.

But the user can have different `’company-backends’ values in different major modes. It would make it a tiny bit more efficient, and also allow to use certain backends only in certain modes, like ‘company-dabbrev’ and ‘company-ispell’, which would generally apply to almost any buffer and thus block all backends after them.

  (add-hook 'css-mode-hook
            (lambda ()
              (set (make-local-variable 'company-backends) '(company-css))))

Can I complete by typing a key instead of waiting for the delay timer?

Yes, just bind ‘company-manual-begin’, ‘company-complete-common’, ‘company-complete’, ‘company-select-next’ to a key in the global map.

  (global-set-key "\t" 'company-complete-common)

Alternatively, you can set ‘company-idle-delay’ to 0, to always complete immediately.

I don't like the default colors!

Contributions welcome.

Meanwhile, you can look for a theme that customizes the Company faces.

Or customize them yourself.

And if you’re using a theme with a dark background, here’s another quick option:

  (require 'color)
  
  (let ((bg (face-attribute 'default :background)))
    (custom-set-faces
     `(company-tooltip ((t (:inherit default :background ,(color-lighten-name bg 2)))))
     `(company-scrollbar-bg ((t (:background ,(color-lighten-name bg 10)))))
     `(company-scrollbar-fg ((t (:background ,(color-lighten-name bg 5)))))
     `(company-tooltip-selection ((t (:inherit font-lock-function-name-face))))
     `(company-tooltip-common ((t (:inherit font-lock-constant-face))))))

(Borrowed from Joel Holdbrooks's init directory).

How do I get substring completion?

Non-prefix completion is supported since version 0.6.13, but returning appropriate candidates is the responsibility of each backend. If ‘company-capf’ is in ‘company-backends’ (which is by default when using Emacs 24.4), it obeys ‘completion-styles’ when fetching candidates from ‘completion-at-point-functions’.

Any other backend would need to support non-prefix matching explicitly.

When non-prefix matching is used, ‘company-preview-if-just-one-frontend’ isn’t displayed instead of ‘company-pseudo-tooltip-unless-just-one-frontend’, as would be usual. Even if there’s just one candidate, as long as it doesn’t start with the input, you get a popup menu.

<…> I want to complete ‘eval-when-compile’ and I know there are several functions beginning with ‘eval’, but there are fewer which have ‘when’ in their names. So I type ‘when’ and company lists ‘eval-when-compile’ too among others with ‘when’ in the name.

Similarly to iswitchb. Iswitchb is great, because I don’t have to type the beginning of the buffer name, I can type any part of it.

<…> Better yet: obey ‘completion-styles’, so the user can choose between ‘prefix’, ‘substring’, ‘partial-completion’, or any other style she likes.

Note that ‘completion-styles’ begins with ‘basic’ by default, and all styles are tried in turn. So unless you customize this variable, if you just type ‘when’, you won’t get ‘eval-when-compile’ among the candidates. You’ll have to input a prefix that doesn’t have any matches when using the ‘basic’ style.

The search feature was actually inspired by the way I use iswitchb. You can start completion with an empty prefix, type C-s, search for “when”, and press C-o to limit the search. – nschum

I’ve written another command named ‘company-filter-candidates’ that will probably be (edit: is) in version 0.2. It works like automatically pressing C-o after every search char. The search still has to be started manually (i.e. one key stroke). – nschum

How do I disable the down-case feature of the dabbrev backend?

Customize ‘company-dabbrev-downcase’.

I want to have completion and indent bound to tab key (as it is done and works fine in Ca2+), how can i achieve that?

Depending on the emphasis, one of these:

  (defun complete-or-indent ()
    (interactive)
    (if (company-manual-begin)
        (company-complete-common)
      (indent-according-to-mode)))
  (defun indent-or-complete ()
    (interactive)
    (if (looking-at "\\_>")
        (company-complete-common)
      (indent-according-to-mode)))

Yasnippet integration

If the two fixes below don’t work for you, you can try: https://gist.github.com/sebastiencs/a16ea58b2d23e2ea52f62fcce70f4073

Company interferes with Yasnippet’s native behaviour. Here’s a quick fix: http://gist.github.com/265010

Another code for solving conflicts in Company and Yasnippet.

  (defun check-expansion ()
    (save-excursion
      (if (looking-at "\\_>") t
        (backward-char 1)
        (if (looking-at "\\.") t
          (backward-char 1)
          (if (looking-at "->") t nil)))))

  (defun do-yas-expand ()
    (let ((yas/fallback-behavior 'return-nil))
      (yas/expand)))

  (defun tab-indent-or-complete ()
    (interactive)
    (if (minibufferp)
        (minibuffer-complete)
      (if (or (not yas/minor-mode)
              (null (do-yas-expand)))
          (if (check-expansion)
              (company-complete-common)
            (indent-for-tab-command)))))

  (global-set-key [tab] 'tab-indent-or-complete)

YoungtaekOh

Discussion

How does this compare to CompletionUI and AutoComplete? – TobyCubitt

See also the discussion at AutoComplete. – AndyStewart

It predates AutoComplete. The focus for CompanyMode is to have self-contained front and backends that can be easily swapped and loaded.


IMHO, the loading mechanism of company-mode needs to be changed. Instead of asking the user to autoload directly from their .emacs, company-mode can provide a feature file, which looks at company-backends, and automatically adds hooks to company mode to the modes declared in the elements in the backend list? This will of course, makes naming any backend “company-<major-mode>.el a convention that affects real functionality. Company mode can still be autoloaded, but from the feature file instead. This way company mode can load faster and the user won’t have to do anything besides requiring the company feature.

I don’t think so. This logic has too much magic, and it doesn’t really solve any big problems.

  (require 'company-foo)

in a user’s config isn’t much better than

  (push 'company-foo company-backends)

CategoryCompletion