Updated 2016-12-19 17:25:40 by karll

The GNU Readline library [1] 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.

An up-to-date version of tclreadline that works with the latest GNU readline and has had tender loving care from dbohdan can be found at https://github.com/flightaware/tclreadline

Usage 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".

Older stuff appears below:

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 [2].

Code

tclreadline.tcl
load [file dirname [info script]]/tclreadline01.dll
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]} {
            set word $subword
            if {$subword eq [lindex $matches 0]} { break; }
            set subword [string range [lindex $matches 0] 0 [string length $word]]
        }
        return [list $word $matches]

    }
} 

pkgIndex.tcl
package ifneeded tclreadline 0.1 \
[list source [file dirname [info script]]/tclreadline.tcl]

See also:


For those on Linux (and probably most other Unix-like platforms) who don't want to bother building an extension, rlwrap [3] can provide readline editing with:
rlwrap tclsh 

There are other command history filters. These are programs which sit between the user's shell and a program and attempt to provide a history mechanism to commands which have no such capability. One that used to be mentioned is "ile". The master site for the latest version of ile that I knew was is ftp://ftp.inria.fr/system/user/lile-2.0.tar.z , but that URL is no longer active. If someone knows where the latest is, please rewrite this paragraph to mention it.

Another commonly referred command history program is "fep". The master ftp site for the source code for it is ftp://ftp.sra.co.jp/pub/cmd/fep/fep.tar.gz .

See also cle (http://kaolin.unice.fr/Cle/) which has been mentioned as a command history filter that Linux users have available.

A useful place to begin looking for source code for these and other programs is ftp://ftp.freebsd.org/ and its mirrors. A WWW site for this would be http://www.freebsd.org/ .

PT: It is also possible to use [socat] to wrap readline around your tclsh executable.
# Rename your tclsh8.6 binary to tclsh8.6.exe then create the following wrapper script
mv tclsh8.6 tclsh8.6.exe
cat > tclsh8.6 <<EOF
#!/bin/bash
bin=$(readlink -f $0).exe
if [ $# -lt 1 ]; then
    exec socat READLINE,history=$HOME/.tclsh_history EXEC:$bin,pty,ctty
else
    exec $bin $*
fi

The same can be usefully done for wish.