Class: Textbringer::Window

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

Direct Known Subclasses

EchoArea, FloatingWindow

Defined Under Namespace

Classes: Cursor

Constant Summary collapse

KEY_NAMES =
{}
HAVE_GET_KEY_MODIFIERS =
defined?(Curses.get_key_modifiers)
ALT_NUMBER_BASE =
Curses::ALT_0 - ?0.ord
ALT_ALPHA_BASE =
Curses::ALT_A - ?a.ord
FALLBACK_CHARACTERS =
{
  # Diacritical marks
  ## https://www.unicode.org/charts/PDF/U0300.pdf
  ## characters combinable with alphabet:
  ## ("\u{0300}".."\u{036f}").filter { |c|
  ##   ("a".."z").any? { |c2|
  ##     "#{c2}#{c}".unicode_normalize(:nfc).size == 1
  ##   }
  ## }
  "̀" => "`",
  "́" => "´",
  "̂" => "ˆ",
  "̃" => "˜",
  "̄" => "¯",
  "̆" => "˘",
  "̇" => "˙",
  "̈" => "¨",
  "̉" => "ˀ",
  "̊" => "˚",
  "̋" => "˝",
  "̌" => "ˇ",
  "̏" => '"',
  "̑" => nil, # combining character only
  "̛" => nil, # combining character only
  "̣" => nil, # combining character only
  "̤" => nil, # combining character only
  "̥" => "˳",
  "̦" => ",",
  "̧" => "¸",
  "̨" => "˛",
  "̭" => nil, # combining character only
  "̮" => nil, # combining character only
  "̰" => nil, # combining character only
  "̱" => "ˍ",
  "̀" => nil, # combining character only
  "́" => nil, # combining character only
  "̈́" => nil, # combining character only

  # Hiragana
  "゙" => "゛",
  "゚" => "゜",

  # Hangul jamo
  ## initial
  "ᄀ" => "ㄱ",
  "ᄁ" => "ㄲ",
  "ᄂ" => "ㄴ",
  "ᄃ" => "ㄷ",
  "ᄄ" => "ㄸ",
  "ᄅ" => "ㄹ",
  "ᄆ" => "ㅁ",
  "ᄇ" => "ㅂ",
  "ᄈ" => "ㅃ",
  "ᄉ" => "ㅅ",
  "ᄊ" => "ㅆ",
  "ᄋ" => "ㅇ",
  "ᄌ" => "ㅈ",
  "ᄍ" => "ㅉ",
  "ᄎ" => "ㅊ",
  "ᄏ" => "ㅋ",
  "ᄐ" => "ㅌ",
  "ᄑ" => "ㅍ",
  "ᄒ" => "ㅎ",
  ## medial
  "ᅡ" => "ㅏ",
  "ᅢ" => "ㅐ",
  "ᅣ" => "ㅑ",
  "ᅤ" => "ㅒ",
  "ᅥ" => "ㅓ",
  "ᅦ" => "ㅔ",
  "ᅧ" => "ㅕ",
  "ᅨ" => "ㅖ",
  "ᅩ" => "ㅗ",
  "ᅪ" => "ㅘ",
  "ᅫ" => "ㅙ",
  "ᅬ" => "ㅚ",
  "ᅭ" => "ㅛ",
  "ᅮ" => "ㅜ",
  "ᅯ" => "ㅝ",
  "ᅰ" => "ㅞ",
  "ᅱ" => "ㅟ",
  "ᅲ" => "ㅠ",
  "ᅳ" => "ㅡ",
  "ᅴ" => "ㅢ",
  "ᅵ" => "ㅣ",
  ## final
  "ᆨ" => "ㄱ",
  "ᆩ" => "ㄲ",
  "ᆪ" => "ㄳ",
  "ᆫ" => "ㄴ",
  "ᆬ" => "ㄵ",
  "ᆭ" => "ㄶ",
  "ᆮ" => "ㄷ",
  "ᆯ" => "ㄹ",
  "ᆰ" => "ㄺ",
  "ᆱ" => "ㄻ",
  "ᆲ" => "ㄼ",
  "ᆳ" => "ㄽ",
  "ᆴ" => "ㄾ",
  "ᆵ" => "ㄿ",
  "ᆶ" => "ㅀ",
  "ᆷ" => "ㅁ",
  "ᆸ" => "ㅂ",
  "ᆹ" => "ㅄ",
  "ᆺ" => "ㅅ",
  "ᆻ" => "ㅆ",
  "ᆼ" => "ㅇ",
  "ᆽ" => "ㅈ",
  "ᆾ" => "ㅊ",
  "ᆿ" => "ㅋ",
  "ᇀ" => "ㅌ",
  "ᇁ" => "ㅍ",
  "ᇂ" => "ㅎ"
}
@@started =
false
@@list =
[]
@@current =
nil
@@minibuffer_selected =
nil
@@echo_area =
nil
@@has_colors =
false
@@default_fg =
"default"
@@default_bg =
"default"

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.



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/textbringer/window.rb', line 266

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
  @raw_key_buffer = []
  @key_buffer = []
  @cursor = Cursor.new(0, 0)
  @in_region = false
  @in_isearch = false
  @current_hl_face = nil
