aboutsummaryrefslogtreecommitdiff
path: root/examples/jtclsh.tcl
blob: 83d0b0d850f5d5ecbc0e8e30d2cf53b44410506a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Simple example of how the history extension
# can be used to provide line editing and history

# Build jimsh with the history extension and enable line editing (the default)
# ./configure --with-ext=history

package require history

set histfile [env HOME]/.jtclsh
history load $histfile
# Use the standard Tcl autocompletion and hint support
history completion tcl::autocomplete tcl::autohint
set prefix ""
while {1} {
	# Read a complete line (script)
	set prompt "${prefix}jim> "
	set cmd {}
	while {1} {
		if {[history getline $prompt line] < 0} {
			exit 0
		}
		if {$cmd ne ""} {
			append cmd \n
		}
		append cmd $line
		if {[info complete $cmd char]} {
			break
		}
		set prompt "$char> "
	}

	if {$cmd eq "h"} {
		history show
		continue
	}

	# Don't bother adding single char commands to the history
	if {[string length $cmd] > 1} {
		history add $cmd
		history save $histfile
	}

	# Evaluate the script and display the error
	try {
		set result [eval $cmd]
		set prefix ""
	} on {error return break continue signal} {result opts} {
		set rcname [info returncodes $opts(-code)]
		if {$rcname eq "ok" } {
			# Note: return set -code to 0
			set rcname return
		} elseif {$rcname eq "error"} {
			set result [errorInfo $result]
		}
		set prefix "\[$rcname\] "
	}
	if {$result ne {}} {
		puts $result
	}
}