gnu: youtube-dl: Update to 2014.12.15.
[jackhill/guix/guix.git] / emacs / guix-base.el
1 ;;; guix-base.el --- Common definitions -*- lexical-binding: t -*-
2
3 ;; Copyright © 2014 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 some base and common definitions for guix.el
23 ;; package.
24
25 ;; List and info buffers have many common patterns that are defined
26 ;; using `guix-define-buffer-type' macro from this file.
27
28 ;;; Code:
29
30 (require 'cl-lib)
31 (require 'guix-profiles)
32 (require 'guix-backend)
33 (require 'guix-utils)
34 (require 'guix-history)
35 (require 'guix-messages)
36
37 \f
38 ;;; Parameters of the entries
39
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:
87
88 (ENTRY-TYPE . ((PARAM . TITLE) ...))")
89
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))))
97
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))))
102
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))
108
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)))
114
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=))
119
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)))
125
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))
131
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))))
139
140 \f
141 ;;; Location of the packages
142
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.")
147
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"))))
153
154 (add-hook 'guix-after-start-repl-hook 'guix-set-directory)
155
156 (defun guix-find-location (location)
157 "Go to LOCATION of a package.
158 LOCATION is a string of the form:
159
160 \"PATH:LINE:COLUMN\"
161
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))))
174
175 \f
176 ;;; Buffers and auto updating.
177
178 (defcustom guix-update-after-operation 'current
179 "Define what information to update after executing an operation.
180
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.
185
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)
193
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)
203
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)))
211
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)))))
235
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)))
243
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))))
249
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)))))
258
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)))
265
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)))
270
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)))
283
284 (add-hook 'guix-after-repl-operation-hook
285 'guix-update-buffers-maybe-after-operation)
286
287 \f
288 ;;; Common definitions for buffer types
289
290 (defvar-local guix-profile nil
291 "Profile used for the current buffer.")
292 (put 'guix-profile 'permanent-local t)
293
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:
298
299 ((PARAM . VAL) ...)
300
301 PARAM is a name of the entry parameter.
302 VAL is a value of this parameter.")
303 (put 'guix-entries 'permanent-local t)
304
305 (defvar-local guix-buffer-type nil
306 "Type of the current buffer.")
307 (put 'guix-buffer-type 'permanent-local t)
308
309 (defvar-local guix-entry-type nil
310 "Type of the current entry.")
311 (put 'guix-entry-type 'permanent-local t)
312
313 (defvar-local guix-search-type nil
314 "Type of the current search.")
315 (put 'guix-search-type 'permanent-local t)
316
317 (defvar-local guix-search-vals nil
318 "Values of the current search.")
319 (put 'guix-search-vals 'permanent-local t)
320
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))
331
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)))
337
338 (defmacro guix-define-buffer-type (buf-type entry-type &rest args)
339 "Define common for BUF-TYPE buffers for displaying ENTRY-TYPE entries.
340
341 In the text below TYPE means ENTRY-TYPE-BUF-TYPE.
342
343 This macro defines `guix-TYPE-mode', a custom group and several
344 user variables.
345
346 The following stuff should be defined outside this macro:
347
348 - `guix-BUF-TYPE-mode' - parent mode for the defined mode.
349
350 - `guix-TYPE-mode-initialize' (optional) - function for
351 additional mode settings; it is called without arguments.
352
353 Remaining argument (ARGS) should have a form [KEYWORD VALUE] ... The
354 following keywords are available:
355
356 - `:buffer-name' - default value for the defined
357 `guix-TYPE-buffer-name' variable.
358
359 - `:required' - default value for the defined
360 `guix-TYPE-required-params' variable.
361
362 - `:history-size' - default value for the defined
363 `guix-TYPE-history-size' variable.
364
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)))
388
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))))
397
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)))
403
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)
408
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)
414
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)
419
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."))
429
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)))
436
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)))))
443
444 (put 'guix-define-buffer-type 'lisp-indent-function 'defun)
445
446 \f
447 ;;; Getting and displaying info about packages and generations
448
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
453 a separate line)."
454 :type '(choice (const :tag "List of packages" package)
455 (const :tag "List of outputs" output))
456 :group 'guix)
457
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
462 information)."
463 :type '(choice (const :tag "Display packages" package)
464 (const :tag "Display outputs" output))
465 :group 'guix)
466
467 (defun guix-get-entries (profile entry-type search-type search-vals
468 &optional params)
469 "Search for entries of ENTRY-TYPE.
470
471 Call an appropriate scheme function and return a list of the
472 form of `guix-entries'.
473
474 ENTRY-TYPE should be one of the following symbols: `package',
475 `output' or `generation'.
476
477 SEARCH-TYPE may be one of the following symbols:
478
479 - If ENTRY-TYPE is `package' or `output': `id', `name', `regexp',
480 `all-available', `newest-available', `installed', `obsolete',
481 `generation'.
482
483 - If ENTRY-TYPE is `generation': `id', `last', `all', `time'.
484
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
488 'entries
489 profile params entry-type search-type search-vals)))
490
491 (defun guix-get-show-entries (profile buffer-type entry-type search-type
492 &rest search-vals)
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)))
500
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.
504
505 Insert ENTRIES in buffer, set variables and make history item.
506 ENTRIES should have a form of `guix-entries'.
507
508 See `guix-get-entries' for the meaning of SEARCH-TYPE and SEARCH-VALS.
509
510 If HISTORY-REPLACE is non-nil, replace current history item,
511 otherwise add the new one.
512
513 If NO-DISPLAY is non-nil, do not switch to the buffer."
514 (when entries
515 (let ((buf (if (and (eq major-mode
516 (guix-get-symbol "mode" buffer-type entry-type))
517 (equal guix-profile profile))
518 (current-buffer)
519 (get-buffer-create
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
528 #'guix-history-add)
529 (guix-make-history-item)))
530 (or no-display
531 (guix-switch-to-buffer buf))))
532 (guix-result-message profile entries entry-type
533 search-type search-vals))
534
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))
538 (erase-buffer)
539 (funcall (symbol-function (guix-get-symbol
540 "mode" buffer-type entry-type)))
541 (funcall (guix-get-symbol "insert-entries" buffer-type)
542 entries entry-type)
543 (goto-char (point-min))))
544
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))
553
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))
559
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)
566 (cl-union required
567 (funcall (guix-get-symbol "get-displayed-params"
568 buffer-type)
569 entry-type)))))
570
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."
575 (when (or noconfirm
576 (symbol-value
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
583 guix-entry-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)))
594 (progn
595 (setq search-type 'name
596 search-vals (guix-entries-to-specifications
597 guix-entries))
598 (guix-get-entries
599 guix-profile guix-entry-type
600 search-type search-vals params))
601 entries)))
602 (guix-set-buffer guix-profile entries guix-buffer-type guix-entry-type
603 search-type search-vals t t))))
604
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.
609
610 This function will not update the information, use
611 \"\\[revert-buffer]\" if you want the full update.
612
613 If BUFFER is nil, use the current buffer. For the meaning of the
614 rest arguments, see `guix-set-buffer'."
615 (interactive)
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)
632 t t)
633 (goto-char point)
634 (and was-at-button
635 (not (button-at (point)))
636 (forward-button 1))
637 (when window
638 (set-window-point window (point))
639 (set-window-start window window-start)))))
640
641 \f
642 ;;; Generations
643
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"))
652 :group 'guix)
653
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
662 performance."
663 :type 'boolean
664 :group 'guix)
665
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.")
669
670 (defun guix-generation-file (profile generation)
671 "Return the file name of a PROFILE's GENERATION."
672 (format "%s-%s-link" profile generation))
673
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
677 this generation."
678 (expand-file-name "manifest"
679 (if generation
680 (guix-generation-file profile generation)
681 profile)))
682
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))))
690 (sort names+paths
691 (lambda (a b)
692 (string< (car a) (car b))))))
693
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)))
700
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)*"
705 generation profile))
706
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)))
713
714 (defun guix-generation-insert-package (name path)
715 "Insert package output NAME and PATH at point."
716 (insert name)
717 (indent-to guix-output-name-width 2)
718 (insert path "\n"))
719
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)
725 (erase-buffer)
726 (mapc (lambda (name+path)
727 (guix-generation-insert-package
728 (car name+path) (cadr name+path)))
729 (guix-generation-packages profile generation))))
730
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)
740 buf))))
741
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))
746
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))
751
752 \f
753 ;;; Actions on packages and generations
754
755 (defface guix-operation-option-key
756 '((t :inherit font-lock-warning-face))
757 "Face used for the keys of operation options."
758 :group 'guix)
759
760 (defcustom guix-operation-confirm t
761 "If nil, do not prompt to confirm an operation."
762 :type 'boolean
763 :group 'guix)
764
765 (defcustom guix-use-substitutes t
766 "If non-nil, use substitutes for the Guix packages."
767 :type 'boolean
768 :group 'guix)
769
770 (defvar guix-dry-run nil
771 "If non-nil, do not perform the real actions, just simulate.")
772
773 (defvar guix-temp-buffer-name " *Guix temp*"
774 "Name of a buffer used for displaying info before executing operation.")
775
776 (defvar guix-operation-option-true-string "yes"
777 "String displayed in the mode-line when operation option is t.")
778
779 (defvar guix-operation-option-false-string "no "
780 "String displayed in the mode-line when operation option is nil.")
781
782 (defvar guix-operation-option-separator " | "
783 "String used in the mode-line to separate operation options.")
784
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:
790
791 (KEY NAME VARIABLE)
792
793 KEY is a character that may be pressed during confirmation to
794 toggle the option.
795 NAME is a string displayed in the mode-line.
796 VARIABLE is a name of an option variable.")
797
798 (defun guix-operation-option-by-key (key)
799 "Return operation option by KEY (character)."
800 (assq key guix-operation-options))
801
802 (defun guix-operation-option-key (option)
803 "Return key (character) of the operation OPTION."
804 (car option))
805
806 (defun guix-operation-option-name (option)
807 "Return name of the operation OPTION."
808 (nth 1 option))
809
810 (defun guix-operation-option-variable (option)
811 "Return name of the variable of the operation OPTION."
812 (nth 2 option))
813
814 (defun guix-operation-option-value (option)
815 "Return boolean value of the operation OPTION."
816 (symbol-value (guix-operation-option-variable option)))
817
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))
823
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:
828
829 (ACTION-TYPE PACKAGE-SPEC ...)
830
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)))
838 (cl-case action-type
839 (install (setq install (append install specs)))
840 (upgrade (setq upgrade (append upgrade specs)))
841 ((remove delete) (setq remove (append remove specs))))))
842 actions)
843 (when (guix-continue-package-operation-p
844 profile
845 :install install :upgrade upgrade :remove remove)
846 (guix-eval-in-repl
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)))))
853
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
862 profile 'package 'id
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)
875 (erase-buffer)
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
881 buf
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.")
888 nil))))
889
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."
894 (delq nil
895 (mapcar
896 (lambda (spec)
897 (let* ((id (car spec))
898 (outputs (cdr spec))
899 (entry (guix-get-entry-by-id id entries)))
900 (when entry
901 (let ((location (guix-get-key-val entry 'location)))
902 (concat (guix-get-full-name entry)
903 (when outputs
904 (concat ":"
905 (mapconcat #'identity outputs ",")))
906 (when location
907 (concat "\t(" location ")")))))))
908 specs)))
909
910 (defun guix-insert-package-strings (strings action)
911 "Insert information STRINGS at point for performing package ACTION."
912 (when strings
913 (insert "Package(s) to " (propertize action 'face 'bold) ":\n")
914 (mapc (lambda (str)
915 (insert " " str "\n"))
916 strings)
917 (insert "\n")))
918
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)
928 " ("
929 (mapconcat
930 (lambda (key)
931 (propertize (string key)
932 'face 'guix-operation-option-key))
933 keys
934 ", ")
935 ") ")))
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.
940 (message "")))))
941
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)))
946 (cl-case key
947 (?y t)
948 ((?n ?\C-g) nil)
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))))))
953
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)
959 (mapconcat
960 (lambda (option)
961 (let ((key (guix-operation-option-key option))
962 (name (guix-operation-option-name option))
963 (val (guix-operation-option-string-value option)))
964 (concat name
965 " ("
966 (propertize (string key)
967 'face 'guix-operation-option-key)
968 "): " val)))
969 guix-operation-options
970 guix-operation-option-separator)))
971 (force-mode-line-update))
972
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)
978 (y-or-n-p
979 (let ((count (length generations)))
980 (if (> count 1)
981 (format "Delete %d generations from profile '%s'? "
982 count profile)
983 (format "Delete generation %d from profile '%s'? "
984 (car generations) profile)))))
985 (guix-eval-in-repl
986 (guix-make-guile-expression
987 'delete-generations* profile generations)
988 operation-buffer)))
989
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)))
996 (guix-eval-in-repl
997 (guix-make-guile-expression
998 'switch-to-generation profile generation)
999 operation-buffer)))
1000
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 ...")
1004 (guix-eval-read
1005 (guix-make-guile-expression
1006 'package-source-path package-id)))
1007
1008 (defvar guix-after-source-download-hook nil
1009 "Hook run after successful performing a 'source-download' operation.")
1010
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?")))
1017 (guix-eval-in-repl
1018 (guix-make-guile-expression
1019 'package-source-build-derivation
1020 package-id
1021 :use-substitutes? (or guix-use-substitutes 'f)
1022 :dry-run? (or guix-dry-run 'f))
1023 nil 'source-download)))
1024
1025 \f
1026 ;;; Pull
1027
1028 (defcustom guix-update-after-pull t
1029 "If non-nil, update Guix buffers after performing \\[guix-pull]."
1030 :type 'boolean
1031 :group 'guix)
1032
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.")
1036
1037 (defun guix-restart-repl-after-pull ()
1038 "Restart Guix REPL after `guix-pull' operation."
1039 (guix-repl-exit)
1040 (guix-start-process-maybe
1041 "Restarting Guix REPL after pull operation ..."))
1042
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.")))
1053
1054 ;;;###autoload
1055 (defun guix-pull (&optional verbose)
1056 "Run Guix pull operation.
1057 If VERBOSE is non-nil (with prefix argument), produce verbose output."
1058 (interactive)
1059 (let ((args (and verbose '("--verbose"))))
1060 (guix-eval-in-repl
1061 (apply #'guix-make-guile-expression 'guix-pull args)
1062 nil 'pull)))
1063
1064 (provide 'guix-base)
1065
1066 ;;; guix-base.el ends here