PageRenderTime 69ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/lisp/pcmpl-unix.el

https://bitbucket.org/zielmicha/emacs
Emacs Lisp | 220 lines | 149 code | 37 blank | 34 comment | 11 complexity | 2e865bba549f82d693db952d36d2e392 MD5 | raw file
  1. ;;; pcmpl-unix.el --- standard UNIX completions
  2. ;; Copyright (C) 1999-2012 Free Software Foundation, Inc.
  3. ;; Package: pcomplete
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;; Code:
  17. (require 'pcomplete)
  18. ;; User Variables:
  19. (defcustom pcmpl-unix-group-file "/etc/group"
  20. "If non-nil, a string naming the group file on your system."
  21. :type '(choice file (const nil))
  22. :group 'pcmpl-unix)
  23. (defcustom pcmpl-unix-passwd-file "/etc/passwd"
  24. "If non-nil, a string naming the passwd file on your system."
  25. :type '(choice file (const nil))
  26. :group 'pcmpl-unix)
  27. (defcustom pcmpl-ssh-known-hosts-file "~/.ssh/known_hosts"
  28. "If non-nil, a string naming your SSH \"known_hosts\" file.
  29. This allows one method of completion of SSH host names, the other
  30. being via `pcmpl-ssh-config-file'. Note that newer versions of
  31. ssh hash the hosts by default, to prevent Island-hopping SSH
  32. attacks. This can be disabled, at some risk, with the SSH option
  33. \"HashKnownHosts no\"."
  34. :type '(choice file (const nil))
  35. :group 'pcmpl-unix
  36. :version "23.1")
  37. (defcustom pcmpl-ssh-config-file "~/.ssh/config"
  38. "If non-nil, a string naming your SSH \"config\" file.
  39. This allows one method of completion of SSH host names, the other
  40. being via `pcmpl-ssh-known-hosts-file'."
  41. :type '(choice file (const nil))
  42. :group 'pcmpl-unix
  43. :version "24.1")
  44. ;; Functions:
  45. ;;;###autoload
  46. (defun pcomplete/cd ()
  47. "Completion for `cd'."
  48. (while (pcomplete-here (pcomplete-dirs))))
  49. ;;;###autoload
  50. (defalias 'pcomplete/pushd 'pcomplete/cd)
  51. ;;;###autoload
  52. (defun pcomplete/rmdir ()
  53. "Completion for `rmdir'."
  54. (while (pcomplete-here (pcomplete-dirs))))
  55. ;;;###autoload
  56. (defun pcomplete/rm ()
  57. "Completion for `rm'."
  58. (let ((pcomplete-help "(fileutils)rm invocation"))
  59. (pcomplete-opt "dfirRv")
  60. (while (pcomplete-here (pcomplete-all-entries) nil
  61. 'expand-file-name))))
  62. ;;;###autoload
  63. (defun pcomplete/xargs ()
  64. "Completion for `xargs'."
  65. (pcomplete-here (funcall pcomplete-command-completion-function))
  66. (funcall (or (pcomplete-find-completion-function (pcomplete-arg 1))
  67. pcomplete-default-completion-function)))
  68. ;;;###autoload
  69. (defalias 'pcomplete/time 'pcomplete/xargs)
  70. ;;;###autoload
  71. (defun pcomplete/which ()
  72. "Completion for `which'."
  73. (while (pcomplete-here (funcall pcomplete-command-completion-function))))
  74. (defun pcmpl-unix-read-passwd-file (file)
  75. "Return an alist correlating gids to group names in FILE.
  76. If FILE is in hashed format (as described in the OpenSSH
  77. documentation), this function returns nil."
  78. (let (names)
  79. (when (file-readable-p file)
  80. (with-temp-buffer
  81. (insert-file-contents file)
  82. (goto-char (point-min))
  83. (while (not (eobp))
  84. (let* ((fields
  85. (split-string (buffer-substring
  86. (point) (progn (end-of-line)
  87. (point))) ":")))
  88. (setq names (cons (nth 0 fields) names)))
  89. (forward-line))))
  90. (pcomplete-uniqify-list names)))
  91. (defsubst pcmpl-unix-group-names ()
  92. "Read the contents of /etc/group for group names."
  93. (if pcmpl-unix-group-file
  94. (pcmpl-unix-read-passwd-file pcmpl-unix-group-file)))
  95. (defsubst pcmpl-unix-user-names ()
  96. "Read the contents of /etc/passwd for user names."
  97. (if pcmpl-unix-passwd-file
  98. (pcmpl-unix-read-passwd-file pcmpl-unix-passwd-file)))
  99. ;;;###autoload
  100. (defun pcomplete/chown ()
  101. "Completion for the `chown' command."
  102. (unless (pcomplete-match "\\`-")
  103. (if (pcomplete-match "\\`[^.]*\\'" 0)
  104. (pcomplete-here* (pcmpl-unix-user-names))
  105. (if (pcomplete-match "\\.\\([^.]*\\)\\'" 0)
  106. (pcomplete-here* (pcmpl-unix-group-names)
  107. (pcomplete-match-string 1 0))
  108. (pcomplete-here*))))
  109. (while (pcomplete-here (pcomplete-entries))))
  110. ;;;###autoload
  111. (defun pcomplete/chgrp ()
  112. "Completion for the `chgrp' command."
  113. (unless (pcomplete-match "\\`-")
  114. (pcomplete-here* (pcmpl-unix-group-names)))
  115. (while (pcomplete-here (pcomplete-entries))))
  116. ;; ssh support by Phil Hagelberg.
  117. ;; http://www.emacswiki.org/cgi-bin/wiki/pcmpl-ssh.el
  118. (defun pcmpl-ssh-known-hosts ()
  119. "Return a list of hosts found in `pcmpl-ssh-known-hosts-file'."
  120. (when (and pcmpl-ssh-known-hosts-file
  121. (file-readable-p pcmpl-ssh-known-hosts-file))
  122. (with-temp-buffer
  123. (insert-file-contents-literally pcmpl-ssh-known-hosts-file)
  124. (let ((host-re "\\(?:\\([-.[:alnum:]]+\\)\\|\\[\\([-.[:alnum:]]+\\)\\]:[0-9]+\\)[, ]")
  125. ssh-hosts-list)
  126. (while (re-search-forward (concat "^ *" host-re) nil t)
  127. (add-to-list 'ssh-hosts-list (concat (match-string 1)
  128. (match-string 2)))
  129. (while (and (looking-back ",")
  130. (re-search-forward host-re (line-end-position) t))
  131. (add-to-list 'ssh-hosts-list (concat (match-string 1)
  132. (match-string 2)))))
  133. ssh-hosts-list))))
  134. (defun pcmpl-ssh-config-hosts ()
  135. "Return a list of hosts found in `pcmpl-ssh-config-file'."
  136. (when (and pcmpl-ssh-config-file
  137. (file-readable-p pcmpl-ssh-config-file))
  138. (with-temp-buffer
  139. (insert-file-contents-literally pcmpl-ssh-config-file)
  140. (let (ssh-hosts-list
  141. (case-fold-search t))
  142. (while (re-search-forward "^ *host\\(name\\)? +\\([-.[:alnum:]]+\\)"
  143. nil t)
  144. (add-to-list 'ssh-hosts-list (match-string 2)))
  145. ssh-hosts-list))))
  146. (defun pcmpl-ssh-hosts ()
  147. "Return a list of known SSH hosts.
  148. Uses both `pcmpl-ssh-config-file' and `pcmpl-ssh-known-hosts-file'."
  149. (let ((hosts (pcmpl-ssh-known-hosts)))
  150. (dolist (h (pcmpl-ssh-config-hosts))
  151. (add-to-list 'hosts h))
  152. hosts))
  153. ;;;###autoload
  154. (defun pcomplete/ssh ()
  155. "Completion rules for the `ssh' command."
  156. (pcomplete-opt "1246AaCfgKkMNnqsTtVvXxYbcDeFiLlmOopRSw")
  157. (pcomplete-here (pcmpl-ssh-hosts)))
  158. ;;;###autoload
  159. (defun pcomplete/scp ()
  160. "Completion rules for the `scp' command.
  161. Includes files as well as host names followed by a colon."
  162. (pcomplete-opt "1246BCpqrvcFiloPS")
  163. (while t (pcomplete-here
  164. (lambda (string pred action)
  165. (let ((table
  166. (cond
  167. ((string-match "\\`[^:/]+:" string) ; Remote file name.
  168. (if (and (eq action 'lambda)
  169. (eq (match-end 0) (length string)))
  170. ;; Avoid connecting to the remote host when we're
  171. ;; only completing the host name.
  172. (list string)
  173. (completion-table-subvert (pcomplete-all-entries)
  174. "" "/ssh:")))
  175. ((string-match "/" string) ; Local file name.
  176. (pcomplete-all-entries))
  177. (t ;Host name or local file name.
  178. (append (all-completions string (pcomplete-all-entries))
  179. (mapcar (lambda (host) (concat host ":"))
  180. (pcmpl-ssh-hosts)))))))
  181. (complete-with-action action table string pred))))))
  182. (provide 'pcmpl-unix)
  183. ;;; pcmpl-unix.el ends here