moved ftplugins, and fixed some plugins

This commit is contained in:
Jeremie Fraeys 2024-09-15 11:21:15 -04:00
parent 3a09e91ad6
commit 8c56556c86
23 changed files with 578 additions and 244 deletions

View file

@ -1,7 +1,7 @@
vim.opt.expandtab = true
vim.opt.shiftwidth = 4
vim.opt.tabstop = 4
vim.opt.expandtab = true
vim.opt.shiftwidth = 2
vim.opt.tabstop = 2
vim.opt.softtabstop = 2
vim.opt_local.formatoptions:remove('o')

88
ftplugin/markdown.lua Normal file
View file

@ -0,0 +1,88 @@
-- markdown.lua
-- Update PATH to include TeX binaries
vim.env.PATH = vim.env.PATH .. ':/Library/TeX/texbin'
-- Utility function to create directories if they do not exist
local function ensure_directory_exists(path)
if not vim.fn.isdirectory(path) then
vim.fn.mkdir(path, 'p')
end
end
-- Generate the PDF target directory and file paths
local function generate_pdf_paths(filename)
local base_dir = vim.fn.fnamemodify(filename, ':h')
local target_dir = base_dir:gsub('/src$', '/pdf')
local output_file = vim.fn.fnamemodify(filename, ':t:r') .. '.pdf'
local output_path = target_dir .. '/' .. output_file
-- Ensure the target directory exists
ensure_directory_exists(target_dir)
return target_dir, output_file, output_path
end
-- Build PDF using the buildnote script
local function build_pdf(filename, output_path)
local command = string.format('buildnote %s %s', vim.fn.shellescape(filename), vim.fn.shellescape(output_path))
return vim.fn.systemlist(command)
end
-- Check if Zathura is running and open it if not
local function open_pdf_in_zathura(pdf_path)
local zathura_running = vim.fn.systemlist('pgrep -f "zathura ' .. vim.fn.shellescape(pdf_path) .. '"')
if #zathura_running == 0 then
vim.fn.jobstart({ 'zathura', pdf_path }, { detach = true, stdout = 'null', stderr = 'null' })
print('Opening PDF in Zathura: ' .. pdf_path)
else
print('Zathura is already running for this file.')
end
end
-- Keybinding to build the PDF and view it in Zathura
vim.keymap.set('n', '<leader>lv', function()
local filename = vim.fn.expand('%:p')
local _, _, output_path = generate_pdf_paths(filename)
-- Build the PDF
local result = build_pdf(filename, output_path)
if #result == 0 then
print('Error: Could not generate PDF.')
return
end
-- Open the PDF in Zathura
open_pdf_in_zathura(output_path)
end, {
desc = 'View output PDF in Zathura',
noremap = true,
silent = true,
})
-- Autocmd to close all Zathura instances related to the current file when exiting Neovim
vim.api.nvim_create_autocmd('VimLeavePre', {
callback = function()
local _, _, output_path = generate_pdf_paths(vim.fn.expand('%:p'))
vim.fn.system({ 'pkill', '-f', 'zathura ' .. output_path })
end,
})
-- Auto-run `buildnote` for files matching `*note-*.md` on save
vim.api.nvim_create_autocmd('BufWritePost', {
pattern = '*note-*.md',
callback = function()
local filename = vim.fn.expand('%:p')
local _, _, output_path = generate_pdf_paths(filename)
if vim.fn.filereadable(filename) == 1 then
-- Execute the buildnote script to generate the PDF
local command = string.format('buildnote %s %s', vim.fn.shellescape(filename), vim.fn.shellescape(output_path))
vim.cmd('silent !' .. command)
else
print('Error: File ' .. filename .. ' does not exist.')
end
end,
})

View file

