1

I often find myself patching projects that add a relatively big (23 lines out of a 37-line file? ) license-header to every. single. file.

Is there a plugin or straightforward way I can add a global pattern-match to all filetypes that will turn that entire comment into a fold, allowing me to ggzc it out of view no matter what I'm editing?

(The complexity here is probably to do with various commenting-characters in different languages, although I know Vim already has some level of built-in support for that ...)

2
  • 2
    You can insert modeline with foldmethod=marker. But what if you want automatic folds for the rest of the file?
    – Matt
    Feb 5, 2020 at 18:29
  • 3
    I wonder if you could construct a long syntax-match for the license text (ignoring newlines and comment characters) and then use conceal cchar=L...
    – D. Ben Knoble
    Feb 5, 2020 at 19:45

1 Answer 1

1

Ok, so I have a prototype of a license concealer (not fold) written, but I think because of the way conceal and cchar work, I can't get the lines to collapse into one. For that, folding may be better.

A quick example, that doesn't work with comments (yet):

let text =<< trim END
MIT License

Copyright (c) 2016 David Ben Knoble

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
END

let text = text->copy()->filter({s -> !match(s, '\s*')})->join('\%(\s\|\n\)*')

exec printf('syn match license /%s/ conceal cchar=L', text->escape('/'))

3
  • Indeed, folding would be better. The issue is that it would break any other folding for filetypes... or is there a way to make it "nested"? Call myfold() and then Fold() of the filetype?
    – Maxim Kim
    Feb 6, 2020 at 15:49
  • Im really not sure @MaximKim
    – D. Ben Knoble
    Feb 6, 2020 at 16:49
  • This is pretty cool! It feels like you're getting close! Feb 10, 2020 at 6:07

Your Answer

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

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