Skip to main content
 首页 » 编程设计

Emacs 中的 Python 调试器 : set keybindings similar to perldb

2025年05月04日14lidabo

我开始在 Emacs 中使用 pdb Python 调试器,我已经习惯了 perl 调试器中的按键,我希望它也能在 pdb< 中使用。如何在 Emacs 中配置调试器,使其具有与 Perl 中相同的键绑定(bind),例如“restart=R”或“print=x”?

我不介意这是否仅特定于 Emacs 中的调试而不是控制台中的 pdb 调试器,无论如何我将在 Emacs 中进行所有调试。

请您参考如下方法:

这是我用来强制 pdb 重新启动的代码,它首先通过 comint-send-input 发送重新启动,但如果失败则回退到终止并使用相同的参数和默认目录重新启动 pdb 进程。

(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")) 
 
(defun comint-insert-send (input) 
  (insert input) 
  (comint-send-input)) 
 
(defun last-lines-match (regexp &optional n) 
  (unless n (setf n 3)) 
  (re-search-backward regexp (line-beginning-position (- 0 n)) t)) 

你可以通过这样的方式将 pdb-restart 绑定(bind)到“R”:

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

至于打印,在 pdb 中,您可以简单地执行 p pythonexpression,因此没有太多的捷径。但是,您可以使用以下方法将 x 绑定(bind)到“print”:

(define-key gud-mode-map "x" (lambda () (interactive)  (insert "print ")))