]> git.cworth.org Git - sup/blob - lib/sup/modes/scroll-mode.rb
vi-style scroll keys
[sup] / lib / sup / modes / scroll-mode.rb
1 module Redwood
2
3 class ScrollMode < Mode
4   ## we define topline and botline as the top and bottom lines of any
5   ## content in the currentview.
6   
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.)
12
13   attr_reader :status, :topline, :botline, :leftcol
14
15   COL_JUMP = 2
16
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
31   end
32
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
38     @search_query = nil
39     @search_line = nil
40     @status = ""
41     super()
42   end
43
44   def rightcol; @leftcol + buffer.content_width; end
45
46   def draw
47     ensure_mode_validity
48     (@topline ... @botline).each { |ln| draw_line ln }
49     ((@botline - @topline) ... buffer.content_height).each do |ln|
50       if @twiddles
51         buffer.write ln, 0, "~", :color => :twiddle_color
52       else
53         buffer.write ln, 0, ""
54       end
55     end
56     @status = "lines #{@topline + 1}:#{@botline}/#{lines}"
57   end
58
59   def in_search?; @search_line end
60   def cancel_search!; @search_line = nil end
61
62   def continue_search_in_buffer
63     unless @search_query
64       BufferManager.flash "No current search!"
65       return
66     end
67
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
73     end
74     if line
75       @search_line = line + 1
76       search_goto_pos line, col, col + @search_query.length
77       buffer.mark_dirty
78     else
79       BufferManager.flash "Not found!"
80     end
81   end
82
83   def search_in_buffer
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
88   end
89
90   ## subclasses can override these two!
91   def search_goto_pos line, leftcol, rightcol
92     jump_to_line line
93
94     if rightcol > self.rightcol # if it's occluded...
95       jump_to_col [rightcol - buffer.content_width + 1, 0].max # move right
96     end
97   end
98   def search_start_line; @topline end
99
100   def col_left
101     return unless @leftcol > 0
102     @leftcol -= COL_JUMP
103     buffer.mark_dirty
104   end
105
106   def col_right
107     @leftcol += COL_JUMP
108     buffer.mark_dirty
109   end
110
111   def jump_to_col col
112     col = col - (col % COL_JUMP)
113     buffer.mark_dirty unless @leftcol == col
114     @leftcol = col
115   end
116
117   def jump_to_left; jump_to_col 0; end
118
119   ## set top line to l
120   def jump_to_line l
121     l = l.clamp 0, lines - 1
122     return if @topline == l
123     @topline = l
124     @botline = [l + buffer.content_height, lines].min
125     buffer.mark_dirty
126   end
127
128   def at_top?; @topline == 0 end
129   def at_bottom?; @botline == lines end
130
131   def line_down; jump_to_line @topline + 1; end
132   def line_up;  jump_to_line @topline - 1; end
133   def page_down; jump_to_line @topline + buffer.content_height - @slip_rows; end
134   def page_up; jump_to_line @topline - buffer.content_height + @slip_rows; end
135   def half_page_down; jump_to_line @topline + buffer.content_height / 2; end
136   def half_page_up; jump_to_line @topline - buffer.content_height / 2; end
137   def jump_to_start; jump_to_line 0; end
138   def jump_to_end; jump_to_line lines - buffer.content_height; end
139
140   def ensure_mode_validity
141     @topline = @topline.clamp 0, [lines - 1, 0].max
142     @botline = [@topline + buffer.content_height, lines].min
143   end
144
145   def resize *a
146     super(*a)
147     ensure_mode_validity
148   end
149
150 protected
151
152   def find_text query, start_line
153     regex = /#{query}/i
154     (start_line ... lines).each do |i|
155       case(s = self[i])
156       when String
157         match = s =~ regex
158         return [i, match] if match
159       when Array
160         offset = 0
161         s.each do |color, string|
162           match = string =~ regex
163           if match
164             return [i, offset + match]
165           else
166             offset += string.length
167           end
168         end
169       end
170     end
171     nil
172   end
173
174   def draw_line ln, opts={}
175     regex = /(#{@search_query})/i
176     case(s = self[ln])
177     when String
178       if in_search?
179         draw_line_from_array ln, matching_text_array(s, regex), opts
180       else
181         draw_line_from_string ln, s, opts
182       end
183     when Array
184       if in_search?
185         ## seems like there ought to be a better way of doing this
186         array = []
187         s.each do |color, text| 
188           if text =~ regex
189             array += matching_text_array text, regex, color
190           else
191             array << [color, text]
192           end
193         end
194         draw_line_from_array ln, array, opts
195       else
196         draw_line_from_array ln, s, opts
197       end
198     else
199       raise "unknown drawable object: #{s.inspect} in #{self} for line #{ln}" # good for debugging
200     end
201
202       ## speed test
203       # str = s.map { |color, text| text }.join
204       # buffer.write ln - @topline, 0, str, :color => :none, :highlight => opts[:highlight]
205       # return
206   end
207
208   def matching_text_array s, regex, oldcolor=:none
209     s.split(regex).map do |text|
210       next if text.empty?
211       if text =~ regex
212         [:search_highlight_color, text]
213       else
214         [oldcolor, text]
215       end
216     end.compact + [[oldcolor, ""]]
217   end
218
219   def draw_line_from_array ln, a, opts
220     xpos = 0
221     a.each do |color, text|
222       raise "nil text for color '#{color}'" if text.nil? # good for debugging
223       
224       if xpos + text.length < @leftcol
225         buffer.write ln - @topline, 0, "", :color => color,
226                      :highlight => opts[:highlight]
227         xpos += text.length
228       elsif xpos < @leftcol
229         ## partial
230         buffer.write ln - @topline, 0, text[(@leftcol - xpos) .. -1],
231                      :color => color,
232                      :highlight => opts[:highlight]
233         xpos += text.length
234       else
235         buffer.write ln - @topline, xpos - @leftcol, text,
236                      :color => color, :highlight => opts[:highlight]
237         xpos += text.length
238       end
239     end
240   end
241
242   def draw_line_from_string ln, s, opts
243     buffer.write ln - @topline, 0, s[@leftcol .. -1], :highlight => opts[:highlight]
244   end
245 end
246
247 end
248