Created
January 26, 2014 11:34
-
-
Save AmaiSaeta/8631399 to your computer and use it in GitHub Desktop.
(Vim) :ls の結果を、 Vim script から利用しやすい形にして返す関数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
なお、コメントの英語は適当の模様。
各フィールド名や、booleanなフィールドの是非はもうちょっと検討したが良いかもしんない。
補足説明:
bufname()
だと嬉しくないのは、quickfixウィンドウとかで空文字列が返るからです。
:ls
だと、L10Nされた分かり易い名前(例: [Quickfixリスト]
)が得られる。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
こんな感じに、Vimの各コマンドの結果をスクリプトで取り回し易い形式で返してくれる関数(群)が欲しい。
絶対誰かが既に考えていると思うんだけど……