Guile Library

(string completion)

Overview

This module provides a facility that can be used to implement features such as TAB-completion in programs. A class <string-completer> tracks all the potential complete strings. Here is an example usage.

(use-modules (string completion)
             (oop goops)
             (srfi srfi-11))      ;; for the (let-values)

(define c (make <string-completer>))
(add-strings! c "you your yourself yourselves")

(let-values (((completions expansion exact? unique?) (complete c "yours")))
            (display completions)(newline)
            (display expansion) (newline)
            (display exact?)(newline)
            (display unique?)(newline))

==> ("yourself" "yourselves")
    "yoursel"
    #f
    #f

There are several more options for usage, which are detailed in the class and method documentation.

Usage

<string-completer>
[Class]

This is the class that knows what the possible expansions are, and can determine the completions of given partial strings. The following are the recognized keywords on the call to make:

#:strings

This gives the completer an initial set of strings. It is optional, and the add-strings! method can add strings to the completer later, whether these initial strings were given or not. The strings that follow this keyword can take any form that the add-strings! method can take (see below).

#:case-sensitive?

This is a boolean that directs the completer to do its comparisons in a case sensiteve way or not. The default value is #t, for case-sensitive behavior.

case-sensitive-completion?
[Generic]

case-sensitive-completion? completer. Returns #t if the completer is case-sensitive, and #f otherwise.

case-sensitive-completion?
[Method]
add-strings!
[Generic]

add-strings! completer strings. Adds the given strings to the set of possible comletions known to completer. strings can either be a list of strings, or a single string of words separated by spaces. The order of the words given is not important.

add-strings! (sc <string-completer>) (strings <top>)
[Method]
all-completions completer str
[Function]

Returns a list of all possible completions for the given string str. The returned list will be in alphabetical order.

Note that users wanting to customize the completion algorithm can subclass <string-completer> and override this method.

complete
[Generic]

complete completer str. Accepts a string, str, and returns four values via a values call. These are:

completions

This is the same list that would be returned from a call to all-completions.

expansion

This is the longest string that would have returned identical results. In other words, this is what most programs replace your string with when you press TAB once. This value will be equal to str if there were no known completionss.

 ("wonders" "wonderment" "wondering") 
completed against "won" yields an expansion 
of "wonder"
exact?

This will be #t if the returned expansion is an exact match of one of the possible completions.

unique?

This will be #t if there is only one possible completion. Note that when unique? is #t, then exact? will also be #t.

complete (sc <string-completer>) (str <top>)
[Method]