Wednesday, September 19, 2012

Re: FileTypePre autocmd

On 20/09/12 06:14, Fermat 618 wrote:
> On Wednesday, September 19, 2012 11:38:42 PM UTC+8, Ben Fritz wrote:
>> On Wednesday, September 19, 2012 10:36:05 AM UTC-5, Ben Fritz wrote:
>>
>>> On Wednesday, September 19, 2012 2:04:34 AM UTC-5, Fermat 618 wrote:
>>
>>>
>>
>>>> I tried to do some clean-up work when using
>>
>>>
>>
>>>>
>>
>>>
>>
>>>>
>>
>>>
>>
>>>>
>>
>>>
>>
>>>> :setfiletype another_file_type
>>
>>>
>>
>>>>
>>
>>>
>>
>>>>
>>
>>>
>>
>>>>
>>
>>>
>>
>>>> command, but didn't manage to find a FileTypePre auto command.
>>
>>>
>>
>>>>
>>
>>>
>>
>>>>
>>
>>>
>>
>>>>
>>
>>>
>>
>>>> Is there anyway to do clean-up work before a set it to another filetype?
>>
>>>
>>
>>>
>>
>>>
>>
>>> :setfiletype will not not change the filetype if it's already set
>>
>>
>>
>> ...or so I thought, except :help undo_ftplugin specifically mentions :setfiletype, and :help :setfiletype mentions a specific context of inside a nested autocmd. Experimentation shows that it will in fact override the filetype whenever it's used directly.
>
> Thanks, but actually what I want to do is not about a specific filetype, but
> all types.
>
> I used BufEnter and BufLeave event autocmd to handle filetype specific menus
> add and remove, but that failed when I set a buffer to another filetype. The
> autotocmd should be executed in a context that the filetype is the old one.
>
> I read the $VIMRUNTIME/ftplugin.vim where the b:undo_ftplugin take effects, but
> that is also trigger by the FileType event, where the old context is left.
> Therefore event if I add my clean up code to b:undo_ftplugin, (which is not
> that esay), it will not work.
>
> Information about what the old filetype is is needed.
>
> Best regards,
> Fermat
>

If you explained what you want to do, in detail and with examples, we
might be able to understand, and maybe even to find a _different_
solution. Until now, all we know is that you want to do some cleanup
immediately before leaving a filetype. Now what we would expect is that
the cleanup would differ from one filetype to the next, and for that the
b:undo_ftplugin variable is quite adequate: it should be set by the
respective ftplugin when setting the filetype, and Vim will use it for
cleanup when unloading that buffer (or when changing the filetype)

A filetype will never be unset without having first been set. You might,
maybe, set an autocommand for the VimEnter event, to define another
autocommand for the FileType event (Vim Enter is fired at the end of
startup, so that FileType autocommand would be triggered last, after any
filetype-plugin has been run), maybe something like (untested)

au VimEnter * au FileType *
\ call s:DoFiletype() |
\ let b:save_undo_ftp = b:undo_ftplugin |
\ let b:undo_ftplugin =
\ 'call s:UndoFiletype() | exe b:save_undo_ftp'
function s:DoFiletype()
" … do something …
endfunction
function s:UndoFiletype()
" … undo the above …
endfunction


Best regards,
Tony.
--
"Microwave oven? Whaddya mean, it's a microwave oven? I've been
watching Channel 4 on the thing for two weeks."

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments:

Post a Comment