Subject: 29.0.50; Wishlist: Add a command to use XKB "Compose" sequences in Emacs

From: Lars Ingebrigtsen <[email-masked]>
Date: Sun, 29 May 2022 15:39:38 +0200

This spins out of bug#43866.

Juri posted the code below, and Eli then made the point that we should
pre-generate this data from the two files that are being parsed.

I think that sounds like a good idea (i.e., we'd include the two files
and then have a Lisp snippet that transforms it into an .el file, as we
do with other similar data files).

And I think using `C-+' sounds fine, too.

--------

Here's is a working implementation.  It binds all key sequences to the key
'C-+' that has the mnemonics of adding a character.  'C-+' is free because
it can't be used to zoom text since its counterpart key 'C--' is already
taken to input numeric arguments.  'C-+ C-+' is bound to 'insert-char'
like the current longer key sequence 'C-x 8 RET' that is hard to type.

;;; x-compose.el --- Compose input method from X11   -*- lexical-binding: t; -*-

(defvar x-compose-keymap
  (let ((map (make-sparse-keymap)))
    (let ((keysymdef (make-hash-table :test 'equal)))
      (with-temp-buffer
        (insert-file-contents "/usr/include/X11/keysymdef.h")
        (while (re-search-forward "^#define XK_\\(\\S-+\\).+/\\* U\\+\\(\\w+\\)" nil t)
          (puthash (match-string 1) (match-string 2) keysymdef)))
      (with-temp-buffer
        (insert-file-contents "/usr/share/X11/locale/en_US.UTF-8/Compose")
        (while (re-search-forward "^<Multi_key> \\([^:]+\\): \"\\([^\"]+\\)\"" nil t)
          (let* ((to-char (string-to-char (match-string 2)))
                 (from-keys (match-string 1))
                 (from-chars
                  (mapcar (lambda (s)
                            (if (string-match "^U\\([[:xdigit:]]+\\)" s)
                                (string-to-number (match-string 1 s) 16)
                              (string-to-number (gethash s keysymdef "0000") 16)))
                          (split-string from-keys "[<> \t]+" t))))
            (unless (memq 0 from-chars)
              (define-key map (apply 'vector from-chars) (vector to-char)))))))
    map)
  "Keymap for keys of the Compose input method.")

(define-key key-translation-map [(control ?+)] x-compose-keymap)
(global-set-key [(control ?+) (control ?+)] 'insert-char)

(provide 'x-compose)
;;; x-compose.el ends here

In GNU Emacs 29.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.33, cairo version 1.16.0)
 of 2022-05-17 built on xo
Repository revision: 803041e01474f2a522170c9f388068e8460be2ae
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12101003
System Description: Debian GNU/Linux bookworm/sid

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no