1# select.tcl
2#
3# Keystroke bindings to select for message or folder
4#
5# Copyright (c) 1993 Xerox Corporation.
6# Use and copying of this software and preparation of derivative works based
7# upon this software are permitted. Any distribution of this software or
8# derivative works must comply with all applicable United States export
9# control laws. This software is made available AS IS, and Xerox Corporation
10# makes no warranty about the software, its performance or its conformity to
11# any specification.
12
13# Select_Bindings creates bindings on the message and ftoc windows
14# that in turn warp focus to the Status window for folder & message selection
15proc Select_Bindings { w } {
16    global select
17    set select(sel) {}
18    set select(prompt) {}
19    foreach digit {0 1 2 3 4 5 6 7 8 9} {
20	bind $w <Key-$digit> {SelectDigit %W %A}
21    }
22    bind $w <Key-plus> {SelectPlus %W}
23    set select(toggle) { Change Target }
24}
25proc SelectPlus { w } {
26    global select
27    $select(entry) configure -state normal
28    focus $select(entry)
29    set select(sel) {}
30    set select(prompt) "[lindex $select(toggle) 0] Folder:"
31    set select(folder) 1
32    Exmh_Status "$select(prompt) "
33}
34proc SelectDigit {w digit} {
35    global select
36    $select(entry) configure -state normal
37    focus $select(entry)
38    set select(sel) $digit
39    set select(prompt) Msg:
40    catch {unset select(folder)}
41    Exmh_Status "Select Msg: $select(sel)"
42    Msg_Change $select(sel) noshow
43}
44
45# These are the bindings on the status entry widget
46proc Select_EntryBind { w } {
47    global select
48    set select(entry) $w
49    bindtags $w [list $w Entry]
50    bind $w <Any-Key>	{SelectTypein %W %A}
51    bind $w <Key-plus>	{SelectToggle %W }
52    bind $w <Key-minus>	{SelectPrev %W }
53    bind $w <space>	{SelectComplete %W}
54    bind $w <Tab>	{SelectComplete %W}
55    bind $w <Return>	{SelectReturn %W}
56    bind $w <Key-s>	{SelectReturn %W %A}
57    bind $w <BackSpace>	{SelectBackSpace %W}
58    bind $w <Control-h>	{SelectBackSpace %W}
59    bind $w <Delete>	{SelectBackSpace %W}
60    bind $w <Control-c>	{SelectCancel %W}
61    bind $w <Control-g>	{SelectCancel %W}
62    bind $w <Control-u>	{SelectClear %W}
63}
64proc SelectTypein {w {a {}}} {
65    global select
66    if {$a == {}} {
67	return
68    }
69    if [info exists select(match)] {
70	set select(sel) $select(match)
71	unset select(match)
72	catch {unset select(allfolders)}
73    }
74    append select(sel) $a
75    Exmh_Status "$select(prompt) $select(sel)"
76    if ![info exists select(folder)] {
77	catch { Msg_Change $select(sel) noshow }
78    }
79}
80proc SelectBackSpace { w } {
81    global select
82    if [info exists select(match)] {
83	set select(sel) $select(match)
84	unset select(match)
85	catch {unset select(allfolders)}
86    }
87    set select(sel) [string range $select(sel) 0 [expr [string length $select(sel)]-2]]
88    Exmh_Status "$select(prompt) $select(sel)"
89    if ![info exists select(folder)] {
90	catch { Msg_Change $select(sel) noshow }
91    }
92}
93proc SelectToggle {w} {
94    global select
95    if [info exists select(folder)] {
96	if {$select(sel) != ""} {
97	    SelectTypein $w +
98	    return
99	}
100	set select(toggle) [list [lindex $select(toggle) 1] [lindex $select(toggle) 0]]
101	set select(prompt) "[lindex $select(toggle) 0] Folder:"
102    } else {
103	catch {
104	    if [regexp {^[0-9]+$} $select(sel)] {
105	        incr select(sel)
106	    }
107	    Msg_Change $select(sel) noshow
108	}
109    }
110    Exmh_Status "$select(prompt) $select(sel)"
111}
112proc SelectPrev {w} {
113    global select
114    if [info exists select(folder)] {
115	SelectTypein $w "-"
116    } else {
117	catch {
118	    if [regexp {^[0-9]+$} $select(sel)] {
119	        incr select(sel) -1
120	    }
121	    Msg_Change $select(sel) noshow
122	}
123        Exmh_Status "$select(prompt) $select(sel)"
124    }
125}
126proc SelectComplete { w } {
127    global select
128    if [info exists select(folder)] {
129	global flist
130	if ![info exists select(allfolders)] {
131	    set select(allfolders) $flist(allfolders)
132	}
133	set ix 0
134	foreach f $select(allfolders) {
135	    incr ix
136	    if [string match $select(sel)* $f] {
137		set select(allfolders) [lrange $select(allfolders) $ix end]
138		if {$select(allfolders) == {}} {
139		    unset select(allfolders)
140		}
141		set select(match) $f
142		Exmh_Status "$select(prompt) $f"
143		return
144	    }
145	}
146	unset select(allfolders)
147	catch {unset select(match)}
148	Exmh_Status "$select(prompt) $select(sel)"
149    }
150}
151proc SelectReturn { w {a {}} } {
152    global select
153    if [info exists select(folder)] {
154	if {$a != {}} {
155	    SelectTypein $w $a
156	    return
157	}
158	if [info exists select(match)] {
159	    set select(sel) $select(match)
160	    unset select(match)
161	}
162	if [string length $select(sel)] {
163	    Folder_[lindex $select(toggle) 0] $select(sel)
164	}
165	unset select(folder)
166	catch {unset select(allfolders)}
167    } else {
168	if [regexp {^[0-9]+$} $select(sel)] {
169	    Msg_Change $select(sel) show
170	}
171    }
172    $select(entry) configure -state disabled
173    Exmh_Focus
174}
175proc SelectCancel { w } {
176    global select
177    if [info exists select(folder)] {
178	set select(toggle) { Change Target }
179	unset select(folder)
180    }
181    $select(entry) configure -state disabled
182    Exmh_Status ""
183    Exmh_Focus
184}
185proc SelectClear { w } {
186    global select
187    set select(sel) {}
188    Exmh_Status "$select(prompt) $select(sel)"
189}
190