elisprs 0.1.20

Emacs Lisp as a fusevm frontend: a reader and compiler to fusevm::Chunk on an ElispHost object heap, with no bespoke VM or JIT
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
;;; keymap.c / keyboard.c ports: map-keymap, copy-keymap, keymap-prompt and
;;; the event-symbol helpers.

;; get_keymap (OBJECT, ERROR_IF_NOT_KEYMAP, AUTOLOAD)
(defun keymap--get-keymap (object error-if-not-keymap autoload)
  (let ((result
         (cond
          ((null object) nil)
          ((and (consp object) (eq (car object) 'keymap)) object)
          (t
           (let ((tem (indirect-function object)))
             (cond
              ((and (consp tem) (eq (car tem) 'keymap)) tem)
              ((and (consp tem) (eq (car tem) 'autoload) (symbolp object)
                    (or autoload (not error-if-not-keymap))
                    (eq (nth 4 tem) 'keymap))
               (if autoload
                   (progn
                     (autoload-do-load tem object)
                     (let ((new (indirect-function object)))
                       (and (consp new) (eq (car new) 'keymap) new)))
                 object))
              (t nil)))))))
    (if (and (null result) error-if-not-keymap)
        (signal 'wrong-type-argument (list 'keymapp object))
      result)))

(defun keymap--keymapp (object)
  (and (keymap--get-keymap object nil nil) t))

(defun keymap--item (function key val)
  (funcall function key (if (eq val t) nil val)))

;; map_keymap_internal: call FUNCTION on the bindings of MAP's own part and
;; return the tail that begins its parent (or an embedded keymap).
(defun keymap--map-internal (function map)
  (let ((tail (if (and (consp map) (eq (car map) 'keymap)) (cdr map) map))
        (done nil))
    (while (and (not done) (consp tail) (not (eq (car tail) 'keymap)))
      (let ((binding (car tail)))
        (cond
         ((keymap--keymapp binding) (setq done t))
         ((consp binding)
          (keymap--item function (car binding) (cdr binding)))
         ((vectorp binding)
          (let ((c 0) (n (length binding)))
            (while (< c n)
              (keymap--item function c (aref binding c))
              (setq c (1+ c)))))))
      (unless done (setq tail (cdr tail))))
    tail))

(defun keymap--map (function map autoload)
  (setq map (keymap--get-keymap map t autoload))
  (while (consp map)
    (if (keymap--keymapp (car map))
        (progn (keymap--map function (car map) autoload)
               (setq map (cdr map)))
      (setq map (keymap--map-internal function map)))
    (unless (consp map)
      (setq map (keymap--get-keymap map nil autoload)))))

(defun map-keymap (function keymap &optional sort-first)
  "Call FUNCTION once for each event binding in KEYMAP.
FUNCTION is called with two arguments: the event that is bound, and
the definition it is bound to.  The event may be a character range.

If KEYMAP has a parent, the parent's bindings are included as well.
This works recursively: if the parent has itself a parent, then the
grandparent's bindings are also included and so on.

For more information, see Info node `(elisp) Keymaps'.

If SORT-FIRST is non-nil, FUNCTION is called in the order of
`map-keymap-sorted'."
  (if sort-first
      (map-keymap-sorted function keymap)
    (keymap--map function keymap t)
    nil))

(defun map-keymap-internal (function keymap)
  "Call FUNCTION once for each event binding in KEYMAP.
FUNCTION is called with two arguments: the event that is bound, and
the definition it is bound to.  The event may be a character range.
If KEYMAP has a parent, return it without processing it."
  (keymap--map-internal function (keymap--get-keymap keymap t t)))

(defun keymap-prompt (map)
  "Return the prompt-string of a keymap MAP.
If non-nil, the prompt is shown in the echo-area
when reading a key-sequence to be looked-up in this keymap."
  (setq map (keymap--get-keymap map nil nil))
  (let ((res nil))
    (while (and (consp map) (not res))
      (let ((tem (car map)))
        (cond ((stringp tem) (setq res tem))
              ((keymap--keymapp tem)
               (setq res (keymap-prompt tem)))))
      (unless res (setq map (cdr map))))
    res))

(defun keymap--copy-item (elt)
  (if (not (consp elt))
      elt
    (let ((res elt) (tem elt))
      (if (eq (car tem) 'menu-item)
          (progn
            (setq res (cons (car tem) (cdr tem)) elt res tem (cdr elt))
            (when (consp tem)
              (setcdr elt (cons (car tem) (cdr tem)))
              (setq elt (cdr elt) tem (cdr elt)))
            (when (consp tem)
              (setcdr elt (cons (car tem) (cdr tem)))
              (setq elt (cdr elt) tem (car elt))
              (when (and (consp tem) (eq (car tem) 'keymap))
                (setcar elt (copy-keymap tem)))
              (setq tem (cdr elt))))
        (cond
         ((stringp (car tem))
          (setq res (cons (car tem) (cdr tem)) elt res tem (cdr elt))
          (when (and (consp tem) (stringp (car tem)))
            (setcdr elt (cons (car tem) (cdr tem)))
            (setq elt (cdr elt) tem (cdr elt)))
          (when (and (consp tem) (eq (car tem) 'keymap))
            (setcdr elt (copy-keymap tem))))
         ((eq (car tem) 'keymap)
          (setq res (copy-keymap elt)))))
      res)))

(defun copy-keymap (keymap)
  "Return a copy of the keymap KEYMAP.

Note that this is almost never the right way to copy a keymap.  For
instance, it does not handle parent keymaps; it just copies the
keymap's own bindings.  If you want to modify a keymap, you will
generally be better off using `define-keymap' instead."
  (setq keymap (keymap--get-keymap keymap t nil))
  (let* ((copy (list 'keymap)) (tail copy))
    (setq keymap (cdr keymap))
    (while (and (consp keymap) (not (eq (car keymap) 'keymap)))
      (let ((elt (car keymap)))
        (cond
         ((char-table-p elt) (setq elt (copy-sequence elt)))
         ((vectorp elt)
          (setq elt (copy-sequence elt))
          (let ((i 0))
            (while (< i (length elt))
              (aset elt i (keymap--copy-item (aref elt i)))
              (setq i (1+ i)))))
         ((consp elt)
          (setq elt (cons (car elt) (keymap--copy-item (cdr elt))))))
        (setcdr tail (list elt))
        (setq tail (cdr tail)
              keymap (cdr keymap))))
    (setcdr tail keymap)
    copy))

;;;; Event symbols (keyboard.c).

(defconst keymap--modifier-bits
  '((up . 1) (down . 2) (drag . 4) (click . 8) (double . 16) (triple . 32)
    (alt . #x400000) (super . #x800000) (hyper . #x1000000)
    (shift . #x2000000) (control . #x4000000) (meta . #x8000000)))

(defun keymap--symbol-modifiers (name)
  "parse_modifiers_uncached: return (MODIFIER-MASK . END-INDEX) for NAME."
  (let ((i 0) (mods 0) (len (length name)) (go t))
    (while (and go (< i (1- len)))
      (let ((this-mod 0) (this-end 0) (c (aref name i)))
        (cond
         ((eq c ?A) (setq this-end (1+ i) this-mod #x400000))
         ((eq c ?C) (setq this-end (1+ i) this-mod #x4000000))
         ((eq c ?H) (setq this-end (1+ i) this-mod #x1000000))
         ((eq c ?M) (setq this-end (1+ i) this-mod #x8000000))
         ((eq c ?S) (setq this-end (1+ i) this-mod #x2000000))
         ((eq c ?s) (setq this-end (1+ i) this-mod #x800000))
         ((eq c ?d)
          (cond ((and (<= (+ i 5) len) (string= (substring name i (+ i 4)) "drag"))
                 (setq this-end (+ i 4) this-mod 4))
                ((and (<= (+ i 5) len) (string= (substring name i (+ i 4)) "down"))
                 (setq this-end (+ i 4) this-mod 2))
                ((and (<= (+ i 7) len) (string= (substring name i (+ i 6)) "double"))
                 (setq this-end (+ i 6) this-mod 16))))
         ((eq c ?t)
          (when (and (<= (+ i 7) len) (string= (substring name i (+ i 6)) "triple"))
            (setq this-end (+ i 6) this-mod 32)))
         ((eq c ?u)
          (when (and (<= (+ i 3) len) (string= (substring name i (+ i 2)) "up"))
            (setq this-end (+ i 2) this-mod 1))))
        (if (or (= this-end 0) (>= this-end len) (/= (aref name this-end) ?-))
            (setq go nil)
          (setq mods (logior mods this-mod)
                i (1+ this-end)))))
    (when (and (zerop (logand mods (logior 2 4 16 32)))
               (= (+ i 7) len)
               (string= (substring name i (+ i 6)) "mouse-")
               (<= ?0 (aref name (+ i 6)) ?9))
      (setq mods (logior mods 8)))
    (when (and (zerop (logand mods (logior 16 32)))
               (< (+ i 6) len)
               (string= (substring name i (+ i 6)) "wheel-"))
      (setq mods (logior mods 8)))
    (cons mods i)))

(defun keymap--modifier-list (mods)
  (let ((res nil))
    (dolist (m keymap--modifier-bits)
      (unless (zerop (logand mods (cdr m)))
        (push (car m) res)))
    res))

(defun internal-event-symbol-parse-modifiers (symbol)
  "Parse the event symbol.  For internal use."
  (unless (symbolp symbol)
    (signal 'wrong-type-argument (list 'symbolp symbol)))
  (let ((cached (get symbol 'event-symbol-elements)))
    (or cached
        (let* ((name (symbol-name symbol))
               (parsed (keymap--symbol-modifiers name))
               (unmodified (intern (substring name (cdr parsed)))))
          (put symbol 'event-symbol-element-mask (list unmodified (car parsed)))
          (put symbol 'event-symbol-elements
               (cons unmodified (keymap--modifier-list (car parsed))))))))

(defun keymap--make-ctrl-char (c)
  (let ((upper (logand c (lognot #o177))))
    (if (>= c 128)
        (logior c #x4000000)
      (setq c (logand c #o177))
      (cond
       ((and (>= c #o100) (< c #o140))
        (let ((oc c))
          (setq c (logand c (lognot #o140)))
          (when (and (>= oc ?A) (<= oc ?Z))
            (setq c (logior c #x2000000)))))
       ((and (>= c ?a) (<= c ?z))
        (setq c (logand c (lognot #o140))))
       ((>= c ?\s)
        (setq c (logior c #x4000000))))
      (logior c (logand upper (lognot #x4000000))))))

(defun keymap--solitary-modifier (sym)
  (let ((name (symbol-name sym)))
    (cond
     ((member name '("A" "alt")) #x400000)
     ((member name '("C" "ctrl" "control")) #x4000000)
     ((member name '("H" "hyper")) #x1000000)
     ((member name '("M" "meta")) #x8000000)
     ((member name '("S" "shift")) #x2000000)
     ((member name '("s" "super")) #x800000)
     ((string= name "down") 2)
     ((string= name "drag") 4)
     ((string= name "double") 16)
     ((string= name "triple") 32)
     ((string= name "up") 1)
     ((string= name "click") 8)
     (t 0))))

(defun event-convert-list (event-desc)
  "Convert the event description list EVENT-DESC to an event type.
EVENT-DESC should contain one base event type (a character or symbol)
and zero or more modifier names (control, meta, hyper, super, shift, alt,
drag, down, double or triple).  The base must be last.
The return value is an event type (a character or symbol) which
has essentially the same meaning as the event description list.
See Info node `(elisp)Classifying Events' for more information."
  (let ((base nil) (modifiers 0))
    (while (consp event-desc)
      (let* ((elt (car event-desc))
             (this (if (and (symbolp elt) (consp (cdr event-desc)))
                       (keymap--solitary-modifier elt)
                     0)))
        (cond ((/= this 0) (setq modifiers (logior modifiers this)))
              (base (error "Two bases given in one event"))
              (t (setq base elt))))
      (setq event-desc (cdr event-desc)))
    (when (and (symbolp base) (= (length (symbol-name base)) 1))
      (setq base (aref (symbol-name base) 0)))
    (cond
     ((integerp base)
      (when (and (/= 0 (logand modifiers #x2000000)) (<= ?a base ?z))
        (setq base (- base (- ?a ?A))
              modifiers (logand modifiers (lognot #x2000000))))
      (if (/= 0 (logand modifiers #x4000000))
          (logior (logand modifiers (lognot #x4000000))
                  (keymap--make-ctrl-char base))
        (logior modifiers base)))
     ((symbolp base)
      (let ((name (symbol-name base)) (prefix ""))
        (dolist (m '((#x400000 . "A-") (#x4000000 . "C-") (#x1000000 . "H-")
                     (#x8000000 . "M-") (#x2000000 . "S-") (#x800000 . "s-")
                     (16 . "double-") (32 . "triple-") (1 . "up-")
                     (2 . "down-") (4 . "drag-")))
          (unless (zerop (logand modifiers (car m)))
            (setq prefix (concat prefix (cdr m)))))
        (intern (concat prefix name))))
     (t (error "Invalid base event")))))

;;;; Key descriptions (keymap.c).

(defun keymap--lucid-event-type-list-p (object)
  (and (consp object)
       (not (memq (car object)
                  '(help-echo vertical-line mode-line tab-line header-line)))
       (let ((tail object) (ok t))
         (while (and ok (consp tail))
           (let ((elt (car tail)))
             (unless (or (integerp elt) (symbolp elt)) (setq ok nil)))
           (setq tail (cdr tail)))
         (and ok (null tail)))))

(defun keymap--push-key-description (ch)
  "push_key_description: the text of the character CH with modifier bits."
  (let* ((c (logand ch (logior #x8000000 (lognot (- #x8000000)))))
         (c2 (logand c (lognot (logior #x400000 #x4000000 #x1000000
                                       #x8000000 #x2000000 #x800000))))
         (out ""))
    (if (not (characterp c2))
        (format "[%d]" c)
      (let ((tab-as-ci (and (= c2 ?\t) (/= 0 (logand c #x8000000)))))
        (when (/= 0 (logand c #x400000))
          (setq out (concat out "A-") c (- c #x400000)))
        (when (or (/= 0 (logand c #x4000000))
                  (and (< c2 ?\s) (/= c2 27) (/= c2 ?\t) (/= c2 13))
                  tab-as-ci)
          (setq out (concat out "C-") c (logand c (lognot #x4000000))))
        (when (/= 0 (logand c #x1000000))
          (setq out (concat out "H-") c (- c #x1000000)))
        (when (/= 0 (logand c #x8000000))
          (setq out (concat out "M-") c (- c #x8000000)))
        (when (/= 0 (logand c #x2000000))
          (setq out (concat out "S-") c (- c #x2000000)))
        (when (/= 0 (logand c #x800000))
          (setq out (concat out "s-") c (- c #x800000)))
        (cond
         ((< c ?\s)
          (setq out (concat out
                            (cond ((= c 27) "ESC")
                                  (tab-as-ci "i")
                                  ((= c ?\t) "TAB")
                                  ((= c 13) "RET")
                                  ((and (> c 0) (<= c 26)) (string (+ c 96)))
                                  (t (string (+ c 64)))))))
         ((= c 127) (setq out (concat out "DEL")))
         ((= c ?\s) (setq out (concat out "SPC")))
         (t (setq out (concat out (string c)))))
        out))))

(defun single-key-description (key &optional no-angles)
  "Return a pretty description of a character event KEY.
Control characters turn into C-whatever, etc.
Optional argument NO-ANGLES non-nil means don't put angle brackets
around function keys and event symbols."
  (when (keymap--lucid-event-type-list-p key)
    (setq key (event-convert-list key)))
  (if (and (consp key) (integerp (car key)) (integerp (cdr key)))
      (concat (single-key-description (car key) no-angles) ".."
              (single-key-description (cdr key) no-angles))
    (when (consp key) (setq key (car key)))
    (cond
     ((integerp key) (keymap--push-key-description key))
     ((symbolp key)
      (if no-angles
          (symbol-name key)
        (let* ((name (symbol-name key)) (len (length name)) (i 0))
          (while (and (< i (- len 3)) (eq (aref name (1+ i)) ?-)
                      (memq (aref name i) '(?C ?M ?S ?s ?H ?A)))
            (setq i (+ i 2)))
          (concat (substring name 0 i) "<" (substring name i) ">"))))
     ((stringp key) (copy-sequence key))
     (t (error "KEY must be an integer, cons, symbol, or string")))))

(defun key-description (keys &optional prefix)
  "Return a pretty description of a character sequence KEYS.
Optional arg PREFIX is the sequence of keys leading up to KEYS.
For example, [?\\C-x ?l] is converted into the string \"C-x l\"."
  (length keys)
  (length prefix)
  (let ((parts nil) (add-meta nil))
    (dolist (seq (list prefix keys))
      (let ((items
             (cond ((null seq) nil)
                   ((stringp seq)
                    (mapcar (lambda (c)
                              (if (and (< c 256) (/= 0 (logand c #o200)))
                                  (logxor c (logior #o200 #x8000000))
                                c))
                            (append seq nil)))
                   ((vectorp seq) (append seq nil))
                   ((consp seq) seq)
                   (t (signal 'wrong-type-argument (list 'arrayp seq))))))
        (while items
          (let ((key (car items)) (skip nil))
            (cond
             (add-meta
              (if (or (not (integerp key)) (eq key 27)
                      (/= 0 (logand key #x8000000)))
                  (progn
                    (push (single-key-description 27) parts)
                    (when (eq key 27) (setq skip t)))
                (setq key (logior key #x8000000)))
              (unless skip (setq add-meta nil)))
             ((eq key 27) (setq add-meta t skip t)))
            (unless skip (push (single-key-description key) parts)))
          (setq items (cdr items)))))
    (when add-meta (push (single-key-description 27) parts))
    (mapconcat #'identity (nreverse parts) " ")))

(defun command-remapping (command &optional _position keymaps)
  "Return the remapping for command COMMAND.
Return nil if COMMAND is not remapped (or not a symbol).

If the optional argument POSITION is non-nil, it specifies a mouse
position as returned by `event-start' and `event-end', and the
remapping occurs in the keymaps associated with it.  It can also be a
number or marker, in which case the keymap properties at the specified
buffer position instead of point are used.  The KEYMAPS argument is
ignored if POSITION is non-nil.

If the optional argument KEYMAPS is non-nil, it should be a keymap or
list of keymaps to search for command remapping.  Otherwise, search for the
remapping in all currently active keymaps."
  (and (symbolp command)
       keymaps
       (let ((found nil)
             (maps (if (keymapp keymaps) (list keymaps) keymaps)))
         (while (and maps (not found))
           (let ((r (lookup-key (car maps) (vector 'remap command))))
             (when (and r (not (integerp r))) (setq found r)))
           (setq maps (cdr maps)))
         found)))

;; subr.el's `event-basic-type' reads the `event-symbol-elements' cache, which a
;; symbol has only after something parsed it; Emacs parses every function key
;; the keyboard layer or a keymap has met by the time a script runs, so the
;; common call -- `(event-basic-type 'f1)' -- answers `f1' there.  This parses
;; on demand, which gives that answer without a startup table.
(defun event-basic-type (event)
  "Return the basic type of the given event (all modifiers removed).
The value is a printing character (not upper case) or a symbol.
EVENT may be an event or an event type."
  (unless (stringp event)
    (if (consp event)
        (setq event (car event)))
    (if (symbolp event)
        (car (internal-event-symbol-parse-modifiers event))
      (let* ((base (logand event (1- ?\A-\0)))
             (uncontrolled (if (< base 32) (logior base 64) base)))
        (condition-case ()
            (downcase uncontrolled)
          (error uncontrolled))))))

;;;; Char-table keymaps (keymap.c) and `map-char-table' (chartab.c).

(defun map-char-table (function char-table)
  "Call FUNCTION for each character in CHAR-TABLE that has non-nil value.
FUNCTION is called with two arguments, KEY and VALUE.
KEY is a character code or a cons of character codes specifying a
range of characters that have the same value.
VALUE is what (aref CHAR-TABLE CHAR) returns."
  (let ((range (cons 0 #x3FFFFF)) (val nil) (first t))
    (dolist (run (elisprs--char-table-runs char-table))
      (let ((c (car run)))
        (unless first
          (when val
            (setcdr range (1- c))
            (funcall function (if (eq (car range) (cdr range)) (car range) range)
                     val))
          (setcar range c)
          (setcdr range #x3FFFFF))
        (setq val (cdr run) first nil)))
    (when val
      (funcall function (if (eq (car range) (cdr range)) (car range) range) val))
    nil))

(defun make-keymap (&optional string)
  "Construct and return a new keymap, of the form (keymap CHARTABLE . ALIST).
CHARTABLE is a char-table that holds the bindings for all characters
without modifiers.  All entries in it are initially nil, meaning
\"command undefined\".  ALIST is an assoc-list which holds bindings for
function keys, mouse events, and any other things that appear in the
input stream.  Initially, ALIST is nil.

The optional arg STRING supplies a menu name for the keymap
in case you use it as a menu with `x-popup-menu'."
  (cons 'keymap
        (cons (make-char-table 'keymap)
              (and string (list string)))))

(defun keymap--char-event-p (event)
  (and (natnump event) (< event #x400000)))

;; A char-table or vector among KM's own bindings that holds EVENT.
(defun keymap--slot-holder (km event)
  (let ((tail (cdr km)) (res nil))
    (while (and (consp tail) (not res))
      (let ((el (car tail)))
        (cond ((eq el 'keymap) (setq tail nil))
              ((and (char-table-p el) (keymap--char-event-p event))
               (setq res el))
              ((and (vectorp el) (natnump event) (< event (length el)))
               (setq res el))
              (t (setq tail (cdr tail))))))
    res))

(defun keymap--set-binding (km event def)
  (let ((slot (keymap--slot-holder km event)))
    (if slot
        (aset slot event (if (and (char-table-p slot) (null def)) t def))
      (let ((cell (keymap--own-binding km event)))
        (if cell
            (setcdr cell def)
          (setcdr km (cons (cons event def) (cdr km))))))))

(defun keymap--remove-binding (km event)
  (let ((slot (keymap--slot-holder km event)))
    (if slot
        (aset slot event nil)
      (let ((cell (keymap--own-binding km event)))
        (when cell (setcdr km (delq cell (cdr km))))))))

(defun keymap--own-binding (km event)
  (let ((tail (cdr km)) (res nil))
    (while (and (consp tail) (not res))
      (let ((el (car tail)))
        (cond ((eq el 'keymap) (setq tail nil))
              ((and (char-table-p el) (keymap--char-event-p event)
                    (aref el event))
               (setq res (cons event (let ((v (aref el event)))
                                       (if (eq v t) nil v)))))
              ((and (vectorp el) (natnump event) (< event (length el))
                    (aref el event))
               (setq res (cons event (aref el event))))
              ((and (consp el) (equal (car el) event)) (setq res el))
              (t (setq tail (cdr tail))))))
    res))

(defun lookup-key--event (km event accept-default)
  (let ((tail (cdr km)) (res nil) (deflt nil) (done nil))
    (while (and (consp tail) (not done))
      (let ((el (car tail)))
        (cond
         ((eq el 'keymap)
          (setq res (lookup-key--event tail event accept-default) done t))
         ((and (consp el) (eq (car el) 'keymap))
          (let ((r (lookup-key--event el event accept-default)))
            (when r (setq res r done t))))
         ((and (char-table-p el) (keymap--char-event-p event))
          (let ((v (aref el event)))
            (when v
              (setq res (if (eq v t) nil v) done t))))
         ((and (vectorp el) (natnump event) (< event (length el)))
          (setq res (aref el event) done t))
         ((and (consp el) (equal (car el) event))
          (setq res (keymap--get-keyelt (cdr el)) done t))
         ((and (consp el) (eq (car el) t))
          (setq deflt (keymap--get-keyelt (cdr el))))))
      (unless done (setq tail (cdr tail))))
    (or res (and accept-default deflt))))

;; map_keymap_internal also walks a char-table element.
(defun keymap--map-internal (function map)
  (let ((tail (if (and (consp map) (eq (car map) 'keymap)) (cdr map) map))
        (done nil))
    (while (and (not done) (consp tail) (not (eq (car tail) 'keymap)))
      (let ((binding (car tail)))
        (cond
         ((keymap--keymapp binding) (setq done t))
         ((consp binding)
          (keymap--item function (car binding) (cdr binding)))
         ((vectorp binding)
          (let ((c 0) (n (length binding)))
            (while (< c n)
              (keymap--item function c (aref binding c))
              (setq c (1+ c)))))
         ((char-table-p binding)
          (map-char-table
           (lambda (key val)
             (when val
               (keymap--item function
                             (if (consp key) (cons (car key) (cdr key)) key)
                             val)))
           binding))))
      (unless done (setq tail (cdr tail))))
    tail))

;; store_in_keymap: a binding that fits no existing slot goes right after the
;; last char-table or vector among the keymap's own elements.
(defun keymap--set-binding (km event def)
  (let ((slot (keymap--slot-holder km event)))
    (if slot
        (aset slot event (if (and (char-table-p slot) (null def)) t def))
      (let ((cell (keymap--own-binding km event)))
        (if cell
            (setcdr cell def)
          (let ((insertion km) (tail (cdr km)))
            (while (and (consp tail) (not (eq (car tail) 'keymap)))
              (when (or (char-table-p (car tail)) (vectorp (car tail)))
                (setq insertion tail))
              (setq tail (cdr tail)))
            (setcdr insertion (cons (cons event def) (cdr insertion)))))))))

;;;; accessible-keymaps, where-is-internal (keymap.c).

(defun keymap--get-keyelt (object)
  "get_keyelt: the binding itself of a possible menu item."
  (let ((done nil))
    (while (not done)
      (cond
       ((not (consp object)) (setq done t))
       ((eq (car object) (quote menu-item))
        (if (consp (cdr object))
            (progn (setq object (cdr (cdr object)))
                   (when (consp object) (setq object (car object))))
          (setq done t)))
       ((stringp (car object)) (setq object (cdr object)))
       (t (setq done t))))
    object))

(defun accessible-keymaps (keymap &optional prefix)
  "Find all keymaps accessible via prefix characters from KEYMAP.
Returns a list of elements of the form (KEYS . MAP), where the sequence
KEYS starting from KEYMAP gets you to MAP.  These elements are ordered
so that the KEYS increase in length.  The first element is ([] . KEYMAP).
An optional argument PREFIX, if non-nil, should be a key sequence;
then the value includes only maps for prefixes that start with PREFIX."
  (let* ((prefixlen (if prefix (length prefix) 0))
         (maps nil))
    (if (and prefix (> prefixlen 0))
        (let ((tem (lookup-key keymap prefix t)))
          (unless (keymap--keymapp tem)
            (setq tem nil))
          (setq maps (and tem (list (cons (if (stringp prefix)
                                              (vconcat prefix)
                                            prefix)
                                          (keymap--get-keymap tem t nil))))))
      (setq maps (list (cons [] (keymap--get-keymap keymap t nil)))))
    (let ((tail maps))
      (while (consp tail)
        (let* ((thisseq (car (car tail)))
               (thismap (cdr (car tail)))
               (last (1- (length thisseq)))
               (is-metized (and (>= last 0) (>= last prefixlen)
                                (eq (aref thisseq last) 27)))
               (this-tail tail))
          (when (consp thismap)
            (keymap--map
             (lambda (key cmd)
               (setq cmd (keymap--get-keymap (keymap--get-keyelt cmd) nil nil))
               (when cmd
                 (let ((search maps) (cycle nil) found)
                   (while (and (not cycle)
                               (setq found (rassq cmd search)))
                     (let* ((prefix* (car found)) (lim (length prefix*)))
                       (when (<= lim (length thisseq))
                         (let ((i 0))
                           (while (and (< i lim)
                                       (eq (aref prefix* i) (aref thisseq i)))
                             (setq i (1+ i)))
                           (when (>= i lim) (setq cycle t))))
                       (setq search (cdr (memq found search)))))
                   (unless cycle
                     (if (and is-metized (integerp key))
                         (let ((tem (copy-sequence thisseq)))
                           (aset tem last (logior key #x8000000))
                           (setcdr this-tail
                                   (cons (cons tem cmd) (cdr this-tail))))
                       (nconc this-tail
                              (list (cons (vconcat thisseq (list key))
                                          cmd))))))))
             thismap nil)))
        (setq tail (cdr tail))))
    maps))

(defun keymap--preferred-sequence-p (seq)
  "preferred_sequence_p: 2 for an all-character sequence with a plain key,
1 for one with only modified characters, 0 when an event is not a character."
  (let ((result 1) (len (length seq)) (i 0) (bad nil))
    (while (and (not bad) (< i len))
      (let ((elt (aref seq i)))
        (if (not (integerp elt))
            (setq bad t)
          (let ((modifiers (logand elt (logand (lognot #x8000000) #xFC00000))))
            (cond ((= modifiers 0) (setq result 2))
                  (t (setq bad t))))))
      (setq i (1+ i)))
    (if bad 0 result)))

(defun keymap--shadow-lookup (keymaps key remap)
  (let ((res nil) (done nil))
    (while (and keymaps (not done))
      (let ((value (lookup-key (car keymaps) key)))
        (cond
         ((and (integerp value) (natnump value))
          (when (lookup-key (car keymaps) (substring key 0 value))
            (setq res nil done t)))
         (value
          (setq res (or (and remap (symbolp value)
                             (command-remapping value nil keymaps))
                        value)
                done t))))
      (setq keymaps (cdr keymaps)))
    res))

(defun keymap--where-is-sequences (definition keymaps noindirect)
  "where_is_internal: every key sequence in KEYMAPS bound to DEFINITION."
  (let ((sequences nil))
    (dolist (map keymaps)
      (dolist (entry (accessible-keymaps (keymap--get-keymap map t nil)))
        (let ((this (car entry)) (submap (cdr entry)))
          (keymap--map
           (lambda (key binding)
             (setq binding (if noindirect binding (keymap--get-keyelt binding)))
             (when (or (eq binding definition)
                       (and (consp definition) (equal binding definition)))
               (let ((seq (if (and (> (length this) 0)
                                   (eq (aref this (1- (length this))) 27)
                                   (integerp key)
                                   (< key 128))
                              (let ((tem (copy-sequence this)))
                                (aset tem (1- (length tem)) (logior key #x8000000))
                                tem)
                            (vconcat this (list key)))))
                 (unless (member seq sequences)
                   (push seq sequences)))))
           submap nil))))
    (nreverse sequences)))

(defun where-is-internal (definition &optional keymap firstonly noindirect no-remap)
  "Return list of keys that invoke DEFINITION.
If KEYMAP is a keymap, search only KEYMAP and the global keymap.
If KEYMAP is nil, search all the currently active keymaps, except
 for `overriding-local-map' (which is ignored).
If KEYMAP is a list of keymaps, search only those keymaps.

If optional 3rd arg FIRSTONLY is non-nil, return the first key sequence found,
rather than a list of all possible key sequences.
If FIRSTONLY is the symbol `non-ascii', return the first binding found,
no matter what it is.
If FIRSTONLY has another non-nil value, prefer bindings
that use the modifier key specified in `where-is-preferred-modifier'
\(or their meta variants) and entirely reject menu bindings.

If optional 4th arg NOINDIRECT is non-nil, don't extract the commands inside
menu-items.  This makes it possible to search for a menu-item itself.

The optional 5th arg NO-REMAP alters how command remapping is handled:

- If another command OTHER-COMMAND is remapped to DEFINITION, normally
  search for the bindings of OTHER-COMMAND and include them in the
  returned list.  But if NO-REMAP is non-nil, include the vector
  [remap OTHER-COMMAND] in the returned list instead, without
  searching for those other bindings.

- If DEFINITION is remapped to OTHER-COMMAND, normally return the
  bindings for OTHER-COMMAND.  But if NO-REMAP is non-nil, return the
  bindings for DEFINITION instead, ignoring its remapping."
  (let* ((keymaps (cond ((and (consp keymap) (keymap--keymapp (car keymap)))
                         keymap)
                        (keymap (list keymap))
                        (t nil)))
         (sequences (keymap--where-is-sequences definition keymaps noindirect))
         (remapped-sequences nil) (remapped nil)
         (found nil) (result 'unset))
    (while (and (eq result 'unset)
                (or sequences
                    (and (not remapped)
                         (progn (setq sequences remapped-sequences
                                      remapped t)
                                sequences))))
      (let ((sequence (car sequences)) (skip nil))
        (setq sequences (cdr sequences))
        (unless (equal (keymap--shadow-lookup keymaps sequence remapped)
                       definition)
          (setq skip t))
        (when (and (not skip) (not no-remap) (not remapped)
                   (vectorp sequence) (= (length sequence) 2)
                   (eq (aref sequence 0) 'remap) (symbolp (aref sequence 1)))
          (let ((seqs (keymap--where-is-sequences (aref sequence 1) keymaps
                                                  noindirect)))
            (setq remapped-sequences
                  (nconc (reverse seqs) remapped-sequences)))
          (setq skip t))
        (unless skip
          (when (and (> (length sequence) 0)
                     (stringp (aref sequence (1- (length sequence)))))
            (aset sequence (1- (length sequence)) "(any string)"))
          (unless (member sequence found) (push sequence found))
          (cond ((eq firstonly 'non-ascii) (setq result sequence))
                ((and firstonly (= 2 (keymap--preferred-sequence-p sequence)))
                 (setq result sequence))))))
    (cond ((not (eq result 'unset)) result)
          (t (setq found (nreverse found))
             (if firstonly (car found) found)))))

;;;; keymap.c — the global map.

(defvar keymap--current-global-map nil
  "keymap.c's `current_global_map'.")

(defun use-global-map (keymap)
  "Select KEYMAP as the global keymap."
  (setq keymap--current-global-map (keymap--get-keymap keymap t t))
  nil)

(defun current-global-map ()
  "Return the current global keymap."
  keymap--current-global-map)

;;;; subr.el — the global keymap tree (verbatim).  bindings.el, which fills
;;;; it with the rest of the standard bindings, is not loaded.

(defvar esc-map
  (let ((map (make-keymap)))
    (define-key map "u" #'upcase-word)
    (define-key map "l" #'downcase-word)
    (define-key map "c" #'capitalize-word)
    (define-key map "x" #'execute-extended-command)
    (define-key map "X" #'execute-extended-command-for-buffer)
    map)
  "Default keymap for ESC (meta) commands.
The normal global definition of the character ESC indirects to this keymap.")
(fset 'ESC-prefix esc-map)
(make-obsolete 'ESC-prefix 'esc-map "28.1")

(defvar ctl-x-4-map (make-sparse-keymap)
  "Keymap for subcommands of \\`C-x 4'.")
(defalias 'ctl-x-4-prefix ctl-x-4-map)

(defvar ctl-x-5-map (make-sparse-keymap)
  "Keymap for frame commands.")
(defalias 'ctl-x-5-prefix ctl-x-5-map)

(defvar tab-prefix-map (make-sparse-keymap)
  "Keymap for tab-bar related commands.")

(defvar ctl-x-map
  (let ((map (make-keymap)))
    (define-key map "4" 'ctl-x-4-prefix)
    (define-key map "5" 'ctl-x-5-prefix)
    (define-key map "t" tab-prefix-map)

    (define-key map "b" #'switch-to-buffer)
    (define-key map "k" #'kill-buffer)
    (define-key map "\C-u" #'upcase-region)   (put 'upcase-region   'disabled t)
    (define-key map "\C-l" #'downcase-region) (put 'downcase-region 'disabled t)
    (define-key map "<" #'scroll-left)
    (define-key map ">" #'scroll-right)
    map)
  "Default keymap for \\`C-x' commands.
The normal global definition of the character \\`C-x' indirects to this
keymap.")
(fset 'Control-X-prefix ctl-x-map)
(make-obsolete 'Control-X-prefix 'ctl-x-map "28.1")

(defvar global-map
  (let ((map (make-keymap)))
    (define-key map "\C-[" 'ESC-prefix)
    (define-key map "\C-x" 'Control-X-prefix)

    (define-key map "\C-i" #'self-insert-command)
    (let* ((vec1 (make-vector 1 nil))
           (f (lambda (from to)
                (while (< from to)
                  (aset vec1 0 from)
                  (define-key map vec1 #'self-insert-command)
                  (setq from (1+ from))))))
      (funcall f #o040 #o0177)
      (when (eq system-type 'ms-dos)      ;FIXME: Why?
        (funcall f #o0200 #o0240))
      (funcall f #o0240 #o0400))

    (define-key map "\C-a" #'beginning-of-line)
    (define-key map "\C-b" #'backward-char)
    (define-key map "\C-e" #'end-of-line)
    (define-key map "\C-f" #'forward-char)

    (define-key map "\C-z"     #'suspend-emacs) ;FIXME: Re-bound later!
    (define-key map "\C-x\C-z" #'suspend-emacs) ;FIXME: Re-bound later!

    (define-key map "\C-v"    #'scroll-up-command)
    (define-key map "\M-v"    #'scroll-down-command)
    (define-key map "\M-\C-v" #'scroll-other-window)

    (define-key map "\M-\C-c" #'exit-recursive-edit)
    (define-key map "\C-]"    #'abort-recursive-edit)
    map)
  "Default global keymap mapping Emacs keyboard input into commands.
The value is a keymap that is usually (but not necessarily) Emacs's
global map.

See also `current-global-map'.")
(use-global-map global-map)