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