• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1;;; 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
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))
33
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.
39
40This implements title \"Function Declarations and Definitions\"
41of the Google C++ Style Guide for the case where the previous
42line ends with an open parenthese.
43
44\"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
48structures.
49
50Suitable 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)
56    (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))))
65    (vector (+ 4 (current-column)))))
66
67;;;###autoload
68(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)
78                               (inexpr-class-open after)
79                               (inexpr-class-close before)
80                               (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))
98    (c-indent-comments-syntactically-p . t)
99    (comment-column . 40)
100    (c-indent-comment-alist . ((other . (space . 2))))
101    (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))
108    (c-offsets-alist . ((arglist-intro google-c-lineup-expression-plus-4)
109                        (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))))
131  "Google C/C++ Programming Style.")
132
133;;;###autoload
134(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
142;;;###autoload
143(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
152