Function: quail-insert-decode-map

quail-insert-decode-map is a byte-compiled function defined in quail.el.gz.

Signature

(quail-insert-decode-map DECODE-MAP)

Documentation

Insert pairs of key sequences vs the corresponding translations.

These are stored in DECODE-MAP using the concise format. DECODE-MAP should be made by quail-build-decode-map (which see).

Source Code

;; Defined in /usr/src/emacs/lisp/international/quail.el.gz
(defun quail-insert-decode-map (decode-map)
  "Insert pairs of key sequences vs the corresponding translations.
These are stored in DECODE-MAP using the concise format.  DECODE-MAP
should be made by `quail-build-decode-map' (which see)."
  (setq decode-map
	(sort (cdr decode-map)
              (lambda (x y)
                (setq x (car x) y (car y))
                (or (> (length x) (length y))
                    (and (= (length x) (length y))
                         (not (string< x y)))))))
  (let ((window-width (window-width (get-buffer-window
                                     (current-buffer) 'visible)))
	(single-trans-width 4)
	(single-list nil)
	(multiple-list nil)
	trans)
    ;; Divide the elements of decoding map into single ones (i.e. the
    ;; one that has single translation) and multiple ones (i.e. the
    ;; one that has multiple translations).
    (dolist (elt decode-map)
      (setq trans (cdr elt))
      (if (and (vectorp trans) (= (length trans) 1))
	  (setq trans (aref trans 0)))
      (if (vectorp trans)
	  (push elt multiple-list)
	(push (cons (car elt) trans) single-list)
        (let ((width (if (stringp trans) (string-width trans)
                       (char-width trans))))
          (if (> width single-trans-width)
              (setq single-trans-width width)))))
    (when single-list
      ;; Figure out how many columns can fit.
      (let* ((len (length single-list))
             ;; The longest key is at the end, by virtue of the above `sort'.
             (max-key-width (max 3 (length (caar (last single-list)))))
             ;; Starting point: worst case.
             (col-width (+ max-key-width 1 single-trans-width 1))
             (cols (/ window-width col-width))
             rows)
        ;; Now, let's see if we can pack in a few more columns since
        ;; the first columns can often be made narrower thanks to the
        ;; length-sorting.
        (while (let ((newrows (/ (+ len cols) (1+ cols))) ;Round up.
                     (width 0))
                 (dotimes (col (1+ cols))
                   (let ((last-col-elt (or (nth (1- (* (1+ col) newrows))
                                                single-list)
                                           (car (last single-list)))))
                     (cl-incf width (+ (max 3 (length (car last-col-elt)))
                                       1 single-trans-width 1))))
                 (< width window-width))
          (cl-incf cols))
        (setq rows (/ (+ len cols -1) cols)) ;Round up.
        (let ((key-width (max 3 (length (car (nth (1- rows) single-list))))))
          (insert "key")
          (quail-indent-to (1+ key-width))
          (insert "char")
          (quail-indent-to (+ 1 key-width 1 single-trans-width 1)))
        (insert "[type a key sequence to insert the corresponding character]\n")
        (let ((pos (point))
              (col 0))
          (insert-char ?\n (+ rows 2))
          (while single-list
            (goto-char pos)
            (let* ((key-width (max 3 (length
                                      (car (or (nth (1- rows) single-list)
                                               (car (last single-list)))))))
                   (col-width (+ key-width 1 single-trans-width 1)))
              ;; Insert the header-line.
              (move-to-column col)
              (quail-indent-to col)
              (insert-char ?- key-width)
              (insert ?\s)
              (insert-char ?- single-trans-width)
              (forward-line 1)
              ;; Insert the key-tran pairs.
              (dotimes (_ rows)
                (let ((elt (pop single-list)))
                  (when elt
                    (move-to-column col)
                    (quail-indent-to col)
                    (insert (propertize (car elt)
                                        'face 'font-lock-comment-face))
                    (quail-indent-to (+ col key-width 1))
                    (insert (cdr elt))
                    (forward-line 1))))
              (setq col (+ col col-width)))))
        (goto-char (point-max))))

    (when multiple-list
      ;; Since decode-map is sorted, we known the longest key is at the end.
      (let ((max-key-width (max 3 (length (caar (last multiple-list))))))
        (insert "key")
        (quail-indent-to (1+ max-key-width))
        (insert "character(s)  [type a key (sequence) and select one from the list]\n")
        (insert-char ?- max-key-width)
        (insert " ------------\n")
        (dolist (elt multiple-list)
          (insert (propertize (car elt)
                              'face 'font-lock-comment-face))
          (quail-indent-to max-key-width)
          (if (vectorp (cdr elt))
              (mapc (lambda (x)
                      (let ((width (if (integerp x) (char-width x)
                                     (string-width x))))
                        (when (> (+ (current-column) 1 width) window-width)
                          (insert "\n")
                          (quail-indent-to max-key-width))
                        (insert " " x)))
                    (cdr elt))
            (insert " " (cdr elt)))
          (insert ?\n))
        (insert ?\n)))))