adamc@350
|
1 ;;; urweb-move.el --- Buffer navigation functions for urweb-mode
|
adamc@350
|
2
|
adamc@350
|
3 ;; Based on urweb-mode:
|
adamc@350
|
4 ;; Copyright (C) 1999, 2000, 2004 Stefan Monnier <monnier@gnu.org>
|
adamc@350
|
5 ;;
|
adamc@350
|
6 ;; Modified for urweb-mode:
|
adamc@350
|
7 ;; Copyright (C) 2008 Adam Chlipala <adamc@hcoop.net>
|
adamc@350
|
8 ;;
|
adamc@350
|
9 ;; This program is free software; you can redistribute it and/or modify
|
adamc@350
|
10 ;; it under the terms of the GNU General Public License as published by
|
adamc@350
|
11 ;; the Free Software Foundation; either version 2 of the License, or
|
adamc@350
|
12 ;; (at your option) any later version.
|
adamc@350
|
13 ;;
|
adamc@350
|
14 ;; This program is distributed in the hope that it will be useful,
|
adamc@350
|
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
adamc@350
|
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
adamc@350
|
17 ;; GNU General Public License for more details.
|
adamc@350
|
18 ;;
|
adamc@350
|
19 ;; You should have received a copy of the GNU General Public License
|
adamc@350
|
20 ;; along with this program; if not, write to the Free Software
|
adamc@350
|
21 ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
adamc@350
|
22
|
adamc@350
|
23
|
adamc@350
|
24 ;;; Commentary:
|
adamc@350
|
25
|
adamc@350
|
26
|
adamc@350
|
27 ;;; Code:
|
adamc@350
|
28
|
adamc@350
|
29 (eval-when-compile (require 'cl))
|
adamc@350
|
30 (require 'urweb-util)
|
adamc@350
|
31 (require 'urweb-defs)
|
adamc@350
|
32
|
adamc@350
|
33 (defsyntax urweb-internal-syntax-table
|
adamc@350
|
34 '((?_ . "w")
|
adamc@350
|
35 (?' . "w")
|
adamc@350
|
36 (?. . "w"))
|
adamc@350
|
37 "Syntax table used for internal urweb-mode operation."
|
adamc@350
|
38 :copy urweb-mode-syntax-table)
|
adamc@350
|
39
|
adamc@350
|
40 ;;;
|
adamc@350
|
41 ;;; various macros
|
adamc@350
|
42 ;;;
|
adamc@350
|
43
|
adamc@350
|
44 (defmacro urweb-with-ist (&rest r)
|
adamc@350
|
45 (let ((ost-sym (make-symbol "oldtable")))
|
adamc@350
|
46 `(let ((,ost-sym (syntax-table))
|
adamc@350
|
47 (case-fold-search nil)
|
adamc@350
|
48 (parse-sexp-lookup-properties t)
|
adamc@350
|
49 (parse-sexp-ignore-comments t))
|
adamc@350
|
50 (unwind-protect
|
adamc@350
|
51 (progn (set-syntax-table urweb-internal-syntax-table) . ,r)
|
adamc@350
|
52 (set-syntax-table ,ost-sym)))))
|
adamc@350
|
53 (def-edebug-spec urweb-with-ist t)
|
adamc@350
|
54
|
adamc@350
|
55 (defmacro urweb-move-if (&rest body)
|
adamc@350
|
56 (let ((pt-sym (make-symbol "point"))
|
adamc@350
|
57 (res-sym (make-symbol "result")))
|
adamc@350
|
58 `(let ((,pt-sym (point))
|
adamc@350
|
59 (,res-sym ,(cons 'progn body)))
|
adamc@350
|
60 (unless ,res-sym (goto-char ,pt-sym))
|
adamc@350
|
61 ,res-sym)))
|
adamc@350
|
62 (def-edebug-spec urweb-move-if t)
|
adamc@350
|
63
|
adamc@350
|
64 (defmacro urweb-point-after (&rest body)
|
adamc@350
|
65 `(save-excursion
|
adamc@350
|
66 ,@body
|
adamc@350
|
67 (point)))
|
adamc@350
|
68 (def-edebug-spec urweb-point-after t)
|
adamc@350
|
69
|
adamc@350
|
70 ;;
|
adamc@350
|
71
|
adamc@350
|
72 (defvar urweb-op-prec
|
adamc@350
|
73 (urweb-preproc-alist
|
adamc@350
|
74 '((("UNION" "INTERSECT" "EXCEPT") . 0)
|
adamc@350
|
75 (("AND" "OR") . 1)
|
adamc@350
|
76 ((">" ">=" "<>" "<" "<=" "=") . 4)
|
adamc@350
|
77 (("+" "-" "^") . 6)
|
adamc@350
|
78 (("/" "*" "%") . 7)
|
adamc@350
|
79 (("++" "--") 8)
|
adamc@350
|
80 (("NOT") 9)
|
adamc@353
|
81 (("~") 10)))
|
adamc@350
|
82 "Alist of Ur/Web infix operators and their precedence.")
|
adamc@350
|
83
|
adamc@350
|
84 (defconst urweb-syntax-prec
|
adamc@350
|
85 (urweb-preproc-alist
|
adamc@350
|
86 `(((";" ",") . 20)
|
adamc@350
|
87 (("=>" "d=" "=of") . (65 . 40))
|
adamc@350
|
88 ("|" . (47 . 30))
|
adamc@350
|
89 (("case" "of" "fn") . 45)
|
adamc@350
|
90 (("if" "then" "else" ) . 50)
|
adamc@350
|
91 (("<-") . 55)
|
adamc@350
|
92 ("||" . 70)
|
adamc@350
|
93 ("&&" . 80)
|
adamc@350
|
94 ((":" "::" ":::" ":>") . 90)
|
adamc@350
|
95 ("->" . 95)
|
adamc@350
|
96 ("with" . 100)
|
adamc@350
|
97 (,(cons "end" urweb-begin-syms) . 10000)))
|
adamc@350
|
98 "Alist of pseudo-precedence of syntactic elements.")
|
adamc@350
|
99
|
adamc@350
|
100 (defun urweb-op-prec (op dir)
|
adamc@350
|
101 "Return the precedence of OP or nil if it's not an infix.
|
adamc@350
|
102 DIR should be set to BACK if you want to precedence w.r.t the left side
|
adamc@350
|
103 and to FORW for the precedence w.r.t the right side.
|
adamc@350
|
104 This assumes that we are `looking-at' the OP."
|
adamc@350
|
105 (when op
|
adamc@350
|
106 (let ((sprec (cdr (assoc op urweb-syntax-prec))))
|
adamc@350
|
107 (cond
|
adamc@350
|
108 ((consp sprec) (if (eq dir 'back) (car sprec) (cdr sprec)))
|
adamc@350
|
109 (sprec sprec)
|
adamc@350
|
110 (t
|
adamc@350
|
111 (let ((prec (cdr (assoc op urweb-op-prec))))
|
adamc@350
|
112 (when prec (+ prec 100))))))))
|
adamc@350
|
113
|
adamc@350
|
114 ;;
|
adamc@350
|
115
|
adamc@350
|
116 (defun urweb-forward-spaces () (forward-comment 100000))
|
adamc@350
|
117 (defun urweb-backward-spaces () (forward-comment -100000))
|
adamc@350
|
118
|
adamc@350
|
119
|
adamc@350
|
120 ;;
|
adamc@350
|
121 ;; moving forward around matching symbols
|
adamc@350
|
122 ;;
|
adamc@350
|
123
|
adamc@350
|
124 (defun urweb-looking-back-at (re)
|
adamc@350
|
125 (save-excursion
|
adamc@350
|
126 (when (= 0 (skip-syntax-backward "w_")) (backward-char))
|
adamc@350
|
127 (looking-at re)))
|
adamc@350
|
128
|
adamc@350
|
129 (defun urweb-find-match-forward (this match)
|
adamc@350
|
130 "Only works for word matches."
|
adamc@350
|
131 (let ((level 1)
|
adamc@350
|
132 (forward-sexp-function nil)
|
adamc@350
|
133 (either (concat this "\\|" match)))
|
adamc@350
|
134 (while (> level 0)
|
adamc@350
|
135 (forward-sexp 1)
|
adamc@350
|
136 (while (not (or (eobp) (urweb-looking-back-at either)))
|
adamc@350
|
137 (condition-case () (forward-sexp 1) (error (forward-char 1))))
|
adamc@350
|
138 (setq level
|
adamc@350
|
139 (cond
|
adamc@350
|
140 ((and (eobp) (> level 1)) (error "Unbalanced"))
|
adamc@350
|
141 ((urweb-looking-back-at this) (1+ level))
|
adamc@350
|
142 ((urweb-looking-back-at match) (1- level))
|
adamc@350
|
143 (t (error "Unbalanced")))))
|
adamc@350
|
144 t))
|
adamc@350
|
145
|
adamc@350
|
146 (defun urweb-find-match-backward (this match)
|
adamc@350
|
147 (let ((level 1)
|
adamc@350
|
148 (forward-sexp-function nil)
|
adamc@350
|
149 (either (concat this "\\|" match)))
|
adamc@350
|
150 (while (> level 0)
|
adamc@350
|
151 (backward-sexp 1)
|
adamc@350
|
152 (while (not (or (bobp) (looking-at either)))
|
adamc@350
|
153 (condition-case () (backward-sexp 1) (error (backward-char 1))))
|
adamc@350
|
154 (setq level
|
adamc@350
|
155 (cond
|
adamc@350
|
156 ((and (bobp) (> level 1)) (error "Unbalanced"))
|
adamc@350
|
157 ((looking-at this) (1+ level))
|
adamc@350
|
158 ((looking-at match) (1- level))
|
adamc@350
|
159 (t (error "Unbalanced")))))
|
adamc@350
|
160 t))
|
adamc@350
|
161
|
adamc@350
|
162 ;;;
|
adamc@350
|
163 ;;; read a symbol, including the special "op <sym>" case
|
adamc@350
|
164 ;;;
|
adamc@350
|
165
|
adamc@350
|
166 (defmacro urweb-move-read (&rest body)
|
adamc@350
|
167 (let ((pt-sym (make-symbol "point")))
|
adamc@350
|
168 `(let ((,pt-sym (point)))
|
adamc@350
|
169 ,@body
|
adamc@350
|
170 (when (/= (point) ,pt-sym)
|
adamc@350
|
171 (buffer-substring-no-properties (point) ,pt-sym)))))
|
adamc@350
|
172 (def-edebug-spec urweb-move-read t)
|
adamc@350
|
173
|
adamc@350
|
174 (defun urweb-poly-equal-p ()
|
adamc@350
|
175 (< (urweb-point-after (re-search-backward urweb-=-starter-re nil 'move))
|
adamc@350
|
176 (urweb-point-after (re-search-backward "=" nil 'move))))
|
adamc@350
|
177
|
adamc@350
|
178 (defun urweb-nested-of-p ()
|
adamc@350
|
179 (< (urweb-point-after
|
adamc@350
|
180 (re-search-backward urweb-non-nested-of-starter-re nil 'move))
|
adamc@350
|
181 (urweb-point-after (re-search-backward "\\<case\\>" nil 'move))))
|
adamc@350
|
182
|
adamc@350
|
183 (defun urweb-forward-sym-1 ()
|
adamc@350
|
184 (or (/= 0 (skip-syntax-forward "'w_"))
|
adamc@350
|
185 (/= 0 (skip-syntax-forward ".'"))))
|
adamc@350
|
186 (defun urweb-forward-sym ()
|
adamc@350
|
187 (let ((sym (urweb-move-read (urweb-forward-sym-1))))
|
adamc@350
|
188 (cond
|
adamc@350
|
189 ((equal "op" sym)
|
adamc@350
|
190 (urweb-forward-spaces)
|
adamc@350
|
191 (concat "op " (or (urweb-move-read (urweb-forward-sym-1)) "")))
|
adamc@350
|
192 ((equal sym "=")
|
adamc@350
|
193 (save-excursion
|
adamc@350
|
194 (urweb-backward-sym-1)
|
adamc@350
|
195 (if (urweb-poly-equal-p) "=" "d=")))
|
adamc@350
|
196 ((equal sym "of")
|
adamc@350
|
197 (save-excursion
|
adamc@350
|
198 (urweb-backward-sym-1)
|
adamc@350
|
199 (if (urweb-nested-of-p) "of" "=of")))
|
adamc@350
|
200 ;; ((equal sym "datatype")
|
adamc@350
|
201 ;; (save-excursion
|
adamc@350
|
202 ;; (urweb-backward-sym-1)
|
adamc@350
|
203 ;; (urweb-backward-spaces)
|
adamc@350
|
204 ;; (if (eq (preceding-char) ?=) "=datatype" sym)))
|
adamc@350
|
205 (t sym))))
|
adamc@350
|
206
|
adamc@350
|
207 (defun urweb-backward-sym-1 ()
|
adamc@350
|
208 (or (/= 0 (skip-syntax-backward ".'"))
|
adamc@350
|
209 (/= 0 (skip-syntax-backward "'w_"))))
|
adamc@350
|
210 (defun urweb-backward-sym ()
|
adamc@350
|
211 (let ((sym (urweb-move-read (urweb-backward-sym-1))))
|
adamc@350
|
212 (when sym
|
adamc@350
|
213 ;; FIXME: what should we do if `sym' = "op" ?
|
adamc@350
|
214 (let ((point (point)))
|
adamc@350
|
215 (urweb-backward-spaces)
|
adamc@350
|
216 (if (equal "op" (urweb-move-read (urweb-backward-sym-1)))
|
adamc@350
|
217 (concat "op " sym)
|
adamc@350
|
218 (goto-char point)
|
adamc@350
|
219 (cond
|
adamc@350
|
220 ((string= sym "=") (if (urweb-poly-equal-p) "=" "d="))
|
adamc@350
|
221 ((string= sym "of") (if (urweb-nested-of-p) "of" "=of"))
|
adamc@350
|
222 ;; ((string= sym "datatype")
|
adamc@350
|
223 ;; (save-excursion (urweb-backward-spaces)
|
adamc@350
|
224 ;; (if (eq (preceding-char) ?=) "=datatype" sym)))
|
adamc@350
|
225 (t sym)))))))
|
adamc@350
|
226
|
adamc@350
|
227
|
adamc@350
|
228 (defun urweb-backward-sexp (prec)
|
adamc@350
|
229 "Move one sexp backward if possible, or one char else.
|
adamc@350
|
230 Returns t if the move indeed moved through one sexp and nil if not.
|
adamc@350
|
231 PREC is the precedence currently looked for."
|
adamc@350
|
232 (let ((parse-sexp-lookup-properties t)
|
adamc@350
|
233 (parse-sexp-ignore-comments t))
|
adamc@350
|
234 (urweb-backward-spaces)
|
adamc@350
|
235 (let* ((op (urweb-backward-sym))
|
adamc@350
|
236 (op-prec (urweb-op-prec op 'back))
|
adamc@350
|
237 match)
|
adamc@350
|
238 (cond
|
adamc@350
|
239 ((not op)
|
adamc@350
|
240 (let ((point (point)))
|
adamc@350
|
241 (ignore-errors (let ((forward-sexp-function nil)) (backward-sexp 1)))
|
adamc@350
|
242 (if (/= point (point)) t (ignore-errors (backward-char 1)) nil)))
|
adamc@350
|
243 ;; stop as soon as precedence is smaller than `prec'
|
adamc@350
|
244 ((and prec op-prec (>= prec op-prec)) nil)
|
adamc@350
|
245 ;; special rules for nested constructs like if..then..else
|
adamc@350
|
246 ((and (or (not prec) (and prec op-prec))
|
adamc@350
|
247 (setq match (second (assoc op urweb-close-paren))))
|
adamc@350
|
248 (urweb-find-match-backward (concat "\\<" op "\\>") match))
|
adamc@350
|
249 ;; don't back over open-parens
|
adamc@350
|
250 ((assoc op urweb-open-paren) nil)
|
adamc@350
|
251 ;; infix ops precedence
|
adamc@350
|
252 ((and prec op-prec) (< prec op-prec))
|
adamc@350
|
253 ;; [ prec = nil ] a new operator, let's skip the sexps until the next
|
adamc@350
|
254 (op-prec (while (urweb-move-if (urweb-backward-sexp op-prec))) t)
|
adamc@350
|
255 ;; special symbols indicating we're getting out of a nesting level
|
adamc@350
|
256 ((string-match urweb-sexp-head-symbols-re op) nil)
|
adamc@350
|
257 ;; if the op was not alphanum, then we still have to do the backward-sexp
|
adamc@350
|
258 ;; this reproduces the usual backward-sexp, but it might be bogus
|
adamc@350
|
259 ;; in this case since !@$% is a perfectly fine symbol
|
adamc@350
|
260 (t t))))) ;(or (string-match "\\sw" op) (urweb-backward-sexp prec))
|
adamc@350
|
261
|
adamc@350
|
262 (defun urweb-forward-sexp (prec)
|
adamc@350
|
263 "Moves one sexp forward if possible, or one char else.
|
adamc@350
|
264 Returns T if the move indeed moved through one sexp and NIL if not."
|
adamc@350
|
265 (let ((parse-sexp-lookup-properties t)
|
adamc@350
|
266 (parse-sexp-ignore-comments t))
|
adamc@350
|
267 (urweb-forward-spaces)
|
adamc@350
|
268 (let* ((op (urweb-forward-sym))
|
adamc@350
|
269 (op-prec (urweb-op-prec op 'forw))
|
adamc@350
|
270 match)
|
adamc@350
|
271 (cond
|
adamc@350
|
272 ((not op)
|
adamc@350
|
273 (let ((point (point)))
|
adamc@350
|
274 (ignore-errors (let ((forward-sexp-function nil)) (forward-sexp 1)))
|
adamc@350
|
275 (if (/= point (point)) t (forward-char 1) nil)))
|
adamc@350
|
276 ;; stop as soon as precedence is smaller than `prec'
|
adamc@350
|
277 ((and prec op-prec (>= prec op-prec)) nil)
|
adamc@350
|
278 ;; special rules for nested constructs like if..then..else
|
adamc@350
|
279 ((and (or (not prec) (and prec op-prec))
|
adamc@350
|
280 (setq match (cdr (assoc op urweb-open-paren))))
|
adamc@350
|
281 (urweb-find-match-forward (first match) (second match)))
|
adamc@350
|
282 ;; don't forw over close-parens
|
adamc@350
|
283 ((assoc op urweb-close-paren) nil)
|
adamc@350
|
284 ;; infix ops precedence
|
adamc@350
|
285 ((and prec op-prec) (< prec op-prec))
|
adamc@350
|
286 ;; [ prec = nil ] a new operator, let's skip the sexps until the next
|
adamc@350
|
287 (op-prec (while (urweb-move-if (urweb-forward-sexp op-prec))) t)
|
adamc@350
|
288 ;; special symbols indicating we're getting out of a nesting level
|
adamc@350
|
289 ((string-match urweb-sexp-head-symbols-re op) nil)
|
adamc@350
|
290 ;; if the op was not alphanum, then we still have to do the backward-sexp
|
adamc@350
|
291 ;; this reproduces the usual backward-sexp, but it might be bogus
|
adamc@350
|
292 ;; in this case since !@$% is a perfectly fine symbol
|
adamc@350
|
293 (t t))))) ;(or (string-match "\\sw" op) (urweb-backward-sexp prec))
|
adamc@350
|
294
|
adamc@350
|
295 (defun urweb-in-word-p ()
|
adamc@350
|
296 (and (eq ?w (char-syntax (or (char-before) ? )))
|
adamc@350
|
297 (eq ?w (char-syntax (or (char-after) ? )))))
|
adamc@350
|
298
|
adamc@350
|
299 (defun urweb-user-backward-sexp (&optional count)
|
adamc@350
|
300 "Like `backward-sexp' but tailored to the Ur/Web syntax."
|
adamc@350
|
301 (interactive "p")
|
adamc@350
|
302 (unless count (setq count 1))
|
adamc@350
|
303 (urweb-with-ist
|
adamc@350
|
304 (let ((point (point)))
|
adamc@350
|
305 (if (< count 0) (urweb-user-forward-sexp (- count))
|
adamc@350
|
306 (when (urweb-in-word-p) (forward-word 1))
|
adamc@350
|
307 (dotimes (i count)
|
adamc@350
|
308 (unless (urweb-backward-sexp nil)
|
adamc@350
|
309 (goto-char point)
|
adamc@350
|
310 (error "Containing expression ends prematurely")))))))
|
adamc@350
|
311
|
adamc@350
|
312 (defun urweb-user-forward-sexp (&optional count)
|
adamc@350
|
313 "Like `forward-sexp' but tailored to the Ur/Web syntax."
|
adamc@350
|
314 (interactive "p")
|
adamc@350
|
315 (unless count (setq count 1))
|
adamc@350
|
316 (urweb-with-ist
|
adamc@350
|
317 (let ((point (point)))
|
adamc@350
|
318 (if (< count 0) (urweb-user-backward-sexp (- count))
|
adamc@350
|
319 (when (urweb-in-word-p) (backward-word 1))
|
adamc@350
|
320 (dotimes (i count)
|
adamc@350
|
321 (unless (urweb-forward-sexp nil)
|
adamc@350
|
322 (goto-char point)
|
adamc@350
|
323 (error "Containing expression ends prematurely")))))))
|
adamc@350
|
324
|
adamc@350
|
325 ;;(defun urweb-forward-thing ()
|
adamc@350
|
326 ;; (if (= ?w (char-syntax (char-after))) (forward-word 1) (forward-char 1)))
|
adamc@350
|
327
|
adamc@350
|
328 (defun urweb-backward-arg () (urweb-backward-sexp 1000))
|
adamc@350
|
329 (defun urweb-forward-arg () (urweb-forward-sexp 1000))
|
adamc@350
|
330
|
adamc@350
|
331
|
adamc@350
|
332 (provide 'urweb-move)
|
adamc@350
|
333
|
adamc@350
|
334 ;;; urweb-move.el ends here
|