]> gitweb.michael.orlitzky.com - nagios-mode.git/blob - nagios-mode.el
Eliminate the last use of regexp-alt-raw and the function itself.
[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 (delete-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 last-opening-brace()
78 ;; Get the position of the last opening brace, with
79 ;; respect to the current point.
80 (save-excursion
81 (let ((lob (re-search-backward "{" nil t)))
82 (if lob
83 lob
84 -1)
85 )
86 )
87 )
88
89 (defun last-closing-brace()
90 ;; Get the position of the last closing brace, with
91 ;; respect to the current point.
92 (save-excursion
93 (let ((lcb (re-search-backward "}" nil t)))
94 (if lcb
95 lcb
96 -1)
97 )
98 )
99 )
100
101 (defun nagios-in-block()
102 "Determine if the point is inside of a {} block."
103
104 ;; If the last brace seen in the buffer is an opening brace, we're
105 ;; in a block. Otherwise, we aren't.
106 (if (>= (last-closing-brace) (last-opening-brace))
107 nil
108 t)
109 )
110
111
112
113 (defun nagios-calculate-indent()
114 "Calculate the level of indentation."
115 ;; We're either inside a block, or we aren't.
116
117 (setq indent 0)
118
119 (if (nagios-in-block)
120 (setq indent nagios-indent-level))
121
122 (setq pos (point))
123 (end-of-line)
124 (setq eol (point))
125 (beginning-of-line)
126 (setq bol (point))
127
128 ;; Set the indentation level to 0 if we find either brace on this
129 ;; line.
130 (if (re-search-forward "[{}]" eol t)
131 (setq indent 0))
132
133 (goto-char pos)
134
135 indent
136 )
137
138
139
140 ;; Keymaps
141
142 (defun nagios-insert-right-brace-and-indent()
143 "Insert a '}' character, and indent the line."
144 (interactive)
145 (insert "}")
146 (nagios-indent-line)
147 )
148
149
150 (defvar nagios-mode-map()
151 "Keymap used in nagios mode.")
152
153 (when (not nagios-mode-map)
154 (setq nagios-mode-map (make-sparse-keymap))
155 (define-key nagios-mode-map
156 (read-kbd-macro "}")
157 'nagios-insert-right-brace-and-indent)
158 )
159
160
161
162 (defconst nagios-directives
163 (eval-when-compile
164 (concat "^[ \t\r\n]*"
165
166 (regexp-opt
167 '("active_checks_enabled" "address" "alias" "check_command"
168 "check_freshness" "check_interval" "check_period" "checks_enabled"
169 "command_line" "command_name" "contactgroups" "contact_groups"
170 "contactgroup_members" "contact_name" "contactgroup_name" "contacts"
171 "dependent_host_name" "dependent_service_description" "email"
172 "event_handler" "event_handler_enabled" "execution_failure_criteria"
173 "failure_prediction_enabled" "first_notification"
174 "first_notification_delay" "flap_detection_enabled" "freshness_threshold"
175 "friday" "high_flap_threshold" "host_name" "host_notification_commands"
176 "host_notification_options" "host_notification_period"
177 "host_notifications_enabled" "hostgroup_name" "hostgroups"
178 "is_volatile" "last_notification" "low_flap_threshold"
179 "max_check_attempts" "members" "monday" "normal_check_interval"
180 "notes" "notification_failure_criteria"
181 "notification_interval" "notification_options"
182 "notification_period" "notifications_enabled"
183 "obsess_over_service" "pager" "parallelize_check"
184 "parents" "passive_checks_enabled"
185 "process_perf_data" "retain_nonstatus_information"
186 "retain_status_information" "retry_check_interval"
187 "retry_interval" "saturday" "service_description"
188 "service_notification_commands" "service_notification_options"
189 "service_notification_period" "service_notifications_enabled"
190 "servicegroup_name" "stalking_options"
191 "sunday" "thursday" "timeperiod_name" "tuesday" "wednesday") t)
192
193 "[ \r\n\t]+")
194 )
195 )
196
197
198
199 (defconst nagios-macros
200 (eval-when-compile
201 (regexp-opt
202 '("$ADMINEMAIL$"
203 "$ADMINPAGER$"
204 "$ARG1$"
205 "$ARG10$"
206 "$ARG11$"
207 "$ARG12$"
208 "$ARG13$"
209 "$ARG14$"
210 "$ARG15$"
211 "$ARG16$"
212 "$ARG17$"
213 "$ARG18$"
214 "$ARG19$"
215 "$ARG2$"
216 "$ARG20$"
217 "$ARG21$"
218 "$ARG22$"
219 "$ARG23$"
220 "$ARG24$"
221 "$ARG25$"
222 "$ARG26$"
223 "$ARG27$"
224 "$ARG28$"
225 "$ARG29$"
226 "$ARG3$"
227 "$ARG30$"
228 "$ARG31$"
229 "$ARG32$"
230 "$ARG4$"
231 "$ARG5$"
232 "$ARG6$"
233 "$ARG7$"
234 "$ARG8$"
235 "$ARG9$"
236 "$COMMANDFILE$"
237 "$CONTACTALIAS$"
238 "$CONTACTEMAIL$"
239 "$CONTACTGROUPALIAS$"
240 "$CONTACTGROUPMEMBERS$"
241 "$CONTACTGROUPNAME$"
242 "$CONTACTGROUPNAMES$"
243 "$CONTACTNAME$"
244 "$CONTACTPAGER$"
245 "$DATE$"
246 "$EVENTSTARTTIME$"
247 "$HOSTACKAUTHOR$"
248 "$HOSTACKAUTHORALIAS$"
249 "$HOSTACKAUTHORNAME$"
250 "$HOSTACKCOMMENT$"
251 "$HOSTACTIONURL$"
252 "$HOSTADDRESS$"
253 "$HOSTALIAS$"
254 "$HOSTATTEMPT$"
255 "$HOSTCHECKCOMMAND$"
256 "$HOSTCHECKTYPE$"
257 "$HOSTDISPLAYNAME$"
258 "$HOSTDOWNTIME$"
259 "$HOSTDURATION$"
260 "$HOSTDURATIONSEC$"
261 "$HOSTEVENTID$"
262 "$HOSTEXECUTIONTIME$"
263 "$HOSTGROUPACTIONURL$"
264 "$HOSTGROUPALIAS$"
265 "$HOSTGROUPMEMBERS$"
266 "$HOSTGROUPNAME$"
267 "$HOSTGROUPNAMES$"
268 "$HOSTGROUPNOTES$"
269 "$HOSTGROUPNOTESURL$"
270 "$HOSTLATENCY$"
271 "$HOSTNAME$"
272 "$HOSTNOTES$"
273 "$HOSTNOTESURL$"
274 "$HOSTNOTIFICATIONID$"
275 "$HOSTNOTIFICATIONNUMBER$"
276 "$HOSTOUTPUT$"
277 "$HOSTPERCENTCHANGE$"
278 "$HOSTPERFDATA$"
279 "$HOSTPERFDATAFILE$"
280 "$HOSTPROBLEMID$"
281 "$HOSTSTATE$"
282 "$HOSTSTATEID$"
283 "$HOSTSTATETYPE$"
284 "$ISVALIDTIME$"
285 "$LASTHOSTCHECK$"
286 "$LASTHOSTDOWN$"
287 "$LASTHOSTEVENTID$"
288 "$LASTHOSTPROBLEMID$"
289 "$LASTHOSTSTATE$"
290 "$LASTHOSTSTATECHANGE$"
291 "$LASTHOSTSTATEID$"
292 "$LASTHOSTUNREACHABLE$"
293 "$LASTHOSTUP$"
294 "$LASTSERVICECHECK$"
295 "$LASTSERVICECRITICAL$"
296 "$LASTSERVICEEVENTID$"
297 "$LASTSERVICEOK$"
298 "$LASTSERVICEPROBLEMID$"
299 "$LASTSERVICESTATE$"
300 "$LASTSERVICESTATECHANGE$"
301 "$LASTSERVICESTATEID$"
302 "$LASTSERVICEUNKNOWN$"
303 "$LASTSERVICEWARNING$"
304 "$LOGFILE$"
305 "$LONGDATETIME$"
306 "$LONGHOSTOUTPUT$"
307 "$LONGSERVICEOUTPUT$"
308 "$MAINCONFIGFILE$"
309 "$MAXHOSTATTEMPTS$"
310 "$MAXSERVICEATTEMPTS$"
311 "$NEXTVALIDTIME$"
312 "$NOTIFICATIONAUTHOR$"
313 "$NOTIFICATIONAUTHORALIAS$"
314 "$NOTIFICATIONAUTHORNAME$"
315 "$NOTIFICATIONCOMMENT$"
316 "$NOTIFICATIONISESCALATED$"
317 "$NOTIFICATIONNUMBER$"
318 "$NOTIFICATIONRECIPIENTS$"
319 "$NOTIFICATIONTYPE$"
320 "$OBJECTCACHEFILE$"
321 "$PROCESSSTARTTIME$"
322 "$RESOURCEFILE$"
323 "$RETENTIONDATAFILE$"
324 "$SERVICEACKAUTHOR$"
325 "$SERVICEACKAUTHORALIAS$"
326 "$SERVICEACKAUTHORNAME$"
327 "$SERVICEACKCOMMENT$"
328 "$SERVICEACTIONURL$"
329 "$SERVICEATTEMPT$"
330 "$SERVICECHECKCOMMAND$"
331 "$SERVICECHECKTYPE$"
332 "$SERVICEDESC$"
333 "$SERVICEDISPLAYNAME$"
334 "$SERVICEDOWNTIME$"
335 "$SERVICEDURATION$"
336 "$SERVICEDURATIONSEC$"
337 "$SERVICEEVENTID$"
338 "$SERVICEEXECUTIONTIME$"
339 "$SERVICEGROUPACTIONURL$"
340 "$SERVICEGROUPALIAS$"
341 "$SERVICEGROUPMEMBERS$"
342 "$SERVICEGROUPNAME$"
343 "$SERVICEGROUPNAMES$"
344 "$SERVICEGROUPNOTES$"
345 "$SERVICEGROUPNOTESURL$"
346 "$SERVICEISVOLATILE$"
347 "$SERVICELATENCY$"
348 "$SERVICENOTES$"
349 "$SERVICENOTESURL$"
350 "$SERVICENOTIFICATIONID$"
351 "$SERVICENOTIFICATIONNUMBER$"
352 "$SERVICEOUTPUT$"
353 "$SERVICEPERCENTCHANGE$"
354 "$SERVICEPERFDATA$"
355 "$SERVICEPERFDATAFILE$"
356 "$SERVICEPROBLEMID$"
357 "$SERVICESTATE$"
358 "$SERVICESTATEID$"
359 "$SERVICESTATETYPE$"
360 "$SHORTDATETIME$"
361 "$STATUSDATAFILE$"
362 "$TEMPFILE$"
363 "$TEMPPATH$"
364 "$TIME$"
365 "$TIMET$"
366 "$TOTALHOSTPROBLEMS$"
367 "$TOTALHOSTPROBLEMSUNHANDLED$"
368 "$TOTALHOSTSDOWN$"
369 "$TOTALHOSTSDOWNUNHANDLED$"
370 "$TOTALHOSTSERVICES$"
371 "$TOTALHOSTSERVICESCRITICAL$"
372 "$TOTALHOSTSERVICESOK$"
373 "$TOTALHOSTSERVICESUNKNOWN$"
374 "$TOTALHOSTSERVICESWARNING$"
375 "$TOTALHOSTSUNREACHABLE$"
376 "$TOTALHOSTSUNREACHABLEUNHANDLED$"
377 "$TOTALHOSTSUP$"
378 "$TOTALSERVICEPROBLEMS$"
379 "$TOTALSERVICEPROBLEMSUNHANDLED$"
380 "$TOTALSERVICESCRITICAL$"
381 "$TOTALSERVICESCRITICALUNHANDLED$"
382 "$TOTALSERVICESOK$"
383 "$TOTALSERVICESUNKNOWN$"
384 "$TOTALSERVICESUNKNOWNUNHANDLED$"
385 "$TOTALSERVICESWARNING$"
386 "$TOTALSERVICESWARNINGUNHANDLED$"
387 "$USER1$"
388 "$USER10$"
389 "$USER100$"
390 "$USER101$"
391 "$USER102$"
392 "$USER103$"
393 "$USER104$"
394 "$USER105$"
395 "$USER106$"
396 "$USER107$"
397 "$USER108$"
398 "$USER109$"
399 "$USER11$"
400 "$USER110$"
401 "$USER111$"
402 "$USER112$"
403 "$USER113$"
404 "$USER114$"
405 "$USER115$"
406 "$USER116$"
407 "$USER117$"
408 "$USER118$"
409 "$USER119$"
410 "$USER12$"
411 "$USER120$"
412 "$USER121$"
413 "$USER122$"
414 "$USER123$"
415 "$USER124$"
416 "$USER125$"
417 "$USER126$"
418 "$USER127$"
419 "$USER128$"
420 "$USER129$"
421 "$USER13$"
422 "$USER130$"
423 "$USER131$"
424 "$USER132$"
425 "$USER133$"
426 "$USER134$"
427 "$USER135$"
428 "$USER136$"
429 "$USER137$"
430 "$USER138$"
431 "$USER139$"
432 "$USER14$"
433 "$USER140$"
434 "$USER141$"
435 "$USER142$"
436 "$USER143$"
437 "$USER144$"
438 "$USER145$"
439 "$USER146$"
440 "$USER147$"
441 "$USER148$"
442 "$USER149$"
443 "$USER15$"
444 "$USER150$"
445 "$USER151$"
446 "$USER152$"
447 "$USER153$"
448 "$USER154$"
449 "$USER155$"
450 "$USER156$"
451 "$USER157$"
452 "$USER158$"
453 "$USER159$"
454 "$USER16$"
455 "$USER160$"
456 "$USER161$"
457 "$USER162$"
458 "$USER163$"
459 "$USER164$"
460 "$USER165$"
461 "$USER166$"
462 "$USER167$"
463 "$USER168$"
464 "$USER169$"
465 "$USER17$"
466 "$USER170$"
467 "$USER171$"
468 "$USER172$"
469 "$USER173$"
470 "$USER174$"
471 "$USER175$"
472 "$USER176$"
473 "$USER177$"
474 "$USER178$"
475 "$USER179$"
476 "$USER18$"
477 "$USER180$"
478 "$USER181$"
479 "$USER182$"
480 "$USER183$"
481 "$USER184$"
482 "$USER185$"
483 "$USER186$"
484 "$USER187$"
485 "$USER188$"
486 "$USER189$"
487 "$USER19$"
488 "$USER190$"
489 "$USER191$"
490 "$USER192$"
491 "$USER193$"
492 "$USER194$"
493 "$USER195$"
494 "$USER196$"
495 "$USER197$"
496 "$USER198$"
497 "$USER199$"
498 "$USER2$"
499 "$USER20$"
500 "$USER200$"
501 "$USER201$"
502 "$USER202$"
503 "$USER203$"
504 "$USER204$"
505 "$USER205$"
506 "$USER206$"
507 "$USER207$"
508 "$USER208$"
509 "$USER209$"
510 "$USER21$"
511 "$USER210$"
512 "$USER211$"
513 "$USER212$"
514 "$USER213$"
515 "$USER214$"
516 "$USER215$"
517 "$USER216$"
518 "$USER217$"
519 "$USER218$"
520 "$USER219$"
521 "$USER22$"
522 "$USER220$"
523 "$USER221$"
524 "$USER222$"
525 "$USER223$"
526 "$USER224$"
527 "$USER225$"
528 "$USER226$"
529 "$USER227$"
530 "$USER228$"
531 "$USER229$"
532 "$USER23$"
533 "$USER230$"
534 "$USER231$"
535 "$USER232$"
536 "$USER233$"
537 "$USER234$"
538 "$USER235$"
539 "$USER236$"
540 "$USER237$"
541 "$USER238$"
542 "$USER239$"
543 "$USER24$"
544 "$USER240$"
545 "$USER241$"
546 "$USER242$"
547 "$USER243$"
548 "$USER244$"
549 "$USER245$"
550 "$USER246$"
551 "$USER247$"
552 "$USER248$"
553 "$USER249$"
554 "$USER25$"
555 "$USER250$"
556 "$USER251$"
557 "$USER252$"
558 "$USER253$"
559 "$USER254$"
560 "$USER255$"
561 "$USER256$"
562 "$USER26$"
563 "$USER27$"
564 "$USER28$"
565 "$USER29$"
566 "$USER3$"
567 "$USER30$"
568 "$USER31$"
569 "$USER32$"
570 "$USER33$"
571 "$USER34$"
572 "$USER35$"
573 "$USER36$"
574 "$USER37$"
575 "$USER38$"
576 "$USER39$"
577 "$USER4$"
578 "$USER40$"
579 "$USER41$"
580 "$USER42$"
581 "$USER43$"
582 "$USER44$"
583 "$USER45$"
584 "$USER46$"
585 "$USER47$"
586 "$USER48$"
587 "$USER49$"
588 "$USER5$"
589 "$USER50$"
590 "$USER51$"
591 "$USER52$"
592 "$USER53$"
593 "$USER54$"
594 "$USER55$"
595 "$USER56$"
596 "$USER57$"
597 "$USER58$"
598 "$USER59$"
599 "$USER6$"
600 "$USER60$"
601 "$USER61$"
602 "$USER62$"
603 "$USER63$"
604 "$USER64$"
605 "$USER65$"
606 "$USER66$"
607 "$USER67$"
608 "$USER68$"
609 "$USER69$"
610 "$USER7$"
611 "$USER70$"
612 "$USER71$"
613 "$USER72$"
614 "$USER73$"
615 "$USER74$"
616 "$USER75$"
617 "$USER76$"
618 "$USER77$"
619 "$USER78$"
620 "$USER79$"
621 "$USER8$"
622 "$USER80$"
623 "$USER81$"
624 "$USER82$"
625 "$USER83$"
626 "$USER84$"
627 "$USER85$"
628 "$USER86$"
629 "$USER87$"
630 "$USER88$"
631 "$USER89$"
632 "$USER9$"
633 "$USER90$"
634 "$USER91$"
635 "$USER92$"
636 "$USER93$"
637 "$USER94$"
638 "$USER95$"
639 "$USER96$"
640 "$USER97$"
641 "$USER98$"
642 "$USER99$")))
643 )
644
645
646
647 (defconst nagios-definitions
648 (eval-when-compile
649
650 (concat "^[ \t\r\n]*"
651
652 "\\(" ;; Stick parenthesis around whatever comes out
653 ;; of regexp-opt. We use this to match a
654 ;; subexpression during font-lock.
655 (regexp-opt
656 '("define command"
657 "define contact"
658 "define contactgroup"
659 "define host"
660 "define hostdependency"
661 "define hostescalation"
662 "define hostextinfo"
663 "define hostgroup"
664 "define hostgroupescalation"
665 "define null"
666 "define service"
667 "define servicedependency"
668 "define serviceescalation"
669 "define serviceextinfo"
670 "define servicegroup"
671 "define timeperiod"))
672 ;; This closes the parentheses that we opened
673 "\\)" ;; before regexp-opt.
674
675 ;; These can be "terminated" by either an opening curly
676 ;; brace, or a space.
677 "\\({\\| \\)")
678 )
679 )
680
681
682
683 (defconst nagios-special
684 (eval-when-compile
685 (concat "^[ \t\r\n]*"
686
687 (regexp-opt
688 '("name" "register" "use") t)
689
690 "[ \r\n\t]+"))
691 )
692
693
694
695 ;; The One True Font Locking Variable
696
697 (defvar nagios-font-lock-keywords
698 (list
699 (cons nagios-special font-lock-keyword-face)
700 (cons nagios-directives font-lock-variable-name-face)
701 (cons nagios-macros font-lock-constant-face)
702 (cons nagios-definitions '(1 font-lock-function-name-face)))
703
704 "Rules for highlighting Nagios configuration files."
705 )
706
707
708
709 (defvar nagios-mode-syntax-table nil
710 "Syntax table used in nagios-mode buffers.")
711 (if nagios-mode-syntax-table
712 nil
713 (setq nagios-mode-syntax-table (make-syntax-table))
714 (modify-syntax-entry ?# "< b" nagios-mode-syntax-table) ;; Comment style 1
715 (modify-syntax-entry ?\; "< b" nagios-mode-syntax-table) ;; Comment style 2
716 (modify-syntax-entry ?\n "> b" nagios-mode-syntax-table) ;; End comment
717 )
718
719
720 ;; Main Mode Function
721
722 (defun nagios-mode()
723 "Major mode for editing Nagios configuration files."
724
725 (interactive)
726 (kill-all-local-variables)
727 (make-local-variable 'font-lock-defaults)
728 (make-local-variable 'comment-start)
729 (make-local-variable 'comment-start-skip)
730 (make-local-variable 'comment-end)
731 (make-local-variable 'indent-line-function)
732 (make-local-variable 'syntax-begin-function)
733
734 (set-syntax-table nagios-mode-syntax-table)
735
736 (setq mode-name "nagios"
737 major-mode 'nagios-mode
738 indent-line-function 'nagios-indent-line
739 font-lock-defaults '(nagios-font-lock-keywords)
740 comment-start "#"
741 comment-start-skip "#\|; +"
742 comment-end ""
743
744 ;; Since comments and strings do not span multiple lines,
745 ;; the syntax parser can safely start parsing at the beginning
746 ;; of any line.
747 syntax-begin-function 'beginning-of-line
748 )
749
750 ;; Keyboard Mapping
751 (use-local-map nagios-mode-map)
752
753 ;; I don't /think/ I need to define this before attempting
754 ;; to run it. Users can define it if they want.
755 (run-hooks 'nagios-mode-hook)
756 )
757
758
759 (provide 'nagios-mode)