unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Michael Albinus <michael.albinus@gmx.de>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: 60505@debbugs.gnu.org, Eli Zaretskii <eliz@gnu.org>,
	Gregory Heytings <gregory@heytings.org>,
	julien@jroy.ca
Subject: bug#60505: 29.0.60; Fido Mode and Tramp Completion
Date: Fri, 03 Feb 2023 16:40:53 +0100	[thread overview]
Message-ID: <87mt5udaey.fsf@gmx.de> (raw)
In-Reply-To: <jwvsffonlfp.fsf-monnier+emacs@gnu.org> (Stefan Monnier's message of "Thu, 02 Feb 2023 10:39:25 -0500")

[-- Attachment #1: Type: text/plain, Size: 2854 bytes --]

Stefan Monnier <monnier@iro.umontreal.ca> writes:

Hi Stefan,

>> I could imagine that the completion machinery offers an API that a
>> package could register its own idea of a file name syntax.
>
> The completion code relies on `file-name-directory`,
> `directory-file-name`, etc... for that.
>
> The problem as I see it goes as follows:
>
> According to `file-name-directory`, in `/ssh:myhost` the part
> `ssh:myhost` is an element of the `/` directory.  For this reason, the
> completion machinery would expect (file-name-all-completions "s" "/") to
> include "ssh:myhost" in its return list rather than only "ssh:".
>
> Now, it's impractical for Tramp to do that.  So the end result is the
> kind of bug reports we're discussing.
>
> One way to fix the problem is for Tramp to "teach" the rest of the
> system that `/ssh:` is a kind of directory, in which case the completion
> machinery would know that (file-name-all-completions "s" "/") returns
> "ssh:" and that "myhost" would be included only in the return value of
> (file-name-all-completions "" "/ssh:").

I've played with this idea, and the appended patch makes it work,
indeed. I had to adapt expand-file-name, file-exists-p,
file-name-directory and file-name-nondirectory, but the changes look
simple. It works for me now for the default completion styles (basic
partial-completion emacs22) as well as for fido-mode, with both the
default and simplified Tramp syntax.

It doesn't work (yet) for the separated Tramp syntax, but this isn't our
major problem, and could be fixed later. Could people check how it plays
in their environment?

> Another is to change Tramp's syntax so that it uses the regular "/"
> separator rather than ":".  This would get a similar result but without
> touching `file-name-directory` and friends.

No way. We have had syntax ambiguities for Tramp in the past, and I
don't want to go this painful way, again.

> We could also consider introducing a new set of (file-name) functions
>
>     completion-file-name-directory, completion-directory-file-name, ...
>
> so the completion code can use its own notion of how a file name gets
> separated into parts.

That's what I have done in the patch, adding such functions for the
tramp-completion-file-name-handler implementation. An implementation for
directory-file-name wasn't needed, but we could add if necessary.

> But introducing such a subtle distinction would likely introduce a lot
> of bugs&confusion as well.

Perhaps. So this patch isn't a candidate for Emacs 29.1. I would push it
to the master branch (when there is positive feedback), and I would also
add it to the tramp-2-6-stable branch in the Tramp git repo. By this,
the next Tramp 2.6 release on GNU ELPA would contain this change as
well, and we could get broader feedback.

What do people think?

>         Stefan

Best regards, Michael.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Type: text/x-patch, Size: 4437 bytes --]

diff --git a/lisp/net/tramp.el b/lisp/net/tramp.el
index 21dbd40b1d2..0dc7d68983b 100644
--- a/lisp/net/tramp.el
+++ b/lisp/net/tramp.el
@@ -441,6 +441,8 @@ tramp-default-method-alist
 (defconst tramp-default-method-marker "-"
   "Marker for default method in remote file names.")

+(add-to-list 'tramp-methods `(,tramp-default-method-marker))
+
 (defcustom tramp-default-user nil
   "Default user to use for transferring files.
 It is nil by default; otherwise settings in configuration files like
@@ -1414,9 +1416,13 @@ tramp-password-prompt-not-unique
 during direct remote copying with scp.")

 (defconst tramp-completion-file-name-handler-alist
-  '((file-name-all-completions
+  '((expand-file-name . tramp-completion-handle-expand-file-name)
+    (file-exists-p . tramp-completion-handle-file-exists-p)
+    (file-name-all-completions
      . tramp-completion-handle-file-name-all-completions)
-    (file-name-completion . tramp-completion-handle-file-name-completion))
+    (file-name-completion . tramp-completion-handle-file-name-completion)
+    (file-name-directory . tramp-completion-handle-file-name-directory)
+    (file-name-nondirectory . tramp-completion-handle-file-name-nondirectory))
   "Alist of completion handler functions.
 Used for file names matching `tramp-completion-file-name-regexp'.
 Operations not mentioned here will be handled by Tramp's file
@@ -1707,7 +1713,6 @@ tramp-dissect-file-name
 		     :port port :localname localname :hop hop))
 	  ;; The method must be known.
 	  (unless (or nodefault non-essential
-		      (string-equal method tramp-default-method-marker)
 		      (assoc method tramp-methods))
 	    (tramp-user-error
 	     v "Method `%s' is not known." method))
@@ -2941,6 +2946,30 @@ tramp-connectable-p
         (and vec (process-live-p (get-process (tramp-buffer-name vec))))
 	(not non-essential))))

