Develop and Download Open Source Software

Browse Subversion Repository

Contents of /buffer.rb

Parent Directory Parent Directory | Revision Log Revision Log


Revision 9 - (show annotations) (download)
Thu Apr 5 11:26:23 2007 UTC (16 years, 11 months ago) by bluedwarf
File size: 3424 byte(s)
Association buffer with window.

1
2 module Edmaru
3
4 #The instance of this class represents one string sequence.
5 class Buffer
6
7 #Create a new buffer.
8 #
9 #=== Argument
10 #_name_ :: The unique title of this buffer.
11 #
12 #=== Return
13 #An initialized buffer.
14 def initialize(name)
15 @name = name
16 @lines = Array.new
17 @lines.push("")
18
19 @related_window = Array.new
20 end
21
22 #Link the specified window to this buffer.
23 #
24 #=== Argument
25 #_window_ :: The window to display this buffer.
26 def link(window)
27 if @related_window.include?(window)
28 return
29 end
30
31 @related_window.push(window)
32 end
33
34 #Unlink the specified window from this buffer.
35 #
36 #=== Argument
37 #_window_ :: The window to be unlinked.
38 def unlink(window)
39 @related_window.delete(window)
40 end
41
42 #Make linked windows refresh
43 def refresh_windows
44 @related_window.each{ |window|
45 window.refresh
46 }
47 end
48
49 #The name of this buffer.
50 #
51 #=== Return
52 #The name of this buffer.
53 def name
54 @name
55 end
56
57 #All lines in this buffer.
58 #
59 #=== Return
60 #An array of string.
61 #Each element represents one line of this buffer.
62 def lines
63 @lines
64 end
65
66 #Check if the specified position is valid for this buffer.
67 #
68 #=== Arguments
69 #
70 #_row_ :: Row position or line number.
71 #_col_ :: Column position.
72 #
73 #=== Return
74 #_true_ if the specified position is valid for this buffer.
75 #
76 #=== Note
77 #
78 #The position is between two adjacent characters including line
79 #feed character. Thus, this method returns true if _col_ is equal
80 #to the size of the specified line, but false if _row_ is equal to
81 #the size of Edmaru::Buffer#lines.
82 def valid_position?(row, col)
83 if row < 0 || row >= @lines.size
84 # Invalid row position.
85 return false
86 elsif col < 0 || col > @lines[row].size
87 # Invalid column position.
88 return false
89 else
90 return true
91 end
92 end
93
94 #Insert the specified string to the specified position
95 #
96 #=== Arguments
97 #
98 #_row_ :: Row position or line number.
99 #_col_ :: Column position.
100 #_str_ :: A string to be inserted.
101 def insert(row, col, str)
102 if !valid_position?(row, col)
103 raise "Invalid position was specified for 'insert' method."
104 end
105
106 if str.empty?
107 # Nothing to be inserted.
108 return
109 end
110
111 if str.include?("\n")
112 new_lines = str.split(/\n/, -1)
113 new_lines[0] =
114 @lines[row][0, col] + new_lines[0]
115 new_lines[new_lines.size - 1] <<
116 @lines[row][col .. -1]
117 @lines[row] = new_lines
118 @lines.flatten!
119 else
120 current_line = @lines[row]
121 @lines[row] = ""
122 if col > 0
123 @lines[row] = current_line[0, col]
124 end
125 @lines[row] << str
126 @lines[row] << current_line[col .. -1]
127 end
128
129 refresh_windows
130 end
131
132 #Append the specified string to the end of this buffer.
133 #
134 #=== Argument
135 #
136 #_str_ :: A string to be appended.
137 def append(str)
138 row = @lines.size - 1
139 insert(row, @lines[row].size, str)
140
141 refresh_windows
142 end
143
144 #Delete all characters in this buffer to make it empty.
145 def clear
146 @lines.clear
147 @lines.push("")
148 nil
149
150 refresh_windows
151 end
152 end
153 end

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