Skip to main content
added 645 characters in body
Source Link
Luc Hermitte
  • 17.8k
  • 1
  • 33
  • 50

IOW, the s:ls_project function shall become something like this (untested).

function! s:ls() abort " {{{2
  let buffers = filter(range(1, bufnr('$')), 'bufexists(v:val)')
  let lines = map(copy(buffers), 's:As_ls(v:val)')
  echo "Buffer list:"
  echo join(lines, "\n")
endfunction

command! -nargs=0 LS call s:ls()
" or with a mapping
nnoremap <leader>r :<c-u>call <sid>ls()<cr>

And then you'll just have to play with s:As_ls() code to make it produce what you wish to see. a:bid is the formal parameter holding the buffer id, and to know what each function does, see for instance :h buflisted(), and so on.

IOW, the s:ls_project function shall become something like this (untested).

function! s:ls() abort " {{{2
  let buffers = filter(range(1, bufnr('$')), 'bufexists(v:val)')
  let lines = map(copy(buffers), 's:As_ls(v:val)')
  echo "Buffer list:"
  echo join(lines, "\n")
endfunction

command! -nargs=0 LS call s:ls()
" or with a mapping
nnoremap <leader>r :<c-u>call <sid>ls()<cr>

And then you'll just have to play with s:As_ls() code to make it produce what you wish to see. a:bid is the formal parameter holding the buffer id, and to know what each function does, see for instance :h buflisted(), and so on.

added 2 characters in body
Source Link
Luc Hermitte
  • 17.8k
  • 1
  • 33
  • 50

In my library plugin, I have a :Project ls feature that lists only the files that belongs to the current project as :ls would have listed them.

The related (GPL w/ exception) code looks is the following:

function! s:As_ls(bid) abort " {{{2
  let name = bufname(a:bid)
  if empty(name)
    let name = 'Used to be known as: '.lh#project#__buffer(a:bid)
  endif
  return printf('%3d%s %s'
        \ , a:bid
        \ , (buflisted(a:bid) ? ' ' : 'u')
        \ . (bufnr('%') == a:bid ? '%' : bufnr('#') == a:bid ? '#' : ' ')
        \ . (! bufloaded(a:bid) ? ' ' : bufwinnr(a:bid)<0 ? 'h' : 'a')
        \ . (! getbufvar(a:bid, "&modifiable") ? '-' : getbufvar(a:bid, "&readonly") ? '=' : ' ')
        \ . (getbufvar(a:bid, "&modified") ? '+' : ' ')
        \ , '"'.name.'"')
endfunction

function! s:ls_project(prj) abort " {{{2
  if lh#option#is_unset(a:prj)
    echo '(no project specified!)'
  endif
  let lines = map(copy(a:prj.buffers), 's:As_ls(v:val)')
  echo "Buffer list of ".get(a:prj, 'name', '(unnamed)')." project:"
  echo join(lines, "\n")
endfunction

Instead of a:prj.buffers, you would have the list of buffers in range(1, bufnr('$')). It would then need to be filtered to keep only the existing buffers (filter(range(1, bufnr('$')), 'bufexists(v:val)')).

Then you could wrap the call to bufname() with fnamemodify() to display exactly what you need.

In my library plugin, I have a :Project ls feature that lists only the files that belongs to the current project as :ls would have listed them.

The (GPL w/ exception) code looks is the following:

function! s:As_ls(bid) abort " {{{2
  let name = bufname(a:bid)
  if empty(name)
    let name = 'Used to be known as: '.lh#project#__buffer(a:bid)
  endif
  return printf('%3d%s %s'
        \ , a:bid
        \ , (buflisted(a:bid) ? ' ' : 'u')
        \ . (bufnr('%') == a:bid ? '%' : bufnr('#') == a:bid ? '#' : ' ')
        \ . (! bufloaded(a:bid) ? ' ' : bufwinnr(a:bid)<0 ? 'h' : 'a')
        \ . (! getbufvar(a:bid, "&modifiable") ? '-' : getbufvar(a:bid, "&readonly") ? '=' : ' ')
        \ . (getbufvar(a:bid, "&modified") ? '+' : ' ')
        \ , '"'.name.'"')
