]> git.eshelyaron.com Git - emacs.git/commitdiff
Do not hardcode "/bin/sh" in compile. Bug#24338, Bug#29723
authorMichael Albinus <michael.albinus@gmx.de>
Sat, 9 Mar 2019 15:44:24 +0000 (16:44 +0100)
committerMichael Albinus <michael.albinus@gmx.de>
Sat, 9 Mar 2019 15:44:24 +0000 (16:44 +0100)
* doc/emacs/custom.texi (Connection Variables): New node.

* doc/emacs/emacs.texi (Top): Add entry for Connection Variables.

* doc/emacs/misc.texi (Single Shell): Mention default value for
remote buffers.

* doc/lispref/variables.texi (Connection Local Variables):
Describe `with-connection-local-variables' instead of
`with-connection-local-profiles'.

* doc/misc/tramp.texi (Remote processes): Refer to Emacs manual.
Mention default connection-local settings for `shell-file-name'
and `shell-command-switch'.

* etc/NEWS: Mention connection-local variables changes.

* lisp/files-x.el (hack-connection-local-variables):
Push connection-local variables to `file-local-variables-alist'.
(connection-local-criteria-for-default-directory): New defsubst.
(with-connection-local-variables): Rename from
`with-connection-local-profiles'.  Adapt implementation.

* lisp/files.el (hack-local-variables):
Call `hack-connection-local-variables'.

* lisp/shell.el (shell): Use `with-connection-local-variables'.

