-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmicrowiki.lua
288 lines (236 loc) · 7.14 KB
/
microwiki.lua
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
VERSION = "1.0.0"
local micro = import("micro")
local config = import("micro/config")
local filepath = import("path/filepath")
local os = import("os")
local ioutil = import("io/ioutil")
local utf8 = import("utf8")
-- local shell = import("micro/shell")
-- local buffer = import("micro/buffer")
-- local action = import("micro/action")
local function fileName(absolutePath)
local dir, file = filepath.Split(absolutePath)
return file
end
local function getWikiLink(bufPane)
local lineNo = bufPane.Cursor.Y
local colNo = bufPane.Cursor.X
local line = bufPane.Buf:Line(lineNo)
local startCol = nil
for i = colNo, 1, -1 do
local chunk = line:sub(i - 1, i)
if chunk == "[[" then
startCol = i + 1
break
end
end
if startCol == nil then return nil end
local endCol = nil
for i = colNo, line:len(), 1 do
local chunk = line:sub(i + 1, i + 2)
if chunk == "]]" then
endCol = i
break
end
end
if endCol == nil then return nil end
return line:sub(startCol, endCol)
end
local function getWikiFile(bufPane)
local link = getWikiLink(bufPane)
-- do not go to dir/
if link == nil or link:sub(-1) == "/" then return nil end
return link .. ".mwiki"
end
local function getLocalDir(absolutePath)
local dir, file = filepath.Split(absolutePath)
local cwd, err = os.Getwd()
if err ~= nil then
micro.InfoBar():Error(err)
return nil
end
return dir:sub(cwd:len() + 2);
end
function go(bufPane)
local file = getWikiFile(bufPane)
local dir = getLocalDir(bufPane.Buf.AbsPath)
if file == nil or dir == nil then return true end
bufPane:Save()
local filePath = filepath.Join(dir, file)
bufPane:HandleCommand("tab \"" .. filePath .. "\"")
micro.InfoBar():Message("Wiki: Open \"" .. filePath .. "\"")
return false
end
function back(bufPane)
local dir = getLocalDir(bufPane.Buf.AbsPath)
if dir == nil then return true end
bufPane:Save()
bufPane:Quit()
local path = filepath.Join(dir, fileName(bufPane.Buf.AbsPath))
micro.InfoBar():Message("Wiki: Saved \"" .. path .. "\"")
return false
end
function autocomplete(bufPane)
local link = getWikiLink(bufPane)
if link == nil then return false end
-- move cursor to end of link
local colNo = bufPane.Cursor.X
local line = bufPane.Buf:Line(bufPane.Cursor.Y)
local start = line:find("]]", colNo, true)
bufPane.Cursor.X = start - 1
local base = filepath.Split(bufPane.Buf.AbsPath)
local dir
if link == "" then
dir = base
else
dir = filepath.Join(base, link)
end
local localDir = getLocalDir(bufPane.Buf.AbsPath)
if localDir == nil then localDir = dir end
micro.InfoBar():Message("Wiki: List \"" .. localDir .. "\"")
local files, err = ioutil.ReadDir(dir)
if err ~= nil then
micro.InfoBar():Error("Wiki: \"" .. link .. "\" is not a directory")
return true
end
-- if last char is not "/", insert it
if link ~= "" and link:sub(-1) ~= "/" then
local rune, runeSize = utf8.DecodeRuneInString("/")
bufPane:DoRuneInsert(rune)
end
local names = {}
for i = 1, #files do
local file = files[i]
local name = file:Name()
if file:IsDir() then
names[i] = name .. "/"
else
names[i] = name:sub(1, -4)
end
end
bufPane.Buf:Autocomplete(function(buf) return names, names end)
return true
end
local function copyLoc(loc)
local table = {}
table["X"] = loc.X
table["Y"] = loc.Y
return table
end
local function gotoNextLink(bufPane, down)
local match, found, err = bufPane.Buf:FindNext("\\[\\[(.*?)\\]\\]",
bufPane.Buf:Start(),
bufPane.Buf:End(),
-- needs copyLoc or throws
-- bad argument #5 to FindNext (cannot use &{16 3} (type *buffer.Loc) as type buffer.Loc)
copyLoc(bufPane.Cursor.Loc),
down, true)
if err ~= nil then
micro.InfoBar():Error(err)
return false
end
if found == false then return false end
local loc = copyLoc(match[2])
-- move cursor to the end of link accounting for "]]"
loc["X"] = loc.X - 2
bufPane.Cursor:GotoLoc(loc)
return true
end
-- go to next wiki link
function preInsertTab(bufPane)
local link = getWikiLink(bufPane)
if link == nil then return true end
if gotoNextLink(bufPane, true) then
micro.InfoBar():Message("Wiki: Go to next link")
end
return false
end
-- go to previous wiki link
function preOutdentLine(bufPane)
local link = getWikiLink(bufPane)
if link == nil then return true end
if gotoNextLink(bufPane, false) then
micro.InfoBar():Message("Wiki: Go to previus link")
end
return false
end
function onBufPaneOpen(bufPane)
-- necessary for micro to apply wiki link syntax
bufPane.Buf:UpdateRules()
end
-- local function fileMatches(absolutePath)
-- local dir, file = filepath.Split(absolutePath)
-- return file == "todo"
-- end
local function loc(x, y)
local table = {}
table["X"] = x
table["Y"] = y
return table
end
local function indexOf(arr, char)
for i = 1, #arr do if arr[i] == char then return i end end
return nil
end
local checkmarks = {"-", "+", "x"}
local function getNextCheckmark(index)
if index == 1 then
return checkmarks[2]
elseif index == 2 then
return checkmarks[3]
else
return checkmarks[1]
end
end
local function insertString(bufPane, str)
local rune, runeSize = utf8.DecodeRuneInString(str)
bufPane:DoRuneInsert(rune)
end
local function isTodo(first)
for i = 1, #checkmarks do
local checkmark = checkmarks[i]
if first == checkmark then return true end
end
return false
end
function checkmark(bufPane)
-- if fileMatches(bufPane.Buf.AbsPath) == false then return false end
local lineNo = bufPane.Cursor.Y
local line = bufPane.Buf:Line(lineNo)
local first = line:sub(1, 1)
-- if isTodo(first) == false then return false end
local index = indexOf(checkmarks, first)
local nextCheckmark = getNextCheckmark(index)
local colNo = bufPane.Cursor.X
-- move to beginning
bufPane.Cursor:Start()
-- delete one character
bufPane:Delete()
insertString(bufPane, nextCheckmark)
-- return cursor to prev position
bufPane.Cursor:GotoLoc(loc(colNo, lineNo))
return true
end
function onBufPaneOpen(bufPane)
-- necessary for micro to apply syntax
bufPane.Buf:UpdateRules()
end
function init()
-- used when creating newdir/newfile
config.SetGlobalOption("mkparents", "true")
-- remember cursor position, useful when navigating between files
config.SetGlobalOption("savecursor", "true")
-- extend markdown to include [[wikilink]] syntax
config.AddRuntimeFile("microwiki", config.RTSyntax, "microwiki.yaml")
config.AddRuntimeFile("microwiki", config.RTHelp, "help/microwiki.md")
-- necessary for micro to load syntax file
config.Reload()
-- open [[link]].mwiki in new tab
config.TryBindKey("Alt-Enter", "lua:microwiki.go", false)
-- save and close tab
config.TryBindKey("Alt-Backspace", "lua:microwiki.back", false)
-- activate autocomplete
config.TryBindKey("Ctrl-Space", "lua:microwiki.autocomplete", false)
-- open cycle through checkmarks in new tab
config.TryBindKey("Alt-x", "lua:microwiki.checkmark", false)
end