1
;;; google-c-style.el --- Google's C/C++ style for c-mode
5
;; google-c-style.el is Copyright (C) 2008 Google Inc. All Rights Reserved.
7
;; It is free software; you can redistribute it and/or modify it under the
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
13
;; b) the "Artistic License".
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
21
;; (add-hook 'c-mode-common-hook 'google-set-c-style)
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:
26
;; (add-hook 'c-mode-common-hook 'google-make-newline-indent)
30
;; 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))
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")
37
(defun google-c-lineup-expression-plus-4 (langelem)
38
"Indents to the beginning of the current C expression plus 4 spaces.
40
This implements title \"Function Declarations and Definitions\" of the Google
41
C++ Style Guide for the case where the previous line ends with an open
44
\"Current C expression\", as per the Google Style Guide and as clarified by
45
subsequent discussions,
46
means the whole expression regardless of the number of nested parentheses, but
47
excluding non-expression material such as \"if(\" and \"for(\" control
50
Suitable for inclusion in `c-offsets-alist'."
53
;; Go to beginning of *previous* line:
54
(c-backward-syntactic-ws)
56
;; We are making a reasonable assumption that if there is a control
57
;; structure to indent past, it has to be at the beginning of the line.
58
(if (looking-at "\\(\\(if\\|for\\|while\\)\\s *(\\)")
59
(goto-char (match-end 1)))
60
(vector (+ 4 (current-column)))))
62
(defconst google-c-style
63
`((c-recognize-knr-p . nil)
64
(c-enable-xemacs-performance-kludge-p . t) ; speed up indentation in XEmacs
66
(indent-tabs-mode . nil)
67
(c-comment-only-line-offset . 0)
68
(c-hanging-braces-alist . ((defun-open after)
69
(defun-close before after)
71
(class-close before after)
72
(namespace-open after)
74
(inline-close before after)
76
(block-close . c-snug-do-while)
77
(extern-lang-open after)
78
(extern-lang-close after)
79
(statement-case-open after)
80
(substatement-open after)))
81
(c-hanging-colons-alist . ((case-label)
84
(member-init-intro before)
86
(c-hanging-semi&comma-criteria
87
. (c-semi&comma-no-newlines-for-oneline-inliners
88
c-semi&comma-inside-parenlist
89
c-semi&comma-no-newlines-before-nonblanks))
90
(c-indent-comments-syntactically-p . nil)
92
(c-cleanup-list . (brace-else-brace
99
(c-offsets-alist . ((arglist-intro google-c-lineup-expression-plus-4)
100
(func-decl-cont . ++)
101
(member-init-intro . ++)
104
(arglist-close . c-lineup-arglist)
108
(substatement-open . 0)
111
(,(when (fboundp 'c-no-indent-after-java-annotations)
112
'c-no-indent-after-java-annotations)
113
,(when (fboundp 'c-lineup-assignments)
114
'c-lineup-assignments)
118
(statement-case-open . +)
119
(statement-case-intro . +) ; case w/o {
122
"Google C/C++ Programming Style")
124
(defun google-set-c-style ()
125
"Set the current buffer's c-style to Google C/C++ Programming
126
Style. Meant to be added to `c-mode-common-hook'."
128
(make-local-variable 'c-tab-always-indent)
129
(setq c-tab-always-indent t)
130
(c-add-style "Google" google-c-style t))
132
(defun google-make-newline-indent ()
133
"Sets up preferred newline behavior. Not set by default. Meant
134
to be added to `c-mode-common-hook'."
136
(define-key c-mode-base-map "\C-m" 'newline-and-indent)
137
(define-key c-mode-base-map [ret] 'newline-and-indent))
139
(provide 'google-c-style)
140
;;; google-c-style.el ends here