Check-in [dc9268f6ad]
Not logged in
Overview
SHA1:dc9268f6ad4c91f21cb139567a0cb8b4a018c48b
Date: 2014-03-17 21:17:14
User: quentin@minster.io
Comment:Harmonize style in all Tcl files Brace arguments of if/expr constructs
Timelines: family | ancestors | descendants | both | trunk | origin/devel
Downloads: Tarball | ZIP archive
Other Links: files | file ages | folders | manifest
Tags And Properties
Context
2014-03-17
21:17
[320bbe752c] Enhance a bit of logic (expr should be cheaper than regexp) (user: quentin@minster.io, tags: trunk, origin/devel)
21:17
[dc9268f6ad] Harmonize style in all Tcl files Brace arguments of if/expr constructs (user: quentin@minster.io, tags: trunk, origin/devel)
2014-02-26
22:37
[a9511ba2db] Cleanup of compilation warnings * Add the -Wall compilation flag * Use rl_ding() instead of undeclared ding() (user: quentin@minster.io, tags: trunk, origin/devel)
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Modified tclreadlineCompleter.tcl from [1ea8a61e03] to [b445227807].

more than 10,000 changes

Modified tclreadlineInit.tcl.in from [554b1c74c7] to [339306884c].

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

namespace eval tclreadline:: {
    namespace export Init
}

proc ::tclreadline::Init {} {
    uplevel #0 {
    if ![info exists tclreadline::library] {
        if [catch {load [file join @TCLRL_LIBDIR@ libtclreadline[info sharedlibextension]]} msg] {
        puts stderr $msg
        exit 2
        }
    }
    }
}

tclreadline::Init
::tclreadline::readline customcompleter ::tclreadline::ScriptCompleter

source [file join [file dirname [info script]] tclreadlineSetup.tcl]

set auto_index(::tclreadline::ScriptCompleter) \
[list source [file join [file dirname [info script]] tclreadlineCompleter.tcl]]







|
|
|
|
|
|









|
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

namespace eval tclreadline:: {
    namespace export Init
}

proc ::tclreadline::Init {} {
    uplevel #0 {
        if {![info exists tclreadline::library]} {
            if {[catch {load [file join @TCLRL_LIBDIR@ libtclreadline[info sharedlibextension]]} msg]} {
                puts stderr $msg
                exit 2
            }
        }
    }
}

tclreadline::Init
::tclreadline::readline customcompleter ::tclreadline::ScriptCompleter

source [file join [file dirname [info script]] tclreadlineSetup.tcl]

set auto_index(::tclreadline::ScriptCompleter) \
    [list source [file join [file dirname [info script]] tclreadlineCompleter.tcl]]

Modified tclreadlineSetup.tcl.in from [601cc4d255] to [3c98c4bea3].

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74

75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115

