Subject: [PATCH] Use future history to select current script for pdb et al
From: "Paul D. Nelson" <[email-masked]>
To: [email-masked]
Date: Tue, 22 Apr 2025 17:01:18 +0200
List-Id: <debbugs-submit.debbugs.gnu.org>

The simplest way I know to debug the current Python script is:

1. Copy the name of the current file (C-x C-j C-0 w)
2. M-x pdb RET yank RET (possibly after deleting previous file name)

This seems like a good place to introduce a "future history" operation.
The attached patch does so, via a new optional argument to
gud-query-cmdline.  The above then simplifies to M-x pdb RET M-n RET.

Existing behavior is unchanged.  I added the same feature to other gdb
commands for which it seemed to make sense (perldb, guiler).  Feedback
or other suggestions would be welcome.

From 81382b69b83298d5bc098cc49f373d57a0909b42 Mon Sep 17 00:00:00 2001
From: Paul Nelson <[email-masked]>
Date: Mon, 21 Apr 2025 22:14:53 +0200
Subject: [PATCH] Add "forward history" support for some debuggers

* lisp/progmodes/gud.el (gud-query-cmdline): Add an optional
default-list parameter to allow passing a list of "forward
history" suggestions to the minibuffer.
(perldb, pdb, guiler): Use buffer file name to suggest a default
debugging command via "forward history".

* doc/emacs/building.texi (Starting GUD): Document the new
feature.
---
 doc/emacs/building.texi |  3 +++
 etc/NEWS                |  8 ++++++++
 lisp/progmodes/gud.el   | 31 +++++++++++++++++++++++++------
 3 files changed, 36 insertions(+), 6 deletions(-)

diff --git a/doc/emacs/building.texi b/doc/emacs/building.texi
index 02ca71f069b..39c5e79a870 100644
--- a/doc/emacs/building.texi
+++ b/doc/emacs/building.texi
@@ -674,6 +674,9 @@ Starting GUD
 using the minibuffer.  The minibuffer's initial contents contain the
 standard executable name and options for the debugger, and sometimes
 also a guess for the name of the executable file you want to debug.
+For @code{pdb}, @code{perldb}, and @code{guiler}, if the current buffer
+visits a file, pressing @kbd{M-n} (@code{next-history-element})
+in the prompt suggests a full command line including that file name.
 Shell wildcards and variables are not allowed in this command line.
 Emacs assumes that the first command argument which does not start
 with a @samp{-} is the executable file name.
diff --git a/etc/NEWS b/etc/NEWS
index 56a45068daf..3519bc928ff 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1940,6 +1940,14 @@ normal file name starts with "file:", you can disable the URI
 recognition by invoking 'sqlite-open' with the new optional argument
 DISABLE-URI non-nil.
 
+** GUD
+
+---
+*** pdb, perldb, and guiler suggest debugging the current file via 'M-n'.
+When starting these debuggers (e.g., 'M-x pdb') while visiting a file,
+pressing 'M-n' in the command prompt suggests a command line including
+the file name, using the minibuffer's "future history".
+
 
 * New Modes and Packages in Emacs 31.1
 
diff --git a/lisp/progmodes/gud.el b/lisp/progmodes/gud.el
index 3cca55be3a7..7108673dddc 100644
--- a/lisp/progmodes/gud.el
+++ b/lisp/progmodes/gud.el
@@ -757,7 +757,11 @@ gud-minibuffer-local-map
   :parent minibuffer-local-map
   "C-i" #'comint-dynamic-complete-filename)
 
-(defun gud-query-cmdline (minor-mode &optional init)
+(defun gud-query-cmdline (minor-mode &optional init default-list)
+  "Prompt for a command to run the debugger.
+MINOR-MODE is the name of the debugger to run.  INIT is the initial
+command, before any history is available.  DEFAULT-LIST is a list of
+default commands, accessible via \\[next-history-element]."
   (let* ((hist-sym (gud-symbol 'history nil minor-mode))
 	 (cmd-name (gud-val 'command-name minor-mode)))
     (unless (boundp hist-sym) (set hist-sym nil))
@@ -780,7 +784,8 @@ gud-query-cmdline
 			       (setq file f)))
                             file)))))
      gud-minibuffer-local-map nil
-     hist-sym)))
+     hist-sym
+     default-list)))
 
 (defvar gdb-first-prompt t)
 
@@ -1671,8 +1676,13 @@ perldb
 The directory containing the perl program becomes the initial
 working directory and source-file directory for your debugger."
   (interactive
-   (list (gud-query-cmdline 'perldb
-			    (concat (or (buffer-file-name) "-E 0") " "))))
+   (list
+    (gud-query-cmdline
+     'perldb
+     (concat (or (buffer-file-name) "-E 0") " ")
+     (when-let* ((file (buffer-file-name)))
+       (list (concat gud-perldb-command-name " "
+                     (shell-quote-argument file) " "))))))
 
   (gud-common-init command-line 'gud-perldb-massage-args
 		   'gud-perldb-marker-filter)
@@ -1802,7 +1812,11 @@ pdb
 The directory containing this file becomes the initial working
 directory and source-file directory for your debugger."
   (interactive
-   (list (gud-query-cmdline 'pdb)))
+   (list (gud-query-cmdline
+          'pdb nil
+          (when-let* ((file (buffer-file-name)))
+            (list (concat gud-pdb-command-name " "
+                          (shell-quote-argument file)))))))
 
   (gud-common-init command-line nil 'gud-pdb-marker-filter)
   (setq-local gud-minor-mode 'pdb)
@@ -1889,7 +1903,12 @@ guiler
 The directory containing FILE becomes the initial working directory
 and source-file directory for your debugger."
   (interactive
-   (list (gud-query-cmdline 'guiler)))
+   (list
+    (gud-query-cmdline
+     'guiler nil
+     (when-let* ((file (buffer-file-name)))
+       (list (concat gud-guiler-command-name " "
+                     (shell-quote-argument file)))))))
 
   (gud-common-init command-line nil 'gud-guiler-marker-filter)
   (setq-local gud-minor-mode 'guiler)
-- 
2.39.3 (Apple Git-145)