[migemo] Emacs 22 の isearch-yank-* への対応

M.Fujii boochang @ m4.kcn.ne.jp
2009年 1月 10日 (土) 21:37:37 JST


藤井です。

Emacs 22 上で migemo.el を使っていると、いくつか動作に問題があります。
パッチを作成しましたので、ご検討ください。

対応した問題は以下のとおりです。

  1. isearch 以外の lazy-highlight がおかしい。
  2. isearch-yank-char, isearch-yank-char-or-word が追加されたが、
     これらは migemo で対応できない。

あと、Emacs21 でも存在するかもしれませんが以下のような問題もあります。

  3. isearch-yank-kill, isearch-yank-x-selection に migemo が対応していない。

1. については下記のとおり、一昨年パッチを作成しています。今までのところ
特に問題なく動いています。

From: "M.Fujii" <boochang @ m4.kcn.ne.jp>
Subject: [migemo] Re: migemo 利用時の isearch 以外の lazy-highlight の挙動
Date: Fri, 22 Jun 2007 14:08:54 +0900 (JST)
> Emacs 22.1 がリリースされたようなので対応を考えてみました。
> 
> 私としては、Emacs 22 以降では以下のようにするのが良いと思います。
> 
>   1. search-forward() 等の挙動を変えるのを止めて
>      isearch-search-fun-function を設定することにする。
(中略)
> また、上記のように振舞いを変えるパッチを作成してみました。添付しますの
> で、試してみてください。

2. 3. については、これまでのように migemo-isearch-yank-word などを追加
して対応するのではなく、isearch-yank-* が共通して利用する
isearch-yank-string に advice することで対応するのが良いと思います。

こうすれば、メンテナンス性があがるのではないかと思います。
isearch-yank-* が追加されても、新たに migemo-isearch-yank-* を実装する
必要もなくなりますし、その実装した関数のキーアサン設定をする必要もなく
なりますので。

上記 3 点に対応するためパッチを作成しました。ご検討ください。

パッチは以前お送りした lazy-highlight の変更を含む他、
Emacs 22 上では以下のように変わります。Emacs 21 以前では変更ありません。

  - isearch-yank-string への advice を追加
  - migemo-isearch-yank-(char|word|line) の削除
  - \M-m 以外のキーアサインの削除

なお、パッチを当てると Emacs 22 では \C-d (migemo-isearch-yank-char)が
効かなくなります。対応としては、Emacs22 の isearch で
isearch-yank-char に割り当てられている \M-\C-y を使用するか、.emacs で
isearch-yank-char を \C-d に割り当てることになります。

