| 1 |
bluedwarf |
35 |
# 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 |
|
|
|
| 45 |
|
|
@related_window = Array.new |
| 46 |
|
|
end |
| 47 |
|
|
|
| 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 |
|
|
new_lines = str.split(/\n/, -1) |
| 100 |
|
|
new_lines[0] = |
| 101 |
|
|
@lines[row][0, col] + new_lines[0] |
| 102 |
|
|
new_lines[new_lines.size - 1] << |
| 103 |
|
|
@lines[row][col .. -1] |
| 104 |
|
|
@lines[row] = new_lines |
| 105 |
|
|
@lines.flatten! |
| 106 |
|
|
else |
| 107 |
|
|
current_line = @lines[row] |
| 108 |
|
|
@lines[row] = "" |
| 109 |
|
|
if col > 0 |
| 110 |
|
|
@lines[row] = current_line[0, col] |
| 111 |
|
|
end |
| 112 |
|
|
@lines[row] << str |
| 113 |
|
|
@lines[row] << current_line[col .. -1] |
| 114 |
|
|
end |
| 115 |
|
|
|
| 116 |
|
|
refresh_windows |
| 117 |
|
|
end |
| 118 |
|
|
|
| 119 |
|
|
|
| 120 |
|
|
end |
| 121 |
|
|
end |