X-Git-Url: https://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=nagios-mode.el;h=a284c621ae9963448ef533c3110cab14f52d4d2f;hb=60bd3688e6a238c56f1eaf94aba651023786d40c;hp=26a9d9e89ba5137546da317e131498ec39235a61;hpb=2710ee7047c17b54f147ca478e9cc14de99514f2;p=nagios-mode.git diff --git a/nagios-mode.el b/nagios-mode.el index 26a9d9e..a284c62 100644 --- a/nagios-mode.el +++ b/nagios-mode.el @@ -1,22 +1,38 @@ -(require 'apropos) -(require 'regexp-opt) +;; +;; nagios-mode, an Emacs mode for Nagios +;; configuration files. +;; +;; Copyright Michael Orlitzky +;; +;; http://michael.orlitzky.com/ +;; +;; 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 3 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. +;; +;; http://www.fsf.org/licensing/licenses/gpl.html +;; + (require 'font-lock) +(require 'regexp-opt) -;;;;;;;;;;;;;;;;;;;;;; -;; CUSTOM VARIABLES ;; -;;;;;;;;;;;;;;;;;;;;;; +;; Custom Variables (defcustom nagios-indent-level 2 - "*Number of spaces in one indentation (tab)." + "Number of spaces in one indentation (tab)." :type 'integer :group 'nagios ) -;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; INDENTATION FUNCTIONS ;; -;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Indentation Voodoo (defun nagios-indent-line(&optional flag) "Indents a line, taking nesting into account." @@ -60,6 +76,7 @@ (defun nagios-in-block() "Determine if the point is inside of a {} block." + (setq pos (point)) ;; Get the position of the last opening and closing braces, with @@ -114,9 +131,7 @@ -;;;;;;;;;;;; -;; KEYMAP ;; -;;;;;;;;;;;; +;; Keymaps (defun nagios-insert-right-brace-and-indent() "Insert a '}' character, and indent the line." @@ -126,32 +141,27 @@ ) - (defvar nagios-mode-map() "Keymap used in nagios mode.") (when (not nagios-mode-map) (setq nagios-mode-map (make-sparse-keymap)) - (define-key nagios-mode-map (read-kbd-macro "}") 'nagios-insert-right-brace-and-indent) + (define-key nagios-mode-map + (read-kbd-macro "}") + 'nagios-insert-right-brace-and-indent) ) -;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; REGEXP TRANSFORMATIONS ;; -;;;;;;;;;;;;;;;;;;;;;;;;;;;; - -(defun regexp-word(regexp) - "Takes a regular expression as an argument, and adds the word boundary condition to the beginning and end of it." - - (concat "\\<\\(" regexp "\\)\\>") - ) - - +;; Regular Expression Transformations (defun regexp-alt-raw(element-list) "Takes a list of elements, and returns the string '\\(element1\\|element2...\\)'" - + + ;; This is necessary since regexp-opt does not accept regular + ;; expressions as arguments. We use regexp-opt when we can, of + ;; course. + (let ((regexp "\\(")) (mapcar (lambda(elem) (setq regexp (concat regexp "\\(" elem "\\)" "\\|"))) @@ -163,53 +173,44 @@ -;;;;;;;;;;;;;;;;;;;;; -;; SYNTAX PATTERNS ;; -;;;;;;;;;;;;;;;;;;;;; - -(defconst nagios-comments - (eval-when-compile - (regexp-alt-raw - '("#+.*" - ";+.*"))) - ) - - - (defconst nagios-directives (eval-when-compile + (concat "^[ \t\r\n]*" + (regexp-opt '("active_checks_enabled" "address" "alias" "check_command" - "check_freshness" "check_period" "checks_enabled" "command_line" - "command_name" "contact_groups" "contact_name" "contactgroup_name" - "dependent_host_name" "dependent_service_description" - "email" "event_handler" "event_handler_enabled" - "execution_failure_criteria" "first_notification" - "flap_detection_enabled" "freshness_threshold" - "friday" "high_flap_threshold" "host_name" - "host_notification_commands" - "host_notification_options" - "host_notification_period" "hostgroup_name" - "is_volatile" "last_notification" - "low_flap_threshold" "max_check_attempts" - "members" "monday" "normal_check_interval" - "notification_failure_criteria" + "check_freshness" "check_interval" "check_period" "checks_enabled" + "command_line" "command_name" "contactgroups" "contact_groups" + "contactgroup_members" "contact_name" "contactgroup_name" "contacts" + "dependent_host_name" "dependent_service_description" "email" + "event_handler" "event_handler_enabled" "execution_failure_criteria" + "failure_prediction_enabled" "first_notification" + "first_notification_delay" "flap_detection_enabled" "freshness_threshold" + "friday" "high_flap_threshold" "host_name" "host_notification_commands" + "host_notification_options" "host_notification_period" + "host_notifications_enabled" "hostgroup_name" "hostgroups" + "is_volatile" "last_notification" "low_flap_threshold" + "max_check_attempts" "members" "monday" "normal_check_interval" + "notes" "notification_failure_criteria" "notification_interval" "notification_options" "notification_period" "notifications_enabled" "obsess_over_service" "pager" "parallelize_check" "parents" "passive_checks_enabled" "process_perf_data" "retain_nonstatus_information" "retain_status_information" "retry_check_interval" - "saturday" "service_description" - "service_notification_commands" - "service_notification_options" - "service_notification_period" "stalking_options" - "sunday" "thursday" "timeperiod_name" "tuesday" "wednesday"))) + "retry_interval" "saturday" "service_description" + "service_notification_commands" "service_notification_options" + "service_notification_period" "service_notifications_enabled" + "servicegroup_name" "stalking_options" + "sunday" "thursday" "timeperiod_name" "tuesday" "wednesday") t) + + "[ \r\n\t]+") + ) ) - -(defvar nagios-macros + +(defconst nagios-macros (eval-when-compile (regexp-alt-raw '("\\$CONTACT\\(NAME\\|ALIAS\\|EMAIL\\|PAGER\\)\\$" @@ -226,85 +227,91 @@ -(defvar nagios-definitions +(defconst nagios-definitions (eval-when-compile + + (concat "^[ \t\r\n]*" + (regexp-alt-raw '("define +\\(host\\|service\\|timeperiod\\|contact\\|command\\)" - "define +\\(host\\|contact\\)group" + "define +\\(host\\|contact\\|service\\)group" "define +\\(service\\|host\\)dependency" - "define +\\(service\\|host\\|hostgroup\\)escalation"))) + "define +\\(service\\|host\\|hostgroup\\)escalation")) + + ;; These can be "terminated" by either an opening curly + ;; brace, or a space. + "\\({\\| \\)") + ) ) -(defvar nagios-special +(defconst nagios-special (eval-when-compile + (concat "^[ \t\r\n]*" + (regexp-opt - '("name" "register" "use"))) + '("name" "register" "use") t) + + "[ \r\n\t]+")) ) -;;;;;;;;;;;;;;;;;;;;;;;; -;; FONT LOCK VARIABLE ;; -;;;;;;;;;;;;;;;;;;;;;;;; +;; The One True Font Locking Variable (defvar nagios-font-lock-keywords (list - (cons nagios-comments font-lock-comment-face) - (cons (regexp-word nagios-directives) font-lock-variable-name-face) - (cons (regexp-word nagios-macros) font-lock-doc-face) - (cons (regexp-word nagios-definitions) font-lock-function-name-face) - (cons (regexp-word nagios-special) font-lock-keyword-face)) - + (cons nagios-special font-lock-keyword-face) + (cons nagios-directives font-lock-variable-name-face) + (cons nagios-macros font-lock-constant-face) + (cons nagios-definitions '(1 font-lock-function-name-face))) + "Rules for highlighting Nagios configuration files." ) -;;;;;;;;;;;;;;;;;;;;;;;; -;; MAIN MODE FUNCTION ;; -;;;;;;;;;;;;;;;;;;;;;;;; +(defvar nagios-mode-syntax-table nil + "Syntax table used in nagios-mode buffers.") +(if nagios-mode-syntax-table + nil + (setq nagios-mode-syntax-table (make-syntax-table)) + (modify-syntax-entry ?# "< b" nagios-mode-syntax-table) ;; Comment style 1 + (modify-syntax-entry ?\; "< b" nagios-mode-syntax-table) ;; Comment style 2 + (modify-syntax-entry ?\n "> b" nagios-mode-syntax-table) ;; End comment + ) + + +;; Main Mode Function (defun nagios-mode() "Major mode for editing Nagios configuration files." (interactive) - - ; Initializing (kill-all-local-variables) - - ; Setting up indentation handling - (make-local-variable 'indent-line-function) - (setq indent-line-function 'nagios-indent-line) - - ; Setting up font-locking (make-local-variable 'font-lock-defaults) - (setq font-lock-defaults '(nagios-font-lock-keywords nil t nil nil)) - - - ; Setting up syntax recognition (make-local-variable 'comment-start) - (make-local-variable 'comment-end) (make-local-variable 'comment-start-skip) + (make-local-variable 'comment-end) + (make-local-variable 'indent-line-function) - (setq comment-start "# ") - (setq comment-end "") - (setq comment-start-skip nagios-comments) - + (set-syntax-table nagios-mode-syntax-table) + + (setq mode-name "nagios" + major-mode 'nagios-mode + indent-line-function 'nagios-indent-line + font-lock-defaults '(nagios-font-lock-keywords) + comment-start "#" + comment-start-skip "#\|; +" + comment-end "" + ) ;; Keyboard Mapping (use-local-map nagios-mode-map) - - ; Setting up syntax table - (modify-syntax-entry ?* ". 23") - (modify-syntax-entry ?/ ". 14") - - ; Final stuff, then we're done - (setq mode-name "nagios" - major-mode 'nagios-mode) - + ;; I don't /think/ I need to define this before attempting + ;; to run it. Users can define it if they want. (run-hooks 'nagios-mode-hook) )