blob: 1dc555a94bdbaee94195ba53d95fb351383ad3ad [file] [log] [blame] [view]
andybons22afb312015-08-31 02:24:511# Emacs
2
3[TOC]
4
andybons3322f762015-08-24 21:37:095## Debugging
6
Alan Bram06b277682017-10-04 19:32:327[Linux Debugging](linux_debugging.md) has some Emacs-specific debugging tips.
andybons3322f762015-08-24 21:37:098
andybons3322f762015-08-24 21:37:099## Syntax-error Highlighting
andybons22afb312015-08-31 02:24:5110
11[Ninja](ninja_build.md) users get in-line highlighting of syntax errors using
12`flymake.el` on each buffer-save:
13
14
15 (load-file "src/tools/emacs/flymake-chromium.el")
16
andybons3322f762015-08-24 21:37:0917
18## [ycmd](https://github.com/Valloric/ycmd) (YouCompleteMe) + flycheck
19
andybons22afb312015-08-31 02:24:5120[emacs-ycmd](https://github.com/abingham/emacs-ycmd) in combination with
21flycheck provides:
andybons3322f762015-08-24 21:37:0922
andybons22afb312015-08-31 02:24:5123* advanced code completion
24* syntax checking
25* navigation to declarations and definitions (using `ycmd-goto`) based on
26 on-the-fly processing using clang. A quick demo video showing code
27 completion and flycheck highlighting a missing semicolon syntax error:
andybons3322f762015-08-24 21:37:0928
andybons22afb312015-08-31 02:24:5129[![video preview][img]][video]
30
31[img]: http://img.youtube.com/vi/a0zMbm4jACk/0.jpg
32[video]: http://www.youtube.com/watch?feature=player_embedded&v=a0zMbm4jACk
33
34### Requirements
35
andybons3322f762015-08-24 21:37:0936 * Your build system is set up for building with clang or wrapper+clang
37
andybons22afb312015-08-31 02:24:5138### Setup
andybons3322f762015-08-24 21:37:0939
andybons22afb312015-08-31 02:24:51401. Clone, update external git repositories and build.sh ycmd from
41 https://github.com/Valloric/ycmd into a directory, e.g. `~/dev/ycmd`
421. Test `ycmd` by running `~/dev/ycmd$ python ycmd/__main__.py` You should see
43 `KeyError: 'hmac_secret'`
441. Install the following packages to emacs, for example from melpa:
45 * `ycmd`
46 * `company-ycmd`
47 * `flycheck-ycmd`
481. [More info on configuring emacs-ycmd](https://github.com/abingham/emacs-ycmd#quickstart)
49 1. Assuming your checkout of Chromium is in `~/dev/blink`, i.e. this is the
50 directory in which you find the `src`folder, create a symbolic link as
51 follows:
andybons3322f762015-08-24 21:37:0952
andybons22afb312015-08-31 02:24:5153 ```shell
54 cd ~/dev/blink
55 ln -s src/tools/vim/chromium.ycm_extra_conf.py .ycm_extra_conf.py
56 ```
57
58 1. Add something like the following to your `init.el`
59
60```el
andybons3322f762015-08-24 21:37:0961;; ycmd
62
63;;; Googlers can replace a lot of this with (require 'google-ycmd).
64
65(require 'ycmd)
66(require 'company-ycmd)
67(require 'flycheck-ycmd)
68
69(company-ycmd-setup)
70(flycheck-ycmd-setup)
71
72;; Show completions after 0.15 seconds
73(setq company-idle-delay 0.15)
74
75;; Activate for editing C++ files
76(add-hook 'c++-mode-hook 'ycmd-mode)
77(add-hook 'c++-mode-hook 'company-mode)
78(add-hook 'c++-mode-hook 'flycheck-mode)
79
80;; Replace the directory information with where you downloaded ycmd to
81(set-variable 'ycmd-server-command (list "python" (substitute-in-file-name "$HOME/dev/ycmd/ycmd/__main__.py")))
82
83;; Edit according to where you have your Chromium/Blink checkout
84(add-to-list 'ycmd-extra-conf-whitelist (substitute-in-file-name "$HOME/dev/blink/.ycm_extra_conf.py"))
85
86;; Show flycheck errors in idle-mode as well
87(setq ycmd-parse-conditions '(save new-line mode-enabled idle-change))
88
89;; Makes emacs-ycmd less verbose
90(setq url-show-status nil)
91```
92
andybons22afb312015-08-31 02:24:5193### Troubleshooting
andybons3322f762015-08-24 21:37:0994
andybons22afb312015-08-31 02:24:5195* If no completions show up or emacs reports errors, you can check the
96 `*ycmd-server*` buffer for errors. See the next bullet point for how to
97 handle "OS Error: No such file or directory"
98* Launching emacs from an OS menu might result in a different environment so
99 that `ycmd` does not find ninja. In that case, you can use a package like
100 [exec-path from shell](https://github.com/purcell/exec-path-from-shell) and
101 add the following to your `init.el`:
102
103```el
andybons3322f762015-08-24 21:37:09104(require 'exec-path-from-shell)
105(when (memq window-system '(mac ns x))
106 (exec-path-from-shell-initialize))
107```
108
andybons3322f762015-08-24 21:37:09109## ff-get-other-file
110
andybons22afb312015-08-31 02:24:51111There's a builtin function called `ff-get-other-file` which will get the "other
112file" based on file extension. I have this bound to C-o in c-mode
113(`(local-set-key "\C-o" 'ff-get-other-file)`). While "other file" is per-mode
114defined, in c-like languages it means jumping between the header and the source
115file. So I switch back and forth between the header and the source with C-o. If
116we had separate include/ and src/ directories, this would be a pain to setup,
117but this might just work out of the box for you. See the documentation for the
118variable `cc-other-file-alist` for more information.
andybons3322f762015-08-24 21:37:09119
andybons22afb312015-08-31 02:24:51120One drawback of ff-get-other-file is that it will always switch to a matching
121buffer, even if the other file is in a different directory, so if you have
122A.cc,A.h,A.cc(2) then ff-get-other-file will switch to A.h from A.cc(2) rather
123than load A.h(2) from the appropriate directory. If you prefer something (C
124specific) that always finds, try this:
125
126```el
andybons3322f762015-08-24 21:37:09127(defun cc-other-file()
128 "Toggles source/header file"
129 (interactive)
130 (let ((buf (current-buffer))
131 (name (file-name-sans-extension (buffer-file-name)))
andybons22afb312015-08-31 02:24:51132 (other-extens
133 (cadr (assoc (concat "\\."
andybons3322f762015-08-24 21:37:09134 (file-name-extension (buffer-file-name))
andybons22afb312015-08-31 02:24:51135 "\\'")
andybons3322f762015-08-24 21:37:09136 cc-other-file-alist))))
137 (dolist (e other-extens)
138 (if (let ((f (concat name e)))
139 (and (file-exists-p f) (find-file f)))
140 (return)))
141 )
142 )
143```
andybons22afb312015-08-31 02:24:51144
145_Note: if you know an easy way to change the ff-get-other-file behavior, please
146replace this hack with that solution! - [email protected]_
andybons3322f762015-08-24 21:37:09147
148## Use Google's C++ style!
149
andybons22afb312015-08-31 02:24:51150We have an emacs module,
151[google-c-style.el](http://google-styleguide.googlecode.com/svn/trunk/google-c-style.el),
152which adds c-mode formatting. Then add to your .emacs:
153
154```el
155(load "/<path/to/chromium>/src/buildtools/clang_format/script/clang-format.el")
156(add-hook 'c-mode-common-hook
157 (function (lambda () (local-set-key (kbd "TAB") 'clang-format-region))))
andybons3322f762015-08-24 21:37:09158```
andybons22afb312015-08-31 02:24:51159
160Now, you can use the
andybons3322f762015-08-24 21:37:09161
162&lt;Tab&gt;
163
andybons22afb312015-08-31 02:24:51164key to format the current line (even a long line) or region.
andybons3322f762015-08-24 21:37:09165
andybons22afb312015-08-31 02:24:51166## Highlight long lines
andybons3322f762015-08-24 21:37:09167
168One nice way to highlight long lines and other style issues:
andybons22afb312015-08-31 02:24:51169
170```el
andybons3322f762015-08-24 21:37:09171(require 'whitespace)
172(setq whitespace-style '(face indentation trailing empty lines-tail))
173(setq whitespace-line-column nil)
174(set-face-attribute 'whitespace-line nil
175 :background "purple"
176 :foreground "white"
177 :weight 'bold)
178(global-whitespace-mode 1)
179```
180
andybons22afb312015-08-31 02:24:51181Note: You might need to grab the latest version of
182[whitespace.el](http://www.emacswiki.org/emacs-en/download/whitespace.el).
andybons3322f762015-08-24 21:37:09183
184## gyp
185
186### `gyp` style
andybons22afb312015-08-31 02:24:51187There is a gyp mode that provides basic indentation and font-lock (syntax
188highlighting) support. The mode derives from python.el (bundled with newer
189emacsen).
andybons3322f762015-08-24 21:37:09190
andybons22afb312015-08-31 02:24:51191You can find it in /src/tools/gyp/tools/emacs
andybons3322f762015-08-24 21:37:09192
193See the README file there for installation instructions.
194
andybons22afb312015-08-31 02:24:51195**Important**: the mode is only tested with `python.el` (bundled with newer
196emacsen), not with `python-mode.el` (outdated and less maintained these days).
andybons3322f762015-08-24 21:37:09197
198### deep nesting
199
andybons22afb312015-08-31 02:24:51200A couple of helpers that show a summary of where you are; the first by tracing
201the indentation hierarchy upwards, the second by only showing `#if`s and
202`#else`s that are relevant to the current line:
andybons3322f762015-08-24 21:37:09203
204```el
andybons3322f762015-08-24 21:37:09205(defun ami-summarize-indentation-at-point ()
andybons22afb312015-08-31 02:24:51206 "Echo a summary of how one gets from the left-most column to
207 POINT in terms of indentation changes."
208 (interactive)
209 (save-excursion
210 (let ((cur-indent most-positive-fixnum)
211 (trace '()))
212 (while (not (bobp))
213 (let ((current-line (buffer-substring (line-beginning-position)
214 (line-end-position))))
215 (when (and (not (string-match "^\\s-*$" current-line))
216 (< (current-indentation) cur-indent))
217 (setq cur-indent (current-indentation))
218 (setq trace (cons current-line trace))
219 (if (or (string-match "^\\s-*}" current-line)
220 (string-match "^\\s-*else " current-line)
221 (string-match "^\\s-*elif " current-line))
222 (setq cur-indent (1+ cur-indent)))))
223 (forward-line -1))
224 (message "%s" (mapconcat 'identity trace "\n")))))
andybons3322f762015-08-24 21:37:09225
226(require 'cl)
andybons22afb312015-08-31 02:24:51227 (defun ami-summarize-preprocessor-branches-at-point ()
228 "Summarize the C preprocessor branches needed to get to point."
229 (interactive)
230 (flet ((current-line-text ()
231 (buffer-substring (line-beginning-position) (line-end-position))))
232 (save-excursion
233 (let ((eol (or (end-of-line) (point)))
234 deactivate-mark directives-stack)
235 (goto-char (point-min))
236 (while (re-search-forward "^#\\(if\\|else\\|endif\\)" eol t)
237 (if (or (string-prefix-p "#if" (match-string 0))
238 (string-prefix-p "#else" (match-string 0)))
239 (push (current-line-text) directives-stack)
240 (if (string-prefix-p "#endif" (match-string 0))
241 (while (string-prefix-p "#else" (pop directives-stack)) t))))
242 (message "%s" (mapconcat 'identity (reverse directives-stack) "\n"))))))
andybons3322f762015-08-24 21:37:09243```
244
245## find-things-fast
246
andybons22afb312015-08-31 02:24:51247erg wrote a suite of tools that do common operations from the root of your
248repository, called
249[Find Things Fast](https://github.com/eglaysher/find-things-fast). It contains
250ido completion over `git ls-files` (or the svn find equivalent) and `grepsource`
251that only git greps files with extensions we care about (or the equivalent the
252`find | xargs grep` statement in non-git repos.)
andybons3322f762015-08-24 21:37:09253
254## vc-mode and find-file performance
255
andybons22afb312015-08-31 02:24:51256When you first open a file under git control, vc mode kicks in and does a high
257level stat of your git repo. For huge repos, especially WebKit and Chromium,
258this makes opening a file take literally seconds. This snippet disables VC git
259for chrome directories:
andybons3322f762015-08-24 21:37:09260
andybons22afb312015-08-31 02:24:51261```el
andybons3322f762015-08-24 21:37:09262; Turn off VC git for chrome
263(when (locate-library "vc")
264(defadvice vc-registered (around nochrome-vc-registered (file))
265(message (format "nochrome-vc-registered %s" file))
266(if (string-match ".*chrome/src.*" file)
267(progn
268(message (format "Skipping VC mode for %s" % file))
269(setq ad-return-value nil)
270)
271ad-do-it)
272)
273(ad-activate 'vc-registered)
274)
275```
276
277## git tools
andybons3322f762015-08-24 21:37:09278
andybons22afb312015-08-31 02:24:51279We're collecting Chrome-specific tools under `tools/emacs`. See the files there
280for details.
281
282* `trybot.el`: import Windows trybot output into a `compilation-mode` buffer.
andybons3322f762015-08-24 21:37:09283
284## ERC for IRC
285
asargent4adb8112015-12-07 19:42:02286See [ErcIrc](erc_irc.md).
andybons3322f762015-08-24 21:37:09287
288## TODO
289
andybons22afb312015-08-31 02:24:51290* Figure out how to make `M-x compile` default to
291 `cd /path/to/chrome/root; make -r chrome`.