File: anything-migemo.el

package info (click to toggle)
anything-el 1.287-2.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, sid, stretch
  • size: 824 kB
  • sloc: lisp: 13,158; sh: 43; makefile: 3
file content (342 lines) | stat: -rw-r--r-- 14,823 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
;;; anything-migemo.el --- Migemo plug-in for anything
;; $Id: anything-migemo.el,v 1.18 2009-06-07 17:52:22 rubikitch Exp $

;; Copyright (C) 2007, 2008, 2009  rubikitch

;; Author: rubikitch <rubikitch@ruby-lang.org>
;; Keywords: anything, convenience, tools, i18n, japanese
;; URL: http://www.emacswiki.org/cgi-bin/wiki/download/anything-migemo.el

;; This file 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 3, or (at your option)
;; any later version.

;; This file 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., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Commentary:

;; Migemo extension of `anything'. Use `anything-migemo' instead of
;; `anything'. If `anything-migemo' is invoked with prefix argument,
;; `anything' is migemo-ized. This means that pattern matching of
;; `anything' candidates is done by migemo-expanded `anything-pattern'.

;;; Commands:
;;
;; Below are complete command list:
;;
;;  `anything-migemo'
;;    `anything' with migemo extension.
;;
;;; Customizable Options:
;;
;; Below are customizable option list:
;;

;; If you want to use migemo search source-locally, add (migemo) to
;; the source. It sets match and search attribute appropriately for
;; migemo.

;;; Setting:

