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