Giter Site home page Giter Site logo

cmp-vimtex's Introduction

cmp-vimtex

nvim-cmp source providing bespoke support for Vimtex's omnifunc.

Check out the tutorial for an in-depth explanation of the plugin's functionality.

Table of contents

Features

Provides support for:

  • Fuzzy matching against all info provided by Vimtex (including bibliographic details, useful for citations):
  • Perform websearches of bibliographic completion items on research databases and search engines (check out the tutorial);
  • Parse and display all details contained in bibtex files:
  • Trimming long strings in the completion menu (adds space for the documenation window):
  • Trigger the completion menu automatically after typing \cite{ (normally, it has to be done manually).
  • Conveniently toggling symbols and additional information shown in the completion menu:
  • Granuarly configuring the menus, and more (check out the tutorial).

Installation

Install the plugin through your plugin manager:

lazy.nvim:

require("lazy").setup({
  "micangl/cmp-vimtex",
})

Setup

Add cmp-vimtex as a completion source:

require('cmp').setup({
  sources = {
    { name = 'vimtex', },
  },
})

If you're manually specifying a custom format function (this doesn't apply to lspkind.nvim), make sure not to overwrite cmp-vimtex's additional information:

format = function(entry, vim_item)
  vim_item.menu = ({
    -- Use this line if you wish to add a specific kind for cmp-vimtex:
    --vimtex = "[Vimtex]" .. (vim_item.menu ~= nil and vim_item.menu or ""),
    vimtex = vim_item.menu,
    buffer = "[Buffer]",
    nvim_lsp = "[LSP]",
  })[entry.source.name]

  return vim_item
end

Eventually, apply your configuration (note:this is not necessary unless you want to apply a custom configuration):

require('cmp_vimtex').setup({
    -- Eventual options can be specified here.
    -- Check out the tutorial for further details.
})

Advanced setup

This is covered in the tutorial.

Acknowledgments

This plugin is based on @hrsh7th's cmp-omni. The timer implementation has been taken from cmp-buffer. The bibtex parser is a lua rewrite of Vimtex's integrated parser, which has also been adapted for asynchronous execution.

I'd like to thank @lervag for all the help provided when developing this plugin.

cmp-vimtex's People

Contributors

jdujava avatar lervag avatar micangl avatar wduminy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

cmp-vimtex's Issues

Hide package details, show symbols

Is there any way to hide the details that say which source the command comes from, but keep symbols showing? At the moment, I've disabled menu entirely for cmp-vimtex, because it makes the cmp dropdown a lot larger because of the information showing me which package a command comes from. I tried to disable that using info_in_menu = false, and while it hides some information, the source still shows. If I type something like \forall, it shows [cmd: default] ∀. I'd like to just see the ∀, without it telling me the package.

Would this be achievable at all, or require a significant rewrite or config change?

duplicated vimtex source problem

Thank you for your efforts,

If I configure cmp-vimex, CmpStatus shows me the vimtex sources are duplicated
In nvim-cmp lazy.nvim setup, I called the cmp-vimtex plugin as dependencies.

Case 1) If I don't configure cmp-vimtex

dependencies = {
  'hrsh7th/cmp-buffer',       -- source for text in buffer
  'hrsh7th/cmp-path',         -- source for file system path
  'hrsh7th/cmp-cmdline',		-- source for commandline, [command], [path]
  'hrsh7th/cmp-nvim-lsp',     -- using LSP for source
  'f3fora/cmp-spell',			-- source for vim's spellsuggest
  'L3MON4D3/LuaSnip',         -- snippet engine
  'saadparwaiz1/cmp_luasnip', -- using LuaSnip for source
  'mstanciu552/cmp-matlab',   -- source of matlab
  {
    'micangl/cmp-vimtex', 		-- source of vimtex for latex
  },
},

Cmp-vimtex loaded just once for unused source names (I didn't load this source to confirm how many this source is called just.)
image

Case 2) If I configure cmp-vimtex

dependencies = {
  'hrsh7th/cmp-buffer',       -- source for text in buffer
  'hrsh7th/cmp-path',         -- source for file system path
  'hrsh7th/cmp-cmdline',		-- source for commandline, [command], [path]
  'hrsh7th/cmp-nvim-lsp',     -- using LSP for source
  'f3fora/cmp-spell',			-- source for vim's spellsuggest
  'L3MON4D3/LuaSnip',         -- snippet engine
  'saadparwaiz1/cmp_luasnip', -- using LuaSnip for source
  'mstanciu552/cmp-matlab',   -- source of matlab
  {
    'micangl/cmp-vimtex', 		-- source of vimtex for latex
    config = function ()
      require('cmp_vimtex').setup({})
    end
  },
},

Cmp-vimtex loaded twice as below
image

Could you know where the vimtex source are called one more?

