Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What in the world does this vimscript do?

Tags:

vim

haskell

Full source here: http://www.vim.org/scripts/download_script.php?src_id=10391

The line is:

    silent %s/[^λ←→≲≳≡≠⇒»∙∀\\\-!#$%&*+/<=>?@\^|~.]\@<=\\\([^λ←→≲≳≡≠⇒»∙∀\\\-!#$%&*+/<=>\?@\^|~.]\)/λ\1/eg

Someone please decipher this for me. The larger script is intended to unicode-ify some operators in Haskell into more familiar mathematical equivalents.

like image 239
me2 Avatar asked Aug 16 '26 13:08

me2


1 Answers

As the author of that line, I can translate. Complicated regular expressions are often 'write-only' and this relies on a vim regex extension.

The purpose of this is to make sure that it doesn't do the replacement of \ with a pretty printed λ in the middle of an operator like \\.

It checks to make sure that the character that precedes us in the buffer is not a valid operator symbol (the meaning of everything everything up to the \@<=). The \@<= is a 'zero width match look behind', which only succeeds if the stuff to the left of it occurs, but doesn't include it in the resulting match.

And then the \([^...]\) part checks to make sure that the stuff that follows us is a non-symbol as well, in which case, we match it, and then include it in the output thanks to the \1 in the result.

Note, this isn't perfect. Unfortunately, It will still replace backslashes inside of strings, but it works rather well.

like image 199
Edward Kmett Avatar answered Aug 19 '26 04:08

Edward Kmett



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!