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

Latex-style highlighting and editing in .bib file #2301

Closed
bongbang opened this issue Jan 21, 2022 · 7 comments
Closed

Latex-style highlighting and editing in .bib file #2301

bongbang opened this issue Jan 21, 2022 · 7 comments

Comments

@bongbang
Copy link

bongbang commented Jan 21, 2022

Is your feature request related to a problem? Please describe it.
Currently, there is no latex-style highlighting inside an entry field of a .bib file and <F7> doesn't insert a command the way it does for a .tex file.

Describe the solution you'd like
Both of these would be useful for as non-trivial Latex editing is sometimes required.

It may not be necessary to render, say, the content of \emph as emphasized, nor to highlight ~, --, etc (though I think that would be nice) but it would be very helpful to highlight every command at least, as would the <F7> insertion, especially in the visual mode.

Examples from Biblatex developers and a TeX thread


@inproceedings{moraux,
  author       = {Moraux, Paul},
  editor       = {Lloyd, G. E. R. and Owen, G. E. L.},
  title        = {Le \emph{De Anima} dans la tradition gr{\`e}cque},
  date         = 1979,
  booktitle    = {Aristotle on Mind and the Senses},
  subtitle     = {Quelques aspects de l'interpretation du trait{\'e}, de
                  Theophraste {\`a} Themistius},
  booktitleaddon= {Proceedings of the Seventh Symposium Aristotelicum},
  eventdate    = 1975,
  publisher    = cup,
  location     = {Cambridge},
  pages        = {281-324},
}

@Article{gibbard,
  author       = {Gibbard, Allan},
  title        = {Morality in Living},
  subtitle     = {Korsgaard's {Kantian} Lectures},
  journaltitle = {Ethics},
  year         = 1999,
  volume       = 110,
  number       = 1,
  pages        = {140--164},
  titleaddon   = {\bibstring{reviewof} \mkbibemph{The Sources of
                  Normativity}, \bibstring{by} {Christine M. Korsgaard}},
}

@lervag
Copy link
Owner

lervag commented Jan 21, 2022

Reg. highlighting: This is not trivial, but I'll look into it. There may be some possibilities here, and if I can find a simple and elegant way I'll add it.

Reg. F7: this is harder, but I'll try. But I do not want to make it "context sensitive", in the sense that <f7> (and similar commands) will work everywhere, even outside the TeX related contexts. Would that still be fine with you?

@bongbang
Copy link
Author

Wow, I didn't expect highlighting to be difficult, and <F7> even more so. I'd hate to trouble you, since I'm apparently the only one with such a need. I probably can even map the <F7> myself in my bib.vim file; I just thought it would be a nice feature for other users.

By "everywhere", do you mean it would work for not just for .tex and .bib files, but other file types as well? That wouldn't be desirable, since the insertion of a TeX command is useful only for TeX editing. But if you just mean everywhere in the .bib file, then I see no problem. In fact, that would be the expected behavior to me.

@lervag
Copy link
Owner

lervag commented Jan 22, 2022

Wow, I didn't expect highlighting to be difficult, and <F7> even more so.

I think <f7> is not going to be as hard as I thought. But highlighting is more complicated, because it requires nested highlighting and we need to properly define the correct syntax regions. This is not trivial. But it shouldn't be impossible either.

I'd hate to trouble you, since I'm apparently the only one with such a need. I probably can even map the <F7> myself in my bib.vim file; I just thought it would be a nice feature for other users.

No problem. Improved syntax highlighting seems useful, and <f7> does not seem to hard.

By "everywhere", do you mean it would work for not just for .tex and .bib files, but other file types as well? That wouldn't be desirable, since the insertion of a TeX command is useful only for TeX editing. But if you just mean everywhere in the .bib file, then I see no problem. In fact, that would be the expected behavior to me.

The latter - everywhere inside the .bib file. The point is that <f7> doesn't make sense except inside the "variable" part of the bib data structure. But if I can ignore that, then it is not so hard.

lervag added a commit that referenced this issue Jan 22, 2022
@lervag
Copy link
Owner

lervag commented Jan 22, 2022

Now <f7> should work, as well as dsc and csc. I'll look into highlighting later - I might actually turn that one down, but we'll see.

@bongbang
Copy link
Author

it requires nested highlighting and we need to properly define the correct syntax regions.

With the caveat that I've never done any Vim programming and may be grossly oversimplifying here, can't the syntax regions be everywhere inside two curly brackets { { ... } }? As for the highlighting itself, it doesn't need to be as extensive or precise as in a .tex file. Even if you just blindly highlight all brackets and \ plus any word attached to it, that would be a huge improvement and probably sufficient. The argument of a command doesn't need to be highlighted. One might even say it shouldn't be, since the marked-up text still belongs to the same field as the text around it. It's just just the command that should be set apart.

  title        = {Le \emph{De Anima} dans la tradition gr{\`e}cque},
  title        = {Bush Says Saddam Would \mkbibquote{Get His Ass Kicked} in Gulf War},
  titleaddon   = {\bibstring{reviewof} \mkbibemph{The Sources of
                  Normativity}, \bibstring{by} {Christine M. Korsgaard}}

@lervag
Copy link
Owner

lervag commented Jan 23, 2022

With the caveat that I've never done any Vim programming and may be grossly oversimplifying here, can't the syntax regions be everywhere inside two curly brackets { { ... } }?

No, it is not that simple. First, we need to ignore the outer @type{ ... }, then we need to ensure that the delimited groups are proper "right-hand sides".

Finally, and "worse": VimTeX does not provide a syntax script for bib files. So if I were to fix this, I would either have to include a syntax script and modify it, or I would need to override the built-in script. Further, a lot of users are now using Neovim with Tree-sitter, and adding this support would add to the existing confusion between syntax scripts. I'm actually not sure if it is worth it.

I'll make a proof of concept and consider it afterwards.

As for the highlighting itself, it doesn't need to be as extensive or precise as in a .tex file. Even if you just blindly highlight all brackets and \ plus any word attached to it, that would be a huge improvement and probably sufficient. The argument of a command doesn't need to be highlighted. One might even say it shouldn't be, since the marked-up text still belongs to the same field as the text around it. It's just just the command that should be set apart.

If I do this, it will be much easier to do it with syntax nesting. And that will give a much better result as well. The "simple" way is not less complicated here, actually.

@lervag
Copy link
Owner

lervag commented Feb 3, 2022

I've given this some more thought. I thinkg I would either have to fully adopt bibtex syntax into VimTeX or do nothing. Considering that bibtex syntax is now supported by Tree-sitter and that the potential gains are not so very high, I believe the best strategy here is to leave this as it is.

However, if you really want this, you could "make your own". Create ~/.vim/after/syntax/bib.vim (or similar with ~/.config/nvim/... for neovim) with the following content:

if !get(b:, "current_syntax", '') ==# 'bib' | finish | endif
if exists("b:loaded_bib_vimtex_syntax") | finish | endif
let b:loaded_bib_vimtex_syntax = 1

unlet b:current_syntax
syntax include @vimtex_nested_tex syntax/tex.vim
let b:current_syntax = 'bib'

syntax cluster vimtex_nested_tex remove=texGroup,texGroupError
syntax cluster bibVarContents add=@vimtex_nested_tex

I've tested it and it seems to work as expected.

@lervag lervag closed this as completed Feb 3, 2022
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

2 participants