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-mode/trunk/sml-mode.el
ViewVC logotype

Annotation of /sml-mode/trunk/sml-mode.el

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2725 - (view) (download)

1 : monnier 535 ;;; sml-mode.el --- Major mode for editing (Standard) ML
2 : monnier 32
3 : monnier 2646 ;; Copyright (C) 1999,2000,2004,2007 Stefan Monnier
4 : monnier 1477 ;; Copyright (C) 1994-1997 Matthew J. Morley
5 : monnier 541 ;; Copyright (C) 1989 Lars Bo Nielsen
6 : monnier 319
7 : monnier 707 ;; Author: Lars Bo Nielsen
8 :     ;; Olin Shivers
9 :     ;; Fritz Knabe (?)
10 :     ;; Steven Gilmore (?)
11 :     ;; Matthew Morley <mjm@scs.leeds.ac.uk> (aka <matthew@verisity.com>)
12 :     ;; Matthias Blume <blume@cs.princeton.edu> (aka <blume@kurims.kyoto-u.ac.jp>)
13 :     ;; (Stefan Monnier) monnier@cs.yale.edu
14 :     ;; Maintainer: (Stefan Monnier) monnier+lists/emacs/sml@flint.cs.yale.edu
15 :     ;; Keywords: SML
16 : monnier 32 ;; $Revision$
17 :     ;; $Date$
18 :    
19 :     ;; This file is not part of GNU Emacs, but it is distributed under the
20 :     ;; same conditions.
21 :    
22 :     ;; This program is free software; you can redistribute it and/or
23 :     ;; modify it under the terms of the GNU General Public License as
24 :     ;; published by the Free Software Foundation; either version 2, or (at
25 :     ;; your option) any later version.
26 :    
27 :     ;; This program is distributed in the hope that it will be useful, but
28 :     ;; WITHOUT ANY WARRANTY; without even the implied warranty of
29 :     ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30 :     ;; General Public License for more details.
31 :    
32 :     ;; You should have received a copy of the GNU General Public License
33 :     ;; along with GNU Emacs; see the file COPYING. If not, write to the
34 :     ;; Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
35 :    
36 : monnier 535 ;;; Commentary:
37 :    
38 :     ;;; HISTORY
39 :    
40 : monnier 32 ;; Still under construction: History obscure, needs a biographer as
41 :     ;; well as a M-x doctor. Change Log on request.
42 :    
43 :     ;; Hacked by Olin Shivers for comint from Lars Bo Nielsen's sml.el.
44 :    
45 :     ;; Hacked by Matthew Morley to incorporate Fritz Knabe's hilite and
46 :     ;; font-lock patterns, some of Steven Gilmore's (reduced) easy-menus,
47 :     ;; and numerous bugs and bug-fixes.
48 :    
49 : monnier 535 ;;; DESCRIPTION
50 : monnier 32
51 :     ;; See accompanying info file: sml-mode.info
52 :    
53 :     ;;; FOR YOUR .EMACS FILE
54 :    
55 : monnier 535 ;; If sml-mode.el lives in some non-standard directory, you must tell
56 : monnier 32 ;; emacs where to get it. This may or may not be necessary:
57 :    
58 : monnier 707 ;; (add-to-list 'load-path "~jones/lib/emacs/")
59 : monnier 32
60 :     ;; Then to access the commands autoload sml-mode with that command:
61 :    
62 : monnier 707 ;; (load "sml-mode-startup")
63 : monnier 32
64 :     ;; sml-mode-hook is run whenever a new sml-mode buffer is created.
65 :    
66 :     ;; Finally, there are inferior-sml-{mode,load}-hooks -- see comments
67 :     ;; in sml-proc.el. For much more information consult the mode's *info*
68 :     ;; tree.
69 :    
70 : monnier 535 ;;; Code:
71 : monnier 32
72 : monnier 541 (eval-when-compile (require 'cl))
73 : monnier 319 (require 'sml-util)
74 :     (require 'sml-move)
75 :     (require 'sml-defs)
76 : monnier 768 (condition-case nil (require 'skeleton) (error nil))
77 : monnier 32
78 :     ;;; VARIABLES CONTROLLING INDENTATION
79 :    
80 : monnier 394 (defcustom sml-indent-level 4
81 : monnier 2646 "Indentation of blocks in ML (see also `sml-indent-rule')."
82 : monnier 394 :group 'sml
83 :     :type '(integer))
84 : monnier 32
85 : monnier 394 (defcustom sml-indent-args sml-indent-level
86 :     "*Indentation of args placed on a separate line."
87 :     :group 'sml
88 :     :type '(integer))
89 : monnier 39
90 : monnier 341 ;; (defvar sml-indent-align-args t
91 :     ;; "*Whether the arguments should be aligned.")
92 : monnier 39
93 : monnier 341 ;; (defvar sml-case-indent nil
94 :     ;; "*How to indent case-of expressions.
95 :     ;; If t: case expr If nil: case expr of
96 :     ;; of exp1 => ... exp1 => ...
97 :     ;; | exp2 => ... | exp2 => ...
98 : monnier 334
99 : monnier 341 ;; The first seems to be the standard in SML/NJ, but the second
100 :     ;; seems nicer...")
101 : monnier 32
102 : monnier 394 (defcustom sml-electric-semi-mode nil
103 :     "*If non-nil, `\;' will self insert, reindent the line, and do a newline.
104 : monnier 535 If nil, just insert a `\;'. (To insert while t, do: \\[quoted-insert] \;)."
105 : monnier 394 :group 'sml
106 : monnier 892 :type 'boolean)
107 : monnier 32
108 : monnier 892 (defcustom sml-rightalign-and t
109 :     "If non-nil, right-align `and' with its leader.
110 :     If nil: If t:
111 :     datatype a = A datatype a = A
112 :     and b = B and b = B"
113 :     :group 'sml
114 :     :type 'boolean)
115 :    
116 : monnier 32 ;;; OTHER GENERIC MODE VARIABLES
117 :    
118 :     (defvar sml-mode-info "sml-mode"
119 : monnier 535 "*Where to find Info file for `sml-mode'.
120 : monnier 32 The default assumes the info file \"sml-mode.info\" is on Emacs' info
121 : monnier 535 directory path. If it is not, either put the file on the standard path
122 :     or set the variable `sml-mode-info' to the exact location of this file
123 : monnier 32
124 : monnier 535 (setq sml-mode-info \"/usr/me/lib/info/sml-mode\")
125 : monnier 32
126 :     in your .emacs file. You can always set it interactively with the
127 :     set-variable command.")
128 :    
129 :     (defvar sml-mode-hook nil
130 : monnier 535 "*Run upon entering `sml-mode'.
131 : monnier 32 This is a good place to put your preferred key bindings.")
132 :    
133 : monnier 535 ;;; CODE FOR SML-MODE
134 : monnier 32
135 :     (defun sml-mode-info ()
136 : monnier 535 "Command to access the TeXinfo documentation for `sml-mode'.
137 :     See doc for the variable `sml-mode-info'."
138 : monnier 32 (interactive)
139 :     (require 'info)
140 :     (condition-case nil
141 : monnier 535 (info sml-mode-info)
142 : monnier 32 (error (progn
143 :     (describe-variable 'sml-mode-info)
144 :     (message "Can't find it... set this variable first!")))))
145 :    
146 :    
147 :     ;;; Autoload functions -- no-doc is another idea cribbed from AucTeX!
148 :    
149 : monnier 319 (let ((sml-no-doc
150 :     "This function is part of sml-proc, and has not yet been loaded.
151 :     Full documentation will be available after autoloading the function."))
152 : monnier 32
153 : monnier 332 (autoload 'sml-compile "sml-proc" sml-no-doc t)
154 :     (autoload 'sml-load-file "sml-proc" sml-no-doc t)
155 :     (autoload 'switch-to-sml "sml-proc" sml-no-doc t)
156 :     (autoload 'sml-send-region "sml-proc" sml-no-doc t)
157 :     (autoload 'sml-send-buffer "sml-proc" sml-no-doc t))
158 : monnier 32
159 : monnier 33 ;; font-lock setup
160 :    
161 : monnier 39 (defconst sml-keywords-regexp
162 : monnier 319 (sml-syms-re "abstraction" "abstype" "and" "andalso" "as" "before" "case"
163 :     "datatype" "else" "end" "eqtype" "exception" "do" "fn"
164 :     "fun" "functor" "handle" "if" "in" "include" "infix"
165 :     "infixr" "let" "local" "nonfix" "of" "op" "open" "orelse"
166 :     "overload" "raise" "rec" "sharing" "sig" "signature"
167 :     "struct" "structure" "then" "type" "val" "where" "while"
168 : monnier 535 "with" "withtype" "o")
169 : monnier 39 "A regexp that matches any and all keywords of SML.")
170 :    
171 : monnier 892 (defconst sml-tyvarseq-re
172 : monnier 931 "\\(\\('+\\(\\sw\\|\\s_\\)+\\|(\\([,']\\|\\sw\\|\\s_\\|\\s-\\)+)\\)\\s-+\\)?")
173 : monnier 892
174 : monnier 1696 ;;; Font-lock settings ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
175 :    
176 :     (defcustom sml-font-lock-symbols nil
177 :     "Display \\ and -> and such using symbols in fonts.
178 :     This may sound like a neat trick, but be extra careful: it changes the
179 :     alignment and can thus lead to nasty surprises w.r.t layout.
180 :     If t, try to use whichever font is available. Otherwise you can
181 :     set it to a particular font of your preference among `japanese-jisx0208'
182 :     and `unicode'."
183 :     :type '(choice (const nil)
184 :     (const t)
185 :     (const unicode)
186 :     (const japanese-jisx0208)))
187 :    
188 :     (defconst sml-font-lock-symbols-alist
189 :     (append
190 :     ;; The symbols can come from a JIS0208 font.
191 :     (and (fboundp 'make-char) (charsetp 'japanese-jisx0208)
192 :     (memq sml-font-lock-symbols '(t japanese-jisx0208))
193 : monnier 1698 (list (cons "fn" (make-char 'japanese-jisx0208 38 75))
194 : monnier 1711 (cons "andalso" (make-char 'japanese-jisx0208 34 74))
195 :     (cons "orelse" (make-char 'japanese-jisx0208 34 75))
196 :     ;; (cons "as" (make-char 'japanese-jisx0208 34 97))
197 :     (cons "not" (make-char 'japanese-jisx0208 34 76))
198 :     (cons "div" (make-char 'japanese-jisx0208 33 96))
199 :     ;; (cons "*" (make-char 'japanese-jisx0208 33 95))
200 : monnier 1696 (cons "->" (make-char 'japanese-jisx0208 34 42))
201 : monnier 1698 (cons "=>" (make-char 'japanese-jisx0208 34 77))
202 :     (cons "<-" (make-char 'japanese-jisx0208 34 43))
203 : monnier 1711 (cons "<>" (make-char 'japanese-jisx0208 33 98))
204 : monnier 1697 (cons ">=" (make-char 'japanese-jisx0208 33 102))
205 :     (cons "<=" (make-char 'japanese-jisx0208 33 101))
206 : monnier 1711 (cons "..." (make-char 'japanese-jisx0208 33 68))
207 : monnier 1698 ;; Some greek letters for type parameters.
208 :     (cons "'a" (make-char 'japanese-jisx0208 38 65))
209 :     (cons "'b" (make-char 'japanese-jisx0208 38 66))
210 :     (cons "'c" (make-char 'japanese-jisx0208 38 67))
211 :     (cons "'d" (make-char 'japanese-jisx0208 38 68))
212 :     ))
213 : monnier 1696 ;; Or a unicode font.
214 :     (and (fboundp 'decode-char)
215 :     (memq sml-font-lock-symbols '(t unicode))
216 : monnier 1698 (list (cons "fn" (decode-char 'ucs 955))
217 : monnier 1711 (cons "andalso" (decode-char 'ucs 8896))
218 :     (cons "orelse" (decode-char 'ucs 8897))
219 :     ;; (cons "as" (decode-char 'ucs 8801))
220 :     (cons "not" (decode-char 'ucs 160))
221 :     (cons "div" (decode-char 'ucs 247))
222 :     (cons "*" (decode-char 'ucs 215))
223 : monnier 1698 (cons "o" (decode-char 'ucs 9675))
224 : monnier 1696 (cons "->" (decode-char 'ucs 8594))
225 : monnier 1698 (cons "=>" (decode-char 'ucs 8658))
226 :     (cons "<-" (decode-char 'ucs 8592))
227 : monnier 1711 (cons "<>" (decode-char 'ucs 8800))
228 : monnier 1697 (cons ">=" (decode-char 'ucs 8805))
229 :     (cons "<=" (decode-char 'ucs 8804))
230 : monnier 1711 (cons "..." (decode-char 'ucs 8943))
231 : monnier 1696 ;; (cons "::" (decode-char 'ucs 8759))
232 : monnier 1698 ;; Some greek letters for type parameters.
233 :     (cons "'a" (decode-char 'ucs 945))
234 :     (cons "'b" (decode-char 'ucs 946))
235 :     (cons "'c" (decode-char 'ucs 947))
236 :     (cons "'d" (decode-char 'ucs 948))
237 : monnier 1696 ))))
238 :    
239 :     (defun sml-font-lock-compose-symbol (alist)
240 :     "Compose a sequence of ascii chars into a symbol.
241 :     Regexp match data 0 points to the chars."
242 :     ;; Check that the chars should really be composed into a symbol.
243 : monnier 1698 (let* ((start (match-beginning 0))
244 :     (end (match-end 0))
245 :     (syntaxes (if (eq (char-syntax (char-after start)) ?w)
246 :     '(?w) '(?. ?\\))))
247 :     (if (or (memq (char-syntax (or (char-before start) ?\ )) syntaxes)
248 :     (memq (char-syntax (or (char-after end) ?\ )) syntaxes)
249 : monnier 1696 (memq (get-text-property start 'face)
250 :     '(font-lock-doc-face font-lock-string-face
251 :     font-lock-comment-face)))
252 :     ;; No composition for you. Let's actually remove any composition
253 :     ;; we may have added earlier and which is now incorrect.
254 :     (remove-text-properties start end '(composition))
255 :     ;; That's a symbol alright, so add the composition.
256 :     (compose-region start end (cdr (assoc (match-string 0) alist)))))
257 :     ;; Return nil because we're not adding any face property.
258 :     nil)
259 :    
260 :     (defun sml-font-lock-symbols-keywords ()
261 :     (when (fboundp 'compose-region)
262 :     (let ((alist nil))
263 :     (dolist (x sml-font-lock-symbols-alist)
264 :     (when (and (if (fboundp 'char-displayable-p)
265 :     (char-displayable-p (cdr x))
266 :     t)
267 :     (not (assoc (car x) alist))) ;Not yet in alist.
268 :     (push x alist)))
269 :     (when alist
270 :     `((,(regexp-opt (mapcar 'car alist) t)
271 :     (0 (sml-font-lock-compose-symbol ',alist))))))))
272 :    
273 :     ;; The font lock regular expressions.
274 :    
275 : monnier 300 (defconst sml-font-lock-keywords
276 :     `(;;(sml-font-comments-and-strings)
277 : monnier 892 (,(concat "\\<\\(fun\\|and\\)\\s-+" sml-tyvarseq-re "\\(\\sw+\\)\\s-+[^ \t\n=]")
278 : monnier 33 (1 font-lock-keyword-face)
279 : monnier 892 (6 font-lock-function-name-face))
280 :     (,(concat "\\<\\(\\(data\\|abs\\|with\\|eq\\)?type\\)\\s-+" sml-tyvarseq-re "\\(\\sw+\\)")
281 : monnier 33 (1 font-lock-keyword-face)
282 : monnier 892 (7 font-lock-type-def-face))
283 : monnier 544 ("\\<\\(val\\)\\s-+\\(\\sw+\\>\\s-*\\)?\\(\\sw+\\)\\s-*[=:]"
284 : monnier 33 (1 font-lock-keyword-face)
285 :     ;;(6 font-lock-variable-def-face nil t)
286 : monnier 394 (3 font-lock-variable-name-face))
287 : monnier 319 ("\\<\\(structure\\|functor\\|abstraction\\)\\s-+\\(\\sw+\\)"
288 : monnier 33 (1 font-lock-keyword-face)
289 :     (2 font-lock-module-def-face))
290 : monnier 319 ("\\<\\(signature\\)\\s-+\\(\\sw+\\)"
291 : monnier 33 (1 font-lock-keyword-face)
292 :     (2 font-lock-interface-def-face))
293 :    
294 : monnier 1696 (,sml-keywords-regexp . font-lock-keyword-face)
295 :     ,@(sml-font-lock-symbols-keywords))
296 : monnier 33 "Regexps matching standard SML keywords.")
297 :    
298 : monnier 394 (defface font-lock-type-def-face
299 :     '((t (:bold t)))
300 :     "Font Lock mode face used to highlight type definitions."
301 :     :group 'font-lock-highlighting-faces)
302 :     (defvar font-lock-type-def-face 'font-lock-type-def-face
303 :     "Face name to use for type definitions.")
304 : monnier 33
305 : monnier 394 (defface font-lock-module-def-face
306 :     '((t (:bold t)))
307 :     "Font Lock mode face used to highlight module definitions."
308 :     :group 'font-lock-highlighting-faces)
309 :     (defvar font-lock-module-def-face 'font-lock-module-def-face
310 :     "Face name to use for module definitions.")
311 :    
312 :     (defface font-lock-interface-def-face
313 :     '((t (:bold t)))
314 :     "Font Lock mode face used to highlight interface definitions."
315 :     :group 'font-lock-highlighting-faces)
316 :     (defvar font-lock-interface-def-face 'font-lock-interface-def-face
317 :     "Face name to use for interface definitions.")
318 :    
319 : monnier 892 ;;
320 :     ;; Code to handle nested comments and unusual string escape sequences
321 :     ;;
322 : monnier 33
323 : monnier 535 (defsyntax sml-syntax-prop-table
324 :     '((?\\ . ".") (?* . "."))
325 :     "Syntax table for text-properties")
326 :    
327 :     ;; For Emacsen that have no built-in support for nested comments
328 : monnier 300 (defun sml-get-depth-st ()
329 :     (save-excursion
330 :     (let* ((disp (if (eq (char-before) ?\)) (progn (backward-char) -1) nil))
331 : monnier 1692 (_ (backward-char))
332 : monnier 300 (disp (if (eq (char-before) ?\() (progn (backward-char) 0) disp))
333 :     (pt (point)))
334 :     (when disp
335 :     (let* ((depth
336 :     (save-match-data
337 :     (if (re-search-backward "\\*)\\|(\\*" nil t)
338 :     (+ (or (get-char-property (point) 'comment-depth) 0)
339 :     (case (char-after) (?\( 1) (?* 0))
340 :     disp)
341 :     0)))
342 :     (depth (if (> depth 0) depth)))
343 :     (put-text-property pt (1+ pt) 'comment-depth depth)
344 : monnier 319 (when depth sml-syntax-prop-table))))))
345 : monnier 300
346 :     (defconst sml-font-lock-syntactic-keywords
347 : monnier 535 `(("^\\s-*\\(\\\\\\)" (1 ',sml-syntax-prop-table))
348 :     ,@(unless sml-builtin-nested-comments-flag
349 :     '(("(?\\(\\*\\))?" (1 (sml-get-depth-st)))))))
350 : monnier 300
351 :     (defconst sml-font-lock-defaults
352 : monnier 319 '(sml-font-lock-keywords nil nil ((?_ . "w") (?' . "w")) nil
353 : monnier 707 (font-lock-syntactic-keywords . sml-font-lock-syntactic-keywords)))
354 : monnier 300
355 : monnier 707 ;;;;
356 :     ;;;; Imenu support
357 :     ;;;;
358 : monnier 33
359 : monnier 707 (defvar sml-imenu-regexp
360 :     (concat "^[ \t]*\\(let[ \t]+\\)?"
361 :     (regexp-opt (append sml-module-head-syms
362 :     '("and" "fun" "datatype" "abstype" "type")) t)
363 :     "\\>"))
364 :    
365 :     (defun sml-imenu-create-index ()
366 :     (let (alist)
367 :     (goto-char (point-max))
368 :     (while (re-search-backward sml-imenu-regexp nil t)
369 :     (save-excursion
370 :     (let ((kind (match-string 2))
371 :     (column (progn (goto-char (match-beginning 2)) (current-column)))
372 :     (location
373 : monnier 892 (progn (goto-char (match-end 0))
374 :     (sml-forward-spaces)
375 :     (when (looking-at sml-tyvarseq-re)
376 :     (goto-char (match-end 0)))
377 :     (point)))
378 : monnier 707 (name (sml-forward-sym)))
379 :     ;; Eliminate trivial renamings.
380 :     (when (or (not (member kind '("structure" "signature")))
381 :     (progn (search-forward "=")
382 :     (sml-forward-spaces)
383 :     (looking-at "sig\\|struct")))
384 :     (push (cons (concat (make-string (/ column 2) ?\ ) name) location)
385 :     alist)))))
386 :     alist))
387 :    
388 : monnier 32 ;;; MORE CODE FOR SML-MODE
389 :    
390 : monnier 1477 ;;;###autoload (add-to-list 'load-path (file-name-directory load-file-name))
391 :     ;;;###autoload
392 : monnier 535 (add-to-list 'auto-mode-alist '("\\.s\\(ml\\|ig\\)\\'" . sml-mode))
393 : monnier 32
394 : monnier 1477 ;;;###autoload
395 : monnier 535 (define-derived-mode sml-mode fundamental-mode "SML"
396 :     "\\<sml-mode-map>Major mode for editing ML code.
397 :     This mode runs `sml-mode-hook' just before exiting.
398 : monnier 32 \\{sml-mode-map}"
399 : monnier 535 (set (make-local-variable 'font-lock-defaults) sml-font-lock-defaults)
400 : monnier 319 (set (make-local-variable 'outline-regexp) sml-outline-regexp)
401 : monnier 707 (set (make-local-variable 'imenu-create-index-function)
402 :     'sml-imenu-create-index)
403 : monnier 700 (set (make-local-variable 'add-log-current-defun-function)
404 :     'sml-current-fun-name)
405 : monnier 1472 ;; Treat paragraph-separators in comments as paragraph-separators.
406 :     (set (make-local-variable 'paragraph-separate)
407 :     (concat "\\([ \t]*\\*)?\\)?\\(" paragraph-separate "\\)"))
408 :     (set (make-local-variable 'require-final-newline) t)
409 : monnier 700 ;; forward-sexp-function is an experimental variable in my hacked Emacs.
410 :     (set (make-local-variable 'forward-sexp-function) 'sml-user-forward-sexp)
411 : monnier 768 ;; For XEmacs
412 :     (easy-menu-add sml-mode-menu)
413 : monnier 1691 ;; Compatibility. FIXME: we should use `-' in Emacs-CVS.
414 : monnier 768 (unless (boundp 'skeleton-positions) (set (make-local-variable '@) nil))
415 : monnier 535 (sml-mode-variables))
416 : monnier 32
417 :     (defun sml-mode-variables ()
418 :     (set-syntax-table sml-mode-syntax-table)
419 :     (setq local-abbrev-table sml-mode-abbrev-table)
420 : monnier 33 (set (make-local-variable 'indent-line-function) 'sml-indent-line)
421 :     (set (make-local-variable 'comment-start) "(* ")
422 :     (set (make-local-variable 'comment-end) " *)")
423 : monnier 2722 (set (make-local-variable 'comment-start-skip) "(\\*+\\s-*")
424 :     (set (make-local-variable 'comment-end-skip) "\\s-*\\*+)")
425 :     ;; No need to quote nested comments markers.
426 :     (set (make-local-variable 'comment-quote-nested) nil))
427 : monnier 32
428 : monnier 892 (defun sml-funname-of-and ()
429 :     "Name of the function this `and' defines, or nil if not a function.
430 :     Point has to be right after the `and' symbol and is not preserved."
431 :     (sml-forward-spaces)
432 :     (if (looking-at sml-tyvarseq-re) (goto-char (match-end 0)))
433 :     (let ((sym (sml-forward-sym)))
434 :     (sml-forward-spaces)
435 :     (unless (or (member sym '(nil "d="))
436 :     (member (sml-forward-sym) '("d=")))
437 :     sym)))
438 :    
439 : monnier 32 (defun sml-electric-pipe ()
440 : monnier 319 "Insert a \"|\".
441 : monnier 32 Depending on the context insert the name of function, a \"=>\" etc."
442 :     (interactive)
443 : monnier 319 (sml-with-ist
444 : monnier 334 (unless (save-excursion (skip-chars-backward "\t ") (bolp)) (insert "\n"))
445 :     (insert "| ")
446 : monnier 319 (let ((text
447 :     (save-excursion
448 : monnier 334 (backward-char 2) ;back over the just inserted "| "
449 : monnier 544 (let ((sym (sml-find-matching-starter sml-pipeheads
450 :     (sml-op-prec "|" 'back))))
451 :     (sml-forward-sym)
452 : monnier 334 (sml-forward-spaces)
453 :     (cond
454 :     ((string= sym "|")
455 :     (let ((f (sml-forward-sym)))
456 :     (sml-find-forward "\\(=>\\|=\\||\\)\\S.")
457 :     (cond
458 :     ((looking-at "|") "") ;probably a datatype
459 :     ((looking-at "=>") " => ") ;`case', or `fn' or `handle'
460 :     ((looking-at "=") (concat f " = "))))) ;a function
461 :     ((string= sym "and")
462 :     ;; could be a datatype or a function
463 : monnier 892 (setq sym (sml-funname-of-and))
464 :     (if sym (concat sym " = ") ""))
465 : monnier 334 ;; trivial cases
466 :     ((string= sym "fun")
467 :     (while (and (setq sym (sml-forward-sym))
468 :     (string-match "^'" sym))
469 :     (sml-forward-spaces))
470 :     (concat sym " = "))
471 : monnier 542 ((member sym '("case" "handle" "fn" "of")) " => ")
472 : monnier 544 ;;((member sym '("abstype" "datatype")) "")
473 :     (t ""))))))
474 : monnier 32
475 : monnier 334 (insert text)
476 : monnier 535 (indent-according-to-mode)
477 : monnier 319 (beginning-of-line)
478 :     (skip-chars-forward "\t |")
479 :     (skip-syntax-forward "w")
480 :     (skip-chars-forward "\t ")
481 : monnier 1472 (when (eq ?= (char-after)) (backward-char)))))
482 : monnier 319
483 : monnier 32 (defun sml-electric-semi ()
484 : monnier 535 "Insert a \;.
485 :     If variable `sml-electric-semi-mode' is t, indent the current line, insert
486 : monnier 32 a newline, and indent."
487 :     (interactive)
488 :     (insert "\;")
489 :     (if sml-electric-semi-mode
490 :     (reindent-then-newline-and-indent)))
491 :    
492 :     ;;; INDENTATION !!!
493 :    
494 :     (defun sml-mark-function ()
495 : monnier 535 "Synonym for `mark-paragraph' -- sorry.
496 : monnier 32 If anyone has a good algorithm for this..."
497 :     (interactive)
498 :     (mark-paragraph))
499 :    
500 :     (defun sml-indent-line ()
501 :     "Indent current line of ML code."
502 :     (interactive)
503 : monnier 544 (let ((savep (> (current-column) (current-indentation)))
504 : monnier 700 (indent (max (or (ignore-errors (sml-calculate-indentation)) 0) 0)))
505 : monnier 544 (if savep
506 :     (save-excursion (indent-line-to indent))
507 :     (indent-line-to indent))))
508 : monnier 32
509 :     (defun sml-back-to-outer-indent ()
510 :     "Unindents to the next outer level of indentation."
511 :     (interactive)
512 :     (save-excursion
513 :     (beginning-of-line)
514 :     (skip-chars-forward "\t ")
515 :     (let ((start-column (current-column))
516 :     (indent (current-column)))
517 :     (if (> start-column 0)
518 :     (progn
519 :     (save-excursion
520 :     (while (>= indent start-column)
521 :     (if (re-search-backward "^[^\n]" nil t)
522 :     (setq indent (current-indentation))
523 :     (setq indent 0))))
524 :     (backward-delete-char-untabify (- start-column indent)))))))
525 :    
526 : monnier 39 (defun sml-find-comment-indent ()
527 :     (save-excursion
528 :     (let ((depth 1))
529 :     (while (> depth 0)
530 :     (if (re-search-backward "(\\*\\|\\*)" nil t)
531 :     (cond
532 : monnier 892 ;; FIXME: That's just a stop-gap.
533 :     ((eq (get-text-property (point) 'face) 'font-lock-string-face))
534 : monnier 39 ((looking-at "*)") (incf depth))
535 : monnier 300 ((looking-at comment-start-skip) (decf depth)))
536 : monnier 39 (setq depth -1)))
537 :     (if (= depth 0)
538 : monnier 334 (1+ (current-column))
539 : monnier 39 nil))))
540 :    
541 : monnier 32 (defun sml-calculate-indentation ()
542 :     (save-excursion
543 : monnier 319 (beginning-of-line) (skip-chars-forward "\t ")
544 :     (sml-with-ist
545 : monnier 334 ;; Indentation for comments alone on a line, matches the
546 :     ;; proper indentation of the next line.
547 : monnier 378 (when (looking-at "(\\*") (sml-forward-spaces))
548 : monnier 334 (let (data
549 :     (sym (save-excursion (sml-forward-sym))))
550 : monnier 319 (or
551 : monnier 892 ;; Allow the user to override the indentation.
552 : monnier 378 (when (looking-at (concat ".*" (regexp-quote comment-start)
553 :     "[ \t]*fixindent[ \t]*"
554 :     (regexp-quote comment-end)))
555 :     (current-indentation))
556 : monnier 347
557 : monnier 892 ;; Continued comment.
558 : monnier 334 (and (looking-at "\\*") (sml-find-comment-indent))
559 : monnier 300
560 : monnier 319 ;; Continued string ? (Added 890113 lbn)
561 :     (and (looking-at "\\\\")
562 :     (save-excursion
563 :     (if (save-excursion (previous-line 1)
564 :     (beginning-of-line)
565 :     (looking-at "[\t ]*\\\\"))
566 :     (progn (previous-line 1) (current-indentation))
567 :     (if (re-search-backward "[^\\\\]\"" nil t)
568 : monnier 332 (1+ (current-column))
569 : monnier 319 0))))
570 : monnier 300
571 : monnier 892 ;; Closing parens. Could be handled below with `sml-indent-relative'?
572 :     (and (looking-at "\\s)")
573 :     (save-excursion
574 :     (skip-syntax-forward ")")
575 :     (backward-sexp 1)
576 :     (if (sml-dangling-sym)
577 :     (sml-indent-default 'noindent)
578 :     (current-column))))
579 :    
580 : monnier 334 (and (setq data (assoc sym sml-close-paren))
581 :     (sml-indent-relative sym data))
582 : monnier 32
583 : monnier 892 (and (member sym sml-starters-syms)
584 :     (sml-indent-starter sym))
585 : monnier 300
586 : monnier 334 (and (string= sym "|") (sml-indent-pipe))
587 : monnier 300
588 : monnier 319 (sml-indent-arg)
589 :     (sml-indent-default))))))
590 : monnier 300
591 : monnier 892 (defsubst sml-bolp ()
592 :     (save-excursion (skip-chars-backward " \t|") (bolp)))
593 :    
594 : monnier 2722 (defun sml-first-starter-p ()
595 :     "Non-nil if starter at point is immediately preceded by let/local/in/..."
596 :     (save-excursion
597 :     (let ((sym (unless (save-excursion (sml-backward-arg))
598 :     (sml-backward-spaces)
599 :     (sml-backward-sym))))
600 :     (if (member sym '(";" "d=")) (setq sym nil))
601 :     sym)))
602 :    
603 :    
604 : monnier 892 (defun sml-indent-starter (orig-sym)
605 :     "Return the indentation to use for a symbol in `sml-starters-syms'.
606 :     Point should be just before the symbol ORIG-SYM and is not preserved."
607 :     (let ((sym (unless (save-excursion (sml-backward-arg))
608 :     (sml-backward-spaces)
609 :     (sml-backward-sym))))
610 : monnier 1472 (if (member sym '(";" "d=")) (setq sym nil))
611 : monnier 892 (if sym (sml-get-sym-indent sym)
612 :     ;; FIXME: this can take a *long* time !!
613 :     (setq sym (sml-find-matching-starter sml-starters-syms))
614 : monnier 2722 (if (or (sml-first-starter-p)
615 :     ;; Don't align with `and' because it might be specially indented.
616 :     (and (or (equal orig-sym "and") (not (equal sym "and")))
617 :     (sml-bolp)))
618 : monnier 892 (+ (current-column)
619 :     (if (and sml-rightalign-and (equal orig-sym "and"))
620 :     (- (length sym) 3) 0))
621 :     (sml-indent-starter orig-sym)))))
622 :    
623 : monnier 334 (defun sml-indent-relative (sym data)
624 :     (save-excursion
625 :     (sml-forward-sym) (sml-backward-sexp nil)
626 : monnier 339 (unless (second data) (sml-backward-spaces) (sml-backward-sym))
627 : monnier 334 (+ (or (cdr (assoc sym sml-symbol-indent)) 0)
628 :     (sml-delegated-indent))))
629 :    
630 : monnier 319 (defun sml-indent-pipe ()
631 : monnier 544 (let ((sym (sml-find-matching-starter sml-pipeheads
632 :     (sml-op-prec "|" 'back))))
633 :     (when sym
634 :     (if (string= sym "|")
635 :     (if (sml-bolp) (current-column) (sml-indent-pipe))
636 :     (let ((pipe-indent (or (cdr (assoc "|" sml-symbol-indent)) -2)))
637 : monnier 892 (when (or (member sym '("datatype" "abstype"))
638 :     (and (equal sym "and")
639 :     (save-excursion
640 :     (forward-word 1)
641 :     (not (sml-funname-of-and)))))
642 : monnier 544 (re-search-forward "="))
643 :     (sml-forward-sym)
644 :     (sml-forward-spaces)
645 :     (+ pipe-indent (current-column)))))))
646 : monnier 39
647 : monnier 334 (defun sml-find-forward (re)
648 :     (sml-forward-spaces)
649 :     (while (and (not (looking-at re))
650 :     (progn
651 :     (or (ignore-errors (forward-sexp 1) t) (forward-char 1))
652 :     (sml-forward-spaces)
653 :     (not (looking-at re))))))
654 : monnier 39
655 : monnier 319 (defun sml-indent-arg ()
656 :     (and (save-excursion (ignore-errors (sml-forward-arg)))
657 :     ;;(not (looking-at sml-not-arg-re))
658 :     ;; looks like a function or an argument
659 :     (sml-move-if (sml-backward-arg))
660 :     ;; an argument
661 :     (if (save-excursion (not (sml-backward-arg)))
662 :     ;; a first argument
663 :     (+ (current-column) sml-indent-args)
664 :     ;; not a first arg
665 :     (while (and (/= (current-column) (current-indentation))
666 :     (sml-move-if (sml-backward-arg))))
667 :     (unless (save-excursion (sml-backward-arg))
668 :     ;; all earlier args are on the same line
669 :     (sml-forward-arg) (sml-forward-spaces))
670 :     (current-column))))
671 :    
672 : monnier 334 (defun sml-get-indent (data sym)
673 : monnier 1692 (let (d)
674 : monnier 334 (cond
675 :     ((not (listp data)) data)
676 : monnier 1692 ((setq d (member sym data)) (cadr d))
677 : monnier 334 ((and (consp data) (not (stringp (car data)))) (car data))
678 :     (t sml-indent-level))))
679 : monnier 332
680 : monnier 319 (defun sml-dangling-sym ()
681 : monnier 892 "Non-nil if the symbol after point is dangling.
682 :     The symbol can be an SML symbol or an open-paren. \"Dangling\" means that
683 :     it is not on its own line but is the last element on that line."
684 : monnier 32 (save-excursion
685 : monnier 319 (and (not (sml-bolp))
686 :     (< (sml-point-after (end-of-line))
687 : monnier 892 (sml-point-after (or (sml-forward-sym) (skip-syntax-forward "("))
688 : monnier 319 (sml-forward-spaces))))))
689 : monnier 32
690 : monnier 334 (defun sml-delegated-indent ()
691 :     (if (sml-dangling-sym)
692 :     (sml-indent-default 'noindent)
693 :     (sml-move-if (backward-word 1)
694 : monnier 341 (looking-at sml-agglomerate-re))
695 : monnier 334 (current-column)))
696 :    
697 : monnier 319 (defun sml-get-sym-indent (sym &optional style)
698 : monnier 535 "Find the indentation for the SYM we're `looking-at'.
699 : monnier 892 If indentation is delegated, point will move to the start of the parent.
700 :     Optional argument STYLE is currently ignored."
701 : monnier 334 (assert (equal sym (save-excursion (sml-forward-sym))))
702 :     (save-excursion
703 : monnier 1472 (let ((delegate (and (not (equal sym "end")) (assoc sym sml-close-paren)))
704 : monnier 334 (head-sym sym))
705 : monnier 339 (when (and delegate (not (eval (third delegate))))
706 : monnier 334 ;;(sml-find-match-backward sym delegate)
707 :     (sml-forward-sym) (sml-backward-sexp nil)
708 :     (setq head-sym
709 : monnier 339 (if (second delegate)
710 : monnier 334 (save-excursion (sml-forward-sym))
711 :     (sml-backward-spaces) (sml-backward-sym))))
712 : monnier 32
713 : monnier 334 (let ((idata (assoc head-sym sml-indent-rule)))
714 :     (when idata
715 :     ;;(if (or style (not delegate))
716 :     ;; normal indentation
717 : monnier 1692 (let ((indent (sml-get-indent (cdr idata) sym)))
718 : monnier 334 (when indent (+ (sml-delegated-indent) indent)))
719 :     ;; delgate indentation to the parent
720 :     ;;(sml-forward-sym) (sml-backward-sexp nil)
721 :     ;;(let* ((parent-sym (save-excursion (sml-forward-sym)))
722 :     ;; (parent-indent (cdr (assoc parent-sym sml-indent-starters))))
723 :     ;; check the special rules
724 :     ;;(+ (sml-delegated-indent)
725 : monnier 1692 ;; (or (sml-get-indent (cdr indent-data) 1 'strict)
726 :     ;; (sml-get-indent (cdr parent-indent) 1 'strict)
727 :     ;; (sml-get-indent (cdr indent-data) 0)
728 :     ;; (sml-get-indent (cdr parent-indent) 0))))))))
729 : monnier 334 )))))
730 :    
731 : monnier 319 (defun sml-indent-default (&optional noindent)
732 : monnier 333 (let* ((sym-after (save-excursion (sml-forward-sym)))
733 : monnier 319 (_ (sml-backward-spaces))
734 : monnier 333 (sym-before (sml-backward-sym))
735 : monnier 892 (sym-indent (and sym-before (sml-get-sym-indent sym-before)))
736 :     (indent-after (or (cdr (assoc sym-after sml-symbol-indent)) 0)))
737 :     (when (equal sym-before "end")
738 :     ;; I don't understand what's really happening here, but when
739 :     ;; it's `end' clearly, we need to do something special.
740 :     (forward-word 1)
741 :     (setq sym-before nil sym-indent nil))
742 :     (cond
743 :     (sym-indent
744 :     ;; the previous sym is an indentation introducer: follow the rule
745 :     (if noindent
746 :     ;;(current-column)
747 :     sym-indent
748 :     (+ sym-indent indent-after)))
749 :     ;; If we're just after a hanging open paren.
750 :     ((and (eq (char-syntax (preceding-char)) ?\()
751 :     (save-excursion (backward-char) (sml-dangling-sym)))
752 :     (backward-char)
753 :     (sml-indent-default))
754 :     (t
755 : monnier 339 ;; default-default
756 :     (let* ((prec-after (sml-op-prec sym-after 'back))
757 :     (prec (or (sml-op-prec sym-before 'back) prec-after 100)))
758 :     ;; go back until you hit a symbol that has a lower prec than the
759 :     ;; "current one", or until you backed over a sym that has the same prec
760 :     ;; but is at the beginning of a line.
761 :     (while (and (not (sml-bolp))
762 : monnier 892 (while (sml-move-if (sml-backward-sexp (1- prec))))
763 : monnier 339 (not (sml-bolp)))
764 :     (while (sml-move-if (sml-backward-sexp prec))))
765 : monnier 892 (if noindent
766 :     ;; the `noindent' case does back over an introductory symbol
767 :     ;; such as `fun', ...
768 :     (progn
769 :     (sml-move-if
770 :     (sml-backward-spaces)
771 :     (member (sml-backward-sym) sml-starters-syms))
772 :     (current-column))
773 :     ;; Use `indent-after' for cases such as when , or ; should be
774 :     ;; outdented so that their following terms are aligned.
775 :     (+ (if (progn
776 :     (if (equal sym-after ";")
777 :     (sml-move-if
778 :     (sml-backward-spaces)
779 :     (member (sml-backward-sym) sml-starters-syms)))
780 :     (and sym-after (not (looking-at sym-after))))
781 :     indent-after 0)
782 :     (current-column))))))))
783 : monnier 32
784 : monnier 319
785 :     ;; maybe `|' should be set to word-syntax in our temp syntax table ?
786 :     (defun sml-current-indentation ()
787 : monnier 32 (save-excursion
788 : monnier 319 (beginning-of-line)
789 :     (skip-chars-forward " \t|")
790 :     (current-column)))
791 : monnier 32
792 : monnier 300
793 : monnier 544 (defun sml-find-matching-starter (syms &optional prec)
794 :     (let (sym)
795 :     (ignore-errors
796 :     (while
797 :     (progn (sml-backward-sexp prec)
798 :     (setq sym (save-excursion (sml-forward-sym)))
799 :     (not (or (member sym syms) (bobp)))))
800 : monnier 1472 (if (member sym syms) sym))))
801 : monnier 319
802 : monnier 700 (defun sml-skip-siblings ()
803 :     (while (and (not (bobp)) (sml-backward-arg))
804 :     (sml-find-matching-starter sml-starters-syms))
805 :     (when (looking-at "in\\>\\|local\\>")
806 :     ;;skip over `local...in' and continue
807 :     (forward-word 1)
808 :     (sml-backward-sexp nil)
809 :     (sml-skip-siblings)))
810 :    
811 :     (defun sml-beginning-of-defun ()
812 :     (let ((sym (sml-find-matching-starter sml-starters-syms)))
813 : monnier 707 (if (member sym '("fun" "and" "functor" "signature" "structure"
814 : monnier 700 "abstraction" "datatype" "abstype"))
815 :     (save-excursion (sml-forward-sym) (sml-forward-spaces)
816 :     (sml-forward-sym))
817 :     ;; We're inside a "non function declaration": let's skip all other
818 :     ;; declarations that we find at the same level and try again.
819 :     (sml-skip-siblings)
820 :     ;; Obviously, let's not try again if we're at bobp.
821 :     (unless (bobp) (sml-beginning-of-defun)))))
822 :    
823 :     (defcustom sml-max-name-components 3
824 :     "Maximum number of components to use for the current function name."
825 :     :group 'sml
826 :     :type 'integer)
827 :    
828 :     (defun sml-current-fun-name ()
829 :     (save-excursion
830 :     (let ((count sml-max-name-components)
831 :     fullname name)
832 :     (end-of-line)
833 :     (while (and (> count 0)
834 :     (setq name (sml-beginning-of-defun)))
835 :     (decf count)
836 :     (setq fullname (if fullname (concat name "." fullname) name))
837 :     ;; Skip all other declarations that we find at the same level.
838 :     (sml-skip-siblings))
839 :     fullname)))
840 :    
841 : monnier 32
842 : monnier 535 ;;; INSERTING PROFORMAS (COMMON SML-FORMS)
843 : monnier 32
844 : monnier 334 (defvar sml-forms-alist nil
845 : monnier 535 "*Alist of code templates.
846 :     You can extend this alist to your heart's content. For each additional
847 : monnier 32 template NAME in the list, declare a keyboard macro or function (or
848 :     interactive command) called 'sml-form-NAME'.
849 :     If 'sml-form-NAME' is a function it takes no arguments and should
850 :     insert the template at point\; if this is a command it may accept any
851 :     sensible interactive call arguments\; keyboard macros can't take
852 : monnier 535 arguments at all. Apropos keyboard macros, see `name-last-kbd-macro'
853 : monnier 32 and `sml-addto-forms-alist'.
854 :     `sml-forms-alist' understands let, local, case, abstype, datatype,
855 :     signature, structure, and functor by default.")
856 :    
857 : monnier 334 (defmacro sml-def-skeleton (name interactor &rest elements)
858 : monnier 768 (when (fboundp 'define-skeleton)
859 :     (let ((fsym (intern (concat "sml-form-" name))))
860 : monnier 2722 ;; TODO: don't do the expansion in comments and strings.
861 : monnier 768 `(progn
862 :     (add-to-list 'sml-forms-alist ',(cons name fsym))
863 : monnier 1477 (condition-case err
864 :     ;; Try to use the new `system' flag.
865 :     (define-abbrev sml-mode-abbrev-table ,name "" ',fsym nil 'system)
866 :     (wrong-number-of-arguments
867 :     (define-abbrev sml-mode-abbrev-table ,name "" ',fsym)))
868 : monnier 768 (define-skeleton ,fsym
869 :     ,(format "SML-mode skeleton for `%s..' expressions" name)
870 :     ,interactor
871 :     ,(concat name " ") >
872 :     ,@elements)))))
873 : monnier 334 (put 'sml-def-skeleton 'lisp-indent-function 2)
874 : monnier 32
875 : monnier 334 (sml-def-skeleton "let" nil
876 : monnier 768 @ "\nin " > _ "\nend" >)
877 : monnier 32
878 : monnier 334 (sml-def-skeleton "if" nil
879 : monnier 768 @ " then " > _ "\nelse " > _)
880 : monnier 32
881 : monnier 334 (sml-def-skeleton "local" nil
882 : monnier 768 @ "\nin" > _ "\nend" >)
883 : monnier 32
884 : monnier 334 (sml-def-skeleton "case" "Case expr: "
885 : monnier 341 str "\nof " > _ " => ")
886 : monnier 32
887 : monnier 334 (sml-def-skeleton "signature" "Signature name: "
888 :     str " =\nsig" > "\n" > _ "\nend" >)
889 : monnier 32
890 : monnier 334 (sml-def-skeleton "structure" "Structure name: "
891 :     str " =\nstruct" > "\n" > _ "\nend" >)
892 : monnier 32
893 : monnier 334 (sml-def-skeleton "functor" "Functor name: "
894 :     str " () : =\nstruct" > "\n" > _ "\nend" >)
895 :    
896 : monnier 700 (sml-def-skeleton "datatype" "Datatype name and type params: "
897 : monnier 334 str " =" \n)
898 :    
899 : monnier 700 (sml-def-skeleton "abstype" "Abstype name and type params: "
900 : monnier 334 str " =" \n _ "\nwith" > "\nend" >)
901 :    
902 :     ;;
903 :    
904 : monnier 700 (sml-def-skeleton "struct" nil
905 :     _ "\nend" >)
906 :    
907 :     (sml-def-skeleton "sig" nil
908 :     _ "\nend" >)
909 :    
910 :     (sml-def-skeleton "val" nil
911 : monnier 768 @ " = " > _)
912 : monnier 700
913 :     (sml-def-skeleton "fn" nil
914 : monnier 768 @ " =>" > _)
915 : monnier 700
916 :     (sml-def-skeleton "fun" nil
917 : monnier 768 @ " =" > _)
918 : monnier 700
919 :     ;;
920 :    
921 : monnier 334 (defun sml-forms-menu (menu)
922 : monnier 1692 (mapcar (lambda (x) (vector (car x) (cdr x) t))
923 : monnier 768 sml-forms-alist))
924 : monnier 334
925 :     (defvar sml-last-form "let")
926 :    
927 : monnier 341 (defun sml-electric-space ()
928 :     "Expand a symbol into an SML form, or just insert a space.
929 :     If the point directly precedes a symbol for which an SML form exists,
930 :     the corresponding form is inserted."
931 :     (interactive)
932 : monnier 700 (let ((abbrev-mode (not abbrev-mode))
933 :     (last-command-char ?\ )
934 :     ;; Bind `this-command' to fool skeleton's special abbrev handling.
935 :     (this-command 'self-insert-command))
936 :     (call-interactively 'self-insert-command)))
937 : monnier 341
938 : monnier 334 (defun sml-insert-form (name newline)
939 : monnier 535 "Interactive short-cut to insert the NAME common ML form.
940 :     If a prefix argument is given insert a NEWLINE and indent first, or
941 : monnier 32 just move to the proper indentation if the line is blank\; otherwise
942 :     insert at point (which forces indentation to current column).
943 :    
944 :     The default form to insert is 'whatever you inserted last time'
945 : monnier 535 \(just hit return when prompted\)\; otherwise the command reads with
946 : monnier 32 completion from `sml-forms-alist'."
947 : monnier 334 (interactive
948 :     (list (completing-read
949 :     (format "Form to insert: (default %s) " sml-last-form)
950 :     sml-forms-alist nil t nil)
951 :     current-prefix-arg))
952 :     ;; default is whatever the last insert was...
953 :     (if (string= name "") (setq name sml-last-form) (setq sml-last-form name))
954 :     (unless (or (not newline)
955 :     (save-excursion (beginning-of-line) (looking-at "\\s-*$")))
956 :     (insert "\n"))
957 : monnier 892 (unless (/= ?w (char-syntax (preceding-char))) (insert " "))
958 : monnier 334 (let ((f (cdr (assoc name sml-forms-alist))))
959 :     (cond
960 :     ((commandp f) (command-execute f))
961 :     (f (funcall f))
962 :     (t (error "Undefined form: %s" name)))))
963 : monnier 32
964 : monnier 334 ;; See also macros.el in emacs lisp dir.
965 : monnier 32
966 : monnier 334 (defun sml-addto-forms-alist (name)
967 :     "Assign a name to the last keyboard macro defined.
968 :     Argument NAME is transmogrified to sml-form-NAME which is the symbol
969 :     actually defined.
970 : monnier 32
971 : monnier 334 The symbol's function definition becomes the keyboard macro string.
972 : monnier 32
973 : monnier 334 If that works, NAME is added to `sml-forms-alist' so you'll be able to
974 : monnier 535 reinvoke the macro through \\[sml-insert-form]. You might want to save
975 : monnier 334 the macro to use in a later editing session -- see `insert-kbd-macro'
976 :     and add these macros to your .emacs file.
977 : monnier 32
978 : monnier 334 See also `edit-kbd-macro' which is bound to \\[edit-kbd-macro]."
979 :     (interactive "sName for last kbd macro (\"sml-form-\" will be added): ")
980 :     (when (string= name "") (error "No command name given"))
981 :     (let ((fsym (intern (concat "sml-form-" name))))
982 :     (name-last-kbd-macro fsym)
983 :     (message "Macro bound to %s" fsym)
984 :     (add-to-list 'sml-forms-alist (cons name fsym))))
985 : monnier 32
986 : monnier 2722 ;;;
987 :     ;;; MLton support
988 :     ;;;
989 : monnier 32
990 : monnier 2722 (defvar sml-mlton-command "mlton"
991 :     "Command to run MLton. Can include arguments.")
992 :    
993 :     (defvar sml-mlton-mainfile nil)
994 :    
995 : monnier 2725 (defconst sml-mlton-error-regexp-alist
996 :     ;; I wish they just changed MLton to use one of the standard
997 :     ;; error formats.
998 :     `(("^\\(?:Error\\|\\(Warning\\)\\): \\(.+\\) \\([0-9]+\\)\\.\\([0-9]+\\)\\.$"
999 :     2 3 4
1000 :     ;; If subgroup 1 matched, then it's a warning, otherwise it's an error.
1001 :     ,@(if (fboundp 'compilation-fake-loc) '((1))))))
1002 :    
1003 :     (eval-after-load "compile"
1004 :     '(dolist ((x sml-mlton-error-regexp-alist))
1005 :     (add-to-list 'compilation-error-regexp-alist x)))
1006 :    
1007 : monnier 2722 (defun sml-mlton-typecheck (mainfile)
1008 :     "typecheck using MLton."
1009 :     (interactive
1010 :     (list (if (and mainfile (not current-prefix-arg))
1011 :     mainfile
1012 :     (read-file-name "Main file: "))))
1013 :     (save-some-buffers)
1014 :     (require 'compile)
1015 : monnier 2725 (dolist ((x sml-mlton-error-regexp-alist))
1016 :     (add-to-list 'compilation-error-regexp-alist x))
1017 : monnier 2722 (with-current-buffer (find-file-noselect mainfile)
1018 :     (compile (concat sml-mlton-command
1019 :     " -stop tc " ;Stop right after type checking.
1020 :     (shell-quote-argument
1021 :     (file-relative-name buffer-file-name))))))
1022 :    
1023 :     ;;;
1024 :     ;;; MLton's def-use info.
1025 :     ;;;
1026 :    
1027 :     (defvar sml-defuse-file nil)
1028 :    
1029 :     (defun sml-defuse-file ()
1030 :     (or sml-defuse-file (sml-defuse-set-file)))
1031 :    
1032 :     (defun sml-defuse-set-file ()
1033 :     "Specify the def-use file to use."
1034 :     (interactive)
1035 :     (setq sml-defuse-file (read-file-name "Def-use file: ")))
1036 :    
1037 :     (defun sml-defuse-symdata-at-point ()
1038 :     (save-excursion
1039 :     (sml-forward-sym)
1040 :     (let ((symname (sml-backward-sym)))
1041 :     (if (equal symname "op")
1042 :     (save-excursion (setq symname (sml-forward-sym))))
1043 :     (when (string-match "op " symname)
1044 :     (setq symname (substring symname (match-end 0)))
1045 :     (forward-word)
1046 :     (sml-forward-spaces))
1047 :     (list symname
1048 :     ;; Def-use files seem to count chars, not columns.
1049 :     ;; We hope here that they don't actually count bytes.
1050 :     ;; Also they seem to start counting at 1.
1051 :     (1+ (- (point) (progn (beginning-of-line) (point))))
1052 :     (save-restriction
1053 :     (widen) (1+ (count-lines (point-min) (point))))
1054 :     buffer-file-name))))
1055 :    
1056 :     (defconst sml-defuse-def-regexp
1057 :     "^[[:alpha:]]+ \\([^ \n]+\\) \\(.+\\) \\([0-9]+\\)\\.\\([0-9]+\\)$")
1058 :     (defconst sml-defuse-use-regexp-format "^ %s %d\\.%d $")
1059 :    
1060 :     (defun sml-defuse-jump-to-def ()
1061 :     "Jump to the definition corresponding to the symbol at point."
1062 :     (interactive)
1063 :     (let ((symdata (sml-defuse-symdata-at-point)))
1064 :     (if (null (car symdata))
1065 :     (error "Not on a symbol")
1066 :     (with-current-buffer (find-file-noselect (sml-defuse-file))
1067 :     (goto-char (point-min))
1068 :     (unless (re-search-forward
1069 :     (format sml-defuse-use-format
1070 :     (concat "\\(?:"
1071 :     ;; May be an absolute file name.
1072 :     (regexp-quote (nth 3 symdata))
1073 :     "\\|"
1074 :     ;; Or a relative file name.
1075 :     (regexp-quote (file-relative-name
1076 :     (nth 3 symdata)))
1077 :     "\\)")
1078 :     (nth 2 symdata)
1079 :     (nth 1 symdata))
1080 :     nil t)
1081 :     ;; FIXME: This is typically due to editing: any minor editing will
1082 :     ;; mess everything up. We should try to fail more gracefully.
1083 :     (error "Def-use info not found"))
1084 :     (unless (re-search-backward sml-defuse-def-regexp nil t)
1085 :     ;; This indicates a bug in this code.
1086 :     (error "Internal failure while looking up def-use"))
1087 :     (unless (equal (match-string 1) (nth 0 symdata))
1088 :     ;; FIXME: This again is most likely due to editing.
1089 :     (error "Incoherence in the def-use info found"))
1090 :     (let ((line (string-to-number (match-string 3)))
1091 :     (char (string-to-number (match-string 4))))
1092 :     (pop-to-buffer (find-file-noselect (match-string 2)))
1093 :     (goto-line line)
1094 :     (forward-char (1- char)))))))
1095 :    
1096 :     ;;;
1097 :     ;;; SML/NJ's Compilation Manager support
1098 :     ;;;
1099 :    
1100 : monnier 768 (defvar sml-cm-mode-syntax-table sml-mode-syntax-table)
1101 :     (defvar sml-cm-font-lock-keywords
1102 :     `(,(concat "\\<" (regexp-opt '("library" "group" "is" "structure"
1103 :     "functor" "signature" "funsig") t)
1104 :     "\\>")))
1105 : monnier 535 ;;;###autoload
1106 : monnier 2722 (add-to-list 'completion-ignored-extensions ".cm/")
1107 :     ;; This was used with the old compilation manager.
1108 : monnier 535 (add-to-list 'completion-ignored-extensions "CM/")
1109 :     ;;;###autoload
1110 :     (add-to-list 'auto-mode-alist '("\\.cm\\'" . sml-cm-mode))
1111 :     ;;;###autoload
1112 : monnier 768 (define-derived-mode sml-cm-mode fundamental-mode "SML-CM"
1113 :     "Major mode for SML/NJ's Compilation Manager configuration files."
1114 :     (local-set-key "\C-c\C-c" 'sml-compile)
1115 :     (set (make-local-variable 'font-lock-defaults)
1116 :     '(sml-cm-font-lock-keywords nil t nil nil)))
1117 : monnier 32
1118 : monnier 2722 ;;;
1119 :     ;;; ML-Lex support
1120 :     ;;;
1121 : monnier 535
1122 : monnier 768 (defvar sml-lex-font-lock-keywords
1123 :     (append
1124 :     '(("^%\\sw+" . font-lock-builtin-face)
1125 :     ("^%%" . font-lock-module-def-face))
1126 :     sml-font-lock-keywords))
1127 :     (defconst sml-lex-font-lock-defaults
1128 :     (cons 'sml-lex-font-lock-keywords (cdr sml-font-lock-defaults)))
1129 :    
1130 : monnier 707 ;;;###autoload
1131 : monnier 768 (define-derived-mode sml-lex-mode sml-mode "SML-Lex"
1132 :     "Major Mode for editing ML-Lex files."
1133 :     (set (make-local-variable 'font-lock-defaults) sml-lex-font-lock-defaults))
1134 : monnier 707
1135 : monnier 2722 ;;;
1136 :     ;;; ML-Yacc support
1137 :     ;;;
1138 : monnier 768
1139 : monnier 707 (defface sml-yacc-bnf-face
1140 :     '((t (:foreground "darkgreen")))
1141 :     "Face used to highlight (non)terminals in `sml-yacc-mode'.")
1142 :     (defvar sml-yacc-bnf-face 'sml-yacc-bnf-face)
1143 :    
1144 :     (defcustom sml-yacc-indent-action 16
1145 :     "Indentation column of the opening paren of actions."
1146 :     :group 'sml
1147 :     :type 'integer)
1148 :    
1149 :     (defcustom sml-yacc-indent-pipe nil
1150 :     "Indentation column of the pipe char in the BNF.
1151 :     If nil, align it with `:' or with previous cases."
1152 :     :group 'sml
1153 :     :type 'integer)
1154 :    
1155 :     (defcustom sml-yacc-indent-term nil
1156 :     "Indentation column of the (non)term part.
1157 :     If nil, align it with previous cases."
1158 :     :group 'sml
1159 :     :type 'integer)
1160 :    
1161 :     (defvar sml-yacc-font-lock-keywords
1162 : monnier 768 (cons '("^\\(\\sw+\\s-*:\\|\\s-*|\\)\\(\\s-*\\sw+\\)*\\s-*\\(\\(%\\sw+\\)\\s-+\\sw+\\|\\)"
1163 : monnier 707 (0 (save-excursion
1164 :     (save-match-data
1165 :     (goto-char (match-beginning 0))
1166 :     (unless (or (re-search-forward "\\<of\\>" (match-end 0) 'move)
1167 :     (progn (sml-forward-spaces)
1168 :     (not (looking-at "("))))
1169 : monnier 768 sml-yacc-bnf-face))))
1170 :     (4 font-lock-builtin-face t t))
1171 :     sml-lex-font-lock-keywords))
1172 : monnier 707 (defconst sml-yacc-font-lock-defaults
1173 : monnier 768 (cons 'sml-yacc-font-lock-keywords (cdr sml-font-lock-defaults)))
1174 : monnier 707
1175 :     (defun sml-yacc-indent-line ()
1176 :     "Indent current line of ML-Yacc code."
1177 :     (let ((savep (> (current-column) (current-indentation)))
1178 :     (indent (max (or (ignore-errors (sml-yacc-indentation)) 0) 0)))
1179 :     (if savep
1180 :     (save-excursion (indent-line-to indent))
1181 :     (indent-line-to indent))))
1182 :    
1183 :     (defun sml-yacc-indentation ()
1184 :     (save-excursion
1185 :     (back-to-indentation)
1186 :     (or (and (looking-at "%\\|\\(\\sw\\|\\s_\\)+\\s-*:") 0)
1187 :     (when (save-excursion
1188 :     (condition-case nil (progn (up-list -1) nil) (scan-error t)))
1189 :     ;; We're outside an action.
1190 :     (cond
1191 :     ;; Special handling of indentation inside %term and %nonterm
1192 :     ((save-excursion
1193 :     (and (re-search-backward "^%\\(\\sw+\\)" nil t)
1194 :     (member (match-string 1) '("term" "nonterm"))))
1195 :     (if (numberp sml-yacc-indent-term) sml-yacc-indent-term
1196 :     (let ((offset (if (looking-at "|") -2 0)))
1197 :     (forward-line -1)
1198 :     (looking-at "\\s-*\\(%\\sw*\\||\\)?\\s-*")
1199 :     (goto-char (match-end 0))
1200 :     (+ offset (current-column)))))
1201 :     ((looking-at "(") sml-yacc-indent-action)
1202 :     ((looking-at "|")
1203 :     (if (numberp sml-yacc-indent-pipe) sml-yacc-indent-pipe
1204 :     (backward-sexp 1)
1205 :     (while (progn (sml-backward-spaces)
1206 :     (/= 0 (skip-syntax-backward "w_"))))
1207 :     (sml-backward-spaces)
1208 :     (if (not (looking-at "\\s-$"))
1209 :     (1- (current-column))
1210 :     (skip-syntax-forward " ")
1211 :     (- (current-column) 2))))))
1212 :     ;; default to SML rules
1213 :     (sml-calculate-indentation))))
1214 :    
1215 :     ;;;###autoload
1216 :     (add-to-list 'auto-mode-alist '("\\.grm\\'" . sml-yacc-mode))
1217 :     ;;;###autoload
1218 :     (define-derived-mode sml-yacc-mode sml-mode "SML-Yacc"
1219 : monnier 768 "Major Mode for editing ML-Yacc files."
1220 : monnier 707 (set (make-local-variable 'indent-line-function) 'sml-yacc-indent-line)
1221 :     (set (make-local-variable 'font-lock-defaults) sml-yacc-font-lock-defaults))
1222 :    
1223 : monnier 319 (provide 'sml-mode)
1224 : monnier 535
1225 :     ;;; sml-mode.el ends here

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