| 1 |
|
| 2 |
module Edmaru |
| 3 |
|
| 4 |
#The instance of this class represents one string sequence. |
| 5 |
class Buffer |
| 6 |
|
| 7 |
#Create a new buffer. |
| 8 |
# |
| 9 |
#=== Argument |
| 10 |
#_name_ :: The unique title of this buffer. |
| 11 |
# |
| 12 |
#=== Return |
| 13 |
#An initialized buffer. |
| 14 |
def initialize(name) |
| 15 |
@name = name |
| 16 |
@lines = Array.new |
| 17 |
@lines.push("") |
| 18 |
end |
| 19 |
|
| 20 |
#The name of this buffer. |
| 21 |
# |
| 22 |
#=== Return |
| 23 |
#The name of this buffer. |
| 24 |
def name |
| 25 |
@name |
| 26 |
end |
| 27 |
|
| 28 |
#All lines in this buffer. |
| 29 |
# |
| 30 |
#=== Return |
| 31 |
#An array of string. |
| 32 |
#Each element represents one line of this buffer. |
| 33 |
def lines |
| 34 |
@lines |
| 35 |
end |
| 36 |
|
| 37 |
#Check if the specified position is valid for this buffer. |
| 38 |
# |
| 39 |
#=== Arguments |
| 40 |
# |
| 41 |
#_row_ :: Row position or line number. |
| 42 |
#_col_ :: Column position. |
| 43 |
# |
| 44 |
#=== Return |
| 45 |
#_true_ if the specified position is valid for this buffer. |
| 46 |
# |
| 47 |
#=== Note |
| 48 |
# |
| 49 |
#The position is between two adjacent characters including line |
| 50 |
#feed character. Thus, this method returns true if _col_ is equal |
| 51 |
#to the size of the specified line, but false if _row_ is equal to |
| 52 |
#the size of Edmaru::Buffer#lines. |
| 53 |
def valid_position?(row, col) |
| 54 |
if row < 0 || row >= @lines.size |
| 55 |
# Invalid row position. |
| 56 |
return false |
| 57 |
elsif col < 0 || col > @lines[row].size |
| 58 |
# Invalid column position. |
| 59 |
return false |
| 60 |
else |
| 61 |
return true |
| 62 |
end |
| 63 |
end |
| 64 |
|
| 65 |
#Insert the specified string to the specified position |
| 66 |
# |
| 67 |
#=== Arguments |
| 68 |
# |
| 69 |
#_row_ :: Row position or line number. |
| 70 |
#_col_ :: Column position. |
| 71 |
#_str_ :: A string to be inserted. |
| 72 |
def insert(row, col, str) |
| 73 |
if !valid_position?(row, col) |
| 74 |
raise "Invalid position was specified for 'insert' method." |
| 75 |
end |
| 76 |
|
| 77 |
if str.empty? |
| 78 |
# Nothing to be inserted. |
| 79 |
return |
| 80 |
end |
| 81 |
|
| 82 |
if str.include?("\n") |
| 83 |
new_lines = str.split(/\n/, -1) |
| 84 |
new_lines[0] = |
| 85 |
@lines[row][0, col] + new_lines[0] |
| 86 |
new_lines[new_lines.size - 1] << |
| 87 |
@lines[row][col .. -1] |
| 88 |
@lines[row] = new_lines |
| 89 |
@lines.flatten! |
| 90 |
else |
| 91 |
current_line = @lines[row] |
| 92 |
@lines[row] = "" |
| 93 |
if col > 0 |
| 94 |
@lines[row] = current_line[0, col] |
| 95 |
end |
| 96 |
@lines[row] << str |
| 97 |
@lines[row] << current_line[col .. -1] |
| 98 |
end |
| 99 |
end |
| 100 |
|
| 101 |
#Append the specified string to the end of this buffer. |
| 102 |
# |
| 103 |
#=== Argument |
| 104 |
# |
| 105 |
#_str_ :: A string to be appended. |
| 106 |
def append(str) |
| 107 |
row = @lines.size - 1 |
| 108 |
insert(row, @lines[row].size, str) |
| 109 |
end |
| 110 |
|
| 111 |
#Delete all characters in this buffer to make it empty. |
| 112 |
def clear |
| 113 |
@lines.clear |
| 114 |
@lines.push("") |
| 115 |
nil |
| 116 |
end |
| 117 |
end |
| 118 |
end |