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 2824 - (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 : monnier 2817 ;; published by the Free Software Foundation; either version 3, or (at
25 : monnier 32 ;; 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 2817 (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 :     "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 : monnier 2824 (cons "not" (decode-char 'ucs 172))
221 : monnier 1711 (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 : monnier 2817 (or (save-excursion (forward-line -1)
563 :     (if (looking-at "[\t ]*\\\\")
564 :     (current-indentation)))
565 :     (save-excursion
566 :     (if (re-search-backward "[^\\\\]\"" nil t)
567 :     (1+ (current-column))
568 :     0))))
569 : monnier 300
570 : monnier 892 ;; Closing parens. Could be handled below with `sml-indent-relative'?
571 :     (and (looking-at "\\s)")
572 :     (save-excursion
573 :     (skip-syntax-forward ")")
574 :     (backward-sexp 1)
575 :     (if (sml-dangling-sym)
576 :     (sml-indent-default 'noindent)
577 :     (current-column))))
578 :    
579 : monnier 334 (and (setq data (assoc sym sml-close-paren))
580 :     (sml-indent-relative sym data))
581 : monnier 32
582 : monnier 892 (and (member sym sml-starters-syms)
583 :     (sml-indent-starter sym))
584 : monnier 300
585 : monnier 334 (and (string= sym "|") (sml-indent-pipe))
586 : monnier 300
587 : monnier 319 (sml-indent-arg)
588 :     (sml-indent-default))))))
589 : monnier 300
590 : monnier 892 (defsubst sml-bolp ()
591 :     (save-excursion (skip-chars-backward " \t|") (bolp)))
592 :    
593 : monnier 2722 (defun sml-first-starter-p ()
594 :     "Non-nil if starter at point is immediately preceded by let/local/in/..."
595 :     (save-excursion
596 :     (let ((sym (unless (save-excursion (sml-backward-arg))
597 :     (sml-backward-spaces)
598 :     (sml-backward-sym))))
599 :     (if (member sym '(";" "d=")) (setq sym nil))
600 :     sym)))
601 :    
602 :    
603 : monnier 892 (defun sml-indent-starter (orig-sym)
604 :     "Return the indentation to use for a symbol in `sml-starters-syms'.
605 :     Point should be just before the symbol ORIG-SYM and is not preserved."
606 :     (let ((sym (unless (save-excursion (sml-backward-arg))
607 :     (sml-backward-spaces)
608 :     (sml-backward-sym))))
609 : monnier 1472 (if (member sym '(";" "d=")) (setq sym nil))
610 : monnier 892 (if sym (sml-get-sym-indent sym)
611 :     ;; FIXME: this can take a *long* time !!
612 :     (setq sym (sml-find-matching-starter sml-starters-syms))
613 : monnier 2722 (if (or (sml-first-starter-p)
614 :     ;; Don't align with `and' because it might be specially indented.
615 :     (and (or (equal orig-sym "and") (not (equal sym "and")))
616 :     (sml-bolp)))
617 : monnier 892 (+ (current-column)
618 :     (if (and sml-rightalign-and (equal orig-sym "and"))
619 :     (- (length sym) 3) 0))
620 :     (sml-indent-starter orig-sym)))))
621 :    
622 : monnier 334 (defun sml-indent-relative (sym data)
623 :     (save-excursion
624 :     (sml-forward-sym) (sml-backward-sexp nil)
625 : monnier 339 (unless (second data) (sml-backward-spaces) (sml-backward-sym))
626 : monnier 334 (+ (or (cdr (assoc sym sml-symbol-indent)) 0)
627 :     (sml-delegated-indent))))
628 :    
629 : monnier 319 (defun sml-indent-pipe ()
630 : monnier 544 (let ((sym (sml-find-matching-starter sml-pipeheads
631 :     (sml-op-prec "|" 'back))))
632 :     (when sym
633 :     (if (string= sym "|")
634 :     (if (sml-bolp) (current-column) (sml-indent-pipe))
635 :     (let ((pipe-indent (or (cdr (assoc "|" sml-symbol-indent)) -2)))
636 : monnier 892 (when (or (member sym '("datatype" "abstype"))
637 :     (and (equal sym "and")
638 :     (save-excursion
639 :     (forward-word 1)
640 :     (not (sml-funname-of-and)))))
641 : monnier 544 (re-search-forward "="))
642 :     (sml-forward-sym)
643 :     (sml-forward-spaces)
644 :     (+ pipe-indent (current-column)))))))
645 : monnier 39
646 : monnier 334 (defun sml-find-forward (re)
647 :     (sml-forward-spaces)
648 :     (while (and (not (looking-at re))
649 :     (progn
650 :     (or (ignore-errors (forward-sexp 1) t) (forward-char 1))
651 :     (sml-forward-spaces)
652 :     (not (looking-at re))))))
653 : monnier 39
654 : monnier 319 (defun sml-indent-arg ()
655 :     (and (save-excursion (ignore-errors (sml-forward-arg)))
656 :     ;;(not (looking-at sml-not-arg-re))
657 :     ;; looks like a function or an argument
658 :     (sml-move-if (sml-backward-arg))
659 :     ;; an argument
660 :     (if (save-excursion (not (sml-backward-arg)))
661 :     ;; a first argument
662 :     (+ (current-column) sml-indent-args)
663 :     ;; not a first arg
664 :     (while (and (/= (current-column) (current-indentation))
665 :     (sml-move-if (sml-backward-arg))))
666 :     (unless (save-excursion (sml-backward-arg))
667 :     ;; all earlier args are on the same line
668 :     (sml-forward-arg) (sml-forward-spaces))
669 :     (current-column))))
670 :    
671 : monnier 334 (defun sml-get-indent (data sym)
672 : monnier 1692 (let (d)
673 : monnier 334 (cond
674 :     ((not (listp data)) data)
675 : monnier 1692 ((setq d (member sym data)) (cadr d))
676 : monnier 334 ((and (consp data) (not (stringp (car data)))) (car data))
677 :     (t sml-indent-level))))
678 : monnier 332
679 : monnier 319 (defun sml-dangling-sym ()
680 : monnier 892 "Non-nil if the symbol after point is dangling.
681 :     The symbol can be an SML symbol or an open-paren. \"Dangling\" means that
682 :     it is not on its own line but is the last element on that line."
683 : monnier 32 (save-excursion
684 : monnier 319 (and (not (sml-bolp))
685 :     (< (sml-point-after (end-of-line))
686 : monnier 892 (sml-point-after (or (sml-forward-sym) (skip-syntax-forward "("))
687 : monnier 319 (sml-forward-spaces))))))
688 : monnier 32
689 : monnier 334 (defun sml-delegated-indent ()
690 :     (if (sml-dangling-sym)
691 :     (sml-indent-default 'noindent)
692 :     (sml-move-if (backward-word 1)
693 : monnier 341 (looking-at sml-agglomerate-re))
694 : monnier 334 (current-column)))
695 :    
696 : monnier 319 (defun sml-get-sym-indent (sym &optional style)
697 : monnier 535 "Find the indentation for the SYM we're `looking-at'.
698 : monnier 892 If indentation is delegated, point will move to the start of the parent.
699 :     Optional argument STYLE is currently ignored."
700 : monnier 334 (assert (equal sym (save-excursion (sml-forward-sym))))
701 :     (save-excursion
702 : monnier 1472 (let ((delegate (and (not (equal sym "end")) (assoc sym sml-close-paren)))
703 : monnier 334 (head-sym sym))
704 : monnier 339 (when (and delegate (not (eval (third delegate))))
705 : monnier 334 ;;(sml-find-match-backward sym delegate)
706 :     (sml-forward-sym) (sml-backward-sexp nil)
707 :     (setq head-sym
708 : monnier 339 (if (second delegate)
709 : monnier 334 (save-excursion (sml-forward-sym))
710 :     (sml-backward-spaces) (sml-backward-sym))))
711 : monnier 32
712 : monnier 334 (let ((idata (assoc head-sym sml-indent-rule)))
713 :     (when idata
714 :     ;;(if (or style (not delegate))
715 :     ;; normal indentation
716 : monnier 1692 (let ((indent (sml-get-indent (cdr idata) sym)))
717 : monnier 334 (when indent (+ (sml-delegated-indent) indent)))
718 :     ;; delgate indentation to the parent
719 :     ;;(sml-forward-sym) (sml-backward-sexp nil)
720 :     ;;(let* ((parent-sym (save-excursion (sml-forward-sym)))
721 :     ;; (parent-indent (cdr (assoc parent-sym sml-indent-starters))))
722 :     ;; check the special rules
723 :     ;;(+ (sml-delegated-indent)
724 : monnier 1692 ;; (or (sml-get-indent (cdr indent-data) 1 'strict)
725 :     ;; (sml-get-indent (cdr parent-indent) 1 'strict)
726 :     ;; (sml-get-indent (cdr indent-data) 0)
727 :     ;; (sml-get-indent (cdr parent-indent) 0))))))))
728 : monnier 334 )))))
729 :    
730 : monnier 319 (defun sml-indent-default (&optional noindent)
731 : monnier 333 (let* ((sym-after (save-excursion (sml-forward-sym)))
732 : monnier 319 (_ (sml-backward-spaces))
733 : monnier 333 (sym-before (sml-backward-sym))
734 : monnier 892 (sym-indent (and sym-before (sml-get-sym-indent sym-before)))
735 :     (indent-after (or (cdr (assoc sym-after sml-symbol-indent)) 0)))
736 :     (when (equal sym-before "end")
737 :     ;; I don't understand what's really happening here, but when
738 :     ;; it's `end' clearly, we need to do something special.
739 :     (forward-word 1)
740 :     (setq sym-before nil sym-indent nil))
741 :     (cond
742 :     (sym-indent
743 :     ;; the previous sym is an indentation introducer: follow the rule
744 :     (if noindent
745 :     ;;(current-column)
746 :     sym-indent
747 :     (+ sym-indent indent-after)))
748 :     ;; If we're just after a hanging open paren.
749 :     ((and (eq (char-syntax (preceding-char)) ?\()
750 :     (save-excursion (backward-char) (sml-dangling-sym)))
751 :     (backward-char)
752 :     (sml-indent-default))
753 :     (t
754 : monnier 339 ;; default-default
755 :     (let* ((prec-after (sml-op-prec sym-after 'back))
756 :     (prec (or (sml-op-prec sym-before 'back) prec-after 100)))
757 :     ;; go back until you hit a symbol that has a lower prec than the
758 :     ;; "current one", or until you backed over a sym that has the same prec
759 :     ;; but is at the beginning of a line.
760 :     (while (and (not (sml-bolp))
761 : monnier 892 (while (sml-move-if (sml-backward-sexp (1- prec))))
762 : monnier 339 (not (sml-bolp)))
763 :     (while (sml-move-if (sml-backward-sexp prec))))
764 : monnier 892 (if noindent
765 :     ;; the `noindent' case does back over an introductory symbol
766 :     ;; such as `fun', ...
767 :     (progn
768 :     (sml-move-if
769 :     (sml-backward-spaces)
770 :     (member (sml-backward-sym) sml-starters-syms))
771 :     (current-column))
772 :     ;; Use `indent-after' for cases such as when , or ; should be
773 :     ;; outdented so that their following terms are aligned.
774 :     (+ (if (progn
775 :     (if (equal sym-after ";")
776 :     (sml-move-if
777 :     (sml-backward-spaces)
778 :     (member (sml-backward-sym) sml-starters-syms)))
779 :     (and sym-after (not (looking-at sym-after))))
780 :     indent-after 0)
781 :     (current-column))))))))
782 : monnier 32
783 : monnier 319
784 :     ;; maybe `|' should be set to word-syntax in our temp syntax table ?
785 :     (defun sml-current-indentation ()
786 : monnier 32 (save-excursion
787 : monnier 319 (beginning-of-line)
788 :     (skip-chars-forward " \t|")
789 :     (current-column)))
790 : monnier 32
791 : monnier 300
792 : monnier 544 (defun sml-find-matching-starter (syms &optional prec)
793 :     (let (sym)
794 :     (ignore-errors
795 :     (while
796 :     (progn (sml-backward-sexp prec)
797 :     (setq sym (save-excursion (sml-forward-sym)))
798 :     (not (or (member sym syms) (bobp)))))
799 : monnier 1472 (if (member sym syms) sym))))
800 : monnier 319
801 : monnier 700 (defun sml-skip-siblings ()
802 :     (while (and (not (bobp)) (sml-backward-arg))
803 :     (sml-find-matching-starter sml-starters-syms))
804 :     (when (looking-at "in\\>\\|local\\>")
805 :     ;;skip over `local...in' and continue
806 :     (forward-word 1)
807 :     (sml-backward-sexp nil)
808 :     (sml-skip-siblings)))
809 :    
810 :     (defun sml-beginning-of-defun ()
811 :     (let ((sym (sml-find-matching-starter sml-starters-syms)))
812 : monnier 707 (if (member sym '("fun" "and" "functor" "signature" "structure"
813 : monnier 700 "abstraction" "datatype" "abstype"))
814 :     (save-excursion (sml-forward-sym) (sml-forward-spaces)
815 :     (sml-forward-sym))
816 :     ;; We're inside a "non function declaration": let's skip all other
817 :     ;; declarations that we find at the same level and try again.
818 :     (sml-skip-siblings)
819 :     ;; Obviously, let's not try again if we're at bobp.
820 :     (unless (bobp) (sml-beginning-of-defun)))))
821 :    
822 :     (defcustom sml-max-name-components 3
823 :     "Maximum number of components to use for the current function name."
824 :     :group 'sml
825 :     :type 'integer)
826 :    
827 :     (defun sml-current-fun-name ()
828 :     (save-excursion
829 :     (let ((count sml-max-name-components)
830 :     fullname name)
831 :     (end-of-line)
832 :     (while (and (> count 0)
833 :     (setq name (sml-beginning-of-defun)))
834 :     (decf count)
835 :     (setq fullname (if fullname (concat name "." fullname) name))
836 :     ;; Skip all other declarations that we find at the same level.
837 :     (sml-skip-siblings))
838 :     fullname)))
839 :    
840 : monnier 32
841 : monnier 535 ;;; INSERTING PROFORMAS (COMMON SML-FORMS)
842 : monnier 32
843 : monnier 334 (defvar sml-forms-alist nil
844 : monnier 535 "*Alist of code templates.
845 :     You can extend this alist to your heart's content. For each additional
846 : monnier 32 template NAME in the list, declare a keyboard macro or function (or
847 :     interactive command) called 'sml-form-NAME'.
848 :     If 'sml-form-NAME' is a function it takes no arguments and should
849 :     insert the template at point\; if this is a command it may accept any
850 :     sensible interactive call arguments\; keyboard macros can't take
851 : monnier 535 arguments at all. Apropos keyboard macros, see `name-last-kbd-macro'
852 : monnier 32 and `sml-addto-forms-alist'.
853 :     `sml-forms-alist' understands let, local, case, abstype, datatype,
854 :     signature, structure, and functor by default.")
855 :    
856 : monnier 334 (defmacro sml-def-skeleton (name interactor &rest elements)
857 : monnier 768 (when (fboundp 'define-skeleton)
858 :     (let ((fsym (intern (concat "sml-form-" name))))
859 : monnier 2722 ;; TODO: don't do the expansion in comments and strings.
860 : monnier 768 `(progn
861 :     (add-to-list 'sml-forms-alist ',(cons name fsym))
862 : monnier 1477 (condition-case err
863 :     ;; Try to use the new `system' flag.
864 :     (define-abbrev sml-mode-abbrev-table ,name "" ',fsym nil 'system)
865 :     (wrong-number-of-arguments
866 :     (define-abbrev sml-mode-abbrev-table ,name "" ',fsym)))
867 : monnier 2822 (when (fboundp 'abbrev-put)
868 :     (let ((abbrev (abbrev-symbol ,name sml-mode-abbrev-table)))
869 :     (abbrev-put abbrev :case-fixed t)
870 :     (abbrev-put abbrev :enable-function
871 :     (lambda () (not (nth 8 (syntax-ppss)))))))
872 : monnier 768 (define-skeleton ,fsym
873 :     ,(format "SML-mode skeleton for `%s..' expressions" name)
874 :     ,interactor
875 :     ,(concat name " ") >
876 :     ,@elements)))))
877 : monnier 334 (put 'sml-def-skeleton 'lisp-indent-function 2)
878 : monnier 32
879 : monnier 334 (sml-def-skeleton "let" nil
880 : monnier 768 @ "\nin " > _ "\nend" >)
881 : monnier 32
882 : monnier 334 (sml-def-skeleton "if" nil
883 : monnier 768 @ " then " > _ "\nelse " > _)
884 : monnier 32
885 : monnier 334 (sml-def-skeleton "local" nil
886 : monnier 768 @ "\nin" > _ "\nend" >)
887 : monnier 32
888 : monnier 334 (sml-def-skeleton "case" "Case expr: "
889 : monnier 341 str "\nof " > _ " => ")
890 : monnier 32
891 : monnier 334 (sml-def-skeleton "signature" "Signature name: "
892 :     str " =\nsig" > "\n" > _ "\nend" >)
893 : monnier 32
894 : monnier 334 (sml-def-skeleton "structure" "Structure name: "
895 :     str " =\nstruct" > "\n" > _ "\nend" >)
896 : monnier 32
897 : monnier 334 (sml-def-skeleton "functor" "Functor name: "
898 :     str " () : =\nstruct" > "\n" > _ "\nend" >)
899 :    
900 : monnier 700 (sml-def-skeleton "datatype" "Datatype name and type params: "
901 : monnier 334 str " =" \n)
902 :    
903 : monnier 700 (sml-def-skeleton "abstype" "Abstype name and type params: "
904 : monnier 334 str " =" \n _ "\nwith" > "\nend" >)
905 :    
906 :     ;;
907 :    
908 : monnier 700 (sml-def-skeleton "struct" nil
909 :     _ "\nend" >)
910 :    
911 :     (sml-def-skeleton "sig" nil
912 :     _ "\nend" >)
913 :    
914 :     (sml-def-skeleton "val" nil
915 : monnier 768 @ " = " > _)
916 : monnier 700
917 :     (sml-def-skeleton "fn" nil
918 : monnier 768 @ " =>" > _)
919 : monnier 700
920 :     (sml-def-skeleton "fun" nil
921 : monnier 768 @ " =" > _)
922 : monnier 700
923 :     ;;
924 :    
925 : monnier 334 (defun sml-forms-menu (menu)
926 : monnier 1692 (mapcar (lambda (x) (vector (car x) (cdr x) t))
927 : monnier 768 sml-forms-alist))
928 : monnier 334
929 :     (defvar sml-last-form "let")
930 :    
931 : monnier 341 (defun sml-electric-space ()
932 :     "Expand a symbol into an SML form, or just insert a space.
933 :     If the point directly precedes a symbol for which an SML form exists,
934 :     the corresponding form is inserted."
935 :     (interactive)
936 : monnier 700 (let ((abbrev-mode (not abbrev-mode))
937 :     (last-command-char ?\ )
938 :     ;; Bind `this-command' to fool skeleton's special abbrev handling.
939 :     (this-command 'self-insert-command))
940 :     (call-interactively 'self-insert-command)))
941 : monnier 341
942 : monnier 334 (defun sml-insert-form (name newline)
943 : monnier 535 "Interactive short-cut to insert the NAME common ML form.
944 :     If a prefix argument is given insert a NEWLINE and indent first, or
945 : monnier 32 just move to the proper indentation if the line is blank\; otherwise
946 :     insert at point (which forces indentation to current column).
947 :    
948 :     The default form to insert is 'whatever you inserted last time'
949 : monnier 535 \(just hit return when prompted\)\; otherwise the command reads with
950 : monnier 32 completion from `sml-forms-alist'."
951 : monnier 334 (interactive
952 :     (list (completing-read
953 :     (format "Form to insert: (default %s) " sml-last-form)
954 :     sml-forms-alist nil t nil)
955 :     current-prefix-arg))
956 :     ;; default is whatever the last insert was...
957 :     (if (string= name "") (setq name sml-last-form) (setq sml-last-form name))
958 :     (unless (or (not newline)
959 :     (save-excursion (beginning-of-line) (looking-at "\\s-*$")))
960 :     (insert "\n"))
961 : monnier 892 (unless (/= ?w (char-syntax (preceding-char))) (insert " "))
962 : monnier 334 (let ((f (cdr (assoc name sml-forms-alist))))
963 :     (cond
964 :     ((commandp f) (command-execute f))
965 :     (f (funcall f))
966 :     (t (error "Undefined form: %s" name)))))
967 : monnier 32
968 : monnier 334 ;; See also macros.el in emacs lisp dir.
969 : monnier 32
970 : monnier 334 (defun sml-addto-forms-alist (name)
971 :     "Assign a name to the last keyboard macro defined.
972 :     Argument NAME is transmogrified to sml-form-NAME which is the symbol
973 :     actually defined.
974 : monnier 32
975 : monnier 334 The symbol's function definition becomes the keyboard macro string.
976 : monnier 32
977 : monnier 334 If that works, NAME is added to `sml-forms-alist' so you'll be able to
978 : monnier 535 reinvoke the macro through \\[sml-insert-form]. You might want to save
979 : monnier 334 the macro to use in a later editing session -- see `insert-kbd-macro'
980 :     and add these macros to your .emacs file.
981 : monnier 32
982 : monnier 334 See also `edit-kbd-macro' which is bound to \\[edit-kbd-macro]."
983 :     (interactive "sName for last kbd macro (\"sml-form-\" will be added): ")
984 :     (when (string= name "") (error "No command name given"))
985 :     (let ((fsym (intern (concat "sml-form-" name))))
986 :     (name-last-kbd-macro fsym)
987 :     (message "Macro bound to %s" fsym)
988 :     (add-to-list 'sml-forms-alist (cons name fsym))))
989 : monnier 32
990 : monnier 2722 ;;;
991 :     ;;; MLton support
992 :     ;;;
993 : monnier 32
994 : monnier 2722 (defvar sml-mlton-command "mlton"
995 :     "Command to run MLton. Can include arguments.")
996 :    
997 :     (defvar sml-mlton-mainfile nil)
998 :    
999 : monnier 2725 (defconst sml-mlton-error-regexp-alist
1000 :     ;; I wish they just changed MLton to use one of the standard
1001 :     ;; error formats.
1002 :     `(("^\\(?:Error\\|\\(Warning\\)\\): \\(.+\\) \\([0-9]+\\)\\.\\([0-9]+\\)\\.$"
1003 :     2 3 4
1004 :     ;; If subgroup 1 matched, then it's a warning, otherwise it's an error.
1005 :     ,@(if (fboundp 'compilation-fake-loc) '((1))))))
1006 :    
1007 :     (eval-after-load "compile"
1008 : monnier 2817 '(dolist (x sml-mlton-error-regexp-alist)
1009 : monnier 2725 (add-to-list 'compilation-error-regexp-alist x)))
1010 :    
1011 : monnier 2722 (defun sml-mlton-typecheck (mainfile)
1012 :     "typecheck using MLton."
1013 :     (interactive
1014 :     (list (if (and mainfile (not current-prefix-arg))
1015 :     mainfile
1016 :     (read-file-name "Main file: "))))
1017 :     (save-some-buffers)
1018 :     (require 'compile)
1019 : monnier 2822 (dolist (x sml-mlton-error-regexp-alist)
1020 : monnier 2725 (add-to-list 'compilation-error-regexp-alist x))
1021 : monnier 2722 (with-current-buffer (find-file-noselect mainfile)
1022 :     (compile (concat sml-mlton-command
1023 :     " -stop tc " ;Stop right after type checking.
1024 :     (shell-quote-argument
1025 :     (file-relative-name buffer-file-name))))))
1026 :    
1027 :     ;;;
1028 :     ;;; MLton's def-use info.
1029 :     ;;;
1030 :    
1031 :     (defvar sml-defuse-file nil)
1032 :    
1033 :     (defun sml-defuse-file ()
1034 :     (or sml-defuse-file (sml-defuse-set-file)))
1035 :    
1036 :     (defun sml-defuse-set-file ()
1037 :     "Specify the def-use file to use."
1038 :     (interactive)
1039 :     (setq sml-defuse-file (read-file-name "Def-use file: ")))
1040 :    
1041 :     (defun sml-defuse-symdata-at-point ()
1042 :     (save-excursion
1043 :     (sml-forward-sym)
1044 :     (let ((symname (sml-backward-sym)))
1045 :     (if (equal symname "op")
1046 :     (save-excursion (setq symname (sml-forward-sym))))
1047 :     (when (string-match "op " symname)
1048 :     (setq symname (substring symname (match-end 0)))
1049 :     (forward-word)
1050 :     (sml-forward-spaces))
1051 :     (list symname
1052 :     ;; Def-use files seem to count chars, not columns.
1053 :     ;; We hope here that they don't actually count bytes.
1054 :     ;; Also they seem to start counting at 1.
1055 :     (1+ (- (point) (progn (beginning-of-line) (point))))
1056 :     (save-restriction
1057 :     (widen) (1+ (count-lines (point-min) (point))))
1058 :     buffer-file-name))))
1059 :    
1060 :     (defconst sml-defuse-def-regexp
1061 :     "^[[:alpha:]]+ \\([^ \n]+\\) \\(.+\\) \\([0-9]+\\)\\.\\([0-9]+\\)$")
1062 :     (defconst sml-defuse-use-regexp-format "^ %s %d\\.%d $")
1063 :    
1064 :     (defun sml-defuse-jump-to-def ()
1065 :     "Jump to the definition corresponding to the symbol at point."
1066 :     (interactive)
1067 :     (let ((symdata (sml-defuse-symdata-at-point)))
1068 :     (if (null (car symdata))
1069 :     (error "Not on a symbol")
1070 :     (with-current-buffer (find-file-noselect (sml-defuse-file))
1071 :     (goto-char (point-min))
1072 :     (unless (re-search-forward
1073 : monnier 2817 (format sml-defuse-use-regexp-format
1074 : monnier 2722 (concat "\\(?:"
1075 :     ;; May be an absolute file name.
1076 :     (regexp-quote (nth 3 symdata))
1077 :     "\\|"
1078 :     ;; Or a relative file name.
1079 :     (regexp-quote (file-relative-name
1080 :     (nth 3 symdata)))
1081 :     "\\)")
1082 :     (nth 2 symdata)
1083 :     (nth 1 symdata))
1084 :     nil t)
1085 :     ;; FIXME: This is typically due to editing: any minor editing will
1086 :     ;; mess everything up. We should try to fail more gracefully.
1087 :     (error "Def-use info not found"))
1088 :     (unless (re-search-backward sml-defuse-def-regexp nil t)
1089 :     ;; This indicates a bug in this code.
1090 :     (error "Internal failure while looking up def-use"))
1091 :     (unless (equal (match-string 1) (nth 0 symdata))
1092 :     ;; FIXME: This again is most likely due to editing.
1093 :     (error "Incoherence in the def-use info found"))
1094 :     (let ((line (string-to-number (match-string 3)))
1095 :     (char (string-to-number (match-string 4))))
1096 :     (pop-to-buffer (find-file-noselect (match-string 2)))
1097 :     (goto-line line)
1098 :     (forward-char (1- char)))))))
1099 :    
1100 :     ;;;
1101 :     ;;; SML/NJ's Compilation Manager support
1102 :     ;;;
1103 :    
1104 : monnier 768 (defvar sml-cm-mode-syntax-table sml-mode-syntax-table)
1105 :     (defvar sml-cm-font-lock-keywords
1106 :     `(,(concat "\\<" (regexp-opt '("library" "group" "is" "structure"
1107 :     "functor" "signature" "funsig") t)
1108 :     "\\>")))
1109 : monnier 535 ;;;###autoload
1110 : monnier 2722 (add-to-list 'completion-ignored-extensions ".cm/")
1111 :     ;; This was used with the old compilation manager.
1112 : monnier 535 (add-to-list 'completion-ignored-extensions "CM/")
1113 :     ;;;###autoload
1114 :     (add-to-list 'auto-mode-alist '("\\.cm\\'" . sml-cm-mode))
1115 :     ;;;###autoload
1116 : monnier 768 (define-derived-mode sml-cm-mode fundamental-mode "SML-CM"
1117 :     "Major mode for SML/NJ's Compilation Manager configuration files."
1118 :     (local-set-key "\C-c\C-c" 'sml-compile)
1119 :     (set (make-local-variable 'font-lock-defaults)
1120 :     '(sml-cm-font-lock-keywords nil t nil nil)))
1121 : monnier 32
1122 : monnier 2722 ;;;
1123 :     ;;; ML-Lex support
1124 :     ;;;
1125 : monnier 535
1126 : monnier 768 (defvar sml-lex-font-lock-keywords
1127 :     (append
1128 :     '(("^%\\sw+" . font-lock-builtin-face)
1129 :     ("^%%" . font-lock-module-def-face))
1130 :     sml-font-lock-keywords))
1131 :     (defconst sml-lex-font-lock-defaults
1132 :     (cons 'sml-lex-font-lock-keywords (cdr sml-font-lock-defaults)))
1133 :    
1134 : monnier 707 ;;;###autoload
1135 : monnier 768 (define-derived-mode sml-lex-mode sml-mode "SML-Lex"
1136 :     "Major Mode for editing ML-Lex files."
1137 :     (set (make-local-variable 'font-lock-defaults) sml-lex-font-lock-defaults))
1138 : monnier 707
1139 : monnier 2722 ;;;
1140 :     ;;; ML-Yacc support
1141 :     ;;;
1142 : monnier 768
1143 : monnier 707 (defface sml-yacc-bnf-face
1144 :     '((t (:foreground "darkgreen")))
1145 :     "Face used to highlight (non)terminals in `sml-yacc-mode'.")
1146 :     (defvar sml-yacc-bnf-face 'sml-yacc-bnf-face)
1147 :    
1148 :     (defcustom sml-yacc-indent-action 16
1149 :     "Indentation column of the opening paren of actions."
1150 :     :group 'sml
1151 :     :type 'integer)
1152 :    
1153 :     (defcustom sml-yacc-indent-pipe nil
1154 :     "Indentation column of the pipe char in the BNF.
1155 :     If nil, align it with `:' or with previous cases."
1156 :     :group 'sml
1157 :     :type 'integer)
1158 :    
1159 :     (defcustom sml-yacc-indent-term nil
1160 :     "Indentation column of the (non)term part.
1161 :     If nil, align it with previous cases."
1162 :     :group 'sml
1163 :     :type 'integer)
1164 :    
1165 :     (defvar sml-yacc-font-lock-keywords
1166 : monnier 768 (cons '("^\\(\\sw+\\s-*:\\|\\s-*|\\)\\(\\s-*\\sw+\\)*\\s-*\\(\\(%\\sw+\\)\\s-+\\sw+\\|\\)"
1167 : monnier 707 (0 (save-excursion
1168 :     (save-match-data
1169 :     (goto-char (match-beginning 0))
1170 :     (unless (or (re-search-forward "\\<of\\>" (match-end 0) 'move)
1171 :     (progn (sml-forward-spaces)
1172 :     (not (looking-at "("))))
1173 : monnier 768 sml-yacc-bnf-face))))
1174 :     (4 font-lock-builtin-face t t))
1175 :     sml-lex-font-lock-keywords))
1176 : monnier 707 (defconst sml-yacc-font-lock-defaults
1177 : monnier 768 (cons 'sml-yacc-font-lock-keywords (cdr sml-font-lock-defaults)))
1178 : monnier 707
1179 :     (defun sml-yacc-indent-line ()
1180 :     "Indent current line of ML-Yacc code."
1181 :     (let ((savep (> (current-column) (current-indentation)))
1182 :     (indent (max (or (ignore-errors (sml-yacc-indentation)) 0) 0)))
1183 :     (if savep
1184 :     (save-excursion (indent-line-to indent))
1185 :     (indent-line-to indent))))
1186 :    
1187 :     (defun sml-yacc-indentation ()
1188 :     (save-excursion
1189 :     (back-to-indentation)
1190 :     (or (and (looking-at "%\\|\\(\\sw\\|\\s_\\)+\\s-*:") 0)
1191 :     (when (save-excursion
1192 :     (condition-case nil (progn (up-list -1) nil) (scan-error t)))
1193 :     ;; We're outside an action.
1194 :     (cond
1195 :     ;; Special handling of indentation inside %term and %nonterm
1196 :     ((save-excursion
1197 :     (and (re-search-backward "^%\\(\\sw+\\)" nil t)
1198 :     (member (match-string 1) '("term" "nonterm"))))
1199 :     (if (numberp sml-yacc-indent-term) sml-yacc-indent-term
1200 :     (let ((offset (if (looking-at "|") -2 0)))
1201 :     (forward-line -1)
1202 :     (looking-at "\\s-*\\(%\\sw*\\||\\)?\\s-*")
1203 :     (goto-char (match-end 0))
1204 :     (+ offset (current-column)))))
1205 :     ((looking-at "(") sml-yacc-indent-action)
1206 :     ((looking-at "|")
1207 :     (if (numberp sml-yacc-indent-pipe) sml-yacc-indent-pipe
1208 :     (backward-sexp 1)
1209 :     (while (progn (sml-backward-spaces)
1210 :     (/= 0 (skip-syntax-backward "w_"))))
1211 :     (sml-backward-spaces)
1212 :     (if (not (looking-at "\\s-$"))
1213 :     (1- (current-column))
1214 :     (skip-syntax-forward " ")
1215 :     (- (current-column) 2))))))
1216 :     ;; default to SML rules
1217 :     (sml-calculate-indentation))))
1218 :    
1219 :     ;;;###autoload
1220 :     (add-to-list 'auto-mode-alist '("\\.grm\\'" . sml-yacc-mode))
1221 :     ;;;###autoload
1222 :     (define-derived-mode sml-yacc-mode sml-mode "SML-Yacc"
1223 : monnier 768 "Major Mode for editing ML-Yacc files."
1224 : monnier 707 (set (make-local-variable 'indent-line-function) 'sml-yacc-indent-line)
1225 :     (set (make-local-variable 'font-lock-defaults) sml-yacc-font-lock-defaults))
1226 :    
1227 : monnier 319 (provide 'sml-mode)
1228 : monnier 535
1229 :     ;;; sml-mode.el ends here

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