pcmpl-rpm.el 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. ;;; pcmpl-rpm.el --- functions for dealing with rpm 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. ;; These functions provide completion rules for the `rpm' command.
  17. ;;; Code:
  18. (require 'pcomplete)
  19. ;; Functions:
  20. ;; FIXME rpm -qa can be slow, so:
  21. ;; Adding --nodigest --nosignature is MUCH faster.
  22. ;; (Probably need to test --help for those options though.)
  23. ;; Consider caching the result (cf woman).
  24. ;; Consider printing an explanatory message before running -qa.
  25. ;;
  26. ;; Seems pointless for this to be a defsubst.
  27. (defsubst pcmpl-rpm-packages ()
  28. (split-string (pcomplete-process-result "rpm" "-q" "-a")))
  29. (defun pcmpl-rpm-all-query (flag)
  30. (message "Querying all packages with `%s'..." flag)
  31. (let ((pkgs (pcmpl-rpm-packages))
  32. (provs (list t)))
  33. (while pkgs
  34. (nconc provs (split-string
  35. (pcomplete-process-result
  36. "rpm" "-q" (car pkgs) flag)))
  37. (setq pkgs (cdr pkgs)))
  38. (pcomplete-uniqify-list (cdr provs))))
  39. (defsubst pcmpl-rpm-files ()
  40. (pcomplete-dirs-or-entries "\\.rpm\\'"))
  41. ;;;###autoload
  42. (defun pcomplete/rpm ()
  43. "Completion for the `rpm' command."
  44. ;; Originally taken from the output of `rpm --help' on a Red Hat 6.1 system.
  45. (let (mode)
  46. (while (<= pcomplete-index pcomplete-last)
  47. (unless mode
  48. (if (pcomplete-match "^--\\(.*\\)" 0)
  49. (pcomplete-here*
  50. '("--addsign"
  51. "--checksig"
  52. "--erase"
  53. "--help"
  54. "--initdb"
  55. "--install"
  56. "--pipe"
  57. "--querytags"
  58. "--rebuild"
  59. "--rebuilddb"
  60. "--recompile"
  61. "--resign"
  62. "--rmsource"
  63. "--setperms"
  64. "--setugids"
  65. "--upgrade"
  66. "--verify"
  67. "--version"))
  68. (pcomplete-opt "vqVyiUebtK")))
  69. ; -b<stage> <spec>
  70. ; -t<stage> <tarball> - build package, where <stage> is one of:
  71. ; p - prep (unpack sources and apply patches)
  72. ; l - list check (do some cursory checks on %files)
  73. ; c - compile (prep and compile)
  74. ; i - install (prep, compile, install)
  75. ; b - binary package (prep, compile, install, package)
  76. ; a - bin/src package (prep, compile, install, package)
  77. (cond
  78. ((or (eq mode 'query)
  79. (pcomplete-match "-[^-]*q"))
  80. (setq mode 'query)
  81. (if (pcomplete-match "^--\\(.*\\)" 0)
  82. (progn
  83. (pcomplete-here*
  84. '("--changelog"
  85. "--dbpath"
  86. "--dump"
  87. "--file"
  88. "--ftpport" ;nyi for the next four
  89. "--ftpproxy"
  90. "--httpport"
  91. "--httpproxy"
  92. "--provides"
  93. "--queryformat"
  94. "--rcfile"
  95. "--requires"
  96. "--root"
  97. "--scripts"
  98. "--triggeredby"
  99. "--whatprovides"
  100. "--whatrequires"))
  101. (cond
  102. ((pcomplete-test "--dbpath")
  103. (pcomplete-here* (pcomplete-dirs)))
  104. ((pcomplete-test "--queryformat")
  105. (pcomplete-here*))
  106. ((pcomplete-test "--rcfile")
  107. (pcomplete-here* (pcomplete-entries)))
  108. ((pcomplete-test "--file")
  109. (pcomplete-here* (pcomplete-entries)))
  110. ((pcomplete-test "--root")
  111. (pcomplete-here* (pcomplete-dirs)))
  112. ((pcomplete-test "--scripts")
  113. (if (pcomplete-match "^--\\(.*\\)" 0)
  114. (pcomplete-here* '("--triggers"))))
  115. ((pcomplete-test "--triggeredby")
  116. (pcomplete-here* (pcmpl-rpm-packages)))
  117. ((pcomplete-test "--whatprovides")
  118. (pcomplete-here*
  119. (pcmpl-rpm-all-query "--provides")))
  120. ((pcomplete-test "--whatrequires")
  121. (pcomplete-here*
  122. (pcmpl-rpm-all-query "--requires")))))
  123. (if (pcomplete-match "^-" 0)
  124. (pcomplete-opt "af.p(pcmpl-rpm-files)ilsdcvR")
  125. (if (pcomplete-test "-[^-]*p" 'first 1)
  126. (pcomplete-here (pcmpl-rpm-files))
  127. (if (pcomplete-test "-[^-]*f" 'first 1)
  128. (pcomplete-here* (pcomplete-entries))
  129. (pcomplete-here (pcmpl-rpm-packages)))))))
  130. ((pcomplete-test "--pipe")
  131. (pcomplete-here* (funcall pcomplete-command-completion-function)))
  132. ((pcomplete-test "--rmsource")
  133. (pcomplete-here* (pcomplete-entries))
  134. (throw 'pcomplete-completions nil))
  135. ((pcomplete-match "\\`--re\\(build\\|compile\\)\\'")
  136. (pcomplete-here (pcmpl-rpm-files))
  137. (throw 'pcomplete-completions nil))
  138. ((pcomplete-match "\\`--\\(resign\\|addsign\\)\\'")
  139. (while (pcomplete-here (pcmpl-rpm-files))))
  140. ((or (eq mode 'checksig)
  141. (pcomplete-test "--checksig"))
  142. (setq mode 'checksig)
  143. (if (pcomplete-match "^--\\(.*\\)" 0)
  144. (progn
  145. (pcomplete-here*
  146. '("--nopgp"
  147. "--nogpg"
  148. "--nomd5"
  149. "--rcfile"))
  150. (cond
  151. ((pcomplete-test "--rcfile")
  152. (pcomplete-here* (pcomplete-entries)))))
  153. (if (pcomplete-match "^-" 0)
  154. (pcomplete-opt "v")
  155. (pcomplete-here (pcmpl-rpm-files)))))
  156. ((or (eq mode 'rebuilddb)
  157. (pcomplete-test "--rebuilddb"))
  158. (setq mode 'rebuilddb)
  159. (if (pcomplete-match "^--\\(.*\\)" 0)
  160. (progn
  161. (pcomplete-here*
  162. '("--dbpath"
  163. "--root"
  164. "--rcfile"))
  165. (cond
  166. ((pcomplete-test "--dbpath")
  167. (pcomplete-here* (pcomplete-dirs)))
  168. ((pcomplete-test "--root")
  169. (pcomplete-here* (pcomplete-dirs)))
  170. ((pcomplete-test "--rcfile")
  171. (pcomplete-here* (pcomplete-entries)))))
  172. (if (pcomplete-match "^-" 0)
  173. (pcomplete-opt "v")
  174. (pcomplete-here))))
  175. ((memq mode '(install upgrade))
  176. (if (pcomplete-match "^--\\(.*\\)" 0)
  177. (progn
  178. (pcomplete-here*
  179. (append
  180. '("--allfiles"
  181. "--badreloc"
  182. "--dbpath"
  183. "--excludedocs"
  184. "--excludepath"
  185. "--force"
  186. "--hash"
  187. "--ignorearch"
  188. "--ignoreos"
  189. "--ignoresize"
  190. "--includedocs"
  191. "--justdb"
  192. "--nodeps"
  193. "--noorder"
  194. "--noscripts"
  195. "--notriggers")
  196. (if (eq mode 'upgrade)
  197. '("--oldpackage"))
  198. '("--percent"
  199. "--prefix"
  200. "--rcfile"
  201. "--relocate"
  202. "--replacefiles"
  203. "--replacepkgs"
  204. "--root")))
  205. (cond
  206. ((pcomplete-test "--dbpath")
  207. (pcomplete-here* (pcomplete-dirs)))
  208. ((pcomplete-test "--relocate")
  209. (pcomplete-here*))
  210. ((pcomplete-test "--rcfile")
  211. (pcomplete-here* (pcomplete-entries)))
  212. ((pcomplete-test "--excludepath")
  213. (pcomplete-here* (pcomplete-entries)))
  214. ((pcomplete-test "--root")
  215. (pcomplete-here* (pcomplete-dirs)))
  216. ((pcomplete-test "--prefix")
  217. (pcomplete-here* (pcomplete-dirs)))))
  218. (if (pcomplete-match "^-" 0)
  219. (pcomplete-opt "vh")
  220. (pcomplete-here (pcmpl-rpm-files)))))
  221. ((or (pcomplete-test "--install")
  222. (pcomplete-match "-[^-]*i"))
  223. (setq mode 'install))
  224. ((or (pcomplete-test "--upgrade")
  225. (pcomplete-match "-[^-]*U"))
  226. (setq mode 'upgrade))
  227. ((or (eq mode 'erase)
  228. (pcomplete-test "--erase")
  229. (pcomplete-match "-[^-]*e"))
  230. (setq mode 'erase)
  231. (if (pcomplete-match "^--\\(.*\\)" 0)
  232. (progn
  233. (pcomplete-here*
  234. '("--allmatches"
  235. "--dbpath"
  236. "--justdb"
  237. "--nodeps"
  238. "--noorder"
  239. "--noscripts"
  240. "--notriggers"
  241. "--rcfile"
  242. "--root"))
  243. (cond
  244. ((pcomplete-test "--dbpath")
  245. (pcomplete-here* (pcomplete-dirs)))
  246. ((pcomplete-test "--rcfile")
  247. (pcomplete-here* (pcomplete-entries)))
  248. ((pcomplete-test "--root")
  249. (pcomplete-here* (pcomplete-dirs)))))
  250. (if (pcomplete-match "^-" 0)
  251. (pcomplete-opt "v")
  252. (pcomplete-here (pcmpl-rpm-packages)))))
  253. ((or (eq mode 'verify)
  254. (pcomplete-test "--verify"))
  255. (setq mode 'verify)
  256. (if (pcomplete-match "^--\\(.*\\)" 0)
  257. (progn
  258. (pcomplete-here*
  259. '("--dbpath"
  260. "--nodeps"
  261. "--nofiles"
  262. "--nomd5"
  263. "--rcfile"
  264. "--root"
  265. "--triggeredby"
  266. "--whatprovides"
  267. "--whatrequires"))
  268. (cond
  269. ((pcomplete-test "--dbpath")
  270. (pcomplete-here* (pcomplete-dirs)))
  271. ((pcomplete-test "--rcfile")
  272. (pcomplete-here* (pcomplete-entries)))
  273. ((pcomplete-test "--root")
  274. (pcomplete-here* (pcomplete-dirs)))
  275. ((pcomplete-test "--triggeredby")
  276. (pcomplete-here* (pcmpl-rpm-packages)))
  277. ((pcomplete-test "--whatprovides")
  278. (pcomplete-here*
  279. (pcmpl-rpm-all-query "--provides")))
  280. ((pcomplete-test "--whatrequires")
  281. (pcomplete-here*
  282. (pcmpl-rpm-all-query "--requires")))))
  283. (if (pcomplete-match "^-" 0)
  284. (pcomplete-opt "af.p(pcmpl-rpm-files)v")
  285. (pcomplete-here (pcmpl-rpm-packages)))))
  286. ((or (memq mode '(build test))
  287. (pcomplete-match "\\`-[bt]"))
  288. (setq mode (if (pcomplete-match "\\`-b")
  289. 'build
  290. 'test))
  291. (if (pcomplete-match "^--\\(.*\\)" 0)
  292. (progn
  293. (pcomplete-here*
  294. '("--buildroot"
  295. "--clean"
  296. "--nobuild"
  297. "--rcfile"
  298. "--rmsource"
  299. "--short-circuit"
  300. "--sign"
  301. "--target"
  302. "--timecheck"))
  303. (cond
  304. ((pcomplete-test "--buildroot")
  305. (pcomplete-here* (pcomplete-dirs)))
  306. ((pcomplete-test "--rcfile")
  307. (pcomplete-here* (pcomplete-entries)))
  308. ((pcomplete-test "--timecheck")
  309. (pcomplete-here*))))
  310. (if (pcomplete-match "^-" 0)
  311. (pcomplete-opt "v")
  312. (pcomplete-here
  313. (pcomplete-dirs-or-entries (if (eq mode 'test)
  314. "\\.tar\\'"
  315. "\\.spec\\'"))))))
  316. (t
  317. (error "You must select a mode: -q, -i, -U, --verify, etc"))))))
  318. (provide 'pcmpl-rpm)
  319. ;;; pcmpl-rpm.el ends here