* contrib/anything-grep.el: C-o, right: agrep-next-source-or-detail
[anything-config.git] / contrib / anything-migemo.el
blob39775c0f7e84a472059523f0382f9bf04fe2655d
1 ;;; anything-migemo.el --- Migemo plug-in for anything
2 ;; $Id: anything-migemo.el,v 1.18 2009-06-07 17:52:22 rubikitch Exp $
4 ;; Copyright (C) 2007, 2008, 2009 rubikitch
6 ;; Author: rubikitch <rubikitch@ruby-lang.org>
7 ;; Keywords: anything, convenience, tools, i18n, japanese
8 ;; URL: http://www.emacswiki.org/cgi-bin/wiki/download/anything-migemo.el
10 ;; This file is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 3, or (at your option)
13 ;; any later version.
15 ;; This file is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
25 ;;; Commentary:
27 ;; Migemo extension of `anything'. Use `anything-migemo' instead of
28 ;; `anything'. If `anything-migemo' is invoked with prefix argument,
29 ;; `anything' is migemo-ized. This means that pattern matching of
30 ;; `anything' candidates is done by migemo-expanded `anything-pattern'.
32 ;;; Commands:
34 ;; Below are complete command list:
36 ;; `anything-migemo'
37 ;; `anything' with migemo extension.
39 ;;; Customizable Options:
41 ;; Below are customizable option list:
44 ;; If you want to use migemo search source-locally, add (migemo) to
45 ;; the source. It sets match and search attribute appropriately for
46 ;; migemo.
48 ;;; Setting:
50 ;; (require 'anything-config)
51 ;; (require 'anything-migemo)
52 ;; (define-key global-map [(control ?:)] 'anything-migemo)
54 ;;; Bug:
56 ;; Simultaneous use of (candidates-in-buffer), (search
57 ;; . migemo-forward) and (delayed) scrambles *anything* buffer. Maybe
58 ;; because of collision of `migemo-process' and `run-with-idle-timer'
60 ;;; History:
62 ;; $Log: anything-migemo.el,v $
63 ;; Revision 1.18 2009-06-07 17:52:22 rubikitch
64 ;; New macro `anything-migemize-command'.
66 ;; Revision 1.17 2009/06/04 20:32:00 rubikitch
67 ;; migemo is soft-required now; this file has no effect unless migemo is installed.
69 ;; Revision 1.16 2008/10/03 20:43:18 rubikitch
70 ;; Use with anything-match-plugin.el
72 ;; Revision 1.15 2008/10/03 20:01:46 rubikitch
73 ;; refactoring
75 ;; Revision 1.14 2008/08/25 08:29:02 rubikitch
76 ;; `anything-migemo': anything-args
78 ;; Revision 1.13 2008/08/24 20:39:53 rubikitch
79 ;; prevent the unit test from being byte-compiled.
81 ;; Revision 1.12 2008/08/24 18:01:25 rubikitch
82 ;; *** empty log message ***
84 ;; Revision 1.11 2008/08/24 08:23:30 rubikitch
85 ;; Rename `anything-candidates-buffer' -> `anything-candidate-buffer'
87 ;; Revision 1.10 2008/08/24 01:54:21 rubikitch
88 ;; migemo attribute
90 ;; Revision 1.9 2008/08/19 21:38:09 rubikitch
91 ;; match attribute bug fix
93 ;; Revision 1.8 2008/08/19 21:30:29 rubikitch
94 ;; plug-in
96 ;; Revision 1.7 2008/08/10 22:45:02 rubikitch
97 ;; Bug info
99 ;; Revision 1.6 2008/08/08 03:40:51 rubikitch
100 ;; require migemo
102 ;; Revision 1.5 2008/08/08 03:38:34 rubikitch
103 ;; add search attribute
104 ;; unit tests
106 ;; Revision 1.4 2007/12/26 08:36:01 rubikitch
107 ;; changed match priority
109 ;; Revision 1.3 2007/12/25 19:55:59 rubikitch
110 ;; patch is not needed anymore.
112 ;; Revision 1.2 2007/12/25 13:05:46 rubikitch
113 ;; speed up by memoization
115 ;; Revision 1.1 2007/12/25 12:03:25 rubikitch
116 ;; Initial revision
119 ;;; Code:
121 (eval-when-compile (require 'anything))
122 (require 'migemo nil t)
123 (require 'anything-match-plugin)
124 (defvar anything-use-migemo nil
125 "[Internal] If non-nil, `anything' is migemo-ized.")
126 (defun anything-migemo (with-migemo &rest anything-args)
127 "`anything' with migemo extension.
128 With prefix arugument, `anything-pattern' is migemo-ized, otherwise normal `anything'."
129 (interactive "P")
130 (let ((anything-use-migemo with-migemo))
131 (apply 'anything anything-args)))
133 (defvar anything-previous-migemo-info '("" . "")
134 "[Internal] Previous migemo query for anything-migemo.")
135 (defun* anything-string-match-with-migemo (str &optional (pattern anything-pattern))
136 "Migemo version of `string-match'."
137 (unless (string= pattern (car anything-previous-migemo-info))
138 (setq anything-previous-migemo-info (cons pattern (migemo-get-pattern pattern))))
139 (string-match (cdr anything-previous-migemo-info) str))
141 (defun* anything-mp-3migemo-match (str &optional (pattern anything-pattern))
142 (loop for (pred . re) in (anything-mp-3-get-patterns pattern)
143 always (funcall pred (anything-string-match-with-migemo str re))))
144 (defun anything-mp-3migemo-search (pattern &rest ignore)
145 (anything-mp-3-search-base pattern 'migemo-forward 'migemo-forward))
146 (defun anything-mp-3migemo-search-backward (pattern &rest ignore)
147 (anything-mp-3-search-base pattern 'migemo-backward 'migemo-backward))
148 ;; (anything-string-match-with-migemo "日本語入力" "nihongo")
149 ;; (anything-string-match-with-migemo "日本語入力" "nyuuryoku")
150 ;; (anything-mp-3migemo-match "日本語入力" "nihongo nyuuryoku")
151 (defun anything-compile-source--migemo (source)
152 (if (not (featurep 'migemo))
153 source
154 (let* ((match-identity-p
155 (or (assoc 'candidates-in-buffer source)
156 (equal '(identity) (assoc-default 'match source))))
157 (matcher 'anything-mp-3migemo-match)
158 (searcher (if (assoc 'search-from-end source)
159 'anything-mp-3migemo-search-backward
160 'anything-mp-3migemo-search)))
161 (cond (anything-use-migemo
162 `((search ,@(assoc-default 'search source) ,searcher)
163 ,(if match-identity-p
164 '(match identity)
165 `(match ,matcher
166 ,@(assoc-default 'match source)))
167 ,@source))
168 ((assoc 'migemo source)
169 `((search ,searcher)
170 ,(if match-identity-p
171 '(match identity)
172 `(match ,matcher))
173 ,@source))
174 (t source)))))
175 (add-to-list 'anything-compile-source-functions 'anything-compile-source--migemo t)
177 (defvar anything-migemize-command-idle-delay 0.1
178 "`anything-idle-delay' for migemized command.")
179 (defmacro anything-migemize-command (command)
180 "Use migemo in COMMAND when selectiong candidate by `anything'.
181 Bind `anything-use-migemo' = t in COMMAND.
182 `anything-migemize-command-idle-delay' is used instead of `anything-idle-delay'."
183 `(defadvice ,command (around anything-use-migemo activate)
184 (let ((anything-use-migemo t)
185 (anything-idle-delay anything-migemize-command-idle-delay))
186 ad-do-it)))
188 ;;;; unit test
189 ;; (install-elisp "http://www.emacswiki.org/cgi-bin/wiki/download/el-expectations.el")
190 ;; (install-elisp "http://www.emacswiki.org/cgi-bin/wiki/download/el-mock.el")
191 (dont-compile
192 (when (fboundp 'expectations)
193 (expectations
194 (desc "match")
195 (expect '(("TEST" ("日本語")))
196 (let ((anything-use-migemo t))
197 (anything-test-candidates
198 '(((name . "TEST")
199 (candidates "日本語")))
200 "nihongo"
201 '(anything-compile-source--migemo))))
202 (desc "migemo attribute")
203 (expect '(("TEST" ("日本語")))
204 (let ((anything-use-migemo nil))
205 (anything-test-candidates
206 '(((name . "TEST")
207 (candidates "日本語")
208 (migemo)))
209 "nihongo"
210 '(anything-compile-source--migemo))))
212 (desc "with anything-match-plugin")
213 (expect '(("FOO" ("日本語入力")))
214 (let ((anything-use-migemo nil))
215 (anything-test-candidates
216 '(((name . "FOO")
217 (init
218 . (lambda ()
219 (with-current-buffer (anything-candidate-buffer 'global)
220 (insert "日本語会話\n日本語入力\n"))))
221 (candidates-in-buffer)
222 (migemo)))
223 "nihongo nyuuryoku"
224 '(anything-compile-source--candidates-in-buffer
225 anything-compile-source--match-plugin
226 anything-compile-source--migemo))))
227 (expect '(("FOO" ("日本語入力")))
228 (let ((anything-use-migemo t))
229 (anything-test-candidates
230 '(((name . "FOO")
231 (init
232 . (lambda ()
233 (with-current-buffer (anything-candidate-buffer 'global)
234 (insert "日本語会話\n日本語入力\n"))))
235 (candidates-in-buffer)))
236 "nihongo nyuuryoku"
237 '(anything-compile-source--candidates-in-buffer
238 anything-compile-source--match-plugin
239 anything-compile-source--migemo))))
240 (expect '(("FOO" ("日本語入力")))
241 (let ((anything-use-migemo nil))
242 (anything-test-candidates
243 '(((name . "FOO")
244 (init
245 . (lambda ()
246 (with-current-buffer (anything-candidate-buffer 'global)
247 (insert "日本語会話\n日本語入力\n"))))
248 (candidates-in-buffer)
249 (search-from-end)
250 (migemo)))
251 "nihongo nyuuryoku"
252 '(anything-compile-source--candidates-in-buffer
253 anything-compile-source--match-plugin
254 anything-compile-source--migemo))))
255 (expect '(("FOO" ("日本語入力")))
256 (let ((anything-use-migemo t))
257 (anything-test-candidates
258 '(((name . "FOO")
259 (init
260 . (lambda ()
261 (with-current-buffer (anything-candidate-buffer 'global)
262 (insert "日本語会話\n日本語入力\n"))))
263 (candidates-in-buffer)
264 (search-from-end)))
265 "nihongo nyuuryoku"
266 '(anything-compile-source--candidates-in-buffer
267 anything-compile-source--match-plugin
268 anything-compile-source--migemo))))
269 (expect '(("TEST" ("日本語入力")))
270 (let ((anything-use-migemo nil))
271 (anything-test-candidates
272 '(((name . "TEST")
273 (migemo)
274 (candidates "日本語入力")))
275 "nihongo nyuuryoku"
276 '(anything-compile-source--match-plugin anything-compile-source--migemo))))
277 (expect '(("TEST" ("日本語入力")))
278 (let ((anything-use-migemo t))
279 (anything-test-candidates
280 '(((name . "TEST")
281 (candidates "日本語入力")))
282 "nihongo nyuuryoku"
283 '(anything-compile-source--match-plugin anything-compile-source--migemo))))
286 (provide 'anything-migemo)
288 ;; Local Variables:
289 ;; coding: utf-8
290 ;; End:
292 ;; How to save (DO NOT REMOVE!!)
293 ;; (progn (magit-push) (emacswiki-post "anything-migemo.el"))
294 ;;; anything-migemo.el ends here