]> git.eshelyaron.com Git - emacs.git/commitdiff
New macro connection-local-value
authorMichael Albinus <michael.albinus@gmx.de>
Sat, 9 Dec 2023 09:13:14 +0000 (10:13 +0100)
committerMichael Albinus <michael.albinus@gmx.de>
Sat, 9 Dec 2023 09:13:14 +0000 (10:13 +0100)
* doc/lispref/variables.texi (Applying Connection Local Variables):
Add macro 'connection-local-value'.

* etc/NEWS: Add macro 'connection-local-value'.

* lisp/files-x.el (connection-local-value): New macro.
(path-separator, null-device): Use it.

* test/lisp/files-x-tests.el
(files-x-test-connection-local-value): New test.

doc/lispref/variables.texi
etc/NEWS
lisp/files-x.el
test/lisp/files-x-tests.el

index f575b188fc66aabd92ee0696d062e94eed327fd5..bf5fbe844079bfab513fd9443814b2da75eedb98 100644 (file)
@@ -2545,6 +2545,12 @@ profile.
 This variable must not be changed globally.
 @end defvar
 
+@defmac connection-local-value symbol &optional application
+This macro returns the connection-local value of @var{symbol} for
+@var{application}.  If @var{symbol} does not have a connection-local
+binding, the value is the default binding of the variable.
+@end defmac
+
 @defvar enable-connection-local-variables
 If @code{nil}, connection-local variables are ignored.  This variable
 shall be changed temporarily only in special modes.
index 6e6ada42e46def68e5c43607e46d0d7a53d6f05d..8f3d8dea2a989399476dbb1c0cd790018ee49cce 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1641,6 +1641,13 @@ A 5th argument, optional, has been added to
 'modify-dir-local-variable'.  It can be used to specify which
 dir-locals file to modify.
 
+** Connection local variables
+
++++
+*** New macro 'connection-local-value'.
+This macro returns the connection-local value of a variable if any, or
+its current value.
+
 \f
 * Changes in Emacs 30.1 on Non-Free Operating Systems
 
index a8d525ec5ff97709b58aef2326cb62d5c6613659..b2a9cf9bc5e682700ab7ddf841a37e455adbd8d8 100644 (file)
@@ -925,18 +925,31 @@ earlier in the `setq-connection-local'.  The return value of the
           connection-local-criteria
           connection-local-profile-name-for-setq)))))
 
+;;;###autoload
+(defmacro connection-local-value (variable &optional application)
+  "Return connection-local VARIABLE for APPLICATION in `default-directory'.
+If VARIABLE does not have a connection-local binding, the value
+is the default binding of the variable."
+  (unless (symbolp variable)
+    (signal 'wrong-type-argument (list 'symbolp variable)))
+  `(let (connection-local-variables-alist file-local-variables-alist)
+     (hack-connection-local-variables
+      (connection-local-criteria-for-default-directory ,application))
+     (if-let ((result (assq ',variable connection-local-variables-alist)))
+         (cdr result)
+       ,variable)))
+
 ;;;###autoload
 (defun path-separator ()
   "The connection-local value of `path-separator'."
-  (with-connection-local-variables path-separator))
+  (connection-local-value path-separator))
 
 ;;;###autoload
 (defun null-device ()
   "The connection-local value of `null-device'."
-  (with-connection-local-variables null-device))
+  (connection-local-value null-device))
 
 \f
-
 (provide 'files-x)
 
 ;;; files-x.el ends here
index 4e14ae68fb85f6583e5777e5833f376d564c49c8..795d03a071dff3a02d670ad5872c8d053715970a 100644 (file)
@@ -482,5 +482,66 @@ If it's not initialized yet, initialize it."
      `(connection-local-profile-alist ',clpa now)
      `(connection-local-criteria-alist ',clca now))))
 
+(ert-deftest files-x-test-connection-local-value ()
+  "Test getting connection-local values."
+
+  (let ((clpa connection-local-profile-alist)
+       (clca connection-local-criteria-alist))
+    (connection-local-set-profile-variables
+     'remote-bash files-x-test--variables1)
+    (connection-local-set-profile-variables
+     'remote-ksh files-x-test--variables2)
+    (connection-local-set-profile-variables
+     'remote-nullfile files-x-test--variables3)
+
+    (connection-local-set-profiles
+     nil 'remote-ksh 'remote-nullfile)
+
+    (connection-local-set-profiles
+     files-x-test--application 'remote-bash)
+
+    (with-temp-buffer
+      ;; 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"))
+
+        ;; The proper variable values are set.
+        (should
+         (string-equal
+          (connection-local-value remote-shell-file-name) "/bin/ksh"))
+        (should
+         (string-equal
+          (connection-local-value remote-null-device) "/dev/null"))
+
+        ;; Run with a different application.
+        (should
+         (string-equal
+          (connection-local-value
+           remote-shell-file-name (cadr files-x-test--application))
+          "/bin/bash"))
+        (should
+         (string-equal
+          (connection-local-value
+           remote-null-device (cadr files-x-test--application))
+          "/dev/null"))
+
+        ;; The previous bindings haven't changed.
+        (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"))))
+
+    ;; Cleanup.
+    (custom-set-variables
+     `(connection-local-profile-alist ',clpa now)
+     `(connection-local-criteria-alist ',clca now))))
+
 (provide 'files-x-tests)
 ;;; files-x-tests.el ends here