Commit | Line | Data |
---|---|---|
e8af40ee | 1 | ;;; snmp-mode.el --- SNMP & SNMPv2 MIB major mode |
8749abea | 2 | |
ba318903 | 3 | ;; Copyright (C) 1995, 1998, 2001-2014 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 |
f440830d GM |
88 | (require 'imenu)) ; Need this stuff when compiling for imenu macros, etc. |
89 | ||
90 | (require 'tempo) | |
1d09ce77 | 91 | |
8749abea GM |
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 | |
ab8e678c | 104 | "If non-nil, use a simple heuristic to try to guess the right indentation. |
8749abea GM |
105 | If nil, then no special indentation is attempted." |
106 | :type 'boolean | |
107 | :group 'snmp) | |
108 | ||
109 | (defcustom snmp-indent-level 4 | |
ab8e678c | 110 | "Indentation level for SNMP MIBs." |
8749abea GM |
111 | :type 'integer |
112 | :group 'snmp) | |
113 | ||
114 | (defcustom snmp-tab-always-indent nil | |
ab8e678c | 115 | "Non-nil means TAB should always reindent the current line. |
8749abea GM |
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 | |
ab8e678c | 122 | "Non-nil means that case differences are ignored during completion. |
8749abea GM |
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 | |
ab8e678c | 129 | "Hook(s) evaluated when a buffer enters either SNMP or SNMPv2 mode." |
8749abea GM |
130 | :type 'hook |
131 | :group 'snmp) | |
132 | ||
133 | (defcustom snmp-mode-hook nil | |
ab8e678c | 134 | "Hook(s) evaluated when a buffer enters SNMP mode." |
8749abea GM |
135 | :type 'hook |
136 | :group 'snmp) | |
137 | ||
138 | (defcustom snmpv2-mode-hook nil | |
ab8e678c | 139 | "Hook(s) evaluated when a buffer enters SNMPv2 mode." |
8749abea GM |
140 | :type 'hook |
141 | :group 'snmp) | |
142 | ||
143 | (defvar snmp-tempo-tags nil | |
fb7ada5f | 144 | "Tempo tags for SNMP mode.") |
8749abea GM |
145 | |
146 | (defvar snmpv2-tempo-tags nil | |
fb7ada5f | 147 | "Tempo tags for SNMPv2 mode.") |
8749abea GM |
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]+{" | |
59f7af81 | 179 | (1 font-lock-constant-face) (2 font-lock-keyword-face)) |
8749abea | 180 | ("::=[ \t]*{[ \t]*\\([a-z0-9].*[ \t]+\\)?\\([0-9]+\\)[ \t]*}" |
59f7af81 | 181 | (1 font-lock-constant-face nil t) (2 font-lock-variable-name-face))) |
8749abea GM |
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 | |
ab8e678c SM |
197 | '("INTEGER" "OCTET STRING" "OBJECT IDENTIFIER" "NULL" "IpAddress" |
198 | "NetworkAddress" "Counter" "Gauge" "TimeTicks" "Opaque") | |
8749abea GM |
199 | "Types from RFC 1155 v1 SMI.") |
200 | ||
201 | (defvar snmp-rfc1213-types | |
ab8e678c | 202 | '("DisplayString") |
8749abea GM |
203 | "Types from RFC 1213 MIB-II.") |
204 | ||
205 | (defvar snmp-rfc1902-types | |
ab8e678c SM |
206 | '("INTEGER" "OCTET STRING" "OBJECT IDENTIFIER" "Integer32" |
207 | "IpAddress" "Counter32" "Gauge32" "Unsigned32" "TimeTicks" | |
208 | "Opaque" "Counter64") | |
8749abea GM |
209 | "Types from RFC 1902 v2 SMI.") |
210 | ||
211 | (defvar snmp-rfc1903-types | |
ab8e678c SM |
212 | '("DisplayString" "PhysAddress" "MacAddress" "TruthValue" |
213 | "TestAndIncr" "AutonomousType" "InstancePointer" | |
214 | "VariablePointer" "RowPointer" "RowStatus" "TimeStamp" | |
215 | "TimeInterval" "DateAndTime" "StorageType" "TDomain" | |
216 | "TAddress") | |
8749abea GM |
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 | |
ab8e678c | 224 | '("read-only" "read-write" "write-only" "not-accessible") |
8749abea GM |
225 | "ACCESS values from RFC 1155 v1 SMI.") |
226 | ||
227 | (defvar snmp-rfc1902-access | |
ab8e678c SM |
228 | '("read-only" "read-write" "read-create" "not-accessible" |
229 | "accessible-for-notify") | |
8749abea GM |
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 | |
ab8e678c | 237 | '("mandatory" "obsolete" "deprecated") |
8749abea GM |
238 | "STATUS values from RFC 1212 v1 SMI.") |
239 | ||
240 | (defvar snmp-rfc1902-status | |
ab8e678c | 241 | '("current" "obsolete" "deprecated") |
8749abea GM |
242 | "STATUS values from RFC 1902 v2 SMI.") |
243 | ||
244 | ||
245 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
246 | ;;;---------------------------------------------------------------------------- | |
247 | ;; | |
248 | ;; Nothing to customize below here. | |
249 | ;; | |
250 | ;;;---------------------------------------------------------------------------- | |
251 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
252 | ||
253 | ||
8749abea GM |
254 | ;; Create abbrev table for SNMP MIB mode |
255 | ;; | |
256 | (defvar snmp-mode-abbrev-table nil | |
257 | "Abbrev table in use in SNMP mode.") | |
258 | (define-abbrev-table 'snmp-mode-abbrev-table ()) | |
259 | ||
260 | ||
261 | ;; Create abbrev table for SNMPv2 mode | |
262 | ;; | |
263 | (defvar snmpv2-mode-abbrev-table nil | |
264 | "Abbrev table in use in SNMPv2 mode.") | |
265 | (define-abbrev-table 'snmpv2-mode-abbrev-table ()) | |
266 | ||
267 | ||
268 | ;; Set up our keymap | |
269 | ;; | |
ab8e678c SM |
270 | (defvar snmp-mode-map |
271 | (let ((map (make-sparse-keymap))) | |
272 | (define-key map "\177" 'backward-delete-char-untabify) | |
273 | (define-key map "\C-c\C-i" 'tempo-complete-tag) | |
274 | (define-key map "\C-c\C-f" 'tempo-forward-mark) | |
275 | (define-key map "\C-c\C-b" 'tempo-backward-mark) | |
276 | map) | |
8749abea GM |
277 | "Keymap used in SNMP mode.") |
278 | ||
8749abea GM |
279 | |
280 | ;; Set up our syntax table | |
281 | ;; | |
ab8e678c SM |
282 | (defvar snmp-mode-syntax-table |
283 | (let ((st (make-syntax-table))) | |
284 | (modify-syntax-entry ?\\ "\\" st) | |
285 | (modify-syntax-entry ?- "_ 1234" st) | |
286 | (modify-syntax-entry ?\n ">" st) | |
287 | (modify-syntax-entry ?\^m ">" st) | |
288 | (modify-syntax-entry ?_ "." st) | |
289 | (modify-syntax-entry ?: "." st) | |
290 | (modify-syntax-entry ?= "." st) | |
291 | st) | |
8749abea GM |
292 | "Syntax table used for buffers in SNMP mode.") |
293 | ||
8749abea GM |
294 | ;; Set up the stuff that's common between snmp-mode and snmpv2-mode |
295 | ;; | |
296 | (defun snmp-common-mode (name mode abbrev font-keywords imenu-index tempo-tags) | |
297 | (kill-all-local-variables) | |
298 | ||
299 | ;; Become the current major mode | |
300 | (setq mode-name name) | |
301 | (setq major-mode mode) | |
302 | ||
303 | ;; Activate keymap, syntax table, and abbrev table | |
304 | (use-local-map snmp-mode-map) | |
305 | (set-syntax-table snmp-mode-syntax-table) | |
306 | (setq local-abbrev-table abbrev) | |
307 | ||
308 | ;; Set up paragraphs (?) | |
309 | (make-local-variable 'paragraph-start) | |
310 | (setq paragraph-start (concat "$\\|" page-delimiter)) | |
311 | (make-local-variable 'paragraph-separate) | |
312 | (setq paragraph-separate paragraph-start) | |
313 | (make-local-variable 'paragraph-ignore-fill-prefix) | |
314 | (setq paragraph-ignore-fill-prefix t) | |
315 | ||
316 | ;; Set up comments | |
317 | (make-local-variable 'comment-start) | |
318 | (setq comment-start "-- ") | |
319 | (make-local-variable 'comment-start-skip) | |
320 | (setq comment-start-skip "--+[ \t]*") | |
321 | (make-local-variable 'comment-column) | |
322 | (setq comment-column 40) | |
323 | (make-local-variable 'parse-sexp-ignore-comments) | |
324 | (setq parse-sexp-ignore-comments t) | |
325 | ||
326 | ;; Set up indentation | |
ab8e678c SM |
327 | (if snmp-special-indent |
328 | (set (make-local-variable 'indent-line-function) 'snmp-indent-line)) | |
329 | (set (make-local-variable 'tab-always-indent) snmp-tab-always-indent) | |
8749abea GM |
330 | |
331 | ;; Font Lock | |
332 | (make-local-variable 'font-lock-defaults) | |
333 | (setq font-lock-defaults (cons font-keywords '(nil nil ((?- . "w 1234"))))) | |
334 | ||
335 | ;; Imenu | |
336 | (make-local-variable 'imenu-create-index-function) | |
337 | (setq imenu-create-index-function imenu-index) | |
338 | ||
339 | ;; Tempo | |
340 | (tempo-use-tag-list tempo-tags) | |
341 | (make-local-variable 'tempo-match-finder) | |
342 | (setq tempo-match-finder "\\b\\(.+\\)\\=") | |
343 | (make-local-variable 'tempo-interactive) | |
344 | (setq tempo-interactive t) | |
345 | ||
346 | ;; Miscellaneous customization | |
347 | (make-local-variable 'require-final-newline) | |
9f0781df | 348 | (setq require-final-newline mode-require-final-newline)) |
8749abea GM |
349 | |
350 | ||
351 | ;; SNMPv1 MIB Editing Mode. | |
352 | ;; | |
353 | ;;;###autoload | |
354 | (defun snmp-mode () | |
355 | "Major mode for editing SNMP MIBs. | |
356 | Expression and list commands understand all C brackets. | |
357 | Tab indents for C code. | |
358 | Comments start with -- and end with newline or another --. | |
359 | Delete converts tabs to spaces as it moves back. | |
360 | \\{snmp-mode-map} | |
361 | Turning on snmp-mode runs the hooks in `snmp-common-mode-hook', then | |
362 | `snmp-mode-hook'." | |
363 | (interactive) | |
364 | ||
365 | (snmp-common-mode "SNMP" 'snmp-mode | |
366 | snmp-mode-abbrev-table | |
367 | '(snmp-font-lock-keywords | |
368 | snmp-font-lock-keywords-1 | |
369 | snmp-font-lock-keywords-2 | |
370 | snmp-font-lock-keywords-3) | |
371 | 'snmp-mode-imenu-create-index | |
372 | 'snmp-tempo-tags) | |
373 | ||
374 | ;; Completion lists | |
375 | (make-local-variable 'snmp-mode-syntax-list) | |
376 | (setq snmp-mode-syntax-list (append snmp-rfc1155-types | |
377 | snmp-rfc1213-types | |
378 | snmp-mode-syntax-list)) | |
379 | (make-local-variable 'snmp-mode-access-list) | |
380 | (setq snmp-mode-access-list snmp-rfc1155-access) | |
381 | (make-local-variable 'snmp-mode-status-list) | |
382 | (setq snmp-mode-status-list snmp-rfc1212-status) | |
383 | ||
384 | ;; Run hooks | |
57f9923e | 385 | (run-mode-hooks 'snmp-common-mode-hook 'snmp-mode-hook)) |
8749abea GM |
386 | |
387 | ||
388 | ;;;###autoload | |
389 | (defun snmpv2-mode () | |
390 | "Major mode for editing SNMPv2 MIBs. | |
391 | Expression and list commands understand all C brackets. | |
392 | Tab indents for C code. | |
393 | Comments start with -- and end with newline or another --. | |
394 | Delete converts tabs to spaces as it moves back. | |
395 | \\{snmp-mode-map} | |
396 | Turning on snmp-mode runs the hooks in `snmp-common-mode-hook', | |
397 | then `snmpv2-mode-hook'." | |
398 | (interactive) | |
399 | ||
400 | (snmp-common-mode "SNMPv2" 'snmpv2-mode | |
401 | snmpv2-mode-abbrev-table | |
402 | '(snmp-font-lock-keywords | |
403 | snmp-font-lock-keywords-1 | |
404 | snmp-font-lock-keywords-2 | |
405 | snmp-font-lock-keywords-3) | |
406 | 'snmp-mode-imenu-create-index | |
407 | 'snmpv2-tempo-tags) | |
408 | ||
409 | ;; Completion lists | |
410 | (make-local-variable 'snmp-mode-syntax-list) | |
411 | (setq snmp-mode-syntax-list (append snmp-rfc1902-types | |
412 | snmp-rfc1903-types | |
413 | snmp-mode-syntax-list)) | |
414 | (make-local-variable 'snmp-mode-access-list) | |
415 | (setq snmp-mode-access-list snmp-rfc1902-access) | |
416 | (make-local-variable 'snmp-mode-status-list) | |
417 | (setq snmp-mode-status-list snmp-rfc1902-status) | |
418 | ||
419 | ;; Run hooks | |
57f9923e | 420 | (run-mode-hooks 'snmp-common-mode-hook 'snmpv2-mode-hook)) |
8749abea GM |
421 | |
422 | ||
423 | ;;;---------------------------------------------------------------------------- | |
424 | ;; | |
425 | ;; Indentation Setup | |
426 | ;; | |
427 | ;;;---------------------------------------------------------------------------- | |
428 | ||
429 | (defvar snmp-macro-open | |
430 | "[a-zA-Z][-a-zA-Z0-9]*[ \t]*\\(OBJECT\\|TRAP\\)-\\(TYPE\\|GROUP\\)\ | |
431 | \\|DESCRIPTION\\|IMPORTS\\|MODULE\\(-IDENTITY\\|-COMPLIANCE\\)\ | |
432 | \\|.*::=[ \t]*\\(BEGIN\\|TEXTUAL-CONVENTION\\)[ \t]*$") | |
433 | ||
434 | (defvar snmp-macro-close | |
435 | "::=[ \t]*{\\|\\(END\\|.*[;\"]\\)[ \t]*$") | |
436 | ||
437 | (defun snmp-calculate-indent () | |
438 | "Calculate the current line indentation in SNMP MIB code. | |
439 | ||
440 | We use a very simple scheme: if the previous non-empty line was a \"macro | |
441 | open\" string, add `snmp-indent-level' to it. If it was a \"macro close\" | |
442 | string, subtract `snmp-indent-level'. Otherwise, use the same indentation | |
443 | as the previous non-empty line. Note comments are considered empty | |
444 | lines for the purposes of this function." | |
445 | (let ((empty (concat "\\([ \t]*\\)\\(" comment-start-skip "\\|$\\)")) | |
446 | (case-fold-search nil)) ; keywords must be in uppercase | |
447 | (save-excursion | |
448 | (while (and (>= (forward-line -1) 0) | |
449 | (looking-at empty))) | |
450 | (skip-chars-forward " \t") | |
451 | (+ (current-column) | |
452 | ;; Are we looking at a macro open string? If so, add more. | |
453 | (cond ((looking-at snmp-macro-open) | |
454 | snmp-indent-level) | |
455 | ;; macro close string? If so, remove some. | |
456 | ((looking-at snmp-macro-close) | |
457 | (- snmp-indent-level)) | |
458 | ;; Neither; just stay here. | |
459 | (t 0)))))) | |
460 | ||
461 | (defun snmp-indent-line () | |
462 | "Indent current line as SNMP MIB code." | |
463 | (let ((indent (snmp-calculate-indent)) | |
464 | (pos (- (point-max) (point))) | |
ab8e678c | 465 | shift-amt beg) |
8749abea GM |
466 | (beginning-of-line) |
467 | (setq beg (point)) | |
468 | (skip-chars-forward " \t") | |
469 | (setq shift-amt (- indent (current-column))) | |
470 | (if (zerop shift-amt) | |
471 | nil | |
472 | (delete-region beg (point)) | |
473 | (indent-to indent)) | |
474 | ;; If initial point was within line's indentation, | |
475 | ;; position after the indentation. Else stay at same point in text. | |
476 | (if (> (- (point-max) pos) (point)) | |
477 | (goto-char (- (point-max) pos))))) | |
478 | ||
8749abea GM |
479 | |
480 | ;;;---------------------------------------------------------------------------- | |
481 | ;; | |
482 | ;; Imenu Setup | |
483 | ;; | |
484 | ;;;---------------------------------------------------------------------------- | |
485 | ||
486 | (defvar snmp-clause-regexp | |
487 | "^[ \t]*\\([a-zA-Z][-a-zA-Z0-9]*\\)[ \t\n]*\ | |
488 | \\(TRAP-TYPE\\|::=\\|OBJECT\\(-TYPE[ \t\n]+SYNTAX\\|[ \t\n]+IDENTIFIER[ \t\n]*::=\\)\\)") | |
489 | ||
490 | (defun snmp-mode-imenu-create-index () | |
491 | (let ((index-alist '()) | |
492 | (index-oid-alist '()) | |
493 | (index-tc-alist '()) | |
494 | (index-table-alist '()) | |
495 | (index-trap-alist '()) | |
496 | (case-fold-search nil) ; keywords must be uppercase | |
ab8e678c | 497 | prev-pos token end) |
8749abea GM |
498 | (goto-char (point-min)) |
499 | (imenu-progress-message prev-pos 0) | |
500 | ;; Search for a useful MIB item (that's not in a comment) | |
501 | (save-match-data | |
502 | (while (re-search-forward snmp-clause-regexp nil t) | |
503 | (imenu-progress-message prev-pos) | |
504 | (setq | |
505 | end (match-end 0) | |
ab8e678c | 506 | token (cons (match-string 1) |
8749abea GM |
507 | (set-marker (make-marker) (match-beginning 1)))) |
508 | (goto-char (match-beginning 2)) | |
509 | (cond ((looking-at "OBJECT-TYPE[ \t\n]+SYNTAX") | |
510 | (push token index-alist)) | |
511 | ((looking-at "OBJECT[ \t\n]+IDENTIFIER[ \t\n]*::=") | |
512 | (push token index-oid-alist)) | |
513 | ((looking-at "::=[ \t\n]*SEQUENCE[ \t\n]*{") | |
514 | (push token index-table-alist)) | |
515 | ((looking-at "TRAP-TYPE") | |
516 | (push token index-trap-alist)) | |
517 | ((looking-at "::=") | |
518 | (push token index-tc-alist))) | |
519 | (goto-char end))) | |
520 | ;; Create the menu | |
521 | (imenu-progress-message prev-pos 100) | |
522 | (setq index-alist (nreverse index-alist)) | |
523 | (and index-tc-alist | |
524 | (push (cons "Textual Conventions" (nreverse index-tc-alist)) | |
525 | index-alist)) | |
526 | (and index-trap-alist | |
527 | (push (cons "Traps" (nreverse index-trap-alist)) | |
528 | index-alist)) | |
529 | (and index-table-alist | |
530 | (push (cons "Tables" (nreverse index-table-alist)) | |
531 | index-alist)) | |
532 | (and index-oid-alist | |
533 | (push (cons "Object IDs" (nreverse index-oid-alist)) | |
534 | index-alist)) | |
535 | index-alist)) | |
536 | ||
537 | ||
538 | ;;;---------------------------------------------------------------------------- | |
539 | ;; | |
540 | ;; Tempo Setup | |
541 | ;; | |
542 | ;;;---------------------------------------------------------------------------- | |
543 | ||
8749abea GM |
544 | ;; Perform a completing-read with info given |
545 | ;; | |
546 | (defun snmp-completing-read (prompt table &optional pred require init hist) | |
547 | "Read from the minibuffer, with completion. | |
548 | Like `completing-read', but the variable `snmp-completion-ignore-case' | |
549 | controls whether case is significant." | |
550 | (let ((completion-ignore-case snmp-completion-ignore-case)) | |
551 | (completing-read prompt table pred require init hist))) | |
552 | ||
553 | ;; OBJECT-TYPE macro template | |
554 | ;; | |
555 | (tempo-define-template "snmp-object-type" | |
556 | '(> (P "Object Label: ") " OBJECT-TYPE" n> | |
557 | "SYNTAX " | |
558 | (if tempo-interactive | |
559 | (snmp-completing-read "Syntax: " snmp-mode-syntax-list nil nil) | |
560 | p) n> | |
561 | "ACCESS " | |
562 | (if tempo-interactive | |
563 | (snmp-completing-read "Access: " snmp-mode-access-list nil t) | |
564 | p) n> | |
565 | "STATUS " | |
566 | (if tempo-interactive | |
567 | (snmp-completing-read "Status: " snmp-mode-status-list nil t) | |
568 | p) n> | |
569 | "DESCRIPTION" n> "\"" p "\"" n> | |
570 | (P "Default Value: " defval t) | |
571 | (if (string= "" (tempo-lookup-named 'defval)) | |
572 | nil | |
573 | '(l "DEFVAL { " (s defval) " }" n>)) | |
574 | "::= { " (p "OID: ") " }" n) | |
575 | "objectType" | |
576 | "Insert an OBJECT-TYPE macro." | |
577 | 'snmp-tempo-tags) | |
578 | ||
579 | ;; Table macro template | |
580 | ;; | |
581 | (tempo-define-template "snmp-table-type" | |
582 | ;; First the table OBJECT-TYPE | |
583 | '(> (P "Table Name: " table) | |
584 | (P "Entry Name: " entry t) | |
585 | (let* ((entry (tempo-lookup-named 'entry)) | |
586 | (seq (copy-sequence entry))) | |
587 | (aset entry 0 (downcase (aref entry 0))) | |
588 | (aset seq 0 (upcase (aref seq 0))) | |
589 | (tempo-save-named 'obj-entry entry) | |
590 | (tempo-save-named 'seq-entry seq) | |
591 | nil) | |
592 | " OBJECT-TYPE" n> | |
593 | "SYNTAX SEQUENCE OF " | |
594 | (s seq-entry) n> | |
595 | "ACCESS not-accessible" n> | |
596 | "STATUS mandatory" n> | |
597 | "DESCRIPTION" n> "\"" p "\"" n> | |
598 | "::= { " (p "OID: ") " }" n n> | |
599 | ;; Next the row OBJECT-TYPE | |
600 | (s obj-entry) " OBJECT-TYPE" n> | |
601 | "SYNTAX " (s seq-entry) n> | |
602 | "ACCESS not-accessible" n> | |
603 | "STATUS mandatory" n> | |
604 | "DESCRIPTION" n> "\"" p "\"" n> | |
605 | "INDEX { " (p "Index List: ") " }" n> | |
606 | "::= {" (s table) " 1 }" n n> | |
607 | ;; Finally the SEQUENCE type | |
608 | (s seq-entry) " ::= SEQUENCE {" n> p n> "}" n) | |
609 | "tableType" | |
610 | "Insert an SNMP table." | |
611 | 'snmp-tempo-tags) | |
612 | ||
613 | ||
614 | ;; v2 SMI OBJECT-TYPE macro template | |
615 | ;; | |
616 | (tempo-define-template "snmpv2-object-type" | |
617 | '(> (P "Object Label: ") " OBJECT-TYPE" n> | |
618 | "SYNTAX " | |
619 | (if tempo-interactive | |
620 | (snmp-completing-read "Syntax: " snmp-mode-syntax-list nil nil) | |
621 | p) n> | |
622 | "MAX-ACCESS " | |
623 | (if tempo-interactive | |
624 | (snmp-completing-read "Max Access: " snmp-mode-access-list nil t) | |
625 | p) n> | |
626 | "STATUS " | |
627 | (if tempo-interactive | |
628 | (snmp-completing-read "Status: " snmp-mode-status-list nil t) | |
629 | p) n> | |
630 | "DESCRIPTION" n> "\"" p "\"" n> | |
631 | (P "Default Value: " defval t) | |
632 | (if (string= "" (tempo-lookup-named 'defval)) | |
633 | nil | |
634 | '(l "DEFVAL { " (s defval) " }" n>)) | |
635 | "::= { " (p "OID: ") " }" n) | |
636 | "objectType" | |
637 | "Insert an v2 SMI OBJECT-TYPE macro." | |
638 | 'snmpv2-tempo-tags) | |
639 | ||
640 | ;; v2 SMI Table macro template | |
641 | ;; | |
642 | (tempo-define-template "snmpv2-table-type" | |
643 | ;; First the table OBJECT-TYPE | |
644 | '(> (P "Table Name: " table) | |
645 | (P "Entry Name: " entry t) | |
646 | (let* ((entry (tempo-lookup-named 'entry)) | |
647 | (seq (copy-sequence entry))) | |
648 | (aset entry 0 (downcase (aref entry 0))) | |
649 | (aset seq 0 (upcase (aref seq 0))) | |
650 | (tempo-save-named 'obj-entry entry) | |
651 | (tempo-save-named 'seq-entry seq) | |
652 | nil) | |
653 | " OBJECT-TYPE" n> | |
654 | "SYNTAX SEQUENCE OF " | |
655 | (s seq-entry) n> | |
656 | "MAX-ACCESS not-accessible" n> | |
657 | "STATUS current" n> | |
658 | "DESCRIPTION" n> "\"" p "\"" n> | |
659 | "::= { " (p "OID: ") " }" n n> | |
660 | ;; Next the row OBJECT-TYPE | |
661 | (s obj-entry) " OBJECT-TYPE" n> | |
662 | "SYNTAX " (s seq-entry) n> | |
663 | "MAX-ACCESS not-accessible" n> | |
664 | "STATUS current" n> | |
665 | "DESCRIPTION" n> "\"" p "\"" n> | |
666 | "INDEX { " (p "Index List: ") " }" n> | |
667 | "::= { " (s table) " 1 }" n n> | |
668 | ;; Finally the SEQUENCE type | |
669 | (s seq-entry) " ::= SEQUENCE {" n> p n> "}" n) | |
670 | "tableType" | |
671 | "Insert an v2 SMI SNMP table." | |
672 | 'snmpv2-tempo-tags) | |
673 | ||
674 | ;; v2 SMI TEXTUAL-CONVENTION macro template | |
675 | ;; | |
676 | (tempo-define-template "snmpv2-textual-convention" | |
8350f087 | 677 | '(> (P "Textual Convention Type: ") " ::= TEXTUAL-CONVENTION" n> |
8749abea GM |
678 | "STATUS " |
679 | (if tempo-interactive | |
680 | (snmp-completing-read "Status: " snmp-mode-status-list nil t) | |
681 | p) n> | |
682 | "DESCRIPTION" n> "\"" p "\"" n> | |
683 | "SYNTAX " | |
684 | (if tempo-interactive | |
685 | (snmp-completing-read "Syntax: " snmp-mode-syntax-list nil nil) | |
686 | p) n> ) | |
687 | "textualConvention" | |
688 | "Insert an v2 SMI TEXTUAL-CONVENTION macro." | |
689 | 'snmpv2-tempo-tags) | |
690 | ||
691 | ||
692 | (provide 'snmp-mode) | |
693 | ||
e8af40ee | 694 | ;;; snmp-mode.el ends here |