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 &optional ARG-TYPE)

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). ARG-TYPE is either file (the default), default-directory, process, tramp-file-name, or a function symbol. It describes the type of the OPERATION argument to be checked. See the docstring of tramp-file-name-for-operation-external for its meaning.

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 &optional arg-type)
  "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').  ARG-TYPE is either
`file' (the default), `default-directory', `process', `tramp-file-name',
or a function symbol.  It describes the type of the OPERATION argument
to be checked.  See the docstring of
`tramp-file-name-for-operation-external' for its meaning."
  (require backend)
  (when-let* ((fnha
	       (intern-soft
		(concat (symbol-name backend) "-file-name-handler-alist")))
	      ((boundp fnha))
	      (arg-type (or arg-type 'file)))
    (unless (or (memq arg-type '(file default-directory process tramp-file-name))
		(functionp arg-type))
      (tramp-error nil 'remote-file-error "Unknown arg type: %s" arg-type))
    ;; Make BACKEND aware of the new operation.
    (add-to-list fnha (cons operation function))
    (unless (assq operation tramp-file-name-for-operation-external)
      ;; Make Tramp aware of the new operation.
      (add-to-list
       'tramp-file-name-for-operation-external (cons operation arg-type))
      (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
		      (apply #'tramp-file-name-for-operation #',operation args)
		      #',operation)))
	      (apply handler #',operation args)
	    (apply orig-fun args)))
       `((name . ,(concat "tramp-advice-" (symbol-name operation))))))))