store: Memoize 'add-to-store' based on the result of 'lstat', not 'stat'.
[guix.git] / emacs / guix-base.el
blob851ee895b98d71eacc230c35cfe1aa387a9f6446
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/>.
20 ;;; Commentary:
22 ;; This file provides some base and common definitions for guix.el
23 ;; package.
25 ;; List and info buffers have many common patterns that are defined
26 ;; using `guix-define-buffer-type' macro from this file.
28 ;;; Code:
30 (require 'cl-lib)
31 (require 'guix-profiles)
32 (require 'guix-backend)
33 (require 'guix-utils)
34 (require 'guix-history)
35 (require 'guix-messages)
38 ;;; Parameters of the entries
40 (defvar guix-param-titles
41 '((package
42 (id . "ID")
43 (name . "Name")
44 (version . "Version")
45 (source . "Source")
46 (license . "License")
47 (synopsis . "Synopsis")
48 (description . "Description")
49 (home-url . "Home page")
50 (outputs . "Outputs")
51 (inputs . "Inputs")
52 (native-inputs . "Native inputs")
53 (propagated-inputs . "Propagated inputs")
54 (location . "Location")
55 (installed . "Installed"))
56 (installed
57 (path . "Installed path")
58 (dependencies . "Dependencies")
59 (output . "Output"))
60 (output
61 (id . "ID")
62 (name . "Name")
63 (version . "Version")
64 (source . "Source")
65 (license . "License")
66 (synopsis . "Synopsis")
67 (description . "Description")
68 (home-url . "Home page")
69 (output . "Output")
70 (inputs . "Inputs")
71 (native-inputs . "Native inputs")
72 (propagated-inputs . "Propagated inputs")
73 (location . "Location")
74 (installed . "Installed")
75 (path . "Installed path")
76 (dependencies . "Dependencies"))
77 (generation
78 (id . "ID")
79 (number . "Number")
80 (prev-number . "Previous number")
81 (current . "Current")
82 (path . "Path")
83 (time . "Time")))
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
93 entry-type param)
94 (prog1 (symbol-name param)
95 (message "Couldn't find title for '%S %S'."
96 entry-type param))))
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)
107 output))
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)
118 :test #'string=))
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)))
130 entries))
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)
138 output))))
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."
150 (or guix-directory
151 (setq guix-directory
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:
160 \"PATH:LINE:COLUMN\"
162 If PATH is relative, it is considered to be relative to
163 `guix-directory'."
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)))
169 (find-file file)
170 (goto-char (point-min))
171 (forward-line (- line 1))
172 (move-to-column col)
173 (recenter 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
184 this variable.
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))
192 :group 'guix)
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
198 of the arguments."
199 :type '(choice (function-item guix-buffer-name-default)
200 (function-item guix-buffer-name-simple)
201 (function :tag "Other function"))
202 :group 'guix)
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'
208 variable."
209 (symbol-value
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)))
220 (re (rx string-start
221 (group (? "*"))
222 (group (*? any))
223 (group (? "*"))
224 string-end)))
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*)
232 (string= "*" last*))
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
256 (or modes
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))
264 (buffer-list)))
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."
274 (let ((to-update
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
297 following form:
299 ((PARAM . VAL) ...)
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
325 guix-profile profile
326 guix-entries entries
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-"
334 (when entry-type
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
344 user variables.
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))
385 (revert-val nil)
386 (history-val 20)
387 (params-val '(id)))
389 ;; Process the keyword args.
390 (while (keywordp (car args))
391 (pcase (pop 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)))
396 (_ (pop args))))
398 `(progn
399 (defgroup ,group nil
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 ".")
406 :type 'string
407 :group ',group)
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.")
412 :type 'integer
413 :group ',group)
415 (defcustom ,revert-var ,revert-val
416 ,(concat "If non-nil, do not ask to confirm for reverting the " buf-str ".")
417 :type 'boolean
418 :group ',group)
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"
426 "of entries).\n\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 "M") 'guix-apply-manifest)
443 (define-key map (kbd "C-c C-z") 'guix-switch-to-repl)))))
445 (put 'guix-define-buffer-type 'lisp-indent-function 'defun)
448 ;;; Getting and displaying info about packages and generations
450 (defcustom guix-package-list-type 'output
451 "Define how to display packages in a list buffer.
452 May be a symbol `package' or `output' (if `output', display each
453 output on a separate line; if `package', display each package on
454 a separate line)."
455 :type '(choice (const :tag "List of packages" package)
456 (const :tag "List of outputs" output))
457 :group 'guix)
459 (defcustom guix-package-info-type 'package
460 "Define how to display packages in an info buffer.
461 May be a symbol `package' or `output' (if `output', display each
462 output separately; if `package', display outputs inside a package
463 information)."
464 :type '(choice (const :tag "Display packages" package)
465 (const :tag "Display outputs" output))
466 :group 'guix)
468 (defun guix-get-entries (profile entry-type search-type search-vals
469 &optional params)
470 "Search for entries of ENTRY-TYPE.
472 Call an appropriate scheme function and return a list of the
473 form of `guix-entries'.
475 ENTRY-TYPE should be one of the following symbols: `package',
476 `output' or `generation'.
478 SEARCH-TYPE may be one of the following symbols:
480 - If ENTRY-TYPE is `package' or `output': `id', `name', `regexp',
481 `all-available', `newest-available', `installed', `obsolete',
482 `generation'.
484 - If ENTRY-TYPE is `generation': `id', `last', `all', `time'.
486 PARAMS is a list of parameters for receiving. If nil, get
487 information with all available parameters."
488 (guix-eval-read (guix-make-guile-expression
489 'entries
490 profile params entry-type search-type search-vals)))
492 (defun guix-get-show-entries (profile buffer-type entry-type search-type
493 &rest search-vals)
494 "Search for ENTRY-TYPE entries and show results in BUFFER-TYPE buffer.
495 See `guix-get-entries' for the meaning of SEARCH-TYPE and SEARCH-VALS."
496 (let ((entries (guix-get-entries profile entry-type search-type search-vals
497 (guix-get-params-for-receiving
498 buffer-type entry-type))))
499 (guix-set-buffer profile entries buffer-type entry-type
500 search-type search-vals)))
502 (defun guix-set-buffer (profile entries buffer-type entry-type search-type
503 search-vals &optional history-replace no-display)
504 "Set up BUFFER-TYPE buffer for displaying ENTRY-TYPE ENTRIES.
506 Insert ENTRIES in buffer, set variables and make history item.
507 ENTRIES should have a form of `guix-entries'.
509 See `guix-get-entries' for the meaning of SEARCH-TYPE and SEARCH-VALS.
511 If HISTORY-REPLACE is non-nil, replace current history item,
512 otherwise add the new one.
514 If NO-DISPLAY is non-nil, do not switch to the buffer."
515 (when entries
516 (let ((buf (if (and (eq major-mode
517 (guix-get-symbol "mode" buffer-type entry-type))
518 (equal guix-profile profile))
519 (current-buffer)
520 (get-buffer-create
521 (guix-buffer-name profile buffer-type
522 entry-type search-type)))))
523 (with-current-buffer buf
524 (guix-show-entries entries buffer-type entry-type)
525 (guix-set-vars profile entries buffer-type entry-type
526 search-type search-vals)
527 (funcall (if history-replace
528 #'guix-history-replace
529 #'guix-history-add)
530 (guix-make-history-item)))
531 (or no-display
532 (guix-switch-to-buffer buf))))
533 (guix-result-message profile entries entry-type
534 search-type search-vals))
536 (defun guix-show-entries (entries buffer-type entry-type)
537 "Display ENTRY-TYPE ENTRIES in the current BUFFER-TYPE buffer."
538 (let ((inhibit-read-only t))
539 (erase-buffer)
540 (funcall (symbol-function (guix-get-symbol
541 "mode" buffer-type entry-type)))
542 (funcall (guix-get-symbol "insert-entries" buffer-type)
543 entries entry-type)
544 (goto-char (point-min))))
546 (defun guix-history-call (profile entries buffer-type entry-type
547 search-type search-vals)
548 "Function called for moving by history."
549 (guix-show-entries entries buffer-type entry-type)
550 (guix-set-vars profile entries buffer-type entry-type
551 search-type search-vals)
552 (guix-result-message profile entries entry-type
553 search-type search-vals))
555 (defun guix-make-history-item ()
556 "Make and return a history item for the current buffer."
557 (list #'guix-history-call
558 guix-profile guix-entries guix-buffer-type guix-entry-type
559 guix-search-type guix-search-vals))
561 (defun guix-get-params-for-receiving (buffer-type entry-type)
562 "Return parameters that should be received for BUFFER-TYPE, ENTRY-TYPE."
563 (let* ((required-var (guix-get-symbol "required-params"
564 buffer-type entry-type))
565 (required (symbol-value required-var)))
566 (unless (equal required 'all)
567 (cl-union required
568 (funcall (guix-get-symbol "get-displayed-params"
569 buffer-type)
570 entry-type)))))
572 (defun guix-revert-buffer (_ignore-auto noconfirm)
573 "Update information in the current buffer.
574 The function is suitable for `revert-buffer-function'.
575 See `revert-buffer' for the meaning of NOCONFIRM."
576 (when (or noconfirm
577 (symbol-value
578 (guix-get-symbol "revert-no-confirm"
579 guix-buffer-type guix-entry-type))
580 (y-or-n-p "Update current information? "))
581 (let* ((search-type guix-search-type)
582 (search-vals guix-search-vals)
583 (params (guix-get-params-for-receiving guix-buffer-type
584 guix-entry-type))
585 (entries (guix-get-entries
586 guix-profile guix-entry-type
587 guix-search-type guix-search-vals params))
588 ;; If a REPL was restarted, package/output IDs are not actual
589 ;; anymore, because 'object-address'-es died with the REPL, so if a
590 ;; search by ID didn't give results, search again by name.
591 (entries (if (and (null entries)
592 (eq guix-search-type 'id)
593 (or (eq guix-entry-type 'package)
594 (eq guix-entry-type 'output)))
595 (progn
596 (setq search-type 'name
597 search-vals (guix-entries-to-specifications
598 guix-entries))
599 (guix-get-entries
600 guix-profile guix-entry-type
601 search-type search-vals params))
602 entries)))
603 (guix-set-buffer guix-profile entries guix-buffer-type guix-entry-type
604 search-type search-vals t t))))
606 (cl-defun guix-redisplay-buffer (&key buffer profile entries buffer-type
607 entry-type search-type search-vals)
608 "Redisplay a Guix BUFFER.
609 Restore the point and window positions after redisplaying if possible.
611 This function will not update the information, use
612 \"\\[revert-buffer]\" if you want the full update.
614 If BUFFER is nil, use the current buffer. For the meaning of the
615 rest arguments, see `guix-set-buffer'."
616 (interactive)
617 (or buffer (setq buffer (current-buffer)))
618 (with-current-buffer buffer
619 (or (derived-mode-p 'guix-info-mode 'guix-list-mode)
620 (error "%S is not a Guix buffer" buffer))
621 (let* ((point (point))
622 (was-at-button (button-at point))
623 ;; For simplicity, ignore an unlikely case when multiple
624 ;; windows display the same BUFFER.
625 (window (car (get-buffer-window-list buffer nil t)))
626 (window-start (and window (window-start window))))
627 (guix-set-buffer (or profile guix-profile)
628 (or entries guix-entries)
629 (or buffer-type guix-buffer-type)
630 (or entry-type guix-entry-type)
631 (or search-type guix-search-type)
632 (or search-vals guix-search-vals)
633 t t)
634 (goto-char point)
635 (and was-at-button
636 (not (button-at (point)))
637 (forward-button 1))
638 (when window
639 (set-window-point window (point))
640 (set-window-start window window-start)))))
643 ;;; Generations
645 (defcustom guix-generation-packages-buffer-name-function
646 #'guix-generation-packages-buffer-name-default
647 "Function used to define name of a buffer with generation packages.
648 This function is called with 2 arguments: PROFILE (string) and
649 GENERATION (number)."
650 :type '(choice (function-item guix-generation-packages-buffer-name-default)
651 (function-item guix-generation-packages-buffer-name-long)
652 (function :tag "Other function"))
653 :group 'guix)
655 (defcustom guix-generation-packages-update-buffer t
656 "If non-nil, always update list of packages during comparing generations.
657 If nil, generation packages are received only once. So when you
658 compare generation 1 and generation 2, the packages for both
659 generations will be received. Then if you compare generation 1
660 and generation 3, only the packages for generation 3 will be
661 received. Thus if you use comparing of different generations a
662 lot, you may set this variable to nil to improve the
663 performance."
664 :type 'boolean
665 :group 'guix)
667 (defvar guix-output-name-width 30
668 "Width of an output name \"column\".
669 This variable is used in auxiliary buffers for comparing generations.")
671 (defun guix-generation-file (profile generation)
672 "Return the file name of a PROFILE's GENERATION."
673 (format "%s-%s-link" profile generation))
675 (defun guix-manifest-file (profile &optional generation)
676 "Return the file name of a PROFILE's manifest.
677 If GENERATION number is specified, return manifest file name for
678 this generation."
679 (expand-file-name "manifest"
680 (if generation
681 (guix-generation-file profile generation)
682 profile)))
684 (defun guix-generation-packages (profile generation)
685 "Return a list of sorted packages installed in PROFILE's GENERATION.
686 Each element of the list is a list of the package specification and its path."
687 (let ((names+paths (guix-eval-read
688 (guix-make-guile-expression
689 'generation-package-specifications+paths
690 profile generation))))
691 (sort names+paths
692 (lambda (a b)
693 (string< (car a) (car b))))))
695 (defun guix-generation-packages-buffer-name-default (profile generation)
696 "Return name of a buffer for displaying GENERATION's package outputs.
697 Use base name of PROFILE path."
698 (let ((profile-name (file-name-base (directory-file-name profile))))
699 (format "*Guix %s: generation %s*"
700 profile-name generation)))
702 (defun guix-generation-packages-buffer-name-long (profile generation)
703 "Return name of a buffer for displaying GENERATION's package outputs.
704 Use the full PROFILE path."
705 (format "*Guix generation %s (%s)*"
706 generation profile))
708 (defun guix-generation-packages-buffer-name (profile generation)
709 "Return name of a buffer for displaying GENERATION's package outputs."
710 (let ((fun (if (functionp guix-generation-packages-buffer-name-function)
711 guix-generation-packages-buffer-name-function
712 #'guix-generation-packages-buffer-name-default)))
713 (funcall fun profile generation)))
715 (defun guix-generation-insert-package (name path)
716 "Insert package output NAME and PATH at point."
717 (insert name)
718 (indent-to guix-output-name-width 2)
719 (insert path "\n"))
721 (defun guix-generation-insert-packages (buffer profile generation)
722 "Insert package outputs installed in PROFILE's GENERATION in BUFFER."
723 (with-current-buffer buffer
724 (setq buffer-read-only nil
725 indent-tabs-mode nil)
726 (erase-buffer)
727 (mapc (lambda (name+path)
728 (guix-generation-insert-package
729 (car name+path) (cadr name+path)))
730 (guix-generation-packages profile generation))))
732 (defun guix-generation-packages-buffer (profile generation)
733 "Return buffer with package outputs installed in PROFILE's GENERATION.
734 Create the buffer if needed."
735 (let ((buf-name (guix-generation-packages-buffer-name
736 profile generation)))
737 (or (and (null guix-generation-packages-update-buffer)
738 (get-buffer buf-name))
739 (let ((buf (get-buffer-create buf-name)))
740 (guix-generation-insert-packages buf profile generation)
741 buf))))
743 (defun guix-profile-generation-manifest-file (generation)
744 "Return the file name of a GENERATION's manifest.
745 GENERATION is a generation number of `guix-profile' profile."
746 (guix-manifest-file guix-profile generation))
748 (defun guix-profile-generation-packages-buffer (generation)
749 "Insert GENERATION's package outputs in a buffer and return it.
750 GENERATION is a generation number of `guix-profile' profile."
751 (guix-generation-packages-buffer guix-profile generation))
754 ;;; Actions on packages and generations
756 (defface guix-operation-option-key
757 '((t :inherit font-lock-warning-face))
758 "Face used for the keys of operation options."
759 :group 'guix)
761 (defcustom guix-operation-confirm t
762 "If nil, do not prompt to confirm an operation."
763 :type 'boolean
764 :group 'guix)
766 (defcustom guix-use-substitutes t
767 "If non-nil, use substitutes for the Guix packages."
768 :type 'boolean
769 :group 'guix)
771 (defvar guix-dry-run nil
772 "If non-nil, do not perform the real actions, just simulate.")
774 (defvar guix-temp-buffer-name " *Guix temp*"
775 "Name of a buffer used for displaying info before executing operation.")
777 (defvar guix-operation-option-true-string "yes"
778 "String displayed in the mode-line when operation option is t.")
780 (defvar guix-operation-option-false-string "no "
781 "String displayed in the mode-line when operation option is nil.")
783 (defvar guix-operation-option-separator " | "
784 "String used in the mode-line to separate operation options.")
786 (defvar guix-operation-options
787 '((?s "substitutes" guix-use-substitutes)
788 (?d "dry-run" guix-dry-run))
789 "List of available operation options.
790 Each element of the list has a form:
792 (KEY NAME VARIABLE)
794 KEY is a character that may be pressed during confirmation to
795 toggle the option.
796 NAME is a string displayed in the mode-line.
797 VARIABLE is a name of an option variable.")
799 (defun guix-operation-option-by-key (key)
800 "Return operation option by KEY (character)."
801 (assq key guix-operation-options))
803 (defun guix-operation-option-key (option)
804 "Return key (character) of the operation OPTION."
805 (car option))
807 (defun guix-operation-option-name (option)
808 "Return name of the operation OPTION."
809 (nth 1 option))
811 (defun guix-operation-option-variable (option)
812 "Return name of the variable of the operation OPTION."
813 (nth 2 option))
815 (defun guix-operation-option-value (option)
816 "Return boolean value of the operation OPTION."
817 (symbol-value (guix-operation-option-variable option)))
819 (defun guix-operation-option-string-value (option)
820 "Convert boolean value of the operation OPTION to string and return it."
821 (if (guix-operation-option-value option)
822 guix-operation-option-true-string
823 guix-operation-option-false-string))
825 (defun guix-process-package-actions (profile actions
826 &optional operation-buffer)
827 "Process package ACTIONS on PROFILE.
828 Each action is a list of the form:
830 (ACTION-TYPE PACKAGE-SPEC ...)
832 ACTION-TYPE is one of the following symbols: `install',
833 `upgrade', `remove'/`delete'.
834 PACKAGE-SPEC should have the following form: (ID [OUTPUT] ...)."
835 (let (install upgrade remove)
836 (mapc (lambda (action)
837 (let ((action-type (car action))
838 (specs (cdr action)))
839 (cl-case action-type
840 (install (setq install (append install specs)))
841 (upgrade (setq upgrade (append upgrade specs)))
842 ((remove delete) (setq remove (append remove specs))))))
843 actions)
844 (when (guix-continue-package-operation-p
845 profile
846 :install install :upgrade upgrade :remove remove)
847 (guix-eval-in-repl
848 (guix-make-guile-expression
849 'process-package-actions profile
850 :install install :upgrade upgrade :remove remove
851 :use-substitutes? (or guix-use-substitutes 'f)
852 :dry-run? (or guix-dry-run 'f))
853 (and (not guix-dry-run) operation-buffer)))))
855 (cl-defun guix-continue-package-operation-p (profile
856 &key install upgrade remove)
857 "Return non-nil if a package operation should be continued.
858 Ask a user if needed (see `guix-operation-confirm').
859 INSTALL, UPGRADE, REMOVE are 'package action specifications'.
860 See `guix-process-package-actions' for details."
861 (or (null guix-operation-confirm)
862 (let* ((entries (guix-get-entries
863 profile 'package 'id
864 (append (mapcar #'car install)
865 (mapcar #'car upgrade)
866 (mapcar #'car remove))
867 '(id name version location)))
868 (install-strings (guix-get-package-strings install entries))
869 (upgrade-strings (guix-get-package-strings upgrade entries))
870 (remove-strings (guix-get-package-strings remove entries)))
871 (if (or install-strings upgrade-strings remove-strings)
872 (let ((buf (get-buffer-create guix-temp-buffer-name)))
873 (with-current-buffer buf
874 (setq-local cursor-type nil)
875 (setq buffer-read-only nil)
876 (erase-buffer)
877 (insert "Profile: " profile "\n\n")
878 (guix-insert-package-strings install-strings "install")
879 (guix-insert-package-strings upgrade-strings "upgrade")
880 (guix-insert-package-strings remove-strings "remove")
881 (let ((win (temp-buffer-window-show
883 '((display-buffer-reuse-window
884 display-buffer-at-bottom)
885 (window-height . fit-window-to-buffer)))))
886 (prog1 (guix-operation-prompt)
887 (quit-window nil win)))))
888 (message "Nothing to be done. If the REPL was restarted, information is not up-to-date.")
889 nil))))
891 (defun guix-get-package-strings (specs entries)
892 "Return short package descriptions for performing package actions.
893 See `guix-process-package-actions' for the meaning of SPECS.
894 ENTRIES is a list of package entries to get info about packages."
895 (delq nil
896 (mapcar
897 (lambda (spec)
898 (let* ((id (car spec))
899 (outputs (cdr spec))
900 (entry (guix-get-entry-by-id id entries)))
901 (when entry
902 (let ((location (guix-get-key-val entry 'location)))
903 (concat (guix-get-full-name entry)
904 (when outputs
905 (concat ":"
906 (mapconcat #'identity outputs ",")))
907 (when location
908 (concat "\t(" location ")")))))))
909 specs)))
911 (defun guix-insert-package-strings (strings action)
912 "Insert information STRINGS at point for performing package ACTION."
913 (when strings
914 (insert "Package(s) to " (propertize action 'face 'bold) ":\n")
915 (mapc (lambda (str)
916 (insert " " str "\n"))
917 strings)
918 (insert "\n")))
920 (defun guix-operation-prompt (&optional prompt)
921 "Prompt a user for continuing the current operation.
922 Return non-nil, if the operation should be continued; nil otherwise.
923 Ask a user with PROMPT for continuing an operation."
924 (let* ((option-keys (mapcar #'guix-operation-option-key
925 guix-operation-options))
926 (keys (append '(?y ?n) option-keys))
927 (prompt (concat (propertize (or prompt "Continue operation?")
928 'face 'minibuffer-prompt)
929 " ("
930 (mapconcat
931 (lambda (key)
932 (propertize (string key)
933 'face 'guix-operation-option-key))
934 keys
935 ", ")
936 ") ")))
937 (let ((mode-line mode-line-format))
938 (prog1 (guix-operation-prompt-1 prompt keys)
939 (setq mode-line-format mode-line)
940 ;; Clear the minibuffer after prompting.
941 (message "")))))
943 (defun guix-operation-prompt-1 (prompt keys)
944 "This function is internal for `guix-operation-prompt'."
945 (guix-operation-set-mode-line)
946 (let ((key (read-char-choice prompt (cons ?\C-g keys) t)))
947 (cl-case key
948 (?y t)
949 ((?n ?\C-g) nil)
950 (t (let* ((option (guix-operation-option-by-key key))
951 (var (guix-operation-option-variable option)))
952 (set var (not (symbol-value var)))
953 (guix-operation-prompt-1 prompt keys))))))
955 (defun guix-operation-set-mode-line ()
956 "Display operation options in the mode-line of the current buffer."
957 (setq mode-line-format
958 (concat (propertize " Options: "
959 'face 'mode-line-buffer-id)
960 (mapconcat
961 (lambda (option)
962 (let ((key (guix-operation-option-key option))
963 (name (guix-operation-option-name option))
964 (val (guix-operation-option-string-value option)))
965 (concat name
966 " ("
967 (propertize (string key)
968 'face 'guix-operation-option-key)
969 "): " val)))
970 guix-operation-options
971 guix-operation-option-separator)))
972 (force-mode-line-update))
974 (defun guix-delete-generations (profile generations
975 &optional operation-buffer)
976 "Delete GENERATIONS from PROFILE.
977 Each element from GENERATIONS is a generation number."
978 (when (or (not guix-operation-confirm)
979 (y-or-n-p
980 (let ((count (length generations)))
981 (if (> count 1)
982 (format "Delete %d generations from profile '%s'? "
983 count profile)
984 (format "Delete generation %d from profile '%s'? "
985 (car generations) profile)))))
986 (guix-eval-in-repl
987 (guix-make-guile-expression
988 'delete-generations* profile generations)
989 operation-buffer)))
991 (defun guix-switch-to-generation (profile generation
992 &optional operation-buffer)
993 "Switch PROFILE to GENERATION."
994 (when (or (not guix-operation-confirm)
995 (y-or-n-p (format "Switch profile '%s' to generation %d? "
996 profile generation)))
997 (guix-eval-in-repl
998 (guix-make-guile-expression
999 'switch-to-generation profile generation)
1000 operation-buffer)))
1002 (defun guix-package-source-path (package-id)
1003 "Return a store file path to a source of a package PACKAGE-ID."
1004 (message "Calculating the source derivation ...")
1005 (guix-eval-read
1006 (guix-make-guile-expression
1007 'package-source-path package-id)))
1009 (defvar guix-after-source-download-hook nil
1010 "Hook run after successful performing a 'source-download' operation.")
1012 (defun guix-package-source-build-derivation (package-id &optional prompt)
1013 "Build source derivation of a package PACKAGE-ID.
1014 Ask a user with PROMPT for continuing an operation."
1015 (when (or (not guix-operation-confirm)
1016 (guix-operation-prompt (or prompt
1017 "Build the source derivation?")))
1018 (guix-eval-in-repl
1019 (guix-make-guile-expression
1020 'package-source-build-derivation
1021 package-id
1022 :use-substitutes? (or guix-use-substitutes 'f)
1023 :dry-run? (or guix-dry-run 'f))
1024 nil 'source-download)))
1026 ;;;###autoload
1027 (defun guix-apply-manifest (profile file &optional operation-buffer)
1028 "Apply manifest from FILE to PROFILE.
1029 This function has the same meaning as 'guix package --manifest' command.
1030 See Info node `(guix) Invoking guix package' for details.
1032 Interactively, use the current profile and prompt for manifest
1033 FILE. With a prefix argument, also prompt for PROFILE."
1034 (interactive
1035 (let* ((default-profile (or guix-profile guix-current-profile))
1036 (profile (if current-prefix-arg
1037 (guix-profile-prompt)
1038 default-profile))
1039 (file (read-file-name "File with manifest: "))
1040 (buffer (and guix-profile (current-buffer))))
1041 (list profile file buffer)))
1042 (when (or (not guix-operation-confirm)
1043 (y-or-n-p (format "Apply manifest from '%s' to profile '%s'? "
1044 file profile)))
1045 (guix-eval-in-repl
1046 (guix-make-guile-expression
1047 'guix-package
1048 (concat "--profile=" profile)
1049 (concat "--manifest=" file))
1050 operation-buffer)))
1053 ;;; Pull
1055 (defcustom guix-update-after-pull t
1056 "If non-nil, update Guix buffers after performing \\[guix-pull]."
1057 :type 'boolean
1058 :group 'guix)
1060 (defvar guix-after-pull-hook
1061 '(guix-restart-repl-after-pull guix-update-buffers-maybe-after-pull)
1062 "Hook run after successful performing `guix-pull' operation.")
1064 (defun guix-restart-repl-after-pull ()
1065 "Restart Guix REPL after `guix-pull' operation."
1066 (guix-repl-exit)
1067 (guix-start-process-maybe
1068 "Restarting Guix REPL after pull operation ..."))
1070 (defun guix-update-buffers-maybe-after-pull ()
1071 "Update buffers depending on `guix-update-after-pull'."
1072 (when guix-update-after-pull
1073 (mapc #'guix-update-buffer
1074 ;; No need to update "generation" buffers.
1075 (guix-buffers '(guix-package-list-mode
1076 guix-package-info-mode
1077 guix-output-list-mode
1078 guix-output-info-mode)))
1079 (message "Guix buffers have been updated.")))
1081 ;;;###autoload
1082 (defun guix-pull (&optional verbose)
1083 "Run Guix pull operation.
1084 If VERBOSE is non-nil (with prefix argument), produce verbose output."
1085 (interactive)
1086 (let ((args (and verbose '("--verbose"))))
1087 (guix-eval-in-repl
1088 (apply #'guix-make-guile-expression 'guix-pull args)
1089 nil 'pull)))
1091 (provide 'guix-base)
1093 ;;; guix-base.el ends here