| 1 |
bluedwarf |
35 |
# buffer_manager.rb: the class definition of Edmaru::BufferManager |
| 2 |
bluedwarf |
10 |
# |
| 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 |
bluedwarf |
1 |
|
| 20 |
bluedwarf |
12 |
require "buffer.rb" |
| 21 |
bluedwarf |
1 |
|
| 22 |
|
|
module Edmaru |
| 23 |
|
|
|
| 24 |
|
|
#The controller to manage multiple buffers. |
| 25 |
|
|
class BufferManager |
| 26 |
|
|
|
| 27 |
bluedwarf |
2 |
#Create the buffer manager. |
| 28 |
bluedwarf |
1 |
# |
| 29 |
|
|
#=== Return |
| 30 |
|
|
#An initialized manager. |
| 31 |
|
|
def initialize |
| 32 |
|
|
@buffers = Hash.new |
| 33 |
bluedwarf |
38 |
@files = Hash.new # Buffer names related to specific files. |
| 34 |
bluedwarf |
1 |
end |
| 35 |
|
|
|
| 36 |
bluedwarf |
35 |
#Return the specified buffer. |
| 37 |
bluedwarf |
1 |
# |
| 38 |
bluedwarf |
35 |
#=== Arguments |
| 39 |
|
|
#_name_ :: Name of buffer. |
| 40 |
|
|
#_type_ :: Buffer type. |
| 41 |
bluedwarf |
1 |
# |
| 42 |
|
|
#=== Return |
| 43 |
bluedwarf |
35 |
#The buffer instance whose name is the specified _name_. |
| 44 |
|
|
def get(name, type = Buffer) |
| 45 |
|
|
if !@buffers.include?(name) |
| 46 |
|
|
@buffers[name] = type.new(name) |
| 47 |
|
|
elsif @buffers[name].class != type |
| 48 |
|
|
#ToDo: What should I do? |
| 49 |
bluedwarf |
1 |
end |
| 50 |
bluedwarf |
35 |
|
| 51 |
|
|
return @buffers[name] |
| 52 |
bluedwarf |
1 |
end |
| 53 |
|
|
|
| 54 |
bluedwarf |
38 |
def open_file(filename) |
| 55 |
|
|
#If the specified file is already loaded... |
| 56 |
|
|
if @files.include?(filename) |
| 57 |
|
|
return @buffers[@files[filename]] |
| 58 |
|
|
end |
| 59 |
|
|
|
| 60 |
bluedwarf |
51 |
base_buffer_name = File.basename(filename) |
| 61 |
bluedwarf |
52 |
|
| 62 |
|
|
#Append "<n>" behind the buffer name if the buffer name |
| 63 |
|
|
#is already used. |
| 64 |
bluedwarf |
51 |
buffer_name = base_buffer_name.clone |
| 65 |
|
|
i = 1 |
| 66 |
|
|
while(@buffers.include?(buffer_name)) |
| 67 |
|
|
i += 1 |
| 68 |
|
|
buffer_name = "#{base_buffer_name}<#{i}>" |
| 69 |
|
|
end |
| 70 |
bluedwarf |
38 |
|
| 71 |
|
|
begin |
| 72 |
bluedwarf |
52 |
new_buffer = Buffer.new(buffer_name, filename) |
| 73 |
bluedwarf |
38 |
open(filename, "r"){ |f| |
| 74 |
|
|
str = f.read |
| 75 |
|
|
new_buffer.clear |
| 76 |
|
|
new_buffer.append(str) |
| 77 |
|
|
} |
| 78 |
|
|
|
| 79 |
|
|
@files[filename] = buffer_name |
| 80 |
|
|
return @buffers[buffer_name] = new_buffer |
| 81 |
|
|
rescue |
| 82 |
|
|
return nil |
| 83 |
|
|
end |
| 84 |
|
|
end |
| 85 |
bluedwarf |
1 |
end |
| 86 |
|
|
end |