Class: Textbringer::Buffer

Inherits:
Object
  • Object
show all
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
@@global_mark_ring =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s = String.new, 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.



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
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/textbringer/buffer.rb', line 192

def initialize(s = String.new, name: nil,
               file_name: nil,
               file_encoding: CONFIG[:default_file_encoding],
               file_mtime: nil, new_file: true, undo_limit: UNDO_LIMIT,
               read_only: false)
  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 = []
  @version = 0
  @modified = false
  @mode = FundamentalMode.new(self)
  @keymap = nil
  @attributes = {}
  @save_point_level = 0
  @match_offsets = []
  @visible_mark = nil
  @read_only = read_only
  @callbacks = {}
end

Instance Attribute Details

#current_columnObject (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_lineObject (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_encodingObject

Returns the value of attribute file_encoding.



13
14
15
# File 'lib/textbringer/buffer.rb', line 13

def file_encoding
  @file_encoding
end

#file_formatObject

Returns the value of attribute file_format.



13
14
15
# File 'lib/textbringer/buffer.rb', line 13

def file_format
  @file_format
end

#file_nameObject

Returns the value of attribute file_name.



13
14
15
# File 'lib/textbringer/buffer.rb', line 13

def file_name
  @file_name
end

#keymapObject

Returns the value of attribute keymap.



12
13
14
# File 'lib/textbringer/buffer.rb', line 12

def keymap
  @keymap
end

#marksObject (readonly)

Returns the value of attribute marks.



13
14
15
# File 'lib/textbringer/buffer.rb', line 13

def marks
  @marks
end

#modeObject

Returns the value of attribute mode.



12
13
14
# File 'lib/textbringer/buffer.rb', line 12

def mode
  @mode
end

#nameObject

Returns the value of attribute name.



13
14
15
# File 'lib/textbringer/buffer.rb', line 13

def name
  @name
end

#pointObject (readonly)

Returns the value of attribute point.



13
14
15
# File 'lib/textbringer/buffer.rb', line 13

def point
  @point
end

#visible_markObject (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



119
120
121
# File 'lib/textbringer/buffer.rb', line 119

def self.[](name)
  @@table[name]
end

.add(buffer) ⇒ Object



76
77
78
79
# File 'lib/textbringer/buffer.rb', line 76

def self.add(buffer)
  @@table[buffer.name] = buffer
  @@list.push(buffer)
end

.auto_detect_encodingsObject



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

.bury(buffer = @@current) ⇒ Object



109
110
111
112
113
# File 'lib/textbringer/buffer.rb', line 109

def self.bury(buffer = @@current)
  @@list.delete(buffer)
  @@list.push(buffer)
  @@current = @@list.first
end

.countObject



115
116
117
# File 'lib/textbringer/buffer.rb', line 115

def self.count
  @@table.size
end

.currentObject



81
82
83
# File 'lib/textbringer/buffer.rb', line 81

def self.current
  @@current
end

.current=(buffer) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/textbringer/buffer.rb', line 85

def self.current=(buffer)
  if buffer && buffer.name && @@table.key?(buffer.name)
    @@list.delete(buffer)
    @@list.unshift(buffer)
  end
  @@current = buffer
end

.detect_encoding_procObject



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



174
175
176
# File 'lib/textbringer/buffer.rb', line 174

def self.display_width(s)
  Unicode::DisplayWidth.of(s, CONFIG[:east_asian_ambiguous_width])
end

.dump_unsaved_buffers(dir) ⇒ Object



1273
1274
1275
1276
1277
1278
1279
1280
# File 'lib/textbringer/buffer.rb', line 1273

def self.dump_unsaved_buffers(dir)
  FileUtils.mkdir_p(dir)
  @@list.each do |buffer|
    if /\A\*/ !~ buffer.name && buffer.modified?
      buffer.dump(File.expand_path(buffer.object_id.to_s, dir))
    end
  end
end

.dumped_buffers_exist?(dir) ⇒ Boolean

Returns:

  • (Boolean)


1282
1283
1284
# File 'lib/textbringer/buffer.rb', line 1282

def self.dumped_buffers_exist?(dir)
  !Dir.glob(File.expand_path("*.metadata", dir)).empty?
end

.each(&block) ⇒ Object



170
171
172
# File 'lib/textbringer/buffer.rb', line 170

