1 ;;; geiser-repl.el --- Geiser's REPL
3 ;; Copyright (C) 2009, 2010, 2011 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
)
14 (require 'geiser-autodoc
)
15 (require 'geiser-edit
)
16 (require 'geiser-completion
)
17 (require 'geiser-impl
)
18 (require 'geiser-eval
)
19 (require 'geiser-connection
)
20 (require 'geiser-menu
)
21 (require 'geiser-custom
)
22 (require 'geiser-base
)
31 (defgroup geiser-repl nil
32 "Interacting with the Geiser REPL."
35 (geiser-custom--defcustom geiser-repl-use-other-window t
36 "Whether to Use a window other than the current buffer's when
37 switching to the Geiser REPL buffer."
41 (geiser-custom--defcustom geiser-repl-window-allow-split t
42 "Whether to allow window splitting when switching to the Geiser
47 (geiser-custom--defcustom geiser-repl-history-filename
(expand-file-name "~/.geiser_history")
48 "File where REPL input history is saved, so that it persists between sessions.
49 This is actually the base name: the concrete Scheme
50 implementation name gets appended to it."
54 (geiser-custom--defcustom geiser-repl-history-size comint-input-ring-size
55 "Maximum size of the saved REPL input history."
59 (geiser-custom--defcustom geiser-repl-history-no-dups-p t
60 "Whether to skip duplicates when recording history."
64 (geiser-custom--defcustom geiser-repl-autodoc-p t
65 "Whether to enable `geiser-autodoc-mode' in the REPL by default."
69 (geiser-custom--defcustom geiser-repl-company-p t
70 "Whether to use company-mode for completion, if available."
74 (geiser-custom--defcustom geiser-repl-read-only-prompt-p t
75 "Whether the REPL's prompt should be read-only."
79 (geiser-custom--defcustom geiser-repl-auto-indent-p t
80 "Whether newlines for incomplete sexps are autoindented."
84 (geiser-custom--defcustom geiser-repl-forget-old-errors-p t
85 "Whether to forget old errors upon entering a new expression.
87 When on (the default), every time a new expression is entered in
88 the REPL old error messages are flushed, and using \\[next-error]
89 afterwards will jump only to error locations produced by the new
94 (geiser-custom--defcustom geiser-repl-query-on-exit-p nil
95 "Whether to prompt for confirmation on \\[geiser-repl-exit]."
99 (geiser-custom--defcustom geiser-repl-default-host
"localhost"
100 "Default host when connecting to remote REPLs."
104 (geiser-custom--defcustom geiser-repl-default-port
37146
105 "Default port for connecting to remote REPLs."
110 ;;; Implementation-dependent parameters
112 (geiser-impl--define-caller geiser-repl--binary binary
()
113 "A variable or function returning the path to the scheme binary
114 for this implementation.")
116 (geiser-impl--define-caller geiser-repl--arglist arglist
()
117 "A function taking no arguments and returning a list of
118 arguments to be used when invoking the scheme binary.")
120 (geiser-impl--define-caller geiser-repl--prompt-regexp prompt-regexp
()
121 "A variable (or thunk returning a value) giving the regular
122 expression for this implementation's geiser scheme prompt.")
124 (geiser-impl--define-caller
125 geiser-repl--debugger-prompt-regexp debugger-prompt-regexp
()
126 "A variable (or thunk returning a value) giving the regular
127 expression for this implementation's debugging prompt.")
129 (geiser-impl--define-caller geiser-repl--startup repl-startup
(remote)
130 "Function taking no parameters that is called after the REPL
131 has been initialised. All Geiser functionality is available to
134 (geiser-impl--define-caller geiser-repl--enter-cmd enter-command
(module)
135 "Function taking a module designator and returning a REPL enter
136 module command as a string")
138 (geiser-impl--define-caller geiser-repl--import-cmd import-command
(module)
139 "Function taking a module designator and returning a REPL import
140 module command as a string")
142 (geiser-impl--define-caller geiser-repl--exit-cmd exit-command
()
143 "Function returning the REPL exit command as a string")
146 ;;; Geiser REPL buffers and processes:
148 (defvar geiser-repl--repls nil
)
149 (defvar geiser-repl--closed-repls nil
)
151 (make-variable-buffer-local
152 (defvar geiser-repl--repl nil
))
154 (defsubst geiser-repl--set-this-buffer-repl
(r)
155 (setq geiser-repl--repl r
))
157 (defun geiser-repl--live-p ()
158 (and geiser-repl--repl
159 (get-buffer-process geiser-repl--repl
)))
161 (defun geiser-repl--repl/impl
(impl &optional repls
)
163 (dolist (repl (or repls geiser-repl--repls
))
164 (when (buffer-live-p repl
)
165 (with-current-buffer repl
166 (when (eq geiser-impl--implementation impl
)
167 (throw 'repl repl
)))))))
169 (defun geiser-repl--set-up-repl (impl)
170 (or (and (not impl
) geiser-repl--repl
)
171 (setq geiser-repl--repl
173 geiser-impl--implementation
174 (geiser-impl--guess))))
175 (when impl
(geiser-repl--repl/impl impl
))))))
177 (defun geiser-repl--active-impls ()
179 (dolist (repl geiser-repl--repls act
)
180 (with-current-buffer repl
181 (add-to-list 'act geiser-impl--implementation
)))))
183 (defsubst geiser-repl--repl-name
(impl)
184 (format "%s REPL" (geiser-impl--impl-str impl
)))
186 (defun geiser-repl--to-repl-buffer (impl)
187 (unless (and (eq major-mode
'geiser-repl-mode
)
188 (not (get-buffer-process (current-buffer))))
189 (let* ((old (geiser-repl--repl/impl impl geiser-repl--closed-repls
))
190 (old (and (buffer-live-p old
)
191 (not (get-buffer-process old
))
195 (generate-new-buffer (format "* %s *"
196 (geiser-repl--repl-name impl
)))))
199 (geiser-impl--set-buffer-implementation impl
)))))
201 (defun geiser-repl--read-impl (prompt &optional active
)
202 (geiser-impl--read-impl prompt
(and active
(geiser-repl--active-impls))))
204 (defsubst geiser-repl--only-impl-p
()
205 (and (null (cdr geiser-active-implementations
))
206 (car geiser-active-implementations
)))
208 (defun geiser-repl--get-impl (prompt)
209 (or (geiser-repl--only-impl-p)
210 (and (eq major-mode
'geiser-repl-mode
) geiser-impl--implementation
)
211 (geiser-repl--read-impl prompt
)))
216 (make-variable-buffer-local
217 (defvar geiser-repl--address nil
))
219 (make-variable-buffer-local
220 (defvar geiser-repl--connection nil
))
222 (defun geiser-repl--remote-p () geiser-repl--address
)
224 (defsubst geiser-repl--host
() (car geiser-repl--address
))
225 (defsubst geiser-repl--port
() (cdr geiser-repl--address
))
227 (defun geiser-repl--read-address (&optional host port
)
228 (let ((defhost (or (geiser-repl--host) geiser-repl-default-host
))
229 (defport (or (geiser-repl--port) geiser-repl-default-port
)))
231 (read-string (format "Host (default %s): " defhost
)
233 (or port
(read-number "Port: " defport
)))))
235 (defun geiser-repl--autodoc-mode (n)
236 (when (or geiser-repl-autodoc-p
(< n
0))
237 (geiser--save-msg (geiser-autodoc-mode n
))))
239 (defun geiser-repl--save-remote-data (address)
240 (setq geiser-repl--address address
)
241 (setq header-line-format
(and address
242 (format "Host: %s Port: %s"
244 (geiser-repl--port)))))
246 (defun geiser-repl--start-repl (impl address
)
247 (message "Starting Geiser REPL for %s ..." impl
)
248 (geiser-repl--to-repl-buffer impl
)
250 (goto-char (point-max))
251 (geiser-repl--autodoc-mode -
1)
252 (let* ((prompt-rx (geiser-repl--prompt-regexp impl
))
253 (deb-prompt-rx (geiser-repl--debugger-prompt-regexp impl
))
254 (prompt (geiser-con--combined-prompt prompt-rx deb-prompt-rx
)))
256 (error "Sorry, I don't know how to start a REPL for %s" impl
))
257 (geiser-repl--save-remote-data address
)
258 (geiser-repl--start-scheme impl address prompt
)
259 (geiser-repl--quit-setup)
260 (geiser-repl--history-setup)
261 (add-to-list 'geiser-repl--repls
(current-buffer))
262 (geiser-repl--set-this-buffer-repl (current-buffer))
263 (setq geiser-repl--connection
264 (geiser-con--make-connection (get-buffer-process (current-buffer))
267 (geiser-repl--startup impl address
)
268 (geiser-repl--autodoc-mode 1)
269 (geiser-company--setup geiser-repl-company-p
)
270 (message "%s up and running!" (geiser-repl--repl-name impl
))))
272 (defun geiser-repl--start-scheme (impl address prompt
)
273 (setq comint-prompt-regexp prompt
)
274 (let* ((name (geiser-repl--repl-name impl
))
275 (buff (current-buffer))
276 (args (if address
(list address
)
277 `(,(geiser-repl--binary impl
)
279 ,@(geiser-repl--arglist impl
)))))
281 (apply 'make-comint-in-buffer
`(,name
,buff
,@args
))
282 (error (insert "Unable to start REPL:\n"
283 (error-message-string err
)
285 (error "Couldn't start Geiser")))
286 (geiser-repl--wait-for-prompt 10000)))
288 (defun geiser-repl--wait-for-prompt (timeout)
289 (let ((p (point)) (seen) (buffer (current-buffer)))
290 (while (and (not seen
)
292 (get-buffer-process buffer
))
294 (setq timeout
(- timeout
100))
296 (setq seen
(re-search-forward comint-prompt-regexp nil t
)))
297 (goto-char (point-max))
298 (unless seen
(error "%s" "No prompt found!"))))
300 (defun geiser-repl--is-debugging ()
301 (let ((dp (geiser-con--connection-debug-prompt geiser-repl--connection
)))
303 comint-last-prompt-overlay
305 (goto-char (overlay-start comint-last-prompt-overlay
))
306 (re-search-forward dp
307 (overlay-end comint-last-prompt-overlay
)
310 (defun geiser-repl--connection ()
311 (let ((buffer (geiser-repl--set-up-repl geiser-impl--implementation
)))
312 (or (and (buffer-live-p buffer
)
313 (get-buffer-process buffer
)
314 (with-current-buffer buffer geiser-repl--connection
))
315 (error "No Geiser REPL for this buffer (try M-x run-geiser)"))))
317 (setq geiser-eval--default-connection-function
'geiser-repl--connection
)
319 (defun geiser-repl--prepare-send ()
320 (geiser-con--connection-deactivate geiser-repl--connection
))
322 (defun geiser-repl--send (cmd)
323 (when (and cmd
(eq major-mode
'geiser-repl-mode
))
324 (geiser-repl--prepare-send)
325 (goto-char (point-max))
328 (let ((comint-input-filter (lambda (x) nil
)))
329 (comint-send-input nil t
))))
334 (defconst geiser-repl--history-separator
"\n\0\n")
336 (defsubst geiser-repl--history-file
()
337 (format "%s.%s" geiser-repl-history-filename geiser-impl--implementation
))
339 (defun geiser-repl--read-input-ring ()
340 (let ((comint-input-ring-file-name (geiser-repl--history-file))
341 (comint-input-ring-separator geiser-repl--history-separator
))
342 (comint-read-input-ring t
)))
344 (defun geiser-repl--write-input-ring ()
345 (let ((comint-input-ring-file-name (geiser-repl--history-file))
346 (comint-input-ring-separator geiser-repl--history-separator
))
347 (comint-write-input-ring)))
349 (defun geiser-repl--history-setup ()
350 (set (make-local-variable 'comint-input-ring-size
) geiser-repl-history-size
)
351 (set (make-local-variable 'comint-input-filter
) 'geiser-repl--input-filter
)
352 (geiser-repl--read-input-ring))
355 ;;; Cleaning up on quit
357 (defun geiser-repl--on-quit ()
358 (geiser-repl--write-input-ring)
359 (let ((cb (current-buffer))
360 (impl geiser-impl--implementation
)
361 (comint-prompt-read-only nil
))
362 (geiser-con--connection-deactivate geiser-repl--connection t
)
363 (geiser-con--connection-close geiser-repl--connection
)
364 (setq geiser-repl--repls
(remove cb geiser-repl--repls
))
365 (dolist (buffer (buffer-list))
366 (when (buffer-live-p buffer
)
367 (with-current-buffer buffer
368 (when (and (eq geiser-impl--implementation impl
)
369 (equal cb geiser-repl--repl
))
370 (geiser-repl--set-up-repl geiser-impl--implementation
)))))))
372 (defun geiser-repl--sentinel (proc event
)
373 (let ((pb (process-buffer proc
)))
374 (when (buffer-live-p pb
)
375 (with-current-buffer pb
376 (let ((comint-prompt-read-only nil
)
377 (comint-input-ring-file-name (geiser-repl--history-file))
378 (comint-input-ring-separator geiser-repl--history-separator
))
379 (geiser-repl--on-quit)
380 (push pb geiser-repl--closed-repls
)
381 (goto-char (point-max))
382 (comint-kill-region comint-last-input-start
(point))
383 (insert "\nIt's been nice interacting with you!\n")
384 (insert "Press C-c C-z to bring me back.\n" ))))))
386 (defun geiser-repl--on-kill ()
387 (geiser-repl--on-quit)
388 (setq geiser-repl--closed-repls
389 (remove (current-buffer) geiser-repl--closed-repls
)))
391 (defun geiser-repl--input-filter (str)
392 (not (or (geiser-repl--is-debugging)
393 (string-match "^\\s *$" str
)
394 (string-match "^,quit *$" str
))))
396 (defun geiser-repl--old-input ()
400 (buffer-substring (point) end
))))
402 (defun geiser-repl--quit-setup ()
403 (add-hook 'kill-buffer-hook
'geiser-repl--on-kill nil t
)
404 (set-process-sentinel (get-buffer-process (current-buffer))
405 'geiser-repl--sentinel
))
408 ;;; geiser-repl mode:
410 (defun geiser-repl--bol ()
412 (when (= (point) (comint-bol)) (beginning-of-line)))
414 (defun geiser-repl--beginning-of-defun ()
416 (when comint-last-prompt-overlay
417 (narrow-to-region (overlay-end comint-last-prompt-overlay
) (point)))
418 (let ((beginning-of-defun-function nil
))
419 (beginning-of-defun))))
421 (defun geiser-repl--module-function (&optional module
)
423 (funcall geiser-eval--get-impl-module module
)
426 (defun geiser-repl--doc-module ()
428 (let ((geiser-eval--get-module-function
429 (geiser-impl--method 'find-module geiser-impl--implementation
)))
430 (geiser-doc-module)))
432 (defun geiser-repl--newline-and-indent ()
435 (narrow-to-region comint-last-input-start
(point-max))
439 (defun geiser-repl--last-prompt-end ()
440 (if comint-last-prompt-overlay
441 (overlay-end comint-last-prompt-overlay
)
442 (save-excursion (geiser-repl--bol) (point))))
444 (defun geiser-repl--last-prompt-start ()
445 (if comint-last-prompt-overlay
446 (overlay-start comint-last-prompt-overlay
)
447 (save-excursion (geiser-repl--bol) (point))))
449 (defun geiser-repl--nesting-level ()
451 (narrow-to-region (geiser-repl--last-prompt-end) (point-max))
452 (geiser-syntax--nesting-level)))
454 (defun geiser-repl--send-input ()
455 (let* ((proc (get-buffer-process (current-buffer)))
456 (pmark (and proc
(process-mark proc
)))
457 (intxt (and pmark
(buffer-substring pmark
(point)))))
459 (and geiser-repl-forget-old-errors-p
460 (not (geiser-repl--is-debugging))
461 (compilation-forget-errors))
462 (geiser-repl--prepare-send)
464 (when (string-match "^\\s-*$" intxt
)
465 (comint-send-string proc
(geiser-eval--scheme-str '(:ge no-values
)))
466 (comint-send-string proc
"\n")))))
468 (defun geiser-repl--maybe-send ()
471 (cond ((< p
(geiser-repl--last-prompt-start))
472 (ignore-errors (compile-goto-error)))
473 ((progn (end-of-line) (<= (geiser-repl--nesting-level) 0))
474 (geiser-repl--send-input))
476 (if geiser-repl-auto-indent-p
477 (geiser-repl--newline-and-indent)
480 (defun geiser-repl-tab-dwim (n)
481 "If we're after the last prompt, complete symbol or indent (if
482 there's no symbol at point). Otherwise, go to next error in the REPL
485 (if (>= (point) (geiser-repl--last-prompt-end))
486 (completion-at-point)
487 (compilation-next-error n
)))
489 (defun geiser-repl--previous-error (n)
490 "Go to previous error in the REPL buffer."
492 (compilation-next-error (- n
)))
494 (define-derived-mode geiser-repl-mode comint-mode
"REPL"
495 "Major mode for interacting with an inferior scheme repl process.
496 \\{geiser-repl-mode-map}"
497 (scheme-mode-variables)
498 (set (make-local-variable 'mode-line-process
) nil
)
499 (set (make-local-variable 'comint-use-prompt-regexp
) t
)
500 (set (make-local-variable 'comint-prompt-read-only
)
501 geiser-repl-read-only-prompt-p
)
502 (set (make-local-variable 'beginning-of-defun-function
)
503 'geiser-repl--beginning-of-defun
)
504 (set (make-local-variable 'comint-input-ignoredups
)
505 geiser-repl-history-no-dups-p
)
506 (setq geiser-eval--get-module-function
'geiser-repl--module-function
)
507 (geiser-completion--setup t
)
508 (setq geiser-smart-tab-mode-string
"")
509 (geiser-smart-tab-mode t
)
510 ;; enabling compilation-shell-minor-mode without the annoying highlighter
511 (compilation-setup t
))
513 (define-key geiser-repl-mode-map
"\C-d" 'delete-char
)
514 (define-key geiser-repl-mode-map
"\C-m" 'geiser-repl--maybe-send
)
515 (define-key geiser-repl-mode-map
[return] 'geiser-repl--maybe-send)
516 (define-key geiser-repl-mode-map "\C-j" 'geiser-repl--newline-and-indent)
517 (define-key geiser-repl-mode-map (kbd "TAB") 'geiser-repl-tab-dwim)
518 (define-key geiser-repl-mode-map [backtab] 'geiser-repl--previous-error)
520 (define-key geiser-repl-mode-map "\C-a" 'geiser-repl--bol)
521 (define-key geiser-repl-mode-map (kbd "<home>") 'geiser-repl--bol)
523 (geiser-menu--defmenu repl geiser-repl-mode-map
524 ("Complete symbol" ((kbd "M-TAB"))
525 completion-at-point :enable (geiser--symbol-at-point))
526 ("Complete module name" ((kbd "C-.") (kbd "M-`"))
527 geiser-completion--complete-module :enable (geiser--symbol-at-point))
528 ("Edit symbol" "\M-." geiser-edit-symbol-at-point
529 :enable (geiser--symbol-at-point))
531 ("Switch to module..." "\C-c\C-m" switch-to-geiser-module)
532 ("Import module..." "\C-c\C-i" geiser-repl-import-module)
533 ("Add to load path..." "\C-c\C-r" geiser-add-to-load-path)
535 ("Previous matching input" "\M-p" comint-previous-matching-input-from-input
536 "Previous input matching current")
537 ("Next matching input" "\M-n" comint-next-matching-input-from-input
538 "Next input matching current")
539 ("Previous input" "\C-c\M-p" comint-previous-input)
540 ("Next input" "\C-c\M-n" comint-next-input)
542 (mode "Autodoc mode" ("\C-c\C-da" "\C-c\C-d\C-a") geiser-autodoc-mode)
543 ("Symbol documentation" ("\C-c\C-dd" "\C-c\C-d\C-d")
544 geiser-doc-symbol-at-point
545 "Documentation for symbol at point" :enable (geiser--symbol-at-point))
546 ("Module documentation" ("\C-c\C-dm" "\C-c\C-d\C-m") geiser-repl--doc-module
547 "Documentation for module at point" :enable (geiser--symbol-at-point))
549 ("Kill Scheme interpreter" "\C-c\C-q" geiser-repl-exit
550 :enable (geiser-repl--live-p))
551 ("Restart" "\C-c\C-z" switch-to-geiser :enable (not (geiser-repl--live-p)))
553 (custom "REPL options" geiser-repl))
555 (define-key geiser-repl-mode-map [menu-bar completion] 'undefined)
560 (defun run-geiser (impl)
561 "Start a new Geiser REPL."
563 (list (geiser-repl--get-impl "Start Geiser for scheme implementation: ")))
564 (geiser-repl--start-repl impl nil))
566 (defalias 'geiser 'run-geiser)
568 (defun geiser-connect (impl &optional host port)
569 "Start a new Geiser REPL connected to a remote Scheme process."
571 (list (geiser-repl--get-impl "Connect to Scheme implementation: ")))
572 (geiser-repl--start-repl impl
573 (geiser-repl--read-address host port)))
575 (make-variable-buffer-local
576 (defvar geiser-repl--last-scm-buffer nil))
578 (defun switch-to-geiser (&optional ask impl buffer)
579 "Switch to running Geiser REPL.
580 With prefix argument, ask for which one if more than one is running.
581 If no REPL is running, execute `run-geiser' to start a fresh one."
583 (let* ((impl (or impl geiser-impl--implementation))
584 (in-repl (eq major-mode 'geiser-repl-mode))
585 (in-live-repl (and in-repl (get-buffer-process (current-buffer))))
586 (repl (cond ((and (not ask)
589 (or geiser-repl--repl (car geiser-repl--repls))))
593 (geiser-repl--repl/impl impl)))))
594 (pop-up-windows geiser-repl-window-allow-split))
595 (cond ((or in-live-repl
596 (and (eq (current-buffer) repl) (not (eq repl buffer))))
597 (when (buffer-live-p geiser-repl--last-scm-buffer)
598 (pop-to-buffer geiser-repl--last-scm-buffer)))
599 (repl (pop-to-buffer repl))
600 ((geiser-repl--remote-p) (geiser-connect impl))
601 (t (run-geiser impl)))
602 (when (and buffer (eq major-mode 'geiser-repl-mode))
603 (setq geiser-repl--last-scm-buffer buffer))))
605 (defun switch-to-geiser-module (&optional module buffer)
606 "Switch to running Geiser REPL and try to enter a given module."
608 (let* ((module (or module
609 (geiser-completion--read-module
610 "Switch to module (default top-level): ")))
612 (geiser-repl--enter-cmd geiser-impl--implementation
614 (unless (eq major-mode 'geiser-repl-mode)
615 (switch-to-geiser nil nil (or buffer (current-buffer))))
616 (geiser-repl--send cmd)))
618 (defun geiser-repl-import-module (&optional module)
619 "Import a given module in the current namespace of the REPL."
621 (let* ((module (or module
622 (geiser-completion--read-module "Import module: ")))
624 (geiser-repl--import-cmd geiser-impl--implementation
626 (switch-to-geiser nil nil (current-buffer))
627 (geiser-repl--send cmd)))
629 (defun geiser-repl-exit (&optional arg)
630 "Exit the current REPL.
631 With a prefix argument, force exit by killing the scheme process."
633 (when (or (not geiser-repl-query-on-exit-p)
634 (y-or-n-p "Really quit this REPL? "))
635 (geiser-con--connection-deactivate geiser-repl--connection t)
636 (let ((cmd (and (not arg)
637 (geiser-repl--exit-cmd geiser-impl--implementation))))
639 (when (stringp cmd) (geiser-repl--send cmd))
640 (comint-kill-subjob)))))
645 (defun geiser-repl--repl-list ()
647 (dolist (repl geiser-repl--repls lst)
648 (when (buffer-live-p repl)
649 (with-current-buffer repl
650 (push (cons geiser-impl--implementation
651 geiser-repl--address)
654 (defun geiser-repl--restore (impls)
658 (geiser-repl--start-repl (car impl) (cdr impl))
659 (error (message (error-message-string err)))))))
661 (defun geiser-repl-unload-function ()
662 (dolist (repl geiser-repl--repls)
663 (when (buffer-live-p repl)
664 (with-current-buffer repl
665 (let ((geiser-repl-query-on-exit-p nil)) (geiser-repl-exit))
670 (provide 'geiser-repl)