1

I am using Emacs to edit and debug python code and I would like to know how to restart my debugging session within emacs pdb with a single letter command. In perldb, there is a single command R that equates to restart the script, but I can't find the equivalent one-letter instruction for restart in python.

Is there a way to hook up R to do restart in pdb?

719016
  • 9,922
  • 20
  • 85
  • 158

1 Answers1

0

You could bind a function like pdb-restart (which may not exist) to 'R' via something like this:

(define-key gud-mode-map "R" 'pdb-restart)

Though this will really affect all gud sessions. You can always set specific hooks to override this behavior if it is unwanted in other gud sessions.

EDIT:

A better way could be to use pdb-mode-hook instead of modifying gud-mode-map:

(add-hook 'pdb-mode-hook '(define-key (current-local-map) "R" 'pdb-restart))

I've tried to restart the pdb session myself and ended up having to write my own command to do it. It first tries to send a nice 'restart' with comint-send-input, but if that doesn't work, it falls back to killing the buffer (along with the underlying pdb process), and restarts the pdb session, using the same directory and arguments as the last started pdb session.

(defun pdb-restart ()
  (interactive)
  (comint-insert-send "restart")
  (sleep-for .5)
  (when
      (or
       (last-lines-match "raise Restart.*
    Restart")
       (last-lines-match "restart")
       (not (get-buffer-process (current-buffer))))
    (let ((kill-buffer-query-functions nil );disable confirming for process kill
          (pdbcmd (car-safe (symbol-value (gud-symbol 'history nil 'pdb))))
          (default-directory default-directory))
      (kill-this-buffer)
      (cd default-directory)
      (pdb pdbcmd)))

  (comint-insert-send "n"))
ealfonso
  • 6,622
  • 5
  • 39
  • 67