-
[emacs/old-mirror.git] / sml-proc.el
bloba25ffe73d16ad87225b84a0ca9ddbe170c0460f2
1 ;;; sml-proc.el --- Comint based interaction mode for Standard ML.
3 ;; Copyright (C) 1999, 2000, 2003, 2004, 2005, 2007 Stefan Monnier
4 ;; Copyright (C) 1994-1997 Matthew J. Morley
5 ;; Copyright (C) 1989 Lars Bo Nielsen
7 ;; $Revision$
8 ;; $Date$
10 ;; ====================================================================
12 ;; This file is not part of GNU Emacs, but it is distributed under the
13 ;; same conditions.
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 3, or (at
18 ;; your option) any later version.
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.
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.)
30 ;; ====================================================================
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.
36 ;;; Commentary:
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:
44 ;; (autoload 'run-sml "sml-proc" "Run an inferior ML process." t)
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).
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.
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).
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.
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.
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.
83 ;; Bugs:
85 ;; Todo:
87 ;; - Keep improving `sml-compile'.
88 ;; - ignore warnings (if requested) for next-error
90 ;;; Code:
92 (eval-when-compile (require 'cl))
93 (require 'sml-mode)
94 (require 'sml-util)
95 (require 'comint)
96 (require 'compile)
98 (defgroup sml-proc ()
99 "Interacting with an SML process."
100 :group 'sml)
102 (defcustom sml-program-name "sml"
103 "*Program to run as ML."
104 :group 'sml-proc
105 :type '(string))
107 (defcustom sml-default-arg ""
108 "*Default command line option to pass, if any."
109 :group 'sml-proc
110 :type '(string))
112 (defcustom sml-host-name ""
113 "*Host on which to run ML."
114 :group 'sml-proc
115 :type '(string))
117 (defcustom sml-config-file "~/.smlproc.sml"
118 "*File that should be fed to the ML process when started."
119 :group 'sml-proc
120 :type '(string))
122 (defcustom sml-compile-command "CM.make()"
123 "The command used by default by `sml-compile'.
124 See also `sml-compile-commands-alist'.")
126 (defcustom sml-compile-commands-alist
127 '(("CMB.make()" . "all-files.cm")
128 ("CMB.make()" . "pathconfig")
129 ("CM.make()" . "sources.cm")
130 ("use \"load-all\"" . "load-all"))
131 "*Commands used by default by `sml-compile'.
132 Each command is associated with its \"main\" file.
133 It is perfectly OK to associate several files with a command or several
134 commands with the same file.")
136 (defvar inferior-sml-mode-hook nil
137 "*This hook is run when the inferior ML process is started.
138 All buffer local customisations for the interaction buffers go here.")
140 (defvar sml-error-overlay nil
141 "*Non-nil means use an overlay to highlight errorful code in the buffer.
142 The actual value is the name of a face to use for the overlay.
143 Instead of setting this variable to 'region, you can also simply keep
144 it NIL and use (transient-mark-mode) which will provide similar
145 benefits (but with several side effects).")
147 (defvar sml-buffer nil
148 "*The current ML process buffer.
150 MULTIPLE PROCESS SUPPORT (Whoever wants multi-process support anyway?)
151 =====================================================================
152 `sml-mode' supports, in a fairly simple fashion, running multiple ML
153 processes. To run multiple ML processes, you start the first up with
154 \\[sml]. It will be in a buffer named *sml*. Rename this buffer with
155 \\[rename-buffer]. You may now start up a new process with another
156 \\[sml]. It will be in a new buffer, named *sml*. You can switch
157 between the different process buffers with \\[switch-to-buffer].
159 NB *sml* is just the default name for the buffer. It actually gets
160 it's name from the value of `sml-program-name' -- *poly*, *smld*,...
162 If you have more than one ML process around, commands that send text
163 from source buffers to ML processes -- like `sml-send-function' or
164 `sml-send-region' -- have to choose a process to send it to. This is
165 determined by the global variable `sml-buffer'. Suppose you have three
166 inferior ML's running:
167 Buffer Process
168 sml #<process sml>
169 mosml #<process mosml>
170 *sml* #<process sml<2>>
171 If you do a \\[sml-send-function] command on some ML source code,
172 what process do you send it to?
174 - If you're in a process buffer (sml, mosml, or *sml*), you send it to
175 that process (usually makes sense only to `sml-load-file').
176 - If you're in some other buffer (e.g., a source file), you send it to
177 the process attached to buffer `sml-buffer'.
179 This process selection is performed by function `sml-proc' which looks
180 at the value of `sml-buffer' -- which must be a Lisp buffer object, or
181 a string \(or nil\).
183 Whenever \\[sml] fires up a new process, it resets `sml-buffer' to be
184 the new process's buffer. If you only run one process, this will do
185 the right thing. If you run multiple processes, you can change
186 `sml-buffer' to another process buffer with \\[set-variable], or
187 use the command \\[sml-buffer] in the interaction buffer of choice.")
190 ;;; ALL STUFF THAT DEFAULTS TO THE SML/NJ COMPILER (0.93)
192 (defvar sml-use-command "use \"%s\""
193 "*Template for loading a file into the inferior ML process.
194 Set to \"use \\\"%s\\\"\" for SML/NJ or Edinburgh ML;
195 set to \"PolyML.use \\\"%s\\\"\" for Poly/ML, etc.")
197 (defvar sml-cd-command "OS.FileSys.chDir \"%s\""
198 "*Command template for changing working directories under ML.
199 Set this to nil if your compiler can't change directories.
201 The format specifier \"%s\" will be converted into the directory name
202 specified when running the command \\[sml-cd].")
204 (defcustom sml-prompt-regexp "^[-=>#] *"
205 "*Regexp used to recognise prompts in the inferior ML process."
206 :group 'sml-proc
207 :type '(regexp))
209 (defvar sml-error-regexp-alist
210 `( ;; Poly/ML messages
211 ("^\\(Error\\|Warning:\\) in '\\(.+\\)', line \\([0-9]+\\)" 2 3)
212 ;; Moscow ML
213 ("^File \"\\([^\"]+\\)\", line \\([0-9]+\\)\\(-\\([0-9]+\\)\\)?, characters \\([0-9]+\\)-\\([0-9]+\\):" 1 2 5)
214 ;; SML/NJ: the file-pattern is anchored to avoid
215 ;; pathological behavior with very long lines.
216 ("^[-= ]*\\(.*[^\n)]\\)\\( (.*)\\)?:\\([0-9]+\\)\\.\\([0-9]+\\)\\(-\\([0-9]+\\)\\.\\([0-9]+\\)\\)? \\(Error\\|Warnin\\(g\\)\\): .*" 1
217 ,@(if (fboundp 'compilation-fake-loc) ;New compile.el.
218 '((3 . 6) (4 . 7) (9))
219 '(sml-make-error 3 4 6 7)))
220 ;; SML/NJ's exceptions: see above.
221 ("^ +\\(raised at: \\)?\\(.+\\):\\([0-9]+\\)\\.\\([0-9]+\\)\\(-\\([0-9]+\\)\\.\\([0-9]+\\)\\)" 2
222 ,@(if (fboundp 'compilation-fake-loc) ;New compile.el.
223 '((3 . 6) (4 . 7))
224 '(sml-make-error 3 4 6 7))))
225 "Alist that specifies how to match errors in compiler output.
226 See `compilation-error-regexp-alist' for a description of the format.")
228 ;; font-lock support
229 (defconst inferior-sml-font-lock-keywords
230 `(;; prompt and following interactive command
231 ;; FIXME: Actually, this should already be taken care of by comint.
232 (,(concat "\\(" sml-prompt-regexp "\\)\\(.*\\)")
233 (1 font-lock-prompt-face)
234 (2 font-lock-command-face keep))
235 ;; CM's messages
236 ("^\\[\\(.*GC #.*\n\\)*.*\\]" . font-lock-comment-face)
237 ;; SML/NJ's irritating GC messages
238 ("^GC #.*" . font-lock-comment-face)
239 ;; error messages
240 ,@(unless (fboundp 'compilation-fake-loc)
241 (mapcar (lambda (ra) (cons (car ra) 'font-lock-warning-face))
242 sml-error-regexp-alist)))
243 "Font-locking specification for inferior SML mode.")
245 (defface font-lock-prompt-face
246 '((t (:bold t)))
247 "Font Lock mode face used to highlight prompts."
248 :group 'font-lock-highlighting-faces)
249 (defvar font-lock-prompt-face 'font-lock-prompt-face
250 "Face name to use for prompts.")
252 (defface font-lock-command-face
253 '((t (:bold t)))
254 "Font Lock mode face used to highlight interactive commands."
255 :group 'font-lock-highlighting-faces)
256 (defvar font-lock-command-face 'font-lock-command-face
257 "Face name to use for interactive commands.")
259 (defconst inferior-sml-font-lock-defaults
260 '(inferior-sml-font-lock-keywords nil nil nil nil))
263 ;;; CODE
265 (defmap inferior-sml-mode-map
266 '(("\C-c\C-s" . run-sml)
267 ("\C-c\C-l" . sml-load-file)
268 ("\t" . comint-dynamic-complete))
269 "Keymap for inferior-sml mode"
270 :inherit comint-mode-map
271 :group 'sml-proc)
274 ;; buffer-local
276 (defvar sml-temp-file nil)
277 ;;(defvar sml-error-file nil) ; file from which the last error came
278 (defvar sml-error-cursor nil) ; ditto
280 (defun sml-proc-buffer ()
281 "Return the current ML process buffer.
282 or the current buffer if it is in `inferior-sml-mode'. Raises an error
283 if the variable `sml-buffer' does not appear to point to an existing
284 buffer."
285 (or (and (eq major-mode 'inferior-sml-mode) (current-buffer))
286 (and sml-buffer
287 (let ((buf (get-buffer sml-buffer)))
288 ;; buffer-name returns nil if the buffer has been killed
289 (and buf (buffer-name buf) buf)))
290 ;; no buffer found, make a new one
291 (save-excursion (call-interactively 'run-sml))))
293 (defun sml-buffer (echo)
294 "Make the current buffer the current `sml-buffer' if that is sensible.
295 Lookup variable `sml-buffer' to see why this might be useful.
296 If prefix argument ECHO is set, then it only reports on the current state."
297 (interactive "P")
298 (when (not echo)
299 (setq sml-buffer
300 (if (eq major-mode 'inferior-sml-mode) (current-buffer)
301 (read-buffer "Set ML process buffer to: " nil t))))
302 (message "ML process buffer is now %s."
303 (or (ignore-errors (buffer-name (get-buffer sml-buffer)))
304 "undefined")))
306 (defun sml-proc ()
307 "Return the current ML process. See variable `sml-buffer'."
308 (assert (eq major-mode 'inferior-sml-mode))
309 (or (get-buffer-process (current-buffer))
310 (progn (call-interactively 'run-sml)
311 (get-buffer-process (current-buffer)))))
313 (defun sml-proc-comint-input-filter-function (str)
314 ;; `compile.el' in Emacs-22 fails to notice that file location info from
315 ;; errors should be recomputed afresh (without using stale info from
316 ;; earlier compilations). We used to cause a refresh in sml-send-string,
317 ;; but this doesn't catch the case when the user types commands directly
318 ;; at the prompt.
319 (compilation-forget-errors) ;Has to run before compilation-fake-loc.
320 (if (and (fboundp 'compilation-fake-loc) sml-temp-file)
321 (compilation-fake-loc (cdr sml-temp-file) (car sml-temp-file)))
322 str)
324 (defun inferior-sml-next-error-hook ()
325 ;; Try to recognize SML/NJ type error message and to highlight finely the
326 ;; difference between the two types (in case they're large, it's not
327 ;; always obvious to spot it).
329 ;; Sample messages:
331 ;; Data.sml:31.9-33.33 Error: right-hand-side of clause doesn't agree with function result type [tycon mismatch]
332 ;; expression: Hstring
333 ;; result type: Hstring * int
334 ;; in declaration:
335 ;; des2hs = (fn SYM_ID hs => hs
336 ;; | SYM_OP hs => hs
337 ;; | SYM_CHR hs => hs)
338 ;; Data.sml:35.44-35.63 Error: operator and operand don't agree [tycon mismatch]
339 ;; operator domain: Hstring * Hstring
340 ;; operand: (Hstring * int) * (Hstring * int)
341 ;; in expression:
342 ;; HSTRING.ieq (h1,h2)
343 ;; vparse.sml:1861.6-1922.14 Error: case object and rules don't agree [tycon mismatch]
344 ;; rule domain: STConstraints list list option
345 ;; object: STConstraints list option
346 ;; in expression:
347 (save-current-buffer
348 (when (and (derived-mode-p 'sml-mode 'inferior-sml-mode)
349 (boundp 'next-error-last-buffer)
350 (bufferp next-error-last-buffer)
351 (set-buffer next-error-last-buffer)
352 (derived-mode-p 'inferior-sml-mode)
353 ;; The position of `point' is not guaranteed :-(
354 (looking-at (concat ".*\\[tycon mismatch\\]\n"
355 " \\(operator domain\\|expression\\|rule domain\\): +")))
356 (ignore-errors (require 'smerge-mode))
357 (if (not (fboundp 'smerge-refine-subst))
358 (remove-hook 'next-error-hook 'inferior-sml-next-error-hook)
359 (save-excursion
360 (let ((b1 (match-end 0))
361 e1 b2 e2)
362 (when (re-search-forward "\n in \\(expression\\|declaration\\):\n"
363 nil t)
364 (setq e2 (match-beginning 0))
365 (when (re-search-backward
366 "\n \\(operand\\|result type\\|object\\): +"
367 b1 t)
368 (setq e1 (match-beginning 0))
369 (setq b2 (match-end 0))
370 (smerge-refine-subst b1 e1 b2 e2
371 '((face . smerge-refined-change)))))))))))
373 (define-derived-mode inferior-sml-mode comint-mode "Inferior-SML"
374 "Major mode for interacting with an inferior ML process.
376 The following commands are available:
377 \\{inferior-sml-mode-map}
379 An ML process can be fired up (again) with \\[sml].
381 Customisation: Entry to this mode runs the hooks on `comint-mode-hook'
382 and `inferior-sml-mode-hook' (in that order).
384 Variables controlling behaviour of this mode are
386 `sml-program-name' (default \"sml\")
387 Program to run as ML.
389 `sml-use-command' (default \"use \\\"%s\\\"\")
390 Template for loading a file into the inferior ML process.
392 `sml-cd-command' (default \"System.Directory.cd \\\"%s\\\"\")
393 ML command for changing directories in ML process (if possible).
395 `sml-prompt-regexp' (default \"^[\\-=] *\")
396 Regexp used to recognise prompts in the inferior ML process.
398 You can send text to the inferior ML process from other buffers containing
399 ML source.
400 `switch-to-sml' switches the current buffer to the ML process buffer.
401 `sml-send-function' sends the current *paragraph* to the ML process.
402 `sml-send-region' sends the current region to the ML process.
404 Prefixing the sml-send-<whatever> commands with \\[universal-argument]
405 causes a switch to the ML process buffer after sending the text.
407 For information on running multiple processes in multiple buffers, see
408 documentation for variable `sml-buffer'.
410 Commands:
411 RET after the end of the process' output sends the text from the
412 end of process to point.
413 RET before the end of the process' output copies the current line
414 to the end of the process' output, and sends it.
415 DEL converts tabs to spaces as it moves back.
416 TAB file name completion, as in shell-mode, etc.."
417 (setq comint-prompt-regexp sml-prompt-regexp)
418 (sml-mode-variables)
420 ;; We have to install it globally, 'cause it's run in the *source* buffer :-(
421 (add-hook 'next-error-hook 'inferior-sml-next-error-hook)
423 ;; Make TAB add a " rather than a space at the end of a file name.
424 (set (make-local-variable 'comint-completion-addsuffix) '(?/ . ?\"))
425 (add-hook 'comint-input-filter-functions
426 'sml-proc-comint-input-filter-function nil t)
428 (set (make-local-variable 'font-lock-defaults)
429 inferior-sml-font-lock-defaults)
430 ;; For sequencing through error messages:
431 (set (make-local-variable 'sml-error-cursor) (point-max-marker))
432 (set-marker-insertion-type sml-error-cursor nil)
434 ;; Compilation support (used for `next-error').
435 ;; The keymap of compilation-minor-mode is too unbearable, so we
436 ;; just can't use the minor-mode if we can't override the map.
437 (when (boundp 'minor-mode-overriding-map-alist)
438 (set (make-local-variable 'compilation-error-regexp-alist)
439 sml-error-regexp-alist)
440 (compilation-minor-mode 1)
441 ;; Eliminate compilation-minor-mode's map.
442 (let ((map (make-sparse-keymap)))
443 (dolist (keys '([menu-bar] [follow-link]))
444 ;; Preserve some of the bindings.
445 (define-key map keys (lookup-key compilation-minor-mode-map keys)))
446 (add-to-list 'minor-mode-overriding-map-alist
447 (cons 'compilation-minor-mode map)))
448 ;; I'm sure people might kill me for that
449 (setq compilation-error-screen-columns nil)
450 (make-local-variable 'sml-endof-error-alist))
451 ;;(make-local-variable 'sml-error-overlay)
453 (setq mode-line-process '(": %s")))
455 ;;; FOR RUNNING ML FROM EMACS
457 ;;;###autoload
458 (autoload 'run-sml "sml-proc" nil t)
459 (defalias 'run-sml 'sml-run)
460 (defun sml-run (cmd arg &optional host)
461 "Run the program CMD with given arguments ARG.
462 The command is run in buffer *CMD* using mode `inferior-sml-mode'.
463 If the buffer already exists and has a running process, then
464 just go to this buffer.
466 This updates `sml-buffer' to the new buffer.
467 You can have several inferior M(or L process running, but only one (> s
468 current one -- given by `sml-buffer' (qv).
470 If a prefix argument is used, the user is also prompted for a HOST
471 on which to run CMD using `remote-shell-program'.
473 \(Type \\[describe-mode] in the process buffer for a list of commands.)"
474 (interactive
475 (list
476 (read-string "ML command: " sml-program-name)
477 (if (or current-prefix-arg (> (length sml-default-arg) 0))
478 (read-string "Any args: " sml-default-arg)
479 sml-default-arg)
480 (if (or current-prefix-arg (> (length sml-host-name) 0))
481 (read-string "On host: " sml-host-name)
482 sml-host-name)))
483 (let* ((pname (file-name-nondirectory cmd))
484 (args (if (equal arg "") () (split-string arg)))
485 (file (when (and sml-config-file (file-exists-p sml-config-file))
486 sml-config-file)))
487 ;; and this -- to keep these as defaults even if
488 ;; they're set in the mode hooks.
489 (setq sml-program-name cmd)
490 (setq sml-default-arg arg)
491 (setq sml-host-name host)
492 ;; For remote execution, use `remote-shell-program'
493 (when (> (length host) 0)
494 (setq args (list* host "cd" default-directory ";" cmd args))
495 (setq cmd remote-shell-program))
496 ;; go for it
497 (let ((exec-path (if (file-name-directory cmd)
498 ;; If the command has slashes, make sure we
499 ;; first look relative to the current directory.
500 ;; Emacs-21 does it for us, but not Emacs-20.
501 (cons default-directory exec-path) exec-path)))
502 (setq sml-buffer (apply 'make-comint pname cmd file args)))
504 (pop-to-buffer sml-buffer)
505 ;;(message (format "Starting \"%s\" in background." pname))
506 (inferior-sml-mode)
507 (goto-char (point-max))
508 sml-buffer))
510 (defun switch-to-sml (eobp)
511 "Switch to the ML process buffer.
512 Move point to the end of buffer unless prefix argument EOBP is set."
513 (interactive "P")
514 (pop-to-buffer (sml-proc-buffer))
515 (unless eobp
516 (push-mark (point) t)
517 (goto-char (point-max))))
519 ;; Fakes it with a "use <temp-file>;" if necessary.
521 (defun sml-send-region (start end &optional and-go)
522 "Send current region START..END to the inferior ML process.
523 Prefix AND-GO argument means switch-to-sml afterwards.
525 The region is written out to a temporary file and a \"use <temp-file>\" command
526 is sent to the compiler.
527 See variables `sml-use-command'."
528 (interactive "r\nP")
529 (if (= start end)
530 (message "The region is zero (ignored)")
531 (let* ((buf (sml-proc-buffer))
532 (marker (copy-marker start))
533 (tmp (make-temp-file "sml")))
534 (write-region start end tmp nil 'silently)
535 (with-current-buffer buf
536 (when sml-temp-file
537 (ignore-errors (delete-file (car sml-temp-file)))
538 (set-marker (cdr sml-temp-file) nil))
539 (setq sml-temp-file (cons tmp marker))
540 (sml-send-string (format sml-use-command tmp) nil and-go)))))
542 ;; This is quite bogus, so it isn't bound to a key by default.
543 ;; Anyone coming up with an algorithm to recognise fun & local
544 ;; declarations surrounding point will do everyone a favour!
546 (defun sml-send-function (&optional and-go)
547 "Send current paragraph to the inferior ML process.
548 With a prefix argument AND-GO switch to the sml buffer as well
549 \(cf. `sml-send-region'\)."
550 (interactive "P")
551 (save-excursion
552 (sml-mark-function)
553 (sml-send-region (point) (mark)))
554 (if and-go (switch-to-sml nil)))
556 (defvar sml-source-modes '(sml-mode)
557 "*Used to determine if a buffer contains ML source code.
558 If it's loaded into a buffer that is in one of these major modes, it's
559 considered an ML source file by `sml-load-file'. Used by these commands
560 to determine defaults.")
562 (defun sml-send-buffer (&optional and-go)
563 "Send buffer to inferior shell running ML process.
564 With a prefix argument AND-GO switch to the sml buffer as well
565 \(cf. `sml-send-region'\)."
566 (interactive "P")
567 (if (memq major-mode sml-source-modes)
568 (sml-send-region (point-min) (point-max) and-go)))
570 ;; Since sml-send-function/region take an optional prefix arg, these
571 ;; commands are redundant. But they are kept around for the user to
572 ;; bind if she wishes, since its easier to type C-c r than C-u C-c C-r.
574 (defun sml-send-region-and-go (start end)
575 "Send current region START..END to the inferior ML process, and go there."
576 (interactive "r")
577 (sml-send-region start end t))
579 (defun sml-send-function-and-go ()
580 "Send current paragraph to the inferior ML process, and go there."
581 (interactive)
582 (sml-send-function t))
584 ;;; LOADING AND IMPORTING SOURCE FILES:
586 (defvar sml-prev-dir/file nil
587 "Cache for (DIRECTORY . FILE) pair last.
588 Set in `sml-load-file' and `sml-cd' commands.
589 Used to determine the default in the next `ml-load-file'.")
591 (defun sml-load-file (&optional and-go)
592 "Load an ML file into the current inferior ML process.
593 With a prefix argument AND-GO switch to sml buffer as well.
595 This command uses the ML command template `sml-use-command' to construct
596 the command to send to the ML process\; a trailing \"\;\\n\" will be added
597 automatically."
598 (interactive "P")
599 (let ((file (car (comint-get-source
600 "Load ML file: " sml-prev-dir/file sml-source-modes t))))
601 (with-current-buffer (sml-proc-buffer)
602 ;; Check if buffer needs saved. Should (save-some-buffers) instead?
603 (comint-check-source file)
604 (setq sml-prev-dir/file
605 (cons (file-name-directory file) (file-name-nondirectory file)))
606 (sml-send-string (format sml-use-command file) nil and-go))))
608 (defun sml-cd (dir)
609 "Change the working directory of the inferior ML process.
610 The default directory of the process buffer is changed to DIR. If the
611 variable `sml-cd-command' is non-nil it should be an ML command that will
612 be executed to change the compiler's working directory\; a trailing
613 \"\;\\n\" will be added automatically."
614 (interactive "DSML Directory: ")
615 (let ((dir (expand-file-name dir)))
616 (with-current-buffer (sml-proc-buffer)
617 (sml-send-string (format sml-cd-command dir) t)
618 (setq default-directory dir))
619 (setq sml-prev-dir/file (cons dir nil))))
621 (defun sml-send-string (str &optional print and-go)
622 (let ((proc (sml-proc))
623 (str (concat str ";\n"))
624 (win (get-buffer-window (current-buffer) 'visible)))
625 (when win (select-window win))
626 (goto-char (point-max))
627 (when print (insert str))
628 (sml-update-cursor)
629 (set-marker (process-mark proc) (point-max))
630 (setq compilation-last-buffer (current-buffer))
631 (comint-send-string proc str)
632 (when and-go (switch-to-sml nil))))
634 (defun sml-compile (command &optional and-go)
635 "Pass a COMMAND to the SML process to compile the current program.
637 You can then use the command \\[next-error] to find the next error message
638 and move to the source code that caused it.
640 Interactively, prompts for the command if `compilation-read-command' is
641 non-nil. With prefix arg, always prompts.
643 Prefix arg AND-GO also means to `switch-to-sml' afterwards."
644 (interactive
645 (let* ((dir default-directory)
646 (cmd "cd \"."))
647 ;; look for files to determine the default command
648 (while (and (stringp dir)
649 (dolist (cf sml-compile-commands-alist 1)
650 (when (file-exists-p (expand-file-name (cdr cf) dir))
651 (setq cmd (concat cmd "\"; " (car cf))) (return nil))))
652 (let ((newdir (file-name-directory (directory-file-name dir))))
653 (setq dir (unless (equal newdir dir) newdir))
654 (setq cmd (concat cmd "/.."))))
655 (setq cmd
656 (cond
657 ((local-variable-p 'sml-compile-command) sml-compile-command)
658 ((string-match "^\\s-*cd\\s-+\"\\.\"\\s-*;\\s-*" cmd)
659 (substring cmd (match-end 0)))
660 ((string-match "^\\s-*cd\\s-+\"\\(\\./\\)" cmd)
661 (replace-match "" t t cmd 1))
662 ((string-match ";" cmd) cmd)
663 (t sml-compile-command)))
664 ;; code taken from compile.el
665 (if (or compilation-read-command current-prefix-arg)
666 (list (read-from-minibuffer "Compile command: "
667 cmd nil nil '(compile-history . 1)))
668 (list cmd))))
669 ;; ;; now look for command's file to determine the directory
670 ;; (setq dir default-directory)
671 ;; (while (and (stringp dir)
672 ;; (dolist (cf sml-compile-commands-alist t)
673 ;; (when (and (equal cmd (car cf))
674 ;; (file-exists-p (expand-file-name (cdr cf) dir)))
675 ;; (return nil))))
676 ;; (let ((newdir (file-name-directory (directory-file-name dir))))
677 ;; (setq dir (unless (equal newdir dir) newdir))))
678 ;; (setq dir (or dir default-directory))
679 ;; (list cmd dir)))
680 (set (make-local-variable 'sml-compile-command) command)
681 (save-some-buffers (not compilation-ask-about-save) nil)
682 (let ((dir default-directory))
683 (when (string-match "^\\s-*cd\\s-+\"\\([^\"]+\\)\"\\s-*;" command)
684 (setq dir (match-string 1 command))
685 (setq command (replace-match "" t t command)))
686 (setq dir (expand-file-name dir))
687 (with-current-buffer (sml-proc-buffer)
688 (setq default-directory dir)
689 (sml-send-string (concat (format sml-cd-command dir) "; " command)
690 t and-go))))
692 ;;; PARSING ERROR MESSAGES
694 ;; This should need no modification to support other compilers.
696 ;; Update the buffer-local error-cursor in proc-buffer to be its
697 ;; current proc mark.
699 (defvar sml-endof-error-alist nil)
701 (defun sml-update-cursor ()
702 ;; Update buffer local variable.
703 (set-marker sml-error-cursor (1- (process-mark (sml-proc))))
704 (setq sml-endof-error-alist nil)
705 ;; This is now done in comint-input-filter-functions.
706 ;; (compilation-forget-errors) ;Has to run before compilation-fake-loc.
707 ;; (if (and (fboundp 'compilation-fake-loc) sml-temp-file)
708 ;; (compilation-fake-loc (cdr sml-temp-file) (car sml-temp-file)))
709 (if (markerp compilation-parsing-end)
710 (set-marker compilation-parsing-end sml-error-cursor)
711 (setq compilation-parsing-end sml-error-cursor)))
713 (defun sml-make-error (f c)
714 (let ((err (point-marker))
715 (linenum (string-to-number c))
716 (filename (list (first f) (second f)))
717 (column (string-to-number (match-string (third f)))))
718 ;; record the end of error, if any
719 (when (fourth f)
720 (let ((endlinestr (match-string (fourth f))))
721 (when endlinestr
722 (let* ((endline (string-to-number endlinestr))
723 (endcol (string-to-number
724 (or (match-string (fifth f)) "0")))
725 (linediff (- endline linenum)))
726 (push (list err linediff (if (= 0 linediff) (- endcol column) endcol))
727 sml-endof-error-alist)))))
728 ;; build the error descriptor
729 (if (string= (car sml-temp-file) (first f))
730 ;; special case for code sent via sml-send-region
731 (let ((marker (cdr sml-temp-file)))
732 (with-current-buffer (marker-buffer marker)
733 (goto-char marker)
734 (forward-line (1- linenum))
735 (forward-char (1- column))
736 ;; A pair of markers is the right thing to return, but some
737 ;; code in compile.el doesn't like it (when we reach the end
738 ;; of the errors). So we could try to avoid it, but we don't
739 ;; because that doesn't work correctly if the current buffer
740 ;; has unsaved modifications. And it's fixed in Emacs-21.
741 ;; (if buffer-file-name
742 ;; (list err buffer-file-name
743 ;; (count-lines (point-min) (point)) (current-column))
744 (cons err (point-marker)))) ;; )
745 ;; taken from compile.el
746 (list err filename linenum column))))
748 (unless (fboundp 'compilation-fake-loc)
749 (defadvice compilation-goto-locus (after sml-endof-error activate)
750 (let* ((next-error (ad-get-arg 0))
751 (err (car next-error))
752 (pos (cdr next-error))
753 (endof (with-current-buffer (marker-buffer err)
754 (assq err sml-endof-error-alist))))
755 (if (not endof) (sml-error-overlay 'undo)
756 (with-current-buffer (marker-buffer pos)
757 (goto-char pos)
758 (let ((linediff (second endof))
759 (coldiff (third endof)))
760 (when (> 0 linediff) (forward-line linediff))
761 (forward-char coldiff))
762 (sml-error-overlay nil pos (point))
763 (push-mark nil t (not sml-error-overlay))
764 (goto-char pos))))))
766 (defun sml-error-overlay (undo &optional beg end)
767 "Move `sml-error-overlay' to the text region in the current buffer.
768 If the buffer-local variable `sml-error-overlay' is
769 non-nil it should be an overlay \(or extent, in XEmacs speak\)\; this
770 function moves the overlay over the current region. If the optional
771 BUFFER argument is given, move the overlay in that buffer instead of
772 the current buffer.
774 Called interactively, the optional prefix argument UNDO indicates that
775 the overlay should simply be removed: \\[universal-argument] \
776 \\[sml-error-overlay]."
777 (interactive "P")
778 (when sml-error-overlay
779 (unless (overlayp sml-error-overlay)
780 (let ((ol sml-error-overlay))
781 (setq sml-error-overlay (make-overlay 0 0))
782 (overlay-put sml-error-overlay 'face (if (symbolp ol) ol 'region))))
783 (if undo (move-overlay sml-error-overlay 1 1 (current-buffer))
784 ;; if active regions, signals mark not active if no region set
785 (let ((beg (or beg (region-beginning)))
786 (end (or end (region-end))))
787 (move-overlay sml-error-overlay beg end (current-buffer))))))
789 (provide 'sml-proc)
791 ;;; sml-proc.el ends here