]> git.eshelyaron.com Git - emacs.git/commitdiff
with-connection-local-variables: Avoid code duplication
authorStefan Monnier <monnier@iro.umontreal.ca>
Sat, 28 May 2022 16:02:15 +0000 (12:02 -0400)
committerStefan Monnier <monnier@iro.umontreal.ca>
Sat, 28 May 2022 16:02:15 +0000 (12:02 -0400)
Move the bulk of the code of `with-connection-local-variables` into
a separate function, which both avoids duplicating that code but also
avoids duplicating the code passed as the body of
a `with-connection-local-variables`.  Also makes it easier to
debug the code, or change the implementation of
`with-connection-local-variables` without having to recompile all
the users.

* lisp/files-x.el (with-connection-local-variables-1): New function,
extracted from `with-connection-local-variables`.
(with-connection-local-variables): Use it.

lisp/files-x.el

index 0ae9fb076eb50651ef6702a491cd4e9b175c142f..4db6fbd22cc1793e82903c4c0b89b212839fdf1d 100644 (file)
@@ -740,22 +740,28 @@ If APPLICATION is nil, `connection-local-default-application' is used."
   "Apply connection-local variables according to `default-directory'.
 Execute BODY, and unwind connection-local variables."
   (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))
+  `(with-connection-local-variables-1 (lambda () ,@body)))
+
+;;;###autoload
+(defun with-connection-local-variables-1 (body-fun)
+  "Apply connection-local variables according to `default-directory'.
+Call BODY-FUN with no args, and then unwind connection-local variables."
+  (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
+            (funcall body-fun)
+         ;; 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.
+    (funcall body-fun)))
 
 ;;;###autoload
 (defun path-separator ()