Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 190 lines (181 sloc) 5.283 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
;; policy-mode.el
;;
;; A major mode for writing Netcool Impact policy
;;
;; Tested on GNU Emacs 23.1.1
;;
;; Created by Lining (Larry) Song (songlining@gmail.com)
;;
;; Copy this into your local Emacs library directory (~/.emacs.d/lisp
;; in my case) and add a line:
;;
;; (require 'policy-mode)
;;
;; to your .emacs file. All files with a .ipl extension should
;; then be associated with policy mode automatically.

;; From Impact 6.1 Policy Reference Guide
(defconst policy-keywords
  '("Activate"
    "ActivateHibernation"
    "AddDataItem"
    "BatchDelete"
    "BatchUpdate"
    "BeginTransaction"
    "CallDBFunction"
    "CallStoredProcedure"
    "ClassOf"
    "CommandResponse"
    "CommitTransaction"
    "CurrentContext"
    "Decrypt"
    "DeleteDataItem"
    "Deploy"
    "DirectSQL"
    "Distinct"
    "Encrypt"
    "Eval"
    "EvalArray"
    "Exit"
    "Extract"
    "Float"
    "FormatDuration"
    "GetByFilter"
    "GetByKey"
    "GetByLinks"
    "GetByXPath"
    "GetClusterName"
    "GetDate"
    "GetFieldValue"
    "GetGlobalVar"
    "GetHTTP"
    "GetHibernatingPolicies"
    "GetScheduleMember"
    "GetServerName"
    "GetServerVar"
    "Hibernate"
    "Int"
    "JavaCall"
    "JRExecAction"
    "Keys"
    "Length"
    "Load"
    "LocalTime"
    "Log"
    "Merge"
    "NewEvent"
    "NewJavaObject"
    "NewObject"
    "ParseDate"
    "Random"
    "ReceiveJMSMessage"
    "RemoveHibernation"
    "Replace"
    "ReturnEvent"
    "RExtract"
    "RExtractAll"
    "RollbackTransaction"
    "SendEmail"
    "SendInstantMessage"
    "SendJMSMessage"
    "SetFieldValue"
    "SetGlobalVar"
    "SetServerVar"
    "SnmpGetAction"
    "SnmpGetNextAction"
    "SnmpSetAction"
    "SnmpTrapAction"
    "Split"
    "String"
    "Strip"
    "Substring"
    "Synchronized"
    "ToLower"
    "ToUpper"
    "Trim"
    "TBSM"
    "PassToTBSM"
    "RemoteTBSMShell"
    "TBSMShell"
    "UpdateEventQueue"
    "URLDecode"
    "URLEncode"
    "WSDMGetResourceProperty"
    "WSDMInvoke"
    "WSDMUpdateResourceProperty"
    "WSInvokeDL"
    "WSNewArray"
    "WSNewEnum"
    "WSNewObject"
    "WSNewSubObject"
    "WSSetDefaultPKGName"
    ))

;; http://www.emacswiki.org/emacs/FontLockKeywords
(defconst policy-font-lock-keywords
  `(
    (,(regexp-opt '("while" "if" "else" "elseif") 'words) . font-lock-keyword-face)
    (,(regexp-opt policy-keywords 'words) . font-lock-builtin-face)
    ("\\(function\\)\s+\\(\\w+\\).*(.*)" ;; (REGEXP (N1 FACE1) (N2 FACE2) (N3 FACE3) …)
     (1 font-lock-keyword-face)
     (2 font-lock-function-name-face))
    ("\\(\\w+\\)(.*?)" 1 font-lock-function-name-face)
    )
  "Minimal highlighting expressions for Impact Policy mode")

(defvar policy-mode-syntax-table
  (let ((policy-mode-syntax-table (make-syntax-table)))
    ;; This is added so entity names with underscores can be more easily parsed
    (modify-syntax-entry ?_ "w" policy-mode-syntax-table)
    ;; Comment styles are same as C++
    (modify-syntax-entry ?/ ". 124b" policy-mode-syntax-table)
    (modify-syntax-entry ?* ". 23" policy-mode-syntax-table)
    (modify-syntax-entry ?\n "> b" policy-mode-syntax-table)
    policy-mode-syntax-table)
  "Syntax table for policy-mode")

;; modified from http://www.emacswiki.org/emacs/wpdl-mode.el
(defun policy-indent-line ()
  "Indent the policy lines"
  (interactive)
  (beginning-of-line)
  (if (bobp)
      (indent-line-to 0)
    (let ((not-indented t) cur-indent)
      (if (looking-at ".*}[ \t]*\\(\/\/.*\\)*$")
(progn
(save-excursion
(forward-line -1)
(setq cur-indent (- (current-indentation) default-tab-width)))
(if (< cur-indent 0) ; We can't indent past the left margin
(setq cur-indent 0)))
(save-excursion
(while not-indented ; Iterate backwards until we find an indentation hint
(forward-line -1)
(if (looking-at ".*}[ \t]*\\(\/\/.*\\)*$") ; This hint indicates that we need to indent at the level of the END_ token
(progn
(setq cur-indent (current-indentation))
(setq not-indented nil))
(if (looking-at ".*{[ \t]*$") ; This hint indicates that we need to indent an extra level
(progn
(setq cur-indent (+ (current-indentation) default-tab-width)) ; Do the actual indenting
(setq not-indented nil))
(if (bobp)
(setq not-indented nil)))))))
      (if cur-indent
(indent-line-to cur-indent)
(indent-line-to 0))))) ; If we didn't see an indentation hint, then allow no indentation

(define-derived-mode policy-mode fundamental-mode "Impact Policy Mode"
  "Major Mode for Impact Policy Editing"
  (set-syntax-table policy-mode-syntax-table)
  (setq font-lock-defaults
        '(policy-font-lock-keywords
          nil ; KEYWORDS-ONLY: no
          t ; CASE-FOLD: no. IPL is not case-sensitive.
          ((?_ . "w")))) ; SYNTAX-ALIST
  
  (set (make-local-variable 'indent-line-function) 'policy-indent-line))

(add-hook 'policy-mode-hook '(lambda ()
(local-set-key (kbd "RET") 'newline-and-indent)
(local-set-key (kbd "}") '(lambda ()
(interactive)
(self-insert-command 1)
(policy-indent-line)))))

(provide 'policy-mode)

(setq auto-mode-alist (append '(("\\.ipl$" . policy-mode))
auto-mode-alist))
Something went wrong with that request. Please try again.