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

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