Module: Coolline::Editor
- Included in:
- Coolline
- Defined in:
- lib/coolline/editor.rb
Instance Method Summary collapse
-
#backward_char ⇒ Object
Moves to the previous character.
-
#backward_word ⇒ Object
Moves to the previous word.
-
#beginning_of_line ⇒ Object
Moves the cursor to the beginning of the line.
-
#capitalize_word ⇒ Object
Capitalizes the current word.
-
#clear_line ⇒ Object
Removes all the characters in the line.
-
#end_of_line ⇒ Object
Moves to the end of the line.
-
#forward_char ⇒ Object
Moves to the next character.
-
#forward_word ⇒ Object
Moves to the next word.
-
#insert_string(string) ⇒ Object
Inserts a string at the current point in the line.
-
#kill_backward_char ⇒ Object
Removes the previous character.
-
#kill_backward_word ⇒ Object
Removes the previous word.
-
#kill_beginning_of_line ⇒ Object
Removes everything up to the current character.
-
#kill_current_char ⇒ Object
Removes the current character.
-
#kill_forward_word ⇒ Object
Removes the next word.
-
#kill_line ⇒ Object
Removes all the characters beyond the current point.
-
#lowercase_word ⇒ Object
Lowercases the current word.
- #non_word_boundary_after(pos) ⇒ Object
- #non_word_boundary_before(pos) ⇒ Object
-
#transpose_chars ⇒ Object
Swaps the previous character with the current one.
-
#transpose_words ⇒ Object
Swaps the current word with the previous word, or the two previous words if we’re at the end of the line.
-
#uppercase_word ⇒ Object
Uppercases the current word.
- #word_beginning_after(pos) ⇒ Object
- #word_beginning_before(pos) ⇒ Object
-
#word_boundary?(string) ⇒ Boolean
This method can be overriden to change characters considered as word boundaries.
- #word_boundary_after(pos) ⇒ Object
- #word_boundary_before(pos) ⇒ Object
- #word_end_after(pos) ⇒ Object
- #word_end_before(pos) ⇒ Object
Instance Method Details
#backward_char ⇒ Object
Moves to the previous character
84 85 86 |
# File 'lib/coolline/editor.rb', line 84 def backward_char self.pos -= 1 if pos != 0 end |
#backward_word ⇒ Object
Moves to the previous word
94 95 96 |
# File 'lib/coolline/editor.rb', line 94 def backward_word self.pos = word_beginning_before(pos) if pos > 0 end |
#beginning_of_line ⇒ Object
Moves the cursor to the beginning of the line
74 75 76 |
# File 'lib/coolline/editor.rb', line 74 def beginning_of_line self.pos = 0 end |
#capitalize_word ⇒ Object
Capitalizes the current word
217 218 219 220 221 222 |
# File 'lib/coolline/editor.rb', line 217 def capitalize_word if beg = word_beginning_after(pos) and last = word_end_after(pos) line[beg..last] = line[beg..last].capitalize self.pos = last + 1 end end |
#clear_line ⇒ Object
Removes all the characters in the line
147 148 149 150 |
# File 'lib/coolline/editor.rb', line 147 def clear_line line.clear self.pos = 0 end |
#end_of_line ⇒ Object
Moves to the end of the line
79 80 81 |
# File 'lib/coolline/editor.rb', line 79 def end_of_line self.pos = line.size end |
#forward_char ⇒ Object
Moves to the next character
89 90 91 |
# File 'lib/coolline/editor.rb', line 89 def forward_char self.pos += 1 if pos != line.size end |
#forward_word ⇒ Object
Moves to the next word
99 100 101 |
# File 'lib/coolline/editor.rb', line 99 def forward_word self.pos = word_end_after(pos) + 1 if pos != line.size end |
#insert_string(string) ⇒ Object
Inserts a string at the current point in the line
6 7 8 9 |
# File 'lib/coolline/editor.rb', line 6 def insert_string(string) line.insert pos, string self.pos += string.size end |
#kill_backward_char ⇒ Object
Removes the previous character
123 124 125 126 127 128 |
# File 'lib/coolline/editor.rb', line 123 def kill_backward_char if pos > 0 line[pos - 1] = "" self.pos -= 1 end end |
#kill_backward_word ⇒ Object
Removes the previous word
104 105 106 107 108 109 110 111 |
# File 'lib/coolline/editor.rb', line 104 def kill_backward_word if pos > 0 beg = word_beginning_before(pos) line[beg...pos] = "" self.pos = beg end end |
#kill_beginning_of_line ⇒ Object
Removes everything up to the current character
141 142 143 144 |
# File 'lib/coolline/editor.rb', line 141 def kill_beginning_of_line line[0...pos] = "" self.pos = 0 end |
#kill_current_char ⇒ Object
Removes the current character
131 132 133 |
# File 'lib/coolline/editor.rb', line 131 def kill_current_char line[pos] = "" if pos != line.size end |
#kill_forward_word ⇒ Object
Removes the next word
114 115 116 117 118 119 120 |
# File 'lib/coolline/editor.rb', line 114 def kill_forward_word if pos != line.size ending = word_end_after(pos) line[pos..ending] = "" end end |
#kill_line ⇒ Object
Removes all the characters beyond the current point
136 137 138 |
# File 'lib/coolline/editor.rb', line 136 def kill_line line[pos..-1] = "" end |
#lowercase_word ⇒ Object
Lowercases the current word
225 226 227 228 229 230 |
# File 'lib/coolline/editor.rb', line 225 def lowercase_word if beg = word_beginning_after(pos) and last = word_end_after(pos) line[beg..last] = line[beg..last].downcase self.pos = last + 1 end end |
#non_word_boundary_after(pos) ⇒ Object
23 24 25 26 27 |
# File 'lib/coolline/editor.rb', line 23 def non_word_boundary_after(pos) pos += 1 pos += 1 while pos < line.size and word_boundary? line[pos] pos >= line.size ? nil : pos end |
#non_word_boundary_before(pos) ⇒ Object
29 30 31 32 33 |
# File 'lib/coolline/editor.rb', line 29 def non_word_boundary_before(pos) pos -= 1 pos -= 1 while pos >= 0 and word_boundary? line[pos] pos < 0 ? nil : pos end |
#transpose_chars ⇒ Object
Swaps the previous character with the current one
153 154 155 156 157 158 159 160 161 162 |
# File 'lib/coolline/editor.rb', line 153 def transpose_chars if line.size >= 2 if pos == line.size line[pos - 2], line[pos - 1] = line[pos - 1], line[pos - 2] else line[pos - 1], line[pos] = line[pos], line[pos - 1] self.pos += 1 end end end |
#transpose_words ⇒ Object
Swaps the current word with the previous word, or the two previous words if we’re at the end of the line.
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 |
# File 'lib/coolline/editor.rb', line 166 def transpose_words if !non_word_boundary_after(pos) last = non_word_boundary_before(pos) return unless last last_beg = word_beginning_before(last) return unless word_boundary_before(last_beg) whitespace_beg = word_end_before(last_beg) return unless non_word_boundary_before(whitespace_beg + 1) first_beg = word_beginning_before(last_beg) line[first_beg..last] = line[last_beg..last] + line[(whitespace_beg + 1)...last_beg] + line[first_beg..whitespace_beg] self.pos = last + 1 elsif word_boundary? line[pos] # between two words? return unless non_word_boundary_before(pos) last_beg = word_beginning_after(pos) last_end = word_end_after(pos) first_end = word_end_before(pos) first_beg = word_beginning_before(pos) line[first_beg..last_end] = line[last_beg..last_end] + line[(first_end + 1)...last_beg] + line[first_beg..first_end] self.pos = last_end + 1 else # within a word? return unless non_word_boundary_before(pos) return unless word_boundary_before(pos) last_beg = word_beginning_after(pos - 1) last_end = word_end_after(pos) return unless non_word_boundary_before(last_beg) first_end = word_end_before(last_beg) first_beg = word_beginning_before(last_beg) line[first_beg..last_end] = line[last_beg..last_end] + line[(first_end + 1)...last_beg] + line[first_beg..first_end] self.pos = last_end + 1 end end |
#uppercase_word ⇒ Object
Uppercases the current word
233 234 235 236 237 238 |
# File 'lib/coolline/editor.rb', line 233 def uppercase_word if beg = word_beginning_after(pos) and last = word_end_after(pos) line[beg..last] = line[beg..last].upcase self.pos = last + 1 end end |
#word_beginning_after(pos) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/coolline/editor.rb', line 55 def word_beginning_after(pos) if line[pos] and word_boundary? line[pos] non_word_boundary_after(pos) || line.size else pos end end |
#word_beginning_before(pos) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/coolline/editor.rb', line 45 def word_beginning_before(pos) word_end = word_end_before(pos) if first = word_boundary_before(word_end) first + 1 else 0 end end |
#word_boundary?(string) ⇒ Boolean
This method can be overriden to change characters considered as word boundaries.
244 245 246 |
# File 'lib/coolline/editor.rb', line 244 def word_boundary?(string) string =~ /\A[ \t]+\z/ end |
#word_boundary_after(pos) ⇒ Object
11 12 13 14 15 |
# File 'lib/coolline/editor.rb', line 11 def word_boundary_after(pos) pos += 1 # don't return initial pos pos += 1 until pos >= line.size or word_boundary? line[pos] pos >= line.size ? nil : pos end |
#word_boundary_before(pos) ⇒ Object
17 18 19 20 21 |
# File 'lib/coolline/editor.rb', line 17 def word_boundary_before(pos) pos -= 1 pos -= 1 until pos < 0 or word_boundary? line[pos] pos < 0 ? nil : pos end |
#word_end_after(pos) ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/coolline/editor.rb', line 63 def word_end_after(pos) word_beg = word_beginning_after(pos) if first = word_boundary_after(word_beg) first - 1 else line.size - 1 end end |
#word_end_before(pos) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/coolline/editor.rb', line 35 def word_end_before(pos) pos -= 1 if line[pos] and word_boundary? line[pos] non_word_boundary_before(pos) || 0 else pos end end |