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.
An experimental feature is added to this branch — running
ac-clang-syntax-check
to highlight errornous lines in your souce code.
You can now jump to either the declaration or the definition of the element your cursor is on.
To jump to the definition, use ac-clang-jump-definition
.
To jump to the declaration, use ac-clang-jump-declaration
.
To jump to the definition, if it’s available, otherwise to the declaration, use ac-clang-jump-smart
.
To pop the jump stack and go back to a previous location, use ac-clang-jump-back
.
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)
;; Optional keybindings
(define-key ac-mode-map (kbd "M-.") 'ac-clang-jump-smart)
(define-key ac-mode-map (kbd "M-,") 'ac-clang-jump-back)
(define-key ac-mode-map (kbd "C-c `") 'ac-clang-syntax-check))
(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.
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
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.