-(define* (scheme-files directory)
- "Return the list of Scheme files found under DIRECTORY, recursively. The
-returned list is sorted in alphabetical order."
-
- ;; Sort entries so that 'fold-packages' works in a deterministic fashion
- ;; regardless of details of the underlying file system.
- (sort (file-system-fold (const #t) ; enter?
- (lambda (path stat result) ; leaf
- (if (string-suffix? ".scm" path)
- (cons path result)
- result))
- (lambda (path stat result) ; down
- result)
- (lambda (path stat result) ; up
- result)
- (const #f) ; skip
- (lambda (path stat errno result)
- (warning (_ "cannot access `~a': ~a~%")
- path (strerror errno))
- result)
- '()
- directory
- stat)
- string<?))
-
-(define file-name->module-name
- (let ((not-slash (char-set-complement (char-set #\/))))
- (lambda (file)
- "Return the module name (a list of symbols) corresponding to FILE."
- (map string->symbol
- (string-tokenize (string-drop-right file 4) not-slash)))))
-
-(define* (scheme-modules directory #:optional sub-directory)
- "Return the list of Scheme modules available under DIRECTORY.
-Optionally, narrow the search to SUB-DIRECTORY."
- (define prefix-len
- (string-length directory))
-
- (filter-map (lambda (file)
- (let* ((file (substring file prefix-len))
- (module (file-name->module-name file)))
- (catch #t
- (lambda ()
- (resolve-interface module))
- (lambda args
- ;; Report the error, but keep going.
- (warn-about-load-error module args)
- #f))))
- (scheme-files (if sub-directory
- (string-append directory "/" sub-directory)
- directory))))
-
-(define* (all-package-modules #:optional (path (%package-module-path)))
- "Return the list of package modules found in PATH, a list of directories to
-search."
- (fold-right (lambda (spec result)
- (match spec
- ((? string? directory)
- (append (scheme-modules directory) result))
- ((directory . sub-directory)
- (append (scheme-modules directory sub-directory)
- result))))
- '()
- path))
-
-(define (fold-packages proc init)
- "Call (PROC PACKAGE RESULT) for each available package, using INIT as
-the initial value of RESULT. It is guaranteed to never traverse the
-same package twice."
- (identity ; discard second return value
- (fold2 (lambda (module result seen)
- (fold2 (lambda (var result seen)
- (if (and (package? var)
- (not (vhash-assq var seen))
- (not (hidden-package? var)))
- (values (proc var result)
- (vhash-consq var #t seen))
- (values result seen)))
- result
- seen
- (module-map (lambda (sym var)
- (false-if-exception (variable-ref var)))
- module)))
- init
- vlist-null
- (all-package-modules))))
-
-(define find-packages-by-name
+;; This procedure is used by Emacs-Guix up to 0.5.1.1, so keep it for now.
+;; See <https://github.com/alezost/guix.el/issues/30>.
+(define-deprecated find-newest-available-packages
+ find-packages-by-name
+ (mlambda ()
+ "Return a vhash keyed by package names, and with
+associated values of the form
+
+ (newest-version newest-package ...)
+
+where the preferred package is listed first."
+ (fold-packages (lambda (p r)
+ (let ((name (package-name p))
+ (version (package-version p)))
+ (match (vhash-assoc name r)
+ ((_ newest-so-far . pkgs)
+ (case (version-compare version newest-so-far)
+ ((>) (vhash-cons name `(,version ,p) r))
+ ((=) (vhash-cons name `(,version ,p ,@pkgs) r))
+ ((<) r)))
+ (#f (vhash-cons name `(,version ,p) r)))))
+ vlist-null)))
+
+(define (fold-available-packages proc init)
+ "Fold PROC over the list of available packages. For each available package,
+PROC is called along these lines:
+
+ (PROC NAME VERSION RESULT
+ #:outputs OUTPUTS
+ #:location LOCATION
+ …)
+
+PROC can use #:allow-other-keys to ignore the bits it's not interested in.
+When a package cache is available, this procedure does not actually load any
+package module."
+ (define cache
+ (load-package-cache (current-profile)))
+
+ (if (and cache (cache-is-authoritative?))
+ (vhash-fold (lambda (name vector result)
+ (match vector
+ (#(name version module symbol outputs
+ supported? deprecated?
+ file line column)
+ (proc name version result
+ #:outputs outputs
+ #:location (and file
+ (location file line column))
+ #:supported? supported?
+ #:deprecated? deprecated?))))
+ init
+ cache)
+ (fold-packages (lambda (package result)
+ (proc (package-name package)
+ (package-version package)
+ result
+ #:outputs (package-outputs package)
+ #:location (package-location package)
+ #:supported?
+ (->bool (supported-package? package))
+ #:deprecated?
+ (->bool
+ (package-superseded package))))
+ init)))
+
+(define* (fold-packages proc init
+ #:optional
+ (modules (all-modules (%package-module-path)
+ #:warn
+ warn-about-load-error))
+ #:key (select? (negate hidden-package?)))
+ "Call (PROC PACKAGE RESULT) for each available package defined in one of
+MODULES that matches SELECT?, using INIT as the initial value of RESULT. It
+is guaranteed to never traverse the same package twice."
+ (fold-module-public-variables (lambda (object result)
+ (if (and (package? object) (select? object))
+ (proc object result)
+ result))
+ init
+ modules))
+
+(define %package-cache-file
+ ;; Location of the package cache.
+ "/lib/guix/package.cache")
+
+(define load-package-cache
+ (mlambda (profile)
+ "Attempt to load the package cache. On success return a vhash keyed by
+package names. Return #f on failure."
+ (match profile
+ (#f #f)
+ (profile
+ (catch 'system-error
+ (lambda ()
+ (define lst
+ (load-compiled (string-append profile %package-cache-file)))
+ (fold (lambda (item vhash)
+ (match item
+ (#(name version module symbol outputs
+ supported? deprecated?
+ file line column)
+ (vhash-cons name item vhash))))
+ vlist-null
+ lst))
+ (lambda args
+ (if (= ENOENT (system-error-errno args))
+ #f
+ (apply throw args))))))))
+
+(define find-packages-by-name/direct ;bypass the cache