aboutsummaryrefslogtreecommitdiff
path: root/plugins/linecopypaste.lua
blob: 23933a1c30d1355f66c72cd8907efca886dffc4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
-- mod-version:3 --lite-xl 2.1
local core = require "core"
local command = require "core.command"

local function doc()
  return core.active_view.doc
end

local line_in_clipboard = false

local doc_copy = command.map["doc:copy"].perform
command.map["doc:copy"].perform = function()
  if doc():has_selection() then
    doc_copy()
    line_in_clipboard = false
  else
    local line = doc():get_selection()
    system.set_clipboard(doc().lines[line])
    line_in_clipboard = true
  end
end

local doc_cut = command.map["doc:cut"].perform
command.map["doc:cut"].perform = function()
  if doc():has_selection() then
    doc_cut()
    line_in_clipboard = false
  else
    local line = doc():get_selection()
    system.set_clipboard(doc().lines[line])
    if line < #(doc().lines) then
      doc():remove(line, 1, line+1, 1)
    else -- last line in file
      doc():remove(line, 1, line, #(doc().lines[line]))
    end
    doc():set_selection(line, 1)
    line_in_clipboard = true
  end
end

local doc_paste = command.map["doc:paste"].perform
command.map["doc:paste"].perform = function()
  if line_in_clipboard == false then
    doc_paste()
  else
    local line, col = doc():get_selection()
    doc():insert(line, 1, system.get_clipboard():gsub("\r", ""))
    doc():set_selection(line+1, col)
  end
end