Implement another fix for bug#49229

* lisp/minibuffer.el (read-file-name-default): Respect remote files.
(Bug#49229)

* lisp/net/tramp-sh.el (tramp-sh-handle-expand-file-name):
Handle special file names on MS Windows.

* lisp/net/tramp.el (tramp-file-name-handler): Revert patch.
(Bug#49229)
This commit is contained in:
Michael Albinus 2021-07-02 14:51:23 +02:00
parent 38aa2074f8
commit 225ca617b7
3 changed files with 59 additions and 58 deletions

View file

@ -3161,6 +3161,7 @@ See `read-file-name' for the meaning of the arguments."
(unless val (error "No file name specified")) (unless val (error "No file name specified"))
(if (and default-filename (if (and default-filename
(not (file-remote-p dir))
(string-equal val (if (consp insdef) (car insdef) insdef))) (string-equal val (if (consp insdef) (car insdef) insdef)))
(setq val default-filename)) (setq val default-filename))
(setq val (substitute-in-file-name val)) (setq val (substitute-in-file-name val))

View file

@ -2667,56 +2667,63 @@ the result will be a local, non-Tramp, file name."
(setq dir (or dir default-directory "/")) (setq dir (or dir default-directory "/"))
;; Handle empty NAME. ;; Handle empty NAME.
(when (zerop (length name)) (setq name ".")) (when (zerop (length name)) (setq name "."))
;; Unless NAME is absolute, concat DIR and NAME. ;; On MS Windows, some special file names are not returned properly
(unless (file-name-absolute-p name) ;; by `file-name-absolute-p'.
(setq name (concat (file-name-as-directory dir) name))) (if (and (eq system-type 'windows-nt)
;; If connection is not established yet, run the real handler. (string-match-p
(if (not (tramp-connectable-p name)) (concat "^\\([[:alpha:]]:\\|" null-device "$\\)") name))
(tramp-run-real-handler #'expand-file-name (list name nil)) (tramp-run-real-handler #'expand-file-name (list name dir))
;; Dissect NAME. ;; Unless NAME is absolute, concat DIR and NAME.
(with-parsed-tramp-file-name name nil (unless (file-name-absolute-p name)
(unless (tramp-run-real-handler #'file-name-absolute-p (list localname)) (setq name (concat (file-name-as-directory dir) name)))
(setq localname (concat "~/" localname))) ;; If connection is not established yet, run the real handler.
;; Tilde expansion if necessary. This needs a shell which (if (not (tramp-connectable-p name))
;; groks tilde expansion! The function `tramp-find-shell' is (tramp-run-real-handler #'expand-file-name (list name nil))
;; supposed to find such a shell on the remote host. Please ;; Dissect NAME.
;; tell me about it when this doesn't work on your system. (with-parsed-tramp-file-name name nil
(when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname) (unless (tramp-run-real-handler #'file-name-absolute-p (list localname))
(let ((uname (match-string 1 localname)) (setq localname (concat "~/" localname)))
(fname (match-string 2 localname))) ;; Tilde expansion if necessary. This needs a shell which
;; We cannot simply apply "~/", because under sudo "~/" is ;; groks tilde expansion! The function `tramp-find-shell' is
;; expanded to the local user home directory but to the ;; supposed to find such a shell on the remote host. Please
;; root home directory. On the other hand, using always ;; tell me about it when this doesn't work on your system.
;; the default user name for tilde expansion is not (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
;; appropriate either, because ssh and companions might (let ((uname (match-string 1 localname))
;; use a user name from the config file. (fname (match-string 2 localname)))
(when (and (string-equal uname "~") ;; We cannot simply apply "~/", because under sudo "~/" is
(string-match-p "\\`su\\(do\\)?\\'" method)) ;; expanded to the local user home directory but to the
(setq uname (concat uname user))) ;; root home directory. On the other hand, using always
(setq uname ;; the default user name for tilde expansion is not
(with-tramp-connection-property v uname ;; appropriate either, because ssh and companions might
(tramp-send-command ;; use a user name from the config file.
v (format "cd %s && pwd" (tramp-shell-quote-argument uname))) (when (and (string-equal uname "~")
(with-current-buffer (tramp-get-buffer v) (string-match-p "\\`su\\(do\\)?\\'" method))
(goto-char (point-min)) (setq uname (concat uname user)))
(buffer-substring (point) (point-at-eol))))) (setq uname
(setq localname (concat uname fname)))) (with-tramp-connection-property v uname
;; There might be a double slash, for example when "~/" (tramp-send-command
;; expands to "/". Remove this. v
(while (string-match "//" localname) (format "cd %s && pwd" (tramp-shell-quote-argument uname)))
(setq localname (replace-match "/" t t localname))) (with-current-buffer (tramp-get-buffer v)
;; Do not keep "/..". (goto-char (point-min))
(when (string-match-p "^/\\.\\.?$" localname) (buffer-substring (point) (point-at-eol)))))
(setq localname "/")) (setq localname (concat uname fname))))
;; No tilde characters in file name, do normal ;; There might be a double slash, for example when "~/"
;; `expand-file-name' (this does "/./" and "/../"). ;; expands to "/". Remove this.
;; `default-directory' is bound, because on Windows there would (while (string-match "//" localname)
;; be problems with UNC shares or Cygwin mounts. (setq localname (replace-match "/" t t localname)))
(let ((default-directory (tramp-compat-temporary-file-directory))) ;; Do not keep "/..".
(tramp-make-tramp-file-name (when (string-match-p "^/\\.\\.?$" localname)
v (tramp-drop-volume-letter (setq localname "/"))
(tramp-run-real-handler ;; No tilde characters in file name, do normal
#'expand-file-name (list localname)))))))) ;; `expand-file-name' (this does "/./" and "/../").
;; `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)))))))))
;;; Remote commands: ;;; Remote commands:

View file

@ -2610,14 +2610,7 @@ Fall back to normal file name handler if no Tramp file name handler exists."
;; When `tramp-mode' is not enabled, or the file name is quoted, ;; When `tramp-mode' is not enabled, or the file name is quoted,
;; we don't do anything. ;; we don't do anything.
;; When operation is `expand-file-name', and the first argument (tramp-run-real-handler operation args))))
;; is a local absolute file name, we end also here. Handle the
;; MS Windows case.
(funcall
(if (and (eq operation 'expand-file-name)
(not (string-match-p "\\`[[:alpha:]]:/" (car args))))
#'tramp-drop-volume-letter #'identity)
(tramp-run-real-handler operation args)))))
(defun tramp-completion-file-name-handler (operation &rest args) (defun tramp-completion-file-name-handler (operation &rest args)
"Invoke Tramp file name completion handler for OPERATION and ARGS. "Invoke Tramp file name completion handler for OPERATION and ARGS.