1 ;;; geiser-repl.el --- Geiser's REPL
3 ;; Copyright (C) 2009, 2010, 2011, 2012, 2013, 2015, 2016, 2018, 2019, 2020 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>.
13 (require 'geiser-company
)
15 (require 'geiser-autodoc
)
16 (require 'geiser-edit
)
17 (require 'geiser-completion
)
18 (require 'geiser-syntax
)
19 (require 'geiser-impl
)
20 (require 'geiser-eval
)
21 (require 'geiser-connection
)
22 (require 'geiser-menu
)
23 (require 'geiser-image
)
24 (require 'geiser-custom
)
25 (require 'geiser-base
)
35 (defgroup geiser-repl nil
36 "Interacting with the Geiser REPL."
39 (geiser-custom--defcustom geiser-repl-buffer-name-function
40 'geiser-repl-buffer-name
41 "Function used to define the name of a REPL buffer.
42 The function is called with a single argument - an implementation
43 symbol (e.g., `guile', `chicken', etc.)."
44 :type
'(choice (function-item geiser-repl-buffer-name
)
45 (function :tag
"Other function"))
48 (geiser-custom--defcustom geiser-repl-current-project-function
50 "Function used to determine the current project.
51 The function is called from both source and REPL buffers, and
52 should return a value which uniquely identifies the project."
53 :type
'(choice (function-item :tag
"Ignore projects" ignore
)
54 (function-item :tag
"Use Project.el" project-current
)
55 (function-item :tag
"Use Projectile" projectile-project-root
)
56 (function :tag
"Other function"))
59 (geiser-custom--defcustom geiser-repl-use-other-window t
60 "Whether to Use a window other than the current buffer's when
61 switching to the Geiser REPL buffer."
65 (geiser-custom--defcustom geiser-repl-window-allow-split t
66 "Whether to allow window splitting when switching to the Geiser
71 (geiser-custom--defcustom geiser-repl-history-filename
72 (expand-file-name "~/.geiser_history")
73 "File where REPL input history is saved, so that it persists between sessions.
75 This is actually the base name: the concrete Scheme
76 implementation name gets appended to it."
80 (geiser-custom--defcustom geiser-repl-history-size comint-input-ring-size
81 "Maximum size of the saved REPL input history."
85 (geiser-custom--defcustom geiser-repl-history-no-dups-p t
86 "Whether to skip duplicates when recording history."
90 (geiser-custom--defcustom geiser-repl-save-debugging-history-p nil
91 "Whether to skip debugging input in REPL history.
93 By default, REPL interactions while scheme is in the debugger are
94 not added to the REPL command history. Set this variable to t to
99 (geiser-custom--defcustom geiser-repl-autodoc-p t
100 "Whether to enable `geiser-autodoc-mode' in the REPL by default."
104 (geiser-custom--defcustom geiser-repl-company-p t
105 "Whether to use company-mode for completion, if available."
109 (geiser-custom--defcustom geiser-repl-read-only-prompt-p t
110 "Whether the REPL's prompt should be read-only."
114 (geiser-custom--defcustom geiser-repl-read-only-output-p t
115 "Whether the REPL's output should be read-only."
119 (geiser-custom--defcustom geiser-repl-highlight-output-p nil
120 "Whether to syntax highlight REPL output."
124 (geiser-custom--defcustom geiser-repl-auto-indent-p t
125 "Whether newlines for incomplete sexps are autoindented."
129 (geiser-custom--defcustom geiser-repl-send-on-return-p t
130 "Sends input to REPL when ENTER is pressed in a balanced S-expression,
131 regardless of cursor positioning.
133 When off, pressing ENTER inside a balance S-expression will
134 introduce a new line without sending input to the inferior
135 Scheme process. This option is useful when using minor modes
136 which might do parentheses balancing, or when entering additional
137 arguments inside an existing expression.
139 When on (the default), pressing ENTER inside a balanced S-expression
140 will send the input to the inferior Scheme process regardless of the
145 (geiser-custom--defcustom geiser-repl-forget-old-errors-p t
146 "Whether to forget old errors upon entering a new expression.
148 When on (the default), every time a new expression is entered in
149 the REPL old error messages are flushed, and using \\[next-error]
150 afterwards will jump only to error locations produced by the new
155 (geiser-custom--defcustom geiser-repl-skip-version-check-p nil
156 "Whether to skip version checks for the Scheme executable.
158 When set, Geiser won't check the version of the Scheme
159 interpreter when starting a REPL, saving a few tenths of a
165 (geiser-custom--defcustom geiser-repl-query-on-exit-p nil
166 "Whether to prompt for confirmation on \\[geiser-repl-exit]."
170 (geiser-custom--defcustom geiser-repl-delete-last-output-on-exit-p nil
171 "Whether to delete partial outputs when the REPL's process exits."
175 (geiser-custom--defcustom geiser-repl-query-on-kill-p t
176 "Whether to prompt for confirmation when killing a REPL buffer with
181 (geiser-custom--defcustom geiser-repl-default-host
"localhost"
182 "Default host when connecting to remote REPLs."
186 (geiser-custom--defcustom geiser-repl-default-port
37146
187 "Default port for connecting to remote REPLs."
191 (geiser-custom--defcustom geiser-repl-startup-time
10000
192 "Time, in milliseconds, to wait for Racket to startup.
193 If you have a slow system, try to increase this time."
197 (geiser-custom--defcustom geiser-repl-inline-images-p t
198 "Whether to display inline images in the REPL."
202 (geiser-custom--defcustom geiser-repl-auto-display-images-p t
203 "Whether to automatically invoke the external viewer to display
204 images popping up in the REPL.
206 See also `geiser-debug-auto-display-images-p'."
210 (geiser-custom--defface repl-input
211 'comint-highlight-input geiser-repl
"evaluated input highlighting")
213 (geiser-custom--defface repl-output
214 'font-lock-string-face geiser-repl
"REPL output")
216 (geiser-custom--defface repl-prompt
217 'comint-highlight-prompt geiser-repl
"REPL prompt")
221 ;;; Implementation-dependent parameters
223 (geiser-impl--define-caller geiser-repl--binary binary
()
224 "A variable or function returning the path to the scheme binary
225 for this implementation.")
227 (geiser-impl--define-caller geiser-repl--arglist arglist
()
228 "A function taking no arguments and returning a list of
229 arguments to be used when invoking the scheme binary.")
231 (geiser-impl--define-caller geiser-repl--prompt-regexp prompt-regexp
()
232 "A variable (or thunk returning a value) giving the regular
233 expression for this implementation's geiser scheme prompt.")
235 (geiser-impl--define-caller
236 geiser-repl--debugger-prompt-regexp debugger-prompt-regexp
()
237 "A variable (or thunk returning a value) giving the regular
238 expression for this implementation's debugging prompt.")
240 (geiser-impl--define-caller geiser-repl--startup repl-startup
(remote)
241 "Function taking no parameters that is called after the REPL
242 has been initialised. All Geiser functionality is available to
245 (geiser-impl--define-caller geiser-repl--enter-cmd enter-command
(module)
246 "Function taking a module designator and returning a REPL enter
247 module command as a string")
249 (geiser-impl--define-caller geiser-repl--import-cmd import-command
(module)
250 "Function taking a module designator and returning a REPL import
251 module command as a string")
253 (geiser-impl--define-caller geiser-repl--exit-cmd exit-command
()
254 "Function returning the REPL exit command as a string")
256 (geiser-impl--define-caller geiser-repl--version version-command
(binary)
257 "Function returning the version of the corresponding scheme process,
258 given its full path.")
260 (geiser-impl--define-caller geiser-repl--min-version minimum-version
()
261 "A variable providing the minimum required scheme version, as a string.")
264 ;;; Geiser REPL buffers and processes:
266 (defvar geiser-repl--repls nil
)
267 (defvar geiser-repl--closed-repls nil
)
269 (defvar geiser-repl--last-output-start nil
)
270 (defvar geiser-repl--last-output-end nil
)
272 (make-variable-buffer-local
273 (defvar geiser-repl--repl nil
))
275 (make-variable-buffer-local
276 (defvar geiser-repl--project nil
))
278 (defsubst geiser-repl--set-this-buffer-repl
(r)
279 (setq geiser-repl--repl r
))
281 (defsubst geiser-repl--set-this-buffer-project
(p)
282 (setq geiser-repl--project p
))
284 (defsubst geiser-repl--current-project
()
285 (or (funcall geiser-repl-current-project-function
)
288 (defun geiser-repl--live-p ()
289 (and geiser-repl--repl
290 (get-buffer-process geiser-repl--repl
)))
292 (defun geiser-repl--repl/impl
(impl &optional proj repls
)
295 (geiser-repl--current-project)))
297 geiser-repl--repls
)))
300 (when (buffer-live-p repl
)
301 (with-current-buffer repl
302 (when (and (eq geiser-impl--implementation impl
)
303 (equal geiser-repl--project proj
))
304 (throw 'repl repl
))))))))
306 (defun geiser-repl--set-up-repl (impl)
307 (or (and (not impl
) geiser-repl--repl
)
308 (setq geiser-repl--repl
310 geiser-impl--implementation
311 (geiser-impl--guess))))
312 (when impl
(geiser-repl--repl/impl impl
))))))
314 (defun geiser-repl--active-impls ()
316 (dolist (repl geiser-repl--repls act
)
317 (with-current-buffer repl
318 (add-to-list 'act geiser-impl--implementation
)))))
320 (defsubst geiser-repl--repl-name
(impl)
321 (format "%s REPL" (geiser-impl--impl-str impl
)))
323 (defsubst geiser-repl--buffer-name
(impl)
324 (funcall geiser-repl-buffer-name-function impl
))
326 (defun geiser-repl-buffer-name (impl)
327 "Return default name of the REPL buffer for implementation IMPL."
328 (format "* %s *" (geiser-repl--repl-name impl
)))
330 (defun geiser-repl--switch-to-buffer (buffer)
331 (unless (eq buffer
(current-buffer))
332 (let ((pop-up-windows geiser-repl-window-allow-split
))
333 (if geiser-repl-use-other-window
334 (switch-to-buffer-other-window buffer
)
335 (switch-to-buffer buffer
)))))
337 (defun geiser-repl--to-repl-buffer (impl)
338 (unless (and (eq major-mode
'geiser-repl-mode
)
339 (eq geiser-impl--implementation impl
)
340 (not (get-buffer-process (current-buffer))))
341 (let* ((proj (geiser-repl--current-project))
342 (old (geiser-repl--repl/impl impl proj geiser-repl--closed-repls
))
343 (old (and (buffer-live-p old
)
344 (not (get-buffer-process old
))
346 (geiser-repl--switch-to-buffer
347 (or old
(generate-new-buffer (geiser-repl--buffer-name impl
))))
350 (geiser-impl--set-buffer-implementation impl
)
351 (geiser-repl--set-this-buffer-project proj
)
352 (geiser-syntax--add-kws t
)))))
354 (defun geiser-repl--read-impl (prompt &optional active
)
355 (geiser-impl--read-impl prompt
(and active
(geiser-repl--active-impls))))
357 (defsubst geiser-repl--only-impl-p
()
358 (and (null (cdr geiser-active-implementations
))
359 (car geiser-active-implementations
)))
361 (defun geiser-repl--get-impl (prompt)
362 (or (geiser-repl--only-impl-p)
363 (and (eq major-mode
'geiser-repl-mode
) geiser-impl--implementation
)
364 (geiser-repl--read-impl prompt
)))
369 (defun geiser-repl--last-prompt-end ()
370 (cond ((and (boundp 'comint-last-prompt
) (markerp (cdr comint-last-prompt
)))
371 (marker-position (cdr comint-last-prompt
)))
372 ((and (boundp 'comint-last-prompt-overlay
) comint-last-prompt-overlay
)
373 (overlay-end comint-last-prompt-overlay
))
376 (min (+ 1 (point)) (point-max))))))
378 (defun geiser-repl--last-prompt-start ()
379 (cond ((and (boundp 'comint-last-prompt
) (markerp (car comint-last-prompt
)))
380 (marker-position (car comint-last-prompt
)))
381 ((and (boundp 'comint-last-prompt-overlay
) comint-last-prompt-overlay
)
382 (overlay-start comint-last-prompt-overlay
))
383 (t (save-excursion (geiser-repl--bol) (point)))))
388 (make-variable-buffer-local
389 (defvar geiser-repl--address nil
))
391 (make-variable-buffer-local
392 (defvar geiser-repl--connection nil
))
394 (defun geiser-repl--local-p ()
395 "Return non-nil, if current REPL is local (connected to socket)."
396 (stringp geiser-repl--address
))
398 (defun geiser-repl--remote-p ()
399 "Return non-nil, if current REPL is remote (connected to host:port)."
400 (consp geiser-repl--address
))
402 (defsubst geiser-repl--host
() (car geiser-repl--address
))
403 (defsubst geiser-repl--port
() (cdr geiser-repl--address
))
405 (defun geiser-repl--read-address (&optional host port
)
406 (let ((defhost (or (geiser-repl--host) geiser-repl-default-host
))
407 (defport (or (geiser-repl--port) geiser-repl-default-port
)))
409 (read-string (format "Host (default %s): " defhost
)
411 (or port
(read-number "Port: " defport
)))))
413 (defun geiser-repl--autodoc-mode (n)
414 (when (or geiser-repl-autodoc-p
(< n
0))
415 (geiser--save-msg (geiser-autodoc-mode n
))))
417 (defun geiser-repl--save-remote-data (address)
418 (setq geiser-repl--address address
)
419 (cond ((consp address
)
420 (setq header-line-format
421 (format "Host: %s Port: %s"
423 (geiser-repl--port))))
425 (setq header-line-format
426 (format "Socket: %s" address
)))))
428 (defun geiser-repl--fontify-output-region (beg end
)
429 "Apply highlighting to a REPL output region."
430 (remove-text-properties beg end
'(font-lock-face nil face nil
))
431 (if geiser-repl-highlight-output-p
432 (geiser-syntax--fontify-syntax-region beg end
)
433 (geiser-repl--fontify-plaintext beg end
)))
435 (defun geiser-repl--fontify-plaintext (start end
)
436 "Fontify REPL output plainly."
439 '(font-lock-fontified t
441 font-lock-multiline t
442 font-lock-face geiser-font-lock-repl-output
)))
444 (defun geiser-repl--narrow-to-prompt ()
445 "Narrow to active prompt region and return t, otherwise returns nil."
446 (let* ((proc (get-buffer-process (current-buffer)))
447 (pmark (and proc
(process-mark proc
)))
448 (intxt (when (>= (point) (marker-position pmark
))
450 (if comint-eol-on-send
451 (if comint-use-prompt-regexp
453 (goto-char (field-end))))
454 (buffer-substring pmark
(point)))))
455 (prompt-beg (marker-position pmark
))
456 (prompt-end (+ prompt-beg
(length intxt
))))
457 (when (> (length intxt
) 0)
458 (narrow-to-region prompt-beg prompt-end
)
461 (defun geiser-repl--wrap-fontify-region-function (beg end
&optional loudly
)
463 (when (geiser-repl--narrow-to-prompt)
464 (let ((font-lock-dont-widen t
))
465 (font-lock-default-fontify-region (point-min) (point-max) nil
)))))
467 (defun geiser-repl--wrap-unfontify-region-function (beg end
&optional loudly
)
469 (when (geiser-repl--narrow-to-prompt)
470 (let ((font-lock-dont-widen t
))
471 (font-lock-default-unfontify-region (point-min) (point-max))))))
473 (defun geiser-repl--output-filter (txt)
474 (let ((mark-output nil
))
476 (goto-char (point-max))
477 (re-search-backward comint-prompt-regexp
)
478 ;; move to start of line to prevent accidentally marking a REPL prompt
480 ;; Only mark output which:
481 ;; a) is not on the REPL output line
482 ;; b) has at least one character
484 ;; This makes the magic number for distance 3 -- as the newline
485 ;; after executing expression is also counted. This is due to the point
486 ;; being set before comint-send-input.
488 ;; Restriction a) applies due to our inability to distinguish between
489 ;; output from the REPL, and the REPL prompt output.
490 (let ((distance (- (point) geiser-repl--last-output-start
)))
493 (set-marker geiser-repl--last-output-end
(point)))))
495 (with-silent-modifications
496 (add-text-properties (1+ geiser-repl--last-output-start
)
497 geiser-repl--last-output-end
498 `(read-only ,geiser-repl-read-only-output-p
))
499 (geiser-repl--fontify-output-region geiser-repl--last-output-start
500 geiser-repl--last-output-end
)
501 (geiser--font-lock-ensure geiser-repl--last-output-start
502 geiser-repl--last-output-end
))))
504 (geiser-con--connection-update-debugging geiser-repl--connection txt
)
505 (geiser-image--replace-images geiser-repl-inline-images-p
506 geiser-repl-auto-display-images-p
)
507 (when (string-match-p (geiser-con--connection-prompt geiser-repl--connection
)
509 (geiser-autodoc--disinhibit-autodoc)))
511 (defun geiser-repl--check-version (impl)
512 (when (not geiser-repl-skip-version-check-p
)
513 (let ((v (geiser-repl--version impl
(geiser-repl--binary impl
)))
514 (r (geiser-repl--min-version impl
)))
515 (when (and v r
(geiser--version< v r
))
516 (error "Geiser requires %s version %s but detected %s" impl r v
)))))
518 (defvar geiser-repl--last-scm-buffer
)
520 (defun geiser-repl--start-repl (impl address
)
521 (message "Starting Geiser REPL ...")
522 (when (not address
) (geiser-repl--check-version impl
))
523 (let ((buffer (current-buffer))
524 (binary (geiser-repl--binary impl
))
525 (arglist (geiser-repl--arglist impl
)))
526 (geiser-repl--to-repl-buffer impl
)
527 (setq geiser-repl--last-scm-buffer buffer
528 geiser-repl--binary binary
529 geiser-repl--arglist arglist
))
531 (goto-char (point-max))
532 (geiser-repl--autodoc-mode -
1)
533 (let* ((prompt-rx (geiser-repl--prompt-regexp impl
))
534 (deb-prompt-rx (geiser-repl--debugger-prompt-regexp impl
))
535 (prompt (geiser-con--combined-prompt prompt-rx deb-prompt-rx
)))
537 (error "Sorry, I don't know how to start a REPL for %s" impl
))
538 (geiser-repl--save-remote-data address
)
539 (geiser-repl--start-scheme impl address prompt
)
540 (geiser-repl--quit-setup)
541 (geiser-repl--history-setup)
542 (add-to-list 'geiser-repl--repls
(current-buffer))
543 (geiser-repl--set-this-buffer-repl (current-buffer))
544 (setq geiser-repl--connection
545 (geiser-con--make-connection (get-buffer-process (current-buffer))
548 (geiser-repl--startup impl address
)
549 (geiser-repl--autodoc-mode 1)
550 (geiser-company--setup geiser-repl-company-p
)
551 (add-hook 'comint-output-filter-functions
552 'geiser-repl--output-filter
555 (set-process-query-on-exit-flag (get-buffer-process (current-buffer))
556 geiser-repl-query-on-kill-p
)
557 (message "%s up and running!" (geiser-repl--repl-name impl
))))
559 (defun geiser-repl--start-scheme (impl address prompt
)
560 (setq comint-prompt-regexp prompt
)
561 (let* ((name (geiser-repl--repl-name impl
))
562 (buff (current-buffer))
563 (args (cond ((consp address
) (list address
))
564 ((stringp address
) '(()))
565 (t `(,(geiser-repl--get-binary impl
)
567 ,@(geiser-repl--get-arglist impl
))))))
569 (if (and address
(stringp address
))
570 ;; Connect over a Unix-domain socket.
571 (let ((proc (make-network-process :name
(buffer-name buff
)
575 ;; brittleness warning: this is stuff
576 ;; make-comint-in-buffer sets up, via comint-exec, when
577 ;; it creates its own process, something we're doing
578 ;; here by ourselves.
579 (set-process-filter proc
'comint-output-filter
)
580 (goto-char (point-max))
581 (set-marker (process-mark proc
) (point)))
582 (apply 'make-comint-in-buffer
`(,name
,buff
,@args
)))
583 (error (insert "Unable to start REPL:\n"
584 (error-message-string err
)
586 (error "Couldn't start Geiser: %s" err
)))
587 (geiser-repl--wait-for-prompt geiser-repl-startup-time
)))
589 (defun geiser-repl--wait-for-prompt (timeout)
590 (let ((p (point)) (seen) (buffer (current-buffer)))
591 (while (and (not seen
)
593 (get-buffer-process buffer
))
595 (setq timeout
(- timeout
100))
597 (setq seen
(re-search-forward comint-prompt-regexp nil t
)))
598 (goto-char (point-max))
599 (unless seen
(error "%s" "No prompt found!"))))
601 (defun geiser-repl--is-debugging ()
602 (let ((dp (geiser-con--connection-debug-prompt geiser-repl--connection
)))
605 (goto-char (geiser-repl--last-prompt-start))
606 (re-search-forward dp
(geiser-repl--last-prompt-end) t
)))))
608 (defun geiser-repl--connection* ()
609 (let ((buffer (geiser-repl--set-up-repl geiser-impl--implementation
)))
610 (and (buffer-live-p buffer
)
611 (get-buffer-process buffer
)
612 (with-current-buffer buffer geiser-repl--connection
))))
614 (defun geiser-repl--connection ()
615 (or (geiser-repl--connection*)
616 (error "No Geiser REPL for this buffer (try M-x run-geiser)")))
618 (setq geiser-eval--default-connection-function
'geiser-repl--connection
)
620 (defun geiser-repl--prepare-send ()
621 (geiser-image--clean-cache)
622 (geiser-autodoc--inhibit-autodoc)
623 (geiser-con--connection-deactivate geiser-repl--connection
))
625 (defun geiser-repl--send (cmd &optional save-history
)
626 "Send CMD input string to the current REPL buffer.
627 If SAVE-HISTORY is non-nil, save CMD in the REPL history."
628 (when (and cmd
(eq major-mode
'geiser-repl-mode
))
629 (geiser-repl--prepare-send)
630 (goto-char (point-max))
633 (let ((comint-input-filter (if save-history
636 (comint-send-input nil t
))))
638 (defun geiser-repl-interrupt ()
640 (when (get-buffer-process (current-buffer))
641 (interrupt-process nil comint-ptyp
)))
646 (defconst geiser-repl--history-separator
"\n}{\n")
648 (defsubst geiser-repl--history-file
()
649 (format "%s.%s" geiser-repl-history-filename geiser-impl--implementation
))
651 (defun geiser-repl--read-input-ring ()
652 (let ((comint-input-ring-file-name (geiser-repl--history-file))
653 (comint-input-ring-separator geiser-repl--history-separator
)
654 (buffer-file-coding-system 'utf-8
))
655 (comint-read-input-ring t
)))
657 (defun geiser-repl--write-input-ring ()
658 (let ((comint-input-ring-file-name (geiser-repl--history-file))
659 (comint-input-ring-separator geiser-repl--history-separator
)
660 (buffer-file-coding-system 'utf-8
))
661 (comint-write-input-ring)))
663 (defun geiser-repl--history-setup ()
664 (set (make-local-variable 'comint-input-ring-size
) geiser-repl-history-size
)
665 (set (make-local-variable 'comint-input-filter
) 'geiser-repl--input-filter
)
666 (geiser-repl--read-input-ring))
671 (defun geiser-repl--on-quit ()
672 (geiser-repl--write-input-ring)
673 (let ((cb (current-buffer))
674 (impl geiser-impl--implementation
)
675 (comint-prompt-read-only nil
))
676 (geiser-con--connection-deactivate geiser-repl--connection t
)
677 (geiser-con--connection-close geiser-repl--connection
)
678 (setq geiser-repl--repls
(remove cb geiser-repl--repls
))
679 (dolist (buffer (buffer-list))
680 (when (buffer-live-p buffer
)
681 (with-current-buffer buffer
682 (when (and (eq geiser-impl--implementation impl
)
683 (equal cb geiser-repl--repl
))
684 (geiser-repl--set-up-repl geiser-impl--implementation
)))))))
686 (defun geiser-repl--sentinel (proc event
)
687 (let ((pb (process-buffer proc
)))
688 (when (buffer-live-p pb
)
689 (with-current-buffer pb
690 (let ((comint-prompt-read-only nil
)
691 (comint-input-ring-file-name (geiser-repl--history-file))
692 (comint-input-ring-separator geiser-repl--history-separator
))
693 (geiser-repl--on-quit)
694 (push pb geiser-repl--closed-repls
)
695 (goto-char (point-max))
696 (when geiser-repl-delete-last-output-on-exit-p
697 (comint-kill-region comint-last-input-start
(point)))
698 (insert "\nIt's been nice interacting with you!\n")
699 (insert "Press C-c C-z to bring me back.\n"))))))
701 (defun geiser-repl--on-kill ()
702 (geiser-repl--on-quit)
703 (setq geiser-repl--closed-repls
704 (remove (current-buffer) geiser-repl--closed-repls
)))
706 (defun geiser-repl--input-filter (str)
707 (not (or (and (not geiser-repl-save-debugging-history-p
)
708 (geiser-repl--is-debugging))
709 (string-match "^\\s *$" str
)
710 (string-match "^,quit *$" str
))))
712 (defun geiser-repl--old-input ()
716 (buffer-substring (point) end
))))
718 (defun geiser-repl--quit-setup ()
719 (add-hook 'kill-buffer-hook
'geiser-repl--on-kill nil t
)
720 (set-process-sentinel (get-buffer-process (current-buffer))
721 'geiser-repl--sentinel
))
724 ;;; geiser-repl mode:
726 (defun geiser-repl--bol ()
728 (when (= (point) (comint-bol)) (beginning-of-line)))
730 (defun geiser-repl--beginning-of-defun ()
732 (narrow-to-region (geiser-repl--last-prompt-end) (point))
733 (let ((beginning-of-defun-function nil
))
734 (beginning-of-defun))))
736 (defun geiser-repl--module-function (&optional module
)
737 (if (and module geiser-eval--get-impl-module
)
738 (funcall geiser-eval--get-impl-module module
)
741 (defun geiser-repl--doc-module ()
743 (let ((geiser-eval--get-module-function
744 (geiser-impl--method 'find-module geiser-impl--implementation
)))
745 (geiser-doc-module)))
747 (defun geiser-repl--newline-and-indent ()
750 (narrow-to-region comint-last-input-start
(point-max))
754 (defun geiser-repl--nesting-level ()
756 (narrow-to-region (geiser-repl--last-prompt-end) (point-max))
757 (geiser-syntax--nesting-level)))
759 (defun geiser-repl--is-input ()
760 (not (eq (field-at-pos (point)) 'output
)))
762 (defun geiser-repl--grab-input ()
763 (let ((pos (comint-bol)))
764 (goto-char (point-max))
765 (insert (field-string-no-properties pos
))))
767 (defun geiser-repl--send-input ()
768 (set-marker geiser-repl--last-output-start
(point-max))
770 (let* ((proc (get-buffer-process (current-buffer)))
771 (pmark (and proc
(process-mark proc
)))
772 (intxt (and pmark
(buffer-substring pmark
(point))))
775 (and geiser-repl-forget-old-errors-p
776 (not (geiser-repl--is-debugging))
777 (compilation-forget-errors))
778 (geiser-repl--prepare-send)
780 (when (string-match "^\\s-*$" intxt
)
781 (comint-send-string proc
(geiser-eval--scheme-str '(:ge no-values
)))
782 (comint-send-string proc
"\n")))))
784 (defun geiser-repl--maybe-send ()
787 (cond ((< p
(geiser-repl--last-prompt-start))
788 (if (geiser-repl--is-input)
789 (geiser-repl--grab-input)
790 (ignore-errors (compile-goto-error))))
791 ((let ((inhibit-field-text-motion t
))
792 (when geiser-repl-send-on-return-p
794 (<= (geiser-repl--nesting-level) 0))
795 (geiser-repl--send-input))
797 (if geiser-repl-auto-indent-p
798 (geiser-repl--newline-and-indent)
801 (defun geiser-repl-tab-dwim (n)
802 "If we're after the last prompt, complete symbol or indent (if
803 there's no symbol at point). Otherwise, go to next error in the REPL
806 (if (>= (point) (geiser-repl--last-prompt-end))
807 (or (completion-at-point)
809 (compilation-next-error n
)))
811 (defun geiser-repl--previous-error (n)
812 "Go to previous error in the REPL buffer."
814 (compilation-next-error (- n
)))
816 (defun geiser-repl-clear-buffer ()
817 "Delete the output generated by the scheme process."
819 (let ((inhibit-read-only t
))
820 (delete-region (point-min) (geiser-repl--last-prompt-start))
821 (when (< (point) (geiser-repl--last-prompt-end))
822 (goto-char (geiser-repl--last-prompt-end)))
825 (defvar geiser-repl-mode-map
826 (let ((map (make-sparse-keymap)))
827 (set-keymap-parent map comint-mode-map
)
829 (define-key map
"\C-d" 'delete-char
)
830 (define-key map
"\C-m" 'geiser-repl--maybe-send
)
831 (define-key map
[return] 'geiser-repl--maybe-send)
832 (define-key map "\C-j" 'geiser-repl--newline-and-indent)
833 (define-key map (kbd "TAB") 'geiser-repl-tab-dwim)
834 (define-key map [backtab] 'geiser-repl--previous-error)
836 (define-key map "\C-a" 'geiser-repl--bol)
837 (define-key map (kbd "<home>") 'geiser-repl--bol)
839 (geiser-menu--defmenu repl map
840 ("Complete symbol" ((kbd "M-TAB"))
841 completion-at-point :enable (geiser--symbol-at-point))
842 ("Complete module name" ((kbd "C-.") (kbd "M-`"))
843 geiser-completion--complete-module :enable (geiser--symbol-at-point))
844 ("Edit symbol" "\M-." geiser-edit-symbol-at-point
845 :enable (geiser--symbol-at-point))
847 ("Load scheme file..." "\C-c\C-l" geiser-load-file)
848 ("Switch to module..." "\C-c\C-m" switch-to-geiser-module)
849 ("Import module..." "\C-c\C-i" geiser-repl-import-module)
850 ("Add to load path..." "\C-c\C-r" geiser-add-to-load-path)
852 ("Previous matching input" "\M-p" comint-previous-matching-input-from-input
853 "Previous input matching current")
854 ("Next matching input" "\M-n" comint-next-matching-input-from-input
855 "Next input matching current")
856 ("Previous prompt" "\C-c\C-p" geiser-repl-previous-prompt)
857 ("Next prompt" "\C-c\C-n" geiser-repl-next-prompt)
858 ("Previous input" "\C-c\M-p" comint-previous-input)
859 ("Next input" "\C-c\M-n" comint-next-input)
861 ("Interrupt evaluation" ("\C-c\C-k" "\C-c\C-c")
862 geiser-repl-interrupt)
864 (mode "Autodoc mode" ("\C-c\C-da" "\C-c\C-d\C-a") geiser-autodoc-mode)
865 ("Symbol documentation" ("\C-c\C-dd" "\C-c\C-d\C-d")
866 geiser-doc-symbol-at-point
867 "Documentation for symbol at point" :enable (geiser--symbol-at-point))
868 ("Lookup symbol in manual" ("\C-c\C-di" "\C-c\C-d\C-i")
869 geiser-doc-look-up-manual
870 "Documentation for symbol at point" :enable (geiser--symbol-at-point))
871 ("Module documentation" ("\C-c\C-dm" "\C-c\C-d\C-m") geiser-repl--doc-module
872 "Documentation for module at point" :enable (geiser--symbol-at-point))
874 ("Clear buffer" "\C-c\M-o" geiser-repl-clear-buffer
875 "Clean up REPL buffer, leaving just a lonely prompt")
876 ("Toggle ()/[]" ("\C-c\C-e\C-[" "\C-c\C-e[") geiser-squarify)
877 ("Insert λ" ("\C-c\\" "\C-c\C-\\") geiser-insert-lambda)
879 ("Kill Scheme interpreter" "\C-c\C-q" geiser-repl-exit
880 :enable (geiser-repl--live-p))
881 ("Restart" "\C-c\C-z" switch-to-geiser :enable (not (geiser-repl--live-p)))
884 (custom "REPL options" geiser-repl))
886 (define-key map [menu-bar completion] 'undefined)
889 (define-derived-mode geiser-repl-mode comint-mode "REPL"
890 "Major mode for interacting with an inferior scheme repl process.
891 \\{geiser-repl-mode-map}"
892 (scheme-mode-variables)
893 (set (make-local-variable 'geiser-repl--last-output-start) (point-marker))
894 (set (make-local-variable 'geiser-repl--last-output-end) (point-marker))
895 (set (make-local-variable 'face-remapping-alist)
896 '((comint-highlight-prompt geiser-font-lock-repl-prompt)
897 (comint-highlight-input geiser-font-lock-repl-input)))
898 (set (make-local-variable 'mode-line-process) nil)
899 (set (make-local-variable 'comint-use-prompt-regexp) nil)
900 (set (make-local-variable 'comint-prompt-read-only)
901 geiser-repl-read-only-prompt-p)
902 (setq comint-process-echoes nil)
903 (set (make-local-variable 'beginning-of-defun-function)
904 'geiser-repl--beginning-of-defun)
905 (set (make-local-variable 'comint-input-ignoredups)
906 geiser-repl-history-no-dups-p)
907 (setq geiser-eval--get-module-function 'geiser-repl--module-function)
908 (geiser-completion--setup t)
909 (setq geiser-smart-tab-mode-string "")
910 (geiser-smart-tab-mode t)
912 (setq-local font-lock-fontify-region-function
913 #'geiser-repl--wrap-fontify-region-function)
914 (setq-local font-lock-unfontify-region-function
915 #'geiser-repl--wrap-unfontify-region-function)
917 ;; enabling compilation-shell-minor-mode without the annoying highlighter
918 (compilation-setup t))
923 (defun run-geiser (impl)
924 "Start a new Geiser REPL."
926 (list (geiser-repl--get-impl "Start Geiser for scheme implementation: ")))
927 (geiser-repl--start-repl impl nil))
929 (defalias 'geiser 'run-geiser)
931 (defun geiser-connect (impl &optional host port)
932 "Start a new Geiser REPL connected to a remote Scheme process."
934 (list (geiser-repl--get-impl "Connect to Scheme implementation: ")))
935 (geiser-repl--start-repl impl (geiser-repl--read-address host port)))
937 (defun geiser-connect-local (impl socket)
938 "Start a new Geiser REPL connected to a remote Scheme process
939 over a Unix-domain socket."
941 (list (geiser-repl--get-impl "Connect to Scheme implementation: ")
942 (expand-file-name (read-file-name "Socket file name: "))))
943 (geiser-repl--start-repl impl socket))
945 (make-variable-buffer-local
946 (defvar geiser-repl--last-scm-buffer nil))
948 (defun geiser-repl--maybe-remember-scm-buffer (buffer)
950 (eq 'scheme-mode (with-current-buffer buffer major-mode))
951 (eq major-mode 'geiser-repl-mode))
952 (setq geiser-repl--last-scm-buffer buffer)))
954 (make-variable-buffer-local
955 (defvar geiser-repl--binary nil))
957 (make-variable-buffer-local
958 (defvar geiser-repl--arglist nil))
960 (defun geiser-repl--get-binary (impl)
961 (or geiser-repl--binary (geiser-repl--binary impl)))
963 (defun geiser-repl--get-arglist (impl)
964 (or geiser-repl--arglist (geiser-repl--arglist impl)))
966 (defun switch-to-geiser (&optional ask impl buffer)
967 "Switch to running Geiser REPL.
969 If REPL is the current buffer, switch to the previously used
972 With prefix argument, ask for which one if more than one is running.
973 If no REPL is running, execute `run-geiser' to start a fresh one."
975 (let* ((impl (or impl geiser-impl--implementation))
976 (in-repl (eq major-mode 'geiser-repl-mode))
977 (in-live-repl (and in-repl (get-buffer-process (current-buffer))))
980 (geiser-repl--repl/impl impl)
981 (or geiser-repl--repl (car geiser-repl--repls))))))
983 (when (and (not (eq repl buffer))
984 (buffer-live-p geiser-repl--last-scm-buffer))
985 (geiser-repl--switch-to-buffer geiser-repl--last-scm-buffer)))
986 (repl (geiser-repl--switch-to-buffer repl))
987 ((geiser-repl--remote-p)
988 (geiser-connect impl (geiser-repl--host) (geiser-repl--port)))
989 ((geiser-repl--local-p)
990 (geiser-connect-local impl geiser-repl--address))
991 (impl (run-geiser impl))
992 (t (call-interactively 'run-geiser)))
993 (geiser-repl--maybe-remember-scm-buffer buffer)))
995 (defun switch-to-geiser-module (&optional module buffer)
996 "Switch to running Geiser REPL and try to enter a given module."
998 (let* ((module (or module
999 (geiser-completion--read-module
1000 "Switch to module (default top-level): ")))
1002 (geiser-repl--enter-cmd geiser-impl--implementation
1004 (unless (eq major-mode 'geiser-repl-mode)
1005 (switch-to-geiser nil nil (or buffer (current-buffer))))
1006 (geiser-repl--send cmd)))
1008 (defun geiser-repl-import-module (&optional module)
1009 "Import a given module in the current namespace of the REPL."
1011 (let* ((module (or module
1012 (geiser-completion--read-module "Import module: ")))
1014 (geiser-repl--import-cmd geiser-impl--implementation
1016 (switch-to-geiser nil nil (current-buffer))
1017 (geiser-repl--send cmd)))
1019 (defun geiser-repl-exit (&optional arg)
1020 "Exit the current REPL.
1021 With a prefix argument, force exit by killing the scheme process."
1023 (when (or (not geiser-repl-query-on-exit-p)
1024 (y-or-n-p "Really quit this REPL? "))
1025 (geiser-con--connection-deactivate geiser-repl--connection t)
1026 (let ((cmd (and (not arg)
1027 (geiser-repl--exit-cmd geiser-impl--implementation))))
1029 (when (stringp cmd) (geiser-repl--send cmd))
1030 (comint-kill-subjob)))))
1032 (defun geiser-repl-next-prompt (n)
1036 (re-search-forward comint-prompt-regexp nil 'go n)))
1038 (defun geiser-repl-previous-prompt (n)
1042 (when (re-search-backward comint-prompt-regexp nil 'go n)
1043 (goto-char (match-end 0)))))
1048 (defun geiser-repl--repl-list ()
1050 (dolist (repl geiser-repl--repls lst)
1051 (when (buffer-live-p repl)
1052 (with-current-buffer repl
1053 (push (cons geiser-impl--implementation
1054 geiser-repl--address)
1057 (defun geiser-repl--restore (impls)
1058 (dolist (impl impls)
1061 (geiser-repl--start-repl (car impl) (cdr impl))
1062 (error (message (error-message-string err)))))))
1064 (defun geiser-repl-unload-function ()
1065 (dolist (repl geiser-repl--repls)
1066 (when (buffer-live-p repl)
1067 (with-current-buffer repl
1068 (let ((geiser-repl-query-on-exit-p nil)) (geiser-repl-exit))
1073 (provide 'geiser-repl)
1077 ;; After providing 'geiser-repl, so that impls can use us.
1078 (mapc 'geiser-impl--load-impl geiser-active-implementations)