Develop and Download Open Source Software

Browse Subversion Repository

Contents of /buffer_manager.rb

Parent Directory Parent Directory | Revision Log Revision Log


Revision 51 - (show annotations) (download)
Sun Apr 22 19:17:56 2007 UTC (16 years, 11 months ago) by bluedwarf
File size: 2225 byte(s)
Changes to open the same name files.

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 base_buffer_name = File.basename(filename)
61 buffer_name = base_buffer_name.clone
62 i = 1
63 while(@buffers.include?(buffer_name))
64 i += 1
65 buffer_name = "#{base_buffer_name}<#{i}>"
66 end
67
68 begin
69 new_buffer = Buffer.new(buffer_name)
70 open(filename, "r"){ |f|
71 str = f.read
72 new_buffer.clear
73 new_buffer.append(str)
74 }
75
76 @files[filename] = buffer_name
77 return @buffers[buffer_name] = new_buffer
78 rescue
79 return nil
80 end
81 end
82 end
83 end

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