> "fix-readline-related-bug.patch" does that.

Bloody parentheses.  This is better.

Cheers.
Description: Fix readline-related bug
 Non-printable character (meant to be interpreted by the terminal) in the
 prompt string was not enclosed in RL_PROMPT_START_IGNORE/RL_PROMPT_END_IGNORE
 pairs, resulting in improper behaviour by GNU readline.
 .
 This is a Scheme-only fix, but it's not ideal because it hardcodes
 libreadline constants.  Ideally, "Iedline.scm" would get eval-ed in an
 environment where C code has made the constants (from <readline/readline.h>)
 available.  This way the constants are not hardcoded in the source code and
 the Scheme namespace is not polluted with implementation details.
Author: astian <ast...@elude.in>
Bug-Debian: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=831017
Forwarded: no
Last-Update: 2018-03-05

--- scm-5f2.orig/Iedline.scm
+++ scm-5f2/Iedline.scm
@@ -23,7 +23,17 @@
 ;; lines, i.e. lines unterminated by a newline.
 
 (define (make-edited-line-port)
-  (let ((prompt "")
+  (let  ; The prompt string we receive.
+       ((prompt "")
+	; Hack to make readline overwrite the prompt we manually write.
+	; Notice that #\cr is being wrapped in #\soh and #\stx, respectively
+	; RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE (defined in
+	; <readline/readline.h>), to signal to readline that #\cr is
+	; invisible.
+	(prompt-hack (string #\soh #\cr #\stx))
+	; Concatenation of "prompt-hack" and "prompt", this is the actual
+	; prompt we send to readline.
+	(prompt-real "")
 	(outp (default-output-port))
 	(inp (default-input-port))
 	(strp (call-with-input-string "" identity)))
@@ -31,18 +41,40 @@
      (vector (lambda (c)
 	       (write-char c outp))
 	     (lambda (s)
+	       ; Reverse-engineered assumption: string-write to this port will
+	       ; only be done in the following cases:
+	       ;
+	       ;   - Empty string.
+	       ;
+	       ;   - Printing repl results, in which case the string will
+	       ;     always come linefeed-terminated.
+	       ;
+	       ;   - Prompt printing, which will never be linefeed-terminated.
+	       ;     The prompt string is immediately printed (manually),
+	       ;     later, when GNU readline is called, a carriage return
+	       ;     prepended to the same prompt string will cause it to
+	       ;     write its prompt on top of the one we printed ourselves
+	       ;     (therefore only one instance of the prompt string is
+	       ;     actually visible to the interactive user).  Presumably
+	       ;     this is the result of kluging optional readline support
+	       ;     on top of an existing prompting mechanism.
+	       ;
+	       ; -- astian
 	       (display s outp)
 	       (or (zero? (string-length s))
 		   (eq? #\newline (string-ref s (- (string-length s) 1)))
 		   (begin
-		     (set! prompt (string-append "\r" s))
+		     (or (string=? s prompt)
+			 (begin
+			   (set! prompt s)
+			   (set! prompt-real (string-append prompt-hack prompt))))
 		     (force-output outp))))
 	     (lambda ()
 	       (force-output outp))
 	     (lambda ()
 	       (let tail ((c (read-char strp)))
 		 (if (char? c) c
-		     (let ((str (read-edited-line prompt)))
+		     (let ((str (read-edited-line prompt-real)))
 		       (if (string? str)
 			   (let ((n (string-length str)))
 			     (add-history str)

Reply via email to