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