Merge branch 'master' into core-updates
[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, 2016 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-location)
42 (require 'guix-profiles)
43
44 (guix-ui-define-entry-type package)
45 (guix-ui-define-entry-type output)
46
47 (defcustom guix-package-list-type 'output
48 "Define how to display packages in 'list' buffer.
49 Should be a symbol `package' or `output' (if `output', display each
50 output on a separate line; if `package', display each package on
51 a 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.
58 Should be a symbol `package' or `output' (if `output', display
59 each output separately; if `package', display outputs inside
60 package data)."
61 :type '(choice (const :tag "Display packages" package)
62 (const :tag "Display outputs" output))
63 :group 'guix-package)
64
65 (defun guix-package-get-display (profile search-type &rest search-values)
66 "Search for packages/outputs and show results.
67
68 If PROFILE is nil, use `guix-current-profile'.
69
70 See `guix-ui-get-entries' for the meaning of SEARCH-TYPE and
71 SEARCH-VALUES.
72
73 Results are displayed in the list buffer, unless a single package
74 is 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.
120 Each action is a list of the form:
121
122 (ACTION-TYPE PACKAGE-SPEC ...)
123
124 ACTION-TYPE is one of the following symbols: `install',
125 `upgrade', `remove'/`delete'.
126 PACKAGE-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.
150 Ask a user if needed (see `guix-operation-confirm').
151 INSTALL, UPGRADE, REMOVE are 'package action specifications'.
152 See `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.
181 If 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.
186 See `guix-process-package-actions' for the meaning of SPECS.
187 ENTRIES 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 simple guix-package-info-insert-location)
227 (home-url format (format guix-url))
228 (license format (format guix-package-license))
229 (systems format guix-package-info-insert-systems)
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)))
234 :titles '((home-url . "Home page")
235 (systems . "Supported systems"))
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.
314 If 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.
320 After pressing a \"Show\" button, a derivation of the package
321 source is calculated and a store file path is displayed. If this
322 variable is non-nil and the source file does not exist in the
323 store, it will be automatically downloaded (with a possible
324 prompt 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.
333 It should be a '%s'-sequence. After inserting an output name
334 formatted 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
346 (define-button-type 'guix-package-license
347 :supertype 'guix
348 'face 'guix-package-info-license
349 'help-echo "Display license info"
350 'action (lambda (btn)
351 (require 'guix-ui-license)
352 (guix-buffer-get-display-entries
353 'info 'license
354 (list 'name (button-label btn))
355 'add)))
356
357 (define-button-type 'guix-package-name
358 :supertype 'guix
359 'face 'guix-package-info-name-button
360 'help-echo "Describe this package"
361 'action (lambda (btn)
362 (guix-buffer-get-display-entries-current
363 'info guix-package-info-type
364 (list (guix-ui-current-profile)
365 'name (or (button-get btn 'spec)
366 (button-label btn)))
367 'add)))
368
369 (define-button-type 'guix-package-heading
370 :supertype 'guix-package-name
371 'face 'guix-package-info-heading)
372
373 (define-button-type 'guix-package-source
374 :supertype 'guix
375 'face 'guix-package-info-source
376 'help-echo ""
377 'action (lambda (_)
378 ;; As a source may not be a real URL (e.g., "mirror://..."),
379 ;; no action is bound to a source button.
380 (message "Yes, this is the source URL. What did you expect?")))
381
382 (defun guix-package-info-insert-heading (entry)
383 "Insert package ENTRY heading (name and version) at point."
384 (guix-insert-button
385 (concat (guix-entry-value entry 'name) " "
386 (guix-entry-value entry 'version))
387 'guix-package-heading
388 'spec (guix-package-entry->name-specification entry)))
389
390 (defun guix-package-info-insert-location (location &optional _)
391 "Insert package LOCATION at point."
392 (if (null location)
393 (guix-format-insert nil)
394 (let ((location-file (car (split-string location ":"))))
395 (guix-info-insert-value-indent location 'guix-package-location)
396 (guix-info-insert-indent)
397 (guix-info-insert-action-button
398 "Packages"
399 (lambda (btn)
400 (guix-package-get-display (guix-ui-current-profile)
401 'location
402 (button-get btn 'location)))
403 (format "Display packages from location '%s'" location-file)
404 'location location-file))))
405
406 (defun guix-package-info-insert-systems (systems entry)
407 "Insert supported package SYSTEMS at point."
408 (guix-info-insert-value-format
409 systems 'guix-hydra-build-system
410 'action (lambda (btn)
411 (let ((args (guix-hydra-build-latest-prompt-args
412 :job (button-get btn 'job-name)
413 :system (button-label btn))))
414 (apply #'guix-hydra-build-get-display
415 'latest args)))
416 'job-name (guix-hydra-job-name-specification
417 (guix-entry-value entry 'name)
418 (guix-entry-value entry 'version))))
419
420 (defmacro guix-package-info-define-insert-inputs (&optional type)
421 "Define a face and a function for inserting package inputs.
422 TYPE is a type of inputs.
423 Function name is `guix-package-info-insert-TYPE-inputs'.
424 Face name is `guix-package-info-TYPE-inputs'."
425 (let* ((type-str (symbol-name type))
426 (type-name (and type (concat type-str "-")))
427 (type-desc (and type (concat type-str " ")))
428 (face (intern (concat "guix-package-info-" type-name "inputs")))
429 (btn (intern (concat "guix-package-" type-name "input"))))
430 `(progn
431 (defface ,face
432 '((t :inherit guix-package-info-name-button))
433 ,(concat "Face used for " type-desc "inputs of a package.")
434 :group 'guix-package-info-faces)
435
436 (define-button-type ',btn
437 :supertype 'guix-package-name
438 'face ',face))))
439
440 (guix-package-info-define-insert-inputs)
441 (guix-package-info-define-insert-inputs native)
442 (guix-package-info-define-insert-inputs propagated)
443
444 (defun guix-package-info-insert-outputs (outputs entry)
445 "Insert OUTPUTS from package ENTRY at point."
446 (and (guix-entry-value entry 'obsolete)
447 (guix-package-info-insert-obsolete-text))
448 (and (guix-entry-value entry 'non-unique)
449 (guix-entry-value entry 'installed)
450 (guix-package-info-insert-non-unique-text
451 (guix-package-entry->name-specification entry)))
452 (insert "\n")
453 (dolist (output outputs)
454 (guix-package-info-insert-output output entry)))
455
456 (defun guix-package-info-insert-obsolete-text ()
457 "Insert a message about obsolete package at point."
458 (guix-info-insert-indent)
459 (guix-format-insert guix-package-info-obsolete-string
460 'guix-package-info-obsolete))
461
462 (defun guix-package-info-insert-non-unique-text (full-name)
463 "Insert a message about non-unique package with FULL-NAME at point."
464 (insert "\n")
465 (guix-info-insert-indent)
466 (insert "Installed outputs are displayed for a non-unique ")
467 (guix-insert-button full-name 'guix-package-name)
468 (insert " package."))
469
470 (defun guix-package-info-insert-output (output entry)
471 "Insert OUTPUT at point.
472 Make some fancy text with buttons and additional stuff if the
473 current OUTPUT is installed (if there is such output in
474 `installed' parameter of a package ENTRY)."
475 (let* ((installed (guix-entry-value entry 'installed))
476 (obsolete (guix-entry-value entry 'obsolete))
477 (installed-entry (cl-find-if
478 (lambda (entry)
479 (string= (guix-entry-value entry 'output)
480 output))
481 installed))
482 (action-type (if installed-entry 'delete 'install))
483 (profile (guix-ui-current-profile)))
484 (guix-info-insert-indent)
485 (guix-format-insert output
486 (if installed-entry
487 'guix-package-info-installed-outputs
488 'guix-package-info-uninstalled-outputs)
489 guix-package-info-output-format)
490 ;; Do not allow a user to install/delete anything to/from a system
491 ;; profile, so add action buttons only for non-system profiles.
492 (when (and profile
493 (not (guix-system-profile? profile)))
494 (guix-package-info-insert-action-button action-type entry output)
495 (when obsolete
496 (guix-info-insert-indent)
497 (guix-package-info-insert-action-button 'upgrade entry output)))
498 (insert "\n")
499 (when installed-entry
500 (guix-info-insert-entry installed-entry 'installed-output 2))))
501
502 (defun guix-package-info-insert-action-button (type entry output)
503 "Insert button to process an action on a package OUTPUT at point.
504 TYPE is one of the following symbols: `install', `delete', `upgrade'.
505 ENTRY is an alist with package info."
506 (let ((type-str (capitalize (symbol-name type)))
507 (full-name (guix-package-entry->name-specification entry output)))
508 (guix-info-insert-action-button
509 type-str
510 (lambda (btn)
511 (guix-process-package-actions
512 (guix-ui-current-profile)
513 `((,(button-get btn 'action-type) (,(button-get btn 'id)
514 ,(button-get btn 'output))))
515 (current-buffer)))
516 (concat type-str " '" full-name "'")
517 'action-type type
518 'id (or (guix-entry-value entry 'package-id)
519 (guix-entry-id entry))
520 'output output)))
521
522 (defun guix-package-info-show-source (entry-id package-id)
523 "Show file name of a package source in the current info buffer.
524 Find the file if needed (see `guix-package-info-auto-find-source').
525 ENTRY-ID is an ID of the current entry (package or output).
526 PACKAGE-ID is an ID of the package which source to show."
527 (let* ((entries (guix-buffer-current-entries))
528 (entry (guix-entry-by-id entry-id entries))
529 (file (guix-package-source-path package-id)))
530 (or file
531 (error "Couldn't define file name of the package source"))
532 (let* ((new-entry (cons (cons 'source-file file)
533 entry))
534 (new-entries (guix-replace-entry entry-id new-entry entries)))
535 (setf (guix-buffer-item-entries guix-buffer-item)
536 new-entries)
537 (guix-buffer-redisplay-goto-button)
538 (if (file-exists-p file)
539 (if guix-package-info-auto-find-source
540 (guix-find-file file)
541 (message "The source store path is displayed."))
542 (if guix-package-info-auto-download-source
543 (guix-package-info-download-source package-id)
544 (message "The source does not exist in the store."))))))
545
546 (defun guix-package-info-download-source (package-id)
547 "Download a source of the package PACKAGE-ID."
548 (setq guix-package-info-download-buffer (current-buffer))
549 (guix-package-source-build-derivation
550 package-id
551 "The source does not exist in the store. Download it?"))
552
553 (defun guix-package-info-insert-source (source entry)
554 "Insert SOURCE from package ENTRY at point.
555 SOURCE is a list of URLs."
556 (if (null source)
557 (guix-format-insert nil)
558 (let* ((source-file (guix-entry-value entry 'source-file))
559 (entry-id (guix-entry-id entry))
560 (package-id (or (guix-entry-value entry 'package-id)
561 entry-id)))
562 (if (null source-file)
563 (guix-info-insert-action-button
564 "Show"
565 (lambda (btn)
566 (guix-package-info-show-source (button-get btn 'entry-id)
567 (button-get btn 'package-id)))
568 "Show the source store directory of the current package"
569 'entry-id entry-id
570 'package-id package-id)
571 (unless (file-exists-p source-file)
572 (guix-info-insert-action-button
573 "Download"
574 (lambda (btn)
575 (guix-package-info-download-source
576 (button-get btn 'package-id)))
577 "Download the source into the store"
578 'package-id package-id))
579 (guix-info-insert-value-indent source-file 'guix-file))
580 (guix-info-insert-value-indent source 'guix-package-source))))
581
582 (defun guix-package-info-redisplay-after-download ()
583 "Redisplay an 'info' buffer after downloading the package source.
584 This function is used to hide a \"Download\" button if needed."
585 (when (buffer-live-p guix-package-info-download-buffer)
586 (with-current-buffer guix-package-info-download-buffer
587 (guix-buffer-redisplay-goto-button))
588 (setq guix-package-info-download-buffer nil)))
589
590 (add-hook 'guix-after-source-download-hook
591 'guix-package-info-redisplay-after-download)
592
593 \f
594 ;;; Package 'list'
595
596 (guix-ui-list-define-interface package
597 :buffer-name "*Guix Package List*"
598 :format '((name guix-package-list-get-name 20 t)
599 (version nil 10 nil)
600 (outputs nil 13 t)
601 (installed guix-package-list-get-installed-outputs 13 t)
602 (synopsis guix-list-get-one-line 30 nil))
603 :sort-key '(name)
604 :marks '((install . ?I)
605 (upgrade . ?U)
606 (delete . ?D)))
607
608 (let ((map guix-package-list-mode-map))
609 (define-key map (kbd "B") 'guix-package-list-latest-builds)
610 (define-key map (kbd "e") 'guix-package-list-edit)
611 (define-key map (kbd "x") 'guix-package-list-execute)
612 (define-key map (kbd "i") 'guix-package-list-mark-install)
613 (define-key map (kbd "d") 'guix-package-list-mark-delete)
614 (define-key map (kbd "U") 'guix-package-list-mark-upgrade)
615 (define-key map (kbd "^") 'guix-package-list-mark-upgrades))
616
617 (defface guix-package-list-installed
618 '((t :inherit guix-package-info-installed-outputs))
619 "Face used if there are installed outputs for the current package."
620 :group 'guix-package-list-faces)
621
622 (defface guix-package-list-obsolete
623 '((t :inherit guix-package-info-obsolete))
624 "Face used if a package is obsolete."
625 :group 'guix-package-list-faces)
626
627 (defcustom guix-package-list-generation-marking-enabled nil
628 "If non-nil, allow putting marks in a list with 'generation packages'.
629
630 By default this is disabled, because it may be confusing. For
631 example, a package is installed in some generation, so a user can
632 mark it for deletion in the list of packages from this
633 generation, but the package may not be installed in the latest
634 generation, so actually it cannot be deleted.
635
636 If you managed to understand the explanation above or if you
637 really know what you do or if you just don't care, you can set
638 this variable to t. It should not do much harm anyway (most
639 likely)."
640 :type 'boolean
641 :group 'guix-package-list)
642
643 (defun guix-package-list-get-name (name entry)
644 "Return NAME of the package ENTRY.
645 Colorize it with `guix-package-list-installed' or
646 `guix-package-list-obsolete' if needed."
647 (guix-get-string name
648 (cond ((guix-entry-value entry 'obsolete)
649 'guix-package-list-obsolete)
650 ((guix-entry-value entry 'installed)
651 'guix-package-list-installed))))
652
653 (defun guix-package-list-get-installed-outputs (installed &optional _)
654 "Return string with outputs from INSTALLED entries."
655 (guix-get-string
656 (mapcar (lambda (entry)
657 (guix-entry-value entry 'output))
658 installed)))
659
660 (defun guix-package-list-marking-check ()
661 "Signal an error if marking is disabled for the current buffer."
662 (when (and (not guix-package-list-generation-marking-enabled)
663 (or (derived-mode-p 'guix-package-list-mode)
664 (derived-mode-p 'guix-output-list-mode))
665 (eq (guix-ui-current-search-type) 'generation))
666 (error "Action marks are disabled for lists of 'generation packages'")))
667
668 (defun guix-package-list-mark-outputs (mark default
669 &optional prompt available)
670 "Mark the current package with MARK and move to the next line.
671 If PROMPT is non-nil, use it to ask a user for outputs from
672 AVAILABLE list, otherwise mark all DEFAULT outputs."
673 (let ((outputs (if prompt
674 (guix-completing-read-multiple
675 prompt available nil t)
676 default)))
677 (apply #'guix-list--mark mark t outputs)))
678
679 (defun guix-package-list-mark-install (&optional arg)
680 "Mark the current package for installation and move to the next line.
681 With ARG, prompt for the outputs to install (several outputs may
682 be separated with \",\")."
683 (interactive "P")
684 (guix-package-list-marking-check)
685 (let* ((entry (guix-list-current-entry))
686 (all (guix-entry-value entry 'outputs))
687 (installed (guix-package-installed-outputs entry))
688 (available (cl-set-difference all installed :test #'string=)))
689 (or available
690 (user-error "This package is already installed"))
691 (guix-package-list-mark-outputs
692 'install '("out")
693 (and arg "Output(s) to install: ")
694 available)))
695
696 (defun guix-package-list-mark-delete (&optional arg)
697 "Mark the current package for deletion and move to the next line.
698 With ARG, prompt for the outputs to delete (several outputs may
699 be separated with \",\")."
700 (interactive "P")
701 (guix-package-list-marking-check)
702 (let* ((entry (guix-list-current-entry))
703 (installed (guix-package-installed-outputs entry)))
704 (or installed
705 (user-error "This package is not installed"))
706 (guix-package-list-mark-outputs
707 'delete installed
708 (and arg "Output(s) to delete: ")
709 installed)))
710
711 (defun guix-package-list-mark-upgrade (&optional arg)
712 "Mark the current package for upgrading and move to the next line.
713 With ARG, prompt for the outputs to upgrade (several outputs may
714 be separated with \",\")."
715 (interactive "P")
716 (guix-package-list-marking-check)
717 (let* ((entry (guix-list-current-entry))
718 (installed (guix-package-installed-outputs entry)))
719 (or installed
720 (user-error "This package is not installed"))
721 (when (or (guix-entry-value entry 'obsolete)
722 (y-or-n-p "This package is not obsolete. Try to upgrade it anyway? "))
723 (guix-package-list-mark-outputs
724 'upgrade installed
725 (and arg "Output(s) to upgrade: ")
726 installed))))
727
728 (defun guix-package-mark-upgrades (fun)
729 "Mark all obsolete packages for upgrading.
730 Use FUN to perform marking of the current line. FUN should
731 take an entry as argument."
732 (guix-package-list-marking-check)
733 (let ((obsolete (cl-remove-if-not
734 (lambda (entry)
735 (guix-entry-value entry 'obsolete))
736 (guix-buffer-current-entries))))
737 (guix-list-for-each-line
738 (lambda ()
739 (let* ((id (guix-list-current-id))
740 (entry (cl-find-if
741 (lambda (entry)
742 (equal id (guix-entry-id entry)))
743 obsolete)))
744 (when entry
745 (funcall fun entry)))))))
746
747 (defun guix-package-list-mark-upgrades ()
748 "Mark all obsolete packages for upgrading."
749 (interactive)
750 (guix-package-mark-upgrades
751 (lambda (entry)
752 (apply #'guix-list--mark
753 'upgrade nil
754 (guix-package-installed-outputs entry)))))
755
756 (defun guix-package-assert-non-system-profile ()
757 "Verify that the current profile is not a system one.
758 The current profile is the one used by the current buffer."
759 (let ((profile (guix-ui-current-profile)))
760 (and profile
761 (guix-system-profile? profile)
762 (user-error "Packages cannot be installed or removed to/from \
763 profile '%s'.
764 Use 'guix system reconfigure' shell command to modify a system profile."
765 profile))))
766
767 (defun guix-package-execute-actions (fun)
768 "Perform actions on the marked packages.
769 Use FUN to define actions suitable for `guix-process-package-actions'.
770 FUN should take action-type as argument."
771 (guix-package-assert-non-system-profile)
772 (let ((actions (delq nil
773 (mapcar fun '(install delete upgrade)))))
774 (if actions
775 (guix-process-package-actions (guix-ui-current-profile)
776 actions (current-buffer))
777 (user-error "No operations specified"))))
778
779 (defun guix-package-list-execute ()
780 "Perform actions on the marked packages."
781 (interactive)
782 (guix-package-execute-actions #'guix-package-list-make-action))
783
784 (defun guix-package-list-make-action (action-type)
785 "Return action specification for the packages marked with ACTION-TYPE.
786 Return nil, if there are no packages marked with ACTION-TYPE.
787 The specification is suitable for `guix-process-package-actions'."
788 (let ((specs (guix-list-get-marked-args action-type)))
789 (and specs (cons action-type specs))))
790
791 (defun guix-package-list-edit (&optional directory)
792 "Go to the location of the current package.
793 See `guix-find-location' for the meaning of DIRECTORY."
794 (interactive (list (guix-read-directory)))
795 (guix-edit (guix-list-current-id) directory))
796
797 (defun guix-package-list-latest-builds (number &rest args)
798 "Display latest NUMBER of Hydra builds of the current package.
799 Interactively, prompt for NUMBER. With prefix argument, prompt
800 for all ARGS."
801 (interactive
802 (let ((entry (guix-list-current-entry)))
803 (guix-hydra-build-latest-prompt-args
804 :job (guix-hydra-job-name-specification
805 (guix-entry-value entry 'name)
806 (guix-entry-value entry 'version)))))
807 (apply #'guix-hydra-latest-builds number args))
808
809 \f
810 ;;; Output 'info'
811
812 (guix-ui-info-define-interface output
813 :buffer-name "*Guix Package Info*"
814 :format '((name format (format guix-package-info-name))
815 (version format guix-output-info-insert-version)
816 (output format guix-output-info-insert-output)
817 (synopsis simple (indent guix-package-info-synopsis))
818 (source simple guix-package-info-insert-source)
819 (path simple (indent guix-file))
820 (dependencies simple (indent guix-file))
821 (location simple guix-package-info-insert-location)
822 (home-url format (format guix-url))
823 (license format (format guix-package-license))
824 (systems format guix-package-info-insert-systems)
825 (inputs format (format guix-package-input))
826 (native-inputs format (format guix-package-native-input))
827 (propagated-inputs format
828 (format guix-package-propagated-input))
829 (description simple (indent guix-package-info-description)))
830 :titles guix-package-info-titles
831 :required '(id package-id installed non-unique))
832
833 (defun guix-output-info-insert-version (version entry)
834 "Insert output VERSION and obsolete text if needed at point."
835 (guix-info-insert-value-format version
836 'guix-package-info-version)
837 (and (guix-entry-value entry 'obsolete)
838 (guix-package-info-insert-obsolete-text)))
839
840 (defun guix-output-info-insert-output (output entry)
841 "Insert OUTPUT and action buttons at point."
842 (let* ((installed (guix-entry-value entry 'installed))
843 (obsolete (guix-entry-value entry 'obsolete))
844 (action-type (if installed 'delete 'install)))
845 (guix-info-insert-value-format
846 output
847 (if installed
848 'guix-package-info-installed-outputs
849 'guix-package-info-uninstalled-outputs))
850 (guix-info-insert-indent)
851 (guix-package-info-insert-action-button action-type entry output)
852 (when obsolete
853 (guix-info-insert-indent)
854 (guix-package-info-insert-action-button 'upgrade entry output))))
855
856 \f
857 ;;; Output 'list'
858
859 (guix-ui-list-define-interface output
860 :buffer-name "*Guix Package List*"
861 :describe-function 'guix-output-list-describe
862 :format '((name guix-package-list-get-name 20 t)
863 (version nil 10 nil)
864 (output nil 9 t)
865 (installed nil 12 t)
866 (synopsis guix-list-get-one-line 30 nil))
867 :required '(id package-id)
868 :sort-key '(name)
869 :marks '((install . ?I)
870 (upgrade . ?U)
871 (delete . ?D)))
872
873 (let ((map guix-output-list-mode-map))
874 (define-key map (kbd "B") 'guix-package-list-latest-builds)
875 (define-key map (kbd "e") 'guix-output-list-edit)
876 (define-key map (kbd "x") 'guix-output-list-execute)
877 (define-key map (kbd "i") 'guix-output-list-mark-install)
878 (define-key map (kbd "d") 'guix-output-list-mark-delete)
879 (define-key map (kbd "U") 'guix-output-list-mark-upgrade)
880 (define-key map (kbd "^") 'guix-output-list-mark-upgrades))
881
882 (defun guix-output-list-mark-install ()
883 "Mark the current output for installation 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 (if installed
889 (user-error "This output is already installed")
890 (guix-list--mark 'install t))))
891
892 (defun guix-output-list-mark-delete ()
893 "Mark the current output for deletion and move to the next line."
894 (interactive)
895 (guix-package-list-marking-check)
896 (let* ((entry (guix-list-current-entry))
897 (installed (guix-entry-value entry 'installed)))
898 (if installed
899 (guix-list--mark 'delete t)
900 (user-error "This output is not installed"))))
901
902 (defun guix-output-list-mark-upgrade ()
903 "Mark the current output for upgrading and move to the next line."
904 (interactive)
905 (guix-package-list-marking-check)
906 (let* ((entry (guix-list-current-entry))
907 (installed (guix-entry-value entry 'installed)))
908 (or installed
909 (user-error "This output is not installed"))
910 (when (or (guix-entry-value entry 'obsolete)
911 (y-or-n-p "This output is not obsolete. Try to upgrade it anyway? "))
912 (guix-list--mark 'upgrade t))))
913
914 (defun guix-output-list-mark-upgrades ()
915 "Mark all obsolete package outputs for upgrading."
916 (interactive)
917 (guix-package-mark-upgrades
918 (lambda (_) (guix-list--mark 'upgrade))))
919
920 (defun guix-output-list-execute ()
921 "Perform actions on the marked outputs."
922 (interactive)
923 (guix-package-execute-actions #'guix-output-list-make-action))
924
925 (defun guix-output-list-make-action (action-type)
926 "Return action specification for the outputs marked with ACTION-TYPE.
927 Return nil, if there are no outputs marked with ACTION-TYPE.
928 The specification is suitable for `guix-process-output-actions'."
929 (let ((ids (guix-list-get-marked-id-list action-type)))
930 (and ids (cons action-type
931 (mapcar #'guix-package-id-and-output-by-output-id
932 ids)))))
933
934 (defun guix-output-list-describe (ids)
935 "Describe outputs with IDS (list of output identifiers).
936 See `guix-package-info-type'."
937 (if (eq guix-package-info-type 'output)
938 (guix-buffer-get-display-entries
939 'info 'output
940 (cl-list* (guix-ui-current-profile) 'id ids)
941 'add)
942 (let ((pids (mapcar (lambda (oid)
943 (car (guix-package-id-and-output-by-output-id
944 oid)))
945 ids)))
946 (guix-buffer-get-display-entries
947 'info 'package
948 (cl-list* (guix-ui-current-profile)
949 'id (cl-remove-duplicates pids))
950 'add))))
951
952 (defun guix-output-list-edit (&optional directory)
953 "Go to the location of the current package.
954 See `guix-find-location' for the meaning of DIRECTORY."
955 (interactive (list (guix-read-directory)))
956 (guix-edit (guix-entry-value (guix-list-current-entry)
957 'package-id)
958 directory))
959
960 \f
961 ;;; Interactive commands
962
963 (defvar guix-package-search-params '(name synopsis description)
964 "Default list of package parameters for searching by regexp.")
965
966 (defvar guix-package-search-history nil
967 "A history of minibuffer prompts.")
968
969 ;;;###autoload
970 (defun guix-packages-by-name (name &optional profile)
971 "Display Guix packages with NAME.
972 NAME is a string with name specification. It may optionally contain
973 a version number. Examples: \"guile\", \"guile@2.0.11\".
974
975 If PROFILE is nil, use `guix-current-profile'.
976 Interactively with prefix, prompt for PROFILE."
977 (interactive
978 (list (guix-read-package-name)
979 (guix-ui-read-profile)))
980 (guix-package-get-display profile 'name name))
981
982 ;;;###autoload
983 (defun guix-packages-by-license (license &optional profile)
984 "Display Guix packages with LICENSE.
985 LICENSE is a license name string.
986 If PROFILE is nil, use `guix-current-profile'.
987 Interactively with prefix, prompt for PROFILE."
988 (interactive
989 (list (guix-read-license-name)
990 (guix-ui-read-profile)))
991 (guix-package-get-display profile 'license license))
992
993 ;;;###autoload
994 (defun guix-packages-by-location (location &optional profile)
995 "Display Guix packages placed in LOCATION file.
996 If PROFILE is nil, use `guix-current-profile'.
997 Interactively with prefix, prompt for PROFILE."
998 (interactive
999 (list (guix-read-package-location)
1000 (guix-ui-read-profile)))
1001 (guix-package-get-display profile 'location location))
1002
1003 ;;;###autoload
1004 (defun guix-search-by-regexp (regexp &optional params profile)
1005 "Search for Guix packages by REGEXP.
1006 PARAMS are package parameters that should be searched.
1007 If PARAMS are not specified, use `guix-package-search-params'.
1008
1009 If PROFILE is nil, use `guix-current-profile'.
1010 Interactively with prefix, prompt for PROFILE."
1011 (interactive
1012 (list (read-regexp "Regexp: " nil 'guix-package-search-history)
1013 nil (guix-ui-read-profile)))
1014 (guix-package-get-display profile 'regexp regexp
1015 (or params guix-package-search-params)))
1016
1017 ;;;###autoload
1018 (defun guix-search-by-name (regexp &optional profile)
1019 "Search for Guix packages matching REGEXP in a package name.
1020 If PROFILE is nil, use `guix-current-profile'.
1021 Interactively with prefix, prompt for PROFILE."
1022 (interactive
1023 (list (read-string "Package name by regexp: "
1024 nil 'guix-package-search-history)
1025 (guix-ui-read-profile)))
1026 (guix-search-by-regexp regexp '(name) profile))
1027
1028 ;;;###autoload
1029 (defun guix-installed-packages (&optional profile)
1030 "Display information about installed Guix packages.
1031 If PROFILE is nil, use `guix-current-profile'.
1032 Interactively with prefix, prompt for PROFILE."
1033 (interactive (list (guix-ui-read-profile)))
1034 (guix-package-get-display profile 'installed))
1035
1036 ;;;###autoload
1037 (defun guix-installed-user-packages ()
1038 "Display information about Guix packages installed in a user profile."
1039 (interactive)
1040 (guix-installed-packages guix-user-profile))
1041
1042 ;;;###autoload
1043 (defun guix-installed-system-packages ()
1044 "Display information about Guix packages installed in a system profile."
1045 (interactive)
1046 (guix-installed-packages
1047 (guix-packages-profile guix-system-profile nil t)))
1048
1049 ;;;###autoload
1050 (defun guix-obsolete-packages (&optional profile)
1051 "Display information about obsolete Guix packages.
1052 If PROFILE is nil, use `guix-current-profile'.
1053 Interactively with prefix, prompt for PROFILE."
1054 (interactive (list (guix-ui-read-profile)))
1055 (guix-package-get-display profile 'obsolete))
1056
1057 ;;;###autoload
1058 (defun guix-all-available-packages (&optional profile)
1059 "Display information about all available Guix packages.
1060 If PROFILE is nil, use `guix-current-profile'.
1061 Interactively with prefix, prompt for PROFILE."
1062 (interactive (list (guix-ui-read-profile)))
1063 (guix-package-get-display profile 'all-available))
1064
1065 ;;;###autoload
1066 (defun guix-newest-available-packages (&optional profile)
1067 "Display information about the newest available Guix packages.
1068 If PROFILE is nil, use `guix-current-profile'.
1069 Interactively with prefix, prompt for PROFILE."
1070 (interactive (list (guix-ui-read-profile)))
1071 (guix-package-get-display profile 'newest-available))
1072
1073 (provide 'guix-ui-package)
1074
1075 ;;; guix-ui-package.el ends here