问题
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). I have absolutely no idea what a back-end is, or how to use it. Any help would be greatly appreciated.
回答1:
A/ From the official doc,
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)
B/ 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 char.
There are plenty of good tutorials to configure Emacs as a C++ editor. If think this one is a good starting point.
来源:https://stackoverflow.com/questions/44959535/company-backends-in-gnu-emacs