Class: Textbringer::Window

Inherits:
Object
  • Object
show all
Defined in:
lib/textbringer/window.rb

Direct Known Subclasses

EchoArea

Constant Summary collapse

KEY_NAMES =
{}
@@started =
false
@@windows =
[]
@@current =
nil
@@echo_area =
nil
@@has_colors =
false

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lines, columns, y, x) ⇒ Window

Returns a new instance of Window.



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/textbringer/window.rb', line 245

def initialize(lines, columns, y, x)
  @lines = lines
  @columns = columns
  @y = y
  @x = x
  initialize_window(lines, columns, y, x)
  @window.keypad = true
  @window.scrollok(false)
  @window.idlok(true)
  @buffer = nil
  @top_of_window = nil
  @bottom_of_window = nil
  @point_mark = nil
  @deleted = false
  @key_buffer = []
end

Instance Attribute Details

#bottom_of_windowObject (readonly)

Returns the value of attribute bottom_of_window.



243
244
245
# File 'lib/textbringer/window.rb', line 243

def bottom_of_window
  @bottom_of_window
end

#bufferObject

Returns the value of attribute buffer.



242
243
244
# File 'lib/textbringer/window.rb', line 242

def buffer
  @buffer
end

#columnsObject (readonly)

Returns the value of attribute columns.



242
243
244
# File 'lib/textbringer/window.rb', line 242

def columns
  @columns
end

#linesObject (readonly)

Returns the value of attribute lines.



242
243
244
# File 'lib/textbringer/window.rb', line 242

def lines
  @lines
end

#mode_lineObject (readonly)

Returns the value of attribute mode_line.



242
243
244
# File 'lib/textbringer/window.rb', line 242

def mode_line
  @mode_line
end

#top_of_windowObject (readonly)

Returns the value of attribute top_of_window.



243
244
245
# File 'lib/textbringer/window.rb', line 243

def top_of_window
  @top_of_window
end

#windowObject (readonly)

Returns the value of attribute window.



242
243
244
# File 'lib/textbringer/window.rb', line 242

def window
  @window
end

#xObject (readonly)

Returns the value of attribute x.



242
243
244
# File 'lib/textbringer/window.rb', line 242

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



242
243
244
# File 'lib/textbringer/window.rb', line 242

def y
  @y
end

Class Method Details

.beepObject



238
239
240
# File 'lib/textbringer/window.rb', line 238

def self.beep
  Curses.beep
end

.colorsObject



132
133
134
# File 'lib/textbringer/window.rb', line 132

def self.colors
  Curses.colors
end

.columnsObject



211
212
213
# File 'lib/textbringer/window.rb', line 211

def self.columns
  Curses.cols
end

.currentObject



61
62
63
# File 'lib/textbringer/window.rb', line 61

def self.current
  @@current
end

.current=(window) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/textbringer/window.rb', line 65

def self.current=(window)
  if window.deleted?
    window = @@windows.first
  end
  @@current.save_point if @@current && !@@current.deleted?
  @@current = window
  @@current.restore_point
  Buffer.current = window.buffer
end

.delete_other_windowsObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/textbringer/window.rb', line 95

def self.delete_other_windows
  if @@current.echo_area?
    raise EditorError, "Can't expand the echo area to full screen"
  end
  @@windows.delete_if do |window|
    if window.current? || window.echo_area?
      false
    else
      window.delete
      true
    end
  end
  @@current.move(0, 0)
  @@current.resize(Window.lines - 1, @@current.columns)
end

.delete_windowObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/textbringer/window.rb', line 75

def self.delete_window
  if @@current.echo_area?
    raise EditorError, "Can't delete the echo area"
  end
  if @@windows.size == 2
    raise EditorError, "Can't delete the sole window"
  end
  i = @@windows.index(@@current)
  if i == 0
    window = @@windows[1]
    window.move(0, 0)
  else
    window = @@windows[i - 1]
  end
  window.resize(@@current.lines + window.lines, window.columns)
  @@current.delete
  @@windows.delete_at(i)
  self.current = window
end

.echo_areaObject



120
121
122
# File 'lib/textbringer/window.rb', line 120

def self.echo_area
  @@echo_area
end

.has_colors=(value) ⇒ Object



124
125
126
# File 'lib/textbringer/window.rb', line 124

def self.has_colors=(value)
  @@has_colors = value
end

.has_colors?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/textbringer/window.rb', line 128

def self.has_colors?
  @@has_colors
end

.linesObject



207
208
209
# File 'lib/textbringer/window.rb', line 207

def self.lines
  Curses.lines
end

.load_facesObject



141
142
143
144
# File 'lib/textbringer/window.rb', line 141

def self.load_faces
  require_relative "faces/basic"
  require_relative "faces/programming"
end

