;;; Copyright (C) 2025 Free Software Foundation, Inc. ;;; ;;; This library is free software: you can redistribute it and/or modify ;;; it under the terms of the GNU Lesser General Public License as ;;; published by the Free Software Foundation, either version 3 of the ;;; License, or (at your option) any later version. ;;; ;;; This library is distributed in the hope that it will be useful, but ;;; WITHOUT ANY WARRANTY; without even the implied warranty of ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ;;; Lesser General Public License for more details. ;;; ;;; You should have received a copy of the GNU Lesser General Public ;;; License along with this program. If not, see ;;; . ;;; Commentary: ;;; ;;; ;;; Code: (define-module (system repl reader) #:use-module (ice-9 hooks) #:use-module (system repl hooks) ;; FIXME: #:export instead of #:replace once deprecated code is ;; removed. #:replace (repl-reader) #:export (set-repl-reader!)) ;;; The default repl-reader function. We may override this if we've ;;; the readline library. (define repl-reader (lambda* (prompt #:optional (reader (fluid-ref current-reader))) (unless (char-ready?) (display (if (string? prompt) prompt (prompt))) ;; An interesting situation. The printer resets the column to 0 ;; by printing a newline, but we then advance it by printing the ;; prompt. However the port-column of the output port does not ;; typically correspond with the actual column on the screen, ;; because the input is echoed back! Since the input is ;; line-buffered and thus ends with a newline, the output will ;; really start on column zero. So, here we zero it out. See bug ;; 9664. ;; ;; Note that for similar reasons, the output-line will not reflect ;; the actual line on the screen. But given the possibility of ;; multiline input, the fix is not as straightforward, so we don't ;; bother. ;; ;; Also note that the readline implementation papers over these ;; concerns, because it's readline itself printing the prompt, and ;; not Guile. (set-port-column! (current-output-port) 0)) (force-output) (run-hook before-read-hook) ((or reader read) (current-input-port)))) (define (set-repl-reader! reader) (set! repl-reader reader))