-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyang-mode.el
More file actions
313 lines (279 loc) · 9.15 KB
/
Copy pathyang-mode.el
File metadata and controls
313 lines (279 loc) · 9.15 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
;;; yang-mode.el --- Major mode for editing YANG modules
;; This program 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; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;; Note: The interface used in this file requires CC Mode 5.30 or
;; later.
;; Version:
;; 00.4 - 2010-04-30
;; draft-ietf-netmod-yang-12 compliant,
;; added instructions for emacs 23
;; 00.3 - 2009-12-19
;; draft-ietf-netmod-yang-09 compliant,
;; 00.2 - 2008-11-04
;; draft-ietf-netmod-yang-02 compliant.
;; 00.1 - 2007-11-14
;; Initial version, draft-bjorklund-netconf-yang-00 compliant.
;; Useful tips:
;;
;; For use with emacs 23, put this in your .emacs:
;; (autoload 'yang-mode "yang-mode" "Major mode for editing YANG modules." t)
;; (add-to-list 'auto-mode-alist '("\\.yang$" . yang-mode))
;;
;; Some users have reported other errors with emacs 23, and have found
;; that removing the byte-compiled cc-mode.elc file fixes these problems.
;; (e.g. /usr/share/emacs/23.1/lisp/progmodes/cc-mode.elc)
;;
;; For use with emacs 22, just use:
;; (require 'yang-mode nil t)
;;
;; For editing somewhat larger YANG modules, add this to your .emacs
;; (setq blink-matching-paren-distance nil)
;;
;; Common YANG layout:
;; (defun my-yang-mode-hook ()
;; "Configuration for YANG Mode. Add this to `yang-mode-hook'."
;; (if window-system
;; (progn
;; (c-set-style "BSD")
;; (setq indent-tabs-mode nil)
;; (setq c-basic-offset 2)
;; (setq font-lock-maximum-decoration t)
;; (font-lock-mode t))))
;;
;; (add-hook 'yang-mode-hook 'my-yang-mode-hook)
;;
;; Use the oultine minor mode for YANG.
;; This is very useful to get a good overview of the structure of
;; a module.
;;
;; Put this in your .emacs:
;;
;; (defun show-onelevel ()
;; "show entry and children in outline mode"
;; (interactive)
;; (show-entry)
;; (show-children))
;;
;; (defun my-outline-bindings ()
;; "sets shortcut bindings for outline minor mode"
;; (interactive)
;; (local-set-key [?\C-,] 'hide-body)
;; (local-set-key [?\C-.] 'show-all)
;; (local-set-key [C-up] 'outline-previous-visible-heading)
;; (local-set-key [C-down] 'outline-next-visible-heading)
;; (local-set-key [C-left] 'hide-subtree)
;; (local-set-key [C-right] 'show-onelevel)
;; (local-set-key [M-up] 'outline-backward-same-level)
;; (local-set-key [M-down] 'outline-forward-same-level)
;; (local-set-key [M-left] 'hide-subtree)
;; (local-set-key [M-right] 'show-subtree))
;;
;; (add-hook
;; 'outline-minor-mode-hook
;; 'my-outline-bindings)
;;
;; (defconst sort-of-yang-identifier-regexp "[-a-zA-Z0-9_\\.:]*")
;;
;; (add-hook
;; 'yang-mode-hook
;; '(lambda ()
;; (outline-minor-mode)
;; (setq outline-regexp
;; (concat "^ *" sort-of-yang-identifier-regexp " *"
;; sort-of-yang-identifier-regexp
;; " *{"))))
;;; Code:
(require 'cc-mode)
;; These are only required at compile time to get the sources for the
;; language constants. (The cc-fonts require and the font-lock
;; related constants could additionally be put inside an
;; (eval-after-load "font-lock" ...) but then some trickery is
;; necessary to get them compiled.)
(eval-when-compile
(require 'cc-langs)
(require 'cc-fonts))
(eval-and-compile
;; Make our mode known to the language constant system. Use Java
;; mode as the fallback for the constants we don't change here.
;; This needs to be done also at compile time since the language
;; constants are evaluated then.
(c-add-language 'yang-mode 'java-mode))
;; YANG has no primitive types in the C/Java sense.
(c-lang-defconst c-primitive-type-kwds
yang '())
(c-lang-defconst c-modifier-kwds
yang '())
(c-lang-defconst c-label-kwds
yang '())
(c-lang-defconst c-before-label-kwds
yang '())
(c-lang-defconst c-class-decl-kwds
yang '(
"anyxml"
"argument"
"augment"
"base"
"belongs-to"
"bit"
"case"
"choice"
"config"
"contact"
"container"
"default"
"description"
"deviate"
"deviation"
"enum"
"error-app-tag"
"error-message"
"extension"
"feature"
"fraction-digits"
"grouping"
"identity"
"if-feature"
"import"
"include"
"input"
"key"
"leaf"
"leaf-list"
"length"
"list"
"mandatory"
"max-elements"
"min-elements"
"module"
"must"
"namespace"
"notification"
"ordered-by"
"organization"
"output"
"path"
"pattern"
"position"
"prefix"
"presence"
"range"
"reference"
"refine"
"require-instance"
"revision"
"revision-date"
"rpc"
"status"
"submodule"
"type"
"typedef"
"unique"
"units"
"uses"
"value"
"when"
"yang-version"
"yin-element"
))
;; No cpp in this language. (The definitions for the extra keywords
;; above are enough to incorporate them into the fontification regexps
;; for types and keywords, so no additional font-lock patterns are
;; required.)
(c-lang-defconst c-cpp-matchers
yang
;; There are some other things in `c-cpp-matchers' besides the
;; preprocessor support, so include it.
(c-lang-const c-cpp-matchers))
;; '-' is part of an identifier in YANG
;; FIXME: how do I make '.' part of the identifier?
(c-lang-defconst c-identifier-syntax-modifications
yang (append '((?- . "w") (?: . "w"))
(c-lang-const c-identifier-syntax-modifications)))
(c-lang-defconst c-symbol-chars
yang (concat c-alnum ":_-"))
;; YANG does not have labels
(c-lang-defconst c-recognize-colon-labels
yang nil)
(defcustom yang-font-lock-extra-types nil
"*List of extra types (aside from the type keywords) to recognize in YANG mode.
Each list item should be a regexp matching a single identifier.")
(defconst yang-font-lock-keywords-1 (c-lang-const c-matchers-1 yang)
"Minimal highlighting for YANG mode.")
(defconst yang-font-lock-keywords-2 (c-lang-const c-matchers-2 yang)
"Fast normal highlighting for YANG mode.")
(defconst yang-font-lock-keywords-3 (c-lang-const c-matchers-3 yang)
"Accurate normal highlighting for YANG mode.")
(defvar yang-font-lock-keywords yang-font-lock-keywords-3
"Default expressions to highlight in YANG mode.")
(defvar yang-mode-syntax-table nil
"Syntax table used in yang-mode buffers.")
(if yang-mode-syntax-table
()
(setq yang-mode-syntax-table
(funcall (c-lang-const c-make-mode-syntax-table yang)))
(modify-syntax-entry ?- "_" yang-mode-syntax-table))
(defvar yang-mode-abbrev-table nil
"Abbreviation table used in yang-mode buffers.")
(c-define-abbrev-table 'yang-mode-abbrev-table
;; Keywords that if they occur first on a line might alter the
;; syntactic context, and which therefore should trig reindentation
;; when they are completed.
'())
(defvar yang-mode-map (let ((map (c-make-inherited-keymap)))
;; Add bindings which are only useful for YANG
map)
"Keymap used in yang-mode buffers.")
(easy-menu-define yang-menu yang-mode-map "YANG Mode Commands"
;; Can use `yang' as the language for `c-mode-menu'
;; since its definition covers any language. In
;; this case the language is used to adapt to the
;; nonexistence of a cpp pass and thus removing some
;; irrelevant menu alternatives.
(cons "YANG" (c-lang-const c-mode-menu yang)))
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.yang\\'" . yang-mode))
;;;###autoload
(defun yang-mode ()
"Major mode for editing YANG modules.
The hook `c-mode-common-hook' is run with no args at mode
initialization, then `yang-mode-hook'.
Key bindings:
\\{yang-mode-map}"
(interactive)
(kill-all-local-variables)
(c-initialize-cc-mode t)
(set-syntax-table yang-mode-syntax-table)
(setq major-mode 'yang-mode
mode-name "YANG"
local-abbrev-table yang-mode-abbrev-table
abbrev-mode t)
(use-local-map c-mode-map)
;; `c-init-language-vars' is a macro that is expanded at compile
;; time to a large `setq' with all the language variables and their
;; customized values for our language.
(c-init-language-vars yang-mode)
;; `c-common-init' initializes most of the components of a CC Mode
;; buffer, including setup of the mode menu, font-lock, etc.
;; There's also a lower level routine `c-basic-common-init' that
;; only makes the necessary initialization to get the syntactic
;; analysis and similar things working.
(c-common-init 'yang-mode)
(easy-menu-add yang-menu)
(run-hooks 'c-mode-common-hook)
(run-hooks 'yang-mode-hook)
(c-update-modeline))
(provide 'yang-mode)
;;; yang.el ends here