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