lang/cc: rewrite & polish style settings

Doom used to have its own cc-mode style, which was difficult to
customize without first undoing all its changes.

A doom entry has been added to c-style-alist, which represents
a marriage of various styles (mostly linux), plus some context-sensitive
indentation functions which I think are reasonable.

More importantly, it can be disabled by changing c-default-style.

Also, removed a few hacks that have been merged into v5.33+ of cc-mode.
25.1 users beware! You may not have these changes.
This commit is contained in:
Henrik Lissner 2018-02-23 04:44:02 -05:00
parent 55c6244e9f
commit 5fc4ed3f81
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
2 changed files with 51 additions and 37 deletions

View file

@ -19,19 +19,6 @@
(when (and (featurep 'irony) irony-mode) (when (and (featurep 'irony) irony-mode)
(+cc|irony-init-compile-options))) (+cc|irony-init-compile-options)))
;;;###autoload
(defun +cc*align-lambda-arglist (orig-fun &rest args)
"Improve indentation of continued C++11 lambda function opened as argument."
(if (and (eq major-mode 'c++-mode)
(ignore-errors
(save-excursion
(goto-char (c-langelem-pos langelem))
;; Detect "[...](" or "[...]{". preceded by "," or "(",
;; and with unclosed brace.
(looking-at-p ".*[(,][ \t]*\\[[^]]*\\][ \t]*[({][^}]*$"))))
0 ; no additional indent
(apply orig-fun args)))
;;;###autoload ;;;###autoload
(defun +cc-sp-point-is-template-p (id action context) (defun +cc-sp-point-is-template-p (id action context)
"Return t if point is in the right place for C++ angle-brackets." "Return t if point is in the right place for C++ angle-brackets."
@ -50,17 +37,26 @@
(looking-at-p "[ ]*#include[^<]+")))) (looking-at-p "[ ]*#include[^<]+"))))
;;;###autoload ;;;###autoload
(defun +cc-c-lineup-inclass (_langelem) (defun +cc-c++-lineup-inclass (langelem)
"Indent privacy keywords at same level as class properties." "Indent inclass lines one level further than access modifier keywords."
(if (memq major-mode '(c-mode c++-mode)) (when (and (eq major-mode 'c++-mode)
(let ((inclass (assq 'inclass c-syntactic-context))) (or (assoc 'access-label c-syntactic-context)
(save-excursion (save-excursion
(goto-char (c-langelem-pos inclass)) (save-match-data
(if (or (looking-at "struct") (re-search-backward
(looking-at "typedef struct")) "\\(?:p\\(?:ublic\\|r\\(?:otected\\|ivate\\)\\)\\)"
'+ (c-langelem-pos langelem) t)))))
'++))) '++))
'+))
;;;###autoload
(defun +cc-lineup-arglist-close (langlem)
"Line up the closing brace in an arglist with the opening brace IF cursor is
preceded by the opening brace or a comma (disregarding whitespace in between)."
(when (save-excursion
(save-match-data
(skip-chars-backward " \t\n" (c-langelem-pos langelem))
(memq (char-before) (list ?, ?\( ?\;))))
(c-lineup-arglist langlem)))
;; ;;

View file

@ -48,7 +48,8 @@ compilation database is present in the project.")
:init :init
(setq-default c-basic-offset tab-width (setq-default c-basic-offset tab-width
c-backspace-function #'delete-backward-char) c-backspace-function #'delete-backward-char
c-default-style "doom")
:config :config
(set! :electric '(c-mode c++-mode objc-mode java-mode) (set! :electric '(c-mode c++-mode objc-mode java-mode)
@ -61,24 +62,41 @@ compilation database is present in the project.")
;; C/C++ style settings ;; C/C++ style settings
(c-toggle-electric-state -1) (c-toggle-electric-state -1)
(c-toggle-auto-newline -1) (c-toggle-auto-newline -1)
(c-set-offset 'substatement-open '0) ; don't indent brackets
(c-set-offset 'inline-open '+)
(c-set-offset 'block-open '+)
(c-set-offset 'brace-list-open '+)
(c-set-offset 'case-label '+)
(c-set-offset 'access-label '-)
(c-set-offset 'arglist-intro '+)
(c-set-offset 'arglist-close '0)
;; Indent privacy keywords at same level as class properties
;; (c-set-offset 'inclass #'+cc-c-lineup-inclass)
;;; Better fontification (also see `modern-cpp-font-lock') ;;; Better fontification (also see `modern-cpp-font-lock')
(add-hook 'c-mode-common-hook #'rainbow-delimiters-mode) (add-hook 'c-mode-common-hook #'rainbow-delimiters-mode)
(add-hook! (c-mode c++-mode) #'highlight-numbers-mode) (add-hook! (c-mode c++-mode) #'highlight-numbers-mode)
(add-hook! (c-mode c++-mode) #'+cc|fontify-constants) (add-hook! (c-mode c++-mode) #'+cc|fontify-constants)
;; Improve indentation of inline lambdas in C++11 ;; Custom style, based off of linux
(advice-add #'c-lineup-arglist :around #'+cc*align-lambda-arglist) (map-put c-style-alist "doom"
`((c-basic-offset . ,tab-width)
(c-comment-only-line-offset . 0)
(c-hanging-braces-alist (brace-list-open)
(brace-entry-open)
(substatement-open after)
(block-close . c-snug-do-while)
(arglist-cont-nonempty))
(c-cleanup-list brace-else-brace)
(c-offsets-alist
(statement-block-intro . +)
(knr-argdecl-intro . 0)
(substatement-open . 0)
(substatement-label . 0)
(statement-cont . +)
(case-label . +)
;; align args with open brace OR don't indent at all (if open brace
;; is at eolp and close brace is after arg with no trailing comma)
(arglist-intro . +)
(arglist-close +cc-lineup-arglist-close 0)
;; don't over-indent lambda blocks
(inline-open . 0)
(inlambda . 0)
;; indent access keywords +1 level, and properties beneath them
;; another level
(access-label . -)
(inclass +cc-c++-lineup-inclass +)
(label . 0))))
;;; Keybindings ;;; Keybindings
;; Completely disable electric keys because it interferes with smartparens and ;; Completely disable electric keys because it interferes with smartparens and