Skip to content
Snippets Groups Projects
vim-gdb.vim 1.69 KiB
Newer Older
Moritz Sichert's avatar
Moritz Sichert committed
highlight VimgdbCurrentLine ctermbg=130 guibg=#FFE568


let s:vimgdb_dir = fnamemodify(resolve(expand('<sfile>:p')), ':h:h')
let s:next_session_id = 0
let s:sessions = {}


function! Tapi_vimgdb_goto(bufnum, arglist)
    if len(a:arglist) != 3
        return
    endif
    let session_id = a:arglist[0]
    let filename = a:arglist[1]
    let line = a:arglist[2]
    let session = s:sessions[session_id]
    " Go to source window and jump to current line
    noautocmd call win_gotoid(session['source_winid'])
    execute 'edit' fnameescape(filename)
    execute 'normal!' line . 'G'
    " Update highlighted line
    if session['current_line_matchid'] != -1
        call matchdelete(session['current_line_matchid'])
    endif
    let session['current_line_matchid'] = matchaddpos('VimgdbCurrentLine', [line])
    " Switch back to debugger window
    noautocmd call win_gotoid(bufwinid(a:bufnum))
endfunction


function! s:close_session(session_id)
    if !has_key(s:sessions, a:session_id)
        return
    endif
    let session = s:sessions[a:session_id]
    if session['current_line_matchid'] != -1
        noautocmd call win_gotoid(session['source_winid'])
        call matchdelete(session['current_line_matchid'])
    endif
endfunction


function! s:vimgdb(mods, gdb_args)
    let session_id = s:next_session_id
    let s:next_session_id = s:next_session_id + 1
    let s:sessions[session_id] = {'source_winid': win_getid(), 'current_line_matchid': -1}
    execute a:mods 'terminal' '++close' s:vimgdb_dir . '/gdb/gdb-wrapper' session_id a:gdb_args
    execute 'autocmd BufDelete <buffer> call s:close_session(' . session_id . ')'
endfunction


command! -nargs=+ -complete=shellcmd Vimgdb call s:vimgdb('<mods>', "<args>")