Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
6.1 KiB

5 years ago
  1. ;;; google-c-style.el --- Google's C/C++ style for c-mode
  2. ;; Keywords: c, tools
  3. ;; Package-Version: 20180130.1736
  4. ;; Package-Commit: 9c8784ded344f6a35d1e1550385002f613a0c788
  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. ;;; Commentary:
  15. ;; Provides the google C/C++ coding style. You may wish to add
  16. ;; `google-set-c-style' to your `c-mode-common-hook' after requiring this
  17. ;; file. For example:
  18. ;;
  19. ;; (add-hook 'c-mode-common-hook 'google-set-c-style)
  20. ;;
  21. ;; If you want the RETURN key to go to the next line and space over
  22. ;; to the right place, add this to your .emacs right after the load-file:
  23. ;;
  24. ;; (add-hook 'c-mode-common-hook 'google-make-newline-indent)
  25. ;;; Code:
  26. ;; For some reason 1) c-backward-syntactic-ws is a macro and 2) under Emacs 22
  27. ;; bytecode cannot call (unexpanded) macros at run time:
  28. (eval-when-compile (require 'cc-defs))
  29. ;; Wrapper function needed for Emacs 21 and XEmacs (Emacs 22 offers the more
  30. ;; elegant solution of composing a list of lineup functions or quantities with
  31. ;; operators such as "add")
  32. (defun google-c-lineup-expression-plus-4 (langelem)
  33. "Indents to the beginning of the current C expression plus 4 spaces.
  34. This implements title \"Function Declarations and Definitions\"
  35. of the Google C++ Style Guide for the case where the previous
  36. line ends with an open parenthese.
  37. \"Current C expression\", as per the Google Style Guide and as
  38. clarified by subsequent discussions, means the whole expression
  39. regardless of the number of nested parentheses, but excluding
  40. non-expression material such as \"if(\" and \"for(\" control
  41. structures.
  42. Suitable for inclusion in `c-offsets-alist'."
  43. (save-excursion
  44. (back-to-indentation)
  45. ;; Go to beginning of *previous* line:
  46. (c-backward-syntactic-ws)
  47. (back-to-indentation)
  48. (cond
  49. ;; We are making a reasonable assumption that if there is a control
  50. ;; structure to indent past, it has to be at the beginning of the line.
  51. ((looking-at "\\(\\(if\\|for\\|while\\)\\s *(\\)")
  52. (goto-char (match-end 1)))
  53. ;; For constructor initializer lists, the reference point for line-up is
  54. ;; the token after the initial colon.
  55. ((looking-at ":\\s *")
  56. (goto-char (match-end 0))))
  57. (vector (+ 4 (current-column)))))
  58. ;;;###autoload
  59. (defconst google-c-style
  60. `((c-recognize-knr-p . nil)
  61. (c-enable-xemacs-performance-kludge-p . t) ; speed up indentation in XEmacs
  62. (c-basic-offset . 2)
  63. (indent-tabs-mode . nil)
  64. (c-comment-only-line-offset . 0)
  65. (c-hanging-braces-alist . ((defun-open after)
  66. (defun-close before after)
  67. (class-open after)
  68. (class-close before after)
  69. (inexpr-class-open after)
  70. (inexpr-class-close before)
  71. (namespace-open after)
  72. (inline-open after)
  73. (inline-close before after)
  74. (block-open after)
  75. (block-close . c-snug-do-while)
  76. (extern-lang-open after)
  77. (extern-lang-close after)
  78. (statement-case-open after)
  79. (substatement-open after)))
  80. (c-hanging-colons-alist . ((case-label)
  81. (label after)
  82. (access-label after)
  83. (member-init-intro before)
  84. (inher-intro)))
  85. (c-hanging-semi&comma-criteria
  86. . (c-semi&comma-no-newlines-for-oneline-inliners
  87. c-semi&comma-inside-parenlist
  88. c-semi&comma-no-newlines-before-nonblanks))
  89. (c-indent-comments-syntactically-p . t)
  90. (comment-column . 40)
  91. (c-indent-comment-alist . ((other . (space . 2))))
  92. (c-cleanup-list . (brace-else-brace
  93. brace-elseif-brace
  94. brace-catch-brace
  95. empty-defun-braces
  96. defun-close-semi
  97. list-close-comma
  98. scope-operator))
  99. (c-offsets-alist . ((arglist-intro google-c-lineup-expression-plus-4)
  100. (func-decl-cont . ++)
  101. (member-init-intro . ++)
  102. (inher-intro . ++)
  103. (comment-intro . 0)
  104. (arglist-close . c-lineup-arglist)
  105. (topmost-intro . 0)
  106. (block-open . 0)
  107. (inline-open . 0)
  108. (substatement-open . 0)
  109. (statement-cont
  110. .
  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)
  115. ++))
  116. (label . /)
  117. (case-label . +)
  118. (statement-case-open . +)
  119. (statement-case-intro . +) ; case w/o {
  120. (access-label . /)
  121. (innamespace . 0))))
  122. "Google C/C++ Programming Style.")
  123. ;;;###autoload
  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'."
  127. (interactive)
  128. (make-local-variable 'c-tab-always-indent)
  129. (setq c-tab-always-indent t)
  130. (c-add-style "Google" google-c-style t))
  131. ;;;###autoload
  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'."
  135. (interactive)
  136. (define-key c-mode-base-map "\C-m" 'newline-and-indent)
  137. (define-key c-mode-base-map [ret] 'newline-and-indent))
  138. (provide 'google-c-style)
  139. ;;; google-c-style.el ends here