@ -1,21 +1,103 @@
-- LaTeX keybindings and Zathura management
-- Keybinding to compile LaTeX to PDF using xelatex with latexmk and output to the "output" directory
vim.keymap.set('n', '<leader>ll', ':!mkdir -p output && latexmk -pdf -xelatex -output-directory=output -synctex=1 %<CR>',
vim.keymap.set(
'n',
'<leader>ll',
':!mkdir -p output && latexmk -pdf -xelatex -output-directory=output -synctex=1 %<CR>',
{
desc = 'Compile LaTeX to PDF using xelatex with SyncTeX in the output directory',
noremap = true,
silent = true
})
silent = true,
}
)
-- Keybinding to view the compiled PDF in Zathura from the output directory
vim.keymap.set('n', '<leader>lv', function()
local pdf_path = vim.fn.expand('%:p:h') .. '/output/' .. vim.fn.expand('%:t:r') .. '.pdf'
vim.cmd(':silent !nohup zathura ' .. pdf_path .. ' >/dev/null 2>&1 &')
local pdf_path = vim.fn.expand('%:p:h/output/%:t:r.pdf')
-- Check if Zathura is already running for this PDF
local zathura_running = vim.fn.systemlist('pgrep -f "zathura ' .. pdf_path .. '"')
if #zathura_running == 0 then
-- Start Zathura if it's not already running for this PDF
vim.fn.jobstart({ 'zathura', pdf_path }, { detach = true, stdout = 'null', stderr = 'null' })
else
print('Zathura is already running for this file.')
end
end, {
desc = 'View PDF in Zathura from the output directory',
noremap = true,
silent = true
silent = true,
})
-- Custom surroundings for LaTeX
vim.g.surround_110 = "\\begin{\r}\n\\end{\r}" -- map `yssn` for \begin{} \end{}
-- Function to close the Zathura instance for the current PDF
local function close_zathura()
local pdf_path = vim.fn.expand('%:p:h') .. '/output/' .. vim.fn.expand('%:t:r') .. '.pdf'
-- Terminate Zathura process associated with the PDF
vim.fn.system({ 'pkill', '-f', 'zathura ' .. pdf_path })
end
-- Keybinding to close Zathura for the current PDF
vim.keymap.set(
'n',
'<leader>lc', -- Replace with your preferred key combination
close_zathura,
{
desc = 'Close Zathura instance for the current PDF',
noremap = true,
silent = true,
}
)
-- Prevent multiple Zathura instances and add cooldown period
local last_open_time = 0
local cooldown_period = 5 -- Cooldown period in seconds
local max_instances = 5
local zathura_pids = {}
local auto_close_min = 180
-- Function to schedule Zathura closure after a delay in minutes
local function schedule_close_zathura(minutes)
local delay_ms = minutes * 60 * 1000 -- Convert minutes to milliseconds
vim.defer_fn(close_zathura, delay_ms)
end
-- Autocmd to automatically open the PDF in Zathura when a .tex file is opened
vim.api.nvim_create_autocmd('BufEnter', {
pattern = '*.tex',
callback = function()
local current_time = vim.fn.reltimefloat(vim.fn.reltime())
if current_time - last_open_time < cooldown_period then
print('Cooldown active, skipping Zathura launch.')
return
end
last_open_time = current_time
if #zathura_pids >= max_instances then
print('Maximum number of Zathura instances reached.')
return
end
local pdf_path = vim.fn.expand('%:p:h') .. '/output/' .. vim.fn.expand('%:t:r') .. '.pdf'
local zathura_running = vim.fn.systemlist('pgrep -f "zathura ' .. pdf_path .. '"')
if #zathura_running == 0 and vim.fn.filereadable(pdf_path) == 1 then
local job_id = vim.fn.jobstart({ 'zathura', pdf_path }, { detach = true })
table.insert(zathura_pids, job_id)
-- Schedule the closure of Zathura after 180 minutes (3 hours)
schedule_close_zathura(auto_close_min)
else
print('Zathura is already running for this file or PDF not found.')
end
end,
})
-- Autocmd to close all Zathura instances related to the current file when exiting Neovim
vim.api.nvim_create_autocmd('VimLeavePre', {
callback = function()
local pdf_path = vim.fn.expand('%:p:h') .. '/output/' .. vim.fn.expand('%:t:r') .. '.pdf'
vim.fn.system({ 'pkill', '-f', 'zathura ' .. pdf_path })
end,
})