* `anything-mklist': Make a list when the argument is lambda.
[anything-config.git] / anything-migemo.el
blob6698933d7095973ef3d5566af3614852fa2729d7
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 nil t)
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 (when (memq 'anything-compile-source--match-plugin anything-compile-source-functions)
142 (defun* anything-mp-3migemo-match (str &optional (pattern anything-pattern))
143 (loop for (pred . re) in (anything-mp-3-get-patterns pattern)
144 always (funcall pred (anything-string-match-with-migemo str re))))
145 (defun anything-mp-3migemo-search (pattern &rest ignore)
146 (anything-mp-3-search-base migemo-forward migemo-forward bol eol))
147 (defun anything-mp-3migemo-search-backward (pattern &rest ignore)
148 (anything-mp-3-search-base migemo-backward migemo-backward eol bol)))
149 ;; (anything-string-match-with-migemo "ÆüËܸìÆþÎÏ" "nihongo")
150 ;; (anything-string-match-with-migemo "ÆüËܸìÆþÎÏ" "nyuuryoku")
151 ;; (anything-mp-3migemo-match "ÆüËܸìÆþÎÏ" "nihongo nyuuryoku")
152 (defun anything-compile-source--migemo (source)
153 (if (not (featurep 'migemo))
154 source
155 (let* ((match-identity-p
156 (or (assoc 'candidates-in-buffer source)
157 (equal '(identity) (assoc-default 'match source))))
158 (use-match-plugin
159 (memq 'anything-compile-source--match-plugin anything-compile-source-functions))
160 (matcher (if use-match-plugin
161 'anything-mp-3migemo-match
162 'anything-string-match-with-migemo))
163 (searcher (if (assoc 'search-from-end source)
164 (if use-match-plugin
165 'anything-mp-3migemo-search-backward
166 'migemo-backward)
167 (if use-match-plugin
168 'anything-mp-3migemo-search
169 'migemo-forward))))
170 (cond (anything-use-migemo
171 `((delayed)
172 (search ,@(assoc-default 'search source) ,searcher)
173 ,(if match-identity-p
174 '(match identity)
175 `(match ,matcher
176 ,@(assoc-default 'match source)))
177 ,@source))
178 ((assoc 'migemo source)
179 `((search ,searcher)
180 ,(if match-identity-p
181 '(match identity)
182 `(match ,matcher))
183 ,@source))
184 (t source)))))
185 (add-to-list 'anything-compile-source-functions 'anything-compile-source--migemo t)
187 (defmacro anything-migemize-command (command)
188 "Use migemo in COMMAND when selectiong candidate by `anything'.
189 Bind `anything-use-migemo' = t in COMMAND."
190 `(defadvice ,command (around anything-use-migemo activate)
191 (let ((anything-use-migemo t)) ad-do-it)))
193 ;;;; unit test
194 ;; (install-elisp "http://www.emacswiki.org/cgi-bin/wiki/download/el-expectations.el")
195 ;; (install-elisp "http://www.emacswiki.org/cgi-bin/wiki/download/el-mock.el")
196 (dont-compile
197 (when (fboundp 'expectations)
198 (expectations
199 (desc "match")
200 (expect '(("TEST" ("ÆüËܸì")))
201 (let ((anything-use-migemo t))
202 (anything-test-candidates
203 '(((name . "TEST")
204 (candidates "ÆüËܸì")))
205 "nihongo"
206 '(anything-compile-source--migemo))))
207 (desc "candidates buffer")
208 (expect '(("TEST" ("ÆüËܸì")))
209 (let ((anything-use-migemo t))
210 (anything-test-candidates
211 '(((name . "TEST")
212 (init
213 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
214 (insert "ÆüËܸì\n"))))
215 (candidates-in-buffer)))
216 "nihongo"
217 '(anything-compile-source--candidates-in-buffer
218 anything-compile-source--migemo))))
219 (desc "migemo attribute")
220 (expect '(("TEST" ("ÆüËܸì")))
221 (let ((anything-use-migemo nil))
222 (anything-test-candidates
223 '(((name . "TEST")
224 (candidates "ÆüËܸì")
225 (migemo)))
226 "nihongo"
227 '(anything-compile-source--migemo))))
228 (expect '(("TEST" ("ÆüËܸì")))
229 (let ((anything-use-migemo nil))
230 (anything-test-candidates
231 '(((name . "TEST")
232 (init
233 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
234 (insert "ÆüËܸì\n"))))
235 (candidates-in-buffer)
236 (migemo)))
237 "nihongo"
238 '(anything-compile-source--candidates-in-buffer
239 anything-compile-source--migemo))))
240 (desc "search-from-end attribute")
242 (expect '(("FOO" ("ÆüËܸìÆþÎÏ" "ÆüËܸì²ñÏÃ")))
243 (let ((anything-use-migemo nil))
244 (anything-test-candidates '(((name . "FOO")
245 (init
246 . (lambda ()
247 (with-current-buffer (anything-candidate-buffer 'global)
248 (insert "ÆüËܸì²ñÏÃ\nÆüËܸìÆþÎÏ\n"))))
249 (candidates-in-buffer)
250 (migemo)
251 (search-from-end)))
252 "nihongo"
253 '(anything-compile-source--candidates-in-buffer
254 anything-compile-source--migemo))))
255 (expect '(("FOO" ("ÆüËܸìÆþÎÏ" "ÆüËܸì²ñÏÃ")))
256 (let ((anything-use-migemo t))
257 (anything-test-candidates '(((name . "FOO")
258 (init
259 . (lambda ()
260 (with-current-buffer (anything-candidate-buffer 'global)
261 (insert "ÆüËܸì²ñÏÃ\nÆüËܸìÆþÎÏ\n"))))
262 (candidates-in-buffer)
263 (search-from-end)))
264 "nihongo"
265 '(anything-compile-source--candidates-in-buffer
266 anything-compile-source--migemo))))
267 (desc "with anything-match-plugin")
269 (expect '(("FOO" ("ÆüËܸìÆþÎÏ")))
270 (let ((anything-use-migemo nil))
271 (anything-test-candidates '(((name . "FOO")
272 (init
273 . (lambda ()
274 (with-current-buffer (anything-candidate-buffer 'global)
275 (insert "ÆüËܸì²ñÏÃ\nÆüËܸìÆþÎÏ\n"))))
276 (candidates-in-buffer)
277 (migemo)))
278 "nihongo nyuuryoku"
279 '(anything-compile-source--candidates-in-buffer
280 anything-compile-source--match-plugin
281 anything-compile-source--migemo))))
282 (expect '(("FOO" ("ÆüËܸìÆþÎÏ")))
283 (let ((anything-use-migemo t))
284 (anything-test-candidates '(((name . "FOO")
285 (init
286 . (lambda ()
287 (with-current-buffer (anything-candidate-buffer 'global)
288 (insert "ÆüËܸì²ñÏÃ\nÆüËܸìÆþÎÏ\n"))))
289 (candidates-in-buffer)))
290 "nihongo nyuuryoku"
291 '(anything-compile-source--candidates-in-buffer
292 anything-compile-source--match-plugin
293 anything-compile-source--migemo))))
294 (expect '(("FOO" ("ÆüËܸìÆþÎÏ")))
295 (let ((anything-use-migemo nil))
296 (anything-test-candidates '(((name . "FOO")
297 (init
298 . (lambda ()
299 (with-current-buffer (anything-candidate-buffer 'global)
300 (insert "ÆüËܸì²ñÏÃ\nÆüËܸìÆþÎÏ\n"))))
301 (candidates-in-buffer)
302 (search-from-end)
303 (migemo)))
304 "nihongo nyuuryoku"
305 '(anything-compile-source--candidates-in-buffer
306 anything-compile-source--match-plugin
307 anything-compile-source--migemo))))
308 (expect '(("FOO" ("ÆüËܸìÆþÎÏ")))
309 (let ((anything-use-migemo t))
310 (anything-test-candidates '(((name . "FOO")
311 (init
312 . (lambda ()
313 (with-current-buffer (anything-candidate-buffer 'global)
314 (insert "ÆüËܸì²ñÏÃ\nÆüËܸìÆþÎÏ\n"))))
315 (candidates-in-buffer)
316 (search-from-end)))
317 "nihongo nyuuryoku"
318 '(anything-compile-source--candidates-in-buffer
319 anything-compile-source--match-plugin
320 anything-compile-source--migemo))))
321 (expect '(("TEST" ("ÆüËܸìÆþÎÏ")))
322 (let ((anything-use-migemo nil))
323 (anything-test-candidates
324 '(((name . "TEST")
325 (migemo)
326 (candidates "ÆüËܸìÆþÎÏ")))
327 "nihongo nyuuryoku"
328 '(anything-compile-source--match-plugin anything-compile-source--migemo))))
329 (expect '(("TEST" ("ÆüËܸìÆþÎÏ")))
330 (let ((anything-use-migemo t))
331 (anything-test-candidates
332 '(((name . "TEST")
333 (candidates "ÆüËܸìÆþÎÏ")))
334 "nihongo nyuuryoku"
335 '(anything-compile-source--match-plugin anything-compile-source--migemo))))
338 (provide 'anything-migemo)
340 ;; How to save (DO NOT REMOVE!!)
341 ;; (emacswiki-post "anything-migemo.el")
342 ;;; anything-migemo.el ends here