Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /sml/trunk/sml-mode/sml-mode.el
ViewVC logotype

Annotation of /sml/trunk/sml-mode/sml-mode.el

Parent Directory Parent Directory | Revision Log Revision Log


Revision 300 - (view) (download)

1 : monnier 32 ;;; sml-mode.el. Major mode for editing (Standard) ML. Version 3.3(beta)
2 :    
3 :     ;; Copyright (C) 1989, Lars Bo Nielsen; 1994,1997, Matthew J. Morley
4 :    
5 :     ;; $Revision$
6 :     ;; $Date$
7 :    
8 :     ;; This file is not part of GNU Emacs, but it is distributed under the
9 :     ;; same conditions.
10 :    
11 :     ;; ====================================================================
12 :    
13 :     ;; This program is free software; you can redistribute it and/or
14 :     ;; modify it under the terms of the GNU General Public License as
15 :     ;; published by the Free Software Foundation; either version 2, or (at
16 :     ;; your option) any later version.
17 :    
18 :     ;; This program is distributed in the hope that it will be useful, but
19 :     ;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 :     ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 :     ;; General Public License for more details.
22 :    
23 :     ;; You should have received a copy of the GNU General Public License
24 :     ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 :     ;; Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 :    
27 :     ;; ====================================================================
28 :    
29 :     ;;; HISTORY
30 :    
31 :     ;; Still under construction: History obscure, needs a biographer as
32 :     ;; well as a M-x doctor. Change Log on request.
33 :    
34 :     ;; Hacked by Olin Shivers for comint from Lars Bo Nielsen's sml.el.
35 :    
36 :     ;; Hacked by Matthew Morley to incorporate Fritz Knabe's hilite and
37 :     ;; font-lock patterns, some of Steven Gilmore's (reduced) easy-menus,
38 :     ;; and numerous bugs and bug-fixes.
39 :    
40 :     ;;; DESCRIPTION
41 :    
42 :     ;; See accompanying info file: sml-mode.info
43 :    
44 :     ;;; FOR YOUR .EMACS FILE
45 :    
46 :     ;; If sml-mode.el lives in some non-standard directory, you must tell
47 :     ;; emacs where to get it. This may or may not be necessary:
48 :    
49 :     ;; (setq load-path (cons (expand-file-name "~jones/lib/emacs") load-path))
50 :    
51 :     ;; Then to access the commands autoload sml-mode with that command:
52 :    
53 :     ;; (autoload 'sml-mode "sml-mode" "Major mode for editing ML programs." t)
54 :     ;;
55 :     ;; If files ending in ".sml" or ".ML" are hereafter considered to contain
56 :     ;; Standard ML source, put their buffers into sml-mode automatically:
57 :    
58 :     ;; (setq auto-mode-alist
59 :     ;; (cons '(("\\.sml$" . sml-mode)
60 :     ;; ("\\.ML$" . sml-mode)) auto-mode-alist))
61 :    
62 :     ;; Here's an example of setting things up in the sml-mode-hook:
63 :    
64 :     ;; (setq sml-mode-hook
65 :     ;; '(lambda() "ML mode hacks"
66 :     ;; (setq sml-indent-level 2 ; conserve on horiz. space
67 :     ;; indent-tabs-mode nil))) ; whatever
68 :    
69 :     ;; sml-mode-hook is run whenever a new sml-mode buffer is created.
70 :     ;; There is an sml-load-hook too, which is only run when this file is
71 :     ;; loaded. One use for this hook is to select your preferred
72 :     ;; highlighting scheme, like this:
73 :    
74 :     ;; (setq sml-load-hook
75 :     ;; '(lambda() "Highlights." (require 'sml-hilite)))
76 :    
77 :     ;; hilit19 is the magic that actually does the highlighting. My set up
78 :     ;; for hilit19 runs something like this:
79 :    
80 :     ;; (if window-system
81 :     ;; (setq hilit-background-mode t ; monochrome (alt: 'dark or 'light)
82 :     ;; hilit-inhibit-hooks nil
83 :     ;; hilit-inhibit-rebinding nil
84 :     ;; hilit-quietly t))
85 :    
86 :     ;; Alternatively, you can (require 'sml-font) which uses the font-lock
87 :     ;; package instead.
88 :    
89 :     ;; Finally, there are inferior-sml-{mode,load}-hooks -- see comments
90 :     ;; in sml-proc.el. For much more information consult the mode's *info*
91 :     ;; tree.
92 :    
93 :     ;;; VERSION STRING
94 :    
95 :     (defconst sml-mode-version-string
96 : monnier 39 "sml-mode, version 3.3")
97 : monnier 32
98 : monnier 33 (require 'cl)
99 : monnier 32 (provide 'sml-mode)
100 :    
101 :     ;;; VARIABLES CONTROLLING INDENTATION
102 :    
103 :     (defvar sml-indent-level 4
104 :     "*Indentation of blocks in ML (see also `sml-structure-indent').")
105 :    
106 :     (defvar sml-structure-indent 4 ; Not currently an option.
107 : monnier 39 "*Indentation of signature/structure/functor declarations.")
108 : monnier 32
109 :     (defvar sml-pipe-indent -2
110 : monnier 39 "*Extra (usually negative) indentation for lines beginning with `|'.")
111 : monnier 32
112 : monnier 300 (defvar sml-indent-case-arm 0
113 : monnier 39 "*Indentation of case arms.")
114 :    
115 : monnier 300 (defvar sml-indent-case-of 2
116 :     "*Indentation of an `of'¬†on its own line.")
117 :    
118 : monnier 39 (defvar sml-indent-equal -2
119 :     "*Extra (usually negative) indenting for lines beginning with `='.")
120 :    
121 : monnier 300 (defvar sml-indent-fn -3
122 :     "*Extra (usually negative) indenting for lines beginning with `fn'.")
123 :    
124 :     ;;(defvar sml-indent-paren -1
125 :     ;; "*Extra (usually negative) indenting for lines beginning with `('.")
126 :    
127 : monnier 39 (defvar sml-indent-args 4
128 :     "*Indentation of args placed on a separate line.")
129 :    
130 :     (defvar sml-indent-align-args t
131 :     "*Whether the arguments should be aligned.")
132 :    
133 : monnier 32 (defvar sml-case-indent nil
134 :     "*How to indent case-of expressions.
135 :     If t: case expr If nil: case expr of
136 :     of exp1 => ... exp1 => ...
137 :     | exp2 => ... | exp2 => ...
138 :    
139 :     The first seems to be the standard in SML/NJ, but the second
140 :     seems nicer...")
141 :    
142 :     (defvar sml-nested-if-indent nil
143 :     "*Determine how nested if-then-else will be formatted:
144 :     If t: if exp1 then exp2 If nil: if exp1 then exp2
145 :     else if exp3 then exp4 else if exp3 then exp4
146 :     else if exp5 then exp6 else if exp5 then exp6
147 : monnier 39 else exp7 else exp7")
148 : monnier 32
149 :     (defvar sml-type-of-indent t
150 :     "*How to indent `let' `struct' etc.
151 :     If t: fun foo bar = let If nil: fun foo bar = let
152 :     val p = 4 val p = 4
153 :     in in
154 :     bar + p bar + p
155 :     end end
156 :    
157 :     Will not have any effect if the starting keyword is first on the line.")
158 :    
159 :     (defvar sml-electric-semi-mode nil
160 :     "*If t, `\;' will self insert, reindent the line, and do a newline.
161 :     If nil, just insert a `\;'. (To insert while t, do: C-q \;).")
162 :    
163 :     (defvar sml-paren-lookback 1000
164 :     "*How far back (in chars) the indentation algorithm should look
165 :     for open parenthesis. High value means slow indentation algorithm. A
166 :     value of 1000 (being the equivalent of 20-30 lines) should suffice
167 :     most uses. (A value of nil, means do not look at all)")
168 :    
169 :     ;;; OTHER GENERIC MODE VARIABLES
170 :    
171 :     (defvar sml-mode-info "sml-mode"
172 :     "*Where to find Info file for sml-mode.
173 :     The default assumes the info file \"sml-mode.info\" is on Emacs' info
174 :     directory path. If it is not, either put the file on the standard path
175 :     or set the variable sml-mode-info to the exact location of this file
176 :     which is part of the sml-mode 3.2 (and later) distribution. E.g:
177 :    
178 :     (setq sml-mode-info \"/usr/me/lib/info/sml-mode\")
179 :    
180 :     in your .emacs file. You can always set it interactively with the
181 :     set-variable command.")
182 :    
183 :     (defvar sml-mode-hook nil
184 :     "*This hook is run when sml-mode is loaded, or a new sml-mode buffer created.
185 :     This is a good place to put your preferred key bindings.")
186 :    
187 :     (defvar sml-load-hook nil
188 :     "*This hook is run when sml-mode (sml-mode.el) is loaded into Emacs.")
189 :    
190 :     (defvar sml-mode-abbrev-table nil "*SML mode abbrev table (default nil)")
191 :    
192 :     (defvar sml-error-overlay t
193 :     "*Non-nil means use an overlay to highlight errorful code in the buffer.
194 :    
195 :     This gets set when `sml-mode' is invoked\; if you don't like/want SML
196 :     source errors to be highlighted in this way, do something like
197 :    
198 :     \(setq-default sml-error-overlay nil\)
199 :    
200 :     in your `sml-load-hook', say.")
201 :    
202 :     (make-variable-buffer-local 'sml-error-overlay)
203 :    
204 :     ;;; CODE FOR SML-MODE
205 :    
206 :     (defun sml-mode-info ()
207 :     "Command to access the TeXinfo documentation for sml-mode.
208 :     See doc for the variable sml-mode-info."
209 :     (interactive)
210 :     (require 'info)
211 :     (condition-case nil
212 :     (funcall 'Info-goto-node (concat "(" sml-mode-info ")"))
213 :     (error (progn
214 :     (describe-variable 'sml-mode-info)
215 :     (message "Can't find it... set this variable first!")))))
216 :    
217 :     (defun sml-indent-level (&optional indent)
218 :     "Allow the user to change the block indentation level. Numeric prefix
219 :     accepted in lieu of prompting."
220 :     (interactive "NIndentation level: ")
221 :     (setq sml-indent-level indent))
222 :    
223 :     (defun sml-pipe-indent (&optional indent)
224 :     "Allow to change pipe indentation level (usually negative). Numeric prefix
225 :     accepted in lieu of prompting."
226 :     (interactive "NPipe Indentation level: ")
227 :     (setq sml-pipe-indent indent))
228 :    
229 :     (defun sml-case-indent (&optional of)
230 :     "Toggle sml-case-indent. Prefix means set it to nil."
231 :     (interactive "P")
232 :     (setq sml-case-indent (and (not of) (not sml-case-indent)))
233 :     (if sml-case-indent (message "%s" "true") (message "%s" nil)))
234 :    
235 :     (defun sml-nested-if-indent (&optional of)
236 :     "Toggle sml-nested-if-indent. Prefix means set it to nil."
237 :     (interactive "P")
238 :     (setq sml-nested-if-indent (and (not of) (not sml-nested-if-indent)))
239 :     (if sml-nested-if-indent (message "%s" "true") (message "%s" nil)))
240 :    
241 :     (defun sml-type-of-indent (&optional of)
242 :     "Toggle sml-type-of-indent. Prefix means set it to nil."
243 :     (interactive "P")
244 :     (setq sml-type-of-indent (and (not of) (not sml-type-of-indent)))
245 :     (if sml-type-of-indent (message "%s" "true") (message "%s" nil)))
246 :    
247 :     (defun sml-electric-semi-mode (&optional of)
248 :     "Toggle sml-electric-semi-mode. Prefix means set it to nil."
249 :     (interactive "P")
250 :     (setq sml-electric-semi-mode (and (not of) (not sml-electric-semi-mode)))
251 :     (message "%s" (concat "Electric semi mode is "
252 :     (if sml-electric-semi-mode "on" "off"))))
253 :    
254 :     ;;; BINDINGS: these should be common to the source and process modes...
255 :    
256 :     (defun install-sml-keybindings (map)
257 :     ;; Text-formatting commands:
258 :     (define-key map "\C-c\C-m" 'sml-insert-form)
259 :     (define-key map "\C-c\C-i" 'sml-mode-info)
260 :     (define-key map "\M-|" 'sml-electric-pipe)
261 :     (define-key map "\;" 'sml-electric-semi)
262 :     (define-key map "\M-\t" 'sml-back-to-outer-indent)
263 :     (define-key map "\C-\M-\\" 'sml-indent-region)
264 :     (define-key map "\t" 'sml-indent-line) ; ...except this one
265 :     ;; Process commands added to sml-mode-map -- these should autoload
266 :     (define-key map "\C-c\C-l" 'sml-load-file)
267 :     (define-key map "\C-c`" 'sml-next-error))
268 :    
269 :     ;;; Autoload functions -- no-doc is another idea cribbed from AucTeX!
270 :    
271 :     (defvar sml-no-doc
272 :     "This function is part of sml-proc, and has not yet been loaded.
273 :     Full documentation will be available after autoloading the function."
274 :     "Documentation for autoloading functions.")
275 :    
276 : monnier 33 (autoload 'run-sml "sml-proc" sml-no-doc t)
277 :     (autoload 'sml-make "sml-proc" sml-no-doc t)
278 : monnier 32 (autoload 'sml-load-file "sml-proc" sml-no-doc t)
279 :    
280 :     (autoload 'switch-to-sml "sml-proc" sml-no-doc t)
281 :     (autoload 'sml-send-region "sml-proc" sml-no-doc t)
282 :     (autoload 'sml-send-buffer "sml-proc" sml-no-doc t)
283 :     (autoload 'sml-next-error "sml-proc" sml-no-doc t)
284 :    
285 :     (defvar sml-mode-map nil "The keymap used in sml-mode.")
286 :     (cond ((not sml-mode-map)
287 :     (setq sml-mode-map (make-sparse-keymap))
288 :     (install-sml-keybindings sml-mode-map)
289 : monnier 33 (define-key sml-mode-map "\C-c\C-c" 'sml-make)
290 : monnier 32 (define-key sml-mode-map "\C-c\C-s" 'switch-to-sml)
291 :     (define-key sml-mode-map "\C-c\C-r" 'sml-send-region)
292 :     (define-key sml-mode-map "\C-c\C-b" 'sml-send-buffer)))
293 :    
294 : monnier 33 ;; font-lock setup
295 :    
296 : monnier 39 (defconst sml-keywords-regexp
297 : monnier 300 (eval-when-compile
298 :     (concat
299 :     "\\<"
300 :     (regexp-opt '("abstraction" "abstype" "and" "andalso" "as" "before" "case"
301 :     "datatype" "else" "end" "eqtype" "exception" "do" "fn"
302 :     "fun" "functor" "handle" "if" "in" "include" "infix"
303 :     "infixr" "let" "local" "nonfix" "of" "op" "open" "orelse"
304 :     "overload" "raise" "rec" "sharing" "sig" "signature"
305 :     "struct" "structure" "then" "type" "val" "where" "while"
306 :     "with" "withtype") t)
307 :     "\\>"))
308 : monnier 39 "A regexp that matches any and all keywords of SML.")
309 :    
310 : monnier 300 (defconst sml-font-lock-keywords
311 :     `(;;(sml-font-comments-and-strings)
312 :     ("\\<\\(fun\\|and\\)\\s-+\\(\\(\\sw\\|\\s_\\)+\\)"
313 : monnier 33 (1 font-lock-keyword-face)
314 :     (2 font-lock-function-def-face))
315 : monnier 300 ("\\<\\(\\(data\\|abs\\|with\\|eq\\)?type\\)\\s-+\\('\\s-*\\(\\sw\\|\\s_\\)+\\s-+\\)*\\(\\(\\sw\\|\\s_\\)+\\)"
316 : monnier 33 (1 font-lock-keyword-face)
317 : monnier 300 (5 font-lock-type-def-face))
318 :     ("\\<\\(val\\)\\s-+\\(\\(\\sw\\|\\s_\\)+\\>\\s-*\\)?\\(\\(\\sw\\|\\s_\\)+\\)\\s-*="
319 : monnier 33 (1 font-lock-keyword-face)
320 :     ;;(6 font-lock-variable-def-face nil t)
321 : monnier 300 (4 font-lock-variable-def-face))
322 :     ("\\<\\(structure\\|functor\\|abstraction\\)\\s-+\\(\\(\\sw\\|\\s_\\)+\\)"
323 : monnier 33 (1 font-lock-keyword-face)
324 :     (2 font-lock-module-def-face))
325 : monnier 300 ("\\<\\(signature\\)\\s-+\\(\\(\\sw\\|\\s_\\)+\\)"
326 : monnier 33 (1 font-lock-keyword-face)
327 :     (2 font-lock-interface-def-face))
328 :    
329 : monnier 39 (,sml-keywords-regexp . font-lock-keyword-face))
330 : monnier 33 "Regexps matching standard SML keywords.")
331 :    
332 :     ;; default faces values
333 : monnier 300 (flet ((def-face (face def)
334 :     "Define a face for font-lock."
335 :     (unless (boundp face)
336 :     (set face (cond
337 :     ((facep face) face)
338 :     ((facep def) (copy-face def face))
339 :     (t def))))))
340 :     (def-face 'font-lock-function-def-face 'font-lock-function-name-face)
341 :     (def-face 'font-lock-type-def-face 'font-lock-type-face)
342 :     (def-face 'font-lock-module-def-face 'font-lock-function-name-face)
343 :     (def-face 'font-lock-interface-def-face 'font-lock-type-face)
344 :     (def-face 'font-lock-variable-def-face 'font-lock-variable-name-face))
345 : monnier 33
346 : monnier 300 ;; (setq sml-alt-syntax-table
347 :     ;; (let ((st (make-syntax-table)))
348 :     ;; (modify-syntax-entry ?l "(d" st)
349 :     ;; (modify-syntax-entry ?d ")l" st)
350 :     ;; (modify-syntax-entry ?\) ")(" st)
351 :     ;; st))
352 : monnier 33
353 : monnier 300 (defun sml-get-depth-st ()
354 :     (save-excursion
355 :     (let* ((disp (if (eq (char-before) ?\)) (progn (backward-char) -1) nil))
356 :     (foo (backward-char))
357 :     (disp (if (eq (char-before) ?\() (progn (backward-char) 0) disp))
358 :     (pt (point)))
359 :     (when disp
360 :     (let* ((depth
361 :     (save-match-data
362 :     (if (re-search-backward "\\*)\\|(\\*" nil t)
363 :     (+ (or (get-char-property (point) 'comment-depth) 0)
364 :     (case (char-after) (?\( 1) (?* 0))
365 :     disp)
366 :     0)))
367 :     (depth (if (> depth 0) depth)))
368 :     (put-text-property pt (1+ pt) 'comment-depth depth)
369 :     (when depth '(?.)))))))
370 :    
371 :     (defconst sml-font-lock-syntactic-keywords
372 :     '(;;("\\<\\(l\\)et\\>" (1 (?\( . ?d))) ;; sml-alt-syntax-table))
373 :     ;;("\\<en\\(d\\)\\>" (1 (?\) . ?l))) ;;sml-alt-syntax-table))
374 :     ("(?\\(\\*\\))?" (1 (sml-get-depth-st)))))
375 :    
376 :     (defconst sml-font-lock-defaults
377 :     '(sml-font-lock-keywords nil nil nil nil
378 :     (font-lock-syntactic-keywords . sml-font-lock-syntactic-keywords)))
379 :    
380 : monnier 33 ;; code to get comment fontification working in the face of recursive
381 :     ;; comments. It's lots more work than it should be. -- stefan
382 : monnier 300 ;; (defvar sml-font-cache '((0 . normal))
383 :     ;; "List of (POSITION . STATE) pairs for an SML buffer.
384 :     ;; The STATE is either `normal', `comment', or `string'. The POSITION is
385 :     ;; immediately after the token that caused the state change.")
386 :     ;; (make-variable-buffer-local 'sml-font-cache)
387 : monnier 33
388 : monnier 300 ;; (defun sml-font-comments-and-strings (limit)
389 :     ;; "Fontify SML comments and strings up to LIMIT.
390 :     ;; Handles nested comments and SML's escapes for breaking a string over lines.
391 :     ;; Uses sml-font-cache to maintain the fontification state over the buffer."
392 :     ;; (let ((beg (point))
393 :     ;; last class)
394 :     ;; (while (< beg limit)
395 :     ;; (while (and sml-font-cache
396 :     ;; (> (caar sml-font-cache) beg))
397 :     ;; (pop sml-font-cache))
398 :     ;; (setq last (caar sml-font-cache))
399 :     ;; (setq class (cdar sml-font-cache))
400 :     ;; (goto-char last)
401 :     ;; (cond
402 :     ;; ((eq class 'normal)
403 :     ;; (cond
404 :     ;; ((not (re-search-forward "\\((\\*\\)\\|\\(\"\\)" limit t))
405 :     ;; (goto-char limit))
406 :     ;; ((match-beginning 1)
407 :     ;; (push (cons (point) 'comment) sml-font-cache))
408 :     ;; ((match-beginning 2)
409 :     ;; (push (cons (point) 'string) sml-font-cache))))
410 :     ;; ((eq class 'comment)
411 :     ;; (cond
412 :     ;; ((let ((nest 1))
413 :     ;; (while (and (> nest 0)
414 :     ;; (re-search-forward "\\((\\*\\)\\|\\(\\*)\\)" limit t))
415 :     ;; (cond
416 :     ;; ((match-beginning 1) (incf nest))
417 :     ;; ((match-beginning 2) (decf nest))))
418 :     ;; (> nest 0))
419 :     ;; (goto-char limit))
420 :     ;; (t
421 :     ;; (push (cons (point) 'normal) sml-font-cache)))
422 :     ;; (put-text-property (- last 2) (point) 'face 'font-lock-comment-face))
423 :     ;; ((eq class 'string)
424 :     ;; (while (and (re-search-forward
425 :     ;; "\\(\"\\)\\|\\(\\\\\\s-*\\\\\\)\\|\\(\\\\\"\\)" limit t)
426 :     ;; (not (match-beginning 1))))
427 :     ;; (cond
428 :     ;; ((match-beginning 1)
429 :     ;; (push (cons (point) 'normal) sml-font-cache))
430 :     ;; (t
431 :     ;; (goto-char limit)))
432 :     ;; (put-text-property (- last 1) (point) 'face 'font-lock-string-face)))
433 :     ;; (setq beg (point)))))
434 : monnier 33
435 : monnier 32 ;;; H A C K A T T A C K ! X E M A C S V E R S U S E M A C S
436 :    
437 :     (cond ((fboundp 'make-extent)
438 :     ;; suppose this is XEmacs
439 :    
440 :     (defun sml-make-overlay ()
441 :     "Create a new text overlay (extent) for the SML buffer."
442 :     (let ((ex (make-extent 1 1)))
443 :     (set-extent-property ex 'face 'zmacs-region) ex))
444 :    
445 :     (defalias 'sml-is-overlay 'extentp)
446 :    
447 :     (defun sml-overlay-active-p ()
448 :     "Determine whether the current buffer's error overlay is visible."
449 :     (and (sml-is-overlay sml-error-overlay)
450 :     (not (zerop (extent-length sml-error-overlay)))))
451 :    
452 :     (defalias 'sml-move-overlay 'set-extent-endpoints))
453 :    
454 :     ((fboundp 'make-overlay)
455 :     ;; otherwise assume it's Emacs
456 :    
457 :     (defun sml-make-overlay ()
458 :     "Create a new text overlay (extent) for the SML buffer."
459 :     (let ((ex (make-overlay 0 0)))
460 :     (overlay-put ex 'face 'region) ex))
461 :    
462 :     (defalias 'sml-is-overlay 'overlayp)
463 :    
464 :     (defun sml-overlay-active-p ()
465 :     "Determine whether the current buffer's error overlay is visible."
466 :     (and (sml-is-overlay sml-error-overlay)
467 :     (not (equal (overlay-start sml-error-overlay)
468 :     (overlay-end sml-error-overlay)))))
469 :    
470 :     (defalias 'sml-move-overlay 'move-overlay))
471 :     (t
472 :     ;; what *is* this!?
473 :     (defalias 'sml-is-overlay 'ignore)
474 :     (defalias 'sml-overlay-active-p 'ignore)
475 :     (defalias 'sml-make-overlay 'ignore)
476 :     (defalias 'sml-move-overlay 'ignore)))
477 :    
478 :     ;;; MORE CODE FOR SML-MODE
479 :    
480 :     (defun sml-mode-version ()
481 :     "This file's version number (sml-mode)."
482 :     (interactive)
483 :     (message sml-mode-version-string))
484 :    
485 :     (defvar sml-mode-syntax-table nil "The syntax table used in sml-mode.")
486 :     (if sml-mode-syntax-table
487 :     ()
488 :     (setq sml-mode-syntax-table (make-syntax-table))
489 :     ;; Set everything to be "." (punctuation) except for [A-Za-z0-9],
490 :     ;; which will default to "w" (word-constituent).
491 :     (let ((i 0))
492 :     (while (< i ?0)
493 :     (modify-syntax-entry i "." sml-mode-syntax-table)
494 :     (setq i (1+ i)))
495 :     (setq i (1+ ?9))
496 :     (while (< i ?A)
497 :     (modify-syntax-entry i "." sml-mode-syntax-table)
498 :     (setq i (1+ i)))
499 :     (setq i (1+ ?Z))
500 :     (while (< i ?a)
501 :     (modify-syntax-entry i "." sml-mode-syntax-table)
502 :     (setq i (1+ i)))
503 :     (setq i (1+ ?z))
504 :     (while (< i 128)
505 :     (modify-syntax-entry i "." sml-mode-syntax-table)
506 :     (setq i (1+ i))))
507 :    
508 :     ;; Now we change the characters that are meaningful to us.
509 : monnier 300 (modify-syntax-entry ?. "_" sml-mode-syntax-table)
510 : monnier 33 (modify-syntax-entry ?\\ "\\" sml-mode-syntax-table)
511 : monnier 32 (modify-syntax-entry ?\( "()1" sml-mode-syntax-table)
512 :     (modify-syntax-entry ?\) ")(4" sml-mode-syntax-table)
513 :     (modify-syntax-entry ?\[ "(]" sml-mode-syntax-table)
514 :     (modify-syntax-entry ?\] ")[" sml-mode-syntax-table)
515 :     (modify-syntax-entry ?{ "(}" sml-mode-syntax-table)
516 :     (modify-syntax-entry ?} "){" sml-mode-syntax-table)
517 :     (modify-syntax-entry ?\* ". 23" sml-mode-syntax-table)
518 :     (modify-syntax-entry ?\" "\"" sml-mode-syntax-table)
519 :     (modify-syntax-entry ? " " sml-mode-syntax-table)
520 :     (modify-syntax-entry ?\t " " sml-mode-syntax-table)
521 :     (modify-syntax-entry ?\n " " sml-mode-syntax-table)
522 :     (modify-syntax-entry ?\f " " sml-mode-syntax-table)
523 : monnier 300 (modify-syntax-entry ?\' "_" sml-mode-syntax-table)
524 :     (modify-syntax-entry ?\_ "_" sml-mode-syntax-table))
525 : monnier 32
526 :     ;;;###Autoload
527 :     (defun sml-mode ()
528 :     "Major mode for editing ML code.
529 :     Tab indents for ML code.
530 :     Comments are delimited with (* ... *).
531 :     Blank lines and form-feeds separate paragraphs.
532 :     Delete converts tabs to spaces as it moves back.
533 :    
534 :     For information on running an inferior ML process, see the documentation
535 :     for inferior-sml-mode (set this up with \\[sml]).
536 :    
537 :     Customisation: Entry to this mode runs the hooks on sml-mode-hook.
538 :    
539 :     Variables controlling the indentation
540 :     =====================================
541 :    
542 :     Seek help (\\[describe-variable]) on individual variables to get current settings.
543 :    
544 :     sml-indent-level (default 4)
545 :     The indentation of a block of code.
546 :    
547 :     sml-pipe-indent (default -2)
548 :     Extra indentation of a line starting with \"|\".
549 :    
550 :     sml-case-indent (default nil)
551 :     Determine the way to indent case-of expression.
552 :    
553 :     sml-nested-if-indent (default nil)
554 :     Determine how nested if-then-else expressions are formatted.
555 :    
556 :     sml-type-of-indent (default t)
557 :     How to indent let, struct, local, etc.
558 :     Will not have any effect if the starting keyword is first on the line.
559 :    
560 :     sml-electric-semi-mode (default nil)
561 :     If t, a `\;' will reindent line, and perform a newline.
562 :    
563 :     sml-paren-lookback (default 1000)
564 :     Determines how far back (in chars) the indentation algorithm should
565 :     look to match parenthesis. A value of nil, means do not look at all.
566 :    
567 :     Mode map
568 :     ========
569 :     \\{sml-mode-map}"
570 :    
571 :     (interactive)
572 :     (kill-all-local-variables)
573 :     (sml-mode-variables)
574 :     (use-local-map sml-mode-map)
575 :     (setq major-mode 'sml-mode)
576 :     (setq mode-name "SML")
577 :     (run-hooks 'sml-mode-hook)) ; Run the hook last
578 :    
579 :     (defun sml-mode-variables ()
580 :     (set-syntax-table sml-mode-syntax-table)
581 :     (setq local-abbrev-table sml-mode-abbrev-table)
582 :     ;; A paragraph is separated by blank lines or ^L only.
583 : monnier 33
584 :     (set (make-local-variable 'paragraph-start)
585 :     (concat "^[\t ]*$\\|" page-delimiter))
586 :     (set (make-local-variable 'paragraph-separate) paragraph-start)
587 :     (set (make-local-variable 'indent-line-function) 'sml-indent-line)
588 :     (set (make-local-variable 'comment-start) "(* ")
589 :     (set (make-local-variable 'comment-end) " *)")
590 :     (set (make-local-variable 'comment-column) 40)
591 :     (set (make-local-variable 'comment-start-skip) "(\\*+[ \t]?")
592 :     (set (make-local-variable 'comment-indent-function) 'sml-comment-indent)
593 :     (set (make-local-variable 'font-lock-defaults) sml-font-lock-defaults)
594 : monnier 300 (set (make-local-variable 'parse-sexp-lookup-properties) t)
595 :     (set (make-local-variable 'parse-sexp-ignore-comments) t)
596 : monnier 32 (setq sml-error-overlay (and sml-error-overlay (sml-make-overlay))))
597 :    
598 :     ;; Adding these will fool the matching of parens -- because of a
599 :     ;; bug in Emacs (in scan_lists, i think)... it would be nice to
600 :     ;; have comments treated as white-space.
601 :     ;;(make-local-variable 'parse-sexp-ignore-comments)
602 :     ;;(setq parse-sexp-ignore-comments t)
603 :    
604 :     (defun sml-error-overlay (undo &optional beg end buffer)
605 :     "Move `sml-error-overlay' so it surrounds the text region in the
606 :     current buffer. If the buffer-local variable `sml-error-overlay' is
607 :     non-nil it should be an overlay \(or extent, in XEmacs speak\)\; this
608 :     function moves the overlay over the current region. If the optional
609 :     BUFFER argument is given, move the overlay in that buffer instead of
610 :     the current buffer.
611 :    
612 :     Called interactively, the optional prefix argument UNDO indicates that
613 :     the overlay should simply be removed: \\[universal-argument] \
614 :     \\[sml-error-overlay]."
615 :     (interactive "P")
616 :     (save-excursion
617 :     (set-buffer (or buffer (current-buffer)))
618 :     (if (sml-is-overlay sml-error-overlay)
619 :     (if undo
620 :     (sml-move-overlay sml-error-overlay 1 1)
621 :     ;; if active regions, signals mark not active if no region set
622 :     (let ((beg (or beg (region-beginning)))
623 :     (end (or end (region-end))))
624 :     (sml-move-overlay sml-error-overlay beg end))))))
625 :    
626 :     (defconst sml-pipe-matchers-reg
627 : monnier 300 (eval-when-compile
628 :     (concat
629 :     "\\<"
630 :     (regexp-opt '("case" "fn" "fun" "handle" "datatype" "abstype" "and") t)
631 :     "\\>"))
632 : monnier 32 "The keywords a `|' can follow.")
633 :    
634 :     (defun sml-electric-pipe ()
635 :     "Insert a \"|\".
636 :     Depending on the context insert the name of function, a \"=>\" etc."
637 :     (interactive)
638 :     (let ((case-fold-search nil) ; Case sensitive
639 :     (here (point))
640 :     (match (save-excursion
641 :     (sml-find-matching-starter sml-pipe-matchers-reg)
642 :     (point)))
643 :     (tmp " => ")
644 :     (case-or-handle-exp t))
645 :     (if (/= (save-excursion (beginning-of-line) (point))
646 :     (save-excursion (skip-chars-backward "\t ") (point)))
647 :     (insert "\n"))
648 :     (insert "|")
649 :     (save-excursion
650 :     (goto-char match)
651 :     (cond
652 :     ;; It was a function, insert the function name
653 :     ((looking-at "fun\\b")
654 :     (setq tmp (concat " " (buffer-substring
655 :     (progn (forward-char 3)
656 :     (skip-chars-forward "\t\n ") (point))
657 :     (progn (forward-word 1) (point))) " "))
658 :     (setq case-or-handle-exp nil))
659 :     ;; It was a datatype, insert nothing
660 :     ((looking-at "datatype\\b\\|abstype\\b")
661 :     (setq tmp " ") (setq case-or-handle-exp nil))
662 :     ;; If it is an and, then we have to see what is was
663 :     ((looking-at "and\\b")
664 :     (let (isfun)
665 :     (save-excursion
666 :     (condition-case ()
667 :     (progn
668 :     (re-search-backward "datatype\\b\\|abstype\\b\\|fun\\b")
669 :     (setq isfun (looking-at "fun\\b")))
670 :     (error (setq isfun nil))))
671 :     (if isfun
672 :     (progn
673 :     (setq tmp
674 :     (concat " " (buffer-substring
675 :     (progn (forward-char 3)
676 :     (skip-chars-forward "\t\n ") (point))
677 :     (progn (forward-word 1) (point))) " "))
678 :     (setq case-or-handle-exp nil))
679 :     (setq tmp " ") (setq case-or-handle-exp nil))))))
680 :     (insert tmp)
681 :     (sml-indent-line)
682 :     (beginning-of-line)
683 :     (skip-chars-forward "\t ")
684 :     (forward-char (1+ (length tmp)))
685 :     (if case-or-handle-exp
686 :     (forward-char -4))))
687 :    
688 :     (defun sml-electric-semi ()
689 :     "Inserts a \;.
690 :     If variable sml-electric-semi-mode is t, indent the current line, insert
691 :     a newline, and indent."
692 :     (interactive)
693 :     (insert "\;")
694 :     (if sml-electric-semi-mode
695 :     (reindent-then-newline-and-indent)))
696 :    
697 :     ;;; INDENTATION !!!
698 :    
699 :     (defun sml-mark-function ()
700 :     "Synonym for mark-paragraph -- sorry.
701 :     If anyone has a good algorithm for this..."
702 :     (interactive)
703 :     (mark-paragraph))
704 :    
705 :     (defun sml-indent-region (begin end)
706 :     "Indent region of ML code."
707 :     (interactive "r")
708 :     (message "Indenting region...")
709 :     (save-excursion
710 :     (goto-char end) (setq end (point-marker)) (goto-char begin)
711 :     (while (< (point) end)
712 :     (skip-chars-forward "\t\n ")
713 :     (sml-indent-line)
714 :     (end-of-line))
715 :     (move-marker end nil))
716 :     (message "Indenting region... done"))
717 :    
718 :     (defun sml-indent-line ()
719 :     "Indent current line of ML code."
720 :     (interactive)
721 :     (let ((indent (sml-calculate-indentation)))
722 :     (if (/= (current-indentation) indent)
723 :     (save-excursion ;; Added 890601 (point now stays)
724 :     (let ((beg (progn (beginning-of-line) (point))))
725 :     (skip-chars-forward "\t ")
726 :     (delete-region beg (point))
727 :     (indent-to indent))))
728 :     ;; If point is before indentation, move point to indentation
729 :     (if (< (current-column) (current-indentation))
730 :     (skip-chars-forward "\t "))))
731 :    
732 :     (defun sml-back-to-outer-indent ()
733 :     "Unindents to the next outer level of indentation."
734 :     (interactive)
735 :     (save-excursion
736 :     (beginning-of-line)
737 :     (skip-chars-forward "\t ")
738 :     (let ((start-column (current-column))
739 :     (indent (current-column)))
740 :     (if (> start-column 0)
741 :     (progn
742 :     (save-excursion
743 :     (while (>= indent start-column)
744 :     (if (re-search-backward "^[^\n]" nil t)
745 :     (setq indent (current-indentation))
746 :     (setq indent 0))))
747 :     (backward-delete-char-untabify (- start-column indent)))))))
748 :    
749 :     (defconst sml-indent-starters-reg
750 : monnier 300 (eval-when-compile
751 :     (concat "\\<"
752 :     (regexp-opt '("abstype" "and" "case" "datatype" "else"
753 :     "fun" "if" "sharing" "in" "infix" "infixr"
754 :     "let" "local" "nonfix" "of" "open" "raise" "sig"
755 :     "struct" "then" "btype" "val"
756 :     "while" "with" "withtype") t)
757 :     ;; removed "signature" "structure" "functor"
758 :     "\\>"))
759 : monnier 32 "The indentation starters. The next line will be indented.")
760 :    
761 :     (defconst sml-starters-reg
762 : monnier 300 (eval-when-compile
763 :     (concat "\\<"
764 :     (regexp-opt '("abstraction" "abstype" "datatype" "exception" "fun"
765 :     "functor" "local" "infix" "infixr" "sharing" "nonfix"
766 :     "open" "signature" "structure" "type" "val"
767 :     "withtype" "with") t)
768 :     "\\>"))
769 : monnier 32 "The starters of new expressions.")
770 :    
771 :     (defconst sml-end-starters-reg
772 : monnier 300 (eval-when-compile
773 :     (concat "\\<" (regexp-opt '("let" "local" "sig" "struct" "with") t) "\\>"))
774 : monnier 32 "Matching reg-expression for the \"end\" keyword.")
775 :    
776 :     (defconst sml-starters-indent-after
777 : monnier 300 (eval-when-compile
778 :     (concat "\\<" (regexp-opt '("let" "local" "struct" "in" "sig" "with") t)
779 :     "\\>"))
780 : monnier 32 "Indent after these.")
781 :    
782 : monnier 300 (defconst sml-pipehead-regexp
783 :     (eval-when-compile
784 :     (concat "\\<" (regexp-opt '("fun" "fn" "and" "handle" "case" "datatype") t)
785 :     "\\>"))
786 :     "A `|' corresponds to one of these.")
787 :    
788 :     (defconst sml-not-arg-regexp
789 :     (eval-when-compile
790 :     (concat "\\<" (regexp-opt '("in" "of" "end") t) "\\>"))
791 :     "Regexp matching lines that should never be indented as args.")
792 :    
793 :    
794 : monnier 39 (defun sml-find-comment-indent ()
795 :     (save-excursion
796 :     (let ((depth 1))
797 :     (while (> depth 0)
798 :     (if (re-search-backward "(\\*\\|\\*)" nil t)
799 :     (cond
800 :     ((looking-at "*)") (incf depth))
801 : monnier 300 ((looking-at comment-start-skip) (decf depth)))
802 : monnier 39 (setq depth -1)))
803 :     (if (= depth 0)
804 :     (current-column)
805 :     nil))))
806 :    
807 : monnier 32 (defun sml-calculate-indentation ()
808 :     (save-excursion
809 : monnier 39 (let ((case-fold-search nil)
810 : monnier 300 (indent 0))
811 :     (or
812 :     (and (beginning-of-line) nil)
813 :     (and (bobp) 0)
814 :     (and (skip-chars-forward "\t ") nil)
815 :    
816 :     ;; Indentation for comments alone on a line, matches the
817 :     ;; proper indentation of the next line.
818 :     (and (looking-at comment-start-skip) (sml-skip-spaces) nil)
819 :    
820 :     ;; continued comment
821 :     (and (looking-at "\\*") (setq indent (sml-find-comment-indent))
822 :     (1+ indent))
823 :    
824 :     ;; Continued string ? (Added 890113 lbn)
825 :     (and (looking-at "\\\\")
826 :     (save-excursion
827 :     (if (save-excursion (previous-line 1)
828 :     (beginning-of-line)
829 :     (looking-at "[\t ]*\\\\"))
830 :     (progn (previous-line 1) (current-indentation))
831 :     (if (re-search-backward "[^\\\\]\"" nil t)
832 :     (1+ (current-indentation))
833 :     0))))
834 :    
835 :     (and (looking-at "and\\>")
836 :     (if (sml-find-matching-starter sml-starters-reg)
837 : monnier 39 (current-column)
838 : monnier 300 0))
839 : monnier 32
840 : monnier 300 (and (looking-at "in\\>") ; Match the beginning let/local
841 :     (sml-find-match-indent "in" "\\<in\\>" "\\<l\\(ocal\\|et\\)\\>"))
842 :    
843 :     (and (looking-at "end\\>") ; Match the beginning
844 :     (sml-find-match-indent "end" "\\<end\\>" sml-end-starters-reg))
845 :    
846 :     (and (looking-at "else\\>") ; Match the if
847 : monnier 39 (progn
848 : monnier 300 (sml-find-match-backward "else" "\\<else\\>" "\\<if\\>")
849 :     (let ((indent (current-column)))
850 :     (if (and sml-nested-if-indent
851 :     (progn (sml-backward-sexp)
852 :     (looking-at "else[ \t]+if\\b")))
853 :     (current-column)
854 :     indent))))
855 : monnier 39
856 : monnier 300 (and (looking-at "then\\>") ; Match the if + extra indentation
857 :     (sml-find-match-indent "then" "\\<then\\>" "\\<if\\>" t))
858 :    
859 :     (and (looking-at "of\\>")
860 :     (progn
861 :     (sml-re-search-backward "\\<case\\>")
862 :     (+ (current-column) sml-indent-case-of)))
863 :    
864 :     (and (looking-at sml-starters-reg)
865 :     (let ((start (point)))
866 :     (if (not (sml-backward-sexp))
867 :     (if (and (looking-at sml-starters-indent-after)
868 :     (/= start (point)))
869 :     (+ (if sml-type-of-indent
870 :     (current-column)
871 :     (if (progn (beginning-of-line)
872 :     (skip-chars-forward "\t ")
873 :     (looking-at "|"))
874 :     (- (current-indentation) sml-pipe-indent)
875 :     (current-indentation)))
876 :     sml-indent-level)
877 :     (beginning-of-line)
878 :     (skip-chars-forward "\t ")
879 :     (if (and (looking-at sml-starters-indent-after)
880 :     (/= start (point)))
881 :     (+ (if sml-type-of-indent
882 :     (current-column)
883 :     (current-indentation))
884 :     sml-indent-level)))
885 :     (goto-char start)
886 :     (if (sml-find-matching-starter sml-starters-reg)
887 :     (current-column)
888 :     0))))
889 :    
890 :     (and (looking-at "|")
891 :     (when (sml-find-matching-starter sml-pipehead-regexp)
892 :     (cond
893 :     ((looking-at "datatype")
894 :     (re-search-forward "=[ \n\t]*") nil t)
895 :     ((looking-at "case\\>")
896 :     (forward-word 1) ;skip `case'
897 :     (sml-forward-sexps "of\\>") ;skip the argument
898 :     (sml-forward-word) ;skif the `of'
899 :     (sml-skip-spaces))
900 :     (t
901 :     (forward-word 1)
902 :     (sml-skip-spaces)))
903 :     (+ sml-pipe-indent (current-column))))
904 :    
905 :     (and (setq indent (sml-get-indent)) nil)
906 :    
907 :     (and (looking-at "=[^>]") (+ indent sml-indent-equal))
908 :     (and (looking-at "fn\\>") (+ indent sml-indent-fn))
909 :     ;; (and (looking-at "(") (+ indent sml-indent-paren))
910 :    
911 :     (and sml-paren-lookback ; Look for open parenthesis ?
912 :     (max indent (sml-get-paren-indent)))
913 :     indent))))
914 :    
915 :     (defun sml-goto-first-subexp ()
916 :     (let ((initpoint (point)))
917 :     (skip-chars-forward " \t")
918 :     (let ((argp (and (looking-at "[\\-\\[({a-zA-Z0-9_'#~+*]\\|$")
919 :     (not (looking-at (concat "[ \t]*" sml-not-arg-regexp))))))
920 :     (while (and argp (not (bobp)))
921 :     (let* ((endpoint (point))
922 :     (startpoint endpoint))
923 :     (setq argp
924 :     (condition-case ()
925 :     (progn (backward-sexp 1)
926 :     (setq startpoint (point))
927 :     (and (not (looking-at sml-keywords-regexp))
928 :     (progn (forward-sexp 1)
929 :     (sml-skip-spaces
930 :     (concat comment-start-skip "\\|[-~+*]"))
931 :     (>= (point) endpoint))))
932 :     (error nil)))
933 :     (goto-char (if argp startpoint endpoint))))
934 :     (let ((res (point)))
935 :     (skip-syntax-backward " ") (skip-syntax-backward "^ ")
936 :     (if (looking-at "*\\|:[^=]\\|->\\|of\\>")
937 :     (goto-char initpoint)
938 :     (goto-char res)
939 :     (sml-skip-spaces))))))
940 :    
941 : monnier 32 (defun sml-get-indent ()
942 :     (save-excursion
943 : monnier 39 (let ((case-fold-search nil)
944 :     (endpoint (point))
945 :     rover)
946 : monnier 32 (beginning-of-line)
947 :    
948 : monnier 39 ;; let's try to see whether we are inside an expression
949 :     (sml-goto-first-subexp)
950 :     (setq rover (current-column))
951 : monnier 300 (sml-skip-spaces)
952 :     (if (< (point) endpoint)
953 : monnier 39 (progn ; we're not the first subexp
954 : monnier 300 (sml-forward-sexp)
955 : monnier 39 (if (and sml-indent-align-args
956 :     (< (point) endpoint)
957 :     (re-search-forward "[^ \n\t]" endpoint t))
958 :     ;; we're not the second subexp
959 :     (- (current-column) 1)
960 :     (+ rover sml-indent-args)))
961 :    
962 :     (goto-char endpoint)
963 :     ;; we're not inside an expr
964 : monnier 300 (skip-syntax-backward " ") (skip-chars-backward ";")
965 : monnier 39 (if (looking-at ";") (sml-backward-sexp))
966 :     (cond
967 : monnier 300 ((save-excursion (sml-backward-sexp) (looking-at "end\\>"))
968 : monnier 39 (- (current-indentation) sml-indent-level))
969 :     (t
970 : monnier 300 (while (/= (point)
971 :     (save-excursion
972 :     (beginning-of-line)
973 :     (skip-chars-forward " \t|")
974 :     (point)))
975 : monnier 39 (sml-backward-sexp))
976 :     (when (looking-at "of") (forward-char 2))
977 :     (skip-chars-forward "\t |")
978 :     (let ((indent (current-column)))
979 :     (skip-chars-forward "\t (")
980 :     (cond
981 :     ;; a "let fun" or "let val"
982 :     ((looking-at "let \\(fun\\|val\\)\\>")
983 :     (+ (current-column) 4 sml-indent-level))
984 :     ;; Started val/fun/structure...
985 :     ((looking-at sml-indent-starters-reg)
986 :     (+ (current-column) sml-indent-level))
987 :     ;; Indent after "=>" pattern, but only if its not an fn _ =>
988 :     ;; (890726)
989 :     ((looking-at ".*=>")
990 : monnier 300 (if (looking-at ".*\\<fn\\>.*=>")
991 : monnier 39 indent
992 : monnier 300 (+ indent sml-indent-case-arm)))
993 : monnier 39 ;; else keep the same indentation as previous line
994 :     (t indent)))))))))
995 :    
996 : monnier 32 (defun sml-get-paren-indent ()
997 :     (save-excursion
998 :     (let ((levelpar 0) ; Level of "()"
999 :     (levelcurl 0) ; Level of "{}"
1000 :     (levelsqr 0) ; Level of "[]"
1001 :     (backpoint (max (- (point) sml-paren-lookback) (point-min))))
1002 :     (catch 'loop
1003 :     (while (and (/= levelpar 1) (/= levelsqr 1) (/= levelcurl 1))
1004 :     (if (re-search-backward "[][{}()]" backpoint t)
1005 :     (if (not (sml-inside-comment-or-string-p))
1006 :     (cond
1007 :     ((looking-at "(") (setq levelpar (1+ levelpar)))
1008 :     ((looking-at ")") (setq levelpar (1- levelpar)))
1009 :     ((looking-at "\\[") (setq levelsqr (1+ levelsqr)))
1010 :     ((looking-at "\\]") (setq levelsqr (1- levelsqr)))
1011 :     ((looking-at "{") (setq levelcurl (1+ levelcurl)))
1012 :     ((looking-at "}") (setq levelcurl (1- levelcurl)))))
1013 :     (throw 'loop 0))) ; Exit with value 0
1014 :     (if (save-excursion
1015 :     (forward-char 1)
1016 :     (looking-at sml-indent-starters-reg))
1017 :     (1+ (+ (current-column) sml-indent-level))
1018 :     (1+ (current-column)))))))
1019 :    
1020 :     (defun sml-inside-comment-or-string-p ()
1021 :     (let ((start (point)))
1022 :     (if (save-excursion
1023 :     (condition-case ()
1024 :     (progn
1025 :     (search-backward "(*")
1026 :     (search-forward "*)")
1027 :     (forward-char -1) ; A "*)" is not inside the comment
1028 :     (> (point) start))
1029 :     (error nil)))
1030 :     t
1031 :     (let ((numb 0))
1032 :     (save-excursion
1033 :     (save-restriction
1034 :     (narrow-to-region (progn (beginning-of-line) (point)) start)
1035 :     (condition-case ()
1036 :     (while t
1037 :     (search-forward "\"")
1038 :     (setq numb (1+ numb)))
1039 :     (error (if (and (not (zerop numb))
1040 :     (not (zerop (% numb 2))))
1041 :     t nil)))))))))
1042 :    
1043 : monnier 300 (defun sml-find-match-backward (unquoted-this this match)
1044 :     (let ((case-fold-search nil)
1045 :     (level 1)
1046 :     (pattern (concat this "\\|" match)))
1047 :     (while (not (zerop level))
1048 :     (if (sml-re-search-backward pattern)
1049 :     (setq level (cond
1050 :     ((looking-at this) (1+ level))
1051 :     ((looking-at match) (1- level))))
1052 :     ;; The right match couldn't be found
1053 :     (error (concat "Unbalanced: " unquoted-this))))))
1054 : monnier 32
1055 :     (defun sml-find-match-indent (unquoted-this this match &optional indented)
1056 :     (save-excursion
1057 : monnier 300 (sml-find-match-backward unquoted-this this match)
1058 : monnier 32 (if (or sml-type-of-indent indented)
1059 :     (current-column)
1060 :     (if (progn
1061 :     (beginning-of-line)
1062 :     (skip-chars-forward "\t ")
1063 :     (looking-at "|"))
1064 :     (- (current-indentation) sml-pipe-indent)
1065 :     (current-indentation)))))
1066 :    
1067 :     (defun sml-find-matching-starter (regexp)
1068 : monnier 300 (sml-backward-sexp)
1069 :     (while (not (or (looking-at regexp) (bobp)))
1070 :     (sml-backward-sexp))
1071 :     (not (bobp)))
1072 : monnier 32
1073 :     (defun sml-re-search-backward (regexpr)
1074 :     (let ((case-fold-search nil) (found t))
1075 :     (if (re-search-backward regexpr nil t)
1076 :     (progn
1077 :     (condition-case ()
1078 :     (while (sml-inside-comment-or-string-p)
1079 :     (re-search-backward regexpr))
1080 :     (error (setq found nil)))
1081 :     found)
1082 :     nil)))
1083 :    
1084 :     (defun sml-up-list ()
1085 :     (save-excursion
1086 :     (condition-case ()
1087 :     (progn
1088 :     (up-list 1)
1089 :     (point))
1090 :     (error 0))))
1091 :    
1092 : monnier 300
1093 :     (defun sml-forward-word ()
1094 :     (sml-skip-spaces)
1095 :     (forward-word 1))
1096 :    
1097 :     ;; should skip comments, deal with "let", "local" and such expressions
1098 :     (defun sml-forward-sexp ()
1099 :     (condition-case ()
1100 :     (forward-sexp 1)
1101 :     (error (forward-char 1))))
1102 :    
1103 :     ;; the terminators should be chosen more carefully:
1104 :     ;; `let' isn't one while `=' may be
1105 :     (defun sml-forward-sexps (&optional end)
1106 :     (sml-skip-spaces)
1107 :     (while (not (looking-at (or end (concat sml-keywords-regexp "\\|[])}|:;]"))))
1108 :     (sml-forward-sexp)
1109 :     (sml-skip-spaces)))
1110 :    
1111 :     (defun sml-skip-spaces (&optional reg)
1112 :     (let ((parse-sexp-ignore-comments nil))
1113 :     (skip-syntax-forward " ")
1114 :     (while (looking-at (or reg comment-start-skip))
1115 :     (forward-sexp 1)
1116 :     (skip-syntax-forward " "))))
1117 :    
1118 :     ;; maybe we should do sml-backward-sexps and use it if we try to
1119 :     ;; backward-sexp over a semi-colon ??
1120 :     ;; return nil if it had to "move out"
1121 : monnier 32 (defun sml-backward-sexp ()
1122 :     (condition-case ()
1123 :     (progn
1124 : monnier 300 (backward-sexp 1)
1125 :     (while (and (looking-at comment-start-skip) (not (bobp)))
1126 :     (backward-sexp 1))
1127 :     (if (looking-at "end\\>")
1128 :     (progn
1129 :     (sml-find-match-backward "end" "\\<end\\>" sml-end-starters-reg)
1130 :     t)
1131 :     (not (looking-at sml-end-starters-reg))))
1132 :     (error (forward-char -1) nil)))
1133 : monnier 32
1134 :     (defun sml-comment-indent ()
1135 :     (if (looking-at "^(\\*") ; Existing comment at beginning
1136 :     0 ; of line stays there.
1137 :     (save-excursion
1138 :     (skip-chars-backward " \t")
1139 :     (max (1+ (current-column)) ; Else indent at comment column
1140 :     comment-column)))) ; except leave at least one space.
1141 :    
1142 :     ;;; INSERTING PROFORMAS (COMMON SML-FORMS)
1143 :    
1144 :     (defvar sml-forms-alist
1145 :     '(("let") ("local") ("case") ("abstype") ("datatype")
1146 :     ("signature") ("structure") ("functor"))
1147 :     "*The list of templates to auto-insert.
1148 :    
1149 :     You can extend this alist to your heart's content. For each additional
1150 :     template NAME in the list, declare a keyboard macro or function (or
1151 :     interactive command) called 'sml-form-NAME'.
1152 :    
1153 :     If 'sml-form-NAME' is a function it takes no arguments and should
1154 :     insert the template at point\; if this is a command it may accept any
1155 :     sensible interactive call arguments\; keyboard macros can't take
1156 :     arguments at all. Apropos keyboard macros, see `name-last-kbd-macro'
1157 :     and `sml-addto-forms-alist'.
1158 :    
1159 :     `sml-forms-alist' understands let, local, case, abstype, datatype,
1160 :     signature, structure, and functor by default.")
1161 :    
1162 :     ;; See also macros.el in emacs lisp dir.
1163 :    
1164 :     (defun sml-addto-forms-alist (name)
1165 :     "Assign a name to the last keyboard macro defined.
1166 :     Argument NAME is transmogrified to sml-form-NAME which is the symbol
1167 :     actually defined.
1168 :    
1169 :     The symbol's function definition becomes the keyboard macro string.
1170 :    
1171 :     If that works, NAME is added to `sml-forms-alist' so you'll be able to
1172 :     reinvoke the macro through \\[sml-insert-form]. You might want to save
1173 :     the macro to use in a later editing session -- see `insert-kbd-macro'
1174 :     and add these macros to your .emacs file.
1175 :    
1176 :     See also `edit-kbd-macro' which is bound to \\[edit-kbd-macro]."
1177 :     (interactive "sName for last kbd macro (\"sml-form-\" will be added): ")
1178 :     (if (string-equal name "")
1179 :     (error "No command name given")
1180 :     (name-last-kbd-macro (intern (concat "sml-form-" name)))
1181 :     (message (concat "Macro bound to sml-form-" name))
1182 :     (or (assoc name sml-forms-alist)
1183 :     (setq sml-forms-alist (cons (list name) sml-forms-alist)))))
1184 :    
1185 :     ;; at a pinch these could be added to SML/Forms menu through the good
1186 :     ;; offices of activate-menubar-hook or something... but documentation
1187 :     ;; of this and/or menu-bar-update-hook is sparse in 19.33. anyway, use
1188 :     ;; completing read for sml-insert-form prompt...
1189 :    
1190 :     (defvar sml-last-form "let"
1191 :     "The most recent sml form inserted.")
1192 :    
1193 :     (defun sml-insert-form (arg)
1194 :     "Interactive short-cut to insert a common ML form.
1195 :     If a perfix argument is given insert a newline and indent first, or
1196 :     just move to the proper indentation if the line is blank\; otherwise
1197 :     insert at point (which forces indentation to current column).
1198 :    
1199 :     The default form to insert is 'whatever you inserted last time'
1200 :     \(just hit return when prompted\)\; otherwise the command reads with
1201 :     completion from `sml-forms-alist'."
1202 :     (interactive "P")
1203 :     (let ((name (completing-read
1204 :     (format "Form to insert: (default %s) " sml-last-form)
1205 :     sml-forms-alist nil t nil)))
1206 :     ;; default is whatever the last insert was...
1207 :     (if (string= name "") (setq name sml-last-form))
1208 :     (setq sml-last-form name)
1209 :     (if arg
1210 :     (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
1211 :     (sml-indent-line)
1212 :     (newline-and-indent)))
1213 :     (cond ((string= name "let") (sml-form-let))
1214 :     ((string= name "local") (sml-form-local))
1215 :     ((string= name "case") (sml-form-case))
1216 :     ((string= name "abstype") (sml-form-abstype))
1217 :     ((string= name "datatype") (sml-form-datatype))
1218 :     ((string= name "functor") (sml-form-functor))
1219 :     ((string= name "structure") (sml-form-structure))
1220 :     ((string= name "signature") (sml-form-signature))
1221 :     (t
1222 :     (let ((template (intern (concat "sml-form-" name))))
1223 :     (if (fboundp template)
1224 :     (if (commandp template)
1225 :     ;; it may be a named kbd macro too
1226 :     (command-execute template)
1227 :     (funcall template))
1228 :     (error
1229 :     (format "Undefined format function: %s" template))))))))
1230 :    
1231 :     (defun sml-form-let ()
1232 :     "Insert a `let in end' template."
1233 :     (interactive)
1234 :     (sml-let-local "let"))
1235 :    
1236 :     (defun sml-form-local ()
1237 :     "Insert a `local in end' template."
1238 :     (interactive)
1239 :     (sml-let-local "local"))
1240 :    
1241 :     (defun sml-let-local (starter)
1242 :     "Insert a let or local template, depending on STARTER string."
1243 :     (let ((indent (current-column)))
1244 :     (insert starter)
1245 :     (insert "\n") (indent-to (+ sml-indent-level indent))
1246 :     (save-excursion ; so point returns here
1247 :     (insert "\n")
1248 :     (indent-to indent)
1249 :     (insert "in\n")
1250 :     (indent-to (+ sml-indent-level indent))
1251 :     (insert "\n")
1252 :     (indent-to indent)
1253 :     (insert "end"))))
1254 :    
1255 :     (defun sml-form-case ()
1256 :     "Insert a case expression template, prompting for the case-expresion."
1257 :     (interactive)
1258 :     (let ((expr (read-string "Case expr: "))
1259 :     (indent (current-column)))
1260 :     (insert (concat "case " expr))
1261 :     (if sml-case-indent
1262 :     (progn
1263 :     (insert "\n")
1264 :     (indent-to (+ 2 indent))
1265 :     (insert "of "))
1266 :     (insert " of\n")
1267 :     (indent-to (+ indent sml-indent-level)))
1268 :     (save-excursion (insert " => "))))
1269 :    
1270 :     (defun sml-form-signature ()
1271 :     "Insert a generative signature binding, prompting for the name."
1272 :     (interactive)
1273 :     (let ((indent (current-column))
1274 :     (name (read-string "Signature name: ")))
1275 :     (insert (concat "signature " name " ="))
1276 :     (insert "\n")
1277 :     (indent-to (+ sml-structure-indent indent))
1278 :     (insert "sig\n")
1279 :     (indent-to (+ sml-structure-indent sml-indent-level indent))
1280 :     (save-excursion
1281 :     (insert "\n")
1282 :     (indent-to (+ sml-structure-indent indent))
1283 :     (insert "end"))))
1284 :    
1285 :     (defun sml-form-structure ()
1286 :     "Insert a generative structure binding, prompting for the name.
1287 :     The command also prompts for any signature constraint -- you should
1288 :     specify \":\" or \":>\" and the constraining signature."
1289 :     (interactive)
1290 :     (let ((indent (current-column))
1291 :     (name (read-string (concat "Structure name: ")))
1292 :     (signame (read-string "Signature constraint (default none): ")))
1293 :     (insert (concat "structure " name " "))
1294 :     (insert (if (string= "" signame) "=" (concat signame " =")))
1295 :     (insert "\n")
1296 :     (indent-to (+ sml-structure-indent indent))
1297 :     (insert "struct\n")
1298 :     (indent-to (+ sml-structure-indent sml-indent-level indent))
1299 :     (save-excursion
1300 :     (insert "\n")
1301 :     (indent-to (+ sml-structure-indent indent))
1302 :     (insert "end"))))
1303 :    
1304 :     (defun sml-form-functor ()
1305 :     "Insert a genarative functor binding, prompting for the name.
1306 :     The command also prompts for the required signature constraint -- you
1307 :     should specify \":\" or \":>\" and the constraining signature."
1308 :     (interactive)
1309 :     (let ((indent(current-indentation))
1310 :     (name (read-string "Name of functor: "))
1311 :     (signame (read-string "Signature constraint: " ":" )))
1312 :     (insert (concat "functor " name " () " signame " ="))
1313 :     (insert "\n")
1314 :     (indent-to (+ sml-structure-indent indent))
1315 :     (insert "struct\n")
1316 :     (indent-to (+ sml-structure-indent sml-indent-level indent))
1317 :     (save-excursion ; return to () instead?
1318 :     (insert "\n")
1319 :     (indent-to (+ sml-structure-indent indent))
1320 :     (insert "end"))))
1321 :    
1322 :     (defun sml-form-datatype ()
1323 :     "Insert a datatype declaration, prompting for name and type parameter."
1324 :     (interactive)
1325 :     (let ((indent (current-indentation))
1326 :     (type (read-string "Datatype type parameter (default none): "))
1327 :     (name (read-string (concat "Name of datatype: "))))
1328 :     (insert (concat "datatype "
1329 :     (if (string= type "") "" (concat type " "))
1330 :     name " ="))
1331 :     (insert "\n")
1332 :     (indent-to (+ sml-indent-level indent))))
1333 :    
1334 :     (defun sml-form-abstype ()
1335 :     "Insert an abstype declaration, prompting for name and type parameter."
1336 :     (interactive)
1337 :     (let ((indent(current-indentation))
1338 :     (type (read-string "Abstype type parameter (default none): "))
1339 :     (name (read-string "Name of abstype: ")))
1340 :     (insert (concat "abstype "
1341 :     (if (string= type "") "" (concat type " "))
1342 :     name " ="))
1343 :     (insert "\n")
1344 :     (indent-to (+ sml-indent-level indent))
1345 :     (save-excursion
1346 :     (insert "\n")
1347 :     (indent-to indent)
1348 :     (insert "with\n")
1349 :     (indent-to (+ sml-indent-level indent))
1350 :     (insert "\n")
1351 :     (indent-to indent)
1352 :     (insert "end"))))
1353 :    
1354 :     ;;; Load the menus, if they can be found on the load-path
1355 :    
1356 :     (condition-case nil
1357 :     (require 'sml-menus)
1358 :     (error (message "Sorry, not able to load SML mode menus.")))
1359 :    
1360 :     ;;; & do the user's customisation
1361 :    
1362 :     (add-hook 'sml-load-hook 'sml-mode-version t)
1363 :    
1364 :     (run-hooks 'sml-load-hook)
1365 :    
1366 :     ;;; sml-mode.el has just finished.

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0