Class: VER::Entry

Inherits:
Tk::Tile::Entry
  • Object
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_lineObject

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.



236
237
238
239
240
# File 'lib/ver/entry.rb', line 236

def accept_line
  line = get
  Tk::Event.generate(self, '<<AcceptLine>>')
  delete(0, :end)
end

#beginning_of_historyObject



222
223
224
225
# File 'lib/ver/entry.rb', line 222

def beginning_of_history
  @history_index = @history.size - 1
  self.value = @history[@history_index]
end

#cursor=(pos) ⇒ Object



62
63
64
65
66
# File 'lib/ver/entry.rb', line 62

def cursor=(pos)
  selection_clear
  super
  Tk::Event.generate(self, '<<Movement>>')
end

#delete(*args) ⇒ Object



51
52
53
54
55
# File 'lib/ver/entry.rb', line 51

def delete(*args)
  super
  Tk::Event.generate(self, '<<Deleted>>')
  Tk::Event.generate(self, '<<Modified>>')
end

#delete_next_charObject



109
110
111
# File 'lib/ver/entry.rb', line 109

def delete_next_char
  deleting :next_char
end

#delete_next_wordObject



117
118
119
# File 'lib/ver/entry.rb', line 117

def delete_next_word
  deleting :next_word
end

#delete_prev_charObject



105
106
107
# File 'lib/ver/entry.rb', line 105

def delete_prev_char
  deleting :prev_char
end

#delete_prev_wordObject



113
114
115
# File 'lib/ver/entry.rb', line 113

def delete_prev_word
  deleting :prev_word
end

#deleting(motion) ⇒ Object

Delete



97
98
99
# File 'lib/ver/entry.rb', line 97

def deleting(motion)
  delete(*virtual_movement(motion))
end

#end_of_historyObject



227
228
229
230
# File 'lib/ver/entry.rb', line 227

def end_of_history
  @history_index = 0
  self.value = @history[@history_index]
end

#end_of_lineObject

Move to the end of the entry line.



165
166
167
# File 'lib/ver/entry.rb', line 165

def end_of_line
  self.cursor = :end
end

#error(*args) ⇒ Object



34
35
36
# File 'lib/ver/entry.rb', line 34

def error(*args)
  VER.warn(*args)
end

#eventObject



20
21
22
# File 'lib/ver/entry.rb', line 20

def event
  major_mode.event_history.last
end

#eventsObject



16
17
18
# File 'lib/ver/entry.rb', line 16

def events
  major_mode.event_history
end

#insert(*args) ⇒ Object



38
39
40
41
42
# File 'lib/ver/entry.rb', line 38

def insert(*args)
  super
  Tk::Event.generate(self, '<<Inserted>>')
  Tk::Event.generate(self, '<<Modified>>')
end

#insert_selectionObject

Insert X selection at cursor position



75
76
77
# File 'lib/ver/entry.rb', line 75

def insert_selection
  insert(cursor, Tk::Selection.get(type: 'UTF8_STRING'))
end

#insert_string(event = self.event) ⇒ Object

Insert



70
71
72
# File 'lib/ver/entry.rb', line 70

def insert_string(event = self.event)
  insert(cursor, event.unicode)
end

#insert_tabObject

Insert a literal tab character at cursor position



80
81
82
# File 'lib/ver/entry.rb', line 80

def insert_tab
  insert(cursor, "\t")
end

#kill(*args) ⇒ Object



57
58
59
60
# File 'lib/ver/entry.rb', line 57

def kill(*args)
  Clipboard.dwim = get(*args)
  delete(*args)
end

#kill_end_of_lineObject



121
122
123
# File 'lib/ver/entry.rb', line 121

def kill_end_of_line
  killing :end_of_line
end

#killing(motion) ⇒ Object



101
102
103
# File 'lib/ver/entry.rb', line 101

def killing(motion)
  kill(*virtual_movement(motion))
end

#message(*args) ⇒ Object



30
31
32
# File 'lib/ver/entry.rb', line 30

def message(*args)
  VER.message(*args)
end

#next_charObject

Move forward a character.



