1
  1. how to have foldtext() for each filetype, because each filetype has a different kind of fold so multiple foldtext() in vimrc and enable for each filetype.

  2. I'm thinking about multiple foldtext() functions in vimrc and enable that customfunc() to foldtext() via local of au command for that filetype. Is it possible and how would be an elegant way to achieve this?

EDIT: from @D.BenKnobl comment, I make it more visual, I'm not good with VimL and still the examples are pretty clear, I still need some clarification, so I'll add what I think and do here:

vim files in /after/ftplugin will alter the default behavior of vim filetype or append function. Because I don't see the calling of function in folding.vim, I understand that vim will automatically recognize that folding.vim contains all things for foldtext() or the whole file is foldtext() function and will be overwritten or alter the default foldtext() function for that filetype.

In vim/after/ftplugin/markdown/folding.vim I see MarkdownFoldText function that make firm about above conclusion. Basically setlocal foldtext(), define MyMarkdownFoldFtpluginUndo and let b:undo_ftplugin once leave that filetype with set local foldtext to <. Do I understand that correct?

So as my understanding, I should put foldtext() in vim/after/ftplugin/rust.vim like below:

if exists('b:undo_ftplugin') let b:undo_ftplugin .= ' | ' else let b:undo_ftplugin = '' endif " This is where my local foldtext() function for rust.vim foldtext() <...> if !exists("*MyRustFtpluginUndo") function MyCFtpluginUndo() setlocal tags< setlocal path< silent! nunmap <buffer> <LocalLeader>tr endfunction endif let b:undo_ftplugin .= 'call MyRustFtpluginUndo()' 
4
  • Suggestion for answerers and OP: use filetype plugin structure for setting filetype-based things. Commented Nov 4, 2018 at 20:00
  • @D.BenKnoble: Does that mean when I leave that filetype, foldtext will be reset to default with filetype that not specify the foldtext() in their filetype plugin? Commented Nov 5, 2018 at 1:23
  • You can ensure that by setting b:undo_ftplugin properly. I use a sort of template for that; you can find examples in my dotfiles Commented Nov 5, 2018 at 1:26
  • @D.BenKnoble: Thanks, I've just added to 1st post. Can you help me to review? Commented Nov 5, 2018 at 2:53

1 Answer 1

2

Example

" ~/.vim/after/ftplugin/rust.vim if exists('b:undo_ftplugin') let b:undo_ftplugin .= ' | ' else let b:undo_ftplugin = '' endif function! RustFoldText() " cf. :help fold-foldtext return "text to display for foldtext" endfunction setlocal foldtext=RustFoldText() if !exists("*MyRustFtpluginUndo") function MyRustFtpluginUndo() " reset options, undefine functions/commands, etc setlocal foldtext< endfunction endif let b:undo_ftplugin .= 'call MyRustFtpluginUndo()' 

Explanation of example

First, the file: ~/.vim/after/ftplugin/<filetype>.vim can contain custom vimscript to execute for filetypes. cf. :help ftplugin, :help after

The b:undo_ftplugin boilerplate is responsible coding; it ensures that, when switching filetypes (such as editing a new buffer), vim restores changed options to sensible values. You can also use it to clean up cruft (like removing mappings). cf. :help b:undo_ftplugin.

The meat is of course the function RustFoldText, which the option 'foldtext' takes on locally. I've never written a foldtext function, but the vim help doc I reference above has good information on how to. So does this site, probably. It must return the text for the fold, and can make use of some magic variables to assist with that. cf. :help fold-foldtext, :help setlocal


Notes

I'm not sure if foldtext will allow s: script-local functions or not; you could experiment with that by using function! s:RustFoldText() and setlocal foldtext=<SID>RustFoldText().

4
  • foldtext() must be global, just enable fold in vimrc locally for rust filetype. Thanks, the question's answered, but I have another, how to persist hi for fold of each filetype, I find that each fold needs different highlight style for it. I'll open another question. Commented Nov 5, 2018 at 3:40
  • foldtext is a local option in vim, so not sure what you mean by must be global (windows for which it is not globally set retain the default). Do ask another question for your second one. Commented Nov 5, 2018 at 3:46
  • I see, so each object in vim is local meaning we can use setlocal to map it to a local variable? I setlocal foldtext=RustFoldText() and it only works this way, plus s:RustFoldText() in define the function and setlocal make RustFoldText isn't applied. Commented Nov 5, 2018 at 3:51
  • I’m not sure i quite follow what youre saying... take a look at the help in vim and see if you can answer your questions Commented Nov 5, 2018 at 4:23

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.