I'm using guile 3 and trying to customize the "print" aspect of the repl

257 Views Asked by At

I understand that the option (repl-default-option-set! 'print ...) is to be used and i've tried so in many variations, i grokked the idea behind customizing the prompt (repl-default-option-set! 'promp ">>>") but can't seem to get the print aspect to work, ideally i would like to get rid of the increasing $num print that happens before the evalutations result

4

There are 4 best solutions below

0
ignis volens On

The answer does indeed seem to be 'write your own REPL', which is extremely depressing.

Given:

(define p (lambda it (display "here") (newline))

Then two things happen:

scheme@(guile-user)> (repl-default-option-set! 'print p)
scheme@(guile-user)> 1
$6 = 1

So this doesn't work because you're setting some default option but the existing REPL has already chosen its printer. If you convince the system to start a new REPL, for instance by getting into a break loop, you get this:

Entering a new prompt.  Type `,bt' for a backtrace or `,q' to continue.
scheme@(guile-user) [1]> 1
$7 = here

So the print function is indeed printing here, but that happens after the annoying $7 = thing (which I know has some purpose).

So, well, it looks like alinsoar's comment is correct, sadly: the REPL is customisable but ... not enough.

0
Stefan On

You actually have an option to modify the printer by overriding write and display GOOPS generalized methods (this is done e.g. in python-on-guile repository https://gitlab.com/python-on-guile/python-on-guile). You can also overwrite the standard printer function entirely if you want total control, here is a simplified example (you need to be able to handle a port argument as well), this example only work for terminals that understand unix terminal escape sequence and is a bit hacky, you can find escape sequences at, https://man7.org/linux/man-pages/man4/console_codes.4.html

Add this to your .guile file

(use-modules (system repl common))

(define old-write (@ (guile) write))

(define (my-write . x)
  ;; reverse line feed
  (display (list->string '(#\Escape #\M)))

  ;; some other text
  (format #t "~%And the result is ...~%")
 
  (apply old-write x))

(repl-default-option-set! 'print
                      (lambda (repl obj)
                        (my-write obj)
                        (newline)))

Then a shell interaction in a unix escape terminal can look like

scheme@(guile-user)> (values 1 2)
And the result is ...
1
And the result is ...
2

See https://www.gnu.org/software/guile-cv/manual/html_node/Configuring-Guile_0027s-repl_002dprint-procedure.html

0
lloda On

You can change the options on the current repl with repl-option-set!. For example (repl-option-set! (car (fluid-ref *repl-stack*)) 'print p) will set the print function on the current repl. repl-option-set! and *repl-stack* are undocumented, but I don't think they're going anywhere. You should still use repl-default-option-set! to configure future repls, such as in ~/.guile.

0
ZarakshR On

,o prompt ">_" to change the prompt while already in a REPL