Commit | Line | Data |
---|---|---|
e8af40ee | 1 | ;;; snmp-mode.el --- SNMP & SNMPv2 MIB major mode |
8749abea GM |
2 | |
3 | ;; Copyright (C) 1995,1998 Free Software Foundation, Inc. | |
4 | ||
5 | ;; Author: Paul D. Smith <psmith@BayNetworks.com> | |
6 | ;; Keywords: data | |
7 | ||
8 | ;; This file is part of GNU Emacs. | |
9 | ||
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 2, or (at your option) | |
13 | ;; any later version. | |
14 | ||
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. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
21 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
22 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 | ;; Boston, MA 02111-1307, USA. | |
24 | ||
e8af40ee PJ |
25 | ;;; Commentary: |
26 | ||
8749abea GM |
27 | ;; INTRODUCTION |
28 | ;; ------------ | |
29 | ;; This package provides a major mode for editing SNMP MIBs. It | |
30 | ;; provides all the modern Emacs 19 bells and whistles: default | |
31 | ;; fontification via font-lock, imenu search functions, etc. | |
32 | ;; | |
33 | ;; SNMP mode also uses tempo, a textual boilerplate insertion package | |
34 | ;; distributed with Emacs, to add in boilerplate SNMP MIB structures. | |
35 | ;; See tempo.el for more details about tempo. | |
36 | ;; | |
37 | ;; If you want to change or add new tempo templates, use the tempo tag | |
38 | ;; list `snmp-tempo-tags' (or `snmpv2-tempo-tags'): this list is | |
39 | ;; automatically installed when snmp-mode (or snmpv2-mode) is entered. | |
40 | ;; | |
41 | ;; The SNMPv2 mode in this version has been enhanced thanks to popular | |
42 | ;; demand. | |
43 | ;; | |
44 | ;; I'm very interested in new tempo macros for both v1 and v2, and any | |
45 | ;; other suggestions for enhancements (different syntax table items, new | |
46 | ;; keybindings, etc.) | |
47 | ;; | |
48 | ;; | |
49 | ;; USAGE | |
50 | ;; ----- | |
51 | ;; Mostly, use it as you would any other mode. There's a very | |
52 | ;; simplistic auto-indent feature; hopefully it'll help more than get in | |
53 | ;; your way. For the most part it tries to indent to the same level as | |
54 | ;; the previous line. It will try to recognize some very simple tokens | |
55 | ;; on the previous line that tell it to use extra indent or outdent. | |
56 | ;; | |
57 | ;; Templates | |
58 | ;; --------- | |
59 | ;; To use the Tempo templates, type the Tempo tag (or a unique prefix) | |
60 | ;; and use C-c C-i (C-c TAB) to complete it; if you don't have | |
61 | ;; tempo-interactive set to nil it will ask you to fill in values. | |
62 | ;; Fields with predefined values (SYNTAX, STATUS, etc.) will do | |
63 | ;; completing-reads on a list of valid values; use the normal SPC or TAB | |
64 | ;; to complete. | |
65 | ;; | |
66 | ;; Currently the following templates are available: | |
67 | ;; | |
68 | ;; objectType -- Defines an OBJECT-TYPE macro. | |
69 | ;; | |
70 | ;; tableType -- Defines both a Table and Entry OBJECT-TYPE, and a | |
71 | ;; SEQUENCE for the ASN.1 Entry definition. | |
72 | ;; | |
73 | ;; Once the template is done, you can use C-cC-f and C-cC-b to move back | |
74 | ;; and forth between the Tempo sequence points to fill in the rest of | |
75 | ;; the information. | |
76 | ;; | |
77 | ;; Font Lock | |
78 | ;; ------------ | |
79 | ;; | |
80 | ;; If you want font-lock in your MIB buffers, add this: | |
81 | ;; | |
82 | ;; (add-hook 'snmp-common-mode-hook 'turn-on-font-lock) | |
83 | ;; | |
84 | ;; Enabling global-font-lock-mode is also sufficient. | |
85 | ;; | |
86 | ||
e8af40ee PJ |
87 | ;;; Code: |
88 | ||
8749abea GM |
89 | ;;;---------------------------------------------------------------------------- |
90 | ;; | |
91 | ;; Customize these: | |
92 | ;; | |
93 | ;;;---------------------------------------------------------------------------- | |
94 | ||
95 | (defgroup snmp nil | |
96 | "Mode for editing SNMP MIB files." | |
97 | :group 'data | |
98 | :version "20.4") | |
99 | ||
100 | (defcustom snmp-special-indent t | |
101 | "*If non-nil, use a simple heuristic to try to guess the right indentation. | |
102 | If nil, then no special indentation is attempted." | |
103 | :type 'boolean | |
104 | :group 'snmp) | |
105 | ||
106 | (defcustom snmp-indent-level 4 | |
107 | "*Indentation level for SNMP MIBs." | |
108 | :type 'integer | |
109 | :group 'snmp) | |
110 | ||
111 | (defcustom snmp-tab-always-indent nil | |
112 | "*Non-nil means TAB should always reindent the current line. | |
113 | A value of nil means reindent if point is within the initial line indentation; | |
114 | otherwise insert a TAB." | |
115 | :type 'boolean | |
116 | :group 'snmp) | |
117 | ||
118 | (defcustom snmp-completion-ignore-case t | |
119 | "*Non-nil means that case differences are ignored during completion. | |
120 | A value of nil means that case is significant. | |
121 | This is used during Tempo template completion." | |
122 | :type 'boolean | |
123 | :group 'snmp) | |
124 | ||
125 | (defcustom snmp-common-mode-hook nil | |
126 | "*Hook(s) evaluated when a buffer enters either SNMP or SNMPv2 mode." | |
127 | :type 'hook | |
128 | :group 'snmp) | |
129 | ||
130 | (defcustom snmp-mode-hook nil | |
131 | "*Hook(s) evaluated when a buffer enters SNMP mode." | |
132 | :type 'hook | |
133 | :group 'snmp) | |
134 | ||
135 | (defcustom snmpv2-mode-hook nil | |
136 | "*Hook(s) evaluated when a buffer enters SNMPv2 mode." | |
137 | :type 'hook | |
138 | :group 'snmp) | |
139 | ||
140 | (defvar snmp-tempo-tags nil | |
141 | "*Tempo tags for SNMP mode.") | |
142 | ||
143 | (defvar snmpv2-tempo-tags nil | |
144 | "*Tempo tags for SNMPv2 mode.") | |
145 | ||
146 | ||
147 | ;; Enable fontification for SNMP MIBs | |
148 | ;; | |
149 | ||
150 | ;; These are pretty basic fontifications. Note we assume these macros | |
151 | ;; are first on a line (except whitespace), to speed up fontification. | |
152 | ;; | |
153 | (defvar snmp-font-lock-keywords-1 | |
154 | (list | |
155 | ;; OBJECT-TYPE, TRAP-TYPE, and OBJECT-IDENTIFIER macros | |
156 | '("^[ \t]*\\([a-z][-a-zA-Z0-9]+\\)[ \t]+\\(\\(MODULE-\\(COMPLIANCE\\|IDENTITY\\)\\|OBJECT-\\(COMPLIANCE\\|GROUP\\|IDENTITY\\|TYPE\\)\\|TRAP-\\(GROUP\\|TYPE\\)\\)\\|\\(OBJECT\\)[ \t]+\\(IDENTIFIER\\)[ \t]*::=\\)" | |
157 | (1 font-lock-variable-name-face) (3 font-lock-keyword-face nil t) | |
158 | (7 font-lock-keyword-face nil t) (8 font-lock-keyword-face nil t)) | |
159 | ||
160 | ;; DEFINITIONS clause | |
161 | '("^[ \t]*\\([A-Z][-a-zA-Z0-9]+\\)[ \t]+\\(DEFINITIONS\\)[ \t]*::=" | |
162 | (1 font-lock-function-name-face) (2 font-lock-keyword-face)) | |
163 | ) | |
164 | "Basic SNMP MIB mode expression highlighting.") | |
165 | ||
166 | (defvar snmp-font-lock-keywords-2 | |
167 | (append | |
168 | '(("ACCESS\\|BEGIN\\|DE\\(FVAL\\|SCRIPTION\\)\\|END\\|FROM\\|I\\(MPORTS\\|NDEX\\)\\|S\\(TATUS\\|YNTAX\\)" | |
169 | (0 font-lock-keyword-face))) | |
170 | snmp-font-lock-keywords-1) | |
171 | "Medium SNMP MIB mode expression highlighting.") | |
172 | ||
173 | (defvar snmp-font-lock-keywords-3 | |
174 | (append | |
175 | '(("\\([^\n]+\\)[ \t]+::=[ \t]+\\(SEQUENCE\\)[ \t]+{" | |
176 | (1 font-lock-reference-face) (2 font-lock-keyword-face)) | |
177 | ("::=[ \t]*{[ \t]*\\([a-z0-9].*[ \t]+\\)?\\([0-9]+\\)[ \t]*}" | |
178 | (1 font-lock-reference-face nil t) (2 font-lock-variable-name-face))) | |
179 | snmp-font-lock-keywords-2) | |
180 | "Gaudy SNMP MIB mode expression highlighting.") | |
181 | ||
182 | (defvar snmp-font-lock-keywords snmp-font-lock-keywords-1 | |
183 | "Default SNMP MIB mode expression highlighting.") | |
184 | ||
185 | ||
186 | ;; These lists are used for the completion capabilities in the tempo | |
187 | ;; templates. | |
188 | ;; | |
189 | ||
190 | (defvar snmp-mode-syntax-list nil | |
191 | "Predefined types for SYNTAX clauses.") | |
192 | ||
193 | (defvar snmp-rfc1155-types | |
194 | '(("INTEGER") ("OCTET STRING") ("OBJECT IDENTIFIER") ("NULL") ("IpAddress") | |
195 | ("NetworkAddress") ("Counter") ("Gauge") ("TimeTicks") ("Opaque")) | |
196 | "Types from RFC 1155 v1 SMI.") | |
197 | ||
198 | (defvar snmp-rfc1213-types | |
199 | '(("DisplayString")) | |
200 | "Types from RFC 1213 MIB-II.") | |
201 | ||
202 | (defvar snmp-rfc1902-types | |
203 | '(("INTEGER") ("OCTET STRING") ("OBJECT IDENTIFIER") ("Integer32") | |
204 | ("IpAddress") ("Counter32") ("Gauge32") ("Unsigned32") ("TimeTicks") | |
205 | ("Opaque") ("Counter64")) | |
206 | "Types from RFC 1902 v2 SMI.") | |
207 | ||
208 | (defvar snmp-rfc1903-types | |
209 | '(("DisplayString") ("PhysAddress") ("MacAddress") ("TruthValue") | |
210 | ("TestAndIncr") ("AutonomousType") ("InstancePointer") | |
211 | ("VariablePointer") ("RowPointer") ("RowStatus") ("TimeStamp") | |
212 | ("TimeInterval") ("DateAndTime") ("StorageType") ("TDomain") | |
213 | ("TAddress")) | |
214 | "Types from RFC 1903 Textual Conventions.") | |
215 | ||
216 | ||
217 | (defvar snmp-mode-access-list nil | |
218 | "Predefined values for ACCESS clauses.") | |
219 | ||
220 | (defvar snmp-rfc1155-access | |
221 | '(("read-only") ("read-write") ("write-only") ("not-accessible")) | |
222 | "ACCESS values from RFC 1155 v1 SMI.") | |
223 | ||
224 | (defvar snmp-rfc1902-access | |
225 | '(("read-only") ("read-write") ("read-create") ("not-accessible") | |
226 | ("accessible-for-notify")) | |
227 | "ACCESS values from RFC 1155 v1 SMI.") | |
228 | ||
229 | ||
230 | (defvar snmp-mode-status-list nil | |
231 | "Predefined values for STATUS clauses.") | |
232 | ||
233 | (defvar snmp-rfc1212-status | |
234 | '(("mandatory") ("obsolete") ("deprecated")) | |
235 | "STATUS values from RFC 1212 v1 SMI.") | |
236 | ||
237 | (defvar snmp-rfc1902-status | |
238 | '(("current") ("obsolete") ("deprecated")) | |
239 | "STATUS values from RFC 1902 v2 SMI.") | |
240 | ||
241 | ||
242 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
243 | ;;;---------------------------------------------------------------------------- | |
244 | ;; | |
245 | ;; Nothing to customize below here. | |
246 | ;; | |
247 | ;;;---------------------------------------------------------------------------- | |
248 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
249 | ||
250 | ||
251 | ;; Need this stuff when compiling for imenu macros, etc. | |
252 | ;; | |
253 | (eval-when-compile | |
254 | (require 'cl) | |
255 | (require 'imenu)) | |
256 | ||
257 | ||
258 | ;; Create abbrev table for SNMP MIB mode | |
259 | ;; | |
260 | (defvar snmp-mode-abbrev-table nil | |
261 | "Abbrev table in use in SNMP mode.") | |
262 | (define-abbrev-table 'snmp-mode-abbrev-table ()) | |
263 | ||
264 | ||
265 | ;; Create abbrev table for SNMPv2 mode | |
266 | ;; | |
267 | (defvar snmpv2-mode-abbrev-table nil | |
268 | "Abbrev table in use in SNMPv2 mode.") | |
269 | (define-abbrev-table 'snmpv2-mode-abbrev-table ()) | |
270 | ||
271 | ||
272 | ;; Set up our keymap | |
273 | ;; | |
274 | (defvar snmp-mode-map (make-sparse-keymap) | |
275 | "Keymap used in SNMP mode.") | |
276 | ||
277 | (define-key snmp-mode-map "\t" 'snmp-indent-command) | |
278 | (define-key snmp-mode-map "\177" 'backward-delete-char-untabify) | |
279 | ||
280 | (define-key snmp-mode-map "\C-c\C-i" 'tempo-complete-tag) | |
281 | (define-key snmp-mode-map "\C-c\C-f" 'tempo-forward-mark) | |
282 | (define-key snmp-mode-map "\C-c\C-b" 'tempo-backward-mark) | |
283 | ||
284 | ||
285 | ;; Set up our syntax table | |
286 | ;; | |
287 | (defvar snmp-mode-syntax-table nil | |
288 | "Syntax table used for buffers in SNMP mode.") | |
289 | ||
290 | (if snmp-mode-syntax-table | |
291 | () | |
292 | (setq snmp-mode-syntax-table (make-syntax-table)) | |
293 | (modify-syntax-entry ?\\ "\\" snmp-mode-syntax-table) | |
294 | (modify-syntax-entry ?- "_ 1234" snmp-mode-syntax-table) | |
295 | (modify-syntax-entry ?\n ">" snmp-mode-syntax-table) | |
296 | (modify-syntax-entry ?\^m ">" snmp-mode-syntax-table) | |
297 | (modify-syntax-entry ?_ "." snmp-mode-syntax-table) | |
298 | (modify-syntax-entry ?: "." snmp-mode-syntax-table) | |
299 | (modify-syntax-entry ?= "." snmp-mode-syntax-table)) | |
300 | ||
301 | ;; Set up the stuff that's common between snmp-mode and snmpv2-mode | |
302 | ;; | |
303 | (defun snmp-common-mode (name mode abbrev font-keywords imenu-index tempo-tags) | |
304 | (kill-all-local-variables) | |
305 | ||
306 | ;; Become the current major mode | |
307 | (setq mode-name name) | |
308 | (setq major-mode mode) | |
309 | ||
310 | ;; Activate keymap, syntax table, and abbrev table | |
311 | (use-local-map snmp-mode-map) | |
312 | (set-syntax-table snmp-mode-syntax-table) | |
313 | (setq local-abbrev-table abbrev) | |
314 | ||
315 | ;; Set up paragraphs (?) | |
316 | (make-local-variable 'paragraph-start) | |
317 | (setq paragraph-start (concat "$\\|" page-delimiter)) | |
318 | (make-local-variable 'paragraph-separate) | |
319 | (setq paragraph-separate paragraph-start) | |
320 | (make-local-variable 'paragraph-ignore-fill-prefix) | |
321 | (setq paragraph-ignore-fill-prefix t) | |
322 | ||
323 | ;; Set up comments | |
324 | (make-local-variable 'comment-start) | |
325 | (setq comment-start "-- ") | |
326 | (make-local-variable 'comment-start-skip) | |
327 | (setq comment-start-skip "--+[ \t]*") | |
328 | (make-local-variable 'comment-column) | |
329 | (setq comment-column 40) | |
330 | (make-local-variable 'parse-sexp-ignore-comments) | |
331 | (setq parse-sexp-ignore-comments t) | |
332 | ||
333 | ;; Set up indentation | |
334 | (make-local-variable 'indent-line-function) | |
335 | (setq indent-line-function (if snmp-special-indent | |
336 | 'snmp-indent-line | |
337 | 'indent-to-left-margin)) | |
338 | ||
339 | ;; Font Lock | |
340 | (make-local-variable 'font-lock-defaults) | |
341 | (setq font-lock-defaults (cons font-keywords '(nil nil ((?- . "w 1234"))))) | |
342 | ||
343 | ;; Imenu | |
344 | (make-local-variable 'imenu-create-index-function) | |
345 | (setq imenu-create-index-function imenu-index) | |
346 | ||
347 | ;; Tempo | |
348 | (tempo-use-tag-list tempo-tags) | |
349 | (make-local-variable 'tempo-match-finder) | |
350 | (setq tempo-match-finder "\\b\\(.+\\)\\=") | |
351 | (make-local-variable 'tempo-interactive) | |
352 | (setq tempo-interactive t) | |
353 | ||
354 | ;; Miscellaneous customization | |
355 | (make-local-variable 'require-final-newline) | |
356 | (setq require-final-newline t)) | |
357 | ||
358 | ||
359 | ;; SNMPv1 MIB Editing Mode. | |
360 | ;; | |
361 | ;;;###autoload | |
362 | (defun snmp-mode () | |
363 | "Major mode for editing SNMP MIBs. | |
364 | Expression and list commands understand all C brackets. | |
365 | Tab indents for C code. | |
366 | Comments start with -- and end with newline or another --. | |
367 | Delete converts tabs to spaces as it moves back. | |
368 | \\{snmp-mode-map} | |
369 | Turning on snmp-mode runs the hooks in `snmp-common-mode-hook', then | |
370 | `snmp-mode-hook'." | |
371 | (interactive) | |
372 | ||
373 | (snmp-common-mode "SNMP" 'snmp-mode | |
374 | snmp-mode-abbrev-table | |
375 | '(snmp-font-lock-keywords | |
376 | snmp-font-lock-keywords-1 | |
377 | snmp-font-lock-keywords-2 | |
378 | snmp-font-lock-keywords-3) | |
379 | 'snmp-mode-imenu-create-index | |
380 | 'snmp-tempo-tags) | |
381 | ||
382 | ;; Completion lists | |
383 | (make-local-variable 'snmp-mode-syntax-list) | |
384 | (setq snmp-mode-syntax-list (append snmp-rfc1155-types | |
385 | snmp-rfc1213-types | |
386 | snmp-mode-syntax-list)) | |
387 | (make-local-variable 'snmp-mode-access-list) | |
388 | (setq snmp-mode-access-list snmp-rfc1155-access) | |
389 | (make-local-variable 'snmp-mode-status-list) | |
390 | (setq snmp-mode-status-list snmp-rfc1212-status) | |
391 | ||
392 | ;; Run hooks | |
393 | (run-hooks 'snmp-common-mode-hook) | |
394 | (run-hooks 'snmp-mode-hook)) | |
395 | ||
396 | ||
397 | ;;;###autoload | |
398 | (defun snmpv2-mode () | |
399 | "Major mode for editing SNMPv2 MIBs. | |
400 | Expression and list commands understand all C brackets. | |
401 | Tab indents for C code. | |
402 | Comments start with -- and end with newline or another --. | |
403 | Delete converts tabs to spaces as it moves back. | |
404 | \\{snmp-mode-map} | |
405 | Turning on snmp-mode runs the hooks in `snmp-common-mode-hook', | |
406 | then `snmpv2-mode-hook'." | |
407 | (interactive) | |
408 | ||
409 | (snmp-common-mode "SNMPv2" 'snmpv2-mode | |
410 | snmpv2-mode-abbrev-table | |
411 | '(snmp-font-lock-keywords | |
412 | snmp-font-lock-keywords-1 | |
413 | snmp-font-lock-keywords-2 | |
414 | snmp-font-lock-keywords-3) | |
415 | 'snmp-mode-imenu-create-index | |
416 | 'snmpv2-tempo-tags) | |
417 | ||
418 | ;; Completion lists | |
419 | (make-local-variable 'snmp-mode-syntax-list) | |
420 | (setq snmp-mode-syntax-list (append snmp-rfc1902-types | |
421 | snmp-rfc1903-types | |
422 | snmp-mode-syntax-list)) | |
423 | (make-local-variable 'snmp-mode-access-list) | |
424 | (setq snmp-mode-access-list snmp-rfc1902-access) | |
425 | (make-local-variable 'snmp-mode-status-list) | |
426 | (setq snmp-mode-status-list snmp-rfc1902-status) | |
427 | ||
428 | ;; Run hooks | |
429 | (run-hooks 'snmp-common-mode-hook) | |
430 | (run-hooks 'snmpv2-mode-hook)) | |
431 | ||
432 | ||
433 | ;;;---------------------------------------------------------------------------- | |
434 | ;; | |
435 | ;; Indentation Setup | |
436 | ;; | |
437 | ;;;---------------------------------------------------------------------------- | |
438 | ||
439 | (defvar snmp-macro-open | |
440 | "[a-zA-Z][-a-zA-Z0-9]*[ \t]*\\(OBJECT\\|TRAP\\)-\\(TYPE\\|GROUP\\)\ | |
441 | \\|DESCRIPTION\\|IMPORTS\\|MODULE\\(-IDENTITY\\|-COMPLIANCE\\)\ | |
442 | \\|.*::=[ \t]*\\(BEGIN\\|TEXTUAL-CONVENTION\\)[ \t]*$") | |
443 | ||
444 | (defvar snmp-macro-close | |
445 | "::=[ \t]*{\\|\\(END\\|.*[;\"]\\)[ \t]*$") | |
446 | ||
447 | (defun snmp-calculate-indent () | |
448 | "Calculate the current line indentation in SNMP MIB code. | |
449 | ||
450 | We use a very simple scheme: if the previous non-empty line was a \"macro | |
451 | open\" string, add `snmp-indent-level' to it. If it was a \"macro close\" | |
452 | string, subtract `snmp-indent-level'. Otherwise, use the same indentation | |
453 | as the previous non-empty line. Note comments are considered empty | |
454 | lines for the purposes of this function." | |
455 | (let ((empty (concat "\\([ \t]*\\)\\(" comment-start-skip "\\|$\\)")) | |
456 | (case-fold-search nil)) ; keywords must be in uppercase | |
457 | (save-excursion | |
458 | (while (and (>= (forward-line -1) 0) | |
459 | (looking-at empty))) | |
460 | (skip-chars-forward " \t") | |
461 | (+ (current-column) | |
462 | ;; Are we looking at a macro open string? If so, add more. | |
463 | (cond ((looking-at snmp-macro-open) | |
464 | snmp-indent-level) | |
465 | ;; macro close string? If so, remove some. | |
466 | ((looking-at snmp-macro-close) | |
467 | (- snmp-indent-level)) | |
468 | ;; Neither; just stay here. | |
469 | (t 0)))))) | |
470 | ||
471 | (defun snmp-indent-line () | |
472 | "Indent current line as SNMP MIB code." | |
473 | (let ((indent (snmp-calculate-indent)) | |
474 | (pos (- (point-max) (point))) | |
475 | shift-amt beg end) | |
476 | (beginning-of-line) | |
477 | (setq beg (point)) | |
478 | (skip-chars-forward " \t") | |
479 | (setq shift-amt (- indent (current-column))) | |
480 | (if (zerop shift-amt) | |
481 | nil | |
482 | (delete-region beg (point)) | |
483 | (indent-to indent)) | |
484 | ;; If initial point was within line's indentation, | |
485 | ;; position after the indentation. Else stay at same point in text. | |
486 | (if (> (- (point-max) pos) (point)) | |
487 | (goto-char (- (point-max) pos))))) | |
488 | ||
489 | (defun snmp-indent-command () | |
490 | "Indent current line as SNMP MIB code, or sometimes insert a TAB. | |
491 | If `snmp-tab-always-indent' is t, always reindent the current line when | |
492 | this command is run. | |
493 | If `snmp-tab-always-indent' is nil, reindent the current line if point is | |
494 | in the initial indentation. Otherwise, insert a TAB." | |
495 | (interactive) | |
496 | (if (and (not snmp-tab-always-indent) | |
497 | (save-excursion | |
498 | (skip-chars-backward " \t") | |
499 | (not (bolp)))) | |
500 | (insert-tab) | |
501 | (snmp-indent-line))) | |
502 | ||
503 | ||
504 | ;;;---------------------------------------------------------------------------- | |
505 | ;; | |
506 | ;; Imenu Setup | |
507 | ;; | |
508 | ;;;---------------------------------------------------------------------------- | |
509 | ||
510 | (defvar snmp-clause-regexp | |
511 | "^[ \t]*\\([a-zA-Z][-a-zA-Z0-9]*\\)[ \t\n]*\ | |
512 | \\(TRAP-TYPE\\|::=\\|OBJECT\\(-TYPE[ \t\n]+SYNTAX\\|[ \t\n]+IDENTIFIER[ \t\n]*::=\\)\\)") | |
513 | ||
514 | (defun snmp-mode-imenu-create-index () | |
515 | (let ((index-alist '()) | |
516 | (index-oid-alist '()) | |
517 | (index-tc-alist '()) | |
518 | (index-table-alist '()) | |
519 | (index-trap-alist '()) | |
520 | (case-fold-search nil) ; keywords must be uppercase | |
521 | prev-pos token marker end) | |
522 | (goto-char (point-min)) | |
523 | (imenu-progress-message prev-pos 0) | |
524 | ;; Search for a useful MIB item (that's not in a comment) | |
525 | (save-match-data | |
526 | (while (re-search-forward snmp-clause-regexp nil t) | |
527 | (imenu-progress-message prev-pos) | |
528 | (setq | |
529 | end (match-end 0) | |
530 | token (cons (buffer-substring (match-beginning 1) (match-end 1)) | |
531 | (set-marker (make-marker) (match-beginning 1)))) | |
532 | (goto-char (match-beginning 2)) | |
533 | (cond ((looking-at "OBJECT-TYPE[ \t\n]+SYNTAX") | |
534 | (push token index-alist)) | |
535 | ((looking-at "OBJECT[ \t\n]+IDENTIFIER[ \t\n]*::=") | |
536 | (push token index-oid-alist)) | |
537 | ((looking-at "::=[ \t\n]*SEQUENCE[ \t\n]*{") | |
538 | (push token index-table-alist)) | |
539 | ((looking-at "TRAP-TYPE") | |
540 | (push token index-trap-alist)) | |
541 | ((looking-at "::=") | |
542 | (push token index-tc-alist))) | |
543 | (goto-char end))) | |
544 | ;; Create the menu | |
545 | (imenu-progress-message prev-pos 100) | |
546 | (setq index-alist (nreverse index-alist)) | |
547 | (and index-tc-alist | |
548 | (push (cons "Textual Conventions" (nreverse index-tc-alist)) | |
549 | index-alist)) | |
550 | (and index-trap-alist | |
551 | (push (cons "Traps" (nreverse index-trap-alist)) | |
552 | index-alist)) | |
553 | (and index-table-alist | |
554 | (push (cons "Tables" (nreverse index-table-alist)) | |
555 | index-alist)) | |
556 | (and index-oid-alist | |
557 | (push (cons "Object IDs" (nreverse index-oid-alist)) | |
558 | index-alist)) | |
559 | index-alist)) | |
560 | ||
561 | ||
562 | ;;;---------------------------------------------------------------------------- | |
563 | ;; | |
564 | ;; Tempo Setup | |
565 | ;; | |
566 | ;;;---------------------------------------------------------------------------- | |
567 | ||
568 | (require 'tempo) | |
569 | ||
570 | ;; Perform a completing-read with info given | |
571 | ;; | |
572 | (defun snmp-completing-read (prompt table &optional pred require init hist) | |
573 | "Read from the minibuffer, with completion. | |
574 | Like `completing-read', but the variable `snmp-completion-ignore-case' | |
575 | controls whether case is significant." | |
576 | (let ((completion-ignore-case snmp-completion-ignore-case)) | |
577 | (completing-read prompt table pred require init hist))) | |
578 | ||
579 | ;; OBJECT-TYPE macro template | |
580 | ;; | |
581 | (tempo-define-template "snmp-object-type" | |
582 | '(> (P "Object Label: ") " OBJECT-TYPE" n> | |
583 | "SYNTAX " | |
584 | (if tempo-interactive | |
585 | (snmp-completing-read "Syntax: " snmp-mode-syntax-list nil nil) | |
586 | p) n> | |
587 | "ACCESS " | |
588 | (if tempo-interactive | |
589 | (snmp-completing-read "Access: " snmp-mode-access-list nil t) | |
590 | p) n> | |
591 | "STATUS " | |
592 | (if tempo-interactive | |
593 | (snmp-completing-read "Status: " snmp-mode-status-list nil t) | |
594 | p) n> | |
595 | "DESCRIPTION" n> "\"" p "\"" n> | |
596 | (P "Default Value: " defval t) | |
597 | (if (string= "" (tempo-lookup-named 'defval)) | |
598 | nil | |
599 | '(l "DEFVAL { " (s defval) " }" n>)) | |
600 | "::= { " (p "OID: ") " }" n) | |
601 | "objectType" | |
602 | "Insert an OBJECT-TYPE macro." | |
603 | 'snmp-tempo-tags) | |
604 | ||
605 | ;; Table macro template | |
606 | ;; | |
607 | (tempo-define-template "snmp-table-type" | |
608 | ;; First the table OBJECT-TYPE | |
609 | '(> (P "Table Name: " table) | |
610 | (P "Entry Name: " entry t) | |
611 | (let* ((entry (tempo-lookup-named 'entry)) | |
612 | (seq (copy-sequence entry))) | |
613 | (aset entry 0 (downcase (aref entry 0))) | |
614 | (aset seq 0 (upcase (aref seq 0))) | |
615 | (tempo-save-named 'obj-entry entry) | |
616 | (tempo-save-named 'seq-entry seq) | |
617 | nil) | |
618 | " OBJECT-TYPE" n> | |
619 | "SYNTAX SEQUENCE OF " | |
620 | (s seq-entry) n> | |
621 | "ACCESS not-accessible" n> | |
622 | "STATUS mandatory" n> | |
623 | "DESCRIPTION" n> "\"" p "\"" n> | |
624 | "::= { " (p "OID: ") " }" n n> | |
625 | ;; Next the row OBJECT-TYPE | |
626 | (s obj-entry) " OBJECT-TYPE" n> | |
627 | "SYNTAX " (s seq-entry) n> | |
628 | "ACCESS not-accessible" n> | |
629 | "STATUS mandatory" n> | |
630 | "DESCRIPTION" n> "\"" p "\"" n> | |
631 | "INDEX { " (p "Index List: ") " }" n> | |
632 | "::= {" (s table) " 1 }" n n> | |
633 | ;; Finally the SEQUENCE type | |
634 | (s seq-entry) " ::= SEQUENCE {" n> p n> "}" n) | |
635 | "tableType" | |
636 | "Insert an SNMP table." | |
637 | 'snmp-tempo-tags) | |
638 | ||
639 | ||
640 | ;; v2 SMI OBJECT-TYPE macro template | |
641 | ;; | |
642 | (tempo-define-template "snmpv2-object-type" | |
643 | '(> (P "Object Label: ") " OBJECT-TYPE" n> | |
644 | "SYNTAX " | |
645 | (if tempo-interactive | |
646 | (snmp-completing-read "Syntax: " snmp-mode-syntax-list nil nil) | |
647 | p) n> | |
648 | "MAX-ACCESS " | |
649 | (if tempo-interactive | |
650 | (snmp-completing-read "Max Access: " snmp-mode-access-list nil t) | |
651 | p) n> | |
652 | "STATUS " | |
653 | (if tempo-interactive | |
654 | (snmp-completing-read "Status: " snmp-mode-status-list nil t) | |
655 | p) n> | |
656 | "DESCRIPTION" n> "\"" p "\"" n> | |
657 | (P "Default Value: " defval t) | |
658 | (if (string= "" (tempo-lookup-named 'defval)) | |
659 | nil | |
660 | '(l "DEFVAL { " (s defval) " }" n>)) | |
661 | "::= { " (p "OID: ") " }" n) | |
662 | "objectType" | |
663 | "Insert an v2 SMI OBJECT-TYPE macro." | |
664 | 'snmpv2-tempo-tags) | |
665 | ||
666 | ;; v2 SMI Table macro template | |
667 | ;; | |
668 | (tempo-define-template "snmpv2-table-type" | |
669 | ;; First the table OBJECT-TYPE | |
670 | '(> (P "Table Name: " table) | |
671 | (P "Entry Name: " entry t) | |
672 | (let* ((entry (tempo-lookup-named 'entry)) | |
673 | (seq (copy-sequence entry))) | |
674 | (aset entry 0 (downcase (aref entry 0))) | |
675 | (aset seq 0 (upcase (aref seq 0))) | |
676 | (tempo-save-named 'obj-entry entry) | |
677 | (tempo-save-named 'seq-entry seq) | |
678 | nil) | |
679 | " OBJECT-TYPE" n> | |
680 | "SYNTAX SEQUENCE OF " | |
681 | (s seq-entry) n> | |
682 | "MAX-ACCESS not-accessible" n> | |
683 | "STATUS current" n> | |
684 | "DESCRIPTION" n> "\"" p "\"" n> | |
685 | "::= { " (p "OID: ") " }" n n> | |
686 | ;; Next the row OBJECT-TYPE | |
687 | (s obj-entry) " OBJECT-TYPE" n> | |
688 | "SYNTAX " (s seq-entry) n> | |
689 | "MAX-ACCESS not-accessible" n> | |
690 | "STATUS current" n> | |
691 | "DESCRIPTION" n> "\"" p "\"" n> | |
692 | "INDEX { " (p "Index List: ") " }" n> | |
693 | "::= { " (s table) " 1 }" n n> | |
694 | ;; Finally the SEQUENCE type | |
695 | (s seq-entry) " ::= SEQUENCE {" n> p n> "}" n) | |
696 | "tableType" | |
697 | "Insert an v2 SMI SNMP table." | |
698 | 'snmpv2-tempo-tags) | |
699 | ||
700 | ;; v2 SMI TEXTUAL-CONVENTION macro template | |
701 | ;; | |
702 | (tempo-define-template "snmpv2-textual-convention" | |
703 | '(> (P "Texual Convention Type: ") " ::= TEXTUAL-CONVENTION" n> | |
704 | "STATUS " | |
705 | (if tempo-interactive | |
706 | (snmp-completing-read "Status: " snmp-mode-status-list nil t) | |
707 | p) n> | |
708 | "DESCRIPTION" n> "\"" p "\"" n> | |
709 | "SYNTAX " | |
710 | (if tempo-interactive | |
711 | (snmp-completing-read "Syntax: " snmp-mode-syntax-list nil nil) | |
712 | p) n> ) | |
713 | "textualConvention" | |
714 | "Insert an v2 SMI TEXTUAL-CONVENTION macro." | |
715 | 'snmpv2-tempo-tags) | |
716 | ||
717 | ||
718 | (provide 'snmp-mode) | |
719 | ||
e8af40ee | 720 | ;;; snmp-mode.el ends here |