gnu: dbus: Add variant with proper helper for service activation.
[guix.git] / emacs / guix-base.el
blobe64e375e3378a974a28c67f71c0d7e13a36e1555
1 ;;; guix-base.el --- Common definitions -*- lexical-binding: t -*-
3 ;; Copyright © 2014, 2015 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-guile)
34 (require 'guix-utils)
35 (require 'guix-history)
36 (require 'guix-messages)
39 ;;; Parameters of the entries
41 (defvar guix-param-titles
42 '((package
43 (id . "ID")
44 (name . "Name")
45 (version . "Version")
46 (source . "Source")
47 (license . "License")
48 (synopsis . "Synopsis")
49 (description . "Description")
50 (home-url . "Home page")
51 (outputs . "Outputs")
52 (inputs . "Inputs")
53 (native-inputs . "Native inputs")
54 (propagated-inputs . "Propagated inputs")
55 (location . "Location")
56 (installed . "Installed"))
57 (installed
58 (path . "Installed path")
59 (dependencies . "Dependencies")
60 (output . "Output"))
61 (output
62 (id . "ID")
63 (name . "Name")
64 (version . "Version")
65 (source . "Source")
66 (license . "License")
67 (synopsis . "Synopsis")
68 (description . "Description")
69 (home-url . "Home page")
70 (output . "Output")
71 (inputs . "Inputs")
72 (native-inputs . "Native inputs")
73 (propagated-inputs . "Propagated inputs")
74 (location . "Location")
75 (installed . "Installed")
76 (path . "Installed path")
77 (dependencies . "Dependencies"))
78 (generation
79 (id . "ID")
80 (number . "Number")
81 (prev-number . "Previous number")
82 (current . "Current")
83 (path . "Path")
84 (time . "Time")))
85 "List for defining titles of entry parameters.
86 Titles are used for displaying information about entries.
87 Each element of the list has a form:
89 (ENTRY-TYPE . ((PARAM . TITLE) ...))")
91 (defun guix-get-param-title (entry-type param)
92 "Return title of an ENTRY-TYPE entry parameter PARAM."
93 (or (guix-assq-value guix-param-titles
94 entry-type param)
95 (prog1 (symbol-name param)
96 (message "Couldn't find title for '%S %S'."
97 entry-type param))))
99 (defun guix-get-name-spec (name version &optional output)
100 "Return Guix package specification by its NAME, VERSION and OUTPUT."
101 (concat name "-" version
102 (when output (concat ":" output))))
104 (defun guix-get-full-name (entry &optional output)
105 "Return name specification of the package ENTRY and OUTPUT."
106 (guix-get-name-spec (guix-assq-value entry 'name)
107 (guix-assq-value entry 'version)
108 output))
110 (defun guix-entry-to-specification (entry)
111 "Return name specification by the package or output ENTRY."
112 (guix-get-name-spec (guix-assq-value entry 'name)
113 (guix-assq-value entry 'version)
114 (guix-assq-value entry 'output)))
116 (defun guix-entries-to-specifications (entries)
117 "Return name specifications by the package or output ENTRIES."
118 (cl-remove-duplicates (mapcar #'guix-entry-to-specification entries)
119 :test #'string=))
121 (defun guix-get-installed-outputs (entry)
122 "Return list of installed outputs for the package ENTRY."
123 (mapcar (lambda (installed-entry)
124 (guix-assq-value installed-entry 'output))
125 (guix-assq-value entry 'installed)))
127 (defun guix-get-entry-by-id (id entries)
128 "Return entry from ENTRIES by entry ID."
129 (cl-find-if (lambda (entry)
130 (equal id (guix-assq-value entry 'id)))
131 entries))
133 (defun guix-get-package-id-and-output-by-output-id (oid)
134 "Return list (PACKAGE-ID OUTPUT) by output id OID."
135 (cl-multiple-value-bind (pid-str output)
136 (split-string oid ":")
137 (let ((pid (string-to-number pid-str)))
138 (list (if (= 0 pid) pid-str pid)
139 output))))
142 ;;; Location of the packages
144 (defvar guix-directory nil
145 "Default Guix directory.
146 If it is not set by a user, it is set after starting Guile REPL.
147 This directory is used to define location of the packages.")
149 (defun guix-set-directory ()
150 "Set `guix-directory' if needed."
151 (or guix-directory
152 (setq guix-directory
153 (guix-eval-read "%guix-dir"))))
155 (add-hook 'guix-after-start-repl-hook 'guix-set-directory)
157 (defun guix-find-location (location)
158 "Go to LOCATION of a package.
159 LOCATION is a string of the form:
161 \"PATH:LINE:COLUMN\"
163 If PATH is relative, it is considered to be relative to
164 `guix-directory'."
165 (cl-multiple-value-bind (path line col)
166 (split-string location ":")
167 (let ((file (expand-file-name path guix-directory))
168 (line (string-to-number line))
169 (col (string-to-number col)))
170 (find-file file)
171 (goto-char (point-min))
172 (forward-line (- line 1))
173 (move-to-column col)
174 (recenter 1))))
176 (defun guix-package-location (id-or-name)
177 "Return location of a package with ID-OR-NAME.
178 For the meaning of location, see `guix-find-location'."
179 (guix-eval-read (guix-make-guile-expression
180 'package-location-string id-or-name)))
183 ;;; Receivable lists of packages, lint checkers, etc.
185 (guix-memoized-defun guix-graph-type-names ()
186 "Return a list of names of available graph node types."
187 (guix-eval-read (guix-make-guile-expression 'graph-type-names)))
189 (guix-memoized-defun guix-lint-checker-names ()
190 "Return a list of names of available lint checkers."
191 (guix-eval-read (guix-make-guile-expression 'lint-checker-names)))
193 (guix-memoized-defun guix-package-names ()
194 "Return a list of names of available packages."
195 (sort
196 ;; Work around <https://github.com/jaor/geiser/issues/64>:
197 ;; list of strings is parsed much slower than list of lists,
198 ;; so we use 'package-names-lists' instead of 'package-names'.
200 ;; (guix-eval-read (guix-make-guile-expression 'package-names))
202 (mapcar #'car
203 (guix-eval-read (guix-make-guile-expression
204 'package-names-lists)))
205 #'string<))
208 ;;; Buffers and auto updating.
210 (defcustom guix-update-after-operation 'current
211 "Define what information to update after executing an operation.
213 After successful executing an operation in the Guix REPL (for
214 example after installing a package), information in Guix buffers
215 will or will not be automatically updated depending on a value of
216 this variable.
218 If nil, update nothing (do not revert any buffer).
219 If `current', update the buffer from which an operation was performed.
220 If `all', update all Guix buffers (not recommended)."
221 :type '(choice (const :tag "Do nothing" nil)
222 (const :tag "Update operation buffer" current)
223 (const :tag "Update all Guix buffers" all))
224 :group 'guix)
226 (defcustom guix-buffer-name-function #'guix-buffer-name-default
227 "Function used to define name of a buffer for displaying information.
228 The function is called with 4 arguments: PROFILE, BUFFER-TYPE,
229 ENTRY-TYPE, SEARCH-TYPE. See `guix-get-entries' for the meaning
230 of the arguments."
231 :type '(choice (function-item guix-buffer-name-default)
232 (function-item guix-buffer-name-simple)
233 (function :tag "Other function"))
234 :group 'guix)
236 (defun guix-buffer-name-simple (_profile buffer-type entry-type
237 &optional _search-type)
238 "Return name of a buffer used for displaying information.
239 The name is defined by `guix-ENTRY-TYPE-BUFFER-TYPE-buffer-name'
240 variable."
241 (symbol-value
242 (guix-get-symbol "buffer-name" buffer-type entry-type)))
244 (defun guix-buffer-name-default (profile buffer-type entry-type
245 &optional _search-type)
246 "Return name of a buffer used for displaying information.
247 The name is almost the same as the one defined by
248 `guix-buffer-name-simple' except the PROFILE name is added to it."
249 (let ((simple-name (guix-buffer-name-simple
250 profile buffer-type entry-type))
251 (profile-name (file-name-base (directory-file-name profile)))
252 (re (rx string-start
253 (group (? "*"))
254 (group (*? any))
255 (group (? "*"))
256 string-end)))
257 (or (string-match re simple-name)
258 (error "Unexpected error in defining guix buffer name"))
259 (let ((first* (match-string 1 simple-name))
260 (name-body (match-string 2 simple-name))
261 (last* (match-string 3 simple-name)))
262 ;; Handle the case when buffer name is wrapped by '*'.
263 (if (and (string= "*" first*)
264 (string= "*" last*))
265 (concat "*" name-body ": " profile-name "*")
266 (concat simple-name ": " profile-name)))))
268 (defun guix-buffer-name (profile buffer-type entry-type search-type)
269 "Return name of a buffer used for displaying information.
270 See `guix-buffer-name-function' for details."
271 (let ((fun (if (functionp guix-buffer-name-function)
272 guix-buffer-name-function
273 #'guix-buffer-name-default)))
274 (funcall fun profile buffer-type entry-type search-type)))
276 (defun guix-switch-to-buffer (buffer)
277 "Switch to a 'list' or 'info' BUFFER."
278 (pop-to-buffer buffer
279 '((display-buffer-reuse-window
280 display-buffer-same-window))))
282 (defun guix-buffer-p (&optional buffer modes)
283 "Return non-nil if BUFFER mode is derived from any of the MODES.
284 If BUFFER is nil, check current buffer.
285 If MODES is nil, use `guix-list-mode' and `guix-info-mode'."
286 (with-current-buffer (or buffer (current-buffer))
287 (apply #'derived-mode-p
288 (or modes
289 '(guix-list-mode guix-info-mode)))))
291 (defun guix-buffers (&optional modes)
292 "Return list of all buffers with major modes derived from MODES.
293 If MODES is nil, return list of all Guix 'list' and 'info' buffers."
294 (cl-remove-if-not (lambda (buf)
295 (guix-buffer-p buf modes))
296 (buffer-list)))
298 (defun guix-update-buffer (buffer)
299 "Update information in a 'list' or 'info' BUFFER."
300 (with-current-buffer buffer
301 (guix-revert-buffer nil t)))
303 (defun guix-update-buffers-maybe-after-operation ()
304 "Update buffers after Guix operation if needed.
305 See `guix-update-after-operation' for details."
306 (let ((to-update
307 (and guix-operation-buffer
308 (cl-case guix-update-after-operation
309 (current (and (buffer-live-p guix-operation-buffer)
310 (guix-buffer-p guix-operation-buffer)
311 (list guix-operation-buffer)))
312 (all (guix-buffers))))))
313 (setq guix-operation-buffer nil)
314 (mapc #'guix-update-buffer to-update)))
316 (add-hook 'guix-after-repl-operation-hook
317 'guix-update-buffers-maybe-after-operation)
320 ;;; Common definitions for buffer types
322 (defvar guix-root-map
323 (let ((map (make-sparse-keymap)))
324 (define-key map (kbd "l") 'guix-history-back)
325 (define-key map (kbd "r") 'guix-history-forward)
326 (define-key map (kbd "g") 'revert-buffer)
327 (define-key map (kbd "R") 'guix-redisplay-buffer)
328 (define-key map (kbd "M") 'guix-apply-manifest)
329 (define-key map (kbd "C-c C-z") 'guix-switch-to-repl)
330 map)
331 "Parent keymap for all guix modes.")
333 (defvar-local guix-profile nil
334 "Profile used for the current buffer.")
335 (put 'guix-profile 'permanent-local t)
337 (defvar-local guix-entries nil
338 "List of the currently displayed entries.
339 Each element of the list is alist with entry info of the
340 following form:
342 ((PARAM . VAL) ...)
344 PARAM is a name of the entry parameter.
345 VAL is a value of this parameter.")
346 (put 'guix-entries 'permanent-local t)
348 (defvar-local guix-buffer-type nil
349 "Type of the current buffer.")
350 (put 'guix-buffer-type 'permanent-local t)
352 (defvar-local guix-entry-type nil
353 "Type of the current entry.")
354 (put 'guix-entry-type 'permanent-local t)
356 (defvar-local guix-search-type nil
357 "Type of the current search.")
358 (put 'guix-search-type 'permanent-local t)
360 (defvar-local guix-search-vals nil
361 "Values of the current search.")
362 (put 'guix-search-vals 'permanent-local t)
364 (defsubst guix-set-vars (profile entries buffer-type entry-type
365 search-type search-vals)
366 "Set local variables for the current Guix buffer."
367 (setq default-directory profile
368 guix-profile profile
369 guix-entries entries
370 guix-buffer-type buffer-type
371 guix-entry-type entry-type
372 guix-search-type search-type
373 guix-search-vals search-vals))
375 (defun guix-get-symbol (postfix buffer-type &optional entry-type)
376 (intern (concat "guix-"
377 (when entry-type
378 (concat (symbol-name entry-type) "-"))
379 (symbol-name buffer-type) "-" postfix)))
381 (defmacro guix-define-buffer-type (buf-type entry-type &rest args)
382 "Define common for BUF-TYPE buffers for displaying ENTRY-TYPE entries.
384 In the text below TYPE means ENTRY-TYPE-BUF-TYPE.
386 This macro defines `guix-TYPE-mode', a custom group and several
387 user variables.
389 The following stuff should be defined outside this macro:
391 - `guix-BUF-TYPE-mode' - parent mode for the defined mode.
393 - `guix-TYPE-mode-initialize' (optional) - function for
394 additional mode settings; it is called without arguments.
396 Remaining argument (ARGS) should have a form [KEYWORD VALUE] ... The
397 following keywords are available:
399 - `:buffer-name' - default value for the defined
400 `guix-TYPE-buffer-name' variable.
402 - `:required' - default value for the defined
403 `guix-TYPE-required-params' variable.
405 - `:history-size' - default value for the defined
406 `guix-TYPE-history-size' variable.
408 - `:revert' - default value for the defined
409 `guix-TYPE-revert-no-confirm' variable."
410 (let* ((entry-type-str (symbol-name entry-type))
411 (buf-type-str (symbol-name buf-type))
412 (Entry-type-str (capitalize entry-type-str))
413 (Buf-type-str (capitalize buf-type-str))
414 (entry-str (concat entry-type-str " entries"))
415 (buf-str (concat buf-type-str " buffer"))
416 (prefix (concat "guix-" entry-type-str "-" buf-type-str))
417 (group (intern prefix))
418 (faces-group (intern (concat prefix "-faces")))
419 (mode-map-str (concat prefix "-mode-map"))
420 (parent-mode (intern (concat "guix-" buf-type-str "-mode")))
421 (mode (intern (concat prefix "-mode")))
422 (mode-init-fun (intern (concat prefix "-mode-initialize")))
423 (buf-name-var (intern (concat prefix "-buffer-name")))
424 (revert-var (intern (concat prefix "-revert-no-confirm")))
425 (history-var (intern (concat prefix "-history-size")))
426 (params-var (intern (concat prefix "-required-params")))
427 (buf-name-val (format "*Guix %s %s*" Entry-type-str Buf-type-str))
428 (revert-val nil)
429 (history-val 20)
430 (params-val '(id)))
432 ;; Process the keyword args.
433 (while (keywordp (car args))
434 (pcase (pop args)
435 (`:required (setq params-val (pop args)))
436 (`:history-size (setq history-val (pop args)))
437 (`:revert (setq revert-val (pop args)))
438 (`:buffer-name (setq buf-name-val (pop args)))
439 (_ (pop args))))
441 `(progn
442 (defgroup ,group nil
443 ,(concat Buf-type-str " buffer with " entry-str ".")
444 :prefix ,(concat prefix "-")
445 :group ',(intern (concat "guix-" buf-type-str)))
447 (defgroup ,faces-group nil
448 ,(concat "Faces for " buf-type-str " buffer with " entry-str ".")
449 :group ',(intern (concat "guix-" buf-type-str "-faces")))
451 (defcustom ,buf-name-var ,buf-name-val
452 ,(concat "Default name of the " buf-str " for displaying " entry-str ".")
453 :type 'string
454 :group ',group)
456 (defcustom ,history-var ,history-val
457 ,(concat "Maximum number of items saved in the history of the " buf-str ".\n"
458 "If 0, the history is disabled.")
459 :type 'integer
460 :group ',group)
462 (defcustom ,revert-var ,revert-val
463 ,(concat "If non-nil, do not ask to confirm for reverting the " buf-str ".")
464 :type 'boolean
465 :group ',group)
467 (defvar ,params-var ',params-val
468 ,(concat "List of required " entry-type-str " parameters.\n\n"
469 "Displayed parameters and parameters from this list are received\n"
470 "for each " entry-type-str ".\n\n"
471 "May be a special value `all', in which case all supported\n"
472 "parameters are received (this may be very slow for a big number\n"
473 "of entries).\n\n"
474 "Do not remove `id' from this list as it is required for\n"
475 "identifying an entry."))
477 (define-derived-mode ,mode ,parent-mode ,(concat "Guix-" Buf-type-str)
478 ,(concat "Major mode for displaying information about " entry-str ".\n\n"
479 "\\{" mode-map-str "}")
480 (setq-local revert-buffer-function 'guix-revert-buffer)
481 (setq-local guix-history-size ,history-var)
482 (and (fboundp ',mode-init-fun) (,mode-init-fun))))))
484 (put 'guix-define-buffer-type 'lisp-indent-function 'defun)
487 ;;; Getting and displaying info about packages and generations
489 (defcustom guix-package-list-type 'output
490 "Define how to display packages in a list buffer.
491 May be a symbol `package' or `output' (if `output', display each
492 output on a separate line; if `package', display each package on
493 a separate line)."
494 :type '(choice (const :tag "List of packages" package)
495 (const :tag "List of outputs" output))
496 :group 'guix)
498 (defcustom guix-package-info-type 'package
499 "Define how to display packages in an info buffer.
500 May be a symbol `package' or `output' (if `output', display each
501 output separately; if `package', display outputs inside a package
502 information)."
503 :type '(choice (const :tag "Display packages" package)
504 (const :tag "Display outputs" output))
505 :group 'guix)
507 (defun guix-get-entries (profile entry-type search-type search-vals
508 &optional params)
509 "Search for entries of ENTRY-TYPE.
511 Call an appropriate scheme function and return a list of the
512 form of `guix-entries'.
514 ENTRY-TYPE should be one of the following symbols: `package',
515 `output' or `generation'.
517 SEARCH-TYPE may be one of the following symbols:
519 - If ENTRY-TYPE is `package' or `output': `id', `name', `regexp',
520 `all-available', `newest-available', `installed', `obsolete',
521 `generation'.
523 - If ENTRY-TYPE is `generation': `id', `last', `all', `time'.
525 PARAMS is a list of parameters for receiving. If nil, get
526 information with all available parameters."
527 (guix-eval-read (guix-make-guile-expression
528 'entries
529 profile params entry-type search-type search-vals)))
531 (defun guix-get-show-entries (profile buffer-type entry-type search-type
532 &rest search-vals)
533 "Search for ENTRY-TYPE entries and show results in BUFFER-TYPE buffer.
534 See `guix-get-entries' for the meaning of SEARCH-TYPE and SEARCH-VALS."
535 (let ((entries (guix-get-entries profile entry-type search-type search-vals
536 (guix-get-params-for-receiving
537 buffer-type entry-type))))
538 (guix-set-buffer profile entries buffer-type entry-type
539 search-type search-vals)))
541 (defun guix-set-buffer (profile entries buffer-type entry-type search-type
542 search-vals &optional history-replace no-display)
543 "Set up BUFFER-TYPE buffer for displaying ENTRY-TYPE ENTRIES.
545 Insert ENTRIES in buffer, set variables and make history item.
546 ENTRIES should have a form of `guix-entries'.
548 See `guix-get-entries' for the meaning of SEARCH-TYPE and SEARCH-VALS.
550 If HISTORY-REPLACE is non-nil, replace current history item,
551 otherwise add the new one.
553 If NO-DISPLAY is non-nil, do not switch to the buffer."
554 (when entries
555 (let ((buf (if (and (eq major-mode
556 (guix-get-symbol "mode" buffer-type entry-type))
557 (equal guix-profile profile))
558 (current-buffer)
559 (get-buffer-create
560 (guix-buffer-name profile buffer-type
561 entry-type search-type)))))
562 (with-current-buffer buf
563 (guix-show-entries entries buffer-type entry-type)
564 (guix-set-vars profile entries buffer-type entry-type
565 search-type search-vals)
566 (funcall (if history-replace
567 #'guix-history-replace
568 #'guix-history-add)
569 (guix-make-history-item)))
570 (or no-display
571 (guix-switch-to-buffer buf))))
572 (guix-result-message profile entries entry-type
573 search-type search-vals))
575 (defun guix-show-entries (entries buffer-type entry-type)
576 "Display ENTRY-TYPE ENTRIES in the current BUFFER-TYPE buffer."
577 (let ((inhibit-read-only t))
578 (erase-buffer)
579 (funcall (symbol-function (guix-get-symbol
580 "mode" buffer-type entry-type)))
581 (funcall (guix-get-symbol "insert-entries" buffer-type)
582 entries entry-type)
583 (goto-char (point-min))))
585 (defun guix-history-call (profile entries buffer-type entry-type
586 search-type search-vals)
587 "Function called for moving by history."
588 (guix-show-entries entries buffer-type entry-type)
589 (guix-set-vars profile entries buffer-type entry-type
590 search-type search-vals)
591 (guix-result-message profile entries entry-type
592 search-type search-vals))
594 (defun guix-make-history-item ()
595 "Make and return a history item for the current buffer."
596 (list #'guix-history-call
597 guix-profile guix-entries guix-buffer-type guix-entry-type
598 guix-search-type guix-search-vals))
600 (defun guix-get-params-for-receiving (buffer-type entry-type)
601 "Return parameters that should be received for BUFFER-TYPE, ENTRY-TYPE."
602 (let* ((required-var (guix-get-symbol "required-params"
603 buffer-type entry-type))
604 (required (symbol-value required-var)))
605 (unless (equal required 'all)
606 (cl-union required
607 (funcall (guix-get-symbol "get-displayed-params"
608 buffer-type)
609 entry-type)))))
611 (defun guix-revert-buffer (_ignore-auto noconfirm)
612 "Update information in the current buffer.
613 The function is suitable for `revert-buffer-function'.
614 See `revert-buffer' for the meaning of NOCONFIRM."
615 (when (or noconfirm
616 (symbol-value
617 (guix-get-symbol "revert-no-confirm"
618 guix-buffer-type guix-entry-type))
619 (y-or-n-p "Update current information? "))
620 (let* ((search-type guix-search-type)
621 (search-vals guix-search-vals)
622 (params (guix-get-params-for-receiving guix-buffer-type
623 guix-entry-type))
624 (entries (guix-get-entries
625 guix-profile guix-entry-type
626 guix-search-type guix-search-vals params))
627 ;; If a REPL was restarted, package/output IDs are not actual
628 ;; anymore, because 'object-address'-es died with the REPL, so if a
629 ;; search by ID didn't give results, search again by name.
630 (entries (if (and (null entries)
631 (eq guix-search-type 'id)
632 (or (eq guix-entry-type 'package)
633 (eq guix-entry-type 'output)))
634 (progn
635 (setq search-type 'name
636 search-vals (guix-entries-to-specifications
637 guix-entries))
638 (guix-get-entries
639 guix-profile guix-entry-type
640 search-type search-vals params))
641 entries)))
642 (guix-set-buffer guix-profile entries guix-buffer-type guix-entry-type
643 search-type search-vals t t))))
645 (cl-defun guix-redisplay-buffer (&key buffer profile entries buffer-type
646 entry-type search-type search-vals)
647 "Redisplay a Guix BUFFER.
648 Restore the point and window positions after redisplaying if possible.
650 This function will not update the information, use
651 \"\\[revert-buffer]\" if you want the full update.
653 If BUFFER is nil, use the current buffer. For the meaning of the
654 rest arguments, see `guix-set-buffer'."
655 (interactive)
656 (or buffer (setq buffer (current-buffer)))
657 (with-current-buffer buffer
658 (or (derived-mode-p 'guix-info-mode 'guix-list-mode)
659 (error "%S is not a Guix buffer" buffer))
660 (let* ((point (point))
661 (was-at-button (button-at point))
662 ;; For simplicity, ignore an unlikely case when multiple
663 ;; windows display the same BUFFER.
664 (window (car (get-buffer-window-list buffer nil t)))
665 (window-start (and window (window-start window))))
666 (guix-set-buffer (or profile guix-profile)
667 (or entries guix-entries)
668 (or buffer-type guix-buffer-type)
669 (or entry-type guix-entry-type)
670 (or search-type guix-search-type)
671 (or search-vals guix-search-vals)
672 t t)
673 (goto-char point)
674 (and was-at-button
675 (not (button-at (point)))
676 (forward-button 1))
677 (when window
678 (set-window-point window (point))
679 (set-window-start window window-start)))))
682 ;;; Generations
684 (defcustom guix-generation-packages-buffer-name-function
685 #'guix-generation-packages-buffer-name-default
686 "Function used to define name of a buffer with generation packages.
687 This function is called with 2 arguments: PROFILE (string) and
688 GENERATION (number)."
689 :type '(choice (function-item guix-generation-packages-buffer-name-default)
690 (function-item guix-generation-packages-buffer-name-long)
691 (function :tag "Other function"))
692 :group 'guix)
694 (defcustom guix-generation-packages-update-buffer t
695 "If non-nil, always update list of packages during comparing generations.
696 If nil, generation packages are received only once. So when you
697 compare generation 1 and generation 2, the packages for both
698 generations will be received. Then if you compare generation 1
699 and generation 3, only the packages for generation 3 will be
700 received. Thus if you use comparing of different generations a
701 lot, you may set this variable to nil to improve the
702 performance."
703 :type 'boolean
704 :group 'guix)
706 (defvar guix-output-name-width 30
707 "Width of an output name \"column\".
708 This variable is used in auxiliary buffers for comparing generations.")
710 (defun guix-generation-file (profile generation)
711 "Return the file name of a PROFILE's GENERATION."
712 (format "%s-%s-link" profile generation))
714 (defun guix-manifest-file (profile &optional generation)
715 "Return the file name of a PROFILE's manifest.
716 If GENERATION number is specified, return manifest file name for
717 this generation."
718 (expand-file-name "manifest"
719 (if generation
720 (guix-generation-file profile generation)
721 profile)))
723 (defun guix-generation-packages (profile generation)
724 "Return a list of sorted packages installed in PROFILE's GENERATION.
725 Each element of the list is a list of the package specification and its path."
726 (let ((names+paths (guix-eval-read
727 (guix-make-guile-expression
728 'generation-package-specifications+paths
729 profile generation))))
730 (sort names+paths
731 (lambda (a b)
732 (string< (car a) (car b))))))
734 (defun guix-generation-packages-buffer-name-default (profile generation)
735 "Return name of a buffer for displaying GENERATION's package outputs.
736 Use base name of PROFILE path."
737 (let ((profile-name (file-name-base (directory-file-name profile))))
738 (format "*Guix %s: generation %s*"
739 profile-name generation)))
741 (defun guix-generation-packages-buffer-name-long (profile generation)
742 "Return name of a buffer for displaying GENERATION's package outputs.
743 Use the full PROFILE path."
744 (format "*Guix generation %s (%s)*"
745 generation profile))
747 (defun guix-generation-packages-buffer-name (profile generation)
748 "Return name of a buffer for displaying GENERATION's package outputs."
749 (let ((fun (if (functionp guix-generation-packages-buffer-name-function)
750 guix-generation-packages-buffer-name-function
751 #'guix-generation-packages-buffer-name-default)))
752 (funcall fun profile generation)))
754 (defun guix-generation-insert-package (name path)
755 "Insert package output NAME and PATH at point."
756 (insert name)
757 (indent-to guix-output-name-width 2)
758 (insert path "\n"))
760 (defun guix-generation-insert-packages (buffer profile generation)
761 "Insert package outputs installed in PROFILE's GENERATION in BUFFER."
762 (with-current-buffer buffer
763 (setq buffer-read-only nil
764 indent-tabs-mode nil)
765 (erase-buffer)
766 (mapc (lambda (name+path)
767 (guix-generation-insert-package
768 (car name+path) (cadr name+path)))
769 (guix-generation-packages profile generation))))
771 (defun guix-generation-packages-buffer (profile generation)
772 "Return buffer with package outputs installed in PROFILE's GENERATION.
773 Create the buffer if needed."
774 (let ((buf-name (guix-generation-packages-buffer-name
775 profile generation)))
776 (or (and (null guix-generation-packages-update-buffer)
777 (get-buffer buf-name))
778 (let ((buf (get-buffer-create buf-name)))
779 (guix-generation-insert-packages buf profile generation)
780 buf))))
782 (defun guix-profile-generation-manifest-file (generation)
783 "Return the file name of a GENERATION's manifest.
784 GENERATION is a generation number of `guix-profile' profile."
785 (guix-manifest-file guix-profile generation))
787 (defun guix-profile-generation-packages-buffer (generation)
788 "Insert GENERATION's package outputs in a buffer and return it.
789 GENERATION is a generation number of `guix-profile' profile."
790 (guix-generation-packages-buffer guix-profile generation))
793 ;;; Actions on packages and generations
795 (defface guix-operation-option-key
796 '((t :inherit font-lock-warning-face))
797 "Face used for the keys of operation options."
798 :group 'guix-faces)
800 (defcustom guix-operation-confirm t
801 "If nil, do not prompt to confirm an operation."
802 :type 'boolean
803 :group 'guix)
805 (defcustom guix-use-substitutes t
806 "If non-nil, use substitutes for the Guix packages."
807 :type 'boolean
808 :group 'guix)
810 (defvar guix-dry-run nil
811 "If non-nil, do not perform the real actions, just simulate.")
813 (defvar guix-temp-buffer-name " *Guix temp*"
814 "Name of a buffer used for displaying info before executing operation.")
816 (defvar guix-operation-option-true-string "yes"
817 "String displayed in the mode-line when operation option is t.")
819 (defvar guix-operation-option-false-string "no "
820 "String displayed in the mode-line when operation option is nil.")
822 (defvar guix-operation-option-separator " | "
823 "String used in the mode-line to separate operation options.")
825 (defvar guix-operation-options
826 '((?s "substitutes" guix-use-substitutes)
827 (?d "dry-run" guix-dry-run))
828 "List of available operation options.
829 Each element of the list has a form:
831 (KEY NAME VARIABLE)
833 KEY is a character that may be pressed during confirmation to
834 toggle the option.
835 NAME is a string displayed in the mode-line.
836 VARIABLE is a name of an option variable.")
838 (defun guix-operation-option-by-key (key)
839 "Return operation option by KEY (character)."
840 (assq key guix-operation-options))
842 (defun guix-operation-option-key (option)
843 "Return key (character) of the operation OPTION."
844 (car option))
846 (defun guix-operation-option-name (option)
847 "Return name of the operation OPTION."
848 (nth 1 option))
850 (defun guix-operation-option-variable (option)
851 "Return name of the variable of the operation OPTION."
852 (nth 2 option))
854 (defun guix-operation-option-value (option)
855 "Return boolean value of the operation OPTION."
856 (symbol-value (guix-operation-option-variable option)))
858 (defun guix-operation-option-string-value (option)
859 "Convert boolean value of the operation OPTION to string and return it."
860 (if (guix-operation-option-value option)
861 guix-operation-option-true-string
862 guix-operation-option-false-string))
864 (defun guix-process-package-actions (profile actions
865 &optional operation-buffer)
866 "Process package ACTIONS on PROFILE.
867 Each action is a list of the form:
869 (ACTION-TYPE PACKAGE-SPEC ...)
871 ACTION-TYPE is one of the following symbols: `install',
872 `upgrade', `remove'/`delete'.
873 PACKAGE-SPEC should have the following form: (ID [OUTPUT] ...)."
874 (let (install upgrade remove)
875 (mapc (lambda (action)
876 (let ((action-type (car action))
877 (specs (cdr action)))
878 (cl-case action-type
879 (install (setq install (append install specs)))
880 (upgrade (setq upgrade (append upgrade specs)))
881 ((remove delete) (setq remove (append remove specs))))))
882 actions)
883 (when (guix-continue-package-operation-p
884 profile
885 :install install :upgrade upgrade :remove remove)
886 (guix-eval-in-repl
887 (guix-make-guile-expression
888 'process-package-actions profile
889 :install install :upgrade upgrade :remove remove
890 :use-substitutes? (or guix-use-substitutes 'f)
891 :dry-run? (or guix-dry-run 'f))
892 (and (not guix-dry-run) operation-buffer)))))
894 (cl-defun guix-continue-package-operation-p (profile
895 &key install upgrade remove)
896 "Return non-nil if a package operation should be continued.
897 Ask a user if needed (see `guix-operation-confirm').
898 INSTALL, UPGRADE, REMOVE are 'package action specifications'.
899 See `guix-process-package-actions' for details."
900 (or (null guix-operation-confirm)
901 (let* ((entries (guix-get-entries
902 profile 'package 'id
903 (append (mapcar #'car install)
904 (mapcar #'car upgrade)
905 (mapcar #'car remove))
906 '(id name version location)))
907 (install-strings (guix-get-package-strings install entries))
908 (upgrade-strings (guix-get-package-strings upgrade entries))
909 (remove-strings (guix-get-package-strings remove entries)))
910 (if (or install-strings upgrade-strings remove-strings)
911 (let ((buf (get-buffer-create guix-temp-buffer-name)))
912 (with-current-buffer buf
913 (setq-local cursor-type nil)
914 (setq buffer-read-only nil)
915 (erase-buffer)
916 (insert "Profile: " profile "\n\n")
917 (guix-insert-package-strings install-strings "install")
918 (guix-insert-package-strings upgrade-strings "upgrade")
919 (guix-insert-package-strings remove-strings "remove")
920 (let ((win (temp-buffer-window-show
922 '((display-buffer-reuse-window
923 display-buffer-at-bottom)
924 (window-height . fit-window-to-buffer)))))
925 (prog1 (guix-operation-prompt)
926 (quit-window nil win)))))
927 (message "Nothing to be done. If the REPL was restarted, information is not up-to-date.")
928 nil))))
930 (defun guix-get-package-strings (specs entries)
931 "Return short package descriptions for performing package actions.
932 See `guix-process-package-actions' for the meaning of SPECS.
933 ENTRIES is a list of package entries to get info about packages."
934 (delq nil
935 (mapcar
936 (lambda (spec)
937 (let* ((id (car spec))
938 (outputs (cdr spec))
939 (entry (guix-get-entry-by-id id entries)))
940 (when entry
941 (let ((location (guix-assq-value entry 'location)))
942 (concat (guix-get-full-name entry)
943 (when outputs
944 (concat ":"
945 (guix-concat-strings outputs ",")))
946 (when location
947 (concat "\t(" location ")")))))))
948 specs)))
950 (defun guix-insert-package-strings (strings action)
951 "Insert information STRINGS at point for performing package ACTION."
952 (when strings
953 (insert "Package(s) to " (propertize action 'face 'bold) ":\n")
954 (mapc (lambda (str)
955 (insert " " str "\n"))
956 strings)
957 (insert "\n")))
959 (defun guix-operation-prompt (&optional prompt)
960 "Prompt a user for continuing the current operation.
961 Return non-nil, if the operation should be continued; nil otherwise.
962 Ask a user with PROMPT for continuing an operation."
963 (let* ((option-keys (mapcar #'guix-operation-option-key
964 guix-operation-options))
965 (keys (append '(?y ?n) option-keys))
966 (prompt (concat (propertize (or prompt "Continue operation?")
967 'face 'minibuffer-prompt)
968 " ("
969 (mapconcat
970 (lambda (key)
971 (propertize (string key)
972 'face 'guix-operation-option-key))
973 keys
974 ", ")
975 ") ")))
976 (let ((mode-line mode-line-format))
977 (prog1 (guix-operation-prompt-1 prompt keys)
978 (setq mode-line-format mode-line)
979 ;; Clear the minibuffer after prompting.
980 (message "")))))
982 (defun guix-operation-prompt-1 (prompt keys)
983 "This function is internal for `guix-operation-prompt'."
984 (guix-operation-set-mode-line)
985 (let ((key (read-char-choice prompt (cons ?\C-g keys) t)))
986 (cl-case key
987 (?y t)
988 ((?n ?\C-g) nil)
989 (t (let* ((option (guix-operation-option-by-key key))
990 (var (guix-operation-option-variable option)))
991 (set var (not (symbol-value var)))
992 (guix-operation-prompt-1 prompt keys))))))
994 (defun guix-operation-set-mode-line ()
995 "Display operation options in the mode-line of the current buffer."
996 (setq mode-line-format
997 (concat (propertize " Options: "
998 'face 'mode-line-buffer-id)
999 (mapconcat
1000 (lambda (option)
1001 (let ((key (guix-operation-option-key option))
1002 (name (guix-operation-option-name option))
1003 (val (guix-operation-option-string-value option)))
1004 (concat name
1005 " ("
1006 (propertize (string key)
1007 'face 'guix-operation-option-key)
1008 "): " val)))
1009 guix-operation-options
1010 guix-operation-option-separator)))
1011 (force-mode-line-update))
1013 (defun guix-delete-generations (profile generations
1014 &optional operation-buffer)
1015 "Delete GENERATIONS from PROFILE.
1016 Each element from GENERATIONS is a generation number."
1017 (when (or (not guix-operation-confirm)
1018 (y-or-n-p
1019 (let ((count (length generations)))
1020 (if (> count 1)
1021 (format "Delete %d generations from profile '%s'? "
1022 count profile)
1023 (format "Delete generation %d from profile '%s'? "
1024 (car generations) profile)))))
1025 (guix-eval-in-repl
1026 (guix-make-guile-expression
1027 'delete-generations* profile generations)
1028 operation-buffer)))
1030 (defun guix-switch-to-generation (profile generation
1031 &optional operation-buffer)
1032 "Switch PROFILE to GENERATION."
1033 (when (or (not guix-operation-confirm)
1034 (y-or-n-p (format "Switch profile '%s' to generation %d? "
1035 profile generation)))
1036 (guix-eval-in-repl
1037 (guix-make-guile-expression
1038 'switch-to-generation profile generation)
1039 operation-buffer)))
1041 (defun guix-package-source-path (package-id)
1042 "Return a store file path to a source of a package PACKAGE-ID."
1043 (message "Calculating the source derivation ...")
1044 (guix-eval-read
1045 (guix-make-guile-expression
1046 'package-source-path package-id)))
1048 (defvar guix-after-source-download-hook nil
1049 "Hook run after successful performing a 'source-download' operation.")
1051 (defun guix-package-source-build-derivation (package-id &optional prompt)
1052 "Build source derivation of a package PACKAGE-ID.
1053 Ask a user with PROMPT for continuing an operation."
1054 (when (or (not guix-operation-confirm)
1055 (guix-operation-prompt (or prompt
1056 "Build the source derivation?")))
1057 (guix-eval-in-repl
1058 (guix-make-guile-expression
1059 'package-source-build-derivation
1060 package-id
1061 :use-substitutes? (or guix-use-substitutes 'f)
1062 :dry-run? (or guix-dry-run 'f))
1063 nil 'source-download)))
1065 ;;;###autoload
1066 (defun guix-apply-manifest (profile file &optional operation-buffer)
1067 "Apply manifest from FILE to PROFILE.
1068 This function has the same meaning as 'guix package --manifest' command.
1069 See Info node `(guix) Invoking guix package' for details.
1071 Interactively, use the current profile and prompt for manifest
1072 FILE. With a prefix argument, also prompt for PROFILE."
1073 (interactive
1074 (let* ((default-profile (or guix-profile guix-current-profile))
1075 (profile (if current-prefix-arg
1076 (guix-profile-prompt)
1077 default-profile))
1078 (file (read-file-name "File with manifest: "))
1079 (buffer (and guix-profile (current-buffer))))
1080 (list profile file buffer)))
1081 (when (or (not guix-operation-confirm)
1082 (y-or-n-p (format "Apply manifest from '%s' to profile '%s'? "
1083 file profile)))
1084 (guix-eval-in-repl
1085 (guix-make-guile-expression
1086 'guix-package
1087 (concat "--profile=" profile)
1088 (concat "--manifest=" file))
1089 operation-buffer)))
1092 ;;; Executing guix commands
1094 (defcustom guix-run-in-shell-function #'guix-run-in-shell
1095 "Function used to run guix command.
1096 The function is called with a single argument - a command line string."
1097 :type '(choice (function-item guix-run-in-shell)
1098 (function-item guix-run-in-eshell)
1099 (function :tag "Other function"))
1100 :group 'guix)
1102 (defcustom guix-shell-buffer-name "*shell*"
1103 "Default name of a shell buffer used for running guix commands."
1104 :type 'string
1105 :group 'guix)
1107 (declare-function comint-send-input "comint" t)
1109 (defun guix-run-in-shell (string)
1110 "Run command line STRING in `guix-shell-buffer-name' buffer."
1111 (shell guix-shell-buffer-name)
1112 (goto-char (point-max))
1113 (insert string)
1114 (comint-send-input))
1116 (declare-function eshell-send-input "esh-mode" t)
1118 (defun guix-run-in-eshell (string)
1119 "Run command line STRING in eshell buffer."
1120 (eshell)
1121 (goto-char (point-max))
1122 (insert string)
1123 (eshell-send-input))
1125 (defun guix-run-command-in-shell (args)
1126 "Execute 'guix ARGS ...' command in a shell buffer."
1127 (funcall guix-run-in-shell-function
1128 (guix-command-string args)))
1130 (defun guix-run-command-in-repl (args)
1131 "Execute 'guix ARGS ...' command in Guix REPL."
1132 (guix-eval-in-repl
1133 (apply #'guix-make-guile-expression
1134 'guix-command args)))
1136 (defun guix-command-output (args)
1137 "Return string with 'guix ARGS ...' output."
1138 (cl-multiple-value-bind (output error)
1139 (guix-eval (apply #'guix-make-guile-expression
1140 'guix-command-output args))
1141 ;; Remove trailing new space from the error string.
1142 (message (replace-regexp-in-string "\n\\'" "" (read error)))
1143 (read output)))
1145 (defun guix-help-string (&optional commands)
1146 "Return string with 'guix COMMANDS ... --help' output."
1147 (guix-eval-read
1148 (apply #'guix-make-guile-expression
1149 'help-string commands)))
1152 ;;; Pull
1154 (defcustom guix-update-after-pull t
1155 "If non-nil, update Guix buffers after performing \\[guix-pull]."
1156 :type 'boolean
1157 :group 'guix)
1159 (defvar guix-after-pull-hook
1160 '(guix-restart-repl-after-pull guix-update-buffers-maybe-after-pull)
1161 "Hook run after successful performing `guix-pull' operation.")
1163 (defun guix-restart-repl-after-pull ()
1164 "Restart Guix REPL after `guix-pull' operation."
1165 (guix-repl-exit)
1166 (guix-start-process-maybe
1167 "Restarting Guix REPL after pull operation ..."))
1169 (defun guix-update-buffers-maybe-after-pull ()
1170 "Update buffers depending on `guix-update-after-pull'."
1171 (when guix-update-after-pull
1172 (mapc #'guix-update-buffer
1173 ;; No need to update "generation" buffers.
1174 (guix-buffers '(guix-package-list-mode
1175 guix-package-info-mode
1176 guix-output-list-mode
1177 guix-output-info-mode)))
1178 (message "Guix buffers have been updated.")))
1180 ;;;###autoload
1181 (defun guix-pull (&optional verbose)
1182 "Run Guix pull operation.
1183 If VERBOSE is non-nil (with prefix argument), produce verbose output."
1184 (interactive)
1185 (let ((args (and verbose '("--verbose"))))
1186 (guix-eval-in-repl
1187 (apply #'guix-make-guile-expression 'guix-pull args)
1188 nil 'pull)))
1190 (provide 'guix-base)
1192 ;;; guix-base.el ends here