branch: externals/crdt commit fae016ba25b9118eed12183a4e705a2b938b9e5a Author: Qiantan Hong <qh...@mit.edu> Commit: Qiantan Hong <qh...@mit.edu>
Preliminary url support --- crdt.el | 45 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/crdt.el b/crdt.el index 9878786a71..b04c69c20e 100644 --- a/crdt.el +++ b/crdt.el @@ -34,6 +34,7 @@ (require 'cl-lib) (require 'url) (require 'color) +(require 'tramp) (defconst crdt-version "0.2.5") (defconst crdt-protocol-version "0.2.5") @@ -547,7 +548,7 @@ after synchronization is completed." (with-current-buffer crdt-buffer ,@body) (unless (process-contact (crdt--session-network-process crdt--session) :server) - (setq crdt-buffer (generate-new-buffer (format "crdt - %s" ,name))) + (setq crdt-buffer (create-file-buffer (concat (crdt--tramp-prefix crdt--session) ,name))) (puthash ,name crdt-buffer (crdt--session-buffer-table crdt--session)) (let ((session crdt--session)) (with-current-buffer crdt-buffer @@ -1942,17 +1943,12 @@ If SESSION is nil, stop sharing the current session." (list (crdt--read-session-maybe 'server))) (crdt--stop-session (or session crdt--session))) -(defun crdt-copy-url (&optional session-name) - "Copy the url for the session with SESSION-NAME. +(defun crdt-copy-url (&optional session) + "Copy the url for the SESSION. Currently this only work if a tuntox proxy is used." (interactive - (list (completing-read "Choose a server session: " - (crdt--get-session-names t) nil t - (when (and crdt--session (crdt--server-p)) - (crdt--session-name crdt--session))))) - (let* ((session (if session-name - (crdt--get-session session-name) - crdt--session)) + (list (crdt--read-session-maybe 'server))) + (let* ((session (or session crdt--session)) (network-process (crdt--session-network-process session)) (tuntox-process (process-get network-process 'tuntox-process))) (if tuntox-process @@ -2676,6 +2672,35 @@ Use CRDT--UNINSTALL-PROCESS-ADVICES to disable those advices for the rescue." (crdt--with-buffer-name buffer-name (process-send-string (get-buffer-process (current-buffer)) string))) +;;; URL and TRAMP + +(defsubst tramp-crdt-file-name-p (filename) + "Check if it's a FILENAME for crdt.el." + (and (tramp-tramp-file-p filename) + (string= (tramp-file-name-method (tramp-dissect-file-name filename)) "crdt"))) + +(defconst tramp-crdt-file-name-handler-alist '() + "Alist of handler functions for Tramp crdt.el method. +Operations not mentioned here will be handled by the default Emacs primitives.") + +(defun tramp-crdt-file-name-handler (operation &rest args) + "Invoke the crdt.el handler for OPERATION and ARGS. +First arg specifies the OPERATION, second arg is a list of arguments to +pass to the OPERATION." + (if-let ((fn (assoc operation tramp-crdt-file-name-handler-alist))) + (save-match-data (apply (cdr fn) args)) + (tramp-run-real-handler operation args))) + +(tramp-register-foreign-file-name-handler #'tramp-crdt-file-name-p #'tramp-crdt-file-name-handler) +(add-to-list 'tramp-methods '("crdt")) + +(defun crdt--tramp-prefix (session) + "Compute TRAMP filename prefix for SESSION." + (let ((contact (process-contact (crdt--session-network-process session)))) + (let ((ipv6-p (string-match-p ":" (car contact)))) ;; poor man's ipv6 test + (concat "/crdt:" (when ipv6-p "[") (car contact) (when ipv6-p "]") + (if (= (cadr contact) 6530) nil (format "#%s" (cdr contact))) ":/")))) + ;;; Built-in package integrations ;; Org