1 ;;; guix-utils.el --- General utility functions
3 ;; Copyright © 2014 Alex Kost <alezost@gmail.com>
5 ;; This file is part of GNU Guix.
7 ;; GNU Guix is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
12 ;; GNU Guix is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
22 ;; This file provides auxiliary general functions for guix.el package.
28 (defvar guix-true-string
"Yes")
29 (defvar guix-false-string
"–")
30 (defvar guix-list-separator
", ")
32 (defvar guix-time-format
"%F %T"
33 "String used to format time values.
34 For possible formats, see `format-time-string'.")
36 (defun guix-get-string (val &optional face
)
37 "Convert VAL into a string and return it.
39 VAL can be an expression of any type.
40 If VAL is t/nil, it is replaced with
41 `guix-true-string'/`guix-false-string'.
42 If VAL is list, its elements are concatenated using
43 `guix-list-separator'.
45 If FACE is non-nil, propertize returned string with this FACE."
48 ((null val
) guix-false-string
)
49 ((eq t val
) guix-true-string
)
50 ((numberp val
) (number-to-string val
))
51 ((listp val
) (mapconcat #'guix-get-string
52 val guix-list-separator
))
53 (t (prin1-to-string val
)))))
55 (propertize str
'font-lock-face face
)
58 (defun guix-get-time-string (seconds)
59 "Return formatted time string from SECONDS.
60 Use `guix-time-format'."
61 (format-time-string guix-time-format
(seconds-to-time seconds
)))
63 (defun guix-get-one-line (str)
64 "Return one-line string from a multi-line STR."
65 (replace-regexp-in-string "\n" " " str
))
67 (defun guix-format-insert (val &optional face format
)
68 "Convert VAL into a string and insert it at point.
69 If FACE is non-nil, propertize VAL with FACE.
70 If FORMAT is non-nil, format VAL with FORMAT."
71 (let ((str (guix-get-string val face
)))
76 (defun guix-mapinsert (function sequence separator
)
77 "Like `mapconcat' but for inserting text.
78 Apply FUNCTION to each element of SEQUENCE, and insert SEPARATOR
79 at point between each FUNCTION call."
81 (funcall function
(car sequence
))
84 (funcall function obj
))
87 (defun guix-insert-button (label &optional type
&rest properties
)
88 "Make button of TYPE with LABEL and insert it at point.
89 See `insert-text-button' for the meaning of PROPERTIES."
91 (guix-format-insert nil
)
92 (apply #'insert-text-button label
93 :type
(or type
'button
)
96 (defun guix-split-insert (val &optional face col separator
)
97 "Convert VAL into a string, split it and insert at point.
99 If FACE is non-nil, propertize returned string with this FACE.
101 If COL is non-nil and result string is a one-line string longer
102 than COL, split it into several short lines.
104 Separate inserted lines with SEPARATOR."
106 (guix-format-insert nil
)
107 (let ((strings (guix-split-string (guix-get-string val
) col
)))
108 (guix-mapinsert (lambda (str) (guix-format-insert str face
))
110 (or separator
"")))))
112 (defun guix-split-string (str &optional col
)
113 "Split string STR by lines and return list of result strings.
114 If COL is non-nil and STR is a one-line string longer than COL,
115 split it into several short lines."
116 (let ((strings (split-string str
"\n *")))
118 (null (cdr strings
)) ; if not multi-line
119 (> (length str
) col
))
120 (split-string (guix-get-filled-string str col
) "\n")
123 (defun guix-get-filled-string (str col
)
124 "Return string by filling STR to column COL."
127 (let ((fill-column col
))
128 (fill-region (point-min) (point-max)))
131 (defun guix-completing-read-multiple (prompt table
&optional predicate
132 require-match initial-input
133 hist def inherit-input-method
)
134 "Same as `completing-read-multiple' but remove duplicates in result."
135 (cl-remove-duplicates
136 (completing-read-multiple prompt table predicate
137 require-match initial-input
138 hist def inherit-input-method
)
141 (declare-function org-read-date
"org" t
)
143 (defun guix-read-date (prompt)
144 "Prompt for a date or time using `org-read-date'.
147 (org-read-date nil t nil prompt
))
149 (defun guix-get-key-val (alist &rest keys
)
150 "Return value from ALIST by KEYS.
151 ALIST is alist of alists of alists ... which can be consecutively
154 (dolist (key keys val
)
155 (setq val
(cdr (assq key val
))))))
157 (defun guix-find-file (file)
158 "Find FILE if it exists."
159 (if (file-exists-p file
)
161 (message "File '%s' does not exist." file
)))
166 (defvar guix-diff-switches
"-u"
167 "A string or list of strings specifying switches to be passed to diff.")
169 (defun guix-diff (old new
&optional switches no-async
)
170 "Same as `diff', but use `guix-diff-switches' as default."
171 (diff old new
(or switches guix-diff-switches
) no-async
))
173 (provide 'guix-utils
)
175 ;;; guix-utils.el ends here