1 ;;; pcmpl-rpm.el --- functions for dealing with rpm completions
3 ;; Copyright (C) 1999-2012 Free Software Foundation, Inc.
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;; These functions provide completion rules for the `rpm' command.
30 (defgroup pcmpl-rpm nil
31 "Options for rpm completion."
35 ;; rpm -qa can be slow. Adding --nodigest --nosignature is MUCH faster.
36 (defcustom pcmpl-rpm-query-options
39 (when (ignore-errors (call-process "rpm" nil t nil
"--help"))
40 (if (search-backward "--nodigest " nil
'move
)
41 (setq opts
'("--nodigest")))
42 (goto-char (point-min))
43 (if (search-forward "--nosignature " nil t
)
44 (push "--nosignature" opts
))))
46 "List of extra options to add to an rpm query command."
48 :type
'(repeat string
)
51 (defcustom pcmpl-rpm-cache t
52 "Whether to cache the list of installed packages."
57 (defconst pcmpl-rpm-cache-stamp-file
"/var/lib/rpm/Packages"
58 "File used to check that the list of installed packages is up-to-date.")
60 (defvar pcmpl-rpm-cache-time nil
61 "Time at which the list of installed packages was updated.")
63 (defvar pcmpl-rpm-packages nil
64 "List of installed packages.")
68 ;; This can be slow, so:
69 ;; Consider printing an explanatory message before running -qa.
70 (defun pcmpl-rpm-packages ()
71 "Return a list of all installed rpm packages."
72 (if (and pcmpl-rpm-cache
74 (let ((mtime (nth 5 (file-attributes pcmpl-rpm-cache-stamp-file
))))
75 (and mtime
(not (time-less-p pcmpl-rpm-cache-time mtime
)))))
77 (setq pcmpl-rpm-cache-time
(current-time)
79 (split-string (apply 'pcomplete-process-result
"rpm"
81 pcmpl-rpm-query-options
))))))
83 ;; Should this use pcmpl-rpm-query-options?
84 ;; I don't think it would speed it up at all (?).
85 (defun pcmpl-rpm-all-query (flag)
86 (message "Querying all packages with `%s'..." flag
)
87 (let ((pkgs (pcmpl-rpm-packages))
90 (nconc provs
(split-string
91 (pcomplete-process-result
92 "rpm" "-q" (car pkgs
) flag
)))
93 (setq pkgs
(cdr pkgs
)))
94 (pcomplete-uniqify-list (cdr provs
))))
96 (defsubst pcmpl-rpm-files
()
97 (pcomplete-dirs-or-entries "\\.rpm\\'"))
100 (defun pcomplete/rpm
()
101 "Completion for the `rpm' command."
102 ;; Originally taken from the output of `rpm --help' on a Red Hat 6.1 system.
104 (while (<= pcomplete-index pcomplete-last
)
106 (if (pcomplete-match "^--\\(.*\\)" 0)
126 (pcomplete-opt "vqVyiUebtK")))
128 ; -t<stage> <tarball> - build package, where <stage> is one of:
129 ; p - prep (unpack sources and apply patches)
130 ; l - list check (do some cursory checks on %files)
131 ; c - compile (prep and compile)
132 ; i - install (prep, compile, install)
133 ; b - binary package (prep, compile, install, package)
134 ; a - bin/src package (prep, compile, install, package)
136 ((or (eq mode
'query
)
137 (pcomplete-match "-[^-]*q"))
139 (if (pcomplete-match "^--\\(.*\\)" 0)
146 "--ftpport" ;nyi for the next four
160 ((pcomplete-test "--dbpath")
161 (pcomplete-here* (pcomplete-dirs)))
162 ((pcomplete-test "--queryformat")
164 ((pcomplete-test "--rcfile")
165 (pcomplete-here* (pcomplete-entries)))
166 ((pcomplete-test "--file")
167 (pcomplete-here* (pcomplete-entries)))
168 ((pcomplete-test "--root")
169 (pcomplete-here* (pcomplete-dirs)))
170 ((pcomplete-test "--scripts")
171 (if (pcomplete-match "^--\\(.*\\)" 0)
172 (pcomplete-here* '("--triggers"))))
173 ((pcomplete-test "--triggeredby")
174 (pcomplete-here* (pcmpl-rpm-packages)))
175 ((pcomplete-test "--whatprovides")
177 (pcmpl-rpm-all-query "--provides")))
178 ((pcomplete-test "--whatrequires")
180 (pcmpl-rpm-all-query "--requires")))))
181 (if (pcomplete-match "^-" 0)
182 (pcomplete-opt "af.p(pcmpl-rpm-files)ilsdcvR")
183 (if (pcomplete-test "-[^-]*p" 'first
1)
184 (pcomplete-here (pcmpl-rpm-files))
185 (if (pcomplete-test "-[^-]*f" 'first
1)
186 (pcomplete-here* (pcomplete-entries))
187 (pcomplete-here (pcmpl-rpm-packages)))))))
188 ((pcomplete-test "--pipe")
189 (pcomplete-here* (funcall pcomplete-command-completion-function
)))
190 ((pcomplete-test "--rmsource")
191 (pcomplete-here* (pcomplete-entries))
192 (throw 'pcomplete-completions nil
))
193 ((pcomplete-match "\\`--re\\(build\\|compile\\)\\'")
194 (pcomplete-here (pcmpl-rpm-files))
195 (throw 'pcomplete-completions nil
))
196 ((pcomplete-match "\\`--\\(resign\\|addsign\\)\\'")
197 (while (pcomplete-here (pcmpl-rpm-files))))
198 ((or (eq mode
'checksig
)
199 (pcomplete-test "--checksig"))
200 (setq mode
'checksig
)
201 (if (pcomplete-match "^--\\(.*\\)" 0)
209 ((pcomplete-test "--rcfile")
210 (pcomplete-here* (pcomplete-entries)))))
211 (if (pcomplete-match "^-" 0)
213 (pcomplete-here (pcmpl-rpm-files)))))
214 ((or (eq mode
'rebuilddb
)
215 (pcomplete-test "--rebuilddb"))
216 (setq mode
'rebuilddb
)
217 (if (pcomplete-match "^--\\(.*\\)" 0)
224 ((pcomplete-test "--dbpath")
225 (pcomplete-here* (pcomplete-dirs)))
226 ((pcomplete-test "--root")
227 (pcomplete-here* (pcomplete-dirs)))
228 ((pcomplete-test "--rcfile")
229 (pcomplete-here* (pcomplete-entries)))))
230 (if (pcomplete-match "^-" 0)
233 ((memq mode
'(install upgrade
))
234 (if (pcomplete-match "^--\\(.*\\)" 0)
254 (if (eq mode
'upgrade
)
264 ((pcomplete-test "--dbpath")
265 (pcomplete-here* (pcomplete-dirs)))
266 ((pcomplete-test "--relocate")
268 ((pcomplete-test "--rcfile")
269 (pcomplete-here* (pcomplete-entries)))
270 ((pcomplete-test "--excludepath")
271 (pcomplete-here* (pcomplete-entries)))
272 ((pcomplete-test "--root")
273 (pcomplete-here* (pcomplete-dirs)))
274 ((pcomplete-test "--prefix")
275 (pcomplete-here* (pcomplete-dirs)))))
276 (if (pcomplete-match "^-" 0)
278 (pcomplete-here (pcmpl-rpm-files)))))
279 ((or (pcomplete-test "--install")
280 (pcomplete-match "-[^-]*i"))
281 (setq mode
'install
))
282 ((or (pcomplete-test "--upgrade")
283 (pcomplete-match "-[^-]*U"))
284 (setq mode
'upgrade
))
285 ((or (eq mode
'erase
)
286 (pcomplete-test "--erase")
287 (pcomplete-match "-[^-]*e"))
289 (if (pcomplete-match "^--\\(.*\\)" 0)
302 ((pcomplete-test "--dbpath")
303 (pcomplete-here* (pcomplete-dirs)))
304 ((pcomplete-test "--rcfile")
305 (pcomplete-here* (pcomplete-entries)))
306 ((pcomplete-test "--root")
307 (pcomplete-here* (pcomplete-dirs)))))
308 (if (pcomplete-match "^-" 0)
310 (pcomplete-here (pcmpl-rpm-packages)))))
311 ((or (eq mode
'verify
)
312 (pcomplete-test "--verify"))
314 (if (pcomplete-match "^--\\(.*\\)" 0)
327 ((pcomplete-test "--dbpath")
328 (pcomplete-here* (pcomplete-dirs)))
329 ((pcomplete-test "--rcfile")
330 (pcomplete-here* (pcomplete-entries)))
331 ((pcomplete-test "--root")
332 (pcomplete-here* (pcomplete-dirs)))
333 ((pcomplete-test "--triggeredby")
334 (pcomplete-here* (pcmpl-rpm-packages)))
335 ((pcomplete-test "--whatprovides")
337 (pcmpl-rpm-all-query "--provides")))
338 ((pcomplete-test "--whatrequires")
340 (pcmpl-rpm-all-query "--requires")))))
341 (if (pcomplete-match "^-" 0)
342 (pcomplete-opt "af.p(pcmpl-rpm-files)v")
343 (pcomplete-here (pcmpl-rpm-packages)))))
344 ((or (memq mode
'(build test
))
345 (pcomplete-match "\\`-[bt]"))
346 (setq mode
(if (pcomplete-match "\\`-b")
349 (if (pcomplete-match "^--\\(.*\\)" 0)
362 ((pcomplete-test "--buildroot")
363 (pcomplete-here* (pcomplete-dirs)))
364 ((pcomplete-test "--rcfile")
365 (pcomplete-here* (pcomplete-entries)))
366 ((pcomplete-test "--timecheck")
368 (if (pcomplete-match "^-" 0)
371 (pcomplete-dirs-or-entries (if (eq mode
'test
)
375 (error "You must select a mode: -q, -i, -U, --verify, etc"))))))
379 ;;; pcmpl-rpm.el ends here