I currently have a skeleton file so that whenever I create a new .cpp file, it copies the c++_template.cpp to the new file. I'm using this line in my .vimrc:

autocmd BufNewFile *.cpp 0r ~/workspace_c++/c++_template.cpp

I would like it to also copy the foldings. Is that possible?

  • 2
    You could embed folds in the comments of your template, would that be acceptable? – Tumbler41 Oct 9 '17 at 16:46
  • @Tumbler41 I could do that if there are no other solution, but I would prefer to not embed the folds in comments. – Nerarith Oct 9 '17 at 17:18

Folding will be copied if it's reproducible. If you're using marker, indent, syntax or expr folding methods, it should be reproducible automatically.

If you're using manual, well, it means your template expander plugin will need to interpret some information and use it to fold manually.

I never though about it, but I guess it should be possible to register such a behaviour in my fork of mu-template. In it I can register post-expansion hooks -- that I mainly use to add missing import/include statements. If in that hooks we create manual folds. It should work. The tricky part would be the extraction of the final line numbers to call :l1,l2fold.

Something like the following will work in simple cases -- if you start to mix this with other hooks (to automate the inclusion of headers for instance), it'll quite likely require more wiring.

MuT: let s:fold_start = s:Line()
for (auto&& <+e+> : <+container+>) {
   <+code+>
}
MuT: let s:fold_end = s:Line() - 1
VimL: call s:AddPostExpandCallback('execute(s:fold_start.",".s:fold_end."fold")')

Regarding the syntax used here.

  • <+ and +> delimit placeholders. Mu-template will try to evaluate it's content, otherwise they are inserted as placeholders
  • lines starting with :VimL are interpreted as Vim commands (that get interpreted with the :execute command)
  • lines starting with :MuT are interpreted by mu-template engine, I use them to conditionally decide what to do, in this is case to define a variable that'll get automatically undefined at the end of the expansion.
  • all the s: functions are helper functions already implemented in mu-template that we can use to define more complex template/snippets: s:Line() returns the line number where the current line in the template line will be actually inserted/expanded ; s:AddPostExpandCallback() registers a hook that'll get executed after the expansion has been completed.
  • otherwise, the text we type is directly inserted when the template file is expanded.
  • I'm currently using manual folds and I have no template expander plugin, I'm just using the line in my question above. Is your code for your fork of mu-template? I don't understand haw it works, can you explain? – Nerarith Oct 9 '17 at 18:06
  • Indeed this is a skeleton for mu-template. It has a few syntaxic elements that permits to do much mote than just inserting raw text. I'll need to check the exact syntax, I can't promise right now that it'll work. – Luc Hermitte Oct 9 '17 at 18:09

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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