Develop and Download Open Source Software

Browse Subversion Repository

Contents of /buffer.rb

Parent Directory Parent Directory | Revision Log Revision Log


Revision 38 - (show annotations) (download)
Wed Apr 18 11:20:22 2007 UTC (16 years, 11 months ago) by bluedwarf
File size: 5988 byte(s)
New function to open a file.

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 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.
31 class Buffer
32
33 #Create a new buffer.
34 #
35 #=== Argument
36 #_name_ :: The unique title of this buffer.
37 #_filename_ :: File name related to this buffer.
38 #
39 #=== Return
40 #An initialized buffer.
41 def initialize(name, filename = "")
42 @name = name
43 @lines = Array.new
44 @lines.push("")
45
46 @filename = filename
47
48 @related_window = Array.new
49 end
50
51 #Link the specified window to this buffer.
52 #
53 #=== Argument
54 #_window_ :: The window to display this buffer.
55 def link(window)
56 if @related_window.include?(window)
57 return
58 end
59
60 @related_window.push(window)
61 end
62
63 #Unlink the specified window from this buffer.
64 #
65 #=== Argument
66 #_window_ :: The window to be unlinked.
67 def unlink(window)
68 @related_window.delete(window)
69 end
70
71 #Make linked windows refresh
72 def refresh_windows
73 @related_window.each{ |window|
74 window.refresh
75 }
76 end
77
78 #The name of this buffer.
79 #
80 #=== Return
81 #The name of this buffer.
82 def name
83 @name
84 end
85
86 #All lines in this buffer.
87 #
88 #=== Return
89 #An array of string.
90 #Each element represents one line of this buffer.
91 def lines
92 @lines
93 end
94
95 #File name related to this buffer.
96 def filename
97 @filename
98 end
99
100 #Check if the specified position is valid for this buffer.
101 #
102 #=== Arguments
103 #
104 #_row_ :: Row position or line number.
105 #_col_ :: Column position.
106 #
107 #=== Return
108 #_true_ if the specified position is valid for this buffer.
109 #
110 #=== Note
111 #
112 #The position is between two adjacent characters including line
113 #feed character. Thus, this method returns true if _col_ is equal
114 #to the size of the specified line, but false if _row_ is equal to
115 #the size of Edmaru::Buffer#lines.
116 def valid_position?(row, col)
117 if row < 0 || row >= @lines.size
118 # Invalid row position.
119 return false
120 elsif col < 0 || col > @lines[row].size
121 # Invalid column position.
122 return false
123 else
124 return true
125 end
126 end
127
128 #Insert the specified string to the specified position
129 #
130 #=== Arguments
131 #_row_ :: Row position or line number.
132 #_col_ :: Column position.
133 #_str_ :: A string to be inserted.
134 def insert(row, col, str)
135 if !valid_position?(row, col)
136 raise "Invalid position was specified for 'insert' method."
137 end
138
139 return if str.empty? # Nothing to be inserted.
140
141 if str.include?("\n")
142 new_lines = str.split(/\n/, -1)
143 new_lines[0] =
144 @lines[row][0, col] + new_lines[0]
145 new_lines[new_lines.size - 1] <<
146 @lines[row][col .. -1]
147 @lines[row] = new_lines
148 @lines.flatten!
149 else
150 current_line = @lines[row]
151 @lines[row] = ""
152 if col > 0
153 @lines[row] = current_line[0, col]
154 end
155 @lines[row] << str
156 @lines[row] << current_line[col .. -1]
157 end
158
159 refresh_windows
160 end
161
162 #Delete one character at the specified position
163 #
164 #=== Arguments
165 #_row_ :: Row position or line number.
166 #_col_ :: Column position.
167 def delete_at(row, col)
168 if !valid_position?(row, col)
169 raise "Invalid position was specified for 'delete' method."
170 end
171
172 if col >= @lines[row].size &&
173 row == (lines.size - 1)
174 raise EndOfBufferException.new
175 elsif col >= @lines[row].size
176 #Concatenate the next line behind the current line.
177 @lines[row] << @lines[row + 1]
178 @lines.delete_at(row + 1)
179
180 refresh_windows
181 else
182 @lines[row] =
183 @lines[row][0, col] + @lines[row][col + 1 .. -1]
184
185 refresh_windows
186 end
187 end
188
189 #Remove a string in the specified line behind the cursor.
190 #
191 #=== Arguments
192 #_row_ :: Row position or line number.
193 #_col_ :: Column position.
194 #
195 #=== Return
196 #Deleted string.
197 def delete_line(row, col)
198 if !valid_position?(row, col)
199 raise "Invalid position was specified for 'delete' method."
200 end
201
202 if col >= @lines[row].size &&
203 row == (lines.size - 1)
204 raise EndOfBufferException.new
205 elsif col >= @lines[row].size
206 #Concatenate the next line behind the current line.
207 @lines[row] << @lines[row + 1]
208 @lines.delete_at(row + 1)
209 refresh_windows
210 return "\n"
211 else
212 ret = @lines[row][col .. -1]
213 @lines[row] = @lines[row][0, col]
214 refresh_windows
215 return ret
216 end
217 end
218
219 #Append the specified string to the end of this buffer.
220 #
221 #=== Argument
222 #
223 #_str_ :: A string to be appended.
224 def append(str)
225 row = @lines.size - 1
226 insert(row, @lines[row].size, str)
227
228 refresh_windows
229 end
230
231 #Delete all characters in this buffer to make it empty.
232 def clear
233 @lines.clear
234 @lines.push("")
235
236 refresh_windows
237 end
238 end
239 end

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26