116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
    # Save the values of errorCode and errorInfo variables, since they
    # may get modified if caught errors occur below.  The variables will
    # be restored just before re-executing the missing command.

    set savedErrorCode $errorCode
    set savedErrorInfo $errorInfo
    set name [lindex $args 0]
    if ![info exists auto_noload] {
    #
    # Make sure we're not trying to load the same proc twice.
    #
    if [info exists unknown_pending($name)] {
        return -code error "self-referential recursion in \"unknown\" for command \"$name\""
    }
    set unknown_pending($name) pending
    set ret [catch {auto_load $name [uplevel 1 {namespace current}]} msg]
    unset unknown_pending($name)
    if {$ret != 0} {
        return -code $ret -errorcode $errorCode \
        "error while autoloading \"$name\": $msg"
    }
    if ![array size unknown_pending] {
        unset unknown_pending
    }
    if $msg {
        set errorCode $savedErrorCode
        set errorInfo $savedErrorInfo
        set code [catch {uplevel 1 $args} msg]
        if {$code ==  1} {
        #
        # Strip the last five lines off the error stack (they're
            # from the "uplevel" command).
        #

        set new [split $errorInfo \n]
        set new [join [lrange $new 0 [expr [llength $new] - 6]] \n]
        return -code error -errorcode $errorCode \
        -errorinfo $new $msg
        } else {
        return -code $code $msg
        }
    }
    }

    # REMOVED THE [info script] TEST (joze, SEP 98)
    if {([info level] == 1) && [info exists tcl_interactive] && $tcl_interactive} {
    if ![info exists auto_noexec] {
        set new [auto_execok $name]
        if {$new != ""} {
        set errorCode $savedErrorCode
        set errorInfo $savedErrorInfo
        set redir ""
        if {[info commands console] == ""} {
            set redir ">&@stdout <@stdin"
        }
        # LOOK FOR GLOB STUFF IN $ARGS (joze, SEP 98)
        return [uplevel eval exec $redir $new \

        [::tclreadline::Glob [lrange $args 1 end]]]
        }
    }
    set errorCode $savedErrorCode
    set errorInfo $savedErrorInfo
    if {$name == "!!"} {
        set newcmd [history event]
    } elseif {[regexp {^!(.+)$} $name dummy event]} {
        set newcmd [history event $event]
    } elseif {[regexp {^\^([^^]*)\^([^^]*)\^?$} $name dummy old new]} {
        set newcmd [history event -1]
        catch {regsub -all -- $old $newcmd $new newcmd}
    }
    if [info exists newcmd] {
        tclLog $newcmd
        history change $newcmd 0
        return [uplevel $newcmd]
    }

    set ret [catch {set cmds [info commands $name*]} msg]
    if {[string compare $name "::"] == 0} {
        set name ""
    }
    if {$ret != 0} {
        return -code $ret -errorcode $errorCode \
        "error in unknown while checking if \"$name\" is a unique command abbreviation: $msg"
    }
    if {[llength $cmds] == 1} {
        return [uplevel [lreplace $args 0 0 $cmds]]
    }
    if {[llength $cmds] != 0} {
        if {$name == ""} {
        return -code error "empty command name \"\""
        } else {
        return -code error \
        "ambiguous command name \"$name\": [lsort $cmds]"
        }
    }
    }
    return -code error "invalid command name \"$name\""
}


namespace eval tclreadline {

namespace export Setup Loop InitTclCmds InitTkCmds Print ls

proc ls {args} {
    if {[exec uname -s] == "Linux"} {
        eval exec ls --color -FC [Glob $args]
    } else {
        eval exec ls -FC [Glob $args]
    }
}

proc Setup {args} {

    uplevel #0 {

    if {"" == [info commands ::tclreadline::readline]} {
        ::tclreadline::Init
    }

    if {"" == [info procs ::tclreadline::prompt1] && [info nameofexecutable] != ""} {

        namespace eval ::tclreadline {
        variable prompt_string
        set base [file tail [info nameofexecutable]]

        if {[string match tclsh* $base] && [info exists tcl_version]} {
            set prompt_string \
            "\[0;31mtclsh$tcl_version\[0m"
        } elseif {[string match wish* $base] \
            && [info exists tk_version]} {
            set prompt_string "\[0;34mwish$tk_version\[0m"
            } else {
            set prompt_string "\[0;31m$base\[0m"
            }

        }

        if {"" == [info procs ::tclreadline::prompt1]} {
        proc ::tclreadline::prompt1 {} {
            variable prompt_string
            global env
            if {[catch {set pwd [pwd]} tmp]} {
            set pwd "unable to get pwd"
            }

            if [info exists env(HOME)] {
            regsub $env(HOME) $pwd "~" pwd
            }
            return "$prompt_string \[$pwd\]"
        }
        }
        # puts body=[info body ::tclreadline::prompt1]
    }

    if {"" == [info procs ::tclreadline::prompt2] && [info nameofexecutable] != ""} {

        if {"" == [info procs ::tclreadline::prompt2]} {
        proc ::tclreadline::prompt2 {} {
            return ">"
        }
        }
        # puts body=[info body ::tclreadline::prompt2]
    }

    if {"" == [info procs exit]} {

        catch {rename ::tclreadline::Exit ""}
        rename exit ::tclreadline::Exit

        proc exit {args} {

        if {[catch {
            ::tclreadline::readline write \
            [::tclreadline::HistoryFileGet]
        } ::tclreadline::errorMsg]} {
            puts stderr $::tclreadline::errorMsg
        }

        # this call is ignored, if tclreadline.c
        # was compiled with CLEANUP_AFER_SIGNAL
        # not defined. This is the case for
        # older versions of libreadline.
        #
        ::tclreadline::readline reset-terminal

        if [catch "eval ::tclreadline::Exit $args" message] {
            puts stderr "error:"
            puts stderr "$message"
        }
        # NOTREACHED
        }
    }

    }

    global env
    variable historyfile

    if {[string trim [llength ${args}]]} {
    set historyfile ""
    catch {
        set historyfile [file nativename [lindex ${args} 0]]
    }
    if {"" == [string trim $historyfile]} {
        set historyfile [lindex ${args} 0]
    }
    } else {
    if [info exists env(HOME)] {
        set historyfile  $env(HOME)/.tclsh-history
    } else {
        set historyfile  .tclsh-history
    }
    }
    set ::tclreadline::errorMsg [readline initialize $historyfile]
    if {$::tclreadline::errorMsg != ""} {
    puts stderr $::tclreadline::errorMsg
    }

    # InitCmds

    rename Setup ""
}

proc HistoryFileGet {} {
    variable historyfile
    return $historyfile
}

# obsolete
#
proc Glob {string} {

    set commandstring ""
    foreach name $string {
    set replace [glob -nocomplain -- $name]
    if {$replace == ""} {
        lappend commandstring $name
    } else {
        lappend commandstring $replace
    }
    }
    # return $commandstring
    # Christian Krone <krischan@sql.de> proposed
    return [eval concat $commandstring]
}



proc Loop {args} {

    eval Setup ${args}

    uplevel #0 {

    while {1} {

        if {[catch {
        if {"" != [namespace eval ::tclreadline {info procs prompt1}]} {
            set ::tclreadline::LINE [::tclreadline::readline read \
            [::tclreadline::prompt1]]
        } else {
            set ::tclreadline::LINE [::tclreadline::readline read %]
        }
        while {![::tclreadline::readline complete $::tclreadline::LINE]} {
            append ::tclreadline::LINE "\n"
            if {"" != [namespace eval ::tclreadline {info procs prompt2}]} {
            append ::tclreadline::LINE \
                [tclreadline::readline read [::tclreadline::prompt2]]
            } else {
            append ::tclreadline::LINE [tclreadline::readline read >]
            }
        }
        } ::tclreadline::errorMsg]} {
        puts stderr [list tclreadline::Loop: error. \
        $::tclreadline::errorMsg]
        continue
        }

        # Magnus Eriksson <magnus.eriksson@netinsight.se> proposed
        # to add the line also to tclsh's history.
        #
        # I decided to add only lines which are different from
        # the previous one to the history. This is different
        # from tcsh's behaviour, but I found it quite convenient
        # while using mshell on os9.
        #
        if {[string length $::tclreadline::LINE] && \
            [history event 0] != $::tclreadline::LINE} {
        history add $::tclreadline::LINE
        }

        if [catch {
        set ::tclreadline::result [eval $::tclreadline::LINE]
        if {$::tclreadline::result != "" && [tclreadline::Print]} {
            puts $::tclreadline::result
        }
        set ::tclreadline::result ""
        } ::tclreadline::errorMsg] {
        puts stderr $::tclreadline::errorMsg
        puts stderr [list while evaluating $::tclreadline::LINE]
        }

    }
    }
}

