keymap.scm 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. (raise
  61. (condition
  62. (&installer-step-abort)))))))))
  63. (define (run-variant-page variants variant->text)
  64. (let ((title (G_ "Variant")))
  65. (run-listbox-selection-page
  66. #:title title
  67. #:info-text (G_ "Please choose a variant for your keyboard layout.")
  68. #:listbox-items variants
  69. #:listbox-item->text variant->text
  70. #:sort-listbox-items? #f
  71. #:button-text (G_ "Back")
  72. #:button-callback-procedure
  73. (lambda _
  74. (raise
  75. (condition
  76. (&installer-step-abort)))))))
  77. (define (sort-layouts layouts)
  78. "Sort LAYOUTS list by putting the US layout ahead and return it."
  79. (define (layout<? layout1 layout2)
  80. (let ((text1 (x11-keymap-layout-description layout1))
  81. (text2 (x11-keymap-layout-description layout2)))
  82. ;; XXX: We're calling 'gettext' more than once per item.
  83. (string-locale<? (gettext text1 "xkeyboard-config")
  84. (gettext text2 "xkeyboard-config"))))
  85. (define preferred
  86. ;; Two-letter language tag for the preferred keyboard layout.
  87. (or (getenv "LANGUAGE") "us"))
  88. (call-with-values
  89. (lambda ()
  90. (partition
  91. (lambda (layout)
  92. ;; The 'synopsis' field is usually a language code (e.g., "en")
  93. ;; while the 'name' field is a country code (e.g., "us").
  94. (or (string=? (x11-keymap-layout-name layout) preferred)
  95. (string=? (x11-keymap-layout-synopsis layout) preferred)))
  96. layouts))
  97. (lambda (main others)
  98. (append (sort main layout<?)
  99. (sort others layout<?)))))
  100. (define (add-empty-variant variants)
  101. "Prepend #f to VARIANTS so the user has the option to select no variant.
  102. The resulting layout may be different from all other variants (e.g. for
  103. Azerbaijani)."
  104. (cons #f variants))
  105. (define (sort-variants variants)
  106. "Sort VARIANTS list by putting the international variant ahead and return it."
  107. (call-with-values
  108. (lambda ()
  109. (partition
  110. (lambda (variant)
  111. (and variant
  112. (let ((name (x11-keymap-variant-name variant)))
  113. (string=? name "altgr-intl"))))
  114. variants))
  115. (cut append <> <>)))
  116. (define %non-latin-layouts
  117. ;; List of keyboard layouts marked as $nonlatin in xkeyboard-config.
  118. ;; See comments in xkeyboard-config file /share/X11/xkb/rules/base.
  119. ;; We ignore layouts that support Latin input: "kr"
  120. '("am" "ara" "ben" "bd" "bg" "bt" "by" "cs" "deva" "ge" "gh"
  121. "gr" "guj" "guru" "il" "in" "ir" "iku" "jp" "kan" "kh"
  122. "la" "lao" "lk" "mk" "mm" "mn" "mv" "mal" "olck" "ori" "pk"
  123. "ru" "scc" "sy" "syr" "tel" "th" "tj" "tam" "ua" "uz"
  124. ;; The list from xkeyboard-config is incomplete. Add more layouts when
  125. ;; noticed:
  126. "et" "kz"))
  127. (define %non-latin-variants
  128. '("cyrillic"))
  129. (define %latin-layout+variants
  130. ;; These layout+variant combinations are Latin after all.
  131. '(("ir" "ku")))
  132. (define (toggleable-latin-layout layout variant)
  133. "If LAYOUT is a non-Latin layout, return a new combined layout,
  134. a variant, and options that allow the user to switch between the
  135. non-Latin and the Latin layout. Otherwise, return LAYOUT, VARIANT,
  136. and #f."
  137. (if (and (not (equal? variant "latin"))
  138. (not (member (list layout variant) %latin-layout+variants))
  139. (or (member layout %non-latin-layouts)
  140. (member variant %non-latin-variants)))
  141. (let ((latin-layout (if (equal? variant "azerty") "fr" "us")))
  142. (list
  143. (string-append layout "," latin-layout)
  144. ;; Comma to use variant only for non-Latin:
  145. (and variant (string-append variant ","))
  146. "grp:alt_shift_toggle"))
  147. (list layout variant #f)))
  148. (define* (run-keymap-page layouts #:key (context #f))
  149. "Run a page asking the user to select a keyboard layout and variant. LAYOUTS
  150. is a list of supported X11-KEYMAP-LAYOUT. For non-Latin keyboard layouts, a
  151. second layout and toggle options will be added automatically. Return a list
  152. of three elements, the names of the selected keyboard layout, variant and
  153. options."
  154. (define keymap-steps
  155. (list
  156. (installer-step
  157. (id 'layout)
  158. (compute
  159. (lambda _
  160. (run-layout-page
  161. (sort-layouts layouts)
  162. (lambda (layout)
  163. (gettext (x11-keymap-layout-description layout)
  164. "xkeyboard-config"))
  165. context))))
  166. ;; Propose the user to select a variant among those supported by the
  167. ;; previously selected layout.
  168. (installer-step
  169. (id 'variant)
  170. (compute
  171. (lambda (result _)
  172. (let* ((layout (result-step result 'layout))
  173. (variants (if layout
  174. (x11-keymap-layout-variants layout)
  175. '())))
  176. ;; Return #f if the layout does not have any variant.
  177. (and (not (null? variants))
  178. (run-variant-page
  179. (sort-variants (add-empty-variant variants))
  180. (lambda (variant)
  181. (if variant
  182. (gettext (x11-keymap-variant-description variant)
  183. "xkeyboard-config")
  184. ;; Text to opt for no variant at all:
  185. (gettext (x11-keymap-layout-description layout)
  186. "xkeyboard-config")))))))))))
  187. (define (format-result layout variant)
  188. (let ((layout (x11-keymap-layout-name layout))
  189. (variant (and=> variant
  190. (lambda (variant)
  191. (gettext (x11-keymap-variant-name variant)
  192. "xkeyboard-config")))))
  193. (toggleable-latin-layout layout variant)))
  194. (let* ((result (run-installer-steps #:steps keymap-steps))
  195. (layout (result-step result 'layout))
  196. (variant (result-step result 'variant)))
  197. (and layout
  198. (format-result layout variant))))
  199. (define (keyboard-layout->configuration keymap)
  200. "Return the operating system configuration snippet to install KEYMAP."
  201. (match keymap
  202. ((name #f "grp:alt_shift_toggle")
  203. `((keyboard-layout (keyboard-layout ,name
  204. #:options '("grp:alt_shift_toggle")))))
  205. ((name #f _)
  206. `((keyboard-layout (keyboard-layout ,name))))
  207. ((name variant "grp:alt_shift_toggle")
  208. `((keyboard-layout (keyboard-layout ,name ,variant
  209. #:options '("grp:alt_shift_toggle")))))
  210. ((name variant _)
  211. `((keyboard-layout (keyboard-layout ,name ,variant))))))