]> code.delx.au - gnu-emacs-elpa/blob - sml-proc.el
* Makefile: updated to the version of pcl-cvs.
[gnu-emacs-elpa] / sml-proc.el
1 ;;; sml-proc.el. Comint based interaction mode for Standard ML.
2
3 (defconst rcsid-sml-proc "@(#)$Name$:$Id$")
4
5 ;; Copyright (C) 1989, Lars Bo Nielsen, 1994,1997 Matthew J. Morley
6
7 ;; $Revision$
8 ;; $Date$
9
10 ;; ====================================================================
11
12 ;; This file is not part of GNU Emacs, but it is distributed under the
13 ;; same conditions.
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 0139, USA.
28 ;; (See sml-mode.el for HISTORY.)
29
30 ;; ====================================================================
31
32 ;; [MJM 10/94] Separating this from sml-mode means sml-mode will run
33 ;; under 18.59 (or anywhere without comint, if there are such places).
34 ;; See sml-mode.el for further information.
35
36 ;;; DESCRIPTION
37
38 ;; Inferior-sml-mode is for interacting with an ML process run under
39 ;; emacs. This uses the comint package so you get history, expansion,
40 ;; backup and all the other benefits of comint. Interaction is
41 ;; achieved by M-x run-sml which starts a sub-process under emacs. You may
42 ;; need to set this up for autoloading in your .emacs:
43
44 ;; (autoload 'run-sml "sml-proc" "Run an inferior ML process." t)
45
46 ;; Exactly what process is governed by the variable sml-program-name
47 ;; -- just "sml" by default. If you give a prefix argument (C-u M-x
48 ;; run-sml) you will be prompted for a different program to execute from
49 ;; the default -- if you just hit RETURN you get the default anyway --
50 ;; along with the option to specify any command line arguments. Once
51 ;; you select the ML program name in this manner, it remains the
52 ;; default (unless you set in a hook, or otherwise).
53
54 ;; NOTE: inferior-sml-mode-hook is run AFTER the ML program has been
55 ;; launched. inferior-sml-load-hook is run only when sml-proc.el is
56 ;; loaded into Emacs.
57
58 ;; When running an ML process some further key-bindings are effective
59 ;; in sml-mode buffer(s). C-c C-s (switch-to-sml) will split the
60 ;; screen into two windows if necessary and place you in the ML
61 ;; process buffer. In the interaction buffer, C-c C-s is bound to the
62 ;; `sml' command by default (in case you need to restart).
63
64 ;; C-c C-l (sml-load-file) will load an SML source file into the
65 ;; inferior process, C-c C-r (sml-send-region) will send the current
66 ;; region of text to the ML process, etc. Given a prefix argument to
67 ;; these commands will switch you from the SML buffer to the ML
68 ;; process buffer as well as sending the text. If you get errors
69 ;; reported by the compiler, C-x ` (next-error) will step through
70 ;; the errors with you.
71
72 ;; NOTE. There is only limited support for this as it obviously
73 ;; depends on the compiler's error messages being recognised by the
74 ;; mode. Error reporting is currently only geared up for SML/NJ,
75 ;; Moscow ML, and Poly/ML. For other compilers, add the relevant
76 ;; regexp to sml-error-regexp-alist and send it to me.
77
78 ;; To send pieces of code to the underlying compiler, we never send the text
79 ;; directly but use a temporary file instead. This breaks if the compiler
80 ;; does not understand `use', but has the benefit of allowing better error
81 ;; reporting.
82
83 ;; ===================================================================
84
85 ;;; INFERIOR ML MODE VARIABLES
86
87 (require 'sml-mode)
88 (require 'sml-util)
89 (require 'comint)
90 (require 'compile)
91
92 (defgroup sml-proc ()
93 "Interacting with an SML process."
94 :group 'sml)
95
96 (defcustom sml-program-name "sml"
97 "*Program to run as ML."
98 :group 'sml-proc
99 :type '(string))
100
101 (defcustom sml-default-arg ""
102 "*Default command line option to pass, if any."
103 :group 'sml-proc
104 :type '(string))
105
106 (defvar sml-compile-command "CM.make()"
107 "The command used by default by `sml-make'.")
108
109 (defvar sml-make-file-name "sources.cm"
110 "The name of the makefile that `sml-make' will look for (if non-nil).")
111
112 ;;(defvar sml-raise-on-error nil
113 ;; "*When non-nil, `sml-next-error' will raise the ML process's frame.")
114
115 (defvar inferior-sml-mode-hook nil
116 "*This hook is run when the inferior ML process is started.
117 All buffer local customisations for the interaction buffers go here.")
118
119 (defvar inferior-sml-load-hook nil
120 "*Hook run when inferior-sml-mode (sml-proc.el) is loaded into Emacs.
121 This is a good place to put your preferred key bindings.")
122
123 (defvar sml-error-overlay nil
124 "*Non-nil means use an overlay to highlight errorful code in the buffer.
125 The actual value is the name of a face to use for the overlay.
126 Instead of setting this variable to 'region, you can also simply keep
127 it NIL and use (transient-mark-mode) which will provide similar
128 benefits (but with several side effects).")
129
130 (defvar sml-buffer nil
131 "*The current ML process buffer.
132
133 MULTIPLE PROCESS SUPPORT (Whoever wants multi-process support anyway?)
134 =====================================================================
135 sml-mode supports, in a fairly simple fashion, running multiple ML
136 processes. To run multiple ML processes, you start the first up with
137 \\[sml]. It will be in a buffer named *sml*. Rename this buffer with
138 \\[rename-buffer]. You may now start up a new process with another
139 \\[sml]. It will be in a new buffer, named *sml*. You can switch
140 between the different process buffers with \\[switch-to-buffer].
141
142 NB *sml* is just the default name for the buffer. It actually gets
143 it's name from the value of `sml-program-name' -- *poly*, *smld*,...
144
145 If you have more than one ML process around, commands that send text
146 from source buffers to ML processes -- like `sml-send-function' or
147 `sml-send-region' -- have to choose a process to send it to. This is
148 determined by the global variable `sml-buffer'. Suppose you have three
149 inferior ML's running:
150 Buffer Process
151 sml #<process sml>
152 mosml #<process mosml>
153 *sml* #<process sml<2>>
154 If you do a \\[sml-send-function] command on some ML source code,
155 what process do you send it to?
156
157 - If you're in a process buffer (sml, mosml, or *sml*), you send it to
158 that process (usually makes sense only to `sml-load-file').
159 - If you're in some other buffer (e.g., a source file), you send it to
160 the process attached to buffer `sml-buffer'.
161
162 This process selection is performed by function `sml-proc' which looks
163 at the value of `sml-buffer' -- which must be a lisp buffer object, or
164 a string \(or nil\).
165
166 Whenever \\[sml] fires up a new process, it resets `sml-buffer' to be
167 the new process's buffer. If you only run one process, this will do
168 the right thing. If you run multiple processes, you can change
169 `sml-buffer' to another process buffer with \\[set-variable], or
170 use the command \\[sml-buffer] in the interaction buffer of choice.")
171
172
173 ;;; ALL STUFF THAT DEFAULTS TO THE SML/NJ COMPILER (0.93)
174
175 (defvar sml-use-command "use \"%s\""
176 "*Template for loading a file into the inferior ML process.
177 Set to \"use \\\"%s\\\"\" for SML/NJ or Edinburgh ML;
178 set to \"PolyML.use \\\"%s\\\"\" for Poly/ML, etc.")
179
180 (defvar sml-cd-command "OS.FileSys.chDir \"%s\""
181 "*Command template for changing working directories under ML.
182 Set this to nil if your compiler can't change directories.
183
184 The format specifier \"%s\" will be converted into the directory name
185 specified when running the command \\[sml-cd].")
186
187 (defcustom sml-prompt-regexp "^[-=>#] *"
188 "*Regexp used to recognise prompts in the inferior ML process."
189 :group 'sml-proc
190 :type '(regexp))
191
192 (defconst sml-error-regexp-alist
193 '(;; Poly/ML messages
194 ("\\(Error\\|Warning:\\) in '\\(.+\\)', line \\([0-9]+\\)" 2 3)
195 ;; Moscow ML
196 ("File \"\\([^\"]+\\)\", line \\([0-9]+\\)\\(-\\([0-9]+\\)\\)?, characters \\([0-9]+\\)-\\([0-9]+\\):" 1 2 5)
197 ;; SML/NJ: the file-pattern is anchored to avoid
198 ;; pathological behavior with very long lines.
199 ("^[-= ]*\\(.+\\):\\([0-9]+\\)\\.\\([0-9]+\\)\\(-\\([0-9]+\\)\\.\\([0-9]+\\)\\)? \\(Error\\|Warning\\): .*" 1 sml-make-error 2 3 5 6)
200 ;; SML/NJ's exceptions: see above.
201 ("^ +\\(raised at: \\)?\\(.+\\):\\([0-9]+\\)\\.\\([0-9]+\\)\\(-\\([0-9]+\\)\\.\\([0-9]+\\)\\)" 2 sml-make-error 3 4 6 7)))
202
203 ;; font-lock support
204 (defconst inferior-sml-font-lock-keywords
205 `(;; prompt and following interactive command
206 (,(concat "\\(" sml-prompt-regexp "\\)\\(.*\\)")
207 (1 font-lock-prompt-face)
208 (2 font-lock-command-face keep))
209 ;; CM's messages
210 ("^\\[\\(.*GC #.*\n\\)*.*\\]" . font-lock-comment-face)
211 ;; SML/NJ's irritating GC messages
212 ("^GC #.*" . font-lock-comment-face)
213 ;; error messages
214 ,@(mapcar (lambda (ra) (cons (car ra) 'font-lock-warning-face))
215 sml-error-regexp-alist))
216 "Font-locking specification for inferior SML mode.")
217
218 (defface font-lock-prompt-face
219 '((t (:bold t)))
220 "Font Lock mode face used to highlight prompts."
221 :group 'font-lock-highlighting-faces)
222 (defvar font-lock-prompt-face 'font-lock-prompt-face
223 "Face name to use for prompts.")
224
225 (defface font-lock-command-face
226 '((t (:bold t)))
227 "Font Lock mode face used to highlight interactive commands."
228 :group 'font-lock-highlighting-faces)
229 (defvar font-lock-command-face 'font-lock-command-face
230 "Face name to use for interactive commands.")
231
232 (defconst inferior-sml-font-lock-defaults
233 '(inferior-sml-font-lock-keywords nil nil nil nil))
234
235 ;;; CODE
236
237 (defmap inferior-sml-mode-map
238 '(("\C-c\C-s" . run-sml)
239 ("\t" . comint-dynamic-complete))
240 "Keymap for inferior-sml mode"
241 :inherit (list sml-bindings comint-mode-map)
242 :group 'sml-proc)
243
244
245 ;; buffer-local
246
247 (defvar sml-temp-file nil)
248 ;;(defvar sml-error-file nil) ; file from which the last error came
249 (defvar sml-error-cursor nil) ; ditto
250
251 (defun sml-proc-buffer ()
252 "Returns the current ML process buffer,
253 or the current buffer if it is in `inferior-sml-mode'. Raises an error
254 if the variable `sml-buffer' does not appear to point to an existing
255 buffer."
256 (or (and (eq major-mode 'inferior-sml-mode) (current-buffer))
257 (and sml-buffer
258 (let ((buf (get-buffer sml-buffer)))
259 ;; buffer-name returns nil if the buffer has been killed
260 (and buf (buffer-name buf) buf)))
261 ;; no buffer found, make a new one
262 (run-sml t)))
263
264 (defun sml-proc ()
265 "Returns the current ML process. See variable `sml-buffer'."
266 (assert (eq major-mode 'inferior-sml-mode))
267 (or (get-buffer-process (current-buffer))
268 (progn (run-sml t) (get-buffer-process (current-buffer)))))
269
270 (defun sml-buffer (echo)
271 "Make the current buffer the current `sml-buffer' if that is sensible.
272 Lookup variable `sml-buffer' to see why this might be useful."
273 (interactive "P")
274 (when (and (not echo) (eq major-mode 'inferior-sml-mode))
275 (setq sml-buffer (current-buffer)))
276 (message "ML process buffer is %s."
277 (or (ignore-errors (buffer-name (get-buffer sml-buffer)))
278 "undefined")))
279
280 (defun inferior-sml-mode ()
281 "Major mode for interacting with an inferior ML process.
282
283 The following commands are available:
284 \\{inferior-sml-mode-map}
285
286 An ML process can be fired up (again) with \\[sml].
287
288 Customisation: Entry to this mode runs the hooks on `comint-mode-hook'
289 and `inferior-sml-mode-hook' (in that order).
290
291 Variables controlling behaviour of this mode are
292
293 `sml-program-name' (default \"sml\")
294 Program to run as ML.
295
296 `sml-use-command' (default \"use \\\"%s\\\"\")
297 Template for loading a file into the inferior ML process.
298
299 `sml-cd-command' (default \"System.Directory.cd \\\"%s\\\"\")
300 ML command for changing directories in ML process (if possible).
301
302 `sml-prompt-regexp' (default \"^[\\-=] *\")
303 Regexp used to recognise prompts in the inferior ML process.
304
305 You can send text to the inferior ML process from other buffers containing
306 ML source.
307 `switch-to-sml' switches the current buffer to the ML process buffer.
308 `sml-send-function' sends the current *paragraph* to the ML process.
309 `sml-send-region' sends the current region to the ML process.
310
311 Prefixing the sml-send-<whatever> commands with \\[universal-argument]
312 causes a switch to the ML process buffer after sending the text.
313
314 For information on running multiple processes in multiple buffers, see
315 documentation for variable `sml-buffer'.
316
317 Commands:
318 RET after the end of the process' output sends the text from the
319 end of process to point.
320 RET before the end of the process' output copies the current line
321 to the end of the process' output, and sends it.
322 DEL converts tabs to spaces as it moves back.
323 TAB file name completion, as in shell-mode, etc.."
324 (interactive)
325 (kill-all-local-variables)
326 (comint-mode)
327 (setq comint-prompt-regexp sml-prompt-regexp)
328 (sml-mode-variables)
329
330 ;; For sequencing through error messages:
331 (set (make-local-variable 'sml-error-cursor) (point-max-marker))
332 (set-marker-insertion-type sml-error-cursor nil)
333 (set (make-local-variable 'font-lock-defaults)
334 inferior-sml-font-lock-defaults)
335
336 ;; compilation support (used for next-error)
337 (set (make-local-variable 'compilation-error-regexp-alist)
338 sml-error-regexp-alist)
339 (compilation-shell-minor-mode 1)
340 ;; I'm sure people might kill me for that
341 (setq compilation-error-screen-columns nil)
342 (make-local-variable 'sml-endof-error-alist)
343 ;;(make-local-variable 'sml-error-overlay)
344
345 (setq major-mode 'inferior-sml-mode)
346 (setq mode-name "Inferior ML")
347 (setq mode-line-process '(": %s"))
348 (use-local-map inferior-sml-mode-map)
349 ;;(add-hook 'kill-emacs-hook 'sml-temp-tidy)
350
351 (run-hooks 'inferior-sml-mode-hook))
352
353 ;;; FOR RUNNING ML FROM EMACS
354
355 ;;;###autoload
356 (defun run-sml (&optional pfx)
357 "Run an inferior ML process, input and output via buffer *sml*.
358 With a prefix argument, this command allows you to specify any command
359 line options to pass to the complier. The command runs hook functions
360 on `comint-mode-hook' and `inferior-sml-mode-hook' in that order.
361
362 If there is a process already running in *sml*, just switch to that
363 buffer instead.
364
365 In fact the name of the buffer created is chosen to reflect the name
366 of the program name specified by `sml-program-name', or entered at the
367 prompt. You can have several inferior ML process running, but only one
368 current one -- given by `sml-buffer' (qv).
369
370 \(Type \\[describe-mode] in the process buffer for a list of commands.)"
371 (interactive "P")
372 (let ((cmd (if pfx
373 (read-string "ML command: " sml-program-name)
374 sml-program-name))
375 (args (if pfx
376 (read-string "Any args: " sml-default-arg)
377 sml-default-arg)))
378 (sml-run cmd args)))
379
380 (defun sml-run (cmd arg)
381 "Run the ML program CMD with given arguments ARGS.
382 This usually updates `sml-buffer' to a buffer named *CMD*."
383 (let* ((pname (file-name-nondirectory cmd))
384 (args (if (equal arg "") () (sml-args-to-list arg))))
385 ;; and this -- to keep these as defaults even if
386 ;; they're set in the mode hooks.
387 (setq sml-program-name cmd)
388 (setq sml-default-arg arg)
389 (setq sml-buffer (apply 'make-comint pname cmd nil args))
390
391 (set-buffer sml-buffer)
392 (message (format "Starting \"%s\" in background." pname))
393 (inferior-sml-mode)
394 (goto-char (point-max))
395 sml-buffer))
396
397 (defun sml-args-to-list (string)
398 (let ((where (string-match "[ \t]" string)))
399 (cond ((null where) (list string))
400 ((not (= where 0))
401 (cons (substring string 0 where)
402 (sml-args-to-list (substring string (+ 1 where)
403 (length string)))))
404 (t (let ((pos (string-match "[^ \t]" string)))
405 (if (null pos)
406 nil
407 (sml-args-to-list (substring string pos
408 (length string)))))))))
409
410 ;;;###autoload
411 (defun switch-to-sml (eob-p)
412 "Switch to the ML process buffer.
413 With prefix argument, positions cursor at point, otherwise at end of buffer."
414 (interactive "P")
415 (pop-to-buffer (sml-proc-buffer))
416 (cond ((not eob-p)
417 (push-mark (point) t)
418 (goto-char (point-max)))))
419
420 ;; Fakes it with a "use <temp-file>;" if necessary.
421
422 ;;;###autoload
423 (defun sml-send-region (start end &optional and-go)
424 "Send current region to the inferior ML process.
425 Prefix argument means switch-to-sml afterwards.
426
427 The region is written out to a temporary file and a \"use <temp-file>\" command
428 is sent to the compiler.
429 See variables `sml-use-command'."
430 (interactive "r\nP")
431 (if (= start end)
432 (message "The region is zero (ignored)")
433 (let* ((buf (sml-proc-buffer))
434 (file (buffer-file-name))
435 (marker (copy-marker start))
436 (tmp (make-temp-file "sml")))
437 (write-region start end tmp nil 'silently)
438 (with-current-buffer buf
439 (when sml-temp-file
440 (ignore-errors (delete-file (car sml-temp-file)))
441 (set-marker (cdr sml-temp-file) nil))
442 (setq sml-temp-file (cons tmp marker))
443 (sml-send-string (format sml-use-command tmp) nil and-go)))))
444
445 ;; This is quite bogus, so it isn't bound to a key by default.
446 ;; Anyone coming up with an algorithm to recognise fun & local
447 ;; declarations surrounding point will do everyone a favour!
448
449 (defun sml-send-function (&optional and-go)
450 "Send current paragraph to the inferior ML process.
451 With a prefix argument switch to the sml buffer as well
452 \(cf. `sml-send-region'\)."
453 (interactive "P")
454 (save-excursion
455 (sml-mark-function)
456 (sml-send-region (point) (mark)))
457 (if and-go (switch-to-sml nil)))
458
459 (defvar sml-source-modes '(sml-mode)
460 "*Used to determine if a buffer contains ML source code.
461 If it's loaded into a buffer that is in one of these major modes, it's
462 considered an ML source file by `sml-load-file'. Used by these commands
463 to determine defaults.")
464
465 ;;;###autoload
466 (defun sml-send-buffer (&optional and-go)
467 "Send buffer to inferior shell running ML process.
468 With a prefix argument switch to the sml buffer as well
469 \(cf. `sml-send-region'\)."
470 (interactive "P")
471 (if (memq major-mode sml-source-modes)
472 (sml-send-region (point-min) (point-max) and-go)))
473
474 ;; Since sml-send-function/region take an optional prefix arg, these
475 ;; commands are redundant. But they are kept around for the user to
476 ;; bind if she wishes, since its easier to type C-c r than C-u C-c C-r.
477
478 (defun sml-send-region-and-go (start end)
479 "Send current region to the inferior ML process, and go there."
480 (interactive "r")
481 (sml-send-region start end t))
482
483 (defun sml-send-function-and-go ()
484 "Send current paragraph to the inferior ML process, and go there."
485 (interactive)
486 (sml-send-function t))
487
488 ;;; H A C K A T T A C K ! X E M A C S V E R S U S E M A C S
489
490 (defun sml-drag-region (event)
491 "Highlight the text the mouse is dragged over, and send it to ML.
492 This must be bound to a button-down mouse event, currently \\[sml-drag-region].
493
494 If you drag the mouse (ie, keep the mouse button depressed) the
495 program text sent to the complier is delimited by where you started
496 dragging the mouse, and where you release the mouse button.
497
498 If you only click the mouse, the program text sent to the compiler is
499 delimited by the current position of point and the place where you
500 click the mouse.
501
502 In either event, the values of both point and mark are left
503 undisturbed once this operation is completed."
504 (interactive "e")
505 (let ((mark-ring) ;BAD: selection start gets cons'd
506 (pmark (point))) ;where point is now
507 (if (fboundp 'mouse-track-default)
508 ;; Assume this is XEmacs, otherwise assume its Emacs
509 (save-excursion
510 (let ((zmacs-regions))
511 (set-marker (mark-marker) nil)
512 (mouse-track-default event)
513 (if (not (region-exists-p)) (push-mark pmark nil t))
514 (call-interactively 'sml-send-region)))
515 ;; Emacs: making this buffer-local ought to happen in sml-mode
516 (make-local-variable 'transient-mark-mode)
517 (save-excursion
518 (let ((transient-mark-mode 1))
519 (mouse-drag-region event)
520 (if (not mark-active) (push-mark pmark nil t))
521 (call-interactively 'sml-send-region))))))
522
523
524 ;;; LOADING AND IMPORTING SOURCE FILES:
525
526 (defvar sml-prev-dir/file nil
527 "Caches the (directory . file) pair used in the last `sml-load-file'
528 or `sml-cd' command. Used for determining the default in the next one.")
529
530 ;;;###autoload
531 (defun sml-load-file (&optional and-go)
532 "Load an ML file into the current inferior ML process.
533 With a prefix argument switch to sml buffer as well.
534
535 This command uses the ML command template `sml-use-command' to construct
536 the command to send to the ML process\; a trailing \"\;\\n\" will be added
537 automatically."
538 (interactive "P")
539 (let ((file (car (comint-get-source
540 "Load ML file: " sml-prev-dir/file sml-source-modes t))))
541 (with-current-buffer (sml-proc-buffer)
542 ;; Check if buffer needs saved. Should (save-some-buffers) instead?
543 (comint-check-source file)
544 (setq sml-prev-dir/file
545 (cons (file-name-directory file) (file-name-nondirectory file)))
546 (sml-send-string (format sml-use-command file) nil and-go))))
547
548 (defun sml-cd (dir)
549 "Change the working directory of the inferior ML process.
550 The default directory of the process buffer is changed to DIR. If the
551 variable `sml-cd-command' is non-nil it should be an ML command that will
552 be executed to change the compiler's working directory\; a trailing
553 \"\;\\n\" will be added automatically."
554 (interactive "DSML Directory: ")
555 (let ((dir (expand-file-name dir)))
556 (with-current-buffer (sml-proc-buffer)
557 (sml-send-string (format sml-cd-command dir) t)
558 (setq default-directory dir))
559 (setq sml-prev-dir/file (cons dir nil))))
560
561 (defun sml-send-string (str &optional print and-go)
562 (let ((proc (sml-proc))
563 (str (concat str ";\n"))
564 (win (get-buffer-window (current-buffer) 'visible)))
565 (when win (select-window win))
566 (goto-char (point-max))
567 (when print (insert str))
568 (sml-update-cursor)
569 (set-marker (process-mark proc) (point-max))
570 (setq compilation-last-buffer (current-buffer))
571 (comint-send-string proc str)
572 (when and-go (switch-to-sml nil))))
573
574 (defun sml-compile (command)
575 "re-make a system using (by default) CM.
576 The exact command used can be specified by providing a prefix argument."
577 (interactive
578 ;; code taken straight from compile.el
579 (if (or compilation-read-command current-prefix-arg)
580 (list (read-from-minibuffer "Compile command: "
581 sml-compile-command nil nil
582 '(compile-history . 1)))
583 (list sml-compile-command)))
584 (setq sml-compile-command command)
585 (save-some-buffers (not compilation-ask-about-save) nil)
586 ;; try to find a makefile up the directory tree
587 (let ((dir (when sml-make-file-name default-directory)))
588 (while (and dir (not (file-exists-p (concat dir sml-make-file-name))))
589 (let ((newdir (file-name-directory (directory-file-name dir))))
590 (setq dir (unless (equal newdir dir) newdir))))
591 (unless dir (setq dir default-directory))
592 (with-current-buffer (sml-proc-buffer)
593 (setq default-directory dir)
594 (sml-send-string (concat (format sml-cd-command dir) "; " command) t t))))
595
596 ;;; PARSING ERROR MESSAGES
597
598 ;; This should need no modification to support other compilers.
599
600 ;; Update the buffer-local error-cursor in proc-buffer to be its
601 ;; current proc mark.
602
603 (defvar sml-endof-error-alist nil)
604
605 (defun sml-update-cursor ()
606 ;; update buffer local variable
607 (set-marker sml-error-cursor (1- (process-mark (sml-proc))))
608 (setq sml-endof-error-alist nil)
609 (compilation-forget-errors)
610 (if (markerp compilation-parsing-end)
611 (set-marker compilation-parsing-end sml-error-cursor)
612 (setq compilation-parsing-end sml-error-cursor)))
613
614 (defun sml-make-error (f c)
615 (let ((err (point-marker))
616 (linenum (string-to-number c))
617 (filename (list (first f) (second f)))
618 (column (string-to-number (compile-buffer-substring (third f)))))
619 ;; record the end of error, if any
620 (when (fourth f)
621 (let* ((endline (string-to-number (compile-buffer-substring (fourth f))))
622 (endcol (string-to-number (compile-buffer-substring (fifth f))))
623 (linediff (- endline linenum)))
624 (push (list err linediff (if (= 0 linediff) (- endcol column) endcol))
625 sml-endof-error-alist)))
626 ;; build the error descriptor
627 (if (string= (car sml-temp-file) (first f))
628 ;; special case for code sent via sml-send-region
629 (let ((marker (cdr sml-temp-file)))
630 (with-current-buffer (marker-buffer marker)
631 (goto-char marker)
632 (forward-line (1- linenum))
633 (forward-char (1- column))
634 (cons err (point-marker))))
635 ;; taken from compile.el
636 (list err filename linenum column))))
637
638 (defadvice compilation-goto-locus (after sml-endof-error activate)
639 (let* ((next-error (ad-get-arg 0))
640 (err (car next-error))
641 (pos (cdr next-error))
642 (endof (with-current-buffer (marker-buffer err)
643 (assq err sml-endof-error-alist))))
644 (if (not endof) (sml-error-overlay 'undo)
645 (with-current-buffer (marker-buffer pos)
646 (goto-char pos)
647 (let ((linediff (second endof))
648 (coldiff (third endof)))
649 (when (> 0 linediff) (forward-line linediff))
650 (forward-char coldiff))
651 (sml-error-overlay nil pos (point))
652 (push-mark nil t (not sml-error-overlay))
653 (goto-char pos)))))
654
655 (defun sml-error-overlay (undo &optional beg end)
656 "Move `sml-error-overlay' so it surrounds the text region in the
657 current buffer. If the buffer-local variable `sml-error-overlay' is
658 non-nil it should be an overlay \(or extent, in XEmacs speak\)\; this
659 function moves the overlay over the current region. If the optional
660 BUFFER argument is given, move the overlay in that buffer instead of
661 the current buffer.
662
663 Called interactively, the optional prefix argument UNDO indicates that
664 the overlay should simply be removed: \\[universal-argument] \
665 \\[sml-error-overlay]."
666 (interactive "P")
667 (when sml-error-overlay
668 (unless (overlayp sml-error-overlay)
669 (let ((ol sml-error-overlay))
670 (setq sml-error-overlay (make-overlay 0 0))
671 (overlay-put sml-error-overlay 'face (if (symbolp ol) ol 'region))))
672 (if undo (move-overlay sml-error-overlay 1 1 (current-buffer))
673 ;; if active regions, signals mark not active if no region set
674 (let ((beg (or beg (region-beginning)))
675 (end (or end (region-end))))
676 (move-overlay sml-error-overlay beg end (current-buffer))))))
677
678 ;;; H A C K A T T A C K ! X E M A C S / E M A C S K E Y S
679
680 (if window-system
681 (cond ((string-match "\\(Lucid\\|XEmacs\\)" emacs-version)
682 ;; LUCID (19.10) or later...
683 (define-key sml-mode-map '(meta shift button1) 'sml-drag-region))
684 (t
685 ;; GNU, post circa 19.19
686 (define-key sml-mode-map [M-S-down-mouse-1] 'sml-drag-region))))
687
688 ;;; ...and do the user's customisations.
689
690 (run-hooks 'inferior-sml-load-hook)
691
692 ;;; Here is where sml-proc.el ends
693 (provide 'sml-proc)