1 ;;; guix-base.el --- Common definitions -*- lexical-binding: t -*-
3 ;; Copyright © 2014 Alex Kost <alezost@gmail.com>
5 ;; This file is part of GNU Guix.
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.
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.
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/>.
22 ;; This file provides some base and common definitions for guix.el
25 ;; List and info buffers have many common patterns that are defined
26 ;; using `guix-define-buffer-type' macro from this file.
31 (require 'guix-profiles
)
32 (require 'guix-backend
)
34 (require 'guix-history
)
35 (require 'guix-messages
)
38 ;;; Parameters of the entries
40 (defvar guix-param-titles
47 (synopsis .
"Synopsis")
48 (description .
"Description")
49 (home-url .
"Home page")
52 (native-inputs .
"Native inputs")
53 (propagated-inputs .
"Propagated inputs")
54 (location .
"Location")
55 (installed .
"Installed"))
57 (path .
"Installed path")
58 (dependencies .
"Dependencies")
66 (synopsis .
"Synopsis")
67 (description .
"Description")
68 (home-url .
"Home page")
71 (native-inputs .
"Native inputs")
72 (propagated-inputs .
"Propagated inputs")
73 (location .
"Location")
74 (installed .
"Installed")
75 (path .
"Installed path")
76 (dependencies .
"Dependencies"))
80 (prev-number .
"Previous number")
84 "List for defining titles of entry parameters.
85 Titles are used for displaying information about entries.
86 Each element of the list has a form:
88 (ENTRY-TYPE . ((PARAM . TITLE) ...))")
90 (defun guix-get-param-title (entry-type param
)
91 "Return title of an ENTRY-TYPE entry parameter PARAM."
92 (or (guix-get-key-val guix-param-titles
94 (prog1 (symbol-name param
)
95 (message "Couldn't find title for '%S %S'."
98 (defun guix-get-name-spec (name version
&optional output
)
99 "Return Guix package specification by its NAME, VERSION and OUTPUT."
100 (concat name
"-" version
101 (when output
(concat ":" output
))))
103 (defun guix-get-full-name (entry &optional output
)
104 "Return name specification of the package ENTRY and OUTPUT."
105 (guix-get-name-spec (guix-get-key-val entry
'name
)
106 (guix-get-key-val entry
'version
)
109 (defun guix-entry-to-specification (entry)
110 "Return name specification by the package or output ENTRY."
111 (guix-get-name-spec (guix-get-key-val entry
'name
)
112 (guix-get-key-val entry
'version
)
113 (guix-get-key-val entry
'output
)))
115 (defun guix-entries-to-specifications (entries)
116 "Return name specifications by the package or output ENTRIES."
117 (cl-remove-duplicates (mapcar #'guix-entry-to-specification entries
)
120 (defun guix-get-installed-outputs (entry)
121 "Return list of installed outputs for the package ENTRY."
122 (mapcar (lambda (installed-entry)
123 (guix-get-key-val installed-entry
'output
))
124 (guix-get-key-val entry
'installed
)))
126 (defun guix-get-entry-by-id (id entries
)
127 "Return entry from ENTRIES by entry ID."
128 (cl-find-if (lambda (entry)
129 (equal id
(guix-get-key-val entry
'id
)))
132 (defun guix-get-package-id-and-output-by-output-id (oid)
133 "Return list (PACKAGE-ID OUTPUT) by output id OID."
134 (cl-multiple-value-bind (pid-str output
)
135 (split-string oid
":")
136 (let ((pid (string-to-number pid-str
)))
137 (list (if (= 0 pid
) pid-str pid
)
141 ;;; Location of the packages
143 (defvar guix-directory nil
144 "Default Guix directory.
145 If it is not set by a user, it is set after starting Guile REPL.
146 This directory is used to define location of the packages.")
148 (defun guix-set-directory ()
149 "Set `guix-directory' if needed."
152 (guix-eval-read "%guix-dir"))))
154 (add-hook 'guix-after-start-repl-hook
'guix-set-directory
)
156 (defun guix-find-location (location)
157 "Go to LOCATION of a package.
158 LOCATION is a string of the form:
162 If PATH is relative, it is considered to be relative to
164 (cl-multiple-value-bind (path line col
)
165 (split-string location
":")
166 (let ((file (expand-file-name path guix-directory
))
167 (line (string-to-number line
))
168 (col (string-to-number col
)))
170 (goto-char (point-min))
171 (forward-line (- line
1))
176 ;;; Buffers and auto updating.
178 (defcustom guix-update-after-operation
'current
179 "Define what information to update after executing an operation.
181 After successful executing an operation in the Guix REPL (for
182 example after installing a package), information in Guix buffers
183 will or will not be automatically updated depending on a value of
186 If nil, update nothing (do not revert any buffer).
187 If `current', update the buffer from which an operation was performed.
188 If `all', update all Guix buffers (not recommended)."
189 :type
'(choice (const :tag
"Do nothing" nil
)
190 (const :tag
"Update operation buffer" current
)
191 (const :tag
"Update all Guix buffers" all
))
194 (defcustom guix-buffer-name-function
#'guix-buffer-name-default
195 "Function used to define name of a buffer for displaying information.
196 The function is called with 4 arguments: PROFILE, BUFFER-TYPE,
197 ENTRY-TYPE, SEARCH-TYPE. See `guix-get-entries' for the meaning
199 :type
'(choice (function-item guix-buffer-name-default
)
200 (function-item guix-buffer-name-simple
)
201 (function :tag
"Other function"))
204 (defun guix-buffer-name-simple (_profile buffer-type entry-type
205 &optional _search-type
)
206 "Return name of a buffer used for displaying information.
207 The name is defined by `guix-ENTRY-TYPE-BUFFER-TYPE-buffer-name'
210 (guix-get-symbol "buffer-name" buffer-type entry-type
)))
212 (defun guix-buffer-name-default (profile buffer-type entry-type
213 &optional _search-type
)
214 "Return name of a buffer used for displaying information.
215 The name is almost the same as the one defined by
216 `guix-buffer-name-simple' except the PROFILE name is added to it."
217 (let ((simple-name (guix-buffer-name-simple
218 profile buffer-type entry-type
))
219 (profile-name (file-name-base (directory-file-name profile
)))
225 (or (string-match re simple-name
)
226 (error "Unexpected error in defining guix buffer name"))
227 (let ((first* (match-string 1 simple-name
))
228 (name-body (match-string 2 simple-name
))
229 (last* (match-string 3 simple-name
)))
230 ;; Handle the case when buffer name is wrapped by '*'.
231 (if (and (string= "*" first
*)
233 (concat "*" name-body
": " profile-name
"*")
234 (concat simple-name
": " profile-name
)))))
236 (defun guix-buffer-name (profile buffer-type entry-type search-type
)
237 "Return name of a buffer used for displaying information.
238 See `guix-buffer-name-function' for details."
239 (let ((fun (if (functionp guix-buffer-name-function
)
240 guix-buffer-name-function
241 #'guix-buffer-name-default
)))
242 (funcall fun profile buffer-type entry-type search-type
)))
244 (defun guix-switch-to-buffer (buffer)
245 "Switch to a 'list' or 'info' BUFFER."
246 (pop-to-buffer buffer
247 '((display-buffer-reuse-window
248 display-buffer-same-window
))))
250 (defun guix-buffer-p (&optional buffer modes
)
251 "Return non-nil if BUFFER mode is derived from any of the MODES.
252 If BUFFER is nil, check current buffer.
253 If MODES is nil, use `guix-list-mode' and `guix-info-mode'."
254 (with-current-buffer (or buffer
(current-buffer))
255 (apply #'derived-mode-p
257 '(guix-list-mode guix-info-mode
)))))
259 (defun guix-buffers (&optional modes
)
260 "Return list of all buffers with major modes derived from MODES.
261 If MODES is nil, return list of all Guix 'list' and 'info' buffers."
262 (cl-remove-if-not (lambda (buf)
263 (guix-buffer-p buf modes
))
266 (defun guix-update-buffer (buffer)
267 "Update information in a 'list' or 'info' BUFFER."
268 (with-current-buffer buffer
269 (guix-revert-buffer nil t
)))
271 (defun guix-update-buffers-maybe-after-operation ()
272 "Update buffers after Guix operation if needed.
273 See `guix-update-after-operation' for details."
275 (and guix-operation-buffer
276 (cl-case guix-update-after-operation
277 (current (and (buffer-live-p guix-operation-buffer
)
278 (guix-buffer-p guix-operation-buffer
)
279 (list guix-operation-buffer
)))
280 (all (guix-buffers))))))
281 (setq guix-operation-buffer nil
)
282 (mapc #'guix-update-buffer to-update
)))
284 (add-hook 'guix-after-repl-operation-hook
285 'guix-update-buffers-maybe-after-operation
)
288 ;;; Common definitions for buffer types
290 (defvar-local guix-profile nil
291 "Profile used for the current buffer.")
292 (put 'guix-profile
'permanent-local t
)
294 (defvar-local guix-entries nil
295 "List of the currently displayed entries.
296 Each element of the list is alist with entry info of the
301 PARAM is a name of the entry parameter.
302 VAL is a value of this parameter.")
303 (put 'guix-entries
'permanent-local t
)
305 (defvar-local guix-buffer-type nil
306 "Type of the current buffer.")
307 (put 'guix-buffer-type
'permanent-local t
)
309 (defvar-local guix-entry-type nil
310 "Type of the current entry.")
311 (put 'guix-entry-type
'permanent-local t
)
313 (defvar-local guix-search-type nil
314 "Type of the current search.")
315 (put 'guix-search-type
'permanent-local t
)
317 (defvar-local guix-search-vals nil
318 "Values of the current search.")
319 (put 'guix-search-vals
'permanent-local t
)
321 (defsubst guix-set-vars
(profile entries buffer-type entry-type
322 search-type search-vals
)
323 "Set local variables for the current Guix buffer."
324 (setq default-directory profile
327 guix-buffer-type buffer-type
328 guix-entry-type entry-type
329 guix-search-type search-type
330 guix-search-vals search-vals
))
332 (defun guix-get-symbol (postfix buffer-type
&optional entry-type
)
333 (intern (concat "guix-"
335 (concat (symbol-name entry-type
) "-"))
336 (symbol-name buffer-type
) "-" postfix
)))
338 (defmacro guix-define-buffer-type
(buf-type entry-type
&rest args
)
339 "Define common for BUF-TYPE buffers for displaying ENTRY-TYPE entries.
341 In the text below TYPE means ENTRY-TYPE-BUF-TYPE.
343 This macro defines `guix-TYPE-mode', a custom group and several
346 The following stuff should be defined outside this macro:
348 - `guix-BUF-TYPE-mode' - parent mode for the defined mode.
350 - `guix-TYPE-mode-initialize' (optional) - function for
351 additional mode settings; it is called without arguments.
353 Remaining argument (ARGS) should have a form [KEYWORD VALUE] ... The
354 following keywords are available:
356 - `:buffer-name' - default value for the defined
357 `guix-TYPE-buffer-name' variable.
359 - `:required' - default value for the defined
360 `guix-TYPE-required-params' variable.
362 - `:history-size' - default value for the defined
363 `guix-TYPE-history-size' variable.
365 - `:revert' - default value for the defined
366 `guix-TYPE-revert-no-confirm' variable."
367 (let* ((entry-type-str (symbol-name entry-type
))
368 (buf-type-str (symbol-name buf-type
))
369 (Entry-type-str (capitalize entry-type-str
))
370 (Buf-type-str (capitalize buf-type-str
))
371 (entry-str (concat entry-type-str
" entries"))
372 (buf-str (concat buf-type-str
" buffer"))
373 (prefix (concat "guix-" entry-type-str
"-" buf-type-str
))
374 (group (intern prefix
))
375 (mode-map-str (concat prefix
"-mode-map"))
376 (mode-map (intern mode-map-str
))
377 (parent-mode (intern (concat "guix-" buf-type-str
"-mode")))
378 (mode (intern (concat prefix
"-mode")))
379 (mode-init-fun (intern (concat prefix
"-mode-initialize")))
380 (buf-name-var (intern (concat prefix
"-buffer-name")))
381 (revert-var (intern (concat prefix
"-revert-no-confirm")))
382 (history-var (intern (concat prefix
"-history-size")))
383 (params-var (intern (concat prefix
"-required-params")))
384 (buf-name-val (format "*Guix %s %s*" Entry-type-str Buf-type-str
))
389 ;; Process the keyword args.
390 (while (keywordp (car args
))
392 (`:required
(setq params-val
(pop args
)))
393 (`:history-size
(setq history-val
(pop args
)))
394 (`:revert
(setq revert-val
(pop args
)))
395 (`:buffer-name
(setq buf-name-val
(pop args
)))
400 ,(concat Buf-type-str
" buffer with " entry-str
".")
401 :prefix
,(concat prefix
"-")
402 :group
',(intern (concat "guix-" buf-type-str
)))
404 (defcustom ,buf-name-var
,buf-name-val
405 ,(concat "Default name of the " buf-str
" for displaying " entry-str
".")
409 (defcustom ,history-var
,history-val
410 ,(concat "Maximum number of items saved in the history of the " buf-str
".\n"
411 "If 0, the history is disabled.")
415 (defcustom ,revert-var
,revert-val
416 ,(concat "If non-nil, do not ask to confirm for reverting the " buf-str
".")
420 (defvar ,params-var
',params-val
421 ,(concat "List of required " entry-type-str
" parameters.\n\n"
422 "Displayed parameters and parameters from this list are received\n"
423 "for each " entry-type-str
".\n\n"
424 "May be a special value `all', in which case all supported\n"
425 "parameters are received (this may be very slow for a big number\n"
427 "Do not remove `id' from this list as it is required for\n"
428 "identifying an entry."))
430 (define-derived-mode ,mode
,parent-mode
,(concat "Guix-" Buf-type-str
)
431 ,(concat "Major mode for displaying information about " entry-str
".\n\n"
432 "\\{" mode-map-str
"}")
433 (setq-local revert-buffer-function
'guix-revert-buffer
)
434 (setq-local guix-history-size
,history-var
)
435 (and (fboundp ',mode-init-fun
) (,mode-init-fun
)))
437 (let ((map ,mode-map
))
438 (define-key map
(kbd "l") 'guix-history-back
)
439 (define-key map
(kbd "r") 'guix-history-forward
)
440 (define-key map
(kbd "g") 'revert-buffer
)
441 (define-key map
(kbd "R") 'guix-redisplay-buffer
)
442 (define-key map
(kbd "C-c C-z") 'guix-switch-to-repl
)))))
444 (put 'guix-define-buffer-type
'lisp-indent-function
'defun
)
447 ;;; Getting and displaying info about packages and generations
449 (defcustom guix-package-list-type
'output
450 "Define how to display packages in a list buffer.
451 May be a symbol `package' or `output' (if `output', display each
452 output on a separate line; if `package', display each package on
454 :type
'(choice (const :tag
"List of packages" package
)
455 (const :tag
"List of outputs" output
))
458 (defcustom guix-package-info-type
'package
459 "Define how to display packages in an info buffer.
460 May be a symbol `package' or `output' (if `output', display each
461 output separately; if `package', display outputs inside a package
463 :type
'(choice (const :tag
"Display packages" package
)
464 (const :tag
"Display outputs" output
))
467 (defun guix-get-entries (profile entry-type search-type search-vals
469 "Search for entries of ENTRY-TYPE.
471 Call an appropriate scheme function and return a list of the
472 form of `guix-entries'.
474 ENTRY-TYPE should be one of the following symbols: `package',
475 `output' or `generation'.
477 SEARCH-TYPE may be one of the following symbols:
479 - If ENTRY-TYPE is `package' or `output': `id', `name', `regexp',
480 `all-available', `newest-available', `installed', `obsolete',
483 - If ENTRY-TYPE is `generation': `id', `last', `all', `time'.
485 PARAMS is a list of parameters for receiving. If nil, get
486 information with all available parameters."
487 (guix-eval-read (guix-make-guile-expression
489 profile params entry-type search-type search-vals
)))
491 (defun guix-get-show-entries (profile buffer-type entry-type search-type
493 "Search for ENTRY-TYPE entries and show results in BUFFER-TYPE buffer.
494 See `guix-get-entries' for the meaning of SEARCH-TYPE and SEARCH-VALS."
495 (let ((entries (guix-get-entries profile entry-type search-type search-vals
496 (guix-get-params-for-receiving
497 buffer-type entry-type
))))
498 (guix-set-buffer profile entries buffer-type entry-type
499 search-type search-vals
)))
501 (defun guix-set-buffer (profile entries buffer-type entry-type search-type
502 search-vals
&optional history-replace no-display
)
503 "Set up BUFFER-TYPE buffer for displaying ENTRY-TYPE ENTRIES.
505 Insert ENTRIES in buffer, set variables and make history item.
506 ENTRIES should have a form of `guix-entries'.
508 See `guix-get-entries' for the meaning of SEARCH-TYPE and SEARCH-VALS.
510 If HISTORY-REPLACE is non-nil, replace current history item,
511 otherwise add the new one.
513 If NO-DISPLAY is non-nil, do not switch to the buffer."
515 (let ((buf (if (and (eq major-mode
516 (guix-get-symbol "mode" buffer-type entry-type
))
517 (equal guix-profile profile
))
520 (guix-buffer-name profile buffer-type
521 entry-type search-type
)))))
522 (with-current-buffer buf
523 (guix-show-entries entries buffer-type entry-type
)
524 (guix-set-vars profile entries buffer-type entry-type
525 search-type search-vals
)
526 (funcall (if history-replace
527 #'guix-history-replace
529 (guix-make-history-item)))
531 (guix-switch-to-buffer buf
))))
532 (guix-result-message profile entries entry-type
533 search-type search-vals
))
535 (defun guix-show-entries (entries buffer-type entry-type
)
536 "Display ENTRY-TYPE ENTRIES in the current BUFFER-TYPE buffer."
537 (let ((inhibit-read-only t
))
539 (funcall (symbol-function (guix-get-symbol
540 "mode" buffer-type entry-type
)))
541 (funcall (guix-get-symbol "insert-entries" buffer-type
)
543 (goto-char (point-min))))
545 (defun guix-history-call (profile entries buffer-type entry-type
546 search-type search-vals
)
547 "Function called for moving by history."
548 (guix-show-entries entries buffer-type entry-type
)
549 (guix-set-vars profile entries buffer-type entry-type
550 search-type search-vals
)
551 (guix-result-message profile entries entry-type
552 search-type search-vals
))
554 (defun guix-make-history-item ()
555 "Make and return a history item for the current buffer."
556 (list #'guix-history-call
557 guix-profile guix-entries guix-buffer-type guix-entry-type
558 guix-search-type guix-search-vals
))
560 (defun guix-get-params-for-receiving (buffer-type entry-type
)
561 "Return parameters that should be received for BUFFER-TYPE, ENTRY-TYPE."
562 (let* ((required-var (guix-get-symbol "required-params"
563 buffer-type entry-type
))
564 (required (symbol-value required-var
)))
565 (unless (equal required
'all
)
567 (funcall (guix-get-symbol "get-displayed-params"
571 (defun guix-revert-buffer (_ignore-auto noconfirm
)
572 "Update information in the current buffer.
573 The function is suitable for `revert-buffer-function'.
574 See `revert-buffer' for the meaning of NOCONFIRM."
577 (guix-get-symbol "revert-no-confirm"
578 guix-buffer-type guix-entry-type
))
579 (y-or-n-p "Update current information? "))
580 (let* ((search-type guix-search-type
)
581 (search-vals guix-search-vals
)
582 (params (guix-get-params-for-receiving guix-buffer-type
584 (entries (guix-get-entries
585 guix-profile guix-entry-type
586 guix-search-type guix-search-vals params
))
587 ;; If a REPL was restarted, package/output IDs are not actual
588 ;; anymore, because 'object-address'-es died with the REPL, so if a
589 ;; search by ID didn't give results, search again by name.
590 (entries (if (and (null entries
)
591 (eq guix-search-type
'id
)
592 (or (eq guix-entry-type
'package
)
593 (eq guix-entry-type
'output
)))
595 (setq search-type
'name
596 search-vals
(guix-entries-to-specifications
599 guix-profile guix-entry-type
600 search-type search-vals params
))
602 (guix-set-buffer guix-profile entries guix-buffer-type guix-entry-type
603 search-type search-vals t t
))))
605 (cl-defun guix-redisplay-buffer (&key buffer profile entries buffer-type
606 entry-type search-type search-vals
)
607 "Redisplay a Guix BUFFER.
608 Restore the point and window positions after redisplaying if possible.
610 This function will not update the information, use
611 \"\\[revert-buffer]\" if you want the full update.
613 If BUFFER is nil, use the current buffer. For the meaning of the
614 rest arguments, see `guix-set-buffer'."
616 (or buffer
(setq buffer
(current-buffer)))
617 (with-current-buffer buffer
618 (or (derived-mode-p 'guix-info-mode
'guix-list-mode
)
619 (error "%S is not a Guix buffer" buffer
))
620 (let* ((point (point))
621 (was-at-button (button-at point
))
622 ;; For simplicity, ignore an unlikely case when multiple
623 ;; windows display the same BUFFER.
624 (window (car (get-buffer-window-list buffer nil t
)))
625 (window-start (and window
(window-start window
))))
626 (guix-set-buffer (or profile guix-profile
)
627 (or entries guix-entries
)
628 (or buffer-type guix-buffer-type
)
629 (or entry-type guix-entry-type
)
630 (or search-type guix-search-type
)
631 (or search-vals guix-search-vals
)
635 (not (button-at (point)))
638 (set-window-point window
(point))
639 (set-window-start window window-start
)))))
644 (defcustom guix-generation-packages-buffer-name-function
645 #'guix-generation-packages-buffer-name-default
646 "Function used to define name of a buffer with generation packages.
647 This function is called with 2 arguments: PROFILE (string) and
648 GENERATION (number)."
649 :type
'(choice (function-item guix-generation-packages-buffer-name-default
)
650 (function-item guix-generation-packages-buffer-name-long
)
651 (function :tag
"Other function"))
654 (defcustom guix-generation-packages-update-buffer t
655 "If non-nil, always update list of packages during comparing generations.
656 If nil, generation packages are received only once. So when you
657 compare generation 1 and generation 2, the packages for both
658 generations will be received. Then if you compare generation 1
659 and generation 3, only the packages for generation 3 will be
660 received. Thus if you use comparing of different generations a
661 lot, you may set this variable to nil to improve the
666 (defvar guix-output-name-width
30
667 "Width of an output name \"column\".
668 This variable is used in auxiliary buffers for comparing generations.")
670 (defun guix-generation-file (profile generation
)
671 "Return the file name of a PROFILE's GENERATION."
672 (format "%s-%s-link" profile generation
))
674 (defun guix-manifest-file (profile &optional generation
)
675 "Return the file name of a PROFILE's manifest.
676 If GENERATION number is specified, return manifest file name for
678 (expand-file-name "manifest"
680 (guix-generation-file profile generation
)
683 (defun guix-generation-packages (profile generation
)
684 "Return a list of sorted packages installed in PROFILE's GENERATION.
685 Each element of the list is a list of the package specification and its path."
686 (let ((names+paths
(guix-eval-read
687 (guix-make-guile-expression
688 'generation-package-specifications
+paths
689 profile generation
))))
692 (string< (car a
) (car b
))))))
694 (defun guix-generation-packages-buffer-name-default (profile generation
)
695 "Return name of a buffer for displaying GENERATION's package outputs.
696 Use base name of PROFILE path."
697 (let ((profile-name (file-name-base (directory-file-name profile
))))
698 (format "*Guix %s: generation %s*"
699 profile-name generation
)))
701 (defun guix-generation-packages-buffer-name-long (profile generation
)
702 "Return name of a buffer for displaying GENERATION's package outputs.
703 Use the full PROFILE path."
704 (format "*Guix generation %s (%s)*"
707 (defun guix-generation-packages-buffer-name (profile generation
)
708 "Return name of a buffer for displaying GENERATION's package outputs."
709 (let ((fun (if (functionp guix-generation-packages-buffer-name-function
)
710 guix-generation-packages-buffer-name-function
711 #'guix-generation-packages-buffer-name-default
)))
712 (funcall fun profile generation
)))
714 (defun guix-generation-insert-package (name path
)
715 "Insert package output NAME and PATH at point."
717 (indent-to guix-output-name-width
2)
720 (defun guix-generation-insert-packages (buffer profile generation
)
721 "Insert package outputs installed in PROFILE's GENERATION in BUFFER."
722 (with-current-buffer buffer
723 (setq buffer-read-only nil
724 indent-tabs-mode nil
)
726 (mapc (lambda (name+path
)
727 (guix-generation-insert-package
728 (car name
+path
) (cadr name
+path
)))
729 (guix-generation-packages profile generation
))))
731 (defun guix-generation-packages-buffer (profile generation
)
732 "Return buffer with package outputs installed in PROFILE's GENERATION.
733 Create the buffer if needed."
734 (let ((buf-name (guix-generation-packages-buffer-name
735 profile generation
)))
736 (or (and (null guix-generation-packages-update-buffer
)
737 (get-buffer buf-name
))
738 (let ((buf (get-buffer-create buf-name
)))
739 (guix-generation-insert-packages buf profile generation
)
742 (defun guix-profile-generation-manifest-file (generation)
743 "Return the file name of a GENERATION's manifest.
744 GENERATION is a generation number of `guix-profile' profile."
745 (guix-manifest-file guix-profile generation
))
747 (defun guix-profile-generation-packages-buffer (generation)
748 "Insert GENERATION's package outputs in a buffer and return it.
749 GENERATION is a generation number of `guix-profile' profile."
750 (guix-generation-packages-buffer guix-profile generation
))
753 ;;; Actions on packages and generations
755 (defface guix-operation-option-key
756 '((t :inherit font-lock-warning-face
))
757 "Face used for the keys of operation options."
760 (defcustom guix-operation-confirm t
761 "If nil, do not prompt to confirm an operation."
765 (defcustom guix-use-substitutes t
766 "If non-nil, use substitutes for the Guix packages."
770 (defvar guix-dry-run nil
771 "If non-nil, do not perform the real actions, just simulate.")
773 (defvar guix-temp-buffer-name
" *Guix temp*"
774 "Name of a buffer used for displaying info before executing operation.")
776 (defvar guix-operation-option-true-string
"yes"
777 "String displayed in the mode-line when operation option is t.")
779 (defvar guix-operation-option-false-string
"no "
780 "String displayed in the mode-line when operation option is nil.")
782 (defvar guix-operation-option-separator
" | "
783 "String used in the mode-line to separate operation options.")
785 (defvar guix-operation-options
786 '((?s
"substitutes" guix-use-substitutes
)
787 (?d
"dry-run" guix-dry-run
))
788 "List of available operation options.
789 Each element of the list has a form:
793 KEY is a character that may be pressed during confirmation to
795 NAME is a string displayed in the mode-line.
796 VARIABLE is a name of an option variable.")
798 (defun guix-operation-option-by-key (key)
799 "Return operation option by KEY (character)."
800 (assq key guix-operation-options
))
802 (defun guix-operation-option-key (option)
803 "Return key (character) of the operation OPTION."
806 (defun guix-operation-option-name (option)
807 "Return name of the operation OPTION."
810 (defun guix-operation-option-variable (option)
811 "Return name of the variable of the operation OPTION."
814 (defun guix-operation-option-value (option)
815 "Return boolean value of the operation OPTION."
816 (symbol-value (guix-operation-option-variable option
)))
818 (defun guix-operation-option-string-value (option)
819 "Convert boolean value of the operation OPTION to string and return it."
820 (if (guix-operation-option-value option
)
821 guix-operation-option-true-string
822 guix-operation-option-false-string
))
824 (defun guix-process-package-actions (profile actions
825 &optional operation-buffer
)
826 "Process package ACTIONS on PROFILE.
827 Each action is a list of the form:
829 (ACTION-TYPE PACKAGE-SPEC ...)
831 ACTION-TYPE is one of the following symbols: `install',
832 `upgrade', `remove'/`delete'.
833 PACKAGE-SPEC should have the following form: (ID [OUTPUT] ...)."
834 (let (install upgrade remove
)
835 (mapc (lambda (action)
836 (let ((action-type (car action
))
837 (specs (cdr action
)))
839 (install (setq install
(append install specs
)))
840 (upgrade (setq upgrade
(append upgrade specs
)))
841 ((remove delete
) (setq remove
(append remove specs
))))))
843 (when (guix-continue-package-operation-p
845 :install install
:upgrade upgrade
:remove remove
)
847 (guix-make-guile-expression
848 'process-package-actions profile
849 :install install
:upgrade upgrade
:remove remove
850 :use-substitutes?
(or guix-use-substitutes
'f
)
851 :dry-run?
(or guix-dry-run
'f
))
852 (and (not guix-dry-run
) operation-buffer
)))))
854 (cl-defun guix-continue-package-operation-p (profile
855 &key install upgrade remove
)
856 "Return non-nil if a package operation should be continued.
857 Ask a user if needed (see `guix-operation-confirm').
858 INSTALL, UPGRADE, REMOVE are 'package action specifications'.
859 See `guix-process-package-actions' for details."
860 (or (null guix-operation-confirm
)
861 (let* ((entries (guix-get-entries
863 (append (mapcar #'car install
)
864 (mapcar #'car upgrade
)
865 (mapcar #'car remove
))
866 '(id name version location
)))
867 (install-strings (guix-get-package-strings install entries
))
868 (upgrade-strings (guix-get-package-strings upgrade entries
))
869 (remove-strings (guix-get-package-strings remove entries
)))
870 (if (or install-strings upgrade-strings remove-strings
)
871 (let ((buf (get-buffer-create guix-temp-buffer-name
)))
872 (with-current-buffer buf
873 (setq-local cursor-type nil
)
874 (setq buffer-read-only nil
)
876 (insert "Profile: " profile
"\n\n")
877 (guix-insert-package-strings install-strings
"install")
878 (guix-insert-package-strings upgrade-strings
"upgrade")
879 (guix-insert-package-strings remove-strings
"remove")
880 (let ((win (temp-buffer-window-show
882 '((display-buffer-reuse-window
883 display-buffer-at-bottom
)
884 (window-height . fit-window-to-buffer
)))))
885 (prog1 (guix-operation-prompt)
886 (quit-window nil win
)))))
887 (message "Nothing to be done. If the REPL was restarted, information is not up-to-date.")
890 (defun guix-get-package-strings (specs entries
)
891 "Return short package descriptions for performing package actions.
892 See `guix-process-package-actions' for the meaning of SPECS.
893 ENTRIES is a list of package entries to get info about packages."
897 (let* ((id (car spec
))
899 (entry (guix-get-entry-by-id id entries
)))
901 (let ((location (guix-get-key-val entry
'location
)))
902 (concat (guix-get-full-name entry
)
905 (mapconcat #'identity outputs
",")))
907 (concat "\t(" location
")")))))))
910 (defun guix-insert-package-strings (strings action
)
911 "Insert information STRINGS at point for performing package ACTION."
913 (insert "Package(s) to " (propertize action
'face
'bold
) ":\n")
915 (insert " " str
"\n"))
919 (defun guix-operation-prompt (&optional prompt
)
920 "Prompt a user for continuing the current operation.
921 Return non-nil, if the operation should be continued; nil otherwise.
922 Ask a user with PROMPT for continuing an operation."
923 (let* ((option-keys (mapcar #'guix-operation-option-key
924 guix-operation-options
))
925 (keys (append '(?y ?n
) option-keys
))
926 (prompt (concat (propertize (or prompt
"Continue operation?")
927 'face
'minibuffer-prompt
)
931 (propertize (string key
)
932 'face
'guix-operation-option-key
))
936 (let ((mode-line mode-line-format
))
937 (prog1 (guix-operation-prompt-1 prompt keys
)
938 (setq mode-line-format mode-line
)
939 ;; Clear the minibuffer after prompting.
942 (defun guix-operation-prompt-1 (prompt keys
)
943 "This function is internal for `guix-operation-prompt'."
944 (guix-operation-set-mode-line)
945 (let ((key (read-char-choice prompt
(cons ?\C-g keys
) t
)))
949 (t (let* ((option (guix-operation-option-by-key key
))
950 (var (guix-operation-option-variable option
)))
951 (set var
(not (symbol-value var
)))
952 (guix-operation-prompt-1 prompt keys
))))))
954 (defun guix-operation-set-mode-line ()
955 "Display operation options in the mode-line of the current buffer."
956 (setq mode-line-format
957 (concat (propertize " Options: "
958 'face
'mode-line-buffer-id
)
961 (let ((key (guix-operation-option-key option
))
962 (name (guix-operation-option-name option
))
963 (val (guix-operation-option-string-value option
)))
966 (propertize (string key
)
967 'face
'guix-operation-option-key
)
969 guix-operation-options
970 guix-operation-option-separator
)))
971 (force-mode-line-update))
973 (defun guix-delete-generations (profile generations
974 &optional operation-buffer
)
975 "Delete GENERATIONS from PROFILE.
976 Each element from GENERATIONS is a generation number."
977 (when (or (not guix-operation-confirm
)
979 (let ((count (length generations
)))
981 (format "Delete %d generations from profile '%s'? "
983 (format "Delete generation %d from profile '%s'? "
984 (car generations
) profile
)))))
986 (guix-make-guile-expression
987 'delete-generations
* profile generations
)
990 (defun guix-switch-to-generation (profile generation
991 &optional operation-buffer
)
992 "Switch PROFILE to GENERATION."
993 (when (or (not guix-operation-confirm
)
994 (y-or-n-p (format "Switch profile '%s' to generation %d? "
995 profile generation
)))
997 (guix-make-guile-expression
998 'switch-to-generation profile generation
)
1001 (defun guix-package-source-path (package-id)
1002 "Return a store file path to a source of a package PACKAGE-ID."
1003 (message "Calculating the source derivation ...")
1005 (guix-make-guile-expression
1006 'package-source-path package-id
)))
1008 (defvar guix-after-source-download-hook nil
1009 "Hook run after successful performing a 'source-download' operation.")
1011 (defun guix-package-source-build-derivation (package-id &optional prompt
)
1012 "Build source derivation of a package PACKAGE-ID.
1013 Ask a user with PROMPT for continuing an operation."
1014 (when (or (not guix-operation-confirm
)
1015 (guix-operation-prompt (or prompt
1016 "Build the source derivation?")))
1018 (guix-make-guile-expression
1019 'package-source-build-derivation
1021 :use-substitutes?
(or guix-use-substitutes
'f
)
1022 :dry-run?
(or guix-dry-run
'f
))
1023 nil
'source-download
)))
1028 (defcustom guix-update-after-pull t
1029 "If non-nil, update Guix buffers after performing \\[guix-pull]."
1033 (defvar guix-after-pull-hook
1034 '(guix-restart-repl-after-pull guix-update-buffers-maybe-after-pull
)
1035 "Hook run after successful performing `guix-pull' operation.")
1037 (defun guix-restart-repl-after-pull ()
1038 "Restart Guix REPL after `guix-pull' operation."
1040 (guix-start-process-maybe
1041 "Restarting Guix REPL after pull operation ..."))
1043 (defun guix-update-buffers-maybe-after-pull ()
1044 "Update buffers depending on `guix-update-after-pull'."
1045 (when guix-update-after-pull
1046 (mapc #'guix-update-buffer
1047 ;; No need to update "generation" buffers.
1048 (guix-buffers '(guix-package-list-mode
1049 guix-package-info-mode
1050 guix-output-list-mode
1051 guix-output-info-mode
)))
1052 (message "Guix buffers have been updated.")))
1055 (defun guix-pull (&optional verbose
)
1056 "Run Guix pull operation.
1057 If VERBOSE is non-nil (with prefix argument), produce verbose output."
1059 (let ((args (and verbose
'("--verbose"))))
1061 (apply #'guix-make-guile-expression
'guix-pull args
)
1064 (provide 'guix-base
)
1066 ;;; guix-base.el ends here