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

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

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