Trim whitespace in lm-keywords-list
[emacs.git] / lisp / textmodes / dns-mode.el
blobfe7ae17373c5e548b06c76e66202eccbf7509ef3
1 ;;; dns-mode.el --- a mode for viewing/editing Domain Name System master files
3 ;; Copyright (C) 2000-2001, 2004-2013 Free Software Foundation, Inc.
5 ;; Author: Simon Josefsson <simon@josefsson.org>
6 ;; Keywords: DNS master zone file SOA comm
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; Use M-x dns-mode RET to invoke in master files.
27 ;; C-c C-s Increment SOA serial.
28 ;; Understands YYYYMMDDNN, Unix time, and serial number formats,
29 ;; and complains if it fail to find SOA serial.
31 ;;; References:
33 ;; RFC 1034, "DOMAIN NAMES - CONCEPTS AND FACILITIES", P. Mockapetris.
34 ;; RFC 1035, "DOMAIN NAMES - IMPLEMENTATION AND SPECIFICATION", P. Mockapetris.
36 ;;; Release history:
38 ;; 2004-09-11 Posted on gnu.emacs.sources.
39 ;; 2004-09-13 Ported to XEmacs.
40 ;; 2004-09-14 Installed in Emacs CVS.
42 ;;; Code:
44 (defgroup dns-mode nil
45 "DNS master file mode configuration."
46 :group 'data)
48 (defconst dns-mode-classes '("IN" "CS" "CH" "HS")
49 "List of strings with known DNS classes.")
51 (defconst dns-mode-types '("A" "NS" "MD" "MF" "CNAME" "SOA" "MB" "MG" "MR"
52 "NULL" "WKS" "PTR" "HINFO" "MINFO" "MX" "TXT"
53 "RP" "AFSDB" "X25" "ISDN" "RT" "NSAP" "NSAP"
54 "SIG" "KEY" "PX" "GPOS" "AAAA" "LOC" "NXT"
55 "EID" "NIMLOC" "SRV" "ATMA" "NAPTR" "KX" "CERT"
56 "A6" "DNAME" "SINK" "OPT" "APL" "DS" "SSHFP"
57 "RRSIG" "NSEC" "DNSKEY" "UINFO" "UID" "GID"
58 "UNSPEC" "TKEY" "TSIG" "IXFR" "AXFR" "MAILB"
59 "MAILA")
60 "List of strings with known DNS types.")
62 ;; Font lock.
64 (defvar dns-mode-control-entity-face 'font-lock-keyword-face
65 "Name of face used for control entities, e.g. $ORIGIN.")
67 (defvar dns-mode-bad-control-entity-face 'font-lock-warning-face
68 "Name of face used for non-standard control entities, e.g. $FOO.")
70 (defvar dns-mode-type-face 'font-lock-type-face
71 "Name of face used for DNS types, e.g., SOA.")
73 (defvar dns-mode-class-face 'font-lock-constant-face
74 "Name of face used for DNS classes, e.g., IN.")
76 (defcustom dns-mode-font-lock-keywords
77 `(("^$ORIGIN" 0 ,dns-mode-control-entity-face)
78 ("^$INCLUDE" 0 ,dns-mode-control-entity-face)
79 ("^$[a-z0-9A-Z]+" 0 ,dns-mode-bad-control-entity-face)
80 (,(regexp-opt dns-mode-classes) 0 ,dns-mode-class-face)
81 (,(regexp-opt dns-mode-types) 0 ,dns-mode-type-face))
82 "Font lock keywords used to highlight text in DNS master file mode."
83 :type 'sexp
84 :group 'dns-mode)
86 (defcustom dns-mode-soa-auto-increment-serial t
87 "Whether to increment the SOA serial number automatically.
89 If this variable is t, the serial number is incremented upon each save of
90 the file. If it is `ask', Emacs asks for confirmation whether it should
91 increment the serial upon saving. If nil, serials must be incremented
92 manually with \\[dns-mode-soa-increment-serial]."
93 :type '(choice (const :tag "Always" t)
94 (const :tag "Ask" ask)
95 (const :tag "Never" nil))
96 :group 'dns-mode)
98 ;; Syntax table.
100 (defvar dns-mode-syntax-table
101 (let ((table (make-syntax-table)))
102 (modify-syntax-entry ?\; "< " table)
103 (modify-syntax-entry ?\n "> " table)
104 table)
105 "Syntax table in use in DNS master file buffers.")
107 ;; Keymap.
109 (defvar dns-mode-map
110 (let ((map (make-sparse-keymap)))
111 (define-key map "\C-c\C-s" 'dns-mode-soa-increment-serial)
112 map)
113 "Keymap for DNS master file mode.")
115 ;; Menu.
117 (defvar dns-mode-menu nil
118 "Menubar used in DNS master file mode.")
120 (easy-menu-define dns-mode-menu dns-mode-map
121 "DNS Menu."
122 '("DNS"
123 ["Increment SOA serial" dns-mode-soa-increment-serial t]))
125 ;; Mode.
127 ;;;###autoload
128 (define-derived-mode dns-mode text-mode "DNS"
129 "Major mode for viewing and editing DNS master files.
130 This mode is inherited from text mode. It add syntax
131 highlighting, and some commands for handling DNS master files.
132 Its keymap inherits from `text-mode' and it has the same
133 variables for customizing indentation. It has its own abbrev
134 table and its own syntax table.
136 Turning on DNS mode runs `dns-mode-hook'."
137 (set (make-local-variable 'comment-start) ";")
138 (set (make-local-variable 'comment-end) "")
139 (set (make-local-variable 'comment-start-skip) ";+ *")
140 (unless (featurep 'xemacs)
141 (set (make-local-variable 'font-lock-defaults)
142 '(dns-mode-font-lock-keywords nil nil ((?_ . "w")))))
143 (add-hook 'before-save-hook 'dns-mode-soa-maybe-increment-serial
144 nil t)
145 (easy-menu-add dns-mode-menu dns-mode-map))
147 ;;;###autoload (defalias 'zone-mode 'dns-mode)
149 ;; Tools.
151 ;;;###autoload
152 (defun dns-mode-soa-increment-serial ()
153 "Locate SOA record and increment the serial field."
154 (interactive)
155 (save-excursion
156 (goto-char (point-min))
157 (unless (re-search-forward
158 (concat "^\\(\\(\\([^ \t]+[ \t]+\\)?[^ \t]+"
159 "[ \t]+\\)?[^ \t]+[ \t]+\\)?SOA") nil t)
160 (error "Cannot locate SOA record"))
161 (if (re-search-forward (concat "\\<\\("
162 ;; year
163 "\\(198\\|199\\|20[0-9]\\)[0-9]"
164 ;; month
165 "\\(0[0-9]\\|10\\|11\\|12\\)"
166 ;; day
167 "\\([012][0-9]\\|30\\|31\\)"
168 ;; counter
169 "\\([0-9]\\{1,3\\}\\)"
170 "\\)\\>")
171 nil t)
172 ;; YYYYMMDDIII format, one to three I's.
173 (let* ((serial (match-string 1))
174 (counterstr (match-string 5))
175 (counter (string-to-number counterstr))
176 (now (format-time-string "%Y%m%d"))
177 (nowandoldserial (concat now counterstr)))
178 (if (string< serial nowandoldserial)
179 (let ((new (format "%s00" now)))
180 (replace-match new nil nil nil 1)
181 (message "Replaced old serial %s with %s" serial new))
182 (if (string= serial nowandoldserial)
183 (let ((new (format (format "%%s%%0%dd" (length counterstr))
184 now (1+ counter))))
185 (replace-match new nil nil nil 1)
186 (message "Replaced old serial %s with %s" serial new))
187 (error "Current SOA serial is in the future"))))
188 (if (re-search-forward "\\<\\([0-9]\\{9,10\\}\\)\\>" nil t)
189 ;; Unix time
190 (let* ((serial (match-string 1))
191 (new (format-time-string "%s")))
192 (if (not (string< serial new))
193 (error "Current SOA serial is in the future")
194 (replace-match new nil nil nil 1)
195 (message "Replaced old serial %s with %s" serial new)))
196 (if (re-search-forward "\\<\\([0-9]+\\)\\>" nil t)
197 ;; Just any serial number.
198 (let* ((serial (match-string 1))
199 (new (format "%d" (1+ (string-to-number serial)))))
200 (replace-match new nil nil nil 1)
201 (message "Replaced old serial %s with %s" serial new))
202 (error "Cannot locate serial number in SOA record"))))))
204 (defun dns-mode-soa-maybe-increment-serial ()
205 "Increment SOA serial if needed.
207 This function is run from `before-save-hook'."
208 (when (and (buffer-modified-p)
209 dns-mode-soa-auto-increment-serial
210 (or (eq dns-mode-soa-auto-increment-serial t)
211 (y-or-n-p "Increment SOA serial? ")))
212 ;; If `dns-mode-soa-increment-serial' signals an error saving will
213 ;; fail but that probably means that the serial should be fixed to
214 ;; comply with the RFC anyway! -rfr
215 (progn (dns-mode-soa-increment-serial)
216 ;; We return nil in case this is used in write-contents-functions.
217 nil)))
219 (provide 'dns-mode)
221 ;;; dns-mode.el ends here