extensions: Allow customization of filetypes

Define the variable :let g:airline_filetype_overrides to allow for
specific customization how a specific filetype will be displayed.

For e.g. a German display, you can use: >

    :let g:airline_filetype_overrides = {'help': ['Hilfe', ':%f']}
<

This defines how section a and b will look like for a window with that
specific filetype.

Closes #1888
This commit is contained in:
Christian Brabandt 2019-04-30 17:06:08 +02:00
parent 170b6d4c26
commit 7469672a46
No known key found for this signature in database
GPG Key ID: F3F92DA383FDDE09
2 changed files with 32 additions and 15 deletions

View File

@ -23,12 +23,15 @@ endfunction
let s:script_path = tolower(resolve(expand('<sfile>:p:h'))) let s:script_path = tolower(resolve(expand('<sfile>:p:h')))
let s:filetype_overrides = { let s:filetype_overrides = {
\ 'defx': ['defx', '%{b:defx.paths[0]}'],
\ 'gundo': [ 'Gundo', '' ], \ 'gundo': [ 'Gundo', '' ],
\ 'help': [ 'Help', '%f' ],
\ 'minibufexpl': [ 'MiniBufExplorer', '' ], \ 'minibufexpl': [ 'MiniBufExplorer', '' ],
\ 'nerdtree': [ get(g:, 'NERDTreeStatusline', 'NERD'), '' ], \ 'nerdtree': [ get(g:, 'NERDTreeStatusline', 'NERD'), '' ],
\ 'startify': [ 'startify', '' ], \ 'startify': [ 'startify', '' ],
\ 'vim-plug': [ 'Plugins', '' ], \ 'vim-plug': [ 'Plugins', '' ],
\ 'vimfiler': [ 'vimfiler', '%{vimfiler#get_status_string()}' ], \ 'vimfiler': [ 'vimfiler', '%{vimfiler#get_status_string()}' ],
\ 'vimshell': ['vimshell','%{vimshell#get_status_string()}'],
\ } \ }
let s:filetype_regex_overrides = {} let s:filetype_regex_overrides = {}
@ -58,18 +61,13 @@ function! airline#extensions#apply_left_override(section1, section2)
endfunction endfunction
function! airline#extensions#apply(...) function! airline#extensions#apply(...)
let filetype_overrides = get(s:, 'filetype_overrides', {})
call extend(filetype_overrides, get(g:, 'airline_filetype_overrides', {}), 'force')
if s:is_excluded_window() if s:is_excluded_window()
return -1 return -1
endif endif
if &buftype == 'help'
call airline#extensions#apply_left_override('Help', '%f')
let w:airline_section_x = ''
let w:airline_section_y = ''
let w:airline_render_right = 1
endif
if &buftype == 'terminal' if &buftype == 'terminal'
let w:airline_section_x = '' let w:airline_section_x = ''
let w:airline_section_y = '' let w:airline_section_y = ''
@ -81,11 +79,20 @@ function! airline#extensions#apply(...)
let w:airline_section_c = bufname(winbufnr(winnr())) let w:airline_section_c = bufname(winbufnr(winnr()))
endif endif
if has_key(s:filetype_overrides, &ft) if has_key(filetype_overrides, &ft) &&
let args = s:filetype_overrides[&ft] \ ((&filetype == 'help' && &buftype == 'help') || &filetype !~ 'help')
" for help files only override it, if the buftype is also of type 'help',
" else it would trigger when editing Vim help files
let args = filetype_overrides[&ft]
call airline#extensions#apply_left_override(args[0], args[1]) call airline#extensions#apply_left_override(args[0], args[1])
endif endif
if &buftype == 'help'
let w:airline_section_x = ''
let w:airline_section_y = ''
let w:airline_render_right = 1
endif
for item in items(s:filetype_regex_overrides) for item in items(s:filetype_regex_overrides)
if match(&ft, item[0]) >= 0 if match(&ft, item[0]) >= 0
call airline#extensions#apply_left_override(item[1][0], item[1][1]) call airline#extensions#apply_left_override(item[1][0], item[1][1])
@ -213,14 +220,9 @@ function! airline#extensions#load()
endif endif
if exists(':VimShell') if exists(':VimShell')
let s:filetype_overrides['vimshell'] = ['vimshell','%{vimshell#get_status_string()}']
let s:filetype_regex_overrides['^int-'] = ['vimshell','%{substitute(&ft, "int-", "", "")}'] let s:filetype_regex_overrides['^int-'] = ['vimshell','%{substitute(&ft, "int-", "", "")}']
endif endif
if exists(':Defx')
let s:filetype_overrides['defx'] = ['defx', '%{b:defx.paths[0]}']
endif
if get(g:, 'airline#extensions#branch#enabled', 1) && ( if get(g:, 'airline#extensions#branch#enabled', 1) && (
\ airline#util#has_fugitive() || \ airline#util#has_fugitive() ||
\ airline#util#has_lawrencium() || \ airline#util#has_lawrencium() ||

View File

@ -203,12 +203,27 @@ values):
statusline modified > statusline modified >
let g:airline_exclude_filetypes = [] " see source for current list let g:airline_exclude_filetypes = [] " see source for current list
< <
* define the set of names to be displayed instead of a specific filetypes (for
section a and b): >
let g:airline_filetype_overrides = {
\ 'defx': ['defx', '%{b:defx.paths[0]}'],
\ 'gundo': [ 'Gundo', '' ],
\ 'help': [ 'Help', '%f' ],
\ 'minibufexpl': [ 'MiniBufExplorer', '' ],
\ 'nerdtree': [ get(g:, 'NERDTreeStatusline', 'NERD'), '' ],
\ 'startify': [ 'startify', '' ],
\ 'vim-plug': [ 'Plugins', '' ],
\ 'vimfiler': [ 'vimfiler', '%{vimfiler#get_status_string()}' ],
\ 'vimshell': ['vimshell','%{vimshell#get_status_string()}'],
\ }
<
* defines whether the preview window should be excluded from have its window * defines whether the preview window should be excluded from have its window
statusline modified (may help with plugins which use the preview window statusline modified (may help with plugins which use the preview window
heavily) > heavily) >
let g:airline_exclude_preview = 0 let g:airline_exclude_preview = 0
< <
* disable the Airline customization for selective windows (this is a * disable the Airline customization for selected windows (this is a
window-local variable so you can disable it per-window) > window-local variable so you can disable it per-window) >
let w:airline_disabled = 1 let w:airline_disabled = 1