;;; x-kbgrunge.learn                                    -*-scheme-*-

;; Copyright (C) 2009, 2011 Thien-Thi Nguyen
;;
;; This file is part of ttn-do, released under the terms of the
;; GNU General Public License as published by the Free Software
;; Foundation; either version 3, or (at your option) any later
;; version.  There is NO WARRANTY.  See file COPYING for details.

;;; Code:

(primitive-load "./personally.scm")
(primitive-load "../mogrify.scm")

(use-modules
 ((ttn-do zzz personally) #:select (FE fso))
 ((ice-9 pretty-print) #:select (pretty-print))
 ((ttn-do mogrify) #:select (find-file-read-only
                             editing-buffer)))

(define K '(0))
(define S '(NoSymbol))
(define COUNT 1)

(editing-buffer (find-file-read-only "/usr/include/X11/keysymdef.h")
  (while (re-search-forward "^#define XK_([A-Za-z0-9_]+).+0x([0-9a-f]+)"
                            #f #t)
    (set! K (cons (string->number (match-string 2) 16) K))
    (set! S (cons (string->symbol (match-string 1)) S))
    ;; Aliases lose.  This policy favors Mode_Switch over its many aliases.
    ;; Also notable are Prior (over Page_Up) and Next (over Page_Down).
    ;; Unfortunately, this is sensitive to dist-time (ttn's) keysymdef.h.
    (cond ((memq (car K) (cdr K))
           (set! K (cdr K))
           (set! S (cdr S))))
    (set! COUNT (1+ COUNT))))

(pretty-print
 `(let ((p (current-load-port))
        (k #f)
        (s #f))
    (let loop ((count ,COUNT))
      (cond ((zero? count))
            (else (set! k (read p))
                  (set! s (read p))
                  (hashq-set! S2K s k)
                  (hashq-set! K2S k s)
                  (loop (1- count)))))
    ;; Add aliases for symbols `0' through `9'.
    (for-each (lambda (n)
                (hashq-set! S2K (string->symbol (number->string n))
                            (hashq-ref S2K n)))
              (iota 10))))

(FE K S (lambda (k s)
          (fso "~S ~S~%" k s)))

;;; x-kbgrunge.learn ends here