1 ;;; pcmpl-rpm.el --- functions for dealing with rpm completions
3 ;; Copyright (C) 1999-2017 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 "String, or list of strings, with extra options for an rpm query command."
48 :type
'(choice (const :tag
"No options" nil
)
49 (string :tag
"Single option")
50 (repeat :tag
"List of options" string
))
53 (defcustom pcmpl-rpm-cache t
54 "Whether to cache the list of installed packages."
59 (defconst pcmpl-rpm-cache-stamp-file
"/var/lib/rpm/Packages"
60 "File used to check that the list of installed packages is up-to-date.")
62 (defvar pcmpl-rpm-cache-time nil
63 "Time at which the list of installed packages was updated.")
65 (defvar pcmpl-rpm-packages nil
66 "List of installed packages.")
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 (message "Getting list of installed rpms...")
78 (setq pcmpl-rpm-cache-time
(current-time)
80 (split-string (apply 'pcomplete-process-result
"rpm"
82 (if (stringp pcmpl-rpm-query-options
)
83 (list pcmpl-rpm-query-options
)
84 pcmpl-rpm-query-options
)))))
85 (message "Getting list of installed rpms...done")
88 ;; Should this use pcmpl-rpm-query-options?
89 ;; I don't think it would speed it up at all (?).
90 (defun pcmpl-rpm-all-query (flag)
91 (message "Querying all packages with `%s'..." flag
)
92 (let ((pkgs (pcmpl-rpm-packages))
95 (nconc provs
(split-string
96 (pcomplete-process-result
97 "rpm" "-q" (car pkgs
) flag
)))
98 (setq pkgs
(cdr pkgs
)))
99 (pcomplete-uniqify-list (cdr provs
))))
101 (defsubst pcmpl-rpm-files
()
102 (pcomplete-dirs-or-entries "\\.rpm\\'"))
105 (defun pcomplete/rpm
()
106 "Completion for the `rpm' command."
107 ;; Originally taken from the output of `rpm --help' on a Red Hat 6.1 system.
109 (while (<= pcomplete-index pcomplete-last
)
111 (if (pcomplete-match "^--\\(.*\\)" 0)
131 (pcomplete-opt "vqVyiUebtK")))
133 ; -t<stage> <tarball> - build package, where <stage> is one of:
134 ; p - prep (unpack sources and apply patches)
135 ; l - list check (do some cursory checks on %files)
136 ; c - compile (prep and compile)
137 ; i - install (prep, compile, install)
138 ; b - binary package (prep, compile, install, package)
139 ; a - bin/src package (prep, compile, install, package)
141 ((or (eq mode
'query
)
142 (pcomplete-match "-[^-]*q"))
144 (if (pcomplete-match "^--\\(.*\\)" 0)
151 "--ftpport" ;nyi for the next four
165 ((pcomplete-test "--dbpath")
166 (pcomplete-here* (pcomplete-dirs)))
167 ((pcomplete-test "--queryformat")
169 ((pcomplete-test "--rcfile")
170 (pcomplete-here* (pcomplete-entries)))
171 ((pcomplete-test "--file")
172 (pcomplete-here* (pcomplete-entries)))
173 ((pcomplete-test "--root")
174 (pcomplete-here* (pcomplete-dirs)))
175 ((pcomplete-test "--scripts")
176 (if (pcomplete-match "^--\\(.*\\)" 0)
177 (pcomplete-here* '("--triggers"))))
178 ((pcomplete-test "--triggeredby")
179 (pcomplete-here* (pcmpl-rpm-packages)))
180 ((pcomplete-test "--whatprovides")
182 (pcmpl-rpm-all-query "--provides")))
183 ((pcomplete-test "--whatrequires")
185 (pcmpl-rpm-all-query "--requires")))))
186 (if (pcomplete-match "^-" 0)
187 (pcomplete-opt "af.p(pcmpl-rpm-files)ilsdcvR")
188 (if (pcomplete-test "-[^-]*p" 'first
1)
189 (pcomplete-here (pcmpl-rpm-files))
190 (if (pcomplete-test "-[^-]*f" 'first
1)
191 (pcomplete-here* (pcomplete-entries))
192 (pcomplete-here (pcmpl-rpm-packages)))))))
193 ((pcomplete-test "--pipe")
194 (pcomplete-here* (funcall pcomplete-command-completion-function
)))
195 ((pcomplete-test "--rmsource")
196 (pcomplete-here* (pcomplete-entries))
197 (throw 'pcomplete-completions nil
))
198 ((pcomplete-match "\\`--re\\(build\\|compile\\)\\'")
199 (pcomplete-here (pcmpl-rpm-files))
200 (throw 'pcomplete-completions nil
))
201 ((pcomplete-match "\\`--\\(resign\\|addsign\\)\\'")
202 (while (pcomplete-here (pcmpl-rpm-files))))
203 ((or (eq mode
'checksig
)
204 (pcomplete-test "--checksig"))
205 (setq mode
'checksig
)
206 (if (pcomplete-match "^--\\(.*\\)" 0)
214 ((pcomplete-test "--rcfile")
215 (pcomplete-here* (pcomplete-entries)))))
216 (if (pcomplete-match "^-" 0)
218 (pcomplete-here (pcmpl-rpm-files)))))
219 ((or (eq mode
'rebuilddb
)
220 (pcomplete-test "--rebuilddb"))
221 (setq mode
'rebuilddb
)
222 (if (pcomplete-match "^--\\(.*\\)" 0)
229 ((pcomplete-test "--dbpath")
230 (pcomplete-here* (pcomplete-dirs)))
231 ((pcomplete-test "--root")
232 (pcomplete-here* (pcomplete-dirs)))
233 ((pcomplete-test "--rcfile")
234 (pcomplete-here* (pcomplete-entries)))))
235 (if (pcomplete-match "^-" 0)
238 ((memq mode
'(install upgrade
))
239 (if (pcomplete-match "^--\\(.*\\)" 0)
259 (if (eq mode
'upgrade
)
269 ((pcomplete-test "--dbpath")
270 (pcomplete-here* (pcomplete-dirs)))
271 ((pcomplete-test "--relocate")
273 ((pcomplete-test "--rcfile")
274 (pcomplete-here* (pcomplete-entries)))
275 ((pcomplete-test "--excludepath")
276 (pcomplete-here* (pcomplete-entries)))
277 ((pcomplete-test "--root")
278 (pcomplete-here* (pcomplete-dirs)))
279 ((pcomplete-test "--prefix")
280 (pcomplete-here* (pcomplete-dirs)))))
281 (if (pcomplete-match "^-" 0)
283 (pcomplete-here (pcmpl-rpm-files)))))
284 ((or (pcomplete-test "--install")
285 (pcomplete-match "-[^-]*i"))
286 (setq mode
'install
))
287 ((or (pcomplete-test "--upgrade")
288 (pcomplete-match "-[^-]*U"))
289 (setq mode
'upgrade
))
290 ((or (eq mode
'erase
)
291 (pcomplete-test "--erase")
292 (pcomplete-match "-[^-]*e"))
294 (if (pcomplete-match "^--\\(.*\\)" 0)
307 ((pcomplete-test "--dbpath")
308 (pcomplete-here* (pcomplete-dirs)))
309 ((pcomplete-test "--rcfile")
310 (pcomplete-here* (pcomplete-entries)))
311 ((pcomplete-test "--root")
312 (pcomplete-here* (pcomplete-dirs)))))
313 (if (pcomplete-match "^-" 0)
315 (pcomplete-here (pcmpl-rpm-packages)))))
316 ((or (eq mode
'verify
)
317 (pcomplete-test "--verify"))
319 (if (pcomplete-match "^--\\(.*\\)" 0)
332 ((pcomplete-test "--dbpath")
333 (pcomplete-here* (pcomplete-dirs)))
334 ((pcomplete-test "--rcfile")
335 (pcomplete-here* (pcomplete-entries)))
336 ((pcomplete-test "--root")
337 (pcomplete-here* (pcomplete-dirs)))
338 ((pcomplete-test "--triggeredby")
339 (pcomplete-here* (pcmpl-rpm-packages)))
340 ((pcomplete-test "--whatprovides")
342 (pcmpl-rpm-all-query "--provides")))
343 ((pcomplete-test "--whatrequires")
345 (pcmpl-rpm-all-query "--requires")))))
346 (if (pcomplete-match "^-" 0)
347 (pcomplete-opt "af.p(pcmpl-rpm-files)v")
348 (pcomplete-here (pcmpl-rpm-packages)))))
349 ((or (memq mode
'(build test
))
350 (pcomplete-match "\\`-[bt]"))
351 (setq mode
(if (pcomplete-match "\\`-b")
354 (if (pcomplete-match "^--\\(.*\\)" 0)
367 ((pcomplete-test "--buildroot")
368 (pcomplete-here* (pcomplete-dirs)))
369 ((pcomplete-test "--rcfile")
370 (pcomplete-here* (pcomplete-entries)))
371 ((pcomplete-test "--timecheck")
373 (if (pcomplete-match "^-" 0)
376 (pcomplete-dirs-or-entries (if (eq mode
'test
)
380 (error "You must select a mode: -q, -i, -U, --verify, etc"))))))
384 ;;; pcmpl-rpm.el ends here