all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* normalize.el
@ 2009-04-02  6:40 Kenichi Handa
  2009-04-02  8:10 ` normalize.el Kim F. Storm
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Kenichi Handa @ 2009-04-02  6:40 UTC (permalink / raw)
  To: emacs-devel; +Cc: kawabata.taichi

The attached is an Unicode normalization tool contributed by
Kawabata-san.  It performs all the Unicode normalization
NFC/NFD/NFKD/NFKC, and provides a coding system utf-8-hfs
that is suitable to be used for Mac OS 8.1's file names.

The assignment paper from Kawabata-san is already arrived at
FSF.

Perhaps committing it to the trunk now is not good
considering that we are already pretesting for 23.1.  What
should I do with it?  At least, I want Mac users to test it
by setting the default-file-name-coding-system to
`utf-8-hfs'.

---
Kenichi Handa
handa@m17n.org

;;; normalize.el --- Unicode normalization NFC/NFD/NFKD/NFKC

;; Copyright (C) 2009
;;   Free Software Foundation, Inc.

;; Author: Taichi Kawabata <kawabata.taichi@gmail.com>
;; Maintainer: Taichi Kawabata <kawabata.taichi@gmail.com>
;; Keywords: unicode, normalization

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; The following code will normalize the string or text according to
;; the NFC/NFD/NFKD (single step) NFKC (dual step).

;; This program has passed the NormalizationTest-4.1.0.txt except
;; Hangul normalizations.

;; References:
;; http://www.unicode.org/reports/tr15/
;; http://www.unicode.org/review/pr-29.html

;; Furthermore, it supports HFS normalization, for people who don't
;; want to normalize the CJK characters.

