-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscratch.lua
More file actions
60 lines (54 loc) · 1.71 KB
/
Copy pathscratch.lua
File metadata and controls
60 lines (54 loc) · 1.71 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
local M = {}
State = {
scratch_buf = -1,
scratch_win = -1,
}
---Creates a scratch window and opens it.
---@param conf Config | {} # Plugin configuration.
---@param scratch_handler fun(buf: integer, win: integer):nil # Handler for the scratch window.
local function create_scratch_win(conf, scratch_handler)
conf = conf or {}
local scratch_conf = conf.scratch or {}
local width = scratch_conf.width or math.floor(vim.o.columns * 0.8)
local height = scratch_conf.height or math.floor(vim.o.lines * 0.8)
local col = scratch_conf.col or math.floor((vim.o.columns - width) / 2)
local row = scratch_conf.row or math.floor((vim.o.lines - height) / 3)
local border = scratch_conf.border or "rounded"
local title_pos = scratch_conf.title_pos or "center"
local buf = -1
if vim.api.nvim_buf_is_valid(State.scratch_buf) then
buf = State.scratch_buf
else
buf = vim.api.nvim_create_buf(false, true)
end
local win_cfg = {
relative = "editor",
width = width,
height = height,
col = col,
row = row,
style = "minimal",
border = border,
title = vim.g.tmutils_selected_repl or "sh",
title_pos = title_pos,
}
local win = vim.api.nvim_open_win(buf, true, win_cfg)
scratch_handler(buf, win)
State.scratch_buf = buf
State.scratch_win = win
end
---Toggles a tmutils scratch.
---@param conf Config | {} # Plugin configuration.
M.toggle_scratch = function(_, conf)
if not vim.api.nvim_win_is_valid(State.scratch_win) then
create_scratch_win(conf, function(buf, win)
vim.bo[buf].syntax = vim.g.tmutils_selected_repl or "sh"
vim.bo[buf].filetype = vim.g.tmutils_selected_repl or "sh"
vim.wo[win].number = true
vim.wo[win].relativenumber = true
end)
else
vim.api.nvim_win_hide(State.scratch_win)
end
end
return M