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 333 - (view) (download)

1 : monnier 32 ;;; sml-mode.el. Major mode for editing (Standard) ML. Version 3.3(beta)
2 :    
3 : monnier 319 (defconst rcsid-sml-mode "@(#)$Name$:$Id$")
4 : monnier 32
5 : monnier 319 ;; Copyright (C) 1989-1999, Lars Bo Nielsen; 1994,1997, Matthew J. Morley
6 :    
7 : monnier 32 ;; $Revision$
8 :     ;; $Date$
9 :    
10 :     ;; This file is not part of GNU Emacs, but it is distributed under the
11 :     ;; same conditions.
12 :    
13 :     ;; ====================================================================
14 :    
15 :     ;; This program is free software; you can redistribute it and/or
16 :     ;; modify it under the terms of the GNU General Public License as
17 :     ;; published by the Free Software Foundation; either version 2, or (at
18 :     ;; your option) any later version.
19 :    
20 :     ;; This program is distributed in the hope that it will be useful, but
21 :     ;; WITHOUT ANY WARRANTY; without even the implied warranty of
22 :     ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 :     ;; General Public License for more details.
24 :    
25 :     ;; You should have received a copy of the GNU General Public License
26 :     ;; along with GNU Emacs; see the file COPYING. If not, write to the
27 :     ;; Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
28 :    
29 :     ;; ====================================================================
30 :    
31 :     ;;; HISTORY
32 :    
33 :     ;; Still under construction: History obscure, needs a biographer as
34 :     ;; well as a M-x doctor. Change Log on request.
35 :    
36 :     ;; Hacked by Olin Shivers for comint from Lars Bo Nielsen's sml.el.
37 :    
38 :     ;; Hacked by Matthew Morley to incorporate Fritz Knabe's hilite and
39 :     ;; font-lock patterns, some of Steven Gilmore's (reduced) easy-menus,
40 :     ;; and numerous bugs and bug-fixes.
41 :    
42 : monnier 319 ;; Author: Lars Bo Nielsen
43 :     ;; Olin Shivers
44 :     ;; Fritz Knabe (?)
45 :     ;; Steven Gilmore (?)
46 :     ;; Matthew Morley <mjm@scs.leeds.ac.uk> (aka <matthew@verisity.com>)
47 :     ;; Matthias Blume <blume@cs.princeton.edu> (aka <blume@kurims.kyoto-u.ac.jp>)
48 :     ;; (Stefan Monnier) monnier@cs.yale.edu
49 :     ;; Maintainer: (Stefan Monnier) monnier+lists/emacs/sml@tequila.cs.yale.edu
50 :     ;; Keywords: SML
51 :    
52 : monnier 32 ;;; DESCRIPTION
53 :    
54 :     ;; See accompanying info file: sml-mode.info
55 :    
56 :     ;;; FOR YOUR .EMACS FILE
57 :    
58 :     ;; If sml-mode.el lives in some non-standard directory, you must tell
59 :     ;; emacs where to get it. This may or may not be necessary:
60 :    
61 :     ;; (setq load-path (cons (expand-file-name "~jones/lib/emacs") load-path))
62 :    
63 :     ;; Then to access the commands autoload sml-mode with that command:
64 :    
65 :     ;; (autoload 'sml-mode "sml-mode" "Major mode for editing ML programs." t)
66 :     ;;
67 :     ;; If files ending in ".sml" or ".ML" are hereafter considered to contain
68 :     ;; Standard ML source, put their buffers into sml-mode automatically:
69 :    
70 :     ;; (setq auto-mode-alist
71 :     ;; (cons '(("\\.sml$" . sml-mode)
72 :     ;; ("\\.ML$" . sml-mode)) auto-mode-alist))
73 :    
74 :     ;; Here's an example of setting things up in the sml-mode-hook:
75 :    
76 :     ;; (setq sml-mode-hook
77 :     ;; '(lambda() "ML mode hacks"
78 :     ;; (setq sml-indent-level 2 ; conserve on horiz. space
79 :     ;; indent-tabs-mode nil))) ; whatever
80 :    
81 :     ;; sml-mode-hook is run whenever a new sml-mode buffer is created.
82 :     ;; There is an sml-load-hook too, which is only run when this file is
83 :     ;; loaded. One use for this hook is to select your preferred
84 :     ;; highlighting scheme, like this:
85 :    
86 :     ;; (setq sml-load-hook
87 :     ;; '(lambda() "Highlights." (require 'sml-hilite)))
88 :    
89 :     ;; hilit19 is the magic that actually does the highlighting. My set up
90 :     ;; for hilit19 runs something like this:
91 :    
92 :     ;; (if window-system
93 :     ;; (setq hilit-background-mode t ; monochrome (alt: 'dark or 'light)
94 :     ;; hilit-inhibit-hooks nil
95 :     ;; hilit-inhibit-rebinding nil
96 :     ;; hilit-quietly t))
97 :    
98 :     ;; Alternatively, you can (require 'sml-font) which uses the font-lock
99 :     ;; package instead.
100 :    
101 :     ;; Finally, there are inferior-sml-{mode,load}-hooks -- see comments
102 :     ;; in sml-proc.el. For much more information consult the mode's *info*
103 :     ;; tree.
104 :    
105 :     ;;; VERSION STRING
106 :    
107 : monnier 332 (defconst sml-mode-version-string "sml-mode, version 3.9.1")
108 : monnier 32
109 : monnier 33 (require 'cl)
110 : monnier 319 (require 'sml-util)
111 :     (require 'sml-move)
112 :     (require 'sml-defs)
113 : monnier 32
114 :     ;;; VARIABLES CONTROLLING INDENTATION
115 :    
116 :     (defvar sml-indent-level 4
117 :     "*Indentation of blocks in ML (see also `sml-structure-indent').")
118 :    
119 :     (defvar sml-structure-indent 4 ; Not currently an option.
120 : monnier 39 "*Indentation of signature/structure/functor declarations.")
121 : monnier 32
122 :     (defvar sml-pipe-indent -2
123 : monnier 39 "*Extra (usually negative) indentation for lines beginning with `|'.")
124 : monnier 32
125 : monnier 300 (defvar sml-indent-case-of 2
126 :     "*Indentation of an `of'¬†on its own line.")
127 :    
128 : monnier 39 (defvar sml-indent-args 4
129 :     "*Indentation of args placed on a separate line.")
130 :    
131 :     (defvar sml-indent-align-args t
132 :     "*Whether the arguments should be aligned.")
133 :    
134 : monnier 32 (defvar sml-case-indent nil
135 :     "*How to indent case-of expressions.
136 :     If t: case expr If nil: case expr of
137 :     of exp1 => ... exp1 => ...
138 :     | exp2 => ... | exp2 => ...
139 :    
140 :     The first seems to be the standard in SML/NJ, but the second
141 :     seems nicer...")
142 :    
143 :     (defvar sml-electric-semi-mode nil
144 :     "*If t, `\;' will self insert, reindent the line, and do a newline.
145 :     If nil, just insert a `\;'. (To insert while t, do: C-q \;).")
146 :    
147 :     ;;; OTHER GENERIC MODE VARIABLES
148 :    
149 :     (defvar sml-mode-info "sml-mode"
150 :     "*Where to find Info file for sml-mode.
151 :     The default assumes the info file \"sml-mode.info\" is on Emacs' info
152 :     directory path. If it is not, either put the file on the standard path
153 :     or set the variable sml-mode-info to the exact location of this file
154 :     which is part of the sml-mode 3.2 (and later) distribution. E.g:
155 :    
156 :     (setq sml-mode-info \"/usr/me/lib/info/sml-mode\")
157 :    
158 :     in your .emacs file. You can always set it interactively with the
159 :     set-variable command.")
160 :    
161 :     (defvar sml-mode-hook nil
162 :     "*This hook is run when sml-mode is loaded, or a new sml-mode buffer created.
163 :     This is a good place to put your preferred key bindings.")
164 :    
165 :     (defvar sml-load-hook nil
166 :     "*This hook is run when sml-mode (sml-mode.el) is loaded into Emacs.")
167 :    
168 :     (defvar sml-mode-abbrev-table nil "*SML mode abbrev table (default nil)")
169 :    
170 :     ;;; CODE FOR SML-MODE
171 :    
172 :     (defun sml-mode-info ()
173 :     "Command to access the TeXinfo documentation for sml-mode.
174 :     See doc for the variable sml-mode-info."
175 :     (interactive)
176 :     (require 'info)
177 :     (condition-case nil
178 : monnier 319 (Info-goto-node (concat "(" sml-mode-info ")"))
179 : monnier 32 (error (progn
180 :     (describe-variable 'sml-mode-info)
181 :     (message "Can't find it... set this variable first!")))))
182 :    
183 :    
184 :     ;;; Autoload functions -- no-doc is another idea cribbed from AucTeX!
185 :    
186 : monnier 319 (let ((sml-no-doc
187 :     "This function is part of sml-proc, and has not yet been loaded.
188 :     Full documentation will be available after autoloading the function."))
189 : monnier 32
190 : monnier 332 (autoload 'run-sml "sml-proc" sml-no-doc t)
191 :     (autoload 'sml-compile "sml-proc" sml-no-doc t)
192 :     (autoload 'sml-load-file "sml-proc" sml-no-doc t)
193 :     (autoload 'switch-to-sml "sml-proc" sml-no-doc t)
194 :     (autoload 'sml-send-region "sml-proc" sml-no-doc t)
195 :     (autoload 'sml-send-buffer "sml-proc" sml-no-doc t))
196 : monnier 32
197 : monnier 33 ;; font-lock setup
198 :    
199 : monnier 39 (defconst sml-keywords-regexp
200 : monnier 319 (sml-syms-re "abstraction" "abstype" "and" "andalso" "as" "before" "case"
201 :     "datatype" "else" "end" "eqtype" "exception" "do" "fn"
202 :     "fun" "functor" "handle" "if" "in" "include" "infix"
203 :     "infixr" "let" "local" "nonfix" "of" "op" "open" "orelse"
204 :     "overload" "raise" "rec" "sharing" "sig" "signature"
205 :     "struct" "structure" "then" "type" "val" "where" "while"
206 :     "with" "withtype")
207 : monnier 39 "A regexp that matches any and all keywords of SML.")
208 :    
209 : monnier 300 (defconst sml-font-lock-keywords
210 :     `(;;(sml-font-comments-and-strings)
211 : monnier 319 ("\\<\\(fun\\|and\\)\\s-+\\(\\sw+\\)"
212 : monnier 33 (1 font-lock-keyword-face)
213 :     (2 font-lock-function-def-face))
214 : monnier 319 ("\\<\\(\\(data\\|abs\\|with\\|eq\\)?type\\)\\s-+\\('\\s-*\\sw+\\s-+\\)*\\(\\sw+\\)"
215 : monnier 33 (1 font-lock-keyword-face)
216 : monnier 319 (4 font-lock-type-def-face))
217 :     ("\\<\\(val\\)\\s-+\\(\\sw+\\>\\s-*\\)?\\(\\sw+\\)\\s-*="
218 : monnier 33 (1 font-lock-keyword-face)
219 :     ;;(6 font-lock-variable-def-face nil t)
220 : monnier 319 (3 font-lock-variable-def-face))
221 :     ("\\<\\(structure\\|functor\\|abstraction\\)\\s-+\\(\\sw+\\)"
222 : monnier 33 (1 font-lock-keyword-face)
223 :     (2 font-lock-module-def-face))
224 : monnier 319 ("\\<\\(signature\\)\\s-+\\(\\sw+\\)"
225 : monnier 33 (1 font-lock-keyword-face)
226 :     (2 font-lock-interface-def-face))
227 :    
228 : monnier 39 (,sml-keywords-regexp . font-lock-keyword-face))
229 : monnier 33 "Regexps matching standard SML keywords.")
230 :    
231 :     ;; default faces values
232 : monnier 300 (flet ((def-face (face def)
233 :     "Define a face for font-lock."
234 :     (unless (boundp face)
235 :     (set face (cond
236 :     ((facep face) face)
237 :     ((facep def) (copy-face def face))
238 :     (t def))))))
239 :     (def-face 'font-lock-function-def-face 'font-lock-function-name-face)
240 :     (def-face 'font-lock-type-def-face 'font-lock-type-face)
241 :     (def-face 'font-lock-module-def-face 'font-lock-function-name-face)
242 :     (def-face 'font-lock-interface-def-face 'font-lock-type-face)
243 :     (def-face 'font-lock-variable-def-face 'font-lock-variable-name-face))
244 : monnier 33
245 : monnier 319 (defvar sml-syntax-prop-table
246 :     (let ((st (make-syntax-table)))
247 :     (modify-syntax-entry ?l "(d" st)
248 :     (modify-syntax-entry ?s "(d" st)
249 :     (modify-syntax-entry ?d ")l" st)
250 : monnier 332 (modify-syntax-entry ?\\ "." st)
251 : monnier 319 (modify-syntax-entry ?* "." st)
252 :     st))
253 : monnier 33
254 : monnier 300 (defun sml-get-depth-st ()
255 :     (save-excursion
256 :     (let* ((disp (if (eq (char-before) ?\)) (progn (backward-char) -1) nil))
257 :     (foo (backward-char))
258 :     (disp (if (eq (char-before) ?\() (progn (backward-char) 0) disp))
259 :     (pt (point)))
260 :     (when disp
261 :     (let* ((depth
262 :     (save-match-data
263 :     (if (re-search-backward "\\*)\\|(\\*" nil t)
264 :     (+ (or (get-char-property (point) 'comment-depth) 0)
265 :     (case (char-after) (?\( 1) (?* 0))
266 :     disp)
267 :     0)))
268 :     (depth (if (> depth 0) depth)))
269 :     (put-text-property pt (1+ pt) 'comment-depth depth)
270 : monnier 319 (when depth sml-syntax-prop-table))))))
271 : monnier 300
272 :     (defconst sml-font-lock-syntactic-keywords
273 : monnier 319 `(;;("\\<\\(l\\)\\(et\\|ocal\\)\\>" (1 ',sml-syntax-prop-table))
274 :     ;;("\\<\\(s\\)\\(ig\\truct\\)\\>" (1 ',sml-syntax-prop-table))
275 :     ;;("\\<en\\(d\\)\\>" (1 ',sml-syntax-prop-table))
276 : monnier 332 ("^\\s-*\\(\\\\\\)" (1 ',sml-syntax-prop-table))
277 : monnier 300 ("(?\\(\\*\\))?" (1 (sml-get-depth-st)))))
278 :    
279 :     (defconst sml-font-lock-defaults
280 : monnier 319 '(sml-font-lock-keywords nil nil ((?_ . "w") (?' . "w")) nil
281 : monnier 300 (font-lock-syntactic-keywords . sml-font-lock-syntactic-keywords)))
282 :    
283 : monnier 33
284 : monnier 32 ;;; MORE CODE FOR SML-MODE
285 :    
286 :     (defun sml-mode-version ()
287 :     "This file's version number (sml-mode)."
288 :     (interactive)
289 :     (message sml-mode-version-string))
290 :    
291 :     ;;;###Autoload
292 :     (defun sml-mode ()
293 :     "Major mode for editing ML code.
294 :     Tab indents for ML code.
295 :     Comments are delimited with (* ... *).
296 :     Blank lines and form-feeds separate paragraphs.
297 :     Delete converts tabs to spaces as it moves back.
298 :    
299 :     For information on running an inferior ML process, see the documentation
300 :     for inferior-sml-mode (set this up with \\[sml]).
301 :    
302 :     Customisation: Entry to this mode runs the hooks on sml-mode-hook.
303 :    
304 :     Variables controlling the indentation
305 :     =====================================
306 :    
307 :     Seek help (\\[describe-variable]) on individual variables to get current settings.
308 :    
309 :     sml-indent-level (default 4)
310 :     The indentation of a block of code.
311 :    
312 :     sml-pipe-indent (default -2)
313 :     Extra indentation of a line starting with \"|\".
314 :    
315 :     sml-case-indent (default nil)
316 :     Determine the way to indent case-of expression.
317 :    
318 :     sml-electric-semi-mode (default nil)
319 :     If t, a `\;' will reindent line, and perform a newline.
320 :    
321 :     Mode map
322 :     ========
323 :     \\{sml-mode-map}"
324 :    
325 :     (interactive)
326 :     (kill-all-local-variables)
327 :     (sml-mode-variables)
328 :     (use-local-map sml-mode-map)
329 :     (setq major-mode 'sml-mode)
330 :     (setq mode-name "SML")
331 : monnier 319 (set (make-local-variable 'outline-regexp) sml-outline-regexp)
332 : monnier 32 (run-hooks 'sml-mode-hook)) ; Run the hook last
333 :    
334 :     (defun sml-mode-variables ()
335 :     (set-syntax-table sml-mode-syntax-table)
336 :     (setq local-abbrev-table sml-mode-abbrev-table)
337 :     ;; A paragraph is separated by blank lines or ^L only.
338 : monnier 33
339 :     (set (make-local-variable 'paragraph-start)
340 :     (concat "^[\t ]*$\\|" page-delimiter))
341 :     (set (make-local-variable 'paragraph-separate) paragraph-start)
342 :     (set (make-local-variable 'indent-line-function) 'sml-indent-line)
343 :     (set (make-local-variable 'comment-start) "(* ")
344 :     (set (make-local-variable 'comment-end) " *)")
345 :     (set (make-local-variable 'comment-column) 40)
346 :     (set (make-local-variable 'comment-start-skip) "(\\*+[ \t]?")
347 :     (set (make-local-variable 'comment-indent-function) 'sml-comment-indent)
348 : monnier 332 (set (make-local-variable 'font-lock-defaults) sml-font-lock-defaults))
349 : monnier 32
350 :     (defun sml-electric-pipe ()
351 : monnier 319 "Insert a \"|\".
352 : monnier 32 Depending on the context insert the name of function, a \"=>\" etc."
353 :     (interactive)
354 : monnier 319 (sml-with-ist
355 :     (let ((text
356 :     (save-excursion
357 :     (sml-find-matching-starter sml-pipehead-re)
358 :     (cond
359 :     ;; It was a function, insert the function name
360 :     ((or (looking-at "fun\\>")
361 :     (and (looking-at "and\\>")
362 :     (save-excursion
363 :     (sml-find-matching-starter
364 :     (sml-syms-re "datatype" "abstype" "fun"))
365 :     (looking-at "fun\\>"))))
366 :     (forward-word 1) (sml-forward-spaces)
367 :     (concat
368 :     (buffer-substring (point) (progn (forward-word 1) (point)))
369 :     " = "))
370 : monnier 32
371 : monnier 319 ((looking-at (sml-syms-re "case" "handle" "fn")) " => ")
372 :     ((looking-at (sml-syms-re "abstype" "datatype" "and")) "")
373 :     (t (error "Wow, now, there's a bug"))))))
374 :    
375 :     (unless (save-excursion (skip-chars-backward "\t ") (bolp)) (insert "\n"))
376 :     (insert "| " text)
377 :     (sml-indent-line)
378 :     (beginning-of-line)
379 :     (skip-chars-forward "\t |")
380 :     (skip-syntax-forward "w")
381 :     (skip-chars-forward "\t ")
382 :     (when (= ?= (char-after)) (backward-char)))))
383 :    
384 : monnier 32 (defun sml-electric-semi ()
385 :     "Inserts a \;.
386 :     If variable sml-electric-semi-mode is t, indent the current line, insert
387 :     a newline, and indent."
388 :     (interactive)
389 :     (insert "\;")
390 :     (if sml-electric-semi-mode
391 :     (reindent-then-newline-and-indent)))
392 :    
393 :     ;;; INDENTATION !!!
394 :    
395 :     (defun sml-mark-function ()
396 :     "Synonym for mark-paragraph -- sorry.
397 :     If anyone has a good algorithm for this..."
398 :     (interactive)
399 :     (mark-paragraph))
400 :    
401 :     (defun sml-indent-region (begin end)
402 :     "Indent region of ML code."
403 :     (interactive "r")
404 :     (message "Indenting region...")
405 :     (save-excursion
406 :     (goto-char end) (setq end (point-marker)) (goto-char begin)
407 :     (while (< (point) end)
408 :     (skip-chars-forward "\t\n ")
409 :     (sml-indent-line)
410 :     (end-of-line))
411 :     (move-marker end nil))
412 :     (message "Indenting region... done"))
413 :    
414 :     (defun sml-indent-line ()
415 :     "Indent current line of ML code."
416 :     (interactive)
417 :     (let ((indent (sml-calculate-indentation)))
418 :     (if (/= (current-indentation) indent)
419 :     (save-excursion ;; Added 890601 (point now stays)
420 :     (let ((beg (progn (beginning-of-line) (point))))
421 :     (skip-chars-forward "\t ")
422 :     (delete-region beg (point))
423 :     (indent-to indent))))
424 :     ;; If point is before indentation, move point to indentation
425 :     (if (< (current-column) (current-indentation))
426 :     (skip-chars-forward "\t "))))
427 :    
428 :     (defun sml-back-to-outer-indent ()
429 :     "Unindents to the next outer level of indentation."
430 :     (interactive)
431 :     (save-excursion
432 :     (beginning-of-line)
433 :     (skip-chars-forward "\t ")
434 :     (let ((start-column (current-column))
435 :     (indent (current-column)))
436 :     (if (> start-column 0)
437 :     (progn
438 :     (save-excursion
439 :     (while (>= indent start-column)
440 :     (if (re-search-backward "^[^\n]" nil t)
441 :     (setq indent (current-indentation))
442 :     (setq indent 0))))
443 :     (backward-delete-char-untabify (- start-column indent)))))))
444 :    
445 : monnier 39 (defun sml-find-comment-indent ()
446 :     (save-excursion
447 :     (let ((depth 1))
448 :     (while (> depth 0)
449 :     (if (re-search-backward "(\\*\\|\\*)" nil t)
450 :     (cond
451 :     ((looking-at "*)") (incf depth))
452 : monnier 300 ((looking-at comment-start-skip) (decf depth)))
453 : monnier 39 (setq depth -1)))
454 :     (if (= depth 0)
455 :     (current-column)
456 :     nil))))
457 :    
458 : monnier 32 (defun sml-calculate-indentation ()
459 :     (save-excursion
460 : monnier 319 (beginning-of-line) (skip-chars-forward "\t ")
461 :     (sml-with-ist
462 :     (let ((indent 0)
463 :     (sml-point (point)))
464 :     (or
465 :     ;;(and (bobp) 0)
466 : monnier 300
467 : monnier 319 ;; Indentation for comments alone on a line, matches the
468 :     ;; proper indentation of the next line.
469 :     (and (looking-at comment-start-skip) (sml-forward-spaces) nil)
470 : monnier 300
471 : monnier 319 ;; continued comment
472 :     (and (looking-at "\\*") (setq indent (sml-find-comment-indent))
473 :     (1+ indent))
474 : monnier 300
475 : monnier 319 ;; Continued string ? (Added 890113 lbn)
476 :     (and (looking-at "\\\\")
477 :     (save-excursion
478 :     (if (save-excursion (previous-line 1)
479 :     (beginning-of-line)
480 :     (looking-at "[\t ]*\\\\"))
481 :     (progn (previous-line 1) (current-indentation))
482 :     (if (re-search-backward "[^\\\\]\"" nil t)
483 : monnier 332 (1+ (current-column))
484 : monnier 319 0))))
485 : monnier 300
486 : monnier 319 (and (looking-at "in\\>") ; Match the beginning let/local
487 :     (sml-find-match-indent "\\<in\\>" "\\<l\\(ocal\\|et\\)\\>"))
488 : monnier 32
489 : monnier 319 (and (looking-at "end\\>") ; Match the beginning
490 :     ;; FIXME: should match "in" if available. Or maybe not
491 :     (sml-find-match-indent "\\<end\\>" sml-begin-symbols-re))
492 : monnier 300
493 : monnier 319 (and (looking-at "else\\>") ; Match the if
494 :     (progn
495 :     (sml-find-match-backward "\\<else\\>" "\\<if\\>")
496 : monnier 332 ;;(sml-move-if (backward-word 1)
497 :     ;; (and sml-nested-if-indent
498 :     ;; (looking-at "else[ \t]+if\\>")))
499 :     (if (sml-dangling-sym)
500 :     (sml-indent-default 'noindent)
501 :     (current-column))))
502 : monnier 300
503 : monnier 319 (and (looking-at "then\\>") ; Match the if + extra indentation
504 :     (sml-find-match-indent "\\<then\\>" "\\<if\\>" t))
505 : monnier 39
506 : monnier 319 (and (looking-at "of\\>")
507 :     (progn
508 :     (sml-find-match-backward "\\<of\\>" "\\<case\\>")
509 :     (+ (current-column) sml-indent-case-of)))
510 : monnier 300
511 : monnier 319 (and (looking-at sml-starters-re)
512 : monnier 333 (let ((sym (unless (save-excursion (sml-backward-arg))
513 :     (sml-backward-spaces)
514 :     (sml-backward-sym))))
515 : monnier 319 (if sym (sml-get-sym-indent sym)
516 :     (sml-find-matching-starter sml-starters-re)
517 :     (current-column))))
518 : monnier 300
519 : monnier 319 (and (looking-at "|") (sml-indent-pipe))
520 : monnier 300
521 : monnier 319 (sml-indent-arg)
522 :     (sml-indent-default))))))
523 : monnier 300
524 : monnier 319 (defun sml-indent-pipe ()
525 :     (when (sml-find-matching-starter (concat "|\\|\\<of\\>\\|" sml-pipehead-re)
526 :     (sml-op-prec "|" 'back))
527 :     (if (looking-at "|")
528 :     (if (sml-bolp) (current-column) (sml-indent-pipe))
529 :     (cond
530 : monnier 333 ((looking-at "datatype\\>")
531 : monnier 319 (re-search-forward "=")
532 :     (forward-char))
533 :     ((looking-at "case\\>")
534 :     (sml-forward-sym) ;skip `case'
535 :     (sml-find-match-forward "\\<case\\>" "\\<of\\>"))
536 :     (t
537 :     (forward-word 1)))
538 :     (sml-forward-spaces)
539 :     (+ sml-pipe-indent (current-column)))))
540 : monnier 39
541 :    
542 : monnier 319 (defun sml-indent-arg ()
543 :     (and (save-excursion (ignore-errors (sml-forward-arg)))
544 :     ;;(not (looking-at sml-not-arg-re))
545 :     ;; looks like a function or an argument
546 :     (sml-move-if (sml-backward-arg))
547 :     ;; an argument
548 :     (if (save-excursion (not (sml-backward-arg)))
549 :     ;; a first argument
550 :     (+ (current-column) sml-indent-args)
551 :     ;; not a first arg
552 :     (while (and (/= (current-column) (current-indentation))
553 :     (sml-move-if (sml-backward-arg))))
554 :     (unless (save-excursion (sml-backward-arg))
555 :     ;; all earlier args are on the same line
556 :     (sml-forward-arg) (sml-forward-spaces))
557 :     (current-column))))
558 :    
559 :     (defun sml-re-assoc (al sym)
560 :     (when sym
561 :     (cdr (assoc* sym al
562 :     :test (lambda (x y) (string-match y x))))))
563 : monnier 332
564 : monnier 319 (defun sml-get-indent (data n &optional strict)
565 :     (eval (if (listp data)
566 :     (nth n data)
567 :     (and (not strict) data))))
568 :    
569 :     (defun sml-dangling-sym ()
570 : monnier 32 (save-excursion
571 : monnier 319 (and (not (sml-bolp))
572 :     (< (sml-point-after (end-of-line))
573 :     (sml-point-after (sml-forward-sym)
574 :     (sml-forward-spaces))))))
575 : monnier 32
576 : monnier 319 (defun sml-get-sym-indent (sym &optional style)
577 : monnier 332 "expects to be looking-at SYM.
578 :     If indentation is delegated, the point will be at the start of
579 :     the parent at the end of this function."
580 : monnier 319 (let ((indent-data (sml-re-assoc sml-indent-starters sym))
581 :     (delegate (eval (sml-re-assoc sml-delegate sym))))
582 :     (or (when indent-data
583 :     (if (or style (not delegate))
584 :     ;; normal indentation
585 :     (let ((indent (sml-get-indent indent-data (or style 0))))
586 :     (when indent
587 :     (+ (if (sml-dangling-sym)
588 :     (sml-indent-default 'noindent)
589 :     (current-column))
590 :     indent)))
591 :     ;; delgate indentation to the parent
592 :     (sml-forward-sym) (sml-backward-sexp nil)
593 : monnier 333 (let* ((parent-sym (save-excursion (sml-forward-sym)))
594 : monnier 319 (parent-indent (sml-re-assoc sml-indent-starters parent-sym)))
595 :     ;; check the special rules
596 : monnier 332 ;;(sml-move-if (backward-word 1)
597 :     ;; (looking-at "\\<else[ \t]+if\\>"))
598 : monnier 319 (+ (if (sml-dangling-sym)
599 :     (sml-indent-default 'noindent)
600 :     (current-column))
601 :     (or (sml-get-indent indent-data 1 'strict)
602 :     (sml-get-indent parent-indent 1 'strict)
603 :     (sml-get-indent indent-data 0)
604 :     (sml-get-indent parent-indent 0))))))
605 :     ;; (save-excursion
606 :     ;; (sml-forward-sym)
607 :     ;; (when (> (sml-point-after (end-of-line))
608 :     ;; (progn (sml-forward-spaces) (point)))
609 :     ;; (current-column)))
610 :     )))
611 : monnier 32
612 : monnier 319 (defun sml-indent-default (&optional noindent)
613 : monnier 333 (let* ((sym-after (save-excursion (sml-forward-sym)))
614 : monnier 319 (prec-after (sml-op-prec sym-after 'back))
615 :     (_ (sml-backward-spaces))
616 : monnier 333 (sym-before (sml-backward-sym))
617 : monnier 319 (prec (or (sml-op-prec sym-before 'back) prec-after 100))
618 : monnier 332 (sym-indent (and sym-before (sml-get-sym-indent sym-before))))
619 :     (or (and sym-indent (if noindent (current-column) sym-indent))
620 : monnier 319 (progn
621 :     ;;(sml-forward-sym)
622 :     (while (and (not (sml-bolp))
623 :     (sml-move-if (sml-backward-sexp (1- prec)))
624 :     (not (sml-bolp)))
625 :     (while (sml-move-if (sml-backward-sexp prec))))
626 :     (or (and (not (sml-bolp))
627 : monnier 332 ;; If we backed over an equal char which was not the
628 :     ;; polymorphic equality, then we did what amounts to
629 :     ;; delegate indent from `=' to the corresponding head, so we
630 :     ;; need to look at the preceding symbol and follow its
631 :     ;; intentation instructions.
632 :     (= prec 65) (string-equal "=" sym-before)
633 : monnier 319 (save-excursion
634 :     (sml-backward-spaces)
635 : monnier 333 (let* ((sym (sml-backward-sym))
636 : monnier 319 (sym-indent (sml-re-assoc sml-indent-starters sym)))
637 :     (when sym-indent
638 :     (if noindent
639 :     (current-column)
640 :     (sml-get-sym-indent sym 1))))))
641 :     (current-column))))))
642 : monnier 32
643 : monnier 319
644 :     (defun sml-bolp ()
645 : monnier 32 (save-excursion
646 : monnier 319 (skip-chars-backward " \t|") (bolp)))
647 : monnier 32
648 :    
649 : monnier 319 ;; maybe `|' should be set to word-syntax in our temp syntax table ?
650 :     (defun sml-current-indentation ()
651 : monnier 32 (save-excursion
652 : monnier 319 (beginning-of-line)
653 :     (skip-chars-forward " \t|")
654 :     (current-column)))
655 : monnier 32
656 : monnier 300
657 : monnier 319 (defun sml-find-match-indent (this match &optional indented)
658 :     (save-excursion
659 :     (sml-find-match-backward this match)
660 :     (if (or indented (not (sml-dangling-sym)))
661 :     (current-column)
662 :     (sml-indent-default 'noindent))))
663 :    
664 :     (defun sml-find-matching-starter (regexp &optional prec)
665 :     (sml-backward-sexp prec)
666 :     (while (not (or (looking-at regexp) (bobp)))
667 :     (sml-backward-sexp prec))
668 :     (not (bobp)))
669 :    
670 : monnier 32 (defun sml-comment-indent ()
671 :     (if (looking-at "^(\\*") ; Existing comment at beginning
672 :     0 ; of line stays there.
673 :     (save-excursion
674 :     (skip-chars-backward " \t")
675 :     (max (1+ (current-column)) ; Else indent at comment column
676 :     comment-column)))) ; except leave at least one space.
677 :    
678 :     ;;; INSERTING PROFORMAS (COMMON SML-FORMS)
679 :    
680 :     (defvar sml-forms-alist
681 :     '(("let") ("local") ("case") ("abstype") ("datatype")
682 :     ("signature") ("structure") ("functor"))
683 :     "*The list of templates to auto-insert.
684 :    
685 :     You can extend this alist to your heart's content. For each additional
686 :     template NAME in the list, declare a keyboard macro or function (or
687 :     interactive command) called 'sml-form-NAME'.
688 :    
689 :     If 'sml-form-NAME' is a function it takes no arguments and should
690 :     insert the template at point\; if this is a command it may accept any
691 :     sensible interactive call arguments\; keyboard macros can't take
692 :     arguments at all. Apropos keyboard macros, see `name-last-kbd-macro'
693 :     and `sml-addto-forms-alist'.
694 :    
695 :     `sml-forms-alist' understands let, local, case, abstype, datatype,
696 :     signature, structure, and functor by default.")
697 :    
698 :     ;; See also macros.el in emacs lisp dir.
699 :    
700 :     (defun sml-addto-forms-alist (name)
701 :     "Assign a name to the last keyboard macro defined.
702 :     Argument NAME is transmogrified to sml-form-NAME which is the symbol
703 :     actually defined.
704 :    
705 :     The symbol's function definition becomes the keyboard macro string.
706 :    
707 :     If that works, NAME is added to `sml-forms-alist' so you'll be able to
708 :     reinvoke the macro through \\[sml-insert-form]. You might want to save
709 :     the macro to use in a later editing session -- see `insert-kbd-macro'
710 :     and add these macros to your .emacs file.
711 :    
712 :     See also `edit-kbd-macro' which is bound to \\[edit-kbd-macro]."
713 :     (interactive "sName for last kbd macro (\"sml-form-\" will be added): ")
714 :     (if (string-equal name "")
715 :     (error "No command name given")
716 :     (name-last-kbd-macro (intern (concat "sml-form-" name)))
717 :     (message (concat "Macro bound to sml-form-" name))
718 :     (or (assoc name sml-forms-alist)
719 :     (setq sml-forms-alist (cons (list name) sml-forms-alist)))))
720 :    
721 :     ;; at a pinch these could be added to SML/Forms menu through the good
722 :     ;; offices of activate-menubar-hook or something... but documentation
723 :     ;; of this and/or menu-bar-update-hook is sparse in 19.33. anyway, use
724 :     ;; completing read for sml-insert-form prompt...
725 :    
726 :     (defvar sml-last-form "let"
727 :     "The most recent sml form inserted.")
728 :    
729 :     (defun sml-insert-form (arg)
730 :     "Interactive short-cut to insert a common ML form.
731 :     If a perfix argument is given insert a newline and indent first, or
732 :     just move to the proper indentation if the line is blank\; otherwise
733 :     insert at point (which forces indentation to current column).
734 :    
735 :     The default form to insert is 'whatever you inserted last time'
736 :     \(just hit return when prompted\)\; otherwise the command reads with
737 :     completion from `sml-forms-alist'."
738 :     (interactive "P")
739 :     (let ((name (completing-read
740 :     (format "Form to insert: (default %s) " sml-last-form)
741 :     sml-forms-alist nil t nil)))
742 :     ;; default is whatever the last insert was...
743 :     (if (string= name "") (setq name sml-last-form))
744 :     (setq sml-last-form name)
745 :     (if arg
746 :     (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
747 :     (sml-indent-line)
748 :     (newline-and-indent)))
749 :     (cond ((string= name "let") (sml-form-let))
750 :     ((string= name "local") (sml-form-local))
751 :     ((string= name "case") (sml-form-case))
752 :     ((string= name "abstype") (sml-form-abstype))
753 :     ((string= name "datatype") (sml-form-datatype))
754 :     ((string= name "functor") (sml-form-functor))
755 :     ((string= name "structure") (sml-form-structure))
756 :     ((string= name "signature") (sml-form-signature))
757 :     (t
758 :     (let ((template (intern (concat "sml-form-" name))))
759 :     (if (fboundp template)
760 :     (if (commandp template)
761 :     ;; it may be a named kbd macro too
762 :     (command-execute template)
763 :     (funcall template))
764 :     (error
765 :     (format "Undefined format function: %s" template))))))))
766 :    
767 :     (defun sml-form-let ()
768 :     "Insert a `let in end' template."
769 :     (interactive)
770 :     (sml-let-local "let"))
771 :    
772 :     (defun sml-form-local ()
773 :     "Insert a `local in end' template."
774 :     (interactive)
775 :     (sml-let-local "local"))
776 :    
777 :     (defun sml-let-local (starter)
778 :     "Insert a let or local template, depending on STARTER string."
779 :     (let ((indent (current-column)))
780 :     (insert starter)
781 :     (insert "\n") (indent-to (+ sml-indent-level indent))
782 :     (save-excursion ; so point returns here
783 :     (insert "\n")
784 :     (indent-to indent)
785 :     (insert "in\n")
786 :     (indent-to (+ sml-indent-level indent))
787 :     (insert "\n")
788 :     (indent-to indent)
789 :     (insert "end"))))
790 :    
791 :     (defun sml-form-case ()
792 :     "Insert a case expression template, prompting for the case-expresion."
793 :     (interactive)
794 :     (let ((expr (read-string "Case expr: "))
795 :     (indent (current-column)))
796 :     (insert (concat "case " expr))
797 :     (if sml-case-indent
798 :     (progn
799 :     (insert "\n")
800 :     (indent-to (+ 2 indent))
801 :     (insert "of "))
802 :     (insert " of\n")
803 :     (indent-to (+ indent sml-indent-level)))
804 :     (save-excursion (insert " => "))))
805 :    
806 :     (defun sml-form-signature ()
807 :     "Insert a generative signature binding, prompting for the name."
808 :     (interactive)
809 :     (let ((indent (current-column))
810 :     (name (read-string "Signature name: ")))
811 :     (insert (concat "signature " name " ="))
812 :     (insert "\n")
813 :     (indent-to (+ sml-structure-indent indent))
814 :     (insert "sig\n")
815 :     (indent-to (+ sml-structure-indent sml-indent-level indent))
816 :     (save-excursion
817 :     (insert "\n")
818 :     (indent-to (+ sml-structure-indent indent))
819 :     (insert "end"))))
820 :    
821 :     (defun sml-form-structure ()
822 :     "Insert a generative structure binding, prompting for the name.
823 :     The command also prompts for any signature constraint -- you should
824 :     specify \":\" or \":>\" and the constraining signature."
825 :     (interactive)
826 :     (let ((indent (current-column))
827 :     (name (read-string (concat "Structure name: ")))
828 :     (signame (read-string "Signature constraint (default none): ")))
829 :     (insert (concat "structure " name " "))
830 :     (insert (if (string= "" signame) "=" (concat signame " =")))
831 :     (insert "\n")
832 :     (indent-to (+ sml-structure-indent indent))
833 :     (insert "struct\n")
834 :     (indent-to (+ sml-structure-indent sml-indent-level indent))
835 :     (save-excursion
836 :     (insert "\n")
837 :     (indent-to (+ sml-structure-indent indent))
838 :     (insert "end"))))
839 :    
840 :     (defun sml-form-functor ()
841 :     "Insert a genarative functor binding, prompting for the name.
842 :     The command also prompts for the required signature constraint -- you
843 :     should specify \":\" or \":>\" and the constraining signature."
844 :     (interactive)
845 :     (let ((indent(current-indentation))
846 :     (name (read-string "Name of functor: "))
847 :     (signame (read-string "Signature constraint: " ":" )))
848 :     (insert (concat "functor " name " () " signame " ="))
849 :     (insert "\n")
850 :     (indent-to (+ sml-structure-indent indent))
851 :     (insert "struct\n")
852 :     (indent-to (+ sml-structure-indent sml-indent-level indent))
853 :     (save-excursion ; return to () instead?
854 :     (insert "\n")
855 :     (indent-to (+ sml-structure-indent indent))
856 :     (insert "end"))))
857 :    
858 :     (defun sml-form-datatype ()
859 :     "Insert a datatype declaration, prompting for name and type parameter."
860 :     (interactive)
861 :     (let ((indent (current-indentation))
862 :     (type (read-string "Datatype type parameter (default none): "))
863 :     (name (read-string (concat "Name of datatype: "))))
864 :     (insert (concat "datatype "
865 :     (if (string= type "") "" (concat type " "))
866 :     name " ="))
867 :     (insert "\n")
868 :     (indent-to (+ sml-indent-level indent))))
869 :    
870 :     (defun sml-form-abstype ()
871 :     "Insert an abstype declaration, prompting for name and type parameter."
872 :     (interactive)
873 :     (let ((indent(current-indentation))
874 :     (type (read-string "Abstype type parameter (default none): "))
875 :     (name (read-string "Name of abstype: ")))
876 :     (insert (concat "abstype "
877 :     (if (string= type "") "" (concat type " "))
878 :     name " ="))
879 :     (insert "\n")
880 :     (indent-to (+ sml-indent-level indent))
881 :     (save-excursion
882 :     (insert "\n")
883 :     (indent-to indent)
884 :     (insert "with\n")
885 :     (indent-to (+ sml-indent-level indent))
886 :     (insert "\n")
887 :     (indent-to indent)
888 :     (insert "end"))))
889 :    
890 :     ;;; & do the user's customisation
891 :     (run-hooks 'sml-load-hook)
892 :    
893 :     ;;; sml-mode.el has just finished.
894 : monnier 319 (provide 'sml-mode)

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