[BACK]Return to NetBSD.el CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / share / misc

File: [cvs.NetBSD.org] / src / share / misc / NetBSD.el (download)

Revision 1.5, Mon Sep 24 15:38:11 2007 UTC (13 years, 6 months ago) by christos
Branch: MAIN
CVS Tags: yamt-pf42-baseX, yamt-pf42-base4, yamt-pf42-base3, yamt-pf42-base2, yamt-pf42-base, yamt-pf42, wrstuden-revivesa-base-3, wrstuden-revivesa-base-2, wrstuden-revivesa-base-1, wrstuden-revivesa-base, wrstuden-revivesa, netbsd-5-base, netbsd-5-2-RELEASE, netbsd-5-2-RC1, netbsd-5-2-3-RELEASE, netbsd-5-2-2-RELEASE, netbsd-5-2-1-RELEASE, netbsd-5-2, netbsd-5-1-RELEASE, netbsd-5-1-RC4, netbsd-5-1-RC3, netbsd-5-1-RC2, netbsd-5-1-RC1, netbsd-5-1-5-RELEASE, netbsd-5-1-4-RELEASE, netbsd-5-1-3-RELEASE, netbsd-5-1-2-RELEASE, netbsd-5-1-1-RELEASE, netbsd-5-1, netbsd-5-0-RELEASE, netbsd-5-0-RC4, netbsd-5-0-RC3, netbsd-5-0-RC2, netbsd-5-0-RC1, netbsd-5-0-2-RELEASE, netbsd-5-0-1-RELEASE, netbsd-5-0, netbsd-5, mjf-devfs2-base, mjf-devfs2, matt-nb5-pq3-base, matt-nb5-pq3, matt-nb5-mips64-u2-k2-k4-k7-k8-k9, matt-nb5-mips64-u1-k1-k5, matt-nb5-mips64-premerge-20101231, matt-nb5-mips64-premerge-20091211, matt-nb5-mips64-k15, matt-nb5-mips64, matt-nb4-mips64-k7-u2a-k9b, matt-mips64-base2, matt-armv6-nbase, matt-armv6-base, keiichi-mipv6-nbase, keiichi-mipv6-base, keiichi-mipv6, hpcarm-cleanup-nbase, hpcarm-cleanup-base, cube-autoconf-base, cube-autoconf
Branch point for: jym-xensuspend
Changes since 1.4: +47 -10 lines

Add code to support whitespace checking/cleanup. This disabled by
default.  To enable it, set netbsd-knf-whitespace-check to 't'. See
the help message for this variable for related knobs and issues. This
was tested on emacs 21.4 and 22.1. (From Anon Ymous)

;; $NetBSD: NetBSD.el,v 1.5 2007/09/24 15:38:11 christos Exp $

(defconst netbsd-knf-style
   ;; (c-auto-newline . nil)
   ;; default indentation level
   (c-basic-offset . 8)
   ;; in which column to add backslashes when macroizing a region
   (c-backslash-column . 78)
   ;; automatically compact brace-else(if)-brace on one line and
   ;; semi-colon after closing struct brace
   (c-cleanup-list . (brace-else-brace
   ;; do not indent lines containing only start-of-comment more than default
   (c-comment-only-line-offset . 0)
   ;; start new lines after braces
   ;; default is: before and after (for all other cases)
   (c-hanging-braces-alist . ((defun-open . (before after))
			      (defun-close . (before after))
			      (block-open . (after))
			      (block-close . c-snug-do-while)
			      (substatement-open . after)
			      (statement-case-open . nil)
			      (brace-list-open . after)
			      (brace-list-close . nil)
   ;; where to put newlines around colons
   (c-hanging-colons-alist . (quote ((label after)
				     (case-label after))))
   ;; indent comments syntactically
   (c-indent-comments-syntactically-p . t)
   ;; no spaces needed before a label
   ;; (c-label-minimum-indentation . 0)
   ;; define offsets for some code parts
   (c-offsets-alist . ((arglist-cont-nonempty . 4)
		       (block-open        . 0)
;;		       (block-open        . -)
		       (brace-list-entry  . 8)
		       (brace-list-open   . 8)
		       (brace-list-close  . 0)
		       (knr-argdecl       . 0)
		       (knr-argdecl-intro . +)
		       (label             . -)
		       (member-init-intro . ++)
		       (statement-cont    . 4)
		       (substatement-open . 0)
		       (case-label        . 0)))
   ;; XXX: undocumented. Recognize KNR style?
   (c-recognize-knr-p . t)
   ;; indent line when pressing tab, instead of a plain tab character
   (c-tab-always-indent . t)
   ;; use TABs for indentation, not spaces
   (indent-tabs-mode . t)
   ;; set default tab width to 8
   (tab-width . 8)
  "NetBSD KNF Style")

(eval-when-compile (require 'whitespace nil t))

(defcustom netbsd-knf-whitespace-check nil
  "Enable NetBSD KNF whitespace cleanup when saving the buffer.
See also:
1) `whitespace-check-spacetab-whitespace' will replace any RE-match of
   \" +\\t\" with single '\\t' and hence may break tabbing.
2) Both `whitespace-check-spacetab-whitespace' and
   `whitespace-check-indent-whitespace' may alter strings."
  :type 'boolean
  :group 'netbsd-knf)

(defun netbsd-knf-whitespace-cleanup ()
  ;; XXX - emacs 21.4 whitespace.el was badly behaved on blank
  ;; buffers.  This was fixed in 22.1.  I don't know about other
  ;; versions, so these conditions may need to be more restrictive.
  (cond ((> emacs-major-version 21)
	(t ;; default
	 (if (save-excursion
	       (goto-char (point-min))
	       (re-search-forward "[^ \t\n]" nil t))
	   (delete-region (point-min) (point-max))))))

(defun netbsd-knf-write-contents-hook ()
  (if (and (string-equal c-indentation-style "netbsd knf")
	   (require 'whitespace nil t))
      (if netbsd-knf-whitespace-check
	  (if whitespace-auto-cleanup
	    (if (and (whitespace-buffer) whitespace-abort-on-error)
		(error (concat "Abort write due to whitespaces in "
    (remove-hook 'write-contents-hook 'netbsd-knf-write-contents-hook))

(defun netbsd-knf-c-mode-hook ()
  ;; Add style and set it for current buffer
  (c-add-style "NetBSD KNF" netbsd-knf-style t)
  ;; useful, but not necessary for the mode
  ;; give syntactic information in message buffer
  ;;(setq c-echo-syntactic-information-p t)
  ;; automatic newlines after special characters
  (setq c-toggle-auto-state 1)
  ;; delete all connected whitespace when pressing delete
  (setq c-toggle-hungry-state 1)
  ;; auto-indent new lines
  (define-key c-mode-base-map "\C-m" 'newline-and-indent)
  ;; check/cleanup whitespace when saving
  (add-hook 'write-contents-hooks 'netbsd-knf-write-contents-hook))

(add-hook 'c-mode-hook 'netbsd-knf-c-mode-hook)