170
171
172
# File 'lib/ver/entry.rb', line 170

def next_char
  self.cursor += 1
end

#next_historyObject

Fetch the next command from the history list, moving forward in the list.



212
213
214
215
216
217
218
219
220
# File 'lib/ver/entry.rb', line 212

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_wordObject

Move forward to the end of the next word. Words are composed of alphanumeric characters (letters and digits).



181
182
183
184
# File 'lib/ver/entry.rb', line 181

def next_word
  return unless md = get.match(FORWARD_WORD, cursor)
  self.cursor = md.offset(0).last
end

#pasteObject



84
85
86
87
# File 'lib/ver/entry.rb', line 84

def paste
  return unless content = VER::Clipboard.get
  insert(cursor, content)
end

#prev_charObject

Move back a character.



175
176
177
# File 'lib/ver/entry.rb', line 175

def prev_char
  self.cursor -= 1
end

#prev_wordObject

Move back to the start of the current or previous word. Words are composed of alphanumeric characters (letters and digits).



188
189
190
191
192
193
194
# File 'lib/ver/entry.rb', line 188

def prev_word
  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_historyObject

Fetch the previous command from the history list, moving back in the list.



199
200
201
202
203
204
205
206
207
208
209
# File 'lib/ver/entry.rb', line 199

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

#sel_end_of_lineObject



152
153
154
155
# File 'lib/ver/entry.rb', line 152

def sel_end_of_line
  # bind TEntry <Shift-Key-End>		{ ttk::entry::Extend %W end }
  Tk.execute_only('ttk::entry::Extend', self, :end)
end

#sel_next_charObject



132
133
134
135
# File 'lib/ver/entry.rb', line 132

def sel_next_char
  # bind TEntry <Shift-Key-Right>		{ ttk::entry::Extend %W nextchar }
  Tk.execute_only('ttk::entry::Extend', self, :nextchar)
end

#sel_next_wordObject



142
143
144
145
# File 'lib/ver/entry.rb', line 142

def sel_next_word
  # bind TEntry <Shift-Control-Key-Right>	{ ttk::entry::Extend %W nextword }
  Tk.execute_only('ttk::entry::Extend', self, :nextword)
end

#sel_prev_charObject

Selection



127
128
129
130
# File 'lib/ver/entry.rb', line 127

def sel_prev_char
  # bind TEntry <Shift-Key-Left> 		{ ttk::entry::Extend %W prevchar }
  Tk.execute_only('ttk::entry::Extend', self, :prevchar)
end

#sel_prev_wordObject



137
138
139
140
# File 'lib/ver/entry.rb', line 137

def sel_prev_word
  # bind TEntry <Shift-Control-Key-Left>	{ ttk::entry::Extend %W prevword }
  Tk.excute_only('ttk::entry::Extend', self, :prevword)
end

#sel_start_of_lineObject



147
148
149
150
# File 'lib/ver/entry.rb', line 147

def sel_start_of_line
  # bind TEntry <Shift-Key-Home>		{ ttk::entry::Extend %W home }
  Tk.execute_only('ttk::entry::Extend', self, :home)
end

#start_of_lineObject

Move to the start of the current line.



160
161
162
# File 'lib/ver/entry.rb', line 160

def start_of_line
  self.cursor = 0
end

#styleObject

Maintenance



25
26
27
28
# File 'lib/ver/entry.rb', line 25

def style
  style = cget(:style)
  style.flatten.first if style
end

#transpose_charsObject



89
90
91
92
93
# File 'lib/ver/entry.rb', line 89

def transpose_chars
  char = get[cursor]
  delete(cursor)
  insert(cursor - 1, char)
end

#value=(string) ⇒ Object



44
45
46
47
48
49
# File 'lib/ver/entry.rb', line 44

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, *args) ⇒ Object



242
243
244
245
246
247
248
249
250
# File 'lib/ver/entry.rb', line 242

def virtual_movement(name, *args)
  pos = cursor
  __send__(name, *args)
  mark = cursor
  self.cursor = pos
  return [pos, mark].sort
rescue => ex
  VER.error(ex)
end