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

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

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