Search code examples
pythonemacselisppdbgud

how do i hook commands sent to pdb through gud?


i've started using pdb through gud in emacs 23.3, how can i hook command messages sent to the debugger from the buffer? i wrote the advice below for use with gdb, in order to persist comint's ring, but can't find an equivalent function to hook for pdb. i'm using python-mode.el as my major mode.

thanks.

(defadvice gdb-send-item (before gdb-save-history first nil activate)
  "write input ring on quit"
  (if (equal (type-of item) 'string) ; avoid problems with 'unprintable' structures sent to this function..
    (if (string-match "^q\\(u\\|ui\\|uit\\)?$" item)
      (progn (comint-write-input-ring)
             (message "history file '%s' written" comint-input-ring-file-name)))))

Solution

  • i think i probably could have answered my own question at the time with just a little more digging, but the first gdb solution rather took it out of me on the old learning front. i recovered, so..

    C-h b C-s Major

    after a bit of scrolling we can identify 'comint-send-input' as the function bound to key 'enter'. looking at this function's source, comint.el:1765 is a call to 'run-hook-with-args' ..this is where we realise that there's nowhere specifically 'pdb' to do what we want.

    gud is a generic wrapper to call external debugging processes and return the results ..hence control isn't there in elisp. it was the same with gdb, but there was a nice (pre-existing) wrapper around the external call which made advising that function feel 'clean'.

    so the hack.. just above 'comint-send-input' lies 'comint-add-to-input-history'.. dead easy.

    ;;save command history
    (defadvice comint-add-to-input-history (before pdb-save-history activate compile)
      "write input ring on exit"
      (message "%s" cmd)
      (if (string-match "^e\\(x\\|xi\\|xit\\)?$" cmd)
      (progn (comint-write-input-ring)
         (message "history file '%s' written" comint-input-ring-file-name)))
    )
    

    fyi, i have these to initiate the input ring for the debugging sessions

    ;#debugger history
    (defun debug-history-ring (file)
      (comint-read-input-ring t)
      (setq comint-input-ring-file-name file)
      (setq comint-input-ring-size 1000)
      (setq comint-input-ignoredups t))
    (let ((hooks '((gdb-mode-hook . (lambda () (debug-history-ring "~/.gdbhist")))
           (pdb-mode-hook . (lambda () (debug-history-ring  "~/.pythonhist"))))))
      (dolist (hook hooks) (print (cdr hook)) (add-hook (car hook) (cdr hook))))
    

    ..and to write to the history file if the debugging buffer is killed

      (add-hook 'kill-buffer-hook 'comint-write-input-ring)
    

    cheers.