proc Print {args} {
    variable PRINT
    if ![info exists PRINT] {
    set PRINT yes
    }
    if [regexp -nocase \(true\|yes\|1\) $args] {
    set PRINT yes
    } elseif [regexp -nocase \(false\|no\|0\) $args] {
    set PRINT no
    }
    return $PRINT
}
#
#
# proc InitCmds {} {
#     # XXX
#     return
#     # XXX
#     global tcl_version tk_version
#     if {[info exists tcl_version]} {
#         InitTclCmds
#     }
#     if {[info exists tk_version]} {
#         InitTkCmds
#     }
#     rename InitCmds ""
# }
#
# proc InitTclCmds {} {
#     variable known_cmds
#     foreach line {
#         "after option ?arg arg ...?"
#         "append varName ?value value ...?"
#         "array option arrayName ?arg ...?"
#         "bgerror"
#         "break"
#         "catch command ?varName?"
#         "cd"
#         "clock"
#         "close <channelId>"
#         "concat"
#         "continue"
#         "eof <channelId>"
#         "error message ?errorInfo? ?errorCode?"
#         "eval arg ?arg ...?"
#         "exec ?switches? arg ?arg ...?"
#         "exit ?returnCode?"
#         "fblocked <channelId>"
#         "for start test next command"
#         "foreach varList list ?varList list ...? command"
#         "format formatString ?arg arg ...?"
#         "gets channelId ?varName?"
#         "glob"
#         "global varName ?varName ...?"
#         "incr varName ?increment?"
#         "info option ?arg arg ...?"
#         "interp cmd ?arg ...?"
#         "join list ?joinString?"
#         "lappend varName ?value value ...?"
#         "lindex list index"
#         "linsert list <index> <element> ?element ...?"
#         "list"
#         "llength list"
#         "lrange list first last"
#         "lreplace list first last ?element element ...?"
#         "lsearch ?mode? list pattern"
#         "lsort ?options? list"
#         "namespace"
#         "package option ?arg arg ...?"
#         "proc name args body"
#         "read ?-nonewline? channelId"
#         "regexp ?switches? exp string ?matchVar? ?subMatchVar subMatchVar ...?"
#         "rename oldName newName"
#         "scan <string> <format> ?varName varName ...?"
#         "set varName ?newValue?"
#         "split <string> ?splitChars?"
#         "subst ?-nobackslashes? ?-nocommands? ?-novariables? string"
#         "switch ?switches? string pattern body ... ?default body?"
#         "time <command> ?count?"
#         "unknown <cmdName> ?arg? ?...?"
#         "uplevel ?level? command ?arg ...?"
#         "vwait name"
#         "while test command"
#     } {
#         readline add $line
#         set known_cmds([lindex $line 0]) ${line}
#     }
#     rename InitTclCmds ""
# }
#
# proc InitTkCmds {} {
#     variable known_cmds
#     foreach line {
#         "bind window ?pattern? ?command?"
#         "bindtags window ?tags?"
#         "button pathName ?options?"
#         "canvas pathName ?options?"
#         "checkbutton pathName ?options?"
#         "clipboard option ?arg arg ...?"
#         "entry pathName ?options?"
#         "event option ?arg1?"
#         "font option ?arg?"
#         "frame pathName ?options?"
#         "grab option ?arg arg ...?"
#         "grid option arg ?arg ...?"
#         "image option ?args?"
#         "label pathName ?options?"
#         "listbox pathName ?options?"
#         "lower window ?belowThis?"
#         "menu pathName ?options?"
#         "menubutton pathName ?options?"
#         "message pathName ?options?"
#         "option cmd arg ?arg ...?"
#         "pack option arg ?arg ...?"
#         "radiobutton pathName ?options?"
#         "raise window ?aboveThis?"
#         "scale pathName ?options?"
#         "scrollbar pathName ?options?"
#         "selection option ?arg arg ...?"
#         "send ?options? interpName arg ?arg ...?"
#         "text pathName ?options?"
#         "tk option ?arg?"
#         "tkwait variable|visibility|window name"
#         "toplevel pathName ?options?"
#         "winfo option ?arg?"
#         "wm option window ?arg ...?"
#     } {
#         readline add $line
#         set known_cmds([lindex $line 0]) ${line}
#     }
#     rename InitTkCmds ""
# }
#


}; # namespace tclreadline







|
|
|
|
|
|
|
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|

|
|
|
<
|
|
|
|


<

|


|
|
|
|
|
|
<
<
>
|












|











|






|

<
|





>



|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<
<
<
<
<
<
<
<
<
<
<
<
<
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
61
62
63
64
65
66
67
68
69


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104

