Skip to content

Commit 53d555b

Browse files
committed
Added support for mouse double/triple click+drag selection
Resolves #159 Resolves #161
1 parent 11df722 commit 53d555b

File tree

1 file changed

+34
-19
lines changed

1 file changed

+34
-19
lines changed

data/core/docview.lua

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -192,28 +192,42 @@ function DocView:scroll_to_make_visible(line, col)
192192
end
193193

194194

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+
195216
function DocView:on_mouse_pressed(button, x, y, clicks)
196217
local caught = DocView.super.on_mouse_pressed(self, button, x, y, clicks)
197218
if caught then
198219
return
199220
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)
208226
end
209-
self.doc:set_selection(line + 1, 1, line, 1)
210227
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 }
217231
end
218232
self.blink_timer = 0
219233
end
@@ -229,16 +243,17 @@ function DocView:on_mouse_moved(x, y, ...)
229243
end
230244

231245
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))
235250
end
236251
end
237252

238253

239254
function DocView:on_mouse_released(button)
240255
DocView.super.on_mouse_released(self, button)
241-
self.mouse_selecting = false
256+
self.mouse_selecting = nil
242257
end
243258

244259

0 commit comments

Comments
 (0)