anything.el: New attribute: coerce
[anything-config.git] / extensions / anything-migemo.el
blob4e18d2f1ba2c908d53ffe2e906c89cde32f21480
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 (defvar anything-migemize-command-idle-delay 0.1
188 "`anything-idle-delay' for migemized command.")
189 (defmacro anything-migemize-command (command)
190 "Use migemo in COMMAND when selectiong candidate by `anything'.
191 Bind `anything-use-migemo' = t in COMMAND.
192 `anything-migemize-command-idle-delay' is used instead of `anything-idle-delay'."
193 `(defadvice ,command (around anything-use-migemo activate)
194 (let ((anything-use-migemo t)
195 (anything-idle-delay anything-migemize-command-idle-delay))
196 ad-do-it)))
198 ;;;; unit test
199 ;; (install-elisp "http://www.emacswiki.org/cgi-bin/wiki/download/el-expectations.el")
200 ;; (install-elisp "http://www.emacswiki.org/cgi-bin/wiki/download/el-mock.el")
201 (dont-compile
202 (when (fboundp 'expectations)
203 (expectations
204 (desc "match")
205 (expect '(("TEST" ("日本語")))
206 (let ((anything-use-migemo t))
207 (anything-test-candidates
208 '(((name . "TEST")
209 (candidates "日本語")))
210 "nihongo"
211 '(anything-compile-source--migemo))))
212 (desc "candidates buffer")
213 (expect '(("TEST" ("日本語")))
214 (let ((anything-use-migemo t))
215 (anything-test-candidates
216 '(((name . "TEST")
217 (init
218 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
219 (insert "日本語\n"))))
220 (candidates-in-buffer)))
221 "nihongo"
222 '(anything-compile-source--candidates-in-buffer
223 anything-compile-source--migemo))))
224 (desc "migemo attribute")
225 (expect '(("TEST" ("日本語")))
226 (let ((anything-use-migemo nil))
227 (anything-test-candidates
228 '(((name . "TEST")
229 (candidates "日本語")
230 (migemo)))
231 "nihongo"
232 '(anything-compile-source--migemo))))
233 (expect '(("TEST" ("日本語")))
234 (let ((anything-use-migemo nil))
235 (anything-test-candidates
236 '(((name . "TEST")
237 (init
238 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
239 (insert "日本語\n"))))
240 (candidates-in-buffer)
241 (migemo)))
242 "nihongo"
243 '(anything-compile-source--candidates-in-buffer
244 anything-compile-source--migemo))))
245 (desc "search-from-end attribute")
247 (expect '(("FOO" ("日本語入力" "日本語会話")))
248 (let ((anything-use-migemo nil))
249 (anything-test-candidates '(((name . "FOO")
250 (init
251 . (lambda ()
252 (with-current-buffer (anything-candidate-buffer 'global)
253 (insert "日本語会話\n日本語入力\n"))))
254 (candidates-in-buffer)
255 (migemo)
256 (search-from-end)))
257 "nihongo"
258 '(anything-compile-source--candidates-in-buffer
259 anything-compile-source--migemo))))
260 (expect '(("FOO" ("日本語入力" "日本語会話")))
261 (let ((anything-use-migemo t))
262 (anything-test-candidates '(((name . "FOO")
263 (init
264 . (lambda ()
265 (with-current-buffer (anything-candidate-buffer 'global)
266 (insert "日本語会話\n日本語入力\n"))))
267 (candidates-in-buffer)
268 (search-from-end)))
269 "nihongo"
270 '(anything-compile-source--candidates-in-buffer
271 anything-compile-source--migemo))))
272 (desc "with anything-match-plugin")
274 (expect '(("FOO" ("日本語入力")))
275 (let ((anything-use-migemo nil))
276 (anything-test-candidates '(((name . "FOO")
277 (init
278 . (lambda ()
279 (with-current-buffer (anything-candidate-buffer 'global)
280 (insert "日本語会話\n日本語入力\n"))))
281 (candidates-in-buffer)
282 (migemo)))
283 "nihongo nyuuryoku"
284 '(anything-compile-source--candidates-in-buffer
285 anything-compile-source--match-plugin
286 anything-compile-source--migemo))))
287 (expect '(("FOO" ("日本語入力")))
288 (let ((anything-use-migemo t))
289 (anything-test-candidates '(((name . "FOO")
290 (init
291 . (lambda ()
292 (with-current-buffer (anything-candidate-buffer 'global)
293 (insert "日本語会話\n日本語入力\n"))))
294 (candidates-in-buffer)))
295 "nihongo nyuuryoku"
296 '(anything-compile-source--candidates-in-buffer
297 anything-compile-source--match-plugin
298 anything-compile-source--migemo))))
299 (expect '(("FOO" ("日本語入力")))
300 (let ((anything-use-migemo nil))
301 (anything-test-candidates '(((name . "FOO")
302 (init
303 . (lambda ()
304 (with-current-buffer (anything-candidate-buffer 'global)
305 (insert "日本語会話\n日本語入力\n"))))
306 (candidates-in-buffer)
307 (search-from-end)
308 (migemo)))
309 "nihongo nyuuryoku"
310 '(anything-compile-source--candidates-in-buffer
311 anything-compile-source--match-plugin
312 anything-compile-source--migemo))))
313 (expect '(("FOO" ("日本語入力")))
314 (let ((anything-use-migemo t))
315 (anything-test-candidates '(((name . "FOO")
316 (init
317 . (lambda ()
318 (with-current-buffer (anything-candidate-buffer 'global)
319 (insert "日本語会話\n日本語入力\n"))))
320 (candidates-in-buffer)
321 (search-from-end)))
322 "nihongo nyuuryoku"
323 '(anything-compile-source--candidates-in-buffer
324 anything-compile-source--match-plugin
325 anything-compile-source--migemo))))
326 (expect '(("TEST" ("日本語入力")))
327 (let ((anything-use-migemo nil))
328 (anything-test-candidates
329 '(((name . "TEST")
330 (migemo)
331 (candidates "日本語入力")))
332 "nihongo nyuuryoku"
333 '(anything-compile-source--match-plugin anything-compile-source--migemo))))
334 (expect '(("TEST" ("日本語入力")))
335 (let ((anything-use-migemo t))
336 (anything-test-candidates
337 '(((name . "TEST")
338 (candidates "日本語入力")))
339 "nihongo nyuuryoku"
340 '(anything-compile-source--match-plugin anything-compile-source--migemo))))
343 (provide 'anything-migemo)
345 ;; How to save (DO NOT REMOVE!!)
346 ;; (progn (magit-push) (emacswiki-post "anything-migemo.el"))
347 ;;; anything-migemo.el ends here