Further speed up rpm completion, by caching the installed packages
authorGlenn Morris <rgm@gnu.org>
Fri, 22 Jun 2012 07:38:26 +0000 (00:38 -0700)
committerGlenn Morris <rgm@gnu.org>
Fri, 22 Jun 2012 07:38:26 +0000 (00:38 -0700)
* lisp/pcmpl-rpm.el (pcmpl-rpm-cache): New option.
(pcmpl-rpm-cache-stamp-file): New constant.
(pcmpl-rpm-cache-time, pcmpl-rpm-packages): New variables.
(pcmpl-rpm-packages): Optionally cache list of packages.

lisp/ChangeLog
lisp/pcmpl-rpm.el

index 1119a52..50950a2 100644 (file)
 
 2012-06-22  Glenn Morris  <rgm@gnu.org>
 
+       * pcmpl-rpm.el (pcmpl-rpm-cache): New option.
+       (pcmpl-rpm-cache-stamp-file): New constant.
+       (pcmpl-rpm-cache-time, pcmpl-rpm-packages): New variables.
+       (pcmpl-rpm-packages): Optionally cache list of packages.
+
        * pcmpl-rpm.el (pcmpl-rpm): New group.
        (pcmpl-rpm-query-options): New option.
        (pcmpl-rpm-packages): No need to inline it.
index 6347666..12fce36 100644 (file)
   :type '(repeat string)
   :group 'pcmpl-rpm)
 
+(defcustom pcmpl-rpm-cache t
+  "Whether to cache the list of installed packages."
+  :version "24.2"
+  :type 'boolean
+  :group 'pcmpl-rpm)
+
+(defconst pcmpl-rpm-cache-stamp-file "/var/lib/rpm/Packages"
+  "File used to check that the list of installed packages is up-to-date.")
+
+(defvar pcmpl-rpm-cache-time nil
+  "Time at which the list of installed packages was updated.")
+
+(defvar pcmpl-rpm-packages nil
+  "List of installed packages.")
+
 ;; Functions:
 
-;; TODO
 ;; This can be slow, so:
-;; Consider caching the result (cf woman).
 ;; Consider printing an explanatory message before running -qa.
 (defun pcmpl-rpm-packages ()
   "Return a list of all installed rpm packages."
-  (split-string (apply 'pcomplete-process-result "rpm"
-                       (append '("-q" "-a") pcmpl-rpm-query-options))))
+  (if (and pcmpl-rpm-cache
+           pcmpl-rpm-cache-time
+           (let ((mtime (nth 5 (file-attributes pcmpl-rpm-cache-stamp-file))))
+             (and mtime (not (time-less-p pcmpl-rpm-cache-time mtime)))))
+      pcmpl-rpm-packages
+    (setq pcmpl-rpm-cache-time (current-time)
+          pcmpl-rpm-packages
+          (split-string (apply 'pcomplete-process-result "rpm"
+                               (append '("-q" "-a")
+                                       pcmpl-rpm-query-options))))))
 
 ;; Should this use pcmpl-rpm-query-options?
 ;; I don't think it would speed it up at all (?).