]> git.donarmstrong.com Git - lib.git/blob - emacs_el/vcl-mode.el
fix missing ) for org-mode
[lib.git] / emacs_el / vcl-mode.el
1 ;;; vcl-mode.el - Syntax highlighting for Varnish Command Language
2 ;;; 
3 ;;; Copyright (c) 2008-2009 Linpro AS
4 ;;; All rights reserved.
5 ;;;
6 ;;; Author: Stig Sandbeck Mathisen <ssm@linpro.no>
7 ;;;
8 ;;; Redistribution and use in source and binary forms, with or without
9 ;;; modification, are permitted provided that the following conditions
10 ;;; are met:
11 ;;; 1. Redistributions of source code must retain the above copyright
12 ;;;    notice, this list of conditions and the following disclaimer.
13 ;;; 2. Redistributions in binary form must reproduce the above
14 ;;;    copyright notice, this list of conditions and the following
15 ;;;    disclaimer in the documentation and/or other materials provided
16 ;;;    with the distribution.
17 ;;;
18 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
19 ;;; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 ;;; TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
21 ;;; PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR
22 ;;; CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 ;;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
25 ;;; USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 ;;; AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 ;;; LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 ;;; ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 ;;; POSSIBILITY OF SUCH DAMAGE.
30 ;;;
31 ;;; $Id$
32 ;;;
33 ;;; Based on
34 ;;; https://www.varnish-cache.org/trac/browser/fragmentation/varnish-tools/emacs/vcl-mode.el?rev=72322230cfe73244bcf31e008a05a9c2c3867816
35 ;;; Updated to work with varnish 3 a bit better; highlight more of the
36 ;;; variable names and functions
37
38 (defgroup vcl nil
39   "Customizations for vcl-mode")
40
41 (defcustom vcl-indent-level 8
42   "*The level of indentation (number of space characters) in VCL-mode."
43   :type 'integer  :group 'vcl)
44
45 (defcustom vcl-indent-tabs-mode nil
46   "*Allow tabs when indentation in vcl-mode if non-nil"
47   :type 'boolean :group 'vcl)
48
49 ;; I just love standards, there are so many to choose from
50 (if (string-match "XEmacs\\|Lucid" emacs-version)
51     (require 'generic-mode)
52   (require 'generic))
53
54 ;; Add a VCL major mode called "vcl-mode", based on generic-mode
55
56 (define-generic-mode 'vcl-mode
57   ;; comments (defined in "vcl-mode-setup-function"
58   nil
59   ;; keywords (defined under "others" instead)
60   nil
61   ;; others
62   (list
63    ;; Logic
64    (generic-make-keywords-list
65     (list
66      "else"
67      "elsif"
68      "if"
69      "remove"
70      )
71     'font-lock-keyword-face)
72    
73    ;; Types
74    (generic-make-keywords-list
75     (list
76      "set"
77      "unset"
78      "ban_url"
79      "ban"
80      "regsub"
81      "regsuball"
82      "hash_data"
83      "return"
84      "call"
85      "import"
86      )
87     'font-lock-builtin-face)
88    
89    ;; VCL Functions
90    (generic-make-keywords-list
91     (list
92      "acl"
93      "backend"
94      "sub"
95      "vcl_deliver"
96      "vcl_discard"
97      "vcl_fetch"
98      "vcl_hash"
99      "vcl_hit"
100      "vcl_miss"
101      "vcl_pass"
102      "vcl_pipe"
103      "vcl_recv"
104      "vcl_timeout"
105      )
106     'font-lock-function-name-face)
107    
108    ;; Actions
109    (generic-make-keywords-list
110     (list
111      "deliver"
112      "discard"
113      "error"
114      "fetch"
115      "hash"
116      "keep"
117      "lookup"
118      "pass"
119      "pipe"
120      "hit_for_pass"
121      )
122     'font-lock-function-name-face)
123
124    ;; Variables
125    (generic-make-keywords-list
126     (list
127      "now"
128      ".host"
129      ".port"
130
131      "client.ip"
132      "client.identity"
133      "server.ip"
134      "server.port"
135      "server.hostname"
136      "server.identity"
137
138      "req.request"
139      "req.url"
140      "req.proto"
141      "req.backend"
142      "req.backend.healthy"
143      "req.hash_always_miss"
144      "req.hash_ignore_busy"
145      "req.can_gzip"
146      "req.restarts"
147      "req.esi"
148      "req.esi_level"
149      "req.grace"
150      "req.xid"
151
152
153      "bereq.request"
154      "bereq.url"
155      "bereq.proto"
156      "bereq.connect_timeout"
157      "bereq.first_byte_timeout"
158      "bereq.between_bytes_timeout"
159
160      
161      "beresp.do_stream"
162      "beresp.do_esi"
163      "beresp.do_gzip"
164      "beresp.do_gunzip"
165      "beresp.proto"
166      "beresp.status"
167      "beresp.response"
168      "beresp.ttl"
169      "beresp.grace"
170      "beresp.saintmode"
171      "beresp.backend.name"
172      "beresp.backend.ip"
173      "beresp.backend.port"
174      "beresp.storage"
175
176
177      "obj.proto"
178      "obj.status"
179      "obj.response"
180      "obj.ttl"
181      "obj.lastuse"
182      "obj.hits"
183      "obj.grace"
184      "obj.http.header"
185
186
187      "req.hash"
188      "resp.proto"
189      "resp.status"
190      "resp.response"
191      "resp.http.header"
192      )
193     'font-lock-variable-name-face)
194
195    ;; More variables
196    '("\\(\\(be\\)?\\(req\\|resp\\|obj\\)\\)\.http\.[A-Za-z-]+" .
197      font-lock-variable-name-face))
198   
199   ;; Filenames to highlight
200   '("\\.vcl\\'")
201   (list 'vcl-mode-setup-function)
202   "Mode for Varnish Command Language")
203
204
205 ;; A function to modify syntax, add a hook if needed, and setup
206 ;; indentation.
207
208 (defun vcl-mode-setup-function ()
209   ;; These are "part of words"
210   (modify-syntax-entry ?_ "w")
211   (modify-syntax-entry ?. "w")
212
213   ;; C++-style comments
214   (modify-syntax-entry ?/ ". 124")
215   (modify-syntax-entry ?* ". 23b")
216
217   ;; Perl-style comments
218   (modify-syntax-entry ?# "<")
219   (modify-syntax-entry ?\n ">")
220   
221   (run-hooks 'vcl-mode-hook)
222   (set (make-local-variable 'indent-line-function) 'vcl-indent-line)  
223   (setq indent-tabs-mode vcl-indent-tabs-mode)
224   )
225
226 (defvar vcl-mode-hook nil)
227
228 (defun vcl-indent-line ()
229   "Indent the current VCL line according to syntax."
230   (interactive)
231   (indent-line-to
232    (max (vcl-calculate-indentation) 0)))
233   
234
235 ;; The function to calculate indentation level.  This is a really
236 ;; simple and naive function, and does not perform anything like a
237 ;; syntax check.
238 (defun vcl-calculate-indentation ()
239   "Return the column to which the current line should be indented."
240   (interactive)
241   (save-excursion
242                                         ; Do not indent the first line.
243     (if (vcl-first-line-p) 0
244                                         ; Reduce indent level if we
245                                         ; close a block on this line
246       (if (vcl-closing-tag-on-this-line-p)
247           (- (vcl-previous-line-indentation)
248              vcl-indent-level)
249                                         ; Increase indent level if a
250                                         ; block opened on the previous
251                                         ; line
252         (if (vcl-opening-tag-on-previous-line-p)
253             (+ (vcl-previous-line-indentation)
254                vcl-indent-level)
255                                         ; By default, indent to the
256                                         ; level of the previous
257                                         ; non-empty line
258           (vcl-previous-line-indentation))))))
259
260 (defun vcl-opening-tag-on-previous-line-p ()
261   "Checks if we have an opening tag on the previous line."
262   (interactive)
263   (save-excursion
264     (beginning-of-line)
265     (skip-chars-backward " \t\n")
266     (beginning-of-line)
267     (if (and (looking-at ".*{[ \t]*$")
268              (not (vcl-comment-p)))
269         t)))
270
271 (defun vcl-closing-tag-on-this-line-p ()
272   "Checks if we have a closing tag on this line."
273   (interactive)
274   (save-excursion
275     (back-to-indentation)
276     (looking-at "}")))
277
278 (defun vcl-previous-line-indentation ()
279   "Return the indent level of the previous line."
280   (interactive)
281   (save-excursion
282     (beginning-of-line)
283     (skip-chars-backward " \t\n")
284     (back-to-indentation)
285     (current-column)))
286
287 (defun vcl-comment-p ()
288   "Checks if we have a commented line."
289   (interactive)
290   (save-excursion
291     (beginning-of-line)
292     (looking-at "^[ \t]*#")))
293
294 (defun vcl-first-line-p ()
295   "Checks if we are on the first line."
296   (interactive)
297   (save-excursion
298     (beginning-of-line)
299     (eq (point) 1)))
300
301 (provide 'vcl-mode)