+(defun tramp-completion-handle-expand-file-name (filename &optional directory)
+  "Like `expand-file-name' for partial Tramp files."
+  (if (file-name-absolute-p filename)
+      filename
+    (concat (or directory default-directory "/") filename)))
+
+(defun tramp-completion-handle-file-exists-p (filename)
+  "Like `file-exists-p' for partial Tramp files."
+  ;; We need special handling only when a method is needed.  Then we
+  ;; regard all files "/method:" or "/[method/" as existent, if
+  ;; "method" is a valid Tramp method.
+  (or (and (not (string-empty-p tramp-method-regexp))
+           (string-match
+	    (rx
+	     (regexp tramp-prefix-regexp)
+	     (group (regexp tramp-method-regexp))
+	     (regexp tramp-postfix-method-regexp))
+            filename)
+           ;; Is it a valid method?
+           (member
+            filename
+            (tramp-get-completion-methods (match-string 1 filename))))
+      (tramp-run-real-handler #'file-exists-p (list filename))))
+
 ;; Method, host name and user name completion.
 ;; `tramp-completion-dissect-file-name' returns a list of
 ;; `tramp-file-name' structures.  For all of them we return possible
@@ -3176,6 +3205,27 @@ tramp-get-completion-user-host
   (unless (zerop (+ (length user) (length host)))
     (tramp-completion-make-tramp-file-name method user host nil)))

+(defun tramp-completion-handle-file-name-directory (filename)
+  "Like `file-name-directory' for partial Tramp files."
+  ;; We need special handling only when a method is needed.  Then we
+  ;; return "/method:" or "/[method/", if "method" is a valid Tramp
+  ;; method.
+  (if (and (not (string-empty-p tramp-method-regexp))
+           (string-match
+            (rx (group
+	         (regexp tramp-prefix-regexp)
+                 (group (regexp tramp-method-regexp))
+	         (regexp tramp-postfix-method-regexp)))
+            filename)
+           ;; Is it a valid method?
+           (assoc (match-string 2 filename) tramp-methods))
+      (match-string 1 filename)
+    (tramp-run-real-handler #'file-name-directory (list filename))))
+
+(defun tramp-completion-handle-file-name-nondirectory (filename)
+  "Like `file-name-nondirectory' for partial Tramp files."
+  (tramp-compat-string-replace (file-name-directory filename) "" filename))
+
 (defun tramp-parse-default-user-host (method)
   "Return a list of (user host) tuples allowed to access for METHOD.
 This function is added always in `tramp-get-completion-function'

  reply	other threads:[~2023-02-03 15:40 UTC|newest]

Thread overview: 64+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-02 19:37 bug#60505: 29.0.60; Fido Mode and Tramp Completion Julien Roy
2023-01-03  8:56 ` Michael Albinus
2023-01-05 13:07 ` Gregory Heytings
2023-01-05 13:55   ` Michael Albinus
2023-01-05 14:14     ` Gregory Heytings
2023-01-06  9:51       ` Gregory Heytings
2023-01-14 21:37         ` Gregory Heytings
2023-01-15 19:23           ` Michael Albinus
2023-01-15 22:38             ` Gregory Heytings
2023-01-18 12:30               ` Michael Albinus
2023-02-01 18:12                 ` Gregory Heytings
2023-02-01 20:15                   ` Michael Albinus
2023-02-01 21:27                     ` Gregory Heytings
2023-02-02  6:37                       ` Eli Zaretskii
2023-02-02  8:25                         ` Michael Albinus
2023-02-02  9:15                           ` Eli Zaretskii
2023-02-02 15:39                           ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-02-03 15:40                             ` Michael Albinus [this message]
2023-02-03 18:43                               ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-02-03 19:23                                 ` Michael Albinus
2023-02-03 20:51                                   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-02-04 16:04                                     ` Michael Albinus
2023-02-04 16:48                                       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-02-03 22:33                               ` Gregory Heytings
2023-02-04  9:54                                 ` Michael Albinus
2023-02-06 17:26                               ` Michael Albinus
2023-02-06 17:41                                 ` Gregory Heytings
2023-02-07  0:35                                 ` Michael Heerdegen
2023-02-07  8:54                                   ` Michael Albinus
2023-02-07 18:20                                     ` Michael Heerdegen
2023-02-07 18:30                                       ` Michael Albinus
2023-02-07 20:39                                         ` Michael Heerdegen
     [not found]                                           ` <87357h19qj.fsf@dick>
2023-02-08  8:05                                             ` Michael Albinus
2023-02-08 11:27                                               ` dick
2023-02-08 13:08                                                 ` Michael Albinus
2023-02-08 13:20                                                   ` Eli Zaretskii
2023-02-08 14:13                                                     ` dick
2023-02-08 14:42                                                       ` Michael Albinus
2023-02-08 13:30                                                   ` dick
2023-02-08 15:04                                           ` Michael Albinus
2023-02-07 22:22                                         ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-02-08 15:11                                           ` Michael Albinus
2023-02-08 16:18                                             ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-02-08 17:59                                               ` Michael Albinus
2023-02-08 19:03                                                 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-02-10 16:55                                                   ` Michael Albinus
2023-02-12 19:26                                                     ` Michael Albinus
2023-02-07 19:18                                     ` Michael Heerdegen
2023-02-08 15:09                                       ` Michael Albinus
2023-02-09  0:38                                         ` Michael Heerdegen
2023-02-03  0:23                         ` Gregory Heytings
2023-02-03  7:43                           ` Juri Linkov
2023-02-03  8:39                             ` Michael Albinus
2023-02-03 12:01                             ` Eli Zaretskii
2023-02-02 15:16                 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-02-02 15:05     ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-05 20:58   ` Julien Roy
     [not found] <874k4xblcy.fsf.ref@aol.com>
2022-02-17 14:47 ` bug#54042: 29.0.50; fido-mode and ssh not listing hosts Ergus via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-02-17 16:43   ` Michael Albinus
     [not found]   ` <handler.54042.D60505.167623003032452.notifdone@debbugs.gnu.org>
2023-02-15 18:31     ` bug#54042: closed (Re: bug#60505: 29.0.60; Fido Mode and Tramp Completion) Ergus via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-02-16  8:51       ` Michael Albinus
2023-02-18 17:25         ` Michael Albinus
2023-02-19  2:04           ` Ergus via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-02-19  9:53             ` Michael Albinus

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87mt5udaey.fsf@gmx.de \
    --to=michael.albinus@gmx.de \
    --cc=60505@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    --cc=gregory@heytings.org \
    --cc=julien@jroy.ca \
    --cc=monnier@iro.umontreal.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).