Develop and Download Open Source Software

Browse Subversion Repository

Contents of /window.rb

Parent Directory Parent Directory | Revision Log Revision Log


Revision 36 - (show annotations) (download)
Sat Apr 14 17:29:51 2007 UTC (16 years, 11 months ago) by bluedwarf
File size: 8130 byte(s)
Improvement on mini buffer.

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.mini_window.show_alert("End of buffer")
124 @view.beep
125 break
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.mini_window.show_alert("Beginning of buffer")
150 @view.beep
151 break
152 elsif @cursor.column == 0
153 @cursor.row -= 1
154 @cursor.column = @buffer.lines[@cursor.row].size
155 else
156 @cursor.column -= 1
157 end
158 }
159
160 refresh_cursor
161 end
162
163 #Move the cursor to the begin of the current line.
164 #
165 #=== Warning
166 #This method *MUST* *NOT* be overrided in derived classes.
167 def cursor_goto_line_head
168 @cursor.column = 0
169 refresh_cursor
170 end
171
172 #Move the cursor to the end of the current line.
173 #
174 #=== Warning
175 #This method *MUST* *NOT* be overrided in derived classes.
176 def cursor_goto_line_tail
177 @cursor.column = buffer.lines[@cursor.row].size
178 refresh_cursor
179 end
180
181 #Move the cursor to the next line.
182 #
183 #=== Warning
184 #This method *MUST* *NOT* be overrided in derived classes.
185 def cursor_goto_next_line
186 if @cursor.row == (@buffer.lines.size - 1)
187 @view.mini_window.show_alert("The last line")
188 @view.beep
189 else
190 @cursor.row += 1
191 if @buffer.lines[@cursor.row].size < @cursor.column
192 @cursor.column = @buffer.lines[@cursor.row].size
193 end
194
195 refresh_cursor
196 end
197 end
198
199 #Move the cursor to the previous line.
200 #
201 #=== Warning
202 #This method *MUST* *NOT* be overrided in derived classes.
203 def cursor_goto_previous_line
204 if @cursor.row == 0
205 @view.mini_window.show_alert("The first line")
206 @view.beep
207 else
208 @cursor.row -= 1
209 if @buffer.lines[@cursor.row].size < @cursor.column
210 @cursor.column = @buffer.lines[@cursor.row].size
211 end
212
213 refresh_cursor
214 end
215 end
216
217 #Remove a character just in front of the cursor and move the cursor
218 #backward.
219 #
220 #=== Warning
221 #This method *MUST* *NOT* be overrided in derived classes.
222 def backspace
223 if @cursor.column == 0 && @cursor.row == 0
224 #ToDo: Alert that the cursor is at the begin of the current buffer.
225 @view.mini_window.show_alert("Beginning of buffer")
226 @view.beep
227 elsif @cursor.column == 0 # && @cursor.row != 0
228 #Concatenate the current lien into the previous line.
229 @cursor.row -= 1
230 @cursor.column = @buffer.lines[@cursor.row].size
231 delete_at_cursor
232 else
233 @cursor.column -= 1
234 delete_at_cursor
235 end
236 end
237
238 #Remove a character beneath the cursor.
239 #
240 #=== Warning
241 #This method *MUST* *NOT* be overrided in derived classes.
242 def delete_at_cursor
243 begin
244 @buffer.delete_at(@cursor.row, @cursor.column)
245 rescue EndOfBufferException => e
246 @view.mini_window.show_alert(e.to_s)
247 @view.beep
248 end
249 end
250
251 #Remove the string in the current line behind the cursor and push it to
252 #the kill ring.
253 #
254 #=== Warning
255 #This method *MUST* *NOT* be overrided in derived classes.
256 def kill_line
257 begin
258 @view.push_to_kill_ring(@buffer.delete_line(@cursor.row, @cursor.column))
259 rescue EndOfBufferException => e
260 @view.mini_window.show_alert(e.to_s)
261 @view.beep
262 end
263 end
264
265 #Get a certain string from kill ring and insert it at the cursor position.
266 #
267 #=== Warning
268 #This method *MUST* *NOT* be overrided in derived classes.
269 def yank
270 insert_at_cursor(@view.yank_from_kill_ring)
271 end
272
273 #Redraw the cursor.
274 #
275 #=== Warning
276 #This method *SHOULD* be overrided in derived clases.
277 def refresh_cursor
278 end
279
280 #Redraw this window.
281 #
282 #=== Warning
283 #This method *SHOULD* be overrided in derived clases.
284 def refresh
285 end
286
287 #Terminate this view instance.
288 #
289 #=== Warning
290 #This method *MUST* *NOT* be overrided in derived classes.
291 def terminate
292 @modeline.terminate
293
294 terminate_ui
295 end
296
297 #Free system specific resources.
298 #
299 #=== Warning
300 #This method *SHOULD* be overrided in derived classes.
301 def terminate_ui
302 end
303 end
304 end

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