Function: tramp-add-external-operation

tramp-add-external-operation is a byte-compiled function defined in tramp.el.gz.

Signature

(tramp-add-external-operation OPERATION FUNCTION BACKEND)

Documentation

Add FUNCTION to Tramp BACKEND as handler for OPERATION.

OPERATION must not be one of the magic operations listed in Info node (elisp) Magic File Names. FUNCTION must have the same argument list as OPERATION. BACKEND, a symbol, must be one of the Tramp backend packages like tramp-sh (except tramp-ftp).

Probably introduced at or before Emacs version 31.1.

Source Code

;; Defined in /usr/src/emacs/lisp/net/tramp.el.gz
(defun tramp-add-external-operation (operation function backend)
  "Add FUNCTION to Tramp BACKEND as handler for OPERATION.
OPERATION must not be one of the magic operations listed in Info
node `(elisp) Magic File Names'.  FUNCTION must have the same argument
list as OPERATION.  BACKEND, a symbol, must be one of the Tramp backend
packages like `tramp-sh' (except `tramp-ftp')."
  (require backend)
  (when-let* ((fnha
	       (intern-soft
		(concat (symbol-name backend) "-file-name-handler-alist")))
	      ((boundp fnha)))
    ;; Make BACKEND aware of the new operation.
    (add-to-list fnha (cons operation function))
    (unless (memq operation tramp-file-name-for-operation-external)
      ;; Make Tramp aware of the new operation.
      (add-to-list 'tramp-file-name-for-operation-external operation)
      (put #'tramp-file-name-handler
	   'operations
           (cons operation (get 'tramp-file-name-handler 'operations)))
      ;; Add an advice for OPERATION, in order to invoke the handler FUNCTION.
      (advice-add
       operation :around
       `(lambda (orig-fun &rest args)
	  (if-let* ((handler
		     (find-file-name-handler
		      (if (and (car args) (file-name-absolute-p (car args)))
			  (car args) default-directory)
		      #',operation)))
	      (apply handler #',operation args)
	    (apply orig-fun args)))
       `((name . ,(concat "tramp-advice-" (symbol-name operation))))))))