105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442













    # Save the values of errorCode and errorInfo variables, since they
    # may get modified if caught errors occur below.  The variables will
    # be restored just before re-executing the missing command.

    set savedErrorCode $errorCode
    set savedErrorInfo $errorInfo
    set name [lindex $args 0]
    if {![info exists auto_noload]} {
        #
        # Make sure we're not trying to load the same proc twice.
        #
        if {[info exists unknown_pending($name)]} {
            return -code error "self-referential recursion in \"unknown\" for command \"$name\""
        }
        set unknown_pending($name) pending
        set ret [catch {auto_load $name [uplevel 1 {namespace current}]} msg]
        unset unknown_pending($name)
        if {$ret != 0} {

            return -code $ret -errorcode $errorCode "error while autoloading \"$name\": $msg"
        }
        if {![array size unknown_pending]} {
            unset unknown_pending
        }
        if {$msg} {
            set errorCode $savedErrorCode
            set errorInfo $savedErrorInfo
            set code [catch {uplevel 1 $args} msg]
            if {$code == 1} {
                #
                # Strip the last five lines off the error stack (they're
                # from the "uplevel" command).
                #

                set new [split $errorInfo \n]
                set new [join [lrange $new 0 [expr {[llength $new] - 6}]] \n]
                return -code error -errorcode $errorCode -errorinfo $new $msg

            } else {
                return -code $code $msg
            }
        }
    }


    if {([info level] == 1) && [info exists tcl_interactive] && $tcl_interactive} {
    if {![info exists auto_noexec]} {
        set new [auto_execok $name]
        if {$new != ""} {
            set errorCode $savedErrorCode
            set errorInfo $savedErrorInfo
            set redir ""
            if {[info commands console] == ""} {
                set redir ">&@stdout <@stdin"
            }


            # look for glob stuff in $args
            return [uplevel eval exec $redir $new [::tclreadline::Glob [lrange $args 1 end]]]
        }
    }
    set errorCode $savedErrorCode
    set errorInfo $savedErrorInfo
    if {$name == "!!"} {
        set newcmd [history event]
    } elseif {[regexp {^!(.+)$} $name dummy event]} {
        set newcmd [history event $event]
    } elseif {[regexp {^\^([^^]*)\^([^^]*)\^?$} $name dummy old new]} {
        set newcmd [history event -1]
        catch {regsub -all -- $old $newcmd $new newcmd}
    }
    if {[info exists newcmd]} {
        tclLog $newcmd
        history change $newcmd 0
        return [uplevel $newcmd]
    }

    set ret [catch {set cmds [info commands $name*]} msg]
    if {[string compare $name "::"] == 0} {
        set name ""
    }
    if {$ret != 0} {
        return -code $ret -errorcode $errorCode \
            "error in unknown while checking if \"$name\" is a unique command abbreviation: $msg"
    }
    if {[llength $cmds] == 1} {
        return [uplevel [lreplace $args 0 0 $cmds]]
    }
    if {[llength $cmds] != 0} {
        if {$name == ""} {
            return -code error "empty command name \"\""
        } else {

            return -code error "ambiguous command name \"$name\": [lsort $cmds]"
        }
    }
    }
    return -code error "invalid command name \"$name\""
}


namespace eval tclreadline {

    namespace export Setup Loop InitTclCmds InitTkCmds Print ls

    proc ls {args} {
        if {[exec uname -s] == "Linux"} {
            eval exec ls --color -FC [Glob $args]
        } else {
            eval exec ls -FC [Glob $args]
        }
    }

