diff -uNr --exclude='*~' --exclude='*.orig' emacs/Emacs.finkwrap emacs22-carbon/Emacs.finkwrap
--- emacs/Emacs.finkwrap	1970-01-01 09:00:00.000000000 +0900
+++ emacs22-carbon/Emacs.finkwrap	2005-12-31 09:40:28.000000000 +0900
@@ -0,0 +1,3 @@
+#!/bin/bash
+source @PREFIX@/bin/init.sh
+exec -a "$0" "$0.bin" "$@"
diff -uNr --exclude='*~' --exclude='*.orig' emacs/Makefile.in emacs22-carbon/Makefile.in
--- emacs/Makefile.in	2005-12-06 20:50:23.000000000 +0900
+++ emacs22-carbon/Makefile.in	2005-12-31 09:40:28.000000000 +0900
@@ -423,7 +423,7 @@
 	      (cd $${dir}; tar -chf - . ) \
 		| (cd $${dest}; umask 022; \
                    tar -xvf - && cat > /dev/null) || exit 1; \
-	      find $${dest} -exec chown $${LOGNAME:-$$USERNAME} {} ';' ;\
+	      find $${dest} -exec chown root:admin {} ';' ;\
 	      for subdir in `find $${dest} -type d ! -name RCS ! -name CVS -print` ; do \
 		chmod a+rx $${subdir} ; \
 		rm -rf $${subdir}/RCS ; \
@@ -463,7 +463,7 @@
 	   echo "Copying etc/DOC-* to ${docdir} ..." ; \
 	   (cd ./etc; tar -chf - DOC*) \
 	     |(cd ${docdir}; umask 022; tar -xvf - && cat > /dev/null) || exit 1; \
