2

I'm trying to develop a plugin that would show results of SQL queries, by executing them inside tmux split and using mysql pager option to save them to the specific file.

What I want now is to either create the new window to show results (if no such window have been created before) or reuse created one. But I don't know how to identify windows and how to switch to the specific window. For example, let's say I always want to display results in a horizontal split. How do I know that such split doesn't exist already? Try wincmd j and check if winnr haven't changed? What if user would move this window to another location? How do a find it?

fu! s:ShowResults(query_hash, jobid, data, event)
    unlet s:jobid
    if exists('s:results_window')
        exe s:results_window . ' wincmd w'
    else
        split
        wincmd j
    endif
    exe 'view /tmp/queries/' . a:query_hash
    let s:results_window = winnr()
    setlocal nowrap
    nnoremap <buffer> q :q<CR>
endfu
1

That's what I've done in lh-vim-lib lh#buffer#jump(). You'll have to adapt the ...#find() function to one that fits better your needs.

" Function: lh#buffer#jump({filename},{cmd}) {{{2
function! lh#buffer#jump(filename, cmd) abort
  let b = lh#buffer#find(a:filename)
  if b != -1 || type(a:filename) == type(0) | return b | endif
  call lh#window#create_window_with(a:cmd . ' ' . a:filename)
  return winnr()
endfunction

" Function: lh#buffer#find({filename}) {{{2
" If {filename} is opened in a window, jump to this window, otherwise return -1
" Moved from searchInRuntimeTime.vim
function! lh#buffer#find(filename) abort
  let b = bufwinnr(a:filename)
  " Workaround a bug in event execution: we may a have a non null buffer, but
  " with a name that doesn't match what is looked for.
  " -> "|| bufname(winbufnr(b)) != a:filename"
  " The second case is used when the filename is actually a buffer name
  if b == -1 || (fnamemodify(bufname(winbufnr(b)), ':p') != fnamemodify(a:filename, ':p') && winbufnr(b) != a:filename)
    return -1
  endif
  exe b.'wincmd w'
  return b
endfunction

" Function: lh#window#create_window_with(cmd) {{{3
" Since a few versions, vim throws a lot of E36 errors around:
" everythime we try to split from a windows where its height equals &winheight
" (the minimum height)
function! lh#window#create_window_with(cmd) abort
  try
    exe a:cmd
  catch /E36:/
    " Try again after an increase of the current window height
    resize +1
    exe a:cmd
  endtry
endfunction

But ... what about using the quickfix window?

  • quickfix window? What does it have to do with my task? – user1685095 Jul 14 '17 at 22:46
  • It's unique and easy to find. It's quite adapted to present execution results. However if you have no information that relates to files, it's indeed not adapted. The other solution is to choose a filename or a pattern for filename, check in buffers if a matching one already exist. And if it does, fond its associated window to jump to it or make sure it's opened. That's what I do in the functions from my library. – Luc Hermitte Jul 14 '17 at 23:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.