Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /buffer.rb

Parent Directory Parent Directory | Revision Log Revision Log


Revision 52 - (hide annotations) (download)
Sun Apr 22 19:56:11 2007 UTC (16 years, 11 months ago) by bluedwarf
File size: 6182 byte(s)
New function to save 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 bluedwarf 38 #_filename_ :: File name related to this buffer.
38 bluedwarf 4 #
39 bluedwarf 1 #=== Return
40     #An initialized buffer.
41 bluedwarf 38 def initialize(name, filename = "")
42 bluedwarf 1 @name = name
43     @lines = Array.new
44     @lines.push("")
45 bluedwarf 9
46 bluedwarf 38 @filename = filename
47    
48 bluedwarf 9 @related_window = Array.new
49 bluedwarf 1 end
50    
51 bluedwarf 9 #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 bluedwarf 1 #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 bluedwarf 38 #File name related to this buffer.
96     def filename
97     @filename
98     end
99    
100 bluedwarf 1 #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 bluedwarf 32 return if str.empty? # Nothing to be inserted.
140 bluedwarf 1
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 bluedwarf 9
159     refresh_windows
160 bluedwarf 1 end
161    
162 bluedwarf 33 #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 bluedwarf 1 #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 bluedwarf 9
228     refresh_windows
229 bluedwarf 1 end
230    
231     #Delete all characters in this buffer to make it empty.
232     def clear
233     @lines.clear
234     @lines.push("")
235 bluedwarf 9
236     refresh_windows
237 bluedwarf 1 end
238 bluedwarf 52
239     def save
240     if @filename.empty?
241     #ToDo: make user input file name.
242     else
243     f = open(@filename, "w")
244     f.write @lines.join("\n")
245     f.close
246     end
247     end
248 bluedwarf 1 end
249     end

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