;;; japanese-hooks.el --- pre-loaded support for Japanese.

;; Copyright (C) 1992,93,94,95 Free Software Foundation, Inc.
;; Copyright (C) 1995 Amdahl Corporation.
;; Copyright (C) 1995 Sun Microsystems.

;; This file is part of XEmacs.

;; XEmacs 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 2, or (at your option)
;; any later version.

;; XEmacs 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 XEmacs; see the file COPYING.  If not, write to the Free
;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

;;; Synched up with: Mule 2.3.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; JAPANESE
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Syntax of Japanese characters.
(loop for row in '(33 34 40) do (modify-syntax-entry `[japanese ,row] "."))
(loop for row from 35 to 39  do (modify-syntax-entry `[japanese ,row] "e"))
(loop for row from 48 to 126 do (modify-syntax-entry `[japanese ,row] "e"))
(loop for char in '(?ー ?゛ ?゜ ?ヽ ?ヾ ?ゝ ?ゞ ?〃 ?仝 ?々 ?〆 ?〇)
      do (modify-syntax-entry char "e"))
(modify-syntax-entry ?\（ "(）")
(modify-syntax-entry ?\［ "(］")
(modify-syntax-entry ?\｛ "(｝")
(modify-syntax-entry ?\「 "(」")
(modify-syntax-entry ?\『 "(』")
(modify-syntax-entry ?\） ")（")
(modify-syntax-entry ?\］ ")［")
(modify-syntax-entry ?\｝ ")｛")
(modify-syntax-entry ?\」 ")「")
(modify-syntax-entry ?\』 ")『")

(modify-syntax-entry 'japanese-kana "w")

(modify-syntax-entry 'japanese-2 "e")

;;; Character categories S, A, H, K, G, Y, and C
(define-category ?S "Japanese 2-byte symbol character.")
(modify-category-entry [japanese 33] ?S)
(modify-category-entry [japanese 34] ?S)
(modify-category-entry [japanese 40] ?S)
(define-category ?A "Japanese 2-byte Alphanumeric character.")
(modify-category-entry [japanese 35] ?A)
(define-category ?H "Japanese 2-byte Hiragana character.")
(modify-category-entry [japanese 36] ?H)
(define-category ?K "Japanese 2-byte Katakana character.")
(modify-category-entry [japanese 37] ?K)
(define-category ?G "Japanese 2-byte Greek character.")
(modify-category-entry [japanese 38] ?G)
(define-category ?Y "Japanese 2-byte Cyrillic character.")
(modify-category-entry [japanese 39] ?Y)
(define-category ?C "Japanese 2-byte Kanji characters.")
(loop for row from 48 to 126 do (modify-category-entry `[japanese ,row] ?C))
(loop for char in '(?ー ?゛ ?゜)
      do (modify-category-entry char ?K)
         (modify-category-entry char ?H))
(loop for char in '(?ヽ ?ヾ ?ゝ ?ゞ ?〃 ?仝 ?々 ?〆 ?〇)
      do (modify-category-entry char ?C))
(modify-category-entry 'japanese-2 ?C)

(defvar japanese-word-regexp
  "\\cA+\\cH*\\|\\cK+\\cH*\\|\\cC+\\cH*\\|\\cH+\\|\\ck+\\|\\sw+"
  "Regular expression used to match a Japanese word.")

(set-word-regexp japanese-word-regexp)
(setq forward-word-regexp  "\\w\\>")
(setq backward-word-regexp "\\<\\w")

;;; Paragraph setting
(setq sentence-end
      (concat 
       "\\("
       "\\("
       "[.?!][]\"')}]*"
       "\\|"
       "[．？！][］”’）｝〕〉》」』]*"
       "\\)"
       "\\($\\|\t\\|  \\)"
       "\\|"
       "。"
       "\\)"
       "[ \t\n]*"))
(setq paragraph-start    "^[ 　\t\n\f]")
(setq paragraph-separate "^[ 　\t\f]*$")

;; EGG specific setup
(when (featurep 'egg)
  (setq wnn-server-type 'jserver)
  (load "its/hira")
  (load "its/kata")
  (load "its/hankaku")
  (load "its/zenkaku")
  (setq its:*standard-modes*
        (append
         (list (its:get-mode-map "roma-kana")
               (its:get-mode-map "roma-kata")
               (its:get-mode-map "downcase")
               (its:get-mode-map "upcase")
               (its:get-mode-map "zenkaku-downcase")
               (its:get-mode-map "zenkaku-upcase"))
         its:*standard-modes*)))

(make-coding-system
 'shift-jis 'shift-jis
 "Coding-system of Shift-JIS used in Japan."
 '(mnemonic "SJIS"))

(copy-coding-system 'shift-jis 'sjis)

(make-coding-system
 'iso-2022-jp 'iso2022
 "Coding-system used for communication with mail and news in Japan."
 '(charset-g0 ascii
   short t
   seven t
   input-charset-conversion ((japanese-roman ascii)
			     (japanese-old japanese))
   mnemonic "Mail/Ja"
   ))

(copy-coding-system 'iso-2022-jp 'junet)

(make-coding-system
 'oldjis 'iso2022
 "Coding-system used for old JIS terminal."
 '(charset-g0 ascii
   short t
   seven t
   output-charset-conversion ((ascii japanese-roman)
			      (japanese japanese-old))
   mnemonic "Mail/Ja-old"
   ))

(make-coding-system
 'euc-japan 'iso2022
 "Coding-system of Japanese EUC (Extended Unix Code)."
 '(charset-g0 ascii
   charset-g1 japanese
   charset-g2 japanese-kana
   charset-g3 japanese-2
   short t
   mnemonic "EUC/Ja"
   ))

(define-language-environment 'japanese
  "Japanese (includes JIS, Shift-JIS, and EUC)"
  (lambda ()
    (set-coding-category-system 'iso-7 'iso-2022-jp)
    (set-coding-category-system 'iso-8-2 'euc-japan)
    (set-coding-priority-list
     '(iso-8-2 iso-8-designate iso-8-1 shift-jis big5))
    (when (featurep 'egg)
      (setq-default its:*current-map* (its:get-mode-map "roma-kana")))

    ;; Added by mrb, who doesn't speak japanese - so be sceptical...
    ;; (when (string-match "solaris\\|sunos" system-configuration)
    ;;(set-native-coding-system          'euc-japan) ; someday
    (set-pathname-coding-system 'euc-japan)
    (add-hook 'comint-exec-hook
              (lambda ()
                (let ((proc (get-buffer-process (current-buffer))))
                  (set-process-input-coding-system  proc 'euc-japan)
                  (set-process-output-coding-system proc 'euc-japan))))
    (set-file-coding-system-for-read 'autodetect)
    (set-default-file-coding-system  'euc-japan)
    (setq keyboard-coding-system     'euc-japan)
    (setq display-coding-system      'euc-japan)
    (set-charset-registry 'ascii "JISX0201")
    
    (when (eq system-type 'ms-dos)
      ;; Shift-JIS is the standard coding system under Japanese MS-DOS
      ;; This isn't really code - just a hint to future implementors
      (setq keyboard-coding-system       'shift-jis-dos)
      (setq display-coding-system        'shift-jis-dos)
      (set-default-file-coding-system    'shift-jis-dos)
      (set-default-process-coding-system 'shift-jis-dos 'shift-jis-dos))
    ))

(set-coding-category-system 'shift-jis 'shift-jis)
