| 17 |
# 02110-1301 USA. |
# 02110-1301 USA. |
| 18 |
# |
# |
| 19 |
|
|
| 20 |
|
require "exception.rb" |
| 21 |
|
|
| 22 |
module Edmaru |
module Edmaru |
| 23 |
|
|
| 24 |
|
class EndOfBufferException < EdmaruException |
| 25 |
|
def initialize |
| 26 |
|
super("End of buffer.") |
| 27 |
|
end |
| 28 |
|
end |
| 29 |
|
|
| 30 |
#The instance of this class represents one string sequence. |
#The instance of this class represents one string sequence. |
| 31 |
class Buffer |
class Buffer |
| 120 |
#Insert the specified string to the specified position |
#Insert the specified string to the specified position |
| 121 |
# |
# |
| 122 |
#=== Arguments |
#=== Arguments |
|
# |
|
| 123 |
#_row_ :: Row position or line number. |
#_row_ :: Row position or line number. |
| 124 |
#_col_ :: Column position. |
#_col_ :: Column position. |
| 125 |
#_str_ :: A string to be inserted. |
#_str_ :: A string to be inserted. |
| 151 |
refresh_windows |
refresh_windows |
| 152 |
end |
end |
| 153 |
|
|
| 154 |
|
#Delete one character at the specified position |
| 155 |
|
# |
| 156 |
|
#=== Arguments |
| 157 |
|
#_row_ :: Row position or line number. |
| 158 |
|
#_col_ :: Column position. |
| 159 |
|
def delete_at(row, col) |
| 160 |
|
if !valid_position?(row, col) |
| 161 |
|
raise "Invalid position was specified for 'delete' method." |
| 162 |
|
end |
| 163 |
|
|
| 164 |
|
if col >= @lines[row].size && |
| 165 |
|
row == (lines.size - 1) |
| 166 |
|
raise EndOfBufferException.new |
| 167 |
|
elsif col >= @lines[row].size |
| 168 |
|
#Concatenate the next line behind the current line. |
| 169 |
|
@lines[row] << @lines[row + 1] |
| 170 |
|
@lines.delete_at(row + 1) |
| 171 |
|
|
| 172 |
|
refresh_windows |
| 173 |
|
else |
| 174 |
|
@lines[row] = |
| 175 |
|
@lines[row][0, col] + @lines[row][col + 1 .. -1] |
| 176 |
|
|
| 177 |
|
refresh_windows |
| 178 |
|
end |
| 179 |
|
end |
| 180 |
|
|
| 181 |
|
#Remove a string in the specified line behind the cursor. |
| 182 |
|
# |
| 183 |
|
#=== Arguments |
| 184 |
|
#_row_ :: Row position or line number. |
| 185 |
|
#_col_ :: Column position. |
| 186 |
|
# |
| 187 |
|
#=== Return |
| 188 |
|
#Deleted string. |
| 189 |
|
def delete_line(row, col) |
| 190 |
|
if !valid_position?(row, col) |
| 191 |
|
raise "Invalid position was specified for 'delete' method." |
| 192 |
|
end |
| 193 |
|
|
| 194 |
|
if col >= @lines[row].size && |
| 195 |
|
row == (lines.size - 1) |
| 196 |
|
raise EndOfBufferException.new |
| 197 |
|
elsif col >= @lines[row].size |
| 198 |
|
#Concatenate the next line behind the current line. |
| 199 |
|
@lines[row] << @lines[row + 1] |
| 200 |
|
@lines.delete_at(row + 1) |
| 201 |
|
refresh_windows |
| 202 |
|
return "\n" |
| 203 |
|
else |
| 204 |
|
ret = @lines[row][col .. -1] |
| 205 |
|
@lines[row] = @lines[row][0, col] |
| 206 |
|
refresh_windows |
| 207 |
|
return ret |
| 208 |
|
end |
| 209 |
|
end |
| 210 |
|
|
| 211 |
#Append the specified string to the end of this buffer. |
#Append the specified string to the end of this buffer. |
| 212 |
# |
# |
| 213 |
#=== Argument |
#=== Argument |