unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
* bug#37086: Guile Ice-9 Readline with-readline-completion-function
@ 2019-08-19 17:36 Matthew Henry
  2019-08-20  0:12 ` Matthew Henry
  2019-08-20  3:00 ` Mark H Weaver
  0 siblings, 2 replies; 4+ messages in thread
From: Matthew Henry @ 2019-08-19 17:36 UTC (permalink / raw)
  To: 37086

[-- Attachment #1: Type: text/plain, Size: 1122 bytes --]

Seen in: guile (GNU Guile) 2.2.4

When using the with-readline-completion-function, the passed readline
uses the default (apropos) completion function instead of the one
provided to with-readline-completion-function.

I believe that this is because root/guile-readline/ice-9/readline.scm
has defined with-readline-completion-function as a function instead of
as a macro.  The readline provided in thunk is executed before the
body of with-readline-completion-function executes and overrides
*readline-completion-function*.

As an aside, I think the API would be better if the completion
function could be provided to readline directly.

Attached is a sample program.

Below is sample output of a run of the attached program.  You can see
that it's autocompleting Guile functions and variables (the default
apropos completion) rather than the provided one which should have had
only 3 options with just one starting in "th".

;;;;;;;;;;;;;;;;;;;;;;;;;;;
Prompt:
Display all 1859 possibilities? (y or n)
Prompt: th
the-eof-object   the-scm-module   thread-exited?   thunk?
the-root-module  thread?          throw
Prompt: th