* lisp/subr.el (start-file-process-shell-command):
* lisp/progmodes/compile.el (compilation-start):
Use `with-connection-local-variables'.  Do not set "/bin/sh" for
remote buffers, trust settings of `shell-file-name'.  (Bug#24338),
(Bug#29723)

* lisp/net/ange-ftp.el (ange-ftp-compress, ange-ftp-uncompress):
Use `shell-command-switch'.

* lisp/net/tramp-adb.el (tramp-adb-connection-local-default-profile):
New defvar.  Add it to connection-local profiles after loading "shell".

* lisp/net/tramp-integration.el (tramp-compat): Require tramp-compat.
(tramp-compat-exec-path): Do not declare anymore.
(tramp-connection-local-safe-shell-file-names): New defvar.
(tramp-connection-local-default-profile): New defconst.  Activate
it after loading "shell".
(shell-file-name, shell-command-switch): Add safe-local-variable
property.

* lisp/net/tramp-sh.el (tramp-display-escape-sequence-regexp):
Add tramp-autoload cookie.

* test/lisp/files-x-tests.el (remote-shell-file-name):
Add safe-local-variable property to remote-* variables.
(tramp-connection-local-default-profile): Declare.
(files-x-test-with-connection-local-variables):
Rename from `files-x-test-with-connection-local-profiles'.  Adapt
implementation.

* test/lisp/net/tramp-tests.el
(tramp-test34-connection-local-variables): New test.
(tramp-test34-explicit-shell-file-name): Run it also for tramp-adb.
Bind connection-local-{profile,criteria}-alist.  Use tramp-adb
specific `shell-file-name'.  Add safe-local-variable property to
`explicit-shell-file-name' and `explicit-sh-args'.

17 files changed:
doc/emacs/custom.texi
doc/emacs/emacs.texi
doc/emacs/misc.texi
doc/lispref/variables.texi
doc/misc/tramp.texi
etc/NEWS
lisp/files-x.el
lisp/files.el
lisp/net/ange-ftp.el
lisp/net/tramp-adb.el
lisp/net/tramp-integration.el
lisp/net/tramp-sh.el
lisp/progmodes/compile.el
lisp/shell.el
lisp/subr.el
test/lisp/files-x-tests.el
test/lisp/net/tramp-tests.el

index 474149fcae6cc596a91d9ec1e147f2b55a234eab..c649c17029316211be881146be243f27634d54e6 100644 (file)
@@ -765,6 +765,8 @@ expects (@pxref{Examining}).
 * Locals::              Per-buffer values of variables.
 * File Variables::      How files can specify variable values.
 * Directory Variables:: How variable values can be specified by directory.
+* Connection Variables:: Variables which are valid for buffers with a
+                           remote default directory.
 @end menu
 
 @node Examining
@@ -1421,6 +1423,52 @@ variables are handled in the same way as unsafe file-local variables
 do not visit a file directly but perform work within a directory, such
 as Dired buffers (@pxref{Dired}).
 
+@node Connection Variables
+@subsection Per-Connection Local Variables
+@cindex local variables, for all remote connections
+@cindex connection-local variables
+@cindex per-connection local variables
+
+  Most of the variables reflect the situation on the local machine.
+Often, they must use a different value when you operate in buffers
+with a remote default directory.  Think about the shell to be applied
+when calling @code{shell} -- it might be @file{/bin/bash} on your
+local machine, and @file{/bin/ksh} on a remote machine.
+
+  This can be accomplished with @dfn{connection-local variables}.
+Directory and file local variables override connection-local
+variables.  Unsafe connection-local variables are handled in the same
+way as unsafe file-local variables (@pxref{Safe File Variables}).
+
+@findex connection-local-set-profile-variables
+@findex connection-local-set-profiles
+  Connection-local variables are declared as a group of
+variables/value pairs in a @dfn{profile}, using the
+@code{connection-local-set-profile-variables} function.  The function
+@code{connection-local-set-profiles} activates profiles for a given
+criteria, identifying a remote machine:
+
+@example
+(connection-local-set-profile-variables 'remote-ksh
+   '((shell-file-name . "/bin/ksh")
+     (shell-command-switch . "-c")))
+
+(connection-local-set-profile-variables 'remote-bash
+   '((shell-file-name . "/bin/bash")
+     (shell-command-switch . "-c")))
+
+(connection-local-set-profiles
+   '(:application tramp :machine "remotemachine") 'remote-ksh)
+@end example
+
+  This code declares two different profiles, @code{remote-ksh} and
+@code{remote-bash}. The profile @code{remote-ksh} is applied to all
+buffers which have a remote default directory matching the regexp
+@code{"remotemachine} as host name.  Such a criteria can also
+discriminate for the properties @code{:protocol} (this is the Tramp
+method) or @code{:user} (a remote user name).  The @code{nil} criteria
+matches all buffers with a remote default directory.
+
 @node Key Bindings
 @section Customizing Key Bindings
 @cindex key bindings
index 950ddc01123b7dee617cf2c095836d759434edad..7edc1a5fae15b552de178027c10e7768e634846f 100644 (file)
@@ -1135,6 +1135,8 @@ Variables
 * Locals::              Per-buffer values of variables.
 * File Variables::      How files can specify variable values.
 * Directory Variables:: How variable values can be specified by directory.
+* Connection Variables:: Variables which are valid for buffers with a
+                           remote default directory.
 
 Local Variables in Files
 
index a3aa71e267203595269e6fc4c9934a93e04a67c4..7d7065a441afe526bb21884542ec9b93688f209b 100644 (file)
@@ -795,6 +795,10 @@ to @command{gpg}.  This will output the list of keys to the
 name is relative, Emacs searches the directories listed in
 @code{exec-path} (@pxref{Shell}).
 
+  If the default directory is remote (@pxref{Remote Files}), the
+default value is @file{/bin/sh}.  This can be changed by declaring
+@code{shell-file-name} connection-local (@pxref{Connection Variables}).
+
   To specify a coding system for @kbd{M-!} or @kbd{M-|}, use the command
 @kbd{C-x @key{RET} c} immediately beforehand.  @xref{Communication Coding}.
 
index f1e0e37e6d6dc918667e4842566f3c66e1f52487..aca7d2f5e936f06c7df20fcf0ce50154e68fe9c2 100644 (file)
@@ -2191,9 +2191,9 @@ This function looks for connection-local variables according to
 @var{criteria}, and immediately applies them in the current buffer.
 @end defun
 
-@defmac with-connection-local-profiles profiles &rest body
-All connection-local variables, which are specified by a connection
-profile in @var{profiles}, are applied.
+@defmac with-connection-local-variables &rest body
+All connection-local variables, which are specified by
+@code{default-directory}, are applied.
 
 After that, @var{body} is executed, and the connection-local variables
 are unwound.  Example:
@@ -2207,8 +2207,15 @@ are unwound.  Example:
 @end group
 
 @group
-(with-connection-local-profiles '(remote-perl)
-  do something useful)
+(connection-local-set-profiles
+  '(:application 'tramp :protocol "ssh" :machine "remotehost")
+  'remote-perl)
+@end group
+
+@group
+(let ((default-directory "/ssh:remotehost:/working/dir/"))
+  (with-connection-local-variables
+    do something useful))
 @end group
 @end example
 @end defmac
index db3b3f7fee5f4a2e205288b6892b2eed2502f639..ea6ad15dc3d93a31533fe7a281d14d143b48cdb9 100644 (file)
@@ -2970,7 +2970,7 @@ Starting with Emacs 26, you could use connection-local variables for
 setting different values of @code{explicit-shell-file-name} for
 different remote hosts.
 @ifinfo
-@pxref{Connection Local Variables, , , elisp}
+@xref{Connection Variables, , , emacs}
 @end ifinfo
 
 @lisp
@@ -3023,6 +3023,14 @@ host.  Example:
 @kbd{M-x auto-revert-tail-mode @key{RET}} runs similarly showing
 continuous output.
 
+@code{shell-command} uses the variables @code{shell-file-name} and
+@code{shell-command-switch} in order to determine which shell to run.
+For remote hosts, their default values are @file{/bin/sh} and
+@option{-c}, respectively (except for the @option{adb} method, which
+uses @file{/system/bin/sh}).  Like the variables in the previous
+section, these variables can be changed via connection-local
+variables.
+
 
 @subsection Running @code{eshell} on a remote host
 @cindex @code{eshell}
index 1095ecc7e5ebf25fd52c66ee80d13f78c33f4f05..0a1d9ea2220907a63c7f5b80036a3ceebe778597 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -279,6 +279,17 @@ matches strings where the pattern appears as a subsequence.  Put
 simply, makes "foo" complete to both "barfoo" and "frodo".  Add 'flex'
 to 'completion-styles' or 'completion-category-overrides' to use it.
 
+** Connection-local variables
+
++++
+*** Connection-local variables are applied by default like file-local
+and directory-local variables.
+
++++
+*** The macro 'with-connection-local-variables' has been renamed from
+'with-connection-local-profiles'.  No argument 'profiles' needed any
+longer.
+
 \f
 * Editing Changes in Emacs 27.1
 
@@ -994,7 +1005,12 @@ followed when Emacs writes the relevant history variables to the disk.
 ---
 *** Program name completion inside remote shells works now as expected.
 
++++
+*** The variable 'shell-file-name' can be set now as connection-local
+variable for remote shells.  It still defaults to "/bin/sh".
+
 ** Pcomplete
+
 *** The function 'pcomplete-uniquify-list' has been renamed from
 'pcomplete-uniqify-list'.
 
index c9abb6957006189062666f30243b63406be089cb..eedf630b71f33b57fe69e94e80c55f0621cbff3b 100644 (file)
@@ -582,7 +582,7 @@ changed by the user.")
 (setq ignored-local-variables
       (cons 'connection-local-variables-alist ignored-local-variables))
 
-(defvar connection-local-profile-alist '()
+(defvar connection-local-profile-alist nil
   "Alist mapping connection profiles to variable lists.
 Each element in this list has the form (PROFILE VARIABLES).
 PROFILE is the name of a connection profile (a symbol).
@@ -590,7 +590,7 @@ VARIABLES is a list that declares connection-local variables for
 PROFILE.  An element in VARIABLES is an alist whose elements are
 of the form (VAR . VALUE).")
 
-(defvar connection-local-criteria-alist '()
+(defvar connection-local-criteria-alist nil
   "Alist mapping connection criteria to connection profiles.
 Each element in this list has the form (CRITERIA PROFILES).
 CRITERIA is a plist identifying a connection and the application
@@ -685,7 +685,9 @@ This does nothing if `enable-connection-local-variables' is nil."
       ;; Loop over variables.
       (dolist (variable (connection-local-get-profile-variables profile))
         (unless (assq (car variable) connection-local-variables-alist)
-          (push variable connection-local-variables-alist))))))
+          (push variable connection-local-variables-alist))))
+    ;; Push them to `file-local-variables-alist'.
+    (hack-local-variables-filter connection-local-variables-alist nil)))
 
 ;;;###autoload
 (defun hack-connection-local-variables-apply (criteria)
@@ -697,24 +699,35 @@ will not be changed."
         (copy-tree connection-local-variables-alist)))
    (hack-local-variables-apply)))
 
+(defsubst connection-local-criteria-for-default-directory ()
+  "Return a connection-local criteria, which represents `default-directory'."
+  (when (file-remote-p default-directory)
+    `(:application tramp
+       :protocol ,(file-remote-p default-directory 'method)
+       :user     ,(file-remote-p default-directory 'user)
+       :machine  ,(file-remote-p default-directory 'host))))
+
 ;;;###autoload
-(defmacro with-connection-local-profiles (profiles &rest body)
-  "Apply connection-local variables according to PROFILES in current buffer.
+(defmacro with-connection-local-variables (&rest body)
+  "Apply connection-local variables according to `default-directory'.
 Execute BODY, and unwind connection-local variables."
-  (declare (indent 1) (debug t))
-  `(let ((enable-connection-local-variables t)
-         (old-buffer-local-variables (buffer-local-variables))
-        connection-local-variables-alist connection-local-criteria-alist)
-     (apply 'connection-local-set-profiles nil ,profiles)
-     (hack-connection-local-variables-apply nil)
-     (unwind-protect
-         (progn ,@body)
-       ;; Cleanup.
-       (dolist (variable connection-local-variables-alist)
-        (let ((elt (assq (car variable) old-buffer-local-variables)))
-          (if elt
-              (set (make-local-variable (car elt)) (cdr elt))
-           (kill-local-variable (car variable))))))))
+  (declare (debug t))
+  `(if (file-remote-p default-directory)
+       (let ((enable-connection-local-variables t)
+             (old-buffer-local-variables (buffer-local-variables))
+            connection-local-variables-alist)
+        (hack-connection-local-variables-apply
+         (connection-local-criteria-for-default-directory))
+        (unwind-protect
+             (progn ,@body)
+          ;; Cleanup.
+          (dolist (variable connection-local-variables-alist)
+            (let ((elt (assq (car variable) old-buffer-local-variables)))
+              (if elt
+                  (set (make-local-variable (car elt)) (cdr elt))
+                (kill-local-variable (car variable)))))))
+     ;; No connection-local variables to apply.
+     ,@body))
 
 \f
 
index 9948bd4a03438087f492d8341412876ae97c8157..77a194b085d030dba2a03104f85e8b451d871ffd 100644 (file)
@@ -3590,6 +3590,11 @@ local variables, but directory-local variables may still be applied."
        result)
     (unless (eq handle-mode t)
       (setq file-local-variables-alist nil)
+      (when (file-remote-p default-directory)
+        (with-demoted-errors "Connection-local variables error: %s"
+         ;; Note this is a no-op if enable-local-variables is nil.
+         (hack-connection-local-variables
+           (connection-local-criteria-for-default-directory))))
       (with-demoted-errors "Directory-local variables error: %s"
        ;; Note this is a no-op if enable-local-variables is nil.
        (hack-dir-local-variables)))
index 428bf32947b635f353ce04942782015c5e658672..5af9ea75ed16292bad1ec3638a4d9f20b586258c 100644 (file)
@@ -4277,7 +4277,7 @@ NEWNAME should be the name to give the new compressed or uncompressed file.")
                               nil
                               t
                               nil
-                              "-c"
+                              shell-command-switch
                               (format "compress -f -c < %s > %s" tmp1 tmp2))
          (and ange-ftp-process-verbose
               (ange-ftp-message "Compressing %s...done" abbr))
@@ -4313,7 +4313,7 @@ NEWNAME should be the name to give the new compressed or uncompressed file.")
                               nil
                               t
                               nil
-                              "-c"
+                              shell-command-switch
                               (format "uncompress -c < %s > %s" tmp1 tmp2))
          (and ange-ftp-process-verbose
               (ange-ftp-message "Uncompressing %s...done" abbr))
