Subject: bug#79441: [PATCH v2] Re: bug#79441: shell-resync-dirs breaks on PS1 with newline

From: Janneke Nieuwenhuizen <[email-masked]>
Date: Sun, 14 Sep 2025 07:50:38 +0200

Janneke Nieuwenhuizen writes:

Hello!

> [.]
>
>>> To reproduce, a
>>> single character (that is not '.') and a newline suffices, typing
>>> something like:
>>> 
>>>     PS1='0\n$ '
>>> 
>>> in an emacs shell and typing M-RET reproduces the problem.
>>
>> Can you suggest how to solve this?
>
> Good question!  When I tried to add (message ...) debugging this
> morning, it started (and kept) looping; so I had no idea how to proceed.
> However, I tried again and got better results this time and have created
> a patch that "works for me" (see attached).

[..]  Snip rationale that still holds...but

...initially I only used `seq-filter' in my patch, so I removed the
stripping of the optional final newline.  When I added the fall-back to
takk the last element of the list, we'd need to bring back that final
newline removal.  Version 2 attached.

Greetings,
Janneke

>From a08fc315b30044c7feb86a04a135ea7de434bf11 Mon Sep 17 00:00:00 2001
From: Janneke Nieuwenhuizen <[email-masked]>
Date: Sat, 13 Sep 2025 16:30:42 +0200
Subject: [PATCH v2] Fix 'shell-resync-dirs' for multi-line prompt.

* lisp/shell.el (shell-eval-command): Instead of always taking the last
element from the shell-dirtrack-query command, first try selecting the
last directory.  (Bug#79441)
---
 lisp/shell.el | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/lisp/shell.el b/lisp/shell.el
index 09d4161ba7a..a9c71488f2d 100644
--- a/lisp/shell.el
+++ b/lisp/shell.el
@@ -1236,11 +1236,12 @@ shell-resync-dirs
 `shell-dirstack-query' (default \"dirs\"), reads the next
 line output and parses it to form the new directory stack."
   (interactive)
-  (let* ((dls (car
-               (last
-                (string-lines
-                 (string-chop-newline
-                  (shell-eval-command (concat shell-dirstack-query "\n")))))))
+  (let* ((lines (nreverse
+                 (string-lines
+                  (string-chop-newline
+                   (shell-eval-command (concat shell-dirstack-query "\n"))))))
+         (dls (or (seq-find #'file-directory-p lines)
+                  (car lines)))
          (dlsl nil)
          (pos 0)
          (ds nil))
-- 
2.51.0

-- 
Janneke Nieuwenhuizen <[email-masked]>  | GNU LilyPond https://LilyPond.org
Freelance IT https://www.JoyOfSource.com | Avatar® https://AvatarAcademy.com