1 ;;; pcmpl-rpm.el --- functions for dealing with rpm completions
3 ;; Copyright (C) 1999-2018 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 <https://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 (file-attribute-modification-time
75 (file-attributes pcmpl-rpm-cache-stamp-file
))))
76 (and mtime
(not (time-less-p pcmpl-rpm-cache-time mtime
)))))
78 (message "Getting list of installed rpms...")
79 (setq pcmpl-rpm-cache-time
(current-time)
81 (split-string (apply 'pcomplete-process-result
"rpm"
83 (if (stringp pcmpl-rpm-query-options
)
84 (list pcmpl-rpm-query-options
)
85 pcmpl-rpm-query-options
)))))
86 (message "Getting list of installed rpms...done")
89 ;; Should this use pcmpl-rpm-query-options?
90 ;; I don't think it would speed it up at all (?).
91 (defun pcmpl-rpm-all-query (flag)
92 (message "Querying all packages with `%s'..." flag
)
93 (let ((pkgs (pcmpl-rpm-packages))
96 (nconc provs
(split-string
97 (pcomplete-process-result
98 "rpm" "-q" (car pkgs
) flag
)))
99 (setq pkgs
(cdr pkgs
)))
100 (pcomplete-uniquify-list (cdr provs
))))
102 (defsubst pcmpl-rpm-files
()
103 (pcomplete-dirs-or-entries "\\.rpm\\'"))
106 (defun pcomplete/rpm
()
107 "Completion for the `rpm' command."
108 ;; Originally taken from the output of `rpm --help' on a Red Hat 6.1 system.
110 (while (<= pcomplete-index pcomplete-last
)
112 (if (pcomplete-match "^--\\(.*\\)" 0)
132 (pcomplete-opt "vqVyiUebtK")))
134 ; -t<stage> <tarball> - build package, where <stage> is one of:
135 ; p - prep (unpack sources and apply patches)
136 ; l - list check (do some cursory checks on %files)
137 ; c - compile (prep and compile)
138 ; i - install (prep, compile, install)
139 ; b - binary package (prep, compile, install, package)
140 ; a - bin/src package (prep, compile, install, package)
142 ((or (eq mode
'query
)
143 (pcomplete-match "-[^-]*q"))
145 (if (pcomplete-match "^--\\(.*\\)" 0)
152 "--ftpport" ;nyi for the next four
166 ((pcomplete-test "--dbpath")
167 (pcomplete-here* (pcomplete-dirs)))
168 ((pcomplete-test "--queryformat")
170 ((pcomplete-test "--rcfile")
171 (pcomplete-here* (pcomplete-entries)))
172 ((pcomplete-test "--file")
173 (pcomplete-here* (pcomplete-entries)))
174 ((pcomplete-test "--root")
175 (pcomplete-here* (pcomplete-dirs)))
176 ((pcomplete-test "--scripts")
177 (if (pcomplete-match "^--\\(.*\\)" 0)
178 (pcomplete-here* '("--triggers"))))
179 ((pcomplete-test "--triggeredby")
180 (pcomplete-here* (pcmpl-rpm-packages)))
181 ((pcomplete-test "--whatprovides")
183 (pcmpl-rpm-all-query "--provides")))
184 ((pcomplete-test "--whatrequires")
186 (pcmpl-rpm-all-query "--requires")))))
187 (if (pcomplete-match "^-" 0)
188 (pcomplete-opt "af.p(pcmpl-rpm-files)ilsdcvR")
189 (if (pcomplete-test "-[^-]*p" 'first
1)
190 (pcomplete-here (pcmpl-rpm-files))
191 (if (pcomplete-test "-[^-]*f" 'first
1)
192 (pcomplete-here* (pcomplete-entries))
193 (pcomplete-here (pcmpl-rpm-packages)))))))
194 ((pcomplete-test "--pipe")
195 (pcomplete-here* (funcall pcomplete-command-completion-function
)))
196 ((pcomplete-test "--rmsource")
197 (pcomplete-here* (pcomplete-entries))
198 (throw 'pcomplete-completions nil
))
199 ((pcomplete-match "\\`--re\\(build\\|compile\\)\\'")
200 (pcomplete-here (pcmpl-rpm-files))
201 (throw 'pcomplete-completions nil
))
202 ((pcomplete-match "\\`--\\(resign\\|addsign\\)\\'")
203 (while (pcomplete-here (pcmpl-rpm-files))))
204 ((or (eq mode
'checksig
)
205 (pcomplete-test "--checksig"))
206 (setq mode
'checksig
)
207 (if (pcomplete-match "^--\\(.*\\)" 0)
215 ((pcomplete-test "--rcfile")
216 (pcomplete-here* (pcomplete-entries)))))
217 (if (pcomplete-match "^-" 0)
219 (pcomplete-here (pcmpl-rpm-files)))))
220 ((or (eq mode
'rebuilddb
)
221 (pcomplete-test "--rebuilddb"))
222 (setq mode
'rebuilddb
)
223 (if (pcomplete-match "^--\\(.*\\)" 0)
230 ((pcomplete-test "--dbpath")
231 (pcomplete-here* (pcomplete-dirs)))
232 ((pcomplete-test "--root")
233 (pcomplete-here* (pcomplete-dirs)))
234 ((pcomplete-test "--rcfile")
235 (pcomplete-here* (pcomplete-entries)))))
236 (if (pcomplete-match "^-" 0)
239 ((memq mode
'(install upgrade
))
240 (if (pcomplete-match "^--\\(.*\\)" 0)
260 (if (eq mode
'upgrade
)
270 ((pcomplete-test "--dbpath")
271 (pcomplete-here* (pcomplete-dirs)))
272 ((pcomplete-test "--relocate")
274 ((pcomplete-test "--rcfile")
275 (pcomplete-here* (pcomplete-entries)))
276 ((pcomplete-test "--excludepath")
277 (pcomplete-here* (pcomplete-entries)))
278 ((pcomplete-test "--root")
279 (pcomplete-here* (pcomplete-dirs)))
280 ((pcomplete-test "--prefix")
281 (pcomplete-here* (pcomplete-dirs)))))
282 (if (pcomplete-match "^-" 0)
284 (pcomplete-here (pcmpl-rpm-files)))))
285 ((or (pcomplete-test "--install")
286 (pcomplete-match "-[^-]*i"))
287 (setq mode
'install
))
288 ((or (pcomplete-test "--upgrade")
289 (pcomplete-match "-[^-]*U"))
290 (setq mode
'upgrade
))
291 ((or (eq mode
'erase
)
292 (pcomplete-test "--erase")
293 (pcomplete-match "-[^-]*e"))
295 (if (pcomplete-match "^--\\(.*\\)" 0)
308 ((pcomplete-test "--dbpath")
309 (pcomplete-here* (pcomplete-dirs)))
310 ((pcomplete-test "--rcfile")
311 (pcomplete-here* (pcomplete-entries)))
312 ((pcomplete-test "--root")
313 (pcomplete-here* (pcomplete-dirs)))))
314 (if (pcomplete-match "^-" 0)
316 (pcomplete-here (pcmpl-rpm-packages)))))
317 ((or (eq mode
'verify
)
318 (pcomplete-test "--verify"))
320 (if (pcomplete-match "^--\\(.*\\)" 0)
333 ((pcomplete-test "--dbpath")
334 (pcomplete-here* (pcomplete-dirs)))
335 ((pcomplete-test "--rcfile")
336 (pcomplete-here* (pcomplete-entries)))
337 ((pcomplete-test "--root")
338 (pcomplete-here* (pcomplete-dirs)))
339 ((pcomplete-test "--triggeredby")
340 (pcomplete-here* (pcmpl-rpm-packages)))
341 ((pcomplete-test "--whatprovides")
343 (pcmpl-rpm-all-query "--provides")))
344 ((pcomplete-test "--whatrequires")
346 (pcmpl-rpm-all-query "--requires")))))
347 (if (pcomplete-match "^-" 0)
348 (pcomplete-opt "af.p(pcmpl-rpm-files)v")
349 (pcomplete-here (pcmpl-rpm-packages)))))
350 ((or (memq mode
'(build test
))
351 (pcomplete-match "\\`-[bt]"))
352 (setq mode
(if (pcomplete-match "\\`-b")
355 (if (pcomplete-match "^--\\(.*\\)" 0)
368 ((pcomplete-test "--buildroot")
369 (pcomplete-here* (pcomplete-dirs)))
370 ((pcomplete-test "--rcfile")
371 (pcomplete-here* (pcomplete-entries)))
372 ((pcomplete-test "--timecheck")
374 (if (pcomplete-match "^-" 0)
377 (pcomplete-dirs-or-entries (if (eq mode
'test
)
381 (error "You must select a mode: -q, -i, -U, --verify, etc"))))))
385 ;;; pcmpl-rpm.el ends here