Subject: bug#67688: 30.0.50; ffap prepends buffers default-directory to guessed file name in case of tramp's file name syntax

From: Visuwesh <[email-masked]>
Date: Tue, 12 Dec 2023 20:41:28 +0530

[Tuesday December 12, 2023] Visuwesh wrote:

> [Friday December 08, 2023] Michael Albinus wrote:
>
>> Gregor Zattler via "Bug reports for GNU Emacs, the Swiss army knife of
>> text editors" <[email-masked]> writes:
>>
>>> Dear Emacs developers,
>>
>> Hi Gregor,
>>
>>> following this recipe (in
>>> some Gnu/Linux shell):
>>>
>>> $ cd /tmp
>>> tmp$ emacs -Q -nw
>>> # type /ssh:user@server:/home/test
>>> # type M-x ffap RET
>>>
>>> the proposed file name in minibuffer then is:
>>> /tmp/ssh:user@server:/home/test
>>> instead of
>>> /ssh:user@server:/home/test
>>>
>>> Therefore it's not possible to just hit RET in order
>>> to open that file.  But that would be nice.
>>
>> I can reproduce the behavior for all Emacsen down to Emacs 25 (the
>> oldest I have a running copy of), so I guess it didn't work ever. 
>
> You were quite close.  That was when ffap-lax-url was set to t, previous
> versions would have done the right thing I think.
>
>> The problem is, that `ffap-guesser' returns "ssh:user@server:/home/test",
>> which looks like a relative file name. Therefore, it is expanded to the
>> default directory "/tmp/".
>>
>> I have no idea about ffap.el and thingatpt.el, so I fear I cannot do too
>> much. Somebody else who knows these libraries might fix it.
>
> With the default value of ffap-lax-url=t,
> thing-at-point-bounds-url-at-point does not check the scheme of the url
> so it ends up matching ssh:user@server alone.  With the nil value, ffap
> correctly suggests /ssh:user@server:/home/test because the correct
> scheme is ssh:// not ssh:.
> But with ffap-lax-url=nil, you lose the ability to insert
> mailto:user@host when the point is on user@host because it is not a
> "well-formed" url [1] and thusly does not get picked up by
> thing-at-point-url-at-point.  I hope my explanation is understandable.
>
> I'm not sure what to do here frankly.
>
> Hmm... looking at the region history for the defcustom, it was changed
> to t by Eli for bug#25264: the user wanted the email address like
> user@host to be matched by ffap.  I suppose we can try to see if the
> point is on an email address in ffap-guesser after URL and filename
> guesses, then we can turn ffap-lax-url back to nil and everyone will be
> happy.
>
> 1. See thing-at-point--bounds-of-well-formed-url.

I was thinking of the patch below.  There is probably a better solution
out there but I am not sure what other lax URL we will miss out by
setting ffap-lax-url to nil.  It feels like a hack though...

diff --git a/lisp/ffap.el b/lisp/ffap.el
index 530e3da182e..b8da1292d4f 100644
--- a/lisp/ffap.el
+++ b/lisp/ffap.el
@@ -152,7 +152,7 @@ ffap-url-unwrap-remote
   :group 'ffap
   :version "24.3")
 
-(defcustom ffap-lax-url t
+(defcustom ffap-lax-url nil
   "If non-nil, allow lax URL matching.
 The default non-nil value might produce false URLs in C++ code
 with symbols like \"std::find\".  On the other hand, setting
@@ -160,7 +160,7 @@ ffap-lax-url
 well-formed, such as \"user@host\" or \"<user@host>\"."
   :type 'boolean
   :group 'ffap
-  :version "25.2")                      ; nil -> t
+  :version "30.1")
 
 (defcustom ffap-ftp-default-user "anonymous"
   "User name in FTP file names generated by `ffap-host-to-filename'.
@@ -609,6 +609,13 @@ ffap-fixup-url
    ((and ffap-url-unwrap-remote (ffap-url-unwrap-remote url)))
    (url)))
 
+(defun ffap-fixup-email (email)
+  "Clean up EMAIL and return it as a mailto: URL."
+  (when (stringp email)
+    (if (string-prefix-p "mailto:" email)
+        email
+      (concat "mailto:" email))))
+
 
 ;;; File Name Handling:
 
@@ -1571,6 +1578,7 @@ ffap-guesser
 	   (ffap-fixup-url (or (ffap-url-at-point)
 			       (ffap-gopher-at-point))))
       (ffap-file-at-point)		; may yield url!
+      (ffap-fixup-email (thing-at-point 'email))
       (ffap-fixup-machine (ffap-machine-at-point))))
 
 (defun ffap-prompter (&optional guess suffix)