]> gitweb.michael.orlitzky.com - nagios-mode.git/blob - nagios-mode.el
e1573345a61670b5fb8a9989d3abf622c367ecf4
[nagios-mode.git] / nagios-mode.el
1 ;;
2 ;; nagios-mode, an Emacs mode for Nagios <http://www.nagios.org/>
3 ;; configuration files.
4 ;;
5 ;; Copyright Michael Orlitzky
6 ;;
7 ;; http://michael.orlitzky.com/
8 ;;
9 ;; This program is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
13 ;;
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18 ;;
19 ;; http://www.fsf.org/licensing/licenses/gpl.html
20 ;;
21
22 (require 'font-lock)
23 (require 'regexp-opt)
24
25
26 ;; Custom Variables
27
28 (defcustom nagios-indent-level 2
29 "Number of spaces in one indentation (tab)."
30 :type 'integer :group 'nagios
31 )
32
33
34
35 ;; Indentation Voodoo
36
37 (defun nagios-indent-line(&optional flag)
38 "Indents a line, taking nesting into account."
39 (nagios-indent-to (nagios-calculate-indent))
40 )
41
42
43
44 (defun nagios-indent-to(indent-column)
45 "Indent the current line to column indent-column."
46 (setq pos (point))
47 (beginning-of-line)
48 (setq bol (point))
49 (setq pos-offset (- pos bol))
50
51 (setq first-char-offset
52 (skip-chars-forward " \t"))
53
54 (setq first-char-pos
55 (+ bol first-char-offset))
56
57 (kill-region bol first-char-pos)
58
59 (beginning-of-line)
60
61 (setq pos-change (- indent-column first-char-offset))
62 (setq pos-offset (+ pos-offset pos-change))
63
64 (if (<= pos-offset indent-column)
65 (setq pos-offset indent-column))
66
67 (while (< 0 indent-column)
68 (insert " ")
69 (setq indent-column (- indent-column 1)))
70
71 (goto-char (+ bol pos-offset))
72
73 )
74
75
76
77 (defun nagios-in-block()
78 "Determine if the point is inside of a {} block."
79
80 (setq pos (point))
81
82 ;; Get the position of the last opening and closing braces, with
83 ;; respect to the current point
84 (setq last-opening-brace (re-search-backward "{" nil t))
85 (goto-char pos)
86
87 (setq last-closing-brace (re-search-backward "}" nil t))
88 (goto-char pos)
89
90 ;; If either is nil (not found) just set it to -1, so the comparison
91 ;; doesn't die.
92 (if (not last-opening-brace)
93 (setq last-opening-brace -1))
94
95 (if (not last-closing-brace)
96 (setq last-closing-brace -1))
97
98 ;; If the last brace seen in the buffer is an opening brace, we're
99 ;; in a block. Otherwise, we aren't.
100 (if (>= last-closing-brace last-opening-brace)
101 nil
102 t)
103 )
104
105
106
107 (defun nagios-calculate-indent()
108 "Calculate the level of indentation."
109 ;; We're either inside a block, or we aren't.
110
111 (setq indent 0)
112
113 (if (nagios-in-block)
114 (setq indent nagios-indent-level))
115
116 (setq pos (point))
117 (end-of-line)
118 (setq eol (point))
119 (beginning-of-line)
120 (setq bol (point))
121
122 ;; Set the indentation level to 0 if we find either brace on this
123 ;; line.
124 (if (re-search-forward "[{}]" eol t)
125 (setq indent 0))
126
127 (goto-char pos)
128
129 indent
130 )
131
132
133
134 ;; Keymaps
135
136 (defun nagios-insert-right-brace-and-indent()
137 "Insert a '}' character, and indent the line."
138 (interactive)
139 (insert "}")
140 (nagios-indent-line)
141 )
142
143
144 (defvar nagios-mode-map()
145 "Keymap used in nagios mode.")
146
147 (when (not nagios-mode-map)
148 (setq nagios-mode-map (make-sparse-keymap))
149 (define-key nagios-mode-map
150 (read-kbd-macro "}")
151 'nagios-insert-right-brace-and-indent)
152 )
153
154
155
156 ;; Regular Expression Transformations
157
158 (defun regexp-word(regexp)
159 "Takes a regular expression as an argument, and adds the word boundary condition to the beginning and end of it. Newlines are treated as word boundaries."
160
161 ;; This basically joins two expressions in an alternation.
162 ;; The first allows for a newline followed by our regexp (on
163 ;; a word boundary), and the second checks for any-non-word
164 ;; character followed by our regexp.
165 ;;
166 ;; I consider neither a hyphen nor an underscore to be a word
167 ;; boundary for the purpose of syntax highlighting, so I stick
168 ;; the no-hyphens-or-underscores class on each end of the
169 ;; expressions.
170
171 (concat "\\("
172 (concat "\\(^\\<\\(" regexp "\\)\\>[^_-]\\)")
173 "\\|"
174 (concat "\\([^_-]\\<\\(" regexp "\\)\\>[^_-]\\)")
175 "\\)")
176 )
177
178
179 (defun regexp-alt-raw(element-list)
180 "Takes a list of elements, and returns the string '\\(element1\\|element2...\\)'"
181
182 ;; This is necessary since regexp-opt does not accept regular
183 ;; expressions as arguments. We use regexp-opt when we can, of
184 ;; course.
185
186 (let ((regexp "\\("))
187 (mapcar (lambda(elem)
188 (setq regexp (concat regexp "\\(" elem "\\)" "\\|")))
189 element-list)
190 (concat (substring regexp 0 -2) ; Cut the last "\\|"
191 "\\)")
192 )
193 )
194
195
196
197 ;; Syntax Highlighting Patterns
198
199 (defconst nagios-comments
200 (eval-when-compile
201 (regexp-alt-raw
202 '("#+.*"
203 ";+.*")))
204 )
205
206
207 (defconst nagios-directives
208 (eval-when-compile
209 (regexp-opt
210 '("active_checks_enabled" "address" "alias" "check_command"
211 "check_freshness" "check_interval" "check_period" "checks_enabled"
212 "command_line" "command_name" "contact_groups" "contact_name"
213 "contactgroup_name" "contacts" "dependent_host_name"
214 "dependent_service_description" "email" "event_handler"
215 "event_handler_enabled" "execution_failure_criteria"
216 "failure_prediction_enabled" "first_notification"
217 "flap_detection_enabled" "freshness_threshold" "friday"
218 "high_flap_threshold" "host_name" "host_notification_commands"
219 "host_notification_options" "host_notification_period"
220 "host_notifications_enabled" "hostgroup_name" "hostgroups"
221 "is_volatile" "last_notification" "low_flap_threshold"
222 "max_check_attempts" "members" "monday" "normal_check_interval"
223 "notes" "notification_failure_criteria"
224 "notification_interval" "notification_options"
225 "notification_period" "notifications_enabled"
226 "obsess_over_service" "pager" "parallelize_check"
227 "parents" "passive_checks_enabled"
228 "process_perf_data" "retain_nonstatus_information"
229 "retain_status_information" "retry_check_interval"
230 "retry_interval" "saturday" "service_description"
231 "service_notification_commands" "service_notification_options"
232 "service_notification_period" "service_notifications_enabled"
233 "servicegroup_name" "stalking_options"
234 "sunday" "thursday" "timeperiod_name" "tuesday" "wednesday") t))
235 )
236
237
238
239 (defconst nagios-macros
240 (eval-when-compile
241 (regexp-alt-raw
242 '("\\$CONTACT\\(NAME\\|ALIAS\\|EMAIL\\|PAGER\\)\\$"
243 "\\$HOST\\(NAME\\|ALIAS\\|ADDRESS\\|STATE\\)\\$"
244 "\\$\\(ARG\\|USER\\)\\([1-9]\\|[1-2][0-9]\\|3[0-2]\\)\\$"
245 "\\$SERVICE\\(DESC\\|STATE\\)\\$"
246 "\\$\\(OUTPUT\\|PERFDATA\\|EXECUTIONTIME\\|LATENCY\\)\\$"
247 "\\$NOTIFICATION\\(TYPE\\|NUMBER\\)\\$"
248 "\\$\\(\\(SHORT\\)?DATETIME\\|DATE\\|TIME\\|TIMET\\)\\$"
249 "\\$\\(LASTSTATECHANGE\\|STATETYPE\\)\\$"
250 "\\$ADMIN\\(EMAIL\\|PAGER\\)\\$"
251 "\\$\\(SERVICE\\|HOST\\)ATTEMPT\\$")))
252 )
253
254
255
256 (defconst nagios-definitions
257 (eval-when-compile
258 (regexp-alt-raw
259 '("define +\\(host\\|service\\|timeperiod\\|contact\\|command\\)"
260 "define +\\(host\\|contact\\|service\\)group"
261 "define +\\(service\\|host\\)dependency"
262 "define +\\(service\\|host\\|hostgroup\\)escalation")))
263 )
264
265
266
267 (defconst nagios-special
268 (eval-when-compile
269 (regexp-opt
270 '("name" "register" "use") t))
271 )
272
273
274
275 ;; The One True Font Locking Variable
276
277 (defvar nagios-font-lock-keywords
278 (list
279
280 ;; This first bit of ugliness allows us to override any
281 ;; other font-locking with the comment font.
282 (cons nagios-comments '(0 font-lock-comment-delimiter-face t))
283
284 ;; The rest just map regular expressions to font faces.
285 (cons (regexp-word nagios-directives) font-lock-variable-name-face)
286 (cons (regexp-word nagios-macros) font-lock-constant-face)
287 (cons (regexp-word nagios-definitions) font-lock-function-name-face)
288 (cons (regexp-word nagios-special) font-lock-keyword-face))
289
290 "Rules for highlighting Nagios configuration files."
291 )
292
293
294
295 ;; Main Mode Function
296
297 (defun nagios-mode()
298 "Major mode for editing Nagios configuration files."
299
300 (interactive)
301
302 ;; Initializing. This is actually important to cover
303 ;; up some Emacs stupidity. Font locking won't occur
304 ;; without it.
305 (kill-all-local-variables)
306
307 ;; Set up indentation handling using the functions
308 ;; defined earlier.
309 (make-local-variable 'indent-line-function)
310 (setq indent-line-function 'nagios-indent-line)
311
312 ;; Configure font locking. Set the defaults to something
313 ;; sensible, defined earlier.
314 (make-local-variable 'font-lock-defaults)
315 (setq font-lock-defaults '(nagios-font-lock-keywords nil t))
316
317 ;; Keyboard Mapping
318 (use-local-map nagios-mode-map)
319
320 ;; Rock and roll.
321 (setq mode-name "nagios"
322 major-mode 'nagios-mode)
323
324 ;; I don't /think/ I need to define this before attempting
325 ;; to run it. Users can define it if they want.
326 (run-hooks 'nagios-mode-hook)
327 )
328
329
330 (provide 'nagios-mode)