3 class ScrollMode < Mode
4 ## we define topline and botline as the top and bottom lines of any
5 ## content in the currentview.
7 ## we left leftcol and rightcol as the left and right columns of any
8 ## content in the current view. but since we're operating in a
9 ## line-centric fashion, rightcol is always leftcol + the buffer
10 ## width. (whereas botline is topline + at most the buffer height,
11 ## and can be == to topline in the case that there's no content.)
13 attr_reader :status, :topline, :botline, :leftcol
17 register_keymap do |k|
18 k.add :line_down, "Down one line", :down, 'j', 'J'
19 k.add :line_up, "Up one line", :up, 'k', 'K'
20 k.add :col_left, "Left one column", :left, 'h'
21 k.add :col_right, "Right one column", :right, 'l'
22 k.add :page_down, "Down one page", :page_down, ' '
23 k.add :page_up, "Up one page", :page_up, 'p', :backspace
24 k.add :jump_to_start, "Jump to top", :home, '^', '1'
25 k.add :jump_to_end, "Jump to bottom", :end, '$', '0'
26 k.add :jump_to_left, "Jump to the left", '['
27 k.add :search_in_buffer, "Search in current buffer", '/'
28 k.add :continue_search_in_buffer, "Jump to next search occurrence in buffer", BufferManager::CONTINUE_IN_BUFFER_SEARCH_KEY
31 def initialize opts={}
32 @topline, @botline, @leftcol = 0, 0, 0
33 @slip_rows = opts[:slip_rows] || 0 # when we pgup/pgdown,
34 # how many lines do we keep?
35 @twiddles = opts.member?(:twiddles) ? opts[:twiddles] : true
42 def rightcol; @leftcol + buffer.content_width; end
46 (@topline ... @botline).each { |ln| draw_line ln }
47 ((@botline - @topline) ... buffer.content_height).each do |ln|
49 buffer.write ln, 0, "~", :color => :twiddle_color
51 buffer.write ln, 0, ""
54 @status = "lines #{@topline + 1}:#{@botline}/#{lines}"
57 def in_search?; @search_line end
58 def cancel_search!; @search_line = nil end
60 def continue_search_in_buffer
62 BufferManager.flash "No current search!"
66 start = @search_line || search_start_line
67 line, col = find_text @search_query, start
68 if line.nil? && (start > 0)
69 line, col = find_text @search_query, 0
70 BufferManager.flash "Search wrapped to top!" if line
73 @search_line = line + 1
74 search_goto_pos line, col, col + @search_query.length
77 BufferManager.flash "Not found!"
82 query = BufferManager.ask :search, "search in buffer: "
83 return if query.nil? || query.empty?
84 @search_query = Regexp.escape query
85 continue_search_in_buffer
88 ## subclasses can override these two!
89 def search_goto_pos line, leftcol, rightcol
92 if rightcol > self.rightcol # if it's occluded...
93 jump_to_col [rightcol - buffer.content_width + 1, 0].max # move right
96 def search_start_line; @topline end
99 return unless @leftcol > 0
110 col = col - (col % COL_JUMP)
111 buffer.mark_dirty unless @leftcol == col
115 def jump_to_left; jump_to_col 0; end
119 l = l.clamp 0, lines - 1
120 return if @topline == l
122 @botline = [l + buffer.content_height, lines].min
126 def at_top?; @topline == 0 end
127 def at_bottom?; @botline == lines end
129 def line_down; jump_to_line @topline + 1; end
130 def line_up; jump_to_line @topline - 1; end
131 def page_down; jump_to_line @topline + buffer.content_height - @slip_rows; end
132 def page_up; jump_to_line @topline - buffer.content_height + @slip_rows; end
133 def jump_to_start; jump_to_line 0; end
134 def jump_to_end; jump_to_line lines - buffer.content_height; end
136 def ensure_mode_validity
137 @topline = @topline.clamp 0, [lines - 1, 0].max
138 @botline = [@topline + buffer.content_height, lines].min
148 def find_text query, start_line
150 (start_line ... lines).each do |i|
154 return [i, match] if match
157 s.each do |color, string|
158 match = string =~ regex
160 return [i, offset + match]
162 offset += string.length
170 def draw_line ln, opts={}
171 regex = /(#{@search_query})/i
175 draw_line_from_array ln, matching_text_array(s, regex), opts
177 draw_line_from_string ln, s, opts
181 ## seems like there ought to be a better way of doing this
183 s.each do |color, text|
185 array += matching_text_array text, regex, color
187 array << [color, text]
190 draw_line_from_array ln, array, opts
192 draw_line_from_array ln, s, opts
195 raise "unknown drawable object: #{s.inspect} in #{self} for line #{ln}" # good for debugging
199 # str = s.map { |color, text| text }.join
200 # buffer.write ln - @topline, 0, str, :color => :none, :highlight => opts[:highlight]
204 def matching_text_array s, regex, oldcolor=:none
205 s.split(regex).map do |text|
208 [:search_highlight_color, text]
212 end.compact + [[oldcolor, ""]]
215 def draw_line_from_array ln, a, opts
217 a.each do |color, text|
218 raise "nil text for color '#{color}'" if text.nil? # good for debugging
220 if xpos + text.length < @leftcol
221 buffer.write ln - @topline, 0, "", :color => color,
222 :highlight => opts[:highlight]
224 elsif xpos < @leftcol
226 buffer.write ln - @topline, 0, text[(@leftcol - xpos) .. -1],
228 :highlight => opts[:highlight]
231 buffer.write ln - @topline, xpos - @leftcol, text,
232 :color => color, :highlight => opts[:highlight]
238 def draw_line_from_string ln, s, opts
239 buffer.write ln - @topline, 0, s[@leftcol .. -1], :highlight => opts[:highlight]