Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /mini_buffer.rb

Parent Directory Parent Directory | Revision Log Revision Log


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

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 bluedwarf 38 @prefix = ""
45 bluedwarf 35
46     @related_window = Array.new
47     end
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 bluedwarf 38
57 bluedwarf 35 #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 bluedwarf 36 str = str.split(/\n/, -1)[0]
100 bluedwarf 35 end
101    
102 bluedwarf 38 if !@prefix.empty? && col < @prefix.size
103     raise ReadOnlyException.new
104     end
105    
106 bluedwarf 36 pre = @line.clone
107     @line = pre[0, col]
108     @line << str
109     @line << pre[col .. -1]
110    
111 bluedwarf 35 refresh_windows
112     end
113    
114 bluedwarf 36 #Delete one character at the specified position
115     #
116     #=== Arguments
117     #_row_ :: Row position or line number. This must be 1 for mini buffer.
118     #_col_ :: Column position.
119     def delete_at(row, col)
120     if !valid_position?(row, col)
121     raise "Invalid position was specified for 'delete' method."
122     end
123 bluedwarf 35
124 bluedwarf 36 if col >= @line.size
125     raise EndOfBufferException.new
126 bluedwarf 38 elsif col < @prefix.size
127     raise ReadOnlyException.new
128 bluedwarf 36 else
129     @line = @line[0, col] + @line[col + 1 .. -1]
130     refresh_windows
131     end
132     end
133 bluedwarf 38
134     #Remove a string in the specified line behind the cursor.
135     #
136     #=== Arguments
137     #_row_ :: Row position or line number.
138     #_col_ :: Column position.
139     #
140     #=== Return
141     #Deleted string.
142     def delete_line(row, col)
143     if !valid_position?(row, col)
144     raise "Invalid position was specified for 'delete' method."
145     end
146    
147     if col >= @line.size
148     raise EndOfBufferException.new
149     else
150     if col < @prefix.size
151     raise ReadOnlyException.new
152     end
153    
154     ret = @line[col .. -1]
155     @line = @line[0, col]
156     refresh_windows
157     return ret
158     end
159     end
160    
161     #Append the specified string to the end of this buffer.
162     #
163     #=== Argument
164     #
165     #_str_ :: A string to be appended.
166     def append(str)
167     insert(0, @line.size, str)
168     end
169 bluedwarf 36
170 bluedwarf 38 #Delete all characters in this buffer to make it empty but prefix
171     #will be remained.
172 bluedwarf 36 def clear
173 bluedwarf 38 @line = @prefix
174 bluedwarf 36
175     refresh_windows
176     end
177 bluedwarf 38
178     #Set prefix for this buffer. Prefix cannot be changed by users.
179     #
180     #=== Argument
181     #_new_prefix_ :: New prefix to be set.
182     def prefix=(new_prefix)
183     @prefix = new_prefix
184    
185     if @line[0, @prefix.size] != @prefix
186     @line = @prefix + @line
187     end
188     end
189    
190     #Get prefix for this buffer.
191     def prefix
192     @prefix
193     end
194    
195     #Get text in this buffer excluding prefix.
196     def text_without_prefix
197     @line[@prefix.size .. -1]
198     end
199 bluedwarf 35 end
200     end

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