25
|
1 ;;; xquery-mode.el --- A simple mode for editing xquery programs
|
27
|
2 ;; Time-stamp: <2010-08-10 12:15:14 mblakele>
|
25
|
3
|
|
4 ;;; Copyright (C) 2005 Suraj Acharya
|
27
|
5 ;;; Copyright (C) 2006-2012 Michael Blakeley
|
25
|
6
|
27
|
7 ;; Authors:
|
|
8 ;; Suraj Acharya <sacharya@cs.indiana.edu>
|
|
9 ;; Michael Blakeley <mike@blakeley.com>
|
25
|
10
|
|
11 ;; This file is not part of GNU Emacs.
|
|
12
|
|
13 ;; xquery-mode.el is free software; you can redistribute it
|
|
14 ;; and/or modify it under the terms of the GNU General Public License
|
|
15 ;; as published by the Free Software Foundation; either version 2, or
|
27
|
16 ;; (at your option) any later version.
|
25
|
17
|
|
18 ;; This software is distributed in the hope that it will be useful,
|
|
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
21 ;; GNU General Public License for more details.
|
|
22
|
|
23 ;; You should have received a copy of the GNU General Public License
|
|
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
26 ;; Boston, MA 02111-1307, USA.
|
|
27
|
|
28 ;;; Commentary:
|
27
|
29 ;;;
|
|
30
|
25
|
31
|
27
|
32 ;;; History:
|
|
33 ;;
|
|
34 ;; 2011-10-08 mostly rewritten, knows about some MarkLogic extensions
|
|
35 ;;
|
|
36 ;; 2005-03-26 release by sacharya
|
|
37 ;; to http://www.emacswiki.org/cgi-bin/wiki/xquery-mode.el
|
|
38 ;;
|
|
39
|
|
40 (require 'font-lock)
|
|
41
|
|
42 ;; TODO 'if()' is highlighted as a function
|
|
43
|
|
44 ;; TODO requiring nxml-mode excludes XEmacs - just for colors?
|
|
45 ;; TODO test using featurep 'xemacs
|
25
|
46 (require 'nxml-mode)
|
27
|
47
|
|
48 ;; TODO use nxml for element completion?
|
|
49
|
25
|
50 (require 'generic-mode)
|
|
51 (define-generic-mode 'xquery-mode
|
27
|
52 '()
|
|
53 '()
|
|
54 '() ;font-lock-list
|
|
55 '(".xq\\'") ;auto-mode-list
|
|
56 '(xquery-set-indent-function nil) ;function list
|
|
57 "A Major mode for editing xquery.")
|
|
58
|
|
59 ;; customization hook
|
|
60 (defcustom xquery-mode-hook nil
|
|
61 "Hook run after entering XQuery mode."
|
|
62 :type 'hook
|
|
63 :options '(turn-on-xquery-indent turn-on-font-lock))
|
|
64
|
|
65 (defvar xquery-toplevel-bovine-table nil "Top level bovinator table")
|
|
66
|
|
67 (defvar xquery-mode-syntax-table () "Syntax table for xquery-mode")
|
|
68
|
|
69 (setq xquery-mode-syntax-table
|
|
70 (let ((xquery-mode-syntax-table (make-syntax-table)))
|
|
71 ;; single-quotes are equivalent to double-quotes
|
|
72 (modify-syntax-entry ?' "\"" xquery-mode-syntax-table)
|
|
73 ;; treat underscores as punctuation
|
|
74 (modify-syntax-entry ?\_ "." xquery-mode-syntax-table)
|
|
75 ;; treat hypens as punctuation
|
|
76 (modify-syntax-entry ?\- "." xquery-mode-syntax-table)
|
|
77 ;; colons are both punctuation and comments
|
|
78 ;; the space after '.' indicates an unused matching character slot
|
|
79 (modify-syntax-entry ?\: ". 23" xquery-mode-syntax-table)
|
|
80 ;; XPath step separator / is punctuation
|
|
81 (modify-syntax-entry ?/ "." xquery-mode-syntax-table)
|
|
82 ;; xquery doesn't use backslash-escaping, so \ is punctuation
|
|
83 (modify-syntax-entry ?\\ "." xquery-mode-syntax-table)
|
|
84 ;; set-up the syntax table correctly for all the different braces
|
|
85 (modify-syntax-entry ?\{ "(}" xquery-mode-syntax-table)
|
|
86 (modify-syntax-entry ?\} "){" xquery-mode-syntax-table)
|
|
87 (modify-syntax-entry ?\[ "(]" xquery-mode-syntax-table)
|
|
88 (modify-syntax-entry ?\] ")]" xquery-mode-syntax-table)
|
|
89 ;;(modify-syntax-entry ?\< "(" xquery-mode-syntax-table)
|
|
90 ;;(modify-syntax-entry ?\> ")" xquery-mode-syntax-table)
|
|
91 ;; parens may indicate a comment, or may be a sequence
|
|
92 ;; note that (: will balance ), ( will balance ::), etc.
|
|
93 ;; note 'n' for comment nesting
|
|
94 (modify-syntax-entry ?\( "()1" xquery-mode-syntax-table)
|
|
95 (modify-syntax-entry ?\) ")(4" xquery-mode-syntax-table)
|
|
96 xquery-mode-syntax-table))
|
|
97
|
|
98 (defvar xquery-mode-keywords () "Keywords for xquery-mode")
|
|
99
|
|
100 (defvar xquery-mode-comment-start "(: "
|
|
101 "String used to start an XQuery mode comment.")
|
|
102 ;;(make-local-variable 'comment-start)
|
|
103
|
|
104
|
|
105 (defvar xquery-mode-comment-end " :)"
|
|
106 "String used to end an XQuery mode comment.")
|
|
107
|
|
108
|
|
109 (defvar xquery-mode-comment-fill ":"
|
|
110 "String used to fill an XQuery mode comment.")
|
|
111
|
|
112
|
|
113 (defvar xquery-mode-comment-start-skip "(:\\s-+"
|
|
114 "Regexp to match an XQuery mode comment and any following whitespace.")
|
|
115
|
|
116
|
|
117 ;; NOTE - derived-mode will automatically copy some vars
|
|
118 ;; xquery-map as keymap
|
|
119 ;; xquery-syntax-table as syntax-table
|
|
120 ;; xquery-abbrev-table as abbrev-table
|
|
121 ;; xquery-hook as initialization hook
|
|
122 ;;;###autoload
|
|
123 (define-derived-mode xquery-mode fundamental-mode "XQuery"
|
|
124 "A major mode for W3C XQuery 1.0"
|
|
125 ;; indentation
|
|
126 (set (make-local-variable 'indent-line-function) 'xquery-indent-line)
|
|
127 ;; apparently it's important to set at least an empty list up-front
|
|
128 (set (make-local-variable 'font-lock-defaults)
|
|
129 (list (list ())))
|
|
130 (set (make-local-variable 'comment-start) xquery-mode-comment-start)
|
|
131 (set (make-local-variable 'comment-end) xquery-mode-comment-end)
|
|
132 (set (make-local-variable 'comment-fill) xquery-mode-comment-fill)
|
|
133 (set (make-local-variable 'comment-start-skip) xquery-mode-comment-start-skip)
|
25
|
134 )
|
|
135
|
27
|
136 ;; XQuery doesn't have keywords, but these usually work...
|
|
137 ;; TODO remove as many as possible, in favor of parsing
|
|
138 (setq xquery-mode-keywords
|
|
139 (list
|
|
140 ;; FLWOR
|
|
141 ;;"let" "for"
|
|
142 "at" "in"
|
|
143 "where"
|
|
144 "stable order by" "order by"
|
|
145 "ascending" "descending" "empty" "greatest" "least" "collation"
|
|
146 "return"
|
|
147 ;; XPath axes
|
|
148 "self" "child" "descendant" "descendant-or-self"
|
|
149 "parent" "ancestor" "ancestor-or-self"
|
|
150 "following" "following-sibling"
|
|
151 "preceding" "preceding-sibling"
|
|
152 ;; conditionals
|
|
153 "if" "then" "else"
|
|
154 "typeswitch" ;"case" "default"
|
|
155 ;; quantified expressions
|
|
156 "some" "every" "construction" "satisfies"
|
|
157 ;; schema
|
|
158 "schema-element" "schema-attribute" "validate"
|
|
159 ;; operators
|
|
160 "intersect" "union" "except" "to"
|
|
161 "is" "eq" "ne" "gt" "ge" "lt" "le"
|
|
162 "or" "and"
|
|
163 "div" "idiv" "mod"
|
|
164 ))
|
25
|
165
|
27
|
166 ;; to match only word-boundaries, we turn the keywords into a big regex
|
|
167 (defvar xquery-mode-keywords-regex () "Keywords regex for xquery mode")
|
|
168
|
|
169 ;; transform the list of keywords into regex
|
|
170 ;; check for word-boundaries instead of whitespace
|
|
171 (setq xquery-mode-keywords-regex
|
|
172 (concat (concat "\\b\\("
|
|
173 (mapconcat
|
|
174 (function (lambda (r)
|
|
175 (if (string-match "[ \t]+" r)
|
|
176 (replace-match "[ \t]+" nil t r) r)))
|
|
177 xquery-mode-keywords "\\|"))
|
|
178 "\\)\\b"))
|
|
179
|
|
180 ;;(message xquery-mode-keywords-regex)
|
|
181
|
|
182 ;; XQuery syntax - TODO build a real parser
|
|
183 (defvar xquery-mode-ncname () "NCName regex, in 1 group")
|
|
184 (setq xquery-mode-ncname "\\(\\sw[-_\\.[:word:]]*\\)")
|
|
185
|
|
186 ;; highlighting needs a group, even if it's "" - so use (...?) not (...)?
|
|
187 ;; note that this technique treats the local-name as optional,
|
|
188 ;; when the prefix should be the optional part.
|
|
189 (defvar xquery-mode-qname () "QName regex, in 3 groups")
|
|
190 (setq xquery-mode-qname
|
|
191 (concat
|
|
192 xquery-mode-ncname "\\(:?\\)" "\\(" xquery-mode-ncname "?\\)"))
|
|
193
|
|
194 ;; highlighting
|
|
195 ;; these are "matcher . highlighter" forms
|
|
196 (font-lock-add-keywords
|
|
197 'xquery-mode
|
|
198 `(
|
|
199 ;; prolog version decl
|
|
200 ("\\(xquery\\s-+version\\)\\s-+"
|
|
201 (1 font-lock-keyword-face))
|
|
202 ;; namespace default decl for 0.9 or 1.0
|
|
203 (,(concat
|
|
204 "\\(\\(declare\\)?"
|
|
205 "\\(\\s-+default\\s-+\\(function\\|element\\)\\)"
|
|
206 "\\s-+namespace\\)\\s-+")
|
|
207 (1 font-lock-keyword-face))
|
|
208 ;; namespace decl
|
|
209 (,(concat
|
|
210 "\\(declare\\s-+namespace\\)\\s-+")
|
|
211 (1 font-lock-keyword-face))
|
|
212 ;; option decl
|
|
213 (,(concat "\\(declare\\s-+option\\s-+" xquery-mode-qname "\\)")
|
|
214 (1 font-lock-keyword-face))
|
|
215 ;; import module decl - must precede library module decl
|
|
216 ("\\(import\\s-+module\\)\\s-+\\(namespace\\)?\\s-+"
|
|
217 (1 font-lock-keyword-face)
|
|
218 (2 font-lock-keyword-face))
|
|
219 ;; library module decl, for 1.0 or 0.9-ml
|
|
220 ("\\(module\\)\\s-+\\(namespace\\)?\\s-*"
|
|
221 (1 font-lock-keyword-face)
|
|
222 (2 font-lock-keyword-face))
|
|
223 ;; import schema decl
|
|
224 ("\\(import\\s-+schema\\)\\s-+\\(namespace\\)?\\s-+"
|
|
225 (1 font-lock-keyword-face)
|
|
226 (2 font-lock-keyword-face))
|
|
227 ;; variable decl
|
|
228 ("\\(for\\|let\\|declare\\s-+variable\\|define\\s-+variable\\)\\s-+\\$"
|
|
229 (1 font-lock-keyword-face))
|
|
230 ;; variable name
|
|
231 (,(concat "\\($" xquery-mode-qname "\\)")
|
|
232 (1 font-lock-variable-name-face))
|
|
233 ;; function decl
|
|
234 (,(concat
|
|
235 "\\(declare\\s-+function\\"
|
|
236 "|declare\\s-+private\\s-+function\\"
|
|
237 "|define\\s-+function\\)\\s-+\\("
|
|
238 xquery-mode-qname "\\)(")
|
|
239 (1 font-lock-keyword-face)
|
|
240 (2 font-lock-function-name-face))
|
|
241 ;; schema test or type decl
|
|
242 (,(concat
|
|
243 "\\("
|
|
244 "case"
|
|
245 "\\|instance\\s-+of\\|castable\\s-+as\\|treat\\s-+as\\|cast\\s-+as"
|
|
246 ;; "as" must be last in the list
|
|
247 "\\|as"
|
|
248 "\\)"
|
|
249 "\\s-+\\(" xquery-mode-qname "\\)"
|
|
250 ;; type may be followed by element() or element(x:foo)
|
|
251 "(?\\s-*\\(" xquery-mode-qname "\\)?\\s-*)?")
|
|
252 (1 font-lock-keyword-face)
|
|
253 (2 font-lock-type-face)
|
|
254 ; TODO the second qname never matches
|
|
255 (3 font-lock-type-face))
|
|
256 ;; function call
|
|
257 (,(concat "\\(" xquery-mode-qname "\\)(")
|
|
258 (1 font-lock-function-name-face))
|
|
259 ;; named node constructor
|
|
260 (,(concat "\\(attribute\\|element\\)\\s-+\\(" xquery-mode-qname "\\)\\s-*{")
|
|
261 (1 font-lock-keyword-face)
|
|
262 (2 font-lock-constant-face))
|
|
263 ;; anonymous node constructor
|
|
264 ("\\(binary\\|comment\\|document\\|text\\)\\s-*{"
|
|
265 (1 font-lock-keyword-face))
|
|
266 ;; typeswitch default
|
|
267 ("\\(default\\s-+return\\)\\s-+"
|
|
268 (1 font-lock-keyword-face)
|
|
269 (2 font-lock-keyword-face))
|
|
270 ;;
|
|
271 ;; highlighting - use nxml config to font-lock directly-constructed XML
|
|
272 ;;
|
|
273 ;; xml start element start
|
|
274 (,(concat "<" xquery-mode-qname)
|
|
275 (1 'nxml-element-prefix-face)
|
|
276 (2 'nxml-element-colon-face)
|
|
277 (3 'nxml-element-prefix-face))
|
|
278 ;; xml start element end
|
|
279 ("\\(/?\\)>"
|
|
280 (1 'nxml-tag-slash-face))
|
|
281 ;; xml end element
|
|
282 (,(concat "<\\(/\\)" xquery-mode-qname ">")
|
|
283 (1 'nxml-tag-slash-face)
|
|
284 (2 'nxml-element-prefix-face)
|
|
285 (3 'nxml-element-colon-face)
|
|
286 (4 'nxml-element-local-name-face))
|
|
287 ;; TODO xml attribute or xmlns decl
|
|
288 ;; (,(concat xquery-mode-qname "=\\([\"']\\)\\(.*?\\)\\([\"']\\)")
|
|
289 ;; (1 'nxml-attribute-prefix-face)
|
|
290 ;; (2 'nxml-attribute-colon-face)
|
|
291 ;; (3 'nxml-attribute-local-name-face)
|
|
292 ;; (4 'nxml-attribute-value-delimiter-face)
|
|
293 ;; (5 'nxml-attribute-value-face)
|
|
294 ;; (6 'nxml-attribute-value-delimiter-face))
|
|
295 ;; xml comments
|
|
296 ("\\(<!--\\)\\([^-]*\\)\\(-->\\)"
|
|
297 (1 'nxml-comment-delimiter-face)
|
|
298 (2 'nxml-comment-content-face)
|
|
299 (3 'nxml-comment-delimiter-face))
|
|
300 ;; highlighting XPath expressions, including *:foo
|
|
301 ;; TODO this doesn't match expressions unless they start with slash
|
|
302 ;; TODO but matching without a leading slash overrides all the keywords
|
|
303 (,(concat "\\(//?\\)\\(*\\|\\sw*\\)\\(:?\\)" xquery-mode-ncname)
|
|
304 (1 font-lock-constant-face)
|
|
305 (2 font-lock-constant-face)
|
|
306 (3 font-lock-constant-face)
|
|
307 (4 font-lock-constant-face))
|
|
308 ;;
|
|
309 ;; highlighting pseudo-keywords - must be late, for problems like 'if ()'
|
|
310 ;;
|
|
311 (,xquery-mode-keywords-regex (1 font-lock-keyword-face))
|
|
312 ))
|
|
313
|
|
314 ;; file-extension mappings
|
|
315 ;;;###autoload
|
|
316 (add-to-list 'auto-mode-alist '(".xq[erxy]\\'" . xquery-mode))
|
|
317
|
|
318 (defun xquery-forward-sexp (&optional arg)
|
|
319 "XQuery forward s-expresssion.
|
|
320 This function is not very smart. It tries to use
|
|
321 `nxml-forward-balanced-item' if it sees '>' or '<' characters in
|
|
322 the current line (ARG), and uses the regular `forward-sexp'
|
|
323 otherwise."
|
|
324 (if (> arg 0)
|
|
325 (progn
|
|
326 (if (looking-at "\\s-*<")
|
|
327 (nxml-forward-balanced-item arg)
|
|
328 (let ((forward-sexp-function nil)) (forward-sexp arg))))
|
|
329 (if (looking-back ">\\s-*")
|
|
330 (nxml-forward-balanced-item arg)
|
|
331 (let ((forward-sexp-function nil)) (forward-sexp arg)))))
|
|
332
|
|
333 ;; indentation
|
|
334 (defvar xquery-indent-size tab-width "The size of each indent level.")
|
|
335
|
|
336 ;; (setq debug-on-error t) ;\ DEBUG ::)
|
|
337
|
|
338 (defvar xquery-indent-debug nil)
|
|
339
|
|
340 ;; (setq xquery-indent-debug t) ;\ DEBUG ::)
|
|
341
|
|
342 (defun xquery-toggle-debug-indent ()
|
|
343 "Toggle the debug flag used in `xquery-calculate-indentation'."
|
|
344 (interactive)
|
|
345 (setq xquery-indent-debug (not xquery-indent-debug))
|
|
346 (message "xquery-indent-debug is %sabled"
|
|
347 (if xquery-indent-debug "en" "dis")))
|
|
348
|
|
349 (defun xquery-indent-debug-toggle ()
|
|
350 "Toggle the debug flag used in `xquery-calculate-indentation'."
|
|
351 (interactive) (xquery-toggle-debug-indent))
|
|
352
|
|
353 (defun xquery-indent-debug-message (results)
|
|
354 "Utility function to display debug messages for indentation.
|
|
355 RESULTS must be a list of a column number and a string message."
|
|
356 (if xquery-indent-debug
|
|
357 (let ((cc (car results))
|
|
358 (msg (cdr results)))
|
|
359 (message "xquery-indent-debug: (%d) %S" cc msg)) ) )
|
25
|
360
|
|
361 (defun xquery-set-indent-function ()
|
|
362 "Set the indent function for xquery mode."
|
|
363 (setq nxml-prolog-end (point-min))
|
|
364 (setq nxml-scan-end (copy-marker (point-min) nil))
|
|
365 (set (make-local-variable 'indent-line-function) 'xquery-indent-line)
|
|
366 (make-local-variable 'forward-sexp-function)
|
|
367 (setq forward-sexp-function 'xquery-forward-sexp)
|
27
|
368 (local-set-key "/" 'nxml-electric-slash))
|
25
|
369
|
|
370 (defun xquery-indent-line ()
|
|
371 "Indent current line as xquery code."
|
|
372 (interactive)
|
27
|
373 (let ((savept (> (current-column) (current-indentation)))
|
|
374 (results (xquery-calculate-indentation)))
|
|
375 (xquery-indent-debug-message results)
|
|
376 (let ( (indent (car results)) )
|
|
377 (if (> indent -1)
|
|
378 (if savept
|
|
379 (save-excursion (indent-line-to indent))
|
|
380 (indent-line-to (max 0 indent)) ) ) ) ) )
|
25
|
381
|
27
|
382 (defun xquery-indent-via-nxml ()
|
|
383 "This function uses nxml to calculate the indentation."
|
|
384 (let ((nxml-prolog-end (point-min))
|
|
385 (nxml-scan-end (copy-marker (point-min) nil)) )
|
|
386 (nxml-compute-indent) ) )
|
25
|
387
|
27
|
388 ;; to make debugging easier, use setq to set the actual values
|
|
389 (defvar xquery-indent-regex ""
|
|
390 "A regular expression indicating an indentable xquery sub-expression.")
|
|
391
|
|
392 (setq xquery-indent-regex
|
|
393 (concat "^\\s-*\\("
|
|
394 "typeswitch\\|for\\|let\\|where\\|order\\s-+by\\|return"
|
|
395 "\\|if\\|then\\|else"
|
|
396 "\\)\\s-*$") )
|
25
|
397
|
|
398 (defun xquery-calculate-indentation ()
|
27
|
399 "Calculate the indentation for a line of XQuery.
|
|
400 This function returns the column to which the current line should be indented,
|
|
401 and a debug expression."
|
|
402 (save-excursion
|
|
403 (beginning-of-line)
|
25
|
404 (cond
|
|
405
|
27
|
406 ;; TODO this sort of works, but needs to set some state
|
|
407 ;; TODO once we have state, how and when do we reset it?
|
|
408 ;; ((save-excursion
|
|
409 ;; (previous-line)
|
|
410 ;; (message "current-word = %S" (current-word)) ; DEBUG
|
|
411 ;; (message "looking-at xquery-indent-regex = %S"
|
|
412 ;; (looking-at xquery-indent-regex)) ; DEBUG
|
|
413 ;; (looking-at xquery-indent-regex))
|
|
414 ;; (save-excursion
|
|
415 ;; (previous-line)
|
|
416 ;; (list
|
|
417 ;; (+ xquery-indent-size (current-indentation))
|
|
418 ;; "previous line starts new block")))
|
25
|
419
|
27
|
420 ;; default, using sexp parser
|
|
421 (t
|
|
422 ;; calculate indent for beginning of line indent, then end of line
|
|
423 (let* ((point-bol (point))
|
|
424 (results-bol (parse-partial-sexp (point-min) point-bol))
|
|
425 ;; 0. depth in parens.
|
|
426 (paren-level-bol (car results-bol))
|
|
427 ;; 1. character address of start of innermost containing list.
|
|
428 (list-start-bol (car (cdr results-bol)))
|
|
429 ;; 2. character address of start of last complete sexp.
|
|
430 (sexp-start-bol (car (cdr (cdr results-bol))) )
|
|
431 ;; 3. non-nil if inside a string.
|
|
432 (stringp-bol (car (cdr (cdr (cdr results-bol)))) )
|
|
433 ;; 4. nil if outside comment, t if inside non-nesting comment,
|
|
434 ;; else integer comment nesting.
|
|
435 (comment-level-bol
|
|
436 (car (cdr (cdr (cdr (cdr results-bol))))) )
|
|
437 ;; 5. t if following a quote character.
|
|
438 (quotep-bol
|
|
439 (car (cdr (cdr (cdr (cdr (cdr results-bol)))))) )
|
|
440 ;; 6. the minimum paren-depth encountered during this scan.
|
|
441 (min-level-bol
|
|
442 (car (cdr (cdr (cdr (cdr (cdr (cdr results-bol))))))) )
|
|
443 ;; 7. t if in a comment of style b;
|
|
444 ;; symbol 'syntax-table' if the comment is generic.
|
|
445 (bcommentp-bol
|
|
446 (car (cdr (cdr (cdr (cdr (cdr (cdr (cdr results-bol)))))))) )
|
|
447 ;; 8. character address of start of comment or string, else nil.
|
|
448 (comment-start-bol
|
|
449 (car (cdr (cdr
|
|
450 (cdr (cdr (cdr (cdr (cdr
|
|
451 (cdr results-bol))))))))))
|
|
452 ;; 9. intermediate data for continuation of parsing. (not used)
|
25
|
453
|
27
|
454 (point-eol (save-excursion (end-of-line) (point)))
|
|
455 ;; undocumented, but parse-partial-sexp seems to change point
|
|
456 ;; TODO use state-bol? seems to have problems
|
|
457 (results-eol (save-excursion
|
|
458 (parse-partial-sexp (point-min) point-eol)))
|
|
459 ;; what would nxml do?
|
|
460 (results-nxml
|
|
461 (cond
|
|
462 ((looking-at "\\s-*<!--")
|
|
463 (list (xquery-indent-via-nxml) "xml start-comment"))
|
|
464 ((looking-at "\\s-*-->")
|
|
465 (list (xquery-indent-via-nxml) "xml end-comment"))
|
|
466 ((looking-at "\\s-*<\\sw+")
|
|
467 (list (xquery-indent-via-nxml) "xml start-element"))
|
|
468 ((looking-at "\\s-*</?\\sw+")
|
|
469 (list (xquery-indent-via-nxml) "xml end-element"))
|
|
470 (t nil) ) )
|
|
471 ;; later we will multiple by xquery-indent-size
|
|
472 (nxml-indent
|
|
473 (if results-nxml
|
|
474 (/ (car results-nxml) xquery-indent-size)))
|
|
475 )
|
25
|
476 (if xquery-indent-debug
|
27
|
477 (progn
|
|
478 (message "point-bol = %S" point-bol)
|
|
479 (message "point-eol = %S" point-eol)
|
|
480 (message "point = %S" (point))
|
|
481 (message "results-eol = %S" results-eol)
|
|
482 (message "results-nxml = %S" results-nxml)))
|
|
483 (let* (
|
|
484 ;; 0. depth in parens
|
|
485 (paren-level-eol (car results-eol))
|
|
486 (indent
|
|
487 (cond
|
|
488 (comment-level-bol
|
|
489 ; within a multi-line comment
|
|
490 ; start of comment indentation + 1
|
|
491 (+ 1 (save-excursion
|
|
492 (goto-char comment-start-bol)
|
|
493 (current-indentation) )) )
|
|
494 ; TODO multi-line prolog variable?
|
|
495 (nil -1)
|
|
496 ; mult-line module import?
|
|
497 ((and (save-excursion
|
|
498 (beginning-of-line)
|
|
499 (looking-at "^\\s-*at\\s-+"))
|
|
500 (save-excursion
|
|
501 (beginning-of-line)
|
|
502 (previous-line)
|
|
503 (looking-at "^\\s-*import\\s-+module\\s-+")))
|
|
504 xquery-indent-size)
|
|
505 ; multi-line function decl?
|
|
506 ; TODO handle more than 1 line previous
|
|
507 ((and (save-excursion
|
|
508 (beginning-of-line)
|
|
509 (looking-at "^\\s-*as\\s-+"))
|
|
510 (save-excursion
|
|
511 (beginning-of-line)
|
|
512 (previous-line)
|
|
513 (looking-at
|
|
514 "^\\s-*\\(define\\|declare\\)\\s-+function\\s-+")))
|
|
515 xquery-indent-size)
|
|
516 ; default - use paren-level-bol
|
|
517 (t (* xquery-indent-size
|
|
518 ; special when simply closing 1 level
|
|
519 (cond
|
|
520 ((and (= paren-level-bol (+ 1 paren-level-eol))
|
|
521 (looking-at "^\\s-*\\s)[,;]?\\s-*$") )
|
|
522 paren-level-eol)
|
|
523 ; factor in the nxml-indent
|
|
524 ((and
|
|
525 nxml-indent (> nxml-indent paren-level-bol))
|
|
526 nxml-indent)
|
|
527 (t paren-level-bol)))))))
|
|
528 (list (min 70 indent) results-bol results-eol)))))))
|
25
|
529
|
|
530 (provide 'xquery-mode)
|
|
531
|
|
532 ;;; xquery-mode.el ends here
|