Global Bib File

Although I can get bib completion to work with a local .bib file, I cannot manage to get the same to work for a global .bib ~/texmf/bibtex/bib/Zotero.bib which I source with \bibliography{Zotero} at the end of my latex document. I use this global .bib file for everything and then have a keybinding set up to generate a local .bib file which only includes entries for the citations that occur in the local latex document once it is finished.

I am wondering: how can I get cmp-vimtex to find my global .bib file ~/texmf/bibtex/bib/Zotero.bib?

Thanks so much for an excellent plugin/extension. I'm so pleased to have it back up and running :)

Change of cwd when opening .tex file

I noticed that upon opening a .tex file in neovim, my cwd would change to the same as that of the file itself. I narrowed this behavior down to this plugin - disabling it leaves my cwd unchanged. It is simply added using lazy and, as per the vimtex documentation, vimtex is added as as cmp source for the .tex filetype.

I am unsure where this behavior comes from - but I will be happy to help out if it is not immediately reproducible and turns out to be a more complicated interplay between my plugins.

Crash/errors on unrecognized key type in bibliography

Hi,

Thanks for the plugin!
Unfortunately, I'm getting persistent crashes/error messages when loading in bibliographies from a couple collaborators: the reason seems to be an unrecognized key in the citation.
The error I'm getting is

stack traceback:
	...cal/share/nvim/lazy/cmp-vimtex/lua/cmp_vimtex/parser.lua:64: in function 'format_entry'
	...cal/share/nvim/lazy/cmp-vimtex/lua/cmp_vimtex/parser.lua:143: in function 'callback'
	...ocal/share/nvim/lazy/cmp-vimtex/lua/cmp_vimtex/timer.lua:72: in function <...ocal/share/nvim/lazy/cmp-vimtex/lua/cmp_vimtex/timer.lua:66>