end

Instance Attribute Details

#bottom_of_windowObject (readonly)

Returns the value of attribute bottom_of_window.



264
265
266
# File 'lib/textbringer/window.rb', line 264

def bottom_of_window
  @bottom_of_window
end

#bufferObject

Returns the value of attribute buffer.



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

def buffer
  @buffer
end

#columnsObject (readonly)

Returns the value of attribute columns.



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

def columns
  @columns
end

#cursorObject (readonly)

Returns the value of attribute cursor.



264
265
266
# File 'lib/textbringer/window.rb', line 264

def cursor
  @cursor
end

#linesObject (readonly)

Returns the value of attribute lines.



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

def lines
  @lines
end

#mode_lineObject (readonly)

Returns the value of attribute mode_line.



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

def mode_line
  @mode_line
end

#top_of_windowObject (readonly)

Returns the value of attribute top_of_window.



264
265
266
# File 'lib/textbringer/window.rb', line 264

def top_of_window
  @top_of_window
end

#windowObject (readonly)

Returns the value of attribute window.



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

def window
  @window
end

#xObject (readonly)

Returns the value of attribute x.



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

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



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

def y
  @y
end

Class Method Details

.beepObject



259
260
261
# File 'lib/textbringer/window.rb', line 259

def self.beep
  Curses.beep
end

.colorsObject



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

def self.colors
  Curses.colors
end

.columnsObject



232
233
234
# File 'lib/textbringer/window.rb', line 232

def self.columns
  Curses.cols
end

.currentObject Also known as: selected



47
48
49
# File 'lib/textbringer/window.rb', line 47

def self.current
  @@current
end

.current=(window) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/textbringer/window.rb', line 51

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

.delete_other_windowsObject



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

def self.delete_other_windows
  if @@current.echo_area?
    raise EditorError, "Can't expand the echo area to full screen"
  end
  @@list.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_window(target = @@current) ⇒ Object



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

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

.echo_areaObject



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

def self.echo_area
  @@echo_area
end

.has_colors=(value) ⇒ Object



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

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

.has_colors?Boolean

Returns:

  • (Boolean)


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

def self.has_colors?
  @@has_colors
end

.linesObject



228
229
230
# File 'lib/textbringer/window.rb', line 228

def self.lines
  Curses.lines
end

.list(include_echo_area: false) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/textbringer/window.rb', line 39

def self.list(include_echo_area: false)
  if include_echo_area
    @@list.dup
  else
    @@list.reject(&:echo_area?)
  end
end

.load_facesObject



147
148
149
# File 'lib/textbringer/window.rb', line 147

def self.load_faces
  require_relative "faces/gamegrid"
end

.minibuffer_selectedObject



65
66
67
# File 'lib/textbringer/window.rb', line 65

def self.minibuffer_selected
  @@minibuffer_selected
end

.minibuffer_selected=(window) ⇒ Object



69
70
71
# File 'lib/textbringer/window.rb', line 69

def self.minibuffer_selected=(window)
  @@minibuffer_selected = window
end

.other_windowObject



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

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

.redisplayObject



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/textbringer/window.rb', line 197

def self.redisplay
  return if Controller.current.executing_keyboard_macro?
  return if Window.current.has_input?
  @@list.each do |window|
    window.redisplay unless window.current?
  end
  current.redisplay
  if defined?(FloatingWindow)
    # Render floating windows on top
    FloatingWindow.redisplay_all_floating
    # Ensure cursor position is from the current window, not floating windows
    # Refresh current window's cursor position after floating windows
    if !FloatingWindow.floating_windows.empty?
      current&.window&.noutrefresh
    end
  end
  update
end

.redrawObject



216
217
218
219
220
221
222
# File 'lib/textbringer/window.rb', line 216

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

.resizeObject



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/textbringer/window.rb', line 236

def self.resize
  @@list.delete_if do |window|
    if !window.echo_area? &&
        window.y > Window.lines - CONFIG[:window_min_height]
      window.delete
      true
    else
      false
    end
  end
  @@list.each_with_index do |window, i|
    unless window.echo_area?
      if i < @@list.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



