Skip to content

Instantly share code, notes, and snippets.

@AmaiSaeta
Created January 26, 2014 11:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AmaiSaeta/8631399 to your computer and use it in GitHub Desktop.
Save AmaiSaeta/8631399 to your computer and use it in GitHub Desktop.
(Vim) :ls の結果を、 Vim script から利用しやすい形にして返す関数
function! s:ls(all)
" Return the STRUCTURED all buffer informations that is similar :ls.
" @param[in] all Include "unlisted" buffer informations.
" @return
" Buffer informations. It's a List that contains Dictionary of a buffer
" informatin. Its Dictionary is composed these members:
" bufnr: Buffer number.
" unlisted: If 1, this buffer is "unlisted", otherwise 0.
" focus: "%" means in current window. "#" means alternate buffer.
" active: "a" means active buffer. "h" means hidden buffer.
" modifiable: If 1, this buffer with 'modifiable' off, otherwise 0.
" readonly: If 1, a readonly buffer.
" modified: If 1, a modified buffer.
" readerror: If 1, a buffer with read errors.
" bufname: Buffer name. it's like bufname(), but has few different.
" line: Cursor position.
" Get :ls result
redir => cRes0
execute 'silent ls' . (a:all ? '!' : '')
redir END
let cRes = split(cRes0, "\n")
unlet cRes0
" Parse
let sRes = []
for i in cRes
" Parse a line of :ls.
let items = map(
\ matchlist(i, '\v^\s*(\d+)(.)(.)(.)(.)(.)\s+"([^"]+)".{-}(\d+).*$'),
\ 'v:val == " " ? "" : v:val')
call add(sRes, {
\ 'bufnr' : items[1],
\ 'unlisted' : len(items[2]),
\ 'focus' : items[3],
\ 'active' : items[4],
\ 'modifiable': items[5] != '-',
\ 'readonly' : items[5] == '=',
\ 'modified' : items[6] == '+',
\ 'readerror' : items[6] == 'x',
\ 'bufname' : items[7],
\ 'line' : items[8]
\ })
endfor
return sRes
endfunction
@AmaiSaeta
Copy link
Author

こんな感じに、Vimの各コマンドの結果をスクリプトで取り回し易い形式で返してくれる関数(群)が欲しい。
絶対誰かが既に考えていると思うんだけど……

@AmaiSaeta
Copy link
Author

なお、コメントの英語は適当の模様。

@AmaiSaeta
Copy link
Author

各フィールド名や、booleanなフィールドの是非はもうちょっと検討したが良いかもしんない。

@AmaiSaeta
Copy link
Author

補足説明:
bufname()だと嬉しくないのは、quickfixウィンドウとかで空文字列が返るからです。
:lsだと、L10Nされた分かり易い名前(例: [Quickfixリスト])が得られる。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment