From e5751a93e5fb0dda887a8d8a8ce4691484544acf Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Tue, 19 May 2020 11:48:30 +0100 Subject: [PATCH] el/dot-emacs.el: Improve `mdw-define-c-style' to support simple inheritance. Delete the duplicate settings in `mdw-trustonic-c'. --- el/dot-emacs.el | 56 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/el/dot-emacs.el b/el/dot-emacs.el index 9803cf0..5968944 100644 --- a/el/dot-emacs.el +++ b/el/dot-emacs.el @@ -1974,24 +1974,48 @@ indentation anyway." (defvar mdw-define-c-styles-hook nil "Hook run when `cc-mode' starts up to define styles.") -(defmacro mdw-define-c-style (name &rest assocs) - "Define a C style, called NAME (a symbol), setting ASSOCs. +(defun mdw-merge-style-alists (first second) + (let ((output nil)) + (dolist (item first) + (let ((key (car item)) (value (cdr item))) + (if (string-suffix-p "-alist" (symbol-name key)) + (push (cons key + (mdw-merge-style-alists value + (cdr (assoc key second)))) + output) + (push item output)))) + (dolist (item second) + (unless (assoc (car item) first) + (push item output))) + (nreverse output))) + +(cl-defmacro mdw-define-c-style (name (&optional parent) &rest assocs) + "Define a C style, called NAME (a symbol) based on PARENT, setting ASSOCs. A function, named `mdw-define-c-style/NAME', is defined to actually install the style using `c-add-style', and added to the hook `mdw-define-c-styles-hook'. If CC Mode is already loaded, then the style is set." (declare (indent defun)) (let* ((name-string (symbol-name name)) + (var (intern (concat "mdw-c-style/" name-string))) (func (intern (concat "mdw-define-c-style/" name-string)))) `(progn - (defun ,func () (c-add-style ,name-string ',assocs)) + (setq ,var + ',(if (null parent) + assocs + (let ((parent-list (symbol-value + (intern (concat "mdw-c-style/" + (symbol-name parent)))))) + (mdw-merge-style-alists assocs parent-list)))) + (defun ,func () (c-add-style ,name-string ,var)) (and (featurep 'cc-mode) (,func)) - (add-hook 'mdw-define-c-styles-hook ',func)))) + (add-hook 'mdw-define-c-styles-hook ',func) + ',name))) (eval-after-load "cc-mode" '(run-hooks 'mdw-define-c-styles-hook)) -(mdw-define-c-style mdw-c +(mdw-define-c-style mdw-c () (c-basic-offset . 2) (comment-column . 40) (c-class-key . "class") @@ -2012,31 +2036,15 @@ set." (statement-cont . +) (statement-case-intro . +))) -(mdw-define-c-style mdw-trustonic-c +(mdw-define-c-style mdw-trustonic-c (mdw-c) (c-basic-offset . 4) (comment-column . 0) (c-indent-comment-alist (anchored-comment . (column . 0)) (end-block . (space . 1)) (cpp-end-block . (space . 1)) (other . (space . 1))) - (c-class-key . "class") - (c-backslash-column . 0) - (c-auto-align-backslashes . nil) - (c-label-minimum-indentation . 0) - (c-offsets-alist (substatement-open . (add 0 c-indent-one-line-block)) - (defun-open . (add 0 c-indent-one-line-block)) - (arglist-cont-nonempty . mdw-c-indent-arglist-nested) - (topmost-intro . mdw-c-indent-extern-mumble) - (cpp-define-intro . 0) - (knr-argdecl . 0) - (inextern-lang . [0]) - (label . 0) - (case-label . +) - (access-label . -2) - (inclass . +) - (inline-open . ++) - (statement-cont . +) - (statement-case-intro . +))) + (c-offsets-alist (arglist-cont-nonempty . mdw-c-indent-arglist-nested) + (access-label . -2))) (defun mdw-set-default-c-style (modes style) "Update the default CC Mode style for MODES to be STYLE. -- 2.11.0