def self.each(&block)
  @@list.each(&block)
end

.find_file(file_name) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/textbringer/buffer.rb', line 137

def self.find_file(file_name)
  file_name = File.expand_path(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



123
124
125
# File 'lib/textbringer/buffer.rb', line 123

def self.find_or_new(name, **opts)
  @@table[name] ||= new_buffer(name, **opts)
end

.global_mark_ringObject



97
98
99
# File 'lib/textbringer/buffer.rb', line 97

def self.global_mark_ring
  @@global_mark_ring ||= Ring.new(CONFIG[:global_mark_ring_max])
end

.kill_em_allObject



131
132
133
134
135
# File 'lib/textbringer/buffer.rb', line 131

def self.kill_em_all
  @@table.clear
  @@list.clear
  @@current = nil
end

.lastObject



105
106
107
# File 'lib/textbringer/buffer.rb', line 105

def self.last
  @@list.last
end

.listObject



72
73
74
# File 'lib/textbringer/buffer.rb', line 72

def self.list
  @@list.dup
end

.load(path) ⇒ Object



1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
# File 'lib/textbringer/buffer.rb', line 1262

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



1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
# File 'lib/textbringer/buffer.rb', line 1286

def self.load_dumped_buffers(dir)
  Dir.glob(File.expand_path("*.metadata", dir)).map do ||
    path = .sub(/\.metadata\z/, "")
    buffer = Buffer.load(path)
    add(buffer)
    File.unlink()
    File.unlink(path)
    buffer
  end
end

.minibufferObject



93
94
95
# File 'lib/textbringer/buffer.rb', line 93

def self.minibuffer
  @@minibuffer ||= Buffer.new(name: "*Minibuffer*")
end

.namesObject



127
128
129
# File 'lib/textbringer/buffer.rb', line 127

def self.names
  @@table.keys
end

.new_buffer(name, **opts) ⇒ Object



154
155
156
157
158
# File 'lib/textbringer/buffer.rb', line 154

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



160
161
162
163
164
165
166
167
168
# File 'lib/textbringer/buffer.rb', line 160

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



351
352
353
354
355
356
357
# File 'lib/textbringer/buffer.rb', line 351

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



101
102
103
# File 'lib/textbringer/buffer.rb', line 101

def self.other(buffer = @@current)
  @@list.find { |buf|  buf != buffer } || Buffer.find_or_new("*scratch*")
end

.region_boundaries(s, e) ⇒ Object



886
887
888
889
890
891
892
# File 'lib/textbringer/buffer.rb', line 886

def self.region_boundaries(s, e)
  if s > e
    [e, s]
  else
    [s, e]
  end
end

Instance Method Details

#[](name) ⇒ Object



335
336
337
338
339
340
341
# File 'lib/textbringer/buffer.rb', line 335

def [](name)
  if @attributes.key?(name)
    @attributes[name]
  else
    CONFIG[name]
  end
end

#[]=(name, value) ⇒ Object



343
344
345
# File 'lib/textbringer/buffer.rb', line 343

def []=(name, value)
  @attributes[name] = value
end

#apply_mode(mode_class) ⇒ Object



1236
1237
1238
1239
1240
# File 'lib/textbringer/buffer.rb', line 1236

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



624
625
626
# File 'lib/textbringer/buffer.rb', line 624

def backward_char(n = 1)
  forward_char(-n)
end

#backward_delete_char(n = 1) ⇒ Object



613
614
615
# File 'lib/textbringer/buffer.rb', line 613

def backward_delete_char(n = 1)
  delete_char(-n)
end

#backward_line(n = 1) ⇒ Object



672
673
674
# File 'lib/textbringer/buffer.rb', line 672

def backward_line(n = 1)
  forward_line(-n)
end

#backward_word(n = 1, regexp: /\p{Letter}|\p{Number}/) ⇒ Object



639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
# File 'lib/textbringer/buffer.rb', line 639

def backward_word(n = 1, regexp: /\p{Letter}|\p{Number}/)
  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_bufferObject



697
698
699
700
701
702
703
# File 'lib/textbringer/buffer.rb', line 697

def beginning_of_buffer
  if @save_point_level == 0
    @current_line = 1
    @current_column = 1
  end
  @point = 0
end

#beginning_of_buffer?Boolean

Returns:

  • (Boolean)


705
706
707
# File 'lib/textbringer/buffer.rb', line 705

def beginning_of_buffer?
  @point == 0
end

#beginning_of_lineObject



717
718
719
720
721
722
# File 'lib/textbringer/buffer.rb', line 717

def beginning_of_line
  while !beginning_of_line?
    backward_char
  end
  @point
end

#beginning_of_line?Boolean

Returns:

  • (Boolean)


724
725
726
# File 'lib/textbringer/buffer.rb', line 724

def beginning_of_line?
  beginning_of_buffer? || byte_before == "\n"
end

#binary?Boolean

Returns:

  • (Boolean)


258
259
260
# File 'lib/textbringer/buffer.rb', line 258

def binary?
  @binary
end

#byte_after(location = @point) ⇒ Object



433
434
435
436
437
438
439
# File 'lib/textbringer/buffer.rb', line 433

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



441
442
443
444
445
446
447
# File 'lib/textbringer/buffer.rb', line 441

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



1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
# File 'lib/textbringer/buffer.rb', line 1089

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

#bytesizeObject Also known as: size



471
472
473
# File 'lib/textbringer/buffer.rb', line 471

def bytesize
  @contents.bytesize - gap_size
end

#char_after(location = @point) ⇒ Object



449
450
451
452
453
454
455
456
# File 'lib/textbringer/buffer.rb', line 449

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



458
459
460
461
462
463
464
465
466
467
468
469
# File 'lib/textbringer/buffer.rb', line 458

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

#clearObject



941
942
943
944
945
946
947
948
949
950
951
952
953
954
# File 'lib/textbringer/buffer.rb', line 941

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
  self.modified = true
  @undo_stack.clear
  @redo_stack.clear
end

#composite_editObject



1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
# File 'lib/textbringer/buffer.rb', line 1216

def composite_edit
  @composite_edit_level += 1
  begin
    yield
  ensure
    @composite_edit_level -= 1
    if @composite_edit_level == 0 && !@composite_edit_actions.empty?
      action = CompositeAction.new(self,
                                   @composite_edit_actions.first.location)
      @composite_edit_actions.each do |i|
        action.add_action(i)
      end
      action.version = @composite_edit_actions.first.version
      push_undo(action)
      @composite_edit_actions.clear
    end
  end
  fire_callbacks(:modified)
end

#copy_region(s = @point, e = mark, append = false) ⇒ Object



894
895
896
897
898
899
900
901
902
# File 'lib/textbringer/buffer.rb', line 894

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

Returns:

  • (Boolean)


311
312
313
# File 'lib/textbringer/buffer.rb', line 311

def current?
  @@current == self
end

#current_symbolObject



1297
1298
1299
1300
1301
# File 'lib/textbringer/buffer.rb', line 1297

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

#delete_char(n = 1) ⇒ Object



575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
# File 'lib/textbringer/buffer.rb', line 575

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))
    self.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))
    self.modified = true
  end
  @goal_column = nil