137
138
139
140
141
142
143
144
145
# File 'lib/textbringer/window.rb', line 137

def self.set_default_colors(fg, bg)
  new_fg = fg || @@default_fg
  new_bg = bg || @@default_bg
  Curses.assume_default_colors(Color[new_fg], Color[new_bg])
  @@default_fg = new_fg
  @@default_bg = new_bg
  Face.define(:default, foreground: new_fg, background: new_bg)
  Window.redraw if @@started
end

.startObject



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
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/textbringer/window.rb', line 151

def self.start
  if @@started
    raise EditorError, "Already started"
  end
  Theme.detect_background
  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
  else
    Face.define :mode_line, reverse: true
    Face.define :region, reverse: true
    Face.define :isearch, underline: true
  end
  begin
    window =
      Textbringer::Window.new(Window.lines - 1, Window.columns, 0, 0)
    window.buffer = Buffer.new_buffer("*scratch*")
    @@list.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
    @@list.push(@@echo_area)
    @@started = true
    yield
  ensure
    Buffer.current&.input_method&.on_deactivate
    @@list.each do |win|
      win.close
    end
    @@list.clear
    Curses.echo
    Curses.noraw
    Curses.nl
    Curses.close_screen
    @@started = false
  end
end

.updateObject



224
225
226
# File 'lib/textbringer/window.rb', line 224

def self.update
  Curses.doupdate
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


292
293
294
# File 'lib/textbringer/window.rb', line 292

def active?
  true
end

#closeObject



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

def close
  @window.close
end

#current?Boolean

Returns:

  • (Boolean)


341
342
343
# File 'lib/textbringer/window.rb', line 341

def current?
  self == @@current
end

#current_or_minibuffer_selected?Boolean

Returns:

  • (Boolean)


345
346
347
# File 'lib/textbringer/window.rb', line 345

def current_or_minibuffer_selected?
  self == @@current || self == @@minibuffer_selected
end

#deleteObject



300
301
302
303
304
305
306
307
308
309
# File 'lib/textbringer/window.rb', line 300

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

#deleted?Boolean

Returns:

  • (Boolean)


296
297
298
# File 'lib/textbringer/window.rb', line 296

def deleted?
  @deleted
end

#echo_area?Boolean

Returns:

  • (Boolean)


288
289
290
# File 'lib/textbringer/window.rb', line 288

def echo_area?
  false
end

#enlarge(n) ⇒ Object



607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
# File 'lib/textbringer/window.rb', line 607

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

#has_input?Boolean

Returns:

  • (Boolean)


392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/textbringer/window.rb', line 392

def has_input?
  if !@raw_key_buffer.empty? || !@key_buffer.empty?
    return true
  end
  @window.nodelay = true
  begin
    c = @window.get_char
    if c
      @raw_key_buffer.push(c)
    end
    !c.nil?
  ensure
    @window.nodelay = false
  end
end

#highlightObject



408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/textbringer/window.rb', line 408

def highlight
  @highlight_on = {}
  @highlight_off = {}
  return if !@@has_colors || !CONFIG[:syntax_highlight] || @buffer.binary?
  ctx = HighlightContext.new(
    buffer: @buffer,
    highlight_start: @buffer.point,
    # Use * 4 (max UTF-8 bytes per character) to ensure multi-byte
    # characters near the bottom of the window are fully covered.
    highlight_end: [@buffer.point + columns * (lines - 1) * 4,
                    @buffer.point_max].min,
    highlight_on: @highlight_on,
    highlight_off: @highlight_off
  )
  @buffer.mode.highlight(ctx)
end

#move(y, x) ⇒ Object



532
533
534
535
536
537
# File 'lib/textbringer/window.rb', line 532

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

#read_eventObject



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/textbringer/window.rb', line 349

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

#read_event_nonblockObject



367
368
369
370
371
372
373
374
# File 'lib/textbringer/window.rb', line 367

def read_event_nonblock
  @window.nodelay = true
  begin
    read_event
  ensure
    @window.nodelay = false
  end
end

#recenterObject



547
548
549
550
551
552
553
554
555
556
557
558
# File 'lib/textbringer/window.rb', line 547

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



560
561
562
563
564
565
# File 'lib/textbringer/window.rb', line 560

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

