-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmark.lua
More file actions
138 lines (102 loc) · 2.59 KB
/
mark.lua
File metadata and controls
138 lines (102 loc) · 2.59 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
-- Marking Commands
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 mode = require("vi/mode")
local context = require("vi/context")
local move = require("vi/move")
local marks = {}
--
-- Set Mark / Move to Mark
--
-- m<letter> : Mark current cursor position labelled by <letter>.
local function set(letter)
if #letter ~= 1 then
bell.program_error("1 ~= #letter == " .. #letter)
return
end
mode.show()
local cursor = micro.CurPane().Buf:GetActiveCursor()
local loc = buffer.Loc(cursor.X, cursor.Y)
marks[letter] = loc
end
-- `<letter> : Move cursor to marked position labelled by <letter>.
local function move_to(letter)
if #letter ~= 1 then
bell.program_error("1 ~= #letter == " .. #letter)
return
end
mode.show()
local loc = marks[letter]
if not loc then
bell.ring("no mark set for " .. letter)
return
end
context.memorize()
local buf = micro.CurPane().Buf
local cursor = buf:GetActiveCursor()
local last_line_index = utils.last_line_index()
cursor.Y = math.min(loc.Y, last_line_index)
local line = buf:Line(cursor.Y)
local length = utf8.RuneCount(line)
cursor.X = math.min(loc.X, length - 1)
move.update_virtual_cursor()
end
-- '<letter> : Move cursor to marked line labelled by <letter>.
local function move_to_line(letter)
if #letter ~= 1 then
bell.program_error("1 ~= #letter == " .. #letter)
return
end
mode.show()
local loc = marks[letter]
if not loc then
bell.ring("no mark set for " .. letter)
return
end
context.pre_memorize()
local cursor = micro.CurPane().Buf:GetActiveCursor()
local last_line_index = utils.last_line_index()
cursor.Y = math.min(loc.Y, last_line_index)
cursor.X = 0
move.update_virtual_cursor()
context.memorize()
end
--
-- Move by Context
--
-- `` : Move cursor to previous position in context.
local function back()
mode.show()
if not context.return_by_chars() then
return
end
move.update_virtual_cursor()
end
-- '' : Move cursor to previous line in context.
local function back_to_line()
mode.show()
if not context.return_by_lines() then
return
end
move.update_virtual_cursor()
end
-------------
-- Exports --
-------------
local M = {}
-- Set Mark / Move to Mark
M.set = set
M.move_to = move_to
M.move_to_line = move_to_line
-- Move by Context
M.back = back
M.back_to_line = back_to_line
return M