| 1 | ;;; zone-mode.el --- major mode for editing DNS zone files |
| 2 | |
| 3 | ;; Copyright (C) 1998, 2002, 2003, 2004, 2005, |
| 4 | ;; 2006 Free Software Foundation, Inc. |
| 5 | |
| 6 | ;; Author: John Heidemann <johnh@isi.edu> |
| 7 | ;; Keywords: DNS, languages |
| 8 | |
| 9 | ;; This file is part of GNU Emacs. |
| 10 | |
| 11 | ;; GNU Emacs is free software; you can redistribute it and/or modify |
| 12 | ;; it under the terms of the GNU General Public License as published by |
| 13 | ;; the Free Software Foundation; either version 2, or (at your option) |
| 14 | ;; any later version. |
| 15 | |
| 16 | ;; GNU Emacs is distributed in the hope that it will be useful, |
| 17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | ;; GNU General Public License for more details. |
| 20 | |
| 21 | ;; You should have received a copy of the GNU General Public License |
| 22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
| 23 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 24 | ;; Boston, MA 02110-1301, USA. |
| 25 | |
| 26 | ;;; Commentary: |
| 27 | |
| 28 | ;;; |
| 29 | ;;; See the comments in ``define-derived-mode zone-mode'' |
| 30 | ;;; (the last function in this file) |
| 31 | ;;; for what this mode is and how to use it automatically. |
| 32 | ;;; |
| 33 | |
| 34 | ;;; |
| 35 | ;;; Credits: |
| 36 | ;;; Zone-mode was written by John Heidemann <johnh@isi.edu>, |
| 37 | ;;; with bug fixes from Simon Leinen <simon@limmat.switch.ch>. |
| 38 | ;;; |
| 39 | |
| 40 | ;;; Code: |
| 41 | |
| 42 | (defun zone-mode-update-serial () |
| 43 | "Update the serial number in a zone." |
| 44 | (interactive) |
| 45 | (save-excursion |
| 46 | (goto-char (point-min)) |
| 47 | (while (re-search-forward "\\b\\([0-9]+\\)\\([0-9][0-9]\\)\\([ \t]+;[ \t]+[Ss]erial\\)" (point-max) t) |
| 48 | (let* ((old-date (match-string 1)) |
| 49 | (old-seq (match-string 2)) |
| 50 | (old-seq-num (string-to-number (match-string 2))) |
| 51 | (old-flag (match-string 3)) |
| 52 | (cur-date (format-time-string "%Y%m%d")) |
| 53 | (new-seq |
| 54 | (cond |
| 55 | ((not (string= old-date cur-date)) |
| 56 | "00") ;; reset sequence number |
| 57 | ((>= old-seq-num 99) |
| 58 | (error "Serial number's sequence cannot increment beyond 99")) |
| 59 | (t |
| 60 | (format "%02d" (1+ old-seq-num))))) |
| 61 | (old-serial (concat old-date old-seq)) |
| 62 | (new-serial (concat cur-date new-seq))) |
| 63 | (if (string-lessp new-serial old-serial) |
| 64 | (error "Serial numbers want to move backwards from %s to %s" old-serial new-serial) |
| 65 | (replace-match (concat cur-date new-seq old-flag) t t)))))) |
| 66 | |
| 67 | ;;;###autoload |
| 68 | (defun zone-mode-update-serial-hook () |
| 69 | "Update the serial number in a zone if the file was modified." |
| 70 | (interactive) |
| 71 | (if (buffer-modified-p (current-buffer)) |
| 72 | (zone-mode-update-serial)) |
| 73 | nil ;; so we can run from write-file-hooks |
| 74 | ) |
| 75 | |
| 76 | (defvar zone-mode-syntax-table nil |
| 77 | "Zone-mode's syntax table.") |
| 78 | |
| 79 | (defun zone-mode-load-time-setup () |
| 80 | "Initialize `zone-mode' stuff." |
| 81 | (setq zone-mode-syntax-table (make-syntax-table)) |
| 82 | (modify-syntax-entry ?\; "<" zone-mode-syntax-table) |
| 83 | (modify-syntax-entry ?\n ">" zone-mode-syntax-table)) |
| 84 | |
| 85 | ;;;###autoload |
| 86 | (define-derived-mode zone-mode fundamental-mode "zone" |
| 87 | "A mode for editing DNS zone files. |
| 88 | |
| 89 | Zone-mode does two things: |
| 90 | |
| 91 | - automatically update the serial number for a zone |
| 92 | when saving the file |
| 93 | |
| 94 | - fontification" |
| 95 | |
| 96 | (add-hook 'write-file-functions 'zone-mode-update-serial-hook nil t) |
| 97 | |
| 98 | (if (null zone-mode-syntax-table) |
| 99 | (zone-mode-load-time-setup)) ;; should have been run at load-time |
| 100 | |
| 101 | ;; font-lock support: |
| 102 | (set-syntax-table zone-mode-syntax-table) |
| 103 | (make-local-variable 'comment-start) |
| 104 | (setq comment-start ";") |
| 105 | (make-local-variable 'comment-start-skip) |
| 106 | ;; Look within the line for a ; following an even number of backslashes |
| 107 | ;; after either a non-backslash or the line beginning. |
| 108 | (setq comment-start-skip "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+[ \t]*") |
| 109 | (make-local-variable 'comment-column) |
| 110 | (setq comment-column 40) |
| 111 | (make-local-variable 'font-lock-defaults) |
| 112 | (setq font-lock-defaults |
| 113 | '(nil nil nil nil beginning-of-line))) |
| 114 | |
| 115 | (zone-mode-load-time-setup) |
| 116 | |
| 117 | (provide 'zone-mode) |
| 118 | |
| 119 | ;;; arch-tag: 6a2940ef-fd4f-4de7-b979-b027b09821fe |
| 120 | ;;; zone-mode.el ends here |