10 (defcustom nagios-indent-level 2
11 "*Number of spaces in one indentation (tab)."
12 :type 'integer :group 'nagios
17 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
18 ;; INDENTATION FUNCTIONS ;;
19 ;;;;;;;;;;;;;;;;;;;;;;;;;;;
21 (defun nagios-indent-line(&optional flag)
22 "Indents a line, taking nesting into account."
23 (nagios-indent-to (nagios-calculate-indent))
28 (defun nagios-indent-to(indent-column)
29 "Indent the current line to column indent-column."
33 (setq pos-offset (- pos bol))
35 (setq first-char-offset
36 (skip-chars-forward " \t"))
39 (+ bol first-char-offset))
41 (kill-region bol first-char-pos)
45 (setq pos-change (- indent-column first-char-offset))
46 (setq pos-offset (+ pos-offset pos-change))
48 (if (<= pos-offset indent-column)
49 (setq pos-offset indent-column))
51 (while (< 0 indent-column)
53 (setq indent-column (- indent-column 1)))
55 (goto-char (+ bol pos-offset))
61 (defun nagios-in-block()
62 "Determine if the point is inside of a {} block."
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))
70 (setq last-closing-brace (re-search-backward "}" nil t))
73 ;; If either is nil (not found) just set it to -1, so the comparison
75 (if (not last-opening-brace)
76 (setq last-opening-brace -1))
78 (if (not last-closing-brace)
79 (setq last-closing-brace -1))
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)
90 (defun nagios-calculate-indent()
91 "Calculate the level of indentation."
92 ;; We're either inside a block, or we aren't.
97 (setq indent nagios-indent-level))
105 ;; Set the indentation level to 0 if we find either brace on this
107 (if (re-search-forward "[{}]" eol t)
121 (defun nagios-insert-right-brace-and-indent()
122 "Insert a '}' character, and indent the line."
130 (defvar nagios-mode-map()
131 "Keymap used in nagios mode.")
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)
140 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
141 ;; REGEXP TRANSFORMATIONS ;;
142 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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."
147 (concat "\\<\\(" regexp "\\)\\>")
152 (defun regexp-alt-raw(element-list)
153 "Takes a list of elements, and returns the string '\\(element1\\|element2...\\)'"
155 (let ((regexp "\\("))
156 (mapcar (lambda(elem)
157 (setq regexp (concat regexp "\\(" elem "\\)" "\\|")))
159 (concat (substring regexp 0 -2) ; Cut the last "\\|"
166 ;;;;;;;;;;;;;;;;;;;;;
167 ;; SYNTAX PATTERNS ;;
168 ;;;;;;;;;;;;;;;;;;;;;
170 (defconst nagios-comments
179 (defconst nagios-directives
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")))
212 (defvar nagios-macros
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\\$")))
229 (defvar nagios-definitions
232 '("define +\\(host\\|service\\|timeperiod\\|contact\\|command\\)"
233 "define +\\(host\\|contact\\)group"
234 "define +\\(service\\|host\\)dependency"
235 "define +\\(service\\|host\\|hostgroup\\)escalation")))
240 (defvar nagios-special
243 '("name" "register" "use")))
248 ;;;;;;;;;;;;;;;;;;;;;;;;
249 ;; FONT LOCK VARIABLE ;;
250 ;;;;;;;;;;;;;;;;;;;;;;;;
252 (defvar nagios-font-lock-keywords
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))
260 "Rules for highlighting Nagios configuration files."
265 ;;;;;;;;;;;;;;;;;;;;;;;;
266 ;; MAIN MODE FUNCTION ;;
267 ;;;;;;;;;;;;;;;;;;;;;;;;
270 "Major mode for editing Nagios configuration files."
275 (kill-all-local-variables)
277 ; Setting up indentation handling
278 (make-local-variable 'indent-line-function)
279 (setq indent-line-function 'nagios-indent-line)
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))
286 ; Setting up syntax recognition
287 (make-local-variable 'comment-start)
288 (make-local-variable 'comment-end)
289 (make-local-variable 'comment-start-skip)
291 (setq comment-start "# ")
292 (setq comment-end "")
293 (setq comment-start-skip nagios-comments)
297 (use-local-map nagios-mode-map)
300 ; Setting up syntax table
301 (modify-syntax-entry ?* ". 23")
302 (modify-syntax-entry ?/ ". 14")
304 ; Final stuff, then we're done
305 (setq mode-name "nagios"
306 major-mode 'nagios-mode)
308 (run-hooks 'nagios-mode-hook)
312 (provide 'nagios-mode)