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