Tuesday, October 15, 2013

Vim: Distinguish a location-list from a quickfix-list

Strange that there is no easy way (that I know of) to differentiate a location-list from a quickfix-list. Today I noticed that the buffer-list (:ls) displays a different name for each type, and thus:

fun! GetBufferListOutputAsOneString()
    " Return the ':ls' output as one string.  Call it with ':silent'
    " to suppress the normal ls output.
    let buffer_list = ''
    redir =>> buffer_list
    ls
    redir END
    return buffer_list
endfun

fun! IsLocationListBuffer()
    " Return 1 if the current buffer contains a location list.
    " Return 0 otherwise.
    if &ft != 'qf'
        return 0
    endif

    silent let buffer_list = GetBufferListOutputAsOneString()

    let l:quickfix_match = matchlist(buffer_list,
                \ '\n\s*\(\d\+\)[^\n]*Quickfix List')
    if empty(l:quickfix_match)
        " no match and ft==qf: current buffer contains a location list
        return 1
    endif
    let quickfix_bufnr = l:quickfix_match[1]
    return quickfix_bufnr == bufnr('%') ? 0 : 1
endfun

No comments:

Post a Comment