.other_windowObject



111
112
113
114
115
116
117
118
# File 'lib/textbringer/window.rb', line 111

def self.other_window
  i = @@windows.index(@@current)
  begin
    i += 1
    window = @@windows[i % @@windows.size]
  end while !window.active?
  self.current = window
end

.redisplayObject



186
187
188
189
190
191
192
193
# File 'lib/textbringer/window.rb', line 186

def self.redisplay
  return if Window.current.has_input?
  @@windows.each do |window|
    window.redisplay unless window.current?
  end
  current.redisplay
  update
end

.redrawObject



195
196
197
198
199
200
201
# File 'lib/textbringer/window.rb', line 195

def self.redraw
  @@windows.each do |window|
    window.redraw unless window.current?
  end
  current.redraw
  update
end

.resizeObject



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/textbringer/window.rb', line 215

def self.resize
  @@windows.delete_if do |window|
    if !window.echo_area? &&
        window.y > Window.lines - CONFIG[:window_min_height]
      window.delete
      true
    else
      false
    end
  end
  @@windows.each_with_index do |window, i|
    unless window.echo_area?
      if i < @@windows.size - 2
        window.resize(window.lines, Window.columns)
      else
        window.resize(Window.lines - 1 - window.y, Window.columns)
      end
    end
  end
  @@echo_area.move(Window.lines - 1, 0)
  @@echo_area.resize(1, Window.columns)
end

.set_default_colors(fg, bg) ⇒ Object



136
137
138
139
# File 'lib/textbringer/window.rb', line 136

def self.set_default_colors(fg, bg)
  Curses.assume_default_colors(Color[fg], Color[bg])
  Window.redraw
end

.startObject



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/textbringer/window.rb', line 146

def self.start
  if @@started
    raise EditorError, "Already started"
  end
  Curses.init_screen
  Curses.noecho
  Curses.raw
  Curses.nonl
  self.has_colors = Curses.has_colors?
  if has_colors?
    Curses.start_color
    Curses.use_default_colors
    load_faces
  end
  begin
    window =
      Textbringer::Window.new(Window.lines - 1, Window.columns, 0, 0)
    window.buffer = Buffer.new_buffer("*scratch*")
    @@windows.push(window)
    Window.current = window
    @@echo_area = Textbringer::EchoArea.new(1, Window.columns,
                                            Window.lines - 1, 0)
    Buffer.minibuffer.keymap = MINIBUFFER_LOCAL_MAP
    @@echo_area.buffer = Buffer.minibuffer
    @@windows.push(@@echo_area)
    @@started = true
    yield
  ensure
    @@windows.each do |win|
      win.delete
    end
    @@windows.clear
    Curses.echo
    Curses.noraw
    Curses.nl
    Curses.close_screen
    @@started = false
  end
end

.updateObject



203
204
205
# File 'lib/textbringer/window.rb', line 203

def self.update
  Curses.doupdate
end

.windowsObject



57
58
59
# File 'lib/textbringer/window.rb', line 57

def self.windows
  @@windows
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


266
267
268
# File 'lib/textbringer/window.rb', line 266

def active?
  true
end

#current?Boolean

Returns:

  • (Boolean)


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

def current?
  self == @@current
end

#deleteObject



274
275
276
277
278
279
280
281
282
283
# File 'lib/textbringer/window.rb', line 274

def delete
  unless @deleted
    if current?
      Window.current = @@windows.first
    end
    delete_marks
    @window.close
    @deleted = true
  end
end

#deleted?Boolean

Returns:

  • (Boolean)


270
271
272
# File 'lib/textbringer/window.rb', line 270

def deleted?
  @deleted
end

#echo_area?Boolean

Returns:

  • (Boolean)


262
263
264
# File 'lib/textbringer/window.rb', line 262

def echo_area?
  false
end

#enlarge(n) ⇒ Object



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
612
613
614
615
616
# File 'lib/textbringer/window.rb', line 577

def enlarge(n)
  if n > 0
    max_height = Window.lines -
      CONFIG[:window_min_height] * (@@windows.size - 2) - 1
    new_lines = [lines + n, max_height].min
    needed_lines = new_lines - lines
    resize(new_lines, columns)
    i = @@windows.index(self)
    indices = (i + 1).upto(@@windows.size - 2).to_a +
      (i - 1).downto(0).to_a
    indices.each do |j|
      break if needed_lines == 0
      window = @@windows[j]
      extended_lines = [
        window.lines - CONFIG[:window_min_height],
        needed_lines
      ].min
      window.resize(window.lines - extended_lines, window.columns)
      needed_lines -= extended_lines
    end
    y = 0
    @@windows.each do |win|
      win.move(y, win.x)
      y += win.lines
    end
  elsif n < 0 && @@windows.size > 2
    new_lines = [lines + n, CONFIG[:window_min_height]].max
    diff = lines - new_lines
    resize(new_lines, columns)
    i = @@windows.index(self)
    if i < @@windows.size - 2
      window = @@windows[i + 1]
      window.move(window.y - diff, window.x)
    else
      window = @@windows[i - 1]
      move(self.y + diff, self.x)
    end
    window.resize(window.lines + diff, window.columns)
  end
