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 394 - (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 343 (defconst sml-mode-version-string "sml-mode, version 3.9.2")
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 : monnier 394 (defcustom sml-indent-level 4
117 :     "*Indentation of blocks in ML (see also `sml-structure-indent')."
118 :     :group 'sml
119 :     :type '(integer))
120 : monnier 32
121 : monnier 394 (defcustom sml-indent-args sml-indent-level
122 :     "*Indentation of args placed on a separate line."
123 :     :group 'sml
124 :     :type '(integer))
125 : monnier 39
126 : monnier 341 ;; (defvar sml-indent-align-args t
127 :     ;; "*Whether the arguments should be aligned.")
128 : monnier 39
129 : monnier 341 ;; (defvar sml-case-indent nil
130 :     ;; "*How to indent case-of expressions.
131 :     ;; If t: case expr If nil: case expr of
132 :     ;; of exp1 => ... exp1 => ...
133 :     ;; | exp2 => ... | exp2 => ...
134 : monnier 334
135 : monnier 341 ;; The first seems to be the standard in SML/NJ, but the second
136 :     ;; seems nicer...")
137 : monnier 32
138 : monnier 394 (defcustom sml-electric-semi-mode nil
139 :     "*If non-nil, `\;' will self insert, reindent the line, and do a newline.
140 :     If nil, just insert a `\;'. (To insert while t, do: C-q \;)."
141 :     :group 'sml
142 :     :type '(boolean))
143 : monnier 32
144 :     ;;; OTHER GENERIC MODE VARIABLES
145 :    
146 :     (defvar sml-mode-info "sml-mode"
147 :     "*Where to find Info file for sml-mode.
148 :     The default assumes the info file \"sml-mode.info\" is on Emacs' info
149 :     directory path. If it is not, either put the file on the standard path
150 :     or set the variable sml-mode-info to the exact location of this file
151 :     which is part of the sml-mode 3.2 (and later) distribution. E.g:
152 :    
153 :     (setq sml-mode-info \"/usr/me/lib/info/sml-mode\")
154 :    
155 :     in your .emacs file. You can always set it interactively with the
156 :     set-variable command.")
157 :    
158 :     (defvar sml-mode-hook nil
159 :     "*This hook is run when sml-mode is loaded, or a new sml-mode buffer created.
160 :     This is a good place to put your preferred key bindings.")
161 :    
162 :     (defvar sml-load-hook nil
163 :     "*This hook is run when sml-mode (sml-mode.el) is loaded into Emacs.")
164 :    
165 :     (defvar sml-mode-abbrev-table nil "*SML mode abbrev table (default nil)")
166 :    
167 :     ;;; CODE FOR SML-MODE
168 :    
169 :     (defun sml-mode-info ()
170 :     "Command to access the TeXinfo documentation for sml-mode.
171 :     See doc for the variable sml-mode-info."
172 :     (interactive)
173 :     (require 'info)
174 :     (condition-case nil
175 : monnier 319 (Info-goto-node (concat "(" sml-mode-info ")"))
176 : monnier 32 (error (progn
177 :     (describe-variable 'sml-mode-info)
178 :     (message "Can't find it... set this variable first!")))))
179 :    
180 :    
181 :     ;;; Autoload functions -- no-doc is another idea cribbed from AucTeX!
182 :    
183 : monnier 319 (let ((sml-no-doc
184 :     "This function is part of sml-proc, and has not yet been loaded.
185 :     Full documentation will be available after autoloading the function."))
186 : monnier 32
187 : monnier 332 (autoload 'run-sml "sml-proc" sml-no-doc t)
188 :     (autoload 'sml-compile "sml-proc" sml-no-doc t)
189 :     (autoload 'sml-load-file "sml-proc" sml-no-doc t)
190 :     (autoload 'switch-to-sml "sml-proc" sml-no-doc t)
191 :     (autoload 'sml-send-region "sml-proc" sml-no-doc t)
192 :     (autoload 'sml-send-buffer "sml-proc" sml-no-doc t))
193 : monnier 32
194 : monnier 33 ;; font-lock setup
195 :    
196 : monnier 39 (defconst sml-keywords-regexp
197 : monnier 319 (sml-syms-re "abstraction" "abstype" "and" "andalso" "as" "before" "case"
198 :     "datatype" "else" "end" "eqtype" "exception" "do" "fn"
199 :     "fun" "functor" "handle" "if" "in" "include" "infix"
200 :     "infixr" "let" "local" "nonfix" "of" "op" "open" "orelse"
201 :     "overload" "raise" "rec" "sharing" "sig" "signature"
202 :     "struct" "structure" "then" "type" "val" "where" "while"
203 :     "with" "withtype")
204 : monnier 39 "A regexp that matches any and all keywords of SML.")
205 :    
206 : monnier 300 (defconst sml-font-lock-keywords
207 :     `(;;(sml-font-comments-and-strings)
208 : monnier 347 ("\\<\\(fun\\|and\\)\\s-+\\('\\sw+\\s-+\\)*\\(\\sw+\\)"
209 : monnier 33 (1 font-lock-keyword-face)
210 : monnier 394 (3 font-lock-function-name-face))
211 : monnier 347 ("\\<\\(\\(data\\|abs\\|with\\|eq\\)?type\\)\\s-+\\('\\sw+\\s-+\\)*\\(\\sw+\\)"
212 : monnier 33 (1 font-lock-keyword-face)
213 : monnier 319 (4 font-lock-type-def-face))
214 :     ("\\<\\(val\\)\\s-+\\(\\sw+\\>\\s-*\\)?\\(\\sw+\\)\\s-*="
215 : monnier 33 (1 font-lock-keyword-face)
216 :     ;;(6 font-lock-variable-def-face nil t)
217 : monnier 394 (3 font-lock-variable-name-face))
218 : monnier 319 ("\\<\\(structure\\|functor\\|abstraction\\)\\s-+\\(\\sw+\\)"
219 : monnier 33 (1 font-lock-keyword-face)
220 :     (2 font-lock-module-def-face))
221 : monnier 319 ("\\<\\(signature\\)\\s-+\\(\\sw+\\)"
222 : monnier 33 (1 font-lock-keyword-face)
223 :     (2 font-lock-interface-def-face))
224 :    
225 : monnier 39 (,sml-keywords-regexp . font-lock-keyword-face))
226 : monnier 33 "Regexps matching standard SML keywords.")
227 :    
228 : monnier 394 (defface font-lock-type-def-face
229 :     '((t (:bold t)))
230 :     "Font Lock mode face used to highlight type definitions."
231 :     :group 'font-lock-highlighting-faces)
232 :     (defvar font-lock-type-def-face 'font-lock-type-def-face
233 :     "Face name to use for type definitions.")
234 : monnier 33
235 : monnier 394 (defface font-lock-module-def-face
236 :     '((t (:bold t)))
237 :     "Font Lock mode face used to highlight module definitions."
238 :     :group 'font-lock-highlighting-faces)
239 :     (defvar font-lock-module-def-face 'font-lock-module-def-face
240 :     "Face name to use for module definitions.")
241 :    
242 :     (defface font-lock-interface-def-face
243 :     '((t (:bold t)))
244 :     "Font Lock mode face used to highlight interface definitions."
245 :     :group 'font-lock-highlighting-faces)
246 :     (defvar font-lock-interface-def-face 'font-lock-interface-def-face
247 :     "Face name to use for interface definitions.")
248 :    
249 : monnier 319 (defvar sml-syntax-prop-table
250 :     (let ((st (make-syntax-table)))
251 : monnier 394 ;;(modify-syntax-entry ?l "(d" st)
252 :     ;;(modify-syntax-entry ?s "(d" st)
253 :     ;;(modify-syntax-entry ?d ")l" st)
254 : monnier 332 (modify-syntax-entry ?\\ "." st)
255 : monnier 319 (modify-syntax-entry ?* "." st)
256 :     st))
257 : monnier 33
258 : monnier 300 (defun sml-get-depth-st ()
259 :     (save-excursion
260 :     (let* ((disp (if (eq (char-before) ?\)) (progn (backward-char) -1) nil))
261 :     (foo (backward-char))
262 :     (disp (if (eq (char-before) ?\() (progn (backward-char) 0) disp))
263 :     (pt (point)))
264 :     (when disp
265 :     (let* ((depth
266 :     (save-match-data
267 :     (if (re-search-backward "\\*)\\|(\\*" nil t)
268 :     (+ (or (get-char-property (point) 'comment-depth) 0)
269 :     (case (char-after) (?\( 1) (?* 0))
270 :     disp)
271 :     0)))
272 :     (depth (if (> depth 0) depth)))
273 :     (put-text-property pt (1+ pt) 'comment-depth depth)
274 : monnier 319 (when depth sml-syntax-prop-table))))))
275 : monnier 300
276 :     (defconst sml-font-lock-syntactic-keywords
277 : monnier 319 `(;;("\\<\\(l\\)\\(et\\|ocal\\)\\>" (1 ',sml-syntax-prop-table))
278 :     ;;("\\<\\(s\\)\\(ig\\truct\\)\\>" (1 ',sml-syntax-prop-table))
279 :     ;;("\\<en\\(d\\)\\>" (1 ',sml-syntax-prop-table))
280 : monnier 332 ("^\\s-*\\(\\\\\\)" (1 ',sml-syntax-prop-table))
281 : monnier 300 ("(?\\(\\*\\))?" (1 (sml-get-depth-st)))))
282 :    
283 :     (defconst sml-font-lock-defaults
284 : monnier 319 '(sml-font-lock-keywords nil nil ((?_ . "w") (?' . "w")) nil
285 : monnier 300 (font-lock-syntactic-keywords . sml-font-lock-syntactic-keywords)))
286 :    
287 : monnier 33
288 : monnier 32 ;;; MORE CODE FOR SML-MODE
289 :    
290 :     (defun sml-mode-version ()
291 :     "This file's version number (sml-mode)."
292 :     (interactive)
293 :     (message sml-mode-version-string))
294 :    
295 :     ;;;###Autoload
296 :     (defun sml-mode ()
297 :     "Major mode for editing ML code.
298 : monnier 347 Entry to this mode runs the hooks on sml-mode-hook.
299 : monnier 32 \\{sml-mode-map}"
300 :    
301 :     (interactive)
302 :     (kill-all-local-variables)
303 :     (sml-mode-variables)
304 :     (use-local-map sml-mode-map)
305 :     (setq major-mode 'sml-mode)
306 :     (setq mode-name "SML")
307 : monnier 319 (set (make-local-variable 'outline-regexp) sml-outline-regexp)
308 : monnier 32 (run-hooks 'sml-mode-hook)) ; Run the hook last
309 :    
310 :     (defun sml-mode-variables ()
311 :     (set-syntax-table sml-mode-syntax-table)
312 :     (setq local-abbrev-table sml-mode-abbrev-table)
313 :     ;; A paragraph is separated by blank lines or ^L only.
314 : monnier 33
315 :     (set (make-local-variable 'paragraph-start)
316 :     (concat "^[\t ]*$\\|" page-delimiter))
317 :     (set (make-local-variable 'paragraph-separate) paragraph-start)
318 :     (set (make-local-variable 'indent-line-function) 'sml-indent-line)
319 :     (set (make-local-variable 'comment-start) "(* ")
320 :     (set (make-local-variable 'comment-end) " *)")
321 : monnier 378 ;;(set (make-local-variable 'block-comment-start) "* ")
322 :     ;;(set (make-local-variable 'block-comment-end) "")
323 : monnier 33 (set (make-local-variable 'comment-column) 40)
324 : monnier 378 (set (make-local-variable 'comment-start-skip) "(\\*+\\s-*")
325 : monnier 33 (set (make-local-variable 'comment-indent-function) 'sml-comment-indent)
326 : monnier 332 (set (make-local-variable 'font-lock-defaults) sml-font-lock-defaults))
327 : monnier 32
328 :     (defun sml-electric-pipe ()
329 : monnier 319 "Insert a \"|\".
330 : monnier 32 Depending on the context insert the name of function, a \"=>\" etc."
331 :     (interactive)
332 : monnier 319 (sml-with-ist
333 : monnier 334 (unless (save-excursion (skip-chars-backward "\t ") (bolp)) (insert "\n"))
334 :     (insert "| ")
335 : monnier 319 (let ((text
336 :     (save-excursion
337 : monnier 334 (backward-char 2) ;back over the just inserted "| "
338 :     (sml-find-matching-starter sml-pipehead-re
339 :     (sml-op-prec "|" 'back))
340 :     (let ((sym (sml-forward-sym)))
341 :     (sml-forward-spaces)
342 :     (cond
343 :     ((string= sym "|")
344 :     (let ((f (sml-forward-sym)))
345 :     (sml-find-forward "\\(=>\\|=\\||\\)\\S.")
346 :     (cond
347 :     ((looking-at "|") "") ;probably a datatype
348 :     ((looking-at "=>") " => ") ;`case', or `fn' or `handle'
349 :     ((looking-at "=") (concat f " = "))))) ;a function
350 :     ((string= sym "and")
351 :     ;; could be a datatype or a function
352 :     (while (and (setq sym (sml-forward-sym))
353 :     (string-match "^'" sym))
354 :     (sml-forward-spaces))
355 :     (sml-forward-spaces)
356 :     (if (or (not sym)
357 :     (equal (sml-forward-sym) "d="))
358 :     ""
359 :     (concat sym " = ")))
360 :     ;; trivial cases
361 :     ((string= sym "fun")
362 :     (while (and (setq sym (sml-forward-sym))
363 :     (string-match "^'" sym))
364 :     (sml-forward-spaces))
365 :     (concat sym " = "))
366 :     ((member sym '("case" "handle" "fn")) " => ")
367 :     ((member sym '("abstype" "datatype")) "")
368 :     (t (error "Wow, now, there's a bug")))))))
369 : monnier 32
370 : monnier 334 (insert text)
371 : monnier 319 (sml-indent-line)
372 :     (beginning-of-line)
373 :     (skip-chars-forward "\t |")
374 :     (skip-syntax-forward "w")
375 :     (skip-chars-forward "\t ")
376 :     (when (= ?= (char-after)) (backward-char)))))
377 :    
378 : monnier 32 (defun sml-electric-semi ()
379 :     "Inserts a \;.
380 :     If variable sml-electric-semi-mode is t, indent the current line, insert
381 :     a newline, and indent."
382 :     (interactive)
383 :     (insert "\;")
384 :     (if sml-electric-semi-mode
385 :     (reindent-then-newline-and-indent)))
386 :    
387 :     ;;; INDENTATION !!!
388 :    
389 :     (defun sml-mark-function ()
390 :     "Synonym for mark-paragraph -- sorry.
391 :     If anyone has a good algorithm for this..."
392 :     (interactive)
393 :     (mark-paragraph))
394 :    
395 : monnier 347 ;; (defun sml-indent-region (begin end)
396 :     ;; "Indent region of ML code."
397 :     ;; (interactive "r")
398 :     ;; (message "Indenting region...")
399 :     ;; (save-excursion
400 :     ;; (goto-char end) (setq end (point-marker)) (goto-char begin)
401 :     ;; (while (< (point) end)
402 :     ;; (skip-chars-forward "\t\n ")
403 :     ;; (sml-indent-line)
404 :     ;; (end-of-line))
405 :     ;; (move-marker end nil))
406 :     ;; (message "Indenting region... done"))
407 : monnier 32
408 :     (defun sml-indent-line ()
409 :     "Indent current line of ML code."
410 :     (interactive)
411 :     (let ((indent (sml-calculate-indentation)))
412 :     (if (/= (current-indentation) indent)
413 :     (save-excursion ;; Added 890601 (point now stays)
414 :     (let ((beg (progn (beginning-of-line) (point))))
415 :     (skip-chars-forward "\t ")
416 :     (delete-region beg (point))
417 :     (indent-to indent))))
418 :     ;; If point is before indentation, move point to indentation
419 :     (if (< (current-column) (current-indentation))
420 :     (skip-chars-forward "\t "))))
421 :    
422 :     (defun sml-back-to-outer-indent ()
423 :     "Unindents to the next outer level of indentation."
424 :     (interactive)
425 :     (save-excursion
426 :     (beginning-of-line)
427 :     (skip-chars-forward "\t ")
428 :     (let ((start-column (current-column))
429 :     (indent (current-column)))
430 :     (if (> start-column 0)
431 :     (progn
432 :     (save-excursion
433 :     (while (>= indent start-column)
434 :     (if (re-search-backward "^[^\n]" nil t)
435 :     (setq indent (current-indentation))
436 :     (setq indent 0))))
437 :     (backward-delete-char-untabify (- start-column indent)))))))
438 :    
439 : monnier 39 (defun sml-find-comment-indent ()
440 :     (save-excursion
441 :     (let ((depth 1))
442 :     (while (> depth 0)
443 :     (if (re-search-backward "(\\*\\|\\*)" nil t)
444 :     (cond
445 :     ((looking-at "*)") (incf depth))
446 : monnier 300 ((looking-at comment-start-skip) (decf depth)))
447 : monnier 39 (setq depth -1)))
448 :     (if (= depth 0)
449 : monnier 334 (1+ (current-column))
450 : monnier 39 nil))))
451 :    
452 : monnier 32 (defun sml-calculate-indentation ()
453 :     (save-excursion
454 : monnier 319 (beginning-of-line) (skip-chars-forward "\t ")
455 :     (sml-with-ist
456 : monnier 334 ;; Indentation for comments alone on a line, matches the
457 :     ;; proper indentation of the next line.
458 : monnier 378 (when (looking-at "(\\*") (sml-forward-spaces))
459 : monnier 334 (let (data
460 :     (sml-point (point))
461 :     (sym (save-excursion (sml-forward-sym))))
462 : monnier 319 (or
463 : monnier 347 ;; allow the user to override the indentation
464 : monnier 378 (when (looking-at (concat ".*" (regexp-quote comment-start)
465 :     "[ \t]*fixindent[ \t]*"
466 :     (regexp-quote comment-end)))
467 :     (current-indentation))
468 : monnier 347
469 : monnier 319 ;; continued comment
470 : monnier 334 (and (looking-at "\\*") (sml-find-comment-indent))
471 : monnier 300
472 : monnier 319 ;; Continued string ? (Added 890113 lbn)
473 :     (and (looking-at "\\\\")
474 :     (save-excursion
475 :     (if (save-excursion (previous-line 1)
476 :     (beginning-of-line)
477 :     (looking-at "[\t ]*\\\\"))
478 :     (progn (previous-line 1) (current-indentation))
479 :     (if (re-search-backward "[^\\\\]\"" nil t)
480 : monnier 332 (1+ (current-column))
481 : monnier 319 0))))
482 : monnier 300
483 : monnier 334 (and (setq data (assoc sym sml-close-paren))
484 :     (sml-indent-relative sym data))
485 : monnier 32
486 : monnier 319 (and (looking-at sml-starters-re)
487 : monnier 333 (let ((sym (unless (save-excursion (sml-backward-arg))
488 :     (sml-backward-spaces)
489 :     (sml-backward-sym))))
490 : monnier 319 (if sym (sml-get-sym-indent sym)
491 : monnier 334 ;; FIXME: this can take a *long* time !!
492 : monnier 319 (sml-find-matching-starter sml-starters-re)
493 :     (current-column))))
494 : monnier 300
495 : monnier 334 (and (string= sym "|") (sml-indent-pipe))
496 : monnier 300
497 : monnier 319 (sml-indent-arg)
498 :     (sml-indent-default))))))
499 : monnier 300
500 : monnier 334 (defun sml-indent-relative (sym data)
501 :     (save-excursion
502 :     (sml-forward-sym) (sml-backward-sexp nil)
503 : monnier 339 (unless (second data) (sml-backward-spaces) (sml-backward-sym))
504 : monnier 334 (+ (or (cdr (assoc sym sml-symbol-indent)) 0)
505 :     (sml-delegated-indent))))
506 :    
507 : monnier 319 (defun sml-indent-pipe ()
508 : monnier 334 (when (sml-find-matching-starter sml-pipehead-re
509 : monnier 319 (sml-op-prec "|" 'back))
510 :     (if (looking-at "|")
511 :     (if (sml-bolp) (current-column) (sml-indent-pipe))
512 : monnier 341 (let ((pipe-indent (or (cdr (assoc "|" sml-symbol-indent)) -2)))
513 :     (when (looking-at "\\(data\\|abs\\)type\\>")
514 :     (re-search-forward "="))
515 :     (sml-forward-sym)
516 :     (sml-forward-spaces)
517 :     (+ pipe-indent (current-column))))))
518 : monnier 39
519 : monnier 334 (defun sml-find-forward (re)
520 :     (sml-forward-spaces)
521 :     (while (and (not (looking-at re))
522 :     (progn
523 :     (or (ignore-errors (forward-sexp 1) t) (forward-char 1))
524 :     (sml-forward-spaces)
525 :     (not (looking-at re))))))
526 : monnier 39
527 : monnier 319 (defun sml-indent-arg ()
528 :     (and (save-excursion (ignore-errors (sml-forward-arg)))
529 :     ;;(not (looking-at sml-not-arg-re))
530 :     ;; looks like a function or an argument
531 :     (sml-move-if (sml-backward-arg))
532 :     ;; an argument
533 :     (if (save-excursion (not (sml-backward-arg)))
534 :     ;; a first argument
535 :     (+ (current-column) sml-indent-args)
536 :     ;; not a first arg
537 :     (while (and (/= (current-column) (current-indentation))
538 :     (sml-move-if (sml-backward-arg))))
539 :     (unless (save-excursion (sml-backward-arg))
540 :     ;; all earlier args are on the same line
541 :     (sml-forward-arg) (sml-forward-spaces))
542 :     (current-column))))
543 :    
544 : monnier 334 (defun sml-get-indent (data sym)
545 :     (let ((head-sym (pop data)) d)
546 :     (cond
547 :     ((not (listp data)) data)
548 :     ((setq d (member sym data)) (second d))
549 :     ((and (consp data) (not (stringp (car data)))) (car data))
550 :     (t sml-indent-level))))
551 : monnier 332
552 : monnier 319 (defun sml-dangling-sym ()
553 : monnier 32 (save-excursion
554 : monnier 319 (and (not (sml-bolp))
555 :     (< (sml-point-after (end-of-line))
556 :     (sml-point-after (sml-forward-sym)
557 :     (sml-forward-spaces))))))
558 : monnier 32
559 : monnier 334 (defun sml-delegated-indent ()
560 :     (if (sml-dangling-sym)
561 :     (sml-indent-default 'noindent)
562 :     (sml-move-if (backward-word 1)
563 : monnier 341 (looking-at sml-agglomerate-re))
564 : monnier 334 (current-column)))
565 :    
566 : monnier 319 (defun sml-get-sym-indent (sym &optional style)
567 : monnier 332 "expects to be looking-at SYM.
568 :     If indentation is delegated, the point will be at the start of
569 :     the parent at the end of this function."
570 : monnier 334 (assert (equal sym (save-excursion (sml-forward-sym))))
571 :     (save-excursion
572 :     (let ((delegate (assoc sym sml-close-paren))
573 :     (head-sym sym))
574 : monnier 339 (when (and delegate (not (eval (third delegate))))
575 : monnier 334 ;;(sml-find-match-backward sym delegate)
576 :     (sml-forward-sym) (sml-backward-sexp nil)
577 :     (setq head-sym
578 : monnier 339 (if (second delegate)
579 : monnier 334 (save-excursion (sml-forward-sym))
580 :     (sml-backward-spaces) (sml-backward-sym))))
581 : monnier 32
582 : monnier 334 (let ((idata (assoc head-sym sml-indent-rule)))
583 :     (when idata
584 :     ;;(if (or style (not delegate))
585 :     ;; normal indentation
586 :     (let ((indent (sml-get-indent idata sym)))
587 :     (when indent (+ (sml-delegated-indent) indent)))
588 :     ;; delgate indentation to the parent
589 :     ;;(sml-forward-sym) (sml-backward-sexp nil)
590 :     ;;(let* ((parent-sym (save-excursion (sml-forward-sym)))
591 :     ;; (parent-indent (cdr (assoc parent-sym sml-indent-starters))))
592 :     ;; check the special rules
593 :     ;;(+ (sml-delegated-indent)
594 :     ;; (or (sml-get-indent indent-data 1 'strict)
595 :     ;; (sml-get-indent parent-indent 1 'strict)
596 :     ;; (sml-get-indent indent-data 0)
597 :     ;; (sml-get-indent parent-indent 0))))))))
598 :     )))))
599 :    
600 : monnier 319 (defun sml-indent-default (&optional noindent)
601 : monnier 333 (let* ((sym-after (save-excursion (sml-forward-sym)))
602 : monnier 319 (_ (sml-backward-spaces))
603 : monnier 333 (sym-before (sml-backward-sym))
604 : monnier 332 (sym-indent (and sym-before (sml-get-sym-indent sym-before))))
605 : monnier 334 (if sym-indent
606 : monnier 339 ;; the previous sym is an indentation introducer: follow the rule
607 :     (let ((indent-after (or (cdr (assoc sym-after sml-symbol-indent)) 0)))
608 :     (if noindent (current-column) (+ sym-indent indent-after)))
609 :     ;; default-default
610 :     (let* ((prec-after (sml-op-prec sym-after 'back))
611 :     (prec (or (sml-op-prec sym-before 'back) prec-after 100)))
612 :     ;; go back until you hit a symbol that has a lower prec than the
613 :     ;; "current one", or until you backed over a sym that has the same prec
614 :     ;; but is at the beginning of a line.
615 :     (while (and (not (sml-bolp))
616 :     (sml-move-if (sml-backward-sexp (1- prec)))
617 :     (not (sml-bolp)))
618 :     (while (sml-move-if (sml-backward-sexp prec))))
619 :     ;; the `noindent' case does back over an introductory symbol
620 :     ;; such as `fun', ...
621 :     (when noindent
622 :     (sml-move-if
623 :     (sml-backward-spaces)
624 :     (string-match sml-starters-re (or (sml-backward-sym) ""))))
625 :     (current-column)))))
626 : monnier 32
627 : monnier 319
628 :     (defun sml-bolp ()
629 : monnier 32 (save-excursion
630 : monnier 319 (skip-chars-backward " \t|") (bolp)))
631 : monnier 32
632 :    
633 : monnier 319 ;; maybe `|' should be set to word-syntax in our temp syntax table ?
634 :     (defun sml-current-indentation ()
635 : monnier 32 (save-excursion
636 : monnier 319 (beginning-of-line)
637 :     (skip-chars-forward " \t|")
638 :     (current-column)))
639 : monnier 32
640 : monnier 300
641 : monnier 319 (defun sml-find-matching-starter (regexp &optional prec)
642 : monnier 334 (ignore-errors
643 :     (sml-backward-sexp prec)
644 :     (while (not (or (looking-at regexp) (bobp)))
645 :     (sml-backward-sexp prec))
646 :     (not (bobp))))
647 : monnier 319
648 : monnier 32 (defun sml-comment-indent ()
649 :     (if (looking-at "^(\\*") ; Existing comment at beginning
650 :     0 ; of line stays there.
651 :     (save-excursion
652 :     (skip-chars-backward " \t")
653 :     (max (1+ (current-column)) ; Else indent at comment column
654 :     comment-column)))) ; except leave at least one space.
655 :    
656 :     ;;; INSERTING PROFORMAS (COMMON SML-FORMS)
657 :    
658 : monnier 334 (defvar sml-forms-alist nil
659 :     "*The alist of templates to auto-insert.
660 : monnier 32
661 :     You can extend this alist to your heart's content. For each additional
662 :     template NAME in the list, declare a keyboard macro or function (or
663 :     interactive command) called 'sml-form-NAME'.
664 :    
665 :     If 'sml-form-NAME' is a function it takes no arguments and should
666 :     insert the template at point\; if this is a command it may accept any
667 :     sensible interactive call arguments\; keyboard macros can't take
668 :     arguments at all. Apropos keyboard macros, see `name-last-kbd-macro'
669 :     and `sml-addto-forms-alist'.
670 :    
671 :     `sml-forms-alist' understands let, local, case, abstype, datatype,
672 :     signature, structure, and functor by default.")
673 :    
674 : monnier 334 (defmacro sml-def-skeleton (name interactor &rest elements)
675 :     (let ((fsym (intern (concat "sml-form-" name))))
676 :     `(progn
677 :     (add-to-list 'sml-forms-alist ',(cons name fsym))
678 :     (define-skeleton ,fsym
679 :     ,(format "SML-mode skeleton for `%s..' expressions" name)
680 :     ,interactor
681 : monnier 341 ,(concat name " ") >
682 : monnier 334 ,@elements))))
683 :     (put 'sml-def-skeleton 'lisp-indent-function 2)
684 : monnier 32
685 : monnier 334 (sml-def-skeleton "let" nil
686 :     _ "\nin" > "\nend" >)
687 : monnier 32
688 : monnier 334 (sml-def-skeleton "if" nil
689 :     _ " then " > "\nelse " >)
690 : monnier 32
691 : monnier 334 (sml-def-skeleton "local" nil
692 :     _ "\nin" > "\nend" >)
693 : monnier 32
694 : monnier 334 (sml-def-skeleton "case" "Case expr: "
695 : monnier 341 str "\nof " > _ " => ")
696 : monnier 32
697 : monnier 334 (sml-def-skeleton "signature" "Signature name: "
698 :     str " =\nsig" > "\n" > _ "\nend" >)
699 : monnier 32
700 : monnier 334 (sml-def-skeleton "structure" "Structure name: "
701 :     str " =\nstruct" > "\n" > _ "\nend" >)
702 : monnier 32
703 : monnier 334 (sml-def-skeleton "functor" "Functor name: "
704 :     str " () : =\nstruct" > "\n" > _ "\nend" >)
705 :    
706 :     (sml-def-skeleton "datatype" "Datatype name and type parameters: "
707 :     str " =" \n)
708 :    
709 :     (sml-def-skeleton "abstype" "Abstype name and type parameters: "
710 :     str " =" \n _ "\nwith" > "\nend" >)
711 :    
712 :     ;;
713 :    
714 :     (defun sml-forms-menu (menu)
715 :     (easy-menu-filter-return
716 :     (easy-menu-create-menu "Forms"
717 :     (mapcar (lambda (x)
718 :     (let ((name (car x))
719 :     (fsym (cdr x)))
720 :     (vector name fsym t)))
721 :     sml-forms-alist))))
722 :    
723 :     (defvar sml-last-form "let")
724 :    
725 : monnier 341 (defun sml-electric-space ()
726 :     "Expand a symbol into an SML form, or just insert a space.
727 :     If the point directly precedes a symbol for which an SML form exists,
728 :     the corresponding form is inserted."
729 :     (interactive)
730 :     (let* ((point (point))
731 :     (sym (sml-backward-sym)))
732 :     (if (not (and sym (assoc sym sml-forms-alist)))
733 :     (progn (goto-char point) (insert " "))
734 :     (delete-region (point) point)
735 :     (sml-insert-form sym nil))))
736 :    
737 : monnier 334 (defun sml-insert-form (name newline)
738 : monnier 32 "Interactive short-cut to insert a common ML form.
739 :     If a perfix argument is given insert a newline and indent first, or
740 :     just move to the proper indentation if the line is blank\; otherwise
741 :     insert at point (which forces indentation to current column).
742 :    
743 :     The default form to insert is 'whatever you inserted last time'
744 :     \(just hit return when prompted\)\; otherwise the command reads with
745 :     completion from `sml-forms-alist'."
746 : monnier 334 (interactive
747 :     (list (completing-read
748 :     (format "Form to insert: (default %s) " sml-last-form)
749 :     sml-forms-alist nil t nil)
750 :     current-prefix-arg))
751 :     ;; default is whatever the last insert was...
752 :     (if (string= name "") (setq name sml-last-form) (setq sml-last-form name))
753 :     (unless (or (not newline)
754 :     (save-excursion (beginning-of-line) (looking-at "\\s-*$")))
755 :     (insert "\n"))
756 : monnier 341 (unless (/= ?w (char-syntax (char-before))) (insert " "))
757 : monnier 334 (let ((f (cdr (assoc name sml-forms-alist))))
758 :     (cond
759 :     ((commandp f) (command-execute f))
760 :     (f (funcall f))
761 :     (t (error "Undefined form: %s" name)))))
762 : monnier 32
763 : monnier 334 ;; See also macros.el in emacs lisp dir.
764 : monnier 32
765 : monnier 334 (defun sml-addto-forms-alist (name)
766 :     "Assign a name to the last keyboard macro defined.
767 :     Argument NAME is transmogrified to sml-form-NAME which is the symbol
768 :     actually defined.
769 : monnier 32
770 : monnier 334 The symbol's function definition becomes the keyboard macro string.
771 : monnier 32
772 : monnier 334 If that works, NAME is added to `sml-forms-alist' so you'll be able to
773 :     reinvoke the macro through \\[sml-insert-form]. You might want to save
774 :     the macro to use in a later editing session -- see `insert-kbd-macro'
775 :     and add these macros to your .emacs file.
776 : monnier 32
777 : monnier 334 See also `edit-kbd-macro' which is bound to \\[edit-kbd-macro]."
778 :     (interactive "sName for last kbd macro (\"sml-form-\" will be added): ")
779 :     (when (string= name "") (error "No command name given"))
780 :     (let ((fsym (intern (concat "sml-form-" name))))
781 :     (name-last-kbd-macro fsym)
782 :     (message "Macro bound to %s" fsym)
783 :     (add-to-list 'sml-forms-alist (cons name fsym))))
784 : monnier 32
785 : monnier 334 ;; at a pinch these could be added to SML/Forms menu through the good
786 :     ;; offices of activate-menubar-hook or something... but documentation
787 :     ;; of this and/or menu-bar-update-hook is sparse in 19.33. anyway, use
788 :     ;; completing read for sml-insert-form prompt...
789 : monnier 32
790 :     ;;; & do the user's customisation
791 :     (run-hooks 'sml-load-hook)
792 :    
793 :     ;;; sml-mode.el has just finished.
794 : monnier 319 (provide 'sml-mode)

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