@@ -192,28 +192,42 @@ function DocView:scroll_to_make_visible(line, col)
192
192
end
193
193
194
194
195
+ local function mouse_selection (doc , clicks , line1 , col1 , line2 , col2 )
196
+ local swap = line2 < line1 or line2 == line1 and col2 <= col1
197
+ if swap then
198
+ line1 , col1 , line2 , col2 = line2 , col2 , line1 , col1
199
+ end
200
+ if clicks == 2 then
201
+ line1 , col1 = translate .start_of_word (doc , line1 , col1 )
202
+ line2 , col2 = translate .end_of_word (doc , line2 , col2 )
203
+ elseif clicks == 3 then
204
+ if line2 == # doc .lines and doc .lines [# doc .lines ] ~= " \n " then
205
+ doc :insert (math.huge , math.huge , " \n " )
206
+ end
207
+ line1 , col1 , line2 , col2 = line1 , 1 , line2 + 1 , 1
208
+ end
209
+ if swap then
210
+ return line2 , col2 , line1 , col1
211
+ end
212
+ return line1 , col1 , line2 , col2
213
+ end
214
+
215
+
195
216
function DocView :on_mouse_pressed (button , x , y , clicks )
196
217
local caught = DocView .super .on_mouse_pressed (self , button , x , y , clicks )
197
218
if caught then
198
219
return
199
220
end
200
- local line , col = self :resolve_screen_position (x , y )
201
- if clicks == 2 then
202
- local line1 , col1 = translate .start_of_word (self .doc , line , col )
203
- local line2 , col2 = translate .end_of_word (self .doc , line , col )
204
- self .doc :set_selection (line2 , col2 , line1 , col1 )
205
- elseif clicks == 3 then
206
- if line == # self .doc .lines then
207
- self .doc :insert (math.huge , math.huge , " \n " )
221
+ if keymap .modkeys [" shift" ] then
222
+ if clicks == 1 then
223
+ local line , col = self .doc :get_selection ()
224
+ self .mouse_selecting = { line , col , clicks = 1 }
225
+ self :on_mouse_moved (x , y )
208
226
end
209
- self .doc :set_selection (line + 1 , 1 , line , 1 )
210
227
else
211
- local line2 , col2
212
- if keymap .modkeys [" shift" ] then
213
- line2 , col2 = select (3 , self .doc :get_selection ())
214
- end
215
- self .doc :set_selection (line , col , line2 , col2 )
216
- self .mouse_selecting = true
228
+ local line , col = self :resolve_screen_position (x , y )
229
+ self .doc :set_selection (mouse_selection (self .doc , clicks , line , col , line , col ))
230
+ self .mouse_selecting = { line , col , clicks = clicks }
217
231
end
218
232
self .blink_timer = 0
219
233
end
@@ -229,16 +243,17 @@ function DocView:on_mouse_moved(x, y, ...)
229
243
end
230
244
231
245
if self .mouse_selecting then
232
- local _ , _ , line2 , col2 = self .doc :get_selection ()
233
- local line1 , col1 = self :resolve_screen_position (x , y )
234
- self .doc :set_selection (line1 , col1 , line2 , col2 )
246
+ local l1 , c1 = self :resolve_screen_position (x , y )
247
+ local l2 , c2 = table.unpack (self .mouse_selecting )
248
+ local clicks = self .mouse_selecting .clicks
249
+ self .doc :set_selection (mouse_selection (self .doc , clicks , l1 , c1 , l2 , c2 ))
235
250
end
236
251
end
237
252
238
253
239
254
function DocView :on_mouse_released (button )
240
255
DocView .super .on_mouse_released (self , button )
241
- self .mouse_selecting = false
256
+ self .mouse_selecting = nil
242
257
end
243
258
244
259
0 commit comments