From e0a82ef957e9e8c08529f70f13c9efd7a74c019d Mon Sep 17 00:00:00 2001 From: Jonas Hahn Date: Mon, 3 Nov 2025 14:25:21 +0100 Subject: [PATCH] Add deletion for drawings --- lua/typstar/drawings.lua | 54 +++++++++++++++++++++++++++++++++++ lua/typstar/init.lua | 2 ++ lua/typstar/snippets/math.lua | 7 +++-- 3 files changed, 61 insertions(+), 2 deletions(-) diff --git a/lua/typstar/drawings.lua b/lua/typstar/drawings.lua index 8825b86..e181a0d 100644 --- a/lua/typstar/drawings.lua +++ b/lua/typstar/drawings.lua @@ -104,10 +104,64 @@ local open_drawing = function(prov) end end +local function delete_drawing(provider) + local cfg = provider[1] + local line_num = vim.api.nvim_win_get_cursor(0)[1] - 1 + local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false) + + -- Find the start and end of the #figure() block containing the filename + local start_line, end_line + for i = line_num, 0, -1 do + if lines[i + 1]:match("^#figure%(") then + start_line = i + break + end + end + for i = line_num, #lines - 1 do + if lines[i + 1]:match("%)$") then + end_line = i + break + end + end + + if not start_line or not end_line then + print("No #figure() block found on current line") + return + end + + -- Extract filename from the block + local block_text = table.concat(vim.api.nvim_buf_get_lines(0, start_line, end_line + 1, false), "\n") + local filename = block_text:match('image%("%s*(.-)%s*"%)') + if not filename then + print("No image filename found in #figure() block") + return + end + + local path = vim.fn.expand('%:p:h') .. '/' .. filename + local path_export = vim.fn.expand('%:p:h') .. '/' .. filename:match("^(.*)%.") + + -- Delete files if they exist + if vim.fn.filereadable(path) == 1 then os.remove(path) end + if vim.fn.filereadable(path_export) == 1 then os.remove(path_export) end + + -- Delete the lines of the #figure() block + vim.api.nvim_buf_set_lines(0, start_line, end_line + 1, false, {}) + + print("Deleted drawing: " .. filename) +end + +function M.delete_obsidian_excalidraw() delete_drawing(excalidraw) end + +function M.delete_rnote() delete_drawing(rnote) end + function M.insert_obsidian_excalidraw() insert_drawing(excalidraw) end + function M.insert_rnote() insert_drawing(rnote) end + function M.open_obsidian_excalidraw() open_drawing({ excalidraw }) end + function M.open_rnote() open_drawing({ rnote }) end + function M.open_drawing() open_drawing(providers) end return M diff --git a/lua/typstar/init.lua b/lua/typstar/init.lua index 058dfdc..5772c6b 100644 --- a/lua/typstar/init.lua +++ b/lua/typstar/init.lua @@ -14,7 +14,9 @@ M.setup = function(args) vim.api.nvim_create_user_command('TypstarSmartJumpBack', function() M.smart_jump(-1) end, {}) vim.api.nvim_create_user_command('TypstarInsertExcalidraw', drawings.insert_obsidian_excalidraw, {}) + vim.api.nvim_create_user_command('TypstarDeleteExcalidraw', drawings.delete_obsidian_excalidraw, {}) vim.api.nvim_create_user_command('TypstarInsertRnote', drawings.insert_rnote, {}) + vim.api.nvim_create_user_command('TypstarDeleteRnote', drawings.delete_rnote, {}) vim.api.nvim_create_user_command('TypstarOpenExcalidraw', drawings.open_obsidian_excalidraw, {}) vim.api.nvim_create_user_command('TypstarOpenRnote', drawings.open_rnote, {}) vim.api.nvim_create_user_command('TypstarOpenDrawing', drawings.open_drawing, {}) diff --git a/lua/typstar/snippets/math.lua b/lua/typstar/snippets/math.lua index a147365..f94252e 100644 --- a/lua/typstar/snippets/math.lua +++ b/lua/typstar/snippets/math.lua @@ -38,10 +38,13 @@ return { snip('pl', '+ ', {}, math), snip('nl', '\\ \n<>', {i(1)}, math), snip('pm', '+- ', {}, math), - snip('nx', ', space ', {}, math), - snip('nbx', ', quad ', {}, math), + snip('nx', '\\, space ', {}, math), + snip('nbx', '\\, quad ', {}, math), + snip('deg', 'degree ', {}, math), snip('ta', 'star ', {}, math), snip('del', 'Delta ', {}, math), + snip('apr', 'approx ', {}, math), + snip('pal', 'parallel ', {}, math), -- operators snip('ak([^k ])', '+ <>', { cap(1) }, math, 100, { wordTrig = false }),