Develop and Download Open Source Software

Browse Subversion Repository

Contents of /window.rb

Parent Directory Parent Directory | Revision Log Revision Log


Revision 35 - (show annotations) (download)
Sat Apr 14 00:06:24 2007 UTC (16 years, 11 months ago) by bluedwarf
File size: 7980 byte(s)
Find file 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 @cursor.row = @cursor.column = 0
73 refresh
74 end
75
76 def buffer
77 @buffer
78 end
79
80 def modeline
81 @modeline
82 end
83
84 #Remove all characters from this buffer.
85 #
86 #=== Warning
87 #This method *MUST* *NOT* be overrided in derived classes.
88 #
89 def clear
90 @cursor.row = @cursor.column = 0
91 @buffer.clear
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 end
110
111 #Move the cursor forward.
112 #
113 #=== Argument
114 #_n_ :: the number of times to forward the cursor.
115 #
116 #=== Warning
117 #This method *MUST* *NOT* be overrided in derived classes.
118 def cursor_forward(n = 1)
119 n.times{
120 if @cursor.column >= @buffer.lines[@cursor.row].size &&
121 @cursor.row == (@buffer.lines.size - 1)
122 #ToDo: Alert that the cursor is at the end of this buffer.
123 @view.beep
124 elsif @cursor.column >= @buffer.lines[@cursor.row].size
125 #Go to the begin of the next line
126 @cursor.row += 1
127 @cursor.column = 0
128 else
129 @cursor.column += 1
130 end
131 }
132
133 refresh_cursor
134 end
135
136 #Move the cursor backward.
137 #
138 #=== Argument
139 #_n_ :: the number of times to backward the cursor.
140 #
141 #=== Warning
142 #This method *MUST* *NOT* be overrided in derived classes.
143 def cursor_backward(n = 1)
144 n.times{
145 if @cursor.column == 0 && @cursor.row == 0
146 #ToDo: Alert that the cursor is at the begin of this buffer.
147 @view.beep
148 elsif @cursor.column == 0
149 @cursor.row -= 1
150 @cursor.column = @buffer.lines[@cursor.row].size
151 else
152 @cursor.column -= 1
153 end
154 }
155
156 refresh_cursor
157 end
158
159 #Move the cursor to the begin of the current line.
160 #
161 #=== Warning
162 #This method *MUST* *NOT* be overrided in derived classes.
163 def cursor_goto_line_head
164 @cursor.column = 0
165 refresh_cursor
166 end
167
168 #Move the cursor to the end of the current line.
169 #
170 #=== Warning
171 #This method *MUST* *NOT* be overrided in derived classes.
172 def cursor_goto_line_tail
173 @cursor.column = buffer.lines[@cursor.row].size
174 refresh_cursor
175 end
176
177 #Move the cursor to the next line.
178 #
179 #=== Warning
180 #This method *MUST* *NOT* be overrided in derived classes.
181 def cursor_goto_next_line
182 if @cursor.row == (@buffer.lines.size - 1)
183 @view.mini_window.show_alert("The last line")
184 @view.beep
185 else
186 @cursor.row += 1
187 if @buffer.lines[@cursor.row].size < @cursor.column
188 @cursor.column = @buffer.lines[@cursor.row].size
189 end
190
191 refresh_cursor
192 end
193 end
194
195 #Move the cursor to the previous line.
196 #
197 #=== Warning
198 #This method *MUST* *NOT* be overrided in derived classes.
199 def cursor_goto_previous_line
200 if @cursor.row == 0
201 @view.mini_window.show_alert("The first line")
202 @view.beep
203 else
204 @cursor.row -= 1
205 if @buffer.lines[@cursor.row].size < @cursor.column
206 @cursor.column = @buffer.lines[@cursor.row].size
207 end
208
209 refresh_cursor
210 end
211 end
212
213 #Remove a character just in front of the cursor and move the cursor
214 #backward.
215 #
216 #=== Warning
217 #This method *MUST* *NOT* be overrided in derived classes.
218 def backspace
219 if @cursor.column == 0 && @cursor.row == 0
220 #ToDo: Alert that the cursor is at the begin of the current buffer.
221 @view.mini_window.show_alert("Beginning of buffer")
222 @view.beep
223 elsif @cursor.column == 0 # && @cursor.row != 0
224 #Concatenate the current lien into the previous line.
225 @cursor.row -= 1
226 @cursor.column = @buffer.lines[@cursor.row].size
227 delete_at_cursor
228 else
229 @cursor.column -= 1
230 delete_at_cursor
231 end
232 end
233
234 #Remove a character beneath the cursor.
235 #
236 #=== Warning
237 #This method *MUST* *NOT* be overrided in derived classes.
238 def delete_at_cursor
239 begin
240 @buffer.delete_at(@cursor.row, @cursor.column)
241 rescue EndOfBufferException => e
242 @view.mini_window.show_alert(e.to_s)
243 @view.beep
244 end
245 end
246
247 #Remove the string in the current line behind the cursor and push it to
248 #the kill ring.
249 #
250 #=== Warning
251 #This method *MUST* *NOT* be overrided in derived classes.
252 def kill_line
253 begin
254 @view.push_to_kill_ring(@buffer.delete_line(@cursor.row, @cursor.column))
255 rescue EndOfBufferException => e
256 @view.mini_window.show_alert(e.to_s)
257 @view.beep
258 end
259 end
260
261 #Get a certain string from kill ring and insert it at the cursor position.
262 #
263 #=== Warning
264 #This method *MUST* *NOT* be overrided in derived classes.
265 def yank
266 insert_at_cursor(@view.yank_from_kill_ring)
267 end
268
269 #Redraw the cursor.
270 #
271 #=== Warning
272 #This method *SHOULD* be overrided in derived clases.
273 def refresh_cursor
274 end
275
276 #Redraw this window.
277 #
278 #=== Warning
279 #This method *SHOULD* be overrided in derived clases.
280 def refresh
281 end
282
283 #Terminate this view instance.
284 #
285 #=== Warning
286 #This method *MUST* *NOT* be overrided in derived classes.
287 def terminate
288 @modeline.terminate
289
290 terminate_ui
291 end
292
293 #Free system specific resources.
294 #
295 #=== Warning
296 #This method *SHOULD* be overrided in derived classes.
297 def terminate_ui
298 end
299 end
300 end

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