+ 'entries
+ profile params entry-type search-type search-vals)))
+
+(defun guix-get-show-entries (profile buffer-type entry-type search-type
+ &rest search-vals)
+ "Search for ENTRY-TYPE entries and show results in BUFFER-TYPE buffer.
+See `guix-get-entries' for the meaning of SEARCH-TYPE and SEARCH-VALS."
+ (let ((entries (guix-get-entries profile entry-type search-type search-vals
+ (guix-get-params-for-receiving
+ buffer-type entry-type))))
+ (guix-set-buffer profile entries buffer-type entry-type
+ search-type search-vals)))
+
+(defun guix-set-buffer (profile entries buffer-type entry-type search-type
+ search-vals &optional history-replace no-display)
+ "Set up BUFFER-TYPE buffer for displaying ENTRY-TYPE ENTRIES.
+
+Insert ENTRIES in buffer, set variables and make history item.
+ENTRIES should have a form of `guix-entries'.
+
+See `guix-get-entries' for the meaning of SEARCH-TYPE and SEARCH-VALS.
+
+If HISTORY-REPLACE is non-nil, replace current history item,
+otherwise add the new one.
+
+If NO-DISPLAY is non-nil, do not switch to the buffer."
+ (when entries
+ (let ((buf (if (and (eq major-mode
+ (guix-get-symbol "mode" buffer-type entry-type))
+ (equal guix-profile profile))
+ (current-buffer)
+ (get-buffer-create
+ (guix-buffer-name profile buffer-type
+ entry-type search-type)))))
+ (with-current-buffer buf
+ (guix-show-entries entries buffer-type entry-type)
+ (guix-set-vars profile entries buffer-type entry-type
+ search-type search-vals)
+ (funcall (if history-replace
+ #'guix-history-replace
+ #'guix-history-add)
+ (guix-make-history-item)))
+ (or no-display
+ (guix-switch-to-buffer buf))))
+ (guix-result-message profile entries entry-type
+ search-type search-vals))
+
+(defun guix-show-entries (entries buffer-type entry-type)
+ "Display ENTRY-TYPE ENTRIES in the current BUFFER-TYPE buffer."
+ (let ((inhibit-read-only t))
+ (erase-buffer)
+ (funcall (symbol-function (guix-get-symbol
+ "mode" buffer-type entry-type)))
+ (funcall (guix-get-symbol "insert-entries" buffer-type)
+ entries entry-type)
+ (goto-char (point-min))))
+
+(defun guix-history-call (profile entries buffer-type entry-type
+ search-type search-vals)
+ "Function called for moving by history."
+ (guix-show-entries entries buffer-type entry-type)
+ (guix-set-vars profile entries buffer-type entry-type
+ search-type search-vals)
+ (guix-result-message profile entries entry-type
+ search-type search-vals))
+
+(defun guix-make-history-item ()
+ "Make and return a history item for the current buffer."
+ (list #'guix-history-call
+ guix-profile guix-entries guix-buffer-type guix-entry-type
+ guix-search-type guix-search-vals))
+
+(defun guix-get-params-for-receiving (buffer-type entry-type)
+ "Return parameters that should be received for BUFFER-TYPE, ENTRY-TYPE."
+ (let* ((required-var (guix-get-symbol "required-params"
+ buffer-type entry-type))
+ (required (symbol-value required-var)))
+ (unless (equal required 'all)
+ (cl-union required
+ (funcall (guix-get-symbol "get-displayed-params"
+ buffer-type)
+ entry-type)))))
+
+(defun guix-revert-buffer (_ignore-auto noconfirm)
+ "Update information in the current buffer.
+The function is suitable for `revert-buffer-function'.
+See `revert-buffer' for the meaning of NOCONFIRM."
+ (when (or noconfirm
+ (symbol-value
+ (guix-get-symbol "revert-no-confirm"
+ guix-buffer-type guix-entry-type))
+ (y-or-n-p "Update current information? "))
+ (let* ((search-type guix-search-type)
+ (search-vals guix-search-vals)
+ (params (guix-get-params-for-receiving guix-buffer-type
+ guix-entry-type))
+ (entries (guix-get-entries
+ guix-profile guix-entry-type
+ guix-search-type guix-search-vals params))
+ ;; If a REPL was restarted, package/output IDs are not actual
+ ;; anymore, because 'object-address'-es died with the REPL, so if a
+ ;; search by ID didn't give results, search again by name.
+ (entries (if (and (null entries)
+ (eq guix-search-type 'id)
+ (or (eq guix-entry-type 'package)
+ (eq guix-entry-type 'output)))
+ (progn
+ (setq search-type 'name
+ search-vals (guix-entries-to-specifications
+ guix-entries))
+ (guix-get-entries
+ guix-profile guix-entry-type
+ search-type search-vals params))
+ entries)))
+ (guix-set-buffer guix-profile entries guix-buffer-type guix-entry-type
+ search-type search-vals t t))))
+
+(cl-defun guix-redisplay-buffer (&key buffer profile entries buffer-type
+ entry-type search-type search-vals)
+ "Redisplay a Guix BUFFER.
+Restore the point and window positions after redisplaying if possible.
+
+This function will not update the information, use
+\"\\[revert-buffer]\" if you want the full update.
+
+If BUFFER is nil, use the current buffer. For the meaning of the
+rest arguments, see `guix-set-buffer'."
+ (interactive)
+ (or buffer (setq buffer (current-buffer)))
+ (with-current-buffer buffer
+ (or (derived-mode-p 'guix-info-mode 'guix-list-mode)
+ (error "%S is not a Guix buffer" buffer))
+ (let* ((point (point))
+ (was-at-button (button-at point))
+ ;; For simplicity, ignore an unlikely case when multiple
+ ;; windows display the same BUFFER.
+ (window (car (get-buffer-window-list buffer nil t)))
+ (window-start (and window (window-start window))))
+ (guix-set-buffer (or profile guix-profile)
+ (or entries guix-entries)
+ (or buffer-type guix-buffer-type)
+ (or entry-type guix-entry-type)
+ (or search-type guix-search-type)
+ (or search-vals guix-search-vals)
+ t t)
+ (goto-char point)
+ (and was-at-button
+ (not (button-at (point)))
+ (forward-button 1))
+ (when window
+ (set-window-point window (point))
+ (set-window-start window window-start)))))
+
+\f
+;;; Generations
+
+(defcustom guix-generation-packages-buffer-name-function
+ #'guix-generation-packages-buffer-name-default
+ "Function used to define name of a buffer with generation packages.
+This function is called with 2 arguments: PROFILE (string) and
+GENERATION (number)."
+ :type '(choice (function-item guix-generation-packages-buffer-name-default)
+ (function-item guix-generation-packages-buffer-name-long)
+ (function :tag "Other function"))
+ :group 'guix)
+
+(defcustom guix-generation-packages-update-buffer t
+ "If non-nil, always update list of packages during comparing generations.
+If nil, generation packages are received only once. So when you
+compare generation 1 and generation 2, the packages for both
+generations will be received. Then if you compare generation 1
+and generation 3, only the packages for generation 3 will be
+received. Thus if you use comparing of different generations a
+lot, you may set this variable to nil to improve the
+performance."
+ :type 'boolean
+ :group 'guix)
+
+(defvar guix-output-name-width 30
+ "Width of an output name \"column\".
+This variable is used in auxiliary buffers for comparing generations.")
+
+(defun guix-generation-file (profile generation)
+ "Return the file name of a PROFILE's GENERATION."
+ (format "%s-%s-link" profile generation))
+
+(defun guix-manifest-file (profile &optional generation)
+ "Return the file name of a PROFILE's manifest.
+If GENERATION number is specified, return manifest file name for
+this generation."
+ (expand-file-name "manifest"
+ (if generation
+ (guix-generation-file profile generation)
+ profile)))
+
+(defun guix-generation-packages (profile generation)
+ "Return a list of sorted packages installed in PROFILE's GENERATION.
+Each element of the list is a list of the package specification and its path."
+ (let ((names+paths (guix-eval-read
+ (guix-make-guile-expression
+ 'generation-package-specifications+paths
+ profile generation))))
+ (sort names+paths
+ (lambda (a b)
+ (string< (car a) (car b))))))
+
+(defun guix-generation-packages-buffer-name-default (profile generation)
+ "Return name of a buffer for displaying GENERATION's package outputs.
+Use base name of PROFILE path."
+ (let ((profile-name (file-name-base (directory-file-name profile))))
+ (format "*Guix %s: generation %s*"
+ profile-name generation)))
+
+(defun guix-generation-packages-buffer-name-long (profile generation)
+ "Return name of a buffer for displaying GENERATION's package outputs.
+Use the full PROFILE path."
+ (format "*Guix generation %s (%s)*"
+ generation profile))
+
+(defun guix-generation-packages-buffer-name (profile generation)
+ "Return name of a buffer for displaying GENERATION's package outputs."
+ (let ((fun (if (functionp guix-generation-packages-buffer-name-function)
+ guix-generation-packages-buffer-name-function
+ #'guix-generation-packages-buffer-name-default)))
+ (funcall fun profile generation)))
+
+(defun guix-generation-insert-package (name path)
+ "Insert package output NAME and PATH at point."
+ (insert name)
+ (indent-to guix-output-name-width 2)
+ (insert path "\n"))
+
+(defun guix-generation-insert-packages (buffer profile generation)
+ "Insert package outputs installed in PROFILE's GENERATION in BUFFER."
+ (with-current-buffer buffer
+ (setq buffer-read-only nil
+ indent-tabs-mode nil)
+ (erase-buffer)
+ (mapc (lambda (name+path)
+ (guix-generation-insert-package
+ (car name+path) (cadr name+path)))
+ (guix-generation-packages profile generation))))
+
+(defun guix-generation-packages-buffer (profile generation)
+ "Return buffer with package outputs installed in PROFILE's GENERATION.
+Create the buffer if needed."
+ (let ((buf-name (guix-generation-packages-buffer-name
+ profile generation)))
+ (or (and (null guix-generation-packages-update-buffer)
+ (get-buffer buf-name))
+ (let ((buf (get-buffer-create buf-name)))
+ (guix-generation-insert-packages buf profile generation)
+ buf))))
+
+(defun guix-profile-generation-manifest-file (generation)
+ "Return the file name of a GENERATION's manifest.
+GENERATION is a generation number of `guix-profile' profile."
+ (guix-manifest-file guix-profile generation))
+
+(defun guix-profile-generation-packages-buffer (generation)
+ "Insert GENERATION's package outputs in a buffer and return it.
+GENERATION is a generation number of `guix-profile' profile."
+ (guix-generation-packages-buffer guix-profile generation))