Version 44 of readline

Updated 2018-03-21 18:15:49 by dbohdan

The GNU Readline library [L1 ] provides a set of functions for use by applications that allow users to edit command lines as they are typed in. Most of the work in the readline library is done by the readline function.

Tcl bindings

Original tclreadline

What tclreadline
Where http://tclreadline.sourceforge.net/
Description Tcl bindings for GNU readline, by Johannes Zellner, used to enhance an interactive Tcl.
Latest version 2.1.0
Updated 07/2001

More recently updated forks

Use example

If you put the following in your .tclshrc file in your home directory then when you run Tcl interactively you'll get tclreadline support, with a fancy prompt showing you what machine you're on and the last directory of your current directory:

package require tclreadline

proc ::tclreadline::prompt1 {} {
    return "[lindex [split [info hostname] "."] 0] [lindex [split [pwd] "/"] end] % "
}

::tclreadline::Loop

You can access your Tcl command line history using vi or emacs-style keystrokes by creating a .inputrc file in your home directory and putting a line it it that says "set editing-mode vi" or "set editing-mode emacs".

Implementing completion yourself with an old version of tclreadline

The readline procs can be used to create for instance a basic Tcl sh with command completion:

package require tclreadline


# This proc returns the larges common startstring in a list, e.g.:
# for {list link} it will return li
 
namespace eval readline {
    namespace export readline history
    proc largest_common_substring { word matches } {
        # returns a list with the largest common substring
        if {[llength $matches]==1} {
            return [list [lindex $matches 0] $matches]
        }
        set subword $word
        while {[llength [lsearch -inline -all $matches $subword*]]==[llength $matches]} {
            if {$subword eq [lindex $matches 0]} break;
            set word $subword
            set subword [string range [lindex $matches 0] 0 [string length $word]]
        }
        return [list $word $matches]

    }
}

readline::history read ~/.sh_history ; # read saved history

# unknown should behave as in an interactive tclsh
set tcl_interactive 1 ; info script ""

# save history before exit use hide instead if rename so no _exit is show in [info commands]
interp hide {} exit
 
proc exit {args} {
     readline::history write ~/.sh_history
     interp invokehidden {} exit
}

# Define completion proc
# The completion proc is called from readline with the arguments:
# line:  The complete line in the readline buffer
# word:  The word that is being completed
# start: The start index of the word in line
# end:   The end index of the word in line
#
# A completion proc returns a list with two elements 
# 0: The text that will replace the word that is being completed
# 1: A list of all possible matches for the word that is currently being completed

proc complete {line word start end} {
    set matches {}
    if {[string index $word 0] eq {$}} {
        # variable completion
        set var_name [string range $word 1 end]
        foreach var [uplevel #0 [list info vars ${var_name}*]] {
            lappend matches \$[set var]
        }
    } elseif {$word eq $line} {
        # command completion
        set matches [uplevel #0 [list info commands $word*]]
        foreach ns [namespace children ::] {
            if {[string match $word* $ns]!=0} {
                lappend matches $ns
            }
        }
    } else {
        foreach file [glob -nocomplain $word*] {
            string map [list [file normalize ~] ~] $file
            lappend matches [string map  {{ } {\ }} $file]
        }
        foreach file [glob -nocomplain -type hidden $word*] {
            string map [list [file normalize ~] ~] $file
            lappend matches [string map  {{ } {\ }} $file]
        }
    }
    # suppress space
    if {[llength $matches] == 1} {
        return [list [lindex $matches 0] [list {}]]
    }
    return [::readline::largest_common_substring $word $matches]
} 

 
# register compeletion proc, completion can be disabled by readline::completion {}

readline::completion complete
    
# command loop
while {1} {
    set command [readline::readline "([file tail [pwd]]) % "]
    while {![info complete $command]} {
     set command $command\n[readline::readline "> "]
    }
    readline::history add $command
    catch [eval $command] result
    if {($result ne "") && ([string range $command end-1 end] ne ";;")} { 
        puts $result
    }
}

Notes

slebetman notes that for the Win32 platform, readline is not necessary as native tclsh on Win32 already have line editing capability including history and history substitution (which I believe is due to DOSKEY). People usually want readline for Tcl when they are on Unix.

MJ agrees that on Windows it is not necessary per se, but I like a consistent interface in my application regardless if it is run on Linux or Windows. Readline keyboard shortcuts have a tendency to stick in your fingers, which makes working with a Windows CLI application painful. Also note that the code below should be easy to adapt to Linux (probably only removing the __declspec(dllexport)) giving you a binding to readline on Linux.

MJ -- In the previous build an access violation occured on the free(line_read) this was caused by the fact that the dll linked to msvcr70.dll and msvcrt.dll at the same time. malloc was used from the one dll and free from the other resulting in a crash. The current dll at the url above only links to msvcrt.dll, solving the problem.

MJ -- 14/08/2006 -- The newer version includes history modification commands and allows the readline completion to be performed by a Tcl proc. This allows integration into your own Tcl programs where the default readline completions don't make sense. It has been tested on Windows, but should be easy to adapt to Linux. A built for Windows compiled against 8.4.13 with stubs enabled can be downloaded from the URL above.

MJ -- When calling readline::readline, no event processing in the eventloop will take place. This is something to keep in mind when including this in GUI apps. It can be worked around by executing readline in a different thread from everything else. See [L2 ].

See also