[-- Attachment #2: readline-completion-bug.scm --]
[-- Type: application/octet-stream, Size: 142 bytes --]

(use-modules (ice-9 readline))

(with-readline-completion-function
 (make-completion-function '("one" "two" "three"))
 (readline "Prompt: "))

^ permalink raw reply	[flat|nested] 4+ messages in thread

* bug#37086: Guile Ice-9 Readline with-readline-completion-function
  2019-08-19 17:36 bug#37086: Guile Ice-9 Readline with-readline-completion-function Matthew Henry
@ 2019-08-20  0:12 ` Matthew Henry
  2019-08-20  3:00 ` Mark H Weaver
  1 sibling, 0 replies; 4+ messages in thread
From: Matthew Henry @ 2019-08-20  0:12 UTC (permalink / raw)
  To: 37086

I'm early in my Scheme journey, but here's a suggested fix:

(define-syntax-rule (with-readline-completion-function completer expr ...)
  "With @var{completer} as readline completion function, call @var{expr ...}."
  (let ((old-completer *readline-completion-function*))
    (dynamic-wind
      (lambda ()
    (set! *readline-completion-function* completer))
      (lambda () expr ...)
      (lambda ()
    (set! *readline-completion-function* old-completer)))))

(export with-readline-completion-function)





^ permalink raw reply	[flat|nested] 4+ messages in thread

* bug#37086: Guile Ice-9 Readline with-readline-completion-function
  2019-08-19 17:36 bug#37086: Guile Ice-9 Readline with-readline-completion-function Matthew Henry
  2019-08-20  0:12 ` Matthew Henry
@ 2019-08-20  3:00 ` Mark H Weaver
  2019-08-20 11:35   ` Matthew Henry
  1 sibling, 1 reply; 4+ messages in thread
From: Mark H Weaver @ 2019-08-20  3:00 UTC (permalink / raw)
  To: Matthew Henry; +Cc: 37086

tags 37086 + notabug
close 37086
thanks

Hi Matthew,

Matthew Henry <mcthenry@gmail.com> writes:

> Seen in: guile (GNU Guile) 2.2.4
>
> When using the with-readline-completion-function, the passed readline
> uses the default (apropos) completion function instead of the one
> provided to with-readline-completion-function.
>
> I believe that this is because root/guile-readline/ice-9/readline.scm
> has defined with-readline-completion-function as a function instead of
> as a macro.  The readline provided in thunk is executed before the
> body of with-readline-completion-function executes and overrides
> *readline-completion-function*.
>
> As an aside, I think the API would be better if the completion
> function could be provided to readline directly.
>
> Attached is a sample program.
>
> Below is sample output of a run of the attached program.  You can see
> that it's autocompleting Guile functions and variables (the default
> apropos completion) rather than the provided one which should have had
> only 3 options with just one starting in "th".
>
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;
> Prompt:
> Display all 1859 possibilities? (y or n)
> Prompt: th
> the-eof-object   the-scm-module   thread-exited?   thunk?
> the-root-module  thread?          throw
> Prompt: th
>
> (use-modules (ice-9 readline))
>
> (with-readline-completion-function
>  (make-completion-function '("one" "two" "three"))
>  (readline "Prompt: "))

The problem is that 'with-readline-completion-function' expects a
"thunk" as the second argument.  A "thunk" is a procedure that takes no
arguments.  Typically this means that you wrap the relevant code within
(lambda () ...), like this:

  (use-modules (ice-9 readline))
  
  (with-readline-completion-function
   (make-completion-function '("one" "two" "three"))
   (lambda () (readline "Prompt: ")))

> I'm early in my Scheme journey, but here's a suggested fix:
> 
> (define-syntax-rule (with-readline-completion-function completer expr ...)
>   "With @var{completer} as readline completion function, call @var{expr ...}."
>   (let ((old-completer *readline-completion-function*))
>     (dynamic-wind
>       (lambda ()
>     (set! *readline-completion-function* completer))
>       (lambda () expr ...)
>       (lambda ()
>     (set! *readline-completion-function* old-completer)))))
> 
> (export with-readline-completion-function)

This is fine, but it's a different API.  It's true that you need to use
a macro if you want to avoid wrapping the body within (lambda () ...),
but just like 'dynamic-wind' itself, 'with-readline-completion-function'
was designed to be an ordinary procedure that accepts the body
expression(s) as a THUNK.

   Happy hacking!
        Mark





^ permalink raw reply	[flat|nested] 4+ messages in thread

* bug#37086: Guile Ice-9 Readline with-readline-completion-function
  2019-08-20  3:00 ` Mark H Weaver
@ 2019-08-20 11:35   ` Matthew Henry
  0 siblings, 0 replies; 4+ messages in thread
From: Matthew Henry @ 2019-08-20 11:35 UTC (permalink / raw)
  To: Mark H Weaver; +Cc: 37086

[-- Attachment #1: Type: text/plain, Size: 2941 bytes --]

Ah gotcha.  Thanks!

On Mon, Aug 19, 2019 at 11:01 PM Mark H Weaver <mhw@netris.org> wrote:

> tags 37086 + notabug
> close 37086
> thanks
>
> Hi Matthew,
>
> Matthew Henry <mcthenry@gmail.com> writes:
>
> > Seen in: guile (GNU Guile) 2.2.4
> >
> > When using the with-readline-completion-function, the passed readline
> > uses the default (apropos) completion function instead of the one
> > provided to with-readline-completion-function.
> >
> > I believe that this is because root/guile-readline/ice-9/readline.scm
> > has defined with-readline-completion-function as a function instead of
> > as a macro.  The readline provided in thunk is executed before the
> > body of with-readline-completion-function executes and overrides
> > *readline-completion-function*.
> >
> > As an aside, I think the API would be better if the completion
> > function could be provided to readline directly.
> >
> > Attached is a sample program.
> >
> > Below is sample output of a run of the attached program.  You can see
> > that it's autocompleting Guile functions and variables (the default
> > apropos completion) rather than the provided one which should have had
> > only 3 options with just one starting in "th".
> >
> > ;;;;;;;;;;;;;;;;;;;;;;;;;;;
> > Prompt:
> > Display all 1859 possibilities? (y or n)
> > Prompt: th
> > the-eof-object   the-scm-module   thread-exited?   thunk?
> > the-root-module  thread?          throw
> > Prompt: th
> >
> > (use-modules (ice-9 readline))
> >
> > (with-readline-completion-function
> >  (make-completion-function '("one" "two" "three"))
> >  (readline "Prompt: "))
>
> The problem is that 'with-readline-completion-function' expects a
> "thunk" as the second argument.  A "thunk" is a procedure that takes no
> arguments.  Typically this means that you wrap the relevant code within
> (lambda () ...), like this:
>
>   (use-modules (ice-9 readline))
>
>   (with-readline-completion-function
>    (make-completion-function '("one" "two" "three"))
>    (lambda () (readline "Prompt: ")))
>
> > I'm early in my Scheme journey, but here's a suggested fix:
> >
> > (define-syntax-rule (with-readline-completion-function completer expr
> ...)
> >   "With @var{completer} as readline completion function, call @var{expr
> ...}."
> >   (let ((old-completer *readline-completion-function*))
> >     (dynamic-wind
> >       (lambda ()
> >     (set! *readline-completion-function* completer))
> >       (lambda () expr ...)
> >       (lambda ()
> >     (set! *readline-completion-function* old-completer)))))
> >
> > (export with-readline-completion-function)
>
> This is fine, but it's a different API.  It's true that you need to use
> a macro if you want to avoid wrapping the body within (lambda () ...),
> but just like 'dynamic-wind' itself, 'with-readline-completion-function'
> was designed to be an ordinary procedure that accepts the body
> expression(s) as a THUNK.
>
>    Happy hacking!
>         Mark
>

[-- Attachment #2: Type: text/html, Size: 3890 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-08-20 11:35 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-19 17:36 bug#37086: Guile Ice-9 Readline with-readline-completion-function Matthew Henry
2019-08-20  0:12 ` Matthew Henry
2019-08-20  3:00 ` Mark H Weaver
2019-08-20 11:35   ` Matthew Henry

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).