2

In Emacs, I have installed the MELPA company-irony-c-header package. I then did some research on the web, and apparently in order to configure the package, what I took to mean "activating" it, I had to add 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))))

into something called a "back-end" (with foo standing for whatever my filename was). What is a back-end, and how can I to use it?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ezra Erives
  • 77
  • 1
  • 6

1 Answers1

2

1. From the official documentation

Company is a text completion framework for Emacs. The name stands for "complete anything". It uses pluggable back-ends and front-ends to retrieve and display completion candidates.

That means that "back-ends" are the sources of information (parser, database...) company uses to suggest completion.

As an example, for C/C++ work I use the excellent RTags. Adding this back-end to company is done thanks:

(push 'company-rtags company-backends)

2. Now for your problem

A minimal company-c-headers working example is

(package-initialize)

(require 'company)
(require 'company-c-headers)

(add-to-list 'company-backends 'company-c-headers)

;; system dirs (for include <...>)
(add-to-list 'company-c-headers-path-system "/usr/include/c++/6")
;; (add-to-list 'company-c-headers-path-system "ANOTHER_SYSTEM_DIR")
;; -> use "gcc -E -Wp,-v -" to get the complete list

;; You can also define (for include "...")
;;(add-to-list 'company-c-headers-path-user "/home/YOUR_PROJECT")

(add-hook 'c++-mode-hook 'company-mode)
(add-hook 'c-mode-hook 'company-mode)

Save this file testing_init.el and start Emacs with

emacs -q --load "testing_init.el" your_prog.cpp

Now if you type

 #include<...

in your_prog.cpp file, company should trigger an auto completion after the third typed character.

There are plenty of good tutorials to configure Emacs as a C++ editor. I think this one is a good starting point.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Picaud Vincent
  • 10,518
  • 5
  • 31
  • 70