1 ;;; guix-read.el --- Minibuffer readers
3 ;; Copyright © 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/>.
22 ;; This file provides functions to prompt a user for packages, system
23 ;; types, hash formats and other guix related stuff.
27 (require 'guix-help-vars
)
31 (defun guix-read-file-name (prompt &optional dir default-filename
32 mustmatch initial predicate
)
34 This function is similar to `read-file-name' except it also
35 expands the file name."
36 (expand-file-name (read-file-name prompt dir default-filename
37 mustmatch initial predicate
)))
39 (defmacro guix-define-reader
(name read-fun completions prompt
)
40 "Define NAME function to read from minibuffer.
41 READ-FUN may be `completing-read', `completing-read-multiple' or
42 another function with the same arguments."
43 `(defun ,name
(&optional prompt initial-contents
)
44 (,read-fun
,(if prompt
47 ,completions nil nil initial-contents
)))
49 (defmacro guix-define-readers
(&rest args
)
50 "Define reader functions.
52 ARGS should have a form [KEYWORD VALUE] ... The following
53 keywords are available:
55 - `completions-var' - variable used to get completions.
57 - `completions-getter' - function used to get completions.
59 - `single-reader', `single-prompt' - name of a function to read
60 a single value, and a prompt for it.
62 - `multiple-reader', `multiple-prompt' - name of a function to
63 read multiple values, and a prompt for it.
65 - `multiple-separator' - if specified, another
66 `<multiple-reader-name>-string' function returning a string
67 of multiple values separated the specified separator will be
77 ;; Process the keyword args.
78 (while (keywordp (car args
))
80 (`:completions-var
(setq completions-var
(pop args
)))
81 (`:completions-getter
(setq completions-getter
(pop args
)))
82 (`:single-reader
(setq single-reader
(pop args
)))
83 (`:single-prompt
(setq single-prompt
(pop args
)))
84 (`:multiple-reader
(setq multiple-reader
(pop args
)))
85 (`:multiple-prompt
(setq multiple-prompt
(pop args
)))
86 (`:multiple-separator
(setq multiple-separator
(pop args
)))
90 (cond ((and completions-var completions-getter
)
92 (setq ,completions-var
93 (funcall ',completions-getter
))))
97 `(funcall ',completions-getter
)))))
99 ,(when (and completions-var
100 (not (boundp completions-var
)))
101 `(defvar ,completions-var nil
))
104 `(guix-define-reader ,single-reader completing-read
105 ,completions
,single-prompt
))
107 ,(when multiple-reader
108 `(guix-define-reader ,multiple-reader completing-read-multiple
109 ,completions
,multiple-prompt
))
111 ,(when (and multiple-reader multiple-separator
)
112 (let ((name (intern (concat (symbol-name multiple-reader
)
114 `(defun ,name
(&optional prompt initial-contents
)
116 (,multiple-reader prompt initial-contents
)
117 ,multiple-separator
))))))))
120 :completions-var guix-help-system-types
121 :single-reader guix-read-system-type
122 :single-prompt
"System type: ")
125 :completions-var guix-help-source-types
126 :single-reader guix-read-source-type
127 :single-prompt
"Source type: ")
130 :completions-var guix-help-hash-formats
131 :single-reader guix-read-hash-format
132 :single-prompt
"Hash format: ")
135 :completions-var guix-help-refresh-subsets
136 :single-reader guix-read-refresh-subset
137 :single-prompt
"Refresh subset: ")
140 :completions-var guix-help-key-policies
141 :single-reader guix-read-key-policy
142 :single-prompt
"Key policy: ")
145 :completions-var guix-help-elpa-archives
146 :single-reader guix-read-elpa-archive
147 :single-prompt
"ELPA archive: ")
150 :completions-var guix-help-verify-options
151 :multiple-reader guix-read-verify-options
152 :multiple-prompt
"Verify option,s: "
153 :multiple-separator
",")
156 :completions-getter guix-graph-type-names
157 :single-reader guix-read-graph-type
158 :single-prompt
"Graph node type: ")
161 :completions-getter guix-lint-checker-names
162 :multiple-reader guix-read-lint-checker-names
163 :multiple-prompt
"Linter,s: "
164 :multiple-separator
",")
167 :completions-getter guix-package-names
168 :single-reader guix-read-package-name
169 :single-prompt
"Package: "
170 :multiple-reader guix-read-package-names
171 :multiple-prompt
"Package,s: "
172 :multiple-separator
" ")
176 ;;; guix-read.el ends here