;; Layouts: which character each key of the board types. ;; ;; - A layout is written as three rows of characters, in the board's order: ;; the top row from Q, the home row from A, the bottom row from <. Spaces ;; are skipped, so a row can be split at the hands. ;; - A row may be shorter than the board's: its last keys type nothing. ;; - Letters are written in either case. ’ is the same character as '. ;; - The shifted character of a punctuation key is typed on that key: ;; , ; . : - _ ' * < >, as on Swedish QWERTY and Svdvorak. (import (std prelude)) (import (keyscore board)) (export Layout make-layout aligned-rows layout-key layout-chars layout-base-chars swap-keys common-chars normalize layouts) ;; - rows: the three rows as written, for making variants. ;; - keys: the key of every character typed, shifted ones included. (type (: Layout (Record (: name string) (: rows (Vec string)) (: keys (Map char Key))))) ;; The shifted character typed on the same key as `c`. (: (-> char (Option char))) (defun (shifted c) (match c (#\, (Some #\;)) (#\. (Some #\:)) (#\- (Some #\_)) (#\' (Some #\*)) (#\< (Some #\>)) (_ None))) ;; A character as the scorer counts it: lower case, with ’ as '. (: (-> char char)) (defun (normalize c) (if (char=? c (int->char 0x2019)) #\' (char-downcase c))) ;; The keys of `row` on the board, in column order. (: (-> int (List Key))) (defun (row-keys row) (->list (filter (fun (k) (= (record-ref k row) row)) board))) ;; A layout named `name` from its three rows. Raises if a row is longer than ;; the board's. (: (-> string string string string Layout)) (defun (make-layout name top home bottom) (def keys (loop (:for (Tuple row text) (list (Tuple 0 top) (Tuple 1 home) (Tuple 2 bottom))) (:let chars (->list (filter (fun (c) (not (char=? c #\space))) text))) (:let ks (row-keys row)) (:do (when (> (list-length chars) (list-length ks)) (panic! (str name ": the row " (written text) " has more keys than the board")))) (:for (Tuple c k) (zip chars ks)) (:let base (normalize c)) (:for (Tuple ch key) (match (shifted base) ((Some s) (list (Tuple base k) (Tuple s k))) (None (list (Tuple base k))))) (:acc out (mapping (Tuple ch key))))) (Layout (name name) (rows [top home bottom]) (keys keys))) ;; The key that types `c`, if the layout has one. (: (-> Layout char (Option Key))) (defun (layout-key l c) (map-try-ref (record-ref l keys) c)) ;; Every character the layout types. (: (-> Layout (List char))) (defun (layout-chars l) (->list (map-keys (record-ref l keys)))) ;; The characters written in the layout's rows, normalized: one per key. (: (-> Layout (List char))) (defun (layout-base-chars l) (loop (:for r (record-ref l rows)) (:let row r) (:for c row) (:when (not (char=? c #\space))) (:acc out (listing (normalize c))))) ;; `l` with the keys of `a` and `b` swapped, named `name`. `a` and `b` are ;; compared with the rows' characters after `normalize`. (: (-> Layout string char char Layout)) (defun (swap-keys l name a b) (: swap (-> char char)) (def swap (fun (c) (def n (normalize c)) (cond ((char=? n a) b) ((char=? n b) a) (else c)))) (def rows (vec-map (fun (r) (list->string (->list (map swap r)))) (record-ref l rows))) (make-layout name (vec-ref rows 0) (vec-ref rows 1) (vec-ref rows 2))) ;; `rows` written with the hands apart, so that the rows line up in a ;; fixed-width font: ;; ;; ÖUOF, QVLBJ ;; IEADY CTRNS ;; ÅÄ.HK Z MGWPX ;; ;; - Top and home rows: five keys, three spaces, the rest. ;; - Bottom row: five keys, the middle key between single spaces, the rest. ;; - Spaces already in `rows` are dropped first. (: (-> (Vec string) (Vec string))) (defun (aligned-rows rows) (: gap (-> int int string)) (def gap (fun (row i) (cond ((and (< row 2) (= i 5)) " ") ((and (= row 2) (or (= i 5) (= i 6))) " ") (else "")))) (loop (:for text rows) (:for row (range 0 3)) (:let chars (->list (filter (fun (c) (not (char=? c #\space))) text))) (:let parts (loop (:for c chars) (:for i (range 0 (list-length chars))) (:acc out (vecing (str (gap row i) (char->string c)))))) (:acc out (vecing (string-join parts ""))))) ;; The characters every layout in `ls` types. (: (-> (Vec Layout) (List char))) (defun (common-chars ls) (->list (filter (fun (c) (all? (fun (l) (some? (layout-key l c))) ls)) (layout-chars (vec-ref ls 0))))) (: (Vec Layout)) (def layouts [(make-layout "QWERTY" "qwert yuiopå" "asdfg hjklöä'" "