index eb3295ee352576ea2572f9effa78bed29c2b6018..b40e69ef6344e429b950e03c78f655b99ad9b07e 100644 (file)
@@ -1370,6 +1370,26 @@ connection if a previous connection has died for some reason."
            ;; Mark it as connected.
            (tramp-set-connection-property p "connected" t)))))))
 
+;; Default settings for connection-local variables.
+(defconst tramp-adb-connection-local-default-profile
+  '((shell-file-name . "/system/bin/sh")
+    (shell-command-switch . "-c"))
+  "Default connection-local variables for remote adb connections.")
+(add-to-list 'tramp-connection-local-safe-shell-file-names "/system/bin/sh")
+
+;; `connection-local-set-profile-variables' and
+;; `connection-local-set-profiles' exists since Emacs 26.1.
+(eval-after-load "shell"
+  '(progn
+     (tramp-compat-funcall
+      'connection-local-set-profile-variables
+      'tramp-adb-connection-local-default-profile
+      tramp-adb-connection-local-default-profile)
+     (tramp-compat-funcall
+      'connection-local-set-profiles
+      `(:application tramp :protocol ,tramp-adb-method)
+      'tramp-adb-connection-local-default-profile)))
+
 (add-hook 'tramp-unload-hook
          (lambda ()
            (unload-feature 'tramp-adb 'force)))
index 2a4617634805cbda5ef0200a34a5cced2c7a5630..946d7f8cbab4a3ae5658e6a5c75b45938f116d05 100644 (file)
 
 ;;; Code:
 
+(require 'tramp-compat)
+
 ;; Pacify byte-compiler.
 (require 'cl-lib)
-(declare-function tramp-compat-exec-path "tramp")
 (declare-function tramp-dissect-file-name "tramp")
 (declare-function tramp-file-name-equal-p "tramp")
 (declare-function tramp-tramp-file-p "tramp")
@@ -170,6 +171,37 @@ NAME must be equal to `tramp-current-connection'."
                 (remove-hook 'tramp-cleanup-all-connections-hook
                              #'tramp-recentf-cleanup-all)))))
 
+;;; Default connection-local variables for Tramp:
+
+;;;###tramp-autoload
+(defvar tramp-connection-local-safe-shell-file-names nil
+  "List of safe `shell-file-name' values for remote hosts.")
+(add-to-list 'tramp-connection-local-safe-shell-file-names "/bin/sh")
+
+(defconst tramp-connection-local-default-profile
+  '((shell-file-name . "/bin/sh")
+    (shell-command-switch . "-c"))
+  "Default connection-local variables for remote connections.")
+(put 'shell-file-name 'safe-local-variable
+     (lambda (item)
+       (and (stringp item)
+           (member item tramp-connection-local-safe-shell-file-names))))
+(put 'shell-command-switch 'safe-local-variable
+     (lambda (item) (and (stringp item) (string-equal item "-c"))))
+
+;; `connection-local-set-profile-variables' and
+;; `connection-local-set-profiles' exists since Emacs 26.1.
+(eval-after-load "shell"
+  '(progn
+     (tramp-compat-funcall
+      'connection-local-set-profile-variables
+      'tramp-connection-local-default-profile
+      tramp-connection-local-default-profile)
+     (tramp-compat-funcall
+      'connection-local-set-profiles
+      `(:application tramp)
+      'tramp-connection-local-default-profile)))
+
 (add-hook 'tramp-unload-hook
          (lambda () (unload-feature 'tramp-integration 'force)))
 
index ee16138f7006ea7066bd77ba5fedf8154eddc02f..d9edcb14198603f18669712f4bca6062d9025668 100644 (file)
@@ -81,6 +81,7 @@ the default storage location, e.g. \"$HOME/.sh_history\"."
                  (const :tag "Unset HISTFILE" t)
                  (string :tag "Redirect to a file")))
 
+;;;###tramp-autoload
 (defconst tramp-display-escape-sequence-regexp "\e[[;0-9]+m"
   "Terminal control escape sequences for display attributes.")
 
index b3f32c82316e1e410a0d36bfc305ddad5d3cefa7..3650b05607c2bf292cece4151ad8b904535fd352 100644 (file)
@@ -1754,15 +1754,16 @@ Returns the compilation buffer created."
        (if (fboundp 'make-process)
            (let ((proc
                   (if (eq mode t)
-                      ;; comint uses `start-file-process'.
-                      (get-buffer-process
-                       (with-no-warnings
-                         (comint-exec
-                          outbuf (downcase mode-name)
-                          (if (file-remote-p default-directory)
-                              "/bin/sh"
-                            shell-file-name)
-                          nil `("-c" ,command))))
+                       ;; On remote hosts, the local `shell-file-name'
+                       ;; might be useless.
+                       (with-connection-local-variables
+                       ;; comint uses `start-file-process'.
+                       (get-buffer-process
+                        (with-no-warnings
+                          (comint-exec
+                           outbuf (downcase mode-name)
+                           shell-file-name
+                           nil `(,shell-command-switch ,command)))))
                     (start-file-process-shell-command (downcase mode-name)
                                                       outbuf command))))
               ;; Make the buffer's mode line show process state.
index 524a8848840d21645997a7625d4a6b705a1a07ea..8a2d4489b8a6d053cc7380886a96d3a0a60c28db 100644 (file)
@@ -99,6 +99,7 @@
 
 (require 'comint)
 (require 'pcomplete)
+(eval-when-compile (require 'files-x)) ;with-connection-local-variables
 
 ;;; Customization and Buffer Variables
 
@@ -721,23 +722,17 @@ Otherwise, one argument `-i' is passed to the shell.
 
   (with-current-buffer buffer
     (when (file-remote-p default-directory)
-      ;; Apply connection-local variables.
-      (hack-connection-local-variables-apply
-       `(:application tramp
-         :protocol ,(file-remote-p default-directory 'method)
-         :user ,(file-remote-p default-directory 'user)
-         :machine ,(file-remote-p default-directory 'host)))
-
       ;; On remote hosts, the local `shell-file-name' might be useless.
-      (if (and (called-interactively-p 'any)
-               (null explicit-shell-file-name)
-               (null (getenv "ESHELL")))
-          (set (make-local-variable 'explicit-shell-file-name)
-               (file-local-name
-               (expand-file-name
-                 (read-file-name
-                  "Remote shell path: " default-directory shell-file-name
-                  t shell-file-name)))))))
+      (with-connection-local-variables
+       (if (and (called-interactively-p 'any)
+                (null explicit-shell-file-name)
+                (null (getenv "ESHELL")))
+           (set (make-local-variable 'explicit-shell-file-name)
+                (file-local-name
+                (expand-file-name
+                  (read-file-name
+                   "Remote shell path: " default-directory shell-file-name
+                   t shell-file-name))))))))
 
   ;; The buffer's window must be correctly set when we call comint
   ;; (so that comint sets the COLUMNS env var properly).
index 5b0330745fa4730cfd2f569fc3c62f6937cea26e..4024c68e68d45a67296ed0828a7570cbf1314ad8 100644 (file)
@@ -3185,11 +3185,12 @@ discouraged."
   "Start a program in a subprocess.  Return the process object for it.
 Similar to `start-process-shell-command', but calls `start-file-process'."
   (declare (advertised-calling-convention (name buffer command) "23.1"))
-  (start-file-process
-   name buffer
-   (if (file-remote-p default-directory) "/bin/sh" shell-file-name)
-   (if (file-remote-p default-directory) "-c" shell-command-switch)
-   (mapconcat 'identity args " ")))
+  ;; On remote hosts, the local `shell-file-name' might be useless.
+  (with-connection-local-variables
+   (start-file-process
+    name buffer
+    shell-file-name shell-command-switch
+    (mapconcat 'identity args " "))))
 
 (defun call-process-shell-command (command &optional infile buffer display
                                           &rest args)
index d678be409d138402783618358cec2bf444ad29ba..568a89844792a641c55785d9c43127563ad999c4 100644 (file)
   '((remote-null-device . "/dev/null")))
 (defconst files-x-test--variables4
   '((remote-null-device . "null")))
+(put 'remote-shell-file-name 'safe-local-variable #'identity)
+(put 'remote-shell-command-switch 'safe-local-variable #'identity)
+(put 'remote-shell-interactive-switch 'safe-local-variable #'identity)
+(put 'remote-shell-login-switch 'safe-local-variable #'identity)
+(put 'remote-null-device 'safe-local-variable #'identity)
 
 (defconst files-x-test--application '(:application 'my-application))
 (defconst files-x-test--another-application
         (should-not (local-variable-p 'remote-shell-file-name))
         (should-not (boundp 'remote-shell-file-name))))))
 
-(ert-deftest files-x-test-with-connection-local-profiles ()
+(defvar tramp-connection-local-default-profile)
+
+(ert-deftest files-x-test-with-connection-local-variables ()
   "Test setting connection-local variables."
 
   (let (connection-local-profile-alist connection-local-criteria-alist)
          (string-equal (symbol-value 'remote-null-device) "/dev/null"))
 
        ;; A candidate connection-local variable is not bound yet.
-        (should-not (local-variable-p 'remote-shell-command-switch))
-
-       ;; Use the macro.
-        (with-connection-local-profiles '(remote-bash remote-ksh)
-          ;; All connection-local variables are set.  They apply in
-          ;; reverse order in `connection-local-variables-alist'.
-          ;; This variable keeps only the variables to be set inside
-          ;; the macro.
-          (should
-           (equal connection-local-variables-alist
-                  (nreverse (copy-tree files-x-test--variables1))))
-          ;; The variables exist also as local variables.
-          (should (local-variable-p 'remote-shell-file-name))
-          (should (local-variable-p 'remote-shell-command-switch))
-          ;; The proper variable values are set.  The settings from
-          ;; `remote-bash' overwrite the same variables as in
-          ;; `remote-ksh'.
-          (should
-           (string-equal (symbol-value 'remote-shell-file-name) "/bin/bash"))
-          (should
-           (string-equal (symbol-value 'remote-shell-command-switch) "-c")))
-
-        ;; Everything is rewound.  The old variable values are reset.
-        (should
-         (equal connection-local-variables-alist
-               (append
-                (nreverse (copy-tree files-x-test--variables3))
-                (nreverse (copy-tree files-x-test--variables2)))))
-        ;; The variables exist also as local variables.
-        (should (local-variable-p 'remote-shell-file-name))
-        (should (local-variable-p 'remote-null-device))
-        ;; The proper variable values are set.  The settings from
-       ;; `remote-ksh' are back.
-        (should
-         (string-equal (symbol-value 'remote-shell-file-name) "/bin/ksh"))
-        (should
-         (string-equal (symbol-value 'remote-null-device) "/dev/null"))
+        (should-not (local-variable-p 'remote-shell-command-switch))))
 
-       ;; The variable set temporarily is not unbound, again.
-        (should-not (local-variable-p 'remote-shell-command-switch))))))
+    (with-temp-buffer
+      ;; Use the macro.  We need a remote `default-directory'.
+      (let ((enable-connection-local-variables t)
+           (default-directory "/method:host:")
+           (remote-null-device "null"))
+        (should-not connection-local-variables-alist)
+        (should-not (local-variable-p 'remote-shell-file-name))
+        (should-not (local-variable-p 'remote-null-device))
+        (should-not (boundp 'remote-shell-file-name))
+        (should (string-equal (symbol-value 'remote-null-device) "null"))
+
+       (with-connection-local-variables
+        ;; All connection-local variables are set.  They apply in
+        ;; reverse order in `connection-local-variables-alist'.
+        ;; Since we ha a remote default directory, Tramp's settings
+        ;; are appended as well.
+         (should
+          (equal
+           connection-local-variables-alist
+          (append
+           (nreverse (copy-tree files-x-test--variables3))
+           (nreverse (copy-tree files-x-test--variables2))
+            (nreverse (copy-tree tramp-connection-local-default-profile)))))
+         ;; The variables exist also as local variables.
+         (should (local-variable-p 'remote-shell-file-name))
+         (should (local-variable-p 'remote-null-device))
+         ;; The proper variable values are set.
+         (should
+          (string-equal (symbol-value 'remote-shell-file-name) "/bin/ksh"))
+         (should
+          (string-equal (symbol-value 'remote-null-device) "/dev/null")))
+
+       ;; Everything is rewound.  The old variable values are reset.
+       (should-not connection-local-variables-alist)
+       ;; The variables don't exist as local variables.
+       (should-not (local-variable-p 'remote-shell-file-name))
+       (should-not (local-variable-p 'remote-null-device))
+       ;; The variable values are reset.
+       (should-not (boundp 'remote-shell-file-name))
+       (should (string-equal (symbol-value 'remote-null-device) "null"))))))
 
 (provide 'files-x-tests)
 ;;; files-x-tests.el ends here
index 69d5ba8b7df4a44d5610007a14a27c0a80746278..bf7cdfafabec0631644c892eaa90bdc806964cd9 100644 (file)
@@ -4274,12 +4274,78 @@ This tests also `make-symbolic-link', `file-truename' and `add-name-to-file'."
     (dolist (dir '("/mock:localhost#11111:" "/mock:localhost#22222:"))
       (tramp-cleanup-connection (tramp-dissect-file-name dir)))))
 
+;; Connection-local variables are enabled per default since Emacs 27.1.
+(ert-deftest tramp-test34-connection-local-variables ()
+  "Check that connection-local variables are enabled."
+  :tags '(:expensive-test)
+  (skip-unless (tramp--test-enabled))
+  ;; Since Emacs 27.1.
+  (skip-unless (fboundp 'with-connection-local-variables))
+
+  ;; `connection-local-set-profile-variables' and
+  ;; `connection-local-set-profiles' exist since Emacs 26.1.  We don't
+  ;; want to see compiler warnings for older Emacsen.
+  (let* ((default-directory tramp-test-temporary-file-directory)
+        (tmp-name1 (tramp--test-make-temp-name))
+        (tmp-name2 (expand-file-name "foo" tmp-name1))
+        (enable-local-variables :all)
+        (enable-remote-dir-locals t)
+        kill-buffer-query-functions
+        connection-local-profile-alist connection-local-criteria-alist)
+    (unwind-protect
+       (progn
+         (make-directory tmp-name1)
+          (should (file-directory-p tmp-name1))
+
+         ;; `local-variable' is buffer-local due to explicit setting.
+         (with-no-warnings
+           (defvar-local local-variable 'buffer))
+         (with-temp-buffer
+           (should (eq local-variable 'buffer)))
+
+         ;; `local-variable' is connection-local due to Tramp.
+         (write-region "foo" nil tmp-name2)
+         (should (file-exists-p tmp-name2))
+         (with-no-warnings
+           (connection-local-set-profile-variables
+            'local-variable-profile
+            '((local-variable . connect)))
+           (connection-local-set-profiles
+            `(:application tramp
+              :protocol ,(file-remote-p default-directory 'method)
+              :user ,(file-remote-p default-directory 'user)
+              :machine ,(file-remote-p default-directory 'host))
+            'local-variable-profile))
+         (with-current-buffer (find-file-noselect tmp-name2)
+           (should (eq local-variable 'connect))
+           (kill-buffer (current-buffer)))
+
+         ;; `local-variable' is dir-local due to existence of .dir-locals.el.
+         (write-region
+          "((nil . ((local-variable . dir))))" nil
+          (expand-file-name ".dir-locals.el" tmp-name1))
+         (should (file-exists-p (expand-file-name ".dir-locals.el" tmp-name1)))
+         (with-current-buffer (find-file-noselect tmp-name2)
+           (should (eq local-variable 'dir))
+           (kill-buffer (current-buffer)))
+
+         ;; `local-variable' is file-local due to specifying as file variable.
+         (write-region
+          "-*- mode: comint; local-variable: file; -*-" nil tmp-name2)
+          (should (file-exists-p tmp-name2))
+         (with-current-buffer (find-file-noselect tmp-name2)
+           (should (eq local-variable 'file))
+           (kill-buffer (current-buffer))))
+
+      ;; Cleanup.
+      (ignore-errors (delete-directory tmp-name1 'recursive)))))
+
 ;; The functions were introduced in Emacs 26.1.
 (ert-deftest tramp-test34-explicit-shell-file-name ()
   "Check that connection-local `explicit-shell-file-name' is set."
   :tags '(:expensive-test)
   (skip-unless (tramp--test-enabled))
-  (skip-unless (tramp--test-sh-p))
+  (skip-unless (or (tramp--test-adb-p) (tramp--test-sh-p)))
   ;; Since Emacs 26.1.
   (skip-unless (and (fboundp 'connection-local-set-profile-variables)
                    (fboundp 'connection-local-set-profiles)))
@@ -4288,7 +4354,8 @@ This tests also `make-symbolic-link', `file-truename' and `add-name-to-file'."
   ;; `connection-local-set-profiles' exist since Emacs 26.1.  We don't
   ;; want to see compiler warnings for older Emacsen.
   (let ((default-directory tramp-test-temporary-file-directory)
-       explicit-shell-file-name kill-buffer-query-functions)
+       explicit-shell-file-name kill-buffer-query-functions
+       connection-local-profile-alist connection-local-criteria-alist)
     (unwind-protect
        (progn
          ;; `shell-mode' would ruin our test, because it deletes all
@@ -4298,7 +4365,8 @@ This tests also `make-symbolic-link', `file-truename' and `add-name-to-file'."
          (with-no-warnings
            (connection-local-set-profile-variables
             'remote-sh
-            '((explicit-shell-file-name . "/bin/sh")
+            `((explicit-shell-file-name
+               . ,(if (tramp--test-adb-p) "/system/bin/sh" "/bin/sh"))
               (explicit-sh-args . ("-i"))))
            (connection-local-set-profiles
             `(:application tramp
@@ -4306,6 +4374,8 @@ This tests also `make-symbolic-link', `file-truename' and `add-name-to-file'."
               :user ,(file-remote-p default-directory 'user)
               :machine ,(file-remote-p default-directory 'host))
             'remote-sh))
+         (put 'explicit-shell-file-name 'safe-local-variable #'identity)
+         (put 'explicit-sh-args 'safe-local-variable #'identity)
 
          ;; Run interactive shell.  Since the default directory is
          ;; remote, `explicit-shell-file-name' shall be set in order
@@ -4316,6 +4386,7 @@ This tests also `make-symbolic-link', `file-truename' and `add-name-to-file'."
            (call-interactively #'shell)
            (should explicit-shell-file-name)))
 
+      ;; Cleanup.
       (put 'explicit-shell-file-name 'permanent-local nil)
       (kill-buffer "*shell*"))))