Class: Textbringer::Buffer
- Inherits:
-
Object
- Object
- Textbringer::Buffer
- Extended by:
- Enumerable
- Defined in:
- lib/textbringer/buffer.rb
Defined Under Namespace
Modules: RectangleMethods
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) case e when Encoding::US_ASCII if s.ascii_only? Encoding::UTF_8 else Encoding::ASCII_8BIT end when Encoding::ASCII_8BIT s.force_encoding(Encoding::UTF_8) if s.valid_encoding? Encoding::UTF_8 else s.force_encoding(Encoding::ASCII_8BIT) Encoding::ASCII_8BIT end else e end }
- BYTESPLICE_SUPPORTS_PARTIAL_COPY =
begin (+"foo").bytesplice(0, 2, "bar", 1, 2) == "aro" rescue NoMethodError, ArgumentError false end
- WORD_COMPONENT_REGEXP =
/\p{Letter}|\p{Number}|\p{M}/- @@auto_detect_encodings =
[ Encoding::UTF_8, Encoding::EUC_JP, Encoding::WINDOWS_31J ]
- @@detect_encoding_proc =
DEFAULT_DETECT_ENCODING- @@table =
{}
- @@list =
[]
- @@current =
nil- @@minibuffer =
nil- @@global_mark_ring =
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.
-
#file_version ⇒ Object
readonly
Returns the value of attribute file_version.
-
#input_method ⇒ Object
readonly
Returns the value of attribute input_method.
-
#isearch_mark ⇒ Object
readonly
Returns the value of attribute isearch_mark.
-
#keymap ⇒ Object
Returns the value of attribute keymap.
-
#last_match ⇒ Object
readonly
Returns the value of attribute last_match.
-
#mark_active ⇒ Object
readonly
Returns the value of attribute mark_active.
-
#marks ⇒ Object
readonly
Returns the value of attribute marks.
-
#mode ⇒ Object
Returns the value of attribute mode.
-
#name ⇒ Object
Returns the value of attribute name.
-
#point ⇒ Object
readonly
Returns the value of attribute point.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
-
#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
- .bury(buffer = @@current) ⇒ 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
- .global_mark_ring ⇒ Object
- .kill_em_all ⇒ Object
- .last ⇒ Object
- .list ⇒ 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
- .other(buffer = @@current) ⇒ Object
- .region_boundaries(s, e) ⇒ Object
Instance Method Summary collapse
- #[](name) ⇒ Object
- #[]=(name, value) ⇒ Object
- #activate_mark ⇒ 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, regexp: WORD_COMPONENT_REGEXP) ⇒ 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)
- #capitalize_word(n = 1) ⇒ Object
- #char_after(location = @point) ⇒ Object
- #char_before(location = @point) ⇒ Object
- #clear ⇒ Object
- #composite_edit ⇒ Object
- #convert_word(n = 1, &block) ⇒ Object
- #copy_region(s = @point, e = mark, append = false) ⇒ Object
- #current? ⇒ Boolean
- #current_symbol ⇒ Object
- #deactivate_mark ⇒ Object
- #delete_char(n = 1) ⇒ Object
- #delete_isearch_mark ⇒ Object
- #delete_region(s = @point, e = mark) ⇒ Object
- #delete_visible_mark ⇒ Object
- #disable_input_method ⇒ Object
- #display_width(s) ⇒ Object
- #downcase_word(n = 1) ⇒ 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
- #expand_tab(s) ⇒ Object
- #file_modified? ⇒ Boolean
- #filter_event(event) ⇒ Object
- #forward_char(n = 1) ⇒ Object
- #forward_line(n = 1) ⇒ Object
- #forward_word(n = 1, regexp: WORD_COMPONENT_REGEXP) ⇒ Object
- #gap_filled_with_nul? ⇒ Boolean
- #get_line_and_column(pos) ⇒ Object
- #goto_char(pos) ⇒ Object
- #goto_line(n) ⇒ Object
- #gsub(*args, &block) ⇒ Object
- #indent_to(column) ⇒ Object
-
#initialize(s = +"",, name: nil, file_name: nil, file_encoding: , file_mtime: nil, new_file: true, undo_limit: UNDO_LIMIT, read_only: false) ⇒ Buffer
constructor
s might not be copied.
- #input_method_status ⇒ Object
- #insert(x, merge_undo = false) ⇒ Object
- #insert_final_newline ⇒ 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_active? ⇒ Boolean
- #mark_to_point(mark) ⇒ Object
- #match_beginning(n) ⇒ Object
- #match_end(n) ⇒ Object
- #match_string(n) ⇒ Object
- #minor_mode_active?(mode_class) ⇒ Boolean
- #mode_names ⇒ Object
- #modified=(modified) ⇒ Object
- #modified? ⇒ Boolean
- #new_file? ⇒ Boolean
- #new_mark(location = @point) ⇒ Object
- #newline ⇒ Object
- #next_line(n = 1) ⇒ Object
- #on(name, &callback) ⇒ Object
- #on_global_mark_ring? ⇒ Boolean
- #on_killed(&callback) ⇒ Object
- #on_modified(&callback) ⇒ Object
- #point_after_mark?(mark) ⇒ Boolean
- #point_at_mark?(mark) ⇒ Boolean
- #point_before_mark?(mark) ⇒ Boolean
- #point_compare_mark(mark) ⇒ Object
- #point_max ⇒ Object
- #point_min ⇒ Object
- #point_to_mark(mark) ⇒ Object
- #pop_mark ⇒ Object
- #pop_to_mark ⇒ Object
- #pos_to_line_and_column(pos) ⇒ Object
- #previous_line(n = 1) ⇒ Object
- #push_global_mark(pos = @point, force: false) ⇒ Object
-
#push_mark(pos = @point) ⇒ Object
Set mark at pos, and push the mark on the mark ring.
- #re_search_backward(s, raise_error: true, count: 1) ⇒ Object
- #re_search_forward(s, raise_error: true, goto_beginning: false, count: 1) ⇒ Object
- #read_only=(value) ⇒ Object
- #read_only? ⇒ Boolean
- #read_only_edit ⇒ Object
- #redo ⇒ Object
- #replace(str = nil, start: point_min, end: point_max, &block) ⇒ Object
- #replace_match(str) ⇒ Object
- #replace_regexp_forward(regexp, to_str) ⇒ Object
- #replace_region(str = nil, &block) ⇒ Object
- #revert(enc = nil) ⇒ 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_isearch_mark(pos = @point) ⇒ Object
- #set_mark(pos = @point) ⇒ Object
- #set_minor_mode(mode_class, arg = nil) ⇒ Object
- #set_read_only(value) ⇒ Object
- #set_visible_mark(pos = @point) ⇒ Object
- #skip_re_backward(re) ⇒ Object
- #skip_re_forward(re) ⇒ Object
- #substring(s, e) ⇒ Object
- #to_s ⇒ Object
- #toggle_input_method(name) ⇒ Object
- #transpose_chars ⇒ Object
- #trim_trailing_whitespace ⇒ Object
- #undo ⇒ Object
- #upcase_word(n = 1) ⇒ Object
- #yank ⇒ Object
- #yank_pop ⇒ Object
Constructor Details
#initialize(s = +"",, name: nil, file_name: nil, file_encoding: , file_mtime: nil, new_file: true, undo_limit: UNDO_LIMIT, read_only: false) ⇒ Buffer
s might not be copied.
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/textbringer/buffer.rb', line 224 def initialize(s = +"", name: nil, file_name: nil, file_encoding: CONFIG[:default_file_encoding], file_mtime: nil, new_file: true, undo_limit: UNDO_LIMIT, read_only: false) @version = 0 set_contents(s, file_encoding) @name = name @file_name = file_name self.file_encoding = file_encoding @file_mtime = file_mtime @new_file = new_file @undo_limit = undo_limit @point = 0 @gap_start = 0 @gap_end = 0 @marks = [] @mark = nil @mark_ring = Ring.new(CONFIG[:mark_ring_max], on_delete: ->(mark) { mark.delete }) @current_line = 1 @current_column = 1 # One-based character count @goal_column = nil # Zero-based display width count @undo_stack = [] @redo_stack = [] @undoing = false @composite_edit_level = 0 @composite_edit_actions = [] @file_version = 0 @modified = false @mode = FundamentalMode.new(self) @minor_modes = [] @keymap = nil @attributes = {} @save_point_level = 0 @match_offsets = [] @visible_mark = nil @isearch_mark = nil @mark_active = false @read_only = read_only @callbacks = {} @last_match = nil @input_method = nil 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.
12 13 14 |
# File 'lib/textbringer/buffer.rb', line 12 def file_encoding @file_encoding end |
#file_format ⇒ Object
Returns the value of attribute file_format.
12 13 14 |
# File 'lib/textbringer/buffer.rb', line 12 def file_format @file_format end |
#file_name ⇒ Object
Returns the value of attribute file_name.
12 13 14 |
# File 'lib/textbringer/buffer.rb', line 12 def file_name @file_name end |
#file_version ⇒ Object (readonly)
Returns the value of attribute file_version.
12 13 14 |
# File 'lib/textbringer/buffer.rb', line 12 def file_version @file_version end |
#input_method ⇒ Object (readonly)
Returns the value of attribute input_method.
16 17 18 |
# File 'lib/textbringer/buffer.rb', line 16 def input_method @input_method end |
#isearch_mark ⇒ Object (readonly)
Returns the value of attribute isearch_mark.
14 15 16 |
# File 'lib/textbringer/buffer.rb', line 14 def isearch_mark @isearch_mark end |
#keymap ⇒ Object
Returns the value of attribute keymap.
11 12 13 |
# File 'lib/textbringer/buffer.rb', line 11 def keymap @keymap end |
#last_match ⇒ Object (readonly)
Returns the value of attribute last_match.
15 16 17 |
# File 'lib/textbringer/buffer.rb', line 15 def last_match @last_match end |
#mark_active ⇒ Object (readonly)
Returns the value of attribute mark_active.
14 15 16 |
# File 'lib/textbringer/buffer.rb', line 14 def mark_active @mark_active end |
#marks ⇒ Object (readonly)
Returns the value of attribute marks.
12 13 14 |
# File 'lib/textbringer/buffer.rb', line 12 def marks @marks end |
#mode ⇒ Object
Returns the value of attribute mode.
11 12 13 |
# File 'lib/textbringer/buffer.rb', line 11 def mode @mode end |
#name ⇒ Object
Returns the value of attribute name.
12 13 14 |
# File 'lib/textbringer/buffer.rb', line 12 def name @name end |
#point ⇒ Object (readonly)
Returns the value of attribute point.
12 13 14 |
# File 'lib/textbringer/buffer.rb', line 12 def point @point end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
12 13 14 |
# File 'lib/textbringer/buffer.rb', line 12 def version @version 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
151 152 153 |
# File 'lib/textbringer/buffer.rb', line 151 def self.[](name) @@table[name] end |
.add(buffer) ⇒ Object
106 107 108 109 |
# File 'lib/textbringer/buffer.rb', line 106 def self.add(buffer) @@table[buffer.name] = buffer @@list.push(buffer) end |
.auto_detect_encodings ⇒ Object
86 87 88 |
# File 'lib/textbringer/buffer.rb', line 86 def self.auto_detect_encodings @@auto_detect_encodings end |
.auto_detect_encodings=(encodings) ⇒ Object
90 91 92 |
# File 'lib/textbringer/buffer.rb', line 90 def self.auto_detect_encodings=(encodings) @@auto_detect_encodings = encodings end |
.bury(buffer = @@current) ⇒ Object
141 142 143 144 145 |
# File 'lib/textbringer/buffer.rb', line 141 def self.bury(buffer = @@current) @@list.delete(buffer) @@list.push(buffer) @@current = @@list.first end |
.count ⇒ Object
147 148 149 |
# File 'lib/textbringer/buffer.rb', line 147 def self.count @@table.size end |
.current ⇒ Object
111 112 113 |
# File 'lib/textbringer/buffer.rb', line 111 def self.current @@current end |
.current=(buffer) ⇒ Object
115 116 117 118 119 120 121 122 123 |
# File 'lib/textbringer/buffer.rb', line 115 def self.current=(buffer) @@current&.input_method&.on_deactivate if buffer && buffer.name && @@table.key?(buffer.name) @@list.delete(buffer) @@list.unshift(buffer) end @@current = buffer @@current&.input_method&.on_activate end |
.detect_encoding_proc ⇒ Object
94 95 96 |
# File 'lib/textbringer/buffer.rb', line 94 def self.detect_encoding_proc @@detect_encoding_proc end |
.detect_encoding_proc=(f) ⇒ Object
98 99 100 |
# File 'lib/textbringer/buffer.rb', line 98 def self.detect_encoding_proc=(f) @@detect_encoding_proc = f end |
.display_width(s) ⇒ Object
206 207 208 |
# File 'lib/textbringer/buffer.rb', line 206 def self.display_width(s) Unicode::DisplayWidth.of(s, CONFIG[:east_asian_ambiguous_width]) end |
.dump_unsaved_buffers(dir) ⇒ Object
1402 1403 1404 1405 1406 1407 1408 1409 |
# File 'lib/textbringer/buffer.rb', line 1402 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
1411 1412 1413 |
# File 'lib/textbringer/buffer.rb', line 1411 def self.dumped_buffers_exist?(dir) !Dir.glob(File.("*.metadata", dir)).empty? end |
.each(&block) ⇒ Object
202 203 204 |
# File 'lib/textbringer/buffer.rb', line 202 def self.each(&block) @@list.each(&block) end |
.find_file(file_name) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/textbringer/buffer.rb', line 169 def self.find_file(file_name) file_name = File.(file_name) buffer = @@table.each_value.find { |b| b.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
155 156 157 |
# File 'lib/textbringer/buffer.rb', line 155 def self.find_or_new(name, **opts) @@table[name] ||= new_buffer(name, **opts) end |
.global_mark_ring ⇒ Object
129 130 131 |
# File 'lib/textbringer/buffer.rb', line 129 def self.global_mark_ring @@global_mark_ring ||= Ring.new(CONFIG[:global_mark_ring_max]) end |
.kill_em_all ⇒ Object
163 164 165 166 167 |
# File 'lib/textbringer/buffer.rb', line 163 def self.kill_em_all @@table.clear @@list.clear @@current = nil end |
.last ⇒ Object
137 138 139 |
# File 'lib/textbringer/buffer.rb', line 137 def self.last @@list.last end |
.list ⇒ Object
102 103 104 |
# File 'lib/textbringer/buffer.rb', line 102 def self.list @@list.dup end |
.load(path) ⇒ Object
1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 |
# File 'lib/textbringer/buffer.rb', line 1391 def self.load(path) buffer = Buffer.new(File.binread(path)) = JSON.parse(File.binread(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
1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 |
# File 'lib/textbringer/buffer.rb', line 1415 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
125 126 127 |
# File 'lib/textbringer/buffer.rb', line 125 def self.minibuffer @@minibuffer ||= Buffer.new(name: "*Minibuffer*") end |
.names ⇒ Object
159 160 161 |
# File 'lib/textbringer/buffer.rb', line 159 def self.names @@table.keys end |
.new_buffer(name, **opts) ⇒ Object
186 187 188 189 190 |
# File 'lib/textbringer/buffer.rb', line 186 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
192 193 194 195 196 197 198 199 200 |
# File 'lib/textbringer/buffer.rb', line 192 def self.new_buffer_name(name) if @@table.key?(name) (2..).lazy.map { |i| "#{name}<#{i}>" }.find { |i| !@@table.key?(i) } else name end end |
.open(file_name, name: File.basename(file_name)) ⇒ Object
397 398 399 400 401 402 403 |
# File 'lib/textbringer/buffer.rb', line 397 def self.open(file_name, name: File.basename(file_name)) buffer = Buffer.new(name: name, file_name: file_name, new_file: false) buffer.revert buffer.read_only = !File.writable?(file_name) buffer end |
.other(buffer = @@current) ⇒ Object
133 134 135 |
# File 'lib/textbringer/buffer.rb', line 133 def self.other(buffer = @@current) @@list.find { |buf| buf != buffer } || Buffer.find_or_new("*scratch*") end |
.region_boundaries(s, e) ⇒ Object
992 993 994 995 996 997 998 |
# File 'lib/textbringer/buffer.rb', line 992 def self.region_boundaries(s, e) if s > e [e, s] else [s, e] end end |
Instance Method Details
#[](name) ⇒ Object
381 382 383 384 385 386 387 |
# File 'lib/textbringer/buffer.rb', line 381 def [](name) if @attributes.key?(name) @attributes[name] else CONFIG[name] end end |
#[]=(name, value) ⇒ Object
389 390 391 |
# File 'lib/textbringer/buffer.rb', line 389 def []=(name, value) @attributes[name] = value end |
#activate_mark ⇒ Object
978 979 980 981 |
# File 'lib/textbringer/buffer.rb', line 978 def activate_mark @mark_active = true set_visible_mark(@mark.location) if @mark end |
#apply_mode(mode_class) ⇒ Object
1327 1328 1329 1330 1331 |
# File 'lib/textbringer/buffer.rb', line 1327 def apply_mode(mode_class) @keymap = nil @mode = mode_class.new(self) Utils.run_hooks(mode_class.hook_name) end |
#backward_char(n = 1) ⇒ Object
698 699 700 |
# File 'lib/textbringer/buffer.rb', line 698 def backward_char(n = 1) forward_char(-n) end |
#backward_delete_char(n = 1) ⇒ Object
687 688 689 |
# File 'lib/textbringer/buffer.rb', line 687 def backward_delete_char(n = 1) delete_char(-n) end |
#backward_line(n = 1) ⇒ Object
748 749 750 |
# File 'lib/textbringer/buffer.rb', line 748 def backward_line(n = 1) forward_line(-n) end |
#backward_word(n = 1, regexp: WORD_COMPONENT_REGEXP) ⇒ Object
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 |
# File 'lib/textbringer/buffer.rb', line 714 def backward_word(n = 1, regexp: WORD_COMPONENT_REGEXP) return forward_word(-n) if n < 0 n.times do break if beginning_of_buffer? backward_char while !beginning_of_buffer? && regexp !~ char_after backward_char end while !beginning_of_buffer? && regexp =~ char_after backward_char end if regexp !~ char_after forward_char end end end |
#beginning_of_buffer ⇒ Object
773 774 775 776 777 778 779 |
# File 'lib/textbringer/buffer.rb', line 773 def beginning_of_buffer if @save_point_level == 0 @current_line = 1 @current_column = 1 end @point = 0 end |
#beginning_of_buffer? ⇒ Boolean
781 782 783 |
# File 'lib/textbringer/buffer.rb', line 781 def beginning_of_buffer? @point == 0 end |
#beginning_of_line ⇒ Object
793 794 795 796 797 798 |
# File 'lib/textbringer/buffer.rb', line 793 def beginning_of_line while !beginning_of_line? backward_char end @point end |
#beginning_of_line? ⇒ Boolean
800 801 802 |
# File 'lib/textbringer/buffer.rb', line 800 def beginning_of_line? beginning_of_buffer? || byte_before == "\n" end |
#binary? ⇒ Boolean
300 301 302 |
# File 'lib/textbringer/buffer.rb', line 300 def binary? @binary end |
#byte_after(location = @point) ⇒ Object
493 494 495 496 497 498 499 |
# File 'lib/textbringer/buffer.rb', line 493 def byte_after(location = @point) if location < @gap_start @contents.byteslice(location) else @contents.byteslice(location + gap_size) end&.force_encoding(Encoding::ASCII_8BIT) end |
#byte_before(location = @point) ⇒ Object
501 502 503 504 505 506 507 |
# File 'lib/textbringer/buffer.rb', line 501 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
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 |
# File 'lib/textbringer/buffer.rb', line 1207 def byteindex(forward, re, pos) @match_offsets = [] @last_match = nil method = forward ? :byteindex : :byterindex adjust_gap(0, 0) s = @contents.byteslice(@gap_end..-1) unless binary? s.force_encoding(Encoding::UTF_8) end i = s.send(method, re, pos) if i @last_match = Regexp.last_match (0 .. @last_match.size - 1).each do |j| @match_offsets.push(@last_match.byteoffset(j)) end i else nil end end |
#bytesize ⇒ Object Also known as: size
531 532 533 |
# File 'lib/textbringer/buffer.rb', line 531 def bytesize @contents.bytesize - gap_size end |
#capitalize_word(n = 1) ⇒ Object
1120 1121 1122 |
# File 'lib/textbringer/buffer.rb', line 1120 def capitalize_word(n = 1) convert_word(n, &:capitalize) end |
#char_after(location = @point) ⇒ Object
509 510 511 512 513 514 515 516 |
# File 'lib/textbringer/buffer.rb', line 509 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 |
#char_before(location = @point) ⇒ Object
518 519 520 521 522 523 524 525 526 527 528 529 |
# File 'lib/textbringer/buffer.rb', line 518 def char_before(location = @point) if @binary byte_before(location) else if beginning_of_buffer? nil else pos = get_pos(location, -1) substring(pos, location) end end end |
#clear ⇒ Object
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 |
# File 'lib/textbringer/buffer.rb', line 1059 def clear check_read_only_flag @version += 1 @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 self.modified = true @undo_stack.clear @redo_stack.clear end |
#composite_edit ⇒ Object
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 |
# File 'lib/textbringer/buffer.rb', line 1307 def composite_edit location = @point @composite_edit_level += 1 begin yield ensure @composite_edit_level -= 1 if @composite_edit_level == 0 && !@composite_edit_actions.empty? action = CompositeAction.new(self, location) @composite_edit_actions.each do |i| action.add_action(i) end action.file_version = @composite_edit_actions.first.file_version push_undo(action) @composite_edit_actions.clear end end fire_callbacks(:modified) end |
#convert_word(n = 1, &block) ⇒ Object
1103 1104 1105 1106 1107 1108 1109 1110 |
# File 'lib/textbringer/buffer.rb', line 1103 def convert_word(n = 1, &block) s = point forward_word(n) e = point s, e = Buffer.region_boundaries(s, e) str = substring(s, e).gsub(/[\p{Letter}\p{Number}]+/, &block) replace(str, start: s, end: e) end |
#copy_region(s = @point, e = mark, append = false) ⇒ Object
1000 1001 1002 1003 1004 1005 1006 1007 1008 |
# File 'lib/textbringer/buffer.rb', line 1000 def copy_region(s = @point, e = mark, append = false) s, e = Buffer.region_boundaries(s, e) str = substring(s, e) if append && !KILL_RING.empty? KILL_RING.current.concat(str) else KILL_RING.push(str) end end |
#current? ⇒ Boolean
357 358 359 |
# File 'lib/textbringer/buffer.rb', line 357 def current? @@current == self end |
#current_symbol ⇒ Object
1426 1427 1428 1429 1430 |
# File 'lib/textbringer/buffer.rb', line 1426 def current_symbol from = save_point { skip_re_backward(@mode.symbol_pattern); @point } to = save_point { skip_re_forward(@mode.symbol_pattern); @point } from < to ? substring(from, to) : nil end |
#deactivate_mark ⇒ Object
983 984 985 986 |
# File 'lib/textbringer/buffer.rb', line 983 def deactivate_mark @mark_active = false delete_visible_mark end |
#delete_char(n = 1) ⇒ Object
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 |
# File 'lib/textbringer/buffer.rb', line 645 def delete_char(n = 1) check_read_only_flag adjust_gap s = @point pos = get_pos(@point, n) if n > 0 @version += 1 str = substring(s, pos) # fill the gap with NUL to avoid invalid byte sequence in UTF-8 @contents.bytesplice(@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)) self.modified = true Utils.run_hooks(:after_change_functions, s, s, str) unless @undoing || !current? elsif n < 0 @version += 1 str = substring(pos, s) update_line_and_column(@point, pos) # fill the gap with NUL to avoid invalid byte sequence in UTF-8 @contents.bytesplice(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)) self.modified = true Utils.run_hooks(:after_change_functions, pos, pos, str) unless @undoing || !current? end @goal_column = nil end |
#delete_isearch_mark ⇒ Object
971 972 973 974 975 976 |
# File 'lib/textbringer/buffer.rb', line 971 def delete_isearch_mark if @isearch_mark @isearch_mark.delete @isearch_mark = nil end end |
#delete_region(s = @point, e = mark) ⇒ Object
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 |
# File 'lib/textbringer/buffer.rb', line 1015 def delete_region(s = @point, e = mark) check_read_only_flag old_pos = @point s, e = Buffer.region_boundaries(s, e) update_line_and_column(old_pos, s) save_point do str = substring(s, e) @version += 1 @point = s adjust_gap len = e - s # fill the gap with NUL to avoid invalid byte sequence in UTF-8 @contents.bytesplice(@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)) self.modified = true Utils.run_hooks(:after_change_functions, s, s, str) unless @undoing || !current? end end |
#delete_visible_mark ⇒ Object
959 960 961 962 963 964 |
# File 'lib/textbringer/buffer.rb', line 959 def delete_visible_mark if @visible_mark @visible_mark.delete @visible_mark = nil end end |
#disable_input_method ⇒ Object
1490 1491 1492 |
# File 'lib/textbringer/buffer.rb', line 1490 def disable_input_method @input_method&.disable end |
#display_width(s) ⇒ Object
219 220 221 |
# File 'lib/textbringer/buffer.rb', line 219 def display_width(s) Buffer.display_width((s)) end |
#downcase_word(n = 1) ⇒ Object
1112 1113 1114 |
# File 'lib/textbringer/buffer.rb', line 1112 def downcase_word(n = 1) convert_word(n, &:downcase) end |
#dump(path) ⇒ Object
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 |
# File 'lib/textbringer/buffer.rb', line 1380 def dump(path) File.binwrite(path, to_s) = { "name" => name, "file_name" => file_name, "file_encoding" => file_encoding.name, "file_format" => file_format.to_s } File.binwrite(path + ".metadata", .to_json) end |
#end_of_buffer ⇒ Object
785 786 787 |
# File 'lib/textbringer/buffer.rb', line 785 def end_of_buffer goto_char(bytesize) end |
#end_of_buffer? ⇒ Boolean
789 790 791 |
# File 'lib/textbringer/buffer.rb', line 789 def end_of_buffer? @point == bytesize end |
#end_of_line ⇒ Object
804 805 806 807 808 809 |
# File 'lib/textbringer/buffer.rb', line 804 def end_of_line while !end_of_line? forward_char end @point end |
#end_of_line? ⇒ Boolean
811 812 813 |
# File 'lib/textbringer/buffer.rb', line 811 def end_of_line? end_of_buffer? || byte_after == "\n" end |
#exchange_point_and_mark(mark = @mark) ⇒ Object
845 846 847 848 849 850 851 |
# File 'lib/textbringer/buffer.rb', line 845 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 |
#expand_tab(s) ⇒ Object
210 211 212 213 214 215 216 217 |
# File 'lib/textbringer/buffer.rb', line 210 def (s) # TODO: Support multibyte characters tw = self[:tab_width] fmt = "A#{tw}" s.b.gsub(/([^\t]{#{tw}})|([^\t]*)\t/n) { [$+].pack(fmt) }.force_encoding(Encoding::UTF_8) end |
#file_modified? ⇒ Boolean
465 466 467 |
# File 'lib/textbringer/buffer.rb', line 465 def file_modified? !@file_mtime.nil? && File.mtime(@file_name) != @file_mtime end |
#filter_event(event) ⇒ Object
1494 1495 1496 1497 1498 1499 1500 |
# File 'lib/textbringer/buffer.rb', line 1494 def filter_event(event) if @input_method @input_method.filter_event(event) else event end end |
#forward_char(n = 1) ⇒ Object
691 692 693 694 695 696 |
# File 'lib/textbringer/buffer.rb', line 691 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
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 |
# File 'lib/textbringer/buffer.rb', line 731 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, regexp: WORD_COMPONENT_REGEXP) ⇒ Object
702 703 704 705 706 707 708 709 710 711 712 |
# File 'lib/textbringer/buffer.rb', line 702 def forward_word(n = 1, regexp: WORD_COMPONENT_REGEXP) return backward_word(-n) if n < 0 n.times do while !end_of_buffer? && regexp !~ char_after forward_char end while !end_of_buffer? && regexp =~ char_after forward_char end end end |
#gap_filled_with_nul? ⇒ Boolean
1303 1304 1305 |
# File 'lib/textbringer/buffer.rb', line 1303 def gap_filled_with_nul? @contents.byteslice(@gap_start...@gap_end)&.match?(/\A\0*\z/) end |
#get_line_and_column(pos) ⇒ Object
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 |
# File 'lib/textbringer/buffer.rb', line 544 def get_line_and_column(pos) line = 1 + @contents.byteslice(0...user_to_gap(pos)).count("\n") if pos == point_min column = 1 else begin i = @contents.byterindex("\n", user_to_gap(get_pos(pos, -1))) rescue RangeError i = nil end 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
564 565 566 567 568 569 570 571 572 573 574 575 576 577 |
# File 'lib/textbringer/buffer.rb', line 564 def goto_char(pos) return pos if pos == @point if pos < 0 || pos > size raise RangeError, "Out of buffer" end if !@binary && byte_after(pos)&.match?(/[\x80-\xbf]/n) 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
579 580 581 582 583 584 585 586 587 588 589 590 591 592 |
# File 'lib/textbringer/buffer.rb', line 579 def goto_line(n) pos = point_min i = 1 while i < n && pos < @contents.bytesize pos = @contents.byteindex("\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 |
#gsub(*args, &block) ⇒ Object
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 |
# File 'lib/textbringer/buffer.rb', line 1444 def gsub(*args, &block) if block s = to_s.gsub(*args) { |*params| block.binding.eval('->(backref) { $~ = backref }').call($~) block.call(*params) } else s = to_s.gsub(*args) end composite_edit do delete_region(point_min, point_max) insert(s) end self end |
#indent_to(column) ⇒ Object
1371 1372 1373 1374 1375 1376 1377 1378 |
# File 'lib/textbringer/buffer.rb', line 1371 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 |
#input_method_status ⇒ Object
1502 1503 1504 1505 1506 1507 1508 |
# File 'lib/textbringer/buffer.rb', line 1502 def input_method_status if @input_method&.enabled? @input_method.status else "--" end end |
#insert(x, merge_undo = false) ⇒ Object
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 |
# File 'lib/textbringer/buffer.rb', line 594 def insert(x, merge_undo = false) s = x.to_s if !binary? && !s.valid_encoding? raise EditorError, "Invalid encoding: #{s.dump}" end check_read_only_flag pos = @point size = s.bytesize adjust_gap(size) @contents.bytesplice(@point, size, s.b) @marks.each do |m| if m.location > @point m.location += size end end @point = @gap_start += size @version += 1 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 self.modified = true @goal_column = nil Utils.run_hooks(:after_change_functions, pos, @point, "") unless @undoing || !current? self end |
#insert_final_newline ⇒ Object
1472 1473 1474 1475 1476 1477 1478 1479 |
# File 'lib/textbringer/buffer.rb', line 1472 def insert_final_newline save_excursion do end_of_buffer if char_before != "\n" insert("\n") end end end |
#insert_for_yank(s) ⇒ Object
1124 1125 1126 1127 1128 1129 |
# File 'lib/textbringer/buffer.rb', line 1124 def insert_for_yank(s) if @mark.nil? || !point_at_mark?(@mark) push_mark end insert(s) end |
#inspect ⇒ Object
269 270 271 |
# File 'lib/textbringer/buffer.rb', line 269 def inspect "#<Buffer:#{@name || '0x%x' % object_id}>" end |
#kill ⇒ Object
341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/textbringer/buffer.rb', line 341 def kill @@table.delete(@name) @@list.delete(self) if @@current == self @@current = nil end @marks.each do |mark| mark.detach end fire_callbacks(:killed) end |
#kill_line(append = false) ⇒ Object
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 |
# File 'lib/textbringer/buffer.rb', line 1075 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
1010 1011 1012 1013 |
# File 'lib/textbringer/buffer.rb', line 1010 def kill_region(s = @point, e = mark, append = false) copy_region(s, e, append) delete_region(s, e) end |
#kill_word(append = false) ⇒ Object
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 |
# File 'lib/textbringer/buffer.rb', line 1091 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
1198 1199 1200 1201 1202 1203 1204 1205 |
# File 'lib/textbringer/buffer.rb', line 1198 def looking_at?(re) if re.is_a?(Regexp) r = /\G#{re}/ else r = "\\G(?:#{re})" end byteindex(true, r, @point) == @point end |
#mark ⇒ Object
887 888 889 890 891 892 |
# File 'lib/textbringer/buffer.rb', line 887 def mark if @mark.nil? raise EditorError, "The mark is not set" end @mark.location end |
#mark_active? ⇒ Boolean
988 989 990 |
# File 'lib/textbringer/buffer.rb', line 988 def mark_active? @mark_active end |
#mark_to_point(mark) ⇒ Object
825 826 827 |
# File 'lib/textbringer/buffer.rb', line 825 def mark_to_point(mark) mark.location = @point end |
#match_beginning(n) ⇒ Object
1228 1229 1230 |
# File 'lib/textbringer/buffer.rb', line 1228 def match_beginning(n) @match_offsets[n]&.first end |
#match_end(n) ⇒ Object
1232 1233 1234 |
# File 'lib/textbringer/buffer.rb', line 1232 def match_end(n) @match_offsets[n]&.last end |
#match_string(n) ⇒ Object
1236 1237 1238 1239 1240 1241 1242 1243 |
# File 'lib/textbringer/buffer.rb', line 1236 def match_string(n) b, e = @match_offsets[n] if b.nil? nil else substring(b, e) end end |
#minor_mode_active?(mode_class) ⇒ Boolean
1358 1359 1360 |
# File 'lib/textbringer/buffer.rb', line 1358 def minor_mode_active?(mode_class) @minor_modes.any? { |mode| mode.instance_of?(mode_class) } end |
#mode_names ⇒ Object
1362 1363 1364 1365 1366 1367 1368 1369 |
# File 'lib/textbringer/buffer.rb', line 1362 def mode_names names = [] names.push(mode&.name || 'None') @minor_modes.each do |mode| names.push(mode.name) end names end |
#modified=(modified) ⇒ Object
366 367 368 369 370 371 |
# File 'lib/textbringer/buffer.rb', line 366 def modified=(modified) @modified = modified if @composite_edit_level == 0 && modified fire_callbacks(:modified) end end |
#modified? ⇒ Boolean
373 374 375 |
# File 'lib/textbringer/buffer.rb', line 373 def modified? @modified end |
#new_file? ⇒ Boolean
393 394 395 |
# File 'lib/textbringer/buffer.rb', line 393 def new_file? @new_file end |
#new_mark(location = @point) ⇒ Object
815 816 817 818 819 |
# File 'lib/textbringer/buffer.rb', line 815 def new_mark(location = @point) Mark.new(self, location).tap { |m| @marks << m } end |
#newline ⇒ Object
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 |
# File 'lib/textbringer/buffer.rb', line 626 def newline indentation = save_point { |saved| if char_after&.match?(/[ \t]/) next "" end beginning_of_line s = @point while char_after&.match?(/[ \t]/) 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
752 753 754 755 756 757 758 759 760 |
# File 'lib/textbringer/buffer.rb', line 752 def next_line(n = 1) column = get_goal_column n.times do end_of_line forward_char adjust_column(column) end @goal_column = column end |
#on(name, &callback) ⇒ Object
361 362 363 364 |
# File 'lib/textbringer/buffer.rb', line 361 def on(name, &callback) @callbacks[name] ||= [] @callbacks[name].push(callback) end |
#on_global_mark_ring? ⇒ Boolean
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 |
# File 'lib/textbringer/buffer.rb', line 917 def on_global_mark_ring? mark_ring = Buffer.global_mark_ring if mark_ring.empty? return false end current = mark_ring.current if current&.buffer == self return true end next_mark = mark_ring[-1] if next_mark&.buffer == self return true end false end |
#on_killed(&callback) ⇒ Object
353 354 355 |
# File 'lib/textbringer/buffer.rb', line 353 def on_killed(&callback) on(:killed, &callback) end |
#on_modified(&callback) ⇒ Object
377 378 379 |
# File 'lib/textbringer/buffer.rb', line 377 def on_modified(&callback) on(:modified, &callback) end |
#point_after_mark?(mark) ⇒ Boolean
837 838 839 |
# File 'lib/textbringer/buffer.rb', line 837 def point_after_mark?(mark) @point > mark.location end |
#point_at_mark?(mark) ⇒ Boolean
829 830 831 |
# File 'lib/textbringer/buffer.rb', line 829 def point_at_mark?(mark) @point == mark.location end |
#point_before_mark?(mark) ⇒ Boolean
833 834 835 |
# File 'lib/textbringer/buffer.rb', line 833 def point_before_mark?(mark) @point < mark.location end |
#point_compare_mark(mark) ⇒ Object
841 842 843 |
# File 'lib/textbringer/buffer.rb', line 841 def point_compare_mark(mark) @point - mark.location end |
#point_max ⇒ Object
540 541 542 |
# File 'lib/textbringer/buffer.rb', line 540 def point_max bytesize end |
#point_min ⇒ Object
536 537 538 |
# File 'lib/textbringer/buffer.rb', line 536 def point_min 0 end |
#point_to_mark(mark) ⇒ Object
821 822 823 |
# File 'lib/textbringer/buffer.rb', line 821 def point_to_mark(mark) goto_char(mark.location) end |
#pop_mark ⇒ Object
944 945 946 947 |
# File 'lib/textbringer/buffer.rb', line 944 def pop_mark return if @mark_ring.empty? @mark = @mark_ring.rotate(1) end |
#pop_to_mark ⇒ Object
949 950 951 952 |
# File 'lib/textbringer/buffer.rb', line 949 def pop_to_mark goto_char(mark) pop_mark end |
#pos_to_line_and_column(pos) ⇒ Object
1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 |
# File 'lib/textbringer/buffer.rb', line 1510 def pos_to_line_and_column(pos) return [1, 1] if pos == 0 text_before = substring(0, pos) line = text_before.count("\n") + 1 last_newline = text_before.rindex("\n") column = if last_newline text_before.size - last_newline else text_before.size + 1 end [line, column] end |
#previous_line(n = 1) ⇒ Object
762 763 764 765 766 767 768 769 770 771 |
# File 'lib/textbringer/buffer.rb', line 762 def previous_line(n = 1) column = get_goal_column n.times do beginning_of_line backward_char beginning_of_line adjust_column(column) end @goal_column = column end |
#push_global_mark(pos = @point, force: false) ⇒ Object
933 934 935 936 937 938 939 940 941 942 |
# File 'lib/textbringer/buffer.rb', line 933 def push_global_mark(pos = @point, force: false) if force || !on_global_mark_ring? mark = new_mark mark.location = pos Buffer.global_mark_ring.push(mark) true else false end end |
#push_mark(pos = @point) ⇒ Object
Set mark at pos, and push the mark on the mark ring. Unlike Emacs, the new mark is pushed on the mark ring instead of the old one.
905 906 907 908 909 910 911 912 913 914 915 |
# File 'lib/textbringer/buffer.rb', line 905 def push_mark(pos = @point) @mark = new_mark @mark.location = pos @mark_ring.push(@mark) if self != Buffer.minibuffer global_mark_ring = Buffer.global_mark_ring if global_mark_ring.empty? || global_mark_ring.current.buffer != self push_global_mark(pos) end end end |
#re_search_backward(s, raise_error: true, count: 1) ⇒ Object
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 |
# File 'lib/textbringer/buffer.rb', line 1168 def re_search_backward(s, raise_error: true, count: 1) if count < 0 return re_search_forward(s, raise_error: raise_error, count: -count) end re = new_regexp(s) pos = @point count.times do p = pos begin i = byteindex(false, re, p) if i.nil? if raise_error raise SearchError, "Search failed" else return nil end end p = get_pos(p, -1) rescue RangeError if raise_error raise SearchError, "Search failed" else return nil end end while match_end(0) > pos pos = match_beginning(0) end goto_char(pos) end |
#re_search_forward(s, raise_error: true, goto_beginning: false, count: 1) ⇒ Object
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 |
# File 'lib/textbringer/buffer.rb', line 1148 def re_search_forward(s, raise_error: true, goto_beginning: false, count: 1) if count < 0 return re_search_backward(s, raise_error: raise_error, count: -count) end re = new_regexp(s) pos = @point count.times do i = byteindex(true, re, pos) if i.nil? if raise_error raise SearchError, "Search failed" else return nil end end pos = goto_beginning ? match_beginning(0) : match_end(0) end goto_char(pos) end |
#read_only=(value) ⇒ Object
321 322 323 324 325 326 |
# File 'lib/textbringer/buffer.rb', line 321 def read_only=(value) @read_only = value if @read_only @modified = false end end |
#read_only? ⇒ Boolean
317 318 319 |
# File 'lib/textbringer/buffer.rb', line 317 def read_only? @read_only end |
#read_only_edit ⇒ Object
332 333 334 335 336 337 338 339 |
# File 'lib/textbringer/buffer.rb', line 332 def read_only_edit self.read_only = false begin yield ensure self.read_only = true end end |
#redo ⇒ Object
1144 1145 1146 |
# File 'lib/textbringer/buffer.rb', line 1144 def redo undo_or_redo(:redo, @redo_stack, @undo_stack) end |
#replace(str = nil, start: point_min, end: point_max, &block) ⇒ Object
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 |
# File 'lib/textbringer/buffer.rb', line 1042 def replace(str = nil, start: point_min, end: point_max, &block) end_pos = binding.local_variable_get(:end) if str.nil? str = block.call(substring(start, end_pos)) end composite_edit do delete_region(start, end_pos) goto_char(start) insert(str) end end |
#replace_match(str) ⇒ Object
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 |
# File 'lib/textbringer/buffer.rb', line 1245 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) composite_edit do delete_region(b, e) insert(new_str) end end |
#replace_regexp_forward(regexp, to_str) ⇒ Object
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 |
# File 'lib/textbringer/buffer.rb', line 1265 def replace_regexp_forward(regexp, to_str) result = 0 rest = substring(point, point_max) composite_edit do 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) end result end |
#replace_region(str = nil, &block) ⇒ Object
1054 1055 1056 1057 |
# File 'lib/textbringer/buffer.rb', line 1054 def replace_region(str = nil, &block) s, e = Buffer.region_boundaries(@point, mark) replace(str, start: s, end: e, &block) end |
#revert(enc = nil) ⇒ Object
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 |
# File 'lib/textbringer/buffer.rb', line 405 def revert(enc = nil) if @file_name.nil? raise EditorError, "Buffer has no file name" end clear s, mtime = File.open(@file_name, external_encoding: Encoding::ASCII_8BIT, binmode: true) { |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 set_contents(s, enc) @file_mtime = mtime @modified = false end |
#save(file_name = @file_name) ⇒ Object
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 |
# File 'lib/textbringer/buffer.rb', line 427 def save(file_name = @file_name) if file_name.nil? raise EditorError, "File name is not set" end file_name = File.(file_name) config = EditorConfig.load_file(file_name) if config["trim_trailing_whitespace"] trim_trailing_whitespace end if config["insert_final_newline"] insert_final_newline end begin File.open(file_name, "w", external_encoding: @file_encoding, binmode: true) do |f| f.flock(File::LOCK_EX) write_to_file(f) f.flush f.fsync end @file_mtime = File.mtime(file_name) 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 @file_version += 1 @modified = false @new_file = false @read_only = false end |
#save_excursion ⇒ Object
Don’t save Buffer.current.
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 |
# File 'lib/textbringer/buffer.rb', line 870 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.
855 856 857 858 859 860 861 862 863 864 865 866 867 |
# File 'lib/textbringer/buffer.rb', line 855 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_isearch_mark(pos = @point) ⇒ Object
966 967 968 969 |
# File 'lib/textbringer/buffer.rb', line 966 def set_isearch_mark(pos = @point) @isearch_mark ||= new_mark @isearch_mark.location = pos end |
#set_mark(pos = @point) ⇒ Object
894 895 896 897 898 899 900 |
# File 'lib/textbringer/buffer.rb', line 894 def set_mark(pos = @point) if @mark @mark.location = pos else push_mark(pos) end end |
#set_minor_mode(mode_class, arg = nil) ⇒ Object
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 |
# File 'lib/textbringer/buffer.rb', line 1333 def set_minor_mode(mode_class, arg = nil) mode = @minor_modes.find { |mode| mode.instance_of?(mode_class) } enabled = !!mode enable = case arg when true, false return if enabled == arg arg when nil !enabled else raise ArgumentError, "wrong argument #{arg.inspect} (expected true, false, or nil)" end if enable mode = mode_class.new(self) @minor_modes.push(mode) mode.enable else mode.disable @minor_modes.delete(mode) end end |
#set_read_only(value) ⇒ Object
328 329 330 |
# File 'lib/textbringer/buffer.rb', line 328 def set_read_only(value) @read_only = value end |
#set_visible_mark(pos = @point) ⇒ Object
954 955 956 957 |
# File 'lib/textbringer/buffer.rb', line 954 def set_visible_mark(pos = @point) @visible_mark ||= new_mark @visible_mark.location = pos end |
#skip_re_backward(re) ⇒ Object
1438 1439 1440 1441 1442 |
# File 'lib/textbringer/buffer.rb', line 1438 def skip_re_backward(re) while re =~ char_before backward_char end end |
#skip_re_forward(re) ⇒ Object
1432 1433 1434 1435 1436 |
# File 'lib/textbringer/buffer.rb', line 1432 def skip_re_forward(re) while re =~ char_after forward_char end end |
#substring(s, e) ⇒ Object
478 479 480 481 482 483 484 485 486 487 488 489 490 491 |
# File 'lib/textbringer/buffer.rb', line 478 def substring(s, e) result = if s > @gap_start || e <= @gap_start @contents.byteslice(user_to_gap(s)...user_to_gap(e)) else len = @gap_start - s @contents.byteslice(user_to_gap(s), len) + @contents.byteslice(@gap_end, e - s - len) end if !@binary result.force_encoding(Encoding::UTF_8) end result end |
#to_s ⇒ Object
469 470 471 472 473 474 475 476 |
# File 'lib/textbringer/buffer.rb', line 469 def to_s result = @contents.byteslice(0...@gap_start) + @contents.byteslice(@gap_end..-1) if !@binary result.force_encoding(Encoding::UTF_8) end result end |
#toggle_input_method(name) ⇒ Object
1481 1482 1483 1484 1485 1486 1487 1488 |
# File 'lib/textbringer/buffer.rb', line 1481 def toggle_input_method(name) if name.nil? @input_method ||= InputMethod.find(CONFIG[:default_input_method]) else @input_method = InputMethod.find(name) end @input_method.toggle end |
#transpose_chars ⇒ Object
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 |
# File 'lib/textbringer/buffer.rb', line 1289 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 |
#trim_trailing_whitespace ⇒ Object
1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 |
# File 'lib/textbringer/buffer.rb', line 1461 def trim_trailing_whitespace save_excursion do beginning_of_buffer composite_edit do while re_search_forward(/[ \t]+$/, raise_error: false) replace_match("") end end end end |
#undo ⇒ Object
1140 1141 1142 |
# File 'lib/textbringer/buffer.rb', line 1140 def undo undo_or_redo(:undo, @undo_stack, @redo_stack) end |
#upcase_word(n = 1) ⇒ Object
1116 1117 1118 |
# File 'lib/textbringer/buffer.rb', line 1116 def upcase_word(n = 1) convert_word(n, &:upcase) end |
#yank ⇒ Object
1131 1132 1133 |
# File 'lib/textbringer/buffer.rb', line 1131 def yank insert_for_yank(KILL_RING.current) end |
#yank_pop ⇒ Object
1135 1136 1137 1138 |
# File 'lib/textbringer/buffer.rb', line 1135 def yank_pop delete_region insert_for_yank(KILL_RING.rotate(1)) end |