Thursday, September 20, 2012

Re: FileTypePre autocmd

On Thu, September 20, 2012 09:30, Fermat618 wrote:
> I wrote a small plugin in python to handle filetype specific menu auto
> add and auto remove.
>
> As long as I put
>
> py3 register_filetype_menu('python', VimMenu('Python.Foo', ':call
> bar()<CR>'))
>
> in my .vimrc file, a menu 'Python' will and will only appear when my
> current buffer is
> has 'python' filetype, which means, e.g., when I leave a python buffer
> and
> enter a C buffer, Python menu whill disappear and a C menu will appear.
>
> autocmd BufEnter * :py3 handle_menu_add()
> autocmd BufLeave * :py3 handle_menu_remove()
>
> The above autocmd are what I used. handle_menu_add() function will
> detect the current filetype and get the menu informations of that
> filetype which I stored with register_filetype_menu() function, then add
> those menus.
>
> There should be enough information of what filetype I'm leaving and what
> I'm entering, to remove old menus and add new menus.
>
> This fails when resetting the filetype of a buffer, because I don't know
> how to get the previous filetype of the buffer.

Programmatically, you can get the previous filetype, by recording each
filetype (e.g. this should work to always see the previous filetype):

let g:filetype_stack = []
au FileType * echo get(g:filetype_stack, -1, '') |
\ call add(g:filetype_stack, expand("<amatch>"))

and then in your plugin check g:filetype_stack[-1] or -2 (depending on
whether your Filetype autocommand is called before or after the above
filetype autocommand).

BTW: I would not mix different filetype settings in one single plugin,
but put each filetype specific plugin below a separate
~/.vim/ftplugin/<filetype>.vim (where <filetype> stands for your
actual filetype, as you probably guessed) see also :h filetype-plugin

Here is what I do, in my csv filetype plugin:
https://github.com/chrisbra/csv.vim/blob/master/ftplugin/csv.vim#L247

regards,
Christian

--
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