pcmpl-gnu.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. ;;; pcmpl-gnu.el --- completions for GNU project tools -*- lexical-binding: t -*-
  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. (provide 'pcmpl-gnu)
  18. (require 'pcomplete)
  19. (require 'pcmpl-unix)
  20. (defgroup pcmpl-gnu nil
  21. "Completions for GNU project tools."
  22. :group 'pcomplete)
  23. ;; User Variables:
  24. (defcustom pcmpl-gnu-makefile-regexps
  25. '("\\`GNUmakefile" "\\`[Mm]akefile" "\\.ma?k\\'")
  26. "A list of regexps that will match Makefile names."
  27. :type '(repeat regexp)
  28. :group 'pcmpl-gnu)
  29. ;; Functions:
  30. ;;;###autoload
  31. (defun pcomplete/gzip ()
  32. "Completion for `gzip'."
  33. (let ((pcomplete-help "(gzip)"))
  34. (pcomplete-opt "cdfhlLnNqrStvV123456789")
  35. (while (pcomplete-here
  36. (pcmpl-gnu-zipped-files
  37. (catch 'has-d-flag
  38. (let ((args pcomplete-args))
  39. (while args
  40. (if (string-match "\\`-.*[dt]" (car args))
  41. (throw 'has-d-flag t))
  42. (setq args (cdr args))))))))))
  43. (defun pcmpl-gnu-zipped-files (unzip-p)
  44. "Find all zipped or unzipped files: the inverse of UNZIP-P."
  45. (pcomplete-entries
  46. nil
  47. (function
  48. (lambda (entry)
  49. (when (and (file-readable-p entry)
  50. (file-regular-p entry))
  51. (let ((zipped (string-match "\\.\\(t?gz\\|\\(ta\\)?Z\\)\\'"
  52. entry)))
  53. (or (and unzip-p zipped)
  54. (and (not unzip-p) (not zipped)))))))))
  55. ;;;###autoload
  56. (defun pcomplete/bzip2 ()
  57. "Completion for `bzip2'."
  58. (pcomplete-opt "hdzkftcqvLVs123456789")
  59. (while (pcomplete-here
  60. (pcmpl-gnu-bzipped-files
  61. (catch 'has-d-flag
  62. (let ((args pcomplete-args))
  63. (while args
  64. (if (string-match "\\`-.*[dt]" (car args))
  65. (throw 'has-d-flag t))
  66. (setq args (cdr args)))))))))
  67. (defun pcmpl-gnu-bzipped-files (unzip-p)
  68. "Find all zipped or unzipped files: the inverse of UNZIP-P."
  69. (pcomplete-entries
  70. nil
  71. (function
  72. (lambda (entry)
  73. (when (and (file-readable-p entry)
  74. (file-regular-p entry))
  75. (let ((zipped (string-match "\\.\\(t?z2\\|bz2\\)\\'" entry)))
  76. (or (and unzip-p zipped)
  77. (and (not unzip-p) (not zipped)))))))))
  78. ;;;###autoload
  79. (defun pcomplete/make ()
  80. "Completion for GNU `make'."
  81. (let ((pcomplete-help "(make)Top"))
  82. (pcomplete-opt "bmC/def(pcmpl-gnu-makefile-names)hiI/j?kl?no.pqrsStvwW.")
  83. (while (pcomplete-here (completion-table-in-turn
  84. (pcmpl-gnu-make-rule-names)
  85. (pcomplete-entries))
  86. nil 'identity))))
  87. (defun pcmpl-gnu-makefile-names ()
  88. "Return a list of possible makefile names."
  89. (pcomplete-entries (mapconcat 'identity pcmpl-gnu-makefile-regexps "\\|")))
  90. (defun pcmpl-gnu-make-rule-names ()
  91. "Return a list of possible make rule names in MAKEFILE."
  92. (let* ((minus-f (member "-f" pcomplete-args))
  93. (makefile (or (cadr minus-f)
  94. (cond
  95. ((file-exists-p "GNUmakefile") "GNUmakefile")
  96. ((file-exists-p "makefile") "makefile")
  97. (t "Makefile"))))
  98. rules)
  99. (if (not (file-readable-p makefile))
  100. (unless minus-f (list "-f"))
  101. (with-temp-buffer
  102. (ignore-errors ;Could be a directory or something.
  103. (insert-file-contents makefile))
  104. (while (re-search-forward
  105. (concat "^\\s-*\\([^\n#%.$][^:=\n]*\\)\\s-*:[^=]") nil t)
  106. (setq rules (append (split-string (match-string 1)) rules))))
  107. (pcomplete-uniqify-list rules))))
  108. (defcustom pcmpl-gnu-tarfile-regexp
  109. "\\.t\\(ar\\(\\.\\(gz\\|bz2\\|Z\\)\\)?\\|gz\\|a[zZ]\\|z2\\)\\'"
  110. "A regexp which matches any tar archive."
  111. :type 'regexp
  112. :group 'pcmpl-gnu)
  113. ;; Only used in tar-mode buffers.
  114. (defvar tar-parse-info)
  115. (declare-function tar-header-name "tar-mode" t t)
  116. (defmacro pcmpl-gnu-with-file-buffer (file &rest body)
  117. "Run BODY inside a buffer visiting FILE."
  118. (declare (debug t) (indent 1))
  119. (let ((exist (make-symbol "exist"))
  120. (filesym (make-symbol "file"))
  121. (buf (make-symbol "buf")))
  122. `(let* ((,filesym ,file)
  123. (,exist (find-buffer-visiting ,filesym))
  124. (,buf (or ,exist (find-file-noselect ,filesym))))
  125. (unwind-protect
  126. (with-current-buffer ,buf
  127. ,@body)
  128. (when (and (not ,exist) (buffer-live-p ,buf))
  129. (kill-buffer ,buf))))))
  130. ;;;###autoload
  131. (defun pcomplete/tar ()
  132. "Completion for the GNU tar utility."
  133. ;; options that end in an equal sign will want further completion...
  134. (let (saw-option complete-within)
  135. (let ((pcomplete-suffix-list (cons ?= pcomplete-suffix-list)))
  136. (while (pcomplete-match "^-" 0)
  137. (setq saw-option t)
  138. (if (pcomplete-match "^--" 0)
  139. (if (pcomplete-match "^--\\([^= \t\n\f]*\\)\\'" 0)
  140. ;; FIXME: Extract this list from "tar --help".
  141. (pcomplete-here*
  142. '("--absolute-names"
  143. "--after-date="
  144. "--append"
  145. "--atime-preserve"
  146. "--backup"
  147. "--block-number"
  148. "--blocking-factor="
  149. "--catenate"
  150. "--checkpoint"
  151. "--compare"
  152. "--compress"
  153. "--concatenate"
  154. "--confirmation"
  155. "--create"
  156. "--delete"
  157. "--dereference"
  158. "--diff"
  159. "--directory="
  160. "--exclude="
  161. "--exclude-from="
  162. "--extract"
  163. "--file="
  164. "--files-from="
  165. "--force-local"
  166. "--get"
  167. "--group="
  168. "--gzip"
  169. "--help"
  170. "--ignore-failed-read"
  171. "--ignore-zeros"
  172. "--incremental"
  173. "--info-script="
  174. "--interactive"
  175. "--keep-old-files"
  176. "--label="
  177. "--list"
  178. "--listed-incremental"
  179. "--mode="
  180. "--modification-time"
  181. "--multi-volume"
  182. "--new-volume-script="
  183. "--newer="
  184. "--newer-mtime"
  185. "--no-recursion"
  186. "--null"
  187. "--numeric-owner"
  188. "--old-archive"
  189. "--one-file-system"
  190. "--owner="
  191. "--portability"
  192. "--posix"
  193. "--preserve"
  194. "--preserve-order"
  195. "--preserve-permissions"
  196. "--read-full-records"
  197. "--record-size="
  198. "--recursive-unlink"
  199. "--remove-files"
  200. "--rsh-command="
  201. "--same-order"
  202. "--same-owner"
  203. "--same-permissions"
  204. "--sparse"
  205. "--starting-file="
  206. "--suffix="
  207. "--tape-length="
  208. "--to-stdout"
  209. "--totals"
  210. "--uncompress"
  211. "--ungzip"
  212. "--unlink-first"
  213. "--update"
  214. "--use-compress-program="
  215. "--verbose"
  216. "--verify"
  217. "--version"
  218. "--volno-file=")))
  219. (pcomplete-opt "01234567ABCFGKLMNOPRSTUVWXZbcdfghiklmoprstuvwxz"))
  220. (cond
  221. ((pcomplete-match "\\`-\\'" 0)
  222. (pcomplete-here*))
  223. ((pcomplete-match "\\`--after-date=" 0)
  224. (pcomplete-here*))
  225. ((pcomplete-match "\\`--backup=" 0)
  226. (pcomplete-here*))
  227. ((pcomplete-match "\\`--blocking-factor=" 0)
  228. (pcomplete-here*))
  229. ((pcomplete-match "\\`--directory=\\(.*\\)" 0)
  230. (pcomplete-here* (pcomplete-dirs)
  231. (pcomplete-match-string 1 0)))
  232. ((pcomplete-match "\\`--exclude-from=\\(.*\\)" 0)
  233. (pcomplete-here* (pcomplete-entries)
  234. (pcomplete-match-string 1 0)))
  235. ((pcomplete-match "\\`--exclude=" 0)
  236. (pcomplete-here*))
  237. ((pcomplete-match "\\`--\\(extract\\|list\\)\\'" 0)
  238. (setq complete-within t))
  239. ((pcomplete-match "\\`--file=\\(.*\\)" 0)
  240. (pcomplete-here* (pcomplete-dirs-or-entries pcmpl-gnu-tarfile-regexp)
  241. (pcomplete-match-string 1 0)))
  242. ((pcomplete-match "\\`--files-from=\\(.*\\)" 0)
  243. (pcomplete-here* (pcomplete-entries)
  244. (pcomplete-match-string 1 0)))
  245. ((pcomplete-match "\\`--group=\\(.*\\)" 0)
  246. (pcomplete-here* (pcmpl-unix-group-names)
  247. (pcomplete-match-string 1 0)))
  248. ((pcomplete-match "\\`--info-script=\\(.*\\)" 0)
  249. (pcomplete-here* (pcomplete-entries)
  250. (pcomplete-match-string 1 0)))
  251. ((pcomplete-match "\\`--label=" 0)
  252. (pcomplete-here*))
  253. ((pcomplete-match "\\`--mode=" 0)
  254. (pcomplete-here*))
  255. ((pcomplete-match "\\`--new-volume-script=\\(.*\\)" 0)
  256. (pcomplete-here* (pcomplete-entries)
  257. (pcomplete-match-string 1 0)))
  258. ((pcomplete-match "\\`--newer=" 0)
  259. (pcomplete-here*))
  260. ((pcomplete-match "\\`--owner=\\(.*\\)" 0)
  261. (pcomplete-here* (pcmpl-unix-user-names)
  262. (pcomplete-match-string 1 0)))
  263. ((pcomplete-match "\\`--record-size=" 0)
  264. (pcomplete-here*))
  265. ((pcomplete-match "\\`--rsh-command=\\(.*\\)" 0)
  266. (pcomplete-here* (funcall pcomplete-command-completion-function)
  267. (pcomplete-match-string 1 0)))
  268. ((pcomplete-match "\\`--starting-file=\\(.*\\)" 0)
  269. (pcomplete-here* (pcomplete-entries)
  270. (pcomplete-match-string 1 0)))
  271. ((pcomplete-match "\\`--suffix=" 0)
  272. (pcomplete-here*))
  273. ((pcomplete-match "\\`--tape-length=" 0)
  274. (pcomplete-here*))
  275. ((pcomplete-match "\\`--use-compress-program=\\(.*\\)" 0)
  276. (pcomplete-here* (funcall pcomplete-command-completion-function)
  277. (pcomplete-match-string 1 0)))
  278. ((pcomplete-match "\\`--volno-file=\\(.*\\)" 0)
  279. (pcomplete-here* (pcomplete-entries)
  280. (pcomplete-match-string 1 0))))))
  281. (unless saw-option
  282. (pcomplete-here
  283. (mapcar 'char-to-string
  284. (string-to-list
  285. "01234567ABCFGIKLMNOPRSTUVWXZbcdfghiklmoprstuvwxz")))
  286. (if (pcomplete-match "[xt]" 'first 1)
  287. (setq complete-within t)))
  288. (pcomplete-here (pcomplete-dirs-or-entries pcmpl-gnu-tarfile-regexp))
  289. (while (pcomplete-here
  290. (if (and complete-within
  291. (let* ((fa (file-attributes (pcomplete-arg 1)))
  292. (size (nth 7 fa)))
  293. (and (numberp size)
  294. (or (null large-file-warning-threshold)
  295. (< size large-file-warning-threshold)))))
  296. (let ((file (pcomplete-arg 1)))
  297. (completion-table-dynamic
  298. (lambda (_string)
  299. (pcmpl-gnu-with-file-buffer file
  300. (mapcar #'tar-header-name tar-parse-info)))))
  301. (pcomplete-entries))
  302. nil 'identity))))
  303. ;;;###autoload
  304. (defalias 'pcomplete/gdb 'pcomplete/xargs)
  305. ;;; pcmpl-gnu.el ends here