mmentovai | db0be8e | 2008-12-17 22:24:29 +0000 | [diff] [blame] | 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 | |
erg@google.com | 2df72b3 | 2009-10-09 21:51:56 +0000 | [diff] [blame] | 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)) |
erg@google.com | 21a4958 | 2009-08-03 21:00:44 +0000 | [diff] [blame] | 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") |
erg@google.com | 2df72b3 | 2009-10-09 21:51:56 +0000 | [diff] [blame] | 37 | (defun google-c-lineup-expression-plus-4 (langelem) |
| 38 | "Indents to the beginning of the current C expression plus 4 spaces. |
erg@google.com | 21a4958 | 2009-08-03 21:00:44 +0000 | [diff] [blame] | 39 | |
erg@google.com | 5de56f3 | 2013-04-12 21:15:03 +0000 | [diff] [blame] | 40 | This implements title \"Function Declarations and Definitions\" |
| 41 | of the Google C++ Style Guide for the case where the previous |
| 42 | line ends with an open parenthese. |
erg@google.com | 21a4958 | 2009-08-03 21:00:44 +0000 | [diff] [blame] | 43 | |
erg@google.com | 5de56f3 | 2013-04-12 21:15:03 +0000 | [diff] [blame] | 44 | \"Current C expression\", as per the Google Style Guide and as |
| 45 | clarified by subsequent discussions, means the whole expression |
| 46 | regardless of the number of nested parentheses, but excluding |
| 47 | non-expression material such as \"if(\" and \"for(\" control |
erg@google.com | 2df72b3 | 2009-10-09 21:51:56 +0000 | [diff] [blame] | 48 | structures. |
erg@google.com | 21a4958 | 2009-08-03 21:00:44 +0000 | [diff] [blame] | 49 | |
erg@google.com | 2df72b3 | 2009-10-09 21:51:56 +0000 | [diff] [blame] | 50 | Suitable 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.com | 5de56f3 | 2013-04-12 21:15:03 +0000 | [diff] [blame] | 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)))) |
erg@google.com | 2df72b3 | 2009-10-09 21:51:56 +0000 | [diff] [blame] | 65 | (vector (+ 4 (current-column))))) |
erg@google.com | 5de56f3 | 2013-04-12 21:15:03 +0000 | [diff] [blame] | 66 | |
erg@google.com | a8999a6 | 2014-09-29 18:18:46 +0000 | [diff] [blame] | 67 | ;;;###autoload |
mmentovai | db0be8e | 2008-12-17 22:24:29 +0000 | [diff] [blame] | 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) |
erg@google.com | 5de56f3 | 2013-04-12 21:15:03 +0000 | [diff] [blame] | 78 | (inexpr-class-open after) |
| 79 | (inexpr-class-close before) |
mmentovai | db0be8e | 2008-12-17 22:24:29 +0000 | [diff] [blame] | 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)) |
erg@google.com | 5de56f3 | 2013-04-12 21:15:03 +0000 | [diff] [blame] | 98 | (c-indent-comments-syntactically-p . t) |
mmentovai | db0be8e | 2008-12-17 22:24:29 +0000 | [diff] [blame] | 99 | (comment-column . 40) |
erg@google.com | 5de56f3 | 2013-04-12 21:15:03 +0000 | [diff] [blame] | 100 | (c-indent-comment-alist . ((other . (space . 2)))) |
mmentovai | db0be8e | 2008-12-17 22:24:29 +0000 | [diff] [blame] | 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)) |
erg@google.com | 2df72b3 | 2009-10-09 21:51:56 +0000 | [diff] [blame] | 108 | (c-offsets-alist . ((arglist-intro google-c-lineup-expression-plus-4) |
mmentovai | db0be8e | 2008-12-17 22:24:29 +0000 | [diff] [blame] | 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)))) |
erg@google.com | 5de56f3 | 2013-04-12 21:15:03 +0000 | [diff] [blame] | 131 | "Google C/C++ Programming Style.") |
mmentovai | db0be8e | 2008-12-17 22:24:29 +0000 | [diff] [blame] | 132 | |
erg@google.com | a8999a6 | 2014-09-29 18:18:46 +0000 | [diff] [blame] | 133 | ;;;###autoload |
mmentovai | db0be8e | 2008-12-17 22:24:29 +0000 | [diff] [blame] | 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 | |
erg@google.com | a8999a6 | 2014-09-29 18:18:46 +0000 | [diff] [blame] | 142 | ;;;###autoload |
mmentovai | db0be8e | 2008-12-17 22:24:29 +0000 | [diff] [blame] | 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 |