Skip to content

Commit

Permalink
feat: autorename chats using topic (issue: #133)
Browse files Browse the repository at this point in the history
  • Loading branch information
Robitx committed Aug 17, 2024
1 parent cd1b5f1 commit db9f934
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions after/ftplugin/gpchat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,27 @@ vim.api.nvim_create_autocmd({ "BufEnter", "TextChanged", "InsertLeave" }, {
if M.helpers.deleted_invalid_autocmd(buf, event) then
return
end

local filename = vim.api.nvim_buf_get_name(buf)
local dir = vim.fn.fnamemodify(filename, ":h")

local name = vim.fn.fnamemodify(filename, ":t")
local _, _, prefix = name:find("^(.*)_[^_]*$")
name = prefix and name:sub(#prefix + 2) or name

local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false)
local headers, _, _ = M.helpers.parse_headers(lines)
local topic = headers["topic"] or ""
topic = topic:gsub("[^%w%s]", ""):lower()
topic = topic:gsub("%s+", "_"):gsub("^_+", ""):gsub("_+$", "")

if topic and topic ~= "" and topic ~= prefix then
local new_filename = dir .. "/" .. topic .. "_" .. name
M.logger.debug("gpchat: renaming buffer " .. buf .. " from " .. filename .. " to " .. new_filename)
vim.api.nvim_buf_set_name(buf, new_filename)
M.helpers.delete_file(filename)
end

-- M.logger.debug("gpchat: saving buffer " .. buf .. " " .. vim.json.encode(event))
vim.api.nvim_command("silent! write")
end,
Expand Down

0 comments on commit db9f934

Please sign in to comment.