Skip to content

Commit 553d49b

Browse files
committed
feat: autorename chats using topic (issue: #133)
1 parent d07cd82 commit 553d49b

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

after/ftplugin/gpchat.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,27 @@ vim.api.nvim_create_autocmd({ "BufEnter", "TextChanged", "InsertLeave" }, {
8888
if M.helpers.deleted_invalid_autocmd(buf, event) then
8989
return
9090
end
91+
92+
local filename = vim.api.nvim_buf_get_name(buf)
93+
local dir = vim.fn.fnamemodify(filename, ":h")
94+
95+
local name = vim.fn.fnamemodify(filename, ":t")
96+
local _, _, prefix = name:find("^(.*)_[^_]*$")
97+
name = prefix and name:sub(#prefix + 2) or name
98+
99+
local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false)
100+
local headers, _, _ = M.helpers.parse_headers(lines)
101+
local topic = headers["topic"] or ""
102+
topic = topic:gsub("[^%w%s]", ""):lower()
103+
topic = topic:gsub("%s+", "_"):gsub("^_+", ""):gsub("_+$", "")
104+
105+
if topic and topic ~= "" and topic ~= prefix then
106+
local new_filename = dir .. "/" .. topic .. "_" .. name
107+
M.logger.debug("gpchat: renaming buffer " .. buf .. " from " .. filename .. " to " .. new_filename)
108+
vim.api.nvim_buf_set_name(buf, new_filename)
109+
M.helpers.delete_file(filename)
110+
end
111+
91112
-- M.logger.debug("gpchat: saving buffer " .. buf .. " " .. vim.json.encode(event))
92113
vim.api.nvim_command("silent! write")
93114
end,

0 commit comments

Comments
 (0)