| 1 | ;;; anything-match-plugin.el --- Humane match plug-in for anything |
|---|
| 2 | ;; $Id: anything-match-plugin.el,v 1.19 2008/09/08 06:58:59 rubikitch Exp $ |
|---|
| 3 | |
|---|
| 4 | ;; Copyright (C) 2008 rubikitch |
|---|
| 5 | |
|---|
| 6 | ;; Author: rubikitch <rubikitch@ruby-lang.org> |
|---|
| 7 | ;; Keywords: anything, matching |
|---|
| 8 | ;; URL: http://www.emacswiki.org/cgi-bin/wiki/download/anything-match-plugin.el |
|---|
| 9 | |
|---|
| 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 2, or (at your option) |
|---|
| 13 | ;; any later version. |
|---|
| 14 | |
|---|
| 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. |
|---|
| 19 | |
|---|
| 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. |
|---|
| 24 | |
|---|
| 25 | ;;; Commentary: |
|---|
| 26 | |
|---|
| 27 | ;; Change anything.el matching algorithm humanely. |
|---|
| 28 | ;; It gives anything.el search refinement functionality. |
|---|
| 29 | ;; exact match -> prefix match -> multiple regexp match |
|---|
| 30 | |
|---|
| 31 | ;; A query of multiple regexp match is space-delimited string. |
|---|
| 32 | ;; Anything displays candidates which matches all the regexps. |
|---|
| 33 | ;; A regexp with "!" prefix means not matching the regexp. |
|---|
| 34 | ;; To include spaces to a regexp, prefix "\" before space, |
|---|
| 35 | ;; it is controlled by `anything-mp-space-regexp' variable. |
|---|
| 36 | |
|---|
| 37 | ;; Just require it to use. |
|---|
| 38 | |
|---|
| 39 | ;;; History: |
|---|
| 40 | |
|---|
| 41 | ;; $Log: anything-match-plugin.el,v $ |
|---|
| 42 | ;; Revision 1.19 2008/09/08 06:58:59 rubikitch |
|---|
| 43 | ;; changed default `anything-mp-space-regexp' to "[\\ ] " |
|---|
| 44 | ;; |
|---|
| 45 | ;; Revision 1.18 2008/09/07 12:09:01 rubikitch |
|---|
| 46 | ;; *** empty log message *** |
|---|
| 47 | ;; |
|---|
| 48 | ;; Revision 1.17 2008/09/07 07:48:12 rubikitch |
|---|
| 49 | ;; Append commentary. |
|---|
| 50 | ;; Multiple regexp match with regexp negation. |
|---|
| 51 | ;; |
|---|
| 52 | ;; Revision 1.16 2008/09/07 06:58:11 rubikitch |
|---|
| 53 | ;; Added mp-3p match: permutation with prefix match |
|---|
| 54 | ;; |
|---|
| 55 | ;; Revision 1.15 2008/09/07 05:23:07 rubikitch |
|---|
| 56 | ;; New variable: `anything-mp-space-regexp' |
|---|
| 57 | ;; |
|---|
| 58 | ;; Revision 1.14 2008/09/03 03:33:09 rubikitch |
|---|
| 59 | ;; anything-exact-*, anything-prefix-*: memoize |
|---|
| 60 | ;; |
|---|
| 61 | ;; Revision 1.13 2008/09/02 10:56:50 rubikitch |
|---|
| 62 | ;; anything-mp-3-*: MUCH MUCH FASTER |
|---|
| 63 | ;; changed algorithm |
|---|
| 64 | ;; |
|---|
| 65 | ;; Revision 1.12 2008/09/01 13:41:57 rubikitch |
|---|
| 66 | ;; search functions for search-from-end |
|---|
| 67 | ;; |
|---|
| 68 | ;; Revision 1.11 2008/08/24 20:40:27 rubikitch |
|---|
| 69 | ;; prevent the unit test from being byte-compiled. |
|---|
| 70 | ;; |
|---|
| 71 | ;; Revision 1.10 2008/08/24 17:48:53 rubikitch |
|---|
| 72 | ;; Add commentary |
|---|
| 73 | ;; |
|---|
| 74 | ;; Revision 1.9 2008/08/24 08:23:16 rubikitch |
|---|
| 75 | ;; Rename `anything-candidates-buffer' -> `anything-candidate-buffer' |
|---|
| 76 | ;; |
|---|
| 77 | ;; Revision 1.8 2008/08/22 21:25:44 rubikitch |
|---|
| 78 | ;; *** empty log message *** |
|---|
| 79 | ;; |
|---|
| 80 | ;; Revision 1.7 2008/08/22 21:17:58 rubikitch |
|---|
| 81 | ;; exact, prefix match: faster |
|---|
| 82 | ;; |
|---|
| 83 | ;; Revision 1.6 2008/08/22 19:40:22 rubikitch |
|---|
| 84 | ;; exact -> prefix -> mp-3 by default because of speed |
|---|
| 85 | ;; |
|---|
| 86 | ;; Revision 1.5 2008/08/22 19:04:53 rubikitch |
|---|
| 87 | ;; reimplemented |
|---|
| 88 | ;; |
|---|
| 89 | ;; Revision 1.4 2008/08/20 00:10:15 rubikitch |
|---|
| 90 | ;; *** empty log message *** |
|---|
| 91 | ;; |
|---|
| 92 | ;; Revision 1.3 2008/08/19 23:30:39 rubikitch |
|---|
| 93 | ;; exact match support |
|---|
| 94 | ;; |
|---|
| 95 | ;; Revision 1.2 2008/08/19 23:02:29 rubikitch |
|---|
| 96 | ;; candidates-in-buffer hack |
|---|
| 97 | ;; |
|---|
| 98 | ;; Revision 1.1 2008/08/19 19:45:11 rubikitch |
|---|
| 99 | ;; Initial revision |
|---|
| 100 | ;; |
|---|
| 101 | |
|---|
| 102 | ;;; Code: |
|---|
| 103 | |
|---|
| 104 | (require 'anything) |
|---|
| 105 | (require 'cl) |
|---|
| 106 | |
|---|
| 107 | ;;;; multiple patterns |
|---|
| 108 | (defvar anything-use-multiple-patterns t |
|---|
| 109 | "If non-nil, enable anything-use-multiple-patterns.") |
|---|
| 110 | (defvar anything-mp-space-regexp "[\\ ] " |
|---|
| 111 | "Regexp to represent space itself in multiple regexp match.") |
|---|
| 112 | |
|---|
| 113 | (defun amp-mp-make-regexps (pattern) |
|---|
| 114 | (if (string= pattern "") '("") |
|---|
| 115 | (loop for s in (split-string (replace-regexp-in-string anything-mp-space-regexp "\000\000" pattern) " " t) |
|---|
| 116 | collect (replace-regexp-in-string "\000\000" " " s)))) |
|---|
| 117 | |
|---|
| 118 | (defun amp-mp-1-make-regexp (pattern) |
|---|
| 119 | (mapconcat 'identity (amp-mp-make-regexps pattern) ".*")) |
|---|
| 120 | |
|---|
| 121 | (defmacro amp-define-memoizer (prefix pattern-expr) |
|---|
| 122 | (let ((pattern-str (intern (concat prefix "pattern-str"))) |
|---|
| 123 | (pattern-real (intern (concat prefix "pattern-real"))) |
|---|
| 124 | (get-pattern (intern (concat prefix "get-pattern")))) |
|---|
| 125 | `(progn |
|---|
| 126 | (defvar ,pattern-str nil) |
|---|
| 127 | (defvar ,pattern-real nil) |
|---|
| 128 | (defsubst ,get-pattern (pattern) |
|---|
| 129 | (unless (equal pattern ,pattern-str) |
|---|
| 130 | (setq ,pattern-str pattern |
|---|
| 131 | ,pattern-real ,pattern-expr)) |
|---|
| 132 | ,pattern-real)))) |
|---|
| 133 | |
|---|
| 134 | (defmacro amp-define (prefix pattern-expr) |
|---|
| 135 | (let ((get-pattern (intern (concat prefix "get-pattern"))) |
|---|
| 136 | (match (intern (concat prefix "match"))) |
|---|
| 137 | (search (intern (concat prefix "search"))) |
|---|
| 138 | (search-backward (intern (concat prefix "search-backward")))) |
|---|
| 139 | `(progn |
|---|
| 140 | (amp-define-memoizer ,prefix ,pattern-expr) |
|---|
| 141 | (defun* ,match (str &optional (pattern anything-pattern)) |
|---|
| 142 | (string-match (,get-pattern pattern) str)) |
|---|
| 143 | (defun ,search (pattern &rest ignore) |
|---|
| 144 | (re-search-forward (,get-pattern pattern) nil t)) |
|---|
| 145 | (defun ,search-backward (pattern &rest ignore) |
|---|
| 146 | (re-search-backward (,get-pattern pattern) nil t))))) |
|---|
| 147 | |
|---|
| 148 | ;; exact match |
|---|
| 149 | ;(amp-define "anything-exact-" (concat (anything-prefix-get-pattern pattern) "$")) |
|---|
| 150 | (amp-define-memoizer "anything-exact-" (concat "\n" pattern "\n")) |
|---|
| 151 | (defun anything-exact-match (str &optional pattern) |
|---|
| 152 | (string= str (or pattern anything-pattern))) |
|---|
| 153 | (defun anything-exact-search (pattern &rest ignore) |
|---|
| 154 | (and (search-forward (anything-exact-get-pattern pattern) nil t) |
|---|
| 155 | (forward-line -1))) |
|---|
| 156 | (defun anything-exact-search-backward (pattern &rest ignore) |
|---|
| 157 | (and (search-backward (anything-exact-get-pattern pattern) nil t) |
|---|
| 158 | (forward-line 1))) |
|---|
| 159 | ;; prefix match |
|---|
| 160 | ;;(amp-define "anything-prefix-" (concat "^" (regexp-quote pattern))) |
|---|
| 161 | (amp-define-memoizer "anything-prefix-" (concat "\n" pattern)) |
|---|
| 162 | (defun anything-prefix-match (str &optional pattern) |
|---|
| 163 | (setq pattern (or pattern anything-pattern)) |
|---|
| 164 | (let ((len (length pattern))) |
|---|
| 165 | (and (<= len (length str)) |
|---|
| 166 | (string= (substring str 0 len) pattern )))) |
|---|
| 167 | (defun anything-prefix-search (pattern &rest ignore) |
|---|
| 168 | (search-forward (anything-prefix-get-pattern pattern) nil t)) |
|---|
| 169 | (defun anything-prefix-search-backward (pattern &rest ignore) |
|---|
| 170 | (and (search-backward (anything-prefix-get-pattern pattern) nil t) |
|---|
| 171 | (forward-line 1))) |
|---|
| 172 | ;; multiple regexp patterns 1 (order is preserved / prefix) |
|---|
| 173 | (amp-define "anything-mp-1-" (concat "^" (amp-mp-1-make-regexp pattern))) |
|---|
| 174 | ;; multiple regexp patterns 2 (order is preserved / partial) |
|---|
| 175 | (amp-define "anything-mp-2-" (concat "^.+" (amp-mp-1-make-regexp pattern))) |
|---|
| 176 | |
|---|
| 177 | ;;;; multiple regexp patterns 3 (permutation) |
|---|
| 178 | (defvar anything-mp-3-pattern-str nil) |
|---|
| 179 | (defvar anything-mp-3-pattern-list nil) |
|---|
| 180 | (defsubst anything-mp-3-get-patterns (pattern) |
|---|
| 181 | (unless (equal pattern anything-mp-3-pattern-str) |
|---|
| 182 | (setq anything-mp-3-pattern-str pattern |
|---|
| 183 | anything-mp-3-pattern-list |
|---|
| 184 | (loop for pat in (amp-mp-make-regexps pattern) |
|---|
| 185 | collect (if (string= "!" (substring pat 0 1)) |
|---|
| 186 | (cons 'not (substring pat 1)) |
|---|
| 187 | (cons 'identity pat))))) |
|---|
| 188 | anything-mp-3-pattern-list) |
|---|
| 189 | (defun* anything-mp-3-match (str &optional (pattern anything-pattern)) |
|---|
| 190 | (loop for (pred . re) in (anything-mp-3-get-patterns pattern) |
|---|
| 191 | always (funcall pred (string-match re str)))) |
|---|
| 192 | |
|---|
| 193 | (defmacro anything-mp-3-search-base (searchfn1 searchfn2 b e) |
|---|
| 194 | `(loop with pat = (anything-mp-3-get-patterns pattern) |
|---|
| 195 | while (,searchfn1 (or (cdar pat) "") nil t) |
|---|
| 196 | for bol = (point-at-bol) |
|---|
| 197 | for eol = (point-at-eol) |
|---|
| 198 | if (loop |
|---|
| 199 | for (pred . s) in (cdr pat) |
|---|
| 200 | always (progn (goto-char ,b) |
|---|
| 201 | (funcall pred (,searchfn2 s ,e t)))) |
|---|
| 202 | do (goto-char ,e) (return t) |
|---|
| 203 | else do |
|---|
| 204 | (goto-char ,e) |
|---|
| 205 | finally (return nil))) |
|---|
| 206 | |
|---|
| 207 | (defun anything-mp-3-search (pattern &rest ignore) |
|---|
| 208 | (anything-mp-3-search-base re-search-forward re-search-forward bol eol)) |
|---|
| 209 | (defun anything-mp-3-search-backward (pattern &rest ignore) |
|---|
| 210 | (anything-mp-3-search-base re-search-backward re-search-backward eol bol)) |
|---|
| 211 | |
|---|
| 212 | ;; mp-3p- (multiple regexp pattern 3 with prefix search) |
|---|
| 213 | (defun* anything-mp-3p-match (str &optional (pattern anything-pattern)) |
|---|
| 214 | (destructuring-bind ((first-pred . first-re) . rest) |
|---|
| 215 | (anything-mp-3-get-patterns pattern) |
|---|
| 216 | (and (funcall first-pred (anything-prefix-match str first-re)) |
|---|
| 217 | (loop for (pred . re) in rest |
|---|
| 218 | always (funcall pred (string-match re str)))))) |
|---|
| 219 | (defun anything-mp-3p-search (pattern &rest ignore) |
|---|
| 220 | (anything-mp-3-search-base anything-prefix-search re-search-forward bol eol)) |
|---|
| 221 | |
|---|
| 222 | (defun anything-mp-3p-search-backward (pattern &rest ignore) |
|---|
| 223 | (anything-mp-3-search-base anything-prefix-search-backward re-search-backward eol bol)) |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | ;;;; source compier |
|---|
| 228 | (defvar anything-default-match-functions |
|---|
| 229 | '(anything-exact-match anything-mp-3p-match anything-mp-3-match)) |
|---|
| 230 | (defvar anything-default-search-functions |
|---|
| 231 | '(anything-exact-search anything-mp-3p-search anything-mp-3-search)) |
|---|
| 232 | (defvar anything-default-search-backward-functions |
|---|
| 233 | '(anything-exact-search-backward anything-mp-3p-search-backward anything-mp-3-search-backward)) |
|---|
| 234 | (defun anything-compile-source--match-plugin (source) |
|---|
| 235 | (let ((searchers (if (assoc 'search-from-end source) |
|---|
| 236 | anything-default-search-backward-functions |
|---|
| 237 | anything-default-search-functions))) |
|---|
| 238 | `(,(if (or (assoc 'candidates-in-buffer source) |
|---|
| 239 | (equal '(identity) (assoc-default 'match source))) |
|---|
| 240 | '(match identity) |
|---|
| 241 | `(match ,@anything-default-match-functions |
|---|
| 242 | ,@(assoc-default 'match source))) |
|---|
| 243 | (search ,@searchers |
|---|
| 244 | ,@(assoc-default 'search source)) |
|---|
| 245 | ,@source))) |
|---|
| 246 | |
|---|
| 247 | (add-to-list 'anything-compile-source-functions 'anything-compile-source--match-plugin t) |
|---|
| 248 | |
|---|
| 249 | ;;;; unit test |
|---|
| 250 | ;; (install-elisp "http://www.emacswiki.org/cgi-bin/wiki/download/el-expectations.el") |
|---|
| 251 | ;; (install-elisp "http://www.emacswiki.org/cgi-bin/wiki/download/el-mock.el") |
|---|
| 252 | (dont-compile |
|---|
| 253 | (when (fboundp 'expectations) |
|---|
| 254 | (expectations |
|---|
| 255 | (desc "amp-mp-make-regexps") |
|---|
| 256 | (expect '("") |
|---|
| 257 | (amp-mp-make-regexps "")) |
|---|
| 258 | (expect '("foo" "bar") |
|---|
| 259 | (amp-mp-make-regexps "foo bar")) |
|---|
| 260 | (expect '("foo" "bar") |
|---|
| 261 | (amp-mp-make-regexps " foo bar")) |
|---|
| 262 | (expect '("foo" "bar") |
|---|
| 263 | (amp-mp-make-regexps " foo bar ")) |
|---|
| 264 | (expect '("foo bar" "baz") |
|---|
| 265 | (let ((anything-mp-space-regexp "\\\\ ")) |
|---|
| 266 | (amp-mp-make-regexps "foo\\ bar baz"))) |
|---|
| 267 | (desc "anything-exact-match") |
|---|
| 268 | (expect (non-nil) |
|---|
| 269 | (anything-exact-match "thunder" "thunder")) |
|---|
| 270 | (expect nil |
|---|
| 271 | (anything-exact-match "thunder" "fire")) |
|---|
| 272 | (desc "anything-exact-search") |
|---|
| 273 | (expect (non-nil) |
|---|
| 274 | (with-temp-buffer |
|---|
| 275 | (insert "fire\nthunder\n") |
|---|
| 276 | (goto-char 1) |
|---|
| 277 | (anything-exact-search "thunder" nil t))) |
|---|
| 278 | (expect (non-nil) |
|---|
| 279 | (with-temp-buffer |
|---|
| 280 | (insert "\nfire\nthunder\n") |
|---|
| 281 | (goto-char 1) |
|---|
| 282 | (anything-exact-search "fire" nil t))) |
|---|
| 283 | (desc "anything-prefix-search") |
|---|
| 284 | (expect (non-nil) |
|---|
| 285 | (with-temp-buffer |
|---|
| 286 | (insert "fire\nthunder\n") |
|---|
| 287 | (goto-char (point-min)) |
|---|
| 288 | (anything-prefix-search "thund" nil t))) |
|---|
| 289 | (expect nil |
|---|
| 290 | (with-temp-buffer |
|---|
| 291 | (insert "fire\nthunder\n") |
|---|
| 292 | (goto-char (point-min)) |
|---|
| 293 | (anything-prefix-search "hund" nil t))) |
|---|
| 294 | (desc "anything-prefix-search-backward") |
|---|
| 295 | (expect (non-nil) |
|---|
| 296 | (with-temp-buffer |
|---|
| 297 | (insert "fire\nthunder\n") |
|---|
| 298 | (goto-char (point-max)) |
|---|
| 299 | (anything-prefix-search-backward "thund" nil t))) |
|---|
| 300 | (expect nil |
|---|
| 301 | (with-temp-buffer |
|---|
| 302 | (insert "fire\nthunder\n") |
|---|
| 303 | (goto-char (point-max)) |
|---|
| 304 | (anything-prefix-search-backward "hund" nil t))) |
|---|
| 305 | (desc "amp-mp-1-make-regexp") |
|---|
| 306 | (expect "a.*b" |
|---|
| 307 | (amp-mp-1-make-regexp "a b")) |
|---|
| 308 | (expect "a b" |
|---|
| 309 | (let ((anything-mp-space-regexp "\\\\ ")) |
|---|
| 310 | (amp-mp-1-make-regexp "a\\ b"))) |
|---|
| 311 | (expect "a.*b c" |
|---|
| 312 | (let ((anything-mp-space-regexp "\\\\ ")) |
|---|
| 313 | (amp-mp-1-make-regexp "a b\\ c"))) |
|---|
| 314 | (expect "" |
|---|
| 315 | (amp-mp-1-make-regexp "")) |
|---|
| 316 | (desc "anything-mp-1-search") |
|---|
| 317 | (expect (non-nil) |
|---|
| 318 | (with-temp-buffer |
|---|
| 319 | (insert "fire\nthunder\n") |
|---|
| 320 | (goto-char 1) |
|---|
| 321 | (anything-mp-1-search "th+ r" nil t))) |
|---|
| 322 | (desc "anything-mp-2-search") |
|---|
| 323 | (expect (non-nil) |
|---|
| 324 | (with-temp-buffer |
|---|
| 325 | (insert "fire\nthunder\n") |
|---|
| 326 | (goto-char 1) |
|---|
| 327 | (anything-mp-2-search "h+ r" nil t))) |
|---|
| 328 | (expect nil |
|---|
| 329 | (with-temp-buffer |
|---|
| 330 | (insert "fire\nthunder\n") |
|---|
| 331 | (goto-char 1) |
|---|
| 332 | (anything-mp-2-search "th+ r" nil t))) |
|---|
| 333 | (desc "anything-mp-3-search") |
|---|
| 334 | (expect (non-nil) |
|---|
| 335 | (with-temp-buffer |
|---|
| 336 | (insert "fire\nthunder\n") |
|---|
| 337 | (goto-char 1) |
|---|
| 338 | (anything-mp-3-search "h+ r" nil t))) |
|---|
| 339 | (expect (non-nil) |
|---|
| 340 | (with-temp-buffer |
|---|
| 341 | (insert "fire\nthunder\n") |
|---|
| 342 | (goto-char 1) |
|---|
| 343 | (anything-mp-3-search "th+ r" nil t))) |
|---|
| 344 | (expect (non-nil) |
|---|
| 345 | (with-temp-buffer |
|---|
| 346 | (insert "fire\nthunder\n") |
|---|
| 347 | (goto-char 1) |
|---|
| 348 | (anything-mp-3-search "r th+" nil t))) |
|---|
| 349 | (expect nil |
|---|
| 350 | (with-temp-buffer |
|---|
| 351 | (insert "fire\nthunder\n") |
|---|
| 352 | (goto-char 1) |
|---|
| 353 | (anything-mp-3-search "under hue" nil t))) |
|---|
| 354 | (expect (non-nil) |
|---|
| 355 | (with-temp-buffer |
|---|
| 356 | (insert "fire\nthunder\n") |
|---|
| 357 | (goto-char 1) |
|---|
| 358 | (anything-mp-3-search "r th+ n" nil t))) |
|---|
| 359 | (desc "anything-mp-3-search") |
|---|
| 360 | (expect (non-nil) |
|---|
| 361 | (with-temp-buffer |
|---|
| 362 | (insert "fire\nthunder\n") |
|---|
| 363 | (goto-char 1) |
|---|
| 364 | (anything-mp-3-search "th der" nil t))) |
|---|
| 365 | (expect nil |
|---|
| 366 | (with-temp-buffer |
|---|
| 367 | (insert "fire\nthunder\n") |
|---|
| 368 | (goto-char 1) |
|---|
| 369 | (anything-mp-3-search "th ders" nil t))) |
|---|
| 370 | (desc "anything-mp-3-search not") |
|---|
| 371 | (expect t |
|---|
| 372 | (with-temp-buffer |
|---|
| 373 | (insert "threshold\nthunder\n") |
|---|
| 374 | (goto-char 1) |
|---|
| 375 | (anything-mp-3-search "h !der" nil t))) |
|---|
| 376 | (expect t |
|---|
| 377 | (with-temp-buffer |
|---|
| 378 | (insert "threshold\nthunder\n") |
|---|
| 379 | (goto-char 1) |
|---|
| 380 | (anything-mp-3-search "th !der" nil t))) |
|---|
| 381 | (desc "anything-mp-3p-search") |
|---|
| 382 | (expect (non-nil) |
|---|
| 383 | (with-temp-buffer |
|---|
| 384 | (insert "fire\nthunder\n") |
|---|
| 385 | (goto-char 1) |
|---|
| 386 | (anything-mp-3p-search "th der" nil t))) |
|---|
| 387 | (expect nil |
|---|
| 388 | (with-temp-buffer |
|---|
| 389 | (insert "fire\nthunder\n") |
|---|
| 390 | (goto-char 1) |
|---|
| 391 | (anything-mp-3p-search "h ders" nil t))) |
|---|
| 392 | (desc "anything-mp-3p-search not") |
|---|
| 393 | (expect t |
|---|
| 394 | (with-temp-buffer |
|---|
| 395 | (insert "\nthreshold\nthunder\n") |
|---|
| 396 | (goto-char 1) |
|---|
| 397 | (anything-mp-3p-search "th !der" nil t))) |
|---|
| 398 | (expect nil |
|---|
| 399 | (with-temp-buffer |
|---|
| 400 | (insert "threshold\nthunder\n") |
|---|
| 401 | (goto-char 1) |
|---|
| 402 | (anything-mp-3p-search "h !der" nil t))) |
|---|
| 403 | (desc "anything-mp-3-search-backward") |
|---|
| 404 | (expect (non-nil) |
|---|
| 405 | (with-temp-buffer |
|---|
| 406 | (insert "fire\nthunder\n") |
|---|
| 407 | (goto-char (point-max)) |
|---|
| 408 | (anything-mp-3-search-backward "h der" nil t))) |
|---|
| 409 | (expect nil |
|---|
| 410 | (with-temp-buffer |
|---|
| 411 | (insert "fire\nthunder\n") |
|---|
| 412 | (goto-char (point-max)) |
|---|
| 413 | (anything-mp-3-search-backward "th ders" nil t))) |
|---|
| 414 | (desc "anything-mp-3-search-backward not") |
|---|
| 415 | (expect t |
|---|
| 416 | (with-temp-buffer |
|---|
| 417 | (insert "threshold\nthunder\n") |
|---|
| 418 | (goto-char (point-max)) |
|---|
| 419 | (anything-mp-3-search-backward "h !der" nil t))) |
|---|
| 420 | (expect t |
|---|
| 421 | (with-temp-buffer |
|---|
| 422 | (insert "threshold\nthunder\n") |
|---|
| 423 | (goto-char (point-max)) |
|---|
| 424 | (anything-mp-3-search-backward "th !der" nil t))) |
|---|
| 425 | (desc "anything-mp-3p-search-backward") |
|---|
| 426 | (expect (non-nil) |
|---|
| 427 | (with-temp-buffer |
|---|
| 428 | (insert "fire\nthunder\n") |
|---|
| 429 | (goto-char (point-max)) |
|---|
| 430 | (anything-mp-3p-search-backward "th der" nil t))) |
|---|
| 431 | (expect nil |
|---|
| 432 | (with-temp-buffer |
|---|
| 433 | (insert "fire\nthunder\n") |
|---|
| 434 | (goto-char (point-max)) |
|---|
| 435 | (anything-mp-3p-search-backward "h der" nil t))) |
|---|
| 436 | (desc "anything-mp-3p-search-backward not") |
|---|
| 437 | (expect t |
|---|
| 438 | (with-temp-buffer |
|---|
| 439 | (insert "\nthreshold\nthunder\n") |
|---|
| 440 | (goto-char (point-max)) |
|---|
| 441 | (anything-mp-3p-search-backward "th !der" nil t))) |
|---|
| 442 | (expect nil |
|---|
| 443 | (with-temp-buffer |
|---|
| 444 | (insert "threshold\nthunder\n") |
|---|
| 445 | (goto-char (point-max)) |
|---|
| 446 | (anything-mp-3p-search-backward "h !der" nil t))) |
|---|
| 447 | (desc "anything-mp-1-match") |
|---|
| 448 | (expect (non-nil) |
|---|
| 449 | (anything-mp-1-match "thunder" "th+ r")) |
|---|
| 450 | (desc "anything-mp-2-match") |
|---|
| 451 | (expect (non-nil) |
|---|
| 452 | (anything-mp-2-match "thunder" "h+ r")) |
|---|
| 453 | (expect nil |
|---|
| 454 | (anything-mp-2-match "thunder" "th+ r")) |
|---|
| 455 | (desc "anything-mp-3-match") |
|---|
| 456 | (expect (non-nil) |
|---|
| 457 | (anything-mp-3-match "thunder" "h+ r")) |
|---|
| 458 | (expect (non-nil) |
|---|
| 459 | (anything-mp-3-match "thunder" "th+ r")) |
|---|
| 460 | (expect (non-nil) |
|---|
| 461 | (anything-mp-3-match "thunder" "r th+")) |
|---|
| 462 | (expect nil |
|---|
| 463 | (anything-mp-3-match "thunder" "under hue")) |
|---|
| 464 | (expect (non-nil) |
|---|
| 465 | (anything-mp-3-match "thunder" "r th+ n")) |
|---|
| 466 | (desc "anything-mp-3-match not") |
|---|
| 467 | (expect (non-nil) |
|---|
| 468 | (anything-mp-3-match "threshold" "th !der")) |
|---|
| 469 | (desc "anything-prefix-match") |
|---|
| 470 | (expect (non-nil) |
|---|
| 471 | (anything-prefix-match "fobar" "fo")) |
|---|
| 472 | (expect nil |
|---|
| 473 | (anything-prefix-match "xfobar" "fo")) |
|---|
| 474 | |
|---|
| 475 | (desc "anything-mp-3-match") |
|---|
| 476 | (expect (non-nil) |
|---|
| 477 | (anything-mp-3-match "thunder" "h der")) |
|---|
| 478 | (expect nil |
|---|
| 479 | (anything-mp-3-match "thunder" "h ders")) |
|---|
| 480 | (desc "anything-mp-3p-match") |
|---|
| 481 | (expect (non-nil) |
|---|
| 482 | (anything-mp-3p-match "thunder" "th der")) |
|---|
| 483 | (expect nil |
|---|
| 484 | (anything-mp-3p-match "thunder" "h der")) |
|---|
| 485 | (desc "anything-mp-3p-match not") |
|---|
| 486 | (expect (non-nil) |
|---|
| 487 | (anything-mp-3p-match "threshold" "th !der")) |
|---|
| 488 | (expect nil |
|---|
| 489 | (anything-mp-3p-match "threshold" "h !der")) |
|---|
| 490 | (desc "with identity match") |
|---|
| 491 | (expect '(identity) |
|---|
| 492 | (assoc-default 'match |
|---|
| 493 | (car (anything-compile-sources |
|---|
| 494 | '(((name . "FOO") |
|---|
| 495 | (candidates-in-buffer))) |
|---|
| 496 | '(anything-compile-source--candidates-in-buffer |
|---|
| 497 | anything-compile-source--match-plugin))))) |
|---|
| 498 | (expect '(identity) |
|---|
| 499 | (assoc-default 'match |
|---|
| 500 | (car (anything-compile-sources |
|---|
| 501 | '(((name . "FOO") |
|---|
| 502 | (match identity))) |
|---|
| 503 | '(anything-compile-source--match-plugin))))) |
|---|
| 504 | (desc "functional") |
|---|
| 505 | (expect '(("FOO" ("thunder"))) |
|---|
| 506 | (anything-test-candidates '(((name . "FOO") |
|---|
| 507 | (candidates "fire" "thunder"))) |
|---|
| 508 | "th r" |
|---|
| 509 | '(anything-compile-source--match-plugin))) |
|---|
| 510 | (expect '(("FOO" ("one two"))) |
|---|
| 511 | (let ((anything-mp-space-regexp "\\\\ ")) |
|---|
| 512 | (anything-test-candidates '(((name . "FOO") |
|---|
| 513 | (candidates "one two" "three four"))) |
|---|
| 514 | "e\\ t" |
|---|
| 515 | '(anything-compile-source--match-plugin)))) |
|---|
| 516 | (expect '(("FOO" ("one two"))) |
|---|
| 517 | (let ((anything-mp-space-regexp " ")) |
|---|
| 518 | (anything-test-candidates '(((name . "FOO") |
|---|
| 519 | (candidates "one two" "three four"))) |
|---|
| 520 | "e t" |
|---|
| 521 | '(anything-compile-source--match-plugin)))) |
|---|
| 522 | (expect '(("FOO" ("thunder"))) |
|---|
| 523 | (anything-test-candidates '(((name . "FOO") |
|---|
| 524 | (init |
|---|
| 525 | . (lambda () |
|---|
| 526 | (with-current-buffer (anything-candidate-buffer 'global) |
|---|
| 527 | (insert "fire\nthunder\nthanks\n")))) |
|---|
| 528 | (candidates-in-buffer))) |
|---|
| 529 | "th r" |
|---|
| 530 | '(anything-compile-source--candidates-in-buffer |
|---|
| 531 | anything-compile-source--match-plugin))) |
|---|
| 532 | (expect '(("FOO" ("foo" "foobar"))) |
|---|
| 533 | (anything-test-candidates '(((name . "FOO") |
|---|
| 534 | (candidates "foobar" "foo"))) |
|---|
| 535 | "foo" |
|---|
| 536 | '(anything-compile-source--match-plugin))) |
|---|
| 537 | (expect '(("FOO" ("foo" "foobar"))) |
|---|
| 538 | (anything-test-candidates '(((name . "FOO") |
|---|
| 539 | (init |
|---|
| 540 | . (lambda () |
|---|
| 541 | (with-current-buffer (anything-candidate-buffer 'global) |
|---|
| 542 | (insert "foobar\nfoo\n")))) |
|---|
| 543 | (candidates-in-buffer))) |
|---|
| 544 | "foo" |
|---|
| 545 | '(anything-compile-source--candidates-in-buffer |
|---|
| 546 | anything-compile-source--match-plugin))) |
|---|
| 547 | (expect '(("FOO" ("foo"))) |
|---|
| 548 | (anything-test-candidates '(((name . "FOO") |
|---|
| 549 | (init |
|---|
| 550 | . (lambda () |
|---|
| 551 | (with-current-buffer (anything-candidate-buffer 'global) |
|---|
| 552 | (insert "foo\n")))) |
|---|
| 553 | (candidates-in-buffer))) |
|---|
| 554 | "foo" |
|---|
| 555 | '(anything-compile-source--candidates-in-buffer |
|---|
| 556 | anything-compile-source--match-plugin))) |
|---|
| 557 | (expect '(("FOO" ("foo"))) |
|---|
| 558 | (anything-test-candidates '(((name . "FOO") |
|---|
| 559 | (init |
|---|
| 560 | . (lambda () |
|---|
| 561 | (with-current-buffer (anything-candidate-buffer 'global) |
|---|
| 562 | (insert "bar\nfoo\ntest\n")))) |
|---|
| 563 | (candidates-in-buffer))) |
|---|
| 564 | "foo" |
|---|
| 565 | '(anything-compile-source--candidates-in-buffer |
|---|
| 566 | anything-compile-source--match-plugin))) |
|---|
| 567 | (expect '(("FOO" ("foobar" "foo"))) |
|---|
| 568 | (anything-test-candidates '(((name . "FOO") |
|---|
| 569 | (init |
|---|
| 570 | . (lambda () |
|---|
| 571 | (with-current-buffer (anything-candidate-buffer 'global) |
|---|
| 572 | (insert "foobar\nfoo\n")))) |
|---|
| 573 | (candidates-in-buffer))) |
|---|
| 574 | "" |
|---|
| 575 | '(anything-compile-source--candidates-in-buffer |
|---|
| 576 | anything-compile-source--match-plugin))) |
|---|
| 577 | (expect '(("FOO" ("foo" "foobar"))) |
|---|
| 578 | (anything-test-candidates '(((name . "FOO") |
|---|
| 579 | (init |
|---|
| 580 | . (lambda () |
|---|
| 581 | (with-current-buffer (anything-candidate-buffer 'global) |
|---|
| 582 | (insert "foobar\nfoo\n")))) |
|---|
| 583 | (candidates-in-buffer) |
|---|
| 584 | (search-from-end))) |
|---|
| 585 | "foo" |
|---|
| 586 | '(anything-compile-source--candidates-in-buffer |
|---|
| 587 | anything-compile-source--match-plugin))) |
|---|
| 588 | (expect '(("FOO" ("elisp" "elp"))) |
|---|
| 589 | (anything-test-candidates '(((name . "FOO") |
|---|
| 590 | (init |
|---|
| 591 | . (lambda () |
|---|
| 592 | (with-current-buffer (anything-candidate-buffer 'global) |
|---|
| 593 | (insert "elp\nelisp\n")))) |
|---|
| 594 | (candidates-in-buffer) |
|---|
| 595 | (search-from-end))) |
|---|
| 596 | "el p" |
|---|
| 597 | '(anything-compile-source--candidates-in-buffer |
|---|
| 598 | anything-compile-source--match-plugin))) |
|---|
| 599 | (expect '(("FOO" ("elisp" ))) |
|---|
| 600 | (anything-test-candidates '(((name . "FOO") |
|---|
| 601 | (init |
|---|
| 602 | . (lambda () |
|---|
| 603 | (with-current-buffer (anything-candidate-buffer 'global) |
|---|
| 604 | (insert "elp\nelisp\n")))) |
|---|
| 605 | (candidates-in-buffer) |
|---|
| 606 | (search-from-end))) |
|---|
| 607 | "el+ isp" |
|---|
| 608 | '(anything-compile-source--candidates-in-buffer |
|---|
| 609 | anything-compile-source--match-plugin))) |
|---|
| 610 | ;; prefix multi -> multi |
|---|
| 611 | (expect '(("FOO" ("elisp-info" "info.el"))) |
|---|
| 612 | (anything-test-candidates '(((name . "FOO") |
|---|
| 613 | (init |
|---|
| 614 | . (lambda () |
|---|
| 615 | (with-current-buffer (anything-candidate-buffer 'global) |
|---|
| 616 | (insert "info.el\nelisp-info\n")))) |
|---|
| 617 | (candidates-in-buffer) |
|---|
| 618 | )) |
|---|
| 619 | "el info" |
|---|
| 620 | '(anything-compile-source--candidates-in-buffer |
|---|
| 621 | anything-compile-source--match-plugin))) |
|---|
| 622 | ;; multi not |
|---|
| 623 | (expect '(("FOO" ("info.el"))) |
|---|
| 624 | (anything-test-candidates '(((name . "FOO") |
|---|
| 625 | (init |
|---|
| 626 | . (lambda () |
|---|
| 627 | (with-current-buffer (anything-candidate-buffer 'global) |
|---|
| 628 | (insert "info.el\nelisp-info\n")))) |
|---|
| 629 | (candidates-in-buffer) |
|---|
| 630 | )) |
|---|
| 631 | "info !elisp" |
|---|
| 632 | '(anything-compile-source--candidates-in-buffer |
|---|
| 633 | anything-compile-source--match-plugin))) |
|---|
| 634 | ))) |
|---|
| 635 | ;; (anything-compile-sources '(((name . "test"))) anything-compile-source-functions) |
|---|
| 636 | (provide 'anything-match-plugin) |
|---|
| 637 | |
|---|
| 638 | ;; How to save (DO NOT REMOVE!!) |
|---|
| 639 | ;; (emacswiki-post "anything-match-plugin.el") |
|---|
| 640 | ;;; anything-match-plugin.el ends here |
|---|