]> gitweb.michael.orlitzky.com - nagios-mode.git/blob - nagios-mode.el
26a9d9e89ba5137546da317e131498ec39235a61
[nagios-mode.git] / nagios-mode.el
1 (require 'apropos)
2 (require 'regexp-opt)
3 (require 'font-lock)
4
5
6 ;;;;;;;;;;;;;;;;;;;;;;
7 ;; CUSTOM VARIABLES ;;
8 ;;;;;;;;;;;;;;;;;;;;;;
9
10 (defcustom nagios-indent-level 2
11 "*Number of spaces in one indentation (tab)."
12 :type 'integer :group 'nagios
13 )
14
15
16
17 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
18 ;; INDENTATION FUNCTIONS ;;
19 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
20
21 (defun nagios-indent-line(&optional flag)
22 "Indents a line, taking nesting into account."
23 (nagios-indent-to (nagios-calculate-indent))
24 )
25
26
27
28 (defun nagios-indent-to(indent-column)
29 "Indent the current line to column indent-column."
30 (setq pos (point))
31 (beginning-of-line)
32 (setq bol (point))
33 (setq pos-offset (- pos bol))
34
35 (setq first-char-offset
36 (skip-chars-forward " \t"))
37
38 (setq first-char-pos
39 (+ bol first-char-offset))
40
41 (kill-region bol first-char-pos)
42
43 (beginning-of-line)
44
45 (setq pos-change (- indent-column first-char-offset))
46 (setq pos-offset (+ pos-offset pos-change))
47
48 (if (<= pos-offset indent-column)
49 (setq pos-offset indent-column))
50
51 (while (< 0 indent-column)
52 (insert " ")
53 (setq indent-column (- indent-column 1)))
54
55 (goto-char (+ bol pos-offset))
56
57 )
58
59
60
61 (defun nagios-in-block()
62 "Determine if the point is inside of a {} block."
63 (setq pos (point))
64
65 ;; Get the position of the last opening and closing braces, with
66 ;; respect to the current point
67 (setq last-opening-brace (re-search-backward "{" nil t))
68 (goto-char pos)
69
70 (setq last-closing-brace (re-search-backward "}" nil t))
71 (goto-char pos)
72
73 ;; If either is nil (not found) just set it to -1, so the comparison
74 ;; doesn't die.
75 (if (not last-opening-brace)
76 (setq last-opening-brace -1))
77
78 (if (not last-closing-brace)
79 (setq last-closing-brace -1))
80
81 ;; If the last brace seen in the buffer is an opening brace, we're
82 ;; in a block. Otherwise, we aren't.
83 (if (>= last-closing-brace last-opening-brace)
84 nil
85 t)
86 )
87
88
89
90 (defun nagios-calculate-indent()
91 "Calculate the level of indentation."
92 ;; We're either inside a block, or we aren't.
93
94 (setq indent 0)
95
96 (if (nagios-in-block)
97 (setq indent nagios-indent-level))
98
99 (setq pos (point))
100 (end-of-line)
101 (setq eol (point))
102 (beginning-of-line)
103 (setq bol (point))
104
105 ;; Set the indentation level to 0 if we find either brace on this
106 ;; line.
107 (if (re-search-forward "[{}]" eol t)
108 (setq indent 0))
109
110 (goto-char pos)
111
112 indent
113 )
114
115
116
117 ;;;;;;;;;;;;
118 ;; KEYMAP ;;
119 ;;;;;;;;;;;;
120
121 (defun nagios-insert-right-brace-and-indent()
122 "Insert a '}' character, and indent the line."
123 (interactive)
124 (insert "}")
125 (nagios-indent-line)
126 )
127
128
129
130 (defvar nagios-mode-map()
131 "Keymap used in nagios mode.")
132
133 (when (not nagios-mode-map)
134 (setq nagios-mode-map (make-sparse-keymap))
135 (define-key nagios-mode-map (read-kbd-macro "}") 'nagios-insert-right-brace-and-indent)
136 )
137
138
139
140 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
141 ;; REGEXP TRANSFORMATIONS ;;
142 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
143
144 (defun regexp-word(regexp)
145 "Takes a regular expression as an argument, and adds the word boundary condition to the beginning and end of it."
146
147 (concat "\\<\\(" regexp "\\)\\>")
148 )
149
150
151
152 (defun regexp-alt-raw(element-list)
153 "Takes a list of elements, and returns the string '\\(element1\\|element2...\\)'"
154
155 (let ((regexp "\\("))
156 (mapcar (lambda(elem)
157 (setq regexp (concat regexp "\\(" elem "\\)" "\\|")))
158 element-list)
159 (concat (substring regexp 0 -2) ; Cut the last "\\|"
160 "\\)")
161 )
162 )
163
164
165
166 ;;;;;;;;;;;;;;;;;;;;;
167 ;; SYNTAX PATTERNS ;;
168 ;;;;;;;;;;;;;;;;;;;;;
169
170 (defconst nagios-comments
171 (eval-when-compile
172 (regexp-alt-raw
173 '("#+.*"
174 ";+.*")))
175 )
176
177
178
179 (defconst nagios-directives
180 (eval-when-compile
181 (regexp-opt
182 '("active_checks_enabled" "address" "alias" "check_command"
183 "check_freshness" "check_period" "checks_enabled" "command_line"
184 "command_name" "contact_groups" "contact_name" "contactgroup_name"
185 "dependent_host_name" "dependent_service_description"
186 "email" "event_handler" "event_handler_enabled"
187 "execution_failure_criteria" "first_notification"
188 "flap_detection_enabled" "freshness_threshold"
189 "friday" "high_flap_threshold" "host_name"
190 "host_notification_commands"
191 "host_notification_options"
192 "host_notification_period" "hostgroup_name"
193 "is_volatile" "last_notification"
194 "low_flap_threshold" "max_check_attempts"
195 "members" "monday" "normal_check_interval"
196 "notification_failure_criteria"
197 "notification_interval" "notification_options"
198 "notification_period" "notifications_enabled"
199 "obsess_over_service" "pager" "parallelize_check"
200 "parents" "passive_checks_enabled"
201 "process_perf_data" "retain_nonstatus_information"
202 "retain_status_information" "retry_check_interval"
203 "saturday" "service_description"
204 "service_notification_commands"
205 "service_notification_options"
206 "service_notification_period" "stalking_options"
207 "sunday" "thursday" "timeperiod_name" "tuesday" "wednesday")))
208 )
209
210
211
212 (defvar nagios-macros
213 (eval-when-compile
214 (regexp-alt-raw
215 '("\\$CONTACT\\(NAME\\|ALIAS\\|EMAIL\\|PAGER\\)\\$"
216 "\\$HOST\\(NAME\\|ALIAS\\|ADDRESS\\|STATE\\)\\$"
217 "\\$\\(ARG\\|USER\\)\\([1-9]\\|[1-2][0-9]\\|3[0-2]\\)\\$"
218 "\\$SERVICE\\(DESC\\|STATE\\)\\$"
219 "\\$\\(OUTPUT\\|PERFDATA\\|EXECUTIONTIME\\|LATENCY\\)\\$"
220 "\\$NOTIFICATION\\(TYPE\\|NUMBER\\)\\$"
221 "\\$\\(\\(SHORT\\)?DATETIME\\|DATE\\|TIME\\|TIMET\\)\\$"
222 "\\$\\(LASTSTATECHANGE\\|STATETYPE\\)\\$"
223 "\\$ADMIN\\(EMAIL\\|PAGER\\)\\$"
224 "\\$\\(SERVICE\\|HOST\\)ATTEMPT\\$")))
225 )
226
227
228
229 (defvar nagios-definitions
230 (eval-when-compile
231 (regexp-alt-raw
232 '("define +\\(host\\|service\\|timeperiod\\|contact\\|command\\)"
233 "define +\\(host\\|contact\\)group"
234 "define +\\(service\\|host\\)dependency"
235 "define +\\(service\\|host\\|hostgroup\\)escalation")))
236 )
237
238
239
240 (defvar nagios-special
241 (eval-when-compile
242 (regexp-opt
243 '("name" "register" "use")))
244 )
245
246
247
248 ;;;;;;;;;;;;;;;;;;;;;;;;
249 ;; FONT LOCK VARIABLE ;;
250 ;;;;;;;;;;;;;;;;;;;;;;;;
251
252 (defvar nagios-font-lock-keywords
253 (list
254 (cons nagios-comments font-lock-comment-face)
255 (cons (regexp-word nagios-directives) font-lock-variable-name-face)
256 (cons (regexp-word nagios-macros) font-lock-doc-face)
257 (cons (regexp-word nagios-definitions) font-lock-function-name-face)
258 (cons (regexp-word nagios-special) font-lock-keyword-face))
259
260 "Rules for highlighting Nagios configuration files."
261 )
262
263
264
265 ;;;;;;;;;;;;;;;;;;;;;;;;
266 ;; MAIN MODE FUNCTION ;;
267 ;;;;;;;;;;;;;;;;;;;;;;;;
268
269 (defun nagios-mode()
270 "Major mode for editing Nagios configuration files."
271
272 (interactive)
273
274 ; Initializing
275 (kill-all-local-variables)
276
277 ; Setting up indentation handling
278 (make-local-variable 'indent-line-function)
279 (setq indent-line-function 'nagios-indent-line)
280
281 ; Setting up font-locking
282 (make-local-variable 'font-lock-defaults)
283 (setq font-lock-defaults '(nagios-font-lock-keywords nil t nil nil))
284
285
286 ; Setting up syntax recognition
287 (make-local-variable 'comment-start)
288 (make-local-variable 'comment-end)
289 (make-local-variable 'comment-start-skip)
290
291 (setq comment-start "# ")
292 (setq comment-end "")
293 (setq comment-start-skip nagios-comments)
294
295
296 ;; Keyboard Mapping
297 (use-local-map nagios-mode-map)
298
299
300 ; Setting up syntax table
301 (modify-syntax-entry ?* ". 23")
302 (modify-syntax-entry ?/ ". 14")
303
304 ; Final stuff, then we're done
305 (setq mode-name "nagios"
306 major-mode 'nagios-mode)
307
308 (run-hooks 'nagios-mode-hook)
309 )
310
311
312 (provide 'nagios-mode)