end

#delete_region(s = @point, e = mark) ⇒ Object



909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
# File 'lib/textbringer/buffer.rb', line 909

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)
    @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)) 
    self.modified = true
  end
end

#delete_visible_markObject



879
880
881
882
883
884
# File 'lib/textbringer/buffer.rb', line 879

def delete_visible_mark
  if @visible_mark
    @visible_mark.delete
    @visible_mark = nil
  end
end

#display_width(s) ⇒ Object



187
188
189
# File 'lib/textbringer/buffer.rb', line 187

def display_width(s)
  Buffer.display_width(expand_tab(s))
end

#dump(path) ⇒ Object



1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
# File 'lib/textbringer/buffer.rb', line 1251

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_bufferObject



709
710
711
# File 'lib/textbringer/buffer.rb', line 709

def end_of_buffer
  goto_char(bytesize)
end

#end_of_buffer?Boolean

Returns:

  • (Boolean)


713
714
715
# File 'lib/textbringer/buffer.rb', line 713

def end_of_buffer?
  @point == bytesize
end

#end_of_lineObject



728
729
730
731
732
733
# File 'lib/textbringer/buffer.rb', line 728

def end_of_line
  while !end_of_line?
    forward_char
  end
  @point
end

#end_of_line?Boolean

Returns:

  • (Boolean)


735
736
737
# File 'lib/textbringer/buffer.rb', line 735

