2004-08-20 10:04:35 -07:00
|
|
|
;(***********************************************************************)
|
|
|
|
;(* *)
|
2011-07-27 07:17:02 -07:00
|
|
|
;(* OCaml *)
|
2004-08-20 10:04:35 -07:00
|
|
|
;(* *)
|
|
|
|
;(* Xavier Leroy and Jacques Garrigue *)
|
|
|
|
;(* *)
|
|
|
|
;(* Copyright 1997 Institut National de Recherche en Informatique et *)
|
|
|
|
;(* en Automatique. All rights reserved. This file is distributed *)
|
|
|
|
;(* under the terms of the GNU General Public License. *)
|
|
|
|
;(* *)
|
|
|
|
;(***********************************************************************)
|
|
|
|
|
2012-02-10 08:15:24 -08:00
|
|
|
;;; inf-caml.el --- run the OCaml toplevel in an Emacs buffer
|
1997-02-23 08:23:00 -08:00
|
|
|
|
|
|
|
;; Xavier Leroy, july 1993.
|
|
|
|
|
1997-07-17 02:46:54 -07:00
|
|
|
;; modified by Jacques Garrigue, july 1997.
|
|
|
|
|
1997-02-23 08:23:00 -08:00
|
|
|
(require 'comint)
|
2002-03-18 16:26:58 -08:00
|
|
|
(require 'caml)
|
1997-02-23 08:23:00 -08:00
|
|
|
|
2001-12-03 00:59:32 -08:00
|
|
|
;; User modifiable variables
|
|
|
|
|
|
|
|
;; Whether you want the output buffer to be diplayed when you send a phrase
|
|
|
|
|
2002-03-27 01:13:09 -08:00
|
|
|
(defvar caml-display-when-eval t
|
2001-12-03 00:59:32 -08:00
|
|
|
"*If true, display the inferior caml buffer when evaluating expressions.")
|
|
|
|
|
|
|
|
|
|
|
|
;; End of User modifiable variables
|
|
|
|
|
|
|
|
|
1997-02-23 08:23:00 -08:00
|
|
|
(defvar inferior-caml-mode-map nil)
|
|
|
|
(if inferior-caml-mode-map nil
|
|
|
|
(setq inferior-caml-mode-map
|
|
|
|
(copy-keymap comint-mode-map)))
|
|
|
|
|
2012-02-10 08:15:24 -08:00
|
|
|
;; Augment Caml mode, so you can process OCaml code in the source files.
|
1997-02-23 08:23:00 -08:00
|
|
|
|
|
|
|
(defvar inferior-caml-program "ocaml"
|
2012-02-10 08:15:24 -08:00
|
|
|
"*Program name for invoking an inferior OCaml from Emacs.")
|
1997-02-23 08:23:00 -08:00
|
|
|
|
|
|
|
(defun inferior-caml-mode ()
|
2012-02-10 08:15:24 -08:00
|
|
|
"Major mode for interacting with an inferior OCaml process.
|
|
|
|
Runs an OCaml toplevel as a subprocess of Emacs, with I/O through an
|
1997-02-23 08:23:00 -08:00
|
|
|
Emacs buffer. A history of input phrases is maintained. Phrases can
|
|
|
|
be sent from another buffer in Caml mode.
|
|
|
|
|
|
|
|
\\{inferior-caml-mode-map}"
|
|
|
|
(interactive)
|
|
|
|
(comint-mode)
|
|
|
|
(setq comint-prompt-regexp "^# ?")
|
|
|
|
(setq major-mode 'inferior-caml-mode)
|
|
|
|
(setq mode-name "Inferior Caml")
|
|
|
|
(make-local-variable 'paragraph-start)
|
|
|
|
(setq paragraph-start (concat "^$\\|" page-delimiter))
|
|
|
|
(make-local-variable 'paragraph-separate)
|
|
|
|
(setq paragraph-separate paragraph-start)
|
|
|
|
(make-local-variable 'paragraph-ignore-fill-prefix)
|
|
|
|
(setq paragraph-ignore-fill-prefix t)
|
|
|
|
(make-local-variable 'require-final-newline)
|
|
|
|
(setq require-final-newline t)
|
|
|
|
(make-local-variable 'comment-start)
|
|
|
|
(setq comment-start "(*")
|
|
|
|
(make-local-variable 'comment-end)
|
|
|
|
(setq comment-end "*)")
|
|
|
|
(make-local-variable 'comment-column)
|
|
|
|
(setq comment-column 40)
|
|
|
|
(make-local-variable 'comment-start-skip)
|
|
|
|
(setq comment-start-skip "(\\*+ *")
|
|
|
|
(make-local-variable 'parse-sexp-ignore-comments)
|
|
|
|
(setq parse-sexp-ignore-comments nil)
|
|
|
|
(use-local-map inferior-caml-mode-map)
|
|
|
|
(run-hooks 'inferior-caml-mode-hooks))
|
|
|
|
|
2001-12-03 00:59:32 -08:00
|
|
|
|
|
|
|
(defconst inferior-caml-buffer-subname "inferior-caml")
|
|
|
|
(defconst inferior-caml-buffer-name
|
|
|
|
(concat "*" inferior-caml-buffer-subname "*"))
|
|
|
|
|
2010-01-22 04:48:24 -08:00
|
|
|
;; for compatibility with xemacs
|
2001-12-03 00:59:32 -08:00
|
|
|
|
|
|
|
(defun caml-sit-for (second &optional mili redisplay)
|
|
|
|
(if (and (boundp 'running-xemacs) running-xemacs)
|
|
|
|
(sit-for (if mili (+ second (* mili 0.001)) second) redisplay)
|
|
|
|
(sit-for second mili redisplay)))
|
|
|
|
|
|
|
|
;; To show result of evaluation at toplevel
|
|
|
|
|
|
|
|
(defvar inferior-caml-output nil)
|
|
|
|
(defun inferior-caml-signal-output (s)
|
|
|
|
(if (string-match "[^ ]" s) (setq inferior-caml-output t)))
|
|
|
|
|
|
|
|
(defun inferior-caml-mode-output-hook ()
|
2010-04-27 01:45:18 -07:00
|
|
|
(set-variable 'comint-output-filter-functions
|
2012-01-27 02:42:59 -08:00
|
|
|
(list (function inferior-caml-signal-output))
|
2010-04-27 01:26:53 -07:00
|
|
|
t))
|
2001-12-03 00:59:32 -08:00
|
|
|
(add-hook 'inferior-caml-mode-hooks 'inferior-caml-mode-output-hook)
|
|
|
|
|
|
|
|
;; To launch ocaml whenever needed
|
|
|
|
|
|
|
|
(defun caml-run-process-if-needed (&optional cmd)
|
|
|
|
(if (comint-check-proc inferior-caml-buffer-name) nil
|
|
|
|
(if (not cmd)
|
|
|
|
(if (comint-check-proc inferior-caml-buffer-name)
|
|
|
|
(setq cmd inferior-caml-program)
|
2012-02-10 08:15:24 -08:00
|
|
|
(setq cmd (read-from-minibuffer "OCaml toplevel to run: "
|
2001-12-03 00:59:32 -08:00
|
|
|
inferior-caml-program))))
|
|
|
|
(setq inferior-caml-program cmd)
|
|
|
|
(let ((cmdlist (inferior-caml-args-to-list cmd))
|
|
|
|
(process-connection-type nil))
|
|
|
|
(set-buffer (apply (function make-comint)
|
|
|
|
inferior-caml-buffer-subname
|
|
|
|
(car cmdlist) nil (cdr cmdlist)))
|
|
|
|
(inferior-caml-mode)
|
|
|
|
(display-buffer inferior-caml-buffer-name)
|
|
|
|
t)
|
2002-03-27 01:13:09 -08:00
|
|
|
(setq caml-shell-active t)
|
2001-12-03 00:59:32 -08:00
|
|
|
))
|
|
|
|
|
|
|
|
;; patched to from original run-caml sharing code with
|
|
|
|
;; caml-run-process-when-needed
|
|
|
|
|
|
|
|
(defun run-caml (&optional cmd)
|
2012-02-10 08:15:24 -08:00
|
|
|
"Run an inferior OCaml process.
|
1997-02-23 08:23:00 -08:00
|
|
|
Input and output via buffer `*inferior-caml*'."
|
2001-12-03 00:59:32 -08:00
|
|
|
(interactive
|
|
|
|
(list (if (not (comint-check-proc inferior-caml-buffer-name))
|
2012-02-10 08:15:24 -08:00
|
|
|
(read-from-minibuffer "OCaml toplevel to run: "
|
2002-07-23 07:12:03 -07:00
|
|
|
inferior-caml-program))))
|
2001-12-03 00:59:32 -08:00
|
|
|
(caml-run-process-if-needed cmd)
|
|
|
|
(switch-to-buffer-other-window inferior-caml-buffer-name))
|
|
|
|
|
1997-02-23 08:23:00 -08:00
|
|
|
|
|
|
|
(defun inferior-caml-args-to-list (string)
|
|
|
|
(let ((where (string-match "[ \t]" string)))
|
|
|
|
(cond ((null where) (list string))
|
2000-04-05 11:30:22 -07:00
|
|
|
((not (= where 0))
|
|
|
|
(cons (substring string 0 where)
|
|
|
|
(inferior-caml-args-to-list (substring string (+ 1 where)
|
|
|
|
(length string)))))
|
|
|
|
(t (let ((pos (string-match "[^ \t]" string)))
|
|
|
|
(if (null pos)
|
|
|
|
nil
|
|
|
|
(inferior-caml-args-to-list (substring string pos
|
|
|
|
(length string)))))))))
|
1997-02-23 08:23:00 -08:00
|
|
|
|
1997-07-17 02:46:54 -07:00
|
|
|
(defun inferior-caml-show-subshell ()
|
|
|
|
(interactive)
|
2001-12-03 00:59:32 -08:00
|
|
|
(caml-run-process-if-needed)
|
|
|
|
(display-buffer inferior-caml-buffer-name)
|
|
|
|
; Added by Didier to move the point of inferior-caml to end of buffer
|
|
|
|
(let ((buf (current-buffer))
|
|
|
|
(caml-buf (get-buffer inferior-caml-buffer-name))
|
|
|
|
(count 0))
|
|
|
|
(while
|
2002-03-27 01:13:09 -08:00
|
|
|
(and (< count 10)
|
2001-12-03 00:59:32 -08:00
|
|
|
(not (equal (buffer-name (current-buffer))
|
|
|
|
inferior-caml-buffer-name)))
|
2002-03-27 01:13:09 -08:00
|
|
|
(next-multiframe-window)
|
2001-12-03 00:59:32 -08:00
|
|
|
(setq count (+ count 1)))
|
|
|
|
(if (equal (buffer-name (current-buffer))
|
|
|
|
inferior-caml-buffer-name)
|
2011-07-20 02:17:07 -07:00
|
|
|
(goto-char (point-max)))
|
2001-12-03 00:59:32 -08:00
|
|
|
(while
|
|
|
|
(> count 0)
|
2002-03-27 01:13:09 -08:00
|
|
|
(previous-multiframe-window)
|
2001-12-03 00:59:32 -08:00
|
|
|
(setq count (- count 1)))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2010-01-22 04:48:24 -08:00
|
|
|
;; patched by Didier to move cursor after evaluation
|
1997-07-17 02:46:54 -07:00
|
|
|
|
|
|
|
(defun inferior-caml-eval-region (start end)
|
2012-02-10 08:15:24 -08:00
|
|
|
"Send the current region to the inferior OCaml process."
|
2001-12-03 00:59:32 -08:00
|
|
|
(interactive "r")
|
|
|
|
(save-excursion (caml-run-process-if-needed))
|
|
|
|
(save-excursion
|
|
|
|
(goto-char end)
|
2002-03-27 01:13:09 -08:00
|
|
|
(caml-skip-comments-backward)
|
2002-04-18 01:32:23 -07:00
|
|
|
(comint-send-region inferior-caml-buffer-name start (point))
|
2001-12-03 00:59:32 -08:00
|
|
|
;; normally, ";;" are part of the region
|
2002-04-18 01:32:23 -07:00
|
|
|
(if (and (>= (point) 2)
|
|
|
|
(prog2 (backward-char 2) (looking-at ";;")))
|
|
|
|
(comint-send-string inferior-caml-buffer-name "\n")
|
|
|
|
(comint-send-string inferior-caml-buffer-name ";;\n"))
|
2001-12-03 00:59:32 -08:00
|
|
|
;; the user may not want to see the output buffer
|
|
|
|
(if caml-display-when-eval
|
|
|
|
(display-buffer inferior-caml-buffer-name t))))
|
|
|
|
|
|
|
|
;; jump to errors produced by ocaml compiler
|
1997-02-23 08:23:00 -08:00
|
|
|
|
1998-10-14 00:36:51 -07:00
|
|
|
(defun inferior-caml-goto-error (start end)
|
|
|
|
"Jump to the location of the last error as indicated by inferior toplevel."
|
|
|
|
(interactive "r")
|
|
|
|
(let ((loc (+ start
|
|
|
|
(save-excursion
|
2001-12-03 00:59:32 -08:00
|
|
|
(set-buffer (get-buffer inferior-caml-buffer-name))
|
1998-10-14 00:36:51 -07:00
|
|
|
(re-search-backward
|
|
|
|
(concat comint-prompt-regexp
|
|
|
|
"[ \t]*Characters[ \t]+\\([0-9]+\\)-[0-9]+:$"))
|
2011-07-20 02:17:07 -07:00
|
|
|
(caml-string-to-int (match-string 1))))))
|
1998-10-14 00:36:51 -07:00
|
|
|
(goto-char loc)))
|
2000-04-05 11:30:22 -07:00
|
|
|
|
1998-10-14 00:36:51 -07:00
|
|
|
|
2001-12-03 00:59:32 -08:00
|
|
|
;;; orgininal inf-caml.el ended here
|
|
|
|
|
|
|
|
;; as eval-phrase, but ignores errors.
|
|
|
|
|
|
|
|
(defun inferior-caml-just-eval-phrase (arg &optional min max)
|
|
|
|
"Send the phrase containing the point to the CAML process.
|
|
|
|
With prefix-arg send as many phrases as its numeric value,
|
|
|
|
ignoring possible errors during evaluation.
|
|
|
|
|
|
|
|
Optional arguments min max defines a region within which the phrase
|
|
|
|
should lies."
|
|
|
|
(interactive "p")
|
|
|
|
(let ((beg))
|
|
|
|
(while (> arg 0)
|
|
|
|
(setq arg (- arg 1))
|
|
|
|
(setq beg (caml-find-phrase min max))
|
2002-04-18 01:32:23 -07:00
|
|
|
(caml-eval-region beg (point)))
|
2001-12-03 00:59:32 -08:00
|
|
|
beg))
|
|
|
|
|
|
|
|
(defvar caml-previous-output nil
|
|
|
|
"tells the beginning of output in the shell-output buffer, so that the
|
|
|
|
output can be retreived later, asynchronously.")
|
|
|
|
|
|
|
|
;; enriched version of eval-phrase, to repport errors.
|
|
|
|
|
|
|
|
(defun inferior-caml-eval-phrase (arg &optional min max)
|
|
|
|
"Send the phrase containing the point to the CAML process.
|
2010-01-22 04:48:24 -08:00
|
|
|
With prefix-arg send as many phrases as its numeric value,
|
2001-12-03 00:59:32 -08:00
|
|
|
If an error occurs during evalutaion, stop at this phrase and
|
2010-01-22 04:48:24 -08:00
|
|
|
repport the error.
|
2001-12-03 00:59:32 -08:00
|
|
|
|
|
|
|
Return nil if noerror and position of error if any.
|
|
|
|
|
|
|
|
If arg's numeric value is zero or negative, evaluate the current phrase
|
2010-01-22 04:48:24 -08:00
|
|
|
or as many as prefix arg, ignoring evaluation errors.
|
|
|
|
This allows to jump other erroneous phrases.
|
2001-12-03 00:59:32 -08:00
|
|
|
|
|
|
|
Optional arguments min max defines a region within which the phrase
|
|
|
|
should lies."
|
|
|
|
(interactive "p")
|
|
|
|
(if (save-excursion (caml-run-process-if-needed))
|
|
|
|
(progn
|
|
|
|
(setq inferior-caml-output nil)
|
|
|
|
(caml-wait-output 10 1)))
|
|
|
|
(if (< arg 1) (inferior-caml-just-eval-phrase (max 1 (- 0 arg)) min max)
|
|
|
|
(let ((proc (get-buffer-process inferior-caml-buffer-name))
|
|
|
|
(buf (current-buffer))
|
2002-03-27 01:13:09 -08:00
|
|
|
previous-output orig beg end err)
|
2001-12-03 00:59:32 -08:00
|
|
|
(save-window-excursion
|
2002-03-27 01:13:09 -08:00
|
|
|
(while (and (> arg 0) (not err))
|
2001-12-03 00:59:32 -08:00
|
|
|
(setq previous-output (marker-position (process-mark proc)))
|
|
|
|
(setq caml-previous-output previous-output)
|
|
|
|
(setq inferior-caml-output nil)
|
|
|
|
(setq orig (inferior-caml-just-eval-phrase 1 min max))
|
|
|
|
(caml-wait-output)
|
|
|
|
(switch-to-buffer inferior-caml-buffer-name nil)
|
|
|
|
(goto-char previous-output)
|
|
|
|
(cond ((re-search-forward
|
|
|
|
" *Characters \\([01-9][01-9]*\\)-\\([1-9][01-9]*\\):\n[^W]"
|
|
|
|
(point-max) t)
|
2011-07-20 02:17:07 -07:00
|
|
|
(setq beg (caml-string-to-int (caml-match-string 1)))
|
|
|
|
(setq end (caml-string-to-int (caml-match-string 2)))
|
2001-12-03 00:59:32 -08:00
|
|
|
(switch-to-buffer buf)
|
2002-07-23 07:12:03 -07:00
|
|
|
(goto-char orig)
|
|
|
|
(forward-byte end)
|
|
|
|
(setq end (point))
|
|
|
|
(goto-char orig)
|
|
|
|
(forward-byte beg)
|
|
|
|
(setq beg (point))
|
2002-03-27 01:13:09 -08:00
|
|
|
(setq err beg)
|
2001-12-03 00:59:32 -08:00
|
|
|
)
|
|
|
|
((looking-at
|
|
|
|
"Toplevel input:\n[>]\\([^\n]*\\)\n[>]\\(\\( *\\)^*\\)\n")
|
|
|
|
(let ((expr (caml-match-string 1))
|
|
|
|
(column (- (match-end 3) (match-beginning 3)))
|
|
|
|
(width (- (match-end 2) (match-end 3))))
|
|
|
|
(if (string-match "^\\(.*\\)[<]EOF[>]$" expr)
|
2013-03-22 11:19:29 -07:00
|
|
|
(setq expr (substring expr (match-beginning 1)
|
|
|
|
(match-end 1))))
|
2001-12-03 00:59:32 -08:00
|
|
|
(switch-to-buffer buf)
|
|
|
|
(re-search-backward
|
|
|
|
(concat "^" (regexp-quote expr) "$")
|
|
|
|
(- orig 10))
|
|
|
|
(goto-char (+ (match-beginning 0) column))
|
|
|
|
(setq end (+ (point) width)))
|
2002-03-27 01:13:09 -08:00
|
|
|
(setq err beg))
|
2001-12-03 00:59:32 -08:00
|
|
|
((looking-at
|
|
|
|
"Toplevel input:\n>[.]*\\([^.].*\n\\)\\([>].*\n\\)*[>]\\(.*[^.]\\)[.]*\n")
|
|
|
|
(let* ((e1 (caml-match-string 1))
|
|
|
|
(e2 (caml-match-string 3))
|
|
|
|
(expr
|
|
|
|
(concat
|
|
|
|
(regexp-quote e1) "\\(.*\n\\)*" (regexp-quote e2))))
|
|
|
|
(switch-to-buffer buf)
|
|
|
|
(re-search-backward expr orig 'move)
|
|
|
|
(setq end (match-end 0)))
|
2002-03-27 01:13:09 -08:00
|
|
|
(setq err beg))
|
2001-12-03 00:59:32 -08:00
|
|
|
(t
|
|
|
|
(switch-to-buffer buf)))
|
|
|
|
(setq arg (- arg 1))
|
|
|
|
)
|
|
|
|
(pop-to-buffer inferior-caml-buffer-name)
|
2002-03-27 01:13:09 -08:00
|
|
|
(if err
|
2001-12-03 00:59:32 -08:00
|
|
|
(goto-char (point-max))
|
|
|
|
(goto-char previous-output)
|
|
|
|
(goto-char (point-max)))
|
|
|
|
(pop-to-buffer buf))
|
2002-03-27 01:13:09 -08:00
|
|
|
(if err (progn (beep) (caml-overlay-region (point) end))
|
2001-12-03 00:59:32 -08:00
|
|
|
(if inferior-caml-output
|
|
|
|
(message "No error")
|
|
|
|
(message "No output yet...")
|
|
|
|
))
|
2002-03-27 01:13:09 -08:00
|
|
|
err)))
|
2001-12-03 00:59:32 -08:00
|
|
|
|
|
|
|
(defun caml-overlay-region (beg end &optional wait)
|
|
|
|
(interactive "%r")
|
|
|
|
(cond ((fboundp 'make-overlay)
|
|
|
|
(if caml-error-overlay ()
|
|
|
|
(setq caml-error-overlay (make-overlay 1 1))
|
|
|
|
(overlay-put caml-error-overlay 'face 'region))
|
|
|
|
(unwind-protect
|
|
|
|
(progn
|
|
|
|
(move-overlay caml-error-overlay beg end (current-buffer))
|
|
|
|
(beep) (if wait (read-event) (caml-sit-for 60)))
|
2010-01-22 04:48:24 -08:00
|
|
|
(delete-overlay caml-error-overlay)))))
|
2001-12-03 00:59:32 -08:00
|
|
|
|
2011-07-20 02:17:07 -07:00
|
|
|
;; wait some amount for output, that is, until inferior-caml-output is set
|
2001-12-03 00:59:32 -08:00
|
|
|
;; to true. Hence, interleaves sitting for shorts delays and checking the
|
2010-01-22 04:48:24 -08:00
|
|
|
;; flag. Give up after some time. Typing into the source buffer will cancel
|
|
|
|
;; waiting, i.e. may report 'No result yet'
|
2001-12-03 00:59:32 -08:00
|
|
|
|
|
|
|
(defun caml-wait-output (&optional before after)
|
|
|
|
(let ((c 1))
|
|
|
|
(caml-sit-for 0 (or before 1))
|
|
|
|
(let ((c 1))
|
|
|
|
(while (and (not inferior-caml-output) (< c 99) (caml-sit-for 0 c t))
|
|
|
|
(setq c (+ c 1))))
|
|
|
|
(caml-sit-for (or after 0) 1)))
|
|
|
|
|
|
|
|
;; To insert the last output from caml at point
|
|
|
|
(defun caml-insert-last-output ()
|
|
|
|
"Insert the result of the evaluation of previous phrase"
|
|
|
|
(interactive)
|
|
|
|
(let ((pos (process-mark (get-buffer-process inferior-caml-buffer-name))))
|
|
|
|
(insert-buffer-substring inferior-caml-buffer-name
|
|
|
|
caml-previous-output (- pos 2))))
|
|
|
|
|
|
|
|
;; additional bindings
|
2010-01-22 04:48:24 -08:00
|
|
|
|
2002-03-27 01:13:09 -08:00
|
|
|
;(let ((map (lookup-key caml-mode-map [menu-bar caml])))
|
|
|
|
; (define-key map [indent-buffer] '("Indent buffer" . caml-indent-buffer))
|
|
|
|
; (define-key map [eval-buffer] '("Eval buffer" . caml-eval-buffer))
|
2010-01-22 04:48:24 -08:00
|
|
|
;)
|
2002-03-27 01:13:09 -08:00
|
|
|
;(define-key caml-mode-map "\C-c\C-b" 'caml-eval-buffer)
|
2001-12-03 00:59:32 -08:00
|
|
|
|
1997-02-23 08:23:00 -08:00
|
|
|
|
|
|
|
(provide 'inf-caml)
|