vim.opt_local.textwidth = 80 vim.opt_local.wrap = true vim.opt_local.linebreak = true -- Update PATH to include TeX binaries vim.env.PATH = vim.env.PATH .. ':/Library/TeX/texbin' local function ensure_directory_exists(path) if not vim.fn.isdirectory(path) then vim.fn.mkdir(path, 'p') end end 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_directory_exists(target_dir) return target_dir, output_file, output_path end 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 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 local function close_zathura() local _, _, output_path = generate_pdf_paths(vim.fn.expand('%:p')) vim.fn.system({ 'pkill', '-f', 'zathura ' .. output_path }) end vim.keymap.set('n', 'Rv', function() local filename = vim.fn.expand('%:p') local _, _, output_path = generate_pdf_paths(filename) local result = build_pdf(filename, output_path) if #result == 0 then print('Error: Could not generate PDF.') return end open_pdf_in_zathura(output_path) end, { desc = 'View output PDF in Zathura', noremap = true, silent = true, }) vim.keymap.set('n', 'Rc', close_zathura, { desc = 'Close Zathura instance for the current PDF', noremap = true, silent = true, }) vim.keymap.set('n', 'Rb', function() local filename = vim.fn.expand('%:p') local _, _, output_path = generate_pdf_paths(filename) local result = build_pdf(filename, output_path) if #result == 0 then print('Error: Could not generate PDF.') return end print('PDF generated at: ' .. output_path) end, { desc = 'Build PDF', noremap = true, silent = true, }) 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 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, })