Subject: [gnu.emacs.sources] zone-mode.el -- major mode for editing DNS zone files

From: Mark Eichin <[email-masked]>
Date: 10 Feb 1998 18:52:12 -0500

------- Start of forwarded message -------
From: [email-masked] (John Heidemann)
Newsgroups: gnu.emacs.sources
Subject: zone-mode.el -- major mode for editing DNS zone files
Date: 10 Feb 1998 10:14:20 -0800
Organization: USC Information Sciences Institute
Message-ID: <[email-masked]>

A mode for editing DNS zone files.

Zone-mode does two things:

	  - automatically update the serial number for a zone
	    when saving the file

          - fontification

To install,  put this in your .emacs:
  (setq auto-mode-alist (cons '(\".zone$\" . zone-mode)
					     auto-mode-alist))
or put ;-*-zone-*-
on an the first line of the zone files.

You may also need to add
  (autoload 'zone-mode \"zone-mode\" "")

Font-lock support assumes emacs-20.

   -John Heidemann

----------------------------------------------------------------------
;;;
;;; zone-mode.el
;;; $Id: zone-mode.el,v 1.10 1998/02/10 18:10:21 johnh Exp $
;;;
;;; Copyright (C) 1998 by John Heidemann
;;; Comments to <[email-masked]>.
;;;

;;;
;;; This file is not part of GNU Emacs, but I'm happy to contibute
;;; it if there is demand.
;;;
;;; zone-mode is free software; you can redistribute it and/or
;;; modify it under the terms of version 2 of the GNU General Public
;;; License as published by the Free Software Foundation.
;;;
;;; This program is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
;;; or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
;;; for more details.
;;;
;;; You should have received a copy of the GNU General Public License along
;;; with GNU Emacs.  If you did not, write to the Free Software Foundation,
;;; Inc., 675 Mass Ave., Cambridge, MA 02139, USA.
;;;

;;;
;;; See the commends in ``define-derived-mode zone-mode''
;;; (the last function in this file)
;;; for what this mode is and how to use it automatically.
;;;

(defun zone-mode-update-serial ()
  "Update the serial number in a zone."
  (interactive)
  (save-excursion
    (goto-char (point-min))
    (while (re-search-forward "\\b\\([0-9]+\\)\\([0-9][0-9]\\)\\([ \t]+;[ \t]+[Ss]erial\\)" (point-max) t)
      (let* ((old-date (match-string 1))
           (old-seq (match-string 2))
	        (old-seq-num (string-to-number (match-string 2)))
		     (old-flag (match-string 3))
		          (cur-date (format-time-string "%Y%m%d"))
			       (new-seq
			             (cond
				            ((not (string= old-date cur-date))
						  "00") ;; reset sequeence number
						         ((>= old-seq-num 99)
							      (error "Serial number's sequenece cannot increment beyond 99."))
							             (t
									(format "%02d" (1+ old-seq-num)))))
									     (old-serial (concat old-date old-seq))
									          (new-serial (concat cur-date new-seq)))
										  (if (string-lessp new-serial old-serial)
										      (error (format "Serial numbers want to move backwards from %s to %s!" old-serial new-serial))
										        (replace-match (concat cur-date new-seq old-flag) t t))))))
  
;;;###autoload
(defun zone-mode-update-serial-hook ()
  "Update the serial number in a zone if the file was modified"
  (interactive)
  (if (buffer-modified-p (current-buffer))
      (zone-mode-update-serial))
  nil ;; so we can run from write-file-hooks
  )

(defvar zone-mode-syntax-table nil
  "Zone-mode's syntax table.")

(defun zone-mode-load-time-setup ()
  "init zone-mode stuff"
  (setq zone-mode-syntax-table (make-syntax-table))
  (modify-syntax-entry ?\; "<" zone-mode-syntax-table)
  (modify-syntax-entry ?\n ">" zone-mode-syntax-table))

;;;###autoload
(define-derived-mode zone-mode fundamental-mode "zone"
  "A mode for editing DNS zone files.

Zone-mode does two things:

	  - automatically update the serial number for a zone
	    when saving the file

	    - fontification

To install,  put this in your .emacs:
  (setq auto-mode-alist (cons '(\".zone$\" . zone-mode)
					     auto-mode-alist))
or with ;-*-zone-*-
on an the first line of the zone.

You may also need to add
  (autoload 'zone-mode \"zone-mode\" "")

Font-lock support assumes emacs-20.
"

  (require 'zone-mode)
  (add-hook 'write-file-hooks 'zone-mode-update-serial-hook)

  (if (null zone-mode-syntax-table)
      (zone-mode-load-time-setup)) ;; should have been run at load-time

  ;; font-lock support:
  (set-syntax-table zone-mode-syntax-table)
  (make-local-variable 'font-lock-defaults)
  (setq font-lock-defaults
  '(nil nil nil nil beginning-of-line)))

(zone-mode-load-time-setup)

(provide 'zone-mode)

------- End of forwarded message -------