Add some markdown tweaks
This commit is contained in:
parent
453041b2eb
commit
db60094ef8
1 changed files with 49 additions and 3 deletions
|
|
@ -1,5 +1,50 @@
|
||||||
local keymap = vim.keymap
|
local keymap = vim.keymap
|
||||||
|
|
||||||
|
-- Function to go by cursor path
|
||||||
|
local function open_file_under_cursor()
|
||||||
|
-- Get the current line
|
||||||
|
local line = vim.api.nvim_get_current_line()
|
||||||
|
local col = vim.api.nvim_win_get_cursor(0)[2]
|
||||||
|
|
||||||
|
-- Pattern to match file paths (adjust as needed)
|
||||||
|
-- This pattern matches: /path/to/file.ext or /path/to/file.ext:line:col
|
||||||
|
local pattern = "([~/][%w%p]*%.%w+):?(%d*):?(%d*)"
|
||||||
|
print(col)
|
||||||
|
|
||||||
|
-- Find all matches in the line
|
||||||
|
for path, line_num, col_num in line:gmatch(pattern) do
|
||||||
|
-- Get start and end position of the match
|
||||||
|
local start_pos, end_pos = line:find(vim.pesc(path), 1, true)
|
||||||
|
|
||||||
|
-- Check if cursor is within this match
|
||||||
|
if start_pos and end_pos and col >= start_pos - 1 and col < end_pos then
|
||||||
|
-- Expand home directory if needed
|
||||||
|
local expanded_path = path:gsub("^~", os.getenv("HOME"))
|
||||||
|
|
||||||
|
-- Check if file exists
|
||||||
|
local file = io.open(expanded_path, "r")
|
||||||
|
if file then
|
||||||
|
file:close()
|
||||||
|
|
||||||
|
-- Open the file
|
||||||
|
vim.cmd("edit " .. vim.fn.fnameescape(expanded_path))
|
||||||
|
|
||||||
|
-- Jump to line and column if provided
|
||||||
|
if line_num ~= "" then
|
||||||
|
vim.api.nvim_win_set_cursor(0, { tonumber(line_num), tonumber(col_num) or 0 })
|
||||||
|
end
|
||||||
|
|
||||||
|
return
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- print("No valid file path found under cursor")
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Map to a key, e.g., gf (go to file)
|
||||||
|
vim.keymap.set('n', 'gf', open_file_under_cursor, { desc = 'Open file path under cursor' })
|
||||||
|
|
||||||
-- Picker for markdown headers
|
-- Picker for markdown headers
|
||||||
local pickers = require("telescope.pickers")
|
local pickers = require("telescope.pickers")
|
||||||
local finders = require("telescope.finders")
|
local finders = require("telescope.finders")
|
||||||
|
|
@ -10,14 +55,14 @@ local action_state = require("telescope.actions.state")
|
||||||
local function formatted_date()
|
local function formatted_date()
|
||||||
local current_date = os.date("%Y-%m-%d")
|
local current_date = os.date("%Y-%m-%d")
|
||||||
if type(current_date) == "string" then
|
if type(current_date) == "string" then
|
||||||
vim.api.nvim_put({current_date}, 'c', true, true)
|
vim.api.nvim_put({ current_date }, 'c', true, true)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function formatted_datetime()
|
local function formatted_datetime()
|
||||||
local current_datetime = os.date("%Y-%m-%d %HH:%MM")
|
local current_datetime = os.date("%Y-%m-%d %HH:%MM")
|
||||||
if type(current_datetime) == "string" then
|
if type(current_datetime) == "string" then
|
||||||
vim.api.nvim_put({current_datetime}, 'c', true, true)
|
vim.api.nvim_put({ current_datetime }, 'c', true, true)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -54,7 +99,7 @@ local function markdown_headings()
|
||||||
actions.select_default:replace(function()
|
actions.select_default:replace(function()
|
||||||
actions.close(prompt_bufnr)
|
actions.close(prompt_bufnr)
|
||||||
local selection = action_state.get_selected_entry()
|
local selection = action_state.get_selected_entry()
|
||||||
vim.api.nvim_win_set_cursor(0, {selection.value.lnum, 0})
|
vim.api.nvim_win_set_cursor(0, { selection.value.lnum, 0 })
|
||||||
end)
|
end)
|
||||||
return true
|
return true
|
||||||
end,
|
end,
|
||||||
|
|
@ -64,3 +109,4 @@ end
|
||||||
keymap.set("n", "<leader>fdh", markdown_headings, { desc = "Grep: Markdown headings" })
|
keymap.set("n", "<leader>fdh", markdown_headings, { desc = "Grep: Markdown headings" })
|
||||||
keymap.set("n", "<leader>icd", formatted_date, { desc = "Insert current date" })
|
keymap.set("n", "<leader>icd", formatted_date, { desc = "Insert current date" })
|
||||||
keymap.set("n", "<leader>ict", formatted_datetime, { desc = "Insert current datetime" })
|
keymap.set("n", "<leader>ict", formatted_datetime, { desc = "Insert current datetime" })
|
||||||
|
keymap.set("n", "<leader>ocf", open_file_under_cursor , { desc = "Open file under cursor" })
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue