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