forked from emacsmirror/cisco-router-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cisco-router-mode.el
180 lines (161 loc) · 7.58 KB
/
cisco-router-mode.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
;;; cisco-router-mode.el --- Major mode for editing Cisco router configuration files
;;
;; Copyright (C) 2004 Noufal Ibrahim <nkv at hcoop period net>
;;
;; This program is not part of Gnu Emacs
;;
;; cisco-router-mode.el is free software; you can
;; redistribute it and/or modify it under the terms of the GNU General
;; Public License as published by the Free Software Foundation; either
;; version 2 of the License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software
;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
;; Updated by Fred Renner 2022
(defvar cisco-router-mode-hook nil
"Hook called by \"cisco-router-mode\"")
(defvar cisco-router-mode-map
(let
((cisco-router-mode-map (make-keymap)))
(define-key cisco-router-mode-map "\C-j" 'newline-and-indent)
cisco-router-mode-map)
"Keymap for Cisco router configuration major mode")
;; Font locking definitions.
;; These define the variables used for different font coding.
(defvar cisco-router-command-face 'cisco-router-command-face "Face for basic router commands")
(defvar cisco-router-toplevel-face 'cisco-router-toplevel-face "Face for top level commands")
(defvar cisco-router-no-face 'cisco-router-no-face "Face for \"no\"")
(defvar cisco-router-ipadd-face 'cisco-router-ipadd-face "Face for IP addresses")
(defvar cisco-router-interface-face 'cisco-router-interface-face "Face for Interface Names")
;; From here we are setting the look for each of the above defined Face variables.
;; I don't know where the color listing comes from.
(defface cisco-router-ipadd-face
'(
(((type tty) (class color)) (:foreground "yellow"))
(((type graphic) (class color)) (:foreground "LightGoldenrod"))
(t (:foreground "LightGoldenrod" ))
)
"Face for IP addresses")
(defface cisco-router-command-face
'(
(((type tty) (class color)) (:foreground "cyan"))
(((type graphic) (class color)) (:foreground "cyan"))
(t (:foreground "cyan" ))
)
"Face for basic router commands")
(defface cisco-router-toplevel-face
'(
(((type tty) (class color)) (:foreground "blue"))
(((type graphic) (class color)) (:foreground "lightsteelblue"))
(t (:foreground "blue" ))
)
"Face for basic router commands")
(defface cisco-router-no-face
'(
(((type graphic) (class color)) (:foreground "red"))
(t (:underline t))
)
"Face for \"no\"")
(defface cisco-router-interface-face
'(
(((type tty) (class color)) (:foreground "orange"))
(((type graphic) (class color)) (:foreground "orange"))
(t (:foreground "orange" ))
)
"Face for interface names")
;; (regexp-opt '("interface" "ip vrf" "controller" "class-map" "redundancy" "line" "policy-map" "router" "access-list" "route-map") t)
;; (regexp-opt '("diagnostic" "hostname" "logging" "service" "alias" "snmp-server" "boot" "card" "vtp" "version" "enable") t)
;; This section does the regex to assign above faces to keywords
(defconst cisco-router-font-lock-keywords
(list
'( "\\<\\(access-list\\|c\\(?:lass-map\\|ontroller\\)\\|i\\(?:nterface\\|p vrf\\)\\|line\\|policy-map\\|r\\(?:edundancy\\|oute\\(?:-map\\|r\\)\\)\\)\\>". cisco-router-toplevel-face)
'( "\\<\\(alias\\|boot\\|card\\|diagnostic\\|^enable\\|hostname\\|logging\\|radius-server\\|s\\(?:ervice\\|nmp-server\\)\\|v\\(?:ersion\\|tp\\)\\)\\>" . cisco-router-command-face)
'("\\<\\(no\\|shutdown\\)\\>" . cisco-router-no-face)
'("\\<\\([0-9]\\{1,3\\}\\.[0-9]\\{1,3\\}\\.[0-9]\\{1,3\\}\\.[0-9]\\{1,3\\}\\)\\>" . cisco-router-ipadd-face)
'("\\<\\([a-zA-Z]*Ethernet[0-9]+/[0-9]+/[0-9]+\\|[a-zA-Z]*Ethernet\\([0-9]+\\S+[0-9]+\\)+\\|[a-zA-Z]*Ethernet[0-9]+\\|Vlan[0-9]+\\|vlan [0-9]+\\|ae[0-9]\\S+[0-9]+\\|ae[0-9]+\\|Loopback[0-9]+\\|Tunnel[0-9]+\\|Port-Channel[0-9]+\\|ethernet\\([0-9]+\\S+\\)+\\)\\|Vxlan[0-9]+\\|ethernet[0-9]+\\S+[0-9]+\\>" . cisco-router-interface-face)
;; '("\\<\\(Vlan[0-9]+\\|ae[0-9]\\S+[0-9]+\\|ae[0-9]+\\|Loopback[0-9]+\\|Tunnel[0-9]+\\|Port-Channel[0-9]+\\|[a-zA-Z]+thernet\\([0-9]+\\S+\\)+\\>" . cisco-router-interface-face)
)
"Font locking definitions for cisco router mode")
;; a-zA-Z]*Ethernet\\([0-9]+\\S+[0-9]+\\)+\\
;; Imenu definitions.
;; Not sure what this is doing.
(defvar cisco-router-imenu-expression
'(
("Interfaces" "^[\t ]*interface *\\(.*\\)" 1)
("VRFs" "^ip vrf *\\(.*\\)" 1)
("Controllers" "^[\t ]*controller *\\(.*\\)" 1)
("Routing protocols" "^router *\\(.*\\)" 1)
("Class maps" "^class-map *\\(.*\\)" 1)
("Policy maps" "^policy-map *\\(.*\\)" 1)
))
;; Indentation definitions.
;; This part seems good. I don't think I will make any changes here.
(defun cisco-router-indent-line ()
"Indent current line as cisco router config line"
(let ((indent0 "^interface\\|redundancy\\|^line\\|^ip vrf \\|^controller\\|^class-map\\|^policy-map\\|router\\|access-list\\|route-map")
(indent1 " *main-cpu\\| *class\\W"))
(beginning-of-line)
(let ((not-indented t)
(cur-indent 0))
(cond ((or (bobp) (looking-at indent0) (looking-at "!")) ; Handles the indent0 and indent1 lines
; (message "Indent0")
(setq not-indented nil
cur-indent 0))
((looking-at indent1)
; (message "Indent1")
(setq not-indented nil
cur-indent 1)))
(save-excursion ; Indents regular lines depending on the block they're in.
(while not-indented
(forward-line -1)
(cond ((looking-at indent1)
; (message "Indent1 block")
(setq cur-indent 2
not-indented nil))
((looking-at indent0)
; (message "Indent0 block")
(setq cur-indent 1
not-indented nil))
((looking-at "!")
; (message "Reached !")
(setq cur-indent 0
not-indented nil))
((bobp)
; (message "Buffer beginning reached")
(setq cur-indent 0
not-indented nil)))))
(indent-line-to cur-indent))))
;; Custom syntax table
(defvar cisco-router-mode-syntax-table (make-syntax-table)
"Syntax table for cisco router mode")
(modify-syntax-entry ?_ "w" cisco-router-mode-syntax-table) ;All _'s are part of words.
(modify-syntax-entry ?- "w" cisco-router-mode-syntax-table) ;All -'s are part of words.
(modify-syntax-entry ?! "<" cisco-router-mode-syntax-table) ;All !'s start comments.
(modify-syntax-entry ?\n ">" cisco-router-mode-syntax-table) ;All newlines end comments.
(modify-syntax-entry ?\r ">" cisco-router-mode-syntax-table) ;All linefeeds end comments.
;; Entry point
(defun cisco-router-mode ()
"Major mode for editing Cisco router configuration files"
(interactive)
(kill-all-local-variables)
(set-syntax-table cisco-router-mode-syntax-table)
(use-local-map cisco-router-mode-map)
(set (make-local-variable 'font-lock-defaults) '(cisco-router-font-lock-keywords))
(set (make-local-variable 'indent-line-function) 'cisco-router-indent-line)
(set (make-local-variable 'comment-start) "!")
(set (make-local-variable 'comment-start-skip) "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)!+ *")
(setq imenu-case-fold-search nil)
(set (make-local-variable 'imenu-generic-expression) cisco-router-imenu-expression)
(imenu-add-to-menubar "Imenu")
(setq major-mode 'cisco-router-mode
mode-name "Cisco IOS configuration")
(run-hooks cisco-router-mode-hook))
(add-to-list 'auto-mode-alist '("\\.cfg\\'" . cisco-router-mode))
(provide 'cisco-router-mode)
;;; cisco-router-mode.el ends here