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