-	   (cd $(docdir); chown $${LOGNAME:-$$USERNAME} DOC*; chmod a+r DOC*; \
+	   (cd $(docdir); chown root:admin DOC*; chmod a+r DOC*; \
 	    if test "`echo DOC-*`" != "DOC-*"; then rm DOC; fi); \
 	else true; fi
 	-unset CDPATH; \
@@ -475,7 +475,7 @@
 	   echo "Copying lisp/*.el and lisp/*.elc to ${lispdir} ..." ; \
 	   (cd lisp; tar -chf - *.el *.elc) \
 	     |(cd ${lispdir}; umask 022; tar -xvf - && cat > /dev/null) || exit 1; \
-	   (cd ${lispdir}; find . -exec chown $${LOGNAME:-$$USERNAME} {} ';') ; \
+	   (cd ${lispdir}; find . -exec chown root:admin {} ';') ; \
 	else true; fi
 	-unset CDPATH; \
 	if [ -n "${GZIP_PROG}" ]; \
diff -uNr --exclude='*~' --exclude='*.orig' emacs/configure emacs22-carbon/configure
--- emacs/configure	2005-12-06 20:50:24.000000000 +0900
+++ emacs22-carbon/configure	2005-12-31 09:40:23.000000000 +0900
@@ -1359,7 +1359,7 @@
 etcdir='${datadir}/emacs/${version}/etc'
 archlibdir='${libexecdir}/emacs/${version}/${configuration}'
 docdir='${datadir}/emacs/${version}/etc'
-gamedir='${localstatedir}/games/emacs'
+gamedir='${localstatedir}/games/@EMACS-FLAVOR@'
 
 gameuser=games
 
diff -uNr --exclude='*~' --exclude='*.orig' emacs/fix-info emacs22-carbon/fix-info
--- emacs/fix-info	1970-01-01 09:00:00.000000000 +0900
+++ emacs22-carbon/fix-info	2005-12-31 09:40:28.000000000 +0900
@@ -0,0 +1,10 @@
+#!/bin/sh
+
+THE_INFO_PREFIX=$1
+
+for file in ada-mode autotype ccmode cl dired-x ebrowse ediff efaq \
+            emacs emacs-mime eshell eudc forms gnus idlwave info message mh-e \
+            pcl-cvs reftex sc speedbar vip viper widget woman
+do
+    ./mangle-info ${THE_INFO_PREFIX}/${file}
+done
diff -uNr --exclude='*~' --exclude='*.orig' emacs/lisp/loadup.el emacs22-carbon/lisp/loadup.el
--- emacs/lisp/loadup.el	2005-12-31 09:34:09.000000000 +0900
+++ emacs22-carbon/lisp/loadup.el	2005-12-31 09:40:04.000000000 +0900
@@ -205,6 +205,9 @@
 (if (fboundp 'atan)	; preload some constants and
     (progn		; floating pt. functions if we have float support.
       (load "emacs-lisp/float-sup")))
+(if (eq system-type 'darwin)
+    (progn
+      (load "term/mac-im")))
 (message "%s" (garbage-collect))
 
 (load "vc-hooks")
diff -uNr --exclude='*~' --exclude='*.orig' emacs/lisp/startup.el emacs22-carbon/lisp/startup.el
--- emacs/lisp/startup.el	2005-12-31 09:34:13.000000000 +0900
+++ emacs22-carbon/lisp/startup.el	2005-12-31 09:40:28.000000000 +0900
@@ -288,6 +288,10 @@
 
 (defvar default-frame-background-mode)
 
+(defconst fink-emacs-flavor '@EMACS-FLAVOR@
+  "A symbol representing the particular fink flavor of emacs running.
+Something like 'emacs20, 'xemacs20, etc.")
+
 (defvar pure-space-overflow nil
   "Non-nil if building Emacs overflowed pure space.")
 
@@ -804,8 +808,20 @@
     ;; Run the site-start library if it exists.  The point of this file is
     ;; that it is run before .emacs.  There is no point in doing this after
     ;; .emacs; that is useless.
+
+    ;; (if site-run-file
+    ;;     (load site-run-file t t))
+
+    ;; Fink version of site-start.
     (if site-run-file
-	(load site-run-file t t))
+        (progn
+          ;; Load all the fink package snippets.
+          ;; It's in here because we want -q to kill it too.
+          (if (load "fink-startup" t t nil)
+              (fink-startup fink-emacs-flavor))
+          ;; Now the normal site file...
+          (load site-run-file t t nil)))
+
 
     ;; Sites should not disable this.  Only individuals should disable
     ;; the startup message.
diff -uNr --exclude='*~' --exclude='*.orig' emacs/lisp/term/mac-im.el emacs22-carbon/lisp/term/mac-im.el
--- emacs/lisp/term/mac-im.el	1970-01-01 09:00:00.000000000 +0900
+++ emacs22-carbon/lisp/term/mac-im.el	2005-12-31 09:40:04.000000000 +0900
@@ -0,0 +1,334 @@
+;; mac-im.el --- Input Method for Mac OS X -*-coding: iso-2022-7bit;-*-
+
+;; Copyright (C) 2005 HASHIMOTO Taiichi <taiichi2@mac.com>
+;; Keywords: input method, Mac OS X
+
+;; This file is not 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 2, or (at your option)
+;; any later version.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs; see the file COPYING.  If not, write to the
+;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+;; Boston, MA 02111-1307, USA.
+
+;; Written by Taiichi Hashimoto (taiichi2@mac.com).
+
+;;
+;; Faces for inline input method on MacOSX.
+;;
+(make-face 'mac-input-method-face)
+(set-face-underline-p 'mac-input-method-face t)
+
+(make-face 'mac-input-method-selection-face)
+(set-face-foreground 'mac-input-method-selection-face "black")
+(set-face-background 'mac-input-method-selection-face "light sky blue")
+(set-face-underline-p 'mac-input-method-selection-face t)
+
+
+;;
+;; Variables and funtions for inline input method on MacOSX.
+;;
+(defvar mac-input-method-preedit-start (make-marker)
+  "Start position on buffer for inline input method preedit.")
+(make-variable-buffer-local 'mac-input-method-preedit-start)
+
+(defvar  mac-input-method-preedit-end (make-marker)
+  "End position on buffer for inline input method preedit.")
+(make-variable-buffer-local 'mac-input-method-preedit-end)
+
+(defconst mac-input-method-face-alist
+  '((1 . nil)                             ; kTSMHiliteCaretPosition
+    (2 . mac-input-method-face)           ; kTSMHiliteRawText
+    (3 . mac-input-method-face)           ; kTSMHiliteSelectedRawText
+    (4 . mac-input-method-face)           ; kTSMHiliteConvertedText
+    (5 . mac-input-method-selection-face) ; kTSMHiliteSelectedConvertedText
+    (6 . mac-input-method-face)           ; kTSMHiliteBlockFillText
+    (7 . mac-input-method-face)           ; kTSMHiliteOutlineText
+    (8 . mac-input-method-selection-face) ; kTSMHiliteSelectedText
+    (9 . nil))                            ; kTSMHiliteNoteHilite
+  "An alist of property type of input method vs face.")
+
+(defconst mac-input-method-property-function-alist
+  '((1 . mac-move-caret)                  ; kTSMHiliteCaretPosition
+    (2 . mac-set-input-method-face)       ; kTSMHiliteRawText
+    (3 . mac-set-input-method-face)       ; kTSMHiliteSelectedRawText
+    (4 . mac-set-input-method-face)       ; kTSMHiliteConvertedText
+    (5 . mac-set-input-method-face)       ; kTSMHiliteSelectedConvertedText
+    (6 . mac-set-input-method-face)       ; kTSMHiliteBlockFillText
+    (7 . mac-set-input-method-face)       ; kTSMHiliteOutlineText
+    (8 . mac-set-input-method-face)       ; kTSMHiliteSelectedText
+    (9 . nil))                            ; kTSMHiliteNoteHilite
+  "An alist of property type of input method vs function.")
+
+(defun mac-set-input-method-property (str coding-system prop)
+  "Function to add property to string inserted on buffer."
+  (let* ((type (nth 0 prop))
+	 (pos (marker-position mac-input-method-preedit-start))
+	 (beg (+ pos (length (decode-coding-string
+			      (substring str 0 (nth 1 prop))
+			      coding-system))))
+	 (end (+ pos (length (decode-coding-string
+			      (substring str 0 (nth 2 prop))
+			      coding-system))))
+	 (func (assq type mac-input-method-property-function-alist)))
+    (and func (cdr func) (funcall (cdr func) type beg end))))
+
+(defun mac-set-input-method-face (type beg end)
+  "Function to set face to string inserted on buffer."
+  (let* ((face (assq type mac-input-method-face-alist))
+	 (obj (make-overlay beg end)))
+    (if (and face (cdr face))
+	(overlay-put obj 'face (cdr face)))))
+
+(defun mac-move-caret (type beg end)
+  "Function to set face to string inserted on buffer."
+  (goto-char beg))
+
+(defun mac-input-method-preedit (event)
+  "Function to insert input method preedit string and set property of it."
+  (interactive "e")
+
+  (let* ((coding-system (keyboard-coding-system))
+	 (arg (cdr event))
+	 (str (car arg))
+	 (fix (cadr arg))
+	 (prop (cddr arg)))
+
+    ;; initialize start/end marker for input method
+    (if (equal mac-input-method-preedit-start mac-input-method-preedit-end)
+	(progn
+	  (set-marker mac-input-method-preedit-start (point))
+	  (set-marker mac-input-method-preedit-end (point))
+	  (set-marker-insertion-type mac-input-method-preedit-start nil)
+	  (set-marker-insertion-type mac-input-method-preedit-end t))
+      (delete-region mac-input-method-preedit-start mac-input-method-preedit-end))
+
+    ;; insert character to current buffer 
+    (let ((l (string-to-list (decode-coding-string str coding-system))))
+      (while l
+	(insert (if (and (eq coding-system 'japanese-shift-jis)
+			 (eq (car l) ?\x80))
+		    ?\\
+		  (car l)))
+	(setq l (cdr l))))
+    
+    (if prop
+	(let ((p prop))
+	  (while p
+	    (mac-set-input-method-property str coding-system (car p))
+	    (setq p (cdr p))))
+      (set-marker mac-input-method-preedit-start nil nil)
+      (set-marker mac-input-method-preedit-end nil nil))))
+
+
+;;
+;; Functions of inline input method on isearch-mode
+;;
+(defun mac-input-method-isearch-mode (event)
+  "Function to change from isearch-mode to isearch-edit-string 
+for inline input method on MacOSX."
+  (interactive "e")
+  (let ((minibuffer-setup-hook nil))
+    (setq unread-command-events (cons event unread-command-events))
+    (isearch-edit-string)))
+
+(defun mac-input-method-isearch-edit-string (event)
+  "Function is isearch-edit-string for inline input method on MacOSX."
+  (interactive "e")
+  (mac-input-method-preedit event)
+  (if (equal mac-input-method-preedit-start mac-input-method-preedit-end)
+      (exit-minibuffer)))
+
+
+;;
+;; Variables and functions to change a input method 
+;;
+(defvar mac-input-method-parameters
+  '((0 (title . "")
+       (cursor-color)
+       (cursor-type)
+       (coding-system . mac-roman))            ;; smRoman
+    (1 (title . "$B$"(B")
+       (cursor-color)
+       (cursor-type)
+       (coding-system . japanese-shift-jis))   ;; smJapanese
+    (2 (title . "$BHK(B")
+       (cursor-color)
+       (cursor-type)
+       (coding-system . chinese-big5))         ;; smTradChinese
+    (3 (title . "$(C0!(B")
+       (cursor-color)
+       (cursor-type)
+       (coding-system . korean-iso-8bit))      ;; smKorean
+    (7 (title . "Cy")
+       (cursor-color)
+       (cursor-type)
+       (coding-system . mac-cyrillic))         ;; smCyrillic
+    (25 (title . "$B4J(B")
+	(cursor-color)
+	(cursor-type)
+	(coding-system . chinese-iso-8bit))    ;; smSimpChinese
+    (29 (title . "EU")
+	(cursor-color)
+	(cursor-type)
+	(coding-system . mac-centraleurroman)) ;; smCentralEuroRoman
+    )
+  "Alist of Mac script code vs parameters for input method on MacOSX.")
+
+(defun mac-key-script (lang)
+  "Funtion to convert a language name to a key script."
+  (cond ((symbolp lang)
+	 (cond ((eq lang 'roman) 0)
+	       ((eq lang 'japanese) 1)
+	       ((eq lang 'traditional-chinese) 2)
+	       ((eq lang 'korean) 3)
+	       ((eq lang 'cyrillic) 7)
+	       ((eq lang 'simple-chinese) 25)
+	       ((eq lang 'central-euro-roman) 25)
+	       (t -1)))
+	((numberp lang) lang)
+	(t -1)))
+
+(defun mac-set-input-method-parameter (language key value)
+  "Function to set a parameter of a input method."
+  (let* ((key-script (mac-key-script language))
+	 (lang-param (assq key-script mac-input-method-parameters))
+	 (param (assq key lang-param)))
+    (if lang-param
+	(if param
+	    (setcdr param value)
+	  (setcdr lang-param (cons (cons key value) (cdr lang-param))))
+      (setq mac-input-method-parameters
+	    (cons (list key-script (cons key value))
+		  mac-input-method-parameters)))))
+
+(defun mac-get-input-method-parameter (language key)
+  "Function to get a parameter of a input method."
+  (assq key (cdr (assq (mac-key-script language)
+		       mac-input-method-parameters))))
+
+(defun mac-input-method-update (lang)
+  "Funtion to update parameters of a input method."
+  (let ((title (cdr (mac-get-input-method-parameter lang 'title)))
+	(ctype (cdr (mac-get-input-method-parameter lang 'cursor-type)))
+	(ccolor (cdr (mac-get-input-method-parameter lang 'cursor-color))))
+    (setq current-input-method-title title)
+    (if ctype (setq cursor-type ctype))
+    (if ccolor (set-cursor-color ccolor))
+    (force-mode-line-update 'all)))
+
+(defun mac-toggle-input-method (&optional arg)
+  "Function to toggle input method on MacOSX."
+  (interactive)
+  (let ((lang (mac-get-key-script)))
+    (if arg
+	(progn
+	  (make-local-variable 'input-method-function)
+	  (make-local-variable 'cursor-type)
+
+	  (setq inactivate-current-input-method-function
+		'mac-toggle-input-method)
+	  (setq input-method-function nil)
+	  (setq describe-current-input-method-function nil)
+
+	  (if (= lang 0) (mac-set-key-script -1))
+	  (mac-input-method-update (mac-get-key-script)))
+      (kill-local-variable 'input-method-function)
+      (kill-local-variable 'cursor-type)
+      
+      (setq describe-current-input-method-function nil)
+
+      (if (not (= lang 0)) (mac-set-key-script -17))
+      (mac-input-method-update 0))))
+
+(defun mac-change-language-to-us ()
+  "Function to change language (Apple Key Script) to us."
+  (interactive)
+  (mac-toggle-input-method nil))
+
+(defun mac-handle-input-method-change (event)
+  "Function run when a input method change."
+  (interactive "e")
+  (mac-handle-language-change event)
+  
+  (let ((lang (car (cadr event))))
+
+    (if (or (and (not current-input-method) (> lang 0))
+            (and current-input-method (= lang 0)))
+        (if isearch-mode
+            (isearch-toggle-input-method)
+          (toggle-input-method)))
+
+    (mac-input-method-update lang)))
+
+
+;;
+;; Emacs input method for input method on MacOSX.
+;;
+(register-input-method "MacOSX" "MacOSX" 'mac-toggle-input-method
+		       "Mac" "Input Method on MacOSX System")
+
+
+;;
+;; Setup function of inline input method
+;;
+(defun mac-setup-inline-input-method ()
+  (interactive)
+  
+  (let* ((param (frame-parameters))
+	 (color (cdr (assq 'cursor-color param)))
+	 (type (cdr (assq 'cursor-type (frame-parameters)))))
+    (mac-set-input-method-parameter 0 'cursor-color color)
+    (mac-set-input-method-parameter 0 'cursor-type type)
+    (mac-set-inline-input-method t)
+    (setq default-input-method "MacOSX")))
+
+
+;;
+;; Entry Emacs event for inline input method on MacOSX.
+;;
+(define-key global-map
+  [mac-input-method] 'mac-input-method-preedit)
+(define-key isearch-mode-map
+  [mac-input-method] 'mac-input-method-isearch-mode)
+(define-key minibuffer-local-isearch-map
+  [mac-input-method] 'mac-input-method-isearch-edit-string)
+(define-key special-event-map
+  [mac-change-input-method] 'mac-handle-input-method-change)
+
+
+;;
+;; Functions to ignore system shortcut.
+;;
+(defun mac-add-ignore-shortcut (key)
+  (add-to-list 'mac-ignore-shortcuts
+	       (cond ((symbolp key)
+		      (cond ((eq key 'command) (cons #x100 nil))
+			    ((eq key 'option) (cons #x800 nil))
+			    ((eq key 'control) (cons #x1000 nil))
+			    (t (cons nil nil))))
+		     ((numberp key) (cons 0 key))
+		     ((listp key) 
+		      (let ((l key)
+			    (k nil)
+			    (m 0))
+			(while l
+			  (cond ((eq (car l) 'command) 
+				 (if (= (logand m #x100) 0)
+				     (setq m (logior m #x100))))
+				((eq (car l) 'option) 
+				 (if (= (logand m #x800) 0)
+				     (setq m (logior m #x800))))
+				((eq (car l) 'control)
+				 (if (= (logand m #x1000) 0)
+				     (setq m (logior m #x1000))))
+				((numberp (car l))
+				 (if (not k)
+				     (setq k (car l)))))
+			  (setq l (cdr l)))
+			(cons m k)))
+		     (t (cons nil nil)))))
diff -uNr --exclude='*~' --exclude='*.orig' emacs/lisp/version.el emacs22-carbon/lisp/version.el
--- emacs/lisp/version.el	2005-09-29 12:32:47.000000000 +0900
+++ emacs22-carbon/lisp/version.el	2005-12-31 09:40:23.000000000 +0900
@@ -27,7 +27,7 @@
 
 ;;; Code:
 
-(defconst emacs-version "22.0.50" "\
+(defconst emacs-version "22.0.50-carbon" "\
 Version numbers of this version of Emacs.")
 
 (defconst emacs-major-version
diff -uNr --exclude='*~' --exclude='*.orig' emacs/mangle-info emacs22-carbon/mangle-info
--- emacs/mangle-info	1970-01-01 09:00:00.000000000 +0900
+++ emacs22-carbon/mangle-info	2005-12-31 09:40:28.000000000 +0900
@@ -0,0 +1,17 @@
+#!/usr/bin/perl -w -i
+
+use English;
+use strict;
+
+$RS = undef;
+
+my $prefix = "@EMACS-FLAVOR@";
+
+my $x = <>;
+if(!($x =~ m/^(\s*START-INFO-DIR-ENTRY\s+\*\s*[^:]+:\s*)\(([^\)]+)\)/mo)) {
+  die "Couldn't find START-INFO-DIR-ENTRY.";
+}
+   
+print ${PREMATCH};
+print "$1($prefix/$2)";
+print ${POSTMATCH};
diff -uNr --exclude='*~' --exclude='*.orig' emacs/src/Makefile.in emacs22-carbon/src/Makefile.in
--- emacs/src/Makefile.in	2005-12-31 09:35:08.000000000 +0900
+++ emacs22-carbon/src/Makefile.in	2005-12-31 09:40:05.000000000 +0900
@@ -568,7 +568,7 @@
 
 #ifdef HAVE_CARBON
 mac = $(dot)$(dot)/mac/
-MAC_OBJ = mac.o macterm.o macfns.o macmenu.o macselect.o fontset.o fringe.o image.o
+MAC_OBJ = mac.o macterm.o macfns.o macmenu.o macselect.o fontset.o fringe.o image.o macim.o
 emacsapp = $(PWD)/$(mac)Emacs.app/
 emacsappsrc = ${srcdir}/../mac/Emacs.app/
 #endif
@@ -595,7 +595,7 @@
    in case they are needed there.  */
 SOME_MACHINE_OBJECTS = sunfns.o dosfns.o msdos.o \
   xterm.o xfns.o xmenu.o xselect.o xrdb.o xsmfns.o fringe.o image.o \
-  mac.o macterm.o macfns.o macmenu.o macselect.o fontset.o \
+  mac.o macterm.o macfns.o macmenu.o macselect.o macim.o fontset.o \
   w32.o w32bdf.o w32console.o w32fns.o w32heap.o w32inevt.o \
   w32menu.o w32proc.o w32reg.o w32select.o w32term.o w32xfns.o
 
@@ -1253,6 +1253,10 @@
   frame.h charset.h ccl.h dispextern.h fontset.h termhooks.h termopts.h \
   termchar.h gnu.h disptab.h buffer.h window.h keyboard.h $(INTERVAL_SRC) \
   process.h coding.h $(config_h)
+macim.o: blockinput.h atimer.h systime.h syssignal.h macterm.h macgui.h \
+  frame.h charset.h ccl.h dispextern.h fontset.h termhooks.h termopts.h \
+  termchar.h gnu.h disptab.h buffer.h window.h keyboard.h $(INTERVAL_SRC) \
+  process.h coding.h $(config_h)
 macselect.o: blockinput.h macterm.h macgui.h frame.h keymap.h $(config_h)
 
 ${emacsapp}Contents/Resources/English.lproj:
diff -uNr --exclude='*~' --exclude='*.orig' emacs/src/keyboard.c emacs22-carbon/src/keyboard.c
--- emacs/src/keyboard.c	2005-12-31 09:35:12.000000000 +0900
+++ emacs22-carbon/src/keyboard.c	2005-12-31 09:40:05.000000000 +0900
@@ -525,6 +525,10 @@
 #if defined (WINDOWSNT) || defined (MAC_OS)
 Lisp_Object Qlanguage_change;
 #endif
+#if defined (MAC_OSX)
+Lisp_Object Qmac_input_method;
+Lisp_Object Qmac_change_input_method;
+#endif
 Lisp_Object Qdrag_n_drop;
 Lisp_Object Qsave_session;
 #ifdef MAC_OS
@@ -4045,6 +4049,22 @@
 	  kbd_fetch_ptr = event + 1;
 	}
 #endif
+#if defined (MAC_OSX)
+      else if (event->kind == MAC_INPUT_METHOD_EVENT)
+	{
+	  /* Make an event (mac-input-method (STRING FIXED_LEN 
+	     (HILITE_TYPE1 START1 END1) (HILITE_TYPE2 START2 END2) ...). */
+	  obj = Fcons (Qmac_input_method, event->arg);
+	  kbd_fetch_ptr = event + 1;
+	}
+      else if (event->kind == MAC_CHANGE_INPUT_METHOD_EVENT)
+	{
+	  /* Make an event (mac-change-input-method (KEY_SCRIPT)).  */
+	  obj = Fcons (make_number (event->code), Qnil);
+	  obj = Fcons (Qmac_change_input_method, Fcons (obj, Qnil));
+	  kbd_fetch_ptr = event + 1;
+	}
+#endif
       else if (event->kind == SAVE_SESSION_EVENT)
         {
           obj = Fcons (Qsave_session, Qnil);
@@ -10884,6 +10904,12 @@
   Qlanguage_change = intern ("language-change");
   staticpro (&Qlanguage_change);
 #endif
+#if defined (MAC_OSX)
+  Qmac_input_method = intern ("mac-input-method");
+  staticpro (&Qmac_input_method);
+  Qmac_change_input_method = intern ("mac-change-input-method");
+  staticpro (&Qmac_change_input_method);
+#endif
   Qdrag_n_drop = intern ("drag-n-drop");
   staticpro (&Qdrag_n_drop);
 
diff -uNr --exclude='*~' --exclude='*.orig' emacs/src/macim.c emacs22-carbon/src/macim.c
--- emacs/src/macim.c	1970-01-01 09:00:00.000000000 +0900
+++ emacs22-carbon/src/macim.c	2005-12-31 09:40:05.000000000 +0900
@@ -0,0 +1,427 @@
+/* Implementation of Inline Input Method for MacOS X.
+   Copyright (C) 2004, 2005 Taiichi Hashimoto <taiichi2@mac.com>.
+
+This file is not 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 2, 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; see the file COPYING.  If not, write to
+the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
+
+/* Written by Taiichi Hashimoto (taiichi2@mac.com).  */
+
+
+#include <config.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include "lisp.h"
+#include "charset.h"
+#include "blockinput.h"
+
+#include "macterm.h"
+
+#ifndef MAC_OSX
+#include <alloca.h>
+#endif
+
+#ifdef MAC_OSX
+#undef mktime
+#undef DEBUG
+#undef free
+#undef malloc
+#undef realloc
+/* Macros max and min defined in lisp.h conflict with those in
+   precompiled header Carbon.h.  */
+#undef max
+#undef min
+#undef init_process
+#include <Carbon/Carbon.h>
+#undef free
+#define free unexec_free
+#undef malloc
+#define malloc unexec_malloc
+#undef realloc
+#define realloc unexec_realloc
+#undef min
+#define min(a, b) ((a) < (b) ? (a) : (b))
+#undef max
+#define max(a, b) ((a) > (b) ? (a) : (b))
+#undef init_process
+#define init_process emacs_init_process
+/* USE_CARBON_EVENTS determines if the Carbon Event Manager is used to
+   obtain events from the event queue.  If set to 0, WaitNextEvent is
+   used instead.  */
+#define USE_CARBON_EVENTS 1
+#else /* not MAC_OSX */
+#include <Quickdraw.h>
+#include <ToolUtils.h>
+#include <Sound.h>
+#include <Events.h>
+#include <Script.h>
+#include <Resources.h>
+#include <Fonts.h>
+#include <TextUtils.h>
+#include <LowMem.h>
+#include <Controls.h>
+#if defined (__MRC__) || (__MSL__ >= 0x6000)
+#include <ControlDefinitions.h>
+#endif
+#include <Gestalt.h>
+
+#if __profile__
+#include <profiler.h>
+#endif
+#endif /* not MAC_OSX */
+
+#include "systty.h"
+#include "systime.h"
+#include "atimer.h"
+#include "keymap.h"
+
+#include <ctype.h>
+#include <errno.h>
+#include <setjmp.h>
+#include <sys/stat.h>
+
+#include "keyboard.h"
+#include "frame.h"
+#include "dispextern.h"
+#include "fontset.h"
+#include "termhooks.h"
+#include "termopts.h"
+#include "termchar.h"
+#include "gnu.h"
+#include "disptab.h"
+#include "buffer.h"
+#include "window.h"
+#include "intervals.h"
+#include "composite.h"
+#include "coding.h"
+
+#ifdef USE_CARBON_EVENTS && defined (MAC_OSX)
+
+extern Lisp_Object Qcurrent_input_method;
+Lisp_Object Vmac_use_inline_input_method;
+Lisp_Object Vmac_ignore_shortcuts;
+
+static EventTypeSpec events[] = {
+  { kEventClassTextInput, kEventTextInputOffsetToPos },
+  { kEventClassTextInput, kEventTextInputUpdateActiveInputArea },
+  { kEventClassTextInput, kEventTextInputUnicodeForKeyEvent },
+  { kEventClassTextInput, kEventTextInputPosToOffset },
+  { kEventClassTextInput, kEventTextInputGetSelectedText }
+};
+
+static EventHandlerRef mac_input_method_handler = NULL;
+static EventHandlerUPP mac_input_method_handlerUPP = NULL;
+static TSMDocumentID mac_tsm_doc_id;
+static int converting = FALSE;
+
+pascal OSStatus mac_handle_input_method_event (EventHandlerCallRef, EventRef, void *);
+void init_input_method (void);
+
+
+DEFUN ("mac-set-key-script", Fmac_set_key_script,
+       Smac_set_key_script, 1, 1, 0,
+       doc: /* change languge environment of MacOSX */)
+     (code)
+     Lisp_Object code;
+{
+  BLOCK_INPUT;
+  KeyScript (XINT (code));
+  UNBLOCK_INPUT;
+
+  return Qnil;
+}
+
+DEFUN ("mac-get-key-script", Fmac_get_key_script,
+       Smac_get_key_script, 0, 0, 0,
+       doc: /* get current languge environment of MacOSX */)
+     ()
+{
+  SInt16 current_key_script;
+
+  BLOCK_INPUT;
+  current_key_script = GetScriptManagerVariable (smKeyScript);
+  UNBLOCK_INPUT;
+
+  return make_number (current_key_script);
+}
+
+
+DEFUN ("mac-set-inline-input-method", Fmac_set_inline_input_method,
+       Smac_set_inline_input_method, 1, 1, 0,
+       doc: /* set inline input method */)
+     (on_p)
+     Lisp_Object on_p;
+{
+
+  BLOCK_INPUT;
+  if (NILP (on_p))
+    {
+      DeactivateTSMDocument (mac_tsm_doc_id);
+      Vmac_use_inline_input_method = Qnil;
+    }
+  else
+    {
+      ActivateTSMDocument (mac_tsm_doc_id);
+      Vmac_use_inline_input_method = Qt;
+    }
+  UNBLOCK_INPUT;
+
+  return on_p;
+}
+
+void
+mac_reset_tsm_document ()
+{
+  BLOCK_INPUT;
+  if (!NILP (Vmac_use_inline_input_method))
+    {
+      DeactivateTSMDocument (mac_tsm_doc_id);
+      ActivateTSMDocument (mac_tsm_doc_id);
+    }
+  UNBLOCK_INPUT;
+}
+
+void 
+mac_store_change_input_method_event (unsigned long timestamp)
+{
+  Lisp_Object input_method;
+  static SInt16 last_key_script = -1;
+  SInt16 current_key_script;
+
+  BLOCK_INPUT;
+
+  if (!NILP (Vmac_use_inline_input_method))
+    {
+      current_key_script = GetScriptManagerVariable (smKeyScript);
+
+      input_method = Fsymbol_value (Qcurrent_input_method);
+
+      if (last_key_script != current_key_script
+	  || ((current_key_script && NILP (input_method))
+	      || (!current_key_script && !NILP (input_method))))
+	{
+	  struct input_event event;
+      
+	  EVENT_INIT (event);
+	  event.kind = MAC_CHANGE_INPUT_METHOD_EVENT;
+	  event.arg = Qnil;
+	  event.code = current_key_script;
+	  event.timestamp = timestamp;
+	  kbd_buffer_store_event (&event);
+	}
+
+      last_key_script = current_key_script;
+    }
+
+  UNBLOCK_INPUT;
+}
+
+
+int
+mac_pass_key_to_system (int xkeysym, UInt32 modifiers)
+{
+  if (this_command_key_count
+      || !NILP (current_buffer->read_only)
+      || cursor_in_echo_area)
+    return FALSE;
+  else if (!NILP (Vmac_use_inline_input_method)
+	   && !converting)
+    {
+      
+      Lisp_Object keys = Vmac_ignore_shortcuts;
+      Lisp_Object m, k;
+
+      while (!NILP (keys))
+	{
+	  m = XCAR (XCAR (keys));
+	  k = XCDR (XCAR (keys));
+	  keys = XCDR (keys);
+	  
+	  if (NUMBERP (m) && modifiers == XINT (m))
+	    if (NILP (k)
+		|| (NUMBERP (k) && xkeysym == XINT (k)))
+	      return FALSE;
+	}
+      return TRUE;
+    }
+  else
+    return TRUE;
+}
+
+pascal OSStatus
+mac_handle_input_method_event(EventHandlerCallRef er, EventRef event, void *data)
+{
+  UInt32 ekind = GetEventKind (event), eclass = GetEventClass (event);
+  OSStatus ret = noErr;
+  unsigned long timestamp = GetEventTime (event) / kEventDurationMillisecond;
+
+  BLOCK_INPUT;
+  switch (eclass) {
+  case kEventClassTextInput:
+    switch (ekind) {
+    case kEventTextInputOffsetToPos:
+      {
+	struct frame *f = one_mac_display_info.x_focus_frame;
+	struct window *w = XWINDOW (f->selected_window);
+	Point pt;
+
+	/* set position of candidates window */
+	pt.h = WINDOW_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
+	pt.v = WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y) + w->phys_cursor_height;
+	LocalToGlobal (&pt);
+
+	SetEventParameter (event, kEventParamTextInputReplyPoint,
+			   typeQDPoint, sizeof(pt), &pt);
+      }
+      break;
+    case kEventTextInputPosToOffset:
+      break;
+    case kEventTextInputUpdateActiveInputArea:
+      {
+	UInt32 src_nbytes = 0, array_nbytes = 0, fixed_nbytes = 0;
+	unsigned char *src = NULL;
+	TextRangeArray *array = NULL;
+	Lisp_Object argv[1024], triple[3];
+	int argc = 0, i = 0;
+
+	GetEventParameter (event, kEventParamTextInputSendText,
+			   typeChar,
+			   NULL, 0,
+			   &src_nbytes, NULL);
+	
+	if (src_nbytes) 
+	  {
+	    /* get converting string from TSM */
+	    src = (unsigned char *) xmalloc (src_nbytes + 1);
+	    memset (src, 0, src_nbytes + 1);
+	    GetEventParameter (event, kEventParamTextInputSendText,
+			       typeChar,
+			       NULL, src_nbytes,
+			       NULL, src);
+	    argv[argc++] = make_string_from_bytes (src, src_nbytes, src_nbytes);
+	    
+	    /* get fixed string length (bytes) from TSM */
+	    GetEventParameter (event, kEventParamTextInputSendFixLen,
+			       typeLongInteger, 
+			       NULL, sizeof(fixed_nbytes), 
+			       NULL, &fixed_nbytes);
+	    argv[argc++] = make_number (fixed_nbytes);
+
+	    /* get properties of string from TSM */
+	    GetEventParameter (event, kEventParamTextInputSendHiliteRng,
+			       typeTextRangeArray,
+			       NULL, 0,
+			       &array_nbytes, NULL);
+	    
+	    if (array_nbytes)
+	      {
+		array = (TextRangeArray *) xmalloc (array_nbytes);
+		
+		GetEventParameter (event, kEventParamTextInputSendHiliteRng,
+				   typeTextRangeArray,
+				   NULL, array_nbytes,
+				   NULL, array);
+
+		for (i = 0; i < array->fNumOfRanges && argc < 1024; i++)
+		  {
+		    triple[0] = make_number (array->fRange[i].fHiliteStyle);
+		    triple[1] = make_number (array->fRange[i].fStart);
+		    triple[2] = make_number (array->fRange[i].fEnd);
+		    argv[argc++] = list3 (make_number (array->fRange[i].fHiliteStyle),
+					  make_number (array->fRange[i].fStart),
+					  make_number (array->fRange[i].fEnd));
+		  }
+	      }
+	    if (src) xfree(src);
+	    if (array) xfree(array);
+
+	    if (fixed_nbytes) converting = FALSE;
+	    else converting = TRUE;
+	  }
+	else
+	  { /* src_nbytes == 0 */
+	    argv[argc++] = make_string("", 0);
+	    argv[argc++] = make_number(0);
+
+	    converting = FALSE;
+	  }
+
+	{
+	  struct input_event inev;
+	  
+	  EVENT_INIT (inev);
+	  inev.kind = MAC_INPUT_METHOD_EVENT;
+	  inev.arg = Flist (argc, argv);
+	  inev.timestamp = timestamp;
+	  kbd_buffer_store_event (&inev);
+	}
+      }
+      break;
+    case kEventTextInputGetSelectedText:
+      break;
+    case kEventTextInputUnicodeForKeyEvent:
+      if (!converting) ret = eventNotHandledErr;
+      break;
+    default:
+      break;
+    }
+    break;
+  default:
+    break;
+  }
+  UNBLOCK_INPUT;
+
+  return ret;
+}
+
+void init_input_method (void)
+{
+  if(!mac_input_method_handler) {
+    OSErr err;
+    InterfaceTypeList itl = { kTextService };
+
+    BLOCK_INPUT;
+    err = NewTSMDocument (1, itl, &mac_tsm_doc_id, 715);
+    if (err != noErr) abort (); 
+
+    err = InstallEventHandler(GetApplicationEventTarget (),
+			      NewEventHandlerUPP (mac_handle_input_method_event),
+			      GetEventTypeCount (events),
+			      events,
+			      NULL,
+			      &mac_input_method_handler);
+    if (err != noErr) abort ();
+    UNBLOCK_INPUT;
+  }
+
+  DEFVAR_LISP ("mac-use-inline-input-method", &Vmac_use_inline_input_method,
+   doc: /* If non-nil, users use inline input method on MacOSX. */);
+  Vmac_use_inline_input_method = Qnil;
+  //Fmac_set_inline_input_method (Vmac_use_inline_input_method);
+
+  DEFVAR_LISP ("mac-ignore-shortcuts", &Vmac_ignore_shortcuts,
+   doc: /* The list of pairs of a xkeysym and modifiers 
+	   which don't pass to system. */);
+  Vmac_ignore_shortcuts = Qnil;
+
+  defsubr (&Smac_set_inline_input_method);
+  defsubr (&Smac_set_key_script);
+  defsubr (&Smac_get_key_script);
+}
+
+#endif
+
diff -uNr --exclude='*~' --exclude='*.orig' emacs/src/macterm.c emacs22-carbon/src/macterm.c
--- emacs/src/macterm.c	2005-12-31 09:35:13.000000000 +0900
+++ emacs22-carbon/src/macterm.c	2005-12-31 09:40:05.000000000 +0900
@@ -10045,6 +10045,7 @@
 	    int keycode = (er.message & keyCodeMask) >> 8;
 	    int xkeysym;
 
+#if 0
 #if USE_CARBON_EVENTS && defined (MAC_OSX)
 	    /* When using Carbon Events, we need to pass raw keyboard
 	       events to the TSM ourselves.  If TSM handles it, it
@@ -10061,6 +10062,7 @@
 		  != eventNotHandledErr)
 		break;
 #endif
+#endif /* 0 */
 
 #if 0
 	    if (dpyinfo->x_focus_frame == NULL)
@@ -10127,6 +10129,28 @@
 						   er.message & charCodeMask);
 		  inev.kind = ASCII_KEYSTROKE_EVENT;
 		}
+
+#if USE_CARBON_EVENTS && defined (MAC_OSX)
+	    /* When using Carbon Events, we need to pass raw keyboard
+	       events to the TSM ourselves.  If TSM handles it, it
+	       will pass back noErr, otherwise it will pass back
+	       "eventNotHandledErr" and we can process it
+	       normally.  */
+	    if ((!NILP (mac_pass_command_to_system)
+		 || !(er.modifiers & cmdKey))
+		&& (!NILP (mac_pass_control_to_system)
+		    || !(er.modifiers & controlKey))
+		&& (NILP (Vmac_option_modifier)
+		    || !(er.modifiers & optionKey))
+		&& mac_pass_key_to_system (inev.code,
+					   er.modifiers & (~shiftKey)))
+	      if (SendEventToEventTarget (eventRef, toolbox_dispatcher)
+		  != eventNotHandledErr)
+		{
+		  inev.kind = NO_EVENT;
+		  break;
+		}
+#endif
 	  }
 
 #if USE_CARBON_EVENTS
@@ -10184,6 +10208,7 @@
 	  count++;
 	}
 
+      mac_store_change_input_method_event (timestamp);
     }
 
   /* If the focus was just given to an autoraising frame,
@@ -10699,6 +10724,8 @@
   init_service_handler ();
 
   init_quit_char_handler ();
+
+  init_input_method();
 #endif	/* MAC_OSX */
 
   init_command_handler ();
diff -uNr --exclude='*~' --exclude='*.orig' emacs/src/termhooks.h emacs22-carbon/src/termhooks.h
--- emacs/src/termhooks.h	2005-12-31 09:35:14.000000000 +0900
+++ emacs22-carbon/src/termhooks.h	2005-12-31 09:40:05.000000000 +0900
@@ -260,6 +260,12 @@
 				   language is changed by the
 				   user.  */
 #endif
+#if defined (MAC_OSX)
+  MAC_INPUT_METHOD_EVENT,	/* A MAC_INPUT_METHOD_EVENT is generated 
+				   for inline input method using TSM. */
+  MAC_CHANGE_INPUT_METHOD_EVENT,/* A MAC_CHANGE_INPUT_METHOD_EVENT is generated
+				   for changing input method on MacOSX. */  
+#endif
   SCROLL_BAR_CLICK_EVENT,	/* .code gives the number of the mouse button
 				   that was clicked.
 				   .modifiers holds the state of the modifier

