Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /buffer.rb

Parent Directory Parent Directory | Revision Log Revision Log


Revision 10 - (hide annotations) (download)
Thu Apr 5 11:53:35 2007 UTC (16 years, 11 months ago) by bluedwarf
File size: 4159 byte(s)
Added Copyright notice.

1 bluedwarf 10 #
2     # Edmaru: A scalable editor implemented by Ruby.
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     module Edmaru
21    
22     #The instance of this class represents one string sequence.
23     class Buffer
24    
25     #Create a new buffer.
26     #
27 bluedwarf 4 #=== Argument
28     #_name_ :: The unique title of this buffer.
29     #
30 bluedwarf 1 #=== Return
31     #An initialized buffer.
32     def initialize(name)
33     @name = name
34     @lines = Array.new
35     @lines.push("")
36 bluedwarf 9
37     @related_window = Array.new
38 bluedwarf 1 end
39    
40 bluedwarf 9 #Link the specified window to this buffer.
41     #
42     #=== Argument
43     #_window_ :: The window to display this buffer.
44     def link(window)
45     if @related_window.include?(window)
46     return
47     end
48    
49     @related_window.push(window)
50     end
51    
52     #Unlink the specified window from this buffer.
53     #
54     #=== Argument
55     #_window_ :: The window to be unlinked.
56     def unlink(window)
57     @related_window.delete(window)
58     end
59    
60     #Make linked windows refresh
61     def refresh_windows
62     @related_window.each{ |window|
63     window.refresh
64     }
65     end
66    
67 bluedwarf 1 #The name of this buffer.
68     #
69     #=== Return
70     #The name of this buffer.
71     def name
72     @name
73     end
74    
75     #All lines in this buffer.
76     #
77     #=== Return
78     #An array of string.
79     #Each element represents one line of this buffer.
80     def lines
81     @lines
82     end
83    
84     #Check if the specified position is valid for this buffer.
85     #
86     #=== Arguments
87     #
88     #_row_ :: Row position or line number.
89     #_col_ :: Column position.
90     #
91     #=== Return
92     #_true_ if the specified position is valid for this buffer.
93     #
94     #=== Note
95     #
96     #The position is between two adjacent characters including line
97     #feed character. Thus, this method returns true if _col_ is equal
98     #to the size of the specified line, but false if _row_ is equal to
99     #the size of Edmaru::Buffer#lines.
100     def valid_position?(row, col)
101     if row < 0 || row >= @lines.size
102     # Invalid row position.
103     return false
104     elsif col < 0 || col > @lines[row].size
105     # Invalid column position.
106     return false
107     else
108     return true
109     end
110     end
111    
112     #Insert the specified string to the specified position
113     #
114     #=== Arguments
115     #
116     #_row_ :: Row position or line number.
117     #_col_ :: Column position.
118     #_str_ :: A string to be inserted.
119     def insert(row, col, str)
120     if !valid_position?(row, col)
121     raise "Invalid position was specified for 'insert' method."
122     end
123    
124     if str.empty?
125     # Nothing to be inserted.
126     return
127     end
128    
129     if str.include?("\n")
130     new_lines = str.split(/\n/, -1)
131     new_lines[0] =
132     @lines[row][0, col] + new_lines[0]
133     new_lines[new_lines.size - 1] <<
134     @lines[row][col .. -1]
135     @lines[row] = new_lines
136     @lines.flatten!
137     else
138     current_line = @lines[row]
139     @lines[row] = ""
140     if col > 0
141     @lines[row] = current_line[0, col]
142     end
143     @lines[row] << str
144     @lines[row] << current_line[col .. -1]
145     end
146 bluedwarf 9
147     refresh_windows
148 bluedwarf 1 end
149    
150     #Append the specified string to the end of this buffer.
151     #
152     #=== Argument
153     #
154     #_str_ :: A string to be appended.
155     def append(str)
156     row = @lines.size - 1
157     insert(row, @lines[row].size, str)
158 bluedwarf 9
159     refresh_windows
160 bluedwarf 1 end
161    
162     #Delete all characters in this buffer to make it empty.
163     def clear
164     @lines.clear
165     @lines.push("")
166     nil
167 bluedwarf 9
168     refresh_windows
169 bluedwarf 1 end
170     end
171     end

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