1 ;;; xquery-mode.el --- A simple mode for editing xquery programs
2 ;; Time-stamp: <2005-03-26 18:05:39 sacharya>
4 ;;; Copyright (C) 2005 Suraj Acharya
6 ;; Author: Suraj Acharya <sacharya@cs.indiana.edu>
8 ;; This file is not part of GNU Emacs.
10 ;; xquery-mode.el is free software; you can redistribute it
11 ;; and/or modify it under the terms of the GNU General Public License
12 ;; as published by the Free Software Foundation; either version 2, or
13 ;; (at your option) any later version.:
15 ;; This software is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
31 (define-generic-mode 'xquery-mode
32 '(("(:" . ":)") ("<!--" . "-->"))
33 '("xquery" "version" "encoding" "at" "module" "namespace" "child" "descendant" "parent" "attribute" "self" "descendant-or-self" "ancestor" "following-sibling" "preceding-sibling" "following" "preceding" "ancestor-or-self" "declare" "function" "option" "ordering" "ordered" "unordered" "default" "order" "external" "or" "and" "div" "idiv" "mod" "in" "construction" "satisfies" "return" "then" "else" "boundary-space" "base-uri" "preserve" "strip" "copy-namespaces" "no-preserve" "inherit" "no-inherit" "to" "where" "collation" "intersect" "union" "except" "as" "case" "instance" "of" "castable" "item" "element" "schema-element" "schema-attribute" "processing-instruction" "comment" "text" "empty" "import" "schema" "is" "eq" "ne" "gt" "ge" "lt" "le" "some" "every" "for" "let" "cast" "treat" "validate" "document-node" "document" "node" "if" "typeswitch" "by" "stable" "ascending" "descending" "greatest" "least" "variable") ;keywords
34 '(("\\(\\$\\w+\\)" 1 font-lock-variable-name-face) ;; \\(\\s_\\|\\w\\)
35 ("\\(\\w*:?\\w+\\)\\s *(" 1 font-lock-function-name-face)
36 ("\\(<\\)\\(/?\\)\\(\\w*\\)\\(:?\\)\\(\\w+\\).*?\\(/?\\)\\(>\\)"
37 (1 'nxml-tag-delimiter-face)
38 (2 'nxml-tag-slash-face)
39 (3 'nxml-element-prefix-face)
40 (4 'nxml-element-colon-face)
41 (5 'nxml-element-local-name-face)
42 (6 'nxml-tag-slash-face)
43 (7 'nxml-tag-delimiter-face)
45 ("\\(\\w*\\)\\(:?\\)\\(\\w+\\)=\\([\"']\\)\\(.*?\\)\\([\"']\\)"
46 (1 'nxml-attribute-prefix-face)
47 (2 'nxml-attribute-colon-face)
48 (3 'nxml-attribute-local-name-face)
49 (4 'nxml-attribute-value-delimiter-face)
50 (5 'nxml-attribute-value-face)
51 (6 'nxml-attribute-value-delimiter-face))
52 ("\\(/\\)\\(\\w*\\)\\(:?\\)\\(\\w+\\)"
53 (1 font-lock-constant-face)
54 (2 font-lock-constant-face)
55 (3 font-lock-constant-face)
56 (4 font-lock-constant-face)
58 ("as\\s +\\(\\w*:?\\w+\\)"
59 (1 font-lock-type-face)
62 '(".xq\\'") ;auto-mode-list
63 '(xquery-set-indent-function xquery-set-up-syntax-table) ;function list
64 "A Major mode for editing xquery."
69 (defun xquery-set-indent-function ()
70 "Set the indent function for xquery mode."
71 (setq nxml-prolog-end (point-min))
72 (setq nxml-scan-end (copy-marker (point-min) nil))
73 (set (make-local-variable 'indent-line-function) 'xquery-indent-line)
74 (make-local-variable 'forward-sexp-function)
75 (setq forward-sexp-function 'xquery-forward-sexp)
76 (local-set-key "/" 'nxml-electric-slash)
79 (defun xquery-forward-sexp (&optional arg)
80 "Xquery forward s-expresssion.
81 This function is not very smart, it tries to use
82 `nxml-forward-balanced-item' if it sees '>' or '<' characters in
83 the direction you are going, and uses the regular `forward-sexp'
87 (if (looking-at "[ \t]*<")
88 (nxml-forward-balanced-item arg)
89 (let ((forward-sexp-function nil)) (forward-sexp arg))))
90 (if (looking-back ">[ \t]*")
91 (nxml-forward-balanced-item arg)
92 (let ((forward-sexp-function nil)) (forward-sexp arg))))
96 (defun xquery-set-up-syntax-table ()
97 "Allow the hypen character to be recognized as part of a xquery symbol."
98 (modify-syntax-entry ?- "w" (syntax-table))
99 (modify-syntax-entry ?/ "." (syntax-table))
100 ;; set-up the syntax table correctly for parentheis type characters
101 (modify-syntax-entry ?\{ "(}" (syntax-table))
102 (modify-syntax-entry ?\} "){" (syntax-table))
103 (modify-syntax-entry ?\[ "(]" (syntax-table))
104 (modify-syntax-entry ?\] ")]" (syntax-table))
105 (modify-syntax-entry ?\< "(>1" (syntax-table))
106 (modify-syntax-entry ?\> ")<4" (syntax-table))
107 ;; xquery comments are like (: :)
108 (modify-syntax-entry ?\( "()1" (syntax-table))
109 (modify-syntax-entry ?\) ")(4" (syntax-table))
110 ;; (modify-syntax-entry ?\: ".23" (syntax-table))
115 (defun xquery-indent-line ()
116 "Indent current line as xquery code."
118 (let ((savep (> (current-column) (current-indentation)))
119 (indent (condition-case err (max (xquery-calculate-indentation) 0)
120 (error (message "%S" err)))))
122 (save-excursion (indent-line-to indent))
123 (indent-line-to indent))))
125 (defvar xquery-start-block-regexp "[ \t]*\\((\|{\\|for\\|let\\|where\\|return\\|if\\|else\\|typeswitch\\|declare[ \t]+function\\|.*[({]$\\)"
126 "A regular expression which indicates that a xquery block is starting.")
128 (defvar xquery-flwr-block-regexp "[ \t]*\\(for\\|let\\|where\\|return\\|order\\|stable\\s *order\\)")
130 (defvar xquery-indent-size 2
131 "The size of each indent level.")
133 (defvar xquery-indent-debug nil)
135 (defun xquery-toggle-debug-indent ()
136 "Toggle the debug flag used in `xquery-calculate-indentation'. "
138 (setq xquery-indent-debug (not xquery-indent-debug))
139 (message (concat "xquery-indent-debug is " (if xquery-indent-debug "en" "dis") "abled"))
142 (defun xquery-calculate-indentation ()
143 "Return the column to which the current line should be indented."
146 0 ; First line is always non-indented
147 (skip-chars-forward " \t")
149 ;; do nothing if this is a comment
150 ((eq (get-text-property (point) 'face) 'font-lock-comment-face) (current-indentation))
152 ((looking-at "\\(</?\\w\\|{\\)") ;; xml constructor or enclosed expressions
153 (if xquery-indent-debug
154 (message "xquery-indent-debug: xml constructor"))
155 (let ((nxml-prolog-end (point-min))
156 (nxml-scan-end (copy-marker (point-min) nil)))
157 (nxml-compute-indent)
160 ;; for close braces or else statements indent to the same level as the opening {
162 (if xquery-indent-debug
163 (message "xquery-indent-debug: }"))
166 (let ((cc (current-column)))
168 (if (looking-at xquery-start-block-regexp)
169 (current-indentation)
173 (if xquery-indent-debug
174 (message "xquery-indent-debug: else"))
176 (xquery-previous-non-empty-line)
177 (- (current-indentation) xquery-indent-size)
180 ;; for close parens, indent to the start of the func call
182 (if xquery-indent-debug
183 (message "xquery-indent-debug: )"))
186 (if (looking-back "\\w+\\s *")
191 ;; order flwr expressions on the same column
195 (looking-at xquery-flwr-block-regexp)
197 (xquery-previous-non-empty-line)
199 (looking-at xquery-flwr-block-regexp)))
200 (if xquery-indent-debug
201 (message "xquery-indent-debug: nested flwr"))
202 (current-indentation)
206 ;; if this is the first non-empty line after a block, indent xquery-indent-size chars relative to the block
208 (xquery-previous-non-empty-line)
210 (when (looking-at xquery-start-block-regexp)
211 (if xquery-indent-debug
212 (message "xquery-indent-debug: first line in block"))
213 (+ xquery-indent-size (current-indentation))))
216 ;; for everything else indent relative to the outer list
218 (if xquery-indent-debug
219 (message "xquery-indent-debug: everyting else"))
220 (save-excursion (xquery-previous-non-empty-line) (current-indentation)))
224 (defun xquery-previous-non-empty-line ()
225 "Move to the last non-empty line."
226 (re-search-backward "\\S " (point-min) t)
229 (provide 'xquery-mode)
231 ;;; xquery-mode.el ends here