-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.lua
67 lines (53 loc) · 2.24 KB
/
main.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
-- micro-cheat copyright 2022 Tero Karvinen https://TeroKarvinen.com
-- Cheatsheets are copyrighted by their original authors
-- Cheatsheets from Devhints.io are copyright 2021
-- Rico Sta. Cruz and contributors, received under the MIT license
local micro = import("micro")
local config = import("micro/config")
local os = import("os")
local cheatdir = config.ConfigDir.."/plug/micro-cheat/cheatsheets/"
function getSuffix(filename)
-- return last suffix without dot. Example: "tero.foo.bar.sass" -> "sass"
return filename:match("%.(%w+)$")
end
function fileExists(filename)
-- return true if "filename" exists
local _, err = os.Stat(filename)
return not err
end
function init()
-- runs once when micro starts
config.MakeCommand("cheat", cheatCommand, config.NoComplete)
config.TryBindKey("F1", "command:cheat", false)
end
function cheatCommand(bp)
local filename = bp.Buf:GetName()
local filetype = bp.Buf:FileType()
local filesuffix = getSuffix(filename)
if "Vagrantfile" == filename or "Dockerfile" == filename then
filetype = filename:lower()
micro.InfoBar():Message("Cheatsheet by file name: "..filename)
-- Disabled org-mode support, because literal dot (backslash dot) escape causes error
-- in the micro shipped by latest Kali
-- elseif string.find(filename, "\.org$") then -- not detected by micro
-- filetype = "org"
-- micro.InfoBar():Message("Cheatsheet by file suffix: "..filename)
elseif "unknown" ~= filetype then
-- micro hopefully detected the filetype, the typical case
micro.InfoBar():Message("Cheatsheet by file type: "..filetype)
elseif nil ~= filesuffix then
filetype = filesuffix
micro.InfoBar():Message("Cheatsheet by file suffix: "..filetype)
--else
-- micro.InfoBar():Message("Cheatsheet not found for type '"..filetype.."', filename '"..filename.."'")
-- return
end
local cheatsheet = cheatdir .. filetype .. ".md"
if not fileExists(cheatsheet) then
micro.InfoBar():Message("Cheatsheet for \""..filetype.."\" does not exist. Contribute to https://github.com/terokarvinen/micro-cheat")
end
-- local cmd = "tab " .. cheatdir .. filetype .. ".md"
local cmd = "tab " .. cheatsheet
bp:HandleCommand(cmd)
-- bp:HandleCommand("setlocal readonly true") -- user changes would be overwritten my micro-cheat updates
end