1 ;;; sml-move.el --- Buffer navigation functions for sml-mode
3 ;; Copyright (C) 1999-2000 Stefan Monnier <monnier@cs.yale.edu>
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.
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.
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.
25 (eval-when-compile (require 'cl))
29 (defsyntax sml-internal-syntax-table
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.
37 "Syntax table used for internal sml-mode operation."
38 :copy sml-mode-syntax-table)
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))
51 (progn (set-syntax-table sml-internal-syntax-table) . ,r)
52 (set-syntax-table ,ost-sym)))))
53 (def-edebug-spec sml-with-ist t)
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))
62 (def-edebug-spec sml-move-if t)
64 (defmacro sml-point-after (&rest body)
68 (def-edebug-spec sml-point-after t)
76 ((">" ">=" "<>" "<" "<=" "=") . 4)
79 (("/" "*" "quot" "rem" "div" "mod") . 7)))
80 "Alist of SML infix operators and their precedence.")
82 (defconst sml-syntax-prec
84 `(((";" "," "in" "with") . 10)
85 (("=>" "d=" "=of") . (65 . 40))
87 (("case" "of" "fn") . 45)
88 (("if" "then" "else" "while" "do" "raise") . 50)
94 (,(cons "end" sml-begin-syms) . 10000)))
95 "Alist of pseudo-precedence of syntactic elements.")
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."
103 (let ((sprec (cdr (assoc op sml-syntax-prec))))
105 ((consp sprec) (if (eq dir 'back) (car sprec) (cdr sprec)))
108 (let ((prec (cdr (assoc op sml-op-prec))))
109 (when prec (+ prec 100))))))))
113 (defun sml-forward-spaces () (forward-comment 100000))
114 (defun sml-backward-spaces () (forward-comment -100000))
118 ;; moving forward around matching symbols
121 (defun sml-looking-back-at (re)
123 (when (= 0 (skip-syntax-backward "w_")) (backward-char))
126 (defun sml-find-match-forward (this match)
127 "Only works for word matches."
129 (either (concat this "\\|" match)))
132 (while (not (or (eobp) (sml-looking-back-at either)))
133 (condition-case () (forward-sexp 1) (error (forward-char 1))))
136 ((sml-looking-back-at this) (1+ level))
137 ((sml-looking-back-at match) (1- level))
138 (t (error "Unbalanced")))))
141 (defun sml-find-match-backward (this match)
143 (either (concat this "\\|" match)))
146 (while (not (or (bobp) (looking-at either)))
147 (condition-case () (backward-sexp 1) (error (backward-char 1))))
150 ((looking-at this) (1+ level))
151 ((looking-at match) (1- level))
152 (t (error "Unbalanced")))))
156 ;;; read a symbol, including the special "op <sym>" case
159 (defmacro sml-move-read (&rest body)
160 (let ((pt-sym (make-symbol "point")))
161 `(let ((,pt-sym (point)))
163 (when (/= (point) ,pt-sym)
164 (buffer-substring (point) ,pt-sym)))))
165 (def-edebug-spec sml-move-read t)
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))))
171 (defun sml-nested-of-p ()
173 (re-search-backward sml-non-nested-of-starter-re nil 'move))
174 (sml-point-after (re-search-backward "\\<case\\>" nil 'move))))
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))))
184 (concat "op " (or (sml-move-read (sml-forward-sym-1)) "")))
188 (if (sml-poly-equal-p) "=" "d=")))
192 (if (sml-nested-of-p) "of" "=of")))
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))))
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)))
208 ((string= sym "=") (if (sml-poly-equal-p) "=" "d="))
209 ((string= sym "of") (if (sml-nested-of-p) "of" "=of"))
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))
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))
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))
253 (let* ((point (point))
254 (op (sml-forward-sym))
255 (op-prec (sml-op-prec op 'forw))
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))
281 (defun sml-in-word-p ()
282 (and (eq ?w (char-syntax (or (char-before) ? )))
283 (eq ?w (char-syntax (or (char-after) ? )))))
285 (defun sml-user-backward-sexp (&optional count)
286 "Like `backward-sexp' but tailored to the SML syntax."
288 (unless count (setq count 1))
290 (let ((point (point)))
291 (if (< count 0) (sml-user-forward-sexp (- count))
292 (when (sml-in-word-p) (forward-word 1))
294 (unless (sml-backward-sexp nil)
296 (error "Containing expression ends prematurely")))))))
298 (defun sml-user-forward-sexp (&optional count)
299 "Like `forward-sexp' but tailored to the SML syntax."
301 (unless count (setq count 1))
303 (let ((point (point)))
304 (if (< count 0) (sml-user-backward-sexp (- count))
305 (when (sml-in-word-p) (backward-word 1))
307 (unless (sml-forward-sexp nil)
309 (error "Containing expression ends prematurely")))))))
311 ;;(defun sml-forward-thing ()
312 ;; (if (= ?w (char-syntax (char-after))) (forward-word 1) (forward-char 1)))
314 (defun sml-backward-arg () (sml-backward-sexp 1000))
315 (defun sml-forward-arg () (sml-forward-sexp 1000))
320 ;;; sml-move.el ends here