updating all of my config, specifically doing this to fix the nvim configurations
This commit is contained in:
@@ -1,21 +0,0 @@
|
||||
function map(mode, shortcut, command)
|
||||
vim.api.nvim_set_keymap(mode, shortcut, command, {noremap=true})
|
||||
end
|
||||
|
||||
function nn(shortcut, command)
|
||||
map('n', shortcut, command)
|
||||
end
|
||||
|
||||
function ino(shortcut, command)
|
||||
map('i', shortcut, command)
|
||||
end
|
||||
|
||||
function arduino()
|
||||
baudrate = 9600
|
||||
-- compile and upload sketch
|
||||
nn('<c-n>', ":w | !$HOME/.config/nvim/scripts/arduino -u '%'<cr>")
|
||||
-- show information about connected board
|
||||
nn('+', ":w | !$HOME/.config/nvim/scripts/arduino -i<cr>")
|
||||
-- monitor serial data
|
||||
nn('-', ":w | tabe | ter $HOME/.config/nvim/scripts/arduino -m " .. baudrate .. " <cr>i")
|
||||
end
|
||||
102
nvim/lua/compilation.lua
Normal file
102
nvim/lua/compilation.lua
Normal file
@@ -0,0 +1,102 @@
|
||||
-- File: ~/.config/nvim/lua/compilation.lua
|
||||
|
||||
local M = {}
|
||||
|
||||
-- Function to compile and run the current file
|
||||
function M.compile_and_run()
|
||||
-- Get the current file path
|
||||
local file = vim.fn.expand('%')
|
||||
|
||||
-- Check if the current buffer is a directory
|
||||
if vim.fn.isdirectory(file) == 1 then
|
||||
vim.notify("Cannot compile a directory. Please open a source file.", vim.log.levels.ERROR)
|
||||
return
|
||||
end
|
||||
|
||||
-- Determine the filetype (c, cpp, etc.)
|
||||
local filetype = vim.bo.filetype
|
||||
local compiler = ''
|
||||
local flags = ''
|
||||
|
||||
-- Set compiler and flags based on filetype
|
||||
if filetype == 'c' then
|
||||
compiler = 'gcc'
|
||||
flags = '-lm -O3'
|
||||
elseif filetype == 'cpp' then
|
||||
compiler = 'g++'
|
||||
flags = '-lm -O3'
|
||||
else
|
||||
vim.notify("Unsupported file type: " .. filetype, vim.log.levels.ERROR)
|
||||
return
|
||||
end
|
||||
|
||||
-- Get the base name of the file without extension
|
||||
local output_name = vim.fn.expand('%:t:r') -- %:t:r extracts the filename without extension
|
||||
|
||||
-- Construct the compile command
|
||||
local compile_cmd = string.format('%s %s "%s" -o "%s"', compiler, flags, file, output_name)
|
||||
|
||||
-- Notify the user about the compilation process
|
||||
vim.notify("Compiling...", vim.log.levels.INFO)
|
||||
|
||||
-- Execute the compile command
|
||||
local compile_output = vim.fn.system(compile_cmd)
|
||||
local compile_exit = vim.v.shell_error
|
||||
|
||||
-- Check if compilation was successful
|
||||
if compile_exit ~= 0 then
|
||||
vim.notify("Compilation failed:\n" .. compile_output, vim.log.levels.ERROR)
|
||||
-- Populate quickfix list with errors
|
||||
vim.fn.setqflist({}, ' ')
|
||||
for line in compile_output:gmatch("[^\r\n]+") do
|
||||
if line:match("error") or line:match("warning") then
|
||||
vim.fn.setqflist({}, 'a', { lines = { line } })
|
||||
end
|
||||
end
|
||||
vim.cmd('copen') -- Open the quickfix list to show errors
|
||||
return
|
||||
end
|
||||
|
||||
-- Construct the run command
|
||||
local run_cmd = string.format('./%s', output_name)
|
||||
|
||||
-- Notify the user about the execution process
|
||||
vim.notify("Running executable...", vim.log.levels.INFO)
|
||||
|
||||
-- Function to check if a terminal is already open
|
||||
local function is_terminal_open()
|
||||
for _, win in ipairs(vim.api.nvim_list_wins()) do
|
||||
local buf = vim.api.nvim_win_get_buf(win)
|
||||
if vim.api.nvim_buf_get_option(buf, 'buftype') == 'terminal' then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
-- Run the executable in a terminal split at the bottom
|
||||
if not is_terminal_open() then
|
||||
vim.cmd('botright split') -- Open split at the bottom
|
||||
vim.cmd('resize 15') -- Optional: Adjust the size of the split
|
||||
vim.cmd('terminal ' .. run_cmd) -- Run the executable
|
||||
else
|
||||
-- If a terminal is already open, send the run command to it
|
||||
for _, win in ipairs(vim.api.nvim_list_wins()) do
|
||||
local buf = vim.api.nvim_win_get_buf(win)
|
||||
if vim.api.nvim_buf_get_option(buf, 'buftype') == 'terminal' then
|
||||
vim.api.nvim_set_current_win(win)
|
||||
vim.api.nvim_feedkeys(run_cmd .. '\n', 'n', true)
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Function to set up keybindings
|
||||
function M.setup()
|
||||
-- Bind <leader>c to compile_and_run in normal mode
|
||||
vim.keymap.set('n', '<leader>c', M.compile_and_run, { noremap = true, silent = true })
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
90
nvim/lua/keybindings.lua
Normal file
90
nvim/lua/keybindings.lua
Normal file
@@ -0,0 +1,90 @@
|
||||
--local map = vim.api.nvim_set_keymap
|
||||
local map = vim.keymap.set
|
||||
local opts = { noremap = true, silent = true }
|
||||
|
||||
-- Telescope Keybindings
|
||||
map('n', '<leader>ff', "<cmd>lua require('telescope.builtin').find_files()<CR>", opts)
|
||||
map('n', '<leader>fg', "<cmd>lua require('telescope.builtin').live_grep()<CR>", opts)
|
||||
map('n', '<leader>fb', "<cmd>lua require('telescope.builtin').buffers()<CR>", opts)
|
||||
map('n', '<leader>fh', "<cmd>lua require('telescope.builtin').help_tags()<CR>", opts)
|
||||
|
||||
-- Language Server Protocol (LSP) Keybindings
|
||||
-- gD: Go to the declaration of the symbol under the cursor.
|
||||
-- gd: Go to the definition of the symbol under the cursor.
|
||||
-- K: Show documentation for the symbol under the cursor.
|
||||
-- gi: Go to the implementation of a symbol (e.g., the implementation of an interface or function).
|
||||
-- <C-k>: Show the function signature while coding.\
|
||||
-- <space>rn: Rename a symbol (e.g., a variable or function).
|
||||
-- <space>ca: Trigger a code action (e.g., auto-fix issues, refactor code).
|
||||
-- gr: List all references to the symbol under the cursor.
|
||||
-- <space>f: Format the current buffer using the language server.
|
||||
local function lsp_keybindings(bufnr)
|
||||
local opts = { noremap = true, silent = true, buffer = bufnr }
|
||||
|
||||
vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, opts)
|
||||
vim.keymap.set('n', 'gd', vim.lsp.buf.definition, opts)
|
||||
vim.keymap.set('n', 'K', vim.lsp.buf.hover, opts)
|
||||
vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, opts)
|
||||
vim.keymap.set('n', '<C-k>', vim.lsp.buf.signature_help, opts)
|
||||
vim.keymap.set('n', '<space>rn', vim.lsp.buf.rename, opts)
|
||||
vim.keymap.set('n', '<space>ca', vim.lsp.buf.code_action, opts)
|
||||
vim.keymap.set('n', 'gr', vim.lsp.buf.references, opts)
|
||||
vim.keymap.set('n', '<space>f', vim.lsp.buf.format, opts)
|
||||
end
|
||||
|
||||
-- Wikipedia search
|
||||
function SearchWikipedia()
|
||||
local query = vim.fn.input('Wikipedia Search: ')
|
||||
query = vim.fn.shellescape(query)
|
||||
local search_url = "https://en.wikipedia.org/wiki/Special:Search?search=" .. query
|
||||
vim.cmd('silent !xdg-open ' .. search_url)
|
||||
end
|
||||
vim.api.nvim_set_keymap('n', '<leader>w', ':lua SearchWikipedia()<CR>', { noremap = true, silent = true })
|
||||
|
||||
|
||||
-- Vimwiki Markdown Preview
|
||||
function VimwikiMarkdownPreview()
|
||||
-- Path to the script
|
||||
local script_path = vim.fn.expand('~/.config/nvim/scripts/vimwiki-markdown-preview.sh')
|
||||
-- Check if the script exists
|
||||
if vim.fn.filereadable(script_path) == 0 then
|
||||
vim.notify("Script not found at " .. script_path, vim.log.levels.ERROR)
|
||||
return
|
||||
end
|
||||
-- Run the script with --index-wiki flag
|
||||
local command = string.format('bash %s --index-wiki', vim.fn.shellescape(script_path))
|
||||
vim.cmd('silent !' .. command)
|
||||
end
|
||||
map('n', '<leader>mip', VimwikiMarkdownPreview, opts)
|
||||
|
||||
-- Vimwiki Convert Current File to HTML, move it, and open with qutebrowser
|
||||
function VimwikiConvertCurrent()
|
||||
-- Path to the script
|
||||
local script_path = vim.fn.expand('~/.config/nvim/scripts/vimwiki-markdown-preview.sh')
|
||||
-- Check if the script exists
|
||||
if vim.fn.filereadable(script_path) == 0 then
|
||||
vim.notify("Script not found at " .. script_path, vim.log.levels.ERROR)
|
||||
return
|
||||
end
|
||||
-- Get the current file path
|
||||
local current_file = vim.api.nvim_buf_get_name(0)
|
||||
-- vim.notify("Retrieved file path: " .. current_file, vim.log.levels.INFO) -- Debugging: Print the file path
|
||||
-- Check if it's a markdown file
|
||||
if not current_file:match('%.md$') then
|
||||
vim.notify('Current file is not a Markdown file.', vim.log.levels.ERROR)
|
||||
return
|
||||
end
|
||||
-- Check if the source file exists
|
||||
if vim.fn.filereadable(current_file) == 0 then
|
||||
vim.notify('Current Markdown file does not exist.', vim.log.levels.ERROR)
|
||||
return
|
||||
end
|
||||
-- Explicitly construct the command string
|
||||
local command = "bash " .. vim.fn.shellescape(script_path) .. " --convert " .. current_file
|
||||
vim.notify("Running command: " .. command, vim.log.levels.INFO) -- Debugging: Print the command being run
|
||||
-- Run the command
|
||||
vim.cmd('silent !' .. command)
|
||||
vim.notify('Conversion and opening in qutebrowser completed.', vim.log.levels.INFO)
|
||||
end
|
||||
|
||||
map('n', '<leader>mp', VimwikiConvertCurrent, opts)
|
||||
44
nvim/lua/plugins.lua
Normal file
44
nvim/lua/plugins.lua
Normal file
@@ -0,0 +1,44 @@
|
||||
-- treesitter
|
||||
require'nvim-treesitter.configs'.setup{
|
||||
highlight = {
|
||||
enable = true,
|
||||
},
|
||||
indent = {
|
||||
enable = true,
|
||||
}
|
||||
}
|
||||
|
||||
-- telescope
|
||||
local builtin = require('telescope.builtin')
|
||||
|
||||
-- lualine
|
||||
require('lualine').setup()
|
||||
|
||||
|
||||
-- comment
|
||||
require('Comment').setup()
|
||||
|
||||
|
||||
-- autopairs
|
||||
require('nvim-autopairs').setup()
|
||||
|
||||
|
||||
-- cmp
|
||||
local cmp = require'cmp'
|
||||
cmp.setup({
|
||||
snippet = {
|
||||
expand = function(args)
|
||||
require('luasnip').lsp_expand(args.body)
|
||||
end
|
||||
}
|
||||
})
|
||||
cmp.setup.cmdline(':', {
|
||||
mapping = cmp.mapping.preset.cmdline(),
|
||||
sources = cmp.config.sources({
|
||||
{ name = 'path' }
|
||||
}, {
|
||||
{ name = 'cmdline' }
|
||||
}),
|
||||
matching = { disallow_symbol_nonprefix_matching = false }
|
||||
})
|
||||
-- TODO complete https://github.com/hrsh7th/nvim-cmp
|
||||
4
nvim/lua/settings.lua
Normal file
4
nvim/lua/settings.lua
Normal file
@@ -0,0 +1,4 @@
|
||||
-- General Settings
|
||||
vim.o.number = true
|
||||
vim.o.background = "dark"
|
||||
vim.cmd([[colorscheme gruvbox]])
|
||||
118
nvim/lua/settings.lua.all
Normal file
118
nvim/lua/settings.lua.all
Normal file
@@ -0,0 +1,118 @@
|
||||
-- SETTINGS.LUA: Comprehensive Neovim Configuration
|
||||
|
||||
-- Enable line numbers
|
||||
vim.o.number = true -- Show absolute line numbers
|
||||
vim.o.relativenumber = true -- Show relative line numbers (useful for jump motions)
|
||||
|
||||
-- Cursor behavior
|
||||
vim.o.cursorline = true -- Highlight the line where the cursor is
|
||||
vim.o.cursorcolumn = true -- Highlight the column where the cursor is
|
||||
vim.o.scrolloff = 8 -- Keep 8 lines visible above/below the cursor
|
||||
vim.o.sidescrolloff = 8 -- Keep 8 columns visible to the left/right of the cursor
|
||||
|
||||
-- UI customization
|
||||
vim.o.termguicolors = true -- Enable 24-bit RGB color in the terminal
|
||||
vim.o.signcolumn = "yes" -- Always show the sign column (useful for LSP, Git)
|
||||
vim.o.colorcolumn = "80" -- Highlight the 80th column (good for coding style guides)
|
||||
vim.o.laststatus = 3 -- Global status line (shared between windows)
|
||||
vim.o.showmode = false -- Disable the default mode display (e.g., -- INSERT --)
|
||||
vim.o.wrap = false -- Disable line wrapping (horizontal scrolling for long lines)
|
||||
vim.o.showcmd = true -- Display incomplete commands at the bottom
|
||||
vim.o.ruler = true -- Show the cursor position at the bottom right
|
||||
|
||||
-- Tabs and indentation
|
||||
vim.o.tabstop = 4 -- Number of spaces for a tab
|
||||
vim.o.shiftwidth = 4 -- Number of spaces for indentation
|
||||
vim.o.softtabstop = 4 -- Treat <Tab> as 4 spaces while editing
|
||||
vim.o.expandtab = true -- Convert tabs to spaces
|
||||
vim.o.autoindent = true -- Copy indentation from the previous line
|
||||
vim.o.smartindent = true -- Automatically add extra indentation for new lines
|
||||
|
||||
-- Search settings
|
||||
vim.o.ignorecase = true -- Ignore case when searching
|
||||
vim.o.smartcase = true -- Override ignorecase if the search query contains uppercase
|
||||
vim.o.incsearch = true -- Show incremental search results as you type
|
||||
vim.o.hlsearch = true -- Highlight all matches from the search
|
||||
vim.o.wildmenu = true -- Show a menu for command-line tab completion
|
||||
|
||||
-- File handling
|
||||
vim.o.undofile = true -- Enable persistent undo (saves undo history in a file)
|
||||
vim.o.swapfile = false -- Disable swap files (reduces disk writes)
|
||||
vim.o.backup = false -- Disable file backups
|
||||
vim.o.writebackup = false -- Disable backup before overwriting a file
|
||||
vim.o.autoread = true -- Automatically reload files changed outside of Neovim
|
||||
vim.o.hidden = true -- Allow switching buffers without saving
|
||||
|
||||
-- Split behavior
|
||||
vim.o.splitright = true -- Open vertical splits to the right
|
||||
vim.o.splitbelow = true -- Open horizontal splits below
|
||||
vim.o.equalalways = false -- Prevent automatic resizing of splits
|
||||
|
||||
-- Clipboard
|
||||
vim.o.clipboard = "unnamedplus" -- Use the system clipboard for all operations
|
||||
|
||||
-- Performance
|
||||
vim.o.lazyredraw = true -- Redraw only when needed (improves performance)
|
||||
vim.o.updatetime = 300 -- Time (ms) to trigger CursorHold events (lower = faster)
|
||||
vim.o.timeoutlen = 500 -- Time (ms) to wait for a key sequence to complete
|
||||
|
||||
-- Completion
|
||||
vim.o.completeopt = "menu,menuone,noselect" -- Customize the completion menu
|
||||
vim.o.pumheight = 10 -- Limit the number of items in the popup menu
|
||||
|
||||
-- Folding
|
||||
vim.o.foldmethod = "indent" -- Fold based on indentation levels
|
||||
vim.o.foldlevel = 99 -- Start unfolded
|
||||
vim.o.foldenable = true -- Enable folding by default
|
||||
|
||||
-- Backup directory (if enabled)
|
||||
vim.o.backupdir = vim.fn.stdpath("data") .. "/backup//" -- Store backups in a custom directory
|
||||
|
||||
-- Mouse support
|
||||
vim.o.mouse = "a" -- Enable mouse in all modes
|
||||
|
||||
-- Command-line
|
||||
vim.o.cmdheight = 1 -- Height of the command-line area
|
||||
vim.o.shortmess = vim.o.shortmess .. "c" -- Avoid showing extra messages during completion
|
||||
|
||||
-- Spell checking
|
||||
vim.o.spell = false -- Disable spell checking by default
|
||||
vim.o.spelllang = "en" -- Set spell check language to English
|
||||
|
||||
-- Wildmenu (command-line completion)
|
||||
vim.o.wildignore = "*.o,*.obj,*.bak,*.exe,*.pyc,*.class" -- Ignore specific file types during file navigation
|
||||
vim.o.wildmode = "longest:full,full" -- Customize command-line completion behavior
|
||||
|
||||
-- Terminal integration
|
||||
vim.o.shell = "bash" -- Set the default shell
|
||||
vim.o.shellcmdflag = "-c" -- Set shell command flag
|
||||
|
||||
-- Key mappings display
|
||||
vim.o.showcmdloc = "statusline" -- Display key mappings in the status line
|
||||
|
||||
-- Diagnostics (LSP)
|
||||
vim.diagnostic.config({
|
||||
virtual_text = true, -- Show diagnostics as virtual text
|
||||
signs = true, -- Show signs in the sign column
|
||||
underline = true, -- Underline problematic code
|
||||
update_in_insert = false, -- Don't update diagnostics while typing
|
||||
})
|
||||
|
||||
-- Performance optimizations for large files
|
||||
vim.cmd([[
|
||||
augroup LargeFiles
|
||||
autocmd!
|
||||
autocmd BufReadPre * if getfsize(expand("<afile>")) > 1048576 | setlocal syntax=off | endif
|
||||
augroup END
|
||||
]])
|
||||
|
||||
-- Custom filetype settings
|
||||
vim.cmd([[
|
||||
augroup FileTypeSpecific
|
||||
autocmd!
|
||||
autocmd FileType markdown setlocal wrap spell
|
||||
autocmd FileType python setlocal expandtab shiftwidth=4 tabstop=4
|
||||
autocmd FileType go setlocal noexpandtab tabstop=8 shiftwidth=8
|
||||
augroup END
|
||||
]])
|
||||
|
||||
314
nvim/lua/vimwiki.lua
Normal file
314
nvim/lua/vimwiki.lua
Normal file
@@ -0,0 +1,314 @@
|
||||
-- Vimwiki Configuration
|
||||
vim.cmd([[
|
||||
set nocompatible
|
||||
filetype plugin on
|
||||
syntax on
|
||||
]])
|
||||
|
||||
vim.g.vimwiki_list = {
|
||||
{
|
||||
path = '~/vimwiki/',
|
||||
syntax = 'markdown',
|
||||
ext = '.md',
|
||||
diary_rel_path = 'Diary/'
|
||||
}
|
||||
}
|
||||
|
||||
local M = {}
|
||||
|
||||
-- Create a LaTeX file with a more advanced template
|
||||
function M.create_latex()
|
||||
local markdown_path = vim.fn.expand('%:p') -- Full path of the current markdown file
|
||||
local tex_name = vim.fn.expand('%:t:r') .. ".tex" -- Base name with .tex extension
|
||||
local tex_path = markdown_path:gsub('/[^/]*$', '') .. "/" .. tex_name -- Path to the new .tex file
|
||||
|
||||
-- Check if the .tex file already exists
|
||||
if vim.fn.filereadable(tex_path) == 0 then
|
||||
-- Define the LaTeX template as a table of lines
|
||||
local template = {
|
||||
"\\documentclass[12pt]{article}",
|
||||
"\\usepackage{amsmath, amssymb, amsthm}",
|
||||
"\\usepackage{graphicx}", -- For including images
|
||||
"\\usepackage{hyperref}", -- For clickable links
|
||||
"\\usepackage[margin=1in]{geometry}", -- Page margins
|
||||
"\\usepackage{enumitem}", -- Better lists
|
||||
"",
|
||||
"\\title{Document Title}",
|
||||
"\\author{Author Name}",
|
||||
"\\date{\\today}",
|
||||
"",
|
||||
"\\begin{document}",
|
||||
"",
|
||||
"\\maketitle",
|
||||
"",
|
||||
"\\tableofcontents",
|
||||
"\\newpage",
|
||||
"",
|
||||
"\\section{Introduction}",
|
||||
"This is the introduction section.",
|
||||
"",
|
||||
"\\section{Main Content}",
|
||||
"This is where the main content goes.",
|
||||
"",
|
||||
"\\section{Conclusion}",
|
||||
"This is the conclusion.",
|
||||
"",
|
||||
"\\end{document}"
|
||||
}
|
||||
|
||||
-- Write the template to the .tex file
|
||||
local success, err = pcall(function()
|
||||
vim.fn.writefile(template, tex_path)
|
||||
end)
|
||||
|
||||
if not success then
|
||||
print("Error writing LaTeX template to file: " .. err)
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
-- Open the newly created .tex file
|
||||
vim.cmd('edit ' .. tex_path)
|
||||
end
|
||||
|
||||
-- Delete key: Save the .tex file and return to the markdown file
|
||||
function M.delete_latex()
|
||||
local tex_path = vim.fn.expand('%:p') -- Get the current LaTeX file path
|
||||
local markdown_path = tex_path:gsub('%.tex$', '.md') -- Replace .tex with .md
|
||||
|
||||
vim.cmd('write') -- Save the LaTeX file
|
||||
vim.cmd('edit ' .. markdown_path) -- Open the corresponding markdown file
|
||||
end
|
||||
|
||||
local zathura_pid = nil -- Global variable to store Zathura's actual PID
|
||||
|
||||
-- Helper function to get the PID of Zathura
|
||||
local function get_zathura_pid(pdf_path)
|
||||
local cmd = string.format("pgrep -f 'zathura %s'", pdf_path)
|
||||
local pid = vim.fn.system(cmd):gsub("%s+", "") -- Get PID and trim whitespace
|
||||
if pid == "" then
|
||||
return nil
|
||||
end
|
||||
return tonumber(pid)
|
||||
end
|
||||
|
||||
-- Stop monitoring and kill Zathura process
|
||||
function M.stop_monitoring()
|
||||
vim.cmd([[
|
||||
augroup LatexLiveMonitor
|
||||
autocmd!
|
||||
augroup END
|
||||
]])
|
||||
|
||||
if zathura_pid ~= nil then
|
||||
print("Stopping Zathura (PID: " .. zathura_pid .. ")...")
|
||||
local result = vim.fn.system({'kill', '-9', tostring(zathura_pid)})
|
||||
if result ~= "" then
|
||||
print("Zathura process stopped.")
|
||||
else
|
||||
print("Failed to kill Zathura process.")
|
||||
end
|
||||
zathura_pid = nil
|
||||
else
|
||||
print("No Zathura process to stop.")
|
||||
end
|
||||
end
|
||||
|
||||
-- Compile LaTeX and handle PDF
|
||||
function M.compile_latex()
|
||||
local tex_path = vim.fn.expand('%:p') -- Full path of the current .tex file
|
||||
|
||||
-- Ensure the current file is a .tex file
|
||||
if not tex_path:match("%.tex$") then
|
||||
print("This command can only be run inside a .tex file!")
|
||||
return
|
||||
end
|
||||
|
||||
-- Save the .tex file
|
||||
vim.cmd('write')
|
||||
|
||||
-- Define paths
|
||||
local pdf_name = vim.fn.expand('%:t:r') .. ".pdf" -- PDF file name
|
||||
local root_vimwiki = vim.fn.expand(vim.g.vimwiki_list[1].path) -- Expand ~ to home directory
|
||||
local supporting_files_path = root_vimwiki .. "/SupportingFiles/" -- SupportingFiles directory
|
||||
local pdf_dest = supporting_files_path .. pdf_name -- Full destination path for the PDF
|
||||
|
||||
-- Ensure SupportingFiles directory exists
|
||||
if vim.fn.isdirectory(supporting_files_path) == 0 then
|
||||
vim.fn.mkdir(supporting_files_path, "p")
|
||||
end
|
||||
|
||||
-- Compile LaTeX
|
||||
local compile_cmd = 'pdflatex -output-directory=' .. vim.fn.shellescape(vim.fn.expand('%:p:h')) .. ' ' .. vim.fn.shellescape(tex_path)
|
||||
vim.fn.system(compile_cmd)
|
||||
|
||||
-- Move the generated PDF to SupportingFiles
|
||||
local compiled_pdf_path = vim.fn.expand('%:p:h') .. '/' .. pdf_name
|
||||
if vim.fn.filereadable(compiled_pdf_path) == 1 then
|
||||
local move_cmd = 'mv ' .. vim.fn.shellescape(compiled_pdf_path) .. ' ' .. vim.fn.shellescape(pdf_dest)
|
||||
vim.fn.system(move_cmd)
|
||||
else
|
||||
print("Error: PDF file was not generated.")
|
||||
return
|
||||
end
|
||||
|
||||
-- Stop existing Zathura process if running
|
||||
if zathura_pid ~= nil then
|
||||
print("Stopping existing Zathura (PID: " .. zathura_pid .. ")...")
|
||||
vim.fn.system({'kill', '-9', tostring(zathura_pid)})
|
||||
zathura_pid = nil
|
||||
end
|
||||
|
||||
-- Start Zathura and store its actual PID
|
||||
vim.fn.jobstart({'zathura', pdf_dest}, {detach = true})
|
||||
zathura_pid = get_zathura_pid(pdf_dest) -- Get the actual PID of Zathura
|
||||
if zathura_pid == nil then
|
||||
print("Failed to retrieve Zathura PID.")
|
||||
return
|
||||
end
|
||||
|
||||
print("Zathura started (PID: " .. zathura_pid .. ").")
|
||||
|
||||
-- Cleanup auxiliary files
|
||||
M.cleanup_auxiliary_files()
|
||||
|
||||
-- Provide feedback to the user
|
||||
print("LaTeX compiled successfully. PDF moved to SupportingFiles.")
|
||||
end
|
||||
|
||||
-- Stop Zathura when exiting Neovim or closing the buffer
|
||||
vim.cmd([[
|
||||
augroup CloseZathuraOnExit
|
||||
autocmd!
|
||||
autocmd VimLeavePre * lua require('vimwiki').stop_monitoring()
|
||||
autocmd BufWinLeave,BufDelete *.tex lua require('vimwiki').stop_monitoring()
|
||||
augroup END
|
||||
]])
|
||||
|
||||
-- Monitor file and recompile on Enter or `.`
|
||||
function M.monitor_latex()
|
||||
local tex_path = vim.fn.expand('%:p') -- Full path of the current .tex file
|
||||
|
||||
-- Ensure the current file is a .tex file
|
||||
if not tex_path:match("%.tex$") then
|
||||
print("This command can only be run inside a .tex file!")
|
||||
return
|
||||
end
|
||||
|
||||
-- Run the initial compile (no Zathura is opened here)
|
||||
M.compile_latex_no_zathura()
|
||||
|
||||
-- Define paths
|
||||
local pdf_name = vim.fn.expand('%:t:r') .. ".pdf" -- PDF file name
|
||||
local root_vimwiki = vim.fn.expand(vim.g.vimwiki_list[1].path) -- Expand ~ to home directory
|
||||
local supporting_files_path = root_vimwiki .. "/SupportingFiles/" -- SupportingFiles directory
|
||||
local pdf_dest = supporting_files_path .. pdf_name -- Full destination path for the PDF
|
||||
|
||||
-- Ensure the PDF exists before starting monitoring
|
||||
if vim.fn.filereadable(pdf_dest) == 0 then
|
||||
print("Error: PDF file was not generated. Cannot start monitoring.")
|
||||
return
|
||||
end
|
||||
|
||||
-- Start Zathura and store its actual PID
|
||||
if zathura_pid ~= nil then
|
||||
vim.fn.system({'kill', '-9', tostring(zathura_pid)}) -- Kill the old Zathura process
|
||||
end
|
||||
vim.fn.jobstart({'zathura', pdf_dest}, {detach = true})
|
||||
zathura_pid = get_zathura_pid(pdf_dest) -- Get the actual PID of Zathura
|
||||
if zathura_pid == nil then
|
||||
print("Failed to retrieve Zathura PID.")
|
||||
return
|
||||
end
|
||||
print("Zathura started (PID: " .. zathura_pid .. ").")
|
||||
|
||||
-- Enter live monitoring mode
|
||||
vim.cmd([[
|
||||
augroup LatexLiveMonitor
|
||||
autocmd!
|
||||
" Trigger compilation on save
|
||||
autocmd BufWritePost <buffer> lua require('vimwiki').compile_latex_no_zathura()
|
||||
" Stop monitoring when the file is closed or buffer is unloaded
|
||||
autocmd BufWinLeave,BufDelete <buffer> lua require('vimwiki').stop_monitoring()
|
||||
augroup END
|
||||
]])
|
||||
|
||||
-- Map Enter and . to save the file
|
||||
vim.api.nvim_buf_set_keymap(0, 'i', '<CR>', '<CR><Esc>:w<CR>a', { noremap = true, silent = true })
|
||||
vim.api.nvim_buf_set_keymap(0, 'i', '.', '.<Esc>:w<CR>a', { noremap = true, silent = true })
|
||||
|
||||
print("Live monitoring started. PDF will update on save.")
|
||||
end
|
||||
|
||||
-- Compile LaTeX without opening Zathura
|
||||
function M.compile_latex_no_zathura()
|
||||
local tex_path = vim.fn.expand('%:p') -- Full path of the current .tex file
|
||||
|
||||
-- Ensure the current file is a .tex file
|
||||
if not tex_path:match("%.tex$") then
|
||||
return
|
||||
end
|
||||
|
||||
-- Save the .tex file
|
||||
vim.cmd('write')
|
||||
|
||||
-- Define paths
|
||||
local pdf_name = vim.fn.expand('%:t:r') .. ".pdf" -- PDF file name
|
||||
local root_vimwiki = vim.fn.expand(vim.g.vimwiki_list[1].path) -- Expand ~ to home directory
|
||||
local supporting_files_path = root_vimwiki .. "/SupportingFiles/" -- SupportingFiles directory
|
||||
local pdf_dest = supporting_files_path .. pdf_name -- Full destination path for the PDF
|
||||
|
||||
-- Compile LaTeX
|
||||
local compile_cmd = 'pdflatex -output-directory=' .. vim.fn.shellescape(vim.fn.expand('%:p:h')) .. ' ' .. vim.fn.shellescape(tex_path)
|
||||
vim.fn.system(compile_cmd)
|
||||
|
||||
-- Move the generated PDF to SupportingFiles
|
||||
local compiled_pdf_path = vim.fn.expand('%:p:h') .. '/' .. pdf_name
|
||||
if vim.fn.filereadable(compiled_pdf_path) == 1 then
|
||||
vim.fn.system('mv ' .. vim.fn.shellescape(compiled_pdf_path) .. ' ' .. vim.fn.shellescape(pdf_dest))
|
||||
else
|
||||
print("Error: PDF file was not generated.")
|
||||
return
|
||||
end
|
||||
|
||||
-- Cleanup auxiliary files
|
||||
M.cleanup_auxiliary_files()
|
||||
end
|
||||
|
||||
-- Cleanup auxiliary files
|
||||
function M.cleanup_auxiliary_files()
|
||||
local tex_path = vim.fn.expand('%:p') -- Full path of the current .tex file
|
||||
local tex_dir = vim.fn.expand('%:p:h') -- Directory containing the .tex file
|
||||
local base_name = vim.fn.expand('%:t:r') -- Base name of the .tex file
|
||||
|
||||
-- Auxiliary files to clean
|
||||
local aux_files = {
|
||||
tex_dir .. "/" .. base_name .. ".log",
|
||||
tex_dir .. "/" .. base_name .. ".aux",
|
||||
tex_dir .. "/texput.log"
|
||||
}
|
||||
|
||||
for _, file in ipairs(aux_files) do
|
||||
if vim.fn.filereadable(file) == 1 then
|
||||
vim.fn.delete(file)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Link PDF in the markdown file
|
||||
function M.link_pdf()
|
||||
local pdf_path = vim.fn.getreg('a') -- Get the PDF path from register "a"
|
||||
local link = "[" .. vim.fn.expand('%:t:r') .. "](" .. pdf_path .. ")"
|
||||
vim.fn.append(vim.fn.line('.'), link) -- Insert the link below the current line
|
||||
print("PDF linked in markdown!")
|
||||
end
|
||||
|
||||
-- Keybindings
|
||||
vim.api.nvim_set_keymap('n', '<leader>lt', [[:lua require('vimwiki').create_latex()<CR>]], { noremap = true, silent = true })
|
||||
vim.api.nvim_set_keymap('n', '<leader>ld', [[:lua require('vimwiki').stop_monitoring()<CR>:lua require('vimwiki').delete_latex()<CR>]], { noremap = true, silent = true })
|
||||
vim.api.nvim_set_keymap('n', '<leader>lc', [[:lua require('vimwiki').compile_latex()<CR>]], { noremap = true, silent = true })
|
||||
vim.api.nvim_set_keymap('n', '<leader>lm', [[:lua require('vimwiki').monitor_latex()<CR>]], { noremap = true, silent = true })
|
||||
vim.api.nvim_set_keymap('n', '<leader>ll', [[:lua require('vimwiki').link_pdf()<CR>]], { noremap = true, silent = true })
|
||||
|
||||
return M
|
||||
|
||||
Reference in New Issue
Block a user