vim-maktaba icon indicating copy to clipboard operation
vim-maktaba copied to clipboard

Implement a hook for "filetype known"

Open dbarnett opened this issue 11 years ago • 1 comments

If a plugin wants to have configuration based on a buffer's filetype, it turns out to be tricky to handle if you need to explicitly handle the case where filetype wasn't set. You can use "autocmd FileType", but that will never fire for a new buffer with no filetype, and can fire multiple times for a single new buffer if autocmds are overriding filetype on each other.

It would be useful to have a hook for code that needs to read the 'filetype' setting and expects it to already be valid even for no-filetype buffers. This hook should fire:

  • Once after all filetypedetect autocmds have fired (i.e., after the last BufRead or BufNewFile autocmd).
  • Any time the filetype changes afterwards. This should avoid firing multiple times for the same event if autocmds set and then override the filetype (especially for the initial filetype detection, but also for later events if possible).

Ideally, this would be implemented inside vim, and might be tricky or impossible to implement properly in maktaba, but it's worth discussing.

dbarnett avatar Nov 09 '14 05:11 dbarnett

A few terrible ways I can imagine for implementing this:

  • Use a BufReadPre autocmd to add a BufRead autocmd immediately before the event fires so it's the last event executed and can assume filetype has already stabilized.
  • Force filetype detection with ":filetype detect" before checking 'filetype' (could be expensive).
  • Find another event or combination of events that fires after 'filetype' changes.

A really bad example of the last point for illustration purposes:

autocmd BufRead,BufNewFile,FileType * let g:ft_related_event = 1
autocmd CursorHold,InsertChange * if g:ft_related_event |
    \ call FiletypeKnownHook() | let g:ft_related_event = 0 | endif

dbarnett avatar Nov 13 '14 16:11 dbarnett