Develop and Download Open Source Software

Browse Subversion Repository

Contents of /window.rb

Parent Directory Parent Directory | Revision Log Revision Log


Revision 31 - (show annotations) (download)
Thu Apr 12 05:42:00 2007 UTC (16 years, 11 months ago) by bluedwarf
File size: 9146 byte(s)
Scroll function for NCurses mode.

1 # window.rb: the module definition of Edmaru::Window.
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 "cursor.rb"
21 require "modeline.rb"
22
23 module Edmaru
24
25 #Abstract class to implement system specific window.
26 module Window
27
28 #Construct a window instance.
29 #
30 #=== Arguments
31 #_view_ :: The parent view.
32 #_config_ :: An instance of Edmaru::ConfigurationManager.
33 #
34 #=== Warning
35 #This method *MUST* *NOT* be overrided in derived classes.
36 #
37 #=== Return
38 #An initialized instance of Window.
39 def initialize(view, config)
40 @buffer = nil
41 @view = view
42 @config = config
43 @cursor = Cursor.new(0, 0) # logical cursor
44 @modeline = Edmaru::SYSTEM_MODELINE.new(self)
45 @start_line = 0
46
47 init_ui
48 end
49
50 #The system specific initialization for this window.
51 #
52 #=== Warning
53 #This method *SHOULD* be overrided in derived classes.
54 def init_ui
55 end
56
57 #Discard the current buffer and set the specified new buffer.
58 #
59 #=== Warning
60 #This method *MUST* *NOT* be overrided in derived classes.
61 #
62 #=== Argument
63 #_new_buffer_ :: The buffer to be shown in this window.
64 def buffer=(new_buffer)
65 if @buffer != nil
66 @buffer.unlink(self)
67 end
68
69 #Linked buffer to this window.
70 @buffer = new_buffer
71 @buffer.link(self)
72 refresh
73 end
74
75 def buffer
76 @buffer
77 end
78
79 def modeline
80 @modeline
81 end
82
83 #Remove all characters from this buffer.
84 #
85 #=== Warning
86 #This method *MUST* *NOT* be overrided in derived classes.
87 #
88 def clear
89 @cursor.row = @cursor.column = 0
90 @buffer.clear
91 refresh
92 end
93
94 #Insert the specified string to the buffer shown in this window.
95 #
96 #=== Warning
97 #This method *MUST* *NOT* be overrided in derived classes.
98 #
99 #=== Argument
100 #_str_ :: String to be inserted.
101 #_move_cursor_ :: _true_ to cause the cursor move to the end of the
102 #inserted string.
103 def insert_at_cursor(str, move_cursor = true)
104 #Insert to the buffer.
105 @buffer.insert(@cursor.row, @cursor.column, str)
106
107 #Move the cursor to the end of inserted string.
108 cursor_forward(str.size) if move_cursor
109
110 refresh
111 end
112
113 #Move the cursor forward.
114 #
115 #=== Argument
116 #_n_ :: the number of times to forward the cursor.
117 #
118 #=== Warning
119 #This method *MUST* *NOT* be overrided in derived classes.
120 def cursor_forward(n = 1)
121 n.times{
122 if @cursor.column >= @buffer.lines[@cursor.row].size &&
123 @cursor.row == (@buffer.lines.size - 1)
124 #ToDo: Alert that the cursor is at the end of this buffer.
125 @view.beep
126 elsif @cursor.column >= @buffer.lines[@cursor.row].size
127 #Go to the begin of the next line
128 @cursor.row += 1
129 @cursor.column = 0
130 else
131 @cursor.column += 1
132 end
133 }
134
135 refresh_cursor
136 end
137
138 #Move the cursor backward.
139 #
140 #=== Argument
141 #_n_ :: the number of times to backward the cursor.
142 #
143 #=== Warning
144 #This method *MUST* *NOT* be overrided in derived classes.
145 def cursor_backward(n = 1)
146 n.times{
147 if @cursor.column == 0 && @cursor.row == 0
148 #ToDo: Alert that the cursor is at the begin of this buffer.
149 @view.beep
150 elsif @cursor.column == 0
151 @cursor.row -= 1
152 @cursor.column = @buffer.lines[@cursor.row].size
153 else
154 @cursor.column -= 1
155 end
156 }
157
158 refresh_cursor
159 end
160
161 #Move the cursor to the begin of the current line.
162 #
163 #=== Warning
164 #This method *MUST* *NOT* be overrided in derived classes.
165 def cursor_goto_line_head
166 @cursor.column = 0
167 refresh_cursor
168 end
169
170 #Move the cursor to the end of the current line.
171 #
172 #=== Warning
173 #This method *MUST* *NOT* be overrided in derived classes.
174 def cursor_goto_line_tail
175 @cursor.column = buffer.lines[@cursor.row].size
176 refresh_cursor
177 end
178
179 #Move the cursor to the next line.
180 #
181 #=== Warning
182 #This method *MUST* *NOT* be overrided in derived classes.
183 def cursor_goto_next_line
184 if @cursor.row == (@buffer.lines.size - 1)
185 #ToDo: Alert that the cursor is in the last line.
186 @view.beep
187 else
188 @cursor.row += 1
189 if @buffer.lines[@cursor.row].size < @cursor.column
190 @cursor.column = @buffer.lines[@cursor.row].size
191 end
192
193 refresh_cursor
194 end
195 end
196
197 #Move the cursor to the previous line.
198 #
199 #=== Warning
200 #This method *MUST* *NOT* be overrided in derived classes.
201 def cursor_goto_previous_line
202 if @cursor.row == 0
203 #ToDo: Alert that the cursor is in the previous line.
204 @view.beep
205 else
206 @cursor.row -= 1
207 if @buffer.lines[@cursor.row].size < @cursor.column
208 @cursor.column = @buffer.lines[@cursor.row].size
209 end
210
211 refresh_cursor
212 end
213 end
214
215 #Remove a character just in front of the cursor and move the cursor
216 #backward.
217 #
218 #=== Warning
219 #This method *MUST* *NOT* be overrided in derived classes.
220 def backspace
221 if @cursor.column == 0 && @cursor.row == 0
222 #ToDo: Alert that the cursor is at the begin of the current buffer.
223 @view.beep
224 elsif @cursor.column == 0
225 #Concatenate the current lien into the previous line.
226 current_line = @buffer.lines[@cursor.row]
227 prev_line_size = @buffer.lines[@cursor.row - 1].size
228
229 @buffer.lines[@cursor.row - 1] << current_line
230 @buffer.lines.delete_at(@cursor.row)
231
232 @cursor.column = prev_line_size
233 @cursor.row -= 1
234
235 refresh
236 else
237 line = @buffer.lines[@cursor.row]
238 @buffer.lines[@cursor.row] =
239 line[0, @cursor.column - 1] + line[@cursor.column .. -1]
240 @cursor.column -= 1
241 refresh
242 end
243 end
244
245 #Remove a character beneath the cursor.
246 #
247 #=== Warning
248 #This method *MUST* *NOT* be overrided in derived classes.
249 def delete
250 if @cursor.column >= @buffer.lines[@cursor.row].size &&
251 @cursor.row == (@buffer.lines.size - 1)
252 #ToDo: Alert that the cursor is at the end of this buffer.
253 @view.beep
254 elsif @cursor.column >= @buffer.lines[@cursor.row].size
255 #Concatenate the next line behind the current line.
256 @buffer.lines[@cursor.row] << @buffer.lines[@cursor.row + 1]
257 @buffer.lines.delete_at(@cursor.row + 1)
258 refresh
259 else
260 line = @buffer.lines[@cursor.row]
261 @buffer.lines[@cursor.row] =
262 line[0, @cursor.column] + line[@cursor.column + 1 .. -1]
263 refresh
264 end
265 end
266
267 #Remove the string in the current line behind the cursor and push it to
268 #the kill ring.
269 #
270 #=== Warning
271 #This method *MUST* *NOT* be overrided in derived classes.
272 def kill_line
273 if @cursor.column >= @buffer.lines[@cursor.row].size &&
274 @cursor.row == (@buffer.lines.size - 1)
275 #ToDo: Alert that the cursor is at the end of this buffer.
276 @view.beep
277 elsif @cursor.column >= @buffer.lines[@cursor.row].size
278 #Concatenate the next line into the current line.
279 @buffer.lines[@cursor.row] << @buffer.lines[@cursor.row + 1]
280 @buffer.lines.delete_at(@cursor.row + 1)
281 @view.push_to_kill_ring("\n")
282 refresh
283 else
284 line = @buffer.lines[@cursor.row]
285 @view.push_to_kill_ring(line[@cursor.column .. -1])
286 @buffer.lines[@cursor.row] = line[0, @cursor.column]
287 refresh
288 end
289 end
290
291 #Get a certain string from kill ring and insert it at the cursor position.
292 #
293 #=== Warning
294 #This method *MUST* *NOT* be overrided in derived classes.
295 def yank
296 insert_at_cursor(@view.yank_from_kill_ring)
297 end
298
299 #Redraw the cursor.
300 #
301 #=== Warning
302 #This method *SHOULD* be overrided in derived clases.
303 def refresh_cursor
304 end
305
306 #Redraw this window.
307 #
308 #=== Warning
309 #This method *SHOULD* be overrided in derived clases.
310 def refresh
311 end
312
313 #Terminate this view instance.
314 #
315 #=== Warning
316 #This method *MUST* *NOT* be overrided in derived classes.
317 def terminate
318 @modeline.terminate
319
320 terminate_ui
321 end
322
323 #Free system specific resources.
324 #
325 #=== Warning
326 #This method *SHOULD* be overrided in derived classes.
327 def terminate_ui
328 end
329 end
330 end

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