;;; Note on HFS Normalization.
;;
;; HFS-Normalization is the way to normalize text, as described by HFS
;; Plus Volue format specification
;; (http://developer.apple.com/technotes/tn/tn1150.html).  This is
;; useful espcially for CJK people, since it doesn't normlize CJK
;; compatibility characters.
;;
;; Normalization Exclusion Area:
;;  U+02000 .. U+02FFF :: Punctuation, symbols, dingbats, arrows, etc.
;;  U+0F900 .. U+0FAFF :: CJK compatibility Ideographs.
;;  U+2F800 .. U+2FFFF :: CJK compatibility Ideographs.

;;;
;;; Implementation Notes on NFC/HFS-NFC. (ccc = canonical combining class)
;;
;; A. `re-search-forward' for the following characters.
;;   (1) characters that should be decomposed anyway.
;;   (1-a) composition-exclusion characters
;;   (1-b) singleton characters
;;   (1-c) characters whose decomposed first char does not have ccc=0.
;;   (2) characters that may appear as second of composed character.
;;   (2-a) character whose ccc != 0.
;;   (2-b) character whose ccc = 0.
;;
;; B. In case of (1) in step A, translate character, go to the
;;    beginning position of translated region, and go back to step A.
;;
;; C. In case of (2) in step A, normalize the block around the
;;    searched character.  (`normalize-block')
;;
;;    (1) start of the block
;;        In case of (2-a) in step A, if the previous character
;;        exists, then the beginning of the block is the previous
;;        character.  In case of (2-b), if the previous character's
;;        ccc = 0, then the beginning of the block is the previous
;;        character.  Otherwise, the beginning of the block is the
;;        searched character.
;;    (2) end of the block
;;        from the searched character, the end of the block is
;;        searched until the character whose ccc=0.
;;
;; D. Normalize the block
;;    (1) decompose the block
;;    (2) sort the combining characters
;;    (3) compose the block.

;;;
;;; Implementation Notes on NFD/HFS-NFD.
;;
;; A. `re-search-forward' for the combining characters sequence.
;;
;; B. Normalize the block around the searched character sequence.
;;    (1) Start of the block is the previous character if it exists.
;;        If not, start of the block is the searched character.
;;    (2) End of the block is the end of the combining character
;;        sequence.
;;
;; C. Normalize the block
;;    (1) decompose the block
;;    (2) sort the combining characters
;;
;; D. Translate all the rest at once.

;;;
;;; Implementation Notes on NFKC/NFKD
;;
;; NFKC/NFKD is realized as applying NFC/NFD to the text which is
;; firstly translated by NFKD(-unique) decomposition patterns.

;;; Code:

(defconst normalize-composition-exclusions
  '(#x0958 #x0959 #x095A #x095B #x095C #x095D #x095E #x095F
    #x09DC #x09DD #x09DF #x0A33 #x0A36 #x0A59 #x0A5A #x0A5B
    #x0A5E #x0B5C #x0B5D #x0F43 #x0F4D #x0F52 #x0F57 #x0F5C
    #x0F69 #x0F76 #x0F78 #x0F93 #x0F9D #x0FA2 #x0FA7 #x0FAC
    #x0FB9 #xFB1D #xFB1F #xFB2A #xFB2B #xFB2C #xFB2D #xFB2E
    #xFB2F #xFB30 #xFB31 #xFB32 #xFB33 #xFB34 #xFB35 #xFB36
    #xFB38 #xFB39 #xFB3A #xFB3B #xFB3C #xFB3E #xFB40 #xFB41
    #xFB43 #xFB44 #xFB46 #xFB47 #xFB48 #xFB49 #xFB4A #xFB4B
    #xFB4C #xFB4D #xFB4E #x2ADC #x1D15E #x1D15F #x1D160 #x1D161
    #x1D162 #x1D163 #x1D164 #x1D1BB #x1D1BC #x1D1BD #x1D1BE
    #x1D1BF #x1D1C0)
 "Composition Exclusion Table.
The table is taken from
  http://www.unicode.org/Public/UNIDATA/CompositionExclusions-4.1.0.txt")

;; 
(eval-when-compile

  ;; generics
  (setq combining-chars nil)
  (setq decomp-pair-to-comp-hash (make-hash-table :test 'equal :size 2000)) ; 1003 in Unicode 4.1.0 (except hangul)

  ;; NFC/NFD
  (defun nfc-predicate (char) t)
  (setq nfc-search-chars nil)
  (setq nfc-singletons nil)
  (setq nfd-alist nil)

  ;; HFS-NFC/HFS-NFD
  (defun hfs-nfc-predicate (char)
    (or (and (>= char 0) (< char #x2000))
             (and (>= char #x3000) (< char #xf900))
             (and (>= char #xfb00) (< char #x2f800))
             (>= char #x30000)))
  (setq hfs-nfc-search-chars nil)
  (setq hfs-nfc-singletons nil)
  (setq hfs-nfd-alist nil)

  ;; NFKC/NFKD
  (setq nfkd-alist nil)

  ;; macros
  (defmacro register-database (predicate 
                               search-chars
                               singletons
                               decomposition-alist)
    `(when (apply #',predicate (list char))
       (let ((decomp-1st-ccc
              (get-char-code-property 
               (car decomposition) 'canonical-combining-class)))
         (setq ,decomposition-alist (cons (cons char (vconcat decomposition))
                                          ,decomposition-alist))
         (unless (memq char normalize-composition-exclusions)
           (if (= 1 (length decomposition))
               (setq ,singletons (cons char ,singletons))
             ;; add second char of decomposition for search-chars iff ccc = 0
             (if (= 0 (get-char-code-property (cadr decomposition)
                                              'canonical-combining-class))
                 (add-to-list ',search-chars (cadr decomposition))))
           ;; For fast normalization, the case of positive integer
           ;; `decomp-1st-ccc' means that the char should also be
           ;; considered as something like singleton.
           (if (and decomp-1st-ccc (/= 0 decomp-1st-ccc))
               (setq ,singletons (cons char ,singletons)))))))

  (let ((char 0) ccc decomposition)
    (while (< char #x30000) 
      (setq ccc (get-char-code-property
                 char 'canonical-combining-class))
      (setq decomposition (get-char-code-property 
                           char 'decomposition))
      ;; add all combining-char to fast search list.
      (if (and ccc (/= 0 ccc)) (add-to-list 'combining-chars char))
      (when decomposition
        (if (symbolp (car decomposition))
            ;; compatibiliy
            (setq nfkd-alist (cons (cons char (vconcat (cdr decomposition)))
                                   nfkd-alist))
          ;; canonical
          (setq nfkd-alist (cons (cons char (vconcat decomposition))
                                 nfkd-alist))
          (if (and (= 2 (length decomposition))
                   (null (memq char normalize-composition-exclusions)))
              (puthash decomposition char decomp-pair-to-comp-hash))
          ;; NFC/NFD
          (register-database nfc-predicate
                             nfc-search-chars
                             nfc-singletons
                             nfd-alist)
          ;; HFS-NFC/HFS-NFD
          (register-database hfs-nfc-predicate
                             hfs-nfc-search-chars
                             hfs-nfc-singletons
                             hfs-nfd-alist)))
      (setq char (1+ char))))

  (defun normalize-repetitively-expand-alist (alist)
    (let ((flag t) elem hit)
      (while flag
        (setq flag nil)
        (dolist (entry alist)
          (setq elem (mapcar (lambda (x) 
                               (setq hit (assoc x alist))
                               (if hit (setq flag t))
                               (if hit (mapcar 'identity (cdr hit)) (list x)))
                             (cdr entry)))
          (if flag (setcdr entry (vconcat (apply 'append elem))))))))

  (normalize-repetitively-expand-alist nfd-alist)
  (normalize-repetitively-expand-alist hfs-nfd-alist)
  (normalize-repetitively-expand-alist nfkd-alist)

)


(defvar normalize-combining-chars-regexp nil
  "Regular expression to match sequence of combining characters.")
  (setq normalize-combining-chars-regexp
  (eval-when-compile (concat (regexp-opt (mapcar 'char-to-string combining-chars)) "+")))

(defvar normalize-decomp-pair-to-comp-hash nil
  "Hashtable of decomposed pair to primary composite.")
  (setq normalize-decomp-pair-to-comp-hash
  (eval-when-compile decomp-pair-to-comp-hash))

;; NFC/NFD
(defvar normalize-nfc-search-regexp nil)
  (setq normalize-nfc-search-regexp
  (eval-when-compile (regexp-opt 
                      (append (mapcar 'char-to-string nfc-search-chars)
                              (mapcar 'char-to-string combining-chars)
                              (mapcar 'char-to-string normalize-composition-exclusions)
                              (mapcar 'char-to-string nfc-singletons)))))
(defvar normalize-nfc-translation-chars nil)
  (setq normalize-nfc-translation-chars
  (eval-when-compile (append normalize-composition-exclusions nfc-singletons)))
(define-translation-table 'normalize-nfd-table 
  (eval-when-compile (make-translation-table-from-alist nfd-alist)))

;; HFS-NFC/NFD
(defvar normalize-hfs-nfc-search-regexp nil)
  (setq normalize-hfs-nfc-search-regexp
  (eval-when-compile (regexp-opt
                      (append (mapcar 'char-to-string hfs-nfc-search-chars)
                              (mapcar 'char-to-string combining-chars)
                              (mapcar 'char-to-string normalize-composition-exclusions)
                              (mapcar 'char-to-string hfs-nfc-singletons)))))
(defvar normalize-hfs-nfc-translation-chars nil)
  (setq normalize-hfs-nfc-translation-chars
  (eval-when-compile (append normalize-composition-exclusions hfs-nfc-singletons)))
(define-translation-table 'normalize-hfs-nfd-table
  (eval-when-compile (make-translation-table-from-alist hfs-nfd-alist)))

;; NFKC/NFKD
(define-translation-table 'normalize-nfkd-table
  (eval-when-compile (make-translation-table-from-alist nfkd-alist)))

;;------------------------------------------------------------------------------------------

;; Normalize local region.
(defun normalize-block (from to &optional translation-table compose)
  ;; block-string = [starter?  diacritics+]
  (save-restriction
    (narrow-to-region from to)
    (goto-char (point-min))
    ;; decompose if needed.
    (if translation-table
        (translate-region from to translation-table))
    (let ((starter (char-after from)) diacritics)
      (if (= (get-char-code-property starter 'canonical-combining-class) 0)
          (setq diacritics (string-to-list (buffer-substring (1+ from) (point-max))))
        (setq starter nil)
        (setq diacritics (string-to-list (buffer-substring from (point-max)))))
      ;; sort diacritical marks if needed.
      (if (< 1 (length diacritics))
          (setq diacritics
                (sort diacritics
                      (lambda (ch1 ch2)
                        (< (get-char-code-property ch1 'canonical-combining-class)
                           (get-char-code-property ch2 'canonical-combining-class))))))
      ;; compose if needed (and both starter and diacritics exists).
      (if (and compose (and starter diacritics))
          (let ((diac diacritics) prev (prev-ccc 0) ccc prim-comp)
            (while diac
              (setq ccc (get-char-code-property (car diac)
                                                     'canonical-combining-class))
              (if (and (or (< prev-ccc ccc) (= ccc 0)) ;; e.g. #x09c7 #x09be -> #x09cb
                       (setq prim-comp (gethash (list starter (car diac))
                                                normalize-decomp-pair-to-comp-hash)))
                  (progn
                    (setq prev-ccc 0)
                    (setq starter prim-comp)
                    (if prev (setcdr prev (cdr diac)) (setq diacritics (cdr diacritics)))
                    (setq diac diacritics))
                (setq prev-ccc ccc
                      prev diac
                      diac (cdr diac))))))
      ;; replace buffer
      (delete-region (point-min) (point-max))
      (insert (concat (if starter (list starter)) diacritics)))
    (point-max))) ;; return the last position.

(defun normalize-composition-region (from to regexp translation-table translation-chars 
                                          &optional compat)
  (save-excursion
    (save-restriction
      (narrow-to-region from to)
      (if compat (translate-region from to 'normalize-nfkd-table))
      (goto-char (point-min))
      (let (start-pos starter)
        (while (re-search-forward regexp nil t)
          (setq starter (string-to-char (match-string 0)))
          (setq start-pos (match-beginning 0))
          (if (memq starter translation-chars)
              (progn
                (translate-region start-pos (match-end 0) translation-table)
                (goto-char start-pos))
            (goto-char
             (normalize-block 
              ;; from
              (if (or (= start-pos (point-min))
                      (and (= 0 (get-char-code-property starter 'canonical-combining-class))
                           (/= 0 (get-char-code-property (char-after (1- start-pos))
                                                        'canonical-combining-class))))
                  start-pos (1- start-pos))
              ;; to
              (if (looking-at normalize-combining-chars-regexp)
                  (match-end 0) (1+ start-pos))
              'normalize-nfd-table t))))))))

(defun normalize-decomposition-region (from to translation-table)
  (save-excursion
    (save-restriction
      (narrow-to-region from to)
      (goto-char (point-min))
      (let (start-pos starter)
        (while (re-search-forward normalize-combining-chars-regexp nil t)
          (setq starter (string-to-char (match-string 0)))
          (setq start-pos (match-beginning 0))
          (goto-char
           (normalize-block
            (if (= start-pos (point-min)) start-pos (1- start-pos))
            (match-end 0)
            translation-table nil))))
      (translate-region (point-min) (point-max) translation-table))))

;; --------------------------------------------------------------------------------

;;;###autoload
(defun normalize-NFC-region (from to)
  "Normalize the current region by the Unicode NFC."
  (interactive "r")
  (normalize-composition-region from to 
                                normalize-nfc-search-regexp
                                'normalize-nfd-table
                                normalize-nfc-translation-chars))
;;;###autoload
(defun normalize-NFC-string (str)
  "Normalize the string STR by the Unicode NFC."
  (with-temp-buffer
    (insert str)
    (normalize-NFC-region (point-min) (point-max))
    (buffer-string)))
;;;###autoload
(defun normalize-NFD-region (from to)
  "Normalize the current region by the Unicode NFD."
  (interactive "r")
  (normalize-decomposition-region from to 'normalize-nfd-table))
;;;###autoload
(defun normalize-NFD-string (str)
  "Normalize the string STR by the Unicode NFD."
  (with-temp-buffer
    (insert str)
    (normalize-NFD-region (point-min) (point-max))
    (buffer-string)))


;;;###autoload
(defun normalize-HFS-NFC-region (from to)
  "Normalize the current region by the Unicode NFC and Mac OS's HFS Plus."
  (interactive "r")
  (normalize-composition-region from to
                                normalize-hfs-nfc-search-regexp
                                'normalize-hfs-nfd-table
                                normalize-hfs-nfc-translation-chars))
;;;###autoload
(defun normalize-HFS-NFC-string (str)
  "Normalize the string STR by the Unicode NFC and Mac OS's HFS Plus."
  (with-temp-buffer
    (insert str)
    (normalize-HFS-NFC-region (point-min) (point-max))
    (buffer-string)))
;;;###autoload
(defun normalize-HFS-NFD-region (from to)
  "Normalize the current region by the Unicode NFD and Mac OS's HFS Plus."
  (interactive "r")
  (normalize-decomposition-region from to 'normalize-hfs-nfd-table))
;;;###autoload
(defun normalize-HFS-NFD-string (str)
  "Normalize the string STR by the Unicode NFD and Mac OS's HFS Plus."
  (with-temp-buffer
    (insert str)
    (normalize-HFS-NFD-region (point-min) (point-max))
    (buffer-string)))


;;;###autoload
(defun normalize-NFKC-region (from to)
  "Normalize the current region by the Unicode NFKC."
  (interactive "r")
  (normalize-composition-region from to
                                normalize-nfc-search-regexp
                                'normalize-nfd-table
                                normalize-nfc-translation-chars t))
;;;###autoload
(defun normalize-NFKC-string (str)
  "Normalize the string STR by the Unicode NFKC."
  (with-temp-buffer
    (insert str)
    (normalize-NFKC-region (point-min) (point-max))
    (buffer-string)))
;;;###autoload
(defun normalize-NFKD-region (from to)
  "Normalize the current region by the Unicode NFKD."
  (interactive "r")
  (normalize-decomposition-region from to 'normalize-nfkd-table))
;;;###autoload
(defun normalize-NFKD-string (str)
  "Normalize the string STR by the Unicode NFKD."
  (with-temp-buffer
    (insert str)
    (normalize-NFKD-region (point-min) (point-max))
    (buffer-string)))

;; Post-read-conversion function for `utf-8-hfs'.
(defun normalize-hfs-nfd-post-read-conversion (len)
  (save-excursion
    (save-restriction
      (narrow-to-region (point) (+ (point) len))
      (let ((buffer-modified-p (buffer-modified-p)))
        (normalize-hfs-NFC-region (point-min) (point-max))
        (- (point-max) (point-min))))))

;;; coding-system definition
(define-coding-system 'utf-8-hfs
  "UTF-8 base coding system with normalization on decoding.
The characters in HFS normalization exclusion area are not normalized.
On encoding, don't perform normalization."
  :coding-type 'utf-8
  :mnemonic ?U
  :charset-list '(unicode)
  ;; NFD decoder
  ;; :decode-translation-table (not necessary)
  :post-read-conversion 'normalize-hfs-nfd-post-read-conversion
  ;; NFD encoder is not necessary because MacOS will automatically do it.
  ;; :encode-translation-table 'nfd-encode-translation-table
  ;; :pre-write-conversion 'nfd-encode-function
  )

;;; normalize.el ends here




^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: normalize.el
  2009-04-02  6:40 normalize.el Kenichi Handa
@ 2009-04-02  8:10 ` Kim F. Storm
  2009-04-02 11:54   ` normalize.el Kenichi Handa
  2009-04-02 16:49 ` normalize.el Stefan Monnier
  2009-04-03  9:10 ` normalize.el Richard M Stallman
  2 siblings, 1 reply; 11+ messages in thread
From: Kim F. Storm @ 2009-04-02  8:10 UTC (permalink / raw)
  To: Kenichi Handa; +Cc: kawabata.taichi, emacs-devel

Kenichi Handa <handa@m17n.org> writes:

> The attached is an Unicode normalization tool contributed by
> Kawabata-san.  It performs all the Unicode normalization
> NFC/NFD/NFKD/NFKC, and provides a coding system utf-8-hfs
> that is suitable to be used for Mac OS 8.1's file names.
>
>
> ;;; normalize.el --- Unicode normalization NFC/NFD/NFKD/NFKC
>

Handa-san,

I have no opinion on the functionality of this package - or
whether it should go in now or later - but I think the name
of the package is way too generic - "normalize" reallly says
nothing at all - and can mean 1000 things to different people
and in different contexts.

So maybe something like

utf-8-hfs.el
hfs-normalize.el
utf-normalize.el

or whatever...

(and change the normalize- prefix accordingly)

-- 
Kim F. Storm <storm@cua.dk> http://www.cua.dk





^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: normalize.el
  2009-04-02  8:10 ` normalize.el Kim F. Storm
@ 2009-04-02 11:54   ` Kenichi Handa
  0 siblings, 0 replies; 11+ messages in thread
From: Kenichi Handa @ 2009-04-02 11:54 UTC (permalink / raw)
  To: Kim F. Storm; +Cc: kawabata.taichi, emacs-devel

In article <87ljqjscfp.fsf@kfs-lx.rd.rdm>, storm@cua.dk (Kim F. Storm) writes:

> Kenichi Handa <handa@m17n.org> writes:
> > The attached is an Unicode normalization tool contributed by
> > Kawabata-san.  It performs all the Unicode normalization
> > NFC/NFD/NFKD/NFKC, and provides a coding system utf-8-hfs
> > that is suitable to be used for Mac OS 8.1's file names.
> >
> >
> > ;;; normalize.el --- Unicode normalization NFC/NFD/NFKD/NFKC
> >

> Handa-san,

> I have no opinion on the functionality of this package - or
> whether it should go in now or later - but I think the name
> of the package is way too generic - "normalize" reallly says
> nothing at all - and can mean 1000 things to different people
> and in different contexts.

Ah, good point.

> So maybe something like

> utf-8-hfs.el
> hfs-normalize.el
> utf-normalize.el

As the Unicode normalization is for character sequence, not for
UTF (transformaiton format), I think something like these
are better:

ucs-normalize.el
uni-normalize.el  (but it's confusing with the other uni-*.el files)

Kawabata-san, what do you think?

---
Kenichi Handa
handa@m17n.org




^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: normalize.el
  2009-04-02  6:40 normalize.el Kenichi Handa
  2009-04-02  8:10 ` normalize.el Kim F. Storm
@ 2009-04-02 16:49 ` Stefan Monnier
  2009-04-08  5:39   ` normalize.el Kenichi Handa
  2009-04-03  9:10 ` normalize.el Richard M Stallman
  2 siblings, 1 reply; 11+ messages in thread
From: Stefan Monnier @ 2009-04-02 16:49 UTC (permalink / raw)
  To: Kenichi Handa; +Cc: kawabata.taichi, emacs-devel

> The attached is an Unicode normalization tool contributed by
> Kawabata-san.  It performs all the Unicode normalization
> NFC/NFD/NFKD/NFKC, and provides a coding system utf-8-hfs
> that is suitable to be used for Mac OS 8.1's file names.

Great.  Please rename it to uni-normalize.el or ucs-normalize.el.

> Perhaps committing it to the trunk now is not good
> considering that we are already pretesting for 23.1.

Yes, let's keep it for Emacs-23.2.

> What should I do with it?  At least, I want Mac users to test it by
> setting the default-file-name-coding-system to `utf-8-hfs'.

Advertize it.


        Stefan




^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: normalize.el
  2009-04-02  6:40 normalize.el Kenichi Handa
  2009-04-02  8:10 ` normalize.el Kim F. Storm
  2009-04-02 16:49 ` normalize.el Stefan Monnier
@ 2009-04-03  9:10 ` Richard M Stallman
  2009-04-03 13:24   ` normalize.el Stefan Monnier
  2 siblings, 1 reply; 11+ messages in thread
From: Richard M Stallman @ 2009-04-03  9:10 UTC (permalink / raw)
  To: Kenichi Handa; +Cc: kawabata.taichi, emacs-devel

An entirely new package can't break anything,
so I have in general been willing to add a new package
during pretest.




^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: normalize.el
  2009-04-03  9:10 ` normalize.el Richard M Stallman
@ 2009-04-03 13:24   ` Stefan Monnier
  0 siblings, 0 replies; 11+ messages in thread
From: Stefan Monnier @ 2009-04-03 13:24 UTC (permalink / raw)
  To: rms; +Cc: kawabata.taichi, emacs-devel, Kenichi Handa

> An entirely new package can't break anything,

That's true.  But it can bring more bug reports about the new feature.

> so I have in general been willing to add a new package
> during pretest.

I want to keep the pretest as short as possible.


        Stefan




^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: normalize.el
  2009-04-02 16:49 ` normalize.el Stefan Monnier
@ 2009-04-08  5:39   ` Kenichi Handa
  2009-04-18 19:06     ` normalize.el 川幡 太一
  0 siblings, 1 reply; 11+ messages in thread
From: Kenichi Handa @ 2009-04-08  5:39 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: kawabata.taichi, emacs-devel

In article <jwv1vsb89au.fsf-monnier+emacs@gnu.org>, Stefan Monnier <monnier@iro.umontreal.ca> writes:

> > The attached is an Unicode normalization tool contributed by
> > Kawabata-san.  It performs all the Unicode normalization
> > NFC/NFD/NFKD/NFKC, and provides a coding system utf-8-hfs
> > that is suitable to be used for Mac OS 8.1's file names.

> Great.  Please rename it to uni-normalize.el or ucs-normalize.el.

I renamed it to ucs-normalize.el and changed
variable/function names as attached.  But, byte-compiling
shows these warnings and error.  As the original
normalize.el causes the same error, Kawabata-san, could you
please fix it.

Compiling file /home/handa/ucs-normalize.el at Wed Apr  8 14:37:56 2009
Entering directory `/home/handa/'
ucs-normalize.el:187:46:Warning: assignment to free variable `combining-chars'
ucs-normalize.el:198:43:Warning: assignment to free variable
    `decomp-pair-to-comp-hash'
ucs-normalize.el:201:30:Warning: assignment to free variable
    `nfc-search-chars'
ucs-normalize.el:202:30:Warning: assignment to free variable `nfc-singletons'
ucs-normalize.el:203:30:Warning: assignment to free variable `nfd-alist'
ucs-normalize.el:206:30:Warning: assignment to free variable
    `hfs-nfc-search-chars'
ucs-normalize.el:207:30:Warning: assignment to free variable
    `hfs-nfc-singletons'
ucs-normalize.el:208:30:Warning: assignment to free variable `hfs-nfd-alist'
ucs-normalize.el:191:19:Warning: assignment to free variable `nfkd-alist'
ucs-normalize.el:192:36:Warning: reference to free variable `nfkd-alist'
ucs-normalize.el:209:13:Warning: reference to free variable
    `decomp-pair-to-comp-hash'
ucs-normalize.el:205:12:Warning: reference to free variable `nfc-predicate'
ucs-normalize.el:205:12:Warning: reference to free variable `nfc-search-chars'
ucs-normalize.el:205:12:Warning: reference to free variable `nfc-singletons'
ucs-normalize.el:223:40:Warning: reference to free variable `nfd-alist'
ucs-normalize.el:223:40:Warning: reference to free variable
    `hfs-nfc-predicate'
ucs-normalize.el:223:40:Warning: reference to free variable
    `hfs-nfc-search-chars'
ucs-normalize.el:223:40:Warning: reference to free variable
    `hfs-nfc-singletons'
ucs-normalize.el:224:40:Warning: reference to free variable `hfs-nfd-alist'
ucs-normalize.el:225:4:Error: Symbol's value as variable is void: nfc-predicate

---
Kenichi Handa
handa@m17n.org

;;; ucs-normalize.el --- Unicode normalization NFC/NFD/NFKD/NFKC

;; Copyright (C) 2009
;;   Free Software Foundation, Inc.

;; Author: Taichi Kawabata <kawabata.taichi@gmail.com>
;; Maintainer: Taichi Kawabata <kawabata.taichi@gmail.com>
;; Keywords: unicode, normalization

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; The following code will normalize the string or text according to
;; the NFC/NFD/NFKD (single step) NFKC (dual step).

;; This program has passed the NormalizationTest-4.1.0.txt except
;; Hangul normalizations.

;; References:
;; http://www.unicode.org/reports/tr15/
;; http://www.unicode.org/review/pr-29.html

;; Furthermore, it supports HFS normalization, for people who don't
;; want to normalize the CJK characters.

;;; Note on HFS Normalization.
;;
;; HFS-Normalization is the way to normalize text, as described by HFS
;; Plus Volue format specification
;; (http://developer.apple.com/technotes/tn/tn1150.html).  This is
;; useful espcially for CJK people, since it doesn't normlize CJK
;; compatibility characters.
;;
;; Normalization Exclusion Area:
;;  U+02000 .. U+02FFF :: Punctuation, symbols, dingbats, arrows, etc.
;;  U+0F900 .. U+0FAFF :: CJK compatibility Ideographs.
;;  U+2F800 .. U+2FFFF :: CJK compatibility Ideographs.

;;;
;;; Implementation Notes on NFC/HFS-NFC. (ccc = canonical combining class)
;;
;; A. `re-search-forward' for the following characters.
;;   (1) characters that should be decomposed anyway.
;;   (1-a) composition-exclusion characters
;;   (1-b) singleton characters
;;   (1-c) characters whose decomposed first char does not have ccc=0.
;;   (2) characters that may appear as second of composed character.
;;   (2-a) character whose ccc != 0.
;;   (2-b) character whose ccc = 0.
;;
;; B. In case of (1) in step A, translate character, go to the
;;    beginning position of translated region, and go back to step A.
;;
;; C. In case of (2) in step A, normalize the block around the
;;    searched character.  (`normalize-block')
;;
;;    (1) start of the block
;;        In case of (2-a) in step A, if the previous character
;;        exists, then the beginning of the block is the previous
;;        character.  In case of (2-b), if the previous character's
;;        ccc = 0, then the beginning of the block is the previous
;;        character.  Otherwise, the beginning of the block is the
;;        searched character.
;;    (2) end of the block
;;        from the searched character, the end of the block is
;;        searched until the character whose ccc=0.
;;
;; D. Normalize the block
;;    (1) decompose the block
;;    (2) sort the combining characters
;;    (3) compose the block.

;;;
;;; Implementation Notes on NFD/HFS-NFD.
;;
;; A. `re-search-forward' for the combining characters sequence.
;;
;; B. Normalize the block around the searched character sequence.
;;    (1) Start of the block is the previous character if it exists.
;;        If not, start of the block is the searched character.
;;    (2) End of the block is the end of the combining character
;;        sequence.
;;
;; C. Normalize the block
;;    (1) decompose the block
;;    (2) sort the combining characters
;;
;; D. Translate all the rest at once.

;;;
;;; Implementation Notes on NFKC/NFKD
;;
;; NFKC/NFKD is realized as applying NFC/NFD to the text which is
;; firstly translated by NFKD(-unique) decomposition patterns.

;;; Code:

(eval-and-compile
(defconst ucs-normalize-composition-exclusions
  '(#x0958 #x0959 #x095A #x095B #x095C #x095D #x095E #x095F
    #x09DC #x09DD #x09DF #x0A33 #x0A36 #x0A59 #x0A5A #x0A5B
    #x0A5E #x0B5C #x0B5D #x0F43 #x0F4D #x0F52 #x0F57 #x0F5C
    #x0F69 #x0F76 #x0F78 #x0F93 #x0F9D #x0FA2 #x0FA7 #x0FAC
    #x0FB9 #xFB1D #xFB1F #xFB2A #xFB2B #xFB2C #xFB2D #xFB2E
    #xFB2F #xFB30 #xFB31 #xFB32 #xFB33 #xFB34 #xFB35 #xFB36
    #xFB38 #xFB39 #xFB3A #xFB3B #xFB3C #xFB3E #xFB40 #xFB41
    #xFB43 #xFB44 #xFB46 #xFB47 #xFB48 #xFB49 #xFB4A #xFB4B
    #xFB4C #xFB4D #xFB4E #x2ADC #x1D15E #x1D15F #x1D160 #x1D161
    #x1D162 #x1D163 #x1D164 #x1D1BB #x1D1BC #x1D1BD #x1D1BE
    #x1D1BF #x1D1C0)
 "Composition Exclusion Table.
The table is taken from
  http://www.unicode.org/Public/UNIDATA/CompositionExclusions-4.1.0.txt")
)

;; 
(eval-when-compile

  ;; generics
  (setq combining-chars nil)
  (setq decomp-pair-to-comp-hash (make-hash-table :test 'equal :size 2000)) ; 1003 in Unicode 4.1.0 (except hangul)

  ;; NFC/NFD
  (defun nfc-predicate (char) t)
  (setq nfc-search-chars nil)
  (setq nfc-singletons nil)
  (setq nfd-alist nil)

  ;; HFS-NFC/HFS-NFD
  (defun hfs-nfc-predicate (char)
    (or (and (>= char 0) (< char #x2000))
             (and (>= char #x3000) (< char #xf900))
             (and (>= char #xfb00) (< char #x2f800))
             (>= char #x30000)))
  (setq hfs-nfc-search-chars nil)
  (setq hfs-nfc-singletons nil)
  (setq hfs-nfd-alist nil)

  ;; NFKC/NFKD
  (setq nfkd-alist nil)

  ;; macros
  (defmacro register-database (predicate 
                               search-chars
                               singletons
                               decomposition-alist)
    `(when (apply #',predicate (list char))
       (let ((decomp-1st-ccc
              (get-char-code-property 
               (car decomposition) 'canonical-combining-class)))
         (setq ,decomposition-alist (cons (cons char (vconcat decomposition))
                                          ,decomposition-alist))
         (unless (memq char ucs-normalize-composition-exclusions)
           (if (= 1 (length decomposition))
               (setq ,singletons (cons char ,singletons))
             ;; add second char of decomposition for search-chars iff ccc = 0
             (if (= 0 (get-char-code-property (cadr decomposition)
                                              'canonical-combining-class))
                 (add-to-list ',search-chars (cadr decomposition))))
           ;; For fast normalization, the case of positive integer
           ;; `decomp-1st-ccc' means that the char should also be
           ;; considered as something like singleton.
           (if (and decomp-1st-ccc (/= 0 decomp-1st-ccc))
               (setq ,singletons (cons char ,singletons)))))))

  (let ((char 0) ccc decomposition)
    (while (< char #x30000) 
      (setq ccc (get-char-code-property
                 char 'canonical-combining-class))
      (setq decomposition (get-char-code-property 
                           char 'decomposition))
      ;; add all combining-char to fast search list.
      (if (and ccc (/= 0 ccc)) (add-to-list 'combining-chars char))
      (when decomposition
        (if (symbolp (car decomposition))
            ;; compatibiliy
            (setq nfkd-alist (cons (cons char (vconcat (cdr decomposition)))
                                   nfkd-alist))
          ;; canonical
          (setq nfkd-alist (cons (cons char (vconcat decomposition))
                                 nfkd-alist))
          (if (and (= 2 (length decomposition))
                   (null (memq char ucs-normalize-composition-exclusions)))
              (puthash decomposition char decomp-pair-to-comp-hash))
          ;; NFC/NFD
          (register-database nfc-predicate
                             nfc-search-chars
                             nfc-singletons
                             nfd-alist)
          ;; HFS-NFC/HFS-NFD
          (register-database hfs-nfc-predicate
                             hfs-nfc-search-chars
                             hfs-nfc-singletons
                             hfs-nfd-alist)))
      (setq char (1+ char))))

  (defun normalize-repetitively-expand-alist (alist)
    (let ((flag t) elem hit)
      (while flag
        (setq flag nil)
        (dolist (entry alist)
          (setq elem (mapcar (lambda (x) 
                               (setq hit (assoc x alist))
                               (if hit (setq flag t))
                               (if hit (mapcar 'identity (cdr hit)) (list x)))
                             (cdr entry)))
          (if flag (setcdr entry (vconcat (apply 'append elem))))))))

  (normalize-repetitively-expand-alist nfd-alist)
  (normalize-repetitively-expand-alist hfs-nfd-alist)
  (normalize-repetitively-expand-alist nfkd-alist)

)


(defvar ucs-normalize-combining-chars-regexp nil
  "Regular expression to match sequence of combining characters.")
  (setq ucs-normalize-combining-chars-regexp
  (eval-when-compile (concat (regexp-opt (mapcar 'char-to-string combining-chars)) "+")))

(defvar ucs-normalize-decomp-pair-to-comp-hash nil
  "Hashtable of decomposed pair to primary composite.")
  (setq ucs-normalize-decomp-pair-to-comp-hash
  (eval-when-compile decomp-pair-to-comp-hash))

;; NFC/NFD
(defvar ucs-normalize-nfc-search-regexp nil)
  (setq ucs-normalize-nfc-search-regexp
  (eval-when-compile (regexp-opt 
                      (append (mapcar 'char-to-string nfc-search-chars)
                              (mapcar 'char-to-string combining-chars)
                              (mapcar 'char-to-string ucs-normalize-composition-exclusions)
                              (mapcar 'char-to-string nfc-singletons)))))
(defvar ucs-normalize-nfc-translation-chars nil)
  (setq ucs-normalize-nfc-translation-chars
  (eval-when-compile (append ucs-normalize-composition-exclusions nfc-singletons)))
(define-translation-table 'ucs-normalize-nfd-table 
  (eval-when-compile (make-translation-table-from-alist nfd-alist)))

;; HFS-NFC/NFD
(defvar ucs-normalize-hfs-nfc-search-regexp nil)
  (setq ucs-normalize-hfs-nfc-search-regexp
  (eval-when-compile (regexp-opt
                      (append (mapcar 'char-to-string hfs-nfc-search-chars)
                              (mapcar 'char-to-string combining-chars)
                              (mapcar 'char-to-string ucs-normalize-composition-exclusions)
                              (mapcar 'char-to-string hfs-nfc-singletons)))))
(defvar ucs-normalize-hfs-nfc-translation-chars nil)
  (setq ucs-normalize-hfs-nfc-translation-chars
  (eval-when-compile (append ucs-normalize-composition-exclusions hfs-nfc-singletons)))
(define-translation-table 'ucs-normalize-hfs-nfd-table
  (eval-when-compile (make-translation-table-from-alist hfs-nfd-alist)))

;; NFKC/NFKD
(define-translation-table 'ucs-normalize-nfkd-table
  (eval-when-compile (make-translation-table-from-alist nfkd-alist)))

;;------------------------------------------------------------------------------------------

;; Normalize local region.
(defun ucs-normalize-block (from to &optional translation-table compose)
  ;; block-string = [starter?  diacritics+]
  (save-restriction
    (narrow-to-region from to)
    (goto-char (point-min))
    ;; decompose if needed.
    (if translation-table
        (translate-region from to translation-table))
    (let ((starter (char-after from)) diacritics)
      (if (= (get-char-code-property starter 'canonical-combining-class) 0)
          (setq diacritics (string-to-list (buffer-substring (1+ from) (point-max))))
        (setq starter nil)
        (setq diacritics (string-to-list (buffer-substring from (point-max)))))
      ;; sort diacritical marks if needed.
      (if (< 1 (length diacritics))
          (setq diacritics
                (sort diacritics
                      (lambda (ch1 ch2)
                        (< (get-char-code-property ch1 'canonical-combining-class)
                           (get-char-code-property ch2 'canonical-combining-class))))))
      ;; compose if needed (and both starter and diacritics exists).
      (if (and compose (and starter diacritics))
          (let ((diac diacritics) prev (prev-ccc 0) ccc prim-comp)
            (while diac
              (setq ccc (get-char-code-property (car diac)
                                                     'canonical-combining-class))
              (if (and (or (< prev-ccc ccc) (= ccc 0)) ;; e.g. #x09c7 #x09be -> #x09cb
                       (setq prim-comp (gethash (list starter (car diac))
                                                ucs-normalize-decomp-pair-to-comp-hash)))
                  (progn
                    (setq prev-ccc 0)
                    (setq starter prim-comp)
                    (if prev (setcdr prev (cdr diac)) (setq diacritics (cdr diacritics)))
                    (setq diac diacritics))
                (setq prev-ccc ccc
                      prev diac
                      diac (cdr diac))))))
      ;; replace buffer
      (delete-region (point-min) (point-max))
      (insert (concat (if starter (list starter)) diacritics)))
    (point-max))) ;; return the last position.

(defun ucs-normalize-composition-region (from to regexp translation-table translation-chars 
                                          &optional compat)
  (save-excursion
    (save-restriction
      (narrow-to-region from to)
      (if compat (translate-region from to 'ucs-normalize-nfkd-table))
      (goto-char (point-min))
      (let (start-pos starter)
        (while (re-search-forward regexp nil t)
          (setq starter (string-to-char (match-string 0)))
          (setq start-pos (match-beginning 0))
          (if (memq starter translation-chars)
              (progn
                (translate-region start-pos (match-end 0) translation-table)
                (goto-char start-pos))
            (goto-char
             (ucs-normalize-block 
              ;; from
              (if (or (= start-pos (point-min))
                      (and (= 0 (get-char-code-property starter 'canonical-combining-class))
                           (/= 0 (get-char-code-property (char-after (1- start-pos))
                                                        'canonical-combining-class))))
                  start-pos (1- start-pos))
              ;; to
              (if (looking-at ucs-normalize-combining-chars-regexp)
                  (match-end 0) (1+ start-pos))
              'ucs-normalize-nfd-table t))))))))

(defun ucs-normalize-decomposition-region (from to translation-table)
  (save-excursion
    (save-restriction
      (narrow-to-region from to)
      (goto-char (point-min))
      (let (start-pos starter)
        (while (re-search-forward ucs-normalize-combining-chars-regexp nil t)
          (setq starter (string-to-char (match-string 0)))
          (setq start-pos (match-beginning 0))
          (goto-char
           (ucs-normalize-block
            (if (= start-pos (point-min)) start-pos (1- start-pos))
            (match-end 0)
            translation-table nil))))
      (translate-region (point-min) (point-max) translation-table))))

;; --------------------------------------------------------------------------------

;;;###autoload
(defun ucs-normalize-NFC-region (from to)
  "Normalize the current region by the Unicode NFC."
  (interactive "r")
  (ucs-normalize-composition-region from to 
                                ucs-normalize-nfc-search-regexp
                                'ucs-normalize-nfd-table
                                ucs-normalize-nfc-translation-chars))
;;;###autoload
(defun ucs-normalize-NFC-string (str)
  "Normalize the string STR by the Unicode NFC."
  (with-temp-buffer
    (insert str)
    (ucs-normalize-NFC-region (point-min) (point-max))
    (buffer-string)))
;;;###autoload
(defun ucs-normalize-NFD-region (from to)
  "Normalize the current region by the Unicode NFD."
  (interactive "r")
  (ucs-normalize-decomposition-region from to 'ucs-normalize-nfd-table))
;;;###autoload
(defun ucs-normalize-NFD-string (str)
  "Normalize the string STR by the Unicode NFD."
  (with-temp-buffer
    (insert str)
    (ucs-normalize-NFD-region (point-min) (point-max))
    (buffer-string)))


;;;###autoload
(defun ucs-normalize-HFS-NFC-region (from to)
  "Normalize the current region by the Unicode NFC and Mac OS's HFS Plus."
  (interactive "r")
  (ucs-normalize-composition-region from to
                                ucs-normalize-hfs-nfc-search-regexp
                                'ucs-normalize-hfs-nfd-table
                                ucs-normalize-hfs-nfc-translation-chars))
;;;###autoload
(defun ucs-normalize-HFS-NFC-string (str)
  "Normalize the string STR by the Unicode NFC and Mac OS's HFS Plus."
  (with-temp-buffer
    (insert str)
    (ucs-normalize-HFS-NFC-region (point-min) (point-max))
    (buffer-string)))
;;;###autoload
(defun ucs-normalize-HFS-NFD-region (from to)
  "Normalize the current region by the Unicode NFD and Mac OS's HFS Plus."
  (interactive "r")
  (ucs-normalize-decomposition-region from to 'ucs-normalize-hfs-nfd-table))
;;;###autoload
(defun ucs-normalize-HFS-NFD-string (str)
  "Normalize the string STR by the Unicode NFD and Mac OS's HFS Plus."
  (with-temp-buffer
    (insert str)
    (ucs-normalize-HFS-NFD-region (point-min) (point-max))
    (buffer-string)))


;;;###autoload
(defun ucs-normalize-NFKC-region (from to)
  "Normalize the current region by the Unicode NFKC."
  (interactive "r")
  (ucs-normalize-composition-region from to
                                ucs-normalize-nfc-search-regexp
                                'ucs-normalize-nfd-table
                                ucs-normalize-nfc-translation-chars t))
;;;###autoload
(defun ucs-normalize-NFKC-string (str)
  "Normalize the string STR by the Unicode NFKC."
  (with-temp-buffer
    (insert str)
    (ucs-normalize-NFKC-region (point-min) (point-max))
    (buffer-string)))
;;;###autoload
(defun ucs-normalize-NFKD-region (from to)
  "Normalize the current region by the Unicode NFKD."
  (interactive "r")
  (ucs-normalize-decomposition-region from to 'ucs-normalize-nfkd-table))
;;;###autoload
(defun ucs-normalize-NFKD-string (str)
  "Normalize the string STR by the Unicode NFKD."
  (with-temp-buffer
    (insert str)
    (ucs-normalize-NFKD-region (point-min) (point-max))
    (buffer-string)))

;; Post-read-conversion function for `utf-8-hfs'.
(defun ucs-normalize-hfs-nfd-post-read-conversion (len)
  (save-excursion
    (save-restriction
      (narrow-to-region (point) (+ (point) len))
      (let ((buffer-modified-p (buffer-modified-p)))
        (normalize-hfs-NFC-region (point-min) (point-max))
        (- (point-max) (point-min))))))

;;; coding-system definition
(define-coding-system 'utf-8-hfs
  "UTF-8 base coding system with normalization on decoding.
The characters in HFS normalization exclusion area are not normalized.
On encoding, don't perform normalization."
  :coding-type 'utf-8
  :mnemonic ?U
  :charset-list '(unicode)
  ;; NFD decoder
  ;; :decode-translation-table (not necessary)
  :post-read-conversion 'ucs-normalize-hfs-nfd-post-read-conversion
  ;; NFD encoder is not necessary because MacOS will automatically do it.
  ;; :encode-translation-table 'nfd-encode-translation-table
  ;; :pre-write-conversion 'nfd-encode-function
  )

;;; normalize.el ends here




^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: normalize.el
  2009-04-08  5:39   ` normalize.el Kenichi Handa
@ 2009-04-18 19:06     ` 川幡 太一
  2009-04-19  3:39       ` normalize.el 川幡 太一
  0 siblings, 1 reply; 11+ messages in thread
From: 川幡 太一 @ 2009-04-18 19:06 UTC (permalink / raw)
  To: Kenichi Handa; +Cc: Stefan Monnier, emacs-devel

Dear sirs,

>> In <E1LrQVa-0003Qz-Er@etlken>, 
>> 半田さん wrote:

> In article <jwv1vsb89au.fsf-monnier+emacs@gnu.org>, Stefan Monnier <monnier@iro.umontreal.ca> writes:

> > > The attached is an Unicode normalization tool contributed by
> > > Kawabata-san.  It performs all the Unicode normalization
> > > NFC/NFD/NFKD/NFKC, and provides a coding system utf-8-hfs
> > > that is suitable to be used for Mac OS 8.1's file names.

> > Great.  Please rename it to uni-normalize.el or ucs-normalize.el.

> I renamed it to ucs-normalize.el and changed
> variable/function names as attached.  But, byte-compiling
> shows these warnings and error.  As the original
> normalize.el causes the same error, Kawabata-san, could you
> please fix it.

I'm sorry for late reply.  Here is fixed version I tried to remove all
warnings.  I believe this version fully passes `NormalizationTest-5.1.0.txt'.

However, it takes somewhat longer time for byte-compilation (About 1 min
for my 2GHz machine), and byte-compiled code will be quite larger than
original code.  

These problems would be reduced if we could provide specifically
customized codes for Hangual character decompositions.

Note that HFS-NFD/HFS-NFC is prepared for `MacOS X' file names.  While
UCS Normalization may remove stylistic and other infomations for CJK
Ideographs, Apple Normalization preserve them.  (Equivalence test for
CJK Ideographs may need other schemes anyway).

====================================================================

;;; ucs-normalize.el --- Unicode normalization NFC/NFD/NFKD/NFKC

;; Copyright (C) 2009
;;   Free Software Foundation, Inc.

;; Author: Taichi Kawabata <kawabata.taichi@gmail.com>
;; Keywords: unicode, normalization

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; This program has passed the NormalizationTest-4.1.0.txt.
;;
;; References:
;; http://www.unicode.org/reports/tr15/
;; http://www.unicode.org/review/pr-29.html
;;
;; HFS-Normalization:
;; Reference:
;; http://developer.apple.com/technotes/tn/tn1150.html
;;
;; HFS Normalization excludes following area for decomposition.
;;
;;  U+02000 .. U+02FFF :: Punctuation, symbols, dingbats, arrows, etc.
;;                        (Characters in this region will be composed.)
;;  U+0F900 .. U+0FAFF :: CJK compatibility Ideographs.
;;  U+2F800 .. U+2FFFF :: CJK compatibility Ideographs.
;;
;; HFS-Normalization is useful for normalizing text involving CJK Ideographs.
;;
;;;
;;; Implementation Notes on NFC/HFS-NFC.
;;;
;;
;;    <Stages>   Decomposition    Composition
;;   NFD:        'nfd             nil
;;   NFC:        'nfd             t
;;   NFKD:       'nfkd            nil
;;   NFKC:       'nfkd            t
;;   HFS-NFD:    'hfs-nfd         'hfs-nfd-comp-p
;;   HFS-NFC     'hfs-nfd         t
;;
;; Algorithm for Normalization
;;
;; Before normalization, following data will be prepared.
;;
;; 1. quick-check-list
;;
;;  `quick-check-list' consists of characters that will be decomposed
;;  during normalization.  It includes composition-exclusions,
;;  singletons, non-starter-decompositions and decomposable
;;  characters.
;;
;;  `quick-check-regexp' will search the above characters plus
;;  combining characters.
;;
;; 2. decomposition-translation
;;
;;  `decomposition-translation' is a translation table that will be
;;  used to decompose the characters.
;;
;;
;; Normalization Process
;;
;; A. Searching (`ucs-normalize-region')
;;
;;    Region is searched for `quick-check-regexp' to find possibly
;;    normalizable point.
;;
;; B. Identification of Normalization Block
;;
;;    (1) start of the block
;;        If the searched character is in quick-check-list, then the
;;        beginning of the block is the searched character.
;;        If searched character is combining character, then previous
;;        character will be the target character
;;    (2) end of the block
;;        Block ends at non-composable starter character.
;;
;; C. Decomposition  (`ucs-normalize-block')
;;
;;    The entire block will be decomposed by
;;    `decomposition-translation' table.
;;
;; D. Sorting and Composition of Smaller Blocks (`ucs-normalize-block-compose-chars')
;;
;;    The block will be split to multiple samller blocks by starter
;;    charcters.  Each block is sorted, and composed if necessary.
;;
;; E. Composition of Entire Block (`ucs-normalize-compose-chars')
;;
;;   Composed blocks are collected and again composed.

;;; Code:

(eval-when-compile (require 'cl))

(eval-when-compile

  (defconst ucs-normalize-composition-exclusions
    '(#x0958 #x0959 #x095A #x095B #x095C #x095D #x095E #x095F
      #x09DC #x09DD #x09DF #x0A33 #x0A36 #x0A59 #x0A5A #x0A5B
      #x0A5E #x0B5C #x0B5D #x0F43 #x0F4D #x0F52 #x0F57 #x0F5C
      #x0F69 #x0F76 #x0F78 #x0F93 #x0F9D #x0FA2 #x0FA7 #x0FAC
      #x0FB9 #xFB1D #xFB1F #xFB2A #xFB2B #xFB2C #xFB2D #xFB2E
      #xFB2F #xFB30 #xFB31 #xFB32 #xFB33 #xFB34 #xFB35 #xFB36
      #xFB38 #xFB39 #xFB3A #xFB3B #xFB3C #xFB3E #xFB40 #xFB41
      #xFB43 #xFB44 #xFB46 #xFB47 #xFB48 #xFB49 #xFB4A #xFB4B
      #xFB4C #xFB4D #xFB4E #x2ADC #x1D15E #x1D15F #x1D160 #x1D161
      #x1D162 #x1D163 #x1D164 #x1D1BB #x1D1BC #x1D1BD #x1D1BE
      #x1D1BF #x1D1C0)
   "Composition Exclusion List.
  This list is taken from
    http://www.unicode.org/Public/UNIDATA/CompositionExclusions-5.1.0.txt")

  ;; Unicode ranges that decompositions & combinings are defined.
  (defvar check-range
  '((#x00a0 . #x3400) (#xA600 . #x10fff) (#x1d000 . #x1dfff) (#x2f800 . #x2faff)))

  ;; Basic normalization functions
  (defun nfd (char)
    (let ((decomposition
           (get-char-code-property char 'decomposition)))
      (if (and decomposition (numberp (car decomposition)))
          decomposition)))

  (defun nfkd (char)
    (let ((decomposition
           (get-char-code-property char 'decomposition)))
      (if (symbolp (car decomposition)) (cdr decomposition)
        decomposition)))

  (defun hfs-nfd (char)
    (when (or (and (>= char 0) (< char #x2000))
              (and (>= char #x3000) (< char #xf900))
              (and (>= char #xfb00) (< char #x2f800))
              (>= char #x30000))
      (nfd char))))

(defun ucs-normalize-hfs-nfd-comp-p (char)
  (and (>= char #x2000) (< char #x3000)))

(defsubst ucs-normalize-ccc (char)
  (get-char-code-property char 'canonical-combining-class))

;; Data common to all normalizations

(eval-when-compile

  (defvar combining-chars nil)
  (defvar decomposition-pair-to-composition nil)
  (defvar non-starter-decompositions nil)
  (let ((char 0) ccc decomposition)
    (mapc
     (lambda (start-end)
       (do ((char (car start-end) (+ char 1))) ((> char (cdr start-end)))
         (setq ccc (ucs-normalize-ccc char))
         (setq decomposition (get-char-code-property
                              char 'decomposition))
         (if (and ccc (/= 0 ccc)) (add-to-list 'combining-chars char))
         (if (and (numberp (car decomposition))
                  (/= (ucs-normalize-ccc (car decomposition))
                      0))
             (add-to-list 'non-starter-decompositions char))
         (when (numberp (car decomposition))
           (if (and (= 2 (length decomposition))
                    (null (memq char ucs-normalize-composition-exclusions))
                    (null (memq char non-starter-decompositions)))
               (setq decomposition-pair-to-composition
                     (cons (cons decomposition char)
                           decomposition-pair-to-composition)))
           ;; If not singleton decomposition, second and later characters in
           ;; decomposition will be the subject of combining characters.
           (if (cdr decomposition)
               (dolist (char (cdr decomposition))
                 (add-to-list 'combining-chars char))))))
     check-range)))

(defun ucs-normalize-make-hash-table-from-alist (alist)
  (let ((table (make-hash-table :test 'equal :size 2000)))
    (mapc (lambda (x) (puthash (car x) (cdr x) table)) alist)
    table))

(defvar ucs-normalize-decomposition-pair-to-primary-composite nil
  "Hashtable of decomposed pair to primary composite.")
  (setq ucs-normalize-decomposition-pair-to-primary-composite
        (ucs-normalize-make-hash-table-from-alist
         (eval-when-compile decomposition-pair-to-composition)))

(defun ucs-normalize-primary-composite (decomposition-pair composition-predicate)
  (let ((char (gethash decomposition-pair 
                       ucs-normalize-decomposition-pair-to-primary-composite)))
    (if (and char 
             (functionp composition-predicate)
             (null (funcall composition-predicate char)))
        nil char)))

(defvar ucs-normalize-combining-chars nil)
(setq ucs-normalize-combining-chars (eval-when-compile combining-chars))

(defvar ucs-normalize-combining-chars-regexp nil
  "Regular expression to match sequence of combining characters.")
  (setq ucs-normalize-combining-chars-regexp
  (eval-when-compile (concat (regexp-opt (mapcar 'char-to-string combining-chars)) "+")))

(eval-when-compile

  (defun decomposition-translation-alist (decomposition-function)
    (let (decomposition alist)
      (mapc
       (lambda (start-end)
         (do ((char (car start-end) (+ char 1))) ((> char (cdr start-end)))
           (setq decomposition (funcall decomposition-function char))
           (if decomposition
               (setq alist (cons (cons char
                                       (apply 'append
                                              (mapcar (lambda (x) 
                                                        (decomposition-char-recursively
                                                         x decomposition-function))
                                                      decomposition)))
                                 alist)))))
       check-range)
      alist))

  (defun decomposition-char-recursively (char decomposition-function)
    (let ((decomposition (funcall decomposition-function char)))
      (if decomposition
          (apply 'append
                 (mapcar (lambda (x) 
                           (decomposition-char-recursively x decomposition-function))
                         decomposition))
        (list char))))

  (defun alist-list-to-vector (alist)
    (mapcar (lambda (x) (cons (car x) (apply 'vector (cdr x)))) alist))

  (defvar nfd-alist     (alist-list-to-vector (decomposition-translation-alist 'nfd)))
  (defvar nfkd-alist    (alist-list-to-vector (decomposition-translation-alist 'nfkd)))
  (defvar hfs-nfd-alist (alist-list-to-vector (decomposition-translation-alist 'hfs-nfd)))
  )

(define-translation-table 'ucs-normalize-nfd-table
  (make-translation-table-from-alist (eval-when-compile nfd-alist)))
(define-translation-table 'ucs-normalize-nfkd-table
  (make-translation-table-from-alist (eval-when-compile nfkd-alist)))
(define-translation-table 'ucs-normalize-hfs-nfd-table
  (make-translation-table-from-alist (eval-when-compile hfs-nfd-alist)))

(defun ucs-normalize-sort (chars)
  "Sort by canonical combining class of chars."
  (sort chars
        (lambda (ch1 ch2)
          (< (ucs-normalize-ccc ch1) (ucs-normalize-ccc ch2)))))

(defun ucs-normalize-compose-chars (chars composition-predicate)
  "Compose CHARS by COMPOSITION-PREDICATE.
CHARS must be sorted and normalized in starter-combining pairs."
  (if composition-predicate
      (let* ((starter (car chars))
             remain result prev-ccc
             (target-chars (cdr chars))
             target target-ccc
             primary-composite)
     (while target-chars
       (setq target     (car target-chars)
             target-ccc (ucs-normalize-ccc target))
       (if (and (or (null prev-ccc)
                    (< prev-ccc target-ccc))
                (setq primary-composite
                      (ucs-normalize-primary-composite (list starter target)
                                                       composition-predicate)))
           ;; case 1: composable
           (setq starter primary-composite
                 prev-ccc nil)
         (if (= 0 target-ccc)
             ;; case 2: move starter
             (setq result (append result (cons starter (nreverse remain)))
                   starter target
                   remain nil)
           ;; case 3: move target
           (setq prev-ccc target-ccc
                 remain (cons target remain))))
       (setq target-chars (cdr target-chars)))
     (append result (cons starter (nreverse remain))))
    chars))

(defun ucs-normalize-block-compose-chars (chars composition-predicate)
  "Try composing CHARS by COMPOSITION-PREDICATE.
If COMPOSITION-PREDICATE is not given, then do nothing."
  (let ((chars (ucs-normalize-sort chars)))
    (if composition-predicate
        (ucs-normalize-compose-chars chars composition-predicate)
      chars)))

(eval-when-compile

  (defun quick-check-list (decomposition-translation
                           &optional composition-predicate)
    "Quick-Check List for DECOMPOSITION-TRANSLATION and COMPOSITION-PREDICATE.
It includes Singletons, CompositionExclusions, and Non-Starter
decomposition.  "
    (let (entries decomposition composition)
      (mapc
       (lambda (start-end)
         (do ((i (car start-end) (+ i 1))) ((> i (cdr start-end)))
           (setq decomposition
                 (string-to-list
                  (translate-string (char-to-string i) decomposition-translation)))
           (setq composition
                 (ucs-normalize-block-compose-chars decomposition composition-predicate))
           (when (not (equal composition (list i)))
             (setq entries (cons i entries)))))
       check-range)
      ;;(remove-duplicates
       (append entries
               ucs-normalize-composition-exclusions
               non-starter-decompositions)))
  ;;)

  (defvar nfd-quick-check-list     (quick-check-list 'ucs-normalize-nfd-table       ))
  (defvar nfc-quick-check-list     (quick-check-list 'ucs-normalize-nfd-table     t ))
  (defvar nfkd-quick-check-list    (quick-check-list 'ucs-normalize-nfkd-table      ))
  (defvar nfkc-quick-check-list    (quick-check-list 'ucs-normalize-nfkd-table    t ))
  (defvar hfs-nfd-quick-check-list (quick-check-list 'ucs-normalize-hfs-nfd-table
                                                     'ucs-normalize-hfs-nfd-comp-p))
  (defvar hfs-nfc-quick-check-list (quick-check-list 'ucs-normalize-hfs-nfd-table t ))

  (defun quick-check-list-to-regexp (quick-check-list)
    (regexp-opt (mapcar 'char-to-string (append quick-check-list combining-chars))))
)


;; NFD/NFC
(defvar ucs-normalize-nfd-quick-check-regexp nil)
  (setq ucs-normalize-nfd-quick-check-regexp
  (eval-when-compile (quick-check-list-to-regexp nfd-quick-check-list)))
(defvar ucs-normalize-nfc-quick-check-regexp nil)
  (setq ucs-normalize-nfc-quick-check-regexp
  (eval-when-compile (quick-check-list-to-regexp nfc-quick-check-list)))

;; NFKD/NFKC
(defvar ucs-normalize-nfkd-quick-check-regexp nil)
  (setq ucs-normalize-nfkd-quick-check-regexp
  (eval-when-compile (quick-check-list-to-regexp nfkd-quick-check-list)))
(defvar ucs-normalize-nfkc-quick-check-regexp nil)
  (setq ucs-normalize-nfkc-quick-check-regexp
  (eval-when-compile (quick-check-list-to-regexp nfkc-quick-check-list)))

;; HFS-NFD/HFS-NFC
(defvar ucs-normalize-hfs-nfd-quick-check-regexp nil)
  (setq ucs-normalize-hfs-nfd-quick-check-regexp
  (eval-when-compile (quick-check-list-to-regexp hfs-nfd-quick-check-list)))
(defvar ucs-normalize-hfs-nfc-quick-check-regexp nil)
  (setq ucs-normalize-hfs-nfc-quick-check-regexp
  (eval-when-compile (quick-check-list-to-regexp hfs-nfc-quick-check-list)))

;;------------------------------------------------------------------------------------------

;; Normalize local region.

(defun ucs-normalize-block
  (from to &optional decomposition-translation-table composition-predicate)
  "Normalize region FROM TO, by sorting the region with canonical-cc.
If DECOMPOSITION-TRANSLATION-TABLE is given, translate region
before sorting.  If COMPOSE is t and COMPOSITION-PREDICATE is
given, then compose the region by using it."
  (save-restriction
    (narrow-to-region from to)
    (goto-char (point-min))
    (if decomposition-translation-table
        (translate-region from to decomposition-translation-table))
    (goto-char (point-min))
    (let ((start (point)) chars); ccc)
      (while (not (eobp))
        (forward-char)
        (when (or (eobp)
                  (= 0 (ucs-normalize-ccc (char-after (point)))))
          (setq chars
                (nconc chars
                       (ucs-normalize-block-compose-chars
                        (string-to-list (buffer-substring start (point)))
                        composition-predicate))
                start (point)))
        ;;(unless ccc (error "Undefined character can not be normalized!"))
        )
      (delete-region (point-min) (point-max))
;;      (message "debug chars=%s" chars)
      (apply 'insert
             (ucs-normalize-compose-chars
              chars composition-predicate)))))

(defun ucs-normalize-region
  (from to quick-check-regexp translation-table composition-predicate)
  "Normalize region from FROM to TO.
QUICK-CHECK-REGEXP is applied for searching the region.
TRANSLATION-TABLE will be used to decompose region.
COMPOSITION-PREDICATE will be used to compose region."
  (save-excursion
    (save-restriction
      (narrow-to-region from to)
      (goto-char (point-min))
      (let (start-pos starter)
        (while (re-search-forward quick-check-regexp nil t)
          (setq starter (string-to-char (match-string 0)))
          (setq start-pos (match-beginning 0))
          (ucs-normalize-block
           ;; from
           (if (or (= start-pos (point-min))
                   (and (= 0 (ucs-normalize-ccc starter))
                        (not (memq starter ucs-normalize-combining-chars))))
               start-pos (1- start-pos))
           ;; to
           (if (looking-at ucs-normalize-combining-chars-regexp)
               (match-end 0) (1+ start-pos))
           translation-table composition-predicate))))))

;; --------------------------------------------------------------------------------

(defmacro ucs-normalize-string (ucs-normalize-region)
  `(with-temp-buffer
     (insert str)
     (,ucs-normalize-region (point-min) (point-max))
     (buffer-string)))

;;;###autoload
(defun ucs-normalize-NFD-region (from to)
  "Normalize the current region by the Unicode NFD."
  (interactive "r")
  (ucs-normalize-region from to
                        ucs-normalize-nfd-quick-check-regexp
                        'ucs-normalize-nfd-table nil))
;;;###autoload
(defun ucs-normalize-NFD-string (str)
  "Normalize the string STR by the Unicode NFD."
  (ucs-normalize-string ucs-normalize-NFD-region))

;;;###autoload
(defun ucs-normalize-NFC-region (from to)
  "Normalize the current region by the Unicode NFC."
  (interactive "r")
  (ucs-normalize-region from to
                        ucs-normalize-nfc-quick-check-regexp
                        'ucs-normalize-nfd-table t))
;;;###autoload
(defun ucs-normalize-NFC-string (str)
  "Normalize the string STR by the Unicode NFC."
  (ucs-normalize-string ucs-normalize-NFC-region))

;;;###autoload
(defun ucs-normalize-NFKD-region (from to)
  "Normalize the current region by the Unicode NFKD."
  (interactive "r")
  (ucs-normalize-region from to
                        ucs-normalize-nfkd-quick-check-regexp
                        'ucs-normalize-nfkd-table nil))
;;;###autoload
(defun ucs-normalize-NFKD-string (str)
  "Normalize the string STR by the Unicode NFKD."
  (ucs-normalize-string ucs-normalize-NFKD-region))

;;;###autoload
(defun ucs-normalize-NFKC-region (from to)
  "Normalize the current region by the Unicode NFKC."
  (interactive "r")
  (ucs-normalize-region from to
                        ucs-normalize-nfkc-quick-check-regexp
                        'ucs-normalize-nfkd-table t))
;;;###autoload
(defun ucs-normalize-NFKC-string (str)
  "Normalize the string STR by the Unicode NFKC."
  (ucs-normalize-string ucs-normalize-NFKC-region))

;;;###autoload
(defun ucs-normalize-HFS-NFD-region (from to)
  "Normalize the current region by the Unicode NFD and Mac OS's HFS Plus."
  (interactive "r")
  (ucs-normalize-region from to
                        ucs-normalize-hfs-nfd-quick-check-regexp
                        'ucs-normalize-hfs-nfd-table
                        'ucs-normalize-hfs-nfd-comp-p))
;;;###autoload
(defun ucs-normalize-HFS-NFD-string (str)
  "Normalize the string STR by the Unicode NFD and Mac OS's HFS Plus."
  (ucs-normalize-string ucs-normalize-HFS-NFD-region))
;;;###autoload
(defun ucs-normalize-HFS-NFC-region (from to)
  "Normalize the current region by the Unicode NFC and Mac OS's HFS Plus."
  (interactive "r")
  (ucs-normalize-region from to
                        ucs-normalize-hfs-nfc-quick-check-regexp
                        'ucs-normalize-hfs-nfd-table t))
;;;###autoload
(defun ucs-normalize-HFS-NFC-string (str)
  "Normalize the string STR by the Unicode NFC and Mac OS's HFS Plus."
  (ucs-normalize-string ucs-normalize-HFS-NFC-region))

;; Post-read-conversion function for `utf-8-hfs'.
(defun ucs-normalize-hfs-nfd-post-read-conversion (len)
  (save-excursion
    (save-restriction
      (narrow-to-region (point) (+ (point) len))
      (let ((buffer-modified-p (buffer-modified-p)))
        (ucs-normalize-HFS-NFC-region (point-min) (point-max))
        (- (point-max) (point-min))))))

;;; coding-system definition
(define-coding-system 'utf-8-hfs
  "UTF-8 base coding system with normalization on decoding.
The singleton characters in HFS normalization exclusion will not
be decomposed.  It doesn't perform normalization on encoding."
  :coding-type 'utf-8
  :mnemonic ?U
  :charset-list '(unicode)
  ;; :decode-translation-table (not necessary)
  :post-read-conversion 'ucs-normalize-hfs-nfd-post-read-conversion
  ;; NFD encoder is not necessary because MacOS will automatically do it.
  ;; :encode-translation-table 'nfd-encode-translation-table
  ;; :pre-write-conversion 'nfd-encode-function
  )

;;; ucs-normalize.el ends here



-- 
---------------------------------------------------------------------
        kawabata.taichi@gmail.com       KAWABATA, Taichi




^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: normalize.el
  2009-04-18 19:06     ` normalize.el 川幡 太一
@ 2009-04-19  3:39       ` 川幡 太一
  2009-04-21  2:15         ` normalize.el Kenichi Handa
  0 siblings, 1 reply; 11+ messages in thread
From: 川幡 太一 @ 2009-04-19  3:39 UTC (permalink / raw)
  To: Kenichi Handa, emacs-devel; +Cc: Stefan Monnier

Handa-san,

>> In <877i1hzsqx.fsf%kawabata.taichi@gmail.com>, 
>> I wrote:

> > > Great.  Please rename it to uni-normalize.el or ucs-normalize.el.

> > I renamed it to ucs-normalize.el and changed
> > variable/function names as attached.  But, byte-compiling
> > shows these warnings and error.  As the original
> > normalize.el causes the same error, Kawabata-san, could you
> > please fix it.

> I'm sorry for late reply.  Here is fixed version I tried to remove all
> warnings.  I believe this version fully passes `NormalizationTest-5.1.0.txt'.

> However, it takes somewhat longer time for byte-compilation (About 1 min
> for my 2GHz machine), and byte-compiled code will be quite larger than
> original code.  

> These problems would be reduced if we could provide specifically
> customized codes for Hangul character decompositions.

Here is revised code that tried to reduce the compilation time (about
half) and byte-compiled file size (from 2M to 200K) by customizing
Hangul Normalization process.  It passed NormalizationTest-5.1.0.txt.

Cheers,

;;===================================================================

;;; ucs-normalize.el --- Unicode normalization NFC/NFD/NFKD/NFKC

;; Copyright (C) 2009
;;   Free Software Foundation, Inc.

;; Author: Taichi Kawabata <kawabata.taichi@gmail.com>
;; Keywords: unicode, normalization

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; This program has passed the NormalizationTest-5.1.0.txt.
;;
;; References:
;; http://www.unicode.org/reports/tr15/
;; http://www.unicode.org/review/pr-29.html
;;
;; HFS-Normalization:
;; Reference:
;; http://developer.apple.com/technotes/tn/tn1150.html
;;
;; HFS Normalization excludes following area for decomposition.
;;
;;  U+02000 .. U+02FFF :: Punctuation, symbols, dingbats, arrows, etc.
;;                        (Characters in this region will be composed.)
;;  U+0F900 .. U+0FAFF :: CJK compatibility Ideographs.
;;  U+2F800 .. U+2FFFF :: CJK compatibility Ideographs.
;;
;; HFS-Normalization is useful for normalizing text involving CJK Ideographs.
;;
;;;
;;; Implementation Notes on NFC/HFS-NFC.
;;;
;;
;;    <Stages>   Decomposition    Composition
;;   NFD:        'nfd             nil
;;   NFC:        'nfd             t
;;   NFKD:       'nfkd            nil
;;   NFKC:       'nfkd            t
;;   HFS-NFD:    'hfs-nfd         'hfs-nfd-comp-p
;;   HFS-NFC:    'hfs-nfd         t
;;
;; Algorithm for Normalization
;;
;; Before normalization, following data will be prepared.
;;
;; 1. quick-check-list
;;
;;  `quick-check-list' consists of characters that will be decomposed
;;  during normalization.  It includes composition-exclusions,
;;  singletons, non-starter-decompositions and decomposable
;;  characters.
;;
;;  `quick-check-regexp' will search the above characters plus
;;  combining characters.
;;
;; 2. decomposition-translation
;;
;;  `decomposition-translation' is a translation table that will be
;;  used to decompose the characters.
;;
;;
;; Normalization Process
;;
;; A. Searching (`ucs-normalize-region')
;;
;;    Region is searched for `quick-check-regexp' to find possibly
;;    normalizable point.
;;
;; B. Identification of Normalization Block
;;
;;    (1) start of the block
;;        If the searched character is in quick-check-list, then the
;;        beginning of the block is the searched character.
;;        If searched character is combining character, then previous
;;        character will be the target character
;;    (2) end of the block
;;        Block ends at non-composable starter character.
;;
;; C. Decomposition  (`ucs-normalize-block')
;;
;;    The entire block will be decomposed by
;;    `decomposition-translation' table.
;;
;; D. Sorting and Composition  of Smaller Blocks (`ucs-normalize-block-compose-chars')
;;
;;    The block will be split to multiple samller blocks by starter
;;    charcters.  Each block is sorted, and composed if necessary.
;;
;; E. Composition of Entire Block (`ucs-normalize-compose-chars')
;;
;;   Composed blocks are collected and again composed.

;;; Code:

(defconst ucs-normalize-version "1.1beta")

(eval-when-compile (require 'cl))

(eval-when-compile

  (defconst ucs-normalize-composition-exclusions
    '(#x0958 #x0959 #x095A #x095B #x095C #x095D #x095E #x095F
      #x09DC #x09DD #x09DF #x0A33 #x0A36 #x0A59 #x0A5A #x0A5B
      #x0A5E #x0B5C #x0B5D #x0F43 #x0F4D #x0F52 #x0F57 #x0F5C
      #x0F69 #x0F76 #x0F78 #x0F93 #x0F9D #x0FA2 #x0FA7 #x0FAC
      #x0FB9 #xFB1D #xFB1F #xFB2A #xFB2B #xFB2C #xFB2D #xFB2E
      #xFB2F #xFB30 #xFB31 #xFB32 #xFB33 #xFB34 #xFB35 #xFB36
      #xFB38 #xFB39 #xFB3A #xFB3B #xFB3C #xFB3E #xFB40 #xFB41
      #xFB43 #xFB44 #xFB46 #xFB47 #xFB48 #xFB49 #xFB4A #xFB4B
      #xFB4C #xFB4D #xFB4E #x2ADC #x1D15E #x1D15F #x1D160 #x1D161
      #x1D162 #x1D163 #x1D164 #x1D1BB #x1D1BC #x1D1BD #x1D1BE
      #x1D1BF #x1D1C0)
   "Composition Exclusion List.
  This list is taken from
    http://www.unicode.org/Public/UNIDATA/CompositionExclusions-5.1.0.txt")

  ;; Unicode ranges that decompositions & combinings are defined.
  (defvar check-range nil)
    (setq check-range '((#x00a0 . #x3400) (#xA600 . #xAC00) (#xF900 . #x10fff) (#x1d000 . #x1dfff) (#x2f800 . #x2faff)))

  ;; Basic normalization functions
  (defun nfd (char)
    (let ((decomposition
           (get-char-code-property char 'decomposition)))
      (if (and decomposition (numberp (car decomposition)))
          decomposition)))

  (defun nfkd (char)
    (let ((decomposition
           (get-char-code-property char 'decomposition)))
      (if (symbolp (car decomposition)) (cdr decomposition)
        decomposition)))

  (defun hfs-nfd (char)
    (when (or (and (>= char 0) (< char #x2000))
              (and (>= char #x3000) (< char #xf900))
              (and (>= char #xfb00) (< char #x2f800))
              (>= char #x30000))
      (nfd char))))

(defun ucs-normalize-hfs-nfd-comp-p (char)
  (and (>= char #x2000) (< char #x3000)))

(defsubst ucs-normalize-ccc (char)
  (get-char-code-property char 'canonical-combining-class))

;; Data common to all normalizations

(eval-when-compile

  (defvar combining-chars nil) 
    (setq combining-chars nil)
  (defvar decomposition-pair-to-composition nil)
    (setq decomposition-pair-to-composition nil)
  (defvar non-starter-decompositions nil)
    (setq non-starter-decompositions nil)
  (let ((char 0) ccc decomposition)
    (mapc
     (lambda (start-end)
       (do ((char (car start-end) (+ char 1))) ((> char (cdr start-end)))
         (setq ccc (ucs-normalize-ccc char))
         (setq decomposition (get-char-code-property
                              char 'decomposition))
         (if (and ccc (/= 0 ccc)) (add-to-list 'combining-chars char))
         (if (and (numberp (car decomposition))
                  (/= (ucs-normalize-ccc (car decomposition))
                      0))
             (add-to-list 'non-starter-decompositions char))
         (when (numberp (car decomposition))
           (if (and (= 2 (length decomposition))
                    (null (memq char ucs-normalize-composition-exclusions))
                    (null (memq char non-starter-decompositions)))
               (setq decomposition-pair-to-composition
                     (cons (cons decomposition char)
                           decomposition-pair-to-composition)))
           ;; If not singleton decomposition, second and later characters in
           ;; decomposition will be the subject of combining characters.
           (if (cdr decomposition)
               (dolist (char (cdr decomposition))
                 (add-to-list 'combining-chars char))))))
     check-range))

  (setq combining-chars
        (append combining-chars 
                '(?ᄀ ?ᄁ ?ᄂ ?ᄃ ?ᄄ ?ᄅ ?ᄆ ?ᄇ ?ᄈ ?ᄉ ?ᄊ ?ᄋ
                ?ᄌ ?ᄍ ?ᄎ ?ᄏ ?ᄐ ?ᄑ ?ᄒ ?ᅡ ?ᅢ ?ᅣ ?ᅤ ?ᅥ ?ᅦ ?ᅧ ?ᅨ ?ᅩ ?ᅪ
                ?ᅫ ?ᅬ ?ᅭ ?ᅮ ?ᅯ ?ᅰ ?ᅱ ?ᅲ ?ᅳ ?ᅴ ?ᅵ ?ᆨ ?ᆩ ?ᆪ ?ᆫ 
                ?ᆬ ?ᆭ ?ᆮ ?ᆯ ?ᆰ ?ᆱ ?ᆲ ?ᆳ ?ᆴ
                ?ᆵ ?ᆶ ?ᆷ ?ᆸ ?ᆹ ?ᆺ ?ᆻ ?ᆼ ?ᆽ ?ᆾ ?ᆿ ?ᇀ ?ᇁ ?ᇂ)))

  )

(defun ucs-normalize-make-hash-table-from-alist (alist)
  (let ((table (make-hash-table :test 'equal :size 2000)))
    (mapc (lambda (x) (puthash (car x) (cdr x) table)) alist)
    table))

(defvar ucs-normalize-decomposition-pair-to-primary-composite nil
  "Hashtable of decomposed pair to primary composite.
Note that Hangul are excluded.")
  (setq ucs-normalize-decomposition-pair-to-primary-composite
        (ucs-normalize-make-hash-table-from-alist
         (eval-when-compile decomposition-pair-to-composition)))

(defun ucs-normalize-primary-composite (decomposition-pair composition-predicate)
  "Convert DECOMPOSITION-PAIR to primay composit using COMPOSITION-PREDICATE."
  (let ((char (or (gethash decomposition-pair
                           ucs-normalize-decomposition-pair-to-primary-composite)
                  (and (<= #x1100 (car decomposition-pair))
                       (< (car decomposition-pair) #x1113)
                       (<= #x1161 (cadr decomposition-pair))
                       (< (car decomposition-pair) #x1176)
                       (let ((lindex (- (car decomposition-pair) #x1100))
                             (vindex (- (cadr decomposition-pair) #x1161)))
                         (+ #xAC00 (* (+ (* lindex  21) vindex) 28))))
                  (and (<= #xac00 (car decomposition-pair))
                       (< (car decomposition-pair) #xd7a4)
                       (<= #x11a7 (cadr decomposition-pair))
                       (< (cadr decomposition-pair) #x11c3)
                       (= 0 (% (- (car decomposition-pair) #xac00) 28))
                       (let ((tindex (- (cadr decomposition-pair) #x11a7)))
                         (+ (car decomposition-pair) tindex))))))
    (if (and char
             (functionp composition-predicate)
             (null (funcall composition-predicate char)))
        nil char)))

(defvar ucs-normalize-combining-chars nil)
  (setq ucs-normalize-combining-chars (eval-when-compile combining-chars))

(defvar ucs-normalize-combining-chars-regexp nil
  "Regular expression to match sequence of combining characters.")
  (setq ucs-normalize-combining-chars-regexp
  (eval-when-compile (concat (regexp-opt (mapcar 'char-to-string combining-chars)) "+")))

(eval-when-compile

  (defun decomposition-translation-alist (decomposition-function)
    (let (decomposition alist)
      (mapc
       (lambda (start-end)
         (do ((char (car start-end) (+ char 1))) ((> char (cdr start-end)))
           (setq decomposition (funcall decomposition-function char))
           (if decomposition
               (setq alist (cons (cons char
                                       (apply 'append
                                              (mapcar (lambda (x) 
                                                        (decomposition-char-recursively
                                                         x decomposition-function))
                                                      decomposition)))
                                 alist)))))
       check-range)
      alist))

  (defun decomposition-char-recursively (char decomposition-function)
    (let ((decomposition (funcall decomposition-function char)))
      (if decomposition
          (apply 'append
                 (mapcar (lambda (x) 
                           (decomposition-char-recursively x decomposition-function))
                         decomposition))
        (list char))))

  (defun alist-list-to-vector (alist)
    (mapcar (lambda (x) (cons (car x) (apply 'vector (cdr x)))) alist))

  (defvar nfd-alist nil)
    (setq nfd-alist (alist-list-to-vector (decomposition-translation-alist 'nfd)))
  (defvar nfkd-alist nil)
    (setq nfkd-alist (alist-list-to-vector (decomposition-translation-alist 'nfkd)))
  (defvar hfs-nfd-alist nil)
    (setq hfs-nfd-alist (alist-list-to-vector (decomposition-translation-alist 'hfs-nfd)))
  )

(defvar ucs-normalize-hangul-translation-alist nil)
  (setq ucs-normalize-hangul-translation-alist
        (let ((i 0) entries)
          (while (< i 11172)
            (setq entries 
                  (cons (cons (+ #xac00 i) 
                              (if (= 0 (% i 28))
                                  (vector (+ #x1100 (/ i 588))
                                          (+ #x1161 (/ (% i 588) 28)))
                                (vector (+ #x1100 (/ i 588))
                                        (+ #x1161 (/ (% i 588) 28))
                                        (+ #x11a7 (% i 28)))))
                        entries)
                  i (1+ i))) entries))

(defun ucs-normalize-make-translation-table-from-alist (alist)
  (make-translation-table-from-alist 
     (append alist ucs-normalize-hangul-translation-alist)))

(define-translation-table 'ucs-normalize-nfd-table
  (ucs-normalize-make-translation-table-from-alist (eval-when-compile nfd-alist)))
(define-translation-table 'ucs-normalize-nfkd-table
  (ucs-normalize-make-translation-table-from-alist (eval-when-compile nfkd-alist)))
(define-translation-table 'ucs-normalize-hfs-nfd-table
  (ucs-normalize-make-translation-table-from-alist (eval-when-compile hfs-nfd-alist)))

(defun ucs-normalize-sort (chars)
  "Sort by canonical combining class of chars."
  (sort chars
        (lambda (ch1 ch2)
          (< (ucs-normalize-ccc ch1) (ucs-normalize-ccc ch2)))))

(defun ucs-normalize-compose-chars (chars composition-predicate)
  "Compose CHARS by COMPOSITION-PREDICATE.
CHARS must be sorted and normalized in starter-combining pairs."
  (if composition-predicate
      (let* ((starter (car chars))
             remain result prev-ccc
             (target-chars (cdr chars))
             target target-ccc
             primary-composite)
     (while target-chars
       (setq target     (car target-chars)
             target-ccc (ucs-normalize-ccc target))
       (if (and (or (null prev-ccc)
                    (< prev-ccc target-ccc))
                (setq primary-composite
                      (ucs-normalize-primary-composite (list starter target)
                                                       composition-predicate)))
           ;; case 1: composable
           (setq starter primary-composite
                 prev-ccc nil)
         (if (= 0 target-ccc)
             ;; case 2: move starter
             (setq result (nconc result (cons starter (nreverse remain)))
                   starter target
                   remain nil)
           ;; case 3: move target
           (setq prev-ccc target-ccc
                 remain (cons target remain))))
       (setq target-chars (cdr target-chars)))
     (nconc result (cons starter (nreverse remain))))
    chars))

(defun ucs-normalize-block-compose-chars (chars composition-predicate)
  "Try composing CHARS by COMPOSITION-PREDICATE.
If COMPOSITION-PREDICATE is not given, then do nothing."
  (let ((chars (ucs-normalize-sort chars)))
    (if composition-predicate
        (ucs-normalize-compose-chars chars composition-predicate)
      chars)))

(eval-when-compile

  (defun quick-check-list (decomposition-translation
                           &optional composition-predicate)
    "Quick-Check List for DECOMPOSITION-TRANSLATION and COMPOSITION-PREDICATE.
It includes Singletons, CompositionExclusions, and Non-Starter
decomposition.  "
    (let (entries decomposition composition)
      (mapc
       (lambda (start-end)
         (do ((i (car start-end) (+ i 1))) ((> i (cdr start-end)))
           (setq decomposition
                 (string-to-list
                  (translate-string (char-to-string i) decomposition-translation)))
           (setq composition
                 (ucs-normalize-block-compose-chars decomposition composition-predicate))
           (when (not (equal composition (list i)))
             (setq entries (cons i entries)))))
       check-range)
      ;;(remove-duplicates
       (append entries
               ucs-normalize-composition-exclusions
               non-starter-decompositions)))
  ;;)

  (defvar nfd-quick-check-list nil)
    (setq nfd-quick-check-list     (quick-check-list 'ucs-normalize-nfd-table      ))
  (defvar nfc-quick-check-list nil)
    (setq nfc-quick-check-list     (quick-check-list 'ucs-normalize-nfd-table     t ))
  (defvar nfkd-quick-check-list nil)
    (setq nfkd-quick-check-list    (quick-check-list 'ucs-normalize-nfkd-table      ))
  (defvar nfkc-quick-check-list nil)
    (setq nfkc-quick-check-list    (quick-check-list 'ucs-normalize-nfkd-table    t ))
  (defvar hfs-nfd-quick-check-list nil)
    (setq hfs-nfd-quick-check-list (quick-check-list 'ucs-normalize-hfs-nfd-table
                                                     'ucs-normalize-hfs-nfd-comp-p))
  (defvar hfs-nfc-quick-check-list nil)
    (setq hfs-nfc-quick-check-list (quick-check-list 'ucs-normalize-hfs-nfd-table t ))

  (defun quick-check-list-to-regexp (quick-check-list)
    (regexp-opt (mapcar 'char-to-string (append quick-check-list combining-chars))))

  (defun quick-check-decomposition-list-to-regexp (quick-check-list)
    (concat (quick-check-list-to-regexp quick-check-list) "\\|[가-힣]"))

  (defun quick-check-composition-list-to-regexp (quick-check-list)
    (concat (quick-check-list-to-regexp quick-check-list) "\\|[ᄀ-하-ᅵᆧ-ᇂ]"))
)


;; NFD/NFC
(defvar ucs-normalize-nfd-quick-check-regexp nil)
  (setq ucs-normalize-nfd-quick-check-regexp
  (eval-when-compile (quick-check-decomposition-list-to-regexp nfd-quick-check-list)))
(defvar ucs-normalize-nfc-quick-check-regexp nil)
  (setq ucs-normalize-nfc-quick-check-regexp
  (eval-when-compile (quick-check-composition-list-to-regexp nfc-quick-check-list)))

;; NFKD/NFKC
(defvar ucs-normalize-nfkd-quick-check-regexp nil)
  (setq ucs-normalize-nfkd-quick-check-regexp
  (eval-when-compile (quick-check-decomposition-list-to-regexp nfkd-quick-check-list)))
(defvar ucs-normalize-nfkc-quick-check-regexp nil)
  (setq ucs-normalize-nfkc-quick-check-regexp
  (eval-when-compile (quick-check-composition-list-to-regexp nfkc-quick-check-list)))

;; HFS-NFD/HFS-NFC
(defvar ucs-normalize-hfs-nfd-quick-check-regexp nil)
  (setq ucs-normalize-hfs-nfd-quick-check-regexp
  (eval-when-compile (concat (quick-check-decomposition-list-to-regexp hfs-nfd-quick-check-list))))
(defvar ucs-normalize-hfs-nfc-quick-check-regexp nil)
  (setq ucs-normalize-hfs-nfc-quick-check-regexp
  (eval-when-compile (quick-check-composition-list-to-regexp hfs-nfc-quick-check-list)))

;;------------------------------------------------------------------------------------------

;; Normalize local region.

(defun ucs-normalize-block
  (from to &optional decomposition-translation-table composition-predicate)
  "Normalize region FROM TO, by sorting the region with canonical-cc.
If DECOMPOSITION-TRANSLATION-TABLE is given, translate region
before sorting.  If COMPOSITION-PREDICATE is given, then compose
the region by using it."
  (save-restriction
    (narrow-to-region from to)
    (goto-char (point-min))
    (if decomposition-translation-table
        (translate-region from to decomposition-translation-table))
    (goto-char (point-min))
    (let ((start (point)) chars); ccc)
      (while (not (eobp))
        (forward-char)
        (when (or (eobp)
                  (= 0 (ucs-normalize-ccc (char-after (point)))))
          (setq chars
                (nconc chars
                       (ucs-normalize-block-compose-chars
                        (string-to-list (buffer-substring start (point)))
                        composition-predicate))
                start (point)))
        ;;(unless ccc (error "Undefined character can not be normalized!"))
        )
      (delete-region (point-min) (point-max))
      (apply 'insert
             (ucs-normalize-compose-chars
              chars composition-predicate)))))

(defun ucs-normalize-region
  (from to quick-check-regexp translation-table composition-predicate)
  "Normalize region from FROM to TO.
QUICK-CHECK-REGEXP is applied for searching the region.
TRANSLATION-TABLE will be used to decompose region.
COMPOSITION-PREDICATE will be used to compose region."
  (save-excursion
    (save-restriction
      (narrow-to-region from to)
      (goto-char (point-min))
      (let (start-pos starter)
        (while (re-search-forward quick-check-regexp nil t)
          (setq starter (string-to-char (match-string 0)))
          (setq start-pos (match-beginning 0))
          (ucs-normalize-block
           ;; from
           (if (or (= start-pos (point-min))
                   (and (= 0 (ucs-normalize-ccc starter))
                        (not (memq starter ucs-normalize-combining-chars))))
               start-pos (1- start-pos))
           ;; to
           (if (looking-at ucs-normalize-combining-chars-regexp)
               (match-end 0) (1+ start-pos))
           translation-table composition-predicate))))))

;; --------------------------------------------------------------------------------

(defmacro ucs-normalize-string (ucs-normalize-region)
  `(with-temp-buffer
     (insert str)
     (,ucs-normalize-region (point-min) (point-max))
     (buffer-string)))

;;;###autoload
(defun ucs-normalize-NFD-region (from to)
  "Normalize the current region by the Unicode NFD."
  (interactive "r")
  (ucs-normalize-region from to
                        ucs-normalize-nfd-quick-check-regexp
                        'ucs-normalize-nfd-table nil))
;;;###autoload
(defun ucs-normalize-NFD-string (str)
  "Normalize the string STR by the Unicode NFD."
  (ucs-normalize-string ucs-normalize-NFD-region))

;;;###autoload
(defun ucs-normalize-NFC-region (from to)
  "Normalize the current region by the Unicode NFC."
  (interactive "r")
  (ucs-normalize-region from to
                        ucs-normalize-nfc-quick-check-regexp
                        'ucs-normalize-nfd-table t))
;;;###autoload
(defun ucs-normalize-NFC-string (str)
  "Normalize the string STR by the Unicode NFC."
  (ucs-normalize-string ucs-normalize-NFC-region))

;;;###autoload
(defun ucs-normalize-NFKD-region (from to)
  "Normalize the current region by the Unicode NFKD."
  (interactive "r")
  (ucs-normalize-region from to
                        ucs-normalize-nfkd-quick-check-regexp
                        'ucs-normalize-nfkd-table nil))
;;;###autoload
(defun ucs-normalize-NFKD-string (str)
  "Normalize the string STR by the Unicode NFKD."
  (ucs-normalize-string ucs-normalize-NFKD-region))

;;;###autoload
(defun ucs-normalize-NFKC-region (from to)
  "Normalize the current region by the Unicode NFKC."
  (interactive "r")
  (ucs-normalize-region from to
                        ucs-normalize-nfkc-quick-check-regexp
                        'ucs-normalize-nfkd-table t))
;;;###autoload
(defun ucs-normalize-NFKC-string (str)
  "Normalize the string STR by the Unicode NFKC."
  (ucs-normalize-string ucs-normalize-NFKC-region))

;;;###autoload
(defun ucs-normalize-HFS-NFD-region (from to)
  "Normalize the current region by the Unicode NFD and Mac OS's HFS Plus."
  (interactive "r")
  (ucs-normalize-region from to
                        ucs-normalize-hfs-nfd-quick-check-regexp
                        'ucs-normalize-hfs-nfd-table
                        'ucs-normalize-hfs-nfd-comp-p))
;;;###autoload
(defun ucs-normalize-HFS-NFD-string (str)
  "Normalize the string STR by the Unicode NFD and Mac OS's HFS Plus."
  (ucs-normalize-string ucs-normalize-HFS-NFD-region))
;;;###autoload
(defun ucs-normalize-HFS-NFC-region (from to)
  "Normalize the current region by the Unicode NFC and Mac OS's HFS Plus."
  (interactive "r")
  (ucs-normalize-region from to
                        ucs-normalize-hfs-nfc-quick-check-regexp
                        'ucs-normalize-hfs-nfd-table t))
;;;###autoload
(defun ucs-normalize-HFS-NFC-string (str)
  "Normalize the string STR by the Unicode NFC and Mac OS's HFS Plus."
  (ucs-normalize-string ucs-normalize-HFS-NFC-region))

;; Post-read-conversion function for `utf-8-hfs'.
(defun ucs-normalize-hfs-nfd-post-read-conversion (len)
  (save-excursion
    (save-restriction
      (narrow-to-region (point) (+ (point) len))
      (let ((buffer-modified-p (buffer-modified-p)))
        (ucs-normalize-HFS-NFC-region (point-min) (point-max))
        (- (point-max) (point-min))))))

;;; coding-system definition
(define-coding-system 'utf-8-hfs
  "UTF-8 base coding system with normalization on decoding.
The singleton characters in HFS normalization exclusion will not
be decomposed.  It doesn't perform normalization on encoding."
  :coding-type 'utf-8
  :mnemonic ?U
  :charset-list '(unicode)
  ;; :decode-translation-table (not necessary)
  :post-read-conversion 'ucs-normalize-hfs-nfd-post-read-conversion
  ;; NFD encoder is not necessary because MacOS will automatically do it.
  ;; :encode-translation-table 'nfd-encode-translation-table
  ;; :pre-write-conversion 'nfd-encode-function
  )

(provide 'ucs-normalize)

;;; ucs-normalize.el ends here



-- 
---------------------------------------------------------------------
  川幡 太一  (kawabata.taichi@gmail.com)       KAWABATA, Taichi




^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: normalize.el
  2009-04-19  3:39       ` normalize.el 川幡 太一
@ 2009-04-21  2:15         ` Kenichi Handa
  2009-04-21 17:10           ` normalize.el 川幡 太一
  0 siblings, 1 reply; 11+ messages in thread
From: Kenichi Handa @ 2009-04-21  2:15 UTC (permalink / raw)
  To: 川幡 太一; +Cc: monnier, emacs-devel

In article <87skk5wbu9.fsf%kawabata.taichi@gmail.com>, =?iso-2022-jp?B?GyRCQG5IKBsoQiAbJEJCQDBsGyhC?= <kawabata.taichi@gmail.com>( Taichi  KAWABATA ) writes:

> Here is revised code that tried to reduce the compilation time (about
> half) and byte-compiled file size (from 2M to 200K) by customizing
> Hangul Normalization process.  It passed NormalizationTest-5.1.0.txt.

Thank you, but it still causes this compiler error.

Entering directory `/usr/local/work/ucs-normalization/'
ucs-normalize.el:409:10:Error: Symbol's function definition is void: translate-string

---
Kenichi Handa
handa@m17n.org




^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: normalize.el
  2009-04-21  2:15         ` normalize.el Kenichi Handa
@ 2009-04-21 17:10           ` 川幡 太一
  0 siblings, 0 replies; 11+ messages in thread
From: 川幡 太一 @ 2009-04-21 17:10 UTC (permalink / raw)
  To: Kenichi Handa; +Cc: monnier, emacs-devel

川幡です。

>> <E1Lw5WU-00063j-87@etlken> で, 
>> 半田さん は書きました. 

> In article <87skk5wbu9.fsf%kawabata.taichi@gmail.com>, =?iso-2022-jp?B?GyRCQG5IKBsoQiAbJEJCQDBsGyhC?= <kawabata.taichi@gmail.com>( Taichi  KAWABATA ) writes:

> > Here is revised code that tried to reduce the compilation time (about
> > half) and byte-compiled file size (from 2M to 200K) by customizing
> > Hangul Normalization process.  It passed NormalizationTest-5.1.0.txt.

> Thank you, but it still causes this compiler error.

> Entering directory `/usr/local/work/ucs-normalization/'
> ucs-normalize.el:409:10:Error: Symbol's function definition is void:
> translate-string

I've fixed it.  Instead of attaching the newest file every time, 
I've decided to upload it to the following URL, so that the one can
easily check the change history.

http://lookup.cvs.sourceforge.net/viewvc/lookup/lookup/lisp/ucs-normalize.el?view=log

`ndspotlight.el', SpotLight Emacs interface is one good example of
utilization of ucs-normalize.el.  SpotLight handles HFS-NFD text data,
but it is better to normalize it to HFS-NFC to view it in Emacs.

-- 
---------------------------------------------------------------------
  川幡 太一  (kawabata.taichi@gmail.com)       KAWABATA, Taichi




^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2009-04-21 17:10 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-04-02  6:40 normalize.el Kenichi Handa
2009-04-02  8:10 ` normalize.el Kim F. Storm
2009-04-02 11:54   ` normalize.el Kenichi Handa
2009-04-02 16:49 ` normalize.el Stefan Monnier
2009-04-08  5:39   ` normalize.el Kenichi Handa
2009-04-18 19:06     ` normalize.el 川幡 太一
2009-04-19  3:39       ` normalize.el 川幡 太一
2009-04-21  2:15         ` normalize.el Kenichi Handa
2009-04-21 17:10           ` normalize.el 川幡 太一
2009-04-03  9:10 ` normalize.el Richard M Stallman
2009-04-03 13:24   ` normalize.el Stefan Monnier

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.