User:VladVukicevic/EmacsClangComplete

From MozillaWiki
Jump to: navigation, search

Clang is neat. clang-complete is neater, and more importantly, seems to work fine with Gecko with a bit of setup. Here's what I did under linux with emacs.

z7S9W.png

#!/bin/sh

if [ -z "$1" ] ; then
  TARGET=firefox
else
  TARGET=$1
fi

SRCDIR="`pwd`"
OBJDIR=`echo $SRCDIR | sed s,mozilla-central,$TARGET,`

make -C $OBJDIR showbuild 2>&1 | awk '
   BEGIN { ORS=" "; }
   /^COMPILE_CXXFLAGS / {
      gsub(/-I\.\./,"-I'$OBJDIR'/..");
      gsub(/ \.\./," '$OBJDIR'/..");
      for (f=1;f<=NF;f++) {
        if ($f ~ /^-[ID]/) print $f;
	if ($f ~ /^-include/) { print $f; print $(f+1); }
	#if ($f ~ /^-isystem/) { print $f; print $(f+1); }
      }
   }
'
  • In your .emacs:
;; yasnippet
(require 'yasnippet)
(yas/global-mode 1)

;; clang autocomplete
(require 'auto-complete-config)
(require 'auto-complete-clang)

(setq moz-ac-objdir "fx-android-debug")

(defun moz-ac-set (objdir-name)
  (interactive "sObjdir name to switch completion to: ")
  (if (not (eq moz-ac-objdir objdir-name))
      (setq moz-ac-objdir objdir-name)
      (ac-clang-set-cflags-from-shell-command (concat "~/bin/clang-moz-flags " objdir-name)))
)

(setq ac-auto-start nil)
(setq ac-quick-help-delay 0.5)
;(ac-set-trigger-key "TAB")
(define-key ac-mode-map  [(control tab)] 'auto-complete)
(defun my-ac-config ()
  (setq-default ac-sources '(ac-source-abbrev ac-source-dictionary ac-source-words-in-same-mode-buffers))
  (add-hook 'emacs-lisp-mode-hook 'ac-emacs-lisp-mode-setup)
  (add-hook 'c-mode-common-hook 'ac-cc-mode-setup)
  (add-hook 'css-mode-hook 'ac-css-mode-setup)
  (add-hook 'auto-complete-mode-hook 'ac-common-setup)
  (global-auto-complete-mode t))
(defun my-ac-cc-mode-setup ()
  (setq ac-sources (append '(ac-source-clang ac-source-yasnippet) ac-sources)))
(add-hook 'c-mode-common-hook 'my-ac-cc-mode-setup)
;; ac-source-gtags
(my-ac-config)
  • Then, open a source file and do M-x moz-ac-set and give it an objdir name. The above stuff assumes that your objdirs are alongside your mozilla-central dir (not inside it, because you shouldn't do that, that's gross, stop being gross); if you're doing something else, you'll need to tweak the above. Same with mozilla-central not being your source tree name.
  • Hit control-tab to autocomplete!
  • If something goes wrong, look at the *clang-output* buffer.