From 714fb7ef372bc764cd8d78a15bf4efae448b5fda Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Fri, 24 Oct 2025 06:18:55 +0200 Subject: [PATCH] feat(nvim): create custom harpoon commands --- nvim/.config/nvim/lua/plugins/harpoon.lua | 153 ++++++++++++++++++ nvim/.config/nvim/snippets/package.json | 15 ++ .../.config/nvim/snippets/snippets/latex.json | 29 ++++ nvim/.config/nvim/snippets/tex.lua | 21 +++ 4 files changed, 218 insertions(+) create mode 100644 nvim/.config/nvim/lua/plugins/harpoon.lua create mode 100644 nvim/.config/nvim/snippets/package.json create mode 100644 nvim/.config/nvim/snippets/snippets/latex.json create mode 100644 nvim/.config/nvim/snippets/tex.lua diff --git a/nvim/.config/nvim/lua/plugins/harpoon.lua b/nvim/.config/nvim/lua/plugins/harpoon.lua new file mode 100644 index 0000000..0654fbd --- /dev/null +++ b/nvim/.config/nvim/lua/plugins/harpoon.lua @@ -0,0 +1,153 @@ +-- plugins/h-- plugins/harpoon.lua +return { + "ThePrimeagen/harpoon", + branch = "harpoon2", + dependencies = { "nvim-lua/plenary.nvim" }, + config = function() + local harpoon = require("harpoon") + + -- Setup Harpoon pour sessions temporaires (pas de persistence) + harpoon:setup({ + settings = { + save_on_toggle = false, -- Pas de sauvegarde automatique + sync_on_ui_close = false, -- Pas de sync à la fermeture + save_on_change = true, -- Sauvegarde les changements dans la session + excluded_filetypes = { "harpoon", "alpha", "dashboard", "gitcommit", "fugitive" }, + }, + }) + + -- === Fonction helper pour arrêter VimTeX === + local function stop_all_vimtex() + -- Récupérer tous les buffers LaTeX ouverts + local latex_buffers = {} + for _, buf in ipairs(vim.api.nvim_list_bufs()) do + if vim.api.nvim_buf_is_loaded(buf) then + local filetype = vim.api.nvim_buf_get_option(buf, "filetype") + if filetype == "tex" or filetype == "latex" then + table.insert(latex_buffers, buf) + end + end + end + + -- Arrêter VimTeX pour chaque buffer LaTeX + for _, buf in ipairs(latex_buffers) do + -- Switcher temporairement vers ce buffer pour exécuter VimtexStop + local current_buf = vim.api.nvim_get_current_buf() + vim.api.nvim_set_current_buf(buf) + + if vim.fn.exists(":VimtexStop") == 2 then + pcall(vim.cmd, "VimtexStop") -- pcall pour éviter les erreurs + end + + -- Revenir au buffer original + if vim.api.nvim_buf_is_valid(current_buf) then + vim.api.nvim_set_current_buf(current_buf) + end + end + + -- Alternative plus robuste: tuer directement les processus latexmk + local handle = io.popen("pgrep -f latexmk") + if handle then + local result = handle:read("*a") + handle:close() + if result and result ~= "" then + os.execute("pkill -f latexmk") + print("Killed background latexmk processes") + end + end + end + + -- Keymaps AZERTY-friendly groupés sous h + local keymap = vim.keymap.set + + -- === Gestion de session === + keymap("n", "ha", function() + harpoon:list():append() + print("Added: " .. vim.fn.expand("%:t")) + end, { desc = "Harpoon: Add file to session" }) + + keymap("n", "hm", function() + harpoon.ui:toggle_quick_menu(harpoon:list()) + end, { desc = "Harpoon: Toggle menu" }) + + keymap("n", "hc", function() + -- Confirmation simple + local choice = vim.fn.confirm("Clear session and stop VimTeX?", "&Yes\n&No", 2) + + -- Si No, on fait rien + if choice ~= 1 then + return + end + + -- Arrêter toutes les compilations VimTeX + stop_all_vimtex() + + -- Clear Harpoon + harpoon:list():clear() + + -- Fermer tous les buffers + vim.cmd("silent! %bdelete") + + print("Session cleared, VimTeX stopped, buffers closed!") + end, { desc = "Harpoon: Clear session & stop VimTeX" }) + + -- === Navigation directe (rangée de base AZERTY) === + local nav_keys = { + { key = "q", pos = 1 }, + { key = "s", pos = 2 }, + { key = "d", pos = 3 }, + { key = "f", pos = 4 }, + { key = "g", pos = 5 }, + { key = "h", pos = 6 }, + } + + for _, mapping in ipairs(nav_keys) do + keymap("n", "h" .. mapping.key, function() + harpoon:list():select(mapping.pos) + end, { desc = "Harpoon: Go to file " .. mapping.pos }) + end + + -- === Navigation cyclique === + keymap("n", "hn", function() + harpoon:list():next() + end, { desc = "Harpoon: Next file" }) + + keymap("n", "hp", function() + harpoon:list():prev() + end, { desc = "Harpoon: Previous file" }) + + -- === Commandes utiles === + vim.api.nvim_create_user_command("HarpoonClear", function() + harpoon:list():clear() + print("Harpoon session cleared!") + end, { desc = "Clear current Harpoon session" }) + + vim.api.nvim_create_user_command("HarpoonList", function() + local list = harpoon:list() + if #list.items == 0 then + print("No files in Harpoon session") + return + end + + print("Current Harpoon session:") + for i, item in ipairs(list.items) do + print(string.format(" %d. %s", i, item.value)) + end + end, { desc = "List files in current Harpoon session" }) + end, + + -- Chargement paresseux: seulement quand on utilise les keymaps + keys = { + { "ha", desc = "Harpoon: Add file" }, + { "hm", desc = "Harpoon: Menu" }, + { "hc", desc = "Harpoon: Clear & stop VimTeX" }, + { "hq", desc = "Harpoon: File 1" }, + { "hs", desc = "Harpoon: File 2" }, + { "hd", desc = "Harpoon: File 3" }, + { "hf", desc = "Harpoon: File 4" }, + { "hg", desc = "Harpoon: File 5" }, + { "hh", desc = "Harpoon: File 6" }, + { "hn", desc = "Harpoon: Next" }, + { "hp", desc = "Harpoon: Previous" }, + }, +} diff --git a/nvim/.config/nvim/snippets/package.json b/nvim/.config/nvim/snippets/package.json new file mode 100644 index 0000000..81d0103 --- /dev/null +++ b/nvim/.config/nvim/snippets/package.json @@ -0,0 +1,15 @@ +{ + "name": "custom-latex-snippets", + "contributes": { + "snippets": [ + { + "language": "tex", + "path": "./snippets/latex.json" + }, + { + "language": "latex", + "path": "./snippets/latex.json" + } + ] + } +} \ No newline at end of file diff --git a/nvim/.config/nvim/snippets/snippets/latex.json b/nvim/.config/nvim/snippets/snippets/latex.json new file mode 100644 index 0000000..e6c590d --- /dev/null +++ b/nvim/.config/nvim/snippets/snippets/latex.json @@ -0,0 +1,29 @@ +{ + "Begin/End environment": { + "prefix": "beg", + "body": [ + "\\begin{${1:environment}}", + "\t$0", + "\\end{$1}" + ], + "description": "Begin/End environment" + }, + "Multicols": { + "prefix": "multicols", + "body": [ + "\\begin{multicols}{${1:2}}", + "\t$0", + "\\end{multicols}" + ], + "description": "Multicols environment" + }, + "Minipage": { + "prefix": "minipage", + "body": [ + "\\begin{minipage}{${1:0.5}\\textwidth}", + "\t$0", + "\\end{minipage}" + ], + "description": "Minipage environment" + } +} \ No newline at end of file diff --git a/nvim/.config/nvim/snippets/tex.lua b/nvim/.config/nvim/snippets/tex.lua new file mode 100644 index 0000000..0aaf6da --- /dev/null +++ b/nvim/.config/nvim/snippets/tex.lua @@ -0,0 +1,21 @@ +local ls = require("luasnip") +local s = ls.snippet +local t = ls.text_node +local i = ls.insert_node +local f = ls.function_node + +return { + -- Auto-expand: tape "beg" + TAB + s({ trig = "beg", dscr = "Begin/End environment" }, { + t("\\begin{"), + i(1, "environment"), + t("}"), + t({ "", "\t" }), + i(0), + t({ "", "\\end{" }), + f(function(args) + return args[1][1] + end, { 1 }), + t("}"), + }), +}