Subject: bug#28064: eshell-cmpl should not auto-complete ~/./

From: Jim Porter <[email-masked]>
Date: Sun, 12 Mar 2023 00:02:52 -0800

On 8/12/2017 6:44 AM, Yegor Timoshenko wrote:
> M-x eshell
> Type `cd ~/.` and press TAB.
> 
> It will auto-complete to `cd ~/./`. I don't think it's useful.

A much-delayed thanks for reporting this. I looked into this, and it was 
due to 'eshell-expand-user-reference' calling 'expand-file-name' on the 
argument. This meant that when you press TAB, the "~/." gets converted 
into "/home/user" before passing the command to Pcomplete. Then 
Pcomplete would incorrectly assume you wanted to complete "/home/user", 
which just becomes "/home/user/".

Attached is a fix. Normally I'd write a regression test for this, but I 
haven't come up with a good way to do this without messing up the user's 
real home directory...

From 74b57726ed08940e9744b77934e7e8077e359801 Mon Sep 17 00:00:00 2001
From: Jim Porter <[email-masked]>
Date: Sat, 11 Mar 2023 23:53:34 -0800
Subject: [PATCH] Don't over-normalize file names starting with "~" in Eshell

Previously, this would call 'expand-file-name' on the file name, but
that normalizes the value, turning something like "~/." into
"/home/user".  As a result, Pcomplete didn't work for dotfiles after
"~/" (bug#28064).

* lisp/eshell/em-dirs.el (eshell-expand-user-reference): New
function...
(eshell-expand-user-reference): ... use it.
---
 lisp/eshell/em-dirs.el | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/lisp/eshell/em-dirs.el b/lisp/eshell/em-dirs.el
index eb679b80cb5..4bc6342d422 100644
--- a/lisp/eshell/em-dirs.el
+++ b/lisp/eshell/em-dirs.el
@@ -253,11 +253,17 @@ eshell-dirs-substitute-cd
     (throw 'eshell-replace-command
 	   (eshell-parse-command "cd" (flatten-tree args)))))
 
+(defun eshell-expand-user-reference (file)
+  "Expand a user reference in FILE to its real directory name."
+  (replace-regexp-in-string
+   (rx bos (group "~" (*? anychar)) (or "/" eos))
+   #'expand-file-name file))
+
 (defun eshell-parse-user-reference ()
   "An argument beginning with ~ is a filename to be expanded."
   (when (and (not eshell-current-argument)
-	     (eq (char-after) ?~))
-    (add-to-list 'eshell-current-modifiers 'expand-file-name)
+             (eq (char-after) ?~))
+    (add-to-list 'eshell-current-modifiers #'eshell-expand-user-reference)
     (forward-char)
     (char-to-string (char-before))))
 
-- 
2.25.1