以上、よろしくお願いします。
--
藤井 正行 / Masayuki FUJII
-------------- next part --------------
Index: migemo.el.in
===================================================================
RCS file: /cvsroot/migemo/migemo/migemo.el.in,v
retrieving revision 1.8
diff -u -r1.8 migemo.el.in
--- migemo.el.in	22 Sep 2006 08:43:33 -0000	1.8
+++ migemo.el.in	10 Jan 2009 11:31:41 -0000
@@ -155,6 +155,7 @@
 (defvar migemo-pattern-alist nil)
 (defvar migemo-frequent-pattern-alist nil)
 (defconst migemo-emacs21p (and (> emacs-major-version 20) (not (featurep 'xemacs))))
+(defconst migemo-emacs22p (and (> emacs-major-version 21) (not (featurep 'xemacs))))
 (defvar migemo-search-pattern-alist nil)
 (defvar migemo-do-isearch nil)
 
@@ -211,7 +212,9 @@
 	(setq migemo-process (migemo-start-process 
 			      "migemo" migemo-buffer migemo-command options))
 	(process-kill-without-query migemo-process)
-	t)))
+	t))
+  (when migemo-emacs22p
+    (setq isearch-search-fun-function 'migemo-isearch-search-function)))
 
 (defun migemo-replace-in-string (string from to)
   (with-temp-buffer
@@ -379,6 +382,19 @@
       (delete-region (point) pos)
       (insert jrpat))))
 
+(defun migemo-isearch-search-function ()
+  (if (and migemo-isearch-enable-p
+	   (not (or isearch-regexp isearch-word)))
+      (lambda (string &optional bound noerror count)
+	(funcall
+	 (if isearch-forward 're-search-forward 're-search-backward)
+	 (if (delq 'ascii (find-charset-string string))
+	     string
+	   (migemo-search-pattern-get string))
+	 bound noerror count))
+    (let ((isearch-search-fun-function nil))
+      (isearch-search-fun))))
+
 (defun migemo-forward (word &optional bound noerror count)
   (interactive "sSearch: \nP\nP")
   (if (delq 'ascii (find-charset-string word))
@@ -511,28 +527,29 @@
       ad-do-it
     (setq migemo-do-isearch nil)))
 
-(defadvice isearch-search-and-update (around migemo-search-ad activate)
-  "adviced by migemo."
-  (let ((isearch-adjusted isearch-adjusted))
-    (when (and migemo-isearch-enable-p
-	       (not isearch-forward) (not isearch-regexp) (not isearch-word))
-      ;; don't use 'looking-at'
-      (setq isearch-adjusted t))
-    ad-do-it))
+(unless migemo-emacs22p
+  (defadvice isearch-search-and-update (around migemo-search-ad activate)
+    "adviced by migemo."
+    (let ((isearch-adjusted isearch-adjusted))
+      (when (and migemo-isearch-enable-p
+		 (not isearch-forward) (not isearch-regexp) (not isearch-word))
+	;; don't use 'looking-at'
+	(setq isearch-adjusted t))
+      ad-do-it))
 
-(defadvice search-forward (around migemo-search-ad activate)
-  "adviced by migemo."
-  (if migemo-do-isearch
-      (setq ad-return-value
-	    (migemo-forward (ad-get-arg 0) (ad-get-arg 1) (ad-get-arg 2) (ad-get-arg 3)))
-    ad-do-it))
+  (defadvice search-forward (around migemo-search-ad activate)
+    "adviced by migemo."
+    (if migemo-do-isearch
+	(setq ad-return-value
+	      (migemo-forward (ad-get-arg 0) (ad-get-arg 1) (ad-get-arg 2) (ad-get-arg 3)))
+      ad-do-it))
 
-(defadvice search-backward (around migemo-search-ad activate)
-  "adviced by migemo."
-  (if migemo-do-isearch
-      (setq ad-return-value
-	    (migemo-backward (ad-get-arg 0) (ad-get-arg 1) (ad-get-arg 2) (ad-get-arg 3)))
-    ad-do-it))
+  (defadvice search-backward (around migemo-search-ad activate)
+    "adviced by migemo."
+    (if migemo-do-isearch
+	(setq ad-return-value
+	      (migemo-backward (ad-get-arg 0) (ad-get-arg 1) (ad-get-arg 2) (ad-get-arg 3)))
+      ad-do-it)))
 
 ;; Turn off input-method automatically when C-s or C-r are typed.
 (defadvice isearch-mode (before migemo-search-ad activate)
@@ -572,6 +589,9 @@
     (when migemo-emacs21p
       (put-text-property 0 (length str) 'face migemo-message-prefix-face str))
     (when (and migemo-isearch-enable-p
+	       (or (not migemo-emacs22p)
+		   (eq isearch-search-fun-function
+		       'migemo-isearch-search-function))
 	       (not (or isearch-regexp isearch-word)))
       (setq ad-return-value (concat str " " ret)))))
 
@@ -609,7 +629,8 @@
                  (window-start)))
              t)))
 
-(when (fboundp 'isearch-lazy-highlight-search)
+(when (and (not migemo-emacs22p)
+	   (fboundp 'isearch-lazy-highlight-search))
   (defalias 'isearch-lazy-highlight-search 'migemo-isearch-lazy-highlight-search))
 
 ;;;; for isearch-highlightify-region (XEmacs 21)
@@ -628,9 +649,10 @@
 	   (fboundp 'isearch-yank-line))
   (add-hook 'isearch-mode-hook
 	    (lambda ()
-	      (define-key isearch-mode-map "\C-d" 'migemo-isearch-yank-char)
-	      (define-key isearch-mode-map "\C-w" 'migemo-isearch-yank-word)
-	      (define-key isearch-mode-map "\C-y" 'migemo-isearch-yank-line)
+	      (unless migemo-emacs22p
+		(define-key isearch-mode-map "\C-d" 'migemo-isearch-yank-char)
+		(define-key isearch-mode-map "\C-w" 'migemo-isearch-yank-word)
+		(define-key isearch-mode-map "\C-y" 'migemo-isearch-yank-line))
 	      (define-key isearch-mode-map "\M-m" 'migemo-isearch-toggle-migemo)))
   
   (defun migemo-isearch-toggle-migemo ()
@@ -647,56 +669,70 @@
 	   (isearch-lazy-highlight-new-loop nil nil)))))
     (isearch-message))
 
-  (defun migemo-isearch-yank-char ()
-    "Pull next character from buffer into search string with migemo."
-    (interactive)
-    (when (and migemo-isearch-enable-p
-               (not isearch-regexp) isearch-other-end)
-      (setq isearch-string (buffer-substring-no-properties
-                            isearch-other-end (point)))
-      (setq isearch-message isearch-string))
-    (let ((search-upper-case (unless migemo-isearch-enable-p
-			       search-upper-case)))
-      (isearch-yank-string
-       (save-excursion
-	 (and (not isearch-forward) isearch-other-end
-	      (goto-char isearch-other-end))
-	 (buffer-substring-no-properties (point) 
-					 (progn (forward-char 1) (point)))))))
-
-  (defun migemo-isearch-yank-word ()
-    "Pull next character from buffer into search string with migemo."
-    (interactive)
-    (when (and migemo-isearch-enable-p
-               (not isearch-regexp) isearch-other-end)
-      (setq isearch-string (buffer-substring-no-properties
-                            isearch-other-end (point)))
-      (setq isearch-message isearch-string))
-    (let ((search-upper-case (unless migemo-isearch-enable-p
-			       search-upper-case)))
-      (isearch-yank-string
-       (save-excursion
-	 (and (not isearch-forward) isearch-other-end
-	      (goto-char isearch-other-end))
-	 (buffer-substring-no-properties (point) 
-					 (progn (forward-word 1) (point)))))))
-
-  (defun migemo-isearch-yank-line ()
-    "Pull next character from buffer into search string with migemo."
-    (interactive)
-    (when (and migemo-isearch-enable-p
-               (not isearch-regexp) isearch-other-end)
-      (setq isearch-string (buffer-substring-no-properties
-                            isearch-other-end (point)))
-      (setq isearch-message isearch-string))
-    (let ((search-upper-case (unless migemo-isearch-enable-p
-			       search-upper-case)))
-      (isearch-yank-string
-       (save-excursion
-	 (and (not isearch-forward) isearch-other-end
-	      (goto-char isearch-other-end))
-	 (buffer-substring-no-properties (point)
-					 (line-end-position))))))
+  (cond
+   (migemo-emacs22p
+    (defadvice isearch-yank-string (around migemo-search-ad activate)
+      "adviced by migemo."
+      (when (and migemo-isearch-enable-p
+		 (not isearch-regexp) isearch-other-end)
+	(setq isearch-string (buffer-substring-no-properties
+			      isearch-other-end (point)))
+	(setq isearch-message isearch-string))
+      (let ((search-upper-case (unless migemo-isearch-enable-p
+				 search-upper-case)))
+	ad-do-it)))
+
+   (migemo-emacs21p
+    (defun migemo-isearch-yank-char ()
+      "Pull next character from buffer into search string with migemo."
+      (interactive)
+      (when (and migemo-isearch-enable-p
+		 (not isearch-regexp) isearch-other-end)
+	(setq isearch-string (buffer-substring-no-properties
+			      isearch-other-end (point)))
+	(setq isearch-message isearch-string))
+      (let ((search-upper-case (unless migemo-isearch-enable-p
+				 search-upper-case)))
+	(isearch-yank-string
+	 (save-excursion
+	   (and (not isearch-forward) isearch-other-end
+		(goto-char isearch-other-end))
+	   (buffer-substring-no-properties (point) 
+					   (progn (forward-char 1) (point)))))))
+
+    (defun migemo-isearch-yank-word ()
+      "Pull next character from buffer into search string with migemo."
+      (interactive)
+      (when (and migemo-isearch-enable-p
+		 (not isearch-regexp) isearch-other-end)
+	(setq isearch-string (buffer-substring-no-properties
+			      isearch-other-end (point)))
+	(setq isearch-message isearch-string))
+      (let ((search-upper-case (unless migemo-isearch-enable-p
+				 search-upper-case)))
+	(isearch-yank-string
+	 (save-excursion
+	   (and (not isearch-forward) isearch-other-end
+		(goto-char isearch-other-end))
+	   (buffer-substring-no-properties (point) 
+					   (progn (forward-word 1) (point)))))))
+
+    (defun migemo-isearch-yank-line ()
+      "Pull next character from buffer into search string with migemo."
+      (interactive)
+      (when (and migemo-isearch-enable-p
+		 (not isearch-regexp) isearch-other-end)
+	(setq isearch-string (buffer-substring-no-properties
+			      isearch-other-end (point)))
+	(setq isearch-message isearch-string))
+      (let ((search-upper-case (unless migemo-isearch-enable-p
+				 search-upper-case)))
+	(isearch-yank-string
+	 (save-excursion
+	   (and (not isearch-forward) isearch-other-end
+		(goto-char isearch-other-end))
+	   (buffer-substring-no-properties (point)
+					   (line-end-position))))))))
 )
 
 (add-hook 'kill-emacs-hook 'migemo-pattern-alist-save)


Migemo メーリングリストの案内