Merge branch 'master' into staging
[jackhill/guix/guix.git] / gnu / installer / newt / keymap.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018 Mathieu Othacehe <m.othacehe@gmail.com>
3 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
10 ;;;
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19 (define-module (gnu installer newt keymap)
20 #:use-module (gnu installer keymap)
21 #:use-module (gnu installer steps)
22 #:use-module (gnu installer newt page)
23 #:use-module (guix i18n)
24 #:use-module (guix records)
25 #:use-module (newt)
26 #:use-module (srfi srfi-1)
27 #:use-module (srfi srfi-26)
28 #:use-module (srfi srfi-34)
29 #:use-module (srfi srfi-35)
30 #:export (run-keymap-page))
31
32 (define (run-layout-page layouts layout->text)
33 (let ((title (G_ "Layout")))
34 (run-listbox-selection-page
35 #:title title
36 #:info-text (G_ "Please choose your keyboard layout.")
37 #:listbox-items layouts
38 #:listbox-item->text layout->text
39 #:sort-listbox-items? #f
40 #:button-text (G_ "Exit")
41 #:button-callback-procedure
42 (lambda _
43 (raise
44 (condition
45 (&installer-step-abort)))))))
46
47 (define (run-variant-page variants variant->text)
48 (let ((title (G_ "Variant")))
49 (run-listbox-selection-page
50 #:title title
51 #:info-text (G_ "Please choose a variant for your keyboard layout.")
52 #:listbox-items variants
53 #:listbox-item->text variant->text
54 #:sort-listbox-items? #f
55 #:button-text (G_ "Back")
56 #:button-callback-procedure
57 (lambda _
58 (raise
59 (condition
60 (&installer-step-abort)))))))
61
62 (define (sort-layouts layouts)
63 "Sort LAYOUTS list by putting the US layout ahead and return it."
64 (call-with-values
65 (lambda ()
66 (partition
67 (lambda (layout)
68 (let ((name (x11-keymap-layout-name layout)))
69 (string=? name "us")))
70 layouts))
71 (cut append <> <>)))
72
73 (define (sort-variants variants)
74 "Sort VARIANTS list by putting the international variant ahead and return it."
75 (call-with-values
76 (lambda ()
77 (partition
78 (lambda (variant)
79 (let ((name (x11-keymap-variant-name variant)))
80 (string=? name "altgr-intl")))
81 variants))
82 (cut append <> <>)))
83
84 (define* (run-keymap-page layouts)
85 "Run a page asking the user to select a keyboard layout and variant. LAYOUTS
86 is a list of supported X11-KEYMAP-LAYOUT. Return a list of two elements, the
87 names of the selected keyboard layout and variant."
88 (define keymap-steps
89 (list
90 (installer-step
91 (id 'layout)
92 (compute
93 (lambda _
94 (run-layout-page
95 (sort-layouts layouts)
96 (lambda (layout)
97 (x11-keymap-layout-description layout))))))
98 ;; Propose the user to select a variant among those supported by the
99 ;; previously selected layout.
100 (installer-step
101 (id 'variant)
102 (compute
103 (lambda (result _)
104 (let* ((layout (result-step result 'layout))
105 (variants (x11-keymap-layout-variants layout)))
106 ;; Return #f if the layout does not have any variant.
107 (and (not (null? variants))
108 (run-variant-page
109 (sort-variants variants)
110 (lambda (variant)
111 (x11-keymap-variant-description
112 variant))))))))))
113
114 (define (format-result result)
115 (let ((layout (x11-keymap-layout-name
116 (result-step result 'layout)))
117 (variant (and=> (result-step result 'variant)
118 (lambda (variant)
119 (x11-keymap-variant-name variant)))))
120 (list layout (or variant ""))))
121 (format-result
122 (run-installer-steps #:steps keymap-steps)))