endfunction

function! s:ls_project(prj) abort " {{{2
  if lh#option#is_unset(a:prj)
    echo '(no project specified!)'
  endif
  let lines = map(copy(a:prj.buffers), 's:As_ls(v:val)')
  echo "Buffer list of ".get(a:prj, 'name', '(unnamed)')." project:"
  echo join(lines, "\n")
endfunction

Instead of a:prj.buffers, you would have the list of buffers in range(1, bufnr('$')). It would then need to be filtered to keep only the existing buffers (filter(range(1, bufnr('$')), 'bufexists(v:val)')).

Then you could wrap the call to bufname() with fnamemodify() to display exactly what you need.

In my library plugin, I have a :Project ls feature that lists only the files that belongs to the current project as :ls would have listed them.

The related (GPL w/ exception) code is the following:

function! s:As_ls(bid) abort " {{{2
  let name = bufname(a:bid)
  if empty(name)
    let name = 'Used to be known as: '.lh#project#__buffer(a:bid)
  endif
  return printf('%3d%s %s'
        \ , a:bid
        \ , (buflisted(a:bid) ? ' ' : 'u')
        \ . (bufnr('%') == a:bid ? '%' : bufnr('#') == a:bid ? '#' : ' ')
        \ . (! bufloaded(a:bid) ? ' ' : bufwinnr(a:bid)<0 ? 'h' : 'a')
        \ . (! getbufvar(a:bid, "&modifiable") ? '-' : getbufvar(a:bid, "&readonly") ? '=' : ' ')
        \ . (getbufvar(a:bid, "&modified") ? '+' : ' ')
        \ , '"'.name.'"')
endfunction

function! s:ls_project(prj) abort " {{{2
  if lh#option#is_unset(a:prj)
    echo '(no project specified!)'
  endif
  let lines = map(copy(a:prj.buffers), 's:As_ls(v:val)')
  echo "Buffer list of ".get(a:prj, 'name', '(unnamed)')." project:"
  echo join(lines, "\n")
endfunction

Instead of a:prj.buffers, you would have the list of buffers in range(1, bufnr('$')). It would then need to be filtered to keep only the existing buffers (filter(range(1, bufnr('$')), 'bufexists(v:val)')).

Then you could wrap the call to bufname() with fnamemodify() to display exactly what you need.

Source Link
Luc Hermitte
  • 17.8k
  • 1
  • 33
  • 50

In my library plugin, I have a :Project ls feature that lists only the files that belongs to the current project as :ls would have listed them.

The (GPL w/ exception) code looks is the following:

function! s:As_ls(bid) abort " {{{2
  let name = bufname(a:bid)
  if empty(name)
    let name = 'Used to be known as: '.lh#project#__buffer(a:bid)
  endif
  return printf('%3d%s %s'
        \ , a:bid
        \ , (buflisted(a:bid) ? ' ' : 'u')
        \ . (bufnr('%') == a:bid ? '%' : bufnr('#') == a:bid ? '#' : ' ')
        \ . (! bufloaded(a:bid) ? ' ' : bufwinnr(a:bid)<0 ? 'h' : 'a')
        \ . (! getbufvar(a:bid, "&modifiable") ? '-' : getbufvar(a:bid, "&readonly") ? '=' : ' ')
        \ . (getbufvar(a:bid, "&modified") ? '+' : ' ')
        \ , '"'.name.'"')
endfunction

function! s:ls_project(prj) abort " {{{2
  if lh#option#is_unset(a:prj)
    echo '(no project specified!)'
  endif
  let lines = map(copy(a:prj.buffers), 's:As_ls(v:val)')
  echo "Buffer list of ".get(a:prj, 'name', '(unnamed)')." project:"
  echo join(lines, "\n")
endfunction

Instead of a:prj.buffers, you would have the list of buffers in range(1, bufnr('$')). It would then need to be filtered to keep only the existing buffers (filter(range(1, bufnr('$')), 'bufexists(v:val)')).

Then you could wrap the call to bufname() with fnamemodify() to display exactly what you need.