1 ;;; geiser-repl.el --- Geiser's REPL
3 ;; Copyright (C) 2009, 2010 Jose Antonio Ortega Ruiz
5 ;; This program is free software; you can redistribute it and/or
6 ;; modify it under the terms of the Modified BSD License. You should
7 ;; have received a copy of the license along with this program. If
8 ;; not, see <http://www.xfree86.org/3.3.6/COPYRIGHT2.html#5>.
12 (require 'geiser-company
)
13 (require 'geiser-autodoc
)
14 (require 'geiser-edit
)
15 (require 'geiser-completion
)
16 (require 'geiser-impl
)
17 (require 'geiser-eval
)
18 (require 'geiser-connection
)
19 (require 'geiser-menu
)
20 (require 'geiser-custom
)
21 (require 'geiser-base
)
30 (defgroup geiser-repl nil
31 "Interacting with the Geiser REPL."
34 (geiser-custom--defcustom geiser-repl-use-other-window t
35 "Whether to Use a window other than the current buffer's when
36 switching to the Geiser REPL buffer."
40 (geiser-custom--defcustom geiser-repl-window-allow-split t
41 "Whether to allow window splitting when switching to the Geiser
46 (geiser-custom--defcustom geiser-repl-history-filename
(expand-file-name "~/.geiser_history")
47 "File where REPL input history is saved, so that it persists between sessions.
48 This is actually the base name: the concrete Scheme
49 implementation name gets appended to it."
53 (geiser-custom--defcustom geiser-repl-history-size comint-input-ring-size
54 "Maximum size of the saved REPL input history."
58 (geiser-custom--defcustom geiser-repl-history-no-dups-p t
59 "Whether to skip duplicates when recording history."
63 (geiser-custom--defcustom geiser-repl-autodoc-p t
64 "Whether to enable `geiser-autodoc-mode' in the REPL by default."
68 (geiser-custom--defcustom geiser-repl-company-p t
69 "Whether to use company-mode for completion, if available."
73 (geiser-custom--defcustom geiser-repl-read-only-prompt-p t
74 "Whether the REPL's prompt should be read-only."
78 (geiser-custom--defcustom geiser-repl-auto-indent-p t
79 "Whether newlines for incomplete sexps are autoindented."
83 (geiser-custom--defcustom geiser-repl-forget-old-errors-p t
84 "Whether to forget old errors upon entering a new expression.
86 When on (the default), every time a new expression is entered in
87 the REPL old error messages are flushed, and using \\[next-error]
88 afterwards will jump only to error locations produced by the new
93 (geiser-custom--defcustom geiser-repl-query-on-exit-p nil
94 "Whether to prompt for confirmation on \\[geiser-repl-exit]."
98 (geiser-custom--defcustom geiser-repl-default-host
"localhost"
99 "Default host when connecting to remote REPLs."
103 (geiser-custom--defcustom geiser-repl-default-port
37146
104 "Default port for connecting to remote REPLs."
109 ;;; Implementation-dependent parameters
111 (geiser-impl--define-caller geiser-repl--binary binary
()
112 "A variable or function returning the path to the scheme binary
113 for this implementation.")
115 (geiser-impl--define-caller geiser-repl--arglist arglist
()
116 "A function taking no arguments and returning a list of
117 arguments to be used when invoking the scheme binary.")
119 (geiser-impl--define-caller geiser-repl--prompt-regexp prompt-regexp
()
120 "A variable (or thunk returning a value) giving the regular
121 expression for this implementation's geiser scheme prompt.")
123 (geiser-impl--define-caller
124 geiser-repl--debugger-prompt-regexp debugger-prompt-regexp
()
125 "A variable (or thunk returning a value) giving the regular
126 expression for this implementation's debugging prompt.")
128 (geiser-impl--define-caller geiser-repl--startup repl-startup
(remote)
129 "Function taking no parameters that is called after the REPL
130 has been initialised. All Geiser functionality is available to
133 (geiser-impl--define-caller geiser-repl--enter-cmd enter-command
(module)
134 "Function taking a module designator and returning a REPL enter
135 module command as a string")
137 (geiser-impl--define-caller geiser-repl--import-cmd import-command
(module)
138 "Function taking a module designator and returning a REPL import
139 module command as a string")
141 (geiser-impl--define-caller geiser-repl--exit-cmd exit-command
()
142 "Function returning the REPL exit command as a string")
145 ;;; Geiser REPL buffers and processes:
147 (defvar geiser-repl--repls nil
)
148 (defvar geiser-repl--closed-repls nil
)
150 (make-variable-buffer-local
151 (defvar geiser-repl--repl nil
))
153 (defsubst geiser-repl--set-this-buffer-repl
(r)
154 (setq geiser-repl--repl r
))
156 (defun geiser-repl--live-p ()
157 (and geiser-repl--repl
158 (get-buffer-process geiser-repl--repl
)))
160 (defun geiser-repl--repl/impl
(impl &optional repls
)
162 (dolist (repl (or repls geiser-repl--repls
))
163 (when (buffer-live-p repl
)
164 (with-current-buffer repl
165 (when (eq geiser-impl--implementation impl
)
166 (throw 'repl repl
)))))))
168 (defun geiser-repl--set-up-repl (impl)
169 (or (and (not impl
) geiser-repl--repl
)
170 (setq geiser-repl--repl
172 geiser-impl--implementation
173 (geiser-impl--guess))))
174 (when impl
(geiser-repl--repl/impl impl
))))))
176 (defun geiser-repl--active-impls ()
178 (dolist (repl geiser-repl--repls act
)
179 (with-current-buffer repl
180 (add-to-list 'act geiser-impl--implementation
)))))
182 (defsubst geiser-repl--repl-name
(impl)
183 (format "%s REPL" (geiser-impl--impl-str impl
)))
185 (defun geiser-repl--to-repl-buffer (impl)
186 (unless (and (eq major-mode
'geiser-repl-mode
)
187 (not (get-buffer-process (current-buffer))))
188 (let* ((old (geiser-repl--repl/impl impl geiser-repl--closed-repls
))
189 (old (and (buffer-live-p old
)
190 (not (get-buffer-process old
))
194 (generate-new-buffer (format "* %s *"
195 (geiser-repl--repl-name impl
)))))
198 (geiser-impl--set-buffer-implementation impl
)))))
200 (defun geiser-repl--read-impl (prompt &optional active
)
201 (geiser-impl--read-impl prompt
(and active
(geiser-repl--active-impls))))
203 (defsubst geiser-repl--only-impl-p
()
204 (and (null (cdr geiser-active-implementations
))
205 (car geiser-active-implementations
)))
207 (defun geiser-repl--get-impl (prompt)
208 (or (geiser-repl--only-impl-p)
209 (and (eq major-mode
'geiser-repl-mode
) geiser-impl--implementation
)
210 (geiser-repl--read-impl prompt
)))
215 (make-variable-buffer-local
216 (defvar geiser-repl--address nil
))
218 (make-variable-buffer-local
219 (defvar geiser-repl--connection nil
))
221 (defun geiser-repl--remote-p () geiser-repl--address
)
223 (defsubst geiser-repl--host
() (car geiser-repl--address
))
224 (defsubst geiser-repl--port
() (cdr geiser-repl--address
))
226 (defun geiser-repl--read-address (&optional host port
)
227 (let ((defhost (or (geiser-repl--host) geiser-repl-default-host
))
228 (defport (or (geiser-repl--port) geiser-repl-default-port
)))
230 (read-string (format "Host (default %s): " defhost
)
232 (or port
(read-number "Port: " defport
)))))
234 (defun geiser-repl--autodoc-mode (n)
235 (when (or geiser-repl-autodoc-p
(< n
0))
236 (geiser--save-msg (geiser-autodoc-mode n
))))
238 (defun geiser-repl--save-remote-data (address)
239 (setq geiser-repl--address address
)
240 (setq header-line-format
(and address
241 (format "Host: %s Port: %s"
243 (geiser-repl--port)))))
245 (defun geiser-repl--start-repl (impl address
)
246 (message "Starting Geiser REPL for %s ..." impl
)
247 (geiser-repl--to-repl-buffer impl
)
249 (goto-char (point-max))
250 (geiser-repl--autodoc-mode -
1)
251 (let* ((prompt-rx (geiser-repl--prompt-regexp impl
))
252 (deb-prompt-rx (geiser-repl--debugger-prompt-regexp impl
))
253 (prompt (geiser-con--combined-prompt prompt-rx deb-prompt-rx
)))
255 (error "Sorry, I don't know how to start a REPL for %s" impl
))
256 (geiser-repl--save-remote-data address
)
257 (geiser-repl--start-scheme impl address prompt
)
258 (geiser-repl--history-setup)
259 (add-to-list 'geiser-repl--repls
(current-buffer))
260 (geiser-repl--set-this-buffer-repl (current-buffer))
261 (setq geiser-repl--connection
262 (geiser-con--make-connection (get-buffer-process (current-buffer))
265 (geiser-repl--startup impl address
)
266 (geiser-repl--autodoc-mode 1)
267 (message "%s up and running!" (geiser-repl--repl-name impl
))))
269 (defun geiser-repl--start-scheme (impl address prompt
)
270 (setq comint-prompt-regexp prompt
)
271 (let* ((name (geiser-repl--repl-name impl
))
272 (buff (current-buffer))
273 (args (if address
(list address
)
274 `(,(geiser-repl--binary impl
)
276 ,@(geiser-repl--arglist impl
)))))
278 (apply 'make-comint-in-buffer
`(,name
,buff
,@args
))
279 (error (insert "Unable to start REPL:\n"
280 (error-message-string err
)
282 (error "Couldn't start Geiser")))
283 (geiser-repl--wait-for-prompt 10000)))
285 (defun geiser-repl--wait-for-prompt (timeout)
286 (let ((p (point)) (seen) (buffer (current-buffer)))
287 (while (and (not seen
)
289 (get-buffer-process buffer
))
291 (setq timeout
(- timeout
100))
293 (setq seen
(re-search-forward comint-prompt-regexp nil t
)))
294 (goto-char (point-max))
295 (unless seen
(error "%s" "No prompt found!"))))
297 (defun geiser-repl--is-debugging ()
298 (let ((dp (geiser-con--connection-debug-prompt geiser-repl--connection
)))
300 comint-last-prompt-overlay
302 (goto-char (overlay-start comint-last-prompt-overlay
))
303 (re-search-forward dp
304 (overlay-end comint-last-prompt-overlay
)
307 (defun geiser-repl--connection ()
308 (let ((buffer (geiser-repl--set-up-repl geiser-impl--implementation
)))
309 (or (and (buffer-live-p buffer
)
310 (get-buffer-process buffer
)
311 (with-current-buffer buffer geiser-repl--connection
))
312 (error "No Geiser REPL for this buffer (try M-x run-geiser)"))))
314 (setq geiser-eval--default-connection-function
'geiser-repl--connection
)
316 (defun geiser-repl--prepare-send ()
317 (geiser-con--connection-deactivate geiser-repl--connection
))
319 (defun geiser-repl--send (cmd)
320 (when (and cmd
(eq major-mode
'geiser-repl-mode
))
321 (geiser-repl--prepare-send)
322 (goto-char (point-max))
325 (let ((comint-input-filter (lambda (x) nil
)))
326 (comint-send-input nil t
))))
328 (defun geiser-repl--send-silent (cmd)
329 (geiser-repl--prepare-send)
330 (comint-redirect-results-list cmd
".+" 0))
333 ;;; REPL history and clean-up:
335 (defsubst geiser-repl--history-file
()
336 (format "%s.%s" geiser-repl-history-filename geiser-impl--implementation
))
338 (defun geiser-repl--on-quit ()
339 (comint-write-input-ring)
340 (let ((cb (current-buffer))
341 (impl geiser-impl--implementation
)
342 (comint-prompt-read-only nil
))
343 (geiser-con--connection-deactivate geiser-repl--connection t
)
344 (geiser-con--connection-close geiser-repl--connection
)
345 (setq geiser-repl--repls
(remove cb geiser-repl--repls
))
346 (dolist (buffer (buffer-list))
347 (when (buffer-live-p buffer
)
348 (with-current-buffer buffer
349 (when (and (eq geiser-impl--implementation impl
)
350 (equal cb geiser-repl--repl
))
351 (geiser-repl--set-up-repl geiser-impl--implementation
)))))))
353 (defun geiser-repl--sentinel (proc event
)
354 (let ((pb (process-buffer proc
)))
355 (when (buffer-live-p pb
)
356 (with-current-buffer pb
357 (let ((comint-prompt-read-only nil
)
358 (comint-input-ring-file-name (geiser-repl--history-file)))
359 (geiser-repl--on-quit)
360 (push pb geiser-repl--closed-repls
)
361 (goto-char (point-max))
362 (comint-kill-region comint-last-input-start
(point))
363 (insert "\nIt's been nice interacting with you!\n")
364 (insert "Press C-c C-z to bring me back.\n" ))))))
366 (defun geiser-repl--on-kill ()
367 (geiser-repl--on-quit)
368 (setq geiser-repl--closed-repls
369 (remove (current-buffer) geiser-repl--closed-repls
)))
371 (defun geiser-repl--input-filter (str)
372 (not (or (geiser-repl--is-debugging)
373 (string-match "^\\s *$" str
)
374 (string-match "^,quit *$" str
))))
376 (defun geiser-repl--old-input ()
380 (buffer-substring (point) end
))))
382 (defun geiser-repl--history-setup ()
383 (set (make-local-variable 'comint-input-ring-file-name
)
384 (geiser-repl--history-file))
385 (set (make-local-variable 'comint-input-ring-size
) geiser-repl-history-size
)
386 (set (make-local-variable 'comint-input-filter
) 'geiser-repl--input-filter
)
387 (set (make-local-variable 'comint-get-old-input
) 'geiser-repl--old-input
)
388 (add-hook 'kill-buffer-hook
'geiser-repl--on-kill nil t
)
389 (comint-read-input-ring t
)
390 (set-process-sentinel (get-buffer-process (current-buffer))
391 'geiser-repl--sentinel
))
394 ;;; geiser-repl mode:
396 (defun geiser-repl--bol ()
398 (when (= (point) (comint-bol)) (beginning-of-line)))
400 (defun geiser-repl--beginning-of-defun ()
402 (when comint-last-prompt-overlay
403 (narrow-to-region (overlay-end comint-last-prompt-overlay
) (point)))
404 (let ((beginning-of-defun-function nil
))
405 (beginning-of-defun))))
407 (defun geiser-repl--module-function (&optional ignore
) :f
)
409 (defun geiser-repl--doc-module ()
411 (let ((geiser-eval--get-module-function
412 (geiser-impl--method 'find-module geiser-impl--implementation
)))
413 (geiser-doc-module)))
415 (defun geiser-repl--newline-and-indent ()
418 (narrow-to-region comint-last-input-start
(point-max))
422 (defun geiser-repl--last-prompt-end ()
423 (if comint-last-prompt-overlay
424 (overlay-end comint-last-prompt-overlay
)
425 (save-excursion (geiser-repl--bol) (point))))
427 (defun geiser-repl--last-prompt-start ()
428 (if comint-last-prompt-overlay
429 (overlay-start comint-last-prompt-overlay
)
430 (save-excursion (geiser-repl--bol) (point))))
432 (defun geiser-repl--nesting-level ()
434 (narrow-to-region (geiser-repl--last-prompt-end) (point-max))
435 (geiser-syntax--nesting-level)))
437 (defun geiser-repl--send-input ()
438 (let* ((proc (get-buffer-process (current-buffer)))
439 (pmark (and proc
(process-mark proc
)))
440 (intxt (and pmark
(buffer-substring pmark
(point)))))
442 (and geiser-repl-forget-old-errors-p
443 (not (geiser-repl--is-debugging))
444 (compilation-forget-errors))
445 (geiser-repl--prepare-send)
447 (when (string-match "^\\s-*$" intxt
)
448 (comint-send-string proc
(geiser-eval--scheme-str '(:ge no-values
)))
449 (comint-send-string proc
"\n")))))
451 (defun geiser-repl--maybe-send ()
454 (cond ((< p
(geiser-repl--last-prompt-start))
455 (ignore-errors (compile-goto-error)))
456 ((progn (end-of-line) (<= (geiser-repl--nesting-level) 0))
457 (geiser-repl--send-input))
459 (if geiser-repl-auto-indent-p
460 (geiser-repl--newline-and-indent)
463 (defun geiser-repl--tab (n)
464 "If we're after the last prompt, complete symbol or indent (if
465 there's no symbol at point). Otherwise, go to next error in the REPL
468 (if (> (point) (geiser-repl--last-prompt-end))
469 (geiser-completion--maybe-complete)
470 (compilation-next-error n
)))
472 (defun geiser-repl--previous-error (n)
473 "Go to previous error in the REPL buffer."
475 (compilation-next-error (- n
)))
477 (define-derived-mode geiser-repl-mode comint-mode
"REPL"
478 "Major mode for interacting with an inferior scheme repl process.
479 \\{geiser-repl-mode-map}"
480 (scheme-mode-variables)
481 (set (make-local-variable 'mode-line-process
) nil
)
482 (set (make-local-variable 'comint-use-prompt-regexp
) nil
)
483 (set (make-local-variable 'comint-prompt-read-only
)
484 geiser-repl-read-only-prompt-p
)
485 (set (make-local-variable 'beginning-of-defun-function
)
486 'geiser-repl--beginning-of-defun
)
487 (set (make-local-variable 'comint-input-ignoredups
)
488 geiser-repl-history-no-dups-p
)
489 (setq geiser-eval--get-module-function
'geiser-repl--module-function
)
490 (geiser-company--setup geiser-repl-company-p
)
491 ;; enabling compilation-shell-minor-mode without the annoying highlighter
492 (compilation-setup t
))
494 (define-key geiser-repl-mode-map
"\C-d" 'delete-char
)
495 (define-key geiser-repl-mode-map
"\C-m" 'geiser-repl--maybe-send
)
496 (define-key geiser-repl-mode-map
[return] 'geiser-repl--maybe-send)
497 (define-key geiser-repl-mode-map "\C-j" 'geiser-repl--newline-and-indent)
498 (define-key geiser-repl-mode-map (kbd "TAB") 'geiser-repl--tab)
499 (define-key geiser-repl-mode-map [backtab] 'geiser-repl--previous-error)
501 (define-key geiser-repl-mode-map "\C-a" 'geiser-repl--bol)
502 (define-key geiser-repl-mode-map (kbd "<home>") 'geiser-repl--bol)
504 (geiser-menu--defmenu repl geiser-repl-mode-map
505 ("Complete symbol" ((kbd "M-TAB"))
506 geiser-repl--tab :enable (symbol-at-point))
507 ("Complete module name" ((kbd "C-.") (kbd "M-`"))
508 geiser-completion--complete-module :enable (symbol-at-point))
509 ("Edit symbol" "\M-." geiser-edit-symbol-at-point
510 :enable (symbol-at-point))
512 ("Switch to module..." "\C-c\C-m" switch-to-geiser-module)
513 ("Import module..." "\C-c\C-i" geiser-repl-import-module)
515 ("Previous matching input" "\M-p" comint-previous-matching-input-from-input
516 "Previous input matching current")
517 ("Next matching input" "\M-n" comint-next-matching-input-from-input
518 "Next input matching current")
519 ("Previous input" "\C-c\M-p" comint-previous-input)
520 ("Next input" "\C-c\M-n" comint-next-input)
522 (mode "Autodoc mode" ("\C-c\C-da" "\C-c\C-d\C-a") geiser-autodoc-mode)
523 ("Symbol documentation" ("\C-c\C-dd" "\C-c\C-d\C-d")
524 geiser-doc-symbol-at-point
525 "Documentation for symbol at point" :enable (symbol-at-point))
526 ("Module documentation" ("\C-c\C-dm" "\C-c\C-d\C-m") geiser-repl--doc-module
527 "Documentation for module at point" :enable (symbol-at-point))
529 ("Kill Scheme interpreter" "\C-c\C-q" geiser-repl-exit
530 :enable (geiser-repl--live-p))
531 ("Restart" "\C-c\C-z" switch-to-geiser :enable (not (geiser-repl--live-p)))
533 (custom "REPL options" geiser-repl))
535 (define-key geiser-repl-mode-map [menu-bar completion] 'undefined)
540 (defun run-geiser (impl)
541 "Start a new Geiser REPL."
543 (list (geiser-repl--get-impl "Start Geiser for scheme implementation: ")))
544 (geiser-repl--start-repl impl nil))
546 (defalias 'geiser 'run-geiser)
548 (defun geiser-connect (impl &optional host port)
549 "Start a new Geiser REPL connected to a remote Scheme process."
551 (list (geiser-repl--get-impl "Connect to Scheme implementation: ")))
552 (geiser-repl--start-repl impl
553 (geiser-repl--read-address host port)))
555 (make-variable-buffer-local
556 (defvar geiser-repl--last-scm-buffer nil))
558 (defun switch-to-geiser (&optional ask impl buffer)
559 "Switch to running Geiser REPL.
560 With prefix argument, ask for which one if more than one is running.
561 If no REPL is running, execute `run-geiser' to start a fresh one."
563 (let* ((impl (or impl geiser-impl--implementation))
564 (in-repl (eq major-mode 'geiser-repl-mode))
565 (in-live-repl (and in-repl (get-buffer-process (current-buffer))))
566 (repl (cond ((and (not ask)
569 (or geiser-repl--repl (car geiser-repl--repls))))
573 (geiser-repl--repl/impl impl)))))
574 (pop-up-windows geiser-repl-window-allow-split))
575 (cond ((or in-live-repl
576 (and (eq (current-buffer) repl) (not (eq repl buffer))))
577 (when (buffer-live-p geiser-repl--last-scm-buffer)
578 (pop-to-buffer geiser-repl--last-scm-buffer)))
579 (repl (pop-to-buffer repl))
580 ((geiser-repl--remote-p) (geiser-connect impl))
581 (t (run-geiser impl)))
582 (when (and buffer (eq major-mode 'geiser-repl-mode))
583 (setq geiser-repl--last-scm-buffer buffer))))
585 (defun switch-to-geiser-module (&optional module buffer)
586 "Switch to running Geiser REPL and try to enter a given module."
588 (let* ((module (or module
589 (geiser-completion--read-module
590 "Switch to module (default top-level): ")))
592 (geiser-repl--enter-cmd geiser-impl--implementation
594 (unless (eq major-mode 'geiser-repl-mode)
595 (switch-to-geiser nil nil (or buffer (current-buffer))))
596 (geiser-repl--send cmd)))
598 (defun geiser-repl-import-module (&optional module)
599 "Import a given module in the current namespace of the REPL."
601 (let* ((module (or module
602 (geiser-completion--read-module "Import module: ")))
604 (geiser-repl--import-cmd geiser-impl--implementation
606 (switch-to-geiser nil nil (current-buffer))
607 (geiser-repl--send cmd)))
609 (defun geiser-repl-exit (&optional arg)
610 "Exit the current REPL.
611 With a prefix argument, force exit by killing the scheme process."
613 (when (or (not geiser-repl-query-on-exit-p)
614 (y-or-n-p "Really quit this REPL? "))
615 (let ((cmd (and (not arg)
616 (geiser-repl--exit-cmd geiser-impl--implementation))))
618 (when (stringp cmd) (geiser-repl--send cmd))
619 (comint-kill-subjob)))))
624 (defun geiser-repl--repl-list ()
626 (dolist (repl geiser-repl--repls lst)
627 (when (buffer-live-p repl)
628 (with-current-buffer repl
629 (push (cons geiser-impl--implementation
630 geiser-repl--address)
633 (defun geiser-repl--restore (impls)
637 (geiser-repl--start-repl (car impl) (cdr impl))
638 (error (message (error-message-string err)))))))
640 (defun geiser-repl-unload-function ()
641 (dolist (repl geiser-repl--repls)
642 (when (buffer-live-p repl)
643 (with-current-buffer repl
644 (let ((geiser-repl-query-on-exit-p nil)) (geiser-repl-exit))
649 (provide 'geiser-repl)