Develop and Download Open Source Software

Browse Subversion Repository

Contents of /buffer_manager.rb

Parent Directory Parent Directory | Revision Log Revision Log


Revision 52 - (show annotations) (download)
Sun Apr 22 19:56:11 2007 UTC (16 years, 11 months ago) by bluedwarf
File size: 2322 byte(s)
New function to save buffer.

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
62 #Append "<n>" behind the buffer name if the buffer name
63 #is already used.
64 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
71 begin
72 new_buffer = Buffer.new(buffer_name, filename)
73 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 end
86 end

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