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@354
|
79 (("NOT") 9)))
|
adamc@350
|
80 "Alist of Ur/Web infix operators and their precedence.")
|
adamc@350
|
81
|
adamc@350
|
82 (defconst urweb-syntax-prec
|
adamc@350
|
83 (urweb-preproc-alist
|
adamc@350
|
84 `(((";" ",") . 20)
|
adamc@350
|
85 (("=>" "d=" "=of") . (65 . 40))
|
adamc@350
|
86 ("|" . (47 . 30))
|
adamc@350
|
87 (("case" "of" "fn") . 45)
|
adamc@350
|
88 (("if" "then" "else" ) . 50)
|
adamc@350
|
89 (("<-") . 55)
|
adamc@350
|
90 ("||" . 70)
|
adamc@350
|
91 ("&&" . 80)
|
adamc@354
|
92 ((":" ":>") . 90)
|
adamc@350
|
93 ("->" . 95)
|
adamc@350
|
94 ("with" . 100)
|
adamc@350
|
95 (,(cons "end" urweb-begin-syms) . 10000)))
|
adamc@350
|
96 "Alist of pseudo-precedence of syntactic elements.")
|
adamc@350
|
97
|
adamc@350
|
98 (defun urweb-op-prec (op dir)
|
adamc@350
|
99 "Return the precedence of OP or nil if it's not an infix.
|
adamc@350
|
100 DIR should be set to BACK if you want to precedence w.r.t the left side
|
adamc@350
|
101 and to FORW for the precedence w.r.t the right side.
|
adamc@350
|
102 This assumes that we are `looking-at' the OP."
|
adamc@350
|
103 (when op
|
adamc@350
|
104 (let ((sprec (cdr (assoc op urweb-syntax-prec))))
|
adamc@350
|
105 (cond
|
adamc@350
|
106 ((consp sprec) (if (eq dir 'back) (car sprec) (cdr sprec)))
|
adamc@350
|
107 (sprec sprec)
|
adamc@350
|
108 (t
|
adamc@350
|
109 (let ((prec (cdr (assoc op urweb-op-prec))))
|
adamc@350
|
110 (when prec (+ prec 100))))))))
|
adamc@350
|
111
|
adamc@350
|
112 ;;
|
adamc@350
|
113
|
adamc@350
|
114 (defun urweb-forward-spaces () (forward-comment 100000))
|
adamc@350
|
115 (defun urweb-backward-spaces () (forward-comment -100000))
|
adamc@350
|
116
|
adamc@350
|
117
|
adamc@350
|
118 ;;
|
adamc@350
|
119 ;; moving forward around matching symbols
|
adamc@350
|
120 ;;
|
adamc@350
|
121
|
adamc@350
|
122 (defun urweb-looking-back-at (re)
|
adamc@350
|
123 (save-excursion
|
adamc@350
|
124 (when (= 0 (skip-syntax-backward "w_")) (backward-char))
|
adamc@350
|
125 (looking-at re)))
|
adamc@350
|
126
|
adamc@350
|
127 (defun urweb-find-match-forward (this match)
|
adamc@350
|
128 "Only works for word matches."
|
adamc@350
|
129 (let ((level 1)
|
adamc@350
|
130 (forward-sexp-function nil)
|
adamc@350
|
131 (either (concat this "\\|" match)))
|
adamc@350
|
132 (while (> level 0)
|
adamc@350
|
133 (forward-sexp 1)
|
adamc@350
|
134 (while (not (or (eobp) (urweb-looking-back-at either)))
|
adamc@350
|
135 (condition-case () (forward-sexp 1) (error (forward-char 1))))
|
adamc@350
|
136 (setq level
|
adamc@350
|
137 (cond
|
adamc@350
|
138 ((and (eobp) (> level 1)) (error "Unbalanced"))
|
adamc@350
|
139 ((urweb-looking-back-at this) (1+ level))
|
adamc@350
|
140 ((urweb-looking-back-at match) (1- level))
|
adamc@350
|
141 (t (error "Unbalanced")))))
|
adamc@350
|
142 t))
|
adamc@350
|
143
|
adamc@350
|
144 (defun urweb-find-match-backward (this match)
|
adamc@350
|
145 (let ((level 1)
|
adamc@350
|
146 (forward-sexp-function nil)
|
adamc@350
|
147 (either (concat this "\\|" match)))
|
adamc@350
|
148 (while (> level 0)
|
adamc@350
|
149 (backward-sexp 1)
|
adamc@350
|
150 (while (not (or (bobp) (looking-at either)))
|
adamc@350
|
151 (condition-case () (backward-sexp 1) (error (backward-char 1))))
|
adamc@350
|
152 (setq level
|
adamc@350
|
153 (cond
|
adamc@350
|
154 ((and (bobp) (> level 1)) (error "Unbalanced"))
|
adamc@350
|
155 ((looking-at this) (1+ level))
|
adamc@350
|
156 ((looking-at match) (1- level))
|
adamc@350
|
157 (t (error "Unbalanced")))))
|
adamc@350
|
158 t))
|
adamc@350
|
159
|
adamc@350
|
160 ;;;
|
adamc@350
|
161 ;;; read a symbol, including the special "op <sym>" case
|
adamc@350
|
162 ;;;
|
adamc@350
|
163
|
adamc@350
|
164 (defmacro urweb-move-read (&rest body)
|
adamc@350
|
165 (let ((pt-sym (make-symbol "point")))
|
adamc@350
|
166 `(let ((,pt-sym (point)))
|
adamc@350
|
167 ,@body
|
adamc@350
|
168 (when (/= (point) ,pt-sym)
|
adamc@350
|
169 (buffer-substring-no-properties (point) ,pt-sym)))))
|
adamc@350
|
170 (def-edebug-spec urweb-move-read t)
|
adamc@350
|
171
|
adamc@350
|
172 (defun urweb-poly-equal-p ()
|
adamc@350
|
173 (< (urweb-point-after (re-search-backward urweb-=-starter-re nil 'move))
|
adamc@350
|
174 (urweb-point-after (re-search-backward "=" nil 'move))))
|
adamc@350
|
175
|
adamc@350
|
176 (defun urweb-nested-of-p ()
|
adamc@350
|
177 (< (urweb-point-after
|
adamc@350
|
178 (re-search-backward urweb-non-nested-of-starter-re nil 'move))
|
adamc@350
|
179 (urweb-point-after (re-search-backward "\\<case\\>" nil 'move))))
|
adamc@350
|
180
|
adamc@350
|
181 (defun urweb-forward-sym-1 ()
|
adamc@350
|
182 (or (/= 0 (skip-syntax-forward "'w_"))
|
adamc@350
|
183 (/= 0 (skip-syntax-forward ".'"))))
|
adamc@350
|
184 (defun urweb-forward-sym ()
|
adamc@350
|
185 (let ((sym (urweb-move-read (urweb-forward-sym-1))))
|
adamc@350
|
186 (cond
|
adamc@350
|
187 ((equal "op" sym)
|
adamc@350
|
188 (urweb-forward-spaces)
|
adamc@350
|
189 (concat "op " (or (urweb-move-read (urweb-forward-sym-1)) "")))
|
adamc@350
|
190 ((equal sym "=")
|
adamc@350
|
191 (save-excursion
|
adamc@350
|
192 (urweb-backward-sym-1)
|
adamc@350
|
193 (if (urweb-poly-equal-p) "=" "d=")))
|
adamc@350
|
194 ((equal sym "of")
|
adamc@350
|
195 (save-excursion
|
adamc@350
|
196 (urweb-backward-sym-1)
|
adamc@350
|
197 (if (urweb-nested-of-p) "of" "=of")))
|
adamc@350
|
198 ;; ((equal sym "datatype")
|
adamc@350
|
199 ;; (save-excursion
|
adamc@350
|
200 ;; (urweb-backward-sym-1)
|
adamc@350
|
201 ;; (urweb-backward-spaces)
|
adamc@350
|
202 ;; (if (eq (preceding-char) ?=) "=datatype" sym)))
|
adamc@350
|
203 (t sym))))
|
adamc@350
|
204
|
adamc@350
|
205 (defun urweb-backward-sym-1 ()
|
adamc@350
|
206 (or (/= 0 (skip-syntax-backward ".'"))
|
adamc@350
|
207 (/= 0 (skip-syntax-backward "'w_"))))
|
adamc@350
|
208 (defun urweb-backward-sym ()
|
adamc@350
|
209 (let ((sym (urweb-move-read (urweb-backward-sym-1))))
|
adamc@350
|
210 (when sym
|
adamc@350
|
211 ;; FIXME: what should we do if `sym' = "op" ?
|
adamc@350
|
212 (let ((point (point)))
|
adamc@350
|
213 (urweb-backward-spaces)
|
adamc@350
|
214 (if (equal "op" (urweb-move-read (urweb-backward-sym-1)))
|
adamc@350
|
215 (concat "op " sym)
|
adamc@350
|
216 (goto-char point)
|
adamc@350
|
217 (cond
|
adamc@350
|
218 ((string= sym "=") (if (urweb-poly-equal-p) "=" "d="))
|
adamc@350
|
219 ((string= sym "of") (if (urweb-nested-of-p) "of" "=of"))
|
adamc@350
|
220 ;; ((string= sym "datatype")
|
adamc@350
|
221 ;; (save-excursion (urweb-backward-spaces)
|
adamc@350
|
222 ;; (if (eq (preceding-char) ?=) "=datatype" sym)))
|
adamc@350
|
223 (t sym)))))))
|
adamc@350
|
224
|
adamc@350
|
225
|
adamc@350
|
226 (defun urweb-backward-sexp (prec)
|
adamc@350
|
227 "Move one sexp backward if possible, or one char else.
|
adamc@350
|
228 Returns t if the move indeed moved through one sexp and nil if not.
|
adamc@350
|
229 PREC is the precedence currently looked for."
|
adamc@350
|
230 (let ((parse-sexp-lookup-properties t)
|
adamc@350
|
231 (parse-sexp-ignore-comments t))
|
adamc@350
|
232 (urweb-backward-spaces)
|
adamc@350
|
233 (let* ((op (urweb-backward-sym))
|
adamc@350
|
234 (op-prec (urweb-op-prec op 'back))
|
adamc@350
|
235 match)
|
adamc@350
|
236 (cond
|
adamc@350
|
237 ((not op)
|
adamc@350
|
238 (let ((point (point)))
|
adamc@350
|
239 (ignore-errors (let ((forward-sexp-function nil)) (backward-sexp 1)))
|
adamc@350
|
240 (if (/= point (point)) t (ignore-errors (backward-char 1)) nil)))
|
adamc@350
|
241 ;; stop as soon as precedence is smaller than `prec'
|
adamc@350
|
242 ((and prec op-prec (>= prec op-prec)) nil)
|
adamc@350
|
243 ;; special rules for nested constructs like if..then..else
|
adamc@350
|
244 ((and (or (not prec) (and prec op-prec))
|
adamc@350
|
245 (setq match (second (assoc op urweb-close-paren))))
|
adamc@350
|
246 (urweb-find-match-backward (concat "\\<" op "\\>") match))
|
adamc@350
|
247 ;; don't back over open-parens
|
adamc@350
|
248 ((assoc op urweb-open-paren) nil)
|
adamc@350
|
249 ;; infix ops precedence
|
adamc@350
|
250 ((and prec op-prec) (< prec op-prec))
|
adamc@350
|
251 ;; [ prec = nil ] a new operator, let's skip the sexps until the next
|
adamc@350
|
252 (op-prec (while (urweb-move-if (urweb-backward-sexp op-prec))) t)
|
adamc@350
|
253 ;; special symbols indicating we're getting out of a nesting level
|
adamc@350
|
254 ((string-match urweb-sexp-head-symbols-re op) nil)
|
adamc@350
|
255 ;; if the op was not alphanum, then we still have to do the backward-sexp
|
adamc@350
|
256 ;; this reproduces the usual backward-sexp, but it might be bogus
|
adamc@350
|
257 ;; in this case since !@$% is a perfectly fine symbol
|
adamc@350
|
258 (t t))))) ;(or (string-match "\\sw" op) (urweb-backward-sexp prec))
|
adamc@350
|
259
|
adamc@350
|
260 (defun urweb-forward-sexp (prec)
|
adamc@350
|
261 "Moves one sexp forward if possible, or one char else.
|
adamc@350
|
262 Returns T if the move indeed moved through one sexp and NIL if not."
|
adamc@350
|
263 (let ((parse-sexp-lookup-properties t)
|
adamc@350
|
264 (parse-sexp-ignore-comments t))
|
adamc@350
|
265 (urweb-forward-spaces)
|
adamc@350
|
266 (let* ((op (urweb-forward-sym))
|
adamc@350
|
267 (op-prec (urweb-op-prec op 'forw))
|
adamc@350
|
268 match)
|
adamc@350
|
269 (cond
|
adamc@350
|
270 ((not op)
|
adamc@350
|
271 (let ((point (point)))
|
adamc@350
|
272 (ignore-errors (let ((forward-sexp-function nil)) (forward-sexp 1)))
|
adamc@350
|
273 (if (/= point (point)) t (forward-char 1) nil)))
|
adamc@350
|
274 ;; stop as soon as precedence is smaller than `prec'
|
adamc@350
|
275 ((and prec op-prec (>= prec op-prec)) nil)
|
adamc@350
|
276 ;; special rules for nested constructs like if..then..else
|
adamc@350
|
277 ((and (or (not prec) (and prec op-prec))
|
adamc@350
|
278 (setq match (cdr (assoc op urweb-open-paren))))
|
adamc@350
|
279 (urweb-find-match-forward (first match) (second match)))
|
adamc@350
|
280 ;; don't forw over close-parens
|
adamc@350
|
281 ((assoc op urweb-close-paren) nil)
|
adamc@350
|
282 ;; infix ops precedence
|
adamc@350
|
283 ((and prec op-prec) (< prec op-prec))
|
adamc@350
|
284 ;; [ prec = nil ] a new operator, let's skip the sexps until the next
|
adamc@350
|
285 (op-prec (while (urweb-move-if (urweb-forward-sexp op-prec))) t)
|
adamc@350
|
286 ;; special symbols indicating we're getting out of a nesting level
|
adamc@350
|
287 ((string-match urweb-sexp-head-symbols-re op) nil)
|
adamc@350
|
288 ;; if the op was not alphanum, then we still have to do the backward-sexp
|
adamc@350
|
289 ;; this reproduces the usual backward-sexp, but it might be bogus
|
adamc@350
|
290 ;; in this case since !@$% is a perfectly fine symbol
|
adamc@350
|
291 (t t))))) ;(or (string-match "\\sw" op) (urweb-backward-sexp prec))
|
adamc@350
|
292
|
adamc@350
|
293 (defun urweb-in-word-p ()
|
adamc@350
|
294 (and (eq ?w (char-syntax (or (char-before) ? )))
|
adamc@350
|
295 (eq ?w (char-syntax (or (char-after) ? )))))
|
adamc@350
|
296
|
adamc@350
|
297 (defun urweb-user-backward-sexp (&optional count)
|
adamc@350
|
298 "Like `backward-sexp' but tailored to the Ur/Web syntax."
|
adamc@350
|
299 (interactive "p")
|
adamc@350
|
300 (unless count (setq count 1))
|
adamc@350
|
301 (urweb-with-ist
|
adamc@350
|
302 (let ((point (point)))
|
adamc@350
|
303 (if (< count 0) (urweb-user-forward-sexp (- count))
|
adamc@350
|
304 (when (urweb-in-word-p) (forward-word 1))
|
adamc@350
|
305 (dotimes (i count)
|
adamc@350
|
306 (unless (urweb-backward-sexp nil)
|
adamc@350
|
307 (goto-char point)
|
adamc@350
|
308 (error "Containing expression ends prematurely")))))))
|
adamc@350
|
309
|
adamc@350
|
310 (defun urweb-user-forward-sexp (&optional count)
|
adamc@350
|
311 "Like `forward-sexp' but tailored to the Ur/Web syntax."
|
adamc@350
|
312 (interactive "p")
|
adamc@350
|
313 (unless count (setq count 1))
|
adamc@350
|
314 (urweb-with-ist
|
adamc@350
|
315 (let ((point (point)))
|
adamc@350
|
316 (if (< count 0) (urweb-user-backward-sexp (- count))
|
adamc@350
|
317 (when (urweb-in-word-p) (backward-word 1))
|
adamc@350
|
318 (dotimes (i count)
|
adamc@350
|
319 (unless (urweb-forward-sexp nil)
|
adamc@350
|
320 (goto-char point)
|
adamc@350
|
321 (error "Containing expression ends prematurely")))))))
|
adamc@350
|
322
|
adamc@350
|
323 ;;(defun urweb-forward-thing ()
|
adamc@350
|
324 ;; (if (= ?w (char-syntax (char-after))) (forward-word 1) (forward-char 1)))
|
adamc@350
|
325
|
adamc@350
|
326 (defun urweb-backward-arg () (urweb-backward-sexp 1000))
|
adamc@350
|
327 (defun urweb-forward-arg () (urweb-forward-sexp 1000))
|
adamc@350
|
328
|
adamc@350
|
329
|
adamc@350
|
330 (provide 'urweb-move)
|
adamc@350
|
331
|
adamc@350
|
332 ;;; urweb-move.el ends here
|