def end_of_line?
  end_of_buffer? || byte_after == "\n"
end

#exchange_point_and_mark(mark = @mark) ⇒ Object



765
766
767
768
769
770
771
# File 'lib/textbringer/buffer.rb', line 765

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



178
179
180
181
182
183
184
185
# File 'lib/textbringer/buffer.rb', line 178

def expand_tab(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

Returns:

  • (Boolean)


411
412
413
# File 'lib/textbringer/buffer.rb', line 411

def file_modified?
  !@file_mtime.nil? && File.mtime(@file_name) != @file_mtime
end

#forward_char(n = 1) ⇒ Object



617
618
619
620
621
622
# File 'lib/textbringer/buffer.rb', line 617

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



655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
# File 'lib/textbringer/buffer.rb', line 655

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: /\p{Letter}|\p{Number}/) ⇒ Object



628
629
630
631
632
633
634
635
636
637
# File 'lib/textbringer/buffer.rb', line 628

def forward_word(n = 1, regexp: /\p{Letter}|\p{Number}/)
  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

Returns:

  • (Boolean)


1212
1213
1214
# File 'lib/textbringer/buffer.rb', line 1212

def gap_filled_with_nul?
  /\A\0*\z/ =~ @contents[@gap_start...@gap_end] ? true : false
end

#get_line_and_column(pos) ⇒ Object



484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
# File 'lib/textbringer/buffer.rb', line 484

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



500
501
502
503
504
505
506
507
508
509
510
511
512
# File 'lib/textbringer/buffer.rb', line 500

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



514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/textbringer/buffer.rb', line 514

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

#gsub(*args, &block) ⇒ Object



1315
1316
1317
1318
1319
1320
1321
1322
# File 'lib/textbringer/buffer.rb', line 1315

def gsub(*args, &block)
  s = to_s.gsub(*args, &block)
  composite_edit do
    delete_region(point_min, point_max)
    insert(s)
  end
  self
end

#indent_to(column) ⇒ Object



1242
1243
1244
1245
1246
1247
1248
1249
# File 'lib/textbringer/buffer.rb', line 1242

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(x, merge_undo = false) ⇒ Object



529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
# File 'lib/textbringer/buffer.rb', line 529

def insert(x, merge_undo = false)
  s = x.to_s
  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
  self.modified = true
  @goal_column = nil
  self
end

#insert_for_yank(s) ⇒ Object



984
985
986
987
988
989
# File 'lib/textbringer/buffer.rb', line 984

def insert_for_yank(s)
  if @mark.nil? || !point_at_mark?(@mark)
    push_mark
  end
  insert(s)
end

#inspectObject



231
232
233
# File 'lib/textbringer/buffer.rb', line 231

def inspect
  "#<Buffer:#{@name || '0x%x' % object_id}>"
end

#killObject



295
296
297
298
299
300
301
302
303
304
305
# File 'lib/textbringer/buffer.rb', line 295

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



956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
# File 'lib/textbringer/buffer.rb', line 956

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



904
905
906
907
# File 'lib/textbringer/buffer.rb', line 904

def kill_region(s = @point, e = mark, append = false)
  copy_region(s, e, append)
  delete_region(s, e)
end

#kill_word(append = false) ⇒ Object



972
973
974
975
976
977
978
979
980
981
982
# File 'lib/textbringer/buffer.rb', line 972

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

Returns:

  • (Boolean)


1080
1081
1082
1083
1084
1085
1086
1087
# File 'lib/textbringer/buffer.rb', line 1080

def looking_at?(re)
  if re.is_a?(Regexp)
    r = /\G#{re}/
  else
    r = "\\G(?:#{re})"
  end
  byteindex(true, r, @point) == @point
end

#markObject



807
808
809
810
811
812
# File 'lib/textbringer/buffer.rb', line 807

def mark
  if @mark.nil?
    raise EditorError, "The mark is not set"
  end
  @mark.location
end

#mark_to_point(mark) ⇒ Object



749
750
751
# File 'lib/textbringer/buffer.rb', line 749

def mark_to_point(mark)
  mark.location = @point
end

#match_beginning(n) ⇒ Object



1137
1138
1139
# File 'lib/textbringer/buffer.rb', line 1137

def match_beginning(n)
  @match_offsets[n]&.first
end

#match_end(n) ⇒ Object



