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