blob: 39aacd0665337b0dc2eb7ef12273b346398fd0ab [file] [log] [blame] [view]
andybons3322f762015-08-24 21:37:091## Debugging
2
3LinuxDebugging has some emacs-specific debugging tips.
4
5
6## Blink Style (WebKit)
7
8Chrome and Blink/WebKit style differ. You can use [directory-local variables](http://www.gnu.org/software/emacs/manual/html_node/emacs/Directory-Variables.html) to make the tab key do the right thing. E.g., in `third_party/WebKit`, add a `.dir-locals.el` that contains
9
10```
11((nil . ((indent-tabs-mode . nil)
12 (c-basic-offset . 4)
13 (fill-column . 120))))
14```
15
16This turns off tabs, sets your indent to four spaces, and makes `M-q` wrap at 120 columns (WebKit doesn't define a wrap column, but there's a soft limit somewhere in that area for comments. Some reviewers do enforce the no wrap limit, which Emacs can deal with gracefully; see below.)
17
18Be sure to `echo .dir-locals.el >> .git/info/exclude` so `git clean` doesn't delete your file.
19
20It can be useful to set up a WebKit specific indent style. It's not too much different so it's easy to base off of the core Google style. Somewhere after you've loaded google.el (most likely in your .emacs file), add:
21
22```
23(c-add-style "WebKit" '("Google"
24 (c-basic-offset . 4)
25 (c-offsets-alist . ((innamespace . 0)
26 (access-label . -)
27 (case-label . 0)
28 (member-init-intro . +)
29 (topmost-intro . 0)
30 (arglist-cont-nonempty . +)))))
31```
32
33then you can add
34
35```
36 (c-mode . ((c-file-style . "WebKit")))
37 (c++-mode . ((c-file-style . "WebKit"))))
38```
39
40to the end of the .dir-locals.el file you created above. Note that this style may not yet be complete, but it covers the most common differences.
41
42Now that you have a WebKit specific style being applied, and assuming you have font locking and it's default jit locking turned on, you can also get Emacs 23 to wrap long lines more intelligently by adding the following to your .emacs file:
43
44```
45;; For dealing with WebKit long lines and word wrapping.
46(defun c-mode-adaptive-indent (beg end)
47 "Set the wrap-prefix for the the region between BEG and END with adaptive filling."
48 (goto-char beg)
49 (while
50 (let ((lbp (line-beginning-position))
51 (lep (line-end-position)))
52 (put-text-property lbp lep 'wrap-prefix (concat (fill-context-prefix lbp lep) (make-string c-basic-offset ? )))
53 (search-forward "\n" end t))))
54
55(define-minor-mode c-adaptive-wrap-mode
56 "Wrap the buffer text with adaptive filling for c-mode."
57 :lighter ""
58 (save-excursion
59 (save-restriction
60 (widen)
61 (let ((buffer-undo-list t)
62 (inhibit-read-only t)
63 (mod (buffer-modified-p)))
64 (if c-adaptive-wrap-mode
65 (jit-lock-register 'c-mode-adaptive-indent)
66 (jit-lock-unregister 'c-mode-adaptive-indent)
67 (remove-text-properties (point-min) (point-max) '(wrap-prefix pref)))
68 (restore-buffer-modified-p mod)))))
69
70(defun c-adaptive-wrap-mode-for-webkit ()
71 "Turn on visual line mode and adaptive wrapping for WebKit source files."
72 (if (or (string-equal "webkit" c-indentation-style)
73 (string-equal "WebKit" c-indentation-style))
74 (progn
75 (visual-line-mode t)
76 (c-adaptive-wrap-mode t))))
77
78(add-hook 'c-mode-common-hook 'c-adaptive-wrap-mode-for-webkit)
79(add-hook 'hack-local-variables-hook 'c-adaptive-wrap-mode-for-webkit)
80```
81
82This turns on visual wrap mode for files using the WebKit c style, and sets up a hook to dynamically set the indent on the wrapped lines. It's not quite as intelligent as it could be (e.g., what would the wrap be if there really were a newline there?), but it's very fast. It makes dealing with long code lines anywhere much more tolerable (not just in WebKit).
83
84## Syntax-error Highlighting
85NinjaBuild users get in-line highlighting of syntax errors using `flymake.el` on each buffer-save:
86```
87(load-file "src/tools/emacs/flymake-chromium.el")
88```
89
90## [ycmd](https://github.com/Valloric/ycmd) (YouCompleteMe) + flycheck
91
92[emacs-ycmd](https://github.com/abingham/emacs-ycmd) in combination with flycheck provides:
93 * advanced code completion
94 * syntax checking
95 * navigation to declarations and definitions (using `ycmd-goto`)
96based on on-the fly processing using clang. A quick demo video showing code completion and flycheck highlighting a missing semicolon syntax error:
97
98<a href='http://www.youtube.com/watch?feature=player_embedded&v=a0zMbm4jACk' target='_blank'><img src='http://img.youtube.com/vi/a0zMbm4jACk/0.jpg' width='696' height=250 /></a>
99
100#### Requirements:
101 * Your build system is set up for building with clang or wrapper+clang
102
103#### Setup
104
105 1. Clone, update external git repositories and build.sh ycmd from https://github.com/Valloric/ycmd into a directory, e.g. `~/dev/ycmd`
106 1. Test `ycmd` by running
107> `~/dev/ycmd$ python ycmd/__main__.py`
108
109> You should see `KeyError: 'hmac_secret'`
110 1. Install the following packages to emacs, for example from melpa:
111 * `ycmd`
112 * `company-ycmd`
113 * `flycheck-ycmd`
114> [More info on configuring emacs-ycmd](https://github.com/abingham/emacs-ycmd#quickstart)
115 1. Assuming your checkout of Chromium is in `~/dev/blink`, i.e. this is the directory in which you find the `src`folder, create a symbolic link as follows
116> `cd ~/dev/blink; ln -s src/tools/vim/chromium.ycm_extra_conf.py .ycm_extra_conf.py`
117 1. Add something like the following to your `init.el`
118```
119;; ycmd
120
121;;; Googlers can replace a lot of this with (require 'google-ycmd).
122
123(require 'ycmd)
124(require 'company-ycmd)
125(require 'flycheck-ycmd)
126
127(company-ycmd-setup)
128(flycheck-ycmd-setup)
129
130;; Show completions after 0.15 seconds
131(setq company-idle-delay 0.15)
132
133;; Activate for editing C++ files
134(add-hook 'c++-mode-hook 'ycmd-mode)
135(add-hook 'c++-mode-hook 'company-mode)
136(add-hook 'c++-mode-hook 'flycheck-mode)
137
138;; Replace the directory information with where you downloaded ycmd to
139(set-variable 'ycmd-server-command (list "python" (substitute-in-file-name "$HOME/dev/ycmd/ycmd/__main__.py")))
140
141;; Edit according to where you have your Chromium/Blink checkout
142(add-to-list 'ycmd-extra-conf-whitelist (substitute-in-file-name "$HOME/dev/blink/.ycm_extra_conf.py"))
143
144;; Show flycheck errors in idle-mode as well
145(setq ycmd-parse-conditions '(save new-line mode-enabled idle-change))
146
147;; Makes emacs-ycmd less verbose
148(setq url-show-status nil)
149```
150
151#### Troubleshooting
152
153 * If no completions show up or emacs reports errors, you can check the `*ycmd-server*` buffer for errors. See the next bullet point for how to handle "OS Error: No such file or directory"
154 * Launching emacs from an OS menu might result in a different environment so that `ycmd` does not find ninja. In that case, you can use a package like [exec-path from shell](https://github.com/purcell/exec-path-from-shell) and add the following to your `init.el`:
155```
156(require 'exec-path-from-shell)
157(when (memq window-system '(mac ns x))
158 (exec-path-from-shell-initialize))
159```
160
161
162## ff-get-other-file
163
164There's a builtin function called `ff-get-other-file` which will get the "other file" based on file extension. I have this bound to C-o in c-mode (`(local-set-key "\C-o" 'ff-get-other-file)`). While "other file" is per-mode defined, in c-like languages it means jumping between the header and the source file. So I switch back and forth between the header and the source with C-o. If we had separate include/ and src/ directories, this would be a pain to setup, but this might just work out of the box for you. See the documentation for the variable `cc-other-file-alist` for more information.
165
166One drawback of ff-get-other-file is that it will always switch to a matching buffer, even if the other file is in a different directory, so if you have A.cc,A.h,A.cc(2) then ff-get-other-file will switch to A.h from A.cc(2) rather than load A.h(2) from the appropriate directory. If you prefer something (C specific) that always finds, try this:
167```
168(defun cc-other-file()
169 "Toggles source/header file"
170 (interactive)
171 (let ((buf (current-buffer))
172 (name (file-name-sans-extension (buffer-file-name)))
173 (other-extens
174 (cadr (assoc (concat "\\."
175 (file-name-extension (buffer-file-name))
176 "\\'")
177 cc-other-file-alist))))
178 (dolist (e other-extens)
179 (if (let ((f (concat name e)))
180 (and (file-exists-p f) (find-file f)))
181 (return)))
182 )
183 )
184```
185_Note: if you know an easy way to change the ff-get-other-file behavior, please replace this hack with that solution! - [email protected]_
186
187## Use Google's C++ style!
188
189We have an emacs module, [google-c-style.el](http://google-styleguide.googlecode.com/svn/trunk/google-c-style.el), which adds c-mode formatting.
190Then add to your .emacs:
191```
192 (load "/<path/to/chromium>/src/buildtools/clang_format/script/clang-format.el")
193 (add-hook 'c-mode-common-hook (function (lambda () (local-set-key (kbd "TAB") 'clang-format-region))))
194```
195Now, you can use the
196
197&lt;Tab&gt;
198
199 key to format the current line (even a long line) or region.
200
201### Highlight long lines
202
203One nice way to highlight long lines and other style issues:
204```
205(require 'whitespace)
206(setq whitespace-style '(face indentation trailing empty lines-tail))
207(setq whitespace-line-column nil)
208(set-face-attribute 'whitespace-line nil
209 :background "purple"
210 :foreground "white"
211 :weight 'bold)
212(global-whitespace-mode 1)
213```
214
215
216Note: You might need to grab the latest version of [whitespace.el](http://www.emacswiki.org/emacs-en/download/whitespace.el).
217
218## gyp
219
220### `gyp` style
221There is a gyp mode that provides basic indentation and font-lock (syntax highlighting) support. The mode derives from python.el (bundled with newer emacsen).
222
223You can find it in tools/gyp/tools/emacs or at http://code.google.com/p/gyp/source/browse/trunk/tools/emacs/
224
225See the README file there for installation instructions.
226
227**Important**: the mode is only tested with `python.el` (bundled with newer emacsen), not with `python-mode.el` (outdated and less maintained these days).
228
229### deep nesting
230
231A couple of helpers that show a summary of where you are; the first by tracing the indentation hierarchy upwards, the second by only showing `#if`s and `#else`s that are relevant to the current line:
232
233```el
234
235(defun ami-summarize-indentation-at-point ()
236"Echo a summary of how one gets from the left-most column to
237POINT in terms of indentation changes."
238(interactive)
239(save-excursion
240(let ((cur-indent most-positive-fixnum)
241(trace '()))
242(while (not (bobp))
243(let ((current-line (buffer-substring (line-beginning-position)
244(line-end-position))))
245(when (and (not (string-match "^\\s-*$" current-line))
246(< (current-indentation) cur-indent))
247(setq cur-indent (current-indentation))
248(setq trace (cons current-line trace))
249(if (or (string-match "^\\s-*}" current-line)
250(string-match "^\\s-*else " current-line)
251(string-match "^\\s-*elif " current-line))
252(setq cur-indent (1+ cur-indent)))))
253(forward-line -1))
254(message "%s" (mapconcat 'identity trace "\n")))))
255
256(require 'cl)
257(defun ami-summarize-preprocessor-branches-at-point ()
258"Summarize the C preprocessor branches needed to get to point."
259(interactive)
260(flet ((current-line-text ()
261(buffer-substring (line-beginning-position) (line-end-position))))
262(save-excursion
263(let ((eol (or (end-of-line) (point)))
264deactivate-mark directives-stack)
265(goto-char (point-min))
266(while (re-search-forward "^#\\(if\\|else\\|endif\\)" eol t)
267(if (or (string-prefix-p "#if" (match-string 0))
268(string-prefix-p "#else" (match-string 0)))
269(push (current-line-text) directives-stack)
270(if (string-prefix-p "#endif" (match-string 0))
271(while (string-prefix-p "#else" (pop directives-stack)) t))))
272(message "%s" (mapconcat 'identity (reverse directives-stack) "\n"))))))
273```
274
275## find-things-fast
276
277erg wrote a suite of tools that do common operations from the root of your repository, called [Find Things Fast](https://github.com/eglaysher/find-things-fast). It contains ido completion over `git ls-files` (or the svn find equivalent) and `grepsource` that only git greps files with extensions we care about (or the equivalent the `find | xargs grep` statement in non-git repos.)
278
279## vc-mode and find-file performance
280
281When you first open a file under git control, vc mode kicks in and does a high level stat of your git repo. For huge repos, especially WebKit and Chromium, this makes opening a file take literally seconds. This snippet disables VC git for chrome directories:
282
283```
284
285; Turn off VC git for chrome
286(when (locate-library "vc")
287(defadvice vc-registered (around nochrome-vc-registered (file))
288(message (format "nochrome-vc-registered %s" file))
289(if (string-match ".*chrome/src.*" file)
290(progn
291(message (format "Skipping VC mode for %s" % file))
292(setq ad-return-value nil)
293)
294ad-do-it)
295)
296(ad-activate 'vc-registered)
297)
298```
299
300## git tools
301We're collecting Chrome-specific tools under `tools/emacs`. See the files there for details.
302
303 * `trybot.el`: import Windows trybot output into a `compilation-mode` buffer.
304
305## ERC for IRC
306
307See ErcIrc.
308
309## TODO
310
311 * Figure out how to make `M-x compile` default to `cd /path/to/chrome/root; make -r chrome`.