1 ;;; sml-move.el --- Buffer navigation functions for sml-mode
3 ;; Copyright (C) 1999, 2000, 2004, 2007, 2010 Stefan Monnier <monnier@gnu.org>
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 3 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)
86 (("=>" "d=" "=of") .
(65 .
40))
88 (("case" "of" "fn") .
45)
89 (("if" "then" "else" "while" "do" "raise") .
50)
95 (,(cons "end" sml-begin-syms
) .
10000)))
96 "Alist of pseudo-precedence of syntactic elements.")
98 (defun sml-op-prec (op dir
)
99 "Return the precedence of OP or nil if it's not an infix.
100 DIR should be set to BACK if you want to precedence w.r.t the left side
101 and to FORW for the precedence w.r.t the right side.
102 This assumes that we are `looking-at' the OP."
104 (let ((sprec (cdr (assoc op sml-syntax-prec
))))
106 ((consp sprec
) (if (eq dir
'back
) (car sprec
) (cdr sprec
)))
109 (let ((prec (cdr (assoc op sml-op-prec
))))
110 (when prec
(+ prec
100))))))))
114 (defun sml-forward-spaces () (forward-comment 100000))
115 (defun sml-backward-spaces () (forward-comment -
100000))
119 ;; moving forward around matching symbols
122 (defun sml-looking-back-at (re)
124 (when (= 0 (skip-syntax-backward "w_")) (backward-char))
127 (defun sml-find-match-forward (this match
)
128 "Only works for word matches."
130 (forward-sexp-function nil
)
131 (either (concat this
"\\|" match
)))
132 (while (and (not (eobp)) (> level
0))
134 (while (not (or (eobp) (sml-looking-back-at either
)))
135 (condition-case () (forward-sexp 1) (error (forward-char 1))))
138 ((sml-looking-back-at this
) (1+ level
))
139 ((sml-looking-back-at match
) (1- level
))
140 (t (error "Unbalanced")))))
143 (defun sml-find-match-backward (this match
)
145 (forward-sexp-function nil
)
146 (either (concat this
"\\|" match
)))
149 (while (not (or (bobp) (looking-at either
)))
150 (condition-case () (backward-sexp 1) (error (backward-char 1))))
153 ((looking-at this
) (1+ level
))
154 ((looking-at match
) (1- level
))
155 (t (error "Unbalanced")))))
159 ;;; read a symbol, including the special "op <sym>" case
162 (defmacro sml-move-read
(&rest body
)
163 (let ((pt-sym (make-symbol "point")))
164 `(let ((,pt-sym
(point)))
166 (when (/= (point) ,pt-sym
)
167 (buffer-substring-no-properties (point) ,pt-sym
)))))
168 (def-edebug-spec sml-move-read t
)
170 (defun sml-poly-equal-p ()
171 ;; Figure out which kind of "=" this is.
172 ;; The idea is to look backward for the first occurrence of a token that
173 ;; requires a definitional "=" and then see if there's such a definitional
174 ;; equal between that token and ourselves (in which case we're not
175 ;; a definitional = ourselves).
176 ;; The "search for =" is naive and will match "=>" and "<=", but it turns
177 ;; out to be OK in practice because such tokens very rarely (if ever) appear
178 ;; between the =-starter and the corresponding definitional equal.
179 ;; One known problem case is code like:
180 ;; "functor foo (structure s : S) where type t = s.t ="
181 ;; where the "type t = s.t" is mistaken for a type definition.
182 (< (sml-point-after (re-search-backward sml-
=-starter-re nil
'move
))
183 (sml-point-after (re-search-backward "=" nil
'move
))))
185 (defun sml-nested-of-p ()
187 (re-search-backward sml-non-nested-of-starter-re nil
'move
))
188 (sml-point-after (re-search-backward "\\<case\\>" nil
'move
))))
190 (defun sml-forward-sym-1 ()
191 (or (/= 0 (skip-syntax-forward "'w_"))
192 (/= 0 (skip-syntax-forward ".'"))))
193 (defun sml-forward-sym ()
194 (let ((sym (sml-move-read (sml-forward-sym-1))))
198 (concat "op " (or (sml-move-read (sml-forward-sym-1)) "")))
202 (if (sml-poly-equal-p) "=" "d=")))
206 (if (sml-nested-of-p) "of" "=of")))
207 ;; ((equal sym "datatype")
209 ;; (sml-backward-sym-1)
210 ;; (sml-backward-spaces)
211 ;; (if (eq (preceding-char) ?=) "=datatype" sym)))
214 (defun sml-backward-sym-1 ()
215 (or (/= 0 (skip-syntax-backward ".'"))
216 (/= 0 (skip-syntax-backward "'w_"))))
217 (defun sml-backward-sym ()
218 (let ((sym (sml-move-read (sml-backward-sym-1))))
220 ;; FIXME: what should we do if `sym' = "op" ?
221 (let ((point (point)))
222 (sml-backward-spaces)
223 (if (equal "op" (sml-move-read (sml-backward-sym-1)))
227 ((string= sym
"=") (if (sml-poly-equal-p) "=" "d="))
228 ((string= sym
"of") (if (sml-nested-of-p) "of" "=of"))
229 ;; ((string= sym "datatype")
230 ;; (save-excursion (sml-backward-spaces)
231 ;; (if (eq (preceding-char) ?=) "=datatype" sym)))
235 (defun sml-backward-sexp (prec)
236 "Move one sexp backward if possible, or one char else.
237 Returns t if the move indeed moved through one sexp and nil if not.
238 PREC is the precedence currently looked for."
239 (let ((parse-sexp-lookup-properties t
)
240 (parse-sexp-ignore-comments t
))
241 (sml-backward-spaces)
242 (let* ((op (sml-backward-sym))
243 (op-prec (sml-op-prec op
'back
))
247 (let ((point (point)))
248 (ignore-errors (let ((forward-sexp-function nil
)) (backward-sexp 1)))
249 (if (/= point
(point)) t
(ignore-errors (backward-char 1)) nil
)))
250 ;; stop as soon as precedence is smaller than `prec'
251 ((and prec op-prec
(>= prec op-prec
)) nil
)
252 ;; special rules for nested constructs like if..then..else
253 ((and (or (not prec
) (and prec op-prec
))
254 (setq match
(second (assoc op sml-close-paren
))))
255 (sml-find-match-backward (concat "\\<" op
"\\>") match
))
256 ;; don't back over open-parens
257 ((assoc op sml-open-paren
) nil
)
258 ;; infix ops precedence
259 ((and prec op-prec
) (< prec op-prec
))
260 ;; [ prec = nil ] a new operator, let's skip the sexps until the next
261 (op-prec (while (sml-move-if (sml-backward-sexp op-prec
))) t
)
262 ;; special symbols indicating we're getting out of a nesting level
263 ((string-match sml-sexp-head-symbols-re op
) nil
)
264 ;; if the op was not alphanum, then we still have to do the backward-sexp
265 ;; this reproduces the usual backward-sexp, but it might be bogus
266 ;; in this case since !@$% is a perfectly fine symbol
267 (t t
))))) ;(or (string-match "\\sw" op) (sml-backward-sexp prec))
269 (defun sml-forward-sexp (prec)
270 "Moves one sexp forward if possible, or one char else.
271 Returns T if the move indeed moved through one sexp and NIL if not."
272 (let ((parse-sexp-lookup-properties t
)
273 (parse-sexp-ignore-comments t
))
275 (let* ((op (sml-forward-sym))
276 (op-prec (sml-op-prec op
'forw
))
280 (let ((point (point)))
281 (ignore-errors (let ((forward-sexp-function nil
)) (forward-sexp 1)))
282 (if (/= point
(point)) t
(forward-char 1) nil
)))
283 ;; stop as soon as precedence is smaller than `prec'
284 ((and prec op-prec
(>= prec op-prec
)) nil
)
285 ;; special rules for nested constructs like if..then..else
286 ((and (or (not prec
) (and prec op-prec
))
287 (setq match
(cdr (assoc op sml-open-paren
))))
288 (sml-find-match-forward (first match
) (second match
)))
289 ;; don't forw over close-parens
290 ((assoc op sml-close-paren
) nil
)
291 ;; infix ops precedence
292 ((and prec op-prec
) (< prec op-prec
))
293 ;; [ prec = nil ] a new operator, let's skip the sexps until the next
294 (op-prec (while (sml-move-if (sml-forward-sexp op-prec
))) t
)
295 ;; special symbols indicating we're getting out of a nesting level
296 ((string-match sml-sexp-head-symbols-re op
) nil
)
297 ;; if the op was not alphanum, then we still have to do the backward-sexp
298 ;; this reproduces the usual backward-sexp, but it might be bogus
299 ;; in this case since !@$% is a perfectly fine symbol
300 (t t
))))) ;(or (string-match "\\sw" op) (sml-backward-sexp prec))
302 (defun sml-in-word-p ()
303 (and (eq ?w
(char-syntax (or (char-before) ?
)))
304 (eq ?w
(char-syntax (or (char-after) ?
)))))
306 (defun sml-user-backward-sexp (&optional count
)
307 "Like `backward-sexp' but tailored to the SML syntax."
309 (unless count
(setq count
1))
311 (let ((point (point)))
312 (if (< count
0) (sml-user-forward-sexp (- count
))
313 (when (sml-in-word-p) (forward-word 1))
315 (unless (sml-backward-sexp nil
)
317 (error "Containing expression ends prematurely")))))))
319 (defun sml-user-forward-sexp (&optional count
)
320 "Like `forward-sexp' but tailored to the SML syntax."
322 (unless count
(setq count
1))
324 (let ((point (point)))
325 (if (< count
0) (sml-user-backward-sexp (- count
))
326 (when (sml-in-word-p) (backward-word 1))
328 (unless (sml-forward-sexp nil
)
330 (error "Containing expression ends prematurely")))))))
332 ;;(defun sml-forward-thing ()
333 ;; (if (= ?w (char-syntax (char-after))) (forward-word 1) (forward-char 1)))
335 (defun sml-backward-arg () (sml-backward-sexp 1000))
336 (defun sml-forward-arg () (sml-forward-sexp 1000))
341 ;;; sml-move.el ends here