Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /buffer.rb

Parent Directory Parent Directory | Revision Log Revision Log


Revision 33 - (hide annotations) (download)
Thu Apr 12 19:31:19 2007 UTC (16 years, 11 months ago) by bluedwarf
File size: 5811 byte(s)
All buffer manipulation codes are moved to Edmaru::Buffer.

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

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