Merge branch 'master' into staging
[jackhill/guix/guix.git] / gnu / installer / newt / keymap.scm
CommitLineData
d0f3a672
MO
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2018 Mathieu Othacehe <m.othacehe@gmail.com>
3191b5f6 3;;; Copyright © 2019 Ludovic Courtès <ludo@gnu.org>
d0f3a672
MO
4;;;
5;;; This file is part of GNU Guix.
6;;;
7;;; GNU Guix is free software; you can redistribute it and/or modify it
8;;; under the terms of the GNU General Public License as published by
9;;; the Free Software Foundation; either version 3 of the License, or (at
10;;; your option) any later version.
11;;;
12;;; GNU Guix is distributed in the hope that it will be useful, but
13;;; 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.
16;;;
17;;; You should have received a copy of the GNU General Public License
18;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19
20(define-module (gnu installer newt keymap)
21 #:use-module (gnu installer keymap)
22 #:use-module (gnu installer steps)
23 #:use-module (gnu installer newt page)
24 #:use-module (guix i18n)
25 #:use-module (guix records)
26 #:use-module (newt)
27 #:use-module (srfi srfi-1)
cb614af0 28 #:use-module (srfi srfi-26)
d0f3a672
MO
29 #:use-module (srfi srfi-34)
30 #:use-module (srfi srfi-35)
9015e639 31 #:use-module (ice-9 i18n)
3191b5f6
LC
32 #:use-module (ice-9 match)
33 #:export (run-keymap-page
34 keyboard-layout->configuration))
d0f3a672
MO
35
36(define (run-layout-page layouts layout->text)
5cdb6bd2 37 (let ((title (G_ "Layout")))
d0f3a672
MO
38 (run-listbox-selection-page
39 #:title title
40 #:info-text (G_ "Please choose your keyboard layout.")
41 #:listbox-items layouts
42 #:listbox-item->text layout->text
cb614af0 43 #:sort-listbox-items? #f
7d812901 44 #:button-text (G_ "Exit")
d0f3a672
MO
45 #:button-callback-procedure
46 (lambda _
47 (raise
48 (condition
49 (&installer-step-abort)))))))
50
51(define (run-variant-page variants variant->text)
5cdb6bd2 52 (let ((title (G_ "Variant")))
d0f3a672
MO
53 (run-listbox-selection-page
54 #:title title
55 #:info-text (G_ "Please choose a variant for your keyboard layout.")
56 #:listbox-items variants
57 #:listbox-item->text variant->text
cb614af0 58 #:sort-listbox-items? #f
d0f3a672
MO
59 #:button-text (G_ "Back")
60 #:button-callback-procedure
61 (lambda _
62 (raise
63 (condition
64 (&installer-step-abort)))))))
65
cb614af0
MO
66(define (sort-layouts layouts)
67 "Sort LAYOUTS list by putting the US layout ahead and return it."
9015e639
LC
68 (define (layout<? layout1 layout2)
69 (let ((text1 (x11-keymap-layout-description layout1))
70 (text2 (x11-keymap-layout-description layout2)))
71 ;; XXX: We're calling 'gettext' more than once per item.
72 (string-locale<? (gettext text1 "xkeyboard-config")
73 (gettext text2 "xkeyboard-config"))))
74
75 (define preferred
76 ;; Two-letter language tag for the preferred keyboard layout.
77 (or (getenv "LANGUAGE") "us"))
78
cb614af0
MO
79 (call-with-values
80 (lambda ()
81 (partition
82 (lambda (layout)
9015e639
LC
83 ;; The 'synopsis' field is usually a language code (e.g., "en")
84 ;; while the 'name' field is a country code (e.g., "us").
85 (or (string=? (x11-keymap-layout-name layout) preferred)
86 (string=? (x11-keymap-layout-synopsis layout) preferred)))
cb614af0 87 layouts))
9015e639
LC
88 (lambda (main others)
89 (append (sort main layout<?)
90 (sort others layout<?)))))
cb614af0
MO
91
92(define (sort-variants variants)
b83e4a93 93 "Sort VARIANTS list by putting the international variant ahead and return it."
cb614af0
MO
94 (call-with-values
95 (lambda ()
96 (partition
97 (lambda (variant)
98 (let ((name (x11-keymap-variant-name variant)))
99 (string=? name "altgr-intl")))
100 variants))
101 (cut append <> <>)))
102
c088b2e4
MO
103(define* (run-keymap-page layouts)
104 "Run a page asking the user to select a keyboard layout and variant. LAYOUTS
105is a list of supported X11-KEYMAP-LAYOUT. Return a list of two elements, the
106names of the selected keyboard layout and variant."
d0f3a672
MO
107 (define keymap-steps
108 (list
d0f3a672
MO
109 (installer-step
110 (id 'layout)
111 (compute
112 (lambda _
9e58d4e9 113 (run-layout-page
cb614af0 114 (sort-layouts layouts)
9e58d4e9 115 (lambda (layout)
feaa83a3
LC
116 (gettext (x11-keymap-layout-description layout)
117 "xkeyboard-config"))))))
d0f3a672
MO
118 ;; Propose the user to select a variant among those supported by the
119 ;; previously selected layout.
120 (installer-step
121 (id 'variant)
122 (compute
54754efc 123 (lambda (result _)
9e58d4e9
MO
124 (let* ((layout (result-step result 'layout))
125 (variants (x11-keymap-layout-variants layout)))
126 ;; Return #f if the layout does not have any variant.
127 (and (not (null? variants))
cb614af0
MO
128 (run-variant-page
129 (sort-variants variants)
130 (lambda (variant)
feaa83a3
LC
131 (gettext (x11-keymap-variant-description variant)
132 "xkeyboard-config"))))))))))
d0f3a672
MO
133
134 (define (format-result result)
c088b2e4 135 (let ((layout (x11-keymap-layout-name
d0f3a672
MO
136 (result-step result 'layout)))
137 (variant (and=> (result-step result 'variant)
138 (lambda (variant)
feaa83a3
LC
139 (gettext (x11-keymap-variant-name variant)
140 "xkeyboard-config")))))
c088b2e4 141 (list layout (or variant ""))))
d0f3a672
MO
142 (format-result
143 (run-installer-steps #:steps keymap-steps)))
3191b5f6
LC
144
145(define (keyboard-layout->configuration keymap)
146 "Return the operating system configuration snippet to install KEYMAP."
147 (match keymap
148 ((name "")
149 `((keyboard-layout (keyboard-layout ,name))))
150 ((name variant)
151 `((keyboard-layout (keyboard-layout ,name ,variant))))))