Error executing vim.schedule lua callback: ...cal/share/nvim/lazy/cmp-vimtex/lua/cmp_vimtex/parser.lua:102: attempt to perform arithmetic on field 'lnum' (a nil value)`

and here's a MWE:
'test.tex'

\begin{document}
\title{this is a title}
\bibliography{test}

\end{document}

'test.bib'

@misc{Harvard-01,
    title = "something"
    key = {Harvard}
}

Thanks!

Removing autocompletion for regular commands

I'm trying to figure out how to disable all the regular LaTeX symbol autocompletions:

image

I'd basically like to only display autocompletion data related to things like labels, bibliographic citations, etc. rather than all the commands from packages!

I tried to read the cmp-vimtex documentation but could not figure out what the trigger would be. Currently my sources are:

        sources = cmp.config.sources {
          { name = 'ultisnips', priority = 1000 },
          -- { name = 'omni', priority = 1000 },
          { name = 'vimtex', priority = 750 },
          -- { name = 'nvim_lsp', priority = 600 },
          -- { name = 'buffer', priority = 500 },
          -- { name = 'otter', priority = 500 },
          -- { name = 'path', priority = 250 },
        },

so I have only enabled ultisnips and vimtex sources. The long list of suggestions goes away if I comment out the vimtex sources line.

Bug in bibtex parser

Given the following lua init file e.g. under ~/test/test.lua:

local root = vim.fn.fnamemodify("./.lazy", ":p")

for _, name in ipairs({ "config", "data", "state", "cache" }) do
	vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
	vim.fn.system({ "git", "clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", lazypath })
end
vim.opt.runtimepath:prepend(lazypath)

local plugins = {
	"hrsh7th/nvim-cmp",
	"micangl/cmp-vimtex",
	"lervag/vimtex",
}
require("lazy").setup(plugins, { root = root .. "/plugins" })

vim.g.vimtex_cache_root = "."
vim.g.vimtex_cache_persistent = false

vim.cmd([[silent! close]])
vim.cmd.edit("test.tex")

And the following test.tex:

\documentclass{article}
\usepackage[backend=biber]{biblatex}
\addbibresource{biblatex-examples.bib}

\begin{document}
Test \cite{gaonkar}
\end{document}

We get the following error message:

Error executing vim.schedule lua callback: .../test/.lazy/plugins/cmp-vimtex/lua/cmp_vimtex/parser.lua:64: attempt to index local 'entry' (a nil value)
stack traceback:
        .../test/.lazy/plugins/cmp-vimtex/lua/cmp_vimtex/parser.lua:64: in function 'format_entry'
        .../test/.lazy/plugins/cmp-vimtex/lua/cmp_vimtex/parser.lua:143: in function 'callback'
        ...g/test/.lazy/plugins/cmp-vimtex/lua/cmp_vimtex/timer.lua:72: in function <...g/test/.lazy/plugins/cmp-vimtex/lua/cmp_vimtex/timer.lua:66>

Environment completion completes a brace

Here I tried to explain whats up. Basically I wrote some snippets (\begin snippet) for example, and when the cmp-vimtex completes the environment name it closes the brace. I dont need this brace. Or do I? I just want to know what I am doing wrong.

Loading Delay and Icon Support

I am not sure if this is a bug or not, or if there is anything to be done, but I've noticed that if I open nvim and go to cite something right away, when I cycle through possible completions, it doesn't open up the information menu.
If I exit out and try again, then everything works.
I have also noticed that If I wait 30sec or so before trying to cite something, then everything works.

This is not at all a problem, just wanted to mention it in case that is helpful. Feel free to close immediately if there is nothing to be done.

One other question I had is if it is possible to replace '[article]' or '[book]' etc., with a nerdfont icon. Here is what I tried (first three entries) and seems to work for other sources (I guess nerdfont icons don't work here):

'''
local kind_icons = {
article = "󰧮",
book = "󰧮",
incollection = "󱓷",
Function = "󰊕",
Constructor = "󰧮",
Text = "󰦨",
Method = "",
Field = "󰅪",
ETC
}

formatting = {
  fields = { "kind", "abbr", "menu" },
  format = function(entry, vim_item)
    vim_item.kind = string.format("%s", kind_icons[vim_item.kind]) -- Kind icons
    vim_item.menu = ({
      vimtex = (vim_item.menu ~= nil and vim_item.menu or "[VimTex]"),
      nvim_lsp = "[LSP]",
ETC
}

'''

Thanks for the help. Been loving the plugin!

Failed to source cmp_vimtex.lua

When installing the plugin using lazy.nvim, I get the following error:

Failed to source `/Users/computer/.local/share/nvim/lazy/cmp-vimtex/after/plugin/cmp_vimtex.lua`

vim/_editor.lua:0: function <SNR>38_NetrwOpenFile[35]..BufNewFile Autocommands for "*"..FileType Autocommands for "tex"..script nvim_exec2() called at FileType Autocommands fo
r "tex":0../Users/computer/.local/share/nvim/lazy/cmp-vimtex/after/plugin/cmp_vimtex.lua: Vim(source):E5113: Error while calling lua chunk: vim/_editor.lua:0: function <SNR>38
_NetrwOpenFile[35]..BufNewFile Autocommands for "*"..FileType Autocommands for "tex"..script nvim_exec2() called at FileType Autocommands for "tex":0../Users/computer/.local/s
hare/nvim/lazy/cmp-vimtex/after/plugin/cmp_vimtex.lua..nvim_exec2() called at /Users/computer/.local/share/nvim/lazy/cmp-vimtex/after/plugin/cmp_vimtex.lua:0: Vim(call):E121:
Undefined variable: b:vimtex
stack traceback:
^I[C]: in function 'nvim_exec2'
^Ivim/_editor.lua: in function 'cmd'
^I...cal/share/nvim/lazy/cmp-vimtex/lua/cmp_vimtex/source.lua:19: in function 'start_parser'
^I...local/share/nvim/lazy/cmp-vimtex/lua/cmp_vimtex/init.lua:5: in function '_start_parser'
^I...local/share/nvim/lazy/cmp-vimtex/lua/cmp_vimtex/init.lua:22: in function 'setup'
^I...l/share/nvim/lazy/cmp-vimtex/after/plugin/cmp_vimtex.lua:2: in main chunk
^I[C]: in function 'nvim_exec2'
^Ivim/_editor.lua: in function 'cmd'
^I...local/share/nvim/lazy/lazy.nvim/lua/lazy/core/loader.lua:481: in function <...local/share/nvim/lazy/lazy.nvim/lua/lazy/core/loader.lua:480>
^I[C]: in function 'xpcall'
^I.../.local/share/nvim/lazy/lazy.nvim/lua/lazy/core/util.lua:113: in function 'try'
^I...local/share/nvim/lazy/lazy.nvim/lua/lazy/core/loader.lua:480: in function 'source'
^I...local/share/nvim/lazy/lazy.nvim/lua/lazy/core/loader.lua:439: in function 'source_runtime'
^I...local/share/nvim/lazy/lazy.nvim/lua/lazy/core/loader.lua:410: in function 'packadd'
^I...local/share/nvim/lazy/lazy.nvim/lua/lazy/core/loader.lua:342: in function '_load'
^I...local/share/nvim/lazy/lazy.nvim/lua/lazy/core/loader.lua:187: in function 'load'
^I...hare/nvim/lazy/lazy.nvim/lua/lazy/core/handler/event.lua:83: in function <...hare/nvim/lazy/lazy.nvim/lua/lazy/core/handler/event.lua:72>
^I[C]: in function 'nvim_cmd'
^I...brew/Cellar/neovim/0.9.4/share/nvim/runtime/filetype.lua:25: in function <...brew/Cellar/neovim/0.9.4/share/nvim/runtime/filetype.lua:24>
^I[C]: in function 'nvim_buf_call'
^I...brew/Cellar/neovim/0.9.4/share/nvim/runtime/filetype.lua:24: in function <...brew/Cellar/neovim/0.9.4/share/nvim/runtime/filetype.lua:10>

As you can see, I installed nvim from brew, if that's relevant. I have the vimtex plugin installed and it seems to work fine. I have the nvim-cmp plugin installed by installing zero-lsp nvim with luasnips integration.

Autocommand not triggered

The autocommand defined here is not triggered if cmp-vimtex is loaded lazily by e.g. lazy.nvim. I think this will be a quite common situation.

A simple, but possibly good solution is to add a check if the current file is already a .tex file, and to then start the parser immediately.

I would also advice to put all code inside the setup function. That is, requireing a script should not have a side effect in itself.

Use VimTeXs lua parser?

I'm curious if it may make more sense to utilize the new VimTeX lua parser for bib files directly. It has now become quite fast. And after parsing a large file it can be kept in memory.

Thus, even if there would be a delay while parsing for the completion, the delay would be 1) very small, and 2) should only appear once for each session.

I believe this would simplify the cmp-vimtex plugin. Simpler is often better. :)

Custom formatting work-around

I've just moved over to using cmp-vimtex from cmp-omni. I'm liking it better, and there are a few hick-up's I'm trying to fix. I'd like to see the citation information the plugin provides, but I have a custom formatting for my cmp sources:

cmp.setup({ 
...
	formatting = {
	fields = { "kind", "abbr", "menu" },
	format = function(entry, vim_item)
		vim_item.kind = string.format('%s %s', icons.kind_icons[vim_item.kind], vim_item.kind) -- This concatenates the icons with the name of the item kind
		vim_item.menu = ({
			nvim_lsp = "[Lsp]",
			luasnip = "[Snip]",
			path = "[Path]",
			nerdfont = '[Nerd]',
			buffer = "[Buf]",
			spell = "[Spell]",
			calc = "[Calc]",
			vimtex = "[Vimtex]",
			git = "[Git]",
			Variable = "[Var]",
			Todos = "[Comment]",
		})[entry.source.name]
		return vim_item
	end,
	}
...
})

I have done require('cmp-vimtex').setup(...) as well, but this seems to override it. I think I need to put something here to say that for cmp-vimtex, use its custom formatting. Any ideas on how to do that?

New feature: documentation window highlighting

In the new branch test_highlighting is available a feature I had wanted to implement for some time. Basically, it highlights the bibliographic keys in the documentation windows:
2024-02-23T17:40:20,979233542+01:00

I haven't written the documentation yet. This is the relevant part of the configuration:

local defaults = {
  additional_information = {
    bib_highlighting = true,
    highlight_colors = {
      default_group = "Normal", -- Highlight group for most of the keys (in my config it's the yellow part)
      important_group = "IncSearch", -- Highlight group to distinguish the most important keys
      default = { -- Eventually, color codes can be specified
        fg = "",
        bg = "",
      },
      important = {
        fg = "",
        bg = "",
      },
    },
    highlight_links = { -- This sets, for each key, if "Default" or "Important" is used.
      -- Normal highlight groups can be used too.
      -- Bibtex
      Address = "Default",
      Annote = "Default",
      Author = "Important",
      Booktitle = "Default",
      Email = "Default",
      Chapter = "Default",
      Crossref = "Default",
      Doi = "Default",
      Edition = "Default",
      Editor = "Default",
      Howpublished = "Default",
      Institution = "Default",
      Journal = "Default",
      Key = "Default",
      Month = "Default",
      Note = "Default",
      Number = "Default",
      Organization = "Default",
      Pages = "Default",
      Publisher = "Default",
      School = "Default",
      Series = "Default",
      Title = "Important",
      Type = "Default",
      Volume = "Default",
      Year = "Default",
      -- Biblatex
      Isbn = "Default",
      -- cmp-vimtex-specific keys
      File = "Default",
      Lnum = "Default",
      Cite = "Default",
    },
  },
}

This may interest @lervag and @benbrastmckie.

README instructions not working for me?

Using neovim v0.9.4 on Mac OS Sonoma 14.1.1 with nvim-cmp for autompletion, but when I tried to include cmp-vimtex using your README instructions, none of the functionality seems to be working for LaTeX files. Here is my nvim-cmp.lua file:

Screenshot 2023-11-30 at 5 15 00 PM

And my plugins setup file (uses packer for plugin management):

Screenshot 2023-11-30 at 5 16 31 PM

This isn't exactly a bug, it's more likely that I'm missing something in the configuration, but I'll be grateful for any help you might be able to provide!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.