+ " If there's more than one buffer return "/<nr>" (e.g. "/05") where <nr>
+ " is the highest buffer number, otherwise return nothing. Used in
+ " 'statusline' to get an overview of available buffer numbers.
+ function! s:StatuslineBufferCount()
+ let l:bufnr = bufnr('$')
+ if l:bufnr > 1
+ let l:result = '/'
+ if exists('*printf')
+ let l:result .= printf('%02d', l:bufnr)
+ else
+ " Older Vims don't have printf() (and no .= either). Emulate
+ " "%02d".
+ if l:bufnr < 10
+ let l:result = l:result . '0'
+ endif
+ let l:result = l:result . l:bufnr
+ endif
+ return l:result
+ else
+ return ''
+ endif
+ endfunction
+
+ " Like %f but use relative filename if it's shorter than the absolute path
+ " (e.g. '../../file' vs. '~/long/path/to/file'). fnamemodify()'s ':.' is
+ " not enough because it doesn't create '../'s.
+ function! s:StatuslineRelativeFilename()
+ " Display only filename for help files.
+ if &buftype == 'help'
+ return expand('%:t')
+ endif
+ " Special case for scratch files.
+ if &buftype == 'nofile'
+ return '[Scratch]'
+ endif
+
+ let l:path = expand('%')
+ " No file.
+ if l:path == ''
+ return '[No Name]'
+ endif
+ " Path is already relative, nothing to do.
+ if stridx(l:path, '/') != 0
+ return l:path
+ endif
+
+ " Absolute path to this file.
+ let l:path = expand('%:p')
+ " Shortened path to this file, thanks to bairui in #vim on Freenode
+ " (2012-06-23 00:54) for the tip to use fnamemodify(). This is what
+ " Vim normally uses as %f (minus some exceptions).
+ let l:original_path = fnamemodify(l:path, ':~')
+ " Absolute path to the current working directory.
+ let l:cwd = getcwd()
+
+ " Working directory completely contained in path, replace it with a
+ " relative path. Happens for example when opening a file with netrw.
+ " %f displays this as absolute path, but we want a relative path of
+ " course.
+ if stridx(l:path, l:cwd) == 0
+ return strpart(l:path, strlen(l:cwd) + 1)
+ endif
+
+ let l:path_list = split(l:path, '/')
+ let l:cwd_list = split(l:cwd, '/')
+
+ " Remove the common path.
+ while l:path_list[0] == l:cwd_list[0]
+ call remove(l:path_list, 0)
+ call remove(l:cwd_list, 0)
+ endwhile
+
+ " Add as many '..' as necessary for the relative path and join the
+ " path. Thanks to Raimondi in #vim on Freenode (2012-06-23 01:13) for
+ " the hint to use repeat() instead of a loop.
+ let l:path = repeat('../', len(l:cwd_list)) . join(l:path_list, '/')
+
+ " Use the shorter path, either relative or absolute.
+ if strlen(l:path) < strlen(l:original_path)
+ return l:path
+ else
+ return l:original_path
+ endif
+ endfunction
+
+ " Display unexpected 'fileformat', 'fileencoding' and 'bomb' settings.
+ function! s:StatuslineFileFormat()
+ if &fileformat != 'unix'
+ return '[' . &fileformat . ']'
+ else
+ return ''
+ endif
+ endfunction
+ function! s:StatuslineFileEncoding()
+ if &fileencoding != '' && &fileencoding != 'utf-8'
+ \ && &filetype != 'help'
+ return '[' . &fileencoding . ']'
+ else
+ return ''
+ endif
+ endfunction
+ function! s:StatuslineFileBOMB()
+ if exists('+bomb') && &bomb
+ return '[BOM]'
+ else
+ return ''
+ endif
+ endfunction
+
+ " Return current syntax group in brackets or nothing if there's none.
+ function! s:StatuslineSyntaxGroup()
+ let l:group = synIDattr(synID(line('.'), col('.'), 1), 'name')
+ if l:group != ''
+ return '[' . l:group . '] '
+ else
+ return ''
+ endif
+ endfunction
+
+ " Short function names to make 'statusline' more readable.
+ function! SBC()
+ return s:StatuslineBufferCount()
+ endfunction
+ function! SRF()
+ return s:StatuslineRelativeFilename()
+ endfunction
+ function! SFF()
+ return s:StatuslineFileFormat()
+ endfunction
+ function! SFE()
+ return s:StatuslineFileEncoding()
+ endfunction
+ function! SFB()
+ return s:StatuslineFileBOMB()
+ endfunction
+ function! SSG()
+ return s:StatuslineSyntaxGroup()
+ endfunction
+