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