Skip to content

Commit

Permalink
Improve completion in tramp-gvfs.el
Browse files Browse the repository at this point in the history
* lisp/net/tramp-gvfs.el (tramp-zeroconf-parse-device-names):
Renamed from `tramp-zeroconf-parse-service-device-names'.
(tramp-zeroconf-parse-webdav-device-names): Removed.  Code merged
with `tramp-zeroconf-parse-device-names'.
(tramp-gvfs-parse-device-names): New defun.
(top): Use it when `tramp-zeroconf-parse-device-names' is not
applicable.

* lisp/net/tramp.el (tramp-set-completion-function): The argument
could also be a zeroconf service type.
  • Loading branch information
albinus committed Nov 1, 2015
1 parent 1500667 commit df660da
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 29 deletions.
77 changes: 56 additions & 21 deletions lisp/net/tramp-gvfs.el
Original file line number Diff line number Diff line change
Expand Up @@ -1722,14 +1722,7 @@ be used."

;; D-Bus zeroconf functions.

(defun tramp-zeroconf-parse-service-device-names (service)
"Return a list of (user host) tuples allowed to access."
(mapcar
(lambda (x)
(list nil (zeroconf-service-host x)))
(zeroconf-list-services service)))

(defun tramp-zeroconf-parse-webdav-device-names (_ignore)
(defun tramp-zeroconf-parse-device-names (service)
"Return a list of (user host) tuples allowed to access."
(mapcar
(lambda (x)
Expand All @@ -1745,22 +1738,64 @@ be used."
(setq user (match-string 1 (car text))))
(setq text (cdr text)))
(list user host)))
(zeroconf-list-services "_webdav._tcp")))
(zeroconf-list-services service)))

(defun tramp-gvfs-parse-device-names (service)
"Return a list of (user host) tuples allowed to access.
This uses \"avahi-browse\" in case D-Bus is not enabled in Avahi."
(let ((result
(split-string
(shell-command-to-string (format "avahi-browse -trkp %s" service))
"[\n\r]+" 'omit "^\\+;.*$")))
(tramp-compat-delete-dups
(mapcar
(lambda (x)
(let* ((list (split-string x ";"))
(host (nth 6 list))
(port (nth 8 list))
(text (split-string (nth 9 list) "\" \"" 'omit "\""))
user)
; (when (and port (not (string-equal port "0")))
; (setq host (format "%s%s%s" host tramp-prefix-port-regexp port)))
;; A user is marked in a TXT field like "u=guest".
(while text
(when (string-match "u=\\(.+\\)$" (car text))
(setq user (match-string 1 (car text))))
(setq text (cdr text)))
(list user host)))
result))))

;; Add completion functions for AFP, DAV, DAVS, SFTP and SMB methods.
(when (and tramp-gvfs-enabled
(member zeroconf-service-avahi (dbus-list-known-names :system)))
(when tramp-gvfs-enabled
(zeroconf-init tramp-gvfs-zeroconf-domain)
(tramp-set-completion-function
"afp" '((tramp-zeroconf-parse-service-device-names "_afpovertcp._tcp")))
(tramp-set-completion-function
"dav" '((tramp-zeroconf-parse-webdav-device-names "")))
(tramp-set-completion-function
"davs" '((tramp-zeroconf-parse-webdav-device-names "")))
(tramp-set-completion-function
"sftp" '((tramp-zeroconf-parse-service-device-names "_workstation._tcp")))
(tramp-set-completion-function
"smb" '((tramp-zeroconf-parse-service-device-names "_smb._tcp"))))
(if (zeroconf-list-service-types)
(progn
(tramp-set-completion-function
"afp" '((tramp-zeroconf-parse-device-names "_afpovertcp._tcp")))
(tramp-set-completion-function
"dav" '((tramp-zeroconf-parse-device-names "_webdav._tcp")))
(tramp-set-completion-function
"davs" '((tramp-zeroconf-parse-device-names "_webdav._tcp")))
(tramp-set-completion-function
"sftp" '((tramp-zeroconf-parse-device-names "_ssh._tcp")
(tramp-zeroconf-parse-device-names "_workstation._tcp")))
(when (member "smb" tramp-gvfs-methods)
(tramp-set-completion-function
"smb" '((tramp-zeroconf-parse-device-names "_smb._tcp")))))

(when (executable-find "avahi-browse")
(tramp-set-completion-function
"afp" '((tramp-gvfs-parse-device-names "_afpovertcp._tcp")))
(tramp-set-completion-function
"dav" '((tramp-gvfs-parse-device-names "_webdav._tcp")))
(tramp-set-completion-function
"davs" '((tramp-gvfs-parse-device-names "_webdav._tcp")))
(tramp-set-completion-function
"sftp" '((tramp-gvfs-parse-device-names "_ssh._tcp")
(tramp-gvfs-parse-device-names "_workstation._tcp")))
(when (member "smb" tramp-gvfs-methods)
(tramp-set-completion-function
"smb" '((tramp-gvfs-parse-device-names "_smb._tcp")))))))


;; D-Bus SYNCE functions.
Expand Down
20 changes: 12 additions & 8 deletions lisp/net/tramp.el
Original file line number Diff line number Diff line change
Expand Up @@ -1765,14 +1765,18 @@ Example:
(setcdr v (delete (car v) (cdr v))))
;; Check for function and file or registry key.
(unless (and (functionp (nth 0 (car v)))
(if (string-match "^HKEY_CURRENT_USER" (nth 1 (car v)))
;; Windows registry.
(and (memq system-type '(cygwin windows-nt))
(zerop
(tramp-call-process
v "reg" nil nil nil "query" (nth 1 (car v)))))
;; Configuration file.
(file-exists-p (nth 1 (car v)))))
(cond
;; Windows registry.
((string-match "^HKEY_CURRENT_USER" (nth 1 (car v)))
(and (memq system-type '(cygwin windows-nt))
(zerop
(tramp-call-process
v "reg" nil nil nil "query" (nth 1 (car v))))))
;; Zeroconf service type.
((string-match
"^_[[:alpha:]]+\\._[[:alpha:]]+$" (nth 1 (car v))))
;; Configuration file.
(t (file-exists-p (nth 1 (car v))))))
(setq r (delete (car v) r)))
(setq v (cdr v)))

Expand Down

0 comments on commit df660da

Please sign in to comment.