#redisplayObject



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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
# File 'lib/textbringer/window.rb', line 425

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
    @cursor.y = @cursor.x = 0
    @buffer.point_to_mark(@top_of_window)
    highlight
    @window.erase
    @window.setpos(0, 0)
    @in_region = false
    @in_isearch = false
    @current_hl_face = nil
    if current_or_minibuffer_selected? && @buffer.visible_mark &&
       @buffer.point_after_mark?(@buffer.visible_mark)
      @in_region = true
    end
    if current_or_minibuffer_selected? && @buffer.isearch_mark &&
       @buffer.point_after_mark?(@buffer.isearch_mark)
      @in_region = false
      @in_isearch = true
    end
    apply_window_attrs(@window)
    while !@buffer.end_of_buffer?
      cury = @window.cury
      curx = @window.curx
      update_cursor_and_attr(point, cury, curx)
      if @highlight_off[@buffer.point]
        @current_hl_face = nil
        apply_window_attrs(@window)
      end
      if face = @highlight_on[@buffer.point]
        @current_hl_face = face
        apply_window_attrs(@window)
      end
      c = @buffer.char_after
      if c == "\n"
        # Fill to end of line with region background if in region
        if @in_region
          remaining = columns - curx
          @window.addstr(" " * remaining) if remaining > 0
        else
          @window.clrtoeol
        end
        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 = compose_character(point, cury, curx, c)
      end
      s = escape(c)
      if curx < columns - 4
        newx = nil
      else
        newx = curx + Buffer.display_width(s)
        if newx > columns
          if cury == lines - 2
            break
          else
            # Fill to end of line with region background if in region
            if @in_region
              remaining = columns - curx
              @window.addstr(" " * remaining) if remaining > 0
            else
              @window.clrtoeol
            end
            @window.setpos(cury + 1, 0)
          end
        end
      end
      @window.addstr(s)
      break if newx == columns && cury == lines - 2
      @buffer.forward_char
    end
    apply_window_attrs(@window)
    @buffer.mark_to_point(@bottom_of_window)
    if @buffer.point_at_mark?(point)
      @cursor.y = @window.cury
      @cursor.x = @window.curx
    end
    if @cursor.x == columns - 1
      c = @buffer.char_after(point.location)
      if c && Buffer.display_width(c) > 1
        @cursor.y += 1
        @cursor.x = 0
      end
    end
    @window.setpos(@cursor.y, @cursor.x)
    @window.noutrefresh
  end
end

#redrawObject



527
528
529
530
# File 'lib/textbringer/window.rb', line 527

def redraw
  @window.redraw
  @mode_line.redraw
end

#resize(lines, columns) ⇒ Object



539
540
541
542
543
544
545
# File 'lib/textbringer/window.rb', line 539

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



337
338
339
# File 'lib/textbringer/window.rb', line 337

def restore_point
  @buffer.point_to_mark(@point_mark)
end

#save_pointObject



329
330
331
332
333
334
335
# File 'lib/textbringer/window.rb', line 329

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



577
578
579
580
581
582
583
584
585
# File 'lib/textbringer/window.rb', line 577

def scroll_down
  if @top_of_window.location == @buffer.point_min
    raise RangeError, "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



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

def scroll_up
  if @bottom_of_window.location == @buffer.point_max
    raise RangeError, "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

#shrink(n) ⇒ Object



648
649
650
# File 'lib/textbringer/window.rb', line 648

def shrink(n)
  enlarge(-n)
end

#shrink_if_larger_than_bufferObject



652
653
654
655
656
657
658
659
660
661
662
663
664
665
# File 'lib/textbringer/window.rb', line 652

def shrink_if_larger_than_buffer
  @buffer.save_point do
    @buffer.end_of_buffer
    @buffer.skip_re_backward(/\s/)
    count = beginning_of_line_and_count(Window.lines) + 1
    while !@buffer.beginning_of_buffer?
      @buffer.backward_char
      count += beginning_of_line_and_count(Window.lines) + 1
    end
    if lines - 1 > count
      shrink(lines - 1 - count)
    end
  end
end

#split(other_lines = nil) ⇒ Object



587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
# File 'lib/textbringer/window.rb', line 587

def split(other_lines = nil)
  old_lines = lines
  if other_lines
    if other_lines < CONFIG[:window_min_height]
      raise EditorError, "Window too small"
    end
    new_lines = lines - other_lines
  else
    new_lines = (old_lines / 2.0).ceil
  end
  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 = @@list.index(self)
  @@list.insert(i + 1, new_window)
end

#wait_input(msecs) ⇒ Object



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/textbringer/window.rb', line 376

def wait_input(msecs)
  if !@raw_key_buffer.empty? || !@key_buffer.empty?
    return @raw_key_buffer.first || @key_buffer.first
  end
  @window.timeout = msecs
  begin
    c = @window.get_char
    if c
      @raw_key_buffer.push(c)
    end
    c
  ensure
    @window.timeout = -1
  end
end