end

#has_input?Boolean

Returns:

  • (Boolean)


358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/textbringer/window.rb', line 358

def has_input?
  unless @key_buffer.empty?
    return true
  end
  @window.nodelay = true
  begin
    c = @window.get_char
    if c
      Curses.unget_char(c)
    end
    !c.nil?
  ensure
    @window.nodelay = false
  end
end

#highlightObject



374
375
376
377
378
379
380
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
# File 'lib/textbringer/window.rb', line 374

def highlight
  @highlight_on = {}
  @highlight_off = {}
  return if !@@has_colors || !CONFIG[:syntax_highlight]
  syntax_table = @buffer.mode.syntax_table
  return if syntax_table.empty?
  if @buffer.bytesize < CONFIG[:highlight_buffer_size_limit]
    base_pos = @buffer.point_min
    s = @buffer.to_s
  else
    base_pos = @buffer.point
    len = columns * (lines - 1) / 2 * 3
    s = @buffer.substring(@buffer.point, @buffer.point + len).scrub("")
  end
  re_str = syntax_table.map { |name, re|
    "(?<#{name}>#{re})"
  }.join("|")
  re = Regexp.new(re_str)
  names = syntax_table.keys
  s.scan(re) do
    b = base_pos + $`.bytesize
    e = b + $&.bytesize
    if b < @buffer.point && @buffer.point < e
      b = @buffer.point
    end
    name = names.find { |n| $~[n] }
    attributes = Face[name]&.attributes
    if attributes
      @highlight_on[b] = attributes
      @highlight_off[e] = attributes
    end
  end
end

#move(y, x) ⇒ Object



509
510
511
512
513
514
# File 'lib/textbringer/window.rb', line 509

def move(y, x)
  @y = y
  @x = x
  @window.move(y, x)
  @mode_line.move(y + @window.maxy, x)
end

#read_charObject



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/textbringer/window.rb', line 315

def read_char
  key = get_char
  if key.is_a?(Integer)
    if PDCurses.dll_loaded?
      if PDCurses::ALT_0 <= key && key <= PDCurses::ALT_9
        @key_buffer.push((key - PDCurses::ALT_NUMBER_BASE).chr)
        return "\e"
      elsif PDCurses::ALT_A <= key && key <= PDCurses::ALT_Z
        @key_buffer.push((key - PDCurses::ALT_ALPHA_BASE).chr)
        return "\e"
      end
    end
    KEY_NAMES[key] || key
  else
    key&.encode(Encoding::UTF_8)
  end
end

#read_char_nonblockObject



333
334
335
336
337
338
339
340
# File 'lib/textbringer/window.rb', line 333

def read_char_nonblock
  @window.nodelay = true
  begin
    read_char
  ensure
    @window.nodelay = false
  end
end

#recenterObject



524
525
526
527
528
529
530
531
532
533
534
535
# File 'lib/textbringer/window.rb', line 524

def recenter
  @buffer.save_point do |saved|
    max = (lines - 1) / 2
    count = beginning_of_line_and_count(max)
    while count < max
      break if @buffer.point == 0
      @buffer.backward_char
      count += beginning_of_line_and_count(max - count - 1) + 1
    end
    @buffer.mark_to_point(@top_of_window)
  end
end

#recenter_if_neededObject



537
538
539
540
541
542
# File 'lib/textbringer/window.rb', line 537

def recenter_if_needed
  if @buffer.point_before_mark?(@top_of_window) ||
     @buffer.point_after_mark?(@bottom_of_window)
    recenter
  end
end

#redisplayObject



408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# File 'lib/textbringer/window.rb', line 408

def redisplay
  return if @buffer.nil?
  redisplay_mode_line
  @buffer.save_point do |saved|
    if current?
      point = saved
    else
      point = @point_mark
      @buffer.point_to_mark(@point_mark)
    end
    framer
    y = x = 0
    @buffer.point_to_mark(@top_of_window)
    highlight
    @window.erase
    @window.setpos(0, 0)
    @window.attrset(0)
    if current? && @buffer.visible_mark &&
       @buffer.point_after_mark?(@buffer.visible_mark)
      @window.attron(Curses::A_REVERSE)
    end
    while !@buffer.end_of_buffer?
      cury, curx = @window.cury, @window.curx
      if @buffer.point_at_mark?(point)
        y, x = cury, curx
        if current? && @buffer.visible_mark
          if @buffer.point_after_mark?(@buffer.visible_mark)
            @window.attroff(Curses::A_REVERSE)
          elsif @buffer.point_before_mark?(@buffer.visible_mark)
            @window.attron(Curses::A_REVERSE)
          end
        end
      end
      if current? && @buffer.visible_mark &&
         @buffer.point_at_mark?(@buffer.visible_mark)
        if @buffer.point_after_mark?(point)
          @window.attroff(Curses::A_REVERSE)
        elsif @buffer.point_before_mark?(point)
          @window.attron(Curses::A_REVERSE)
        end
      end
      if attrs = @highlight_off[@buffer.point]
        @window.attroff(attrs)
      end
      if attrs = @highlight_on[@buffer.point]
        @window.attron(attrs)
      end
      c = @buffer.char_after
      if c == "\n"
        @window.clrtoeol
        break if cury == lines - 2   # lines include mode line
        @window.setpos(cury + 1, 0)
        @buffer.forward_char
        next
      elsif c == "\t"
        n = calc_tab_width(curx)
        c = " " * n
      else
        c = escape(c)
      end
      if curx < columns - 4
        newx = nil
      else
        newx = curx + Buffer.display_width(c)
        if newx > columns
          if cury == lines - 2
            break
          else
            @window.clrtoeol
            @window.setpos(cury + 1, 0)
          end
        end
      end
      @window.addstr(c)
      break if newx == columns && cury == lines - 2
      @buffer.forward_char
    end
    if current? && @buffer.visible_mark
      @window.attroff(Curses::A_REVERSE)
    end
    @buffer.mark_to_point(@bottom_of_window)
    if @buffer.point_at_mark?(point)
      y, x = @window.cury, @window.curx
    end
    if x == columns - 1
      c = @buffer.char_after(point.location)
      if c && Buffer.display_width(c) > 1
        y += 1
        x = 0
      end
    end
    @window.setpos(y, x)
    @window.noutrefresh
  end
end

#redrawObject



504
505
506
507
# File 'lib/textbringer/window.rb', line 504

def redraw
  @window.redraw
  @mode_line.redraw
end

#resize(lines, columns) ⇒ Object



516
517
518
519
520
521
522
# File 'lib/textbringer/window.rb', line 516

def resize(lines, columns)
  @lines = lines
  @columns = columns
  @window.resize(lines - 1, columns)
  @mode_line.move(@y + lines - 1, @x)
  @mode_line.resize(1, columns)
end

#restore_pointObject



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

def restore_point
  @buffer.point_to_mark(@point_mark)
end

#save_pointObject



299
300
301
302
303
304
305
# File 'lib/textbringer/window.rb', line 299

def save_point
  @buffer[:top_of_window] ||= @buffer.new_mark
  @buffer[:top_of_window].location = @top_of_window.location
  @buffer[:bottom_of_window] ||= @buffer.new_mark
  @buffer[:bottom_of_window].location = @bottom_of_window.location
  @buffer.mark_to_point(@point_mark)
end

#scroll_downObject



554
555
556
557
558
559
560
561
562
# File 'lib/textbringer/window.rb', line 554

def scroll_down
  if @top_of_window.location == @buffer.point_min
    raise EditorError, "Beginning of buffer"
  end
  @buffer.point_to_mark(@top_of_window)
  @buffer.next_line
  @buffer.beginning_of_line
  @top_of_window.location = 0
end

#scroll_upObject



544
545
546
547
548
549
550
551
552
# File 'lib/textbringer/window.rb', line 544

def scroll_up
  if @bottom_of_window.location == @buffer.point_max
    raise EditorError, "End of buffer"
  end
  @buffer.point_to_mark(@bottom_of_window)
  @buffer.previous_line
  @buffer.beginning_of_line
  @buffer.mark_to_point(@top_of_window)
end

#splitObject



564
565
566
567
568
569
570
571
572
573
574
575
# File 'lib/textbringer/window.rb', line 564

def split
  old_lines = lines
  new_lines = (old_lines / 2.0).ceil
  if new_lines < CONFIG[:window_min_height]
    raise EditorError, "Window too small"
  end
  resize(new_lines, columns)
  new_window = Window.new(old_lines - new_lines, columns, y + new_lines, x)
  new_window.buffer = buffer
  i = @@windows.index(self)
  @@windows.insert(i + 1, new_window)
end

#wait_input(msecs) ⇒ Object



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/textbringer/window.rb', line 342

def wait_input(msecs)
  unless @key_buffer.empty?
    return @key_buffer.first
  end
  @window.timeout = msecs
  begin
    c = @window.get_char
    if c
      Curses.unget_char(c)
    end
    c
  ensure
    @window.timeout = -1
  end
end