| 1 |
--- dvl/puppet/ext/emacs/puppet-mode.el 2008-03-26 10:56:42.000000000 -0700 |
|---|
| 2 |
+++ lisp/puppet-mode.el 2008-03-26 13:45:23.000000000 -0700 |
|---|
| 3 |
@@ -5,56 +5,48 @@ |
|---|
| 4 |
;;; Description: A simple mode for editing puppet manifests |
|---|
| 5 |
;;; |
|---|
| 6 |
|
|---|
| 7 |
-(defconst puppet-mode-version "0.0.1") |
|---|
| 8 |
+(defconst puppet-mode-version "0.1") |
|---|
| 9 |
|
|---|
| 10 |
(defvar puppet-mode-abbrev-table nil |
|---|
| 11 |
"Abbrev table in use in puppet-mode buffers.") |
|---|
| 12 |
|
|---|
| 13 |
(define-abbrev-table 'puppet-mode-abbrev-table ()) |
|---|
| 14 |
|
|---|
| 15 |
-(defvar puppet-mode-map nil "Keymap used in puppet mode.") |
|---|
| 16 |
+(defcustom puppet-indent-level 2 |
|---|
| 17 |
+ "*Indentation of Puppet statements." |
|---|
| 18 |
+ :type 'integer :group 'puppet) |
|---|
| 19 |
|
|---|
| 20 |
-(if puppet-mode-map |
|---|
| 21 |
- nil |
|---|
| 22 |
- (setq puppet-mode-map (make-sparse-keymap)) |
|---|
| 23 |
-;; (define-key puppet-mode-map "{" 'puppet-electric-brace) |
|---|
| 24 |
-;; (define-key puppet-mode-map "}" 'puppet-electric-brace) |
|---|
| 25 |
-;; (define-key puppet-mode-map "\e\C-a" 'puppet-beginning-of-defun) |
|---|
| 26 |
-;; (define-key puppet-mode-map "\e\C-e" 'puppet-end-of-defun) |
|---|
| 27 |
-;; (define-key puppet-mode-map "\e\C-b" 'puppet-backward-sexp) |
|---|
| 28 |
-;; (define-key puppet-mode-map "\e\C-f" 'puppet-forward-sexp) |
|---|
| 29 |
-;; (define-key puppet-mode-map "\e\C-p" 'puppet-beginning-of-block) |
|---|
| 30 |
-;; (define-key puppet-mode-map "\e\C-n" 'puppet-end-of-block) |
|---|
| 31 |
-;; (define-key puppet-mode-map "\e\C-h" 'puppet-mark-defun) |
|---|
| 32 |
-;; (define-key puppet-mode-map "\e\C-q" 'puppet-indent-exp) |
|---|
| 33 |
-;; (define-key puppet-mode-map "\t" 'puppet-indent-command) |
|---|
| 34 |
-;; (define-key puppet-mode-map "\C-c\C-e" 'puppet-insert-end) |
|---|
| 35 |
-;; (define-key puppet-mode-map "\C-j" 'puppet-reindent-then-newline-and-indent) |
|---|
| 36 |
- (define-key puppet-mode-map "\C-m" 'newline)) |
|---|
| 37 |
+(defcustom puppet-include-indent 2 |
|---|
| 38 |
+ "*Indentation of continued Puppet include statements." |
|---|
| 39 |
+ :type 'integer :group 'puppet) |
|---|
| 40 |
|
|---|
| 41 |
-(defvar puppet-mode-syntax-table nil |
|---|
| 42 |
- "Syntax table in use in puppet-mode buffers.") |
|---|
| 43 |
+(defvar puppet-mode-map |
|---|
| 44 |
+ "Key map used in puppet-mode buffers." |
|---|
| 45 |
+ (let ((map (make-sparse-keymap))) |
|---|
| 46 |
+ (define-key map "\C-j" 'newline-and-indent) |
|---|
| 47 |
+ (define-key map "\C-m" 'newline-and-indent) |
|---|
| 48 |
+ map)) |
|---|
| 49 |
|
|---|
| 50 |
-(if puppet-mode-syntax-table |
|---|
| 51 |
- () |
|---|
| 52 |
- (setq puppet-mode-syntax-table (make-syntax-table)) |
|---|
| 53 |
- (modify-syntax-entry ?\' "\"" puppet-mode-syntax-table) |
|---|
| 54 |
- (modify-syntax-entry ?\" "\"" puppet-mode-syntax-table) |
|---|
| 55 |
- (modify-syntax-entry ?# "<" puppet-mode-syntax-table) |
|---|
| 56 |
- (modify-syntax-entry ?\n ">" puppet-mode-syntax-table) |
|---|
| 57 |
- (modify-syntax-entry ?\\ "\\" puppet-mode-syntax-table) |
|---|
| 58 |
- (modify-syntax-entry ?$ "." puppet-mode-syntax-table) |
|---|
| 59 |
- (modify-syntax-entry ?- "_" puppet-mode-syntax-table) |
|---|
| 60 |
- (modify-syntax-entry ?> "." puppet-mode-syntax-table) |
|---|
| 61 |
- (modify-syntax-entry ?= "." puppet-mode-syntax-table) |
|---|
| 62 |
- (modify-syntax-entry ?\; "." puppet-mode-syntax-table) |
|---|
| 63 |
- (modify-syntax-entry ?\( "()" puppet-mode-syntax-table) |
|---|
| 64 |
- (modify-syntax-entry ?\) ")(" puppet-mode-syntax-table) |
|---|
| 65 |
- (modify-syntax-entry ?\{ "(}" puppet-mode-syntax-table) |
|---|
| 66 |
- (modify-syntax-entry ?\} "){" puppet-mode-syntax-table) |
|---|
| 67 |
- (modify-syntax-entry ?\[ "(]" puppet-mode-syntax-table) |
|---|
| 68 |
- (modify-syntax-entry ?\] ")[" puppet-mode-syntax-table) |
|---|
| 69 |
- ) |
|---|
| 70 |
+(defvar puppet-mode-syntax-table nil |
|---|
| 71 |
+ "Syntax table in use in puppet-mode buffers." |
|---|
| 72 |
+ (let ((table (make-syntax-table))) |
|---|
| 73 |
+ (modify-syntax-entry ?\' "\"" table) |
|---|
| 74 |
+ (modify-syntax-entry ?\" "\"" table) |
|---|
| 75 |
+ (modify-syntax-entry ?# "<" table) |
|---|
| 76 |
+ (modify-syntax-entry ?\n ">" table) |
|---|
| 77 |
+ (modify-syntax-entry ?\\ "\\" table) |
|---|
| 78 |
+ (modify-syntax-entry ?$ "." table) |
|---|
| 79 |
+ (modify-syntax-entry ?- "_" table) |
|---|
| 80 |
+ (modify-syntax-entry ?> "." table) |
|---|
| 81 |
+ (modify-syntax-entry ?= "." table) |
|---|
| 82 |
+ (modify-syntax-entry ?\; "." table) |
|---|
| 83 |
+ (modify-syntax-entry ?\( "()" table) |
|---|
| 84 |
+ (modify-syntax-entry ?\) ")(" table) |
|---|
| 85 |
+ (modify-syntax-entry ?\{ "(}" table) |
|---|
| 86 |
+ (modify-syntax-entry ?\} "){" table) |
|---|
| 87 |
+ (modify-syntax-entry ?\[ "(]" table) |
|---|
| 88 |
+ (modify-syntax-entry ?\] ")[" table) |
|---|
| 89 |
+ table)) |
|---|
| 90 |
|
|---|
| 91 |
(defcustom puppet-indent-tabs-mode nil |
|---|
| 92 |
"*Indentation can insert tabs in puppet mode if this is non-nil." |
|---|
| 93 |
@@ -64,31 +56,6 @@ |
|---|
| 94 |
"*Indentation column of comments." |
|---|
| 95 |
:type 'integer :group 'puppet) |
|---|
| 96 |
|
|---|
| 97 |
-(defun puppet-mode-variables () |
|---|
| 98 |
- (set-syntax-table puppet-mode-syntax-table) |
|---|
| 99 |
- (setq local-abbrev-table puppet-mode-abbrev-table) |
|---|
| 100 |
- ;(make-local-variable 'indent-line-function) |
|---|
| 101 |
- ;(setq indent-line-function 'ruby-indent-line) |
|---|
| 102 |
- (make-local-variable 'require-final-newline) |
|---|
| 103 |
- (setq require-final-newline t) |
|---|
| 104 |
- (make-variable-buffer-local 'comment-start) |
|---|
| 105 |
- (setq comment-start "# ") |
|---|
| 106 |
- (make-variable-buffer-local 'comment-end) |
|---|
| 107 |
- (setq comment-end "") |
|---|
| 108 |
- (make-variable-buffer-local 'comment-column) |
|---|
| 109 |
- (setq comment-column puppet-comment-column) |
|---|
| 110 |
- (make-variable-buffer-local 'comment-start-skip) |
|---|
| 111 |
- (setq comment-start-skip "#+ *") |
|---|
| 112 |
- (setq indent-tabs-mode puppet-indent-tabs-mode) |
|---|
| 113 |
- (make-local-variable 'parse-sexp-ignore-comments) |
|---|
| 114 |
- (setq parse-sexp-ignore-comments t) |
|---|
| 115 |
- (make-local-variable 'paragraph-start) |
|---|
| 116 |
- (setq paragraph-start (concat "$\\|" page-delimiter)) |
|---|
| 117 |
- (make-local-variable 'paragraph-separate) |
|---|
| 118 |
- (setq paragraph-separate paragraph-start) |
|---|
| 119 |
- (make-local-variable 'paragraph-ignore-fill-prefix) |
|---|
| 120 |
- (setq paragraph-ignore-fill-prefix t)) |
|---|
| 121 |
- |
|---|
| 122 |
(defun puppet-comment-line-p () |
|---|
| 123 |
"Return non-nil iff this line is a comment." |
|---|
| 124 |
(save-excursion |
|---|
| 125 |
@@ -113,6 +80,27 @@ |
|---|
| 126 |
(if (= (count-matches "\\]" apoint opoint) 0) |
|---|
| 127 |
apoint)))))) |
|---|
| 128 |
|
|---|
| 129 |
+(defun puppet-in-include () |
|---|
| 130 |
+ "If point is in a continued list of include statements, return the position |
|---|
| 131 |
+of the initial include plus puppet-include-indent." |
|---|
| 132 |
+ (save-excursion |
|---|
| 133 |
+ (save-match-data |
|---|
| 134 |
+ (let ((include-column nil) |
|---|
| 135 |
+ (not-found t)) |
|---|
| 136 |
+ (while not-found |
|---|
| 137 |
+ (forward-line -1) |
|---|
| 138 |
+ (cond |
|---|
| 139 |
+ ((puppet-comment-line-p) |
|---|
| 140 |
+ (if (bobp) |
|---|
| 141 |
+ (setq not-found nil))) |
|---|
| 142 |
+ ((looking-at "^\\s-*include\\s-+.*,\\s-*$") |
|---|
| 143 |
+ (setq include-column |
|---|
| 144 |
+ (+ (current-indentation) puppet-include-indent)) |
|---|
| 145 |
+ (setq not-found nil)) |
|---|
| 146 |
+ ((not (looking-at ".*,\\s-*$")) |
|---|
| 147 |
+ (setq not-found nil)))) |
|---|
| 148 |
+ include-column)))) |
|---|
| 149 |
+ |
|---|
| 150 |
(defun puppet-indent-line () |
|---|
| 151 |
"Indent current line as puppet code." |
|---|
| 152 |
(interactive) |
|---|
| 153 |
@@ -121,6 +109,7 @@ |
|---|
| 154 |
(indent-line-to 0) ; First line is always non-indented |
|---|
| 155 |
(let ((not-indented t) |
|---|
| 156 |
(array-start (puppet-in-array)) |
|---|
| 157 |
+ (include-start (puppet-in-include)) |
|---|
| 158 |
cur-indent) |
|---|
| 159 |
(cond |
|---|
| 160 |
(array-start |
|---|
| 161 |
@@ -155,6 +144,8 @@ |
|---|
| 162 |
(re-search-forward "\\S-") |
|---|
| 163 |
(forward-char -1) |
|---|
| 164 |
(setq cur-indent (current-column)))) |
|---|
| 165 |
+ (include-start |
|---|
| 166 |
+ (setq cur-indent include-start)) |
|---|
| 167 |
((looking-at "^[^{\n]*}") |
|---|
| 168 |
;; This line contains the end of a block, but the block does |
|---|
| 169 |
;; not also begin on this line, so decrease the indentation. |
|---|
| 170 |
@@ -162,9 +153,9 @@ |
|---|
| 171 |
(forward-line -1) |
|---|
| 172 |
(if (looking-at "^.*}") |
|---|
| 173 |
(progn |
|---|
| 174 |
- (setq cur-indent (- (current-indentation) 2)) |
|---|
| 175 |
+ (setq cur-indent (- (current-indentation) puppet-indent-level)) |
|---|
| 176 |
(setq not-indented nil)) |
|---|
| 177 |
- (setq cur-indent (- (current-indentation) 2)))) |
|---|
| 178 |
+ (setq cur-indent (- (current-indentation) puppet-indent-level)))) |
|---|
| 179 |
(if (< cur-indent 0) ; We can't indent past the left margin |
|---|
| 180 |
(setq cur-indent 0))) |
|---|
| 181 |
(t |
|---|
| 182 |
@@ -183,7 +174,13 @@ |
|---|
| 183 |
(setq cur-indent (current-indentation)) |
|---|
| 184 |
(setq not-indented nil)) |
|---|
| 185 |
((looking-at "^.*{") ; indent an extra level |
|---|
| 186 |
- (setq cur-indent (+ (current-indentation) 2)) |
|---|
| 187 |
+ (setq cur-indent (+ (current-indentation) puppet-indent-level)) |
|---|
| 188 |
+ (setq not-indented nil)) |
|---|
| 189 |
+ ((looking-at "^.*;\\s-*$") ; Semicolon ends a nested resource |
|---|
| 190 |
+ (setq cur-indent (- (current-indentation) puppet-indent-level)) |
|---|
| 191 |
+ (setq not-indented nil)) |
|---|
| 192 |
+ ((looking-at "^.*:\\s-*$") ; indent an extra level after : |
|---|
| 193 |
+ (setq cur-indent (+ (current-indentation) puppet-indent-level)) |
|---|
| 194 |
(setq not-indented nil)) |
|---|
| 195 |
((bobp) |
|---|
| 196 |
(setq not-indented nil)) |
|---|
| 197 |
@@ -204,13 +201,20 @@ |
|---|
| 198 |
(use-local-map puppet-mode-map) |
|---|
| 199 |
(setq mode-name "Puppet") |
|---|
| 200 |
(setq major-mode 'puppet-mode) |
|---|
| 201 |
- (puppet-mode-variables) |
|---|
| 202 |
- ;; Register our indentation function |
|---|
| 203 |
- (set (make-local-variable 'indent-line-function) 'puppet-indent-line) |
|---|
| 204 |
+ (set-syntax-table puppet-mode-syntax-table) |
|---|
| 205 |
+ (set (make-local-variable 'local-abbrev-table) puppet-mode-abbrev-table) |
|---|
| 206 |
+ (set (make-local-variable 'comment-start) "# ") |
|---|
| 207 |
+ (set (make-local-variable 'comment-start-skip) "#+ *") |
|---|
| 208 |
+ (set (make-local-variable 'comment-end) "") |
|---|
| 209 |
+ (set (make-local-variable 'comment-column) puppet-comment-column) |
|---|
| 210 |
+ (set (make-local-variable 'indent-line-function) 'puppet-indent-line) |
|---|
| 211 |
+ (set (make-local-variable 'indent-tabs-mode) puppet-indent-tabs-mode) |
|---|
| 212 |
+ (set (make-local-variable 'require-final-newline) t) |
|---|
| 213 |
+ (set (make-local-variable 'paragraph-ignore-fill-prefix) t) |
|---|
| 214 |
+ (set (make-local-variable 'paragraph-start) "\f\\|[ ]*$") |
|---|
| 215 |
+ (set (make-local-variable 'paragraph-separate) "[ \f]*$") |
|---|
| 216 |
(run-hooks 'puppet-mode-hook)) |
|---|
| 217 |
|
|---|
| 218 |
- |
|---|
| 219 |
- |
|---|
| 220 |
(cond |
|---|
| 221 |
((featurep 'font-lock) |
|---|
| 222 |
(or (boundp 'font-lock-variable-name-face) |
|---|
| 223 |
@@ -253,8 +257,13 @@ |
|---|
| 224 |
;; defines |
|---|
| 225 |
'("^\\s *\\(define\\|node\\|class\\)\\s +\\([^( \t\n]+\\)" |
|---|
| 226 |
2 font-lock-function-name-face) |
|---|
| 227 |
+ '("\\s +inherits\\s +\\([^( \t\n]+\\)" |
|---|
| 228 |
+ 1 font-lock-function-name-face) |
|---|
| 229 |
;; include |
|---|
| 230 |
- '("^\\s *include\\s +\\([^( \t\n]+\\)" |
|---|
| 231 |
+ '("^\\s *include\\s +\\([^( \t\n,]+\\)" |
|---|
| 232 |
+ 1 font-lock-reference-face) |
|---|
| 233 |
+ ;; hack to catch continued includes |
|---|
| 234 |
+ '("^\\s *\\([a-zA-Z0-9:_-]+\\),?\\s *$" |
|---|
| 235 |
1 font-lock-reference-face) |
|---|
| 236 |
;; keywords |
|---|
| 237 |
(cons (concat |
|---|
| 238 |
@@ -270,6 +279,7 @@ |
|---|
| 239 |
"include" |
|---|
| 240 |
"inherits" |
|---|
| 241 |
"node" |
|---|
| 242 |
+ "realize" |
|---|
| 243 |
"true" |
|---|
| 244 |
) |
|---|
| 245 |
"\\|") |
|---|
| 246 |
@@ -284,7 +294,10 @@ |
|---|
| 247 |
'("\\(\\$\\|@\\|@@\\)\\(\\w\\|_\\)+" |
|---|
| 248 |
0 font-lock-variable-name-face) |
|---|
| 249 |
;; usage of types |
|---|
| 250 |
- '("^\\s +\\([a-zA-Z-]+\\)\\s +{" |
|---|
| 251 |
+ '("^\\s +\\([a-zA-Z_-]+\\)\\s +{" |
|---|
| 252 |
+ 1 font-lock-type-face) |
|---|
| 253 |
+ ;; overrides |
|---|
| 254 |
+ '("^\\s +\\([a-zA-Z_-]+\\)\\[" |
|---|
| 255 |
1 font-lock-type-face) |
|---|
| 256 |
;; general delimited string |
|---|
| 257 |
'("\\(^\\|[[ \t\n<+(,=]\\)\\(%[xrqQwW]?\\([^<[{(a-zA-Z0-9 \n]\\)[^\n\\\\]*\\(\\\\.[^\n\\\\]*\\)*\\(\\3\\)\\)" |
|---|