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