Download
(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")
(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")
(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 "lightsteelblue" ))
)
"Face for basic router commands")
(defface cisco-router-no-face
'(
(t (:underline t))
)
"Face for \"no\"")
(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\\|s\\(?:ervice\\|nmp-server\\)\\|v\\(?:ersion\\|tp\\)\\)\\>" . cisco-router-command-face)
'("\\<\\(no\\)\\>" . cisco-router-no-face)
'("\\<\\([0-9]\\{1,3\\}\\.[0-9]\\{1,3\\}\\.[0-9]\\{1,3\\}\\.[0-9]\\{1,3\\}\\)\\>" . cisco-router-ipadd-face)
)
"Font locking definitions for cisco router mode")
(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)
))
(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 "!"))
(setq not-indented nil
cur-indent 0))
((looking-at indent1)
(setq not-indented nil
cur-indent 1)))
(save-excursion
(while not-indented
(forward-line -1)
(cond ((looking-at indent1)
(setq cur-indent 2
not-indented nil))
((looking-at indent0)
(setq cur-indent 1
not-indented nil))
((looking-at "!")
(setq cur-indent 0
not-indented nil))
((bobp)
(setq cur-indent 0
not-indented nil)))))
(indent-line-to cur-indent))))
(defvar cisco-router-mode-syntax-table (make-syntax-table)
"Syntax table for cisco router mode")
(modify-syntax-entry ?_ "w" cisco-router-mode-syntax-table)
(modify-syntax-entry ?- "w" cisco-router-mode-syntax-table)
(modify-syntax-entry ?! "<" cisco-router-mode-syntax-table)
(modify-syntax-entry ?\n ">" cisco-router-mode-syntax-table)
(modify-syntax-entry ?\r ">" cisco-router-mode-syntax-table)
(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 router configuration")
(run-hooks cisco-router-mode-hook))
(add-to-list 'auto-mode-alist '("\\.cfg\\'" . cisco-router-mode))
(provide 'cisco-router-mode)