aboutsummaryrefslogtreecommitdiff
path: root/emacs/guix-utils.el
blob: c1fe1a3a38d2e6c52785b2dcea1518578889a941 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
;;; guix-utils.el --- General utility functions

;; Copyright © 2014 Alex Kost <alezost@gmail.com>

;; This file is part of GNU Guix.

;; GNU Guix is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Guix is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; This file provides auxiliary general functions for guix.el package.

;;; Code:

;; (require 'cl-lib)

(defvar guix-true-string "Yes")
(defvar guix-false-string "–")
(defvar guix-list-separator ", ")

(defvar guix-time-format "%F %T"
  "String used to format time values.
For possible formats, see `format-time-string'.")

(defun guix-get-string (val &optional face)
  "Convert VAL into a string and return it.

VAL can be an expression of any type.
If VAL is t/nil, it is replaced with
`guix-true-string'/`guix-false-string'.
If VAL is list, its elements are concatenated using
`guix-list-separator'.

If FACE is non-nil, propertize returned string with this FACE."
  (let ((str (cond
              ((stringp val) val)
              ((null val) guix-false-string)
              ((eq t val) guix-true-string)
              ((numberp val) (number-to-string val))
              ((listp val) (mapconcat #'guix-get-string
                                      val guix-list-separator))
              (t (prin1-to-string val)))))
    (if (and val face)
        (propertize str 'face face)
      str)))

(defun guix-get-time-string (seconds)
  "Return formatted time string from SECONDS.
Use `guix-time-format'."
  (format-time-string guix-time-format (seconds-to-time seconds)))

(defun guix-get-one-line (str)
  "Return one-line string from a multi-line STR."
  (replace-regexp-in-string "\n" " " str))

(defun guix-format-insert (val &optional face format)
  "Convert VAL into a string and insert it at point.
If FACE is non-nil, propertize VAL with FACE.
If FORMAT is non-nil, format VAL with FORMAT."
  (let ((str (guix-get-string val face)))
    (insert (if format
                (format format str)
              str))))

(defun guix-mapinsert (function sequence separator)
  "Like `mapconcat' but for inserting text.
Apply FUNCTION to each element of SEQUENCE, and insert SEPARATOR
at point between each FUNCTION call."
  (when sequence
    (funcall function (car sequence))
    (mapc (lambda (obj)
            (insert separator)
            (funcall function obj))
          (cdr sequence))))

(defun guix-insert-button (label face action &optional message
                                 &rest properties)
  "Make button with LABEL and insert it at point.
Propertize button with FACE.
ACTION is a function called when the button is pressed.  It
should accept button as the argument.
MESSAGE is a button message.
See `insert-text-button' for the meaning of PROPERTIES."
  (if (null label)
      (guix-format-insert nil)
    (apply #'insert-text-button
           label
           'face face
           'action action
           'follow-link t
           'help-echo message
           properties)))

(defun guix-split-insert (val &optional face col separator)
  "Convert VAL into a string, split it and insert at point.

If FACE is non-nil, propertize returned string with this FACE.

If COL is non-nil and result string is a one-line string longer
than COL, split it into several short lines.

Separate inserted lines with SEPARATOR."
  (if (null val)
      (guix-format-insert nil)
    (let ((strings (guix-split-string (guix-get-string val) col)))
      (guix-mapinsert (lambda (str) (guix-format-insert str face))
                      strings
                      (or separator "")))))

(defun guix-split-string (str &optional col)
  "Split string STR by lines and return list of result strings.
If COL is non-nil and STR is a one-line string longer than COL,
split it into several short lines."
  (let ((strings (split-string str "\n *")))
    (if (and col
             (null (cdr strings))       ; if not multi-line
             (> (length str) col))
        (split-string (guix-get-filled-string str col) "\n")
      strings)))

(defun guix-get-filled-string (str col)
  "Return string by filling STR to column COL."
  (with-temp-buffer
    (insert str)
    (let ((fill-column col))
      (fill-region (point-min) (point-max)))
    (buffer-string)))

(defun guix-completing-read-multiple (prompt table &optional predicate
                                      require-match initial-input
                                      hist def inherit-input-method)
  "Same as `completing-read-multiple' but remove duplicates in result."
  (cl-remove-duplicates
   (completing-read-multiple prompt table predicate
                             require-match initial-input
                             hist def inherit-input-method)
   :test #'string=))

(defun guix-get-key-val (alist &rest keys)
  "Return value from ALIST by KEYS.
ALIST is alist of alists of alists ... which can be consecutively
accessed with KEYS."
  (let ((val alist))
    (dolist (key keys val)
      (setq val (cdr (assq key val))))))

(provide 'guix-utils)

;;; guix-utils.el ends here
8902910ef7c09183c1d79d51498b2c10c'>maint: Fix invalid calls to 'info'....* HACKING <Contributing>: Remove name of the manual from the item argument. * README <Installation>: Likewise. Mathieu Lirzin 2015-10-06build: Automatically determine libgcrypt's file name....* m4/guix.m4 (GUIX_LIBGCRYPT_LIBDIR): New macro. * configure.ac: Use it when no --with-libgcrypt-* option was passed. * README: Do not recommend --with-libgcrypt-prefix. Co-authored-by: 宋文武 <iyzsong@gmail.com> Ludovic Courtès 2015-06-14doc: Move most 'HACKING' informations into the manual....* HACKING (Contributing): New section. (Building from Git, The Perfect Setup, Coding Style, Submitting Patches): Move to ... * doc/guix.texi (Running Guix Before It Is Installed): Likewise. * doc/contributing.texi: ... here. New file. * doc.am (EXTRA_DIST): Use it. * README (Installation): Adapt to it. * configure.ac (DOT): Likewise. Mathieu Lirzin 2015-06-04Document 'guix environment guix' in README....* README (Installing Guix from Guix): Replace complicated instructions with 'guix environment guix'. Ludovic Courtès 2015-05-10doc: Mention GNU Make as a requirement....* README (Requirements): Add GNU Make. * doc/guix.texi (Requirements): Likewise. Ludovic Courtès 2015-05-10build: Require Guile >= 2.0.7....* configure.ac: Require guile-2.0 >= 2.0.7. * README: Adjust accordingly. * doc/guix.texi (Requirements): Likewise. Ludovic Courtès 2014-10-27doc: Add a note about optional GnuTLS dependency....* README (Requirements): add a note about 'guix download''s GnuTLS dependency. * doc/guix.texi (Requirements): Likewise. Co-authored-by: Ludovic Courtès <ludo@gnu.org> Ian Denhardt 2014-10-06doc: Mention optional dependency on Guile-JSON....* README (Requirements): Add Guile-JSON. * doc/guix.texi (Requirements): Likewise. Ludovic Courtès 2014-09-02doc: Update "Installing Guix from Guix"....* README: Make 'guix package' command line more readable. Mention --localstatedir. Ludovic Courtès 2014-08-26doc: Replace /nix/store with /gnu/store in README.... * README update anachronistic reference to /nix/store John Darrington 2014-04-10doc: Mention 'gcc-toolchain' in README....* README (Installing Guix from Guix): Use "gcc-toolchain" instead of "gcc,binutils,ld-wrapper,glibc". Ludovic Courtès 2013-11-16doc: Improve "Installing Guix from Guix" section....Reported by Mark H. Weaver <mhw@netris.org>. * README (Installing Guix from Guix): Explicitly mention $PATH separately. Mention $ACLOCAL_PATH, not $ACLOCAL. Give the exact command to install the dependencies. Remove mention of $GUIX_LD_WRAPPER_ALLOW_IMPURITIES, which is no longer needed. Ludovic Courtès 2013-07-19doc: Improve build instructions in README and HACKING....* README (Requirements): Remove Nixpkgs. Remove mentions of building from Git. (Installation): Refer to the manual. * HACKING: List requirements for when building from Git. Remove the `dot: Command not found' error. Ludovic Courtès 2013-07-17doc: Mark 2.0.5 as being the minimum Guile version....* doc/guix.texi (Requirements): Require Guile 2.0.5+. * README: Ditto. Ludovic Courtès 2013-06-12doc: Describe the build procedure in more detail....* README (Requirements): Replace "autoreconf" by "bootstrap". (Installing Guix from Guix): Augment and update. Konrad Hinsen 2013-04-25doc: Add note on installing Guix from Guix....* README (Installing Guix from Guix): New section. Suggested by Alex Sassmannshausen <alex.sassmannshausen@gmail.com>. Ludovic Courtès 2012-12-14daemon: Build `nix-setuid-helper'....* daemon.am (libexec_PROGRAMS, nix_setuid_helper_SOURCES, nix_setuid_helper_CPPFLAGS, nix_setuid_helper_LDADD): New variables. * test-env.in: Set and export `NIX_SETUID_HELPER'. * README (Installing Guix as non-root): New section. Ludovic Courtès 2012-12-06doc: Add new dependencies in `README'....* README (Hacking): Rename to... (Requirements): ... this. Add the daemon's dependencies. Ludovic Courtès 2012-11-25doc: Mention the pronunciation of "Guix"....* README: Mention pronunciation of "Guix". * doc/guix.texi (Introduction): Likewise. Ludovic Courtès 2012-11-24doc: Update `README'....* README: Update introductory summary. Point to Savannah instead of Gitorious. Mention <bug-guix@gnu.org>. (Guix & Nix): New section. Ludovic Courtès 2012-11-18doc: Add a "Related software" section to `README'....* README (Related software): New section. Ludovic Courtès 2012-11-18Turn Guix into "GNU Guix"....* configure.ac: Change package name to "GNU Guix", and bug-report address to `gnu-system-discuss@gnu.org'. * doc/guix.texi: Replace "Guix" by "GNU Guix" in some places. (Top, Introduction): Mention "for the GNU system". * HACKING, README: Use "GNU Guix" instead of "Guix" in some places. Ludovic Courtès 2012-11-03build: Require GNU libgcrypt....* guix/utils.scm (sha256): Remove Coreutils- and libchop-based implementations. * README: Update accordingly. * m4/guix.m4: New file. * configure.ac: Use `GUIX_ASSERT_LIBGCRYPT_USABLE'. Set and substitute `LIBGCRYPT_PREFIX'. * Makefile.am (AM_DISTCHECK_CONFIGURE_FLAGS): Pass `--with-libgcrypt-prefix=$(LIBGCRYPT_PREFIX)'. Ludovic Courtès 2012-11-03build: Clearly mark Nixpkgs as optional....* configure.ac: Always show the result of checking for Nixpkgs. Don't warn when Nixpkgs is not found. * Makefile.am (AM_DISTCHECK_CONFIGURE_FLAGS): Remove `--with-nixpkgs' flag. * guix/utils.scm (%nixpkgs-directory): Use either the compile-time or the run-time `NIXPKGS' environment variable. * release.nix (jobs.tarball, jobs.build): Remove `--with-nixpkgs' configure flag. * README: Mark Nixpkgs as optional. * distro/packages/databases.scm, distro/packages/guile.scm, distro/packages/typesetting.scm: Change uses of `nixpkgs-derivation*' to `nixpkgs-derivation', to avoid failing at compile-time. Ludovic Courtès 2012-10-05Augment `README'....* README (Hacking): Mention `--with-nixpkgs'. List the autotools and Gettext, as suggested by Nikita Karetnikov <nikita.karetnikov@gmail.com>. Ludovic Courtès 2012-07-01Update list of dependencies....* README: Require libgcrypt or libchop. Ludovic Courtès