anything.el (anything-insert-match): reformat
[anything-config.git] / anything.el
blob0ddb73618f70354502c53642a709e855b4864334
1 ;;;; anything.el --- open anything / QuickSilver-like candidate-selection framework
3 ;; Copyright (C) 2007 Tamas Patrovics
4 ;; 2008, 2009, 2010 rubikitch <rubikitch@ruby-lang.org>
6 ;; Author: Tamas Patrovics
7 ;; Maintainer: rubikitch <rubikitch@ruby-lang.org>
8 ;; Keywords: files, frames, help, matching, outlines, processes, tools, convenience, anything
9 ;; URL: http://www.emacswiki.org/cgi-bin/wiki/download/anything.el
10 ;; Site: http://www.emacswiki.org/cgi-bin/emacs/Anything
11 (defvar anything-version nil)
12 (setq anything-version "1.285")
14 ;; This file is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; any later version.
19 ;; This file is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
28 ;; Boston, MA 02110-1301, USA.
30 ;;; Commentary:
33 ;; Start with M-x anything, narrow the list by typing some pattern,
34 ;; select with up/down/pgup/pgdown/C-p/C-n/C-v/M-v, choose with enter,
35 ;; left/right moves between sources. With TAB actions can be selected
36 ;; if the selected candidate has more than one possible action.
38 ;; Note that anything.el provides only the framework and some example
39 ;; configurations for demonstration purposes. See anything-config.el
40 ;; for practical, polished, easy to use configurations which can be
41 ;; used to assemble a custom personalized configuration. And many
42 ;; other configurations are in the EmacsWiki.
43 ;;
44 ;; http://www.emacswiki.org/cgi-bin/wiki/download/anything-config.el
45 ;; http://www.emacswiki.org/cgi-bin/emacs/AnythingSources
47 ;; Maintainer's configuration is in the EmacsWiki. It would tell you
48 ;; many tips to write smart sources!
50 ;; http://www.emacswiki.org/cgi-bin/emacs/RubikitchAnythingConfiguration
52 ;; Here is Japanese translation of `anything-sources' attributes. Thanks.
53 ;; http://d.hatena.ne.jp/sirocco634/20091012/1255336649
55 ;;; Bug Report:
57 ;; If you have problems, send a bug report via C-c C-x C-b in anything session (best)
58 ;; or M-x anything-send-bug-report outside anything session.
59 ;; I implemented bug report feature because I want to know your current state.
60 ;; It helps me to solve problems easily.
61 ;; The step is:
62 ;; 0) Setup mail in Emacs, the easiest way is:
63 ;; (setq user-mail-address "your@mail.address")
64 ;; (setq user-full-name "Your Full Name")
65 ;; (setq smtpmail-smtp-server "your.smtp.server.jp")
66 ;; (setq mail-user-agent 'message-user-agent)
67 ;; (setq message-send-mail-function 'message-smtpmail-send-it)
68 ;; 1) Be sure to use the LATEST version of anything.el.
69 ;; 2) Enable debugger. M-x toggle-debug-on-error or (setq debug-on-error t)
70 ;; 3) Use Lisp version instead of compiled one: (load "anything.el")
71 ;; 4) Do it!
72 ;; 5) If you got an error, please do not close *Backtrace* buffer.
73 ;; 6) Type C-c C-x C-b (anything session, best!)
74 ;; or M-x anything-send-bug-report (outside)
75 ;; then M-x insert-buffer *Backtrace* (if you got error)
76 ;; 7) Describe the bug using a precise recipe.
77 ;; 8) Type C-c C-c to send.
78 ;; # If you are a Japanese, please write in Japanese:-)
81 ;;; Commands:
83 ;; Below are complete command list:
85 ;; `anything'
86 ;; Select anything. In Lisp program, some optional arguments can be used.
87 ;; `anything-resume'
88 ;; Resurrect previously invoked `anything'.
89 ;; `anything-at-point'
90 ;; Same as `anything' except when C-u is pressed, the initial input is the symbol at point.
91 ;; `anything-force-update'
92 ;; Recalculate and update candidates.
93 ;; `anything-select-action'
94 ;; Select an action for the currently selected candidate.
95 ;; `anything-previous-line'
96 ;; Move selection to the previous line.
97 ;; `anything-next-line'
98 ;; Move selection to the next line.
99 ;; `anything-previous-page'
100 ;; Move selection back with a pageful.
101 ;; `anything-next-page'
102 ;; Move selection forward with a pageful.
103 ;; `anything-beginning-of-buffer'
104 ;; Move selection at the top.
105 ;; `anything-end-of-buffer'
106 ;; Move selection at the bottom.
107 ;; `anything-previous-source'
108 ;; Move selection to the previous source.
109 ;; `anything-next-source'
110 ;; Move selection to the next source.
111 ;; `anything-exit-minibuffer'
112 ;; Select the current candidate by exiting the minibuffer.
113 ;; `anything-help'
114 ;; Help of `anything'.
115 ;; `anything-debug-output'
116 ;; Show all anything-related variables at this time.
117 ;; `anything-delete-current-selection'
118 ;; Delete the currently selected item.
119 ;; `anything-delete-minibuffer-content'
120 ;; Same as `delete-minibuffer-contents' but this is a command.
121 ;; `anything-toggle-resplit-window'
122 ;; Toggle resplit anything window, vertically or horizontally.
123 ;; `anything-select-2nd-action'
124 ;; Select the 2nd action for the currently selected candidate.
125 ;; `anything-select-3rd-action'
126 ;; Select the 3rd action for the currently selected candidate.
127 ;; `anything-select-4th-action'
128 ;; Select the 4th action for the currently selected candidate.
129 ;; `anything-select-2nd-action-or-end-of-line'
130 ;; Select the 2nd action for the currently selected candidate if the point is at the end of minibuffer.
131 ;; `anything-execute-persistent-action'
132 ;; If a candidate is selected then perform the associated action without quitting anything.
133 ;; `anything-scroll-other-window'
134 ;; Scroll other window (not *Anything* window) upward.
135 ;; `anything-scroll-other-window-down'
136 ;; Scroll other window (not *Anything* window) downward.
137 ;; `anything-display-all-visible-marks'
138 ;; Show all `anything' visible marks strings.
139 ;; `anything-quit-and-find-file'
140 ;; Drop into `find-file' from `anything' like `iswitchb-find-file'.
141 ;; `anything-yank-selection'
142 ;; Set minibuffer contents to current selection.
143 ;; `anything-kill-selection-and-quit'
144 ;; Store current selection to kill ring.
145 ;; `anything-follow-mode'
146 ;; If this mode is on, persistent action is executed everytime the cursor is moved.
147 ;; `anything-migrate-sources'
148 ;; Help to migrate to new `anything' way.
149 ;; `anything-isearch'
150 ;; Start incremental search within results. (UNMAINTAINED)
151 ;; `anything-isearch-printing-char'
152 ;; Add printing char to the pattern.
153 ;; `anything-isearch-again'
154 ;; Search again for the current pattern
155 ;; `anything-isearch-delete'
156 ;; Undo last event.
157 ;; `anything-isearch-default-action'
158 ;; Execute the default action for the selected candidate.
159 ;; `anything-isearch-select-action'
160 ;; Choose an action for the selected candidate.
161 ;; `anything-isearch-cancel'
162 ;; Cancel Anything isearch.
163 ;; `anything-iswitchb-setup'
164 ;; Integrate anything completion into iswitchb (UNMAINTAINED).
165 ;; `anything-iswitchb-cancel-anything'
166 ;; Cancel anything completion and return to standard iswitchb.
167 ;; `anything-describe-anything-attribute'
168 ;; Display the full documentation of ANYTHING-ATTRIBUTE (a symbol).
169 ;; `anything-send-bug-report'
170 ;; Send a bug report of anything.el.
171 ;; `anything-send-bug-report-from-anything'
172 ;; Send a bug report of anything.el in anything session.
174 ;;; Customizable Options:
176 ;; Below are customizable option list:
179 ;; You can extend `anything' by writing plug-ins. As soon as
180 ;; `anything' is invoked, `anything-sources' is compiled into basic
181 ;; attributes, then compiled one is used during invocation.
183 ;; The oldest built-in plug-in is `type' attribute: appends
184 ;; appropriate element of `anything-type-attributes'. Second built-in
185 ;; plug-in is `candidates-in-buffer': selecting a line from candidates
186 ;; buffer.
188 ;; To write a plug-in:
189 ;; 1. Define a compiler: anything-compile-source--*
190 ;; 2. Add compier function to `anything-compile-source-functions'.
191 ;; 3. (optional) Write helper functions.
193 ;; Anything plug-ins are found in the EmacsWiki.
195 ;; http://www.emacswiki.org/cgi-bin/emacs/AnythingPlugins
197 ;; Tested on Emacs 22/23.
200 ;; Thanks to Vagn Johansen for ideas.
201 ;; Thanks to Stefan Kamphausen for fixes and XEmacs support.
202 ;; Thanks to Tassilo Horn for fixes.
203 ;; Thanks to Drew Adams for various fixes (frame, isearch, customization, etc.)
204 ;; Thanks to IMAKADO for candidates-in-buffer idea.
205 ;; Thanks to Tomohiro MATSUYAMA for multiline patch.
208 ;;; (@* "Index")
210 ;; If you have library `linkd.el', load
211 ;; `linkd.el' and turn on `linkd-mode' now. It lets you easily
212 ;; navigate around the sections Linkd mode will
213 ;; highlight this Index. You can get `linkd.el' here:
214 ;; http://www.emacswiki.org/cgi-bin/wiki/download/linkd.el
218 ;;; (@* "INCOMPATIBLE CHANGES")
220 ;; v1.277
222 ;; Default setting of `anything-save-configuration-functions' is changed.
223 ;; Anything saves/restores window configuration instead of frame configuration now.
224 ;; The default is changed because flickering is occurred in some environment.
226 ;; If you want to save and restore frame configuration, set this variable to
227 ;; '(set-frame-configuration . current-frame-configuration)
229 ;; v1.276
231 ;; Fitting frame is disabled by default, because some flickering occurred
232 ;; in some environment. To enable fitting, set both
233 ;; `anything-inhibit-fit-frame-flag' and `fit-frame-inhibit-fitting' to
234 ;; nil.
236 ;; v1.114
238 ;; `anything-attr' returns nil when the source attribute is defined
239 ;; but the value of attribute is nil, eg. (volatile) cell. Use
240 ;; `anything-attr-defined' when testing whether the attribute is
241 ;; defined.
243 ;;; (@* "Tips")
246 ;; `anything' accepts keyword arguments. See docstring.
247 ;; [EVAL IT] (describe-function 'anything)
250 ;; `anything-enable-shortcuts' enables us to select candidate easily.
251 ;; If 'prefix then they can be selected using <prefix-key> <alnum>.
252 ;; The prefix key is `anything-select-with-prefix-shortcut'.
253 ;; If the <prefix-key> is a letter, pressing twice inputs the letter itself.
254 ;; e.g.
255 ;; (setq anything-enable-shortcuts 'prefix)
256 ;; (define-key anything-map \"@\" 'anything-select-with-prefix-shortcut)
259 ;; You can edit current selection using `anything-edit-current-selection'.
260 ;; It is useful after persistent-action.
263 ;; For `anything' users, setting `anything-sources' directly and
264 ;; invoke M-x anything is obsolete way for now. Try M-x
265 ;; `anything-migrate-sources'!
268 ;; If you want to create anything sources, yasnippet would help you.
269 ;; http://yasnippet.googlecode.com/
271 ;; Then get the snippet from
272 ;; http://www.emacswiki.org/cgi-bin/wiki/download/anything-source.yasnippet
274 ;; Put it in ~/.emacs.d/plugins/yasnippet/snippets/text-mode/emacs-lisp-mode/
278 ;; `anything-interpret-value' is useful function to interpret value
279 ;; like `candidates' attribute.
281 ;; (anything-interpret-value "literal") ; => "literal"
282 ;; (anything-interpret-value (lambda () "lambda")) ; => "lambda"
283 ;; (let ((source '((name . "lambda with source name"))))
284 ;; (anything-interpret-value
285 ;; (lambda () anything-source-name)
286 ;; source)) ; => "lambda with source name"
287 ;; (flet ((f () "function symbol"))
288 ;; (anything-interpret-value 'f)) ; => "function symbol"
289 ;; (let ((v "variable symbol"))
290 ;; (anything-interpret-value 'v)) ; => "variable symbol"
291 ;; (anything-interpret-value 'unbounded-1) ; error
294 ;; Now symbols are acceptable as candidates. So you do not have to use
295 ;; `symbol-name' function. The source is much simpler. For example,
296 ;; `apropos-internal' returns a list of symbols.
298 ;; (anything
299 ;; '(((name . "Commands")
300 ;; (candidates . (lambda () (apropos-internal anything-pattern 'commandp)))
301 ;; (volatile)
302 ;; (action . describe-function))))
305 ;; To mark a candidate, press C-SPC as normal Emacs marking. To go to
306 ;; marked candidate, press M-[ or M-].
309 ;; `anything-map' is now Emacs-standard key bindings by default. If
310 ;; you are using `iswitchb', execute `anything-iswitchb-setup'. Then
311 ;; some key bindings are adjusted to `iswitchb'. Note that
312 ;; anything-iswitchb is not maintained.
315 ;; There are many `anything' applications, using `anything' for
316 ;; selecting candidate. In this case, if there is one candidate or no
317 ;; candidate, popping up *anything* buffer is irritating. If one
318 ;; candidate, you want to select it at once. If no candidate, you want
319 ;; to quit `anything'. Set `anything-execute-action-at-once-if-one'
320 ;; and `anything-quit-if-no-candidate' to non-nil to remedy it. Note
321 ;; that setting these variables GLOBALLY is bad idea because of
322 ;; delayed sources. These are meant to be let-binded.
323 ;; See anything-etags.el for example.
325 ;; [EVAL IT] (install-elisp "http://www.emacswiki.org/cgi-bin/wiki/download/anything-etags.el")
327 ;; ex.
328 ;; (let ((anything-execute-action-at-once-if-one t)
329 ;; (anything-quit-if-no-candidate (lambda () (message "No candidate"))))
330 ;; (anything temporary-sources input))
333 ;; `set-frame-configuration' arises flickering. If you hate
334 ;; flickering, eval:
335 ;; (setq anything-save-configuration-functions
336 ;; '(set-window-configuration . current-window-configuration))
337 ;; at the cost of restoring frame configuration (only window configuration).
340 ;; `anything-delete-current-selection' deletes the current line.
341 ;; It is useful when deleting a candidate in persistent action.
342 ;; eg. `kill-buffer'.
344 ;; [EVAL IT] (describe-function 'anything-delete-current-selection)
347 ;; `anything-attr' gets the attribute. `anything-attrset' sets the
348 ;; attribute. `anything-attr-defined' tests whether the attribute is
349 ;; defined. They handles source-local variables.
351 ;; [EVAL IT] (describe-function 'anything-attr)
352 ;; [EVAL IT] (describe-function 'anything-attrset)
353 ;; [EVAL IT] (describe-function 'anything-attr-defined)
356 ;; `anything-sources' accepts many attributes to make your life easier.
357 ;; Now `anything-sources' accepts a list of symbols.
359 ;; [EVAL IT] (describe-variable 'anything-sources)
362 ;; `anything' has optional arguments. Now you do not have to let-bind
363 ;; `anything-sources'.
365 ;; [EVAL IT] (describe-function 'anything)
368 ;; `anything-resume' resumes last `anything' session. Now you do not
369 ;; have to retype pattern.
371 ;; [EVAL IT] (describe-function 'anything-resume)
374 ;; `anything-execute-persistent-action' executes action without
375 ;; quitting `anything'. When popping up a buffer in other window by
376 ;; persistent action, you can scroll with `anything-scroll-other-window' and
377 ;; `anything-scroll-other-window-down'. See also `anything-sources' docstring.
379 ;; [EVAL IT] (describe-function 'anything-execute-persistent-action)
380 ;; [EVAL IT] (describe-variable 'anything-sources)
383 ;; `anything-select-2nd-action', `anything-select-3rd-action' and
384 ;; `anything-select-4th-action' select other than default action
385 ;; without pressing Tab.
388 ;; Using `anything-candidate-buffer' and the candidates-in-buffer
389 ;; attribute is much faster than traditional "candidates and match"
390 ;; way. And `anything-current-buffer-is-modified' avoids to
391 ;; recalculate candidates for unmodified buffer. See docstring of
392 ;; them.
394 ;; [EVAL IT] (describe-function 'anything-candidate-buffer)
395 ;; [EVAL IT] (describe-function 'anything-candidates-in-buffer)
396 ;; [EVAL IT] (describe-function 'anything-current-buffer-is-modified)
399 ;; `anything-current-buffer' and `anything-buffer-file-name' stores
400 ;; `(current-buffer)' and `buffer-file-name' in the buffer `anything'
401 ;; is invoked. Use them freely.
403 ;; [EVAL IT] (describe-variable 'anything-current-buffer)
404 ;; [EVAL IT] (describe-variable 'anything-buffer-file-name)
407 ;; `anything-completing-read' and `anything-read-file-name' are
408 ;; experimental implementation. If you are curious, type M-x
409 ;; anything-read-string-mode. It is a minor mode and toggles on/off.
412 ;; Use `anything-test-candidates' to test your handmade anything
413 ;; sources. It simulates contents of *anything* buffer with pseudo
414 ;; `anything-sources' and `anything-pattern', without side-effect. So
415 ;; you can unit-test your anything sources! Let's TDD!
417 ;; [EVAL IT] (describe-function 'anything-test-candidates)
419 ;; There are many unit-testing framework in Emacs Lisp. See the EmacsWiki.
420 ;; http://www.emacswiki.org/cgi-bin/emacs/UnitTesting
422 ;; There is an unit-test by Emacs Lisp Expectations at the tail of this file.
423 ;; http://www.emacswiki.org/cgi-bin/wiki/download/el-expectations.el
424 ;; http://www.emacswiki.org/cgi-bin/wiki/download/el-mock.el
427 ;; If you want to create anything sources, see anything-config.el.
428 ;; It is huge collection of sources. You can learn from examples.
431 ;; (@* "TODO")
433 ;; - process status indication
435 ;; - async sources doesn't honor digit-shortcut-count
437 ;; - anything-candidate-number-limit can't be nil everywhere
439 ;; (@* "HISTORY")
441 ;; Change log of this file is found at
442 ;; http://repo.or.cz/w/anything-config.git/history/master:/anything.el
444 ;; Change log of this project is found at
445 ;; http://repo.or.cz/w/anything-config.git?a=shortlog
447 (require 'cl)
448 ;; (require 'anything-match-plugin nil t)
450 ;; (@* "User Configuration")
452 ;; This is only an example. Customize it to your own taste!
453 (defvar anything-sources `(((name . "Buffers")
454 (candidates
455 . (lambda ()
456 (remove-if (lambda (name)
457 (or (equal name anything-buffer)
458 (eq ?\ (aref name 0))))
459 (mapcar 'buffer-name (buffer-list)))))
460 (type . buffer))
462 ((name . "File Name History")
463 (candidates . file-name-history)
464 (match (lambda (candidate)
465 ;; list basename matches first
466 (string-match
467 anything-pattern
468 (file-name-nondirectory candidate)))
470 (lambda (candidate)
471 ;; and then directory part matches
472 (let ((dir (file-name-directory candidate)))
473 (if dir
474 (string-match anything-pattern dir)))))
475 (type . file))
477 ((name . "Files from Current Directory")
478 (init . (lambda ()
479 (setq anything-default-directory
480 default-directory)))
481 (candidates . (lambda ()
482 (directory-files
483 anything-default-directory)))
484 (type . file))
486 ((name . "Complex Command History")
487 (candidates . (lambda ()
488 (mapcar 'prin1-to-string
489 command-history)))
490 (action . (("Repeat Complex Command" .
491 (lambda (c)
492 (eval (read c))))))
493 (delayed)))
494 "The source of candidates for anything.
495 It accepts symbols:
496 (setq anything-sources (list anything-c-foo anything-c-bar))
497 can be written as
498 (setq anything-sources '(anything-c-foo anything-c-bar))
499 The latter is recommended because if you change anything-c-* variable,
500 you do not have to update `anything-sources'.
502 You are STRONGLY recommended to define a command which calls
503 `anything' or `anything-other-buffer' with argument rather than
504 to set `anything-sources' externally.
506 If you want to change `anything-sources' during `anything' invocation,
507 use `anything-set-sources', never use `setq'.
509 Attributes:
514 ;; This value is only provided as an example. Customize it to your own
515 ;; taste!
516 (defvar anything-type-attributes
517 '((file (action . (("Find File" . find-file)
518 ("Delete File" . (lambda (file)
519 (if (y-or-n-p (format "Really delete file %s? "
520 file))
521 (delete-file file)))))))
522 (buffer (action . (("Switch to Buffer" . switch-to-buffer)
523 ("Pop to Buffer" . pop-to-buffer)
524 ("Display Buffer" . display-buffer)
525 ("Kill Buffer" . kill-buffer)))))
526 "It's a list of (TYPE ATTRIBUTES ...). ATTRIBUTES are the same
527 as attributes for `anything-sources'. TYPE connects the value
528 to the appropriate sources in `anything-sources'.
530 This allows specifying common attributes for several
531 sources. For example, sources which provide files can specify
532 common attributes with a `file' type.")
535 (defvaralias 'anything-enable-digit-shortcuts 'anything-enable-shortcuts
536 "Alphabet shortcuts are usable now. Then `anything-enable-digit-shortcuts' should be renamed.
537 `anything-enable-digit-shortcuts' is retained for compatibility.")
538 (defvar anything-enable-shortcuts nil
539 "*Whether to use digit/alphabet shortcut to select the first nine matches.
540 If t then they can be selected using Ctrl+<number>.
542 If 'prefix then they can be selected using <prefix-key> <alnum>.
543 The prefix key is `anything-select-with-prefix-shortcut'.
544 If the <prefix-key> is a letter, pressing twice inputs the letter itself.
545 e.g.
546 (setq anything-enable-shortcuts 'prefix)
547 (define-key anything-map \"@\" 'anything-select-with-prefix-shortcut)
549 If 'alphabet then they can be selected using Shift+<alphabet> (deprecated).
550 It is not recommended because you cannot input capital letters in pattern.
552 Keys (digit/alphabet) are listed in `anything-shortcut-keys-alist'.")
554 (defvar anything-shortcut-keys-alist
555 '((alphabet . "asdfghjklzxcvbnmqwertyuiop")
556 (prefix . "asdfghjklzxcvbnmqwertyuiop1234567890")
557 (t . "123456789")))
559 (defvar anything-display-source-at-screen-top t
560 "*If t, `anything-next-source' and `anything-previous-source'
561 display candidates at the top of screen.")
563 (defvar anything-candidate-number-limit 50
564 "*Do not show more candidates than this limit from individual
565 sources. It is usually pointless to show hundreds of matches
566 when the pattern is empty, because it is much simpler to type a
567 few characters to narrow down the list of potential candidates.
569 Set it to nil if you don't want this limit.")
572 (defvar anything-idle-delay 0.5
573 "*The user has to be idle for this many seconds, before
574 candidates from delayed sources are collected. This is useful
575 for sources involving heavy operations (like launching external
576 programs), so that candidates from the source are not retrieved
577 unnecessarily if the user keeps typing.
579 It also can be used to declutter the results anything displays,
580 so that results from certain sources are not shown with every
581 character typed, only if the user hesitates a bit.")
584 (defvar anything-input-idle-delay 0.1
585 "The user has to be idle for this many seconds, before ALL candidates are collected.
586 Unlink `anything-input-idle', it is also effective for non-delayed sources.
587 If nil, candidates are collected immediately. ")
590 (defvar anything-samewindow nil
591 "If t then Anything doesn't pop up a new window, it uses the
592 current window to show the candidates.")
595 (defvar anything-source-filter nil
596 "A list of source names to be displayed. Other sources won't
597 appear in the search results. If nil then there is no filtering.
598 See also `anything-set-source-filter'.")
601 (defvar anything-map
602 (let ((map (copy-keymap minibuffer-local-map)))
603 (define-key map (kbd "<down>") 'anything-next-line)
604 (define-key map (kbd "<up>") 'anything-previous-line)
605 (define-key map (kbd "C-n") 'anything-next-line)
606 (define-key map (kbd "C-p") 'anything-previous-line)
607 (define-key map (kbd "<prior>") 'anything-previous-page)
608 (define-key map (kbd "<next>") 'anything-next-page)
609 (define-key map (kbd "M-v") 'anything-previous-page)
610 (define-key map (kbd "C-v") 'anything-next-page)
611 (define-key map (kbd "M-<") 'anything-beginning-of-buffer)
612 (define-key map (kbd "M->") 'anything-end-of-buffer)
613 (define-key map (kbd "<right>") 'anything-next-source)
614 (define-key map (kbd "<left>") 'anything-previous-source)
615 (define-key map (kbd "<RET>") 'anything-exit-minibuffer)
616 (define-key map (kbd "C-1") 'anything-select-with-digit-shortcut)
617 (define-key map (kbd "C-2") 'anything-select-with-digit-shortcut)
618 (define-key map (kbd "C-3") 'anything-select-with-digit-shortcut)
619 (define-key map (kbd "C-4") 'anything-select-with-digit-shortcut)
620 (define-key map (kbd "C-5") 'anything-select-with-digit-shortcut)
621 (define-key map (kbd "C-6") 'anything-select-with-digit-shortcut)
622 (define-key map (kbd "C-7") 'anything-select-with-digit-shortcut)
623 (define-key map (kbd "C-8") 'anything-select-with-digit-shortcut)
624 (define-key map (kbd "C-9") 'anything-select-with-digit-shortcut)
625 (loop for c from ?A to ?Z do
626 (define-key map (make-string 1 c) 'anything-select-with-digit-shortcut))
627 (define-key map (kbd "C-i") 'anything-select-action)
628 (define-key map (kbd "C-z") 'anything-execute-persistent-action)
629 (define-key map (kbd "C-e") 'anything-select-2nd-action-or-end-of-line)
630 (define-key map (kbd "C-j") 'anything-select-3rd-action)
631 (define-key map (kbd "C-o") 'anything-next-source)
632 (define-key map (kbd "C-M-v") 'anything-scroll-other-window)
633 (define-key map (kbd "M-<next>") 'anything-scroll-other-window)
634 (define-key map (kbd "C-M-y") 'anything-scroll-other-window-down)
635 (define-key map (kbd "C-M-S-v") 'anything-scroll-other-window-down)
636 (define-key map (kbd "M-<prior>") 'anything-scroll-other-window-down)
637 (define-key map (kbd "C-SPC") 'anything-toggle-visible-mark)
638 (define-key map (kbd "M-[") 'anything-prev-visible-mark)
639 (define-key map (kbd "M-]") 'anything-next-visible-mark)
640 (define-key map (kbd "C-k") 'anything-delete-minibuffer-content)
642 (define-key map (kbd "C-s") 'anything-isearch)
643 (define-key map (kbd "C-r") 'undefined)
644 (define-key map (kbd "C-t") 'anything-toggle-resplit-window)
645 (define-key map (kbd "C-x C-f") 'anything-quit-and-find-file)
647 (define-key map (kbd "C-c C-d") 'anything-delete-current-selection)
648 (define-key map (kbd "C-c C-y") 'anything-yank-selection)
649 (define-key map (kbd "C-c C-k") 'anything-kill-selection-and-quit)
650 (define-key map (kbd "C-c C-f") 'anything-follow-mode)
651 (define-key map (kbd "C-c C-u") 'anything-force-update)
653 ;; Debugging command
654 (define-key map "\C-c\C-x\C-d" 'anything-debug-output)
655 (define-key map "\C-c\C-x\C-m" 'anything-display-all-visible-marks)
656 (define-key map "\C-c\C-x\C-b" 'anything-send-bug-report-from-anything)
657 ;; Use `describe-mode' key in `global-map'
658 (dolist (k (where-is-internal 'describe-mode global-map))
659 (define-key map k 'anything-help))
660 ;; the defalias is needed because commands are bound by name when
661 ;; using iswitchb, so only commands having the prefix anything-
662 ;; get rebound
663 (defalias 'anything-previous-history-element 'previous-history-element)
664 (defalias 'anything-next-history-element 'next-history-element)
665 (define-key map (kbd "M-p") 'anything-previous-history-element)
666 (define-key map (kbd "M-n") 'anything-next-history-element)
667 map)
668 "Keymap for anything.
670 If you execute `anything-iswitchb-setup', some keys are modified.
671 See `anything-iswitchb-setup-keys'.")
673 (defvar anything-isearch-map
674 (let ((map (make-sparse-keymap)))
675 (set-keymap-parent map (current-global-map))
676 (define-key map (kbd "<return>") 'anything-isearch-default-action)
677 (define-key map (kbd "<RET>") 'anything-isearch-default-action)
678 (define-key map (kbd "C-i") 'anything-isearch-select-action)
679 (define-key map (kbd "C-g") 'anything-isearch-cancel)
680 (define-key map (kbd "M-s") 'anything-isearch-again)
681 (define-key map (kbd "<backspace>") 'anything-isearch-delete)
682 ;; add printing chars
683 (loop for i from 32 below 256 do
684 (define-key map (vector i) 'anything-isearch-printing-char))
685 map)
686 "Keymap for anything incremental search.")
689 (defgroup anything nil
690 "Open anything." :prefix "anything-" :group 'convenience)
692 (defface anything-header
693 '((t (:inherit header-line)))
694 "Face for header lines in the anything buffer." :group 'anything)
696 (defvar anything-header-face 'anything-header
697 "Face for header lines in the anything buffer.")
699 (defface anything-isearch-match '((t (:background "Yellow")))
700 "Face for isearch in the anything buffer." :group 'anything)
702 (defvar anything-isearch-match-face 'anything-isearch-match
703 "Face for matches during incremental search.")
705 (defvar anything-selection-face 'highlight
706 "Face for currently selected item.")
708 (defvar anything-iswitchb-idle-delay 1
709 "Show anything completions if the user is idle that many
710 seconds after typing.")
712 (defvar anything-iswitchb-dont-touch-iswithcb-keys nil
713 "If t then those commands are not bound from `anything-map'
714 under iswitchb which would override standard iswithcb keys.
716 This allows an even more seamless integration with iswitchb for
717 those who prefer using iswitchb bindings even if the anything
718 completions buffer is popped up.
720 Note that you can bind alternative keys for the same command in
721 `anything-map', so that you can use different keys for anything
722 under iswitchb. For example, I bind the character \ to
723 `anything-exit-minibuffer' which key is just above Enter on my
724 keyboard. This way I can switch buffers with Enter and choose
725 anything completions with \.")
727 ;;----------------------------------------------------------------------
729 (defvar anything-buffer "*anything*"
730 "Buffer showing completions.")
732 (defvar anything-action-buffer "*anything action*"
733 "Buffer showing actions.")
735 (defvar anything-selection-overlay nil
736 "Overlay used to highlight the currently selected item.")
738 (defvar anything-isearch-overlay nil
739 "Overlay used to highlight the current match during isearch.")
741 (defvar anything-digit-overlays nil
742 "Overlays for digit shortcuts. See `anything-enable-shortcuts'.")
744 (defvar anything-candidate-cache nil
745 "Holds the available candidate withing a single anything invocation.")
747 (defvar anything-pattern
748 "The input pattern used to update the anything buffer.")
750 (defvar anything-input
751 "The input typed in the candidates panel.")
753 (defvar anything-async-processes nil
754 "List of information about asynchronous processes managed by anything.")
756 (defvar anything-digit-shortcut-count 0
757 "Number of digit shortcuts shown in the anything buffer.")
759 (defvar anything-before-initialize-hook nil
760 "Run before anything initialization.
761 This hook is run before init functions in `anything-sources'.")
763 (defvar anything-after-initialize-hook nil
764 "Run after anything initialization.
765 Global variables are initialized and the anything buffer is created.
766 But the anything buffer has no contents. ")
768 (defvar anything-update-hook nil
769 "Run after the anything buffer was updated according the new input pattern.
770 This hook is run at the beginning of buffer.
771 The first candidate is selected after running this hook.
772 See also `anything-after-update-hook'.")
774 (defvar anything-after-update-hook nil
775 "Run after the anything buffer was updated according the new input pattern.
776 This is very similar to `anything-update-hook' but selection is not moved.
777 It is useful to select a particular object instead of the first one. ")
779 (defvar anything-cleanup-hook nil
780 "Run after anything minibuffer is closed, IOW this hook is executed BEFORE performing action. ")
782 (defvar anything-after-action-hook nil
783 "Run after executing action.")
785 (defvar anything-after-persistent-action-hook nil
786 "Run after executing persistent action.")
788 (defvar anything-restored-variables
789 '( anything-candidate-number-limit
790 anything-source-filter
791 anything-source-in-each-line-flag
792 anything-map
793 anything-sources
794 deferred-action-list)
795 "Variables which are restored after `anything' invocation.")
796 ;; `anything-saved-sources' is removed
798 (defvar anything-saved-selection nil
799 "Saved value of the currently selected object when the action
800 list is shown.")
802 ;; `anything-original-source-filter' is removed
804 (defvar anything-candidate-separator
805 "--------------------"
806 "Candidates separator of `multiline' source.")
808 (defvar anything-current-buffer nil
809 "Current buffer when `anything' is invoked.")
811 (defvar anything-buffer-file-name nil
812 "`buffer-file-name' when `anything' is invoked.")
814 (defvar anything-saved-action nil
815 "Saved value of the currently selected action by key.")
817 (defvar anything-last-sources nil
818 "OBSOLETE!! Sources of previously invoked `anything'.")
820 (defvar anything-saved-current-source nil
821 "Saved value of the original (anything-get-current-source) when the action
822 list is shown.")
824 (defvar anything-compiled-sources nil
825 "Compiled version of `anything-sources'. ")
827 (defvar anything-in-persistent-action nil
828 "Flag whether in persistent-action or not.")
830 (defvar anything-quick-update nil
831 "If non-nil, suppress displaying sources which are out of screen at first.
832 They are treated as delayed sources at this input.
833 This flag makes `anything' a bit faster with many sources.")
835 (defvar anything-last-sources-local nil
836 "Buffer local value of `anything-sources'.")
837 (defvar anything-last-buffer nil
838 "`anything-buffer' of previously `anything' session.")
840 (defvar anything-save-configuration-functions
841 '(set-window-configuration . current-window-configuration)
842 "If you want to save and restore frame configuration, set this variable to
843 '(set-frame-configuration . current-frame-configuration)
845 Older version saves/restores frame configuration, but the default is changed now,
846 because flickering is occurred in some environment.
849 (defvar anything-persistent-action-use-special-display nil
850 "If non-nil, use `special-display-function' in persistent action.")
852 (defvar anything-execute-action-at-once-if-one nil
853 "If non-nil and there is one candidate, execute the first action without selection.
854 It is useful for `anything' applications.")
856 (defvar anything-quit-if-no-candidate nil
857 "if non-nil and there is no candidate, do not display *anything* buffer and quit.
858 This variable accepts a function, which is executed if no candidate.
860 It is useful for `anything' applications.")
862 (defvar anything-scroll-amount nil
863 "Scroll amount used by `anything-scroll-other-window' and `anything-scroll-other-window-down'.
864 If you prefer scrolling line by line, set this value to 1.")
866 (defvar anything-display-function 'anything-default-display-buffer
867 "Function to display *anything* buffer.
868 It is `anything-default-display-buffer' by default, which affects `anything-samewindow'.")
870 (defvar anything-delayed-init-executed nil)
872 (defvar anything-mode-line-string "\\<anything-map>\\[anything-help]:help \\[anything-select-action]:Acts \\[anything-exit-minibuffer]/\\[anything-select-2nd-action-or-end-of-line]/\\[anything-select-3rd-action]:NthAct \\[anything-send-bug-report-from-anything]:BugReport"
873 "Help string displayed in mode-line in `anything'.
874 If nil, use default `mode-line-format'.")
876 (defvar anything-help-message
877 "\\<anything-map>The keys that are defined for `anything' are:
878 \\{anything-map}"
879 "Detailed help message string for `anything'.
880 It also accepts function or variable symbol.")
882 (put 'anything 'timid-completion 'disabled)
884 (defvar anything-inhibit-fit-frame-flag t
885 "If non-nil, inhibit fitting anything frame to its buffer.
886 It is nil by default because some flickering occurred in some environment.
888 To enable fitting, set both `anything-inhibit-fit-frame-flag' and
889 `fit-frame-inhibit-fitting' to nil.")
891 (defvar anything-source-in-each-line-flag nil
892 "If non-nil, add anything-source text-property in each candidate.
893 experimental feature.")
895 (defvaralias 'anything-debug-variables 'anything-debug-forms)
896 (defvar anything-debug-forms nil
897 "Forms to show in `anything-debug-output'.
898 Otherwise all variables started with `anything-' are shown.
899 It is useful for debug.")
901 (defvar anything-debug nil
902 "If non-nil, write log message into *Anything Log* buffer.
903 If `debug-on-error' is non-nil, write log message regardless of this variable.
904 It is disabled by default because *Anything Log* grows quickly.")
906 ;; (@* "Internal Variables")
907 (defvar anything-test-candidate-list nil)
908 (defvar anything-test-mode nil)
909 (defvar anything-source-name nil)
910 (defvar anything-candidate-buffer-alist nil)
911 (defvar anything-check-minibuffer-input-timer nil)
912 (defvar anything-match-hash (make-hash-table :test 'equal))
913 (defvar anything-cib-hash (make-hash-table :test 'equal))
914 (defvar anything-tick-hash (make-hash-table :test 'equal))
915 (defvar anything-issued-errors nil)
916 (defvar anything-shortcut-keys nil)
917 (defvar anything-once-called-functions nil)
918 (defvar anything-follow-mode nil)
919 (defvar anything-let-variables nil)
921 ;; (@* "Utility: logging")
922 (defun anything-log (format-string &rest args)
923 "Log message if `debug-on-error' or `anything-debug' is non-nil.
924 Messages are written to the *Anythingn Log* buffer.
925 Arguments are same as `format'."
926 (when (or debug-on-error anything-debug)
927 (with-current-buffer (get-buffer-create "*Anything Log*")
928 (buffer-disable-undo)
929 (set (make-local-variable 'inhibit-read-only) t)
930 (goto-char (point-max))
931 (insert (let ((tm (current-time)))
932 (format "%s.%06d (%s) %s\n"
933 (format-time-string "%H:%M:%S" tm)
934 (nth 2 tm)
935 (anything-log-get-current-function)
936 (apply #'format (cons format-string args))))))))
937 (defmacro anything-log-eval (&rest exprs)
938 "Write each EXPR evaluation result to the *Anything Log* buffer."
939 `(anything-log-eval-internal ',exprs))
940 (defun anything-log-run-hook (hook)
941 (anything-log "executing %s" hook)
942 (when (boundp hook)
943 (anything-log-eval (symbol-value hook))
944 (anything-log-eval (default-value hook)))
945 (run-hooks hook)
946 (anything-log "executed %s" hook))
947 (defun anything-log-eval-internal (exprs)
948 (dolist (expr exprs)
949 (condition-case err
950 (anything-log "%S = %S" expr (eval expr))
951 (error (anything-log "%S = ERROR!" expr)))))
952 (defun anything-log-get-current-function ()
953 "Get function name calling `anything-log'.
954 The original idea is from `tramp-debug-message'."
955 (loop for btn from 1 to 40 ;avoid inf-loop
956 for btf = (second (backtrace-frame btn))
957 for fn = (if (symbolp btf) (symbol-name btf) "")
958 do (when (and (string-match "^anything" fn)
959 (not (string-match "^anything-\\(?:interpret\\|log\\|.*funcall\\)" fn)))
960 (return fn))))
962 ;; (anything-log "test")
963 ;; (switch-to-buffer-other-window "*Anything Log*")
965 ;; (@* "Programming Tools")
966 (defmacro anything-aif (test-form then-form &rest else-forms)
967 "Anaphoric if. Temporary variable `it' is the result of test-form."
968 `(let ((it ,test-form))
969 (if it ,then-form ,@else-forms)))
970 (put 'anything-aif 'lisp-indent-function 2)
972 (defun anything-mklist (obj)
973 "If OBJ is a list (but not lambda), return itself, otherwise make a list with one element."
974 (if (and (listp obj) (not (functionp obj)))
976 (list obj)))
978 ;; (@* "Anything API")
979 (defmacro anything-let (varlist &rest body)
980 "[OBSOLETE] Like `let'. Bind anything buffer local variables according to VARLIST then eval BODY."
981 `(anything-let-internal (anything-let-eval-varlist ',varlist)
982 (lambda () ,@body)))
983 (put 'anything-let 'lisp-indent-function 1)
985 (defmacro anything-let* (varlist &rest body)
986 "[OBSOLETE] Like `let*'. Bind anything buffer local variables according to VARLIST then eval BODY."
987 `(anything-let-internal (anything-let*-eval-varlist ',varlist)
988 (lambda () ,@body)))
989 (put 'anything-let* 'lisp-indent-function 1)
991 (defun anything-buffer-get ()
992 "If *anything action* buffer is shown, return `anything-action-buffer', otherwise `anything-buffer'."
993 (if (anything-action-window)
994 anything-action-buffer
995 anything-buffer))
997 (defun anything-window ()
998 "Window of `anything-buffer'."
999 (get-buffer-window (anything-buffer-get) 'visible))
1001 (defun anything-action-window ()
1002 "Window of `anything-action-buffer'."
1003 (get-buffer-window anything-action-buffer 'visible))
1005 (defmacro with-anything-window (&rest body)
1006 `(let ((--tmpfunc-- (lambda () ,@body)))
1007 (if anything-test-mode
1008 (with-current-buffer (anything-buffer-get)
1009 (funcall --tmpfunc--))
1010 (with-selected-window (anything-window)
1011 (funcall --tmpfunc--)))))
1012 (put 'with-anything-window 'lisp-indent-function 0)
1014 (defun anything-deferred-action-function ()
1015 (dolist (f deferred-action-list) (funcall f)))
1016 (defmacro with-anything-restore-variables(&rest body)
1017 "Restore variables specified by `anything-restored-variables' after executing BODY . "
1018 `(let ((--orig-vars (mapcar (lambda (v) (cons v (symbol-value v))) anything-restored-variables))
1019 (deferred-action-function 'anything-deferred-action-function))
1020 (anything-log "save variables: %S" --orig-vars)
1021 (unwind-protect (progn ,@body)
1022 (loop for (var . value) in --orig-vars
1023 do (set var value))
1024 (anything-log "restore variables"))))
1025 (put 'with-anything-restore-variables 'lisp-indent-function 0)
1027 (defun* anything-attr (attribute-name &optional (src (anything-get-current-source)))
1028 "Get the value of ATTRIBUTE-NAME of SRC (source).
1029 if SRC is omitted, use current source.
1030 It is useful to write your sources."
1031 (anything-aif (assq attribute-name src)
1032 (cdr it)))
1034 (defun* anything-attr* (attribute-name &optional (src (anything-get-current-source)))
1035 "Get the value of ATTRIBUTE-NAME of SRC (source) and pass to `anything-interpret-value'.
1036 if SRC is omitted, use current source.
1037 It is useful to write your sources."
1038 (anything-interpret-value (anything-attr attribute-name src)))
1040 (defun* anything-attr-defined (attribute-name &optional (src (anything-get-current-source)))
1041 "Return non-nil if ATTRIBUTE-NAME of SRC (source) is defined.
1042 if SRC is omitted, use current source.
1043 It is useful to write your sources."
1044 (and (assq attribute-name src) t))
1046 (defun* anything-attrset (attribute-name value &optional (src (anything-get-current-source)))
1047 "Set the value of ATTRIBUTE-NAME of SRC (source) to VALUE.
1048 if SRC is omitted, use current source.
1049 It is useful to write your sources."
1050 (anything-aif (assq attribute-name src)
1051 (setcdr it value)
1052 (setcdr src (cons (cons attribute-name value) (cdr src))))
1053 value)
1055 ;; anything-set-source-filter
1057 ;; This function sets a filter for anything sources and it may be
1058 ;; called while anything is running. It can be used to toggle
1059 ;; displaying of sources dinamically. For example, additional keys
1060 ;; can be bound into `anything-map' to display only the file-related
1061 ;; results if there are too many matches from other sources and
1062 ;; you're after files only:
1064 ;; Shift+F shows only file results from some sources:
1066 ;; (define-key anything-map "F" 'anything-my-show-files-only)
1068 ;; (defun anything-my-show-files-only ()
1069 ;; (interactive)
1070 ;; (anything-set-source-filter '("File Name History"
1071 ;; "Files from Current Directory")))
1073 ;; Shift+A shows all results:
1075 ;; (define-key anything-map "A" 'anything-my-show-all)
1077 ;; (defun anything-my-show-all ()
1078 ;; (interactive)
1079 ;; (anything-set-source-filter nil))
1082 ;; Note that you have to prefix the functions with anything- prefix,
1083 ;; otherwise they won't be bound when Anything is used under
1084 ;; Iswitchb. The -my- part is added to avoid collisions with
1085 ;; existing Anything function names.
1087 (defun anything-set-source-filter (sources)
1088 "Sets the value of `anything-source-filter' and updates the list of results."
1089 (unless (and (listp sources)
1090 (loop for name in sources always (stringp name)))
1091 (error "invalid data in `anything-set-source-filter': %S" sources))
1092 (setq anything-source-filter sources)
1093 (anything-log-eval anything-source-filter)
1094 (anything-update))
1096 (defun anything-set-sources (sources &optional no-init no-update)
1097 "Set `anything-sources' during `anything' invocation.
1098 If NO-INIT is non-nil, skip executing init functions of SOURCES.
1099 If NO-UPDATE is non-nil, skip executing `anything-update'."
1100 (with-current-buffer anything-buffer
1101 (setq anything-compiled-sources nil
1102 anything-sources sources
1103 anything-last-sources-local sources)
1104 (anything-log-eval anything-compiled-sources anything-sources))
1105 (unless no-init (anything-funcall-foreach 'init))
1106 (unless no-update (anything-update)))
1108 (defvar anything-compile-source-functions
1109 '(anything-compile-source--type
1110 anything-compile-source--dummy
1111 anything-compile-source--disable-shortcuts
1112 anything-compile-source--candidates-in-buffer)
1113 "Functions to compile elements of `anything-sources' (plug-in).")
1115 (defun anything-get-sources ()
1116 "Return compiled `anything-sources', which is memoized.
1118 Attributes:
1120 - type
1121 `anything-type-attributes' are merged in.
1122 - candidates-buffer
1123 candidates, volatile and match attrubute are created.
1125 (cond
1126 ;; action
1127 ((anything-action-window)
1128 anything-sources)
1129 ;; memoized
1130 (anything-compiled-sources)
1131 ;; first time
1133 (prog1
1134 (setq anything-compiled-sources
1135 (anything-compile-sources anything-sources anything-compile-source-functions))
1136 (anything-log-eval anything-compiled-sources)))))
1138 (defun* anything-get-selection (&optional (buffer nil buffer-s) (force-display-part))
1139 "Return the currently selected item or nil.
1140 if BUFFER is nil or unspecified, use anything-buffer as default value.
1141 If FORCE-DISPLAY-PART is non-nil, return the display string."
1142 (setq buffer (if (and buffer buffer-s) buffer anything-buffer))
1143 (unless (anything-empty-buffer-p buffer)
1144 (with-current-buffer buffer
1145 (let ((selection
1146 (or (and (not force-display-part)
1147 (get-text-property (overlay-start
1148 anything-selection-overlay)
1149 'anything-realvalue))
1150 (let ((disp (buffer-substring-no-properties
1151 (overlay-start anything-selection-overlay)
1152 (1- (overlay-end anything-selection-overlay))))
1153 (source (anything-get-current-source)))
1154 (anything-aif (and (not force-display-part)
1155 (assoc-default 'display-to-real source))
1156 (anything-funcall-with-source source it disp)
1157 disp)))))
1158 (unless (equal selection "")
1159 (anything-log-eval selection)
1160 selection)))))
1162 (defun anything-get-action ()
1163 "Return the associated action for the selected candidate."
1164 (unless (anything-empty-buffer-p (anything-buffer-get))
1165 (anything-aif (anything-attr 'action-transformer)
1166 (anything-composed-funcall-with-source
1167 (anything-get-current-source) it (anything-attr 'action) (anything-get-selection))
1168 (anything-attr 'action))))
1170 (defun anything-get-current-source ()
1171 "Return the source for the current selection / in init/candidates/action/candidate-transformer/filtered-candidate-transformer function."
1172 (declare (special source))
1173 ;; The name `anything-get-current-source' should be used in init function etc.
1174 (if (and (boundp 'anything-source-name) (stringp anything-source-name))
1175 source
1176 (with-current-buffer (anything-buffer-get)
1177 (or (get-text-property (point) 'anything-source)
1178 (block exit
1179 ;; This goto-char shouldn't be necessary, but point is moved to
1180 ;; point-min somewhere else which shouldn't happen.
1181 (goto-char (overlay-start anything-selection-overlay))
1182 (let* ((header-pos (or (anything-get-previous-header-pos)
1183 (anything-get-next-header-pos)))
1184 (source-name
1185 (save-excursion
1186 (unless header-pos
1187 (message "No candidates")
1188 (return-from exit nil))
1189 (goto-char header-pos)
1190 (anything-current-line-contents))))
1191 (some (lambda (source)
1192 (if (equal (assoc-default 'name source) source-name)
1193 source))
1194 (anything-get-sources))))))))
1196 (defun anything-buffer-is-modified (buffer)
1197 "Return non-nil when BUFFER is modified since `anything' was invoked."
1198 (let* ((b (get-buffer buffer))
1199 (key (concat (buffer-name b) "/" (anything-attr 'name)))
1200 (source-tick (or (gethash key anything-tick-hash) 0))
1201 (buffer-tick (buffer-chars-modified-tick b))
1202 (modifiedp (/= source-tick buffer-tick)))
1203 (puthash key buffer-tick anything-tick-hash)
1204 (anything-log-eval buffer modifiedp)
1205 modifiedp))
1206 (defun anything-current-buffer-is-modified ()
1207 "Return non-nil when `anything-current-buffer' is modified since `anything' was invoked."
1208 (anything-buffer-is-modified anything-current-buffer))
1210 (defvar anything-quit nil)
1211 (defun anything-run-after-quit (function &rest args)
1212 "Perform an action after quitting `anything'.
1213 The action is to call FUNCTION with arguments ARGS."
1214 (setq anything-quit t)
1215 (anything-log-eval function args)
1216 (apply 'run-with-idle-timer 0 nil function args)
1217 (anything-exit-minibuffer))
1219 (defun define-anything-type-attribute (type definition &optional doc)
1220 "Register type attribute of TYPE as DEFINITION with DOC.
1221 DOC is displayed in `anything-type-attributes' docstring.
1223 Use this function is better than setting `anything-type-attributes' directly."
1224 (anything-add-type-attribute type definition)
1225 (and doc (anything-document-type-attribute type doc))
1226 nil)
1228 (defvaralias 'anything-attributes 'anything-additional-attributes)
1229 (defvar anything-additional-attributes nil
1230 "List of all `anything' attributes.")
1231 (defun anything-document-attribute (attribute short-doc &optional long-doc)
1232 "Register ATTRIBUTE documentation introduced by plug-in.
1233 SHORT-DOC is displayed beside attribute name.
1234 LONG-DOC is displayed below attribute name and short documentation."
1235 (if long-doc
1236 (setq short-doc (concat "(" short-doc ")"))
1237 (setq long-doc short-doc
1238 short-doc ""))
1239 (add-to-list 'anything-additional-attributes attribute t)
1240 (put attribute 'anything-attrdoc
1241 (concat "- " (symbol-name attribute) " " short-doc "\n\n" long-doc "\n")))
1242 (put 'anything-document-attribute 'lisp-indent-function 2)
1244 (defun anything-require-at-least-version (version)
1245 "Output error message unless anything.el is older than VERSION.
1246 This is suitable for anything applications."
1247 (when (and (string= "1." (substring version 0 2))
1248 (string-match "1\.\\([0-9]+\\)" anything-version)
1249 (< (string-to-number (match-string 1 anything-version))
1250 (string-to-number (substring version 2))))
1251 (error "Please update anything.el!!
1253 M-x auto-install-batch anything
1255 You must have auto-install.el too.
1256 http://www.emacswiki.org/cgi-bin/wiki/download/auto-install.el
1257 ")))
1259 (defun anything-interpret-value (value &optional source)
1260 "interpret VALUE as variable, function or literal.
1261 If VALUE is a function, call it with no arguments and return the value.
1262 If SOURCE is `anything' source, `anything-source-name' is source name.
1264 If VALUE is a variable, return the value.
1266 If VALUE is a symbol, but it is not a function or a variable, cause an error.
1268 Otherwise, return VALUE itself."
1269 (cond ((and source (functionp value))
1270 (anything-funcall-with-source source value))
1271 ((functionp value)
1272 (funcall value))
1273 ((and (symbolp value) (boundp value))
1274 (symbol-value value))
1275 ((symbolp value)
1276 (error "anything-interpret-value: Symbol must be a function or a variable"))
1278 value)))
1280 (defun anything-once (function &rest args)
1281 "Ensure FUNCTION with ARGS to be called once in `anything' session."
1282 (let ((spec (cons function args)))
1283 (unless (member spec anything-once-called-functions)
1284 (apply function args)
1285 (push spec anything-once-called-functions))))
1287 ;; (@* "Core: API helper")
1288 (defun anything-empty-buffer-p (&optional buffer)
1289 (zerop (buffer-size (and buffer (get-buffer buffer)))))
1291 (defun anything-let-eval-varlist (varlist)
1292 (mapcar (lambda (pair)
1293 (if (listp pair)
1294 (cons (car pair) (eval (cadr pair)))
1295 (cons pair nil)))
1296 varlist))
1297 (defun anything-let*-eval-varlist (varlist)
1298 (let ((vars (mapcar (lambda (pair) (or (car-safe pair) pair)) varlist)))
1299 (eval `(let ,vars
1300 ,@(mapcar (lambda (pair)
1301 (if (listp pair)
1302 `(setq ,(car pair) ,(cadr pair))
1303 `(setq ,pair nil)))
1304 varlist)
1305 (mapcar (lambda (v) (cons v (symbol-value v))) ',vars)))))
1306 (defun anything-let-internal (binding bodyfunc)
1307 "Evaluate BODYFUNC and Set BINDING to anything buffer-local variables.
1308 BINDING is a list of (VARNAME . VALUE) pair."
1309 (setq anything-let-variables binding)
1310 (unwind-protect
1311 (funcall bodyfunc)
1312 (setq anything-let-variables nil)))
1315 ;; (@* "Core: tools")
1316 (defun anything-current-line-contents ()
1317 "Current line strig without properties."
1318 (buffer-substring-no-properties (line-beginning-position) (line-end-position)))
1320 (defun anything-funcall-with-source (source func &rest args)
1321 "Call FUNC with ARGS with variable `anything-source-name' and `source' is bound.
1322 FUNC can be function list. Return the result of last function call."
1323 (let ((anything-source-name (assoc-default 'name source))
1324 result)
1325 (anything-log-eval anything-source-name func args)
1326 (dolist (func (if (functionp func) (list func) func) result)
1327 (setq result (apply func args)))))
1329 (defun anything-funcall-foreach (sym)
1330 "Call the sym function(s) for each source if any."
1331 (dolist (source (anything-get-sources))
1332 (anything-aif (assoc-default sym source)
1333 (anything-funcall-with-source source it))))
1335 (defun anything-normalize-sources (sources)
1336 "If SOURCES is only one source, make a list."
1337 (cond ((or (and sources ; avoid nil
1338 (symbolp sources))
1339 (and (listp sources) (assq 'name sources)))
1340 (list sources))
1341 (sources)
1342 (t anything-sources)))
1344 (defun anything-approximate-candidate-number ()
1345 "Approximate Number of candidates.
1346 It is used to check if candidate number is 0, 1, or 2+."
1347 (with-current-buffer anything-buffer
1348 (let ((lines (1- (line-number-at-pos (1- (point-max))))))
1349 (if (zerop lines)
1351 (save-excursion
1352 (goto-char (point-min))
1353 (forward-line 1)
1354 (if (anything-pos-multiline-p)
1355 (if (search-forward anything-candidate-separator nil t) 2 1)
1356 lines))))))
1358 (defmacro with-anything-quittable (&rest body)
1359 `(let (inhibit-quit)
1360 (condition-case v
1361 (progn ,@body)
1362 (quit (setq anything-quit t)
1363 (exit-minibuffer)
1364 (keyboard-quit)))))
1365 (put 'with-anything-quittable 'lisp-indent-function 0)
1367 (defun anything-compose (arg-lst func-lst)
1368 "Call each function in FUNC-LST with the arguments specified in ARG-LST.
1369 The result of each function will be the new `car' of ARG-LST.
1371 This function allows easy sequencing of transformer functions."
1372 (dolist (func func-lst)
1373 (setcar arg-lst (apply func arg-lst)))
1374 (car arg-lst))
1376 (defun anything-composed-funcall-with-source (source funcs &rest args)
1377 (if (functionp funcs)
1378 (apply 'anything-funcall-with-source source funcs args)
1379 (apply 'anything-funcall-with-source
1380 source (lambda (&rest args) (anything-compose args funcs)) args)))
1382 (defun anything-new-timer (variable timer)
1383 "Set new TIMER to VARIABLE. Old timer is cancelled."
1384 (anything-aif (symbol-value variable)
1385 (cancel-timer it))
1386 (set variable timer))
1388 ;; (@* "Core: entry point")
1389 (defconst anything-argument-keys '(:sources :input :prompt :resume :preselect :buffer :keymap))
1390 ;;;###autoload
1391 (defun anything (&rest plist)
1392 "Select anything. In Lisp program, some optional arguments can be used.
1394 PLIST is a list like (:key1 val1 :key2 val2 ...) or
1395 (&optional sources input prompt resume preselect buffer keymap).
1397 Basic keywords are the following:
1399 - :sources
1401 Temporary value of `anything-sources'. It also accepts a
1402 symbol, interpreted as a variable of an anything source. It
1403 also accepts an alist representing an anything source, which is
1404 detected by (assq 'name ANY-SOURCES)
1406 - :input
1408 Temporary value of `anything-pattern', ie. initial input of minibuffer.
1410 - :prompt
1412 Prompt other than \"pattern: \".
1414 - :resume
1416 If t, Resurrect previously instance of `anything'. Skip the initialization.
1417 If 'noresume, this instance of `anything' cannot be resumed.
1419 - :preselect
1421 Initially selected candidate. Specified by exact candidate or a regexp.
1422 Note that it is not working with delayed sources.
1424 - :buffer
1426 `anything-buffer' instead of *anything*.
1428 - :keymap
1430 `anything-map' for current `anything' session.
1433 Of course, conventional arguments are supported, the two are same.
1435 (anything :sources sources :input input :prompt prompt :resume resume
1436 :preselect preselect :buffer buffer :keymap keymap)
1437 (anything sources input prompt resume preselect buffer keymap)
1440 Other keywords are interpreted as local variables of this anything session.
1441 The `anything-' prefix can be omitted. For example,
1443 (anything :sources 'anything-c-source-buffers
1444 :buffer \"*buffers*\" :candidate-number-limit 10)
1446 means starting anything session with `anything-c-source-buffers'
1447 source in *buffers* buffer and set
1448 `anything-candidate-number-limit' to 10 as session local variable. "
1449 (interactive)
1450 (if (keywordp (car plist))
1451 (anything-let-internal
1452 (anything-parse-keys plist)
1453 (lambda ()
1454 (apply 'anything
1455 (mapcar (lambda (key) (plist-get plist key))
1456 anything-argument-keys))))
1457 (apply 'anything-internal plist)))
1459 (defun* anything-resume (&optional (any-buffer anything-last-buffer) buffer-pattern (any-resume t))
1460 "Resurrect previously invoked `anything'."
1461 (interactive)
1462 (when (or current-prefix-arg buffer-pattern)
1463 (setq any-buffer (anything-resume-select-buffer buffer-pattern)))
1464 (setq anything-compiled-sources nil)
1465 (anything
1466 (or (buffer-local-value 'anything-last-sources-local (get-buffer any-buffer))
1467 anything-last-sources anything-sources)
1468 (buffer-local-value 'anything-input-local (get-buffer any-buffer)) nil any-resume nil any-buffer))
1470 ;;; rubikitch: experimental
1471 ;;; I use this and check it whether I am convenient.
1472 ;;; I may introduce an option to control the behavior.
1473 (defun* anything-resume-window-only (&optional (any-buffer anything-last-buffer) buffer-pattern)
1474 (interactive)
1475 (anything-resume any-buffer buffer-pattern 'window-only))
1477 ;;;###autoload
1478 (defun anything-at-point (&optional any-sources any-input any-prompt any-resume any-preselect any-buffer)
1479 "Same as `anything' except when C-u is pressed, the initial input is the symbol at point."
1480 (interactive)
1481 (anything any-sources
1482 (if current-prefix-arg
1483 (concat "\\b" (thing-at-point 'symbol) "\\b"
1484 (if (featurep 'anything-match-plugin) " " ""))
1485 any-input)
1486 any-prompt any-resume any-preselect any-buffer))
1488 ;;;###autoload
1489 (defun anything-other-buffer (any-sources any-buffer)
1490 "Simplified interface of `anything' with other `anything-buffer'"
1491 (anything any-sources nil nil nil nil any-buffer))
1493 ;;; (@* "Core: entry point helper")
1494 (defun anything-internal (&optional any-sources any-input any-prompt any-resume any-preselect any-buffer any-keymap)
1495 "Older interface of `anything'. It is called by `anything'."
1496 (anything-log "++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
1497 (anything-log-eval any-prompt any-preselect any-buffer any-keymap)
1498 (condition-case v
1499 (let ( ;; It is needed because `anything-source-name' is non-nil
1500 ;; when `anything' is invoked by action. Awful global scope.
1501 anything-source-name anything-in-persistent-action
1502 anything-quit
1503 (case-fold-search t)
1504 (anything-buffer (or any-buffer anything-buffer))
1505 ;; cua-mode ; avoid error when region is selected
1507 (with-anything-restore-variables
1508 (anything-initialize-1 any-resume any-input any-sources)
1509 (anything-display-buffer anything-buffer)
1510 (anything-log "show prompt")
1511 (unwind-protect
1512 (anything-read-pattern-maybe any-prompt any-input any-preselect any-resume any-keymap)
1513 (anything-cleanup)))
1514 (prog1 (unless anything-quit (anything-execute-selection-action-1))
1515 (anything-log "end session --------------------------------------------")))
1516 (quit
1517 (anything-on-quit)
1518 (anything-log "end session (quit) -------------------------------------")
1519 nil)))
1523 (defun anything-parse-keys (keys)
1524 (loop for (key value &rest _) on keys by #'cddr
1525 for symname = (substring (symbol-name key) 1)
1526 for sym = (intern (if (string-match "^anything-" symname)
1527 symname
1528 (concat "anything-" symname)))
1529 unless (memq key anything-argument-keys)
1530 collect (cons sym value)))
1532 (defun anything-resume-p (any-resume)
1533 "Whethre current anything session is resumed or not."
1534 (memq any-resume '(t window-only)))
1536 (defvar anything-buffers nil
1537 "All of `anything-buffer' in most recently used order.")
1538 (defun anything-initialize-1 (any-resume any-input any-sources)
1539 "The real initialization of `anything'.
1541 This function name should be `anything-initialize', but anything
1542 extensions may advice `anything-initalize'. I cannot rename, sigh."
1543 (anything-log "start initialization: any-resume=%S any-input=%S" any-resume any-input)
1544 (anything-frame/window-configuration 'save)
1545 (setq anything-sources (anything-normalize-sources any-sources))
1546 (anything-log "sources = %S" anything-sources)
1547 (anything-hooks 'setup)
1548 (anything-current-position 'save)
1549 (if (anything-resume-p any-resume)
1550 (anything-initialize-overlays (anything-buffer-get))
1551 (anything-initialize))
1552 (unless (eq any-resume 'noresume)
1553 (anything-recent-push anything-buffer 'anything-buffers)
1554 (setq anything-last-buffer anything-buffer))
1555 (when any-input (setq anything-input any-input anything-pattern any-input))
1556 (and (anything-resume-p any-resume) (anything-funcall-foreach 'resume))
1557 (anything-log "end initialization"))
1559 (defun anything-execute-selection-action-1 ()
1560 (unwind-protect
1561 (anything-execute-selection-action)
1562 (anything-aif (get-buffer anything-action-buffer)
1563 (kill-buffer it))
1564 (anything-log-run-hook 'anything-after-action-hook)))
1566 (defun anything-on-quit ()
1567 (setq minibuffer-history (cons anything-input minibuffer-history))
1568 (anything-current-position 'restore))
1570 (defun anything-resume-select-buffer (input)
1571 (anything '(((name . "Resume anything buffer")
1572 (candidates . anything-buffers)
1573 (action . identity)))
1574 input nil 'noresume nil "*anything resume*"))
1576 (defun anything-recent-push (elt list-var)
1577 "Add ELT to the value of LIST-VAR as most recently used value."
1578 (let ((m (member elt (symbol-value list-var))))
1579 (and m (set list-var (delq (car m) (symbol-value list-var))))
1580 (push elt (symbol-value list-var))))
1582 ;;; (@* "Core: Accessors")
1583 ;;; rubikitch: I love to create functions to control variables.
1584 (defvar anything-current-position nil
1585 "Cons of (point) and (window-start) when `anything' is invoked.
1586 It is needed because restoring position when `anything' is keyboard-quitted.")
1587 (defun anything-current-position (save-or-restore)
1588 (case save-or-restore
1589 (save
1590 (setq anything-current-position (cons (point) (window-start))))
1591 (restore
1592 (goto-char (car anything-current-position))
1593 (set-window-start (selected-window) (cdr anything-current-position)))))
1595 ;;; FIXME I want to remove them. But anything-iswitchb uses them.
1596 (defun anything-current-frame/window-configuration ()
1597 (funcall (cdr anything-save-configuration-functions)))
1598 (defun anything-set-frame/window-configuration (conf)
1599 (funcall (car anything-save-configuration-functions) conf))
1601 (declare-function 'anything-frame/window-configuration "anything")
1602 (lexical-let (conf)
1603 (defun anything-frame/window-configuration (save-or-restore)
1604 (anything-log-eval anything-save-configuration-functions)
1605 (case save-or-restore
1606 (save (setq conf (funcall (cdr anything-save-configuration-functions))))
1607 (restore (funcall (car anything-save-configuration-functions) conf)))))
1609 ;; (@* "Core: Display *anything* buffer")
1610 (defun anything-display-buffer (buf)
1611 "Display *anything* buffer."
1612 (funcall (with-current-buffer buf anything-display-function) buf))
1614 (defun anything-default-display-buffer (buf)
1615 (funcall (if anything-samewindow 'switch-to-buffer 'pop-to-buffer) buf))
1617 ;; (@* "Core: initialize")
1618 (defun anything-initialize ()
1619 "Initialize anything settings and set up the anything buffer."
1620 (anything-log-run-hook 'anything-before-initialize-hook)
1621 (setq anything-once-called-functions nil)
1622 (setq anything-delayed-init-executed nil)
1623 (setq anything-current-buffer (current-buffer))
1624 (setq anything-buffer-file-name buffer-file-name)
1625 (setq anything-issued-errors nil)
1626 (setq anything-compiled-sources nil)
1627 (setq anything-saved-current-source nil)
1628 ;; Call the init function for sources where appropriate
1629 (anything-funcall-foreach 'init)
1631 (setq anything-pattern "")
1632 (setq anything-input "")
1633 (setq anything-candidate-cache nil)
1634 (setq anything-last-sources anything-sources)
1636 (anything-create-anything-buffer)
1637 (anything-log-run-hook 'anything-after-initialize-hook))
1639 (defun anything-read-pattern-maybe (any-prompt any-input any-preselect any-resume any-keymap)
1640 (if (anything-resume-p any-resume) (anything-mark-current-line) (anything-update))
1641 (select-frame-set-input-focus (window-frame (minibuffer-window)))
1642 (anything-preselect any-preselect)
1643 (let ((minibuffer-local-map
1644 (with-current-buffer (anything-buffer-get)
1645 (and any-keymap (set (make-local-variable 'anything-map) any-keymap))
1646 anything-map)))
1647 (anything-log-eval (anything-approximate-candidate-number)
1648 anything-execute-action-at-once-if-one
1649 anything-quit-if-no-candidate)
1650 (cond ((and anything-execute-action-at-once-if-one
1651 (= (anything-approximate-candidate-number) 1))
1652 (ignore))
1653 ((and anything-quit-if-no-candidate
1654 (= (anything-approximate-candidate-number) 0))
1655 (setq anything-quit t)
1656 (and (functionp anything-quit-if-no-candidate)
1657 (funcall anything-quit-if-no-candidate)))
1659 (read-string (or any-prompt "pattern: ") any-input)))))
1661 (defun anything-create-anything-buffer (&optional test-mode)
1662 "Create newly created `anything-buffer'.
1663 If TEST-MODE is non-nil, clear `anything-candidate-cache'."
1664 (when test-mode
1665 (setq anything-candidate-cache nil))
1666 (with-current-buffer (get-buffer-create anything-buffer)
1667 (anything-log "kill local variables: %S" (buffer-local-variables))
1668 (kill-all-local-variables)
1669 (buffer-disable-undo)
1670 (erase-buffer)
1671 (set (make-local-variable 'inhibit-read-only) t)
1672 (set (make-local-variable 'anything-last-sources-local) anything-sources)
1673 (set (make-local-variable 'anything-follow-mode) nil)
1674 (set (make-local-variable 'anything-display-function) anything-display-function)
1675 (anything-log-eval anything-display-function anything-let-variables)
1676 (loop for (var . val) in anything-let-variables
1677 do (set (make-local-variable var) val))
1679 (setq cursor-type nil)
1680 (setq mode-name "Anything"))
1681 (anything-initialize-overlays anything-buffer)
1682 (get-buffer anything-buffer))
1684 (defun anything-initialize-overlays (buffer)
1685 (anything-log "overlay setup")
1686 (if anything-selection-overlay
1687 ;; make sure the overlay belongs to the anything buffer if
1688 ;; it's newly created
1689 (move-overlay anything-selection-overlay (point-min) (point-min)
1690 (get-buffer buffer))
1692 (setq anything-selection-overlay
1693 (make-overlay (point-min) (point-min) (get-buffer buffer)))
1694 (overlay-put anything-selection-overlay 'face anything-selection-face))
1696 (cond (anything-enable-shortcuts
1697 (setq anything-shortcut-keys
1698 (assoc-default anything-enable-shortcuts anything-shortcut-keys-alist))
1699 (unless anything-digit-overlays
1700 (setq anything-digit-overlays
1701 (loop for key across anything-shortcut-keys
1702 for overlay = (make-overlay (point-min) (point-min) (get-buffer buffer))
1703 do (overlay-put overlay 'before-string
1704 (format "%s - " (upcase (make-string 1 key))))
1705 collect overlay))))
1706 (anything-digit-overlays
1707 (mapc 'delete-overlay anything-digit-overlays)
1708 (setq anything-digit-overlays nil))))
1710 (defun anything-hooks (setup-or-cleanup)
1711 (let ((hooks '((deferred-action-list anything-check-minibuffer-input)
1712 (minibuffer-setup-hook anything-print-error-messages))))
1713 (if (eq setup-or-cleanup 'setup)
1714 (dolist (args hooks) (apply 'add-hook args))
1715 (dolist (args (reverse hooks)) (apply 'remove-hook args)))))
1717 ;; (@* "Core: clean up")
1718 ;;; TODO move
1719 (defun anything-cleanup ()
1720 "Clean up the mess."
1721 (anything-log "start cleanup")
1722 (with-current-buffer anything-buffer
1723 (setq cursor-type t))
1724 (bury-buffer anything-buffer)
1725 (anything-funcall-foreach 'cleanup)
1726 (anything-new-timer 'anything-check-minibuffer-input-timer nil)
1727 (anything-kill-async-processes)
1728 (anything-log-run-hook 'anything-cleanup-hook)
1729 (anything-hooks 'cleanup)
1730 (anything-frame/window-configuration 'restore))
1732 ;; (@* "Core: input handling")
1733 (defun anything-check-minibuffer-input ()
1734 "Extract input string from the minibuffer and check if it needs
1735 to be handled."
1736 (if (or (not anything-input-idle-delay) (anything-action-window))
1737 (anything-check-minibuffer-input-1)
1738 (anything-new-timer
1739 'anything-check-minibuffer-input-timer
1740 (run-with-idle-timer anything-input-idle-delay nil 'anything-check-minibuffer-input-1))))
1742 (defun anything-check-minibuffer-input-1 ()
1743 (with-anything-quittable
1744 (with-selected-window (minibuffer-window)
1745 (anything-check-new-input (minibuffer-contents)))))
1747 (defun anything-check-new-input (input)
1748 "Check input string and update the anything buffer if
1749 necessary."
1750 (unless (equal input anything-pattern)
1751 (setq anything-pattern input)
1752 (unless (anything-action-window)
1753 (setq anything-input anything-pattern))
1754 (anything-log-eval anything-pattern anything-input)
1755 (anything-update)))
1757 ;; (@* "Core: source compiler")
1758 (defvar anything-compile-source-functions-default anything-compile-source-functions
1759 "Plug-ins this file provides.")
1760 (defun anything-compile-sources (sources funcs)
1761 "Compile sources (`anything-sources') with funcs (`anything-compile-source-functions').
1762 Anything plug-ins are realized by this function."
1763 (mapcar
1764 (lambda (source)
1765 (loop with source = (if (listp source) source (symbol-value source))
1766 for f in funcs
1767 do (setq source (funcall f source))
1768 finally (return source)))
1769 sources))
1771 ;; (@* "Core: plug-in attribute documentation hack")
1773 ;; `anything-document-attribute' is public API.
1774 (defadvice documentation-property (after anything-document-attribute activate)
1775 "Hack to display plug-in attributes' documentation as `anything-sources' docstring."
1776 (when (eq symbol 'anything-sources)
1777 (setq ad-return-value
1778 (concat ad-return-value "\n"
1779 (mapconcat (lambda (sym) (get sym 'anything-attrdoc))
1780 anything-additional-attributes
1781 "\n")))))
1782 ;; (describe-variable 'anything-sources)
1783 ;; (documentation-property 'anything-sources 'variable-documentation)
1784 ;; (progn (ad-disable-advice 'documentation-property 'after 'anything-document-attribute) (ad-update 'documentation-property))
1786 ;; (@* "Core: all candidates")
1787 (defun anything-process-delayed-init (source)
1788 (let ((name (assoc-default 'name source)))
1789 (unless (member name anything-delayed-init-executed)
1790 (anything-aif (assoc-default 'delayed-init source)
1791 (with-current-buffer anything-current-buffer
1792 (anything-funcall-with-source source it)
1793 (dolist (f (if (functionp it) (list it) it))
1794 (add-to-list 'anything-delayed-init-executed name)))))))
1796 (defun anything-get-candidates (source)
1797 "Retrieve and return the list of candidates from
1798 SOURCE."
1799 (anything-process-delayed-init source)
1800 (let* ((candidate-source (assoc-default 'candidates source))
1801 (type-error (lambda ()
1802 (error (concat "Candidates must either be a function, "
1803 " a variable or a list: %s")
1804 candidate-source)))
1805 (candidates (condition-case err
1806 (anything-interpret-value candidate-source source)
1807 (error (funcall type-error)))))
1808 (cond ((processp candidates) candidates)
1809 ((listp candidates) (anything-transform-candidates candidates source))
1810 (t (funcall type-error)))))
1813 (defun anything-get-cached-candidates (source)
1814 "Return the cached value of candidates for SOURCE.
1815 Cache the candidates if there is not yet a cached value."
1816 (let* ((name (assoc-default 'name source))
1817 (candidate-cache (assoc name anything-candidate-cache)))
1818 (cond (candidate-cache
1819 (anything-log "use cached candidates")
1820 (cdr candidate-cache))
1822 (anything-log "calculate candidates")
1823 (let ((candidates (anything-get-candidates source)))
1824 (cond ((processp candidates)
1825 (push (cons candidates
1826 (append source
1827 (list (cons 'item-count 0)
1828 (cons 'incomplete-line ""))))
1829 anything-async-processes)
1830 (set-process-filter candidates 'anything-output-filter)
1831 (setq candidates nil))
1832 ((not (assoc 'volatile source))
1833 (setq candidate-cache (cons name candidates))
1834 (push candidate-cache anything-candidate-cache)))
1835 candidates)))))
1837 ;;; (@* "Core: candidate transformers")
1838 (defun anything-process-candidate-transformer (candidates source)
1839 (anything-aif (assoc-default 'candidate-transformer source)
1840 (anything-composed-funcall-with-source source it candidates)
1841 candidates))
1842 (defun anything-process-filtered-candidate-transformer (candidates source)
1843 (anything-aif (assoc-default 'filtered-candidate-transformer source)
1844 (anything-composed-funcall-with-source source it candidates source)
1845 candidates))
1846 (defun anything-process-filtered-candidate-transformer-maybe (candidates source process-p)
1847 (if process-p
1848 (anything-process-filtered-candidate-transformer candidates source)
1849 candidates))
1850 (defun anything-process-real-to-display (candidates source)
1851 (anything-aif (assoc-default 'real-to-display source)
1852 (setq candidates (anything-funcall-with-source
1853 source 'mapcar
1854 (lambda (cand_)
1855 (if (consp cand_)
1856 ;; override DISPLAY from candidate-transformer
1857 (cons (funcall it (cdr cand_)) (cdr cand_))
1858 (cons (funcall it cand_) cand_)))
1859 candidates))
1860 candidates))
1861 (defun anything-transform-candidates (candidates source &optional process-p)
1862 "Transform CANDIDATES according to candidate transformers."
1863 (anything-process-real-to-display
1864 (anything-process-filtered-candidate-transformer-maybe
1865 (anything-process-candidate-transformer candidates source) source process-p)
1866 source))
1869 ;; (@* "Core: narrowing candidates")
1870 (defun anything-candidate-number-limit (source)
1871 "`anything-candidate-number-limit' variable may be overridden by SOURCE.
1872 If (candidate-number-limit) is in SOURCE, show all candidates in SOURCE,
1873 ie. cancel the effect of `anything-candidate-number-limit'."
1874 (anything-aif (assq 'candidate-number-limit source)
1875 (or (cdr it) 99999999)
1876 (or anything-candidate-number-limit 99999999)))
1878 (defconst anything-default-match-functions
1879 (list (lambda (candidate)
1880 (string-match anything-pattern candidate))))
1882 (defun anything-compute-matches (source)
1883 "Compute matches from SOURCE according to its settings."
1884 (if debug-on-error
1885 (anything-compute-matches-internal source)
1886 (condition-case v
1887 (anything-compute-matches-internal source)
1888 (error (anything-log-error
1889 "anything-compute-matches: error when processing source: %s"
1890 (assoc-default 'name source))
1891 nil))))
1893 (defun anything-candidate-get-display (candidate)
1894 "Get display part (searched) from CANDIDATE.
1895 CANDIDATE is a string, a symbol, or (DISPLAY . REAL) cons cell."
1896 (format "%s" (or (car-safe candidate) candidate)))
1898 (defun anything-process-pattern-transformer (pattern source)
1899 (anything-aif (assoc-default 'pattern-transformer source)
1900 (anything-composed-funcall-with-source source it pattern)
1901 pattern))
1903 (defun anything-match-functions (source)
1904 (or (assoc-default 'match source)
1905 anything-default-match-functions))
1907 (defun anything-compute-matches-internal (source)
1908 (let ((matchfns (anything-match-functions source))
1909 (anything-source-name (assoc-default 'name source))
1910 (limit (anything-candidate-number-limit source))
1911 (anything-pattern (anything-process-pattern-transformer anything-pattern source))
1912 matches)
1913 (cond ((or (equal anything-pattern "") (equal matchfns '(identity)))
1914 (setq matches (anything-get-cached-candidates source))
1915 (if (> (length matches) limit)
1916 (setq matches (subseq matches 0 limit))))
1918 (condition-case nil
1919 (let ((item-count 0)
1920 (cands (anything-get-cached-candidates source))
1921 exit newmatches)
1922 (clrhash anything-match-hash)
1923 (dolist (match matchfns)
1924 (dolist (candidate cands)
1925 (when (and (not (gethash candidate anything-match-hash))
1926 (funcall match (anything-candidate-get-display candidate)))
1927 (puthash candidate t anything-match-hash)
1928 (push candidate newmatches)
1929 (incf item-count)
1930 (when (= item-count limit)
1931 (setq exit t)
1932 (return))))
1933 (setq matches (append matches (reverse newmatches)))
1934 (if exit (return))))
1936 (invalid-regexp (setq matches nil)))))
1937 (anything-process-filtered-candidate-transformer matches source)))
1939 ;; (anything '(((name . "error")(candidates . (lambda () (hage))) (action . identity))))
1941 (defun anything-process-source (source)
1942 "Display matches from SOURCE according to its settings."
1943 (anything-log-eval (assoc-default 'name source))
1944 (if (assq 'direct-insert-match source) ;experimental
1945 (anything-process-source--direct-insert-match source)
1946 (let ((matches (anything-compute-matches source)))
1947 (when matches
1948 (when anything-test-mode
1949 (setq anything-test-candidate-list
1950 `(,@anything-test-candidate-list
1951 (,(assoc-default 'name source)
1952 ,matches))))
1953 (anything-insert-header-from-source source)
1954 (if (not (assq 'multiline source))
1955 (mapc 'anything-insert-match-with-digit-overlay matches)
1956 (let ((start (point)) separate)
1957 (dolist (match matches)
1958 (if separate
1959 (anything-insert-candidate-separator)
1960 (setq separate t))
1961 (anything-insert-match-with-digit-overlay match))
1962 (put-text-property start (point) 'anything-multiline t)))))))
1964 (defun anything-insert-match-with-digit-overlay (match)
1965 (declare (special source))
1966 (anything-put-digit-overlay-maybe)
1967 (anything-insert-match match 'insert source))
1969 (defun anything-put-digit-overlay-maybe ()
1970 (when (and anything-enable-shortcuts
1971 (not (eq anything-digit-shortcut-count
1972 (length anything-digit-overlays))))
1973 (move-overlay (nth anything-digit-shortcut-count
1974 anything-digit-overlays)
1975 (line-beginning-position)
1976 (line-beginning-position))
1977 (incf anything-digit-shortcut-count)))
1979 (defun anything-process-source--direct-insert-match (source)
1980 "[EXPERIMENTAL] Insert candidates from `anything-candidate-buffer'"
1981 (anything-log-eval (assoc-default 'name source))
1982 (let ((anything-source-name (assoc-default 'name source))
1983 content-buf)
1984 (funcall (assoc-default 'candidates source))
1985 (setq content-buf (anything-candidate-buffer))
1986 (unless (anything-empty-buffer-p content-buf)
1987 (anything-insert-header-from-source source)
1988 (insert-buffer-substring content-buf)
1989 ;; TODO call anything-put-digit-overlay-maybe with loop
1992 (defun anything-process-delayed-sources (delayed-sources)
1993 "Process delayed sources if the user is idle for
1994 `anything-idle-delay' seconds."
1995 (with-anything-quittable
1996 (anything-log-eval (mapcar (lambda (s) (assoc-default 'name s)) delayed-sources))
1997 (when (sit-for (if anything-input-idle-delay
1998 (max 0 (- anything-idle-delay anything-input-idle-delay))
1999 anything-idle-delay))
2000 (with-current-buffer anything-buffer
2001 (save-excursion
2002 (goto-char (point-max))
2003 (mapc 'anything-process-source delayed-sources)
2004 (when (and (not (anything-empty-buffer-p))
2005 ;; no selection yet
2006 (= (overlay-start anything-selection-overlay)
2007 (overlay-end anything-selection-overlay)))
2008 (goto-char (point-min))
2009 (anything-next-line)))
2010 (save-excursion
2011 (goto-char (point-min))
2012 (anything-log-run-hook 'anything-update-hook))
2013 (anything-maybe-fit-frame)))))
2015 ;; (@* "Core: *anything* buffer contents")
2016 (defvar anything-input-local nil)
2017 (defun anything-update ()
2018 "Update the list of matches in the anything buffer according to
2019 the current pattern."
2020 (anything-log "start update")
2021 (setq anything-digit-shortcut-count 0)
2022 (anything-kill-async-processes)
2023 (with-current-buffer (anything-buffer-get)
2024 (set (make-local-variable 'anything-input-local) anything-pattern)
2025 (erase-buffer)
2026 (when anything-enable-shortcuts
2027 (mapc 'delete-overlay anything-digit-overlays))
2028 (let (delayed-sources)
2029 (unwind-protect
2030 (setq delayed-sources
2031 (loop for source in (remove-if-not 'anything-update-source-p
2032 (anything-get-sources))
2033 if (anything-delayed-source-p source)
2034 collect source
2035 else do (anything-process-source source)))
2036 (anything-log-eval
2037 (mapcar (lambda (s) (assoc-default 'name s)) delayed-sources))
2038 (anything-update-move-first-line)
2039 (if anything-test-mode
2040 (mapc 'anything-process-source delayed-sources)
2041 (anything-maybe-fit-frame)
2042 (when delayed-sources
2043 (run-with-idle-timer (if (featurep 'xemacs) 0.1 0)
2045 'anything-process-delayed-sources
2046 delayed-sources))
2047 ;; FIXME I want to execute anything-after-update-hook
2048 ;; AFTER processing delayed sources
2049 (anything-log-run-hook 'anything-after-update-hook))
2050 (anything-log "end update")))))
2052 (defun anything-update-source-p (source)
2053 (and (or (not anything-source-filter)
2054 (member (assoc-default 'name source) anything-source-filter))
2055 (>= (length anything-pattern)
2056 (anything-aif (assoc 'requires-pattern source)
2057 (or (cdr it) 1)
2058 0))))
2059 (defun anything-delayed-source-p (source)
2060 (or (assoc 'delayed source)
2061 (and anything-quick-update
2062 (< (window-height (get-buffer-window (current-buffer)))
2063 (line-number-at-pos (point-max))))))
2065 (defun anything-update-move-first-line ()
2066 (goto-char (point-min))
2067 (save-excursion (anything-log-run-hook 'anything-update-hook))
2068 (anything-next-line))
2070 (defun anything-force-update ()
2071 "Recalculate and update candidates.
2072 If current source has `update' attribute, a function without argument, call it before update."
2073 (interactive)
2074 (let ((source (anything-get-current-source)))
2075 (anything-aif (anything-funcall-with-source source 'anything-candidate-buffer)
2076 (kill-buffer it))
2077 (dolist (attr '(update init))
2078 (anything-aif (assoc-default attr source)
2079 (anything-funcall-with-source source it)))
2080 (anything-remove-candidate-cache source)
2081 (let ((selection (anything-get-selection nil t)))
2082 (anything-update)
2083 (anything-keep-selection source selection))))
2085 (defun anything-keep-selection (source selection)
2086 (with-anything-window
2087 (anything-goto-source source)
2088 (forward-char -1) ;back to \n
2089 (if (search-forward (concat "\n" selection "\n") nil t)
2090 (forward-line -1)
2091 (goto-char (point-min))
2092 (forward-line 1))
2093 (anything-mark-current-line)))
2095 (defun anything-remove-candidate-cache (source)
2096 (setq anything-candidate-cache
2097 (delete (assoc (assoc-default 'name source) anything-candidate-cache)
2098 anything-candidate-cache)))
2100 (defun anything-insert-match (match insert-function source)
2101 "Insert MATCH into the anything buffer. If MATCH is a list then
2102 insert the string inteneded to appear on the display and store
2103 the real value in a text property."
2104 (let ((start (line-beginning-position (point)))
2105 (string (or (car-safe match) match))
2106 (realvalue (cdr-safe match)))
2107 (when (symbolp string) (setq string (symbol-name string)))
2108 (when (stringp string)
2109 (funcall insert-function string)
2110 ;; Some sources with candidates-in-buffer have already added
2111 ;; 'anything-realvalue property when creating candidate buffer.
2112 (unless (get-text-property start 'anything-realvalue)
2113 (and realvalue
2114 (put-text-property start (line-end-position)
2115 'anything-realvalue realvalue)))
2116 (when anything-source-in-each-line-flag
2117 (put-text-property start (line-end-position) 'anything-source source))
2118 (funcall insert-function "\n"))))
2120 (defun anything-insert-header-from-source (source)
2121 (let ((name (assoc-default 'name source)))
2122 (anything-insert-header name
2123 (anything-aif (assoc-default 'header-name source)
2124 (anything-funcall-with-source source it name)))))
2126 (defun anything-insert-header (name &optional display-string)
2127 "Insert header of source NAME into the anything buffer."
2128 (unless (bobp)
2129 (let ((start (point)))
2130 (insert "\n")
2131 (put-text-property start (point) 'anything-header-separator t)))
2133 (let ((start (point)))
2134 (insert name)
2135 (put-text-property (line-beginning-position)
2136 (line-end-position) 'anything-header t)
2137 (when display-string
2138 (overlay-put (make-overlay (line-beginning-position) (line-end-position))
2139 'display display-string))
2140 (insert "\n")
2141 (put-text-property start (point) 'face anything-header-face)))
2144 (defun anything-insert-candidate-separator ()
2145 "Insert separator of candidates into the anything buffer."
2146 (insert anything-candidate-separator)
2147 (put-text-property (line-beginning-position)
2148 (line-end-position) 'anything-candidate-separator t)
2149 (insert "\n"))
2154 ;; (@* "Core: async process")
2155 (defun anything-output-filter (process string)
2156 "Process output from PROCESS."
2157 (let* ((process-assoc (assoc process anything-async-processes))
2158 (process-info (cdr process-assoc))
2159 (insertion-marker (assoc-default 'insertion-marker process-info))
2160 (incomplete-line-info (assoc 'incomplete-line process-info))
2161 (item-count-info (assoc 'item-count process-info))
2162 (limit (anything-candidate-number-limit process-info)))
2163 (anything-log-eval string (cdr incomplete-line-info))
2164 (with-current-buffer anything-buffer
2165 (save-excursion
2166 (if insertion-marker
2167 (goto-char insertion-marker)
2169 (goto-char (point-max))
2170 (anything-insert-header-from-source process-info)
2171 (setcdr process-assoc
2172 (append process-info `((insertion-marker . ,(point-marker))))))
2174 (let ((lines (split-string string "\n"))
2175 (multiline (assq 'multiline process-info))
2176 (start (point))
2177 candidates separate)
2178 (while lines
2179 (if (not (cdr lines))
2180 ;; store last incomplete line until new output arrives
2181 (setcdr incomplete-line-info (car lines))
2183 (if (cdr incomplete-line-info)
2184 (progn
2185 (push (concat (cdr incomplete-line-info) (car lines))
2186 candidates)
2187 (setcdr incomplete-line-info nil))
2189 (push (car lines) candidates)))
2191 (pop lines))
2193 (setq candidates (reverse candidates))
2194 (dolist (candidate (anything-transform-candidates candidates process-info t))
2195 ;; FIXME
2196 ;; (if (and multiline separate)
2197 ;; (anything-insert-candidate-separator)
2198 ;; (setq separate t))
2199 (anything-insert-match candidate 'insert-before-markers process-info)
2200 (incf (cdr item-count-info))
2201 ;; FIXME
2202 ;; (if multiline
2203 ;; (put-text-property start (point) 'anything-multiline t))
2204 (when (>= (cdr item-count-info) limit)
2205 (anything-kill-async-process process)
2206 (return)))))
2208 (anything-maybe-fit-frame)
2210 (anything-log-run-hook 'anything-update-hook)
2212 (if (bobp)
2213 (anything-next-line)
2215 (save-selected-window
2216 (select-window (get-buffer-window anything-buffer 'visible))
2217 (anything-mark-current-line))))))
2220 (defun anything-kill-async-processes ()
2221 "Kill all known asynchronous processes according to
2222 `anything-async-processes'."
2223 "Kill locate process."
2224 (dolist (process-info anything-async-processes)
2225 (anything-kill-async-process (car process-info)))
2226 (setq anything-async-processes nil))
2229 (defun anything-kill-async-process (process)
2230 "Kill PROCESS and detach the associated functions."
2231 (set-process-filter process nil)
2232 (delete-process process))
2235 ;; (@* "Core: action")
2236 (defun anything-execute-selection-action (&optional selection action clear-saved-action)
2237 "If a candidate was selected then perform the associated
2238 action."
2239 (anything-log "executing action")
2240 (setq selection (or selection (anything-get-selection)))
2241 (setq action (or action
2242 anything-saved-action
2243 (if (get-buffer anything-action-buffer)
2244 (anything-get-selection anything-action-buffer)
2245 (anything-get-action))))
2246 (let ((source (or anything-saved-current-source (anything-get-current-source))))
2247 (if (and (not selection) (assoc 'accept-empty source))
2248 (setq selection ""))
2249 (if (and (listp action)
2250 (not (functionp action))) ; lambda
2251 ;;select the default action
2252 (setq action (cdar action)))
2253 (unless clear-saved-action (setq anything-saved-action nil))
2254 (if (and selection action)
2255 (anything-funcall-with-source source action selection))))
2257 (defun anything-select-action ()
2258 "Select an action for the currently selected candidate.
2259 If action buffer is selected, back to the anything buffer."
2260 (interactive)
2261 (cond ((get-buffer-window anything-action-buffer 'visible)
2262 (set-window-buffer (get-buffer-window anything-action-buffer) anything-buffer)
2263 (kill-buffer anything-action-buffer)
2264 (insert anything-input))
2266 (setq anything-saved-selection (anything-get-selection))
2267 (unless anything-saved-selection
2268 (error "Nothing is selected."))
2269 (setq anything-saved-current-source (anything-get-current-source))
2270 (let ((actions (anything-get-action)))
2271 (if (functionp actions)
2272 (message "Sole action: %s" actions)
2273 (with-current-buffer (get-buffer-create anything-action-buffer)
2274 (erase-buffer)
2275 (buffer-disable-undo)
2276 (set-window-buffer (get-buffer-window anything-buffer) anything-action-buffer)
2277 (set (make-local-variable 'anything-sources)
2278 `(((name . "Actions")
2279 (volatile)
2280 (candidates . ,actions)
2281 ;; Override `anything-candidate-number-limit'
2282 (candidate-number-limit . 9999))))
2283 (set (make-local-variable 'anything-source-filter) nil)
2284 (set (make-local-variable 'anything-selection-overlay) nil)
2285 (set (make-local-variable 'anything-digit-overlays) nil)
2286 (anything-initialize-overlays anything-action-buffer))
2287 (with-selected-window (minibuffer-window)
2288 (delete-minibuffer-contents))
2289 (setq anything-pattern 'dummy) ; so that it differs from the
2290 ; previous one
2292 (anything-check-minibuffer-input))))))
2294 ;; (@* "Core: selection")
2295 (defun anything-move-selection-common (move-func unit direction)
2296 "Move the selection marker to a new position determined by
2297 UNIT and DIRECTION."
2298 (unless (or (anything-empty-buffer-p (anything-buffer-get))
2299 (not (anything-window)))
2300 (with-anything-window
2301 (funcall move-func)
2302 (while (and (not (bobp))
2303 (or (anything-pos-header-line-p)
2304 (anything-pos-candidate-separator-p)))
2305 (forward-line (if (and (eq direction 'previous)
2306 (not (eq (line-beginning-position) (point-min))))
2308 1)))
2309 (and (bobp) (forward-line 1)) ;skip first header
2310 (and (eobp) (forward-line -1)) ;avoid last empty line
2311 (when (and anything-display-source-at-screen-top (eq unit 'source))
2312 (set-window-start (selected-window)
2313 (save-excursion (forward-line -1) (point))))
2314 (when (anything-get-previous-header-pos)
2315 (anything-mark-current-line))
2316 (anything-display-mode-line (anything-get-current-source)))))
2318 (defvar anything-mode-line-string-real nil)
2319 (defun anything-display-mode-line (source)
2320 (set (make-local-variable 'anything-mode-line-string)
2321 (anything-interpret-value (or (assoc-default 'mode-line source)
2322 (default-value 'anything-mode-line-string))
2323 source))
2324 (if anything-mode-line-string
2325 (setq mode-line-format
2326 '(" " mode-line-buffer-identification " "
2327 (line-number-mode "%l") " " (anything-follow-mode "(F)")
2328 " " anything-mode-line-string-real "-%-")
2329 anything-mode-line-string-real
2330 (substitute-command-keys anything-mode-line-string))
2331 (setq mode-line-format
2332 (default-value 'mode-line-format)))
2333 (setq header-line-format
2334 (anything-interpret-value (assoc-default 'header-line source) source)))
2336 (defun anything-previous-line ()
2337 "Move selection to the previous line."
2338 (interactive)
2339 (anything-move-selection-common
2340 (lambda ()
2341 (forward-line -1)
2342 (when (anything-pos-multiline-p)
2343 (if (or (anything-pos-header-line-p)
2344 (anything-pos-candidate-separator-p))
2345 (forward-line -1)
2346 (forward-line 1))
2347 (let ((header-pos (anything-get-previous-header-pos))
2348 (candidate-pos (anything-get-previous-candidate-separator-pos)))
2349 (when header-pos
2350 (if (or (null candidate-pos) (< candidate-pos header-pos))
2351 (goto-char header-pos)
2352 (goto-char candidate-pos))
2353 (forward-line 1)))))
2354 'line 'previous))
2356 (defun anything-next-line ()
2357 "Move selection to the next line."
2358 (interactive)
2359 (anything-move-selection-common
2360 (lambda ()
2361 (if (not (anything-pos-multiline-p))
2362 (forward-line 1)
2363 (let ((header-pos (anything-get-next-header-pos))
2364 (candidate-pos (anything-get-next-candidate-separator-pos)))
2365 (if (and candidate-pos
2366 (or (null header-pos)
2367 (< candidate-pos header-pos)))
2368 (goto-char candidate-pos)
2369 (if header-pos
2370 (goto-char header-pos)))
2371 (if candidate-pos
2372 (forward-line 1)))))
2373 'line 'next))
2375 (defun anything-previous-page ()
2376 "Move selection back with a pageful."
2377 (interactive)
2378 (anything-move-selection-common
2379 (lambda ()
2380 (condition-case nil
2381 (scroll-down)
2382 (beginning-of-buffer (goto-char (point-min)))))
2383 'page 'previous))
2385 (defun anything-next-page ()
2386 "Move selection forward with a pageful."
2387 (interactive)
2388 (anything-move-selection-common
2389 (lambda ()
2390 (condition-case nil
2391 (scroll-up)
2392 (end-of-buffer (goto-char (point-max)))))
2393 'page 'next))
2395 (defun anything-beginning-of-buffer ()
2396 "Move selection at the top."
2397 (interactive)
2398 (anything-move-selection-common (lambda () (goto-char (point-min)))
2399 'edge 'previous))
2401 (defun anything-end-of-buffer ()
2402 "Move selection at the bottom."
2403 (interactive)
2404 (anything-move-selection-common (lambda () (goto-char (point-max)))
2405 'edge 'next))
2407 (defun anything-previous-source ()
2408 "Move selection to the previous source."
2409 (interactive)
2410 (anything-move-selection-common
2411 (lambda ()
2412 (forward-line -1)
2413 (if (bobp)
2414 (goto-char (point-max))
2415 (if (anything-pos-header-line-p)
2416 (forward-line -1)
2417 (forward-line 1)))
2418 (goto-char (anything-get-previous-header-pos))
2419 (forward-line 1))
2420 'source 'previous))
2422 (defun anything-next-source ()
2423 "Move selection to the next source."
2424 (interactive)
2425 (anything-move-selection-common
2426 (lambda () (goto-char (or (anything-get-next-header-pos) (point-min))))
2427 'source 'next))
2429 (defun anything-goto-source (source-or-name)
2430 "Move the selection to the source (SOURCE-OR-NAME)."
2431 (anything-move-selection-common
2432 (lambda ()
2433 (goto-char (point-min))
2434 (let ((name (if (stringp source-or-name) source-or-name
2435 (assoc-default 'name source-or-name))))
2436 (while (not (string= name (anything-current-line-contents)))
2437 (goto-char (anything-get-next-header-pos)))))
2438 'source 'next))
2440 (defun anything-mark-current-line ()
2441 "Move selection overlay to current line."
2442 (move-overlay anything-selection-overlay
2443 (line-beginning-position)
2444 (if (anything-pos-multiline-p)
2445 (let ((header-pos (anything-get-next-header-pos))
2446 (candidate-pos (anything-get-next-candidate-separator-pos)))
2447 (or (and (null header-pos) candidate-pos candidate-pos)
2448 (and header-pos candidate-pos (< candidate-pos header-pos) candidate-pos)
2449 header-pos
2450 (point-max)))
2451 (1+ (line-end-position))))
2452 (anything-follow-execute-persistent-action-maybe))
2454 (defun anything-this-command-key ()
2455 (event-basic-type (elt (this-command-keys-vector) 0)))
2456 ;; (progn (read-key-sequence "Key: ") (p (anything-this-command-key)))
2458 (defun anything-select-with-digit-shortcut ()
2459 (interactive)
2460 (if (eq anything-enable-shortcuts 'alphabet)
2461 (save-selected-window
2462 (select-window (anything-window))
2463 (let* ((index (position (anything-this-command-key) anything-shortcut-keys))
2464 (overlay (nth index anything-digit-overlays)))
2465 (when (overlay-buffer overlay)
2466 (goto-char (overlay-start overlay))
2467 (anything-mark-current-line)
2468 (anything-exit-minibuffer))))
2469 (self-insert-command 1)))
2471 ;;; EXPERIMENTAL TODO documentation
2472 (defun anything-select-with-prefix-shortcut ()
2473 (interactive)
2474 (if (eq anything-enable-shortcuts 'prefix)
2475 (save-selected-window
2476 (select-window (anything-window))
2477 (let* ((key (read-event "Select shortcut key: "))
2478 (index (position key anything-shortcut-keys))
2479 (overlay (ignore-errors (nth index anything-digit-overlays))))
2480 (if (not (and overlay (overlay-buffer overlay)))
2481 (when (numberp key)
2482 (select-window (minibuffer-window))
2483 (self-insert-command 1))
2484 (goto-char (overlay-start overlay))
2485 (anything-mark-current-line)
2486 (anything-exit-minibuffer))))
2487 (self-insert-command 1)))
2488 ;; (setq anything-enable-shortcuts 'prefix)
2489 ;; (define-key anything-map "@" 'anything-select-with-prefix-shortcut)
2490 ;; (define-key anything-map (kbd "<f18>") 'anything-select-with-prefix-shortcut)
2492 (defun anything-exit-minibuffer ()
2493 "Select the current candidate by exiting the minibuffer."
2494 (interactive)
2495 (declare (special anything-iswitchb-candidate-selected))
2496 (setq anything-iswitchb-candidate-selected (anything-get-selection))
2497 (exit-minibuffer))
2500 (defun anything-get-next-header-pos ()
2501 "Return the position of the next header from point."
2502 (next-single-property-change (point) 'anything-header))
2505 (defun anything-get-previous-header-pos ()
2506 "Return the position of the previous header from point"
2507 (previous-single-property-change (point) 'anything-header))
2510 (defun anything-pos-multiline-p ()
2511 "Return non-nil if the current position is in the multiline source region."
2512 (get-text-property (point) 'anything-multiline))
2515 (defun anything-get-next-candidate-separator-pos ()
2516 "Return the position of the next candidate separator from point."
2517 (next-single-property-change (point) 'anything-candidate-separator))
2520 (defun anything-get-previous-candidate-separator-pos ()
2521 "Return the position of the previous candidate separator from point."
2522 (previous-single-property-change (point) 'anything-candidate-separator))
2525 (defun anything-pos-header-line-p ()
2526 "Return t if the current line is a header line."
2527 (or (get-text-property (line-beginning-position) 'anything-header)
2528 (get-text-property (line-beginning-position) 'anything-header-separator)))
2530 (defun anything-pos-candidate-separator-p ()
2531 "Return t if the current line is a candidate separator."
2532 (get-text-property (line-beginning-position) 'anything-candidate-separator))
2534 ;; (@* "Core: error handling")
2535 (defun anything-log-error (&rest args)
2536 "Accumulate error messages into `anything-issued-errors'."
2537 (let ((msg (apply 'format args)))
2538 (unless (member msg anything-issued-errors)
2539 (add-to-list 'anything-issued-errors msg))))
2541 (defun anything-print-error-messages ()
2542 "Print error messages in `anything-issued-errors'."
2543 (message "%s" (mapconcat 'identity (reverse anything-issued-errors) "\n")))
2546 ;; (@* "Core: help")
2547 (defun anything-help-internal (bufname insert-content-fn)
2548 "Show long message during `anything' session."
2549 (save-window-excursion
2550 (select-window (anything-window))
2551 (delete-other-windows)
2552 (switch-to-buffer (get-buffer-create bufname))
2553 (erase-buffer)
2554 (funcall insert-content-fn)
2555 (setq mode-line-format "%b (SPC,C-v:NextPage b,M-v:PrevPage other:Exit)")
2556 (setq cursor-type nil)
2557 (goto-char 1)
2558 (ignore-errors
2559 (loop for event = (read-event) do
2560 (case event
2561 ((?\C-v ? ) (scroll-up))
2562 ((?\M-v ?b) (scroll-down))
2563 (t (return)))))))
2565 (defun anything-help ()
2566 "Help of `anything'."
2567 (interactive)
2568 (anything-help-internal
2569 " *Anything Help*"
2570 (lambda ()
2571 (insert (substitute-command-keys
2572 (anything-interpret-value anything-help-message)))
2573 (org-mode))))
2575 (defun anything-debug-output ()
2576 "Show all anything-related variables at this time."
2577 (interactive)
2578 (anything-help-internal " *Anything Debug*" 'anything-debug-output-function))
2580 (defun anything-debug-output-function (&optional vars)
2581 (message "Calculating all anything-related values...")
2582 (insert "If you debug some variables or forms, set `anything-debug-forms'
2583 to a list of forms.\n\n")
2584 (dolist (v (or vars
2585 anything-debug-forms
2586 (apropos-internal "^anything-" 'boundp)))
2587 (insert "** "
2588 (pp-to-string v) "\n"
2589 (pp-to-string (eval v)) "\n"))
2590 (message "Calculating all anything-related values...Done")
2593 ;; (@* "Core: misc")
2594 (defun anything-kill-buffer-hook ()
2595 "Remove tick entry from `anything-tick-hash' when killing a buffer."
2596 (loop for key being the hash-keys in anything-tick-hash
2597 if (string-match (format "^%s/" (regexp-quote (buffer-name))) key)
2598 do (remhash key anything-tick-hash)))
2599 (add-hook 'kill-buffer-hook 'anything-kill-buffer-hook)
2601 (defun anything-maybe-fit-frame ()
2602 "Fit anything frame to its buffer, and put it at top right of display.
2604 It is disabled by default because some flickering occurred in some environment.
2605 To enable fitting, set both `anything-inhibit-fit-frame-flag' and
2606 `fit-frame-inhibit-fitting' to nil.
2607 You can set user options `fit-frame-max-width-percent' and
2608 `fit-frame-max-height-percent' to control max frame size."
2609 (declare (warn (unresolved 0)))
2610 (when (and (require 'fit-frame nil t)
2611 (boundp 'fit-frame-inhibit-fitting-flag)
2612 (not anything-inhibit-fit-frame-flag)
2613 (not fit-frame-inhibit-fitting-flag)
2614 (anything-window))
2615 (ignore-errors
2616 (with-anything-window
2617 (fit-frame nil nil nil t)
2618 (modify-frame-parameters
2619 (selected-frame)
2620 `((left . ,(- (x-display-pixel-width) (+ (frame-pixel-width) 7)))
2621 (top . 0))))))) ; The (top . 0) shouldn't be necessary (Emacs bug).
2623 (defun anything-preselect (candidate-or-regexp)
2624 (with-anything-window
2625 (when candidate-or-regexp
2626 (goto-char (point-min))
2627 ;; go to first candidate of first source
2628 (forward-line 1)
2629 (let ((start (point)))
2630 (unless (or (re-search-forward (concat "^" (regexp-quote candidate-or-regexp) "$") nil t)
2631 (progn (goto-char start)
2632 (re-search-forward candidate-or-regexp nil t)))
2633 (goto-char start))))
2634 (anything-mark-current-line)))
2636 (defun anything-delete-current-selection ()
2637 "Delete the currently selected item."
2638 (interactive)
2639 (with-anything-window
2640 (cond ((anything-pos-multiline-p)
2641 (anything-aif (anything-get-next-candidate-separator-pos)
2642 (delete-region (point-at-bol)
2643 (1+ (progn (goto-char it) (point-at-eol))))
2644 ;; last candidate
2645 (goto-char (anything-get-previous-candidate-separator-pos))
2646 (delete-region (point-at-bol) (point-max)))
2647 (when (eobp)
2648 (goto-char (or (anything-get-previous-candidate-separator-pos)
2649 (point-min)))
2650 (forward-line 1)))
2652 (delete-region (point-at-bol) (1+ (point-at-eol)))
2653 (when (eobp) (forward-line -1))))
2654 (anything-mark-current-line)))
2656 (defun anything-edit-current-selection-1 (func)
2657 (with-anything-window
2658 (beginning-of-line)
2659 (let ((realvalue (get-text-property (point) 'anything-realvalue)))
2660 (funcall func)
2661 (beginning-of-line)
2662 (and realvalue
2663 (put-text-property (point) (point-at-eol) 'anything-realvalue realvalue))
2664 (anything-mark-current-line))))
2666 (defmacro anything-edit-current-selection (&rest forms)
2667 "Evaluate FORMS at current selection in the anything buffer.
2668 You can edit the line."
2669 `(anything-edit-current-selection-1
2670 (lambda () ,@forms)))
2671 (put 'anything-edit-current-selection 'lisp-indent-function 0)
2673 (defun anything-set-pattern (pattern)
2674 "Set minibuffer contents to PATTERN."
2675 (with-selected-window (minibuffer-window)
2676 (delete-minibuffer-contents)
2677 (insert pattern)))
2679 (defun anything-delete-minibuffer-content ()
2680 "Same as `delete-minibuffer-contents' but this is a command."
2681 (interactive)
2682 (anything-set-pattern ""))
2684 ;; (@* "Built-in plug-in: type")
2685 (defun anything-compile-source--type (source)
2686 (anything-aif (assoc-default 'type source)
2687 (append source (assoc-default it anything-type-attributes) nil)
2688 source))
2690 ;; `define-anything-type-attribute' is public API.
2692 (defun anything-add-type-attribute (type definition)
2693 (anything-aif (assq type anything-type-attributes)
2694 (setq anything-type-attributes (delete it anything-type-attributes)))
2695 (push (cons type definition) anything-type-attributes))
2697 (defvar anything-types nil)
2698 (defun anything-document-type-attribute (type doc)
2699 (add-to-list 'anything-types type t)
2700 (put type 'anything-typeattrdoc
2701 (concat "- " (symbol-name type) "\n\n" doc "\n")))
2703 (defadvice documentation-property (after anything-document-type-attribute activate)
2704 "Hack to display type attributes' documentation as `anything-type-attributes' docstring."
2705 (when (eq symbol 'anything-type-attributes)
2706 (setq ad-return-value
2707 (concat ad-return-value "\n\n++++ Types currently defined ++++\n"
2708 (mapconcat (lambda (sym) (get sym 'anything-typeattrdoc))
2709 anything-types "\n")))))
2711 ;; (@* "Built-in plug-in: dummy")
2712 (defun anything-dummy-candidate (candidate source)
2713 ;; `source' is defined in filtered-candidate-transformer
2714 (list anything-pattern))
2716 (defun anything-compile-source--dummy (source)
2717 (if (assoc 'dummy source)
2718 (append source
2719 '((candidates "dummy")
2720 (accept-empty)
2721 (match identity)
2722 (filtered-candidate-transformer . anything-dummy-candidate)
2723 (disable-shortcuts)
2724 (volatile)))
2725 source))
2727 ;; (@* "Built-in plug-in: disable-shortcuts")
2728 (defvar anything-orig-enable-shortcuts nil)
2729 (defun anything-save-enable-shortcuts ()
2730 (anything-once
2731 (lambda () (setq anything-orig-enable-shortcuts anything-enable-shortcuts
2732 anything-enable-shortcuts nil))))
2733 (defun anything-compile-source--disable-shortcuts (source)
2734 (if (assoc 'disable-shortcuts source)
2735 (append `((init ,@(anything-mklist (assoc-default 'init source))
2736 anything-save-enable-shortcuts)
2737 (resume ,@(anything-mklist (assoc-default 'resume source))
2738 anything-save-enable-shortcuts)
2739 (cleanup ,@(anything-mklist (assoc-default 'cleanup source))
2740 (lambda () (setq anything-enable-shortcuts anything-orig-enable-shortcuts))))
2741 source)
2742 source))
2744 ;; (@* "Built-in plug-in: candidates-in-buffer")
2745 (defun anything-candidates-in-buffer ()
2746 "Get candidates from the candidates buffer according to `anything-pattern'.
2748 BUFFER is `anything-candidate-buffer' by default. Each
2749 candidate must be placed in one line. This function is meant to
2750 be used in candidates-in-buffer or candidates attribute of an
2751 anything source. Especially fast for many (1000+) candidates.
2754 '((name . \"many files\")
2755 (init . (lambda () (with-current-buffer (anything-candidate-buffer 'local)
2756 (insert-many-filenames))))
2757 (search re-search-forward) ; optional
2758 (candidates-in-buffer)
2759 (type . file))
2761 +===============================================================+
2762 | The new way of making and narrowing candidates: Using buffers |
2763 +===============================================================+
2765 By default, `anything' makes candidates by evaluating the
2766 candidates function, then narrows them by `string-match' for each
2767 candidate.
2769 But this way is very slow for many candidates. The new way is
2770 storing all candidates in a buffer and narrowing them by
2771 `re-search-forward'. Search function is customizable by search
2772 attribute. The important point is that buffer processing is MUCH
2773 FASTER than string list processing and is the Emacs way.
2775 The init function writes all candidates to a newly-created
2776 candidate buffer. The candidates buffer is created or specified
2777 by `anything-candidate-buffer'. Candidates are stored in a line.
2779 The candidates function narrows all candidates, IOW creates a
2780 subset of candidates dynamically. It is the task of
2781 `anything-candidates-in-buffer'. As long as
2782 `anything-candidate-buffer' is used,`(candidates-in-buffer)' is
2783 sufficient in most cases.
2785 Note that `(candidates-in-buffer)' is shortcut of three attributes:
2786 (candidates . anything-candidates-in-buffer)
2787 (volatile)
2788 (match identity)
2789 And `(candidates-in-buffer . func)' is shortcut of three attributes:
2790 (candidates . func)
2791 (volatile)
2792 (match identity)
2793 The expansion is performed in `anything-get-sources'.
2795 The candidates-in-buffer attribute implies the volatile attribute.
2796 The volatile attribute is needed because `anything-candidates-in-buffer'
2797 creates candidates dynamically and need to be called everytime
2798 `anything-pattern' changes.
2800 Because `anything-candidates-in-buffer' plays the role of `match' attribute
2801 function, specifying `(match identity)' makes the source slightly faster.
2803 To customize `anything-candidates-in-buffer' behavior, use search,
2804 get-line and search-from-end attributes. See also `anything-sources' docstring.
2806 (declare (special source))
2807 (anything-candidates-in-buffer-1 (anything-candidate-buffer)
2808 anything-pattern
2809 (or (assoc-default 'get-line source)
2810 #'buffer-substring-no-properties)
2811 ;; use external variable `source'.
2812 (or (assoc-default 'search source)
2813 (if (assoc 'search-from-end source)
2814 '(re-search-backward)
2815 '(re-search-forward)))
2816 (anything-candidate-number-limit source)
2817 (assoc 'search-from-end source)))
2819 (defun* anything-candidates-in-buffer-1 (buffer &optional (pattern anything-pattern) (get-line-fn 'buffer-substring-no-properties) (search-fns '(re-search-forward)) (limit anything-candidate-number-limit) search-from-end)
2820 ;; buffer == nil when candidates buffer does not exist.
2821 (when buffer
2822 (with-current-buffer buffer
2823 (let ((start-point (if search-from-end (point-max) (point-min)))
2824 (next-line-fn (if search-from-end
2825 (lambda (x) (goto-char (max (1- (point-at-bol)) 1)))
2826 #'forward-line))
2827 (endp (if search-from-end #'bobp #'eobp)))
2828 (goto-char (1- start-point))
2829 (if (string= pattern "")
2830 (delq nil (loop until (funcall endp)
2831 for i from 1 to limit
2832 collecting (funcall get-line-fn (point-at-bol) (point-at-eol))
2833 do (funcall next-line-fn 1)))
2835 (let ((i 1)
2836 (next-line-fn (if search-from-end
2837 (lambda (x) (goto-char (max (point-at-bol) 1)))
2838 #'forward-line))
2839 buffer-read-only
2840 matches exit newmatches)
2841 (progn
2842 (goto-char (point-min))
2843 (insert "\n")
2844 (goto-char (point-max))
2845 (insert "\n")
2846 (setq start-point (if search-from-end (point-max) (point-min)))
2847 (clrhash anything-cib-hash)
2848 (unwind-protect
2849 (dolist (searcher search-fns)
2850 (goto-char start-point)
2851 (setq newmatches nil)
2852 (loop while (funcall searcher pattern nil t)
2853 if (or (funcall endp) (< limit i))
2854 do (setq exit t) (return)
2855 else do
2856 (let ((cand (funcall get-line-fn (point-at-bol) (point-at-eol))))
2857 (unless (gethash cand anything-cib-hash)
2858 (puthash cand t anything-cib-hash)
2859 (incf i)
2860 (push cand newmatches)))
2861 (funcall next-line-fn 1))
2862 (setq matches (append matches (nreverse newmatches)))
2863 (if exit (return)))
2864 (goto-char (point-min))
2865 (delete-char 1)
2866 (goto-char (1- (point-max)))
2867 (delete-char 1)
2869 (set-buffer-modified-p nil)))
2870 (delq nil matches)))))))
2873 (defun anything-candidate-buffer (&optional create-or-buffer)
2874 "Register and return a buffer containing candidates of current source.
2875 `anything-candidate-buffer' searches buffer-local candidates buffer first,
2876 then global candidates buffer.
2878 Acceptable values of CREATE-OR-BUFFER:
2880 - nil (omit)
2881 Only return the candidates buffer.
2882 - a buffer
2883 Register a buffer as a candidates buffer.
2884 - 'global
2885 Create a new global candidates buffer,
2886 named \" *anything candidates:SOURCE*\".
2887 - other non-nil value
2888 Create a new global candidates buffer,
2889 named \" *anything candidates:SOURCE*ANYTHING-CURRENT-BUFFER\".
2891 (let* ((gbufname (format " *anything candidates:%s*" anything-source-name))
2892 (lbufname (concat gbufname (buffer-name anything-current-buffer)))
2893 buf)
2894 (when create-or-buffer
2895 (if (bufferp create-or-buffer)
2896 (setq anything-candidate-buffer-alist
2897 (cons (cons anything-source-name create-or-buffer)
2898 (delete (assoc anything-source-name anything-candidate-buffer-alist)
2899 anything-candidate-buffer-alist)))
2900 (add-to-list 'anything-candidate-buffer-alist
2901 (cons anything-source-name create-or-buffer))
2902 (when (eq create-or-buffer 'global)
2903 (loop for b in (buffer-list)
2904 if (string-match (format "^%s" (regexp-quote gbufname)) (buffer-name b))
2905 do (kill-buffer b)))
2906 (with-current-buffer
2907 (get-buffer-create (if (eq create-or-buffer 'global) gbufname lbufname))
2908 (buffer-disable-undo)
2909 (erase-buffer)
2910 (font-lock-mode -1))))
2911 (or (get-buffer lbufname)
2912 (get-buffer gbufname)
2913 (anything-aif (assoc-default anything-source-name anything-candidate-buffer-alist)
2914 (and (buffer-live-p it) it)))))
2916 (defun anything-compile-source--candidates-in-buffer (source)
2917 (anything-aif (assoc 'candidates-in-buffer source)
2918 (append source `((candidates . ,(or (cdr it) 'anything-candidates-in-buffer))
2919 (volatile) (match identity)))
2920 source))
2922 ;; (@* "Utility: resplit anything window")
2923 (defun anything-toggle-resplit-window ()
2924 "Toggle resplit anything window, vertically or horizontally."
2925 (interactive)
2926 (with-anything-window
2927 (let ((before-height (window-height)))
2928 (delete-window)
2929 (set-window-buffer
2930 (select-window (if (= (window-height) before-height)
2931 (split-window-vertically)
2932 (split-window-horizontally)))
2933 anything-buffer))))
2935 ;; (@* "Utility: select another action by key")
2936 (defun anything-select-nth-action (n)
2937 "Select the nth action for the currently selected candidate."
2938 (setq anything-saved-selection (anything-get-selection))
2939 (unless anything-saved-selection
2940 (error "Nothing is selected."))
2941 (setq anything-saved-action (cdr (elt (anything-get-action) n)))
2942 (anything-exit-minibuffer))
2944 (defun anything-select-2nd-action ()
2945 "Select the 2nd action for the currently selected candidate."
2946 (interactive)
2947 (anything-select-nth-action 1))
2949 (defun anything-select-3rd-action ()
2950 "Select the 3rd action for the currently selected candidate."
2951 (interactive)
2952 (anything-select-nth-action 2))
2954 (defun anything-select-4th-action ()
2955 "Select the 4th action for the currently selected candidate."
2956 (interactive)
2957 (anything-select-nth-action 3))
2959 (defun anything-select-2nd-action-or-end-of-line ()
2960 "Select the 2nd action for the currently selected candidate if the point is at the end of minibuffer.
2961 Otherwise goto the end of minibuffer."
2962 (interactive)
2963 (if (eolp)
2964 (anything-select-nth-action 1)
2965 (end-of-line)))
2967 ;; (@* "Utility: Persistent Action")
2968 (defmacro with-anything-display-same-window (&rest body)
2969 "Make `pop-to-buffer' and `display-buffer' display in the same window."
2970 `(let ((display-buffer-function 'anything-persistent-action-display-buffer))
2971 ,@body))
2972 (put 'with-anything-display-same-window 'lisp-indent-function 0)
2974 (defun* anything-execute-persistent-action (&optional (attr 'persistent-action))
2975 "If a candidate is selected then perform the associated action without quitting anything."
2976 (interactive)
2977 (anything-log "executing persistent-action")
2978 (save-selected-window
2979 (select-window (get-buffer-window (anything-buffer-get)))
2980 (select-window (setq minibuffer-scroll-window
2981 (if (one-window-p t) (split-window)
2982 (next-window (selected-window) 1))))
2983 (anything-log-eval (current-buffer))
2984 (let ((anything-in-persistent-action t))
2985 (with-anything-display-same-window
2986 (anything-execute-selection-action
2988 (or (assoc-default attr (anything-get-current-source))
2989 (anything-get-action))
2991 (anything-log-run-hook 'anything-after-persistent-action-hook)))))
2993 (defun anything-persistent-action-display-buffer (buf &optional not-this-window)
2994 "Make `pop-to-buffer' and `display-buffer' display in the same window in persistent action.
2995 If `anything-persistent-action-use-special-display' is non-nil and
2996 BUF is to be displayed by `special-display-function', use it.
2997 Otherwise ignores `special-display-buffer-names' and `special-display-regexps'."
2998 (let* ((name (buffer-name buf))
2999 display-buffer-function pop-up-windows
3000 (same-window-regexps
3001 (unless (and anything-persistent-action-use-special-display
3002 (or (member name
3003 (mapcar (lambda (x) (or (car-safe x) x)) special-display-buffer-names))
3004 (remove-if-not
3005 (lambda (x) (string-match (or (car-safe x) x) name))
3006 special-display-regexps)))
3007 '("."))))
3008 (display-buffer buf not-this-window)))
3010 ;; scroll-other-window(-down)? for persistent-action
3011 (defun anything-scroll-other-window-base (command)
3012 (save-selected-window
3013 (select-window
3014 (some-window
3015 (lambda (w) (not (string= anything-buffer (buffer-name (window-buffer w)))))
3016 'no-minibuffer 'current-frame))
3017 (call-interactively command)))
3019 (defun anything-scroll-other-window ()
3020 "Scroll other window (not *Anything* window) upward."
3021 (interactive)
3022 (anything-scroll-other-window-base (lambda ()
3023 (interactive)
3024 (scroll-up anything-scroll-amount))))
3025 (defun anything-scroll-other-window-down ()
3026 "Scroll other window (not *Anything* window) downward."
3027 (interactive)
3028 (anything-scroll-other-window-base (lambda ()
3029 (interactive)
3030 (scroll-down anything-scroll-amount))))
3032 ;; (@* "Utility: Visible Mark")
3033 (defface anything-visible-mark
3034 '((((min-colors 88) (background dark))
3035 (:background "green1" :foreground "black"))
3036 (((background dark)) (:background "green" :foreground "black"))
3037 (((min-colors 88)) (:background "green1"))
3038 (t (:background "green")))
3039 "Face for visible mark."
3040 :group 'anything)
3041 (defvar anything-visible-mark-face 'anything-visible-mark)
3042 (defvar anything-visible-mark-overlays nil)
3044 (defun anything-clear-visible-mark ()
3045 (with-current-buffer (anything-buffer-get)
3046 (mapc 'delete-overlay anything-visible-mark-overlays)
3047 (set (make-local-variable 'anything-visible-mark-overlays)
3048 nil)))
3049 (add-hook 'anything-after-initialize-hook 'anything-clear-visible-mark)
3051 (defvar anything-c-marked-candidate-list nil
3052 "[OBSOLETE] DO NOT USE!!")
3053 (defvar anything-marked-candidates nil
3054 "Marked candadates. List of (source . real) pair.")
3055 (defun anything-toggle-visible-mark ()
3056 (interactive)
3057 (with-anything-window
3058 (let ((display (anything-current-line-contents))
3059 (source (anything-get-current-source))
3060 (selection (anything-get-selection)))
3061 (anything-aif (loop for o in anything-visible-mark-overlays
3062 when (equal (line-beginning-position) (overlay-start o))
3063 do (return o))
3064 ;; delete
3065 (progn
3066 (setq anything-c-marked-candidate-list
3067 (remove
3068 display anything-c-marked-candidate-list))
3069 (setq anything-marked-candidates
3070 (remove
3071 (cons source selection)
3072 anything-marked-candidates))
3073 (delete-overlay it)
3074 (setq anything-visible-mark-overlays (delq it anything-visible-mark-overlays)))
3075 (let ((o (make-overlay (line-beginning-position) (1+ (line-end-position)))))
3076 (overlay-put o 'face anything-visible-mark-face)
3077 (overlay-put o 'source (assoc-default 'name source))
3078 (overlay-put o 'string (buffer-substring (overlay-start o) (overlay-end o)))
3079 (add-to-list 'anything-visible-mark-overlays o)
3080 (push display anything-c-marked-candidate-list)
3081 (push (cons source selection) anything-marked-candidates)))
3082 (anything-next-line))))
3084 (defun anything-display-all-visible-marks ()
3085 "Show all `anything' visible marks strings."
3086 (interactive)
3087 (lexical-let ((overlays (reverse anything-visible-mark-overlays)))
3088 (anything-run-after-quit
3089 (lambda ()
3090 (with-output-to-temp-buffer "*anything visible marks*"
3091 (dolist (o overlays) (princ (overlay-get o 'string))))))))
3093 (defun anything-marked-candidates ()
3094 "Marked candidates (real value) of current source if any,
3095 otherwise 1-element list of current selection.
3097 It is analogous to `dired-get-marked-files'."
3098 (with-current-buffer (anything-buffer-get)
3099 (let ((cands (if anything-marked-candidates
3100 (loop with current-src = (anything-get-current-source)
3101 for (source . real) in (reverse anything-marked-candidates)
3102 when (equal current-src source)
3103 collect real)
3104 (list (anything-get-selection)))))
3105 (anything-log-eval cands)
3106 cands)))
3108 (defun anything-reset-marked-candidates ()
3109 (with-current-buffer (anything-buffer-get)
3110 (set (make-local-variable 'anything-c-marked-candidate-list) nil)
3111 (set (make-local-variable 'anything-marked-candidates) nil)))
3113 (add-hook 'anything-after-initialize-hook 'anything-reset-marked-candidates)
3114 ;; (add-hook 'anything-after-action-hook 'anything-reset-marked-candidates)
3116 (defun anything-revive-visible-mark ()
3117 (interactive)
3118 (with-current-buffer anything-buffer
3119 (loop for o in anything-visible-mark-overlays do
3120 (goto-char (point-min))
3121 (let (moved)
3122 (while (and (not moved)
3123 (search-forward (overlay-get o 'string) nil t))
3124 (forward-line -1)
3125 (when (and (save-excursion
3126 (goto-char (anything-get-previous-header-pos))
3127 (equal (overlay-get o 'source)
3128 (anything-current-line-contents)))
3129 (not (find-if (lambda (x)
3130 (memq x anything-visible-mark-overlays))
3131 (overlays-at (point)))))
3132 (move-overlay o (line-beginning-position) (1+ (line-end-position)))
3133 (setq moved t))
3134 (forward-line 1))))))
3135 (add-hook 'anything-update-hook 'anything-revive-visible-mark)
3137 (defun anything-next-visible-mark (&optional prev)
3138 (interactive)
3139 (with-anything-window
3140 (block 'exit
3141 (setq anything-visible-mark-overlays
3142 (sort* anything-visible-mark-overlays
3143 '< :key 'overlay-start))
3144 (let ((i (position-if (lambda (o) (< (point) (overlay-start o)))
3145 anything-visible-mark-overlays)))
3146 (when prev
3147 (unless anything-visible-mark-overlays (return-from 'exit nil))
3148 (if (not i) (setq i (length anything-visible-mark-overlays)))
3149 (if (equal (point) (overlay-start (nth (1- i) anything-visible-mark-overlays)))
3150 (setq i (1- i))))
3151 (when i
3152 (goto-char (overlay-start (nth (if prev (1- i) i) anything-visible-mark-overlays)))
3153 (anything-mark-current-line))))))
3155 (defun anything-prev-visible-mark ()
3156 (interactive)
3157 (anything-next-visible-mark t))
3159 ;; (@* "Utility: `find-file' integration")
3160 (defun anything-quit-and-find-file ()
3161 "Drop into `find-file' from `anything' like `iswitchb-find-file'.
3162 If current selection is a buffer or a file, `find-file' from its directory."
3163 (interactive)
3164 (anything-run-after-quit
3165 (lambda (f)
3166 (if (file-exists-p f)
3167 (let ((default-directory (file-name-directory f)))
3168 (call-interactively 'find-file))
3169 (call-interactively 'find-file)))
3170 (anything-aif (get-buffer (anything-get-selection))
3171 (buffer-file-name it)
3172 (expand-file-name (anything-get-selection)))))
3174 ;; (@* "Utility: Selection Paste")
3175 (defun anything-yank-selection ()
3176 "Set minibuffer contents to current selection."
3177 (interactive)
3178 (anything-set-pattern (anything-get-selection nil t)))
3180 (defun anything-kill-selection-and-quit ()
3181 "Store current selection to kill ring.
3182 You can paste it by typing C-y."
3183 (interactive)
3184 (anything-run-after-quit
3185 (lambda (sel)
3186 (kill-new sel)
3187 (message "Killed: %s" sel))
3188 (anything-get-selection nil t)))
3191 ;; (@* "Utility: Automatical execution of persistent-action")
3192 (add-to-list 'minor-mode-alist '(anything-follow-mode " AFollow"))
3193 (defun anything-follow-mode ()
3194 "If this mode is on, persistent action is executed everytime the cursor is moved."
3195 (interactive)
3196 (with-current-buffer anything-buffer
3197 (setq anything-follow-mode (not anything-follow-mode))
3198 (message "anything-follow-mode is %s"
3199 (if anything-follow-mode "enabled" "disabled"))))
3201 (defun anything-follow-execute-persistent-action-maybe ()
3202 "Execute persistent action after `anything-input-idle-delay' secs when `anything-follow-mode' is enabled."
3203 (and (buffer-local-value 'anything-follow-mode
3204 (get-buffer-create anything-buffer))
3205 (sit-for anything-input-idle-delay)
3206 (anything-window)
3207 (anything-get-selection)
3208 (save-excursion
3209 (anything-execute-persistent-action))))
3211 ;; (@* "Utility: Migrate `anything-sources' to my-anything command")
3212 (defun anything-migrate-sources ()
3213 "Help to migrate to new `anything' way."
3214 (interactive)
3215 (with-current-buffer (get-buffer-create "*anything migrate*")
3216 (erase-buffer)
3217 (insert (format "\
3218 Setting `anything-sources' directly is not good because
3219 `anything' is not for one command. For now, interactive use of
3220 `anything' (M-x anything) is only for demonstration purpose.
3221 So you should define commands calling `anything'.
3222 I help you to migrate to the new way.
3224 The code below is automatically generated from current
3225 `anything-sources' value. You can use the `my-anything' command
3226 now!
3228 Copy and paste it to your .emacs. Then substitute `my-anything'
3229 for `anything' bindings in all `define-key', `local-set-key' and
3230 `global-set-key' calls.
3232 \(defun my-anything ()
3233 \"Anything command for you.
3235 It is automatically generated by `anything-migrate-sources'.\"
3236 (interactive)
3237 (anything-other-buffer
3239 \"*my-anything*\"))
3240 " anything-sources))
3241 (eval-last-sexp nil)
3242 (substitute-key-definition 'anything 'my-anything global-map)
3243 (pop-to-buffer (current-buffer))))
3245 ;; (@* "Utility: Incremental search within results (unmaintained)")
3247 (defvar anything-isearch-original-global-map nil
3248 "Original global map before Anything isearch is started.")
3250 (defvar anything-isearch-original-message-timeout nil
3251 "Original message timeout before Anything isearch is started.")
3253 (defvar anything-isearch-pattern nil
3254 "The current isearch pattern.")
3256 (defvar anything-isearch-message-suffix ""
3257 "Message suffix indicating the current state of the search.")
3259 (defvar anything-isearch-original-point nil
3260 "Original position of point before isearch is started.")
3262 (defvar anything-isearch-original-window nil
3263 "Original selected window before isearch is started.")
3265 (defvar anything-isearch-original-cursor-in-non-selected-windows nil
3266 "Original value of cursor-in-non-selected-windows before isearch is started.")
3268 (defvar anything-isearch-original-deferred-action-list nil
3269 "Original value of deferred-action-list before isearch is started.")
3271 (defvar anything-isearch-match-positions nil
3272 "Stack of positions of matches or non-matches.
3274 It's a list of plists with two properties: `event', the last user
3275 event, `start', the start position of the current match, and
3276 `pos', the position of point after that event.
3278 The value of `event' can be the following symbols: `char' if a
3279 character was typed, `error' if a non-matching character was
3280 typed, `search' if a forward search had to be done after a
3281 character, and `search-again' if a search was done for the next
3282 occurrence of the current pattern.")
3284 (defvar anything-isearch-match-start nil
3285 "Start position of the current match.")
3288 (defun anything-isearch ()
3289 "Start incremental search within results. (UNMAINTAINED)"
3290 (interactive)
3291 (if (anything-empty-buffer-p (anything-buffer-get))
3292 (message "There are no results.")
3294 (setq anything-isearch-original-message-timeout minibuffer-message-timeout)
3295 (setq minibuffer-message-timeout nil)
3297 (setq anything-isearch-original-global-map global-map)
3299 (condition-case nil
3300 (progn
3301 (setq anything-isearch-original-window (selected-window))
3302 (select-window (anything-window))
3303 (setq cursor-type t)
3305 (setq anything-isearch-original-deferred-action-list
3306 (default-value 'deferred-action-list))
3307 (setq-default deferred-action-list nil)
3308 (add-hook 'deferred-action-list 'anything-isearch-post-command)
3310 (use-global-map anything-isearch-map)
3311 (setq overriding-terminal-local-map anything-isearch-map)
3313 (setq anything-isearch-pattern "")
3315 (setq anything-isearch-original-cursor-in-non-selected-windows
3316 cursor-in-non-selected-windows)
3317 (setq cursor-in-non-selected-windows nil)
3319 (setq anything-isearch-original-point (point-marker))
3320 (goto-char (point-min))
3321 (forward-line)
3322 (anything-mark-current-line)
3324 (setq anything-isearch-match-positions nil)
3325 (setq anything-isearch-match-start (point-marker))
3327 (if anything-isearch-overlay
3328 ;; make sure the overlay belongs to the anything buffer
3329 (move-overlay anything-isearch-overlay (point-min) (point-min)
3330 (get-buffer (anything-buffer-get)))
3332 (setq anything-isearch-overlay (make-overlay (point-min) (point-min)))
3333 (overlay-put anything-isearch-overlay 'face anything-isearch-match-face))
3335 (setq anything-isearch-message-suffix
3336 (substitute-command-keys "cancel with \\[anything-isearch-cancel]")))
3338 (error (anything-isearch-cleanup)))))
3341 (defun anything-isearch-post-command ()
3342 "Print the current pattern after every command."
3343 (anything-isearch-message)
3344 (when (anything-window)
3345 (with-anything-window
3346 (move-overlay anything-isearch-overlay anything-isearch-match-start (point)
3347 (get-buffer (anything-buffer-get))))))
3350 (defun anything-isearch-printing-char ()
3351 "Add printing char to the pattern."
3352 (interactive)
3353 (let ((char (char-to-string last-command-event)))
3354 (setq anything-isearch-pattern (concat anything-isearch-pattern char))
3356 (with-anything-window
3357 (if (looking-at char)
3358 (progn
3359 (push (list 'event 'char
3360 'start anything-isearch-match-start
3361 'pos (point-marker))
3362 anything-isearch-match-positions)
3363 (forward-char))
3365 (let ((start (point)))
3366 (while (and (re-search-forward anything-isearch-pattern nil t)
3367 (anything-pos-header-line-p)))
3368 (if (or (anything-pos-header-line-p)
3369 (eq start (point)))
3370 (progn
3371 (goto-char start)
3372 (push (list 'event 'error
3373 'start anything-isearch-match-start
3374 'pos (point-marker))
3375 anything-isearch-match-positions))
3377 (push (list 'event 'search
3378 'start anything-isearch-match-start
3379 'pos (copy-marker start))
3380 anything-isearch-match-positions)
3381 (setq anything-isearch-match-start (copy-marker (match-beginning 0))))))
3383 (anything-mark-current-line))))
3386 (defun anything-isearch-again ()
3387 "Search again for the current pattern"
3388 (interactive)
3389 (if (equal anything-isearch-pattern "")
3390 (setq anything-isearch-message-suffix "no pattern yet")
3392 (with-anything-window
3393 (let ((start (point)))
3394 (while (and (re-search-forward anything-isearch-pattern nil t)
3395 (anything-pos-header-line-p)))
3396 (if (or (anything-pos-header-line-p)
3397 (eq start (point)))
3398 (progn
3399 (goto-char start)
3400 (unless (eq 'error (plist-get (car anything-isearch-match-positions)
3401 'event))
3402 (setq anything-isearch-message-suffix "no more matches")))
3404 (push (list 'event 'search-again
3405 'start anything-isearch-match-start
3406 'pos (copy-marker start))
3407 anything-isearch-match-positions)
3408 (setq anything-isearch-match-start (copy-marker (match-beginning 0)))
3410 (anything-mark-current-line))))))
3413 (defun anything-isearch-delete ()
3414 "Undo last event."
3415 (interactive)
3416 (unless (equal anything-isearch-pattern "")
3417 (let ((last (pop anything-isearch-match-positions)))
3418 (unless (eq 'search-again (plist-get last 'event))
3419 (setq anything-isearch-pattern
3420 (substring anything-isearch-pattern 0 -1)))
3422 (with-anything-window
3423 (goto-char (plist-get last 'pos))
3424 (setq anything-isearch-match-start (plist-get last 'start))
3425 (anything-mark-current-line)))))
3428 (defun anything-isearch-default-action ()
3429 "Execute the default action for the selected candidate."
3430 (interactive)
3431 (anything-isearch-cleanup)
3432 (with-current-buffer (anything-buffer-get) (anything-exit-minibuffer)))
3435 (defun anything-isearch-select-action ()
3436 "Choose an action for the selected candidate."
3437 (interactive)
3438 (anything-isearch-cleanup)
3439 (with-anything-window
3440 (anything-select-action)))
3443 (defun anything-isearch-cancel ()
3444 "Cancel Anything isearch."
3445 (interactive)
3446 (anything-isearch-cleanup)
3447 (when (anything-window)
3448 (with-anything-window
3449 (goto-char anything-isearch-original-point)
3450 (anything-mark-current-line))))
3453 (defun anything-isearch-cleanup ()
3454 "Clean up the mess."
3455 (setq minibuffer-message-timeout anything-isearch-original-message-timeout)
3456 (with-current-buffer (anything-buffer-get)
3457 (setq overriding-terminal-local-map nil)
3458 (setq cursor-type nil)
3459 (setq cursor-in-non-selected-windows
3460 anything-isearch-original-cursor-in-non-selected-windows))
3461 (when anything-isearch-original-window
3462 (select-window anything-isearch-original-window))
3464 (use-global-map anything-isearch-original-global-map)
3465 (setq-default deferred-action-list anything-isearch-original-deferred-action-list)
3466 (when (overlayp anything-isearch-overlay)
3467 (delete-overlay anything-isearch-overlay)))
3470 (defun anything-isearch-message ()
3471 "Print prompt."
3472 (if (and (equal anything-isearch-message-suffix "")
3473 (eq (plist-get (car anything-isearch-match-positions) 'event)
3474 'error))
3475 (setq anything-isearch-message-suffix "failing"))
3477 (unless (equal anything-isearch-message-suffix "")
3478 (setq anything-isearch-message-suffix
3479 (concat " [" anything-isearch-message-suffix "]")))
3481 (message (concat "Search within results: "
3482 anything-isearch-pattern
3483 anything-isearch-message-suffix))
3485 (setq anything-isearch-message-suffix ""))
3488 ;; (@* "Utility: Iswitchb integration (unmaintained)")
3490 (defvar anything-iswitchb-candidate-selected nil
3491 "Indicates whether an anything candidate is selected from iswitchb.")
3493 (defvar anything-iswitchb-frame-configuration nil
3494 "Saved frame configuration, before anything buffer was displayed.")
3496 (defvar anything-iswitchb-saved-keys nil
3497 "The original in iswitchb before binding anything keys.")
3500 (defun anything-iswitchb-setup ()
3501 "Integrate anything completion into iswitchb (UNMAINTAINED).
3503 If the user is idle for `anything-iswitchb-idle-delay' seconds
3504 after typing something into iswitchb then anything candidates are
3505 shown for the current iswitchb input.
3507 ESC cancels anything completion and returns to normal iswitchb.
3509 Some key bindings in `anything-map' are modified.
3510 See also `anything-iswitchb-setup-keys'."
3511 (interactive)
3513 (require 'iswitchb)
3515 ;; disable timid completion during iswitchb
3516 (put 'iswitchb-buffer 'timid-completion 'disabled)
3517 (add-hook 'minibuffer-setup-hook 'anything-iswitchb-minibuffer-setup)
3519 (defadvice iswitchb-visit-buffer
3520 (around anything-iswitchb-visit-buffer activate)
3521 (if anything-iswitchb-candidate-selected
3522 (anything-execute-selection-action)
3523 ad-do-it))
3525 (defadvice iswitchb-possible-new-buffer
3526 (around anything-iswitchb-possible-new-buffer activate)
3527 (if anything-iswitchb-candidate-selected
3528 (anything-execute-selection-action)
3529 ad-do-it))
3530 (anything-iswitchb-setup-keys)
3531 (message "Iswitchb integration is activated."))
3533 (defun anything-iswitchb-setup-keys ()
3534 "Modify `anything-map' for anything-iswitchb users.
3536 C-p is used instead of M-p, because anything uses ESC
3537 (currently hardcoded) for `anything-iswitchb-cancel-anything' and
3538 Emacs handles ESC and Meta as synonyms, so ESC overrides
3539 other commands with Meta prefix.
3541 Note that iswitchb uses M-p and M-n by default for history
3542 navigation, so you should bind C-p and C-n in
3543 `iswitchb-mode-map' if you use the history keys and don't want
3544 to use different keys for iswitchb while anything is not yet
3545 kicked in. These keys are not bound automatically by anything
3546 in `iswitchb-mode-map' because they (C-n at least) already have
3547 a standard iswitchb binding which you might be accustomed to.
3549 Binding M-s is used instead of C-s, because C-s has a binding in
3550 iswitchb. You can rebind it AFTER `anything-iswitchb-setup'.
3552 Unbind C-r to prevent problems during anything-isearch."
3553 (define-key anything-map (kbd "C-s") nil)
3554 (define-key anything-map (kbd "M-p") nil)
3555 (define-key anything-map (kbd "M-n") nil)
3556 (define-key anything-map (kbd "M-v") nil)
3557 (define-key anything-map (kbd "C-v") nil)
3558 (define-key anything-map (kbd "C-p") 'anything-previous-history-element)
3559 (define-key anything-map (kbd "C-n") 'anything-next-history-element)
3560 (define-key anything-map (kbd "M-s") nil)
3561 (define-key anything-map (kbd "M-s") 'anything-isearch)
3562 (define-key anything-map (kbd "C-r") nil))
3564 (defun anything-iswitchb-minibuffer-setup ()
3565 (when (eq this-command 'iswitchb-buffer)
3566 (add-hook 'minibuffer-exit-hook 'anything-iswitchb-minibuffer-exit)
3568 (setq anything-iswitchb-frame-configuration nil)
3569 (setq anything-iswitchb-candidate-selected nil)
3570 (add-hook 'anything-update-hook 'anything-iswitchb-handle-update)
3572 (anything-initialize)
3574 (add-hook 'deferred-action-list 'anything-iswitchb-check-input)))
3577 (defun anything-iswitchb-minibuffer-exit ()
3578 (remove-hook 'minibuffer-exit-hook 'anything-iswitchb-minibuffer-exit)
3579 (remove-hook 'deferred-action-list 'anything-iswitchb-check-input)
3580 (remove-hook 'anything-update-hook 'anything-iswitchb-handle-update)
3582 (anything-cleanup)
3584 (when anything-iswitchb-frame-configuration
3585 (anything-set-frame/window-configuration anything-iswitchb-frame-configuration)
3586 (setq anything-iswitchb-frame-configuration nil)))
3589 (defun anything-iswitchb-check-input ()
3590 "Extract iswitchb input and check if it needs to be handled."
3591 (declare (special iswitchb-text))
3592 (if (or anything-iswitchb-frame-configuration
3593 (sit-for anything-iswitchb-idle-delay))
3594 (anything-check-new-input iswitchb-text)))
3597 (defun anything-iswitchb-handle-update ()
3598 "Pop up the anything buffer if it's not empty and it's not
3599 shown yet and bind anything commands in iswitchb."
3600 (unless (or (anything-empty-buffer-p anything-buffer)
3601 anything-iswitchb-frame-configuration)
3602 (setq anything-iswitchb-frame-configuration (anything-current-frame/window-configuration))
3604 (save-selected-window
3605 (if (not anything-samewindow)
3606 (pop-to-buffer anything-buffer)
3608 (select-window (get-lru-window))
3609 (switch-to-buffer anything-buffer)))
3611 (with-current-buffer (window-buffer (active-minibuffer-window))
3612 (let* ((anything-prefix "anything-")
3613 (prefix-length (length anything-prefix))
3614 (commands
3615 (delete-dups
3616 (remove-if 'null
3617 (mapcar
3618 (lambda (binding)
3619 (let ((command (cdr binding)))
3620 (when (and (symbolp command)
3621 (eq (compare-strings
3622 anything-prefix
3623 0 prefix-length
3624 (symbol-name command)
3625 0 prefix-length)
3627 command)))
3628 (cdr anything-map)))))
3629 (bindings (mapcar (lambda (command)
3630 (cons command
3631 (where-is-internal command anything-map)))
3632 commands)))
3634 (push (list 'anything-iswitchb-cancel-anything (kbd "<ESC>"))
3635 bindings)
3637 (setq anything-iswitchb-saved-keys nil)
3639 (let* ((iswitchb-prefix "iswitchb-")
3640 (prefix-length (length iswitchb-prefix)))
3641 (dolist (binding bindings)
3642 (dolist (key (cdr binding))
3643 (let ((old-command (lookup-key (current-local-map) key)))
3644 (unless (and anything-iswitchb-dont-touch-iswithcb-keys
3645 (symbolp old-command)
3646 (eq (compare-strings iswitchb-prefix
3647 0 prefix-length
3648 (symbol-name old-command)
3649 0 prefix-length)
3651 (push (cons key old-command)
3652 anything-iswitchb-saved-keys)
3653 (define-key (current-local-map) key (car binding)))))))))))
3656 (defun anything-iswitchb-cancel-anything ()
3657 "Cancel anything completion and return to standard iswitchb."
3658 (interactive)
3659 (save-excursion
3660 (dolist (binding anything-iswitchb-saved-keys)
3661 (define-key (current-local-map) (car binding) (cdr binding)))
3662 (anything-iswitchb-minibuffer-exit)))
3664 ;; (@* "Compatibility")
3666 ;; Copied assoc-default from XEmacs version 21.5.12
3667 (unless (fboundp 'assoc-default)
3668 (defun assoc-default (key alist &optional test default)
3669 "Find object KEY in a pseudo-alist ALIST.
3670 ALIST is a list of conses or objects. Each element (or the element's car,
3671 if it is a cons) is compared with KEY by evaluating (TEST (car elt) KEY).
3672 If that is non-nil, the element matches;
3673 then `assoc-default' returns the element's cdr, if it is a cons,
3674 or DEFAULT if the element is not a cons.
3676 If no element matches, the value is nil.
3677 If TEST is omitted or nil, `equal' is used."
3678 (let (found (tail alist) value)
3679 (while (and tail (not found))
3680 (let ((elt (car tail)))
3681 (when (funcall (or test 'equal) (if (consp elt) (car elt) elt) key)
3682 (setq found t value (if (consp elt) (cdr elt) default))))
3683 (setq tail (cdr tail)))
3684 value)))
3686 ;; Function not available in XEmacs,
3687 (unless (fboundp 'minibuffer-contents)
3688 (defun minibuffer-contents ()
3689 "Return the user input in a minbuffer as a string.
3690 The current buffer must be a minibuffer."
3691 (field-string (point-max)))
3693 (defun delete-minibuffer-contents ()
3694 "Delete all user input in a minibuffer.
3695 The current buffer must be a minibuffer."
3696 (delete-field (point-max))))
3698 ;; Function not available in older Emacs (<= 22.1).
3699 (unless (fboundp 'buffer-chars-modified-tick)
3700 (defun buffer-chars-modified-tick (&optional buffer)
3701 "Return BUFFER's character-change tick counter.
3702 Each buffer has a character-change tick counter, which is set to the
3703 value of the buffer's tick counter (see `buffer-modified-tick'), each
3704 time text in that buffer is inserted or deleted. By comparing the
3705 values returned by two individual calls of `buffer-chars-modified-tick',
3706 you can tell whether a character change occurred in that buffer in
3707 between these calls. No argument or nil as argument means use current
3708 buffer as BUFFER."
3709 (with-current-buffer (or buffer (current-buffer))
3710 (if (listp buffer-undo-list)
3711 (length buffer-undo-list)
3712 (buffer-modified-tick)))))
3714 ;; (@* "CUA workaround")
3715 (defadvice cua-delete-region (around anything-avoid-cua activate)
3716 (ignore-errors ad-do-it))
3717 (defadvice copy-region-as-kill (around anything-avoid-cua activate)
3718 (if cua-mode
3719 (ignore-errors ad-do-it)
3720 ad-do-it))
3723 ;;(@* "Attribute Documentation")
3724 (defun anything-describe-anything-attribute (anything-attribute)
3725 "Display the full documentation of ANYTHING-ATTRIBUTE (a symbol)."
3726 (interactive (list (intern
3727 (completing-read
3728 "Describe anything attribute: "
3729 (mapcar 'symbol-name anything-additional-attributes)))))
3730 (with-output-to-temp-buffer "*Help*"
3731 (princ (get anything-attribute 'anything-attrdoc))))
3733 (anything-document-attribute 'name "mandatory"
3734 " The name of the source. It is also the heading which appears
3735 above the list of matches from the source. Must be unique. ")
3736 (anything-document-attribute 'header-name "optional"
3737 " A function returning the display string of the header. Its
3738 argument is the name of the source. This attribute is useful to
3739 add an additional information with the source name. ")
3740 (anything-document-attribute 'candidates "mandatory if candidates-in-buffer attribute is not provided"
3741 " Specifies how to retrieve candidates from the source. It can
3742 either be a variable name, a function called with no parameters
3743 or the actual list of candidates.
3745 The list must be a list whose members are strings, symbols
3746 or (DISPLAY . REAL) pairs.
3748 In case of (DISPLAY . REAL) pairs, the DISPLAY string is shown
3749 in the Anything buffer, but the REAL one is used as action
3750 argument when the candidate is selected. This allows a more
3751 readable presentation for candidates which would otherwise be,
3752 for example, too long or have a common part shared with other
3753 candidates which can be safely replaced with an abbreviated
3754 string for display purposes.
3756 Note that if the (DISPLAY . REAL) form is used then pattern
3757 matching is done on the displayed string, not on the real
3758 value.
3760 If the candidates have to be retrieved asynchronously (for
3761 example, by an external command which takes a while to run)
3762 then the function should start the external command
3763 asynchronously and return the associated process object.
3764 Anything will take care of managing the process (receiving the
3765 output from it, killing it if necessary, etc.). The process
3766 should return candidates matching the current pattern (see
3767 variable `anything-pattern'.)
3769 Note that currently results from asynchronous sources appear
3770 last in the anything buffer regardless of their position in
3771 `anything-sources'. ")
3772 (anything-document-attribute 'action "mandatory if type attribute is not provided"
3773 " It is a list of (DISPLAY . FUNCTION) pairs or FUNCTION.
3774 FUNCTION is called with one parameter: the selected candidate.
3776 An action other than the default can be chosen from this list
3777 of actions for the currently selected candidate (by default
3778 with TAB). The DISPLAY string is shown in the completions
3779 buffer and the FUNCTION is invoked when an action is
3780 selected. The first action of the list is the default. ")
3781 (anything-document-attribute 'type "optional if action attribute is provided"
3782 " Indicates the type of the items the source returns.
3784 Merge attributes not specified in the source itself from
3785 `anything-type-attributes'.
3787 This attribute is implemented by plug-in. ")
3788 (anything-document-attribute 'init "optional"
3789 " Function called with no parameters when anything is started. It
3790 is useful for collecting current state information which can be
3791 used to create the list of candidates later.
3793 For example, if a source needs to work with the current
3794 directory then it can store its value here, because later
3795 anything does its job in the minibuffer and in the
3796 `anything-buffer' and the current directory can be different
3797 there. ")
3798 (anything-document-attribute 'delayed-init "optional"
3799 " Function called with no parameters before candidate function is
3800 called. It is similar with `init' attribute, but its
3801 evaluation is deferred. It is useful to combine with ")
3802 (anything-document-attribute 'match "optional"
3803 " List of functions called with one parameter: a candidate. The
3804 function should return non-nil if the candidate matches the
3805 current pattern (see variable `anything-pattern').
3807 This attribute allows the source to override the default
3808 pattern matching based on `string-match'. It can be used, for
3809 example, to implement a source for file names and do the
3810 pattern matching on the basename of files, since it's more
3811 likely one is typing part of the basename when searching for a
3812 file, instead of some string anywhere else in its path.
3814 If the list contains more than one function then the list of
3815 matching candidates from the source is constructed by appending
3816 the results after invoking the first function on all the
3817 potential candidates, then the next function, and so on. The
3818 matching candidates supplied by the first function appear first
3819 in the list of results and then results from the other
3820 functions, respectively.
3822 This attribute has no effect for asynchronous sources (see
3823 attribute `candidates'), since they perform pattern matching
3824 themselves. ")
3825 (anything-document-attribute 'candidate-transformer "optional"
3826 " It's a function or a list of functions called with one argument
3827 when the completion list from the source is built. The argument
3828 is the list of candidates retrieved from the source. The
3829 function should return a transformed list of candidates which
3830 will be used for the actual completion. If it is a list of
3831 functions, it calls each function sequentially.
3833 This can be used to transform or remove items from the list of
3834 candidates.
3836 Note that `candidates' is run already, so the given transformer
3837 function should also be able to handle candidates with (DISPLAY
3838 . REAL) format. ")
3839 (anything-document-attribute 'filtered-candidate-transformer "optional"
3840 " It has the same format as `candidate-transformer', except the
3841 function is called with two parameters: the candidate list and
3842 the source.
3844 This transformer is run on the candidate list which is already
3845 filtered by the current pattern. While `candidate-transformer'
3846 is run only once, it is run every time the input pattern is
3847 changed.
3849 It can be used to transform the candidate list dynamically, for
3850 example, based on the current pattern.
3852 In some cases it may also be more efficent to perform candidate
3853 transformation here, instead of with `candidate-transformer'
3854 even if this transformation is done every time the pattern is
3855 changed. For example, if a candidate set is very large then
3856 `candidate-transformer' transforms every candidate while only
3857 some of them will actually be dislpayed due to the limit
3858 imposed by `anything-candidate-number-limit'.
3860 Note that `candidates' and `candidate-transformer' is run
3861 already, so the given transformer function should also be able
3862 to handle candidates with (DISPLAY . REAL) format.
3864 This option has no effect for asynchronous sources. (Not yet,
3865 at least. ")
3866 (anything-document-attribute 'action-transformer "optional"
3867 " It's a function or a list of functions called with two
3868 arguments when the action list from the source is
3869 assembled. The first argument is the list of actions, the
3870 second is the current selection. If it is a list of functions,
3871 it calls each function sequentially.
3873 The function should return a transformed action list.
3875 This can be used to customize the list of actions based on the
3876 currently selected candidate. ")
3877 (anything-document-attribute 'pattern-transformer "optional"
3878 " It's a function or a list of functions called with one argument
3879 before computing matches. Its argument is `anything-pattern'.
3880 Functions should return transformed `anything-pattern'.
3882 It is useful to change interpretation of `anything-pattern'. ")
3883 (anything-document-attribute 'delayed "optional"
3884 " Candidates from the source are shown only if the user stops
3885 typing and is idle for `anything-idle-delay' seconds. ")
3886 (anything-document-attribute 'volatile "optional"
3887 " Indicates the source assembles the candidate list dynamically,
3888 so it shouldn't be cached within a single Anything
3889 invocation. It is only applicable to synchronous sources,
3890 because asynchronous sources are not cached. ")
3891 (anything-document-attribute 'requires-pattern "optional"
3892 " If present matches from the source are shown only if the
3893 pattern is not empty. Optionally, it can have an integer
3894 parameter specifying the required length of input which is
3895 useful in case of sources with lots of candidates. ")
3896 (anything-document-attribute 'persistent-action "optional"
3897 " Function called with one parameter; the selected candidate.
3899 An action performed by `anything-execute-persistent-action'.
3900 If none, use the default action. ")
3901 (anything-document-attribute 'candidates-in-buffer "optional"
3902 " Shortcut attribute for making and narrowing candidates using
3903 buffers. This newly-introduced attribute prevents us from
3904 forgetting to add volatile and match attributes.
3906 See docstring of `anything-candidates-in-buffer'.
3908 (candidates-in-buffer) is equivalent of three attributes:
3909 (candidates . anything-candidates-in-buffer)
3910 (volatile)
3911 (match identity)
3913 (candidates-in-buffer . candidates-function) is equivalent of:
3914 (candidates . candidates-function)
3915 (volatile)
3916 (match identity)
3918 This attribute is implemented by plug-in. ")
3919 (anything-document-attribute 'search "optional"
3920 " List of functions like `re-search-forward' or `search-forward'.
3921 Buffer search function used by `anything-candidates-in-buffer'.
3922 By default, `anything-candidates-in-buffer' uses `re-search-forward'.
3923 This attribute is meant to be used with
3924 (candidates . anything-candidates-in-buffer) or
3925 (candidates-in-buffer) in short. ")
3926 (anything-document-attribute 'search-from-end "optional"
3927 " Make `anything-candidates-in-buffer' search from the end of buffer.
3928 If this attribute is specified, `anything-candidates-in-buffer' uses
3929 `re-search-backward' instead. ")
3930 (anything-document-attribute 'get-line "optional"
3931 " A function like `buffer-substring-no-properties' or `buffer-substring'.
3932 This function converts point of line-beginning and point of line-end,
3933 which represents a candidate computed by `anything-candidates-in-buffer'.
3934 By default, `anything-candidates-in-buffer' uses
3935 `buffer-substring-no-properties'. ")
3936 (anything-document-attribute 'display-to-real "optional"
3937 " Function called with one parameter; the selected candidate.
3939 The function transforms the selected candidate, and the result
3940 is passed to the action function. The display-to-real
3941 attribute provides another way to pass other string than one
3942 shown in Anything buffer.
3944 Traditionally, it is possible to make candidates,
3945 candidate-transformer or filtered-candidate-transformer
3946 function return a list with (DISPLAY . REAL) pairs. But if REAL
3947 can be generated from DISPLAY, display-to-real is more
3948 convenient and faster. ")
3949 (anything-document-attribute 'real-to-display "optional"
3950 " Function called with one parameter; the selected candidate.
3952 The inverse of display-to-real attribute.
3954 The function transforms the selected candidate, which is passed
3955 to the action function, for display. The real-to-display
3956 attribute provides the other way to pass other string than one
3957 shown in Anything buffer.
3959 Traditionally, it is possible to make candidates,
3960 candidate-transformer or filtered-candidate-transformer
3961 function return a list with (DISPLAY . REAL) pairs. But if
3962 DISPLAY can be generated from REAL, real-to-display is more
3963 convenient.
3965 Note that DISPLAY parts returned from candidates /
3966 candidate-transformer are IGNORED as the name `display-to-real'
3967 says. ")
3968 (anything-document-attribute 'cleanup "optional"
3969 " Function called with no parameters when *anything* buffer is closed. It
3970 is useful for killing unneeded candidates buffer.
3972 Note that the function is executed BEFORE performing action. ")
3973 (anything-document-attribute 'candidate-number-limit "optional"
3974 " Override `anything-candidate-number-limit' only for this source. ")
3975 (anything-document-attribute 'accept-empty "optional"
3976 " Pass empty string \"\" to action function. ")
3977 (anything-document-attribute 'disable-shortcuts "optional"
3978 " Disable `anything-enable-shortcuts' in current `anything' session.
3980 This attribute is implemented by plug-in. ")
3981 (anything-document-attribute 'dummy "optional"
3982 " Set `anything-pattern' to candidate. If this attribute is
3983 specified, The candidates attribute is ignored.
3985 This attribute is implemented by plug-in.
3986 This plug-in implies disable-shortcuts plug-in. ")
3987 (anything-document-attribute 'multiline "optional"
3988 " Enable to selection multiline candidates. ")
3989 (anything-document-attribute 'update "optional"
3990 " Function called with no parameters when \\<anything-map>\\[anything-force-update] is pressed. ")
3991 (anything-document-attribute 'mode-line "optional"
3992 " source local `anything-mode-line-string'. (included in `mode-line-format')
3993 It accepts also variable/function name. ")
3994 (anything-document-attribute 'header-line "optional"
3995 " source local `header-line-format'.
3996 It accepts also variable/function name. ")
3997 (anything-document-attribute 'resume "optional" " Function called with no parameters when `anything-resume' is started.")
3999 ;; (@* "Bug Report")
4000 (defvar anything-maintainer-mail-address
4001 (concat "rubiki" "tch@ru" "by-lang.org"))
4002 (defvar anything-bug-report-salutation
4003 "Describe bug below, using a precise recipe.
4005 When I executed M-x ...
4007 How to send a bug report:
4008 1) Be sure to use the LATEST version of anything.el.
4009 2) Enable debugger. M-x toggle-debug-on-error or (setq debug-on-error t)
4010 3) Use Lisp version instead of compiled one: (load \"anything.el\")
4011 4) If you got an error, please paste *Backtrace* buffer.
4012 5) Type C-c C-c to send.
4013 # If you are a Japanese, please write in Japanese:-)")
4014 (defvar anything-no-dump-variables
4015 '(anything-candidate-buffer-alist
4016 anything-digit-overlays
4017 anything-help-message
4018 anything-candidate-cache
4020 "Variables not to dump in bug report.")
4022 (defun anything-dumped-variables-in-bug-report ()
4023 (let ((hash (make-hash-table)))
4024 (loop for var in (apropos-internal "anything-" 'boundp)
4025 for vname = (symbol-name var)
4026 unless (or (string-match "-map$" vname)
4027 (string-match "^anything-c-source-" vname)
4028 (string-match "-hash$" vname)
4029 (string-match "-face$" vname)
4030 (memq var anything-no-dump-variables))
4031 collect var)))
4033 (defun anything-send-bug-report ()
4034 "Send a bug report of anything.el."
4035 (interactive)
4036 (with-current-buffer (or anything-last-buffer
4037 (current-buffer))
4038 (reporter-submit-bug-report
4039 anything-maintainer-mail-address
4040 "anything.el"
4041 (anything-dumped-variables-in-bug-report)
4042 nil nil
4043 anything-bug-report-salutation)))
4045 (defun anything-send-bug-report-from-anything ()
4046 "Send a bug report of anything.el in anything session."
4047 (interactive)
4048 (anything-run-after-quit 'anything-send-bug-report))
4050 ;; (@* "Unit Tests")
4052 (defun* anything-test-candidates (sources &optional (input "") (compile-source-functions anything-compile-source-functions-default))
4053 "Test helper function for anything.
4054 Given pseudo `anything-sources' and `anything-pattern', returns list like
4055 ((\"source name1\" (\"candidate1\" \"candidate2\"))
4056 (\"source name2\" (\"candidate3\" \"candidate4\")))
4058 (let ((anything-test-mode t)
4059 anything-enable-shortcuts
4060 anything-candidate-cache
4061 (anything-compile-source-functions compile-source-functions)
4062 anything-before-initialize-hook
4063 anything-after-initialize-hook
4064 anything-update-hook
4065 anything-test-candidate-list)
4066 (get-buffer-create anything-buffer)
4068 (anything-initialize-1 nil input sources)
4069 (anything-update)
4070 ;; test-mode spec: select 1st candidate!
4071 (with-current-buffer anything-buffer
4072 (forward-line 1)
4073 (anything-mark-current-line))
4074 (prog1
4075 anything-test-candidate-list
4076 (anything-cleanup))))
4078 (defmacro anything-test-update (sources pattern)
4079 "Test helper macro for anything. It is meant for testing *anything* buffer contents."
4080 `(progn (stub anything-get-sources => ,sources)
4081 (stub anything-log-run-hook => nil)
4082 (stub anything-maybe-fit-frame => nil)
4083 (stub run-with-idle-timer => nil)
4084 (let (anything-test-mode (anything-pattern ,pattern))
4085 (anything-update))))
4087 ;;;; unit test
4088 ;; (install-elisp "http://www.emacswiki.org/cgi-bin/wiki/download/el-expectations.el")
4089 ;; (install-elisp "http://www.emacswiki.org/cgi-bin/wiki/download/el-mock.el")
4090 (dont-compile
4091 (when (fboundp 'expectations)
4092 (expectations
4093 (desc "anything-current-buffer")
4094 (expect "__a_buffer"
4095 (with-current-buffer (get-buffer-create "__a_buffer")
4096 (anything-test-candidates '(((name . "FOO"))) "")
4097 (prog1
4098 (buffer-name anything-current-buffer)
4099 (kill-buffer "__a_buffer")
4101 (desc "anything-buffer-file-name")
4102 (expect (regexp "/__a_file__")
4103 (with-current-buffer (get-buffer-create "__a_file__")
4104 (setq buffer-file-name "/__a_file__")
4105 (anything-test-candidates '(((name . "FOO"))) "")
4106 (prog1
4107 anything-buffer-file-name
4108 ;;(kill-buffer "__a_file__")
4110 (desc "anything-interpret-value")
4111 (expect "literal"
4112 (anything-interpret-value "literal"))
4113 (expect "lambda"
4114 (anything-interpret-value (lambda () "lambda")))
4115 (expect "lambda with source name"
4116 (let ((source '((name . "lambda with source name"))))
4117 (anything-interpret-value (lambda () anything-source-name) source)))
4118 (expect "function symbol"
4119 (flet ((f () "function symbol"))
4120 (anything-interpret-value 'f)))
4121 (expect "variable symbol"
4122 (let ((v "variable symbol"))
4123 (anything-interpret-value 'v)))
4124 (expect (error error *)
4125 (anything-interpret-value 'unbounded-1))
4126 (desc "anything-compile-sources")
4127 (expect '(((name . "foo")))
4128 (anything-compile-sources '(((name . "foo"))) nil)
4130 (expect '(((name . "foo") (type . test) (action . identity)))
4131 (let ((anything-type-attributes '((test (action . identity)))))
4132 (anything-compile-sources '(((name . "foo") (type . test)))
4133 '(anything-compile-source--type))))
4134 (desc "anything-sources accepts symbols")
4135 (expect '(((name . "foo")))
4136 (let* ((foo '((name . "foo"))))
4137 (anything-compile-sources '(foo) nil)))
4138 (desc "anything-get-sources action")
4139 (expect '(((name . "Actions") (candidates . actions)))
4140 (stub anything-action-window => t)
4141 (let (anything-compiled-sources
4142 (anything-sources '(((name . "Actions") (candidates . actions)))))
4143 (anything-get-sources)))
4144 (desc "get-buffer-create candidates-buffer")
4145 (expect '(((name . "many") (init . many-init)
4146 (candidates-in-buffer . anything-candidates-in-buffer)
4147 (candidates . anything-candidates-in-buffer)
4148 (volatile) (match identity)))
4149 (anything-compile-sources
4150 '(((name . "many") (init . many-init)
4151 (candidates-in-buffer . anything-candidates-in-buffer)))
4152 '(anything-compile-source--candidates-in-buffer)))
4153 (expect '(((name . "many") (init . many-init)
4154 (candidates-in-buffer)
4155 (candidates . anything-candidates-in-buffer)
4156 (volatile) (match identity)))
4157 (anything-compile-sources
4158 '(((name . "many") (init . many-init)
4159 (candidates-in-buffer)))
4160 '(anything-compile-source--candidates-in-buffer)))
4161 (expect '(((name . "many") (init . many-init)
4162 (candidates-in-buffer)
4163 (type . test)
4164 (action . identity)
4165 (candidates . anything-candidates-in-buffer)
4166 (volatile) (match identity)))
4167 (let ((anything-type-attributes '((test (action . identity)))))
4168 (anything-compile-sources
4169 '(((name . "many") (init . many-init)
4170 (candidates-in-buffer)
4171 (type . test)))
4172 '(anything-compile-source--type
4173 anything-compile-source--candidates-in-buffer))))
4175 (desc "anything-get-candidates")
4176 (expect '("foo" "bar")
4177 (anything-get-candidates '((name . "foo") (candidates "foo" "bar"))))
4178 (expect '("FOO" "BAR")
4179 (anything-get-candidates '((name . "foo") (candidates "foo" "bar")
4180 (candidate-transformer
4181 . (lambda (cands) (mapcar 'upcase cands))))))
4182 (expect '("foo" "bar")
4183 (anything-get-candidates '((name . "foo")
4184 (candidates . (lambda () '("foo" "bar"))))))
4185 (expect '("foo" "bar")
4186 (let ((var '("foo" "bar")))
4187 (anything-get-candidates '((name . "foo")
4188 (candidates . var)))))
4189 (expect (error error *)
4190 (anything-get-candidates '((name . "foo")
4191 (candidates . "err"))))
4192 (expect (error error *)
4193 (let ((var "err"))
4194 (anything-get-candidates '((name . "foo")
4195 (candidates . var)))))
4196 (expect (error error *)
4197 (anything-get-candidates '((name . "foo")
4198 (candidates . unDeFined-syMbol))))
4199 (desc "anything-compute-matches")
4200 (expect '("foo" "bar")
4201 (let ((anything-pattern ""))
4202 (anything-compute-matches '((name . "FOO") (candidates "foo" "bar") (volatile)))))
4203 (expect '("foo")
4204 (let ((anything-pattern "oo"))
4205 (anything-compute-matches '((name . "FOO") (candidates "foo" "bar") (volatile)))))
4206 (expect '("bar")
4207 (let ((anything-pattern "^b"))
4208 (anything-compute-matches '((name . "FOO") (candidates "foo" "bar") (volatile)))))
4209 (expect '("a" "b")
4210 (let ((anything-pattern "")
4211 (anything-candidate-number-limit 2))
4212 (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c") (volatile)))))
4213 (expect '("a" "b")
4214 (let ((anything-pattern ".")
4215 (anything-candidate-number-limit 2))
4216 (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c") (volatile)))))
4217 (expect '("a" "b" "c")
4218 (let ((anything-pattern "")
4219 anything-candidate-number-limit)
4220 (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c") (volatile)))))
4221 (expect '("a" "b" "c")
4222 (let ((anything-pattern "[abc]")
4223 anything-candidate-number-limit)
4224 (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c") (volatile)))))
4225 (expect '(a b c)
4226 (let ((anything-pattern "[abc]")
4227 anything-candidate-number-limit)
4228 (anything-compute-matches '((name . "FOO") (candidates a b c) (volatile)))))
4229 (expect '(("foo" . "FOO") ("bar" . "BAR"))
4230 (let ((anything-pattern ""))
4231 (anything-compute-matches '((name . "FOO") (candidates ("foo" . "FOO") ("bar" . "BAR")) (volatile)))))
4232 (expect '(("foo" . "FOO"))
4233 (let ((anything-pattern "foo"))
4234 (anything-compute-matches '((name . "FOO") (candidates ("foo" . "FOO") ("bar" . "foo")) (volatile)))))
4235 ;; using anything-test-candidate-list
4236 (desc "anything-test-candidates")
4237 (expect '(("FOO" ("foo" "bar")))
4238 (anything-test-candidates '(((name . "FOO") (candidates "foo" "bar")))))
4239 (expect '(("FOO" ("bar")))
4240 (anything-test-candidates '(((name . "FOO") (candidates "foo" "bar"))) "ar"))
4241 (expect '(("T1" ("hoge" "aiue"))
4242 ("T2" ("test" "boke")))
4243 (anything-test-candidates '(((name . "T1") (candidates "hoge" "aiue"))
4244 ((name . "T2") (candidates "test" "boke")))))
4245 (expect '(("T1" ("hoge"))
4246 ("T2" ("boke")))
4247 (anything-test-candidates '(((name . "T1") (candidates "hoge" "aiue"))
4248 ((name . "T2") (candidates "test" "boke"))) "o"))
4249 (desc "requires-pattern attribute")
4250 (expect nil
4251 (anything-test-candidates '(((name . "FOO") (candidates "foo" "bar")
4252 (requires-pattern . 1)))))
4253 (expect '(("FOO" ("bar")))
4254 (anything-test-candidates '(((name . "FOO") (candidates "foo" "bar")
4255 (requires-pattern . 1))) "b"))
4256 (desc "delayed attribute(for test)")
4257 (expect '(("T2" ("boke"))
4258 ("T1" ("hoge")))
4259 (anything-test-candidates
4260 '(((name . "T1") (candidates "hoge" "aiue") (delayed))
4261 ((name . "T2") (candidates "test" "boke")))
4262 "o"))
4263 (desc "match attribute(prefix search)")
4264 (expect '(("FOO" ("bar")))
4265 (anything-test-candidates
4266 '(((name . "FOO") (candidates "foo" "bar")
4267 (match (lambda (c) (string-match (concat "^" anything-pattern) c)))))
4268 "ba"))
4269 (expect nil
4270 (anything-test-candidates
4271 '(((name . "FOO") (candidates "foo" "bar")
4272 (match (lambda (c) (string-match (concat "^" anything-pattern) c)))))
4273 "ar"))
4274 (expect "TestSource"
4275 (let (x)
4276 (anything-test-candidates
4277 '(((name . "TestSource") (candidates "a")
4278 (match (lambda (c) (setq x anything-source-name)))))
4279 "a")
4281 (desc "init attribute")
4282 (expect '(("FOO" ("bar")))
4283 (let (v)
4284 (anything-test-candidates
4285 '(((name . "FOO") (init . (lambda () (setq v '("foo" "bar"))))
4286 (candidates . v)))
4287 "ar")))
4288 (desc "candidate-transformer attribute")
4289 (expect '(("FOO" ("BAR")))
4290 (anything-test-candidates '(((name . "FOO") (candidates "foo" "bar")
4291 (candidate-transformer
4292 . (lambda (cands) (mapcar 'upcase cands)))))
4293 "ar"))
4294 (desc "filtered-candidate-transformer attribute")
4295 ;; needs more tests
4296 (expect '(("FOO" ("BAR")))
4297 (anything-test-candidates '(((name . "FOO") (candidates "foo" "bar")
4298 (filtered-candidate-transformer
4299 . (lambda (cands src) (mapcar 'upcase cands)))))
4300 "ar"))
4301 (desc "anything-transform-candidates in process")
4302 (expect (mock (anything-composed-funcall-with-source
4303 '((name . "FOO") (candidates "foo" "bar")
4304 (filtered-candidate-transformer
4305 . (lambda (cands src) (mapcar 'upcase cands))))
4306 (lambda (cands src) (mapcar 'upcase cands))
4307 '("foo" "bar")
4308 '((name . "FOO") (candidates "foo" "bar")
4309 (filtered-candidate-transformer
4310 . (lambda (cands src) (mapcar 'upcase cands))))
4312 (stub anything-process-candidate-transformer => '("foo" "bar"))
4313 (anything-transform-candidates
4314 '("foo" "bar")
4315 '((name . "FOO") (candidates "foo" "bar")
4316 (filtered-candidate-transformer
4317 . (lambda (cands src) (mapcar 'upcase cands))))
4320 (desc "anything-candidates-in-buffer-1")
4321 (expect nil
4322 (anything-candidates-in-buffer-1 nil))
4323 (expect '("foo+" "bar+" "baz+")
4324 (with-temp-buffer
4325 (insert "foo+\nbar+\nbaz+\n")
4326 (let ((anything-candidate-number-limit 5))
4327 (anything-candidates-in-buffer-1 (current-buffer) ""))))
4328 (expect '("foo+" "bar+")
4329 (with-temp-buffer
4330 (insert "foo+\nbar+\nbaz+\n")
4331 (let ((anything-candidate-number-limit 2))
4332 (anything-candidates-in-buffer-1 (current-buffer) ""))))
4333 (expect '("foo+")
4334 (with-temp-buffer
4335 (insert "foo+\nbar+\nbaz+\n")
4336 (anything-candidates-in-buffer-1 (current-buffer) "oo\\+")))
4337 (expect '("foo+")
4338 (with-temp-buffer
4339 (insert "foo+\nbar+\nbaz+\n")
4340 (anything-candidates-in-buffer-1
4341 (current-buffer) "oo+"
4342 #'buffer-substring-no-properties '(search-forward))))
4343 (expect '(("foo+" "FOO+"))
4344 (with-temp-buffer
4345 (insert "foo+\nbar+\nbaz+\n")
4346 (anything-candidates-in-buffer-1
4347 (current-buffer) "oo\\+"
4348 (lambda (s e)
4349 (let ((l (buffer-substring-no-properties s e)))
4350 (list l (upcase l)))))))
4351 (desc "anything-candidates-in-buffer")
4352 (expect '(("TEST" ("foo+" "bar+" "baz+")))
4353 (anything-test-candidates
4354 '(((name . "TEST")
4355 (init
4356 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4357 (insert "foo+\nbar+\nbaz+\n"))))
4358 (candidates . anything-candidates-in-buffer)
4359 (match identity)
4360 (volatile)))))
4361 (expect '(("TEST" ("foo+" "bar+" "baz+")))
4362 (let (anything-candidate-number-limit)
4363 (anything-test-candidates
4364 '(((name . "TEST")
4365 (init
4366 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4367 (insert "foo+\nbar+\nbaz+\n"))))
4368 (candidates . anything-candidates-in-buffer)
4369 (match identity)
4370 (volatile))))))
4371 (expect '(("TEST" ("foo+")))
4372 (anything-test-candidates
4373 '(((name . "TEST")
4374 (init
4375 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4376 (insert "foo+\nbar+\nbaz+\n"))))
4377 (candidates . anything-candidates-in-buffer)
4378 (match identity)
4379 (volatile)))
4380 "oo\\+"))
4381 (desc "search attribute")
4382 (expect '(("TEST" ("foo+")))
4383 (anything-test-candidates
4384 '(((name . "TEST")
4385 (init
4386 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4387 (insert "foo+\nbar+\nbaz+\nooo\n"))))
4388 (search search-forward)
4389 (candidates . anything-candidates-in-buffer)
4390 (match identity)
4391 (volatile)))
4392 "oo+"))
4393 (expect '(("TEST" ("foo+" "ooo")))
4394 (anything-test-candidates
4395 '(((name . "TEST")
4396 (init
4397 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4398 (insert "foo+\nbar+\nbaz+\nooo\n"))))
4399 (search search-forward re-search-forward)
4400 (candidates . anything-candidates-in-buffer)
4401 (match identity)
4402 (volatile)))
4403 "oo+"))
4404 (expect '(("TEST" ("foo+" "ooo")))
4405 (anything-test-candidates
4406 '(((name . "TEST")
4407 (init
4408 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4409 (insert "foo+\nbar+\nbaz+\nooo\n"))))
4410 (search re-search-forward search-forward)
4411 (candidates . anything-candidates-in-buffer)
4412 (match identity)
4413 (volatile)))
4414 "oo+"))
4415 (expect '(("TEST" ("ooo" "foo+")))
4416 (anything-test-candidates
4417 '(((name . "TEST")
4418 (init
4419 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4420 (insert "bar+\nbaz+\nooo\nfoo+\n"))))
4421 (search re-search-forward search-forward)
4422 (candidates . anything-candidates-in-buffer)
4423 (match identity)
4424 (volatile)))
4425 "oo+"))
4426 ;; faster exact match
4427 (expect '(("TEST" ("bar+")))
4428 (anything-test-candidates
4429 '(((name . "TEST")
4430 (init
4431 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4432 (insert "bar+\nbaz+\nooo\nfoo+\n"))))
4433 (search (lambda (pattern &rest _)
4434 (and (search-forward (concat "\n" pattern "\n") nil t)
4435 (forward-line -1))))
4436 (candidates . anything-candidates-in-buffer)
4437 (match identity)
4438 (volatile)))
4439 "bar+"))
4440 ;; faster prefix match
4441 (expect '(("TEST" ("bar+")))
4442 (anything-test-candidates
4443 '(((name . "TEST")
4444 (init
4445 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4446 (insert "bar+\nbaz+\nooo\nfoo+\n"))))
4447 (search (lambda (pattern &rest _)
4448 (search-forward (concat "\n" pattern) nil t)))
4449 (candidates . anything-candidates-in-buffer)
4450 (match identity)
4451 (volatile)))
4452 "ba"))
4453 (desc "anything-current-buffer-is-modified")
4454 (expect '(("FOO" ("modified")))
4455 (let ((sources '(((name . "FOO")
4456 (candidates
4457 . (lambda ()
4458 (if (anything-current-buffer-is-modified)
4459 '("modified")
4460 '("unmodified"))))))))
4461 (with-temp-buffer
4462 (clrhash anything-tick-hash)
4463 (insert "1")
4464 (anything-test-candidates sources))))
4465 (expect '(("FOO" ("unmodified")))
4466 (let ((sources '(((name . "FOO")
4467 (candidates
4468 . (lambda ()
4469 (if (anything-current-buffer-is-modified)
4470 '("modified")
4471 '("unmodified"))))))))
4472 (with-temp-buffer
4473 (clrhash anything-tick-hash)
4474 (insert "1")
4475 (anything-test-candidates sources)
4476 (anything-test-candidates sources))))
4477 (expect '(("FOO" ("modified")))
4478 (let ((sources '(((name . "FOO")
4479 (candidates
4480 . (lambda ()
4481 (if (anything-current-buffer-is-modified)
4482 '("modified")
4483 '("unmodified"))))))))
4484 (with-temp-buffer
4485 (clrhash anything-tick-hash)
4486 (insert "1")
4487 (anything-test-candidates sources)
4488 (insert "2")
4489 (anything-test-candidates sources))))
4490 (expect '(("BAR" ("modified")))
4491 (let ((sources1 '(((name . "FOO")
4492 (candidates
4493 . (lambda ()
4494 (if (anything-current-buffer-is-modified)
4495 '("modified")
4496 '("unmodified")))))))
4497 (sources2 '(((name . "BAR")
4498 (candidates
4499 . (lambda ()
4500 (if (anything-current-buffer-is-modified)
4501 '("modified")
4502 '("unmodified"))))))))
4503 (with-temp-buffer
4504 (clrhash anything-tick-hash)
4505 (insert "1")
4506 (anything-test-candidates sources1)
4507 (anything-test-candidates sources2))))
4508 (expect '(("FOO" ("unmodified")))
4509 (let ((sources1 '(((name . "FOO")
4510 (candidates
4511 . (lambda ()
4512 (if (anything-current-buffer-is-modified)
4513 '("modified")
4514 '("unmodified")))))))
4515 (sources2 '(((name . "BAR")
4516 (candidates
4517 . (lambda ()
4518 (if (anything-current-buffer-is-modified)
4519 '("modified")
4520 '("unmodified"))))))))
4521 (with-temp-buffer
4522 (clrhash anything-tick-hash)
4523 (insert "1")
4524 (anything-test-candidates sources1)
4525 (anything-test-candidates sources2)
4526 (anything-test-candidates sources1))))
4527 (expect '(("BAR" ("unmodified")))
4528 (let ((sources1 '(((name . "FOO")
4529 (candidates
4530 . (lambda ()
4531 (if (anything-current-buffer-is-modified)
4532 '("modified")
4533 '("unmodified")))))))
4534 (sources2 '(((name . "BAR")
4535 (candidates
4536 . (lambda ()
4537 (if (anything-current-buffer-is-modified)
4538 '("modified")
4539 '("unmodified"))))))))
4540 (with-temp-buffer
4541 (clrhash anything-tick-hash)
4542 (insert "1")
4543 (anything-test-candidates sources1)
4544 (anything-test-candidates sources2)
4545 (anything-test-candidates sources2))))
4546 (expect '(("BAR" ("modified")))
4547 (let ((sources1 '(((name . "FOO")
4548 (candidates
4549 . (lambda ()
4550 (if (anything-current-buffer-is-modified)
4551 '("modified")
4552 '("unmodified")))))))
4553 (sources2 '(((name . "BAR")
4554 (candidates
4555 . (lambda ()
4556 (if (anything-current-buffer-is-modified)
4557 '("modified")
4558 '("unmodified"))))))))
4559 (with-temp-buffer
4560 (clrhash anything-tick-hash)
4561 (insert "1")
4562 (anything-test-candidates sources1)
4563 (anything-test-candidates sources2)
4564 (with-temp-buffer
4565 (anything-test-candidates sources2)))))
4566 (desc "anything-source-name")
4567 (expect "FOO"
4568 (let (v)
4569 (anything-test-candidates '(((name . "FOO")
4570 (init
4571 . (lambda () (setq v anything-source-name)))
4572 (candidates "ok"))))
4574 (expect "FOO"
4575 (let (v)
4576 (anything-test-candidates '(((name . "FOO")
4577 (candidates
4578 . (lambda ()
4579 (setq v anything-source-name)
4580 '("ok"))))))
4582 (expect "FOO"
4583 (let (v)
4584 (anything-test-candidates '(((name . "FOO")
4585 (candidates "ok")
4586 (candidate-transformer
4587 . (lambda (c)
4588 (setq v anything-source-name)
4589 c)))))
4591 (expect "FOO"
4592 (let (v)
4593 (anything-test-candidates '(((name . "FOO")
4594 (candidates "ok")
4595 (filtered-candidate-transformer
4596 . (lambda (c s)
4597 (setq v anything-source-name)
4598 c)))))
4600 (expect "FOO"
4601 (let (v)
4602 (anything-test-candidates '(((name . "FOO")
4603 (candidates "ok")
4604 (display-to-real
4605 . (lambda (c)
4606 (setq v anything-source-name)
4608 (action . identity))))
4609 (anything-execute-selection-action)
4611 (desc "anything-candidate-buffer create")
4612 (expect " *anything candidates:FOO*"
4613 (let* (anything-candidate-buffer-alist
4614 (anything-source-name "FOO")
4615 (buf (anything-candidate-buffer 'global)))
4616 (prog1 (buffer-name buf)
4617 (kill-buffer buf))))
4618 (expect " *anything candidates:FOO*aTestBuffer"
4619 (let* (anything-candidate-buffer-alist
4620 (anything-source-name "FOO")
4621 (anything-current-buffer (get-buffer-create "aTestBuffer"))
4622 (buf (anything-candidate-buffer 'local)))
4623 (prog1 (buffer-name buf)
4624 (kill-buffer anything-current-buffer)
4625 (kill-buffer buf))))
4626 (expect 0
4627 (let (anything-candidate-buffer-alist
4628 (anything-source-name "FOO") buf)
4629 (with-current-buffer (anything-candidate-buffer 'global)
4630 (insert "1"))
4631 (setq buf (anything-candidate-buffer 'global))
4632 (prog1 (buffer-size buf)
4633 (kill-buffer buf))))
4634 (desc "anything-candidate-buffer get-buffer")
4635 (expect " *anything candidates:FOO*"
4636 (let* (anything-candidate-buffer-alist
4637 (anything-source-name "FOO")
4638 (buf (anything-candidate-buffer 'global)))
4639 (prog1 (buffer-name (anything-candidate-buffer))
4640 (kill-buffer buf))))
4641 (expect " *anything candidates:FOO*aTestBuffer"
4642 (let* (anything-candidate-buffer-alist
4643 (anything-source-name "FOO")
4644 (anything-current-buffer (get-buffer-create "aTestBuffer"))
4645 (buf (anything-candidate-buffer 'local)))
4646 (prog1 (buffer-name (anything-candidate-buffer))
4647 (kill-buffer anything-current-buffer)
4648 (kill-buffer buf))))
4649 (expect " *anything candidates:FOO*"
4650 (let* (anything-candidate-buffer-alist
4651 (anything-source-name "FOO")
4652 (buf-local (anything-candidate-buffer 'local))
4653 (buf-global (anything-candidate-buffer 'global)))
4654 (prog1 (buffer-name (anything-candidate-buffer))
4655 (kill-buffer buf-local)
4656 (kill-buffer buf-global))))
4657 (expect " *anything candidates:FOO*aTestBuffer"
4658 (let* (anything-candidate-buffer-alist
4659 (anything-source-name "FOO")
4660 (anything-current-buffer (get-buffer-create "aTestBuffer"))
4661 (buf-global (anything-candidate-buffer 'global))
4662 (buf-local (anything-candidate-buffer 'local)))
4663 (prog1 (buffer-name (anything-candidate-buffer))
4664 (kill-buffer buf-local)
4665 (kill-buffer buf-global))))
4666 (expect nil
4667 (let* (anything-candidate-buffer-alist
4668 (anything-source-name "NOP__"))
4669 (anything-candidate-buffer)))
4670 (desc "anything-candidate-buffer register-buffer")
4671 (expect " *anything test candidates*"
4672 (let (anything-candidate-buffer-alist
4673 (buf (get-buffer-create " *anything test candidates*")))
4674 (with-current-buffer buf
4675 (insert "1\n2\n")
4676 (prog1 (buffer-name (anything-candidate-buffer buf))
4677 (kill-buffer (current-buffer))))))
4678 (expect " *anything test candidates*"
4679 (let (anything-candidate-buffer-alist
4680 (buf (get-buffer-create " *anything test candidates*")))
4681 (with-current-buffer buf
4682 (insert "1\n2\n")
4683 (anything-candidate-buffer buf)
4684 (prog1 (buffer-name (anything-candidate-buffer))
4685 (kill-buffer (current-buffer))))))
4686 (expect "1\n2\n"
4687 (let (anything-candidate-buffer-alist
4688 (buf (get-buffer-create " *anything test candidates*")))
4689 (with-current-buffer buf
4690 (insert "1\n2\n")
4691 (anything-candidate-buffer buf)
4692 (prog1 (buffer-string)
4693 (kill-buffer (current-buffer))))))
4694 (expect "buf1"
4695 (let (anything-candidate-buffer-alist
4696 (anything-source-name "foo")
4697 (buf1 (get-buffer-create "buf1"))
4698 (buf2 (get-buffer-create "buf2")))
4699 (anything-candidate-buffer buf1)
4700 (anything-candidate-buffer buf2)
4701 (prog1 (buffer-name (anything-candidate-buffer buf1))
4702 (kill-buffer buf1)
4703 (kill-buffer buf2))))
4704 (desc "action attribute")
4705 (expect "foo"
4706 (anything-test-candidates
4707 '(((name . "TEST")
4708 (candidates "foo")
4709 (action ("identity" . identity)))))
4710 (anything-execute-selection-action))
4711 (expect "foo"
4712 (anything-test-candidates
4713 '(((name . "TEST")
4714 (candidates "foo")
4715 (action ("identity" . (lambda (c) (identity c)))))))
4716 (anything-execute-selection-action))
4717 (desc "anything-execute-selection-action")
4718 (expect "FOO"
4719 (anything-execute-selection-action
4720 "foo" '(("upcase" . upcase)) nil))
4721 (expect "FOO"
4722 (anything-execute-selection-action
4723 "foo" '(("upcase" . (lambda (c) (upcase c)))) nil))
4724 (desc "display-to-real attribute")
4725 (expect "FOO"
4726 (anything-test-candidates
4727 '(((name . "TEST")
4728 (candidates "foo")
4729 (display-to-real . upcase)
4730 (action ("identity" . identity)))))
4731 (anything-execute-selection-action))
4732 (desc "cleanup test")
4733 (expect 'cleaned
4734 (let (v)
4735 (anything-test-candidates
4736 '(((name . "TEST")
4737 (cleanup . (lambda () (setq v 'cleaned))))))
4739 (desc "anything-get-current-source")
4740 ;; in init/candidates/action/candidate-transformer/filtered-candidate-transformer
4741 ;; display-to-real/cleanup function
4742 (expect "FOO"
4743 (assoc-default
4744 'name
4745 (anything-funcall-with-source '((name . "FOO")) 'anything-get-current-source)))
4746 ;; init
4747 (expect "FOO"
4748 (let (v)
4749 (anything-test-candidates
4750 '(((name . "FOO")
4751 (init . (lambda () (setq v (anything-get-current-source)))))))
4752 (assoc-default 'name v)))
4753 ;; candidates
4754 (expect "FOO"
4755 (let (v)
4756 (anything-test-candidates
4757 '(((name . "FOO")
4758 (candidates . (lambda () (setq v (anything-get-current-source)) '("a"))))))
4759 (assoc-default 'name v)))
4760 ;; action
4761 (expect "FOO"
4762 (let (v)
4763 (anything-test-candidates
4764 '(((name . "FOO")
4765 (candidates "a")
4766 (action
4767 . (lambda (c) (setq v (anything-get-current-source)) c)))))
4768 (anything-execute-selection-action)
4769 (assoc-default 'name v)))
4770 ;; candidate-transformer
4771 (expect "FOO"
4772 (let (v)
4773 (anything-test-candidates
4774 '(((name . "FOO")
4775 (candidates "a")
4776 (candidate-transformer
4777 . (lambda (c) (setq v (anything-get-current-source)) c)))))
4778 (assoc-default 'name v)))
4779 ;; filtered-candidate-transformer
4780 (expect "FOO"
4781 (let (v)
4782 (anything-test-candidates
4783 '(((name . "FOO")
4784 (candidates "a")
4785 (filtered-candidate-transformer
4786 . (lambda (c s) (setq v (anything-get-current-source)) c)))))
4787 (assoc-default 'name v)))
4788 ;; action-transformer
4789 (expect "FOO"
4790 (let (v)
4791 (anything-test-candidates
4792 '(((name . "FOO")
4793 (candidates "a")
4794 (action-transformer
4795 . (lambda (a c) (setq v (anything-get-current-source)) a))
4796 (action . identity))))
4797 (anything-execute-selection-action)
4798 (assoc-default 'name v)))
4799 ;; display-to-real
4800 (expect "FOO"
4801 (let (v)
4802 (anything-test-candidates
4803 '(((name . "FOO")
4804 (init . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4805 (insert "a\n"))))
4806 (candidates-in-buffer)
4807 (display-to-real
4808 . (lambda (c) (setq v (anything-get-current-source)) c))
4809 (action . identity))))
4810 (anything-execute-selection-action)
4811 (assoc-default 'name v)))
4812 ;; cleanup
4813 (expect "FOO"
4814 (let (v)
4815 (anything-test-candidates
4816 '(((name . "FOO")
4817 (candidates "a")
4818 (cleanup
4819 . (lambda () (setq v (anything-get-current-source)))))))
4820 (assoc-default 'name v)))
4821 ;; candidates are displayed
4822 (expect "TEST"
4823 (anything-test-candidates
4824 '(((name . "TEST")
4825 (candidates "foo")
4826 (action ("identity" . identity)))))
4827 (assoc-default 'name (anything-get-current-source)))
4828 (desc "anything-attr")
4829 (expect "FOO"
4830 (anything-funcall-with-source
4831 '((name . "FOO"))
4832 (lambda ()
4833 (anything-attr 'name))))
4834 (expect 'fuga
4835 (let (v)
4836 (anything-test-candidates
4837 '(((name . "FOO")
4838 (hoge . fuga)
4839 (init . (lambda () (setq v (anything-attr 'hoge))))
4840 (candidates "a"))))
4842 (expect nil
4843 (let (v)
4844 (anything-test-candidates
4845 '(((name . "FOO")
4846 (init . (lambda () (setq v (anything-attr 'hoge))))
4847 (candidates "a"))))
4849 (expect nil
4850 (let (v)
4851 (anything-test-candidates
4852 '(((name . "FOO")
4853 (hoge) ;INCOMPATIBLE!
4854 (init . (lambda () (setq v (anything-attr 'hoge))))
4855 (candidates "a"))))
4857 (desc "anything-attr*")
4858 (expect "generic"
4859 (let (v (value1 "generic"))
4860 (anything-test-candidates
4861 '(((name . "FOO")
4862 (hoge . value1)
4863 (init . (lambda () (setq v (anything-attr* 'hoge)))))))
4865 (desc "anything-attr-defined")
4866 (expect (non-nil)
4867 (let (v)
4868 (anything-test-candidates
4869 '(((name . "FOO")
4870 (hoge)
4871 (init . (lambda () (setq v (anything-attr-defined 'hoge))))
4872 (candidates "a"))))
4873 v))
4874 (expect nil
4875 (let (v)
4876 (anything-test-candidates
4877 '(((name . "FOO")
4878 (init . (lambda () (setq v (anything-attr-defined 'hoge))))
4879 (candidates "a"))))
4880 v))
4881 (desc "anything-attrset")
4882 (expect '((name . "FOO") (hoge . 77))
4883 (let ((src '((name . "FOO") (hoge))))
4884 (anything-attrset 'hoge 77 src)
4885 src))
4886 (expect 77
4887 (anything-attrset 'hoge 77 '((name . "FOO") (hoge))))
4889 (expect '((name . "FOO") (hoge . 77))
4890 (let ((src '((name . "FOO") (hoge . 1))))
4891 (anything-attrset 'hoge 77 src)
4892 src))
4894 (expect '((name . "FOO") (hoge . 77) (x))
4895 (let ((src '((name . "FOO") (x))))
4896 (anything-attrset 'hoge 77 src)
4897 src))
4898 (expect 77
4899 (anything-attrset 'hoge 77 '((name . "FOO"))))
4900 (desc "anything-preselect")
4901 ;; entire candidate
4902 (expect "foo"
4903 (with-current-buffer (anything-create-anything-buffer t)
4904 (let ((anything-pattern "")
4905 (anything-test-mode t))
4906 (anything-process-source '((name . "test")
4907 (candidates "hoge" "foo" "bar")))
4908 (anything-preselect "foo")
4909 (anything-get-selection))))
4910 ;; regexp
4911 (expect "foo"
4912 (with-current-buffer (anything-create-anything-buffer t)
4913 (let ((anything-pattern "")
4914 (anything-test-mode t))
4915 (anything-process-source '((name . "test")
4916 (candidates "hoge" "foo" "bar")))
4917 (anything-preselect "fo+")
4918 (anything-get-selection))))
4919 ;; no match -> first entry
4920 (expect "hoge"
4921 (with-current-buffer (anything-create-anything-buffer t)
4922 (let ((anything-pattern "")
4923 (anything-test-mode t))
4924 (anything-process-source '((name . "test")
4925 (candidates "hoge" "foo" "bar")))
4926 (anything-preselect "not found")
4927 (anything-get-selection))))
4928 (desc "anything-check-new-input")
4929 (expect "newpattern"
4930 (stub anything-update)
4931 (stub anything-action-window)
4932 (let ((anything-pattern "pattern"))
4933 (anything-check-new-input "newpattern")
4934 anything-pattern))
4935 ;; anything-input == nil when action window is available
4936 (expect nil
4937 (stub anything-update)
4938 (stub anything-action-window => t)
4939 (let ((anything-pattern "pattern")
4940 anything-input)
4941 (anything-check-new-input "newpattern")
4942 anything-input))
4943 ;; anything-input == anything-pattern unless action window is available
4944 (expect "newpattern"
4945 (stub anything-update)
4946 (stub anything-action-window => nil)
4947 (let ((anything-pattern "pattern")
4948 anything-input)
4949 (anything-check-new-input "newpattern")
4950 anything-input))
4951 (expect (mock (anything-update))
4952 (stub anything-action-window)
4953 (let (anything-pattern)
4954 (anything-check-new-input "foo")))
4955 (desc "anything-update")
4956 (expect (mock (anything-process-source '((name . "1"))))
4957 (anything-test-update '(((name . "1"))) ""))
4958 ;; (find-function 'anything-update)
4959 ;; TODO el-mock.el should express 2nd call of function.
4960 ;; (expect (mock (anything-process-source '((name . "2"))))
4961 ;; (stub anything-get-sources => '(((name . "1")) ((name . "2"))))
4962 ;; (stub anything-log-run-hook)
4963 ;; (stub anything-maybe-fit-frame)
4964 ;; (stub run-with-idle-timer)
4965 ;; (anything-update))
4966 (expect (mock (run-with-idle-timer * nil 'anything-process-delayed-sources
4967 '(((name . "2") (delayed)))))
4968 (stub anything-get-sources => '(((name . "1"))
4969 ((name . "2") (delayed))))
4970 (stub anything-log-run-hook)
4971 (stub anything-maybe-fit-frame)
4972 (let ((anything-pattern "") anything-test-mode)
4973 (anything-update)))
4975 (desc "requires-pattern attribute")
4976 (expect (not-called anything-process-source)
4977 (anything-test-update '(((name . "1") (requires-pattern))) ""))
4978 (expect (not-called anything-process-source)
4979 (anything-test-update '(((name . "1") (requires-pattern . 3))) "xx"))
4981 (desc "delay")
4982 (expect (mock (sit-for 0.25))
4983 (stub with-current-buffer)
4984 (let ((anything-idle-delay 1.0)
4985 (anything-input-idle-delay 0.75))
4986 (anything-process-delayed-sources t)))
4987 (expect (mock (sit-for 0.0))
4988 (stub with-current-buffer)
4989 (let ((anything-idle-delay 0.2)
4990 (anything-input-idle-delay 0.5))
4991 (anything-process-delayed-sources t)))
4992 (expect (mock (sit-for 0.5))
4993 (stub with-current-buffer)
4994 (let ((anything-idle-delay 0.5)
4995 (anything-input-idle-delay nil))
4996 (anything-process-delayed-sources t)))
4997 (desc "anything-normalize-sources")
4998 (expect '(anything-c-source-test)
4999 (anything-normalize-sources 'anything-c-source-test))
5000 (expect '(anything-c-source-test)
5001 (anything-normalize-sources '(anything-c-source-test)))
5002 (expect '(anything-c-source-test)
5003 (let ((anything-sources '(anything-c-source-test)))
5004 (anything-normalize-sources nil)))
5005 (expect '(((name . "test")))
5006 (anything-normalize-sources '((name . "test"))))
5007 (expect '(((name . "test")))
5008 (anything-normalize-sources '(((name . "test")))))
5009 (desc "anything-get-action")
5010 (expect '(("identity" . identity))
5011 (stub buffer-size => 1)
5012 (stub anything-get-current-source => '((name . "test")
5013 (action ("identity" . identity))))
5014 (anything-get-action))
5015 (expect '((("identity" . identity)) "action-transformer is called")
5016 (stub buffer-size => 1)
5017 (stub anything-get-current-source
5018 => '((name . "test")
5019 (action ("identity" . identity))
5020 (action-transformer
5021 . (lambda (actions selection)
5022 (list actions selection)))))
5023 (stub anything-get-selection => "action-transformer is called")
5024 (anything-get-action))
5025 (desc "anything-select-nth-action")
5026 (expect "selection"
5027 (stub anything-get-selection => "selection")
5028 (stub anything-exit-minibuffer)
5029 (let (anything-saved-selection)
5030 (anything-select-nth-action 1)
5031 anything-saved-selection))
5032 (expect 'cadr
5033 (stub anything-get-action => '(("0" . car) ("1" . cdr) ("2" . cadr)))
5034 (stub anything-exit-minibuffer)
5035 (stub anything-get-selection => "selection")
5036 (let (anything-saved-action)
5037 (anything-select-nth-action 2)
5038 anything-saved-action))
5039 (desc "anything-funcall-foreach")
5040 (expect (mock (upcase "foo"))
5041 (stub anything-get-sources => '(((init . (lambda () (upcase "foo"))))))
5042 (anything-funcall-foreach 'init))
5043 (expect (mock (downcase "bar"))
5044 (stub anything-get-sources => '(((init . (lambda () (upcase "foo"))))
5045 ((init . (lambda () (downcase "bar"))))))
5046 (anything-funcall-foreach 'init))
5047 (expect (not-called anything-funcall-with-source)
5048 (stub anything-get-sources => '(((init . (lambda () (upcase "foo"))))))
5049 (anything-funcall-foreach 'not-found))
5050 ;; TODO anything-select-with-digit-shortcut test
5051 (desc "anything-get-cached-candidates")
5052 (expect '("cached" "version")
5053 (let ((anything-candidate-cache '(("test" "cached" "version"))))
5054 (anything-get-cached-candidates '((name . "test")
5055 (candidates "new")))))
5056 (expect '("new")
5057 (let ((anything-candidate-cache '(("other" "cached" "version"))))
5058 (anything-get-cached-candidates '((name . "test")
5059 (candidates "new")))))
5060 (expect '(("test" "new")
5061 ("other" "cached" "version"))
5062 (let ((anything-candidate-cache '(("other" "cached" "version"))))
5063 (anything-get-cached-candidates '((name . "test")
5064 (candidates "new")))
5065 anything-candidate-cache))
5066 (expect '(("other" "cached" "version"))
5067 (let ((anything-candidate-cache '(("other" "cached" "version"))))
5068 (anything-get-cached-candidates '((name . "test")
5069 (candidates "new")
5070 (volatile)))
5071 anything-candidate-cache))
5072 ;; TODO when candidates == process
5073 ;; TODO anything-output-filter
5074 (desc "candidate-number-limit attribute")
5075 (expect '("a" "b")
5076 (let ((anything-pattern "")
5077 (anything-candidate-number-limit 20))
5078 (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c")
5079 (candidate-number-limit . 2) (volatile)))))
5080 (expect '("a" "b")
5081 (let ((anything-pattern "[abc]")
5082 (anything-candidate-number-limit 20))
5083 (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c")
5084 (candidate-number-limit . 2) (volatile)))))
5085 (expect '("a" "b" "c" "d")
5086 (let ((anything-pattern "[abcd]")
5087 (anything-candidate-number-limit 2))
5088 (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c" "d")
5089 (candidate-number-limit) (volatile)))))
5090 (expect '(("TEST" ("a" "b" "c")))
5091 (let ((anything-candidate-number-limit 2))
5092 (anything-test-candidates
5093 '(((name . "TEST")
5094 (init
5095 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5096 (insert "a\nb\nc\nd\n"))))
5097 (candidates . anything-candidates-in-buffer)
5098 (match identity)
5099 (candidate-number-limit . 3)
5100 (volatile))))))
5101 (expect '(("TEST" ("a" "b" "c")))
5102 (let ((anything-candidate-number-limit 2))
5103 (anything-test-candidates
5104 '(((name . "TEST")
5105 (init
5106 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5107 (insert "a\nb\nc\nd\n"))))
5108 (candidates . anything-candidates-in-buffer)
5109 (match identity)
5110 (candidate-number-limit . 3)
5111 (volatile)))
5112 ".")))
5113 (desc "multiple init")
5114 (expect '(1 . 2)
5115 (let (a b)
5116 (anything-test-candidates
5117 '(((name . "test")
5118 (init (lambda () (setq a 1))
5119 (lambda () (setq b 2))))))
5120 (cons a b)))
5121 (expect 1
5122 (let (a)
5123 (anything-test-candidates
5124 '(((name . "test")
5125 (init (lambda () (setq a 1))))))
5127 (desc "multiple cleanup")
5128 (expect '(1 . 2)
5129 (let (a b)
5130 (anything-test-candidates
5131 '(((name . "test")
5132 (cleanup (lambda () (setq a 1))
5133 (lambda () (setq b 2))))))
5134 (cons a b)))
5135 (desc "anything-mklist")
5136 (expect '(1)
5137 (anything-mklist 1))
5138 (expect '(2)
5139 (anything-mklist '(2)))
5140 (expect '((lambda ()))
5141 (anything-mklist (lambda ())))
5142 (desc "anything-before-initialize-hook")
5143 (expect 'called
5144 (let ((anything-before-initialize-hook '((lambda () (setq v 'called))))
5146 (anything-initialize)
5148 (desc "anything-after-initialize-hook")
5149 (expect '(b a)
5150 (let ((anything-before-initialize-hook
5151 '((lambda () (setq v '(a)))))
5152 (anything-after-initialize-hook
5153 '((lambda () (setq v (cons 'b v)))))
5155 (anything-initialize)
5157 (expect 0
5158 (let ((anything-after-initialize-hook
5159 '((lambda () (setq v (buffer-size (get-buffer anything-buffer))))))
5161 (anything-initialize)
5163 (desc "get-line attribute")
5164 (expect '(("TEST" ("FOO+")))
5165 (anything-test-candidates
5166 '(((name . "TEST")
5167 (init
5168 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5169 (insert "foo+\nbar+\nbaz+\n"))))
5170 (candidates-in-buffer)
5171 (get-line . (lambda (s e) (upcase (buffer-substring-no-properties s e))))))
5172 "oo\\+"))
5173 (desc "with-anything-restore-variables")
5174 (expect '(7 8)
5175 (let ((a 7) (b 8)
5176 (anything-restored-variables '(a b)))
5177 (with-anything-restore-variables
5178 (setq a 0 b 0))
5179 (list a b)))
5180 (desc "anything-cleanup-hook")
5181 (expect 'called
5182 (let ((anything-cleanup-hook
5183 '((lambda () (setq v 'called))))
5185 (anything-cleanup)
5187 (desc "with-anything-display-same-window")
5188 (expect (non-nil)
5189 (save-window-excursion
5190 (delete-other-windows)
5191 (split-window)
5193 (let ((buf (get-buffer-create " tmp"))
5194 (win (selected-window)))
5195 (with-anything-display-same-window
5196 (display-buffer buf)
5197 (eq win (get-buffer-window buf))))))
5198 (expect (non-nil)
5199 (save-window-excursion
5200 (delete-other-windows)
5201 (split-window)
5203 (let ((buf (get-buffer-create " tmp"))
5204 (win (selected-window)))
5205 (with-anything-display-same-window
5206 (pop-to-buffer buf)
5207 (eq win (get-buffer-window buf))))))
5208 (expect (non-nil)
5209 (save-window-excursion
5210 (delete-other-windows)
5211 (split-window)
5213 (let ((buf (get-buffer-create " tmp"))
5214 (win (selected-window)))
5215 (with-anything-display-same-window
5216 (switch-to-buffer buf)
5217 (eq win (get-buffer-window buf))))))
5218 (expect (non-nil)
5219 (save-window-excursion
5220 (delete-other-windows)
5221 (let ((buf (get-buffer-create " tmp"))
5222 (win (selected-window)))
5223 (with-anything-display-same-window
5224 (display-buffer buf)
5225 (eq win (get-buffer-window buf))))))
5226 (expect (non-nil)
5227 (save-window-excursion
5228 (delete-other-windows)
5229 (let ((buf (get-buffer-create " tmp"))
5230 (win (selected-window)))
5231 (with-anything-display-same-window
5232 (pop-to-buffer buf)
5233 (eq win (get-buffer-window buf))))))
5234 (desc "search-from-end attribute")
5235 (expect '(("TEST" ("baz+" "bar+" "foo+")))
5236 (anything-test-candidates
5237 '(((name . "TEST")
5238 (init
5239 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5240 (insert "foo+\nbar+\nbaz+\n"))))
5241 (candidates-in-buffer)
5242 (search-from-end)))))
5243 (expect '(("TEST" ("baz+" "bar+" "foo+")))
5244 (anything-test-candidates
5245 '(((name . "TEST")
5246 (init
5247 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5248 (insert "foo+\nbar+\nbaz+\n"))))
5249 (candidates-in-buffer)
5250 (search-from-end)))
5251 "\\+"))
5252 (expect '(("TEST" ("baz+" "bar+")))
5253 (anything-test-candidates
5254 '(((name . "TEST")
5255 (init
5256 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5257 (insert "foo+\nbar+\nbaz+\n"))))
5258 (candidates-in-buffer)
5259 (search-from-end)
5260 (candidate-number-limit . 2)))))
5261 (expect '(("TEST" ("baz+" "bar+")))
5262 (anything-test-candidates
5263 '(((name . "TEST")
5264 (init
5265 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5266 (insert "foo+\nbar+\nbaz+\n"))))
5267 (candidates-in-buffer)
5268 (search-from-end)
5269 (candidate-number-limit . 2)))
5270 "\\+"))
5272 (desc "header-name attribute")
5273 (expect "original is transformed"
5274 (anything-test-update '(((name . "original")
5275 (candidates "1")
5276 (header-name
5277 . (lambda (name)
5278 (format "%s is transformed" name)))))
5280 (with-current-buffer (anything-buffer-get)
5281 (buffer-string)
5282 (overlay-get (car (overlays-at (1+(point-min)))) 'display)))
5283 (desc "volatile and match attribute")
5284 ;; candidates function is called once per `anything-process-delayed-sources'
5285 (expect 1
5286 (let ((v 0))
5287 (anything-test-candidates '(((name . "test")
5288 (candidates . (lambda () (incf v) '("ok")))
5289 (volatile)
5290 (match identity identity identity)))
5291 "o")
5293 (desc "accept-empty attribute")
5294 (expect nil
5295 (anything-test-candidates
5296 '(((name . "test") (candidates "") (action . identity))))
5297 (anything-execute-selection-action))
5298 (expect ""
5299 (anything-test-candidates
5300 '(((name . "test") (candidates "") (action . identity) (accept-empty))))
5301 (anything-execute-selection-action))
5302 (desc "anything-tick-hash")
5303 (expect nil
5304 (with-current-buffer (get-buffer-create " *00create+*")
5305 (puthash " *00create+*/xxx" 1 anything-tick-hash)
5306 (kill-buffer (current-buffer)))
5307 (gethash " *00create+*/xxx" anything-tick-hash))
5308 (desc "anything-execute-action-at-once-if-once")
5309 (expect "HOGE"
5310 (let ((anything-execute-action-at-once-if-one t))
5311 (anything '(((name . "one test1")
5312 (candidates "hoge")
5313 (action . upcase))))))
5314 (expect "ANY"
5315 (let ((anything-execute-action-at-once-if-one t))
5316 (anything '(((name . "one test2")
5317 (candidates "hoge" "any")
5318 (action . upcase)))
5319 "an")))
5320 ;; candidates > 1
5321 (expect (mock (read-string "word: " nil))
5322 (let ((anything-execute-action-at-once-if-one t))
5323 (anything '(((name . "one test3")
5324 (candidates "hoge" "foo" "bar")
5325 (action . identity)))
5326 nil "word: ")))
5327 (desc "anything-quit-if-no-candidate")
5328 (expect nil
5329 (let ((anything-quit-if-no-candidate t))
5330 (anything '(((name . "zero test1") (candidates) (action . upcase))))))
5331 (expect 'called
5332 (let (v (anything-quit-if-no-candidate (lambda () (setq v 'called))))
5333 (anything '(((name . "zero test2") (candidates) (action . upcase))))
5335 (desc "real-to-display attribute")
5336 (expect '(("test" (("DDD" . "ddd"))))
5337 (anything-test-candidates '(((name . "test")
5338 (candidates "ddd")
5339 (real-to-display . upcase)
5340 (action . identity)))))
5341 (expect '(("test" (("DDD" . "ddd"))))
5342 (anything-test-candidates '(((name . "test")
5343 (candidates ("ignored" . "ddd"))
5344 (real-to-display . upcase)
5345 (action . identity)))))
5346 (expect '(("Commands" (("xxxhoge" . "hoge") ("xxxboke" . "boke"))))
5347 (anything-test-candidates '(((name . "Commands")
5348 (candidates
5349 "hoge" "boke")
5350 (real-to-display . (lambda (x) (concat "xxx" x)))
5351 (action . identity)))
5352 "xxx"))
5353 (expect "test\nDDD\n"
5354 (anything-test-update '(((name . "test")
5355 (candidates "ddd")
5356 (real-to-display . upcase)
5357 (action . identity)))
5359 (with-current-buffer (anything-buffer-get) (buffer-string)))
5360 (desc "real-to-display and candidate-transformer attribute")
5361 (expect '(("test" (("DDD" . "ddd"))))
5362 (anything-test-candidates
5363 '(((name . "test")
5364 (candidates "ddd")
5365 (candidate-transformer (lambda (cands) (mapcar (lambda (c) (cons "X" c)) cands)))
5366 (real-to-display . upcase)
5367 (action . identity)))))
5368 (expect "test\nDDD\n"
5369 (anything-test-update
5370 '(((name . "test")
5371 (candidates "ddd")
5372 (candidate-transformer (lambda (cands) (mapcar (lambda (c) (cons "X" c)) cands)))
5373 (real-to-display . upcase)
5374 (action . identity)))
5376 (with-current-buffer (anything-buffer-get) (buffer-string)))
5377 (desc "real-to-display and candidates-in-buffer")
5378 (expect '(("test" (("A" . "a") ("B" . "b"))))
5379 (anything-test-candidates
5380 '(((name . "test")
5381 (init
5382 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5383 (erase-buffer)
5384 (insert "a\nb\n"))))
5385 (candidates-in-buffer)
5386 (real-to-display . upcase)
5387 (action . identity)))))
5388 (expect "test\nA\nB\n"
5389 (stub read-string)
5390 (anything
5391 '(((name . "test")
5392 (init
5393 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5394 (erase-buffer)
5395 (insert "a\nb\n"))))
5396 (candidates-in-buffer)
5397 (real-to-display . upcase)
5398 (action . identity))))
5399 (with-current-buffer (anything-buffer-get) (buffer-string)))
5400 (desc "Symbols are acceptable as candidate.")
5401 (expect '(("test" (sym "str")))
5402 (anything-test-candidates
5403 '(((name . "test")
5404 (candidates sym "str")))))
5405 (expect '(("test" ((sym . realsym) ("str" . "realstr"))))
5406 (anything-test-candidates
5407 '(((name . "test")
5408 (candidates (sym . realsym) ("str" . "realstr"))))))
5409 (expect '(("test" (sym)))
5410 (anything-test-candidates
5411 '(((name . "test")
5412 (candidates sym "str")))
5413 "sym"))
5414 (expect '(("test" ("str")))
5415 (anything-test-candidates
5416 '(((name . "test")
5417 (candidates sym "str")))
5418 "str"))
5419 (expect '(("test" ((sym . realsym))))
5420 (anything-test-candidates
5421 '(((name . "test")
5422 (candidates (sym . realsym) ("str" . "realstr"))))
5423 "sym"))
5424 (expect '(("test" (("str" . "realstr"))))
5425 (anything-test-candidates
5426 '(((name . "test")
5427 (candidates (sym . realsym) ("str" . "realstr"))))
5428 "str"))
5429 (desc "multiple transformers")
5430 (expect '(("test" ("<FOO>")))
5431 (anything-test-candidates
5432 '(((name . "test")
5433 (candidates "foo")
5434 (candidate-transformer
5435 . (lambda (cands)
5436 (anything-compose (list cands)
5437 (list (lambda (c) (mapcar 'upcase c))
5438 (lambda (c) (list (concat "<" (car c) ">")))))))))))
5439 (expect '("<FOO>")
5440 (anything-composed-funcall-with-source
5441 '((name . "test"))
5442 (list (lambda (c) (mapcar 'upcase c))
5443 (lambda (c) (list (concat "<" (car c) ">"))))
5444 '("foo"))
5446 (expect '(("test" ("<FOO>")))
5447 (anything-test-candidates
5448 '(((name . "test")
5449 (candidates "foo")
5450 (candidate-transformer
5451 (lambda (c) (mapcar 'upcase c))
5452 (lambda (c) (list (concat "<" (car c) ">"))))))))
5453 (expect '(("test" ("<BAR>")))
5454 (anything-test-candidates
5455 '(((name . "test")
5456 (candidates "bar")
5457 (filtered-candidate-transformer
5458 (lambda (c s) (mapcar 'upcase c))
5459 (lambda (c s) (list (concat "<" (car c) ">"))))))))
5460 (expect '(("find-file" . find-file)
5461 ("view-file" . view-file))
5462 (stub zerop => nil)
5463 (stub anything-get-current-source
5464 => '((name . "test")
5465 (action)
5466 (action-transformer
5467 . (lambda (a s)
5468 (anything-compose
5469 (list a s)
5470 (list (lambda (a s) (push '("view-file" . view-file) a))
5471 (lambda (a s) (push '("find-file" . find-file) a))))))))
5472 (anything-get-action))
5473 (expect '(("find-file" . find-file)
5474 ("view-file" . view-file))
5475 (stub zerop => nil)
5476 (stub anything-get-current-source
5477 => '((name . "test")
5478 (action)
5479 (action-transformer
5480 (lambda (a s) (push '("view-file" . view-file) a))
5481 (lambda (a s) (push '("find-file" . find-file) a)))))
5482 (anything-get-action))
5483 (desc "define-anything-type-attribute")
5484 (expect '((file (action . find-file)))
5485 (let (anything-type-attributes)
5486 (define-anything-type-attribute 'file '((action . find-file)))
5487 anything-type-attributes))
5488 (expect '((file (action . find-file)))
5489 (let ((anything-type-attributes '((file (action . view-file)))))
5490 (define-anything-type-attribute 'file '((action . find-file)))
5491 anything-type-attributes))
5492 (expect '((file (action . find-file))
5493 (buffer (action . switch-to-buffer)))
5494 (let (anything-type-attributes)
5495 (define-anything-type-attribute 'buffer '((action . switch-to-buffer)))
5496 (define-anything-type-attribute 'file '((action . find-file)))
5497 anything-type-attributes))
5498 (desc "anything-approximate-candidate-number")
5499 (expect 0
5500 (with-temp-buffer
5501 (let ((anything-buffer (current-buffer)))
5502 (anything-approximate-candidate-number))))
5503 (expect 1
5504 (with-temp-buffer
5505 (let ((anything-buffer (current-buffer)))
5506 (insert "Title\n"
5507 "candiate1\n")
5508 (anything-approximate-candidate-number))))
5509 (expect t
5510 (with-temp-buffer
5511 (let ((anything-buffer (current-buffer)))
5512 (insert "Title\n"
5513 "candiate1\n"
5514 "candiate2\n")
5515 (<= 2 (anything-approximate-candidate-number)))))
5516 (expect 1
5517 (with-temp-buffer
5518 (let ((anything-buffer (current-buffer)))
5519 (insert "Title\n"
5520 (propertize "multi\nline\n" 'anything-multiline t))
5521 (anything-approximate-candidate-number))))
5522 (expect t
5523 (with-temp-buffer
5524 (let ((anything-buffer (current-buffer))
5525 (anything-candidate-separator "-----"))
5526 (insert "Title\n"
5527 (propertize "multi\nline1\n" 'anything-multiline t)
5528 "-----\n"
5529 (propertize "multi\nline2\n" 'anything-multiline t))
5530 (<= 2 (anything-approximate-candidate-number)))))
5531 (desc "delayed-init attribute")
5532 (expect 0
5533 (let ((value 0))
5534 (anything-test-candidates '(((name . "test")
5535 (delayed-init . (lambda () (incf value)))
5536 (candiates "abc")
5537 (requires-pattern . 2)))
5539 value))
5540 (expect 1
5541 (let ((value 0))
5542 (anything-test-candidates '(((name . "test")
5543 (delayed-init . (lambda () (incf value)))
5544 (candiates "abc")
5545 (requires-pattern . 2)))
5546 "abc")
5547 value))
5548 (expect 2
5549 (let ((value 0))
5550 (anything-test-candidates '(((name . "test")
5551 (delayed-init (lambda () (incf value))
5552 (lambda () (incf value)))
5553 (candiates "abc")
5554 (requires-pattern . 2)))
5555 "abc")
5556 value))
5557 (expect t
5558 (let (value)
5559 (with-temp-buffer
5560 (anything-test-candidates '(((name . "test")
5561 (delayed-init
5562 . (lambda () (setq value
5563 (eq anything-current-buffer (current-buffer)))))
5564 (candiates "abc")
5565 (requires-pattern . 2)))
5566 "abc")
5567 value)))
5568 (desc "pattern-transformer attribute")
5569 (expect '(("test2" ("foo")) ("test3" ("bar")))
5570 (anything-test-candidates '(((name . "test1")
5571 (candidates "foo" "bar"))
5572 ((name . "test2")
5573 (pattern-transformer . (lambda (pat) (substring pat 1)))
5574 (candidates "foo" "bar"))
5575 ((name . "test3")
5576 (pattern-transformer . (lambda (pat) "bar"))
5577 (candidates "foo" "bar")))
5578 "xfoo"))
5579 (expect '(("test2" ("foo")) ("test3" ("bar")))
5580 (anything-test-candidates '(((name . "test1")
5581 (candidates "foo" "bar"))
5582 ((name . "test2")
5583 (pattern-transformer (lambda (pat) (substring pat 1)))
5584 (candidates "foo" "bar"))
5585 ((name . "test3")
5586 (pattern-transformer (lambda (pat) "bar"))
5587 (candidates "foo" "bar")))
5588 "xfoo"))
5589 (expect '(("test2" ("foo")) ("test3" ("bar")))
5590 (anything-test-candidates '(((name . "test1")
5591 (init
5592 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5593 (insert "foo\nbar\n"))))
5594 (candidates-in-buffer))
5595 ((name . "test2")
5596 (pattern-transformer . (lambda (pat) (substring pat 1)))
5597 (init
5598 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5599 (insert "foo\nbar\n"))))
5600 (candidates-in-buffer))
5601 ((name . "test3")
5602 (pattern-transformer . (lambda (pat) "bar"))
5603 (init
5604 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5605 (insert "foo\nbar\n"))))
5606 (candidates-in-buffer)))
5607 "xfoo"))
5608 (desc "anything-recent-push")
5609 (expect '("foo" "bar" "baz")
5610 (let ((lst '("bar" "baz")))
5611 (anything-recent-push "foo" 'lst)))
5612 (expect '("foo" "bar" "baz")
5613 (let ((lst '("foo" "bar" "baz")))
5614 (anything-recent-push "foo" 'lst)))
5615 (expect '("foo" "bar" "baz")
5616 (let ((lst '("bar" "foo" "baz")))
5617 (anything-recent-push "foo" 'lst)))
5618 (desc "anything-require-at-least-version")
5619 (expect nil
5620 (anything-require-at-least-version "1.1"))
5621 (expect nil
5622 (anything-require-at-least-version "1.200"))
5623 (expect nil
5624 (anything-require-at-least-version
5625 (and (string-match "1\.\\([0-9]+\\)" anything-version)
5626 (match-string 0 anything-version))))
5627 (expect (error)
5628 (anything-require-at-least-version "1.999"))
5629 (expect (error)
5630 (anything-require-at-least-version "1.2000"))
5631 (desc "anything-once")
5632 (expect 2
5633 (let ((i 0))
5634 (anything-test-candidates
5635 '(((name . "1")
5636 (init . (lambda () (incf i))))
5637 ((name . "2")
5638 (init . (lambda () (incf i))))))
5640 (expect 1
5641 (let ((i 0))
5642 (anything-test-candidates
5643 '(((name . "1")
5644 (init . (lambda () (anything-once (lambda () (incf i))))))
5645 ((name . "2")
5646 (init . (lambda () (anything-once (lambda () (incf i))))))))
5648 (expect 1
5649 (let ((i 0))
5650 (flet ((init1 () (anything-once (lambda () (incf i)))))
5651 (anything-test-candidates
5652 '(((name . "1")
5653 (init . init1))
5654 ((name . "2")
5655 (init . init1)))))
5657 (desc "anything-marked-candidates")
5658 (expect '("mark3" "mark1")
5659 (let* ((source '((name . "mark test")))
5660 (anything-marked-candidates
5661 `((,source . "mark1")
5662 (((name . "other")) . "mark2")
5663 (,source . "mark3"))))
5664 (stub anything-buffer-get => (current-buffer))
5665 (stub anything-get-current-source => source)
5666 (anything-marked-candidates)))
5667 (expect '("current")
5668 (let* ((source '((name . "mark test")))
5669 (anything-marked-candidates nil))
5670 (stub anything-get-current-source => source)
5671 (stub anything-get-selection => "current")
5672 (anything-marked-candidates)))
5673 (desc "anything-let")
5674 (expect '(1 10000 nil)
5675 (let ((a 9999)
5676 (b 8)
5678 (anything-buffer (exps-tmpbuf)))
5679 (anything-let ((a 1)
5680 (b (1+ a))
5682 (anything-create-anything-buffer))
5683 (with-current-buffer anything-buffer
5684 (list a b c))))
5685 (expect (non-nil)
5686 (let ((a 9999)
5687 (b 8)
5689 (anything-buffer (exps-tmpbuf)))
5690 (anything-let ((a 1)
5691 (b (1+ a))
5693 (anything-create-anything-buffer))
5694 (with-current-buffer anything-buffer
5695 (and (assq 'a (buffer-local-variables))
5696 (assq 'b (buffer-local-variables))
5697 (assq 'c (buffer-local-variables))))))
5698 (expect 'retval
5699 (let ((a 9999)
5700 (b 8)
5702 (anything-buffer (exps-tmpbuf)))
5703 (anything-let ((a 1)
5704 (b (1+ a))
5706 'retval)))
5707 (desc "anything-let*")
5708 (expect '(1 2 nil)
5709 (let ((a 9999)
5710 (b 8)
5712 (anything-buffer (exps-tmpbuf)))
5713 (anything-let* ((a 1)
5714 (b (1+ a))
5716 (anything-create-anything-buffer))
5717 (with-current-buffer anything-buffer
5718 (list a b c))))
5719 (expect (non-nil)
5720 (let ((a 9999)
5721 (b 8)
5723 (anything-buffer (exps-tmpbuf)))
5724 (anything-let* ((a 1)
5725 (b (1+ a))
5727 (anything-create-anything-buffer))
5728 (with-current-buffer anything-buffer
5729 (and (assq 'a (buffer-local-variables))
5730 (assq 'b (buffer-local-variables))
5731 (assq 'c (buffer-local-variables))))))
5732 (expect 'retval*
5733 (let ((a 9999)
5734 (b 8)
5736 (anything-buffer (exps-tmpbuf)))
5737 (anything-let* ((a 1)
5738 (b (1+ a))
5740 'retval*)))
5741 (desc "anything with keyword")
5742 (expect (mock (anything-internal 'test-source "input" "prompt: " nil "preselect" "*test*" nil))
5743 (anything :sources 'test-source
5744 :input "input"
5745 :prompt "prompt: "
5746 :resume nil
5747 :preselect "preselect"
5748 :buffer "*test*"
5749 :keymap nil))
5750 (expect (mock (anything-internal 'test-source nil nil nil nil "*test*" nil))
5751 (anything :sources 'test-source
5752 :buffer "*test*"
5753 :candidate-number-limit 20))
5754 (expect (mock (anything-internal 'test-source nil nil nil nil "*test*" nil))
5755 (anything 'test-source nil nil nil nil "*test*" nil))
5756 (desc "anything-log-eval-internal")
5757 (expect (mock (anything-log "%S = %S" '(+ 1 2) 3))
5758 (anything-log-eval-internal '((+ 1 2))))
5759 (expect (mock (anything-log "%S = ERROR!" 'unDeFined))
5760 (anything-log-eval-internal '(unDeFined)))
5765 (provide 'anything)
5766 ;; How to save (DO NOT REMOVE!!)
5767 ;; (progn (magit-push) (emacswiki-post "anything.el"))
5768 ;;; anything.el ends here