104 lines
2 KiB
Lua
104 lines
2 KiB
Lua
local M = {}
|
|
|
|
M.generateModeline = function()
|
|
local commentString = vim.o.commentstring
|
|
|
|
if commentString == "" then
|
|
commentString = "# %s"
|
|
end
|
|
|
|
local modelineElements = {
|
|
-- ensure whitespace immediately after left side comment string
|
|
(function()
|
|
if string.match(commentString, "%s%%s") == nil then
|
|
return " "
|
|
end
|
|
return ""
|
|
end)(),
|
|
|
|
"vi: set",
|
|
|
|
(function()
|
|
local encoding = vim.o.fileencoding
|
|
if encoding ~= "utf-8" then
|
|
return " fenc=" .. encoding
|
|
end
|
|
return ""
|
|
end)(),
|
|
|
|
" ft=" .. vim.o.filetype,
|
|
" ts=" .. vim.o.tabstop,
|
|
" sw=" .. vim.o.shiftwidth,
|
|
" sts=" .. vim.o.softtabstop,
|
|
|
|
(function()
|
|
if vim.o.shiftround then
|
|
return " sr"
|
|
else
|
|
return " nosr"
|
|
end
|
|
end)(),
|
|
|
|
(function()
|
|
if vim.o.expandtab then
|
|
return " et"
|
|
else
|
|
return " noet"
|
|
end
|
|
end)(),
|
|
|
|
(function()
|
|
if vim.o.smartindent then
|
|
return " si"
|
|
else
|
|
return " nosi"
|
|
end
|
|
end)(),
|
|
|
|
" tw=" .. vim.o.textwidth,
|
|
" fdm=" .. vim.o.foldmethod,
|
|
|
|
(function()
|
|
if vim.opt.foldmarker:get()[1] ~= "{{{" then
|
|
return " fmr=" .. vim.opt.foldmarker:get()[1] .. "," .. vim.opt.foldmarker:get()[2]
|
|
end
|
|
return ""
|
|
end)(),
|
|
|
|
":",
|
|
|
|
-- ensure whitespace immediately before right side comment string if it exists
|
|
(function()
|
|
if string.match(commentString, "%%s$") == nil then
|
|
return " "
|
|
end
|
|
return ""
|
|
end)(),
|
|
}
|
|
|
|
local modeline = table.concat(modelineElements)
|
|
return commentString:gsub("%%s", modeline)
|
|
end
|
|
|
|
M.insertModeline = function()
|
|
if not vim.bo.modifiable then
|
|
vim.notify("Buffer is not modifiable!")
|
|
return
|
|
end
|
|
|
|
local modeline = M.generateModeline()
|
|
|
|
if modeline == "" then
|
|
vim.notify("Modeline empty!")
|
|
return
|
|
end
|
|
local currentLine = vim.api.nvim_buf_get_lines(vim.api.nvim_win_get_buf(0), 0, 1, true)[1]
|
|
|
|
if string.match(currentLine, "vi:") then
|
|
vim.api.nvim_buf_set_lines(0, 0, 1, true, { modeline })
|
|
else
|
|
vim.api.nvim_buf_set_lines(0, 0, 0, true, { modeline })
|
|
end
|
|
end
|
|
|
|
return M
|