3 class ScrollMode < Mode
4 attr_reader :status, :topline, :botline
9 k.add :line_down, "Down one line", :down, 'j', 'J'
10 k.add :line_up, "Up one line", :up, 'k', 'K'
11 k.add :col_left, "Left one column", :left, 'h'
12 k.add :col_right, "Right one column", :right, 'l'
13 k.add :page_down, "Down one page", :page_down, 'n', ' '
14 k.add :page_up, "Up one page", :page_up, 'p', :backspace
15 k.add :jump_to_home, "Jump to top", :home, '^', '1'
16 k.add :jump_to_end, "Jump to bottom", :end, '$', '0'
19 def initialize opts={}
20 @topline, @botline, @leftcol = 0, 0, 0
21 @slip_rows = opts[:slip_rows] || 0 # when we pgup/pgdown,
22 # how many lines do we keep?
23 @twiddles = opts.member?(:twiddles) ? opts[:twiddles] : true
29 (@topline ... @botline).each { |ln| draw_line ln }
30 ((@botline - @topline) ... buffer.content_height).each do |ln|
32 buffer.write ln, 0, "~", :color => :twiddle_color
34 buffer.write ln, 0, ""
37 @status = "lines #{@topline + 1}:#{@botline}/#{lines}"
41 return unless @leftcol > 0
53 l = l.clamp 0, lines - 1
54 return if @topline == l
56 @botline = [l + buffer.content_height, lines].min
60 def line_down; jump_to_line @topline + 1; end
61 def line_up; jump_to_line @topline - 1; end
62 def page_down; jump_to_line @topline + buffer.content_height - @slip_rows; end
63 def page_up; jump_to_line @topline - buffer.content_height + @slip_rows; end
64 def jump_to_home; jump_to_line 0; end
65 def jump_to_end; jump_to_line lines - buffer.content_height; end
67 def ensure_mode_validity
68 @topline = @topline.clamp 0, lines - 1
69 @topline = 0 if @topline < 0 # empty
70 @botline = [@topline + buffer.content_height, lines].min
75 def draw_line ln, opts={}
78 buffer.write ln - @topline, 0, s[@leftcol .. -1],
79 :highlight => opts[:highlight]
82 s.each do |color, text|
83 raise "nil text for color '#{color}'" if text.nil?
84 if xpos + text.length < @leftcol
85 buffer.write ln - @topline, 0, "", :color => color,
86 :highlight => opts[:highlight]
91 buffer.write ln - @topline, 0, text[(@leftcol - xpos) .. -1],
93 :highlight => opts[:highlight]
96 buffer.write ln - @topline, xpos - @leftcol, text,
97 :color => color, :highlight => opts[:highlight]