xref: /netbsd-src/share/misc/NetBSD.el (revision ac97beab45c5f99d6e919d67bff9936e401102ef)
1;; $NetBSD: NetBSD.el,v 1.6 2009/02/16 21:37:12 uwe Exp $
2
3(defconst netbsd-knf-style
4  '(
5   ;; (c-auto-newline . nil)
6   ;; default indentation level
7   (c-basic-offset . 8)
8   ;; in which column to add backslashes when macroizing a region
9   (c-backslash-column . 78)
10   (c-backslash-max-column . 78)
11   ;; automatically compact brace-else(if)-brace on one line and
12   ;; semi-colon after closing struct brace
13   (c-cleanup-list . (brace-else-brace
14		      brace-elseif-brace
15		      defun-close-semi))
16   ;; do not indent lines containing only start-of-comment more than default
17   (c-comment-only-line-offset . 0)
18   ;; start new lines after braces
19   ;; default is: before and after (for all other cases)
20   (c-hanging-braces-alist . ((defun-open . (before after))
21			      (defun-close . (before after))
22			      (block-open . (after))
23			      (block-close . c-snug-do-while)
24			      (substatement-open . after)
25			      (statement-case-open . nil)
26			      (brace-list-open . after)
27			      (brace-list-close . nil)
28			      ))
29   ;; where to put newlines around colons
30   (c-hanging-colons-alist . (quote ((label after)
31				     (case-label after))))
32   ;; indent comments syntactically
33   (c-indent-comments-syntactically-p . t)
34   ;; no spaces needed before a label
35   ;; (c-label-minimum-indentation . 0)
36   ;; define offsets for some code parts
37   (c-offsets-alist . ((arglist-cont-nonempty . 4)
38		       (block-open        . 0)
39;;		       (block-open        . -)
40		       (brace-list-open   . +)
41		       (brace-list-intro  . +)
42		       (brace-list-entry  . 0)
43		       (brace-list-close  . 0)
44		       (knr-argdecl       . 0)
45		       (knr-argdecl-intro . +)
46		       (label             . -)
47		       (member-init-intro . ++)
48		       (statement-cont    . 4)
49		       (substatement-open . 0)
50		       (case-label        . 0)))
51   ;; XXX: undocumented. Recognize KNR style?
52   (c-recognize-knr-p . t)
53   ;; indent line when pressing tab, instead of a plain tab character
54   (c-tab-always-indent . t)
55   ;; use TABs for indentation, not spaces
56   (indent-tabs-mode . t)
57   ;; set default tab width to 8
58   (tab-width . 8)
59  )
60  "NetBSD KNF Style")
61
62(eval-when-compile (require 'whitespace nil t))
63
64(defcustom netbsd-knf-whitespace-check nil
65  "Enable NetBSD KNF whitespace cleanup when saving the buffer.
66See also:
67  `whitespace-auto-cleanup',
68  `whitespace-abort-on-error',
69  `whitespace-check-leading-whitespace',
70  `whitespace-check-trailing-whitespace',
71  `whitespace-check-spacetab-whitespace',
72  `whitespace-check-indent-whitespace',
73  `whitespace-check-ateol-whitespace'.
74NOTES:
751) `whitespace-check-spacetab-whitespace' will replace any RE-match of
76   \" +\\t\" with single '\\t' and hence may break tabbing.
772) Both `whitespace-check-spacetab-whitespace' and
78   `whitespace-check-indent-whitespace' may alter strings."
79  :type 'boolean
80  :group 'netbsd-knf)
81
82(defun netbsd-knf-whitespace-cleanup ()
83  ;; XXX - emacs 21.4 whitespace.el was badly behaved on blank
84  ;; buffers.  This was fixed in 22.1.  I don't know about other
85  ;; versions, so these conditions may need to be more restrictive.
86  (cond ((> emacs-major-version 21)
87	 (whitespace-cleanup-internal))
88	(t ;; default
89	 (if (save-excursion
90	       (goto-char (point-min))
91	       (re-search-forward "[^ \t\n]" nil t))
92	     (whitespace-cleanup)
93	   (delete-region (point-min) (point-max))))))
94
95(defun netbsd-knf-write-contents-hook ()
96  (if (and (string-equal c-indentation-style "netbsd knf")
97	   (require 'whitespace nil t))
98      (if netbsd-knf-whitespace-check
99	  (if whitespace-auto-cleanup
100	      (netbsd-knf-whitespace-cleanup)
101	    (if (and (whitespace-buffer) whitespace-abort-on-error)
102		(error (concat "Abort write due to whitespaces in "
103			       buffer-file-name)))))
104    (remove-hook 'write-contents-hook 'netbsd-knf-write-contents-hook))
105  nil)
106
107(defun netbsd-knf-c-mode-hook ()
108  ;; Add style and set it for current buffer
109  (c-add-style "NetBSD KNF" netbsd-knf-style t)
110  ;; useful, but not necessary for the mode
111  ;; give syntactic information in message buffer
112  ;;(setq c-echo-syntactic-information-p t)
113  ;; automatic newlines after special characters
114  (setq c-toggle-auto-state 1)
115  ;; delete all connected whitespace when pressing delete
116  (setq c-toggle-hungry-state 1)
117  ;; auto-indent new lines
118  (define-key c-mode-base-map "\C-m" 'newline-and-indent)
119  ;; check/cleanup whitespace when saving
120  (add-hook 'write-contents-hooks 'netbsd-knf-write-contents-hook))
121
122(add-hook 'c-mode-hook 'netbsd-knf-c-mode-hook)
123