Class: Textbringer::Window
- Inherits:
-
Object
- Object
- Textbringer::Window
- Defined in:
- lib/textbringer/window.rb,
lib/textbringer/window/fallback_characters.rb,
sig/lib/textbringer/window.rbs
Direct Known Subclasses
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
-
#bottom_of_window ⇒ Object
readonly
Returns the value of attribute bottom_of_window.
-
#buffer ⇒ Object
Returns the value of attribute buffer.
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#cursor ⇒ Object
readonly
Returns the value of attribute cursor.
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
-
#mode_line ⇒ Object
readonly
Returns the value of attribute mode_line.
-
#top_of_window ⇒ Object
readonly
Returns the value of attribute top_of_window.
-
#window ⇒ Object
readonly
Returns the value of attribute window.
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Class Method Summary collapse
- .beep ⇒ nil
- .colors ⇒ Integer
- .columns ⇒ Integer
- .current ⇒ Object (also: selected)
- .current=(window) ⇒ Object
- .delete_other_windows ⇒ Object
- .delete_window(target = @@current) ⇒ Object
- .echo_area ⇒ Textbringer::EchoArea
- .has_colors=(value) ⇒ Boolean
- .has_colors? ⇒ Boolean
- .lines ⇒ Integer
- .list(include_echo_area: false) ⇒ Array[untyped]
- .load_faces ⇒ Boolean
- .minibuffer_selected ⇒ Object
- .minibuffer_selected=(window) ⇒ Object
- .other_window ⇒ Object
- .redisplay ⇒ nil
- .redraw ⇒ nil
- .resize ⇒ Integer
- .set_default_colors(fg, bg) ⇒ nil
- .start ⇒ nil
- .update ⇒ nil
Instance Method Summary collapse
- #active? ⇒ Boolean
- #beginning_of_line_and_count(max_lines, columns = @columns) ⇒ Integer
- #calc_tab_width(column) ⇒ Integer
- #close ⇒ nil
- #compose_character(point, cury, curx, c) ⇒ String
- #compose_hangul_character(point, cury, curx, initial) ⇒ String
- #current? ⇒ Boolean
- #current_or_minibuffer_selected? ⇒ Boolean
- #delete ⇒ Boolean
- #delete_marks ⇒ nil
- #deleted? ⇒ Boolean
- #echo_area? ⇒ Boolean
- #enlarge(n) ⇒ Object
- #escape(s) ⇒ String
- #framer ⇒ Object
- #get_char ⇒ Object
- #has_input? ⇒ Boolean
- #highlight ⇒ Object
-
#initialize(lines, columns, y, x) ⇒ Window
constructor
A new instance of Window.
- #initialize_window(num_lines, num_columns, y, x) ⇒ void
- #isearch_attr ⇒ Integer
- #move(y, x) ⇒ Integer
- #read_event ⇒ Object
- #read_event_nonblock ⇒ Object
- #recenter ⇒ Integer
- #recenter_if_needed ⇒ Object
- #redisplay ⇒ nil
- #redisplay_mode_line ⇒ nil
- #redraw ⇒ nil
- #region_attr ⇒ Integer
- #resize(lines, columns) ⇒ Integer
- #restore_point ⇒ Integer
- #save_point ⇒ Integer
- #scroll_down ⇒ Object
- #scroll_up ⇒ Object
- #shrink(n) ⇒ Object
- #shrink_if_larger_than_buffer ⇒ Object
- #split(other_lines = nil) ⇒ Object
- #unicode_codepoint(c) ⇒ Object
- #update_cursor_and_attr(point, cury, curx) ⇒ Object
- #wait_input(msecs) ⇒ Object
Constructor Details
#initialize(lines, columns, y, x) ⇒ Window
Returns a new instance of Window.
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/textbringer/window.rb', line 269 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_window ⇒ Object (readonly)
Returns the value of attribute bottom_of_window.
267 268 269 |
# File 'lib/textbringer/window.rb', line 267 def bottom_of_window @bottom_of_window end |
#buffer ⇒ Object
Returns the value of attribute buffer.
266 267 268 |
# File 'lib/textbringer/window.rb', line 266 def buffer @buffer end |
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
266 267 268 |
# File 'lib/textbringer/window.rb', line 266 def columns @columns end |
#cursor ⇒ Object (readonly)
Returns the value of attribute cursor.
267 268 269 |
# File 'lib/textbringer/window.rb', line 267 def cursor @cursor end |
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
266 267 268 |
# File 'lib/textbringer/window.rb', line 266 def lines @lines end |
#mode_line ⇒ Object (readonly)
Returns the value of attribute mode_line.
266 267 268 |
# File 'lib/textbringer/window.rb', line 266 def mode_line @mode_line end |
#top_of_window ⇒ Object (readonly)
Returns the value of attribute top_of_window.
267 268 269 |
# File 'lib/textbringer/window.rb', line 267 def top_of_window @top_of_window end |
#window ⇒ Object (readonly)
Returns the value of attribute window.
266 267 268 |
# File 'lib/textbringer/window.rb', line 266 def window @window end |
#x ⇒ Object (readonly)
Returns the value of attribute x.
266 267 268 |
# File 'lib/textbringer/window.rb', line 266 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
266 267 268 |
# File 'lib/textbringer/window.rb', line 266 def y @y end |
Class Method Details
.beep ⇒ nil
262 263 264 |
# File 'lib/textbringer/window.rb', line 262 def self.beep Curses.beep end |
.colors ⇒ Integer
133 134 135 |
# File 'lib/textbringer/window.rb', line 133 def self.colors Curses.colors end |
.columns ⇒ Integer
235 236 237 |
# File 'lib/textbringer/window.rb', line 235 def self.columns Curses.cols end |
.current ⇒ Textbringer::Window .current ⇒ Textbringer::EchoArea Also known as: selected
47 48 49 |
# File 'lib/textbringer/window.rb', line 47 def self.current @@current end |
.current=(arg0) ⇒ Textbringer::Buffer .current=(arg0) ⇒ Textbringer::Buffer
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_windows ⇒ nil .delete_other_windows ⇒ Integer
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(arg0) ⇒ nil .delete_window(arg0) ⇒ Textbringer::Window .delete_window(arg0) ⇒ nil
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_area ⇒ Textbringer::EchoArea
121 122 123 |
# File 'lib/textbringer/window.rb', line 121 def self.echo_area @@echo_area end |
.has_colors=(value) ⇒ Boolean
125 126 127 |
# File 'lib/textbringer/window.rb', line 125 def self.has_colors=(value) @@has_colors = value end |
.has_colors? ⇒ Boolean
129 130 131 |
# File 'lib/textbringer/window.rb', line 129 def self.has_colors? @@has_colors end |
.lines ⇒ Integer
231 232 233 |
# File 'lib/textbringer/window.rb', line 231 def self.lines Curses.lines end |
.list(include_echo_area: false) ⇒ Array[untyped]
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_faces ⇒ Boolean
150 151 152 |
# File 'lib/textbringer/window.rb', line 150 def self.load_faces require_relative "faces/gamegrid" end |
.minibuffer_selected ⇒ nil .minibuffer_selected ⇒ Textbringer::Window
65 66 67 |
# File 'lib/textbringer/window.rb', line 65 def self.minibuffer_selected @@minibuffer_selected end |
.minibuffer_selected=(arg0) ⇒ Textbringer::Window .minibuffer_selected=(arg0) ⇒ nil
69 70 71 |
# File 'lib/textbringer/window.rb', line 69 def self.minibuffer_selected=(window) @@minibuffer_selected = window end |
.other_window ⇒ Textbringer::Window .other_window ⇒ Textbringer::EchoArea
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 |
.redisplay ⇒ nil
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/textbringer/window.rb', line 200 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 |
.redraw ⇒ nil
219 220 221 222 223 224 225 |
# File 'lib/textbringer/window.rb', line 219 def self.redraw @@list.each do |window| window.redraw unless window.current? end current.redraw update end |
.resize ⇒ Integer
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/textbringer/window.rb', line 239 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) ⇒ nil
137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/textbringer/window.rb', line 137 def self.set_default_colors(fg, bg) new_fg = fg || @@default_fg new_bg = bg || @@default_bg fg_num = Color[new_fg] bg_num = Color[new_bg] Curses.assume_default_colors(fg_num, bg_num) @@default_fg = new_fg @@default_bg = new_bg Face.set_default_color_numbers(fg_num, bg_num) Face.define(:default, foreground: new_fg, background: new_bg) Window.redraw if @@started end |
.start ⇒ nil
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 196 197 198 |
# File 'lib/textbringer/window.rb', line 154 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 |
.update ⇒ nil
227 228 229 |
# File 'lib/textbringer/window.rb', line 227 def self.update Curses.doupdate end |
Instance Method Details
#active? ⇒ Boolean
295 296 297 |
# File 'lib/textbringer/window.rb', line 295 def active? true end |
#beginning_of_line_and_count(max_lines, columns = @columns) ⇒ Integer
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 |
# File 'lib/textbringer/window.rb', line 876 def beginning_of_line_and_count(max_lines, columns = @columns) e = @buffer.point @buffer.beginning_of_line bols = [@buffer.point] column = 0 while @buffer.point < e c = @buffer.char_after if c == ?\t n = calc_tab_width(column) str = " " * n else str = escape(c) end column += Buffer.display_width(str) if column > columns # Don't forward_char if column > columns # to handle multibyte characters across the end of lines. bols.push(@buffer.point) column = 0 else @buffer.forward_char if column == columns bols.push(@buffer.point) column = 0 end end end if bols.size > max_lines @buffer.goto_char(bols[-max_lines]) max_lines else @buffer.goto_char(bols.first) bols.size - 1 end end |
#calc_tab_width(column) ⇒ Integer
870 871 872 873 874 |
# File 'lib/textbringer/window.rb', line 870 def calc_tab_width(column) tw = @buffer[:tab_width] n = tw - column % tw n.nonzero? || tw end |
#close ⇒ nil
314 315 316 |
# File 'lib/textbringer/window.rb', line 314 def close @window.close end |
#compose_character(point, cury, curx, c) ⇒ String
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 |
# File 'lib/textbringer/window.rb', line 787 def compose_character(point, cury, curx, c) return c if @buffer.binary? || c.nil? || c.match?(/\p{M}/) if c.match?(/[\u{1100}-\u{115f}]/) # hangul initial consonants return compose_hangul_character(point, cury, curx, c) end pos = @buffer.point + c.bytesize while nextc = @buffer.char_after(pos) case nextc when /\p{Variation_Selector}/ c += nextc when /[\p{Mn}\p{Me}]/ # nonspacing & enclosing marks # Normalize パ (U+30CF + U+309A) to パ (U+30D1) so that curses can # caluculate display width correctly. newc = (c + nextc).unicode_normalize(:nfc) return c if newc.size != c.size c = newc else return c end @buffer.forward_char update_cursor_and_attr(point, cury, curx) pos += nextc.bytesize end c end |
#compose_hangul_character(point, cury, curx, initial) ⇒ String
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 |
# File 'lib/textbringer/window.rb', line 813 def compose_hangul_character(point, cury, curx, initial) pos = @buffer.point + initial.bytesize medial = @buffer.char_after(pos) if !medial&.match?(/[\u{1160}-\u{11a7}]/) return initial end final = @buffer.char_after(pos + medial.bytesize) if final&.match?(/[\u{11a8}-\u{11ff}]/) newc = (initial + medial + final).unicode_normalize(:nfc) if newc.size == 1 2.times do @buffer.forward_char update_cursor_and_attr(point, cury, curx) end newc else c end else newc = (initial + medial).unicode_normalize(:nfc) if newc.size == 1 @buffer.forward_char update_cursor_and_attr(point, cury, curx) newc else c end end end |
#current? ⇒ Boolean
344 345 346 |
# File 'lib/textbringer/window.rb', line 344 def current? self == @@current end |
#current_or_minibuffer_selected? ⇒ Boolean
348 349 350 |
# File 'lib/textbringer/window.rb', line 348 def current_or_minibuffer_selected? self == @@current || self == @@minibuffer_selected end |
#delete ⇒ Boolean
303 304 305 306 307 308 309 310 311 312 |
# File 'lib/textbringer/window.rb', line 303 def delete unless @deleted if current? Window.current = @@list.first end delete_marks @window.close @deleted = true end end |
#delete_marks ⇒ nil
912 913 914 915 916 917 918 919 920 921 922 923 924 925 |
# File 'lib/textbringer/window.rb', line 912 def delete_marks if @top_of_window @top_of_window.delete @top_of_window = nil end if @bottom_of_window @bottom_of_window.delete @bottom_of_window = nil end if @point_mark @point_mark.delete @point_mark = nil end end |
#deleted? ⇒ Boolean
299 300 301 |
# File 'lib/textbringer/window.rb', line 299 def deleted? @deleted end |
#echo_area? ⇒ Boolean
291 292 293 |
# File 'lib/textbringer/window.rb', line 291 def echo_area? false end |
#enlarge(arg0) ⇒ Array[untyped] #enlarge(arg0) ⇒ Integer
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 647 648 649 |
# File 'lib/textbringer/window.rb', line 610 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 |
#escape(s) ⇒ String
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 |
# File 'lib/textbringer/window.rb', line 843 def escape(s) if !s.valid_encoding? s = s.b end if @buffer.binary? s.gsub(/[\0-\b\v-\x1f\x7f]/) { |c| "^" + (c.ord ^ 0x40).chr }.gsub(/[\x80-\xff]/n) { |c| "<%02X>" % c.ord } else s.gsub(/ (?<ascii_control>[\0-\b\v-\x1f\x7f]) | (?<nonascii_control>\p{C}) | (?<other_special_char>[\p{M}\p{In_Hangul_Jamo}]) /x) { |c| if $~[:ascii_control] "^" + (c.ord ^ 0x40).chr elsif $~[:nonascii_control] "<%04x>" % c.ord elsif $~[:other_special_char] FALLBACK_CHARACTERS[c] || c end } end end |
#framer ⇒ nil #framer ⇒ Integer
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 |
# File 'lib/textbringer/window.rb', line 677 def framer @buffer.save_point do |saved| max = lines - 1 # lines include mode line count = beginning_of_line_and_count(max) new_start_loc = @buffer.point if @buffer.point_before_mark?(@top_of_window) @buffer.mark_to_point(@top_of_window) return end while count < max break if @buffer.point_at_mark?(@top_of_window) break if @buffer.point == 0 new_start_loc = @buffer.point @buffer.backward_char count += beginning_of_line_and_count(max - count - 1) + 1 end if count >= lines - 1 # lines include mode line @top_of_window.location = new_start_loc end end end |
#get_char ⇒ String #get_char ⇒ Integer
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 |
# File 'lib/textbringer/window.rb', line 927 def get_char if @key_buffer.empty? Curses.save_key_modifiers(true) if HAVE_GET_KEY_MODIFIERS begin need_retry = false if @raw_key_buffer.empty? key = @window.get_char else key = @raw_key_buffer.shift end if HAVE_GET_KEY_MODIFIERS mods = Curses.get_key_modifiers if key.is_a?(String) && key.ascii_only? if (mods & Curses::PDC_KEY_MODIFIER_CONTROL) != 0 key = key == ?? ? "\x7f" : (key.ord & 0x9f).chr end if (mods & Curses::PDC_KEY_MODIFIER_ALT) != 0 if key == "\0" # Alt + `, Alt + < etc. return NUL, so ignore it. need_retry = true else @key_buffer.push(key) key = "\e" end end end end end while need_retry key else @key_buffer.shift end end |
#has_input? ⇒ Boolean
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
# File 'lib/textbringer/window.rb', line 395 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 |
#highlight ⇒ String #highlight ⇒ nil
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 |
# File 'lib/textbringer/window.rb', line 411 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 |
#initialize_window(num_lines, num_columns, y, x) ⇒ void
This method returns an undefined value.
672 673 674 675 |
# File 'lib/textbringer/window.rb', line 672 def initialize_window(num_lines, num_columns, y, x) @window = Curses::Window.new(num_lines - 1, num_columns, y, x) @mode_line = Curses::Window.new(1, num_columns, y + num_lines - 1, x) end |
#isearch_attr ⇒ Integer
108 |
# File 'sig/lib/textbringer/window.rbs', line 108
def isearch_attr: () -> Integer
|
#move(y, x) ⇒ Integer
535 536 537 538 539 540 |
# File 'lib/textbringer/window.rb', line 535 def move(y, x) @y = y @x = x @window.move(y, x) @mode_line.move(y + @window.maxy, x) end |
#read_event ⇒ String #read_event ⇒ Symbol
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
# File 'lib/textbringer/window.rb', line 352 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_nonblock ⇒ String #read_event_nonblock ⇒ nil
370 371 372 373 374 375 376 377 |
# File 'lib/textbringer/window.rb', line 370 def read_event_nonblock @window.nodelay = true begin read_event ensure @window.nodelay = false end end |
#recenter ⇒ Integer
550 551 552 553 554 555 556 557 558 559 560 561 |
# File 'lib/textbringer/window.rb', line 550 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_needed ⇒ Integer #recenter_if_needed ⇒ nil
563 564 565 566 567 568 |
# File 'lib/textbringer/window.rb', line 563 def recenter_if_needed if @buffer.point_before_mark?(@top_of_window) || @buffer.point_after_mark?(@bottom_of_window) recenter end end |
#redisplay ⇒ nil
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 526 527 528 |
# File 'lib/textbringer/window.rb', line 428 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 |
#redisplay_mode_line ⇒ nil
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 |
# File 'lib/textbringer/window.rb', line 699 def redisplay_mode_line @mode_line.erase @mode_line.setpos(0, 0) face = Face[:mode_line] @mode_line.attr_set(face.text_attrs, face.color_pair) @mode_line.addstr("#{@buffer.input_method_status} #{@buffer.name} ") @mode_line.addstr("[+]") if @buffer.modified? @mode_line.addstr("[RO]") if @buffer.read_only? @mode_line.addstr("[#{@buffer.file_encoding.name}/") @mode_line.addstr("#{@buffer.file_format}] ") if current? || @buffer.point_at_mark?(@point_mark) c = @buffer.char_after line = @buffer.current_line column = @buffer.current_column else c = @buffer.char_after(@point_mark.location) line, column = @buffer.get_line_and_column(@point_mark.location) end @mode_line.addstr(unicode_codepoint(c)) @mode_line.addstr(" #{line},#{column}") @mode_line.addstr(" (#{@buffer.mode_names.join(' ')})") @mode_line.addstr(" " * (columns - @mode_line.curx)) default = Face[:default] @mode_line.attr_set(default&.text_attrs || 0, default&.color_pair || 0) @mode_line.noutrefresh end |
#redraw ⇒ nil
530 531 532 533 |
# File 'lib/textbringer/window.rb', line 530 def redraw @window.redraw @mode_line.redraw end |
#region_attr ⇒ Integer
110 |
# File 'sig/lib/textbringer/window.rbs', line 110
def region_attr: () -> Integer
|
#resize(lines, columns) ⇒ Integer
542 543 544 545 546 547 548 |
# File 'lib/textbringer/window.rb', line 542 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_point ⇒ Integer
340 341 342 |
# File 'lib/textbringer/window.rb', line 340 def restore_point @buffer.point_to_mark(@point_mark) end |
#save_point ⇒ Integer
332 333 334 335 336 337 338 |
# File 'lib/textbringer/window.rb', line 332 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_down ⇒ Integer #scroll_down ⇒ nil
580 581 582 583 584 585 586 587 588 |
# File 'lib/textbringer/window.rb', line 580 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_up ⇒ Integer #scroll_up ⇒ nil
570 571 572 573 574 575 576 577 578 |
# File 'lib/textbringer/window.rb', line 570 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(arg0) ⇒ Integer #shrink(arg0) ⇒ Array[untyped]
651 652 653 |
# File 'lib/textbringer/window.rb', line 651 def shrink(n) enlarge(-n) end |
#shrink_if_larger_than_buffer ⇒ Integer #shrink_if_larger_than_buffer ⇒ nil
655 656 657 658 659 660 661 662 663 664 665 666 667 668 |
# File 'lib/textbringer/window.rb', line 655 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(arg0) ⇒ Array[untyped] #split(arg0) ⇒ Array[untyped] #split(arg0) ⇒ nil
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 |
# File 'lib/textbringer/window.rb', line 590 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 |
#unicode_codepoint(arg0) ⇒ String #unicode_codepoint(arg0) ⇒ String
726 727 728 729 730 731 732 733 734 |
# File 'lib/textbringer/window.rb', line 726 def unicode_codepoint(c) if c.nil? "<EOF>" else "U+%04X" % c.ord end rescue ArgumentError "0x" + c.unpack("H*")[0] end |
#update_cursor_and_attr(arg0, arg1, arg2) ⇒ nil #update_cursor_and_attr(arg0, arg1, arg2) ⇒ Boolean
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 |
# File 'lib/textbringer/window.rb', line 736 def update_cursor_and_attr(point, cury, curx) changed = false if @buffer.point_at_mark?(point) @cursor.y = cury @cursor.x = curx # Handle visible mark transitions if current_or_minibuffer_selected? && @buffer.visible_mark if @buffer.point_after_mark?(@buffer.visible_mark) @in_region = false changed = true elsif @buffer.point_before_mark?(@buffer.visible_mark) @in_region = true changed = true end end # Handle isearch mark transitions if current_or_minibuffer_selected? && @buffer.isearch_mark if @buffer.point_after_mark?(@buffer.isearch_mark) @in_isearch = false changed = true elsif @buffer.point_before_mark?(@buffer.isearch_mark) @in_isearch = true changed = true end end end # Handle transitions when point crosses isearch mark if current_or_minibuffer_selected? && @buffer.isearch_mark && @buffer.point_at_mark?(@buffer.isearch_mark) if @buffer.point_after_mark?(point) @in_isearch = false changed = true elsif @buffer.point_before_mark?(point) @in_isearch = true changed = true end end # Handle transitions when point crosses visible mark if current_or_minibuffer_selected? && @buffer.visible_mark && @buffer.point_at_mark?(@buffer.visible_mark) if @buffer.point_after_mark?(point) @in_region = false changed = true elsif @buffer.point_before_mark?(point) @in_region = true changed = true end end apply_window_attrs(@window) if changed end |
#wait_input(arg0) ⇒ nil #wait_input(arg0) ⇒ String
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
# File 'lib/textbringer/window.rb', line 379 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 |