Class: VER::Entry
- Inherits:
-
Tk::Tile::Entry
- Object
- Tk::Tile::Entry
- VER::Entry
show all
- Defined in:
- lib/ver/entry.rb
Overview
A custom widget for easier integration
The style related things are needed for Tk versions around 8.5.7, which is what everybody is using currently. It doesn’t GC styles of widgets that are no longer used, so if we simply keep using new style names, we would eventually run out of memory. A small leak, but better we cover this now than tracking it down later. Tk should get some user states, called like ‘user1’, ‘user2’, ‘user3’, that would allow some flexibility, but still won’t be able to represent every mode.
Constant Summary
collapse
- FORWARD_WORD =
/#{space}+#{word}|#{word}+#{space}+#{word}/
- BACKWARD_WORD =
/#{word}+/
Instance Method Summary
collapse
Instance Method Details
#accept_line(event) ⇒ Object
Accept the line regardless of where the cursor is. If this line is non-empty, it will be added to the history list. If the line is a modified history line, the history line is restored to its original state.
225
226
227
228
229
|
# File 'lib/ver/entry.rb', line 225
def accept_line(event)
line = get
Tk::Event.generate(self, '<<AcceptLine>>')
delete(0, :end)
end
|
#beginning_of_history ⇒ Object
209
210
211
212
|
# File 'lib/ver/entry.rb', line 209
def beginning_of_history
@history_index = @history.size - 1
self.value = @history[@history_index]
end
|
#cursor=(pos) ⇒ Object
54
55
56
57
58
|
# File 'lib/ver/entry.rb', line 54
def cursor=(pos)
selection_clear
super
Tk::Event.generate(self, '<<Movement>>')
end
|
#delete(*args) ⇒ Object
Also known as:
kill
47
48
49
50
51
|
# File 'lib/ver/entry.rb', line 47
def delete(*args)
super
Tk::Event.generate(self, '<<Deleted>>')
Tk::Event.generate(self, '<<Modified>>')
end
|
#delete_motion(motion) ⇒ Object
84
85
86
|
# File 'lib/ver/entry.rb', line 84
def delete_motion(motion)
delete(*virtual_movement(motion))
end
|
#end_of_history ⇒ Object
214
215
216
217
|
# File 'lib/ver/entry.rb', line 214
def end_of_history
@history_index = 0
self.value = @history[@history_index]
end
|
#end_of_line(event) ⇒ Object
Move to the end of the entry line.
152
153
154
|
# File 'lib/ver/entry.rb', line 152
def end_of_line(event)
self.cursor = :end
end
|
#error(string) ⇒ Object
30
31
32
|
# File 'lib/ver/entry.rb', line 30
def error(string)
self.value = string
end
|
#insert(*args) ⇒ Object
34
35
36
37
38
|
# File 'lib/ver/entry.rb', line 34
def insert(*args)
super
Tk::Event.generate(self, '<<Inserted>>')
Tk::Event.generate(self, '<<Modified>>')
end
|
#insert_selection(event) ⇒ Object
Insert X selection at cursor position
67
68
69
|
# File 'lib/ver/entry.rb', line 67
def insert_selection(event)
insert(cursor, Tk::Selection.get)
end
|
#insert_string(event) ⇒ Object
62
63
64
|
# File 'lib/ver/entry.rb', line 62
def insert_string(event)
insert(cursor, event.unicode)
end
|
#insert_tab(event) ⇒ Object
Insert a literal tab character at cursor position
72
73
74
|
# File 'lib/ver/entry.rb', line 72
def insert_tab(event)
insert(cursor, "\t")
end
|
#kill_end_of_line(event) ⇒ Object
108
109
110
|
# File 'lib/ver/entry.rb', line 108
def kill_end_of_line(event)
kill_motion :end_of_line
end
|
#kill_motion(motion) ⇒ Object
88
89
90
|
# File 'lib/ver/entry.rb', line 88
def kill_motion(motion)
kill(*virtual_movement(motion))
end
|
#kill_next_char(event) ⇒ Object
96
97
98
|
# File 'lib/ver/entry.rb', line 96
def kill_next_char(event)
kill_motion :next_char
end
|
#kill_next_word(event) ⇒ Object
104
105
106
|
# File 'lib/ver/entry.rb', line 104
def kill_next_word(event)
kill_motion :next_word
end
|
#kill_prev_char(event) ⇒ Object
92
93
94
|
# File 'lib/ver/entry.rb', line 92
def kill_prev_char(event)
kill_motion :prev_char
end
|
#kill_prev_word(event) ⇒ Object
100
101
102
|
# File 'lib/ver/entry.rb', line 100
def kill_prev_word(event)
kill_motion :prev_word
end
|
#message(string) ⇒ Object
26
27
28
|
# File 'lib/ver/entry.rb', line 26
def message(string)
self.value = string
end
|
#next_char(event) ⇒ Object
Move forward a character.
157
158
159
|
# File 'lib/ver/entry.rb', line 157
def next_char(event)
self.cursor += 1
end
|
#next_history ⇒ Object
Fetch the next command from the history list, moving forward in the list.
199
200
201
202
203
204
205
206
207
|
# File 'lib/ver/entry.rb', line 199
def next_history
if @history_index && @history_index > 0
@history_index -= 1
else
@history_index = @history.size - 1
end
self.value = @history[@history_index]
end
|
#next_word(event) ⇒ Object
Move forward to the end of the next word. Words are composed of alphanumeric characters (letters and digits).
168
169
170
171
|
# File 'lib/ver/entry.rb', line 168
def next_word(event)
return unless md = get.match(FORWARD_WORD, cursor)
self.cursor = md.offset(0).last
end
|
#prev_char(event) ⇒ Object
162
163
164
|
# File 'lib/ver/entry.rb', line 162
def prev_char(event)
self.cursor -= 1
end
|
#prev_word(event) ⇒ Object
Move back to the start of the current or previous word. Words are composed of alphanumeric characters (letters and digits).
175
176
177
178
179
180
181
|
# File 'lib/ver/entry.rb', line 175
def prev_word(event)
line = get.reverse
pos = get.size - cursor
return unless md = line.match(BACKWARD_WORD, pos)
self.cursor = (line.size - md.offset(0).last)
end
|
#previous_history ⇒ Object
Fetch the previous command from the history list, moving back in the list.
186
187
188
189
190
191
192
193
194
195
196
|
# File 'lib/ver/entry.rb', line 186
def previous_history
history_size = @history.size
if @history_index && @history_index < history_size
@history_index = [@history_index + 1, history_size - 1].min
else
@history_index = 0
end
self.value = @history[@history_index]
end
|
#quit(event = nil) ⇒ Object
22
23
24
|
# File 'lib/ver/entry.rb', line 22
def quit(event = nil)
VER.exit
end
|
#sel_end_of_line(event) ⇒ Object
139
140
141
142
|
# File 'lib/ver/entry.rb', line 139
def sel_end_of_line(event)
Tk.execute_only('ttk::entry::Extend', self, :end)
end
|
#sel_next_char(event) ⇒ Object
119
120
121
122
|
# File 'lib/ver/entry.rb', line 119
def sel_next_char(event)
Tk.execute_only('ttk::entry::Extend', self, :nextchar)
end
|
#sel_next_word(event) ⇒ Object
129
130
131
132
|
# File 'lib/ver/entry.rb', line 129
def sel_next_word(event)
Tk.execute_only('ttk::entry::Extend', self, :nextword)
end
|
#sel_prev_char(event) ⇒ Object
114
115
116
117
|
# File 'lib/ver/entry.rb', line 114
def sel_prev_char(event)
Tk.execute_only('ttk::entry::Extend', self, :prevchar)
end
|
#sel_prev_word(event) ⇒ Object
124
125
126
127
|
# File 'lib/ver/entry.rb', line 124
def sel_prev_word(event)
Tk.excute_only('ttk::entry::Extend', self, :prevword)
end
|
#sel_start_of_line(event) ⇒ Object
134
135
136
137
|
# File 'lib/ver/entry.rb', line 134
def sel_start_of_line(event)
Tk.execute_only('ttk::entry::Extend', self, :home)
end
|
#start_of_line(event) ⇒ Object
Move to the start of the current line.
147
148
149
|
# File 'lib/ver/entry.rb', line 147
def start_of_line(event)
self.cursor = 0
end
|
#style ⇒ Object
17
18
19
20
|
# File 'lib/ver/entry.rb', line 17
def style
style = cget(:style)
style.flatten.first if style
end
|
#transpose_chars(event) ⇒ Object
76
77
78
79
80
|
# File 'lib/ver/entry.rb', line 76
def transpose_chars(event)
char = get[cursor]
delete(cursor)
insert(cursor - 1, char)
end
|
#value=(string) ⇒ Object
40
41
42
43
44
45
|
# File 'lib/ver/entry.rb', line 40
def value=(string)
execute_only(:delete, 0, :end)
execute_only(:insert, 0, string)
Tk::Event.generate(self, '<<Replaced>>')
Tk::Event.generate(self, '<<Modified>>')
end
|
#virtual_movement(name, count = 1) ⇒ Object
231
232
233
234
235
236
237
238
239
|
# File 'lib/ver/entry.rb', line 231
def virtual_movement(name, count = 1)
pos = cursor
__send__(name, count)
mark = cursor
self.cursor = pos
return [pos, mark].sort
rescue => ex
VER.error(ex)
end
|