;; (require 'anything-config)
;; (require 'anything-migemo)
;; (define-key global-map [(control ?:)] 'anything-migemo)

;;; Bug:

;; Simultaneous use of (candidates-in-buffer), (search
;; . migemo-forward) and (delayed) scrambles *anything* buffer. Maybe
;; because of collision of `migemo-process' and `run-with-idle-timer'

;;; History:

;; $Log: anything-migemo.el,v $
;; Revision 1.18  2009-06-07 17:52:22  rubikitch
;; New macro `anything-migemize-command'.
;;
;; Revision 1.17  2009/06/04 20:32:00  rubikitch
;; migemo is soft-required now; this file has no effect unless migemo is installed.
;;
;; Revision 1.16  2008/10/03 20:43:18  rubikitch
;; Use with anything-match-plugin.el
;;
;; Revision 1.15  2008/10/03 20:01:46  rubikitch
;; refactoring
;;
;; Revision 1.14  2008/08/25 08:29:02  rubikitch
;; `anything-migemo': anything-args
;;
;; Revision 1.13  2008/08/24 20:39:53  rubikitch
;; prevent the unit test from being byte-compiled.
;;
;; Revision 1.12  2008/08/24 18:01:25  rubikitch
;; *** empty log message ***
;;
;; Revision 1.11  2008/08/24 08:23:30  rubikitch
;; Rename `anything-candidates-buffer' -> `anything-candidate-buffer'
;;
;; Revision 1.10  2008/08/24 01:54:21  rubikitch
;; migemo attribute
;;
;; Revision 1.9  2008/08/19 21:38:09  rubikitch
;; match attribute bug fix
;;
;; Revision 1.8  2008/08/19 21:30:29  rubikitch
;; plug-in
;;
;; Revision 1.7  2008/08/10 22:45:02  rubikitch
;; Bug info
;;
;; Revision 1.6  2008/08/08 03:40:51  rubikitch
;; require migemo
;;
;; Revision 1.5  2008/08/08 03:38:34  rubikitch
;; add search attribute
;; unit tests
;;
;; Revision 1.4  2007/12/26 08:36:01  rubikitch
;; changed match priority
;;
;; Revision 1.3  2007/12/25 19:55:59  rubikitch
;; patch is not needed anymore.
;;
;; Revision 1.2  2007/12/25 13:05:46  rubikitch
;; speed up by memoization
;;
;; Revision 1.1  2007/12/25 12:03:25  rubikitch
;; Initial revision
;;

;;; Code:

(eval-when-compile (require 'anything))
(require 'migemo nil t)
(require 'anything-match-plugin nil t)
(defvar anything-use-migemo nil
  "[Internal] If non-nil, `anything' is migemo-ized.")
(defun anything-migemo (with-migemo &rest anything-args)
  "`anything' with migemo extension.
With prefix arugument, `anything-pattern' is migemo-ized, otherwise normal `anything'."
  (interactive "P")
  (let ((anything-use-migemo with-migemo))
    (apply 'anything anything-args)))

(defvar anything-previous-migemo-info '("" . "")
  "[Internal] Previous migemo query for anything-migemo.")
(defun* anything-string-match-with-migemo (str &optional (pattern anything-pattern))
  "Migemo version of `string-match'."
  (unless (string= pattern (car anything-previous-migemo-info))
    (setq anything-previous-migemo-info (cons pattern (migemo-get-pattern pattern))))
  (string-match (cdr anything-previous-migemo-info) str))

(when (memq 'anything-compile-source--match-plugin anything-compile-source-functions)
  (defun* anything-mp-3migemo-match (str &optional (pattern anything-pattern))
    (loop for (pred . re) in (anything-mp-3-get-patterns pattern)
          always (funcall pred (anything-string-match-with-migemo str re))))
  (defun anything-mp-3migemo-search (pattern &rest ignore)
    (anything-mp-3-search-base migemo-forward migemo-forward bol eol))
  (defun anything-mp-3migemo-search-backward (pattern &rest ignore)
    (anything-mp-3-search-base migemo-backward migemo-backward eol bol)))
;; (anything-string-match-with-migemo "日本語入力" "nihongo")
;; (anything-string-match-with-migemo "日本語入力" "nyuuryoku")
;; (anything-mp-3migemo-match "日本語入力" "nihongo nyuuryoku")
(defun anything-compile-source--migemo (source)
  (if (not (featurep 'migemo))
      source
    (let* ((match-identity-p 
            (or (assoc 'candidates-in-buffer source)
                (equal '(identity) (assoc-default 'match source))))
           (use-match-plugin
            (memq 'anything-compile-source--match-plugin anything-compile-source-functions))
           (matcher (if use-match-plugin
                        'anything-mp-3migemo-match
                      'anything-string-match-with-migemo))
           (searcher (if (assoc 'search-from-end source)
                         (if use-match-plugin
                             'anything-mp-3migemo-search-backward
                           'migemo-backward)
                       (if use-match-plugin
                           'anything-mp-3migemo-search
                         'migemo-forward))))
      (cond (anything-use-migemo
             `((delayed)
               (search ,@(assoc-default 'search source) ,searcher)
               ,(if match-identity-p
                    '(match identity)
                  `(match ,matcher
                          ,@(assoc-default 'match source)))
               ,@source))
            ((assoc 'migemo source)
             `((search ,searcher)
               ,(if match-identity-p
                    '(match identity)
                  `(match ,matcher))
               ,@source))
            (t source)))))
(add-to-list 'anything-compile-source-functions 'anything-compile-source--migemo t)

(defmacro anything-migemize-command (command)
  "Use migemo in COMMAND when selectiong candidate by `anything'.
Bind `anything-use-migemo' = t in COMMAND."
  `(defadvice ,command (around anything-use-migemo activate)
     (let ((anything-use-migemo t)) ad-do-it)))

;;;; unit test
;; (install-elisp "http://www.emacswiki.org/cgi-bin/wiki/download/el-expectations.el")
;; (install-elisp "http://www.emacswiki.org/cgi-bin/wiki/download/el-mock.el")
(dont-compile
  (when (fboundp 'expectations)
    (expectations
      (desc "match")
      (expect '(("TEST" ("日本語")))
        (let ((anything-use-migemo t))
          (anything-test-candidates
           '(((name . "TEST")
              (candidates "日本語")))
           "nihongo"
           '(anything-compile-source--migemo))))
      (desc "candidates buffer")
      (expect '(("TEST" ("日本語")))
        (let ((anything-use-migemo t))
          (anything-test-candidates
           '(((name . "TEST")
              (init
               . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
                              (insert "日本語\n"))))
              (candidates-in-buffer)))
           "nihongo"
           '(anything-compile-source--candidates-in-buffer
             anything-compile-source--migemo))))
      (desc "migemo attribute")
      (expect '(("TEST" ("日本語")))
        (let ((anything-use-migemo nil))
          (anything-test-candidates
           '(((name . "TEST")
              (candidates "日本語")
              (migemo)))
           "nihongo"
           '(anything-compile-source--migemo))))
      (expect '(("TEST" ("日本語")))
        (let ((anything-use-migemo nil))
          (anything-test-candidates
           '(((name . "TEST")
              (init
               . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
                              (insert "日本語\n"))))
              (candidates-in-buffer)
              (migemo)))
           "nihongo"
           '(anything-compile-source--candidates-in-buffer
             anything-compile-source--migemo))))
      (desc "search-from-end attribute")
      
      (expect '(("FOO" ("日本語入力" "日本語会話")))
        (let ((anything-use-migemo nil))
          (anything-test-candidates '(((name . "FOO")
                                       (init
                                        . (lambda ()
                                            (with-current-buffer (anything-candidate-buffer 'global)
                                              (insert "日本語会話\n日本語入力\n"))))
                                       (candidates-in-buffer)
                                       (migemo)
                                       (search-from-end)))
                                    "nihongo"
                                    '(anything-compile-source--candidates-in-buffer
                                      anything-compile-source--migemo))))
      (expect '(("FOO" ("日本語入力" "日本語会話")))
        (let ((anything-use-migemo t))
          (anything-test-candidates '(((name . "FOO")
                                       (init
                                        . (lambda ()
                                            (with-current-buffer (anything-candidate-buffer 'global)
                                              (insert "日本語会話\n日本語入力\n"))))
                                       (candidates-in-buffer)
                                       (search-from-end)))
                                    "nihongo"
                                    '(anything-compile-source--candidates-in-buffer
                                      anything-compile-source--migemo))))
      (desc "with anything-match-plugin")
      
      (expect '(("FOO" ("日本語入力")))
        (let ((anything-use-migemo nil))
          (anything-test-candidates '(((name . "FOO")
                                       (init
                                        . (lambda ()
                                            (with-current-buffer (anything-candidate-buffer 'global)
                                              (insert "日本語会話\n日本語入力\n"))))
                                       (candidates-in-buffer)
                                       (migemo)))
                                    "nihongo nyuuryoku"
                                    '(anything-compile-source--candidates-in-buffer
                                      anything-compile-source--match-plugin
                                      anything-compile-source--migemo))))
      (expect '(("FOO" ("日本語入力")))
        (let ((anything-use-migemo t))
          (anything-test-candidates '(((name . "FOO")
                                       (init
                                        . (lambda ()
                                            (with-current-buffer (anything-candidate-buffer 'global)
                                              (insert "日本語会話\n日本語入力\n"))))
                                       (candidates-in-buffer)))
                                    "nihongo nyuuryoku"
                                    '(anything-compile-source--candidates-in-buffer
                                      anything-compile-source--match-plugin
                                      anything-compile-source--migemo))))
      (expect '(("FOO" ("日本語入力")))
        (let ((anything-use-migemo nil))
          (anything-test-candidates '(((name . "FOO")
                                       (init
                                        . (lambda ()
                                            (with-current-buffer (anything-candidate-buffer 'global)
                                              (insert "日本語会話\n日本語入力\n"))))
                                       (candidates-in-buffer)
                                       (search-from-end)
                                       (migemo)))
                                    "nihongo nyuuryoku"
                                    '(anything-compile-source--candidates-in-buffer
                                      anything-compile-source--match-plugin
                                      anything-compile-source--migemo))))
      (expect '(("FOO" ("日本語入力")))
        (let ((anything-use-migemo t))
          (anything-test-candidates '(((name . "FOO")
                                       (init
                                        . (lambda ()
                                            (with-current-buffer (anything-candidate-buffer 'global)
                                              (insert "日本語会話\n日本語入力\n"))))
                                       (candidates-in-buffer)
                                       (search-from-end)))
                                    "nihongo nyuuryoku"
                                    '(anything-compile-source--candidates-in-buffer
                                      anything-compile-source--match-plugin
                                      anything-compile-source--migemo))))
      (expect '(("TEST" ("日本語入力")))
        (let ((anything-use-migemo nil))
          (anything-test-candidates
           '(((name . "TEST")
              (migemo)
              (candidates "日本語入力")))
           "nihongo nyuuryoku"
           '(anything-compile-source--match-plugin anything-compile-source--migemo))))
      (expect '(("TEST" ("日本語入力")))
        (let ((anything-use-migemo t))
          (anything-test-candidates
           '(((name . "TEST")
              (candidates "日本語入力")))
           "nihongo nyuuryoku"
           '(anything-compile-source--match-plugin anything-compile-source--migemo))))
      )))

(provide 'anything-migemo)

;; How to save (DO NOT REMOVE!!)
;; (progn (magit-push) (emacswiki-post "anything-migemo.el"))
;;; anything-migemo.el ends here