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', "\C-e"
19 k.add :line_up, "Up one line", :up, 'k', 'K', "\C-y"
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, ' ', "\C-f"
23 k.add :page_up, "Up one page", :page_up, 'p', :backspace, "\C-b"
24 k.add :half_page_down, "Down one half page", "\C-d"
25 k.add :half_page_up, "Up one half page", "\C-u"
26 k.add :jump_to_start, "Jump to top", :home, '^', '1'
27 k.add :jump_to_end, "Jump to bottom", :end, '$', '0'
28 k.add :jump_to_left, "Jump to the left", '['
29 k.add :search_in_buffer, "Search in current buffer", '/'
30 k.add :continue_search_in_buffer, "Jump to next search occurrence in buffer", BufferManager::CONTINUE_IN_BUFFER_SEARCH_KEY
33 def initialize opts={}
34 @topline, @botline, @leftcol = 0, 0, 0
35 @slip_rows = opts[:slip_rows] || 0 # when we pgup/pgdown,
36 # how many lines do we keep?
37 @twiddles = opts.member?(:twiddles) ? opts[:twiddles] : true
44 def rightcol; @leftcol + buffer.content_width; end
48 (@topline ... @botline).each { |ln| draw_line ln }
49 ((@botline - @topline) ... buffer.content_height).each do |ln|
51 buffer.write ln, 0, "~", :color => :twiddle_color
53 buffer.write ln, 0, ""
56 @status = "lines #{@topline + 1}:#{@botline}/#{lines}"
59 def in_search?; @search_line end
60 def cancel_search!; @search_line = nil end
62 def continue_search_in_buffer
64 BufferManager.flash "No current search!"
68 start = @search_line || search_start_line
69 line, col = find_text @search_query, start
70 if line.nil? && (start > 0)
71 line, col = find_text @search_query, 0
72 BufferManager.flash "Search wrapped to top!" if line
75 @search_line = line + 1
76 search_goto_pos line, col, col + @search_query.display_length
79 BufferManager.flash "Not found!"
84 query = BufferManager.ask :search, "search in buffer: "
85 return if query.nil? || query.empty?
86 @search_query = Regexp.escape query
87 continue_search_in_buffer
90 ## subclasses can override these three!
91 def search_goto_pos line, leftcol, rightcol
94 if rightcol > self.rightcol # if it's occluded...
95 jump_to_col [rightcol - buffer.content_width + 1, 0].max # move right
98 def search_start_line; @topline end
99 def search_goto_line line; jump_to_line line end
102 return unless @leftcol > 0
113 col = col - (col % COL_JUMP)
114 buffer.mark_dirty unless @leftcol == col
118 def jump_to_left; jump_to_col 0; end
122 l = l.clamp 0, lines - 1
123 return if @topline == l
125 @botline = [l + buffer.content_height, lines].min
129 def at_top?; @topline == 0 end
130 def at_bottom?; @botline == lines end
132 def line_down; jump_to_line @topline + 1; end
133 def line_up; jump_to_line @topline - 1; end
134 def page_down; jump_to_line @topline + buffer.content_height - @slip_rows; end
135 def page_up; jump_to_line @topline - buffer.content_height + @slip_rows; end
136 def half_page_down; jump_to_line @topline + buffer.content_height / 2; end
137 def half_page_up; jump_to_line @topline - buffer.content_height / 2; end
138 def jump_to_start; jump_to_line 0; end
139 def jump_to_end; jump_to_line lines - buffer.content_height; end
141 def ensure_mode_validity
142 @topline = @topline.clamp 0, [lines - 1, 0].max
143 @botline = [@topline + buffer.content_height, lines].min
153 def find_text query, start_line
155 (start_line ... lines).each do |i|
159 return [i, match] if match
162 s.each do |color, string|
163 match = string =~ regex
165 return [i, offset + match]
167 offset += string.display_length
175 def draw_line ln, opts={}
176 regex = /(#{@search_query})/i
180 draw_line_from_array ln, matching_text_array(s, regex), opts
182 draw_line_from_string ln, s, opts
186 ## seems like there ought to be a better way of doing this
188 s.each do |color, text|
190 array += matching_text_array text, regex, color
192 array << [color, text]
195 draw_line_from_array ln, array, opts
197 draw_line_from_array ln, s, opts
200 raise "unknown drawable object: #{s.inspect} in #{self} for line #{ln}" # good for debugging
204 # str = s.map { |color, text| text }.join
205 # buffer.write ln - @topline, 0, str, :color => :none, :highlight => opts[:highlight]
209 def matching_text_array s, regex, oldcolor=:none
210 s.split(regex).map do |text|
213 [:search_highlight_color, text]
217 end.compact + [[oldcolor, ""]]
220 def draw_line_from_array ln, a, opts
222 a.each_with_index do |(color, text), i|
223 raise "nil text for color '#{color}'" if text.nil? # good for debugging
224 l = text.display_length
225 no_fill = i != a.size - 1
227 if xpos + l < @leftcol
228 buffer.write ln - @topline, 0, "", :color => color,
229 :highlight => opts[:highlight]
230 elsif xpos < @leftcol
232 buffer.write ln - @topline, 0, text[(@leftcol - xpos) .. -1],
234 :highlight => opts[:highlight], :no_fill => no_fill
236 buffer.write ln - @topline, xpos - @leftcol, text,
237 :color => color, :highlight => opts[:highlight],
244 def draw_line_from_string ln, s, opts
245 buffer.write ln - @topline, 0, s[@leftcol .. -1], :highlight => opts[:highlight]