Commit | Line | Data |
---|---|---|
e8af40ee | 1 | ;;; zone-mode.el --- major mode for editing DNS zone files |
8749abea | 2 | |
ceb4c4d3 TTN |
3 | ;; Copyright (C) 1998, 2002, 2003, 2004, 2005, |
4 | ;; 2006 Free Software Foundation, Inc. | |
8749abea GM |
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 | |
3a35cf56 LK |
23 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
24 | ;; Boston, MA 02110-1301, USA. | |
8749abea | 25 | |
e8af40ee PJ |
26 | ;;; Commentary: |
27 | ||
8749abea GM |
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)) | |
283b4456 | 56 | "00") ;; reset sequence number |
8749abea | 57 | ((>= old-seq-num 99) |
283b4456 | 58 | (error "Serial number's sequence cannot increment beyond 99")) |
8749abea GM |
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) | |
ec3ec9cc | 64 | (error "Serial numbers want to move backwards from %s to %s" old-serial new-serial) |
8749abea | 65 | (replace-match (concat cur-date new-seq old-flag) t t)))))) |
a1506d29 | 66 | |
8749abea GM |
67 | ;;;###autoload |
68 | (defun zone-mode-update-serial-hook () | |
283b4456 | 69 | "Update the serial number in a zone if the file was modified." |
8749abea GM |
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 () | |
325b701b | 80 | "Initialize `zone-mode' stuff." |
8749abea GM |
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 | ||
d42f9038 | 96 | (add-hook 'write-file-functions 'zone-mode-update-serial-hook nil t) |
8749abea GM |
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 | ||
ab5796a9 | 119 | ;;; arch-tag: 6a2940ef-fd4f-4de7-b979-b027b09821fe |
8749abea | 120 | ;;; zone-mode.el ends here |