| 1 |
# buffer_manager.rb: the class definition of Edmaru::BufferManager |
| 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 "buffer.rb" |
| 21 |
|
| 22 |
module Edmaru |
| 23 |
|
| 24 |
#The controller to manage multiple buffers. |
| 25 |
class BufferManager |
| 26 |
|
| 27 |
#Create the buffer manager. |
| 28 |
# |
| 29 |
#=== Return |
| 30 |
#An initialized manager. |
| 31 |
def initialize |
| 32 |
@buffers = Hash.new |
| 33 |
@files = Hash.new # Buffer names related to specific files. |
| 34 |
end |
| 35 |
|
| 36 |
#Return the specified buffer. |
| 37 |
# |
| 38 |
#=== Arguments |
| 39 |
#_name_ :: Name of buffer. |
| 40 |
#_type_ :: Buffer type. |
| 41 |
# |
| 42 |
#=== Return |
| 43 |
#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 |
end |
| 50 |
|
| 51 |
return @buffers[name] |
| 52 |
end |
| 53 |
|
| 54 |
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 |
buffer_name = File.basename(filename) |
| 61 |
|
| 62 |
begin |
| 63 |
new_buffer = Buffer.new(buffer_name) |
| 64 |
open(filename, "r"){ |f| |
| 65 |
str = f.read |
| 66 |
new_buffer.clear |
| 67 |
new_buffer.append(str) |
| 68 |
} |
| 69 |
|
| 70 |
@files[filename] = buffer_name |
| 71 |
return @buffers[buffer_name] = new_buffer |
| 72 |
rescue |
| 73 |
return nil |
| 74 |
end |
| 75 |
end |
| 76 |
end |
| 77 |
end |