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 (message "%s up and running!" (geiser-repl--repl-name impl
))))
271 (defun geiser-repl--start-scheme (impl address prompt
)
272 (setq comint-prompt-regexp prompt
)
273 (let* ((name (geiser-repl--repl-name impl
))
274 (buff (current-buffer))
275 (args (if address
(list address
)
276 `(,(geiser-repl--binary impl
)
278 ,@(geiser-repl--arglist impl
)))))
280 (apply 'make-comint-in-buffer
`(,name
,buff
,@args
))
281 (error (insert "Unable to start REPL:\n"
282 (error-message-string err
)
284 (error "Couldn't start Geiser")))
285 (geiser-repl--wait-for-prompt 10000)))
287 (defun geiser-repl--wait-for-prompt (timeout)
288 (let ((p (point)) (seen) (buffer (current-buffer)))
289 (while (and (not seen
)
291 (get-buffer-process buffer
))
293 (setq timeout
(- timeout
100))
295 (setq seen
(re-search-forward comint-prompt-regexp nil t
)))
296 (goto-char (point-max))
297 (unless seen
(error "%s" "No prompt found!"))))
299 (defun geiser-repl--is-debugging ()
300 (let ((dp (geiser-con--connection-debug-prompt geiser-repl--connection
)))
302 comint-last-prompt-overlay
304 (goto-char (overlay-start comint-last-prompt-overlay
))
305 (re-search-forward dp
306 (overlay-end comint-last-prompt-overlay
)
309 (defun geiser-repl--connection ()
310 (let ((buffer (geiser-repl--set-up-repl geiser-impl--implementation
)))
311 (or (and (buffer-live-p buffer
)
312 (get-buffer-process buffer
)
313 (with-current-buffer buffer geiser-repl--connection
))
314 (error "No Geiser REPL for this buffer (try M-x run-geiser)"))))
316 (setq geiser-eval--default-connection-function
'geiser-repl--connection
)
318 (defun geiser-repl--prepare-send ()
319 (geiser-con--connection-deactivate geiser-repl--connection
))
321 (defun geiser-repl--send (cmd)
322 (when (and cmd
(eq major-mode
'geiser-repl-mode
))
323 (geiser-repl--prepare-send)
324 (goto-char (point-max))
327 (let ((comint-input-filter (lambda (x) nil
)))
328 (comint-send-input nil t
))))
333 (defconst geiser-repl--history-separator
"\n\0\n")
335 (defsubst geiser-repl--history-file
()
336 (format "%s.%s" geiser-repl-history-filename geiser-impl--implementation
))
338 (defun geiser-repl--read-input-ring ()
339 (let ((comint-input-ring-file-name (geiser-repl--history-file))
340 (comint-input-ring-separator geiser-repl--history-separator
))
341 (comint-read-input-ring t
)))
343 (defun geiser-repl--write-input-ring ()
344 (let ((comint-input-ring-file-name (geiser-repl--history-file))
345 (comint-input-ring-separator geiser-repl--history-separator
))
346 (comint-write-input-ring)))
348 (defun geiser-repl--history-setup ()
349 (set (make-local-variable 'comint-input-ring-size
) geiser-repl-history-size
)
350 (set (make-local-variable 'comint-input-filter
) 'geiser-repl--input-filter
)
351 (geiser-repl--read-input-ring))
354 ;;; Cleaning up on quit
356 (defun geiser-repl--on-quit ()
357 (geiser-repl--write-input-ring)
358 (let ((cb (current-buffer))
359 (impl geiser-impl--implementation
)
360 (comint-prompt-read-only nil
))
361 (geiser-con--connection-deactivate geiser-repl--connection t
)
362 (geiser-con--connection-close geiser-repl--connection
)
363 (setq geiser-repl--repls
(remove cb geiser-repl--repls
))
364 (dolist (buffer (buffer-list))
365 (when (buffer-live-p buffer
)
366 (with-current-buffer buffer
367 (when (and (eq geiser-impl--implementation impl
)
368 (equal cb geiser-repl--repl
))
369 (geiser-repl--set-up-repl geiser-impl--implementation
)))))))
371 (defun geiser-repl--sentinel (proc event
)
372 (let ((pb (process-buffer proc
)))
373 (when (buffer-live-p pb
)
374 (with-current-buffer pb
375 (let ((comint-prompt-read-only nil
)
376 (comint-input-ring-file-name (geiser-repl--history-file))
377 (comint-input-ring-separator geiser-repl--history-separator
))
378 (geiser-repl--on-quit)
379 (push pb geiser-repl--closed-repls
)
380 (goto-char (point-max))
381 (comint-kill-region comint-last-input-start
(point))
382 (insert "\nIt's been nice interacting with you!\n")
383 (insert "Press C-c C-z to bring me back.\n" ))))))
385 (defun geiser-repl--on-kill ()
386 (geiser-repl--on-quit)
387 (setq geiser-repl--closed-repls
388 (remove (current-buffer) geiser-repl--closed-repls
)))
390 (defun geiser-repl--input-filter (str)
391 (not (or (geiser-repl--is-debugging)
392 (string-match "^\\s *$" str
)
393 (string-match "^,quit *$" str
))))
395 (defun geiser-repl--old-input ()
399 (buffer-substring (point) end
))))
401 (defun geiser-repl--quit-setup ()
402 (add-hook 'kill-buffer-hook
'geiser-repl--on-kill nil t
)
403 (set-process-sentinel (get-buffer-process (current-buffer))
404 'geiser-repl--sentinel
))
407 ;;; geiser-repl mode:
409 (defun geiser-repl--bol ()
411 (when (= (point) (comint-bol)) (beginning-of-line)))
413 (defun geiser-repl--beginning-of-defun ()
415 (when comint-last-prompt-overlay
416 (narrow-to-region (overlay-end comint-last-prompt-overlay
) (point)))
417 (let ((beginning-of-defun-function nil
))
418 (beginning-of-defun))))
420 (defun geiser-repl--module-function (&optional ignore
) :f
)
422 (defun geiser-repl--doc-module ()
424 (let ((geiser-eval--get-module-function
425 (geiser-impl--method 'find-module geiser-impl--implementation
)))
426 (geiser-doc-module)))
428 (defun geiser-repl--newline-and-indent ()
431 (narrow-to-region comint-last-input-start
(point-max))
435 (defun geiser-repl--last-prompt-end ()
436 (if comint-last-prompt-overlay
437 (overlay-end comint-last-prompt-overlay
)
438 (save-excursion (geiser-repl--bol) (point))))
440 (defun geiser-repl--last-prompt-start ()
441 (if comint-last-prompt-overlay
442 (overlay-start comint-last-prompt-overlay
)
443 (save-excursion (geiser-repl--bol) (point))))
445 (defun geiser-repl--nesting-level ()
447 (narrow-to-region (geiser-repl--last-prompt-end) (point-max))
448 (geiser-syntax--nesting-level)))
450 (defun geiser-repl--send-input ()
451 (let* ((proc (get-buffer-process (current-buffer)))
452 (pmark (and proc
(process-mark proc
)))
453 (intxt (and pmark
(buffer-substring pmark
(point)))))
455 (and geiser-repl-forget-old-errors-p
456 (not (geiser-repl--is-debugging))
457 (compilation-forget-errors))
458 (geiser-repl--prepare-send)
460 (when (string-match "^\\s-*$" intxt
)
461 (comint-send-string proc
(geiser-eval--scheme-str '(:ge no-values
)))
462 (comint-send-string proc
"\n")))))
464 (defun geiser-repl--maybe-send ()
467 (cond ((< p
(geiser-repl--last-prompt-start))
468 (ignore-errors (compile-goto-error)))
469 ((progn (end-of-line) (<= (geiser-repl--nesting-level) 0))
470 (geiser-repl--send-input))
472 (if geiser-repl-auto-indent-p
473 (geiser-repl--newline-and-indent)
476 (defun geiser-repl--tab (n)
477 "If we're after the last prompt, complete symbol or indent (if
478 there's no symbol at point). Otherwise, go to next error in the REPL
481 (if (> (point) (geiser-repl--last-prompt-end))
482 (completion-at-point)
483 (compilation-next-error n
)))
485 (defun geiser-repl--previous-error (n)
486 "Go to previous error in the REPL buffer."
488 (compilation-next-error (- n
)))
490 (define-derived-mode geiser-repl-mode comint-mode
"REPL"
491 "Major mode for interacting with an inferior scheme repl process.
492 \\{geiser-repl-mode-map}"
493 (scheme-mode-variables)
494 (set (make-local-variable 'mode-line-process
) nil
)
495 (set (make-local-variable 'comint-use-prompt-regexp
) t
)
496 (set (make-local-variable 'comint-prompt-read-only
)
497 geiser-repl-read-only-prompt-p
)
498 (set (make-local-variable 'beginning-of-defun-function
)
499 'geiser-repl--beginning-of-defun
)
500 (set (make-local-variable 'comint-input-ignoredups
)
501 geiser-repl-history-no-dups-p
)
502 (setq geiser-eval--get-module-function
'geiser-repl--module-function
)
503 (geiser-completion--setup t
)
504 (setq geiser-smart-tab-mode-string
"")
505 (geiser-smart-tab-mode t
)
506 (geiser-company--setup geiser-repl-company-p
)
507 ;; enabling compilation-shell-minor-mode without the annoying highlighter
508 (compilation-setup t
))
510 (define-key geiser-repl-mode-map
"\C-d" 'delete-char
)
511 (define-key geiser-repl-mode-map
"\C-m" 'geiser-repl--maybe-send
)
512 (define-key geiser-repl-mode-map
[return] 'geiser-repl--maybe-send)
513 (define-key geiser-repl-mode-map "\C-j" 'geiser-repl--newline-and-indent)
514 (define-key geiser-repl-mode-map (kbd "TAB") 'geiser-repl--tab)
515 (define-key geiser-repl-mode-map [backtab] 'geiser-repl--previous-error)
517 (define-key geiser-repl-mode-map "\C-a" 'geiser-repl--bol)
518 (define-key geiser-repl-mode-map (kbd "<home>") 'geiser-repl--bol)
520 (geiser-menu--defmenu repl geiser-repl-mode-map
521 ("Complete symbol" ((kbd "M-TAB"))
522 completion-at-point :enable (geiser--symbol-at-point))
523 ("Complete module name" ((kbd "C-.") (kbd "M-`"))
524 geiser-completion--complete-module :enable (geiser--symbol-at-point))
525 ("Edit symbol" "\M-." geiser-edit-symbol-at-point
526 :enable (geiser--symbol-at-point))
528 ("Switch to module..." "\C-c\C-m" switch-to-geiser-module)
529 ("Import module..." "\C-c\C-i" geiser-repl-import-module)
531 ("Previous matching input" "\M-p" comint-previous-matching-input-from-input
532 "Previous input matching current")
533 ("Next matching input" "\M-n" comint-next-matching-input-from-input
534 "Next input matching current")
535 ("Previous input" "\C-c\M-p" comint-previous-input)
536 ("Next input" "\C-c\M-n" comint-next-input)
538 (mode "Autodoc mode" ("\C-c\C-da" "\C-c\C-d\C-a") geiser-autodoc-mode)
539 ("Symbol documentation" ("\C-c\C-dd" "\C-c\C-d\C-d")
540 geiser-doc-symbol-at-point
541 "Documentation for symbol at point" :enable (geiser--symbol-at-point))
542 ("Module documentation" ("\C-c\C-dm" "\C-c\C-d\C-m") geiser-repl--doc-module
543 "Documentation for module at point" :enable (geiser--symbol-at-point))
545 ("Kill Scheme interpreter" "\C-c\C-q" geiser-repl-exit
546 :enable (geiser-repl--live-p))
547 ("Restart" "\C-c\C-z" switch-to-geiser :enable (not (geiser-repl--live-p)))
549 (custom "REPL options" geiser-repl))
551 (define-key geiser-repl-mode-map [menu-bar completion] 'undefined)
556 (defun run-geiser (impl)
557 "Start a new Geiser REPL."
559 (list (geiser-repl--get-impl "Start Geiser for scheme implementation: ")))
560 (geiser-repl--start-repl impl nil))
562 (defalias 'geiser 'run-geiser)
564 (defun geiser-connect (impl &optional host port)
565 "Start a new Geiser REPL connected to a remote Scheme process."
567 (list (geiser-repl--get-impl "Connect to Scheme implementation: ")))
568 (geiser-repl--start-repl impl
569 (geiser-repl--read-address host port)))
571 (make-variable-buffer-local
572 (defvar geiser-repl--last-scm-buffer nil))
574 (defun switch-to-geiser (&optional ask impl buffer)
575 "Switch to running Geiser REPL.
576 With prefix argument, ask for which one if more than one is running.
577 If no REPL is running, execute `run-geiser' to start a fresh one."
579 (let* ((impl (or impl geiser-impl--implementation))
580 (in-repl (eq major-mode 'geiser-repl-mode))
581 (in-live-repl (and in-repl (get-buffer-process (current-buffer))))
582 (repl (cond ((and (not ask)
585 (or geiser-repl--repl (car geiser-repl--repls))))
589 (geiser-repl--repl/impl impl)))))
590 (pop-up-windows geiser-repl-window-allow-split))
591 (cond ((or in-live-repl
592 (and (eq (current-buffer) repl) (not (eq repl buffer))))
593 (when (buffer-live-p geiser-repl--last-scm-buffer)
594 (pop-to-buffer geiser-repl--last-scm-buffer)))
595 (repl (pop-to-buffer repl))
596 ((geiser-repl--remote-p) (geiser-connect impl))
597 (t (run-geiser impl)))
598 (when (and buffer (eq major-mode 'geiser-repl-mode))
599 (setq geiser-repl--last-scm-buffer buffer))))
601 (defun switch-to-geiser-module (&optional module buffer)
602 "Switch to running Geiser REPL and try to enter a given module."
604 (let* ((module (or module
605 (geiser-completion--read-module
606 "Switch to module (default top-level): ")))
608 (geiser-repl--enter-cmd geiser-impl--implementation
610 (unless (eq major-mode 'geiser-repl-mode)
611 (switch-to-geiser nil nil (or buffer (current-buffer))))
612 (geiser-repl--send cmd)))
614 (defun geiser-repl-import-module (&optional module)
615 "Import a given module in the current namespace of the REPL."
617 (let* ((module (or module
618 (geiser-completion--read-module "Import module: ")))
620 (geiser-repl--import-cmd geiser-impl--implementation
622 (switch-to-geiser nil nil (current-buffer))
623 (geiser-repl--send cmd)))
625 (defun geiser-repl-exit (&optional arg)
626 "Exit the current REPL.
627 With a prefix argument, force exit by killing the scheme process."
629 (when (or (not geiser-repl-query-on-exit-p)
630 (y-or-n-p "Really quit this REPL? "))
631 (geiser-con--connection-deactivate geiser-repl--connection t)
632 (let ((cmd (and (not arg)
633 (geiser-repl--exit-cmd geiser-impl--implementation))))
635 (when (stringp cmd) (geiser-repl--send cmd))
636 (comint-kill-subjob)))))
641 (defun geiser-repl--repl-list ()
643 (dolist (repl geiser-repl--repls lst)
644 (when (buffer-live-p repl)
645 (with-current-buffer repl
646 (push (cons geiser-impl--implementation
647 geiser-repl--address)
650 (defun geiser-repl--restore (impls)
654 (geiser-repl--start-repl (car impl) (cdr impl))
655 (error (message (error-message-string err)))))))
657 (defun geiser-repl-unload-function ()
658 (dolist (repl geiser-repl--repls)
659 (when (buffer-live-p repl)
660 (with-current-buffer repl
661 (let ((geiser-repl-query-on-exit-p nil)) (geiser-repl-exit))
666 (provide 'geiser-repl)