]> code.delx.au - gnu-emacs/blobdiff - lisp/international/quail.el
Fix stack overflow in string creation (Bug#6214).
[gnu-emacs] / lisp / international / quail.el
index 8f48e327f5799add27e7d701692b96ff645900e0..9959b275943075c7300a4bee8ea5a9d3c454b704 100644 (file)
@@ -1,9 +1,9 @@
 ;;; quail.el --- provides simple input method for multilingual text
 
 ;; Copyright (C) 1997, 1998, 2000, 2001, 2002, 2003, 2004, 2005,
-;;   2006, 2007, 2008  Free Software Foundation, Inc.
+;;   2006, 2007, 2008, 2009, 2010  Free Software Foundation, Inc.
 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
-;;   2005, 2006, 2007, 2008
+;;   2005, 2006, 2007, 2008, 2009, 2010
 ;;   National Institute of Advanced Industrial Science and Technology (AIST)
 ;;   Registration Number H14PRO021
 
@@ -54,6 +54,7 @@
 ;;; Code:
 
 (require 'help-mode)
+(eval-when-compile (require 'cl))
 
 (defgroup quail nil
   "Quail: multilingual input method."
@@ -830,10 +831,6 @@ The format of KBD-LAYOUT is the same as `quail-keyboard-layout'."
              (insert-char 32 (+ row (/ (- row 2) 2)))))
        (setq lower (aref layout i)
              upper (aref layout (1+ i)))
-       (if (and (integerp lower) (>= lower 128) (< lower 256))
-           (setq lower (unibyte-char-to-multibyte lower)))
-       (if (and (integerp upper) (>= upper 128) (< upper 256))
-           (setq upper (unibyte-char-to-multibyte upper)))
        (insert bar)
        (if (= (if (stringp lower) (string-width lower) (char-width lower)) 1)
            (insert " "))
@@ -1024,9 +1021,8 @@ the following annotation types are supported.
        (while l
          (setq key (car (car l)) trans (car (cdr (car l))) l (cdr l))
          (quail-defrule-internal key trans map t decode-map props))
-       `(if (not (quail-decode-map))
-            (quail-install-map ',map)
-          (quail-install-map ',map)
+       `(if (prog1 (quail-decode-map)
+              (quail-install-map ',map))
           (quail-install-decode-map ',decode-map))))))
 
 ;;;###autoload
@@ -1093,6 +1089,13 @@ to the current translations for KEY instead of replacing them."
        (setq quail-current-package package)))
   (quail-defrule-internal key translation (quail-map) append))
 
+(defun quail-vunion (v1 v2)
+  (apply 'vector
+         ;; No idea why this was here, but it seems to cause the
+         ;; incorrect ordering, according to Nils Anders Danielsson.
+         ;; (nreverse
+         (delete-dups (nconc (append v1 ()) (append v2 ()))))) ;; )
+
 ;;;###autoload
 (defun quail-defrule-internal (key trans map &optional append decode-map props)
   "Define KEY as TRANS in a Quail map MAP.
@@ -1175,17 +1178,20 @@ function `quail-define-rules' for the detail."
                  (setcdr decode-map
                          (cons (cons elt key) (cdr decode-map)))))))
        (if (and (car map) append)
-           (let ((prev (quail-get-translation (car map) key len)))
-             (if (integerp prev)
-                 (setq prev (vector prev))
-               (setq prev (cdr prev)))
+           (let* ((prev (quail-get-translation (car map) key len))
+                   (prevchars (if (integerp prev)
+                                  (vector prev)
+                                (cdr prev))))
              (if (integerp trans)
                  (setq trans (vector trans))
                (if (stringp trans)
                    (setq trans (string-to-vector trans))))
+              (let ((new (quail-vunion prevchars trans)))
              (setq trans
-                   (cons (list 0 0 0 0 nil)
-                         (vconcat prev trans)))))
+                      (if (equal new prevchars)
+                          ;; Nothing to change, get back to orig value.
+                          prev
+                        (cons (list 0 0 0 0 nil) new))))))
        (setcar map trans)))))
 
 (defun quail-get-translation (def key len)
@@ -1287,11 +1293,12 @@ If STR has `advice' text property, append the following special event:
 \(quail-advice STR)"
   (let ((events (mapcar
                 (lambda (c)
-                  ;; This gives us the chance to unify on input
-                  ;; (e.g. using ucs-tables.el).
-                  (or (and translation-table-for-input
-                           (aref translation-table-for-input c))
-                      c))
+                  (or
+                   ;; Avoid "obsolete" warnings for translation-table-for-input.
+                   (with-no-warnings
+                     (and translation-table-for-input
+                          (aref translation-table-for-input c)))
+                   c))
                 str)))
     (if (or (get-text-property 0 'advice str)
            (next-single-property-change 0 'advice str))
@@ -1358,7 +1365,7 @@ Return the input string."
       (let* ((echo-keystrokes 0)
             (help-char nil)
             (overriding-terminal-local-map (quail-translation-keymap))
-            (generated-events nil)
+            (generated-events nil)     ;FIXME: What is this?
             (input-method-function nil)
             (modified-p (buffer-modified-p))
             last-command-event last-command this-command)
@@ -1416,7 +1423,7 @@ Return the input string."
       (let* ((echo-keystrokes 0)
             (help-char nil)
             (overriding-terminal-local-map (quail-conversion-keymap))
-            (generated-events nil)
+            (generated-events nil)     ;FIXME: What is this?
             (input-method-function nil)
             (modified-p (buffer-modified-p))
             last-command-event last-command this-command)
@@ -1637,7 +1644,7 @@ Make RELATIVE-INDEX the current translation."
              (maxcol (- (window-width)
                         quail-guidance-translations-starting-column))
              (block (nth 3 indices))
-             col idx width trans num-items blocks)
+             col idx width trans num-items)
          (if (< cur start)
              ;; We must calculate from the head.
              (setq start 0 block 0)
@@ -1924,6 +1931,10 @@ Remaining args are for FUNC."
                    (frame-char-height) (* internal-border 2) (* border 2))))
     (if (< newtop 0)
        (setq newtop (+ top (frame-pixel-height) internal-border border)))
+    ;; If I leave the `parent-id' parameter, my frame ends up with 13 lines
+    ;; rather than just 1.  Not sure what is really going on, but
+    ;; clearly this parameter is not needed.  --Stef
+    (setq fparam (delq (assoc 'parent-id fparam) fparam))
     (make-frame (append '((user-position . t) (height . 1)
                          (minibuffer)
                          (menu-bar-lines . 0) (tool-bar-lines . 0))
@@ -1987,7 +1998,7 @@ minibuffer and the selected frame has no other windows)."
            ;; window system.
            (let ((guidance quail-guidance-str))
              (or (frame-live-p quail-guidance-frame)
-                 (setq quail-guidance-frame 
+                 (setq quail-guidance-frame
                        (quail-make-guidance-frame)))
              (or (buffer-live-p quail-guidance-buf)
                  (setq quail-guidance-buf
@@ -1996,14 +2007,15 @@ minibuffer and the selected frame has no other windows)."
                (erase-buffer)
                (setq cursor-type nil)
                (insert guidance))
-             (set-window-buffer (frame-root-window quail-guidance-frame)
-                                quail-guidance-buf)
+              (let ((win (frame-root-window quail-guidance-frame)))
+                (set-window-buffer win quail-guidance-buf)
+                (set-window-dedicated-p win t))
              (quail-minibuffer-message
               (format " [%s]" current-input-method-title)))
          ;; Show the guidance in the next line of the currrent
          ;; minibuffer.
          (quail-minibuffer-message
-          (format "  [%s]\n%s" 
+          (format "  [%s]\n%s"
                   current-input-method-title quail-guidance-str)))
       ;; Show the guidance in echo area without logging.
       (let ((message-log-max nil))
@@ -2176,7 +2188,7 @@ are shown (at most to the depth specified `quail-completion-max-depth')."
   (setq this-command 'quail-completion))
 
 (defun quail-completion-1 (key map indent)
-"List all completions of KEY in MAP with indentation INDENT."
+  "List all completions of KEY in MAP with indentation INDENT."
   (let ((len (length key)))
     (quail-indent-to indent)
     (insert key ":")
@@ -2187,20 +2199,12 @@ are shown (at most to the depth specified `quail-completion-max-depth')."
       (insert " -\n"))
     (setq indent (+ indent 2))
     (if (and (cdr map) (< (/ (1- indent) 2) quail-completion-max-depth))
-       (let ((l (cdr map))
-             (newkey (make-string (1+ len) 0))
-             (i 0))
+       (let ((l (cdr map)))
          (if (functionp l)
              (setq l (funcall l)))
-         ;; Set KEY in the first LEN characters of NEWKEY.
-         (while (< i len)
-           (aset newkey i (aref key i))
-           (setq i (1+ i)))
-         (setq l (reverse l))
-         (while l                      ; L = ((CHAR . DEFN) ....) ;
-           (aset newkey len (car (car l)))
-           (quail-completion-1 newkey (cdr (car l)) indent)
-           (setq l (cdr l)))))))
+         (dolist (elt (reverse l))     ; L = ((CHAR . DEFN) ....) ;
+           (quail-completion-1 (concat key (string (car elt)))
+                                (cdr elt) indent))))))
 
 (defun quail-completion-list-translations (map key indent)
   "List all possible translations of KEY in Quail MAP with indentation INDENT."
@@ -2219,8 +2223,7 @@ are shown (at most to the depth specified `quail-completion-max-depth')."
       (setq translations (cdr translations))
       ;; Insert every 10 elements with indices in a line.
       (let ((len (length translations))
-           (i 0)
-           num)
+           (i 0))
        (while (< i len)
          (when (zerop (% i 10))
            (when (>= i 10)
@@ -2239,13 +2242,15 @@ are shown (at most to the depth specified `quail-completion-max-depth')."
 
 (defun quail-mouse-choose-completion (event)
   "Click on an alternative in the `*Quail Completions*' buffer to choose it."
-  (interactive "e")
   ;; This function is an exact copy of the mouse.el function
   ;; `mouse-choose-completion' except that we:
-  ;; 1) add two lines from `choose-completion' in simple.el to give
-  ;;    the `mouse-2' click a little more leeway.
   ;; 2) don't bury *Quail Completions* buffer, so comment a section, and
   ;; 3) delete/terminate the current quail selection here.
+  ;; FIXME: Consolidate with `choose-completion'.  The point number
+  ;; 1 has been done, already.  The point number 3 should be fairly
+  ;; easy to move to a choose-completion-string-function.  So all
+  ;; that's left is point number 2.
+  (interactive "e")
   ;; Give temporary modes such as isearch a chance to turn off.
   (run-hooks 'mouse-leave-buffer-hook)
   (let ((buffer (window-buffer))
@@ -2285,6 +2290,7 @@ are shown (at most to the depth specified `quail-completion-max-depth')."
 ;; previous implementation.
 (defun quail-choose-completion-string (choice &optional buffer base-size)
   (setq quail-current-str choice)
+  ;; FIXME: We need to pass `base-position' here.
   (choose-completion-string choice buffer))
 
 (defun quail-build-decode-map (map-list key decode-map num
@@ -2348,90 +2354,108 @@ should be made by `quail-build-decode-map' (which see)."
                                   (not (string< x y))))))))
   (let ((window-width (window-width (get-buffer-window
                                      (current-buffer) 'visible)))
-       (single-key-width 3)
        (single-trans-width 4)
-       (multiple-key-width 3)
        (single-list nil)
        (multiple-list nil)
-       elt trans width pos cols rows col row str col-width)
+       trans)
     ;; Divide the elements of decoding map into single ones (i.e. the
-    ;; one that has single translation) and multibyte ones (i.e. the
+    ;; one that has single translation) and multiple ones (i.e. the
     ;; one that has multiple translations).
-    (while decode-map
-      (setq elt (car decode-map) decode-map (cdr decode-map)
-           trans (cdr elt))
+    (dolist (elt decode-map)
+      (setq trans (cdr elt))
       (if (and (vectorp trans) (= (length trans) 1))
          (setq trans (aref trans 0)))
       (if (vectorp trans)
-         (setq multiple-list (cons elt multiple-list))
-       (setq single-list (cons (cons (car elt) trans) single-list)
-             width (if (stringp trans) (string-width trans)
-                     (char-width trans)))
-       (if (> width single-trans-width)
-           (setq single-trans-width width)))
-      (setq width (length (car elt)))
-      (if (> width single-key-width)
-         (setq single-key-width width))
-      (if (> width multiple-key-width)
-         (setq multiple-key-width width)))
+         (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
-      (setq col-width (+ single-key-width 1 single-trans-width 1)
-           cols (/ window-width col-width)
-           rows (/ (length single-list) cols))
-      (if (> (% (length single-list) cols) 0)
-         (setq rows (1+ rows)))
-      (insert "key")
-      (quail-indent-to (1+ single-key-width))
-      (insert "char")
-      (quail-indent-to (1+ col-width))
-      (insert "[type a key sequence to insert the corresponding character]\n")
-      (setq pos (point))
-      (insert-char ?\n (+ rows 2))
-      (goto-char pos)
-      (setq col (- col-width) row 0)
-      (while single-list
-       (setq elt (car single-list) single-list (cdr single-list))
-       (when (= (% row rows) 0)
-         (goto-char pos)
-         (setq col (+ col col-width))
-          (move-to-column col)
-          (quail-indent-to col)
-         (insert-char ?- single-key-width)
-         (insert ? )
-         (insert-char ?- single-trans-width)
-         (forward-line 1))
-       (move-to-column col)
-        (quail-indent-to col)
-       (insert (car elt))
-       (quail-indent-to (+ col single-key-width 1))
-       (insert (cdr elt))
-       (forward-line 1)
-       (setq row (1+ row)))
-      (goto-char (point-max)))
+      ;; 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)))))
+                     (incf width (+ (max 3 (length (car last-col-elt)))
+                                    1 single-trans-width 1))))
+                 (< width window-width))
+          (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 (row 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
-      (insert "key")
-      (quail-indent-to (1+ multiple-key-width))
-      (insert "character(s)  [type a key (sequence) and select one from the list]\n")
-      (insert-char ?- multiple-key-width)
-      (insert " ------------\n")
-      (while multiple-list
-       (setq elt (car multiple-list) multiple-list (cdr multiple-list))
-       (insert (car elt))
-       (quail-indent-to multiple-key-width)
-       (if (vectorp (cdr elt))
-           (mapc (function
-                  (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 multiple-key-width))
-                      (insert " " x))))
-                 (cdr elt))
-         (insert " " (cdr elt)))
-       (insert ?\n))
-      (insert ?\n))))
+      ;; 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 (function
+                     (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)))))
 
 (define-button-type 'quail-keyboard-layout-button
   :supertype 'help-xref
@@ -2524,13 +2548,12 @@ physical keyboard layout as specified with that variable.
          (insert "\n"))
 
        ;; Show key sequences.
-       (let ((decode-map (list 'decode-map))
-             elt pos num)
-         (setq num (quail-build-decode-map (list (quail-map)) "" decode-map
+       (let* ((decode-map (list 'decode-map))
+               (num (quail-build-decode-map (list (quail-map)) "" decode-map
                                             ;; We used to use 512 here, but
                                             ;; TeX has more than 1000 and
                                             ;; it's good to see the list.
-                                           0 5120 done-list))
+                                            0 5120 done-list)))
          (when (> num 0)
            (insert "
 KEY SEQUENCE
@@ -2561,8 +2584,8 @@ KEY BINDINGS FOR CONVERSION
        (run-hooks 'temp-buffer-show-hook)))))
 
 (defun quail-help-insert-keymap-description (keymap &optional header)
-  (let (pos1 pos2)
-    (setq pos1 (point))
+  (let ((pos1 (point))
+        pos2)
     (if header
        (insert header))
     (save-excursion
@@ -2646,10 +2669,12 @@ KEY BINDINGS FOR CONVERSION
          (or (string= key elt)
              (aset table char (list key elt))))
       (aset table char key))
-    (if (and translation-table-for-input
-            (setq char (aref translation-table-for-input char)))
-       (let ((translation-table-for-input nil))
-         (quail-store-decode-map-key table char key)))))
+    ;; Avoid "obsolete" warnings for translation-table-for-input.
+    (with-no-warnings
+      (if (and translation-table-for-input
+              (setq char (aref translation-table-for-input char)))
+         (let ((translation-table-for-input nil))
+           (quail-store-decode-map-key table char key))))))
 
 ;; Helper function for quail-gen-decode-map.  Store key strings to
 ;; type each character under MAP in TABLE (char-table).  MAP is an
@@ -2698,9 +2723,11 @@ KEY BINDINGS FOR CONVERSION
 
 (defsubst quail-char-equal-p (char target)
   (or (= char target)
-      (and translation-table-for-input
-          (setq char (aref translation-table-for-input char))
-          (= char target))))
+      ;; Avoid "obsolete" warnings for translation-table-for-input.
+      (with-no-warnings
+       (and translation-table-for-input
+            (setq char (aref translation-table-for-input char))
+            (= char target)))))
 
 ;; Helper function for quail-find-key.  Prepend key strings to type
 ;; for inputting CHAR by the current input method to KEY-LIST and
@@ -2935,7 +2962,7 @@ of each directory."
   (interactive "FDirectory of LEIM: ")
   (setq dirname (expand-file-name dirname))
   (let ((leim-list (expand-file-name leim-list-file-name dirname))
-       quail-dirs list-buf pkg-list pkg-buf pos)
+       quail-dirs list-buf pkg-list pos)
     (if (not (file-writable-p leim-list))
        (error "Can't write to file \"%s\"" leim-list))
     (message "Updating %s ..." leim-list)
@@ -3027,7 +3054,7 @@ of each directory."
 
     ;; At last, write out LEIM list file.
     (with-current-buffer list-buf
-      (let ((coding-system-for-write 'iso-2022-7bit))
+      (let ((coding-system-for-write 'utf-8))
        (save-buffer 0)))
     (kill-buffer list-buf)
     (message "Updating %s ... done" leim-list)))