blob: 9bb12c61aae404d2b3364e4eaa1412972e0f6671 [file] [log] [blame]
mmentovaidb0be8e2008-12-17 22:24:29 +00001;;; google-c-style.el --- Google's C/C++ style for c-mode
2
3;; Keywords: c, tools
4
5;; google-c-style.el is Copyright (C) 2008 Google Inc. All Rights Reserved.
6;;
7;; It is free software; you can redistribute it and/or modify it under the
8;; terms of either:
9;;
10;; a) the GNU General Public License as published by the Free Software
11;; Foundation; either version 1, or (at your option) any later version, or
12;;
13;; b) the "Artistic License".
14
15;;; Commentary:
16
17;; Provides the google C/C++ coding style. You may wish to add
18;; `google-set-c-style' to your `c-mode-common-hook' after requiring this
19;; file. For example:
20;;
21;; (add-hook 'c-mode-common-hook 'google-set-c-style)
22;;
23;; If you want the RETURN key to go to the next line and space over
24;; to the right place, add this to your .emacs right after the load-file:
25;;
26;; (add-hook 'c-mode-common-hook 'google-make-newline-indent)
27
28;;; Code:
29
erg@google.com2df72b32009-10-09 21:51:56 +000030;; For some reason 1) c-backward-syntactic-ws is a macro and 2) under Emacs 22
31;; bytecode cannot call (unexpanded) macros at run time:
32(eval-when-compile (require 'cc-defs))
erg@google.com21a49582009-08-03 21:00:44 +000033
34;; Wrapper function needed for Emacs 21 and XEmacs (Emacs 22 offers the more
35;; elegant solution of composing a list of lineup functions or quantities with
36;; operators such as "add")
erg@google.com2df72b32009-10-09 21:51:56 +000037(defun google-c-lineup-expression-plus-4 (langelem)
38 "Indents to the beginning of the current C expression plus 4 spaces.
erg@google.com21a49582009-08-03 21:00:44 +000039
erg@google.com5de56f32013-04-12 21:15:03 +000040This implements title \"Function Declarations and Definitions\"
41of the Google C++ Style Guide for the case where the previous
42line ends with an open parenthese.
erg@google.com21a49582009-08-03 21:00:44 +000043
erg@google.com5de56f32013-04-12 21:15:03 +000044\"Current C expression\", as per the Google Style Guide and as
45clarified by subsequent discussions, means the whole expression
46regardless of the number of nested parentheses, but excluding
47non-expression material such as \"if(\" and \"for(\" control
erg@google.com2df72b32009-10-09 21:51:56 +000048structures.
erg@google.com21a49582009-08-03 21:00:44 +000049
erg@google.com2df72b32009-10-09 21:51:56 +000050Suitable for inclusion in `c-offsets-alist'."
51 (save-excursion
52 (back-to-indentation)
53 ;; Go to beginning of *previous* line:
54 (c-backward-syntactic-ws)
55 (back-to-indentation)
erg@google.com5de56f32013-04-12 21:15:03 +000056 (cond
57 ;; We are making a reasonable assumption that if there is a control
58 ;; structure to indent past, it has to be at the beginning of the line.
59 ((looking-at "\\(\\(if\\|for\\|while\\)\\s *(\\)")
60 (goto-char (match-end 1)))
61 ;; For constructor initializer lists, the reference point for line-up is
62 ;; the token after the initial colon.
63 ((looking-at ":\\s *")
64 (goto-char (match-end 0))))
erg@google.com2df72b32009-10-09 21:51:56 +000065 (vector (+ 4 (current-column)))))
erg@google.com5de56f32013-04-12 21:15:03 +000066
erg@google.coma8999a62014-09-29 18:18:46 +000067;;;###autoload
mmentovaidb0be8e2008-12-17 22:24:29 +000068(defconst google-c-style
69 `((c-recognize-knr-p . nil)
70 (c-enable-xemacs-performance-kludge-p . t) ; speed up indentation in XEmacs
71 (c-basic-offset . 2)
72 (indent-tabs-mode . nil)
73 (c-comment-only-line-offset . 0)
74 (c-hanging-braces-alist . ((defun-open after)
75 (defun-close before after)
76 (class-open after)
77 (class-close before after)
erg@google.com5de56f32013-04-12 21:15:03 +000078 (inexpr-class-open after)
79 (inexpr-class-close before)
mmentovaidb0be8e2008-12-17 22:24:29 +000080 (namespace-open after)
81 (inline-open after)
82 (inline-close before after)
83 (block-open after)
84 (block-close . c-snug-do-while)
85 (extern-lang-open after)
86 (extern-lang-close after)
87 (statement-case-open after)
88 (substatement-open after)))
89 (c-hanging-colons-alist . ((case-label)
90 (label after)
91 (access-label after)
92 (member-init-intro before)
93 (inher-intro)))
94 (c-hanging-semi&comma-criteria
95 . (c-semi&comma-no-newlines-for-oneline-inliners
96 c-semi&comma-inside-parenlist
97 c-semi&comma-no-newlines-before-nonblanks))
erg@google.com5de56f32013-04-12 21:15:03 +000098 (c-indent-comments-syntactically-p . t)
mmentovaidb0be8e2008-12-17 22:24:29 +000099 (comment-column . 40)
erg@google.com5de56f32013-04-12 21:15:03 +0000100 (c-indent-comment-alist . ((other . (space . 2))))
mmentovaidb0be8e2008-12-17 22:24:29 +0000101 (c-cleanup-list . (brace-else-brace
102 brace-elseif-brace
103 brace-catch-brace
104 empty-defun-braces
105 defun-close-semi
106 list-close-comma
107 scope-operator))
erg@google.com2df72b32009-10-09 21:51:56 +0000108 (c-offsets-alist . ((arglist-intro google-c-lineup-expression-plus-4)
mmentovaidb0be8e2008-12-17 22:24:29 +0000109 (func-decl-cont . ++)
110 (member-init-intro . ++)
111 (inher-intro . ++)
112 (comment-intro . 0)
113 (arglist-close . c-lineup-arglist)
114 (topmost-intro . 0)
115 (block-open . 0)
116 (inline-open . 0)
117 (substatement-open . 0)
118 (statement-cont
119 .
120 (,(when (fboundp 'c-no-indent-after-java-annotations)
121 'c-no-indent-after-java-annotations)
122 ,(when (fboundp 'c-lineup-assignments)
123 'c-lineup-assignments)
124 ++))
125 (label . /)
126 (case-label . +)
127 (statement-case-open . +)
128 (statement-case-intro . +) ; case w/o {
129 (access-label . /)
130 (innamespace . 0))))
erg@google.com5de56f32013-04-12 21:15:03 +0000131 "Google C/C++ Programming Style.")
mmentovaidb0be8e2008-12-17 22:24:29 +0000132
erg@google.coma8999a62014-09-29 18:18:46 +0000133;;;###autoload
mmentovaidb0be8e2008-12-17 22:24:29 +0000134(defun google-set-c-style ()
135 "Set the current buffer's c-style to Google C/C++ Programming
136 Style. Meant to be added to `c-mode-common-hook'."
137 (interactive)
138 (make-local-variable 'c-tab-always-indent)
139 (setq c-tab-always-indent t)
140 (c-add-style "Google" google-c-style t))
141
erg@google.coma8999a62014-09-29 18:18:46 +0000142;;;###autoload
mmentovaidb0be8e2008-12-17 22:24:29 +0000143(defun google-make-newline-indent ()
144 "Sets up preferred newline behavior. Not set by default. Meant
145 to be added to `c-mode-common-hook'."
146 (interactive)
147 (define-key c-mode-base-map "\C-m" 'newline-and-indent)
148 (define-key c-mode-base-map [ret] 'newline-and-indent))
149
150(provide 'google-c-style)
151;;; google-c-style.el ends here