-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.lua
More file actions
101 lines (80 loc) · 2.21 KB
/
Copy pathcontext.lua
File metadata and controls
101 lines (80 loc) · 2.21 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
local micro = import("micro")
local buffer = import("micro/buffer")
local utf8 = import("unicode/utf8")
local config = import("micro/config")
local plug_path = config.ConfigDir .. "/plug/?.lua"
if not package.path:find(plug_path, 1, true) then
package.path = package.path .. ";" .. plug_path
end
local utils = require("vi/utils")
local bell = require("vi/bell")
local pre_memory = nil
local memory = nil
local function pre_memorize()
local cursor = micro.CurPane().Buf:GetActiveCursor()
pre_memory = buffer.Loc(cursor.X, cursor.Y)
end
local function memorize()
local cursor = micro.CurPane().Buf:GetActiveCursor()
if pre_memory then
if cursor.Y ~= pre_memory.Y then
memory = pre_memory
end
else
memory = buffer.Loc(cursor.X, cursor.Y)
end
pre_memory = nil
end
local function return_by_chars()
if not memory then
return
end
local buf = micro.CurPane().Buf
local last_line_index = utils.last_line_index(buf)
if memory.Y > last_line_index then
bell.ring("line " .. memory.Y + 1 .. " (> " .. last_line_index + 1 .. ") not exists")
return
end
local line = buf:Line(memory.Y)
local length = utf8.RuneCount(line)
local last_column = math.max(length - 1, 0)
if memory.X > last_column then
bell.ring("column " .. memory.X + 1 .. " (> " .. length .. ") not exists")
return
end
local cursor = buf:GetActiveCursor()
local current = buffer.Loc(cursor.X, cursor.Y)
cursor.Y = memory.Y
cursor.X = memory.X
--move.update_virturl_cursor()
memory = current
end
local function return_by_lines()
if not memory then
return
end
local buf = micro.CurPane().Buf
local last_line_index = utils.last_line_index(buf)
if memory.Y > last_line_index then
bell.ring("line " .. memory.Y + 1 .. " (> " .. last_line_index + 1 .. ") not exists")
return
end
local line = buf:Line(memory.Y)
local spaces = line:match("^(%s*)")
local x = utf8.RuneCount(spaces)
local cursor = buf:GetActiveCursor()
local current = buffer.Loc(cursor.X, cursor.Y)
cursor.Y = memory.Y
cursor.X = x
--move.update_virturl_cursor()
memory = current
end
-------------
-- Exports --
-------------
local M = {}
M.pre_memorize = pre_memorize
M.memorize = memorize
M.return_by_chars = return_by_chars
M.return_by_lines = return_by_lines
return M