annotate xquery-mode.el @ 27:803e4156c7a0 laptop

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