0

With this question I've learned of the existence of hlget and hlID, which I use in a plugin of mine like this:

if empty(hlget('SoftWrapHighlightGroup'))
  " do stuff
endif

However, those funcitons have existed for not a very long time (well, the reference is the Debian machine on which I work), so I can't use them in Vim < 8.2.3917.

What options do I have?

2
  • I am confused now. I thought the issue was basically because you were using the -> method script syntax, but that does not seem to be the case. It looks like the problem is the use of hlget() which was added in Vim Patch 8.2.3578. But you just want to check for the existence of a highlighting group, for which you can simply use hlexists(), that should be available even in old vims Jan 11, 2023 at 9:41
  • @ChristianBrabandt, I thought that those funcitons have existed for not a very long time made clear the problem was about hlget not being available in a given version of Vim. Yes, the -> has nothing to do with the problem. As regards when hlget was introduced, yeah, I got the patch number wrong. And I didn't know of hlexists. This last bit is a actually worth an answer. Feel free to post it and I'll accept.
    – Enlico
    Jan 11, 2023 at 10:20

2 Answers 2

1

So Vim Patch 8.2.3578 added the ability to get and set the various attributes for highlighting groups in a more efficient manner.

This is strictly speaking not required to check whether or not a highlighting group exists, for which Vim has already before that patch provided a separate VimScript function: hlexists()

So in your case, you can simply test:

if hlexists('SoftWrapHighlightGroup')
   " do something
else
   " do something else
endif

This function seems to exists for a very long time. I could find it back in Vim 5.1 in the vim-history repository being documented and before that, the function was just highlight_exists().

And Vim 5.1 has been released around April 1998 (!) as you can see here (copied here a condensed table for your information):

Version Release-Date
9.0 28th, June 2022
8.2 12th, December, 2019
8.1 17th May, 2018
8.0 12th September, 2016
7.4 10th August, 2013
7.3 15th August, 2010
7.2 9th August, 2008
7.1 12th May, 2007
7.0 8th May, 2006
6.4 15th October, 2005
6.3 8th June 2004
6.2 1st June 2003
6.1 24th March 2002
6.0 27th September, 2001
5.8 31st May, 2001
5.7 24th June, 2000
5.6 16th January, 2000
5.5 21st September, 1999
5.4 26th July, 1999
5.3 31st August, 1998
5.2 24th August, 1998
5.1 7th April, 1998
5.0 19th February, 1998
4.6 13th March, 1997
4.5 17th October, 1996
4.2 5th July, 1996
4.0 21st May, 1996
3.0 16th August, 1994
2.0 21st December, 1993
1.27 23rd April, 1993
1.24 5 Jan 1993
1.17 21st April, 1992
1.14 2nd November, 1991
1
  • 1
    I should have just scrolled a few lines above hlget() :(
    – Enlico
    Jan 11, 2023 at 13:41
0

I've decided to branch on v:versionlong and, in case of old versions, I'm retrieving what hlget would return (actually hlget(bla)->empty(), because that's all I need).

Here's what I've come up with:

function! s:IsSoftWrapHighlightGroupSet()
  if v:versionlong >= 8023917
    return !empty(hlget('SoftWrapHighlightGroup'))
  else
    try
      silent highlight SoftWrapHighlightGroup
      return v:true
    catch
      return v:false
    endtry
  endif
endfunction

if !s:IsSoftWrapHighlightGroupSet()
  " do stuff
endif

I wonder whether there are any other approaches that would make the code shorter? Not for the sake of being shorter, but of learning something else. Hence I'll not self accept my answer for a while.

5
  • Was echo empty(hlget('SoftWrapHighlightGroup')) too easy? Jan 11, 2023 at 7:33
  • @ChristianBrabandt, I don't understand. That command in place of what?
    – Enlico
    Jan 11, 2023 at 7:40
  • instead of using -> method syntax, simply nest those two functions Jan 11, 2023 at 8:04
  • Ok, sorry, since you wrote echo instead of return, I thought you were referring to an alternative to the content of the try. Anyway, yes, can do that.
    – Enlico
    Jan 11, 2023 at 8:20
  • that doesn't really matter. I just use echo to easily run the command interactively in the command line Jan 11, 2023 at 8:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.