    proc Setup {args} {

        uplevel #0 {

            if {"" == [info commands ::tclreadline::readline]} {
                ::tclreadline::Init
            }

            if {"" == [info procs ::tclreadline::prompt1] && [info nameofexecutable] != ""} {

                namespace eval ::tclreadline {
                    variable prompt_string
                    set base [file tail [info nameofexecutable]]

                    if {[string match tclsh* $base] && [info exists tcl_version]} {
                        set prompt_string "\[0;31mtclsh$tcl_version\[0m"
                    } elseif {[string match wish* $base] && [info exists tk_version]} {
                        set prompt_string "\[0;34mwish$tk_version\[0m"
                    } else {
                        set prompt_string "\[0;31m$base\[0m"
                    }

                }

                if {"" == [info procs ::tclreadline::prompt1]} {
                    proc ::tclreadline::prompt1 {} {
                        variable prompt_string
                        global env
                        if {[catch {set pwd [pwd]} tmp]} {
                            set pwd "unable to get pwd"
                        }

                        if [info exists env(HOME)] {
                            regsub $env(HOME) $pwd "~" pwd
                        }
                        return "$prompt_string \[$pwd\] "
                    }
                }
            }

            if {"" == [info procs ::tclreadline::prompt2] && [info nameofexecutable] != ""} {

                if {"" == [info procs ::tclreadline::prompt2]} {
                    proc ::tclreadline::prompt2 {} {
                        return "> "
                    }
                }
            }

            if {"" == [info procs exit]} {

                catch {rename ::tclreadline::Exit ""}
                rename exit ::tclreadline::Exit

                proc exit {args} {

                    if {[catch {::tclreadline::readline write [::tclreadline::HistoryFileGet]} \
                             ::tclreadline::errorMsg]} {
                        puts stderr $::tclreadline::errorMsg
                    }

                    # this call is ignored, if tclreadline.c
                    # was compiled with CLEANUP_AFER_SIGNAL
                    # not defined. This is the case for
                    # older versions of libreadline.
                    #
                    ::tclreadline::readline reset-terminal

                    if {[catch "eval ::tclreadline::Exit $args" message]} {
                        puts stderr "error:"
                        puts stderr "$message"
                    }
                    # NOTREACHED
                }
            }

        }

        global env
        variable historyfile

        if {[string trim [llength $args]]} {
            set historyfile ""
            catch {set historyfile [file nativename [lindex $args 0]]}
            if {"" == [string trim $historyfile]} {
                set historyfile [lindex $args 0]
            }
        } else {
            if {[info exists env(HOME)]} {
                set historyfile $env(HOME)/.tclsh-history
            } else {
                set historyfile .tclsh-history
            }
        }
        set ::tclreadline::errorMsg [readline initialize $historyfile]
        if {$::tclreadline::errorMsg != ""} {
            puts stderr $::tclreadline::errorMsg
        }

        # InitCmds

        rename Setup ""
    }

    proc HistoryFileGet {} {
        variable historyfile
        return $historyfile
    }

    # obsolete
    #
    proc Glob {string} {

        set commandstring ""
        foreach name $string {
            set replace [glob -nocomplain -- $name]
            if {$replace == ""} {
                lappend commandstring $name
            } else {
                lappend commandstring $replace
            }
        }
        # return $commandstring
        # Christian Krone <krischan@sql.de> proposed
        return [eval concat $commandstring]
    }


    proc Loop {args} {

        eval Setup ${args}

        uplevel #0 {

            while {1} {

                if {[catch {
                    set prompt "% "
                    if {"" != [info procs ::tclreadline::prompt1]} {
                        set prompt [::tclreadline::prompt1]
                    }
                    set ::tclreadline::LINE [::tclreadline::readline read $prompt]
                    while {![::tclreadline::readline complete $::tclreadline::LINE]} {
                        append ::tclreadline::LINE "\n"
                        set prompt2 "> "
                        if {"" != [info procs ::tclreadline::prompt2]} {
                            set prompt2 [::tclreadline::prompt2]
                        }
                        append ::tclreadline::LINE [tclreadline::readline read $prompt2]
                    }
                } ::tclreadline::errorMsg]} {
                    puts stderr "tclreadline::Loop: error. $::tclreadline::errorMsg"
                    continue
                }

                # Magnus Eriksson <magnus.eriksson@netinsight.se> proposed
                # to add the line also to tclsh's history.
                #
                # I decided to add only lines which are different from
                # the previous one to the history. This is different
                # from tcsh's behaviour, but I found it quite convenient
                # while using mshell on os9.
                #
                if {[string length $::tclreadline::LINE]
                        && [history event 0] != $::tclreadline::LINE} {
                    history add $::tclreadline::LINE
                }

                if {[catch {
                    set ::tclreadline::result [eval $::tclreadline::LINE]
                    if {$::tclreadline::result != "" && [tclreadline::Print]} {
                        puts $::tclreadline::result
                    }
                    set ::tclreadline::result ""
                } ::tclreadline::errorMsg]} {
                    puts stderr $::tclreadline::errorMsg
                    puts stderr "while evaluating $::tclreadline::LINE"
                }

            }
        }
    }

    proc Print {args} {
        variable PRINT
        if {![info exists PRINT]} {
            set PRINT yes
        }
        if [regexp -nocase \(true\|yes\|1\) $args] {
            set PRINT yes
        } elseif [regexp -nocase \(false\|no\|0\) $args] {
            set PRINT no
        }
        return $PRINT
    }


