+
+;; ssh support by Phil Hagelberg.
+;; http://www.emacswiki.org/cgi-bin/wiki/pcmpl-ssh.el
+
+(defun pcmpl-ssh-known-hosts ()
+ "Return a list of hosts found in `pcmpl-ssh-known-hosts-file'."
+ (when (and pcmpl-ssh-known-hosts-file
+ (file-readable-p pcmpl-ssh-known-hosts-file))
+ (with-temp-buffer
+ (insert-file-contents-literally pcmpl-ssh-known-hosts-file)
+ (let ((host-re "\\(?:\\([-.[:alnum:]]+\\)\\|\\[\\([-.[:alnum:]]+\\)\\]:[0-9]+\\)[, ]")
+ ssh-hosts-list)
+ (while (re-search-forward (concat "^ *" host-re) nil t)
+ (add-to-list 'ssh-hosts-list (concat (match-string 1)
+ (match-string 2)))
+ (while (and (eq (char-before) ?,)
+ (re-search-forward host-re (line-end-position) t))
+ (add-to-list 'ssh-hosts-list (concat (match-string 1)
+ (match-string 2)))))
+ ssh-hosts-list))))
+
+(defun pcmpl-ssh-config-hosts ()
+ "Return a list of hosts found in `pcmpl-ssh-config-file'."
+ (when (and pcmpl-ssh-config-file
+ (file-readable-p pcmpl-ssh-config-file))
+ (with-temp-buffer
+ (insert-file-contents-literally pcmpl-ssh-config-file)
+ (let (ssh-hosts-list
+ (case-fold-search t))
+ (while (re-search-forward "^ *host\\(name\\)? +\\([-.[:alnum:]]+\\)"
+ nil t)
+ (add-to-list 'ssh-hosts-list (match-string 2)))
+ ssh-hosts-list))))
+
+(defun pcmpl-ssh-hosts ()
+ "Return a list of known SSH hosts.
+Uses both `pcmpl-ssh-config-file' and `pcmpl-ssh-known-hosts-file'."
+ (let ((hosts (pcmpl-ssh-known-hosts)))
+ (dolist (h (pcmpl-ssh-config-hosts))
+ (add-to-list 'hosts h))
+ hosts))
+
+;;;###autoload
+(defun pcomplete/ssh ()
+ "Completion rules for the `ssh' command."
+ (pcomplete-opt "1246AaCfgKkMNnqsTtVvXxYbcDeFiLlmOopRSw")
+ (pcomplete-here (pcmpl-ssh-hosts)))
+
+;;;###autoload
+(defun pcomplete/scp ()
+ "Completion rules for the `scp' command.
+Includes files as well as host names followed by a colon."
+ (pcomplete-opt "1246BCpqrvcFiloPS")
+ (while t (pcomplete-here
+ (lambda (string pred action)
+ (let ((table
+ (cond
+ ((string-match "\\`[^:/]+:" string) ; Remote file name.
+ (if (and (eq action 'lambda)
+ (eq (match-end 0) (length string)))
+ ;; Avoid connecting to the remote host when we're
+ ;; only completing the host name.
+ (list string)
+ (completion-table-subvert (pcomplete-all-entries)
+ "" "/ssh:")))
+ ((string-match "/" string) ; Local file name.
+ (pcomplete-all-entries))
+ (t ;Host name or local file name.
+ (append (all-completions string (pcomplete-all-entries))
+ (mapcar (lambda (host) (concat host ":"))
+ (pcmpl-ssh-hosts)))))))
+ (complete-with-action action table string pred))))))
+
+(provide 'pcmpl-unix)
+