1141
1142
1143
# File 'lib/textbringer/buffer.rb', line 1141

def match_end(n)
  @match_offsets[n]&.last
end

#match_string(n) ⇒ Object



1145
1146
1147
1148
1149
1150
1151
1152
# File 'lib/textbringer/buffer.rb', line 1145

def match_string(n)
  b, e = @match_offsets[n]
  if b.nil?
    nil
  else
    substring(b, e)
  end
end

#modified=(modified) ⇒ Object



320
321
322
323
324
325
# File 'lib/textbringer/buffer.rb', line 320

def modified=(modified)
  @modified = modified
  if @composite_edit_level == 0 && modified
    fire_callbacks(:modified)
  end
end

#modified?Boolean

Returns:

  • (Boolean)


327
328
329
# File 'lib/textbringer/buffer.rb', line 327

def modified?
  @modified
end

#new_file?Boolean

Returns:

  • (Boolean)


347
348
349
# File 'lib/textbringer/buffer.rb', line 347

def new_file?
  @new_file
end

#new_mark(location = @point) ⇒ Object



739
740
741
742
743
# File 'lib/textbringer/buffer.rb', line 739

def new_mark(location = @point)
  Mark.new(self, location).tap { |m|
    @marks << m
  }
end

#newlineObject



556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
# File 'lib/textbringer/buffer.rb', line 556

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



676
677
678
679
680
681
682
683
684
# File 'lib/textbringer/buffer.rb', line 676

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



315
316
317
318
# File 'lib/textbringer/buffer.rb', line 315

def on(name, &callback)
  @callbacks[name] ||= []
  @callbacks[name].push(callback)
end

#on_global_mark_ring?Boolean

Returns:

  • (Boolean)


837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
# File 'lib/textbringer/buffer.rb', line 837

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



307
308
309
# File 'lib/textbringer/buffer.rb', line 307

def on_killed(&callback)
  add_callback(:killed, callback)
end

#on_modified(&callback) ⇒ Object



331
332
333
# File 'lib/textbringer/buffer.rb', line 331

def on_modified(&callback)
  add_callback(:modified, callback)
end

#point_after_mark?(mark) ⇒ Boolean

Returns:

  • (Boolean)


761
762
763
# File 'lib/textbringer/buffer.rb', line 761

def point_after_mark?(mark)
  @point > mark.location
end

#point_at_mark?(mark) ⇒ Boolean

Returns:

  • (Boolean)


753
754
755
# File 'lib/textbringer/buffer.rb', line 753

def point_at_mark?(mark)
  @point == mark.location
end

#point_before_mark?(mark) ⇒ Boolean

Returns:

  • (Boolean)


757
758
759
# File 'lib/textbringer/buffer.rb', line 757

def point_before_mark?(mark)
  @point < mark.location
end

#point_maxObject



480
481
482
# File 'lib/textbringer/buffer.rb', line 480

def point_max
  bytesize
end

#point_minObject



476
477
478
# File 'lib/textbringer/buffer.rb', line 476

def point_min
  0
end

#point_to_mark(mark) ⇒ Object



745
746
747
# File 'lib/textbringer/buffer.rb', line 745

def point_to_mark(mark)
  goto_char(mark.location)
end

#pop_markObject



864
865
866
867
# File 'lib/textbringer/buffer.rb', line 864

def pop_mark
  return if @mark_ring.empty?
  @mark = @mark_ring.rotate(1)
end

#pop_to_markObject



869
870
871
872
# File 'lib/textbringer/buffer.rb', line 869

def pop_to_mark
  goto_char(mark)
  pop_mark
end

#previous_line(n = 1) ⇒ Object



686
687
688
689
690
691
692
693
694
695
# File 'lib/textbringer/buffer.rb', line 686

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



853
854
855
856
857
858
859
860
861
862
# File 'lib/textbringer/buffer.rb', line 853

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.



825
826
827
828
829
830
831
832
833
834
835
# File 'lib/textbringer/buffer.rb', line 825

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) ⇒ Object



1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
# File 'lib/textbringer/buffer.rb', line 1057

def re_search_backward(s, raise_error: true)
  re = new_regexp(s)
  pos = @point
  begin
    i = byteindex(false, re, pos)
    if i.nil?
      if raise_error
        raise SearchError, "Search failed"
      else
        return nil
      end
    end
    pos = get_pos(pos, -1)
  rescue RangeError
    if raise_error
      raise SearchError, "Search failed"
    else
      return nil
    end
  end while match_end(0) > @point
  goto_char(match_beginning(0))
