emacs: Add interface for package locations.
[jackhill/guix/guix.git] / emacs / guix-ui-package.el
CommitLineData
c80ce104
AK
1;;; guix-ui-package.el --- Interface for displaying packages -*- lexical-binding: t -*-
2
b4ea535a 3;; Copyright © 2014, 2015, 2016 Alex Kost <alezost@gmail.com>
c80ce104
AK
4
5;; This file is part of GNU Guix.
6
7;; GNU Guix is free software; you can redistribute it and/or modify
8;; it under the terms of the GNU General Public License as published by
9;; the Free Software Foundation, either version 3 of the License, or
10;; (at your option) any later version.
11
12;; GNU Guix is distributed in the hope that it will be useful,
13;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15;; GNU General Public License for more details.
16
17;; You should have received a copy of the GNU General Public License
18;; along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20;;; Commentary:
21
22;; This file provides an interface for displaying packages and outputs
23;; in 'list' and 'info' buffers, and commands for working with them.
24
25;;; Code:
26
27(require 'cl-lib)
28(require 'guix-buffer)
29(require 'guix-list)
30(require 'guix-info)
31(require 'guix-ui)
32(require 'guix-base)
33(require 'guix-backend)
34(require 'guix-guile)
35(require 'guix-entry)
36(require 'guix-utils)
b8fa5a2a 37(require 'guix-hydra)
5c8994d9 38(require 'guix-hydra-build)
83aab70b 39(require 'guix-read)
cefb7aea 40(require 'guix-license)
79c7a8f2 41(require 'guix-location)
e20f051e 42(require 'guix-profiles)
c80ce104 43
8ed2c92e
AK
44(guix-ui-define-entry-type package)
45(guix-ui-define-entry-type output)
c80ce104
AK
46
47(defcustom guix-package-list-type 'output
48 "Define how to display packages in 'list' buffer.
49Should be a symbol `package' or `output' (if `output', display each
50output on a separate line; if `package', display each package on
51a separate line)."
52 :type '(choice (const :tag "List of packages" package)
53 (const :tag "List of outputs" output))
54 :group 'guix-package)
55
56(defcustom guix-package-info-type 'package
57 "Define how to display packages in 'info' buffer.
58Should be a symbol `package' or `output' (if `output', display
59each output separately; if `package', display outputs inside
60package data)."
61 :type '(choice (const :tag "Display packages" package)
62 (const :tag "Display outputs" output))
63 :group 'guix-package)
64
c80ce104
AK
65(defun guix-package-get-display (profile search-type &rest search-values)
66 "Search for packages/outputs and show results.
67
68If PROFILE is nil, use `guix-current-profile'.
69
70See `guix-ui-get-entries' for the meaning of SEARCH-TYPE and
71SEARCH-VALUES.
72
73Results are displayed in the list buffer, unless a single package
74is found and `guix-package-list-single' is nil."
75 (let* ((args (cl-list* (or profile guix-current-profile)
76 search-type search-values))
77 (entries (guix-buffer-get-entries
78 'list guix-package-list-type args)))
79 (if (or guix-package-list-single
80 (null entries)
81 (cdr entries))
82 (guix-buffer-display-entries
83 entries 'list guix-package-list-type args 'add)
84 (guix-buffer-get-display-entries
85 'info guix-package-info-type args 'add))))
86
87(defun guix-package-entry->name-specification (entry &optional output)
88 "Return name specification of the package ENTRY and OUTPUT."
89 (guix-package-name-specification
90 (guix-entry-value entry 'name)
91 (guix-entry-value entry 'version)
92 (or output (guix-entry-value entry 'output))))
93
94(defun guix-package-entries->name-specifications (entries)
95 "Return name specifications by the package or output ENTRIES."
96 (cl-remove-duplicates (mapcar #'guix-package-entry->name-specification
97 entries)
98 :test #'string=))
99
100(defun guix-package-installed-outputs (entry)
101 "Return a list of installed outputs for the package ENTRY."
102 (mapcar (lambda (installed-entry)
103 (guix-entry-value installed-entry 'output))
104 (guix-entry-value entry 'installed)))
105
106(defun guix-package-id-and-output-by-output-id (output-id)
107 "Return a list (PACKAGE-ID OUTPUT) by OUTPUT-ID."
108 (cl-multiple-value-bind (package-id-str output)
109 (split-string output-id ":")
110 (let ((package-id (string-to-number package-id-str)))
111 (list (if (= 0 package-id) package-id-str package-id)
112 output))))
113
114\f
115;;; Processing package actions
116
117(defun guix-process-package-actions (profile actions
118 &optional operation-buffer)
119 "Process package ACTIONS on PROFILE.
120Each action is a list of the form:
121
122 (ACTION-TYPE PACKAGE-SPEC ...)
123
124ACTION-TYPE is one of the following symbols: `install',
125`upgrade', `remove'/`delete'.
126PACKAGE-SPEC should have the following form: (ID [OUTPUT] ...)."
127 (let (install upgrade remove)
128 (mapc (lambda (action)
129 (let ((action-type (car action))
130 (specs (cdr action)))
131 (cl-case action-type
132 (install (setq install (append install specs)))
133 (upgrade (setq upgrade (append upgrade specs)))
134 ((remove delete) (setq remove (append remove specs))))))
135 actions)
136 (when (guix-continue-package-operation-p
137 profile
138 :install install :upgrade upgrade :remove remove)
139 (guix-eval-in-repl
140 (guix-make-guile-expression
141 'process-package-actions profile
142 :install install :upgrade upgrade :remove remove
143 :use-substitutes? (or guix-use-substitutes 'f)
144 :dry-run? (or guix-dry-run 'f))
145 (and (not guix-dry-run) operation-buffer)))))
146
147(cl-defun guix-continue-package-operation-p (profile
148 &key install upgrade remove)
149 "Return non-nil if a package operation should be continued.
150Ask a user if needed (see `guix-operation-confirm').
151INSTALL, UPGRADE, REMOVE are 'package action specifications'.
152See `guix-process-package-actions' for details."
153 (or (null guix-operation-confirm)
154 (let* ((entries (guix-ui-get-entries
155 profile 'package 'id
156 (append (mapcar #'car install)
157 (mapcar #'car upgrade)
158 (mapcar #'car remove))
159 '(id name version location)))
160 (install-strings (guix-get-package-strings install entries))
161 (upgrade-strings (guix-get-package-strings upgrade entries))
162 (remove-strings (guix-get-package-strings remove entries)))
163 (if (or install-strings upgrade-strings remove-strings)
164 (let ((buf (get-buffer-create guix-temp-buffer-name)))
165 (with-current-buffer buf
166 (setq-local cursor-type nil)
167 (setq buffer-read-only nil)
168 (erase-buffer)
169 (insert "Profile: " profile "\n\n")
170 (guix-insert-package-strings install-strings "install")
171 (guix-insert-package-strings upgrade-strings "upgrade")
172 (guix-insert-package-strings remove-strings "remove")
173 (let ((win (temp-buffer-window-show
174 buf
175 '((display-buffer-reuse-window
176 display-buffer-at-bottom)
177 (window-height . fit-window-to-buffer)))))
178 (prog1 (guix-operation-prompt)
179 (quit-window nil win)))))
180 (message "Nothing to be done.
181If Guix REPL was restarted, the data is not up-to-date.")
182 nil))))
183
184(defun guix-get-package-strings (specs entries)
185 "Return short package descriptions for performing package actions.
186See `guix-process-package-actions' for the meaning of SPECS.
187ENTRIES is a list of package entries to get info about packages."
188 (delq nil
189 (mapcar
190 (lambda (spec)
191 (let* ((id (car spec))
192 (outputs (cdr spec))
193 (entry (guix-entry-by-id id entries)))
194 (when entry
195 (let ((location (guix-entry-value entry 'location)))
196 (concat (guix-package-entry->name-specification entry)
197 (when outputs
198 (concat ":"
199 (guix-concat-strings outputs ",")))
200 (when location
201 (concat "\t(" location ")")))))))
202 specs)))
203
204(defun guix-insert-package-strings (strings action)
205 "Insert information STRINGS at point for performing package ACTION."
206 (when strings
207 (insert "Package(s) to " (propertize action 'face 'bold) ":\n")
208 (mapc (lambda (str)
209 (insert " " str "\n"))
210 strings)
211 (insert "\n")))
212
213\f
214;;; Package 'info'
215
216(guix-ui-info-define-interface package
217 :buffer-name "*Guix Package Info*"
218 :format '(guix-package-info-insert-heading
219 ignore
220 (synopsis ignore (simple guix-package-info-synopsis))
221 ignore
222 (description ignore (simple guix-package-info-description))
223 ignore
224 (outputs simple guix-package-info-insert-outputs)
225 (source simple guix-package-info-insert-source)
226 (location format (format guix-package-location))
227 (home-url format (format guix-url))
cefb7aea 228 (license format (format guix-package-license))
0a5ec709 229 (systems format guix-package-info-insert-systems)
c80ce104
AK
230 (inputs format (format guix-package-input))
231 (native-inputs format (format guix-package-native-input))
232 (propagated-inputs format
233 (format guix-package-propagated-input)))
0a5ec709
AK
234 :titles '((home-url . "Home page")
235 (systems . "Supported systems"))
c80ce104
AK
236 :required '(id name version installed non-unique))
237
238(guix-info-define-interface installed-output
239 :format '((path simple (indent guix-file))
240 (dependencies simple (indent guix-file)))
241 :titles '((path . "Store directory"))
242 :reduced? t)
243
244(defface guix-package-info-heading
245 '((t :inherit guix-info-heading))
246 "Face for package name and version headings."
247 :group 'guix-package-info-faces)
248
249(defface guix-package-info-name
250 '((t :inherit font-lock-keyword-face))
251 "Face used for a name of a package."
252 :group 'guix-package-info-faces)
253
254(defface guix-package-info-name-button
255 '((t :inherit button))
256 "Face used for a full name that can be used to describe a package."
257 :group 'guix-package-info-faces)
258
259(defface guix-package-info-version
260 '((t :inherit font-lock-builtin-face))
261 "Face used for a version of a package."
262 :group 'guix-package-info-faces)
263
264(defface guix-package-info-synopsis
265 '((((type tty pc) (class color)) :weight bold)
266 (t :height 1.1 :weight bold :inherit variable-pitch))
267 "Face used for a synopsis of a package."
268 :group 'guix-package-info-faces)
269
270(defface guix-package-info-description
271 '((t))
272 "Face used for a description of a package."
273 :group 'guix-package-info-faces)
274
275(defface guix-package-info-license
276 '((t :inherit font-lock-string-face))
277 "Face used for a license of a package."
278 :group 'guix-package-info-faces)
279
280(defface guix-package-info-location
281 '((t :inherit link))
282 "Face used for a location of a package."
283 :group 'guix-package-info-faces)
284
285(defface guix-package-info-source
286 '((t :inherit link :underline nil))
287 "Face used for a source URL of a package."
288 :group 'guix-package-info-faces)
289
290(defface guix-package-info-installed-outputs
291 '((default :weight bold)
292 (((class color) (min-colors 88) (background light))
293 :foreground "ForestGreen")
294 (((class color) (min-colors 88) (background dark))
295 :foreground "PaleGreen")
296 (((class color) (min-colors 8))
297 :foreground "green")
298 (t :underline t))
299 "Face used for installed outputs of a package."
300 :group 'guix-package-info-faces)
301
302(defface guix-package-info-uninstalled-outputs
303 '((t :weight bold))
304 "Face used for uninstalled outputs of a package."
305 :group 'guix-package-info-faces)
306
307(defface guix-package-info-obsolete
308 '((t :inherit error))
309 "Face used if a package is obsolete."
310 :group 'guix-package-info-faces)
311
312(defcustom guix-package-info-auto-find-source nil
313 "If non-nil, find a source file after pressing a \"Show\" button.
314If nil, just display the source file path without finding."
315 :type 'boolean
316 :group 'guix-package-info)
317
318(defcustom guix-package-info-auto-download-source t
319 "If nil, do not automatically download a source file if it doesn't exist.
320After pressing a \"Show\" button, a derivation of the package
321source is calculated and a store file path is displayed. If this
322variable is non-nil and the source file does not exist in the
323store, it will be automatically downloaded (with a possible
324prompt depending on `guix-operation-confirm' variable)."
325 :type 'boolean
326 :group 'guix-package-info)
327
328(defvar guix-package-info-download-buffer nil
329 "Buffer from which a current download operation was performed.")
330
331(defvar guix-package-info-output-format "%-10s"
332 "String used to format output names of the packages.
333It should be a '%s'-sequence. After inserting an output name
334formatted with this string, an action button is inserted.")
335
336(defvar guix-package-info-obsolete-string "(This package is obsolete)"
337 "String used if a package is obsolete.")
338
339(define-button-type 'guix-package-location
340 :supertype 'guix
341 'face 'guix-package-info-location
342 'help-echo "Find location of this package"
343 'action (lambda (btn)
344 (guix-find-location (button-label btn))))
345
cefb7aea
AK
346(define-button-type 'guix-package-license
347 :supertype 'guix
348 'face 'guix-package-info-license
349 'help-echo "Browse license URL"
350 'action (lambda (btn)
351 (guix-browse-license-url (button-label btn))))
352
c80ce104
AK
353(define-button-type 'guix-package-name
354 :supertype 'guix
355 'face 'guix-package-info-name-button
356 'help-echo "Describe this package"
357 'action (lambda (btn)
358 (guix-buffer-get-display-entries-current
359 'info guix-package-info-type
360 (list (guix-ui-current-profile)
c292a6f3
AK
361 'name (or (button-get btn 'spec)
362 (button-label btn)))
c80ce104
AK
363 'add)))
364
6b3a1ce8
AK
365(define-button-type 'guix-package-heading
366 :supertype 'guix-package-name
367 'face 'guix-package-info-heading)
368
c80ce104
AK
369(define-button-type 'guix-package-source
370 :supertype 'guix
371 'face 'guix-package-info-source
372 'help-echo ""
373 'action (lambda (_)
374 ;; As a source may not be a real URL (e.g., "mirror://..."),
375 ;; no action is bound to a source button.
376 (message "Yes, this is the source URL. What did you expect?")))
377
378(defun guix-package-info-insert-heading (entry)
c292a6f3 379 "Insert package ENTRY heading (name and version) at point."
c80ce104 380 (guix-insert-button
c292a6f3
AK
381 (concat (guix-entry-value entry 'name) " "
382 (guix-entry-value entry 'version))
383 'guix-package-heading
384 'spec (guix-package-entry->name-specification entry)))
c80ce104 385
0a5ec709
AK
386(defun guix-package-info-insert-systems (systems entry)
387 "Insert supported package SYSTEMS at point."
388 (guix-info-insert-value-format
389 systems 'guix-hydra-build-system
390 'action (lambda (btn)
391 (let ((args (guix-hydra-build-latest-prompt-args
392 :job (button-get btn 'job-name)
393 :system (button-label btn))))
394 (apply #'guix-hydra-build-get-display
395 'latest args)))
b8fa5a2a 396 'job-name (guix-hydra-job-name-specification
0a5ec709
AK
397 (guix-entry-value entry 'name)
398 (guix-entry-value entry 'version))))
399
c80ce104
AK
400(defmacro guix-package-info-define-insert-inputs (&optional type)
401 "Define a face and a function for inserting package inputs.
402TYPE is a type of inputs.
403Function name is `guix-package-info-insert-TYPE-inputs'.
404Face name is `guix-package-info-TYPE-inputs'."
405 (let* ((type-str (symbol-name type))
406 (type-name (and type (concat type-str "-")))
407 (type-desc (and type (concat type-str " ")))
408 (face (intern (concat "guix-package-info-" type-name "inputs")))
409 (btn (intern (concat "guix-package-" type-name "input"))))
410 `(progn
411 (defface ,face
412 '((t :inherit guix-package-info-name-button))
413 ,(concat "Face used for " type-desc "inputs of a package.")
414 :group 'guix-package-info-faces)
415
416 (define-button-type ',btn
417 :supertype 'guix-package-name
418 'face ',face))))
419
420(guix-package-info-define-insert-inputs)
421(guix-package-info-define-insert-inputs native)
422(guix-package-info-define-insert-inputs propagated)
423
424(defun guix-package-info-insert-outputs (outputs entry)
425 "Insert OUTPUTS from package ENTRY at point."
426 (and (guix-entry-value entry 'obsolete)
427 (guix-package-info-insert-obsolete-text))
428 (and (guix-entry-value entry 'non-unique)
429 (guix-entry-value entry 'installed)
430 (guix-package-info-insert-non-unique-text
431 (guix-package-entry->name-specification entry)))
432 (insert "\n")
433 (dolist (output outputs)
434 (guix-package-info-insert-output output entry)))
435
436(defun guix-package-info-insert-obsolete-text ()
437 "Insert a message about obsolete package at point."
438 (guix-info-insert-indent)
439 (guix-format-insert guix-package-info-obsolete-string
440 'guix-package-info-obsolete))
441
442(defun guix-package-info-insert-non-unique-text (full-name)
443 "Insert a message about non-unique package with FULL-NAME at point."
444 (insert "\n")
445 (guix-info-insert-indent)
446 (insert "Installed outputs are displayed for a non-unique ")
447 (guix-insert-button full-name 'guix-package-name)
448 (insert " package."))
449
450(defun guix-package-info-insert-output (output entry)
451 "Insert OUTPUT at point.
452Make some fancy text with buttons and additional stuff if the
453current OUTPUT is installed (if there is such output in
454`installed' parameter of a package ENTRY)."
455 (let* ((installed (guix-entry-value entry 'installed))
456 (obsolete (guix-entry-value entry 'obsolete))
457 (installed-entry (cl-find-if
458 (lambda (entry)
459 (string= (guix-entry-value entry 'output)
460 output))
461 installed))
260795b7
AK
462 (action-type (if installed-entry 'delete 'install))
463 (profile (guix-ui-current-profile)))
c80ce104
AK
464 (guix-info-insert-indent)
465 (guix-format-insert output
466 (if installed-entry
467 'guix-package-info-installed-outputs
468 'guix-package-info-uninstalled-outputs)
469 guix-package-info-output-format)
260795b7
AK
470 ;; Do not allow a user to install/delete anything to/from a system
471 ;; profile, so add action buttons only for non-system profiles.
472 (when (and profile
473 (not (guix-system-profile? profile)))
474 (guix-package-info-insert-action-button action-type entry output)
475 (when obsolete
476 (guix-info-insert-indent)
477 (guix-package-info-insert-action-button 'upgrade entry output)))
c80ce104
AK
478 (insert "\n")
479 (when installed-entry
480 (guix-info-insert-entry installed-entry 'installed-output 2))))
481
482(defun guix-package-info-insert-action-button (type entry output)
483 "Insert button to process an action on a package OUTPUT at point.
484TYPE is one of the following symbols: `install', `delete', `upgrade'.
485ENTRY is an alist with package info."
486 (let ((type-str (capitalize (symbol-name type)))
487 (full-name (guix-package-entry->name-specification entry output)))
488 (guix-info-insert-action-button
489 type-str
490 (lambda (btn)
491 (guix-process-package-actions
492 (guix-ui-current-profile)
493 `((,(button-get btn 'action-type) (,(button-get btn 'id)
494 ,(button-get btn 'output))))
495 (current-buffer)))
496 (concat type-str " '" full-name "'")
497 'action-type type
498 'id (or (guix-entry-value entry 'package-id)
499 (guix-entry-id entry))
500 'output output)))
501
502(defun guix-package-info-show-source (entry-id package-id)
503 "Show file name of a package source in the current info buffer.
504Find the file if needed (see `guix-package-info-auto-find-source').
505ENTRY-ID is an ID of the current entry (package or output).
506PACKAGE-ID is an ID of the package which source to show."
507 (let* ((entries (guix-buffer-current-entries))
508 (entry (guix-entry-by-id entry-id entries))
509 (file (guix-package-source-path package-id)))
510 (or file
511 (error "Couldn't define file name of the package source"))
512 (let* ((new-entry (cons (cons 'source-file file)
513 entry))
514 (new-entries (guix-replace-entry entry-id new-entry entries)))
515 (setf (guix-buffer-item-entries guix-buffer-item)
516 new-entries)
517 (guix-buffer-redisplay-goto-button)
518 (if (file-exists-p file)
519 (if guix-package-info-auto-find-source
520 (guix-find-file file)
521 (message "The source store path is displayed."))
522 (if guix-package-info-auto-download-source
523 (guix-package-info-download-source package-id)
524 (message "The source does not exist in the store."))))))
525
526(defun guix-package-info-download-source (package-id)
527 "Download a source of the package PACKAGE-ID."
528 (setq guix-package-info-download-buffer (current-buffer))
529 (guix-package-source-build-derivation
530 package-id
531 "The source does not exist in the store. Download it?"))
532
533(defun guix-package-info-insert-source (source entry)
534 "Insert SOURCE from package ENTRY at point.
535SOURCE is a list of URLs."
536 (if (null source)
537 (guix-format-insert nil)
538 (let* ((source-file (guix-entry-value entry 'source-file))
539 (entry-id (guix-entry-id entry))
540 (package-id (or (guix-entry-value entry 'package-id)
541 entry-id)))
542 (if (null source-file)
543 (guix-info-insert-action-button
544 "Show"
545 (lambda (btn)
546 (guix-package-info-show-source (button-get btn 'entry-id)
547 (button-get btn 'package-id)))
548 "Show the source store directory of the current package"
549 'entry-id entry-id
550 'package-id package-id)
551 (unless (file-exists-p source-file)
552 (guix-info-insert-action-button
553 "Download"
554 (lambda (btn)
555 (guix-package-info-download-source
556 (button-get btn 'package-id)))
557 "Download the source into the store"
558 'package-id package-id))
559 (guix-info-insert-value-indent source-file 'guix-file))
560 (guix-info-insert-value-indent source 'guix-package-source))))
561
562(defun guix-package-info-redisplay-after-download ()
563 "Redisplay an 'info' buffer after downloading the package source.
564This function is used to hide a \"Download\" button if needed."
565 (when (buffer-live-p guix-package-info-download-buffer)
566 (with-current-buffer guix-package-info-download-buffer
567 (guix-buffer-redisplay-goto-button))
568 (setq guix-package-info-download-buffer nil)))
569
570(add-hook 'guix-after-source-download-hook
571 'guix-package-info-redisplay-after-download)
572
573\f
574;;; Package 'list'
575
576(guix-ui-list-define-interface package
577 :buffer-name "*Guix Package List*"
578 :format '((name guix-package-list-get-name 20 t)
579 (version nil 10 nil)
580 (outputs nil 13 t)
581 (installed guix-package-list-get-installed-outputs 13 t)
582 (synopsis guix-list-get-one-line 30 nil))
583 :sort-key '(name)
584 :marks '((install . ?I)
585 (upgrade . ?U)
586 (delete . ?D)))
587
588(let ((map guix-package-list-mode-map))
5c8994d9 589 (define-key map (kbd "B") 'guix-package-list-latest-builds)
c80ce104
AK
590 (define-key map (kbd "e") 'guix-package-list-edit)
591 (define-key map (kbd "x") 'guix-package-list-execute)
592 (define-key map (kbd "i") 'guix-package-list-mark-install)
593 (define-key map (kbd "d") 'guix-package-list-mark-delete)
594 (define-key map (kbd "U") 'guix-package-list-mark-upgrade)
595 (define-key map (kbd "^") 'guix-package-list-mark-upgrades))
596
597(defface guix-package-list-installed
598 '((t :inherit guix-package-info-installed-outputs))
599 "Face used if there are installed outputs for the current package."
600 :group 'guix-package-list-faces)
601
602(defface guix-package-list-obsolete
603 '((t :inherit guix-package-info-obsolete))
604 "Face used if a package is obsolete."
605 :group 'guix-package-list-faces)
606
607(defcustom guix-package-list-generation-marking-enabled nil
608 "If non-nil, allow putting marks in a list with 'generation packages'.
609
610By default this is disabled, because it may be confusing. For
611example, a package is installed in some generation, so a user can
612mark it for deletion in the list of packages from this
613generation, but the package may not be installed in the latest
614generation, so actually it cannot be deleted.
615
616If you managed to understand the explanation above or if you
617really know what you do or if you just don't care, you can set
618this variable to t. It should not do much harm anyway (most
619likely)."
620 :type 'boolean
621 :group 'guix-package-list)
622
623(defun guix-package-list-get-name (name entry)
624 "Return NAME of the package ENTRY.
625Colorize it with `guix-package-list-installed' or
626`guix-package-list-obsolete' if needed."
627 (guix-get-string name
628 (cond ((guix-entry-value entry 'obsolete)
629 'guix-package-list-obsolete)
630 ((guix-entry-value entry 'installed)
631 'guix-package-list-installed))))
632
633(defun guix-package-list-get-installed-outputs (installed &optional _)
634 "Return string with outputs from INSTALLED entries."
635 (guix-get-string
636 (mapcar (lambda (entry)
637 (guix-entry-value entry 'output))
638 installed)))
639
640(defun guix-package-list-marking-check ()
641 "Signal an error if marking is disabled for the current buffer."
642 (when (and (not guix-package-list-generation-marking-enabled)
643 (or (derived-mode-p 'guix-package-list-mode)
644 (derived-mode-p 'guix-output-list-mode))
645 (eq (guix-ui-current-search-type) 'generation))
646 (error "Action marks are disabled for lists of 'generation packages'")))
647
648(defun guix-package-list-mark-outputs (mark default
649 &optional prompt available)
650 "Mark the current package with MARK and move to the next line.
651If PROMPT is non-nil, use it to ask a user for outputs from
652AVAILABLE list, otherwise mark all DEFAULT outputs."
653 (let ((outputs (if prompt
654 (guix-completing-read-multiple
655 prompt available nil t)
656 default)))
657 (apply #'guix-list--mark mark t outputs)))
658
659(defun guix-package-list-mark-install (&optional arg)
660 "Mark the current package for installation and move to the next line.
661With ARG, prompt for the outputs to install (several outputs may
662be separated with \",\")."
663 (interactive "P")
664 (guix-package-list-marking-check)
665 (let* ((entry (guix-list-current-entry))
666 (all (guix-entry-value entry 'outputs))
667 (installed (guix-package-installed-outputs entry))
668 (available (cl-set-difference all installed :test #'string=)))
669 (or available
670 (user-error "This package is already installed"))
671 (guix-package-list-mark-outputs
672 'install '("out")
673 (and arg "Output(s) to install: ")
674 available)))
675
676(defun guix-package-list-mark-delete (&optional arg)
677 "Mark the current package for deletion and move to the next line.
678With ARG, prompt for the outputs to delete (several outputs may
679be separated with \",\")."
680 (interactive "P")
681 (guix-package-list-marking-check)
682 (let* ((entry (guix-list-current-entry))
683 (installed (guix-package-installed-outputs entry)))
684 (or installed
685 (user-error "This package is not installed"))
686 (guix-package-list-mark-outputs
687 'delete installed
688 (and arg "Output(s) to delete: ")
689 installed)))
690
691(defun guix-package-list-mark-upgrade (&optional arg)
692 "Mark the current package for upgrading and move to the next line.
693With ARG, prompt for the outputs to upgrade (several outputs may
694be separated with \",\")."
695 (interactive "P")
696 (guix-package-list-marking-check)
697 (let* ((entry (guix-list-current-entry))
698 (installed (guix-package-installed-outputs entry)))
699 (or installed
700 (user-error "This package is not installed"))
701 (when (or (guix-entry-value entry 'obsolete)
702 (y-or-n-p "This package is not obsolete. Try to upgrade it anyway? "))
703 (guix-package-list-mark-outputs
704 'upgrade installed
705 (and arg "Output(s) to upgrade: ")
706 installed))))
707
708(defun guix-package-mark-upgrades (fun)
709 "Mark all obsolete packages for upgrading.
710Use FUN to perform marking of the current line. FUN should
711take an entry as argument."
712 (guix-package-list-marking-check)
713 (let ((obsolete (cl-remove-if-not
714 (lambda (entry)
715 (guix-entry-value entry 'obsolete))
716 (guix-buffer-current-entries))))
717 (guix-list-for-each-line
718 (lambda ()
719 (let* ((id (guix-list-current-id))
720 (entry (cl-find-if
721 (lambda (entry)
722 (equal id (guix-entry-id entry)))
723 obsolete)))
724 (when entry
725 (funcall fun entry)))))))
726
727(defun guix-package-list-mark-upgrades ()
728 "Mark all obsolete packages for upgrading."
729 (interactive)
730 (guix-package-mark-upgrades
731 (lambda (entry)
732 (apply #'guix-list--mark
733 'upgrade nil
734 (guix-package-installed-outputs entry)))))
735
260795b7
AK
736(defun guix-package-assert-non-system-profile ()
737 "Verify that the current profile is not a system one.
738The current profile is the one used by the current buffer."
739 (let ((profile (guix-ui-current-profile)))
740 (and profile
741 (guix-system-profile? profile)
742 (user-error "Packages cannot be installed or removed to/from \
743profile '%s'.
744Use 'guix system reconfigure' shell command to modify a system profile."
745 profile))))
746
c80ce104
AK
747(defun guix-package-execute-actions (fun)
748 "Perform actions on the marked packages.
749Use FUN to define actions suitable for `guix-process-package-actions'.
750FUN should take action-type as argument."
260795b7 751 (guix-package-assert-non-system-profile)
c80ce104
AK
752 (let ((actions (delq nil
753 (mapcar fun '(install delete upgrade)))))
754 (if actions
755 (guix-process-package-actions (guix-ui-current-profile)
756 actions (current-buffer))
757 (user-error "No operations specified"))))
758
759(defun guix-package-list-execute ()
760 "Perform actions on the marked packages."
761 (interactive)
762 (guix-package-execute-actions #'guix-package-list-make-action))
763
764(defun guix-package-list-make-action (action-type)
765 "Return action specification for the packages marked with ACTION-TYPE.
766Return nil, if there are no packages marked with ACTION-TYPE.
767The specification is suitable for `guix-process-package-actions'."
768 (let ((specs (guix-list-get-marked-args action-type)))
769 (and specs (cons action-type specs))))
770
2c04e2ee
AK
771(defun guix-package-list-edit (&optional directory)
772 "Go to the location of the current package.
773See `guix-find-location' for the meaning of DIRECTORY."
774 (interactive (list (guix-read-directory)))
775 (guix-edit (guix-list-current-id) directory))
c80ce104 776
5c8994d9
AK
777(defun guix-package-list-latest-builds (number &rest args)
778 "Display latest NUMBER of Hydra builds of the current package.
779Interactively, prompt for NUMBER. With prefix argument, prompt
780for all ARGS."
781 (interactive
782 (let ((entry (guix-list-current-entry)))
783 (guix-hydra-build-latest-prompt-args
b8fa5a2a 784 :job (guix-hydra-job-name-specification
5c8994d9
AK
785 (guix-entry-value entry 'name)
786 (guix-entry-value entry 'version)))))
787 (apply #'guix-hydra-latest-builds number args))
788
c80ce104
AK
789\f
790;;; Output 'info'
791
792(guix-ui-info-define-interface output
793 :buffer-name "*Guix Package Info*"
794 :format '((name format (format guix-package-info-name))
795 (version format guix-output-info-insert-version)
796 (output format guix-output-info-insert-output)
797 (synopsis simple (indent guix-package-info-synopsis))
798 (source simple guix-package-info-insert-source)
799 (path simple (indent guix-file))
800 (dependencies simple (indent guix-file))
801 (location format (format guix-package-location))
802 (home-url format (format guix-url))
cefb7aea 803 (license format (format guix-package-license))
0a5ec709 804 (systems format guix-package-info-insert-systems)
c80ce104
AK
805 (inputs format (format guix-package-input))
806 (native-inputs format (format guix-package-native-input))
807 (propagated-inputs format
808 (format guix-package-propagated-input))
809 (description simple (indent guix-package-info-description)))
810 :titles guix-package-info-titles
811 :required '(id package-id installed non-unique))
812
813(defun guix-output-info-insert-version (version entry)
814 "Insert output VERSION and obsolete text if needed at point."
815 (guix-info-insert-value-format version
816 'guix-package-info-version)
817 (and (guix-entry-value entry 'obsolete)
818 (guix-package-info-insert-obsolete-text)))
819
820(defun guix-output-info-insert-output (output entry)
821 "Insert OUTPUT and action buttons at point."
822 (let* ((installed (guix-entry-value entry 'installed))
823 (obsolete (guix-entry-value entry 'obsolete))
824 (action-type (if installed 'delete 'install)))
825 (guix-info-insert-value-format
826 output
827 (if installed
828 'guix-package-info-installed-outputs
829 'guix-package-info-uninstalled-outputs))
830 (guix-info-insert-indent)
831 (guix-package-info-insert-action-button action-type entry output)
832 (when obsolete
833 (guix-info-insert-indent)
834 (guix-package-info-insert-action-button 'upgrade entry output))))
835
836\f
837;;; Output 'list'
838
839(guix-ui-list-define-interface output
840 :buffer-name "*Guix Package List*"
841 :describe-function 'guix-output-list-describe
842 :format '((name guix-package-list-get-name 20 t)
843 (version nil 10 nil)
844 (output nil 9 t)
845 (installed nil 12 t)
846 (synopsis guix-list-get-one-line 30 nil))
847 :required '(id package-id)
848 :sort-key '(name)
849 :marks '((install . ?I)
850 (upgrade . ?U)
851 (delete . ?D)))
852
853(let ((map guix-output-list-mode-map))
5c8994d9 854 (define-key map (kbd "B") 'guix-package-list-latest-builds)
c80ce104
AK
855 (define-key map (kbd "e") 'guix-output-list-edit)
856 (define-key map (kbd "x") 'guix-output-list-execute)
857 (define-key map (kbd "i") 'guix-output-list-mark-install)
858 (define-key map (kbd "d") 'guix-output-list-mark-delete)
859 (define-key map (kbd "U") 'guix-output-list-mark-upgrade)
860 (define-key map (kbd "^") 'guix-output-list-mark-upgrades))
861
862(defun guix-output-list-mark-install ()
863 "Mark the current output for installation and move to the next line."
864 (interactive)
865 (guix-package-list-marking-check)
866 (let* ((entry (guix-list-current-entry))
867 (installed (guix-entry-value entry 'installed)))
868 (if installed
869 (user-error "This output is already installed")
870 (guix-list--mark 'install t))))
871
872(defun guix-output-list-mark-delete ()
873 "Mark the current output for deletion and move to the next line."
874 (interactive)
875 (guix-package-list-marking-check)
876 (let* ((entry (guix-list-current-entry))
877 (installed (guix-entry-value entry 'installed)))
878 (if installed
879 (guix-list--mark 'delete t)
880 (user-error "This output is not installed"))))
881
882(defun guix-output-list-mark-upgrade ()
883 "Mark the current output for upgrading and move to the next line."
884 (interactive)
885 (guix-package-list-marking-check)
886 (let* ((entry (guix-list-current-entry))
887 (installed (guix-entry-value entry 'installed)))
888 (or installed
889 (user-error "This output is not installed"))
890 (when (or (guix-entry-value entry 'obsolete)
891 (y-or-n-p "This output is not obsolete. Try to upgrade it anyway? "))
892 (guix-list--mark 'upgrade t))))
893
894(defun guix-output-list-mark-upgrades ()
895 "Mark all obsolete package outputs for upgrading."
896 (interactive)
897 (guix-package-mark-upgrades
898 (lambda (_) (guix-list--mark 'upgrade))))
899
900(defun guix-output-list-execute ()
901 "Perform actions on the marked outputs."
902 (interactive)
903 (guix-package-execute-actions #'guix-output-list-make-action))
904
905(defun guix-output-list-make-action (action-type)
906 "Return action specification for the outputs marked with ACTION-TYPE.
907Return nil, if there are no outputs marked with ACTION-TYPE.
908The specification is suitable for `guix-process-output-actions'."
909 (let ((ids (guix-list-get-marked-id-list action-type)))
910 (and ids (cons action-type
911 (mapcar #'guix-package-id-and-output-by-output-id
912 ids)))))
913
914(defun guix-output-list-describe (ids)
915 "Describe outputs with IDS (list of output identifiers).
916See `guix-package-info-type'."
917 (if (eq guix-package-info-type 'output)
918 (guix-buffer-get-display-entries
919 'info 'output
920 (cl-list* (guix-ui-current-profile) 'id ids)
921 'add)
922 (let ((pids (mapcar (lambda (oid)
923 (car (guix-package-id-and-output-by-output-id
924 oid)))
925 ids)))
926 (guix-buffer-get-display-entries
927 'info 'package
928 (cl-list* (guix-ui-current-profile)
929 'id (cl-remove-duplicates pids))
930 'add))))
931
2c04e2ee
AK
932(defun guix-output-list-edit (&optional directory)
933 "Go to the location of the current package.
934See `guix-find-location' for the meaning of DIRECTORY."
935 (interactive (list (guix-read-directory)))
c80ce104 936 (guix-edit (guix-entry-value (guix-list-current-entry)
2c04e2ee
AK
937 'package-id)
938 directory))
c80ce104
AK
939
940\f
941;;; Interactive commands
942
943(defvar guix-package-search-params '(name synopsis description)
944 "Default list of package parameters for searching by regexp.")
945
946(defvar guix-package-search-history nil
947 "A history of minibuffer prompts.")
948
949;;;###autoload
e119ba90
AK
950(defun guix-packages-by-name (name &optional profile)
951 "Display Guix packages with NAME.
c80ce104 952NAME is a string with name specification. It may optionally contain
db0c709b 953a version number. Examples: \"guile\", \"guile@2.0.11\".
c80ce104
AK
954
955If PROFILE is nil, use `guix-current-profile'.
956Interactively with prefix, prompt for PROFILE."
957 (interactive
e119ba90 958 (list (guix-read-package-name)
494a62f2 959 (guix-ui-read-profile)))
c80ce104
AK
960 (guix-package-get-display profile 'name name))
961
83aab70b
AK
962;;;###autoload
963(defun guix-packages-by-license (license &optional profile)
964 "Display Guix packages with LICENSE.
965LICENSE is a license name string.
966If PROFILE is nil, use `guix-current-profile'.
967Interactively with prefix, prompt for PROFILE."
968 (interactive
969 (list (guix-read-license-name)
970 (guix-ui-read-profile)))
971 (guix-package-get-display profile 'license license))
972
b4ea535a
AK
973;;;###autoload
974(defun guix-packages-by-location (location &optional profile)
975 "Display Guix packages placed in LOCATION file.
976If PROFILE is nil, use `guix-current-profile'.
977Interactively with prefix, prompt for PROFILE."
978 (interactive
979 (list (guix-read-package-location)
980 (guix-ui-read-profile)))
981 (guix-package-get-display profile 'location location))
982
c80ce104
AK
983;;;###autoload
984(defun guix-search-by-regexp (regexp &optional params profile)
985 "Search for Guix packages by REGEXP.
986PARAMS are package parameters that should be searched.
987If PARAMS are not specified, use `guix-package-search-params'.
988
989If PROFILE is nil, use `guix-current-profile'.
990Interactively with prefix, prompt for PROFILE."
991 (interactive
992 (list (read-regexp "Regexp: " nil 'guix-package-search-history)
494a62f2 993 nil (guix-ui-read-profile)))
c80ce104
AK
994 (guix-package-get-display profile 'regexp regexp
995 (or params guix-package-search-params)))
996
27a2e483
AK
997;;;###autoload
998(defun guix-search-by-name (regexp &optional profile)
999 "Search for Guix packages matching REGEXP in a package name.
1000If PROFILE is nil, use `guix-current-profile'.
1001Interactively with prefix, prompt for PROFILE."
1002 (interactive
1003 (list (read-string "Package name by regexp: "
1004 nil 'guix-package-search-history)
1005 (guix-ui-read-profile)))
1006 (guix-search-by-regexp regexp '(name) profile))
1007
c80ce104
AK
1008;;;###autoload
1009(defun guix-installed-packages (&optional profile)
1010 "Display information about installed Guix packages.
1011If PROFILE is nil, use `guix-current-profile'.
1012Interactively with prefix, prompt for PROFILE."
494a62f2 1013 (interactive (list (guix-ui-read-profile)))
c80ce104
AK
1014 (guix-package-get-display profile 'installed))
1015
1016;;;###autoload
cfb1c62a
AK
1017(defun guix-installed-user-packages ()
1018 "Display information about Guix packages installed in a user profile."
1019 (interactive)
1020 (guix-installed-packages guix-user-profile))
1021
1022;;;###autoload
1023(defun guix-installed-system-packages ()
1024 "Display information about Guix packages installed in a system profile."
1025 (interactive)
1026 (guix-installed-packages
1027 (guix-packages-profile guix-system-profile nil t)))
1028
1029;;;###autoload
c80ce104
AK
1030(defun guix-obsolete-packages (&optional profile)
1031 "Display information about obsolete Guix packages.
1032If PROFILE is nil, use `guix-current-profile'.
1033Interactively with prefix, prompt for PROFILE."
494a62f2 1034 (interactive (list (guix-ui-read-profile)))
c80ce104
AK
1035 (guix-package-get-display profile 'obsolete))
1036
1037;;;###autoload
1038(defun guix-all-available-packages (&optional profile)
1039 "Display information about all available Guix packages.
1040If PROFILE is nil, use `guix-current-profile'.
1041Interactively with prefix, prompt for PROFILE."
494a62f2 1042 (interactive (list (guix-ui-read-profile)))
c80ce104
AK
1043 (guix-package-get-display profile 'all-available))
1044
1045;;;###autoload
1046(defun guix-newest-available-packages (&optional profile)
1047 "Display information about the newest available Guix packages.
1048If PROFILE is nil, use `guix-current-profile'.
1049Interactively with prefix, prompt for PROFILE."
494a62f2 1050 (interactive (list (guix-ui-read-profile)))
c80ce104
AK
1051 (guix-package-get-display profile 'newest-available))
1052
1053(provide 'guix-ui-package)
1054
1055;;; guix-ui-package.el ends here