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