Develop and Download Open Source Software

Browse Subversion Repository

Contents of /window.rb

Parent Directory Parent Directory | Revision Log Revision Log


Revision 28 - (show annotations) (download)
Wed Apr 11 05:42:05 2007 UTC (16 years, 11 months ago) by bluedwarf
File size: 9155 byte(s)
Accepts extra commands that start with "C-x"

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

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