| 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 |
require "exception.rb" |
| 21 |
|
| 22 |
module Edmaru |
| 23 |
|
| 24 |
class ReadOnlyException < EdmaruException |
| 25 |
def initialize |
| 26 |
super("Text is read-only.") |
| 27 |
end |
| 28 |
end |
| 29 |
|
| 30 |
#The instance of this class represents one string sequence that doesn't |
| 31 |
#contain line feeds. |
| 32 |
class MiniBuffer < Buffer |
| 33 |
|
| 34 |
#Create a new buffer. |
| 35 |
# |
| 36 |
#=== Argument |
| 37 |
#_name_ :: The unique title of this buffer. |
| 38 |
# |
| 39 |
#=== Return |
| 40 |
#An initialized buffer. |
| 41 |
def initialize(name) |
| 42 |
@name = name |
| 43 |
@line = String.new |
| 44 |
@prefix = "" |
| 45 |
|
| 46 |
@related_window = Array.new |
| 47 |
end |
| 48 |
|
| 49 |
#All lines in this buffer. |
| 50 |
# |
| 51 |
#=== Return |
| 52 |
#An array of string including only one element (line). |
| 53 |
def lines |
| 54 |
[@line] |
| 55 |
end |
| 56 |
|
| 57 |
#Check if the specified position is valid for this buffer. |
| 58 |
# |
| 59 |
#=== Arguments |
| 60 |
# |
| 61 |
#_row_ :: Row position or line number. |
| 62 |
#_col_ :: Column position. |
| 63 |
# |
| 64 |
#=== Return |
| 65 |
#_true_ if the specified position is valid for this buffer. |
| 66 |
# |
| 67 |
#=== Note |
| 68 |
# |
| 69 |
#The position is between two adjacent characters including line |
| 70 |
#feed character. Thus, this method returns true if _col_ is equal |
| 71 |
#to the size of the specified line, but false if _row_ is equal to |
| 72 |
#the size of Edmaru::Buffer#lines. |
| 73 |
def valid_position?(row, col) |
| 74 |
if row != 0 |
| 75 |
# Invalid row position. |
| 76 |
return false |
| 77 |
elsif col < 0 || col > @line.size |
| 78 |
# Invalid column position. |
| 79 |
return false |
| 80 |
else |
| 81 |
return true |
| 82 |
end |
| 83 |
end |
| 84 |
|
| 85 |
#Insert the specified string to the specified position |
| 86 |
# |
| 87 |
#=== Arguments |
| 88 |
#_row_ :: Row position or line number. |
| 89 |
#_col_ :: Column position. |
| 90 |
#_str_ :: A string to be inserted. |
| 91 |
def insert(row, col, str) |
| 92 |
if !valid_position?(row, col) |
| 93 |
raise "Invalid position was specified for 'insert' method." |
| 94 |
end |
| 95 |
|
| 96 |
return if str.empty? # Nothing to be inserted. |
| 97 |
|
| 98 |
if str.include?("\n") |
| 99 |
str = str.split(/\n/, -1)[0] |
| 100 |
end |
| 101 |
|
| 102 |
if !@prefix.empty? && col < @prefix.size |
| 103 |
raise ReadOnlyException.new |
| 104 |
end |
| 105 |
|
| 106 |
pre = @line.clone |
| 107 |
@line = pre[0, col] |
| 108 |
@line << str |
| 109 |
@line << pre[col .. -1] |
| 110 |
|
| 111 |
refresh_windows |
| 112 |
end |
| 113 |
|
| 114 |
#Delete one character at the specified position |
| 115 |
# |
| 116 |
#=== Arguments |
| 117 |
#_row_ :: Row position or line number. This must be 1 for mini buffer. |
| 118 |
#_col_ :: Column position. |
| 119 |
def delete_at(row, col) |
| 120 |
if !valid_position?(row, col) |
| 121 |
raise "Invalid position was specified for 'delete' method." |
| 122 |
end |
| 123 |
|
| 124 |
if col >= @line.size |
| 125 |
raise EndOfBufferException.new |
| 126 |
elsif col < @prefix.size |
| 127 |
raise ReadOnlyException.new |
| 128 |
else |
| 129 |
@line = @line[0, col] + @line[col + 1 .. -1] |
| 130 |
refresh_windows |
| 131 |
end |
| 132 |
end |
| 133 |
|
| 134 |
#Remove a string in the specified line behind the cursor. |
| 135 |
# |
| 136 |
#=== Arguments |
| 137 |
#_row_ :: Row position or line number. |
| 138 |
#_col_ :: Column position. |
| 139 |
# |
| 140 |
#=== Return |
| 141 |
#Deleted string. |
| 142 |
def delete_line(row, col) |
| 143 |
if !valid_position?(row, col) |
| 144 |
raise "Invalid position was specified for 'delete' method." |
| 145 |
end |
| 146 |
|
| 147 |
if col >= @line.size |
| 148 |
raise EndOfBufferException.new |
| 149 |
else |
| 150 |
if col < @prefix.size |
| 151 |
raise ReadOnlyException.new |
| 152 |
end |
| 153 |
|
| 154 |
ret = @line[col .. -1] |
| 155 |
@line = @line[0, col] |
| 156 |
refresh_windows |
| 157 |
return ret |
| 158 |
end |
| 159 |
end |
| 160 |
|
| 161 |
#Append the specified string to the end of this buffer. |
| 162 |
# |
| 163 |
#=== Argument |
| 164 |
# |
| 165 |
#_str_ :: A string to be appended. |
| 166 |
def append(str) |
| 167 |
insert(0, @line.size, str) |
| 168 |
end |
| 169 |
|
| 170 |
#Delete all characters in this buffer to make it empty but prefix |
| 171 |
#will be remained. |
| 172 |
def clear |
| 173 |
@line = @prefix |
| 174 |
|
| 175 |
refresh_windows |
| 176 |
end |
| 177 |
|
| 178 |
#Set prefix for this buffer. Prefix cannot be changed by users. |
| 179 |
# |
| 180 |
#=== Argument |
| 181 |
#_new_prefix_ :: New prefix to be set. |
| 182 |
def prefix=(new_prefix) |
| 183 |
@prefix = new_prefix |
| 184 |
|
| 185 |
if @line[0, @prefix.size] != @prefix |
| 186 |
@line = @prefix + @line |
| 187 |
end |
| 188 |
end |
| 189 |
|
| 190 |
#Get prefix for this buffer. |
| 191 |
def prefix |
| 192 |
@prefix |
| 193 |
end |
| 194 |
|
| 195 |
#Get text in this buffer excluding prefix. |
| 196 |
def text_without_prefix |
| 197 |
@line[@prefix.size .. -1] |
| 198 |
end |
| 199 |
end |
| 200 |
end |