Class: Textbringer::Buffer
- Inherits:
-
Object
- Object
- Textbringer::Buffer
- Extended by:
- Enumerable
- Defined in:
- lib/textbringer/buffer.rb
Constant Summary collapse
- GAP_SIZE =
256- UNDO_LIMIT =
1000- UTF8_CHAR_LEN =
Hash.new(1)
- DEFAULT_DETECT_ENCODING =
->(s) { @@auto_detect_encodings.find { |e| s.force_encoding(e) s.valid_encoding? } }
- NKF_DETECT_ENCODING =
->(s) { e = NKF.guess(s) e == Encoding::US_ASCII ? Encoding::UTF_8 : e }
- @@auto_detect_encodings =
[ Encoding::UTF_8, Encoding::EUC_JP, Encoding::Windows_31J ]
- @@detect_encoding_proc =
DEFAULT_DETECT_ENCODING- @@table =
{}
- @@list =
[]
- @@current =
nil- @@minibuffer =
nil
Instance Attribute Summary collapse
-
#current_column ⇒ Object
readonly
Returns the value of attribute current_column.
-
#current_line ⇒ Object
readonly
Returns the value of attribute current_line.
-
#file_encoding ⇒ Object
Returns the value of attribute file_encoding.
-
#file_format ⇒ Object
Returns the value of attribute file_format.
-
#file_name ⇒ Object
Returns the value of attribute file_name.
-
#keymap ⇒ Object
Returns the value of attribute keymap.
-
#marks ⇒ Object
readonly
Returns the value of attribute marks.
-
#mode ⇒ Object
Returns the value of attribute mode.
-
#modified ⇒ Object
writeonly
Sets the attribute modified.
-
#name ⇒ Object
Returns the value of attribute name.
-
#point ⇒ Object
readonly
Returns the value of attribute point.
-
#visible_mark ⇒ Object
readonly
Returns the value of attribute visible_mark.
Class Method Summary collapse
- .[](name) ⇒ Object
- .add(buffer) ⇒ Object
- .auto_detect_encodings ⇒ Object
- .auto_detect_encodings=(encodings) ⇒ Object
- .count ⇒ Object
- .current ⇒ Object
- .current=(buffer) ⇒ Object
- .detect_encoding_proc ⇒ Object
- .detect_encoding_proc=(f) ⇒ Object
- .display_width(s) ⇒ Object
- .dump_unsaved_buffers(dir) ⇒ Object
- .dumped_buffers_exist?(dir) ⇒ Boolean
- .each(&block) ⇒ Object
- .find_file(file_name) ⇒ Object
- .find_or_new(name, **opts) ⇒ Object
- .kill_em_all ⇒ Object
- .last ⇒ Object
- .load(path) ⇒ Object
- .load_dumped_buffers(dir) ⇒ Object
- .minibuffer ⇒ Object
- .names ⇒ Object
- .new_buffer(name, **opts) ⇒ Object
- .new_buffer_name(name) ⇒ Object
- .open(file_name, name: File.basename(file_name)) ⇒ Object
Instance Method Summary collapse
- #[](name) ⇒ Object
- #[]=(name, value) ⇒ Object
- #apply_mode(mode_class) ⇒ Object
- #backward_char(n = 1) ⇒ Object
- #backward_delete_char(n = 1) ⇒ Object
- #backward_line(n = 1) ⇒ Object
- #backward_word(n = 1) ⇒ Object
- #beginning_of_buffer ⇒ Object
- #beginning_of_buffer? ⇒ Boolean
- #beginning_of_line ⇒ Object
- #beginning_of_line? ⇒ Boolean
- #binary? ⇒ Boolean
- #byte_after(location = @point) ⇒ Object
- #byte_before(location = @point) ⇒ Object
- #byteindex(forward, re, pos) ⇒ Object
- #bytesize ⇒ Object (also: #size)
- #char_after(location = @point) ⇒ Object
- #clear ⇒ Object
- #copy_region(s = @point, e = mark, append = false) ⇒ Object
- #current? ⇒ Boolean
- #delete_char(n = 1) ⇒ Object
- #delete_region(s = @point, e = mark) ⇒ Object
- #delete_visible_mark ⇒ Object
- #dump(path) ⇒ Object
- #end_of_buffer ⇒ Object
- #end_of_buffer? ⇒ Boolean
- #end_of_line ⇒ Object
- #end_of_line? ⇒ Boolean
- #exchange_point_and_mark(mark = @mark) ⇒ Object
- #file_modified? ⇒ Boolean
- #forward_char(n = 1) ⇒ Object
- #forward_line(n = 1) ⇒ Object
- #forward_word(n = 1) ⇒ Object
- #gap_filled_with_nul? ⇒ Boolean
- #get_line_and_column(pos) ⇒ Object
- #goto_char(pos) ⇒ Object
- #goto_line(n) ⇒ Object
- #indent_to(column) ⇒ Object
-
#initialize(s = String.new, name: nil, file_name: nil, file_encoding: Encoding::UTF_8, file_mtime: nil, new_file: true, undo_limit: UNDO_LIMIT, read_only: false) ⇒ Buffer
constructor
s might not be copied.
- #insert(s, merge_undo = false) ⇒ Object
- #insert_for_yank(s) ⇒ Object
- #inspect ⇒ Object
- #kill ⇒ Object
- #kill_line(append = false) ⇒ Object
- #kill_region(s = @point, e = mark, append = false) ⇒ Object
- #kill_word(append = false) ⇒ Object
- #looking_at?(re) ⇒ Boolean
- #mark ⇒ Object
- #mark_to_point(mark) ⇒ Object
- #match_beginning(n) ⇒ Object
- #match_end(n) ⇒ Object
- #match_string(n) ⇒ Object
- #merge_undo(n) ⇒ Object
- #modified? ⇒ Boolean
- #new_file? ⇒ Boolean
- #new_mark(location = @point) ⇒ Object
- #newline ⇒ Object
- #next_line(n = 1) ⇒ Object
- #point_after_mark?(mark) ⇒ Boolean
- #point_at_mark?(mark) ⇒ Boolean
- #point_before_mark?(mark) ⇒ Boolean
- #point_max ⇒ Object
- #point_min ⇒ Object
- #point_to_mark(mark) ⇒ Object
- #previous_line(n = 1) ⇒ Object
- #re_search_backward(s) ⇒ Object
- #re_search_forward(s) ⇒ Object
- #read_only=(value) ⇒ Object
- #read_only? ⇒ Boolean
- #redo ⇒ Object
- #replace_match(str) ⇒ Object
- #replace_regexp_forward(regexp, to_str) ⇒ Object
- #save(file_name = @file_name) ⇒ Object
-
#save_excursion ⇒ Object
Don’t save Buffer.current.
-
#save_point ⇒ Object
The buffer should not be modified in the given block because current_line/current_column is not updated in save_point.
- #set_mark(pos = @point) ⇒ Object
- #set_visible_mark(pos = @point) ⇒ Object
- #substring(s, e) ⇒ Object
- #to_s ⇒ Object
- #transpose_chars ⇒ Object
- #undo ⇒ Object
- #yank ⇒ Object
- #yank_pop ⇒ Object
Constructor Details
#initialize(s = String.new, name: nil, file_name: nil, file_encoding: Encoding::UTF_8, file_mtime: nil, new_file: true, undo_limit: UNDO_LIMIT, read_only: false) ⇒ Buffer
s might not be copied.
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/textbringer/buffer.rb', line 166 def initialize(s = String.new, name: nil, file_name: nil, file_encoding: Encoding::UTF_8, file_mtime: nil, new_file: true, undo_limit: UNDO_LIMIT, read_only: false) case s.encoding when Encoding::UTF_8, Encoding::ASCII_8BIT @contents = s.frozen? ? s.dup : s else @contents = s.encode(Encoding::UTF_8) end @contents.force_encoding(Encoding::ASCII_8BIT) @name = name @file_name = file_name self.file_encoding = file_encoding @file_mtime = file_mtime case @contents when /(?<!\r)\n/ @file_format = :unix when /\r(?!\n)/ @file_format = :mac @contents.gsub!(/\r/, "\n") when /\r\n/ @file_format = :dos @contents.gsub!(/\r/, "") else @file_format = :unix end @new_file = new_file @undo_limit = undo_limit @point = 0 @gap_start = 0 @gap_end = 0 @marks = [] @mark = nil @current_line = 1 @current_column = 1 # One-based character count @goal_column = nil # Zero-based display width count @yank_start = new_mark @undo_stack = [] @redo_stack = [] @undoing = false @version = 0 @modified = false @mode = nil @keymap = nil @attributes = {} @save_point_level = 0 @match_offsets = [] @visible_mark = nil @read_only = read_only end |
Instance Attribute Details
#current_column ⇒ Object (readonly)
Returns the value of attribute current_column.
14 15 16 |
# File 'lib/textbringer/buffer.rb', line 14 def current_column @current_column end |
#current_line ⇒ Object (readonly)
Returns the value of attribute current_line.
14 15 16 |
# File 'lib/textbringer/buffer.rb', line 14 def current_line @current_line end |
#file_encoding ⇒ Object
Returns the value of attribute file_encoding.
13 14 15 |
# File 'lib/textbringer/buffer.rb', line 13 def file_encoding @file_encoding end |
#file_format ⇒ Object
Returns the value of attribute file_format.
13 14 15 |
# File 'lib/textbringer/buffer.rb', line 13 def file_format @file_format end |
#file_name ⇒ Object
Returns the value of attribute file_name.
13 14 15 |
# File 'lib/textbringer/buffer.rb', line 13 def file_name @file_name end |
#keymap ⇒ Object
Returns the value of attribute keymap.
12 13 14 |
# File 'lib/textbringer/buffer.rb', line 12 def keymap @keymap end |
#marks ⇒ Object (readonly)
Returns the value of attribute marks.
13 14 15 |
# File 'lib/textbringer/buffer.rb', line 13 def marks @marks end |
#mode ⇒ Object
Returns the value of attribute mode.
12 13 14 |
# File 'lib/textbringer/buffer.rb', line 12 def mode @mode end |
#modified=(value) ⇒ Object (writeonly)
Sets the attribute modified
15 16 17 |
# File 'lib/textbringer/buffer.rb', line 15 def modified=(value) @modified = value end |
#name ⇒ Object
Returns the value of attribute name.
13 14 15 |
# File 'lib/textbringer/buffer.rb', line 13 def name @name end |
#point ⇒ Object (readonly)
Returns the value of attribute point.
13 14 15 |
# File 'lib/textbringer/buffer.rb', line 13 def point @point end |
#visible_mark ⇒ Object (readonly)
Returns the value of attribute visible_mark.
14 15 16 |
# File 'lib/textbringer/buffer.rb', line 14 def visible_mark @visible_mark end |
Class Method Details
.[](name) ⇒ Object
105 106 107 |
# File 'lib/textbringer/buffer.rb', line 105 def self.[](name) @@table[name] end |
.add(buffer) ⇒ Object
72 73 74 75 |
# File 'lib/textbringer/buffer.rb', line 72 def self.add(buffer) @@table[buffer.name] = buffer @@list.unshift(buffer) end |
.auto_detect_encodings ⇒ Object
56 57 58 |
# File 'lib/textbringer/buffer.rb', line 56 def self.auto_detect_encodings @@auto_detect_encodings end |
.auto_detect_encodings=(encodings) ⇒ Object
60 61 62 |
# File 'lib/textbringer/buffer.rb', line 60 def self.auto_detect_encodings=(encodings) @@auto_detect_encodings = encodings end |
.count ⇒ Object
101 102 103 |
# File 'lib/textbringer/buffer.rb', line 101 def self.count @@table.size end |
.current ⇒ Object
77 78 79 |
# File 'lib/textbringer/buffer.rb', line 77 def self.current @@current end |
.current=(buffer) ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/textbringer/buffer.rb', line 81 def self.current=(buffer) if buffer && buffer.name && @@table.key?(buffer.name) @@list.delete(buffer) @@list.push(buffer) end @@current = buffer end |
.detect_encoding_proc ⇒ Object
64 65 66 |
# File 'lib/textbringer/buffer.rb', line 64 def self.detect_encoding_proc @@detect_encoding_proc end |
.detect_encoding_proc=(f) ⇒ Object
68 69 70 |
# File 'lib/textbringer/buffer.rb', line 68 def self.detect_encoding_proc=(f) @@detect_encoding_proc = f end |
.display_width(s) ⇒ Object
160 161 162 163 |
# File 'lib/textbringer/buffer.rb', line 160 def self.display_width(s) # ncurses seems to treat ambiguous east asian characters as narrow. Unicode::DisplayWidth.of(s, 1) end |
.dump_unsaved_buffers(dir) ⇒ Object
1120 1121 1122 1123 1124 1125 1126 1127 |
# File 'lib/textbringer/buffer.rb', line 1120 def self.dump_unsaved_buffers(dir) FileUtils.mkdir_p(dir) @@list.each do |buffer| if /\A\*/ !~ buffer.name && buffer.modified? buffer.dump(File.(buffer.object_id.to_s, dir)) end end end |
.dumped_buffers_exist?(dir) ⇒ Boolean
1129 1130 1131 |
# File 'lib/textbringer/buffer.rb', line 1129 def self.dumped_buffers_exist?(dir) !Dir.glob(File.("*.metadata", dir)).empty? end |
.each(&block) ⇒ Object
156 157 158 |
# File 'lib/textbringer/buffer.rb', line 156 def self.each(&block) @@table.each_value(&block) end |
.find_file(file_name) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/textbringer/buffer.rb', line 123 def self.find_file(file_name) file_name = File.(file_name) buffer = @@table.each_value.find { |buffer| buffer.file_name == file_name } if buffer.nil? name = File.basename(file_name) begin buffer = Buffer.open(file_name, name: new_buffer_name(name)) add(buffer) rescue Errno::ENOENT buffer = new_buffer(name, file_name: file_name) end end buffer end |
.find_or_new(name, **opts) ⇒ Object
109 110 111 |
# File 'lib/textbringer/buffer.rb', line 109 def self.find_or_new(name, **opts) @@table[name] ||= new_buffer(name, **opts) end |
.kill_em_all ⇒ Object
117 118 119 120 121 |
# File 'lib/textbringer/buffer.rb', line 117 def self.kill_em_all @@table.clear @@list.clear @@current = nil end |
.last ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/textbringer/buffer.rb', line 93 def self.last if @@list.last == @@current @@list[-2] else @@list.last end end |
.load(path) ⇒ Object
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 |
# File 'lib/textbringer/buffer.rb', line 1109 def self.load(path) buffer = Buffer.new(File.read(path)) = JSON.parse(File.read(path + ".metadata")) buffer.name = ["name"] buffer.file_name = ["file_name"] if ["file_name"] buffer.file_encoding = Encoding.find(["file_encoding"]) buffer.file_format = ["file_format"].intern buffer.modified = true buffer end |
.load_dumped_buffers(dir) ⇒ Object
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 |
# File 'lib/textbringer/buffer.rb', line 1133 def self.load_dumped_buffers(dir) Dir.glob(File.("*.metadata", dir)).map do || path = .sub(/\.metadata\z/, "") buffer = Buffer.load(path) add(buffer) File.unlink() File.unlink(path) buffer end end |
.minibuffer ⇒ Object
89 90 91 |
# File 'lib/textbringer/buffer.rb', line 89 def self.minibuffer @@minibuffer ||= Buffer.new(name: "*Minibuffer*") end |
.names ⇒ Object
113 114 115 |
# File 'lib/textbringer/buffer.rb', line 113 def self.names @@table.keys end |
.new_buffer(name, **opts) ⇒ Object
140 141 142 143 144 |
# File 'lib/textbringer/buffer.rb', line 140 def self.new_buffer(name, **opts) buffer = Buffer.new(**opts.merge(name: new_buffer_name(name))) add(buffer) buffer end |
.new_buffer_name(name) ⇒ Object
146 147 148 149 150 151 152 153 154 |
# File 'lib/textbringer/buffer.rb', line 146 def self.new_buffer_name(name) if @@table.key?(name) (2..Float::INFINITY).lazy.map { |i| "#{name}<#{i}>" }.find { |i| !@@table.key?(i) } else name end end |
.open(file_name, name: File.basename(file_name)) ⇒ Object
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/textbringer/buffer.rb', line 305 def self.open(file_name, name: File.basename(file_name)) s, mtime = File.open(file_name) { |f| f.flock(File::LOCK_SH) [f.read, f.mtime] } enc = @@detect_encoding_proc.call(s) || Encoding::ASCII_8BIT s.force_encoding(enc) unless s.valid_encoding? enc = Encoding::ASCII_8BIT s.force_encoding(enc) end Buffer.new(s, name: name, file_name: file_name, file_encoding: enc, file_mtime: mtime, new_file: false, read_only: !File.writable?(file_name)) end |
Instance Method Details
#[](name) ⇒ Object
289 290 291 292 293 294 295 |
# File 'lib/textbringer/buffer.rb', line 289 def [](name) if @attributes.key?(name) @attributes[name] else CONFIG[name] end end |
#[]=(name, value) ⇒ Object
297 298 299 |
# File 'lib/textbringer/buffer.rb', line 297 def []=(name, value) @attributes[name] = value end |
#apply_mode(mode_class) ⇒ Object
1084 1085 1086 1087 |
# File 'lib/textbringer/buffer.rb', line 1084 def apply_mode(mode_class) @mode = mode_class.new(self) run_hooks(mode_class.hook_name) end |
#backward_char(n = 1) ⇒ Object
548 549 550 |
# File 'lib/textbringer/buffer.rb', line 548 def backward_char(n = 1) forward_char(-n) end |
#backward_delete_char(n = 1) ⇒ Object
537 538 539 |
# File 'lib/textbringer/buffer.rb', line 537 def backward_delete_char(n = 1) delete_char(-n) end |
#backward_line(n = 1) ⇒ Object
596 597 598 |
# File 'lib/textbringer/buffer.rb', line 596 def backward_line(n = 1) forward_line(-n) end |
#backward_word(n = 1) ⇒ Object
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 |
# File 'lib/textbringer/buffer.rb', line 563 def backward_word(n = 1) n.times do break if beginning_of_buffer? backward_char while !beginning_of_buffer? && /\p{Letter}|\p{Number}/ !~ char_after backward_char end while !beginning_of_buffer? && /\p{Letter}|\p{Number}/ =~ char_after backward_char end if /\p{Letter}|\p{Number}/ !~ char_after forward_char end end end |
#beginning_of_buffer ⇒ Object
641 642 643 644 645 646 647 |
# File 'lib/textbringer/buffer.rb', line 641 def beginning_of_buffer if @save_point_level == 0 @current_line = 1 @current_column = 1 end @point = 0 end |
#beginning_of_buffer? ⇒ Boolean
649 650 651 |
# File 'lib/textbringer/buffer.rb', line 649 def beginning_of_buffer? @point == 0 end |
#beginning_of_line ⇒ Object
661 662 663 664 665 666 |
# File 'lib/textbringer/buffer.rb', line 661 def beginning_of_line while !beginning_of_line? backward_char end @point end |
#beginning_of_line? ⇒ Boolean
668 669 670 |
# File 'lib/textbringer/buffer.rb', line 668 def beginning_of_line? beginning_of_buffer? || byte_before == "\n" end |
#binary? ⇒ Boolean
245 246 247 |
# File 'lib/textbringer/buffer.rb', line 245 def binary? @binary end |
#byte_after(location = @point) ⇒ Object
372 373 374 375 376 377 378 |
# File 'lib/textbringer/buffer.rb', line 372 def byte_after(location = @point) if location < @gap_start @contents.byteslice(location) else @contents.byteslice(location + gap_size) end end |
#byte_before(location = @point) ⇒ Object
380 381 382 383 384 385 386 |
# File 'lib/textbringer/buffer.rb', line 380 def byte_before(location = @point) if location <= point_min || location > point_max nil else byte_after(location - 1) end end |
#byteindex(forward, re, pos) ⇒ Object
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 |
# File 'lib/textbringer/buffer.rb', line 946 def byteindex(forward, re, pos) @match_offsets = [] method = forward ? :index : :rindex adjust_gap(0, point_max) if @binary offset = pos else offset = @contents[0...pos].force_encoding(Encoding::UTF_8).size @contents.force_encoding(Encoding::UTF_8) end begin i = @contents.send(method, re, offset) if i m = Regexp.last_match if m.nil? # A bug of rindex @match_offsets.push([pos, pos]) pos else b = m.pre_match.bytesize e = b + m.to_s.bytesize if e <= bytesize @match_offsets.push([b, e]) match_beg = m.begin(0) match_str = m.to_s (1 .. m.size - 1).each do |j| cb, ce = m.offset(j) if cb.nil? @match_offsets.push([nil, nil]) else bb = b + match_str[0, cb - match_beg].bytesize be = b + match_str[0, ce - match_beg].bytesize @match_offsets.push([bb, be]) end end b else nil end end else nil end ensure @contents.force_encoding(Encoding::ASCII_8BIT) end end |
#bytesize ⇒ Object Also known as: size
397 398 399 |
# File 'lib/textbringer/buffer.rb', line 397 def bytesize @contents.bytesize - gap_size end |
#char_after(location = @point) ⇒ Object
388 389 390 391 392 393 394 395 |
# File 'lib/textbringer/buffer.rb', line 388 def char_after(location = @point) if @binary byte_after(location) else s = substring(location, location + UTF8_CHAR_LEN[byte_after(location)]) s.empty? ? nil : s end end |
#clear ⇒ Object
816 817 818 819 820 821 822 823 824 825 826 827 828 829 |
# File 'lib/textbringer/buffer.rb', line 816 def clear check_read_only_flag @contents = String.new @point = @gap_start = @gap_end = 0 @marks.each do |m| m.location = 0 end @current_line = 1 @current_column = 1 @goal_column = nil @modified = true @undo_stack.clear @redo_stack.clear end |
#copy_region(s = @point, e = mark, append = false) ⇒ Object
775 776 777 778 779 780 781 782 |
# File 'lib/textbringer/buffer.rb', line 775 def copy_region(s = @point, e = mark, append = false) str = s <= e ? substring(s, e) : substring(e, s) if append && !KILL_RING.empty? KILL_RING.current.concat(str) else KILL_RING.push(str) end end |
#current? ⇒ Boolean
281 282 283 |
# File 'lib/textbringer/buffer.rb', line 281 def current? @@current == self end |
#delete_char(n = 1) ⇒ Object
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 |
# File 'lib/textbringer/buffer.rb', line 499 def delete_char(n = 1) check_read_only_flag adjust_gap s = @point pos = get_pos(@point, n) if n > 0 str = substring(s, pos) # fill the gap with NUL to avoid invalid byte sequence in UTF-8 @contents[@gap_end...user_to_gap(pos)] = "\0" * (pos - @point) @gap_end += pos - @point @marks.each do |m| if m.location > pos m.location -= pos - @point elsif m.location > @point m.location = @point end end push_undo(DeleteAction.new(self, s, s, str)) @modified = true elsif n < 0 str = substring(pos, s) update_line_and_column(@point, pos) # fill the gap with NUL to avoid invalid byte sequence in UTF-8 @contents[user_to_gap(pos)...@gap_start] = "\0" * (@point - pos) @marks.each do |m| if m.location >= @point m.location -= @point - pos elsif m.location > pos m.location = pos end end @point = @gap_start = pos push_undo(DeleteAction.new(self, s, pos, str)) @modified = true end @goal_column = nil end |
#delete_region(s = @point, e = mark) ⇒ Object
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 |
# File 'lib/textbringer/buffer.rb', line 789 def delete_region(s = @point, e = mark) check_read_only_flag old_pos = @point if s > e s, e = e, s end update_line_and_column(old_pos, s) save_point do str = substring(s, e) @point = s adjust_gap len = e - s # fill the gap with NUL to avoid invalid byte sequence in UTF-8 @contents[@gap_end, len] = "\0" * len @gap_end += len @marks.each do |m| if m.location > e m.location -= len elsif m.location > s m.location = s end end push_undo(DeleteAction.new(self, old_pos, s, str)) @modified = true end end |
#delete_visible_mark ⇒ Object
768 769 770 771 772 773 |
# File 'lib/textbringer/buffer.rb', line 768 def delete_visible_mark if @visible_mark @visible_mark.delete @visible_mark = nil end end |
#dump(path) ⇒ Object
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 |
# File 'lib/textbringer/buffer.rb', line 1098 def dump(path) File.write(path, to_s) = { "name" => name, "file_name" => file_name, "file_encoding" => file_encoding.name, "file_format" => file_format.to_s } File.write(path + ".metadata", .to_json) end |
#end_of_buffer ⇒ Object
653 654 655 |
# File 'lib/textbringer/buffer.rb', line 653 def end_of_buffer goto_char(bytesize) end |
#end_of_buffer? ⇒ Boolean
657 658 659 |
# File 'lib/textbringer/buffer.rb', line 657 def end_of_buffer? @point == bytesize end |
#end_of_line ⇒ Object
672 673 674 675 676 677 |
# File 'lib/textbringer/buffer.rb', line 672 def end_of_line while !end_of_line? forward_char end @point end |
#end_of_line? ⇒ Boolean
679 680 681 |
# File 'lib/textbringer/buffer.rb', line 679 def end_of_line? end_of_buffer? || byte_after == "\n" end |
#exchange_point_and_mark(mark = @mark) ⇒ Object
709 710 711 712 713 714 715 |
# File 'lib/textbringer/buffer.rb', line 709 def exchange_point_and_mark(mark = @mark) if mark.nil? raise EditorError, "The mark is not set" end update_line_and_column(@point, mark.location) @point, mark.location = mark.location, @point end |
#file_modified? ⇒ Boolean
350 351 352 |
# File 'lib/textbringer/buffer.rb', line 350 def file_modified? !@file_mtime.nil? && File.mtime(@file_name) != @file_mtime end |
#forward_char(n = 1) ⇒ Object
541 542 543 544 545 546 |
# File 'lib/textbringer/buffer.rb', line 541 def forward_char(n = 1) pos = get_pos(@point, n) update_line_and_column(@point, pos) @point = pos @goal_column = nil end |
#forward_line(n = 1) ⇒ Object
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 |
# File 'lib/textbringer/buffer.rb', line 579 def forward_line(n = 1) if n > 0 n.times do end_of_line break if end_of_buffer? forward_char end elsif n < 0 (-n).times do beginning_of_line break if beginning_of_buffer? backward_char beginning_of_line end end end |
#forward_word(n = 1) ⇒ Object
552 553 554 555 556 557 558 559 560 561 |
# File 'lib/textbringer/buffer.rb', line 552 def forward_word(n = 1) n.times do while !end_of_buffer? && /\p{Letter}|\p{Number}/ !~ char_after forward_char end while !end_of_buffer? && /\p{Letter}|\p{Number}/ =~ char_after forward_char end end end |
#gap_filled_with_nul? ⇒ Boolean
1067 1068 1069 |
# File 'lib/textbringer/buffer.rb', line 1067 def gap_filled_with_nul? /\A\0*\z/ =~ @contents[@gap_start...@gap_end] ? true : false end |
#get_line_and_column(pos) ⇒ Object
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 |
# File 'lib/textbringer/buffer.rb', line 410 def get_line_and_column(pos) line = 1 + @contents[0...user_to_gap(pos)].count("\n") if pos == point_min column = 1 else i = @contents.rindex("\n", user_to_gap(pos - 1)) if i i += 1 else i = 0 end column = 1 + substring(gap_to_user(i), pos).size end [line, column] end |
#goto_char(pos) ⇒ Object
426 427 428 429 430 431 432 433 434 435 436 437 438 |
# File 'lib/textbringer/buffer.rb', line 426 def goto_char(pos) if pos < 0 || pos > size raise RangeError, "Out of buffer" end if !@binary && /[\x80-\xbf]/n =~ byte_after(pos) raise ArgumentError, "Position is in the middle of a character" end @goal_column = nil if @save_point_level == 0 @current_line, @current_column = get_line_and_column(pos) end @point = pos end |
#goto_line(n) ⇒ Object
440 441 442 443 444 445 446 447 448 449 450 451 452 453 |
# File 'lib/textbringer/buffer.rb', line 440 def goto_line(n) pos = point_min i = 1 while i < n && pos < @contents.bytesize pos = @contents.index("\n", pos) break if pos.nil? i += 1 pos += 1 end @point = gap_to_user(pos) @current_line = i @current_column = 1 @goal_column = nil end |
#indent_to(column) ⇒ Object
1089 1090 1091 1092 1093 1094 1095 1096 |
# File 'lib/textbringer/buffer.rb', line 1089 def indent_to(column) s = if self[:indent_tabs_mode] "\t" * (column / self[:tab_width]) + " " * (column % self[:tab_width]) else " " * column end insert(s) end |
#insert(s, merge_undo = false) ⇒ Object
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 |
# File 'lib/textbringer/buffer.rb', line 455 def insert(s, merge_undo = false) check_read_only_flag pos = @point size = s.bytesize adjust_gap(size) @contents[@point, size] = s.b @marks.each do |m| if m.location > @point m.location += size end end @point = @gap_start += size update_line_and_column(pos, @point) unless @undoing if merge_undo && @undo_stack.last.is_a?(InsertAction) @undo_stack.last.merge(s) @redo_stack.clear else push_undo(InsertAction.new(self, pos, s)) end end @modified = true @goal_column = nil end |
#insert_for_yank(s) ⇒ Object
859 860 861 862 |
# File 'lib/textbringer/buffer.rb', line 859 def insert_for_yank(s) mark_to_point(@yank_start) insert(s) end |
#inspect ⇒ Object
218 219 220 |
# File 'lib/textbringer/buffer.rb', line 218 def inspect "#<Buffer:#{@name || '0x%x' % object_id}>" end |
#kill ⇒ Object
273 274 275 276 277 278 279 |
# File 'lib/textbringer/buffer.rb', line 273 def kill @@table.delete(@name) @@list.delete(self) if @@current == self @@current = nil end end |
#kill_line(append = false) ⇒ Object
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 |
# File 'lib/textbringer/buffer.rb', line 831 def kill_line(append = false) save_point do |saved| if end_of_buffer? raise RangeError, "End of buffer" end if char_after == ?\n forward_char else end_of_line end pos = @point point_to_mark(saved) kill_region(@point, pos, append) end end |
#kill_region(s = @point, e = mark, append = false) ⇒ Object
784 785 786 787 |
# File 'lib/textbringer/buffer.rb', line 784 def kill_region(s = @point, e = mark, append = false) copy_region(s, e, append) delete_region(s, e) end |
#kill_word(append = false) ⇒ Object
847 848 849 850 851 852 853 854 855 856 857 |
# File 'lib/textbringer/buffer.rb', line 847 def kill_word(append = false) save_point do |saved| if end_of_buffer? raise RangeError, "End of buffer" end forward_word pos = @point point_to_mark(saved) kill_region(@point, pos, append) end end |
#looking_at?(re) ⇒ Boolean
941 942 943 944 |
# File 'lib/textbringer/buffer.rb', line 941 def looking_at?(re) # TODO: optimization byteindex(true, re, @point) == @point end |
#mark ⇒ Object
751 752 753 754 755 756 |
# File 'lib/textbringer/buffer.rb', line 751 def mark if @mark.nil? raise EditorError, "The mark is not set" end @mark.location end |
#mark_to_point(mark) ⇒ Object
693 694 695 |
# File 'lib/textbringer/buffer.rb', line 693 def mark_to_point(mark) mark.location = @point end |
#match_beginning(n) ⇒ Object
994 995 996 |
# File 'lib/textbringer/buffer.rb', line 994 def match_beginning(n) @match_offsets[n]&.first end |
#match_end(n) ⇒ Object
998 999 1000 |
# File 'lib/textbringer/buffer.rb', line 998 def match_end(n) @match_offsets[n]&.last end |
#match_string(n) ⇒ Object
1002 1003 1004 1005 1006 1007 1008 1009 |
# File 'lib/textbringer/buffer.rb', line 1002 def match_string(n) b, e = @match_offsets[n] if b.nil? nil else substring(b, e) end end |
#merge_undo(n) ⇒ Object
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 |
# File 'lib/textbringer/buffer.rb', line 1071 def merge_undo(n) return if @undoing || @undo_limit == 0 actions = @undo_stack.pop(n) if actions action = CompositeAction.new(self, actions.first.location) actions.each do |i| action.add_action(i) end action.version = actions.first.version @undo_stack.push(action) end end |
#modified? ⇒ Boolean
285 286 287 |
# File 'lib/textbringer/buffer.rb', line 285 def modified? @modified end |
#new_file? ⇒ Boolean
301 302 303 |
# File 'lib/textbringer/buffer.rb', line 301 def new_file? @new_file end |
#new_mark(location = @point) ⇒ Object
683 684 685 686 687 |
# File 'lib/textbringer/buffer.rb', line 683 def new_mark(location = @point) Mark.new(self, location).tap { |m| @marks << m } end |
#newline ⇒ Object
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 |
# File 'lib/textbringer/buffer.rb', line 480 def newline indentation = save_point { |saved| if /[ \t]/ =~ char_after next "" end beginning_of_line s = @point while /[ \t]/ =~ char_after forward_char end str = substring(s, @point) if end_of_buffer? || char_after == "\n" delete_region(s, @point) end str } insert("\n" + indentation) end |
#next_line(n = 1) ⇒ Object
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 |
# File 'lib/textbringer/buffer.rb', line 600 def next_line(n = 1) if @goal_column column = @goal_column else prev_point = @point beginning_of_line column = Buffer.display_width(substring(@point, prev_point)) end n.times do end_of_line forward_char s = @point while !end_of_line? && Buffer.display_width(substring(s, @point)) < column forward_char end end @goal_column = column end |
#point_after_mark?(mark) ⇒ Boolean
705 706 707 |
# File 'lib/textbringer/buffer.rb', line 705 def point_after_mark?(mark) @point > mark.location end |
#point_at_mark?(mark) ⇒ Boolean
697 698 699 |
# File 'lib/textbringer/buffer.rb', line 697 def point_at_mark?(mark) @point == mark.location end |
#point_before_mark?(mark) ⇒ Boolean
701 702 703 |
# File 'lib/textbringer/buffer.rb', line 701 def point_before_mark?(mark) @point < mark.location end |
#point_max ⇒ Object
406 407 408 |
# File 'lib/textbringer/buffer.rb', line 406 def point_max bytesize end |
#point_min ⇒ Object
402 403 404 |
# File 'lib/textbringer/buffer.rb', line 402 def point_min 0 end |
#point_to_mark(mark) ⇒ Object
689 690 691 |
# File 'lib/textbringer/buffer.rb', line 689 def point_to_mark(mark) goto_char(mark.location) end |
#previous_line(n = 1) ⇒ Object
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 |
# File 'lib/textbringer/buffer.rb', line 620 def previous_line(n = 1) if @goal_column column = @goal_column else prev_point = @point beginning_of_line column = Buffer.display_width(substring(@point, prev_point)) end n.times do beginning_of_line backward_char beginning_of_line s = @point while !end_of_line? && Buffer.display_width(substring(s, @point)) < column forward_char end end @goal_column = column end |
#re_search_backward(s) ⇒ Object
926 927 928 929 930 931 932 933 934 935 936 937 938 939 |
# File 'lib/textbringer/buffer.rb', line 926 def re_search_backward(s) re = new_regexp(s) pos = @point begin i = byteindex(false, re, pos) if i.nil? raise SearchError, "Search failed" end pos = get_pos(pos, -1) rescue RangeError raise SearchError, "Search failed" end while match_end(0) > @point goto_char(match_beginning(0)) end |
#re_search_forward(s) ⇒ Object
917 918 919 920 921 922 923 924 |
# File 'lib/textbringer/buffer.rb', line 917 def re_search_forward(s) re = new_regexp(s) i = byteindex(true, re, @point) if i.nil? raise SearchError, "Search failed" end goto_char(match_end(0)) end |
#read_only=(value) ⇒ Object
266 267 268 269 270 271 |
# File 'lib/textbringer/buffer.rb', line 266 def read_only=(value) @read_only = value if @read_only @modified = false end end |
#read_only? ⇒ Boolean
262 263 264 |
# File 'lib/textbringer/buffer.rb', line 262 def read_only? @read_only end |
#redo ⇒ Object
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 |
# File 'lib/textbringer/buffer.rb', line 895 def redo check_read_only_flag if @redo_stack.empty? raise EditorError, "No further redo information" end action = @redo_stack.pop @undoing = true begin was_modified = @modified action.redo if action.version == @version @modified = false action.version = nil elsif !was_modified action.version = @version end @undo_stack.push(action) ensure @undoing = false end end |
#replace_match(str) ⇒ Object
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 |
# File 'lib/textbringer/buffer.rb', line 1011 def replace_match(str) new_str = str.gsub(/\\(?:([0-9]+)|(&)|(\\))/) { |s| case when $1 match_string($1.to_i) when $2 match_string(0) when $3 "\\" end } b = match_beginning(0) e = match_end(0) goto_char(b) delete_region(b, e) insert(new_str) merge_undo(2) end |
#replace_regexp_forward(regexp, to_str) ⇒ Object
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 |
# File 'lib/textbringer/buffer.rb', line 1030 def replace_regexp_forward(regexp, to_str) result = 0 rest = substring(point, point_max) delete_region(point, point_max) new_str = rest.gsub(new_regexp(regexp)) { result += 1 m = Regexp.last_match to_str.gsub(/\\(?:([0-9]+)|(&)|(\\))/) { |s| case when $1 m[$1.to_i] when $2 m.to_s when $3 "\\" end } } insert(new_str) merge_undo(2) result end |
#save(file_name = @file_name) ⇒ Object
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
# File 'lib/textbringer/buffer.rb', line 321 def save(file_name = @file_name) if file_name.nil? raise EditorError, "File name is not set" end file_name = File.(file_name) begin File.open(file_name, "w", external_encoding: @file_encoding) do |f| f.flock(File::LOCK_EX) write_to_file(f) f.flush @file_mtime = f.mtime end rescue Errno::EISDIR if @name file_name = File.(@name, file_name) retry else raise end end if file_name != @file_name self.file_name = file_name end @version += 1 @modified = false @new_file = false @read_only = false end |
#save_excursion ⇒ Object
Don’t save Buffer.current.
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 |
# File 'lib/textbringer/buffer.rb', line 734 def save_excursion old_point = new_mark old_mark = @mark&.dup old_column = @goal_column begin yield ensure point_to_mark(old_point) old_point.delete if old_mark @mark.location = old_mark.location old_mark.delete end @goal_column = old_column end end |
#save_point ⇒ Object
The buffer should not be modified in the given block because current_line/current_column is not updated in save_point.
719 720 721 722 723 724 725 726 727 728 729 730 731 |
# File 'lib/textbringer/buffer.rb', line 719 def save_point saved = new_mark column = @goal_column @save_point_level += 1 begin yield(saved) ensure point_to_mark(saved) saved.delete @goal_column = column @save_point_level -= 1 end end |
#set_mark(pos = @point) ⇒ Object
758 759 760 761 |
# File 'lib/textbringer/buffer.rb', line 758 def set_mark(pos = @point) @mark ||= new_mark @mark.location = pos end |
#set_visible_mark(pos = @point) ⇒ Object
763 764 765 766 |
# File 'lib/textbringer/buffer.rb', line 763 def set_visible_mark(pos = @point) @visible_mark ||= new_mark @visible_mark.location = pos end |
#substring(s, e) ⇒ Object
360 361 362 363 364 365 366 367 368 369 370 |
# File 'lib/textbringer/buffer.rb', line 360 def substring(s, e) result = if s > @gap_start || e <= @gap_start @contents[user_to_gap(s)...user_to_gap(e)] else len = @gap_start - s @contents[user_to_gap(s), len] + @contents[@gap_end, e - s - len] end result.force_encoding(Encoding::UTF_8) unless @binary result end |
#to_s ⇒ Object
354 355 356 357 358 |
# File 'lib/textbringer/buffer.rb', line 354 def to_s result = (@contents[0...@gap_start] + @contents[@gap_end..-1]) result.force_encoding(Encoding::UTF_8) unless @binary result end |
#transpose_chars ⇒ Object
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 |
# File 'lib/textbringer/buffer.rb', line 1053 def transpose_chars if end_of_line? backward_char end if beginning_of_buffer? raise RangeError, "Beginning of buffer" end backward_char c = char_after delete_char forward_char insert(c) end |
#undo ⇒ Object
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 |
# File 'lib/textbringer/buffer.rb', line 873 def undo check_read_only_flag if @undo_stack.empty? raise EditorError, "No further undo information" end action = @undo_stack.pop @undoing = true begin was_modified = @modified action.undo if action.version == @version @modified = false action.version = nil elsif !was_modified action.version = @version end @redo_stack.push(action) ensure @undoing = false end end |
#yank ⇒ Object
864 865 866 |
# File 'lib/textbringer/buffer.rb', line 864 def yank insert_for_yank(KILL_RING.current) end |
#yank_pop ⇒ Object
868 869 870 871 |
# File 'lib/textbringer/buffer.rb', line 868 def yank_pop delete_region(@yank_start.location, @point) insert_for_yank(KILL_RING.current(1)) end |