@@ -54,6 +54,7 @@ func s:StartDebug(cmd)
5454 return
5555 endif
5656 let pty = job_info (term_getjob (s: ptybuf ))[' tty_out' ]
57+ let s: ptywin = win_getid (winnr ())
5758
5859 " Create a hidden terminal window to communicate with gdb
5960 let s: commbuf = term_start (' NONE' , {
@@ -81,12 +82,15 @@ func s:StartDebug(cmd)
8182 exe ' bwipe! ' . s: commbuf
8283 return
8384 endif
85+ let s: gdbwin = win_getid (winnr ())
8486
8587 " Connect gdb to the communication pty, using the GDB/MI interface
8688 call term_sendkeys (gdbbuf, ' new-ui mi ' . commpty . " \r " )
8789
88- " Install debugger commands.
90+ " Install debugger commands in the text window.
91+ call win_gotoid (s: startwin )
8992 call s: InstallCommands ()
93+ call win_gotoid (s: gdbwin )
9094
9195 let s: breakpoints = {}
9296endfunc
@@ -116,10 +120,14 @@ func s:CommOutput(chan, msg)
116120 if msg != ' '
117121 if msg = ~ ' ^\*\(stopped\|running\)'
118122 call s: HandleCursor (msg)
119- elseif msg = ~ ' ^\^done,bkpt='
123+ elseif msg = ~ ' ^\^done,bkpt=' || msg = ~ ' ^=breakpoint-created, '
120124 call s: HandleNewBreakpoint (msg)
121125 elseif msg = ~ ' ^=breakpoint-deleted,'
122126 call s: HandleBreakpointDelete (msg)
127+ elseif msg = ~ ' ^\^done,value='
128+ call s: HandleEvaluate (msg)
129+ elseif msg = ~ ' ^\^error,msg='
130+ call s: HandleError (msg)
123131 endif
124132 endif
125133 endfor
@@ -130,19 +138,37 @@ func s:InstallCommands()
130138 command Break call s: SetBreakpoint ()
131139 command Delete call s: DeleteBreakpoint ()
132140 command Step call s: SendCommand (' -exec-step' )
133- command NNext call s: SendCommand (' -exec-next' )
141+ command Over call s: SendCommand (' -exec-next' )
134142 command Finish call s: SendCommand (' -exec-finish' )
135143 command Continue call s: SendCommand (' -exec-continue' )
144+ command - range -nargs =* Evaluate call s: Evaluate (<range> , <q-args> )
145+ command Gdb call win_gotoid (s: gdbwin )
146+ command Program call win_gotoid (s: ptywin )
147+
148+ " TODO: can the K mapping be restored?
149+ nnoremap K :Evaluate<CR>
136150endfunc
137151
138152" Delete installed debugger commands in the current window.
139153func s: DeleteCommands ()
140154 delcommand Break
141155 delcommand Delete
142156 delcommand Step
143- delcommand NNext
157+ delcommand Over
144158 delcommand Finish
145159 delcommand Continue
160+ delcommand Evaluate
161+ delcommand Gdb
162+ delcommand Program
163+
164+ nunmap K
165+ sign undefine debugPC
166+ sign undefine debugBreakpoint
167+ exe ' sign unplace ' . s: pc_id
168+ for key in keys (s: breakpoints )
169+ exe ' sign unplace ' . (s: break_id + key )
170+ endfor
171+ unlet s: breakpoints
146172endfunc
147173
148174" :Break - Set a breakpoint at the cursor position.
@@ -171,6 +197,35 @@ func s:SendCommand(cmd)
171197 call term_sendkeys (s: commbuf , a: cmd . " \r " )
172198endfunc
173199
200+ " :Evaluate - evaluate what is under the cursor
201+ func s: Evaluate (range , arg)
202+ if a: arg != ' '
203+ let expr = a: arg
204+ elseif a: range == 2
205+ let pos = getcurpos ()
206+ let reg = getreg (' v' , 1 , 1 )
207+ let regt = getregtype (' v' )
208+ normal ! gv" vy
209+ let expr = @v
210+ call setpos (' .' , pos)
211+ call setreg (' v' , reg , regt)
212+ else
213+ let expr = expand (' <cexpr>' )
214+ endif
215+ call term_sendkeys (s: commbuf , ' -data-evaluate-expression "' . expr . " \" \r " )
216+ let s: evalexpr = expr
217+ endfunc
218+
219+ " Handle the result of data-evaluate-expression
220+ func s: HandleEvaluate (msg)
221+ echomsg ' "' . s: evalexpr . ' ": ' . substitute (a: msg , ' .*value="\(.*\)"' , ' \1' , ' ' )
222+ endfunc
223+
224+ " Handle an error.
225+ func s: HandleError (msg)
226+ echoerr substitute (a: msg , ' .*msg="\(.*\)"' , ' \1' , ' ' )
227+ endfunc
228+
174229" Handle stopping and running message from gdb.
175230" Will update the sign that shows the current position.
176231func s: HandleCursor (msg)
0 commit comments