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