end

#re_search_forward(s, raise_error: true) ⇒ Object



1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
# File 'lib/textbringer/buffer.rb', line 1044

def re_search_forward(s, raise_error: true)
  re = new_regexp(s)
  i = byteindex(true, re, @point)
  if i.nil?
    if raise_error
      raise SearchError, "Search failed"
    else
      return nil
    end
  end
  goto_char(match_end(0))
end

#read_only=(value) ⇒ Object



279
280
281
282
283
284
# File 'lib/textbringer/buffer.rb', line 279

def read_only=(value)
  @read_only = value
  if @read_only
    @modified = false
  end
end

#read_only?Boolean

Returns:

  • (Boolean)


275
276
277
# File 'lib/textbringer/buffer.rb', line 275

def read_only?
  @read_only
end

#read_only_editObject



286
287
288
289
290
291
292
293
# File 'lib/textbringer/buffer.rb', line 286

def read_only_edit
  self.read_only = false
  begin
    yield
  ensure
    self.read_only = true
  end
end

#redoObject



1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
# File 'lib/textbringer/buffer.rb', line 1022

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(s) ⇒ Object



934
935
936
937
938
939
# File 'lib/textbringer/buffer.rb', line 934

def replace(s)
  composite_edit do
    delete_region(point_min, point_max)
    insert(s)
  end
end

#replace_match(str) ⇒ Object



1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
# File 'lib/textbringer/buffer.rb', line 1154

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



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 1174

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

#revert(enc = nil) ⇒ Object



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/textbringer/buffer.rb', line 359

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



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/textbringer/buffer.rb', line 381

def save(file_name = @file_name)
  if file_name.nil?
    raise EditorError, "File name is not set"
  end
  file_name = File.expand_path(file_name)
  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
      @file_mtime = f.mtime
    end
  rescue Errno::EISDIR
    if @name
      file_name = File.expand_path(@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_excursionObject

Don’t save Buffer.current.



790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
# File 'lib/textbringer/buffer.rb', line 790

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_pointObject

The buffer should not be modified in the given block because current_line/current_column is not updated in save_point.



775
776
777
778
779
780
781
782
783
784
785
786
787
# File 'lib/textbringer/buffer.rb', line 775

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



814
815
816
817
818
819
820
# File 'lib/textbringer/buffer.rb', line 814

def set_mark(pos = @point)
  if @mark
    @mark.location = pos
  else
    push_mark(pos)
  end
end

#set_visible_mark(pos = @point) ⇒ Object



874
875
876
877
# File 'lib/textbringer/buffer.rb', line 874

def set_visible_mark(pos = @point)
  @visible_mark ||= new_mark
  @visible_mark.location = pos
end

#skip_re_backward(re) ⇒ Object



1309
1310
1311
1312
1313
# File 'lib/textbringer/buffer.rb', line 1309

def skip_re_backward(re)
  while re =~ char_before
    backward_char
  end
end

#skip_re_forward(re) ⇒ Object



1303
1304
1305
1306
1307
# File 'lib/textbringer/buffer.rb', line 1303

def skip_re_forward(re)
  while re =~ char_after
    forward_char
  end
end

#substring(s, e) ⇒ Object



421
422
423
424
425
426
427
428
429
430
431
# File 'lib/textbringer/buffer.rb', line 421

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_sObject



415
416
417
418
419
# File 'lib/textbringer/buffer.rb', line 415

def to_s
  result = (@contents[0...@gap_start] + @contents[@gap_end..-1])
  result.force_encoding(Encoding::UTF_8) unless @binary
  result
end

#transpose_charsObject



1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
# File 'lib/textbringer/buffer.rb', line 1198

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

#undoObject



1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
# File 'lib/textbringer/buffer.rb', line 1000

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

#yankObject



991
992
993
# File 'lib/textbringer/buffer.rb', line 991

def yank
  insert_for_yank(KILL_RING.current)
end

#yank_popObject



995
996
997
998
# File 'lib/textbringer/buffer.rb', line 995

def yank_pop
  delete_region
  insert_for_yank(KILL_RING.rotate(1))
end