syscfg/emacs/module/module-remotefs.el

63 lines
2.0 KiB
EmacsLisp

;;; module-remotefs.el --- remotefs automount -*- lexical-binding: t; -*-
;;;
;;; Commentary:
;;;
;;; Automount remotefs on demand.
;;;
;;; Code:
(defvar remote-mounts nil
"Alist of remote mounts. Each entry is (name . (remote-path local-mountpoint)).")
(defun ensure-remote-mounted (name)
"Check if the remote mount NAME is mounted, and if not, mount it."
(let* ((mount-info (assoc name remote-mounts))
(local-path (cadr (cdr mount-info))))
(unless (file-exists-p (expand-file-name ".exists" local-path))
(mount-remote-sshfs name))))
(defun umount-remote-sshfs (name)
"Unmount the remote mount NAME using SSHFS."
(interactive
(list (completing-read "Unmount remote: " remote-mounts nil t)))
(let* ((mount-info (assoc name remote-mounts))
(local-path (cadr (cdr mount-info)))
(default-directory "~"))
(shell-command (format "umount %s" local-path))))
(defun mount-remote-sshfs (name)
"Mount the remote mount NAME using SSHFS."
(interactive
(list (completing-read "Mount remote: " remote-mounts nil t)))
(let* ((mount-info (assoc name remote-mounts))
(remote-path (cadr mount-info))
(local-path (cadr (cdr mount-info)))
(default-directory "~"))
(shell-command (format "sshfs %s %s" remote-path local-path))))
(defun check-and-mount-remote (path)
"Check if PATH is under a remote mount, and ensure it's mounted if so."
(let ((matching-mount
(cl-find-if
(lambda (mount)
(string-prefix-p
(expand-file-name (cadr (cdr mount)))
(expand-file-name path)))
remote-mounts)))
(when matching-mount
(ensure-remote-mounted (car matching-mount)))))
(add-hook 'find-file-hook
(lambda ()
(when buffer-file-name
(check-and-mount-remote buffer-file-name))))
(add-hook 'dired-mode-hook
(lambda ()
(check-and-mount-remote default-directory)))
(provide 'module-remotefs)
;;; module-remotefs.el ends here.