Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More options for context menu in general #2674

Open
gssfaw opened this issue Mar 23, 2023 · 4 comments
Open

More options for context menu in general #2674

gssfaw opened this issue Mar 23, 2023 · 4 comments

Comments

@gssfaw
Copy link

gssfaw commented Mar 23, 2023

Hello, firstly thanks for the great plugin, latex with this plugin made me enjoy writing documents. There are a lot of vimtex features that I discover even now, for example, only after two years of usage I've found extremely useful table of contents. Maybe half a year ago I discovered context menu for some bibliography management. The point here is that vimtex offers a huge number of options, but not all of them are used(by some users in some cases).
And since I had some experience with lsp servers that provide context menu options too (for example rust-analyzer), I thought, maybe it is possible to expand vimtex's context menu to other things?

Let me provide explanation and some ideas:

  • There are a lot of ways to change environments, like changing them, toggling starred option, deleting them etc. While these actions are made in the vim's way so you can memorize them, like cse = change surrounding environment, in my experience it is not enough. I don't really remember every mapping except often used, but it doesn't mean, that I don't need some rarely used.

  • The same goes with math environment: options like tsd, ts$, tsf (by the way I've found the last one only just now)

So, my idea here is to use context menu based on current cursor position. In my opinion, it could make vimtex more friendly for new users and also expose some rarely used features for those who tend to forget them or just don't know about them. I mean if the cursor placed on or inside environment, invoking context menu will bring options based on that context, like cse, dse, tse, maybe visual options too.

I can imagine that it is possible to extend it even more, for example, to show document compiling options like starting the compiler, cleaning after it is stopped etc. Or it would be nice to have a preview on labels like on bib entries, so instead of gd it'd be possible to remind what it was without jumping twice.

It can be generalized like this: use the context menu to show all available options based on the current cursor position and maybe some additional features for certain contexts.

I don't know if it is hard to implement, or it does not follow your vision on vimtex, so if you don't like I'll close the issue. Or if you find this idea interesting, maybe others can better than me propose some good ideas on context menu usage, maybe some of my suggestions are bad or not good enough, but the idea itself has some potential.

@lervag
Copy link
Owner

lervag commented Mar 25, 2023

Hello, firstly thanks for the great plugin, latex with this plugin made me enjoy writing documents.

Thanks, I'm glad to hear that!

… I thought, maybe it is possible to expand vimtex's context menu to other things?

Yes, that was actually sort of the plan. Not necessarily to add everything, but to populate it with more features. But I never really found time or inspiration/motivation to do more than what I've already added.

Let me provide explanation and some ideas: …

Thanks. Yes, these things can be implemented to be discoverable with the context feature.

So, my idea here is to use context menu based on current cursor position. In my opinion, it could make vimtex more friendly for new users and also expose some rarely used features for those who tend to forget them or just don't know about them. I mean if the cursor placed on or inside environment, invoking context menu will bring options based on that context, like cse, dse, tse, maybe visual options too.

Many of the mentioned features are really close to what I would call "core" Vim concepts. The use of text objects, additional verbs (like "change surrounding", "delete surrounding", "toggle surrounding"), are meant to feel intuitive to experienced Vim users. I understand that this is off putting to less experienced users who are still learning Vim concepts, and I understand that there is a lot to learn and that discoverability can be very useful!

It can be generalized like this: use the context menu to show all available options based on the current cursor position and maybe some additional features for certain contexts.

I don't know if it is hard to implement, or it does not follow your vision on vimtex, so if you don't like I'll close the issue. Or if you find this idea interesting, maybe others can better than me propose some good ideas on context menu usage, maybe some of my suggestions are bad or not good enough, but the idea itself has some potential.

Well, ok; first - I think the context menu can and should be improved with additional features and contexts! But I think there are better alternatives for the "standard" mappings and commands, including compiler commands, and the cs*, ds*, ts* mappings. In particular, I think the which-key.nvim may be much closer to want you want. I believe it may even work for VimTeX out of the box, although I've never tried it and don't know.


Thus, for me, the question is: which existing features are less discoverable and will fit in the context menu? This would e.g. be features that don't have an existing default mapping or something that is typically very contextual. I think these may be interesting to consider:

  • Open the package or documentclass file under cursor (gf).
  • Probably others...

Also, new features that may be useful:

  • A preview on labels like on bib entries.

Finally, a short answer to this:

I don't know if it is hard to implement, …

Adding a new context handler should not be very hard*. We need a new module in the code, i.e. a new file under autoload/vimtex/context/NAME.vim, and it needs to create a handler object similar to how it is done for the cite.vim handler.

@kiryph
Copy link
Contributor

kiryph commented Mar 28, 2023

which-key.nvim

Yes, VimTeX works out of the box. Here three screenshots.

  1. <leader>l:
    Screenshot 2023-03-28 at 08 49 36

  2. ts:
    Screenshot 2023-03-28 at 08 51 24

  3. [:
    Screenshot 2023-03-28 at 08 52 12

However, explicit support could be added by providing better descriptions since, e.g. <Plug>(vimtex-[n) is not self-explanatory:

https://github.com/folke/which-key.nvim#-setup

With the default settings, WhichKey will work out of the box for most builtin keybindings, but the real power comes from documenting and organizing your own keybindings.

@lervag
Copy link
Owner

lervag commented Mar 28, 2023

Yes, VimTeX works out of the box. Here three screenshots.

Thanks, @kiryph! This does prove my assumption and shows where it works well and where it is clearly lacking. IMHO, it seems to work well for e.g. <leader>l, since the <plug> mappings are quite self explanatory and the menu already looks good. For the ts menu, it also looks decent, but it may be slightly harder to understand the targets. For the motions with [ it is clearly hard to understand based on the plug mappings.

A solution (for long term future) would be to use lua with the desc key for mappings. But I won't do that for several years yet (if ever).

However, I believe it should not be too hard to manually add descriptions here? I'm not very interested in doing that work myself. But if it makes sense to either 1) include it in the repo, or 2) add something about this to the docs, then I would not mind help to do so.

However, explicit support could be added by providing better descriptions since, e.g. <Plug>(vimtex-[n) is not self-explanatory:

I assume one would need something like this, at some level:

wk.register({
  ts = {
    name = "Toggle surrounding",
    e = { "tse", "Environment" },
    d = { "tsd", "Delimiter" },
  },
  cs = {
    name = "Change surrounding",
    e = { "cse", "Environment" },
    d = { "csd", "Delimiter" },
  },
})

@kiryph
Copy link
Contributor

kiryph commented Mar 28, 2023

Here a possible user configuration for neovim using which-key.nvim:

vim.api.nvim_create_autocmd("BufEnter", {
  pattern = "*.tex",
  callback = function()
    require("vimtexwhichkey")
  end,
})
file vimtexwhichkey.lua
-- file vimtexwhichkey.lua
local wk = require("which-key")
local opts = {
  mode = "n", -- NORMAL mode
  buffer = vim.api.nvim_get_current_buf(), -- Specify a buffer number for buffer local mappings to show only in tex buffers
}

local mappings = {
  ["<localleader>l"] = {
    name = "+VimTeX",
    a = "Show Context Menu",
    C = "Full Clean",
    c = "Clean",
    e = "Show Errors",
    G = "Show Status for All",
    g = "Show Status",
    i = "Show Info",
    I = "Show Full Info",
    k = "Stop VimTeX",
    K = "Stop All VimTeX",
    L = "Compile Selection",
    l = "Compile",
    m = "Show Imaps",
    o = "Show Compiler Output",
    q = "Show VimTeX Log",
    s = "Toggle Main",
    t = "Open Table of Contents",
    T = "Toggle Table of Contents",
    v = "View Compiled Document",
    X = "Reload VimTeX State",
    x = "Reload VimTeX",
  },
  ["ts"] = {
    name = "VimTeX Toggles & Cycles", -- optional group name
    ["$"] = "Cycle inline, display & numbered equation",
    c = "Toggle star of command",
    d = "Cycle (), \\left(\\right) [,...]",
    D = "Reverse Cycle (), \\left(\\right) [, ...]",
    e = "Toggle star of environment",
    f = "Toggle a/b vs \\frac{a}{b}",
  },
  ["[/"] = "Previous start of a LaTeX comment",
  ["[*"] = "Previous end of a LaTeX comment",
  ["[["] = "Previous beginning of a section",
  ["[]"] = "Previous end of a section",
  ["[m"] = "Previous \\begin",
  ["[M"] = "Previous \\end",
  ["[n"] = "Previous start of a math zone",
  ["[N"] = "Previous end of a math zone",
  ["[r"] = "Previous \\begin{frame}",
  ["[R"] = "Previous \\end{frame}",
  ["]/"] = "Next start of a LaTeX comment %",
  ["]*"] = "Next end of a LaTeX comment %",
  ["]["] = "Next beginning of a section",
  ["]]"] = "Next end of a section",
  ["]m"] = "Next \\begin",
  ["]M"] = "Next \\end",
  ["]n"] = "Next start of a math zone",
  ["]N"] = "Next end of a math zone",
  ["]r"] = "Next \\begin{frame}",
  ["]R"] = "Next \\end{frame}",
  ["cs"] = {
    c = "Change surrounding command",
    e = "Change surrounding environment",
    ["$"] = "Change surrounding math zone",
    d = "Change surrounding delimiter",
  },
  ["ds"] = {
    c = "Delete surrounding command",
    e = "Delete surrounding environment",
    ["$"] = "Delete surrounding math zone",
    d = "Delete surrounding delimiter",
  },
}

wk.register(mappings, opts)

-- VimTeX Text Objects without variants with targets.vim
opts = {
  mode = "o", -- Operator pending mode
  buffer = vim.api.nvim_get_current_buf(),
}
local objects = {
  ["ic"] = [[LaTeX Command]],
  ["ac"] = [[LaTeX Command]],
  ["id"] = [[LaTeX Math Delimiter]],
  ["ad"] = [[LaTeX Math Delimiter]],
  ["ie"] = [[LaTeX Environment]],
  ["ae"] = [[LaTeX Environment]],
  ["i$"] = [[LaTeX Math Zone]],
  ["a$"] = [[LaTeX Math Zone]],
  ["iP"] = [[LaTeX Section, Paragraph, ...]],
  ["aP"] = [[LaTeX Section, Paragraph, ...]],
  ["im"] = [[LaTeX Item]],
  ["am"] = [[LaTeX Item]],
}
wk.register(objects, opts)

which will turn my screenshots from before into

  1. <leader>l
    Screenshot 2023-03-29 at 09 29 05

  2. ts
    Screenshot 2023-03-29 at 09 28 46

  3. [
    Screenshot 2023-03-29 at 10 23 34

It is not yet complete, but should you get started. (And writing yourself will help you to explore it.)

Autocommand idea from folke/which-key.nvim#165 (comment)

(Side note: I have installed https://github.com/afreakk/unimpaired-which-key.nvim so that the menus for [ and ] are more readable when vim-unimpaired is installed.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants