keymap.scm 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. (define-module (gnu installer newt keymap)
  19. #:use-module (gnu installer keymap)
  20. #:use-module (gnu installer steps)
  21. #:use-module (gnu installer newt page)
  22. #:use-module (guix i18n)
  23. #:use-module (guix records)
  24. #:use-module (newt)
  25. #:use-module (srfi srfi-1)
  26. #:use-module (srfi srfi-26)
  27. #:use-module (srfi srfi-34)
  28. #:use-module (srfi srfi-35)
  29. #:export (run-keymap-page))
  30. (define (run-layout-page layouts layout->text)
  31. (let ((title (G_ "Layout")))
  32. (run-listbox-selection-page
  33. #:title title
  34. #:info-text (G_ "Please choose your keyboard layout.")
  35. #:listbox-items layouts
  36. #:listbox-item->text layout->text
  37. #:sort-listbox-items? #f
  38. #:button-text (G_ "Exit")
  39. #:button-callback-procedure
  40. (lambda _
  41. (raise
  42. (condition
  43. (&installer-step-abort)))))))
  44. (define (run-variant-page variants variant->text)
  45. (let ((title (G_ "Variant")))
  46. (run-listbox-selection-page
  47. #:title title
  48. #:info-text (G_ "Please choose a variant for your keyboard layout.")
  49. #:listbox-items variants
  50. #:listbox-item->text variant->text
  51. #:sort-listbox-items? #f
  52. #:button-text (G_ "Back")
  53. #:button-callback-procedure
  54. (lambda _
  55. (raise
  56. (condition
  57. (&installer-step-abort)))))))
  58. (define (sort-layouts layouts)
  59. "Sort LAYOUTS list by putting the US layout ahead and return it."
  60. (call-with-values
  61. (lambda ()
  62. (partition
  63. (lambda (layout)
  64. (let ((name (x11-keymap-layout-name layout)))
  65. (string=? name "us")))
  66. layouts))
  67. (cut append <> <>)))
  68. (define (sort-variants variants)
  69. "Sort VARIANTS list by putting the international variant ahead and return it."
  70. (call-with-values
  71. (lambda ()
  72. (partition
  73. (lambda (variant)
  74. (let ((name (x11-keymap-variant-name variant)))
  75. (string=? name "altgr-intl")))
  76. variants))
  77. (cut append <> <>)))
  78. (define* (run-keymap-page layouts)
  79. "Run a page asking the user to select a keyboard layout and variant. LAYOUTS
  80. is a list of supported X11-KEYMAP-LAYOUT. Return a list of two elements, the
  81. names of the selected keyboard layout and variant."
  82. (define keymap-steps
  83. (list
  84. (installer-step
  85. (id 'layout)
  86. (compute
  87. (lambda _
  88. (run-layout-page
  89. (sort-layouts layouts)
  90. (lambda (layout)
  91. (x11-keymap-layout-description layout))))))
  92. ;; Propose the user to select a variant among those supported by the
  93. ;; previously selected layout.
  94. (installer-step
  95. (id 'variant)
  96. (compute
  97. (lambda (result _)
  98. (let* ((layout (result-step result 'layout))
  99. (variants (x11-keymap-layout-variants layout)))
  100. ;; Return #f if the layout does not have any variant.
  101. (and (not (null? variants))
  102. (run-variant-page
  103. (sort-variants variants)
  104. (lambda (variant)
  105. (x11-keymap-variant-description
  106. variant))))))))))
  107. (define (format-result result)
  108. (let ((layout (x11-keymap-layout-name
  109. (result-step result 'layout)))
  110. (variant (and=> (result-step result 'variant)
  111. (lambda (variant)
  112. (x11-keymap-variant-name variant)))))
  113. (list layout (or variant ""))))
  114. (format-result
  115. (run-installer-steps #:steps keymap-steps)))