configs/nvim/lua/config/autocmds.lua
Kacper Marzecki 2f0a29ce9f link tmux
2025-03-16 14:15:37 +01:00

195 lines
6.1 KiB
Lua

-- Autocmds are automatically loaded on the VeryLazy event
-- Default autocmds that are always set: https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/config/autocmds.lua
-- Add any additional autocmds here
--
-- vim.api.nvim_create_autocmd("copyref", {
-- callback = function()
-- print("asd")
-- end,
-- })
--
local function find_available_port()
local tcp = vim.uv.new_tcp()
if tcp:bind("127.0.0.1", 0) then
local port = tcp:getsockname().port
tcp:close()
return port
end
end
local function start_refactorex(port, bufnr)
-- vim.lsp.start({
-- name = "refactorex",
-- cmd = vim.lsp.rpc.connect("127.0.0.1", port),
--
-- local port = (spawned and vim.fn.system("pgrep -fla refactorex"):match("beam%.smp.*--port (%d+)"))
-- or find_available_port()
--
-- if not spawned then
-- vim.uv.spawn("/Users/kacper.marzecki@schibsted.com/installs/refactorex/bin/start", { args = { "--port", port } })
-- end
--
-- vim.defer_fn(function()
-- start_refactorex(port, args.buf)
-- end, 3000)
-- end
-- })
end
-- Function to insert TODO comment with task number
function insert_todo_comment()
-- Get the current branch name
local handle = io.popen("git branch --show-current 2>/dev/null")
local branch
if handle then
branch = handle:read("*a"):match("%S+")
handle:close()
end
-- Extract the task number (e.g., HS-4798) from the branch name
local task_number = branch and branch:match("HS%-%d+")
-- If a task number is found, insert the TODO comment
if task_number then
local todo_comment = "# TODO: " .. task_number
vim.api.nvim_put({ todo_comment }, "l", true, true)
else
print("Task number not found in branch name!")
end
end
-- Keybind for inserting TODO comment
vim.api.nvim_set_keymap("n", "<leader>t", "<cmd>lua insert_todo_comment()<CR>", { noremap = true, silent = true })
-- Function to open the notes file in a floating window
function open_notes()
local notes_file = "/Users/kacper.marzecki@schibsted.com/git/notes/todo.md" -- Path to your notes file
-- -- Read the file content
-- local file_content = {}
-- for line in io.lines(vim.fn.expand(notes_file)) do
-- table.insert(file_content, line)
-- end
--
-- -- Create a floating window
-- local buf = vim.api.nvim_create_buf(false, true) -- Create a buffer
-- vim.api.nvim_buf_set_lines(buf, 0, -1, false, file_content) -- Set buffer content
--
-- Check if the file exists, create it if not
if vim.fn.filereadable(notes_file) == 0 then
vim.fn.writefile({}, notes_file) -- Create an empty file
end
-- Create a buffer linked to the notes file
local buf = vim.fn.bufadd(notes_file)
vim.fn.bufload(buf)
-- Configure floating window size and position
local width = math.floor(vim.o.columns * 0.8) -- 80% of the screen width
local height = math.floor(vim.o.lines * 0.8) -- 80% of the screen height
local row = math.floor((vim.o.lines - height) / 2) -- Center the window vertically
local col = math.floor((vim.o.columns - width) / 2) -- Center the window horizontally
-- Create the window
local win = vim.api.nvim_open_win(buf, true, {
relative = "editor",
width = width,
height = height,
row = row,
col = col,
style = "minimal",
border = "rounded",
})
-- Enable saving and other standard buffer options
vim.api.nvim_buf_set_option(buf, "modifiable", true)
vim.api.nvim_buf_set_option(buf, "buftype", "")
vim.api.nvim_buf_set_option(buf, "filetype", "markdown")
-- vim.api.nvim_buf_set_option(buf, "fil", "text")
-- Scroll to the end of the file
local last_line = vim.api.nvim_buf_line_count(buf)
vim.api.nvim_win_set_cursor(win, { last_line, 0 })
-- Close the popup with 'q'
vim.api.nvim_buf_set_keymap(
buf,
"n",
"q",
"<cmd>lua vim.api.nvim_win_close(" .. win .. ", true)<CR>",
{ noremap = true, silent = true }
)
end
-- Keybind for opening the notes file in a floating window
vim.api.nvim_set_keymap("n", "<leader>n", "<cmd>lua open_notes()<CR>", { noremap = true, silent = true })
-- Define the function to start the timer
function start_timer(duration)
local timer = vim.loop.new_timer()
timer:start(
duration * 60000,
0,
vim.schedule_wrap(function()
vim.api.nvim_out_write("Time's up!\n")
os.execute('osascript -e "display notification \\"Times up\\" with title \\"Timer"" ')
end)
)
end
-- -- Define the function to prompt the user for the desired time
-- function choose_timer()
-- local times = { 1, 3, 5, 10 }
-- local choices = table.concat(times, ", ")
-- local choice = vim.fn.input("Choose timer duration (minutes): [" .. choices .. "] ")
-- local duration = tonumber(choice)
-- if duration and vim.tbl_contains(times, duration) then
-- start_timer(duration)
-- else
-- vim.api.nvim_out_write("Invalid choice. Please choose from: " .. choices .. "\n")
-- end
-- end
--
--
-- Define the function to prompt the user for the desired time using fzf-lua
function choose_timer()
local times = { 1, 3, 5, 10 }
require("fzf-lua").fzf_exec(times, {
prompt = "Choose timer duration (minutes): ",
actions = {
["default"] = function(selected)
local duration = tonumber(selected[1])
if duration then
start_timer(duration)
else
vim.api.nvim_out_write("Invalid choice. Please choose a valid duration.\n")
end
end,
},
})
end
-- Set the keybind to choose the timer duration
vim.api.nvim_set_keymap("n", "<leader>T", "<cmd>lua choose_timer()<CR>", { noremap = true, silent = true })
-- TODO APP MAPPINGS
vim.api.nvim_create_autocmd("BufReadPost", {
pattern = "/tmp/task_edit.txt",
callback = function()
if vim.fn.line("$") == 1 and vim.fn.getline(1) == "" then
vim.cmd("startinsert")
end
end,
})
vim.api.nvim_create_autocmd("FileType", {
pattern = "*",
callback = function()
if vim.fn.expand("%") == "/tmp/task_edit.txt" then
vim.api.nvim_buf_set_keymap(0, "n", "<C-S>", ":wq<CR>", { noremap = true, silent = true })
vim.api.nvim_buf_set_keymap(0, "i", "<C-S>", "<Esc>:wq<CR>", { noremap = true, silent = true })
end
end,
})
-- END TODO APP MAPPINGS