Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /sml/trunk/sml-mode/sml-mode.el
ViewVC logotype

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 768 - (view) (download)

1 : monnier 535 ;;; sml-mode.el --- Major mode for editing (Standard) ML
2 : monnier 32
3 : monnier 541 ;; Copyright (C) 1989 Lars Bo Nielsen
4 :     ;; Copyright (C) 1994-1997 Matthew J. Morley
5 :     ;; Copyright (C) 1999-2000 Stefan Monnier
6 : monnier 319
7 : monnier 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 :     "*Indentation of blocks in ML (see also `sml-structure-indent')."
82 :     :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 :     :type '(boolean))
107 : monnier 32
108 :     ;;; OTHER GENERIC MODE VARIABLES
109 :    
110 :     (defvar sml-mode-info "sml-mode"
111 : monnier 535 "*Where to find Info file for `sml-mode'.
112 : monnier 32 The default assumes the info file \"sml-mode.info\" is on Emacs' info
113 : monnier 535 directory path. If it is not, either put the file on the standard path
114 :     or set the variable `sml-mode-info' to the exact location of this file
115 : monnier 32
116 : monnier 535 (setq sml-mode-info \"/usr/me/lib/info/sml-mode\")
117 : monnier 32
118 :     in your .emacs file. You can always set it interactively with the
119 :     set-variable command.")
120 :    
121 :     (defvar sml-mode-hook nil
122 : monnier 535 "*Run upon entering `sml-mode'.
123 : monnier 32 This is a good place to put your preferred key bindings.")
124 :    
125 : monnier 535 ;;; CODE FOR SML-MODE
126 : monnier 32
127 :     (defun sml-mode-info ()
128 : monnier 535 "Command to access the TeXinfo documentation for `sml-mode'.
129 :     See doc for the variable `sml-mode-info'."
130 : monnier 32 (interactive)
131 :     (require 'info)
132 :     (condition-case nil
133 : monnier 535 (info sml-mode-info)
134 : monnier 32 (error (progn
135 :     (describe-variable 'sml-mode-info)
136 :     (message "Can't find it... set this variable first!")))))
137 :    
138 :    
139 :     ;;; Autoload functions -- no-doc is another idea cribbed from AucTeX!
140 :    
141 : monnier 319 (let ((sml-no-doc
142 :     "This function is part of sml-proc, and has not yet been loaded.
143 :     Full documentation will be available after autoloading the function."))
144 : monnier 32
145 : monnier 332 (autoload 'sml-compile "sml-proc" sml-no-doc t)
146 :     (autoload 'sml-load-file "sml-proc" sml-no-doc t)
147 :     (autoload 'switch-to-sml "sml-proc" sml-no-doc t)
148 :     (autoload 'sml-send-region "sml-proc" sml-no-doc t)
149 :     (autoload 'sml-send-buffer "sml-proc" sml-no-doc t))
150 : monnier 32
151 : monnier 33 ;; font-lock setup
152 :    
153 : monnier 39 (defconst sml-keywords-regexp
154 : monnier 319 (sml-syms-re "abstraction" "abstype" "and" "andalso" "as" "before" "case"
155 :     "datatype" "else" "end" "eqtype" "exception" "do" "fn"
156 :     "fun" "functor" "handle" "if" "in" "include" "infix"
157 :     "infixr" "let" "local" "nonfix" "of" "op" "open" "orelse"
158 :     "overload" "raise" "rec" "sharing" "sig" "signature"
159 :     "struct" "structure" "then" "type" "val" "where" "while"
160 : monnier 535 "with" "withtype" "o")
161 : monnier 39 "A regexp that matches any and all keywords of SML.")
162 :    
163 : monnier 300 (defconst sml-font-lock-keywords
164 :     `(;;(sml-font-comments-and-strings)
165 : monnier 347 ("\\<\\(fun\\|and\\)\\s-+\\('\\sw+\\s-+\\)*\\(\\sw+\\)"
166 : monnier 33 (1 font-lock-keyword-face)
167 : monnier 394 (3 font-lock-function-name-face))
168 : monnier 347 ("\\<\\(\\(data\\|abs\\|with\\|eq\\)?type\\)\\s-+\\('\\sw+\\s-+\\)*\\(\\sw+\\)"
169 : monnier 33 (1 font-lock-keyword-face)
170 : monnier 319 (4 font-lock-type-def-face))
171 : monnier 544 ("\\<\\(val\\)\\s-+\\(\\sw+\\>\\s-*\\)?\\(\\sw+\\)\\s-*[=:]"
172 : monnier 33 (1 font-lock-keyword-face)
173 :     ;;(6 font-lock-variable-def-face nil t)
174 : monnier 394 (3 font-lock-variable-name-face))
175 : monnier 319 ("\\<\\(structure\\|functor\\|abstraction\\)\\s-+\\(\\sw+\\)"
176 : monnier 33 (1 font-lock-keyword-face)
177 :     (2 font-lock-module-def-face))
178 : monnier 319 ("\\<\\(signature\\)\\s-+\\(\\sw+\\)"
179 : monnier 33 (1 font-lock-keyword-face)
180 :     (2 font-lock-interface-def-face))
181 :    
182 : monnier 39 (,sml-keywords-regexp . font-lock-keyword-face))
183 : monnier 33 "Regexps matching standard SML keywords.")
184 :    
185 : monnier 394 (defface font-lock-type-def-face
186 :     '((t (:bold t)))
187 :     "Font Lock mode face used to highlight type definitions."
188 :     :group 'font-lock-highlighting-faces)
189 :     (defvar font-lock-type-def-face 'font-lock-type-def-face
190 :     "Face name to use for type definitions.")
191 : monnier 33
192 : monnier 394 (defface font-lock-module-def-face
193 :     '((t (:bold t)))
194 :     "Font Lock mode face used to highlight module definitions."
195 :     :group 'font-lock-highlighting-faces)
196 :     (defvar font-lock-module-def-face 'font-lock-module-def-face
197 :     "Face name to use for module definitions.")
198 :    
199 :     (defface font-lock-interface-def-face
200 :     '((t (:bold t)))
201 :     "Font Lock mode face used to highlight interface definitions."
202 :     :group 'font-lock-highlighting-faces)
203 :     (defvar font-lock-interface-def-face 'font-lock-interface-def-face
204 :     "Face name to use for interface definitions.")
205 :    
206 : monnier 707 ;;;
207 : monnier 535 ;;; Code to handle nested comments and unusual string escape sequences
208 : monnier 707 ;;;
209 : monnier 33
210 : monnier 535 (defsyntax sml-syntax-prop-table
211 :     '((?\\ . ".") (?* . "."))
212 :     "Syntax table for text-properties")
213 :    
214 :     ;; For Emacsen that have no built-in support for nested comments
215 : monnier 300 (defun sml-get-depth-st ()
216 :     (save-excursion
217 :     (let* ((disp (if (eq (char-before) ?\)) (progn (backward-char) -1) nil))
218 :     (foo (backward-char))
219 :     (disp (if (eq (char-before) ?\() (progn (backward-char) 0) disp))
220 :     (pt (point)))
221 :     (when disp
222 :     (let* ((depth
223 :     (save-match-data
224 :     (if (re-search-backward "\\*)\\|(\\*" nil t)
225 :     (+ (or (get-char-property (point) 'comment-depth) 0)
226 :     (case (char-after) (?\( 1) (?* 0))
227 :     disp)
228 :     0)))
229 :     (depth (if (> depth 0) depth)))
230 :     (put-text-property pt (1+ pt) 'comment-depth depth)
231 : monnier 319 (when depth sml-syntax-prop-table))))))
232 : monnier 300
233 :     (defconst sml-font-lock-syntactic-keywords
234 : monnier 535 `(("^\\s-*\\(\\\\\\)" (1 ',sml-syntax-prop-table))
235 :     ,@(unless sml-builtin-nested-comments-flag
236 :     '(("(?\\(\\*\\))?" (1 (sml-get-depth-st)))))))
237 : monnier 300
238 :     (defconst sml-font-lock-defaults
239 : monnier 319 '(sml-font-lock-keywords nil nil ((?_ . "w") (?' . "w")) nil
240 : monnier 707 (font-lock-syntactic-keywords . sml-font-lock-syntactic-keywords)))
241 : monnier 300
242 : monnier 707 ;;;;
243 :     ;;;; Imenu support
244 :     ;;;;
245 : monnier 33
246 : monnier 707 (defvar sml-imenu-regexp
247 :     (concat "^[ \t]*\\(let[ \t]+\\)?"
248 :     (regexp-opt (append sml-module-head-syms
249 :     '("and" "fun" "datatype" "abstype" "type")) t)
250 :     "\\>"))
251 :    
252 :     (defun sml-imenu-create-index ()
253 :     (let (alist)
254 :     (goto-char (point-max))
255 :     (while (re-search-backward sml-imenu-regexp nil t)
256 :     (save-excursion
257 :     (let ((kind (match-string 2))
258 :     (column (progn (goto-char (match-beginning 2)) (current-column)))
259 :     (location
260 :     (progn (goto-char (match-end 0)) (sml-forward-spaces) (point)))
261 :     (name (sml-forward-sym)))
262 :     ;; Eliminate trivial renamings.
263 :     (when (or (not (member kind '("structure" "signature")))
264 :     (progn (search-forward "=")
265 :     (sml-forward-spaces)
266 :     (looking-at "sig\\|struct")))
267 :     (push (cons (concat (make-string (/ column 2) ?\ ) name) location)
268 :     alist)))))
269 :     alist))
270 :    
271 : monnier 32 ;;; MORE CODE FOR SML-MODE
272 :    
273 : monnier 535 ;;;###Autoload
274 :     (add-to-list 'auto-mode-alist '("\\.s\\(ml\\|ig\\)\\'" . sml-mode))
275 : monnier 32
276 :     ;;;###Autoload
277 : monnier 535 (define-derived-mode sml-mode fundamental-mode "SML"
278 :     "\\<sml-mode-map>Major mode for editing ML code.
279 :     This mode runs `sml-mode-hook' just before exiting.
280 : monnier 32 \\{sml-mode-map}"
281 : monnier 535 (set (make-local-variable 'font-lock-defaults) sml-font-lock-defaults)
282 : monnier 319 (set (make-local-variable 'outline-regexp) sml-outline-regexp)
283 : monnier 707 (set (make-local-variable 'imenu-create-index-function)
284 :     'sml-imenu-create-index)
285 : monnier 700 (set (make-local-variable 'add-log-current-defun-function)
286 :     'sml-current-fun-name)
287 :     ;; forward-sexp-function is an experimental variable in my hacked Emacs.
288 :     (set (make-local-variable 'forward-sexp-function) 'sml-user-forward-sexp)
289 : monnier 768 ;; For XEmacs
290 :     (easy-menu-add sml-mode-menu)
291 :     ;; Compatibility
292 :     (unless (boundp 'skeleton-positions) (set (make-local-variable '@) nil))
293 : monnier 535 (sml-mode-variables))
294 : monnier 32
295 :     (defun sml-mode-variables ()
296 :     (set-syntax-table sml-mode-syntax-table)
297 :     (setq local-abbrev-table sml-mode-abbrev-table)
298 :     ;; A paragraph is separated by blank lines or ^L only.
299 : monnier 33
300 :     (set (make-local-variable 'paragraph-start)
301 :     (concat "^[\t ]*$\\|" page-delimiter))
302 :     (set (make-local-variable 'paragraph-separate) paragraph-start)
303 :     (set (make-local-variable 'indent-line-function) 'sml-indent-line)
304 :     (set (make-local-variable 'comment-start) "(* ")
305 :     (set (make-local-variable 'comment-end) " *)")
306 : monnier 535 (set (make-local-variable 'comment-nested) t)
307 : monnier 378 ;;(set (make-local-variable 'block-comment-start) "* ")
308 :     ;;(set (make-local-variable 'block-comment-end) "")
309 : monnier 33 (set (make-local-variable 'comment-column) 40)
310 : monnier 768 (set (make-local-variable 'comment-start-skip) "(\\*+\\s-*"))
311 : monnier 32
312 :     (defun sml-electric-pipe ()
313 : monnier 319 "Insert a \"|\".
314 : monnier 32 Depending on the context insert the name of function, a \"=>\" etc."
315 :     (interactive)
316 : monnier 319 (sml-with-ist
317 : monnier 334 (unless (save-excursion (skip-chars-backward "\t ") (bolp)) (insert "\n"))
318 :     (insert "| ")
319 : monnier 319 (let ((text
320 :     (save-excursion
321 : monnier 334 (backward-char 2) ;back over the just inserted "| "
322 : monnier 544 (let ((sym (sml-find-matching-starter sml-pipeheads
323 :     (sml-op-prec "|" 'back))))
324 :     (sml-forward-sym)
325 : monnier 334 (sml-forward-spaces)
326 :     (cond
327 :     ((string= sym "|")
328 :     (let ((f (sml-forward-sym)))
329 :     (sml-find-forward "\\(=>\\|=\\||\\)\\S.")
330 :     (cond
331 :     ((looking-at "|") "") ;probably a datatype
332 :     ((looking-at "=>") " => ") ;`case', or `fn' or `handle'
333 :     ((looking-at "=") (concat f " = "))))) ;a function
334 :     ((string= sym "and")
335 :     ;; could be a datatype or a function
336 :     (while (and (setq sym (sml-forward-sym))
337 :     (string-match "^'" sym))
338 :     (sml-forward-spaces))
339 :     (sml-forward-spaces)
340 :     (if (or (not sym)
341 :     (equal (sml-forward-sym) "d="))
342 :     ""
343 :     (concat sym " = ")))
344 :     ;; trivial cases
345 :     ((string= sym "fun")
346 :     (while (and (setq sym (sml-forward-sym))
347 :     (string-match "^'" sym))
348 :     (sml-forward-spaces))
349 :     (concat sym " = "))
350 : monnier 542 ((member sym '("case" "handle" "fn" "of")) " => ")
351 : monnier 544 ;;((member sym '("abstype" "datatype")) "")
352 :     (t ""))))))
353 : monnier 32
354 : monnier 334 (insert text)
355 : monnier 535 (indent-according-to-mode)
356 : monnier 319 (beginning-of-line)
357 :     (skip-chars-forward "\t |")
358 :     (skip-syntax-forward "w")
359 :     (skip-chars-forward "\t ")
360 :     (when (= ?= (char-after)) (backward-char)))))
361 :    
362 : monnier 32 (defun sml-electric-semi ()
363 : monnier 535 "Insert a \;.
364 :     If variable `sml-electric-semi-mode' is t, indent the current line, insert
365 : monnier 32 a newline, and indent."
366 :     (interactive)
367 :     (insert "\;")
368 :     (if sml-electric-semi-mode
369 :     (reindent-then-newline-and-indent)))
370 :    
371 :     ;;; INDENTATION !!!
372 :    
373 :     (defun sml-mark-function ()
374 : monnier 535 "Synonym for `mark-paragraph' -- sorry.
375 : monnier 32 If anyone has a good algorithm for this..."
376 :     (interactive)
377 :     (mark-paragraph))
378 :    
379 :     (defun sml-indent-line ()
380 :     "Indent current line of ML code."
381 :     (interactive)
382 : monnier 544 (let ((savep (> (current-column) (current-indentation)))
383 : monnier 700 (indent (max (or (ignore-errors (sml-calculate-indentation)) 0) 0)))
384 : monnier 544 (if savep
385 :     (save-excursion (indent-line-to indent))
386 :     (indent-line-to indent))))
387 : monnier 32
388 :     (defun sml-back-to-outer-indent ()
389 :     "Unindents to the next outer level of indentation."
390 :     (interactive)
391 :     (save-excursion
392 :     (beginning-of-line)
393 :     (skip-chars-forward "\t ")
394 :     (let ((start-column (current-column))
395 :     (indent (current-column)))
396 :     (if (> start-column 0)
397 :     (progn
398 :     (save-excursion
399 :     (while (>= indent start-column)
400 :     (if (re-search-backward "^[^\n]" nil t)
401 :     (setq indent (current-indentation))
402 :     (setq indent 0))))
403 :     (backward-delete-char-untabify (- start-column indent)))))))
404 :    
405 : monnier 39 (defun sml-find-comment-indent ()
406 :     (save-excursion
407 :     (let ((depth 1))
408 :     (while (> depth 0)
409 :     (if (re-search-backward "(\\*\\|\\*)" nil t)
410 :     (cond
411 :     ((looking-at "*)") (incf depth))
412 : monnier 300 ((looking-at comment-start-skip) (decf depth)))
413 : monnier 39 (setq depth -1)))
414 :     (if (= depth 0)
415 : monnier 334 (1+ (current-column))
416 : monnier 39 nil))))
417 :    
418 : monnier 32 (defun sml-calculate-indentation ()
419 :     (save-excursion
420 : monnier 319 (beginning-of-line) (skip-chars-forward "\t ")
421 :     (sml-with-ist
422 : monnier 334 ;; Indentation for comments alone on a line, matches the
423 :     ;; proper indentation of the next line.
424 : monnier 378 (when (looking-at "(\\*") (sml-forward-spaces))
425 : monnier 334 (let (data
426 :     (sml-point (point))
427 :     (sym (save-excursion (sml-forward-sym))))
428 : monnier 319 (or
429 : monnier 347 ;; allow the user to override the indentation
430 : monnier 378 (when (looking-at (concat ".*" (regexp-quote comment-start)
431 :     "[ \t]*fixindent[ \t]*"
432 :     (regexp-quote comment-end)))
433 :     (current-indentation))
434 : monnier 347
435 : monnier 319 ;; continued comment
436 : monnier 334 (and (looking-at "\\*") (sml-find-comment-indent))
437 : monnier 300
438 : monnier 319 ;; Continued string ? (Added 890113 lbn)
439 :     (and (looking-at "\\\\")
440 :     (save-excursion
441 :     (if (save-excursion (previous-line 1)
442 :     (beginning-of-line)
443 :     (looking-at "[\t ]*\\\\"))
444 :     (progn (previous-line 1) (current-indentation))
445 :     (if (re-search-backward "[^\\\\]\"" nil t)
446 : monnier 332 (1+ (current-column))
447 : monnier 319 0))))
448 : monnier 300
449 : monnier 334 (and (setq data (assoc sym sml-close-paren))
450 :     (sml-indent-relative sym data))
451 : monnier 32
452 : monnier 544 (and (member (save-excursion (sml-forward-sym)) sml-starters-syms)
453 : monnier 333 (let ((sym (unless (save-excursion (sml-backward-arg))
454 :     (sml-backward-spaces)
455 :     (sml-backward-sym))))
456 : monnier 319 (if sym (sml-get-sym-indent sym)
457 : monnier 334 ;; FIXME: this can take a *long* time !!
458 : monnier 544 (sml-find-matching-starter sml-starters-syms)
459 : monnier 319 (current-column))))
460 : monnier 300
461 : monnier 334 (and (string= sym "|") (sml-indent-pipe))
462 : monnier 300
463 : monnier 319 (sml-indent-arg)
464 :     (sml-indent-default))))))
465 : monnier 300
466 : monnier 334 (defun sml-indent-relative (sym data)
467 :     (save-excursion
468 :     (sml-forward-sym) (sml-backward-sexp nil)
469 : monnier 339 (unless (second data) (sml-backward-spaces) (sml-backward-sym))
470 : monnier 334 (+ (or (cdr (assoc sym sml-symbol-indent)) 0)
471 :     (sml-delegated-indent))))
472 :    
473 : monnier 319 (defun sml-indent-pipe ()
474 : monnier 544 (let ((sym (sml-find-matching-starter sml-pipeheads
475 :     (sml-op-prec "|" 'back))))
476 :     (when sym
477 :     (if (string= sym "|")
478 :     (if (sml-bolp) (current-column) (sml-indent-pipe))
479 :     (let ((pipe-indent (or (cdr (assoc "|" sml-symbol-indent)) -2)))
480 :     (when (member sym '("datatype" "abstype"))
481 :     (re-search-forward "="))
482 :     (sml-forward-sym)
483 :     (sml-forward-spaces)
484 :     (+ pipe-indent (current-column)))))))
485 : monnier 39
486 : monnier 334 (defun sml-find-forward (re)
487 :     (sml-forward-spaces)
488 :     (while (and (not (looking-at re))
489 :     (progn
490 :     (or (ignore-errors (forward-sexp 1) t) (forward-char 1))
491 :     (sml-forward-spaces)
492 :     (not (looking-at re))))))
493 : monnier 39
494 : monnier 319 (defun sml-indent-arg ()
495 :     (and (save-excursion (ignore-errors (sml-forward-arg)))
496 :     ;;(not (looking-at sml-not-arg-re))
497 :     ;; looks like a function or an argument
498 :     (sml-move-if (sml-backward-arg))
499 :     ;; an argument
500 :     (if (save-excursion (not (sml-backward-arg)))
501 :     ;; a first argument
502 :     (+ (current-column) sml-indent-args)
503 :     ;; not a first arg
504 :     (while (and (/= (current-column) (current-indentation))
505 :     (sml-move-if (sml-backward-arg))))
506 :     (unless (save-excursion (sml-backward-arg))
507 :     ;; all earlier args are on the same line
508 :     (sml-forward-arg) (sml-forward-spaces))
509 :     (current-column))))
510 :    
511 : monnier 334 (defun sml-get-indent (data sym)
512 :     (let ((head-sym (pop data)) d)
513 :     (cond
514 :     ((not (listp data)) data)
515 :     ((setq d (member sym data)) (second d))
516 :     ((and (consp data) (not (stringp (car data)))) (car data))
517 :     (t sml-indent-level))))
518 : monnier 332
519 : monnier 319 (defun sml-dangling-sym ()
520 : monnier 32 (save-excursion
521 : monnier 319 (and (not (sml-bolp))
522 :     (< (sml-point-after (end-of-line))
523 :     (sml-point-after (sml-forward-sym)
524 :     (sml-forward-spaces))))))
525 : monnier 32
526 : monnier 334 (defun sml-delegated-indent ()
527 :     (if (sml-dangling-sym)
528 :     (sml-indent-default 'noindent)
529 :     (sml-move-if (backward-word 1)
530 : monnier 341 (looking-at sml-agglomerate-re))
531 : monnier 334 (current-column)))
532 :    
533 : monnier 319 (defun sml-get-sym-indent (sym &optional style)
534 : monnier 535 "Find the indentation for the SYM we're `looking-at'.
535 : monnier 332 If indentation is delegated, the point will be at the start of
536 : monnier 535 the parent at the end of this function.
537 :     Optional argument STYLE is currently ignored"
538 : monnier 334 (assert (equal sym (save-excursion (sml-forward-sym))))
539 :     (save-excursion
540 :     (let ((delegate (assoc sym sml-close-paren))
541 :     (head-sym sym))
542 : monnier 339 (when (and delegate (not (eval (third delegate))))
543 : monnier 334 ;;(sml-find-match-backward sym delegate)
544 :     (sml-forward-sym) (sml-backward-sexp nil)
545 :     (setq head-sym
546 : monnier 339 (if (second delegate)
547 : monnier 334 (save-excursion (sml-forward-sym))
548 :     (sml-backward-spaces) (sml-backward-sym))))
549 : monnier 32
550 : monnier 334 (let ((idata (assoc head-sym sml-indent-rule)))
551 :     (when idata
552 :     ;;(if (or style (not delegate))
553 :     ;; normal indentation
554 :     (let ((indent (sml-get-indent idata sym)))
555 :     (when indent (+ (sml-delegated-indent) indent)))
556 :     ;; delgate indentation to the parent
557 :     ;;(sml-forward-sym) (sml-backward-sexp nil)
558 :     ;;(let* ((parent-sym (save-excursion (sml-forward-sym)))
559 :     ;; (parent-indent (cdr (assoc parent-sym sml-indent-starters))))
560 :     ;; check the special rules
561 :     ;;(+ (sml-delegated-indent)
562 :     ;; (or (sml-get-indent indent-data 1 'strict)
563 :     ;; (sml-get-indent parent-indent 1 'strict)
564 :     ;; (sml-get-indent indent-data 0)
565 :     ;; (sml-get-indent parent-indent 0))))))))
566 :     )))))
567 :    
568 : monnier 319 (defun sml-indent-default (&optional noindent)
569 : monnier 333 (let* ((sym-after (save-excursion (sml-forward-sym)))
570 : monnier 319 (_ (sml-backward-spaces))
571 : monnier 333 (sym-before (sml-backward-sym))
572 : monnier 332 (sym-indent (and sym-before (sml-get-sym-indent sym-before))))
573 : monnier 334 (if sym-indent
574 : monnier 339 ;; the previous sym is an indentation introducer: follow the rule
575 :     (let ((indent-after (or (cdr (assoc sym-after sml-symbol-indent)) 0)))
576 : monnier 544 (if noindent
577 :     ;;(current-column)
578 :     sym-indent
579 :     (+ sym-indent indent-after)))
580 : monnier 339 ;; default-default
581 :     (let* ((prec-after (sml-op-prec sym-after 'back))
582 :     (prec (or (sml-op-prec sym-before 'back) prec-after 100)))
583 :     ;; go back until you hit a symbol that has a lower prec than the
584 :     ;; "current one", or until you backed over a sym that has the same prec
585 :     ;; but is at the beginning of a line.
586 :     (while (and (not (sml-bolp))
587 :     (sml-move-if (sml-backward-sexp (1- prec)))
588 :     (not (sml-bolp)))
589 :     (while (sml-move-if (sml-backward-sexp prec))))
590 :     ;; the `noindent' case does back over an introductory symbol
591 :     ;; such as `fun', ...
592 :     (when noindent
593 :     (sml-move-if
594 :     (sml-backward-spaces)
595 : monnier 544 (member (sml-backward-sym) sml-starters-syms)))
596 : monnier 339 (current-column)))))
597 : monnier 32
598 : monnier 319
599 :     (defun sml-bolp ()
600 : monnier 32 (save-excursion
601 : monnier 319 (skip-chars-backward " \t|") (bolp)))
602 : monnier 32
603 :    
604 : monnier 319 ;; maybe `|' should be set to word-syntax in our temp syntax table ?
605 :     (defun sml-current-indentation ()
606 : monnier 32 (save-excursion
607 : monnier 319 (beginning-of-line)
608 :     (skip-chars-forward " \t|")
609 :     (current-column)))
610 : monnier 32
611 : monnier 300
612 : monnier 544 (defun sml-find-matching-starter (syms &optional prec)
613 :     (let (sym)
614 :     (ignore-errors
615 :     (while
616 :     (progn (sml-backward-sexp prec)
617 :     (setq sym (save-excursion (sml-forward-sym)))
618 :     (not (or (member sym syms) (bobp)))))
619 :     (unless (bobp) sym))))
620 : monnier 319
621 : monnier 700 (defun sml-skip-siblings ()
622 :     (while (and (not (bobp)) (sml-backward-arg))
623 :     (sml-find-matching-starter sml-starters-syms))
624 :     (when (looking-at "in\\>\\|local\\>")
625 :     ;;skip over `local...in' and continue
626 :     (forward-word 1)
627 :     (sml-backward-sexp nil)
628 :     (sml-skip-siblings)))
629 :    
630 :     (defun sml-beginning-of-defun ()
631 :     (let ((sym (sml-find-matching-starter sml-starters-syms)))
632 : monnier 707 (if (member sym '("fun" "and" "functor" "signature" "structure"
633 : monnier 700 "abstraction" "datatype" "abstype"))
634 :     (save-excursion (sml-forward-sym) (sml-forward-spaces)
635 :     (sml-forward-sym))
636 :     ;; We're inside a "non function declaration": let's skip all other
637 :     ;; declarations that we find at the same level and try again.
638 :     (sml-skip-siblings)
639 :     ;; Obviously, let's not try again if we're at bobp.
640 :     (unless (bobp) (sml-beginning-of-defun)))))
641 :    
642 :     (defcustom sml-max-name-components 3
643 :     "Maximum number of components to use for the current function name."
644 :     :group 'sml
645 :     :type 'integer)
646 :    
647 :     (defun sml-current-fun-name ()
648 :     (save-excursion
649 :     (let ((count sml-max-name-components)
650 :     fullname name)
651 :     (end-of-line)
652 :     (while (and (> count 0)
653 :     (setq name (sml-beginning-of-defun)))
654 :     (decf count)
655 :     (setq fullname (if fullname (concat name "." fullname) name))
656 :     ;; Skip all other declarations that we find at the same level.
657 :     (sml-skip-siblings))
658 :     fullname)))
659 :    
660 : monnier 32
661 : monnier 535 ;;; INSERTING PROFORMAS (COMMON SML-FORMS)
662 : monnier 32
663 : monnier 334 (defvar sml-forms-alist nil
664 : monnier 535 "*Alist of code templates.
665 :     You can extend this alist to your heart's content. For each additional
666 : monnier 32 template NAME in the list, declare a keyboard macro or function (or
667 :     interactive command) called 'sml-form-NAME'.
668 :     If 'sml-form-NAME' is a function it takes no arguments and should
669 :     insert the template at point\; if this is a command it may accept any
670 :     sensible interactive call arguments\; keyboard macros can't take
671 : monnier 535 arguments at all. Apropos keyboard macros, see `name-last-kbd-macro'
672 : monnier 32 and `sml-addto-forms-alist'.
673 :     `sml-forms-alist' understands let, local, case, abstype, datatype,
674 :     signature, structure, and functor by default.")
675 :    
676 : monnier 334 (defmacro sml-def-skeleton (name interactor &rest elements)
677 : monnier 768 (when (fboundp 'define-skeleton)
678 :     (let ((fsym (intern (concat "sml-form-" name))))
679 :     `(progn
680 :     (add-to-list 'sml-forms-alist ',(cons name fsym))
681 :     (define-abbrev sml-mode-abbrev-table ,name "" ',fsym)
682 :     (define-skeleton ,fsym
683 :     ,(format "SML-mode skeleton for `%s..' expressions" name)
684 :     ,interactor
685 :     ,(concat name " ") >
686 :     ,@elements)))))
687 : monnier 334 (put 'sml-def-skeleton 'lisp-indent-function 2)
688 : monnier 32
689 : monnier 334 (sml-def-skeleton "let" nil
690 : monnier 768 @ "\nin " > _ "\nend" >)
691 : monnier 32
692 : monnier 334 (sml-def-skeleton "if" nil
693 : monnier 768 @ " then " > _ "\nelse " > _)
694 : monnier 32
695 : monnier 334 (sml-def-skeleton "local" nil
696 : monnier 768 @ "\nin" > _ "\nend" >)
697 : monnier 32
698 : monnier 334 (sml-def-skeleton "case" "Case expr: "
699 : monnier 341 str "\nof " > _ " => ")
700 : monnier 32
701 : monnier 334 (sml-def-skeleton "signature" "Signature name: "
702 :     str " =\nsig" > "\n" > _ "\nend" >)
703 : monnier 32
704 : monnier 334 (sml-def-skeleton "structure" "Structure name: "
705 :     str " =\nstruct" > "\n" > _ "\nend" >)
706 : monnier 32
707 : monnier 334 (sml-def-skeleton "functor" "Functor name: "
708 :     str " () : =\nstruct" > "\n" > _ "\nend" >)
709 :    
710 : monnier 700 (sml-def-skeleton "datatype" "Datatype name and type params: "
711 : monnier 334 str " =" \n)
712 :    
713 : monnier 700 (sml-def-skeleton "abstype" "Abstype name and type params: "
714 : monnier 334 str " =" \n _ "\nwith" > "\nend" >)
715 :    
716 :     ;;
717 :    
718 : monnier 700 (sml-def-skeleton "struct" nil
719 :     _ "\nend" >)
720 :    
721 :     (sml-def-skeleton "sig" nil
722 :     _ "\nend" >)
723 :    
724 :     (sml-def-skeleton "val" nil
725 : monnier 768 @ " = " > _)
726 : monnier 700
727 :     (sml-def-skeleton "fn" nil
728 : monnier 768 @ " =>" > _)
729 : monnier 700
730 :     (sml-def-skeleton "fun" nil
731 : monnier 768 @ " =" > _)
732 : monnier 700
733 :     ;;
734 :    
735 : monnier 334 (defun sml-forms-menu (menu)
736 : monnier 768 (mapcar (lambda (x)
737 :     (let ((name (car x))
738 :     (fsym (cdr x)))
739 :     (vector name fsym t)))
740 :     sml-forms-alist))
741 : monnier 334
742 :     (defvar sml-last-form "let")
743 :    
744 : monnier 341 (defun sml-electric-space ()
745 :     "Expand a symbol into an SML form, or just insert a space.
746 :     If the point directly precedes a symbol for which an SML form exists,
747 :     the corresponding form is inserted."
748 :     (interactive)
749 : monnier 700 (let ((abbrev-mode (not abbrev-mode))
750 :     (last-command-char ?\ )
751 :     ;; Bind `this-command' to fool skeleton's special abbrev handling.
752 :     (this-command 'self-insert-command))
753 :     (call-interactively 'self-insert-command)))
754 : monnier 341
755 : monnier 334 (defun sml-insert-form (name newline)
756 : monnier 535 "Interactive short-cut to insert the NAME common ML form.
757 :     If a prefix argument is given insert a NEWLINE and indent first, or
758 : monnier 32 just move to the proper indentation if the line is blank\; otherwise
759 :     insert at point (which forces indentation to current column).
760 :    
761 :     The default form to insert is 'whatever you inserted last time'
762 : monnier 535 \(just hit return when prompted\)\; otherwise the command reads with
763 : monnier 32 completion from `sml-forms-alist'."
764 : monnier 334 (interactive
765 :     (list (completing-read
766 :     (format "Form to insert: (default %s) " sml-last-form)
767 :     sml-forms-alist nil t nil)
768 :     current-prefix-arg))
769 :     ;; default is whatever the last insert was...
770 :     (if (string= name "") (setq name sml-last-form) (setq sml-last-form name))
771 :     (unless (or (not newline)
772 :     (save-excursion (beginning-of-line) (looking-at "\\s-*$")))
773 :     (insert "\n"))
774 : monnier 341 (unless (/= ?w (char-syntax (char-before))) (insert " "))
775 : monnier 334 (let ((f (cdr (assoc name sml-forms-alist))))
776 :     (cond
777 :     ((commandp f) (command-execute f))
778 :     (f (funcall f))
779 :     (t (error "Undefined form: %s" name)))))
780 : monnier 32
781 : monnier 334 ;; See also macros.el in emacs lisp dir.
782 : monnier 32
783 : monnier 334 (defun sml-addto-forms-alist (name)
784 :     "Assign a name to the last keyboard macro defined.
785 :     Argument NAME is transmogrified to sml-form-NAME which is the symbol
786 :     actually defined.
787 : monnier 32
788 : monnier 334 The symbol's function definition becomes the keyboard macro string.
789 : monnier 32
790 : monnier 334 If that works, NAME is added to `sml-forms-alist' so you'll be able to
791 : monnier 535 reinvoke the macro through \\[sml-insert-form]. You might want to save
792 : monnier 334 the macro to use in a later editing session -- see `insert-kbd-macro'
793 :     and add these macros to your .emacs file.
794 : monnier 32
795 : monnier 334 See also `edit-kbd-macro' which is bound to \\[edit-kbd-macro]."
796 :     (interactive "sName for last kbd macro (\"sml-form-\" will be added): ")
797 :     (when (string= name "") (error "No command name given"))
798 :     (let ((fsym (intern (concat "sml-form-" name))))
799 :     (name-last-kbd-macro fsym)
800 :     (message "Macro bound to %s" fsym)
801 :     (add-to-list 'sml-forms-alist (cons name fsym))))
802 : monnier 32
803 : monnier 707 ;;;;
804 :     ;;;; SML/NJ's Compilation Manager support
805 :     ;;;;
806 : monnier 32
807 : monnier 768 (defvar sml-cm-mode-syntax-table sml-mode-syntax-table)
808 :     (defvar sml-cm-font-lock-keywords
809 :     `(,(concat "\\<" (regexp-opt '("library" "group" "is" "structure"
810 :     "functor" "signature" "funsig") t)
811 :     "\\>")))
812 : monnier 535 ;;;###autoload
813 :     (add-to-list 'completion-ignored-extensions "CM/")
814 :     ;;;###autoload
815 :     (add-to-list 'auto-mode-alist '("\\.cm\\'" . sml-cm-mode))
816 :     ;;;###autoload
817 : monnier 768 (define-derived-mode sml-cm-mode fundamental-mode "SML-CM"
818 :     "Major mode for SML/NJ's Compilation Manager configuration files."
819 :     (local-set-key "\C-c\C-c" 'sml-compile)
820 :     (set (make-local-variable 'font-lock-defaults)
821 :     '(sml-cm-font-lock-keywords nil t nil nil)))
822 : monnier 32
823 : monnier 707 ;;;;
824 : monnier 768 ;;;; ML-Lex support
825 : monnier 707 ;;;;
826 : monnier 535
827 : monnier 768 (defvar sml-lex-font-lock-keywords
828 :     (append
829 :     '(("^%\\sw+" . font-lock-builtin-face)
830 :     ("^%%" . font-lock-module-def-face))
831 :     sml-font-lock-keywords))
832 :     (defconst sml-lex-font-lock-defaults
833 :     (cons 'sml-lex-font-lock-keywords (cdr sml-font-lock-defaults)))
834 :    
835 : monnier 707 ;;;###autoload
836 : monnier 768 (define-derived-mode sml-lex-mode sml-mode "SML-Lex"
837 :     "Major Mode for editing ML-Lex files."
838 :     (set (make-local-variable 'font-lock-defaults) sml-lex-font-lock-defaults))
839 : monnier 707
840 : monnier 768 ;;;;
841 :     ;;;; ML-Yacc support
842 :     ;;;;
843 :    
844 : monnier 707 (defface sml-yacc-bnf-face
845 :     '((t (:foreground "darkgreen")))
846 :     "Face used to highlight (non)terminals in `sml-yacc-mode'.")
847 :     (defvar sml-yacc-bnf-face 'sml-yacc-bnf-face)
848 :    
849 :     (defcustom sml-yacc-indent-action 16
850 :     "Indentation column of the opening paren of actions."
851 :     :group 'sml
852 :     :type 'integer)
853 :    
854 :     (defcustom sml-yacc-indent-pipe nil
855 :     "Indentation column of the pipe char in the BNF.
856 :     If nil, align it with `:' or with previous cases."
857 :     :group 'sml
858 :     :type 'integer)
859 :    
860 :     (defcustom sml-yacc-indent-term nil
861 :     "Indentation column of the (non)term part.
862 :     If nil, align it with previous cases."
863 :     :group 'sml
864 :     :type 'integer)
865 :    
866 :     (defvar sml-yacc-font-lock-keywords
867 : monnier 768 (cons '("^\\(\\sw+\\s-*:\\|\\s-*|\\)\\(\\s-*\\sw+\\)*\\s-*\\(\\(%\\sw+\\)\\s-+\\sw+\\|\\)"
868 : monnier 707 (0 (save-excursion
869 :     (save-match-data
870 :     (goto-char (match-beginning 0))
871 :     (unless (or (re-search-forward "\\<of\\>" (match-end 0) 'move)
872 :     (progn (sml-forward-spaces)
873 :     (not (looking-at "("))))
874 : monnier 768 sml-yacc-bnf-face))))
875 :     (4 font-lock-builtin-face t t))
876 :     sml-lex-font-lock-keywords))
877 : monnier 707 (defconst sml-yacc-font-lock-defaults
878 : monnier 768 (cons 'sml-yacc-font-lock-keywords (cdr sml-font-lock-defaults)))
879 : monnier 707
880 :     (defun sml-yacc-indent-line ()
881 :     "Indent current line of ML-Yacc code."
882 :     (let ((savep (> (current-column) (current-indentation)))
883 :     (indent (max (or (ignore-errors (sml-yacc-indentation)) 0) 0)))
884 :     (if savep
885 :     (save-excursion (indent-line-to indent))
886 :     (indent-line-to indent))))
887 :    
888 :     (defun sml-yacc-indentation ()
889 :     (save-excursion
890 :     (back-to-indentation)
891 :     (or (and (looking-at "%\\|\\(\\sw\\|\\s_\\)+\\s-*:") 0)
892 :     (when (save-excursion
893 :     (condition-case nil (progn (up-list -1) nil) (scan-error t)))
894 :     ;; We're outside an action.
895 :     (cond
896 :     ;; Special handling of indentation inside %term and %nonterm
897 :     ((save-excursion
898 :     (and (re-search-backward "^%\\(\\sw+\\)" nil t)
899 :     (member (match-string 1) '("term" "nonterm"))))
900 :     (if (numberp sml-yacc-indent-term) sml-yacc-indent-term
901 :     (let ((offset (if (looking-at "|") -2 0)))
902 :     (forward-line -1)
903 :     (looking-at "\\s-*\\(%\\sw*\\||\\)?\\s-*")
904 :     (goto-char (match-end 0))
905 :     (+ offset (current-column)))))
906 :     ((looking-at "(") sml-yacc-indent-action)
907 :     ((looking-at "|")
908 :     (if (numberp sml-yacc-indent-pipe) sml-yacc-indent-pipe
909 :     (backward-sexp 1)
910 :     (while (progn (sml-backward-spaces)
911 :     (/= 0 (skip-syntax-backward "w_"))))
912 :     (sml-backward-spaces)
913 :     (if (not (looking-at "\\s-$"))
914 :     (1- (current-column))
915 :     (skip-syntax-forward " ")
916 :     (- (current-column) 2))))))
917 :     ;; default to SML rules
918 :     (sml-calculate-indentation))))
919 :    
920 :     ;;;###autoload
921 :     (add-to-list 'auto-mode-alist '("\\.grm\\'" . sml-yacc-mode))
922 :     ;;;###autoload
923 :     (define-derived-mode sml-yacc-mode sml-mode "SML-Yacc"
924 : monnier 768 "Major Mode for editing ML-Yacc files."
925 : monnier 707 (set (make-local-variable 'indent-line-function) 'sml-yacc-indent-line)
926 :     (set (make-local-variable 'font-lock-defaults) sml-yacc-font-lock-defaults))
927 :    
928 : monnier 319 (provide 'sml-mode)
929 : monnier 535
930 :     ;;; sml-mode.el ends here

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