| 1 |
# buffer.rb: the class definition of Edmaru::Buffer |
| 2 |
# |
| 3 |
# Copyright (C) 2007 Takashi Nakamoto |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License version 2 as |
| 7 |
# published by the Free Software Foundation. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, but |
| 10 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
# General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program; if not, write to the Free Software |
| 16 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 17 |
# 02110-1301 USA. |
| 18 |
# |
| 19 |
|
| 20 |
module Edmaru |
| 21 |
|
| 22 |
#The instance of this class represents one string sequence. |
| 23 |
class Buffer |
| 24 |
|
| 25 |
#Create a new buffer. |
| 26 |
# |
| 27 |
#=== Argument |
| 28 |
#_name_ :: The unique title of this buffer. |
| 29 |
# |
| 30 |
#=== Return |
| 31 |
#An initialized buffer. |
| 32 |
def initialize(name) |
| 33 |
@name = name |
| 34 |
@lines = Array.new |
| 35 |
@lines.push("") |
| 36 |
|
| 37 |
@related_window = Array.new |
| 38 |
end |
| 39 |
|
| 40 |
#Link the specified window to this buffer. |
| 41 |
# |
| 42 |
#=== Argument |
| 43 |
#_window_ :: The window to display this buffer. |
| 44 |
def link(window) |
| 45 |
if @related_window.include?(window) |
| 46 |
return |
| 47 |
end |
| 48 |
|
| 49 |
@related_window.push(window) |
| 50 |
end |
| 51 |
|
| 52 |
#Unlink the specified window from this buffer. |
| 53 |
# |
| 54 |
#=== Argument |
| 55 |
#_window_ :: The window to be unlinked. |
| 56 |
def unlink(window) |
| 57 |
@related_window.delete(window) |
| 58 |
end |
| 59 |
|
| 60 |
#Make linked windows refresh |
| 61 |
def refresh_windows |
| 62 |
@related_window.each{ |window| |
| 63 |
window.refresh |
| 64 |
} |
| 65 |
end |
| 66 |
|
| 67 |
#The name of this buffer. |
| 68 |
# |
| 69 |
#=== Return |
| 70 |
#The name of this buffer. |
| 71 |
def name |
| 72 |
@name |
| 73 |
end |
| 74 |
|
| 75 |
#All lines in this buffer. |
| 76 |
# |
| 77 |
#=== Return |
| 78 |
#An array of string. |
| 79 |
#Each element represents one line of this buffer. |
| 80 |
def lines |
| 81 |
@lines |
| 82 |
end |
| 83 |
|
| 84 |
#Check if the specified position is valid for this buffer. |
| 85 |
# |
| 86 |
#=== Arguments |
| 87 |
# |
| 88 |
#_row_ :: Row position or line number. |
| 89 |
#_col_ :: Column position. |
| 90 |
# |
| 91 |
#=== Return |
| 92 |
#_true_ if the specified position is valid for this buffer. |
| 93 |
# |
| 94 |
#=== Note |
| 95 |
# |
| 96 |
#The position is between two adjacent characters including line |
| 97 |
#feed character. Thus, this method returns true if _col_ is equal |
| 98 |
#to the size of the specified line, but false if _row_ is equal to |
| 99 |
#the size of Edmaru::Buffer#lines. |
| 100 |
def valid_position?(row, col) |
| 101 |
if row < 0 || row >= @lines.size |
| 102 |
# Invalid row position. |
| 103 |
return false |
| 104 |
elsif col < 0 || col > @lines[row].size |
| 105 |
# Invalid column position. |
| 106 |
return false |
| 107 |
else |
| 108 |
return true |
| 109 |
end |
| 110 |
end |
| 111 |
|
| 112 |
#Insert the specified string to the specified position |
| 113 |
# |
| 114 |
#=== Arguments |
| 115 |
# |
| 116 |
#_row_ :: Row position or line number. |
| 117 |
#_col_ :: Column position. |
| 118 |
#_str_ :: A string to be inserted. |
| 119 |
def insert(row, col, str) |
| 120 |
if !valid_position?(row, col) |
| 121 |
raise "Invalid position was specified for 'insert' method." |
| 122 |
end |
| 123 |
|
| 124 |
return if str.empty? # Nothing to be inserted. |
| 125 |
|
| 126 |
if str.include?("\n") |
| 127 |
new_lines = str.split(/\n/, -1) |
| 128 |
new_lines[0] = |
| 129 |
@lines[row][0, col] + new_lines[0] |
| 130 |
new_lines[new_lines.size - 1] << |
| 131 |
@lines[row][col .. -1] |
| 132 |
@lines[row] = new_lines |
| 133 |
@lines.flatten! |
| 134 |
else |
| 135 |
current_line = @lines[row] |
| 136 |
@lines[row] = "" |
| 137 |
if col > 0 |
| 138 |
@lines[row] = current_line[0, col] |
| 139 |
end |
| 140 |
@lines[row] << str |
| 141 |
@lines[row] << current_line[col .. -1] |
| 142 |
end |
| 143 |
|
| 144 |
refresh_windows |
| 145 |
end |
| 146 |
|
| 147 |
#Append the specified string to the end of this buffer. |
| 148 |
# |
| 149 |
#=== Argument |
| 150 |
# |
| 151 |
#_str_ :: A string to be appended. |
| 152 |
def append(str) |
| 153 |
row = @lines.size - 1 |
| 154 |
insert(row, @lines[row].size, str) |
| 155 |
|
| 156 |
refresh_windows |
| 157 |
end |
| 158 |
|
| 159 |
#Delete all characters in this buffer to make it empty. |
| 160 |
def clear |
| 161 |
@lines.clear |
| 162 |
@lines.push("") |
| 163 |
|
| 164 |
refresh_windows |
| 165 |
end |
| 166 |
end |
| 167 |
end |