|
109 | 109 | '("charset_map " "events " "geo " "http " "if " "imap " "limit_except " "location " "mail " "map " "server " "split_clients " "upstream ")
|
110 | 110 | "Nginx blocks.")
|
111 | 111 |
|
| 112 | +(defvar nginx-variables |
| 113 | + ;; come up with regexp to check rewrite is first then highlight this? |
| 114 | + '("last" "break" "redirect" "permanent" |
| 115 | + "on" "off" "put" "delete" "mkcol" "copy" "move" |
| 116 | + )) |
| 117 | + |
112 | 118 | ;; create the regex string for each class of keywords
|
113 | 119 | (defvar nginx-keywords-regexp (regexp-opt nginx-keywords 'words))
|
114 | 120 | (defvar nginx-functions-regexp (regexp-opt nginx-functions 'words))
|
| 121 | +(defvar nginx-variables-regexp (regexp-opt nginx-variables 'words)) |
115 | 122 |
|
116 | 123 | ;; clear memory
|
117 | 124 | (setq nginx-keywords nil)
|
118 | 125 | (setq nginx-functions nil)
|
| 126 | +(setq nginx-variables nil) |
| 127 | + |
| 128 | +;; (setq nginx-font-lock-keywords nil) |
| 129 | +;; ("\\(\$[0-9]+\\)[^0-9]" 1 font-lock-constant-face) |
| 130 | +;; ("\$[A-Za-z0-9_\-]+" . font-lock-variable-name-face) |
| 131 | +;; (";$" . font-lock-pseudo-keyword-face) |
119 | 132 |
|
120 | 133 | (setq nginx-font-lock-keywords
|
121 | 134 | `(
|
122 | 135 | (,nginx-functions-regexp . font-lock-function-name-face)
|
123 |
| - (,nginx-keywords-regexp . font-lock-keyword-face))) |
| 136 | + (,nginx-keywords-regexp . font-lock-keyword-face) |
| 137 | + (,nginx-variables-regexp . font-lock-constant-face) )) |
| 138 | + |
| 139 | +;;; Indent logic from ajc's nginx-mode |
| 140 | + |
| 141 | +(defcustom nginx-indent-level 4 |
| 142 | + "*Indentation of Nginx statements." |
| 143 | + :type 'integer :group 'nginx) |
| 144 | + |
| 145 | +(defcustom nginx-indent-tabs-mode nil |
| 146 | + "*Indentation can insert tabs in nginx mode if this is non-nil." |
| 147 | + :type 'boolean :group 'nginx) |
| 148 | + |
| 149 | + |
| 150 | +(defun nginx-block-indent () |
| 151 | + "If point is in a block, return the indentation of the first line of that |
| 152 | +block (the line containing the opening brace). Used to set the indentation |
| 153 | +of the closing brace of a block." |
| 154 | + (save-excursion |
| 155 | + (save-match-data |
| 156 | + (let ((opoint (point)) |
| 157 | + (apoint (search-backward "{" nil t))) |
| 158 | + (when apoint |
| 159 | + ;; This is a bit of a hack and doesn't allow for strings. We really |
| 160 | + ;; want to parse by sexps at some point. |
| 161 | + (let ((close-braces (count-matches "}" apoint opoint)) |
| 162 | + (open-braces 0)) |
| 163 | + (while (and apoint (> close-braces open-braces)) |
| 164 | + (setq apoint (search-backward "{" nil t)) |
| 165 | + (when apoint |
| 166 | + (setq close-braces (count-matches "}" apoint opoint)) |
| 167 | + (setq open-braces (1+ open-braces))))) |
| 168 | + (if apoint |
| 169 | + (current-indentation) |
| 170 | + nil)))))) |
| 171 | + |
| 172 | + |
| 173 | +(defun nginx-comment-line-p () |
| 174 | + "Return non-nil iff this line is a comment." |
| 175 | + (save-excursion |
| 176 | + (save-match-data |
| 177 | + (beginning-of-line) |
| 178 | + (looking-at "^\\s-*#")))) |
| 179 | + |
| 180 | +(defun nginx-indent-line () |
| 181 | + "Indent current line as nginx code." |
| 182 | + (interactive) |
| 183 | + (beginning-of-line) |
| 184 | + (if (bobp) |
| 185 | + (indent-line-to 0) ; First line is always non-indented |
| 186 | + (let ((not-indented t) |
| 187 | + (block-indent (nginx-block-indent)) |
| 188 | + cur-indent) |
| 189 | + (cond |
| 190 | + ((and (looking-at "^\\s-*}\\s-*$") block-indent) |
| 191 | + ;; This line contains a closing brace and we're at the inner |
| 192 | + ;; block, so we should indent it matching the indentation of |
| 193 | + ;; the opening brace of the block. |
| 194 | + (setq cur-indent block-indent)) |
| 195 | + (t |
| 196 | + ;; Otherwise, we did not start on a block-ending-only line. |
| 197 | + (save-excursion |
| 198 | + ;; Iterate backwards until we find an indentation hint |
| 199 | + (while not-indented |
| 200 | + (forward-line -1) |
| 201 | + (cond |
| 202 | + ;; Comment lines are ignored unless we're at the start of the |
| 203 | + ;; buffer. |
| 204 | + ((nginx-comment-line-p) |
| 205 | + (if (bobp) |
| 206 | + (setq not-indented nil))) |
| 207 | + |
| 208 | + ;; Brace or paren on a line by itself will already be indented to |
| 209 | + ;; the right level, so we can cheat and stop there. |
| 210 | + ((looking-at "^\\s-*}\\s-*") |
| 211 | + (setq cur-indent (current-indentation)) |
| 212 | + (setq not-indented nil)) |
| 213 | + |
| 214 | + ;; Indent by one level more than the start of our block. We lose |
| 215 | + ;; if there is more than one block opened and closed on the same |
| 216 | + ;; line but it's still unbalanced; hopefully people don't do that. |
| 217 | + ((looking-at "^.*{[^\n}]*$") |
| 218 | + (setq cur-indent (+ (current-indentation) nginx-indent-level)) |
| 219 | + (setq not-indented nil)) |
| 220 | + |
| 221 | + ;; Start of buffer. |
| 222 | + ((bobp) |
| 223 | + (setq not-indented nil))))))) |
| 224 | + |
| 225 | + ;; We've figured out the indentation, so do it. |
| 226 | + (if (and cur-indent (> cur-indent 0)) |
| 227 | + (indent-line-to cur-indent) |
| 228 | + (indent-line-to 0))))) |
| 229 | + |
| 230 | + |
124 | 231 |
|
125 | 232 | ;;;###autoload
|
126 | 233 | (define-derived-mode nginx-mode conf-space-mode "nginx"
|
127 | 234 | "Major mode for Nginx configuration files"
|
128 | 235 | ;; code for syntax highlighting
|
129 | 236 | (setq font-lock-defaults '((nginx-font-lock-keywords)))
|
130 | 237 |
|
| 238 | + (set (make-local-variable 'comment-start) "# ") |
| 239 | + (set (make-local-variable 'comment-start-skip) "#+ *") |
| 240 | + (set (make-local-variable 'comment-use-syntax) t) |
| 241 | + (set (make-local-variable 'comment-end) "") |
| 242 | + (set (make-local-variable 'comment-auto-fill-only-comments) t) |
| 243 | + |
| 244 | + (set (make-local-variable 'indent-line-function) 'nginx-indent-line) |
| 245 | + (set (make-local-variable 'indent-tabs-mode) nginx-indent-tabs-mode) |
| 246 | + (set (make-local-variable 'require-final-newline) t) |
| 247 | + (set (make-local-variable 'paragraph-ignore-fill-prefix) t) |
| 248 | + (set (make-local-variable 'paragraph-start) "\f\\|[ ]*$\\|#$") |
| 249 | + (set (make-local-variable 'paragraph-separate) "\\([ \f]*\\|#\\)$") |
| 250 | + |
| 251 | + |
| 252 | + |
131 | 253 | ;; clear memory
|
132 | 254 | (setq nginx-keywords-regexp nil)
|
133 |
| - (setq nginx-functions-regexp nil)) |
| 255 | + (setq nginx-functions-regexp nil) |
| 256 | + (setq nginx-variables-regexp nil) |
| 257 | + ) |
| 258 | + |
134 | 259 |
|
135 | 260 | (provide 'nginx-mode)
|
0 commit comments