From: Michael Albinus Date: Thu, 18 Nov 2021 14:06:26 +0000 (+0100) Subject: Extend abbreviate-file-name for further Tramp methods. X-Git-Tag: emacs-29.0.90~2852^2~213 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=6c1190c74936f132cb4173335cb037de89ef8aa7;p=emacs.git Extend abbreviate-file-name for further Tramp methods. * lisp/net/tramp-gvfs.el (tramp-gvfs-file-name-handler-alist): Add 'abbreviate-file-name'. (tramp-gvfs-handle-expand-file-name): * lisp/net/tramp.el (tramp-handle-expand-file-name): Handle case that tilde cannot be expanded. * test/lisp/net/tramp-tests.el (tramp-test07-abbreviate-file-name): Extend test. --- diff --git a/lisp/net/tramp-gvfs.el b/lisp/net/tramp-gvfs.el index a4a7bacd8ac..ab71c9cd13f 100644 --- a/lisp/net/tramp-gvfs.el +++ b/lisp/net/tramp-gvfs.el @@ -744,7 +744,7 @@ It has been changed in GVFS 1.14.") ;; New handlers should be added here. ;;;###tramp-autoload (defconst tramp-gvfs-file-name-handler-alist - '(;; `abbreviate-file-name' performed by default handler. + '((abbreviate-file-name . tramp-handle-abbreviate-file-name) (access-file . tramp-handle-access-file) (add-name-to-file . tramp-handle-add-name-to-file) ;; `byte-compiler-base-file-name' performed by default handler. @@ -1149,15 +1149,12 @@ file names." (make-tramp-file-name :method method :user user :domain domain :host host :port port :localname "/" :hop hop))) - (setq localname - (replace-match - (tramp-get-connection-property v "default-location" "~") - nil t localname 1))) - ;; Tilde expansion is not possible. - (when (string-match-p "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname) - (tramp-error - v 'file-error - "Cannot expand tilde in file `%s'" name)) + (unless (string-empty-p + (tramp-get-connection-property v "default-location" "")) + (setq localname + (replace-match + (tramp-get-connection-property v "default-location" "~") + nil t localname 1)))) (unless (tramp-run-real-handler #'file-name-absolute-p (list localname)) (setq localname (concat "/" localname))) ;; We do not pass "/..". @@ -1172,10 +1169,12 @@ file names." ;; Do not keep "/..". (when (string-match-p "^/\\.\\.?$" localname) (setq localname "/")) - ;; No tilde characters in file name, do normal - ;; `expand-file-name' (this does "/./" and "/../"). + ;; Do normal `expand-file-name' (this does "/./" and "/../"), + ;; unless there are tilde characters in file name. (tramp-make-tramp-file-name - v (tramp-run-real-handler #'expand-file-name (list localname)))))) + v (if (string-match-p "\\`~" localname) + localname + (tramp-run-real-handler #'expand-file-name (list localname))))))) (defun tramp-gvfs-get-directory-attributes (directory) "Return GVFS attributes association list of all files in DIRECTORY." diff --git a/lisp/net/tramp.el b/lisp/net/tramp.el index 7927ddd1072..f43c1d84b87 100644 --- a/lisp/net/tramp.el +++ b/lisp/net/tramp.el @@ -3454,13 +3454,16 @@ User is always nil." ;; Do not keep "/..". (when (string-match-p "^/\\.\\.?$" localname) (setq localname "/")) - ;; Do normal `expand-file-name' (this does "/./" and "/../"). + ;; Do normal `expand-file-name' (this does "/./" and "/../"), + ;; unless there are tilde characters in file name. ;; `default-directory' is bound, because on Windows there would ;; be problems with UNC shares or Cygwin mounts. (let ((default-directory tramp-compat-temporary-file-directory)) (tramp-make-tramp-file-name - v (tramp-drop-volume-letter - (tramp-run-real-handler #'expand-file-name (list localname)))))))) + v (if (string-match-p "\\`~" localname) + localname + (tramp-drop-volume-letter + (tramp-run-real-handler #'expand-file-name (list localname))))))))) (defun tramp-handle-file-accessible-directory-p (filename) "Like `file-accessible-directory-p' for Tramp files." diff --git a/test/lisp/net/tramp-tests.el b/test/lisp/net/tramp-tests.el index 482d3ff554f..98269d5fa39 100644 --- a/test/lisp/net/tramp-tests.el +++ b/test/lisp/net/tramp-tests.el @@ -2295,7 +2295,10 @@ This checks also `file-name-as-directory', `file-name-directory', (skip-unless (tramp--test-emacs29-p)) (let* ((remote-host (file-remote-p tramp-test-temporary-file-directory)) - (home-dir (expand-file-name (concat remote-host "~")))) + ;; Not all methods can expand "~". + (home-dir (ignore-errors (expand-file-name (concat remote-host "~"))))) + (skip-unless home-dir) + ;; Check home-dir abbreviation. (unless (string-suffix-p "~" home-dir) (should (equal (abbreviate-file-name (concat home-dir "/foo/bar"))