Michael Albinus <michael.albi...@gmx.de> writes:

Hi Björn,

> Well. It might be possible that SomeBody [TM] writes an ELPA package,
> offereing local processes for all Tramp methods which use a mounted FUSE
> file system. This would include sshfs, rclone, and all other methods
> listed in tramp-gvfs-methods. All of them (but sshfs) don't support
> processes so far.

I gave it a try. Enclosed is a proof-of-concept, which implements local
processes for Tramp methods using FUSE mounts. Could you please play
with it, and comment?

Best regards, Michael.

;;; tramp-locproc.el --- Tramp local processes for FUSE mounts  -*- 
lexical-binding:t -*-

;; Copyright (C) 2024 Free Software Foundation, Inc.

;; Author: Michael Albinus <michael.albi...@gmx.de>
;; Keywords: comm, processes
;; Package: tramp-locproc
;; Version: 0
;; Package-Requires: ((tramp "2.7"))

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; This package adds process support for "sshfs", "rclone" and all
;; methods defined in `tramp-gvfs-methods'.  It overwrites the remote
;; process support in "sshfs"; the other methods have no process
;; support otherwise.

;;; Code:

(require 'tramp)

(with-eval-after-load 'tramp-gvfs
  (setcdr (assoc 'exec-path tramp-gvfs-file-name-handler-alist)
          #'tramp-locproc-handle-exec-path)
  (setcdr (assoc 'make-process tramp-gvfs-file-name-handler-alist)
          #'tramp-locproc-handle-make-process)
  (setcdr (assoc 'process-file tramp-gvfs-file-name-handler-alist)
          #'tramp-locproc-handle-process-file)
  (setcdr (assoc 'shell-command tramp-gvfs-file-name-handler-alist)
          #'tramp-locproc-handle-shell-command))

(with-eval-after-load 'tramp-rclone
  (setcdr (assoc 'exec-path tramp-rclone-file-name-handler-alist)
          #'tramp-locproc-handle-exec-path)
  (setcdr (assoc 'make-process tramp-rclone-file-name-handler-alist)
          #'tramp-locproc-handle-make-process)
  (setcdr (assoc 'process-file tramp-rclone-file-name-handler-alist)
          #'tramp-locproc-handle-process-file)
  (setcdr (assoc 'shell-command tramp-rclone-file-name-handler-alist)
          #'tramp-locproc-handle-shell-command))

(with-eval-after-load 'tramp-sshfs
  (setcdr (assoc 'exec-path tramp-sshfs-file-name-handler-alist)
          #'tramp-locproc-handle-exec-path)
  (setcdr (assoc 'make-process tramp-sshfs-file-name-handler-alist)
          #'tramp-locproc-handle-make-process)
  (setcdr (assoc 'process-file tramp-sshfs-file-name-handler-alist)
          #'tramp-locproc-handle-process-file)
  (setcdr (assoc 'shell-command tramp-sshfs-file-name-handler-alist)
          #'tramp-locproc-handle-shell-command))

(declare-function tramp-gvfs-maybe-open-connection 'tramp-gvfs)
(declare-function tramp-fuse-local-file-name 'tramp-fuse)

;; This should go to tramp-gvfs.el.
(defun tramp-gvfs-local-file-name (filename)
  "Return local mount name of FILENAME."
  (setq filename (file-name-unquote (expand-file-name filename)))
  (with-parsed-tramp-file-name filename nil
    (with-tramp-file-property v localname "local-file-name"
      ;; As long as we call `tramp-gvfs-maybe-open-connection' here,
      ;; we cache the result.
      (tramp-gvfs-maybe-open-connection v)
      (let ((quoted (file-name-quoted-p localname))
            (localname (file-name-unquote localname)))
        (funcall
         (if quoted #'file-name-quote #'identity)
         (expand-file-name
          (if (file-name-absolute-p localname)
              (substring localname 1) localname)
          (tramp-get-file-property v "/" "fuse-mountpoint")))))))

(defun tramp-locproc-local-file-name (filename)
  "Return local mount name of FILENAME."
  (funcall
   (if (tramp-gvfs-file-name-p filename)
       #'tramp-gvfs-local-file-name #'tramp-fuse-local-file-name)
   filename))

(defun tramp-locproc-handle-exec-path ()
  "Like `exec-path' for Tramp files."
  exec-path)

(defun tramp-locproc-handle-make-process (&rest args)
  "An alternative `make-process' implementation for Tramp files."
  (let ((default-directory
         (tramp-locproc-local-file-name default-directory)))
    (apply #'make-process args)))

(defun tramp-locproc-handle-process-file (&rest args)
  "Like `process-file' for Tramp files."
  (let ((default-directory
         (tramp-locproc-local-file-name default-directory)))
    (apply #'process-file args)))

(defun tramp-locproc-handle-shell-command (&rest args)
  "An alternative `shell-command' implementation for Tramp files."
  (let ((default-directory
         (tramp-locproc-local-file-name default-directory)))
    (apply #'shell-command args)))

(add-hook 'tramp-unload-hook
          (lambda ()
            (unload-feature 'tramp-locproc 'force)))

(provide 'tramp-locproc.el)

;;; tramp-locproc.el.el ends here

Reply via email to