SiteMap Search ElispArea HowTo Glossary RecentChanges News Problems Suggestions
Yemen, National Day

CompanyMode

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

http://nschum.de/src/emacs/company-mode/

http://nschum.de/src/emacs/company-mode/company-elisp.png http://nschum.de/src/emacs/company-mode/company-cpp.png

Back-ends

Back-ends are single functions, they look like this:

  (defun company-my-backend (command &optional arg &rest ignored)
    (case command
      (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 back-ends on the Wiki: CompanyModeBackends

Front-ends

A visualization (front-end) 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.

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?

Yes, ideally. But that would make configuration more difficult. Currently the user just has to drop the back-end file in the load-path, and company.el doesn’t have to be modified. If users want to go through the trouble to make it a bit more efficient, they can do this:

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

RichardRiley : Also in the current (4.3) css mode does not automatically invoke company. The line which worked for me was

   (add-hook 'css-mode-hook (lambda () (company-mode)))

YHWong: 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.

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 t, to always complete immediately.

I want to have completion and indent bound to tab key (as it is done and work 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)))

Can I complete only after running ''self-insert-command''? When moving around the cursor I find it very annoying to get completions (for code which is already good).

Yes, set company-begin-commands:

  (setq company-begin-commands '(self-insert-command))

nschum

Yasnippet integration

Company does interfere 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 back-ends that can be easily swapped and loaded.

I wrote the below snippet to try company with a a predictive backend. I’m aiming to write the opposite snippet as well so both completionui and company can coexist in harmony.

;; completionui-company-freundschaft embryo (defun company-predictive-backend (command &optional arg &rest ignored) (message “company-predictive-backend %s” command) (case command (prefix (let* ((text (downcase(word-at-point)))) (set-text-properties 0 (length text) nil text ) (message “prefix:%s” text) text ) ) (candidates (predictive-complete arg)) (meta (format “This value is named %s” arg))))

;;eval the below statement in a text buffer to try ;;(set (make-local-variable ‘company-backends) ‘(company-predictive-backend))


How about adding an option for substring match completions? E.g. 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.

The option which would control it could be set to prefix match by default (keeping the current behavior) and the user could set it to substring match too.

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

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. But doing that automatically might work well, too. I’ll look into it.

Automatically is the key. Pressing two keys (or even one) to achieve the same effect is not the same. There could also be a switch key, though, in addition to the option.

E.g. I set the default match to substring, so that it works like iswitchb, because I prefer it that way. But sometimes when I’m doing some completion and typed some characters it turns out it would be quicker to get to the desired match with prefix match, so I press the switch key which temporarily (for the current completion) toggles the match mode and updates the completion list instantly accordingly.

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). Picking up already typed text as the search string is more complicated, since it’s difficult to visualize. But I think this should feel a lot more like iswitchb. – nschum

Why is it difficult to visualize? With prefix match you color the matching prefix part, with substring match you color the first matching substring part. Lots of completion packages here on Emacs wiki do that.

Because of the way it was build. The idea is that the interface is standardized, so every front-end has to support this. The “preview” visualization is the one that makes it difficult, because the preview would have to be inserted before point, causing it to jitter as you type. I’ll have to see how well it works.

You could take a look at how icomplete handles this problem (when non-prefix completion is used, it just puts the whole completion after the text rather than only the missing suffix).

Why don’t you add an other option to suppress preview visualization? I don’t think preview is that important anyway, when you see the possible completions in the popup list, so the user could just suppress it altogether with a config option if it bothers him. You could mention the jittering effect at the explanation of the substring match option and point the user to the variable which can be used to suppress it.

Again, because of the way company is build that would mean throwing the standardized visualization interface overboard, or significantly amending it. It’s just not trivially done. I have some ideas for this, but it’ll have to wait.

Would it be possible to make the down-case feature of dabbrev backend customizable as it is counter intuitive for example in shell-mode where it completes in lowercase all environment variables ?


CategoryCompletion