-
Notifications
You must be signed in to change notification settings - Fork 2
/
nvcheatsheet.lua
75 lines (59 loc) · 2.07 KB
/
nvcheatsheet.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
local M = {}
local filetype = 'nvcheatsheet'
-- Since the buffer deletes itself when hidden
-- we need to set this variable to false too
vim.api.nvim_create_autocmd('BufWinLeave', {
group = vim.api.nvim_create_augroup('nvcheatsheet', { clear = true }),
callback = function()
if vim.bo.ft == filetype then
vim.g.nvcheatsheet_displayed = false
end
end,
})
function M.toggle()
local drawer = require('nvcheatsheet.draw')
-- Close the cheatsheet if its opened
if vim.g.nvcheatsheet_displayed then
local cheatsheet_buffer = vim.api.nvim_get_current_buf()
vim.api.nvim_set_current_buf(vim.g.nvcheatsheet_previous_buffer)
vim.api.nvim_buf_delete(cheatsheet_buffer, { force = true })
vim.g.nvcheatsheet_displayed = false
return
end
-- Save previous buffer ID
vim.g.nvcheatsheet_previous_buffer = vim.api.nvim_get_current_buf()
-- Create an empty buffer
local buffer = vim.api.nvim_create_buf(false, true)
-- Mark as displayed
vim.g.nvcheatsheet_displayed = true
-- Switch to the new buffer
vim.api.nvim_set_current_buf(buffer)
-- Draw the cheatsheet
drawer.draw(buffer, M.opts['header'], M.opts['keymaps'])
-- Configure the cheatsheet buffer
vim.opt_local.bufhidden = 'delete'
vim.opt_local.buflisted = false
vim.opt_local.modifiable = false
vim.opt_local.buftype = 'nofile'
vim.opt_local.number = false
vim.opt_local.list = false
vim.opt_local.wrap = false
vim.opt_local.relativenumber = false
vim.opt_local.cursorline = false
vim.opt_local.colorcolumn = '0'
vim.opt_local.foldcolumn = '0'
vim.opt_local.filetype = filetype
-- Create a shortcut to hide and remove the cheatsheet buffer
vim.keymap.set('n', '<Esc>', function()
vim.api.nvim_set_current_buf(vim.g.nvcheatsheet_previous_buffer)
vim.api.nvim_buf_delete(buffer, { force = true })
vim.g.nvcheatsheet_displayed = false
end, { buffer = buffer })
end
function M.setup(opts)
local options = require('nvcheatsheet.options')
-- Merge user options with defaults
M.opts = options.merge_defaults(opts)
end
return M
-- vim: ts=2 sts=2 sw=2 et