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

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

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