keymap.scm 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018, 2020 Mathieu Othacehe <m.othacehe@gmail.com>
  3. ;;; Copyright © 2019 Ludovic Courtès <ludo@gnu.org>
  4. ;;; Copyright © 2020 Florian Pelz <pelzflorian@pelzflorian.de>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  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)
  28. #:use-module (srfi srfi-26)
  29. #:use-module (srfi srfi-34)
  30. #:use-module (srfi srfi-35)
  31. #:use-module (ice-9 i18n)
  32. #:use-module (ice-9 match)
  33. #:export (run-keymap-page
  34. keyboard-layout->configuration))
  35. (define (run-layout-page layouts layout->text context)
  36. (let ((title (G_ "Layout")))
  37. (run-listbox-selection-page
  38. #:title title
  39. #:info-text
  40. (case context
  41. ((param) (G_ "Please choose your keyboard layout. \
  42. It will only be used during the installation process. \
  43. Non-Latin layouts can be toggled with Alt+Shift."))
  44. (else (G_ "Please choose your keyboard layout. \
  45. It will be used during the install process, and for the installed system. \
  46. Non-Latin layouts can be toggled with Alt+Shift. You can switch to a \
  47. different layout at any time from the parameters menu.")))
  48. #:listbox-items layouts
  49. #:listbox-item->text layout->text
  50. #:sort-listbox-items? #f
  51. #:button-text
  52. (case context
  53. ((param) (G_ "Continue"))
  54. (else (G_ "Exit")))
  55. #:button-callback-procedure
  56. (case context
  57. ((param) (const #f))
  58. (else
  59. (lambda _
  60. (abort-to-prompt 'installer-step 'abort)))))))
  61. (define (run-variant-page variants variant->text)
  62. (let ((title (G_ "Variant")))
  63. (run-listbox-selection-page
  64. #:title title
  65. #:info-text (G_ "Please choose a variant for your keyboard layout.")
  66. #:listbox-items variants
  67. #:listbox-item->text variant->text
  68. #:sort-listbox-items? #f
  69. #:button-text (G_ "Back")
  70. #:button-callback-procedure
  71. (lambda _
  72. (abort-to-prompt 'installer-step 'abort)))))
  73. (define (sort-layouts layouts)
  74. "Sort LAYOUTS list by putting the US layout ahead and return it."
  75. (define (layout<? layout1 layout2)
  76. (let ((text1 (x11-keymap-layout-description layout1))
  77. (text2 (x11-keymap-layout-description layout2)))
  78. ;; XXX: We're calling 'gettext' more than once per item.
  79. (string-locale<? (gettext text1 "xkeyboard-config")
  80. (gettext text2 "xkeyboard-config"))))
  81. (define preferred
  82. ;; Two-letter language tag for the preferred keyboard layout.
  83. (or (getenv "LANGUAGE") "us"))
  84. (call-with-values
  85. (lambda ()
  86. (partition
  87. (lambda (layout)
  88. ;; The 'synopsis' field is usually a language code (e.g., "en")
  89. ;; while the 'name' field is a country code (e.g., "us").
  90. (or (string=? (x11-keymap-layout-name layout) preferred)
  91. (string=? (x11-keymap-layout-synopsis layout) preferred)))
  92. layouts))
  93. (lambda (main others)
  94. (append (sort main layout<?)
  95. (sort others layout<?)))))
  96. (define (add-empty-variant variants)
  97. "Prepend #f to VARIANTS so the user has the option to select no variant.
  98. The resulting layout may be different from all other variants (e.g. for
  99. Azerbaijani)."
  100. (cons #f variants))
  101. (define (sort-variants variants)
  102. "Sort VARIANTS list by putting the international variant ahead and return it."
  103. (call-with-values
  104. (lambda ()
  105. (partition
  106. (lambda (variant)
  107. (and variant
  108. (let ((name (x11-keymap-variant-name variant)))
  109. (string=? name "altgr-intl"))))
  110. variants))
  111. (cut append <> <>)))
  112. (define %non-latin-layouts
  113. ;; List of keyboard layouts marked as $nonlatin in xkeyboard-config.
  114. ;; See comments in xkeyboard-config file /share/X11/xkb/rules/base.
  115. ;; We ignore layouts that support Latin input: "kr"
  116. '("am" "ara" "ben" "bd" "bg" "bt" "by" "cs" "deva" "ge" "gh"
  117. "gr" "guj" "guru" "il" "in" "ir" "iku" "jp" "kan" "kh"
  118. "la" "lao" "lk" "mk" "mm" "mn" "mv" "mal" "olck" "ori" "pk"
  119. "ru" "scc" "sy" "syr" "tel" "th" "tj" "tam" "ua" "uz"
  120. ;; The list from xkeyboard-config is incomplete. Add more layouts when
  121. ;; noticed:
  122. "et" "kz"))
  123. (define %non-latin-variants
  124. '("cyrillic"))
  125. (define %latin-layout+variants
  126. ;; These layout+variant combinations are Latin after all.
  127. '(("ir" "ku")))
  128. (define (toggleable-latin-layout layout variant)
  129. "If LAYOUT is a non-Latin layout, return a new combined layout,
  130. a variant, and options that allow the user to switch between the
  131. non-Latin and the Latin layout. Otherwise, return LAYOUT, VARIANT,
  132. and #f."
  133. (if (and (not (equal? variant "latin"))
  134. (not (member (list layout variant) %latin-layout+variants))
  135. (or (member layout %non-latin-layouts)
  136. (member variant %non-latin-variants)))
  137. (let ((latin-layout (if (equal? variant "azerty") "fr" "us")))
  138. (list
  139. (string-append layout "," latin-layout)
  140. ;; Comma to use variant only for non-Latin:
  141. (and variant (string-append variant ","))
  142. "grp:alt_shift_toggle"))
  143. (list layout variant #f)))
  144. (define* (run-keymap-page layouts #:key (context #f))
  145. "Run a page asking the user to select a keyboard layout and variant. LAYOUTS
  146. is a list of supported X11-KEYMAP-LAYOUT. For non-Latin keyboard layouts, a
  147. second layout and toggle options will be added automatically. Return a list
  148. of three elements, the names of the selected keyboard layout, variant and
  149. options."
  150. (define keymap-steps
  151. (list
  152. (installer-step
  153. (id 'layout)
  154. (compute
  155. (lambda _
  156. (run-layout-page
  157. (sort-layouts layouts)
  158. (lambda (layout)
  159. (gettext (x11-keymap-layout-description layout)
  160. "xkeyboard-config"))
  161. context))))
  162. ;; Propose the user to select a variant among those supported by the
  163. ;; previously selected layout.
  164. (installer-step
  165. (id 'variant)
  166. (compute
  167. (lambda (result _)
  168. (let* ((layout (result-step result 'layout))
  169. (variants (if layout
  170. (x11-keymap-layout-variants layout)
  171. '())))
  172. ;; Return #f if the layout does not have any variant.
  173. (and (not (null? variants))
  174. (run-variant-page
  175. (sort-variants (add-empty-variant variants))
  176. (lambda (variant)
  177. (if variant
  178. (gettext (x11-keymap-variant-description variant)
  179. "xkeyboard-config")
  180. ;; Text to opt for no variant at all:
  181. (gettext (x11-keymap-layout-description layout)
  182. "xkeyboard-config")))))))))))
  183. (define (format-result layout variant)
  184. (let ((layout (x11-keymap-layout-name layout))
  185. (variant (and=> variant
  186. (lambda (variant)
  187. (gettext (x11-keymap-variant-name variant)
  188. "xkeyboard-config")))))
  189. (toggleable-latin-layout layout variant)))
  190. (let* ((result (run-installer-steps #:steps keymap-steps))
  191. (layout (result-step result 'layout))
  192. (variant (result-step result 'variant)))
  193. (and layout
  194. (format-result layout variant))))
  195. (define (keyboard-layout->configuration keymap)
  196. "Return the operating system configuration snippet to install KEYMAP."
  197. (match keymap
  198. ((name #f "grp:alt_shift_toggle")
  199. `((keyboard-layout (keyboard-layout ,name
  200. #:options '("grp:alt_shift_toggle")))))
  201. ((name #f _)
  202. `((keyboard-layout (keyboard-layout ,name))))
  203. ((name variant "grp:alt_shift_toggle")
  204. `((keyboard-layout (keyboard-layout ,name ,variant
  205. #:options '("grp:alt_shift_toggle")))))
  206. ((name variant _)
  207. `((keyboard-layout (keyboard-layout ,name ,variant))))))