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.

Direct Known Subclasses

Status, View::Entry

Constant Summary collapse

STYLE_NAME_REGISTER =
[]
STYLE_NAME_POOL =
[]
FORWARD_WORD =
/#{space}+#{word}|#{word}+#{space}+#{word}/
BACKWARD_WORD =
/#{word}+/

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.obtain_style_nameObject



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ver/entry.rb', line 19

def self.obtain_style_name
  unless style_name = STYLE_NAME_POOL.shift
    begin
      id = SecureRandom.hex
      style_name = "#{id}.#{self}.TEntry"
    end while STYLE_NAME_REGISTER.include?(style_name)
    STYLE_NAME_REGISTER << style_name
  end

  style_name
end

.return_style_name(style_name) ⇒ Object



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

def self.return_style_name(style_name)
  STYLE_NAME_POOL << style_name
end

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.



123
124
125
126
127
128
129
# File 'lib/ver/entry.rb', line 123

def accept_line
  line = get
  @history.unshift(line) unless line.empty?
  @history_index = nil
  Event.generate(self, '<<AcceptLine>>')
  delete 0, :end
end

#backward_char(count = 1) ⇒ Object

Move back a character.



94
95
96
# File 'lib/ver/entry.rb', line 94

def backward_char(count = 1)
  self.cursor -= count
end

#backward_delete_char(yank = nil) ⇒ Object



169
170
171
172
173
174
175
176
177
# File 'lib/ver/entry.rb', line 169

def backward_delete_char(yank = nil)
  return if cursor == 0

  if yank
    @killring.unshift get[cursor - 2]
  end

  delete(cursor - 1)
end

#backward_word(count = 1) ⇒ Object

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



109
110
111
112
113
114
115
116
117
# File 'lib/ver/entry.rb', line 109

def backward_word(count = 1)
  line = get.reverse
  count.times do
    pos = get.size - cursor

    return unless md = line.match(BACKWARD_WORD, pos)
    self.cursor = (line.size - md.offset(0).last)
  end
end

#beginning_of_historyObject



155
156
157
158
# File 'lib/ver/entry.rb', line 155

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

#beginning_of_lineObject

Move to the start of the current line.



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

def beginning_of_line
  self.cursor = 0
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_charObject



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

def delete_char
  delete(cursor)
end

#delete_motion(motion, count = 1) ⇒ Object



197
198
199
# File 'lib/ver/entry.rb', line 197

def delete_motion(motion, count = 1)
  delete(*virtual_movement(motion, count))
end

#end_of_historyObject



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

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

#end_of_lineObject

Move to the end of the line.



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

def end_of_line
  self.cursor = :end
end

#error(string) ⇒ Object



67
68
69
# File 'lib/ver/entry.rb', line 67

def error(string)
  self.value = string
end

#forward_backward_delete_charObject

Delete the character under the cursor, unless the cursor is at the end of the line, in which case the character behind the cursor is deleted.



181
182
183
184
185
186
187
188
189
# File 'lib/ver/entry.rb', line 181

def forward_backward_delete_char
  pos = cursor

  if index(:end) == pos
    delete(cursor - 1)
  else
    delete(cursor)
  end
end

#forward_char(count = 1) ⇒ Object

Move forward a character.



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

def forward_char(count = 1)
  self.cursor += count
end

#forward_word(count = 1) ⇒ Object

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



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

def forward_word(count = 1)
  count.times do
    return unless md = get.match(FORWARD_WORD, cursor)
    self.cursor = md.offset(0).last
  end
end

#insert(*args) ⇒ Object



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

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

#insert_string(string) ⇒ Object



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

def insert_string(string)
  insert cursor, string
end

#message(string) ⇒ Object



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

def message(string)
  self.value = string
end

#next_historyObject

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



145
146
147
148
149
150
151
152
153
# File 'lib/ver/entry.rb', line 145

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

#noop(*args) ⇒ Object



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

def noop(*args)
end

#previous_historyObject

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



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/ver/entry.rb', line 132

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

#quitObject



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

def quit
  VER.exit
end

#styleObject



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

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

#transpose_charsObject



191
192
193
194
195
# File 'lib/ver/entry.rb', line 191

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