Introduction
emacs-clang-complete-async is an emacs extension to complete C and C++ code, it uses libclang to parse the source code on the fly and provides completion candidates to auto-complete (http://cx4a.org/software/auto-complete).
This extension is not implemented in pure elisp, it is made up of a client part (auto-complete-clang-async.el, written in elisp) and a server part (clang-complete binary, written in C), they work cooperately in asynchonous client-server fashion.
Experimental Feature - On the fly syntax checking
An experimental feature is added to this branch — running
ac-clang-syntax-check
to highlight errornous lines in your souce code.
Setup
Compile the server part (clang-complete binary) first, by executing
make
. The build process uses llvm-config
to determine the location of
libclang and the appropriate compile flags to use. If llvm-config
is not in
your path or has a different name, you can pass make
an LLVM_CONFIG
argument, e.g. make LLVM_CONFIG=llvm-config-3.4
.
Copy auto-complete-clang-async.el and the previously compiled clang-complete executable to ~/.emacs.d/, and add the following code to your .emacs file.
(require 'auto-complete-clang-async)
(defun ac-cc-mode-setup ()
(setq ac-clang-complete-executable "~/.emacs.d/clang-complete")
(setq ac-sources '(ac-source-clang-async))
(ac-clang-launch-completion-process)
)
(defun my-ac-config ()
(add-hook 'c-mode-common-hook 'ac-cc-mode-setup)
(add-hook 'auto-complete-mode-hook 'ac-common-setup)
(global-auto-complete-mode t))
(my-ac-config)
Now emacs-clang-complete-async will show completion candidates automatically when you type as usual in C or C++ mode.
Usage
This extension fades in emacs C/C++ mode and provides candidates automatically while you typing code, if you want to add parameters to clang (libclang, actually), such as -Ipath, just call ac-clang-set-cflags interactively or set the value of ac-clang-flags in .emacs or .dir-locals.el, maybe you need an explicit call to ac-clang-update-cmdlineargs to make changes to cflags take effect, which is a niggling part of it T T
Note
Most code of auto-complete-clang-async.el is taken from brainjcj’s auto-complete-clang.el, The only difference between this one and bj’s ac-clang-complete is: This one interacts with a process geared with libclang to retrieve completion candidates instead of calling clang process, which is way slower, and the asynchonous nature of our C-S based working model won’t block the editor while parsing source code and resolving completion candidates, which provides a “smooth” coding experience.