#    proc InitCmds {} {
#        # XXX
#        return
#        # XXX
#        global tcl_version tk_version
#        if {[info exists tcl_version]} {
#            InitTclCmds
#        }
#        if {[info exists tk_version]} {
#            InitTkCmds
#        }
#        rename InitCmds ""
#    }
#
#    proc InitTclCmds {} {
#        variable known_cmds
#        foreach line {
#            "after option ?arg arg ...?"
#            "append varName ?value value ...?"
#            "array option arrayName ?arg ...?"
#            "bgerror"
#            "break"
#            "catch command ?varName?"
#            "cd"
#            "clock"
#            "close <channelId>"
#            "concat"
#            "continue"
#            "eof <channelId>"
#            "error message ?errorInfo? ?errorCode?"
#            "eval arg ?arg ...?"
#            "exec ?switches? arg ?arg ...?"
#            "exit ?returnCode?"
#            "fblocked <channelId>"
#            "for start test next command"
#            "foreach varList list ?varList list ...? command"
#            "format formatString ?arg arg ...?"
#            "gets channelId ?varName?"
#            "glob"
#            "global varName ?varName ...?"
#            "incr varName ?increment?"
#            "info option ?arg arg ...?"
#            "interp cmd ?arg ...?"
#            "join list ?joinString?"
#            "lappend varName ?value value ...?"
#            "lindex list index"
#            "linsert list <index> <element> ?element ...?"
#            "list"
#            "llength list"
#            "lrange list first last"
#            "lreplace list first last ?element element ...?"
#            "lsearch ?mode? list pattern"
#            "lsort ?options? list"
#            "namespace"
#            "package option ?arg arg ...?"
#            "proc name args body"
#            "read ?-nonewline? channelId"
#            "regexp ?switches? exp string ?matchVar? ?subMatchVar subMatchVar ...?"
#            "rename oldName newName"
#            "scan <string> <format> ?varName varName ...?"
#            "set varName ?newValue?"
#            "split <string> ?splitChars?"
#            "subst ?-nobackslashes? ?-nocommands? ?-novariables? string"
#            "switch ?switches? string pattern body ... ?default body?"
#            "time <command> ?count?"
#            "unknown <cmdName> ?arg? ?...?"
#            "uplevel ?level? command ?arg ...?"
#            "vwait name"
#            "while test command"
#        } {
#            readline add $line
#            set known_cmds([lindex $line 0]) ${line}
#        }
#        rename InitTclCmds ""
#    }
#
#    proc InitTkCmds {} {
#        variable known_cmds
#        foreach line {
#            "bind window ?pattern? ?command?"
#            "bindtags window ?tags?"
#            "button pathName ?options?"
#            "canvas pathName ?options?"
#            "checkbutton pathName ?options?"
#            "clipboard option ?arg arg ...?"
#            "entry pathName ?options?"
#            "event option ?arg1?"
#            "font option ?arg?"
#            "frame pathName ?options?"
#            "grab option ?arg arg ...?"
#            "grid option arg ?arg ...?"
#            "image option ?args?"
#            "label pathName ?options?"
#            "listbox pathName ?options?"
#            "lower window ?belowThis?"
#            "menu pathName ?options?"
#            "menubutton pathName ?options?"
#            "message pathName ?options?"
#            "option cmd arg ?arg ...?"
#            "pack option arg ?arg ...?"
#            "radiobutton pathName ?options?"
#            "raise window ?aboveThis?"
#            "scale pathName ?options?"
#            "scrollbar pathName ?options?"
#            "selection option ?arg arg ...?"
#            "send ?options? interpName arg ?arg ...?"
#            "text pathName ?options?"
#            "tk option ?arg?"
#            "tkwait variable|visibility|window name"
#            "toplevel pathName ?options?"
#            "winfo option ?arg?"
#            "wm option window ?arg ...?"
#        } {
#            readline add $line
#            set known_cmds([lindex $line 0]) ${line}
#        }
#        rename InitTkCmds ""
#    }


}; # namespace tclreadline