anything.el (anything-search-from-candidate-buffer): refactor: simplify / reindent
[anything-config.git] / anything.el
blobaac3cfc85758d5c08d1d8005544652a14cfa3500
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
2884 limit search-from-end
2885 start-point endp)
2886 (let (buffer-read-only
2887 matches exit newmatches)
2888 (anything-search-from-candidate-buffer-internal
2889 (lambda ()
2890 (clrhash anything-cib-hash)
2891 (dolist (searcher search-fns)
2892 (goto-char start-point)
2893 (setq newmatches nil)
2894 (loop with item-count = 0
2895 while (funcall searcher pattern nil t)
2896 when (eq (point-at-bol) (point-max)) do (return nil)
2897 for cand = (funcall get-line-fn (point-at-bol) (point-at-eol))
2899 (anything-accumulate-candidates-internal
2900 cand newmatches anything-cib-hash item-count limit)
2901 (if search-from-end
2902 (goto-char (max (point-at-bol) 1))
2903 (forward-line 1)))
2904 (setq matches (append matches (nreverse newmatches)))
2905 (if exit (return)))
2906 (delq nil matches)))))
2908 (defun anything-initial-candidates-from-candidate-buffer (endp get-line-fn limit search-from-end)
2909 (delq nil (loop with next-line-fn =
2910 (if search-from-end
2911 (lambda (x) (goto-char (max (1- (point-at-bol)) 1)))
2912 #'forward-line)
2913 until (funcall endp)
2914 for i from 1 to limit
2915 collect (funcall get-line-fn (point-at-bol) (point-at-eol))
2916 do (funcall next-line-fn 1))))
2918 (defun anything-search-from-candidate-buffer-internal (search-fn)
2919 (goto-char (point-min))
2920 (insert "\n")
2921 (goto-char (point-max))
2922 (insert "\n")
2923 (unwind-protect
2924 (funcall search-fn)
2925 (goto-char (point-min))
2926 (delete-char 1)
2927 (goto-char (1- (point-max)))
2928 (delete-char 1)
2930 (set-buffer-modified-p nil)))
2932 (defun anything-candidate-buffer (&optional create-or-buffer)
2933 "Register and return a buffer containing candidates of current source.
2934 `anything-candidate-buffer' searches buffer-local candidates buffer first,
2935 then global candidates buffer.
2937 Acceptable values of CREATE-OR-BUFFER:
2939 - nil (omit)
2940 Only return the candidates buffer.
2941 - a buffer
2942 Register a buffer as a candidates buffer.
2943 - 'global
2944 Create a new global candidates buffer,
2945 named \" *anything candidates:SOURCE*\".
2946 - other non-nil value
2947 Create a new local candidates buffer,
2948 named \" *anything candidates:SOURCE*ANYTHING-CURRENT-BUFFER\".
2950 (let* ((global-bname (format " *anything candidates:%s*" anything-source-name))
2951 (local-bname (format " *anything candidates:%s*%s"
2952 anything-source-name
2953 (buffer-name anything-current-buffer)))
2954 (register-func
2955 (lambda ()
2956 (setq anything-candidate-buffer-alist
2957 (cons (cons anything-source-name create-or-buffer)
2958 (delete (assoc anything-source-name
2959 anything-candidate-buffer-alist)
2960 anything-candidate-buffer-alist)))))
2961 (kill-buffers-func
2962 (lambda ()
2963 (loop for b in (buffer-list)
2964 if (string-match (format "^%s" (regexp-quote global-bname))
2965 (buffer-name b))
2966 do (kill-buffer b))))
2967 (create-func
2968 (lambda ()
2969 (with-current-buffer
2970 (get-buffer-create (if (eq create-or-buffer 'global)
2971 global-bname
2972 local-bname))
2973 (buffer-disable-undo)
2974 (erase-buffer)
2975 (font-lock-mode -1))))
2976 (return-func
2977 (lambda ()
2978 (or (get-buffer local-bname)
2979 (get-buffer global-bname)
2980 (anything-aif (assoc-default anything-source-name
2981 anything-candidate-buffer-alist)
2982 (and (buffer-live-p it) it))))))
2983 (when create-or-buffer
2984 (funcall register-func)
2985 (unless (bufferp create-or-buffer)
2986 (and (eq create-or-buffer 'global) (funcall kill-buffers-func))
2987 (funcall create-func)))
2988 (funcall return-func)))
2990 (defun anything-compile-source--candidates-in-buffer (source)
2991 (anything-aif (assoc 'candidates-in-buffer source)
2992 (append source
2993 `((candidates . ,(or (cdr it) 'anything-candidates-in-buffer))
2994 (volatile) (match identity)))
2995 source))
2997 ;; (@* "Utility: resplit anything window")
2998 (defun anything-toggle-resplit-window ()
2999 "Toggle resplit anything window, vertically or horizontally."
3000 (interactive)
3001 (with-anything-window
3002 (let ((before-height (window-height)))
3003 (delete-window)
3004 (set-window-buffer
3005 (select-window (if (= (window-height) before-height)
3006 (split-window-vertically)
3007 (split-window-horizontally)))
3008 anything-buffer))))
3010 ;; (@* "Utility: select another action by key")
3011 (defun anything-select-nth-action (n)
3012 "Select the nth action for the currently selected candidate."
3013 (setq anything-saved-selection (anything-get-selection))
3014 (unless anything-saved-selection
3015 (error "Nothing is selected."))
3016 (setq anything-saved-action (cdr (elt (anything-get-action) n)))
3017 (anything-exit-minibuffer))
3019 (defun anything-select-2nd-action ()
3020 "Select the 2nd action for the currently selected candidate."
3021 (interactive)
3022 (anything-select-nth-action 1))
3024 (defun anything-select-3rd-action ()
3025 "Select the 3rd action for the currently selected candidate."
3026 (interactive)
3027 (anything-select-nth-action 2))
3029 (defun anything-select-4th-action ()
3030 "Select the 4th action for the currently selected candidate."
3031 (interactive)
3032 (anything-select-nth-action 3))
3034 (defun anything-select-2nd-action-or-end-of-line ()
3035 "Select the 2nd action for the currently selected candidate if the point is at the end of minibuffer.
3036 Otherwise goto the end of minibuffer."
3037 (interactive)
3038 (if (eolp)
3039 (anything-select-nth-action 1)
3040 (end-of-line)))
3042 ;; (@* "Utility: Persistent Action")
3043 (defmacro with-anything-display-same-window (&rest body)
3044 "Make `pop-to-buffer' and `display-buffer' display in the same window."
3045 `(let ((display-buffer-function 'anything-persistent-action-display-buffer))
3046 ,@body))
3047 (put 'with-anything-display-same-window 'lisp-indent-function 0)
3049 (defun* anything-execute-persistent-action (&optional (attr 'persistent-action))
3050 "If a candidate is selected then perform the associated action without quitting anything."
3051 (interactive)
3052 (anything-log "executing persistent-action")
3053 (save-selected-window
3054 (select-window (get-buffer-window (anything-buffer-get)))
3055 (select-window (setq minibuffer-scroll-window
3056 (if (one-window-p t) (split-window)
3057 (next-window (selected-window) 1))))
3058 (anything-log-eval (current-buffer))
3059 (let ((anything-in-persistent-action t))
3060 (with-anything-display-same-window
3061 (anything-execute-selection-action
3063 (or (assoc-default attr (anything-get-current-source))
3064 (anything-get-action))
3066 (anything-log-run-hook 'anything-after-persistent-action-hook)))))
3068 (defun anything-persistent-action-display-buffer (buf &optional not-this-window)
3069 "Make `pop-to-buffer' and `display-buffer' display in the same window in persistent action.
3070 If `anything-persistent-action-use-special-display' is non-nil and
3071 BUF is to be displayed by `special-display-function', use it.
3072 Otherwise ignores `special-display-buffer-names' and `special-display-regexps'."
3073 (let* ((name (buffer-name buf))
3074 display-buffer-function pop-up-windows
3075 (same-window-regexps
3076 (unless (and anything-persistent-action-use-special-display
3077 (or (member name
3078 (mapcar (lambda (x) (or (car-safe x) x))
3079 special-display-buffer-names))
3080 (remove-if-not
3081 (lambda (x) (string-match (or (car-safe x) x) name))
3082 special-display-regexps)))
3083 '("."))))
3084 (display-buffer buf not-this-window)))
3086 ;; scroll-other-window(-down)? for persistent-action
3087 (defun anything-scroll-other-window-base (command)
3088 (save-selected-window
3089 (select-window
3090 (some-window
3091 (lambda (w) (not (string= anything-buffer (buffer-name (window-buffer w)))))
3092 'no-minibuffer 'current-frame))
3093 (funcall command anything-scroll-amount)))
3095 (defun anything-scroll-other-window ()
3096 "Scroll other window (not *Anything* window) upward."
3097 (interactive)
3098 (anything-scroll-other-window-base 'scroll-up))
3099 (defun anything-scroll-other-window-down ()
3100 "Scroll other window (not *Anything* window) downward."
3101 (interactive)
3102 (anything-scroll-other-window-base 'scroll-down))
3104 ;; (@* "Utility: Visible Mark")
3105 (defface anything-visible-mark
3106 '((((min-colors 88) (background dark))
3107 (:background "green1" :foreground "black"))
3108 (((background dark)) (:background "green" :foreground "black"))
3109 (((min-colors 88)) (:background "green1"))
3110 (t (:background "green")))
3111 "Face for visible mark."
3112 :group 'anything)
3113 (defvar anything-visible-mark-face 'anything-visible-mark)
3114 (defvar anything-visible-mark-overlays nil)
3116 (defun anything-clear-visible-mark ()
3117 (with-current-buffer (anything-buffer-get)
3118 (mapc 'delete-overlay anything-visible-mark-overlays)
3119 (set (make-local-variable 'anything-visible-mark-overlays) nil)))
3120 (add-hook 'anything-after-initialize-hook 'anything-clear-visible-mark)
3122 (defvar anything-c-marked-candidate-list nil
3123 "[OBSOLETE] DO NOT USE!!")
3124 (defvar anything-marked-candidates nil
3125 "Marked candadates. List of (source . real) pair.")
3127 (defun anything-this-visible-mark ()
3128 (loop for o in anything-visible-mark-overlays
3129 when (equal (point-at-bol) (overlay-start o))
3130 do (return o)))
3132 (defun anything-delete-visible-mark (overlay)
3133 (setq anything-c-marked-candidate-list
3134 (remove (anything-current-line-contents) anything-c-marked-candidate-list))
3135 (setq anything-marked-candidates
3136 (remove
3137 (cons (anything-get-current-source) (anything-get-selection))
3138 anything-marked-candidates))
3139 (delete-overlay overlay)
3140 (setq anything-visible-mark-overlays
3141 (delq overlay anything-visible-mark-overlays)))
3143 (defun anything-make-visible-mark ()
3144 (let ((o (make-overlay (point-at-bol) (1+ (point-at-eol)))))
3145 (overlay-put o 'face anything-visible-mark-face)
3146 (overlay-put o 'source (assoc-default 'name (anything-get-current-source)))
3147 (overlay-put o 'string (buffer-substring (overlay-start o) (overlay-end o)))
3148 (add-to-list 'anything-visible-mark-overlays o))
3149 (push (anything-current-line-contents) anything-c-marked-candidate-list)
3150 (push (cons (anything-get-current-source) (anything-get-selection))
3151 anything-marked-candidates))
3153 (defun anything-toggle-visible-mark ()
3154 "Toggle anything visible bookmark at point."
3155 (interactive)
3156 (with-anything-window
3157 (anything-aif (anything-this-visible-mark)
3158 (anything-delete-visible-mark it)
3159 (anything-make-visible-mark))
3160 (anything-next-line)))
3162 (defun anything-display-all-visible-marks ()
3163 "Show all `anything' visible marks strings."
3164 (interactive)
3165 (lexical-let ((overlays (reverse anything-visible-mark-overlays)))
3166 (anything-run-after-quit
3167 (lambda ()
3168 (with-output-to-temp-buffer "*anything visible marks*"
3169 (dolist (o overlays) (princ (overlay-get o 'string))))))))
3171 (defun anything-marked-candidates ()
3172 "Marked candidates (real value) of current source if any,
3173 otherwise 1-element list of current selection.
3175 It is analogous to `dired-get-marked-files'."
3176 (with-current-buffer (anything-buffer-get)
3177 (let ((cands
3178 (if anything-marked-candidates
3179 (loop with current-src = (anything-get-current-source)
3180 for (source . real) in (reverse anything-marked-candidates)
3181 when (equal current-src source)
3182 collect real)
3183 (list (anything-get-selection)))))
3184 (anything-log-eval cands)
3185 cands)))
3187 (defun anything-reset-marked-candidates ()
3188 (with-current-buffer (anything-buffer-get)
3189 (set (make-local-variable 'anything-c-marked-candidate-list) nil)
3190 (set (make-local-variable 'anything-marked-candidates) nil)))
3192 (add-hook 'anything-after-initialize-hook 'anything-reset-marked-candidates)
3193 ;; (add-hook 'anything-after-action-hook 'anything-reset-marked-candidates)
3195 (defun anything-current-source-name= (name)
3196 (save-excursion
3197 (goto-char (anything-get-previous-header-pos))
3198 (equal name (anything-current-line-contents))))
3200 (defun anything-revive-visible-mark ()
3201 (with-current-buffer anything-buffer
3202 (dolist (o anything-visible-mark-overlays)
3203 (goto-char (point-min))
3204 (while (and (search-forward (overlay-get o 'string) nil t)
3205 (anything-current-source-name= (overlay-get o 'source)))
3206 ;; Now the next line of visible mark
3207 (move-overlay o (point-at-bol 0) (1+ (point-at-eol 0)))))))
3208 (add-hook 'anything-update-hook 'anything-revive-visible-mark)
3210 (defun anything-next-point-in-list (curpos points &optional prev)
3211 (cond
3212 ;; rule out special cases
3213 ((null points) curpos)
3214 ((and prev (< curpos (car points))) curpos)
3215 ((< (car (last points)) curpos)
3216 (if prev (car (last points)) curpos))
3218 (nth (if prev
3219 (loop for pt in points
3220 for i from 0
3221 if (<= curpos pt)
3222 do (return (1- i)))
3223 (loop for pt in points
3224 for i from 0
3225 if (< curpos pt)
3226 do (return i)))
3227 points))))
3229 (defun anything-next-visible-mark (&optional prev)
3230 "Move next anything visible mark."
3231 (interactive)
3232 (with-anything-window
3233 (goto-char (anything-next-point-in-list
3234 (point)
3235 (sort (mapcar 'overlay-start anything-visible-mark-overlays) '<)
3236 prev))
3237 (anything-mark-current-line)))
3239 (defun anything-prev-visible-mark ()
3240 "Move previous anything visible mark."
3241 (interactive)
3242 (anything-next-visible-mark t))
3244 ;; (@* "Utility: `find-file' integration")
3245 (defun anything-quit-and-find-file ()
3246 "Drop into `find-file' from `anything' like `iswitchb-find-file'.
3247 If current selection is a buffer or a file, `find-file' from its directory."
3248 (interactive)
3249 (anything-run-after-quit
3250 (lambda (f)
3251 (if (file-exists-p f)
3252 (let ((default-directory (file-name-directory f)))
3253 (call-interactively 'find-file))
3254 (call-interactively 'find-file)))
3255 (anything-aif (get-buffer (anything-get-selection))
3256 (buffer-file-name it)
3257 (expand-file-name (anything-get-selection)))))
3259 ;; (@* "Utility: Selection Paste")
3260 (defun anything-yank-selection ()
3261 "Set minibuffer contents to current selection."
3262 (interactive)
3263 (anything-set-pattern (anything-get-selection nil t)))
3265 (defun anything-kill-selection-and-quit ()
3266 "Store current selection to kill ring.
3267 You can paste it by typing C-y."
3268 (interactive)
3269 (anything-run-after-quit
3270 (lambda (sel)
3271 (kill-new sel)
3272 (message "Killed: %s" sel))
3273 (anything-get-selection nil t)))
3276 ;; (@* "Utility: Automatical execution of persistent-action")
3277 (add-to-list 'minor-mode-alist '(anything-follow-mode " AFollow"))
3278 (defun anything-follow-mode ()
3279 "If this mode is on, persistent action is executed everytime the cursor is moved."
3280 (interactive)
3281 (with-current-buffer anything-buffer
3282 (setq anything-follow-mode (not anything-follow-mode))
3283 (message "anything-follow-mode is %s"
3284 (if anything-follow-mode "enabled" "disabled"))))
3286 (defun anything-follow-execute-persistent-action-maybe ()
3287 "Execute persistent action after `anything-input-idle-delay' secs when `anything-follow-mode' is enabled."
3288 (and (buffer-local-value 'anything-follow-mode
3289 (get-buffer-create anything-buffer))
3290 (sit-for anything-input-idle-delay)
3291 (anything-window)
3292 (anything-get-selection)
3293 (save-excursion
3294 (anything-execute-persistent-action))))
3296 ;; (@* "Utility: Migrate `anything-sources' to my-anything command")
3297 (defun anything-migrate-sources ()
3298 "Help to migrate to new `anything' way."
3299 (interactive)
3300 (with-current-buffer (get-buffer-create "*anything migrate*")
3301 (erase-buffer)
3302 (insert (format "\
3303 Setting `anything-sources' directly is not good because
3304 `anything' is not for one command. For now, interactive use of
3305 `anything' (M-x anything) is only for demonstration purpose.
3306 So you should define commands calling `anything'.
3307 I help you to migrate to the new way.
3309 The code below is automatically generated from current
3310 `anything-sources' value. You can use the `my-anything' command
3311 now!
3313 Copy and paste it to your .emacs. Then substitute `my-anything'
3314 for `anything' bindings in all `define-key', `local-set-key' and
3315 `global-set-key' calls.
3317 \(defun my-anything ()
3318 \"Anything command for you.
3320 It is automatically generated by `anything-migrate-sources'.\"
3321 (interactive)
3322 (anything-other-buffer
3324 \"*my-anything*\"))
3325 " anything-sources))
3326 (eval-last-sexp nil)
3327 (substitute-key-definition 'anything 'my-anything global-map)
3328 (pop-to-buffer (current-buffer))))
3330 ;; (@* "Utility: Incremental search within results (unmaintained)")
3332 (defvar anything-isearch-original-global-map nil
3333 "Original global map before Anything isearch is started.")
3335 (defvar anything-isearch-original-message-timeout nil
3336 "Original message timeout before Anything isearch is started.")
3338 (defvar anything-isearch-pattern nil
3339 "The current isearch pattern.")
3341 (defvar anything-isearch-message-suffix ""
3342 "Message suffix indicating the current state of the search.")
3344 (defvar anything-isearch-original-point nil
3345 "Original position of point before isearch is started.")
3347 (defvar anything-isearch-original-window nil
3348 "Original selected window before isearch is started.")
3350 (defvar anything-isearch-original-cursor-in-non-selected-windows nil
3351 "Original value of cursor-in-non-selected-windows before isearch is started.")
3353 (defvar anything-isearch-original-deferred-action-list nil
3354 "Original value of deferred-action-list before isearch is started.")
3356 (defvar anything-isearch-match-positions nil
3357 "Stack of positions of matches or non-matches.
3359 It's a list of plists with two properties: `event', the last user
3360 event, `start', the start position of the current match, and
3361 `pos', the position of point after that event.
3363 The value of `event' can be the following symbols: `char' if a
3364 character was typed, `error' if a non-matching character was
3365 typed, `search' if a forward search had to be done after a
3366 character, and `search-again' if a search was done for the next
3367 occurrence of the current pattern.")
3369 (defvar anything-isearch-match-start nil
3370 "Start position of the current match.")
3373 (defun anything-isearch ()
3374 "Start incremental search within results. (UNMAINTAINED)"
3375 (interactive)
3376 (if (anything-empty-buffer-p (anything-buffer-get))
3377 (message "There are no results.")
3379 (setq anything-isearch-original-message-timeout minibuffer-message-timeout)
3380 (setq minibuffer-message-timeout nil)
3382 (setq anything-isearch-original-global-map global-map)
3384 (condition-case nil
3385 (progn
3386 (setq anything-isearch-original-window (selected-window))
3387 (select-window (anything-window))
3388 (setq cursor-type t)
3390 (setq anything-isearch-original-deferred-action-list
3391 (default-value 'deferred-action-list))
3392 (setq-default deferred-action-list nil)
3393 (add-hook 'deferred-action-list 'anything-isearch-post-command)
3395 (use-global-map anything-isearch-map)
3396 (setq overriding-terminal-local-map anything-isearch-map)
3398 (setq anything-isearch-pattern "")
3400 (setq anything-isearch-original-cursor-in-non-selected-windows
3401 cursor-in-non-selected-windows)
3402 (setq cursor-in-non-selected-windows nil)
3404 (setq anything-isearch-original-point (point-marker))
3405 (goto-char (point-min))
3406 (forward-line)
3407 (anything-mark-current-line)
3409 (setq anything-isearch-match-positions nil)
3410 (setq anything-isearch-match-start (point-marker))
3412 (if anything-isearch-overlay
3413 ;; make sure the overlay belongs to the anything buffer
3414 (move-overlay anything-isearch-overlay (point-min) (point-min)
3415 (get-buffer (anything-buffer-get)))
3417 (setq anything-isearch-overlay (make-overlay (point-min) (point-min)))
3418 (overlay-put anything-isearch-overlay 'face anything-isearch-match-face))
3420 (setq anything-isearch-message-suffix
3421 (substitute-command-keys "cancel with \\[anything-isearch-cancel]")))
3423 (error (anything-isearch-cleanup)))))
3426 (defun anything-isearch-post-command ()
3427 "Print the current pattern after every command."
3428 (anything-isearch-message)
3429 (when (anything-window)
3430 (with-anything-window
3431 (move-overlay anything-isearch-overlay anything-isearch-match-start (point)
3432 (get-buffer (anything-buffer-get))))))
3435 (defun anything-isearch-printing-char ()
3436 "Add printing char to the pattern."
3437 (interactive)
3438 (let ((char (char-to-string last-command-event)))
3439 (setq anything-isearch-pattern (concat anything-isearch-pattern char))
3441 (with-anything-window
3442 (if (looking-at char)
3443 (progn
3444 (push (list 'event 'char
3445 'start anything-isearch-match-start
3446 'pos (point-marker))
3447 anything-isearch-match-positions)
3448 (forward-char))
3450 (let ((start (point)))
3451 (while (and (re-search-forward anything-isearch-pattern nil t)
3452 (anything-pos-header-line-p)))
3453 (if (or (anything-pos-header-line-p)
3454 (eq start (point)))
3455 (progn
3456 (goto-char start)
3457 (push (list 'event 'error
3458 'start anything-isearch-match-start
3459 'pos (point-marker))
3460 anything-isearch-match-positions))
3462 (push (list 'event 'search
3463 'start anything-isearch-match-start
3464 'pos (copy-marker start))
3465 anything-isearch-match-positions)
3466 (setq anything-isearch-match-start
3467 (copy-marker (match-beginning 0))))))
3469 (anything-mark-current-line))))
3472 (defun anything-isearch-again ()
3473 "Search again for the current pattern"
3474 (interactive)
3475 (if (equal anything-isearch-pattern "")
3476 (setq anything-isearch-message-suffix "no pattern yet")
3478 (with-anything-window
3479 (let ((start (point)))
3480 (while (and (re-search-forward anything-isearch-pattern nil t)
3481 (anything-pos-header-line-p)))
3482 (if (or (anything-pos-header-line-p)
3483 (eq start (point)))
3484 (progn
3485 (goto-char start)
3486 (unless (eq 'error
3487 (plist-get (car anything-isearch-match-positions)
3488 'event))
3489 (setq anything-isearch-message-suffix "no more matches")))
3491 (push (list 'event 'search-again
3492 'start anything-isearch-match-start
3493 'pos (copy-marker start))
3494 anything-isearch-match-positions)
3495 (setq anything-isearch-match-start (copy-marker (match-beginning 0)))
3497 (anything-mark-current-line))))))
3500 (defun anything-isearch-delete ()
3501 "Undo last event."
3502 (interactive)
3503 (unless (equal anything-isearch-pattern "")
3504 (let ((last (pop anything-isearch-match-positions)))
3505 (unless (eq 'search-again (plist-get last 'event))
3506 (setq anything-isearch-pattern
3507 (substring anything-isearch-pattern 0 -1)))
3509 (with-anything-window
3510 (goto-char (plist-get last 'pos))
3511 (setq anything-isearch-match-start (plist-get last 'start))
3512 (anything-mark-current-line)))))
3515 (defun anything-isearch-default-action ()
3516 "Execute the default action for the selected candidate."
3517 (interactive)
3518 (anything-isearch-cleanup)
3519 (with-current-buffer (anything-buffer-get) (anything-exit-minibuffer)))
3522 (defun anything-isearch-select-action ()
3523 "Choose an action for the selected candidate."
3524 (interactive)
3525 (anything-isearch-cleanup)
3526 (with-anything-window
3527 (anything-select-action)))
3530 (defun anything-isearch-cancel ()
3531 "Cancel Anything isearch."
3532 (interactive)
3533 (anything-isearch-cleanup)
3534 (when (anything-window)
3535 (with-anything-window
3536 (goto-char anything-isearch-original-point)
3537 (anything-mark-current-line))))
3540 (defun anything-isearch-cleanup ()
3541 "Clean up the mess."
3542 (setq minibuffer-message-timeout anything-isearch-original-message-timeout)
3543 (with-current-buffer (anything-buffer-get)
3544 (setq overriding-terminal-local-map nil)
3545 (setq cursor-type nil)
3546 (setq cursor-in-non-selected-windows
3547 anything-isearch-original-cursor-in-non-selected-windows))
3548 (when anything-isearch-original-window
3549 (select-window anything-isearch-original-window))
3551 (use-global-map anything-isearch-original-global-map)
3552 (setq-default deferred-action-list
3553 anything-isearch-original-deferred-action-list)
3554 (when (overlayp anything-isearch-overlay)
3555 (delete-overlay anything-isearch-overlay)))
3558 (defun anything-isearch-message ()
3559 "Print prompt."
3560 (if (and (equal anything-isearch-message-suffix "")
3561 (eq (plist-get (car anything-isearch-match-positions) 'event)
3562 'error))
3563 (setq anything-isearch-message-suffix "failing"))
3565 (unless (equal anything-isearch-message-suffix "")
3566 (setq anything-isearch-message-suffix
3567 (concat " [" anything-isearch-message-suffix "]")))
3569 (message (concat "Search within results: "
3570 anything-isearch-pattern
3571 anything-isearch-message-suffix))
3573 (setq anything-isearch-message-suffix ""))
3576 ;; (@* "Utility: Iswitchb integration (unmaintained)")
3578 (defvar anything-iswitchb-candidate-selected nil
3579 "Indicates whether an anything candidate is selected from iswitchb.")
3581 (defvar anything-iswitchb-frame-configuration nil
3582 "Saved frame configuration, before anything buffer was displayed.")
3584 (defvar anything-iswitchb-saved-keys nil
3585 "The original in iswitchb before binding anything keys.")
3588 (defun anything-iswitchb-setup ()
3589 "Integrate anything completion into iswitchb (UNMAINTAINED).
3591 If the user is idle for `anything-iswitchb-idle-delay' seconds
3592 after typing something into iswitchb then anything candidates are
3593 shown for the current iswitchb input.
3595 ESC cancels anything completion and returns to normal iswitchb.
3597 Some key bindings in `anything-map' are modified.
3598 See also `anything-iswitchb-setup-keys'."
3599 (interactive)
3601 (require 'iswitchb)
3603 ;; disable timid completion during iswitchb
3604 (put 'iswitchb-buffer 'timid-completion 'disabled)
3605 (add-hook 'minibuffer-setup-hook 'anything-iswitchb-minibuffer-setup)
3607 (defadvice iswitchb-visit-buffer
3608 (around anything-iswitchb-visit-buffer activate)
3609 (if anything-iswitchb-candidate-selected
3610 (anything-execute-selection-action)
3611 ad-do-it))
3613 (defadvice iswitchb-possible-new-buffer
3614 (around anything-iswitchb-possible-new-buffer activate)
3615 (if anything-iswitchb-candidate-selected
3616 (anything-execute-selection-action)
3617 ad-do-it))
3618 (anything-iswitchb-setup-keys)
3619 (message "Iswitchb integration is activated."))
3621 (defun anything-iswitchb-setup-keys ()
3622 "Modify `anything-map' for anything-iswitchb users.
3624 C-p is used instead of M-p, because anything uses ESC
3625 (currently hardcoded) for `anything-iswitchb-cancel-anything' and
3626 Emacs handles ESC and Meta as synonyms, so ESC overrides
3627 other commands with Meta prefix.
3629 Note that iswitchb uses M-p and M-n by default for history
3630 navigation, so you should bind C-p and C-n in
3631 `iswitchb-mode-map' if you use the history keys and don't want
3632 to use different keys for iswitchb while anything is not yet
3633 kicked in. These keys are not bound automatically by anything
3634 in `iswitchb-mode-map' because they (C-n at least) already have
3635 a standard iswitchb binding which you might be accustomed to.
3637 Binding M-s is used instead of C-s, because C-s has a binding in
3638 iswitchb. You can rebind it AFTER `anything-iswitchb-setup'.
3640 Unbind C-r to prevent problems during anything-isearch."
3641 (define-key anything-map (kbd "C-s") nil)
3642 (define-key anything-map (kbd "M-p") nil)
3643 (define-key anything-map (kbd "M-n") nil)
3644 (define-key anything-map (kbd "M-v") nil)
3645 (define-key anything-map (kbd "C-v") nil)
3646 (define-key anything-map (kbd "C-p") 'anything-previous-history-element)
3647 (define-key anything-map (kbd "C-n") 'anything-next-history-element)
3648 (define-key anything-map (kbd "M-s") nil)
3649 (define-key anything-map (kbd "M-s") 'anything-isearch)
3650 (define-key anything-map (kbd "C-r") nil))
3652 (defun anything-iswitchb-minibuffer-setup ()
3653 (when (eq this-command 'iswitchb-buffer)
3654 (add-hook 'minibuffer-exit-hook 'anything-iswitchb-minibuffer-exit)
3656 (setq anything-iswitchb-frame-configuration nil)
3657 (setq anything-iswitchb-candidate-selected nil)
3658 (add-hook 'anything-update-hook 'anything-iswitchb-handle-update)
3660 (anything-initialize)
3662 (add-hook 'deferred-action-list 'anything-iswitchb-check-input)))
3665 (defun anything-iswitchb-minibuffer-exit ()
3666 (remove-hook 'minibuffer-exit-hook 'anything-iswitchb-minibuffer-exit)
3667 (remove-hook 'deferred-action-list 'anything-iswitchb-check-input)
3668 (remove-hook 'anything-update-hook 'anything-iswitchb-handle-update)
3670 (anything-cleanup)
3672 (when anything-iswitchb-frame-configuration
3673 (anything-set-frame/window-configuration
3674 anything-iswitchb-frame-configuration)
3675 (setq anything-iswitchb-frame-configuration nil)))
3678 (defun anything-iswitchb-check-input ()
3679 "Extract iswitchb input and check if it needs to be handled."
3680 (declare (special iswitchb-text))
3681 (if (or anything-iswitchb-frame-configuration
3682 (sit-for anything-iswitchb-idle-delay))
3683 (anything-check-new-input iswitchb-text)))
3686 (defun anything-iswitchb-handle-update ()
3687 "Pop up the anything buffer if it's not empty and it's not
3688 shown yet and bind anything commands in iswitchb."
3689 (unless (or (anything-empty-buffer-p anything-buffer)
3690 anything-iswitchb-frame-configuration)
3691 (setq anything-iswitchb-frame-configuration
3692 (anything-current-frame/window-configuration))
3694 (save-selected-window
3695 (if (not anything-samewindow)
3696 (pop-to-buffer anything-buffer)
3698 (select-window (get-lru-window))
3699 (switch-to-buffer anything-buffer)))
3701 (with-current-buffer (window-buffer (active-minibuffer-window))
3702 (let* ((anything-prefix "anything-")
3703 (prefix-length (length anything-prefix))
3704 (commands
3705 (delete-dups
3706 (remove-if 'null
3707 (mapcar
3708 (lambda (binding)
3709 (let ((command (cdr binding)))
3710 (when (and (symbolp command)
3711 (eq (compare-strings
3712 anything-prefix
3713 0 prefix-length
3714 (symbol-name command)
3715 0 prefix-length)
3717 command)))
3718 (cdr anything-map)))))
3719 (bindings (mapcar (lambda (command)
3720 (cons command
3721 (where-is-internal command anything-map)))
3722 commands)))
3724 (push (list 'anything-iswitchb-cancel-anything (kbd "<ESC>"))
3725 bindings)
3727 (setq anything-iswitchb-saved-keys nil)
3729 (let* ((iswitchb-prefix "iswitchb-")
3730 (prefix-length (length iswitchb-prefix)))
3731 (dolist (binding bindings)
3732 (dolist (key (cdr binding))
3733 (let ((old-command (lookup-key (current-local-map) key)))
3734 (unless (and anything-iswitchb-dont-touch-iswithcb-keys
3735 (symbolp old-command)
3736 (eq (compare-strings iswitchb-prefix
3737 0 prefix-length
3738 (symbol-name old-command)
3739 0 prefix-length)
3741 (push (cons key old-command)
3742 anything-iswitchb-saved-keys)
3743 (define-key (current-local-map) key (car binding)))))))))))
3746 (defun anything-iswitchb-cancel-anything ()
3747 "Cancel anything completion and return to standard iswitchb."
3748 (interactive)
3749 (save-excursion
3750 (dolist (binding anything-iswitchb-saved-keys)
3751 (define-key (current-local-map) (car binding) (cdr binding)))
3752 (anything-iswitchb-minibuffer-exit)))
3754 ;; (@* "Compatibility")
3756 ;; Copied assoc-default from XEmacs version 21.5.12
3757 (unless (fboundp 'assoc-default)
3758 (defun assoc-default (key alist &optional test default)
3759 "Find object KEY in a pseudo-alist ALIST.
3760 ALIST is a list of conses or objects. Each element (or the element's car,
3761 if it is a cons) is compared with KEY by evaluating (TEST (car elt) KEY).
3762 If that is non-nil, the element matches;
3763 then `assoc-default' returns the element's cdr, if it is a cons,
3764 or DEFAULT if the element is not a cons.
3766 If no element matches, the value is nil.
3767 If TEST is omitted or nil, `equal' is used."
3768 (let (found (tail alist) value)
3769 (while (and tail (not found))
3770 (let ((elt (car tail)))
3771 (when (funcall (or test 'equal) (if (consp elt) (car elt) elt) key)
3772 (setq found t value (if (consp elt) (cdr elt) default))))
3773 (setq tail (cdr tail)))
3774 value)))
3776 ;; Function not available in XEmacs,
3777 (unless (fboundp 'minibuffer-contents)
3778 (defun minibuffer-contents ()
3779 "Return the user input in a minbuffer as a string.
3780 The current buffer must be a minibuffer."
3781 (field-string (point-max)))
3783 (defun delete-minibuffer-contents ()
3784 "Delete all user input in a minibuffer.
3785 The current buffer must be a minibuffer."
3786 (delete-field (point-max))))
3788 ;; Function not available in older Emacs (<= 22.1).
3789 (unless (fboundp 'buffer-chars-modified-tick)
3790 (defun buffer-chars-modified-tick (&optional buffer)
3791 "Return BUFFER's character-change tick counter.
3792 Each buffer has a character-change tick counter, which is set to the
3793 value of the buffer's tick counter (see `buffer-modified-tick'), each
3794 time text in that buffer is inserted or deleted. By comparing the
3795 values returned by two individual calls of `buffer-chars-modified-tick',
3796 you can tell whether a character change occurred in that buffer in
3797 between these calls. No argument or nil as argument means use current
3798 buffer as BUFFER."
3799 (with-current-buffer (or buffer (current-buffer))
3800 (if (listp buffer-undo-list)
3801 (length buffer-undo-list)
3802 (buffer-modified-tick)))))
3804 ;; (@* "CUA workaround")
3805 (defadvice cua-delete-region (around anything-avoid-cua activate)
3806 (ignore-errors ad-do-it))
3807 (defadvice copy-region-as-kill (around anything-avoid-cua activate)
3808 (if cua-mode
3809 (ignore-errors ad-do-it)
3810 ad-do-it))
3813 ;;(@* "Attribute Documentation")
3814 (defun anything-describe-anything-attribute (anything-attribute)
3815 "Display the full documentation of ANYTHING-ATTRIBUTE (a symbol)."
3816 (interactive (list (intern
3817 (completing-read
3818 "Describe anything attribute: "
3819 (mapcar 'symbol-name anything-additional-attributes)))))
3820 (with-output-to-temp-buffer "*Help*"
3821 (princ (get anything-attribute 'anything-attrdoc))))
3823 (anything-document-attribute 'name "mandatory"
3824 " The name of the source. It is also the heading which appears
3825 above the list of matches from the source. Must be unique. ")
3826 (anything-document-attribute 'header-name "optional"
3827 " A function returning the display string of the header. Its
3828 argument is the name of the source. This attribute is useful to
3829 add an additional information with the source name. ")
3830 (anything-document-attribute 'candidates "mandatory if candidates-in-buffer attribute is not provided"
3831 " Specifies how to retrieve candidates from the source. It can
3832 either be a variable name, a function called with no parameters
3833 or the actual list of candidates.
3835 The list must be a list whose members are strings, symbols
3836 or (DISPLAY . REAL) pairs.
3838 In case of (DISPLAY . REAL) pairs, the DISPLAY string is shown
3839 in the Anything buffer, but the REAL one is used as action
3840 argument when the candidate is selected. This allows a more
3841 readable presentation for candidates which would otherwise be,
3842 for example, too long or have a common part shared with other
3843 candidates which can be safely replaced with an abbreviated
3844 string for display purposes.
3846 Note that if the (DISPLAY . REAL) form is used then pattern
3847 matching is done on the displayed string, not on the real
3848 value.
3850 If the candidates have to be retrieved asynchronously (for
3851 example, by an external command which takes a while to run)
3852 then the function should start the external command
3853 asynchronously and return the associated process object.
3854 Anything will take care of managing the process (receiving the
3855 output from it, killing it if necessary, etc.). The process
3856 should return candidates matching the current pattern (see
3857 variable `anything-pattern'.)
3859 Note that currently results from asynchronous sources appear
3860 last in the anything buffer regardless of their position in
3861 `anything-sources'. ")
3862 (anything-document-attribute 'action "mandatory if type attribute is not provided"
3863 " It is a list of (DISPLAY . FUNCTION) pairs or FUNCTION.
3864 FUNCTION is called with one parameter: the selected candidate.
3866 An action other than the default can be chosen from this list
3867 of actions for the currently selected candidate (by default
3868 with TAB). The DISPLAY string is shown in the completions
3869 buffer and the FUNCTION is invoked when an action is
3870 selected. The first action of the list is the default. ")
3871 (anything-document-attribute 'type "optional if action attribute is provided"
3872 " Indicates the type of the items the source returns.
3874 Merge attributes not specified in the source itself from
3875 `anything-type-attributes'.
3877 This attribute is implemented by plug-in. ")
3878 (anything-document-attribute 'init "optional"
3879 " Function called with no parameters when anything is started. It
3880 is useful for collecting current state information which can be
3881 used to create the list of candidates later.
3883 For example, if a source needs to work with the current
3884 directory then it can store its value here, because later
3885 anything does its job in the minibuffer and in the
3886 `anything-buffer' and the current directory can be different
3887 there. ")
3888 (anything-document-attribute 'delayed-init "optional"
3889 " Function called with no parameters before candidate function is
3890 called. It is similar with `init' attribute, but its
3891 evaluation is deferred. It is useful to combine with ")
3892 (anything-document-attribute 'match "optional"
3893 " List of functions called with one parameter: a candidate. The
3894 function should return non-nil if the candidate matches the
3895 current pattern (see variable `anything-pattern').
3897 This attribute allows the source to override the default
3898 pattern matching based on `string-match'. It can be used, for
3899 example, to implement a source for file names and do the
3900 pattern matching on the basename of files, since it's more
3901 likely one is typing part of the basename when searching for a
3902 file, instead of some string anywhere else in its path.
3904 If the list contains more than one function then the list of
3905 matching candidates from the source is constructed by appending
3906 the results after invoking the first function on all the
3907 potential candidates, then the next function, and so on. The
3908 matching candidates supplied by the first function appear first
3909 in the list of results and then results from the other
3910 functions, respectively.
3912 This attribute has no effect for asynchronous sources (see
3913 attribute `candidates'), since they perform pattern matching
3914 themselves. ")
3915 (anything-document-attribute 'candidate-transformer "optional"
3916 " It's a function or a list of functions called with one argument
3917 when the completion list from the source is built. The argument
3918 is the list of candidates retrieved from the source. The
3919 function should return a transformed list of candidates which
3920 will be used for the actual completion. If it is a list of
3921 functions, it calls each function sequentially.
3923 This can be used to transform or remove items from the list of
3924 candidates.
3926 Note that `candidates' is run already, so the given transformer
3927 function should also be able to handle candidates with (DISPLAY
3928 . REAL) format. ")
3929 (anything-document-attribute 'filtered-candidate-transformer "optional"
3930 " It has the same format as `candidate-transformer', except the
3931 function is called with two parameters: the candidate list and
3932 the source.
3934 This transformer is run on the candidate list which is already
3935 filtered by the current pattern. While `candidate-transformer'
3936 is run only once, it is run every time the input pattern is
3937 changed.
3939 It can be used to transform the candidate list dynamically, for
3940 example, based on the current pattern.
3942 In some cases it may also be more efficent to perform candidate
3943 transformation here, instead of with `candidate-transformer'
3944 even if this transformation is done every time the pattern is
3945 changed. For example, if a candidate set is very large then
3946 `candidate-transformer' transforms every candidate while only
3947 some of them will actually be dislpayed due to the limit
3948 imposed by `anything-candidate-number-limit'.
3950 Note that `candidates' and `candidate-transformer' is run
3951 already, so the given transformer function should also be able
3952 to handle candidates with (DISPLAY . REAL) format.
3954 This option has no effect for asynchronous sources. (Not yet,
3955 at least. ")
3956 (anything-document-attribute 'action-transformer "optional"
3957 " It's a function or a list of functions called with two
3958 arguments when the action list from the source is
3959 assembled. The first argument is the list of actions, the
3960 second is the current selection. If it is a list of functions,
3961 it calls each function sequentially.
3963 The function should return a transformed action list.
3965 This can be used to customize the list of actions based on the
3966 currently selected candidate. ")
3967 (anything-document-attribute 'pattern-transformer "optional"
3968 " It's a function or a list of functions called with one argument
3969 before computing matches. Its argument is `anything-pattern'.
3970 Functions should return transformed `anything-pattern'.
3972 It is useful to change interpretation of `anything-pattern'. ")
3973 (anything-document-attribute 'delayed "optional"
3974 " Candidates from the source are shown only if the user stops
3975 typing and is idle for `anything-idle-delay' seconds. ")
3976 (anything-document-attribute 'volatile "optional"
3977 " Indicates the source assembles the candidate list dynamically,
3978 so it shouldn't be cached within a single Anything
3979 invocation. It is only applicable to synchronous sources,
3980 because asynchronous sources are not cached. ")
3981 (anything-document-attribute 'requires-pattern "optional"
3982 " If present matches from the source are shown only if the
3983 pattern is not empty. Optionally, it can have an integer
3984 parameter specifying the required length of input which is
3985 useful in case of sources with lots of candidates. ")
3986 (anything-document-attribute 'persistent-action "optional"
3987 " Function called with one parameter; the selected candidate.
3989 An action performed by `anything-execute-persistent-action'.
3990 If none, use the default action. ")
3991 (anything-document-attribute 'candidates-in-buffer "optional"
3992 " Shortcut attribute for making and narrowing candidates using
3993 buffers. This newly-introduced attribute prevents us from
3994 forgetting to add volatile and match attributes.
3996 See docstring of `anything-candidates-in-buffer'.
3998 (candidates-in-buffer) is equivalent of three attributes:
3999 (candidates . anything-candidates-in-buffer)
4000 (volatile)
4001 (match identity)
4003 (candidates-in-buffer . candidates-function) is equivalent of:
4004 (candidates . candidates-function)
4005 (volatile)
4006 (match identity)
4008 This attribute is implemented by plug-in. ")
4009 (anything-document-attribute 'search "optional"
4010 " List of functions like `re-search-forward' or `search-forward'.
4011 Buffer search function used by `anything-candidates-in-buffer'.
4012 By default, `anything-candidates-in-buffer' uses `re-search-forward'.
4013 This attribute is meant to be used with
4014 (candidates . anything-candidates-in-buffer) or
4015 (candidates-in-buffer) in short. ")
4016 (anything-document-attribute 'search-from-end "optional"
4017 " Make `anything-candidates-in-buffer' search from the end of buffer.
4018 If this attribute is specified, `anything-candidates-in-buffer' uses
4019 `re-search-backward' instead. ")
4020 (anything-document-attribute 'get-line "optional"
4021 " A function like `buffer-substring-no-properties' or `buffer-substring'.
4022 This function converts point of line-beginning and point of line-end,
4023 which represents a candidate computed by `anything-candidates-in-buffer'.
4024 By default, `anything-candidates-in-buffer' uses
4025 `buffer-substring-no-properties'. ")
4026 (anything-document-attribute 'display-to-real "optional"
4027 " Function called with one parameter; the selected candidate.
4029 The function transforms the selected candidate, and the result
4030 is passed to the action function. The display-to-real
4031 attribute provides another way to pass other string than one
4032 shown in Anything buffer.
4034 Traditionally, it is possible to make candidates,
4035 candidate-transformer or filtered-candidate-transformer
4036 function return a list with (DISPLAY . REAL) pairs. But if REAL
4037 can be generated from DISPLAY, display-to-real is more
4038 convenient and faster. ")
4039 (anything-document-attribute 'real-to-display "optional"
4040 " Function called with one parameter; the selected candidate.
4042 The inverse of display-to-real attribute.
4044 The function transforms the selected candidate, which is passed
4045 to the action function, for display. The real-to-display
4046 attribute provides the other way to pass other string than one
4047 shown in Anything buffer.
4049 Traditionally, it is possible to make candidates,
4050 candidate-transformer or filtered-candidate-transformer
4051 function return a list with (DISPLAY . REAL) pairs. But if
4052 DISPLAY can be generated from REAL, real-to-display is more
4053 convenient.
4055 Note that DISPLAY parts returned from candidates /
4056 candidate-transformer are IGNORED as the name `display-to-real'
4057 says. ")
4058 (anything-document-attribute 'cleanup "optional"
4059 " Function called with no parameters when *anything* buffer is closed. It
4060 is useful for killing unneeded candidates buffer.
4062 Note that the function is executed BEFORE performing action. ")
4063 (anything-document-attribute 'candidate-number-limit "optional"
4064 " Override `anything-candidate-number-limit' only for this source. ")
4065 (anything-document-attribute 'accept-empty "optional"
4066 " Pass empty string \"\" to action function. ")
4067 (anything-document-attribute 'disable-shortcuts "optional"
4068 " Disable `anything-enable-shortcuts' in current `anything' session.
4070 This attribute is implemented by plug-in. ")
4071 (anything-document-attribute 'dummy "optional"
4072 " Set `anything-pattern' to candidate. If this attribute is
4073 specified, The candidates attribute is ignored.
4075 This attribute is implemented by plug-in.
4076 This plug-in implies disable-shortcuts plug-in. ")
4077 (anything-document-attribute 'multiline "optional"
4078 " Enable to selection multiline candidates. ")
4079 (anything-document-attribute 'update "optional"
4080 " Function called with no parameters when \\<anything-map>\\[anything-force-update] is pressed. ")
4081 (anything-document-attribute 'mode-line "optional"
4082 " source local `anything-mode-line-string'. (included in `mode-line-format')
4083 It accepts also variable/function name. ")
4084 (anything-document-attribute 'header-line "optional"
4085 " source local `header-line-format'.
4086 It accepts also variable/function name. ")
4087 (anything-document-attribute 'resume "optional" " Function called with no parameters when `anything-resume' is started.")
4089 ;; (@* "Bug Report")
4090 (defvar anything-maintainer-mail-address
4091 (concat "rubiki" "tch@ru" "by-lang.org"))
4092 (defvar anything-bug-report-salutation
4093 "Describe bug below, using a precise recipe.
4095 When I executed M-x ...
4097 How to send a bug report:
4098 1) Be sure to use the LATEST version of anything.el.
4099 2) Enable debugger. M-x toggle-debug-on-error or (setq debug-on-error t)
4100 3) Use Lisp version instead of compiled one: (load \"anything.el\")
4101 4) If you got an error, please paste *Backtrace* buffer.
4102 5) Type C-c C-c to send.
4103 # If you are a Japanese, please write in Japanese:-)")
4104 (defvar anything-no-dump-variables
4105 '(anything-candidate-buffer-alist
4106 anything-digit-overlays
4107 anything-help-message
4108 anything-candidate-cache
4110 "Variables not to dump in bug report.")
4112 (defun anything-dumped-variables-in-bug-report ()
4113 (let ((hash (make-hash-table)))
4114 (loop for var in (apropos-internal "anything-" 'boundp)
4115 for vname = (symbol-name var)
4116 unless (or (string-match "-map$" vname)
4117 (string-match "^anything-c-source-" vname)
4118 (string-match "-hash$" vname)
4119 (string-match "-face$" vname)
4120 (memq var anything-no-dump-variables))
4121 collect var)))
4123 (defun anything-send-bug-report ()
4124 "Send a bug report of anything.el."
4125 (interactive)
4126 (with-current-buffer (or anything-last-buffer
4127 (current-buffer))
4128 (reporter-submit-bug-report
4129 anything-maintainer-mail-address
4130 "anything.el"
4131 (anything-dumped-variables-in-bug-report)
4132 nil nil
4133 anything-bug-report-salutation)))
4135 (defun anything-send-bug-report-from-anything ()
4136 "Send a bug report of anything.el in anything session."
4137 (interactive)
4138 (anything-run-after-quit 'anything-send-bug-report))
4140 ;; (@* "Unit Tests")
4142 (defun* anything-test-candidates (sources &optional (input "") (compile-source-functions anything-compile-source-functions-default))
4143 "Test helper function for anything.
4144 Given pseudo `anything-sources' and `anything-pattern', returns list like
4145 ((\"source name1\" (\"candidate1\" \"candidate2\"))
4146 (\"source name2\" (\"candidate3\" \"candidate4\")))
4148 (let ((anything-test-mode t)
4149 anything-enable-shortcuts
4150 anything-candidate-cache
4151 (anything-compile-source-functions compile-source-functions)
4152 anything-before-initialize-hook
4153 anything-after-initialize-hook
4154 anything-update-hook
4155 anything-test-candidate-list)
4156 (get-buffer-create anything-buffer)
4158 (anything-initialize-1 nil input sources)
4159 (anything-update)
4160 ;; test-mode spec: select 1st candidate!
4161 (with-current-buffer anything-buffer
4162 (forward-line 1)
4163 (anything-mark-current-line))
4164 (prog1
4165 anything-test-candidate-list
4166 (anything-cleanup))))
4168 (defmacro anything-test-update (sources pattern)
4169 "Test helper macro for anything. It is meant for testing *anything* buffer contents."
4170 `(progn (stub anything-get-sources => ,sources)
4171 (stub anything-log-run-hook => nil)
4172 (stub anything-maybe-fit-frame => nil)
4173 (stub run-with-idle-timer => nil)
4174 (let (anything-test-mode (anything-pattern ,pattern))
4175 (anything-update))))
4177 ;;;; unit test
4178 ;; (install-elisp "http://www.emacswiki.org/cgi-bin/wiki/download/el-expectations.el")
4179 ;; (install-elisp "http://www.emacswiki.org/cgi-bin/wiki/download/el-mock.el")
4180 (dont-compile
4181 (when (fboundp 'expectations)
4182 (expectations
4183 (desc "anything-current-buffer")
4184 (expect "__a_buffer"
4185 (with-current-buffer (get-buffer-create "__a_buffer")
4186 (anything-test-candidates '(((name . "FOO"))) "")
4187 (prog1
4188 (buffer-name anything-current-buffer)
4189 (kill-buffer "__a_buffer")
4191 (desc "anything-buffer-file-name")
4192 (expect (regexp "/__a_file__")
4193 (with-current-buffer (get-buffer-create "__a_file__")
4194 (setq buffer-file-name "/__a_file__")
4195 (anything-test-candidates '(((name . "FOO"))) "")
4196 (prog1
4197 anything-buffer-file-name
4198 ;;(kill-buffer "__a_file__")
4200 (desc "anything-interpret-value")
4201 (expect "literal"
4202 (anything-interpret-value "literal"))
4203 (expect "lambda"
4204 (anything-interpret-value (lambda () "lambda")))
4205 (expect "lambda with source name"
4206 (let ((source '((name . "lambda with source name"))))
4207 (anything-interpret-value (lambda () anything-source-name) source)))
4208 (expect "function symbol"
4209 (flet ((f () "function symbol"))
4210 (anything-interpret-value 'f)))
4211 (expect "variable symbol"
4212 (let ((v "variable symbol"))
4213 (anything-interpret-value 'v)))
4214 (expect (error error *)
4215 (anything-interpret-value 'unbounded-1))
4216 (desc "anything-compile-sources")
4217 (expect '(((name . "foo")))
4218 (anything-compile-sources '(((name . "foo"))) nil)
4220 (expect '(((name . "foo") (type . test) (action . identity)))
4221 (let ((anything-type-attributes '((test (action . identity)))))
4222 (anything-compile-sources '(((name . "foo") (type . test)))
4223 '(anything-compile-source--type))))
4224 (desc "anything-sources accepts symbols")
4225 (expect '(((name . "foo")))
4226 (let* ((foo '((name . "foo"))))
4227 (anything-compile-sources '(foo) nil)))
4228 (desc "anything-get-sources action")
4229 (expect '(((name . "Actions") (candidates . actions)))
4230 (stub anything-action-window => t)
4231 (let (anything-compiled-sources
4232 (anything-sources '(((name . "Actions") (candidates . actions)))))
4233 (anything-get-sources)))
4234 (desc "get-buffer-create candidates-buffer")
4235 (expect '(((name . "many") (init . many-init)
4236 (candidates-in-buffer . anything-candidates-in-buffer)
4237 (candidates . anything-candidates-in-buffer)
4238 (volatile) (match identity)))
4239 (anything-compile-sources
4240 '(((name . "many") (init . many-init)
4241 (candidates-in-buffer . anything-candidates-in-buffer)))
4242 '(anything-compile-source--candidates-in-buffer)))
4243 (expect '(((name . "many") (init . many-init)
4244 (candidates-in-buffer)
4245 (candidates . anything-candidates-in-buffer)
4246 (volatile) (match identity)))
4247 (anything-compile-sources
4248 '(((name . "many") (init . many-init)
4249 (candidates-in-buffer)))
4250 '(anything-compile-source--candidates-in-buffer)))
4251 (expect '(((name . "many") (init . many-init)
4252 (candidates-in-buffer)
4253 (type . test)
4254 (action . identity)
4255 (candidates . anything-candidates-in-buffer)
4256 (volatile) (match identity)))
4257 (let ((anything-type-attributes '((test (action . identity)))))
4258 (anything-compile-sources
4259 '(((name . "many") (init . many-init)
4260 (candidates-in-buffer)
4261 (type . test)))
4262 '(anything-compile-source--type
4263 anything-compile-source--candidates-in-buffer))))
4265 (desc "anything-get-candidates")
4266 (expect '("foo" "bar")
4267 (anything-get-candidates '((name . "foo") (candidates "foo" "bar"))))
4268 (expect '("FOO" "BAR")
4269 (anything-get-candidates '((name . "foo") (candidates "foo" "bar")
4270 (candidate-transformer
4271 . (lambda (cands) (mapcar 'upcase cands))))))
4272 (expect '("foo" "bar")
4273 (anything-get-candidates '((name . "foo")
4274 (candidates . (lambda () '("foo" "bar"))))))
4275 (expect '("foo" "bar")
4276 (let ((var '("foo" "bar")))
4277 (anything-get-candidates '((name . "foo")
4278 (candidates . var)))))
4279 (expect (error error *)
4280 (anything-get-candidates '((name . "foo")
4281 (candidates . "err"))))
4282 (expect (error error *)
4283 (let ((var "err"))
4284 (anything-get-candidates '((name . "foo")
4285 (candidates . var)))))
4286 (expect (error error *)
4287 (anything-get-candidates '((name . "foo")
4288 (candidates . unDeFined-syMbol))))
4289 (desc "anything-compute-matches")
4290 (expect '("foo" "bar")
4291 (let ((anything-pattern ""))
4292 (anything-compute-matches '((name . "FOO") (candidates "foo" "bar") (volatile)))))
4293 (expect '("foo")
4294 (let ((anything-pattern "oo"))
4295 (anything-compute-matches '((name . "FOO") (candidates "foo" "bar") (volatile)))))
4296 (expect '("bar")
4297 (let ((anything-pattern "^b"))
4298 (anything-compute-matches '((name . "FOO") (candidates "foo" "bar") (volatile)))))
4299 (expect '("a" "b")
4300 (let ((anything-pattern "")
4301 (anything-candidate-number-limit 2))
4302 (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c") (volatile)))))
4303 (expect '("a" "b")
4304 (let ((anything-pattern ".")
4305 (anything-candidate-number-limit 2))
4306 (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c") (volatile)))))
4307 (expect '("a" "b" "c")
4308 (let ((anything-pattern "")
4309 anything-candidate-number-limit)
4310 (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c") (volatile)))))
4311 (expect '("a" "b" "c")
4312 (let ((anything-pattern "[abc]")
4313 anything-candidate-number-limit)
4314 (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c") (volatile)))))
4315 (expect '(a b c)
4316 (let ((anything-pattern "[abc]")
4317 anything-candidate-number-limit)
4318 (anything-compute-matches '((name . "FOO") (candidates a b c) (volatile)))))
4319 (expect '(("foo" . "FOO") ("bar" . "BAR"))
4320 (let ((anything-pattern ""))
4321 (anything-compute-matches '((name . "FOO") (candidates ("foo" . "FOO") ("bar" . "BAR")) (volatile)))))
4322 (expect '(("foo" . "FOO"))
4323 (let ((anything-pattern "foo"))
4324 (anything-compute-matches '((name . "FOO") (candidates ("foo" . "FOO") ("bar" . "foo")) (volatile)))))
4325 ;; using anything-test-candidate-list
4326 (desc "anything-test-candidates")
4327 (expect '(("FOO" ("foo" "bar")))
4328 (anything-test-candidates '(((name . "FOO") (candidates "foo" "bar")))))
4329 (expect '(("FOO" ("bar")))
4330 (anything-test-candidates '(((name . "FOO") (candidates "foo" "bar"))) "ar"))
4331 (expect '(("T1" ("hoge" "aiue"))
4332 ("T2" ("test" "boke")))
4333 (anything-test-candidates '(((name . "T1") (candidates "hoge" "aiue"))
4334 ((name . "T2") (candidates "test" "boke")))))
4335 (expect '(("T1" ("hoge"))
4336 ("T2" ("boke")))
4337 (anything-test-candidates '(((name . "T1") (candidates "hoge" "aiue"))
4338 ((name . "T2") (candidates "test" "boke"))) "o"))
4339 (desc "requires-pattern attribute")
4340 (expect nil
4341 (anything-test-candidates '(((name . "FOO") (candidates "foo" "bar")
4342 (requires-pattern . 1)))))
4343 (expect '(("FOO" ("bar")))
4344 (anything-test-candidates '(((name . "FOO") (candidates "foo" "bar")
4345 (requires-pattern . 1))) "b"))
4346 (desc "delayed attribute(for test)")
4347 (expect '(("T2" ("boke"))
4348 ("T1" ("hoge")))
4349 (anything-test-candidates
4350 '(((name . "T1") (candidates "hoge" "aiue") (delayed))
4351 ((name . "T2") (candidates "test" "boke")))
4352 "o"))
4353 (desc "match attribute(prefix search)")
4354 (expect '(("FOO" ("bar")))
4355 (anything-test-candidates
4356 '(((name . "FOO") (candidates "foo" "bar")
4357 (match (lambda (c) (string-match (concat "^" anything-pattern) c)))))
4358 "ba"))
4359 (expect nil
4360 (anything-test-candidates
4361 '(((name . "FOO") (candidates "foo" "bar")
4362 (match (lambda (c) (string-match (concat "^" anything-pattern) c)))))
4363 "ar"))
4364 (expect "TestSource"
4365 (let (x)
4366 (anything-test-candidates
4367 '(((name . "TestSource") (candidates "a")
4368 (match (lambda (c) (setq x anything-source-name)))))
4369 "a")
4371 (desc "init attribute")
4372 (expect '(("FOO" ("bar")))
4373 (let (v)
4374 (anything-test-candidates
4375 '(((name . "FOO") (init . (lambda () (setq v '("foo" "bar"))))
4376 (candidates . v)))
4377 "ar")))
4378 (desc "candidate-transformer attribute")
4379 (expect '(("FOO" ("BAR")))
4380 (anything-test-candidates '(((name . "FOO") (candidates "foo" "bar")
4381 (candidate-transformer
4382 . (lambda (cands) (mapcar 'upcase cands)))))
4383 "ar"))
4384 (desc "filtered-candidate-transformer attribute")
4385 ;; needs more tests
4386 (expect '(("FOO" ("BAR")))
4387 (anything-test-candidates '(((name . "FOO") (candidates "foo" "bar")
4388 (filtered-candidate-transformer
4389 . (lambda (cands src) (mapcar 'upcase cands)))))
4390 "ar"))
4391 (desc "anything-transform-candidates in process")
4392 (expect (mock (anything-composed-funcall-with-source
4393 '((name . "FOO") (candidates "foo" "bar")
4394 (filtered-candidate-transformer
4395 . (lambda (cands src) (mapcar 'upcase cands))))
4396 (lambda (cands src) (mapcar 'upcase cands))
4397 '("foo" "bar")
4398 '((name . "FOO") (candidates "foo" "bar")
4399 (filtered-candidate-transformer
4400 . (lambda (cands src) (mapcar 'upcase cands))))
4402 (stub anything-process-candidate-transformer => '("foo" "bar"))
4403 (anything-transform-candidates
4404 '("foo" "bar")
4405 '((name . "FOO") (candidates "foo" "bar")
4406 (filtered-candidate-transformer
4407 . (lambda (cands src) (mapcar 'upcase cands))))
4410 (desc "anything-candidates-in-buffer-1")
4411 (expect nil
4412 (anything-candidates-in-buffer-1
4413 nil ""
4414 'buffer-substring-no-properties '(re-search-forward) 50 nil))
4415 (expect '("foo+" "bar+" "baz+")
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) 5 nil)))
4421 (expect '("foo+" "bar+")
4422 (with-temp-buffer
4423 (insert "foo+\nbar+\nbaz+\n")
4424 (anything-candidates-in-buffer-1
4425 (current-buffer) ""
4426 'buffer-substring-no-properties '(re-search-forward) 2 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 '(re-search-forward) 50 nil)))
4433 (expect '("foo+")
4434 (with-temp-buffer
4435 (insert "foo+\nbar+\nbaz+\n")
4436 (anything-candidates-in-buffer-1
4437 (current-buffer) "oo+"
4438 #'buffer-substring-no-properties '(search-forward) 50 nil)))
4439 (expect '("foo+" "bar+")
4440 (with-temp-buffer
4441 (insert "foo+\nbar+\nbaz+\n")
4442 (anything-candidates-in-buffer-1
4443 (current-buffer) "."
4444 'buffer-substring-no-properties '(re-search-forward) 2 nil)))
4445 (expect '(("foo+" "FOO+"))
4446 (with-temp-buffer
4447 (insert "foo+\nbar+\nbaz+\n")
4448 (anything-candidates-in-buffer-1
4449 (current-buffer) "oo\\+"
4450 (lambda (s e)
4451 (let ((l (buffer-substring-no-properties s e)))
4452 (list l (upcase l))))
4453 '(re-search-forward) 50 nil)))
4454 (desc "anything-candidates-in-buffer")
4455 (expect '(("TEST" ("foo+" "bar+" "baz+")))
4456 (anything-test-candidates
4457 '(((name . "TEST")
4458 (init
4459 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4460 (insert "foo+\nbar+\nbaz+\n"))))
4461 (candidates . anything-candidates-in-buffer)
4462 (match identity)
4463 (volatile)))))
4464 (expect '(("TEST" ("foo+" "bar+" "baz+")))
4465 (let (anything-candidate-number-limit)
4466 (anything-test-candidates
4467 '(((name . "TEST")
4468 (init
4469 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4470 (insert "foo+\nbar+\nbaz+\n"))))
4471 (candidates . anything-candidates-in-buffer)
4472 (match identity)
4473 (volatile))))))
4474 (expect '(("TEST" ("foo+")))
4475 (anything-test-candidates
4476 '(((name . "TEST")
4477 (init
4478 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4479 (insert "foo+\nbar+\nbaz+\n"))))
4480 (candidates . anything-candidates-in-buffer)
4481 (match identity)
4482 (volatile)))
4483 "oo\\+"))
4484 (desc "search attribute")
4485 (expect '(("TEST" ("foo+")))
4486 (anything-test-candidates
4487 '(((name . "TEST")
4488 (init
4489 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4490 (insert "foo+\nbar+\nbaz+\nooo\n"))))
4491 (search search-forward)
4492 (candidates . anything-candidates-in-buffer)
4493 (match identity)
4494 (volatile)))
4495 "oo+"))
4496 (expect '(("TEST" ("foo+" "ooo")))
4497 (anything-test-candidates
4498 '(((name . "TEST")
4499 (init
4500 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4501 (insert "foo+\nbar+\nbaz+\nooo\n"))))
4502 (search search-forward re-search-forward)
4503 (candidates . anything-candidates-in-buffer)
4504 (match identity)
4505 (volatile)))
4506 "oo+"))
4507 (expect '(("TEST" ("foo+" "ooo")))
4508 (anything-test-candidates
4509 '(((name . "TEST")
4510 (init
4511 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4512 (insert "foo+\nbar+\nbaz+\nooo\n"))))
4513 (search re-search-forward search-forward)
4514 (candidates . anything-candidates-in-buffer)
4515 (match identity)
4516 (volatile)))
4517 "oo+"))
4518 (expect '(("TEST" ("ooo" "foo+")))
4519 (anything-test-candidates
4520 '(((name . "TEST")
4521 (init
4522 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4523 (insert "bar+\nbaz+\nooo\nfoo+\n"))))
4524 (search re-search-forward search-forward)
4525 (candidates . anything-candidates-in-buffer)
4526 (match identity)
4527 (volatile)))
4528 "oo+"))
4529 ;; faster exact match
4530 (expect '(("TEST" ("bar+")))
4531 (anything-test-candidates
4532 '(((name . "TEST")
4533 (init
4534 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4535 (insert "bar+\nbaz+\nooo\nfoo+\n"))))
4536 (search (lambda (pattern &rest _)
4537 (and (search-forward (concat "\n" pattern "\n") nil t)
4538 (forward-line -1))))
4539 (candidates . anything-candidates-in-buffer)
4540 (match identity)
4541 (volatile)))
4542 "bar+"))
4543 ;; faster prefix match
4544 (expect '(("TEST" ("bar+")))
4545 (anything-test-candidates
4546 '(((name . "TEST")
4547 (init
4548 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4549 (insert "bar+\nbaz+\nooo\nfoo+\n"))))
4550 (search (lambda (pattern &rest _)
4551 (search-forward (concat "\n" pattern) nil t)))
4552 (candidates . anything-candidates-in-buffer)
4553 (match identity)
4554 (volatile)))
4555 "ba"))
4556 (desc "anything-current-buffer-is-modified")
4557 (expect '(("FOO" ("modified")))
4558 (let ((sources '(((name . "FOO")
4559 (candidates
4560 . (lambda ()
4561 (if (anything-current-buffer-is-modified)
4562 '("modified")
4563 '("unmodified"))))))))
4564 (with-temp-buffer
4565 (clrhash anything-tick-hash)
4566 (insert "1")
4567 (anything-test-candidates sources))))
4568 (expect '(("FOO" ("unmodified")))
4569 (let ((sources '(((name . "FOO")
4570 (candidates
4571 . (lambda ()
4572 (if (anything-current-buffer-is-modified)
4573 '("modified")
4574 '("unmodified"))))))))
4575 (with-temp-buffer
4576 (clrhash anything-tick-hash)
4577 (insert "1")
4578 (anything-test-candidates sources)
4579 (anything-test-candidates sources))))
4580 (expect '(("FOO" ("modified")))
4581 (let ((sources '(((name . "FOO")
4582 (candidates
4583 . (lambda ()
4584 (if (anything-current-buffer-is-modified)
4585 '("modified")
4586 '("unmodified"))))))))
4587 (with-temp-buffer
4588 (clrhash anything-tick-hash)
4589 (insert "1")
4590 (anything-test-candidates sources)
4591 (insert "2")
4592 (anything-test-candidates sources))))
4593 (expect '(("BAR" ("modified")))
4594 (let ((sources1 '(((name . "FOO")
4595 (candidates
4596 . (lambda ()
4597 (if (anything-current-buffer-is-modified)
4598 '("modified")
4599 '("unmodified")))))))
4600 (sources2 '(((name . "BAR")
4601 (candidates
4602 . (lambda ()
4603 (if (anything-current-buffer-is-modified)
4604 '("modified")
4605 '("unmodified"))))))))
4606 (with-temp-buffer
4607 (clrhash anything-tick-hash)
4608 (insert "1")
4609 (anything-test-candidates sources1)
4610 (anything-test-candidates sources2))))
4611 (expect '(("FOO" ("unmodified")))
4612 (let ((sources1 '(((name . "FOO")
4613 (candidates
4614 . (lambda ()
4615 (if (anything-current-buffer-is-modified)
4616 '("modified")
4617 '("unmodified")))))))
4618 (sources2 '(((name . "BAR")
4619 (candidates
4620 . (lambda ()
4621 (if (anything-current-buffer-is-modified)
4622 '("modified")
4623 '("unmodified"))))))))
4624 (with-temp-buffer
4625 (clrhash anything-tick-hash)
4626 (insert "1")
4627 (anything-test-candidates sources1)
4628 (anything-test-candidates sources2)
4629 (anything-test-candidates sources1))))
4630 (expect '(("BAR" ("unmodified")))
4631 (let ((sources1 '(((name . "FOO")
4632 (candidates
4633 . (lambda ()
4634 (if (anything-current-buffer-is-modified)
4635 '("modified")
4636 '("unmodified")))))))
4637 (sources2 '(((name . "BAR")
4638 (candidates
4639 . (lambda ()
4640 (if (anything-current-buffer-is-modified)
4641 '("modified")
4642 '("unmodified"))))))))
4643 (with-temp-buffer
4644 (clrhash anything-tick-hash)
4645 (insert "1")
4646 (anything-test-candidates sources1)
4647 (anything-test-candidates sources2)
4648 (anything-test-candidates sources2))))
4649 (expect '(("BAR" ("modified")))
4650 (let ((sources1 '(((name . "FOO")
4651 (candidates
4652 . (lambda ()
4653 (if (anything-current-buffer-is-modified)
4654 '("modified")
4655 '("unmodified")))))))
4656 (sources2 '(((name . "BAR")
4657 (candidates
4658 . (lambda ()
4659 (if (anything-current-buffer-is-modified)
4660 '("modified")
4661 '("unmodified"))))))))
4662 (with-temp-buffer
4663 (clrhash anything-tick-hash)
4664 (insert "1")
4665 (anything-test-candidates sources1)
4666 (anything-test-candidates sources2)
4667 (with-temp-buffer
4668 (anything-test-candidates sources2)))))
4669 (desc "anything-source-name")
4670 (expect "FOO"
4671 (let (v)
4672 (anything-test-candidates '(((name . "FOO")
4673 (init
4674 . (lambda () (setq v anything-source-name)))
4675 (candidates "ok"))))
4677 (expect "FOO"
4678 (let (v)
4679 (anything-test-candidates '(((name . "FOO")
4680 (candidates
4681 . (lambda ()
4682 (setq v anything-source-name)
4683 '("ok"))))))
4685 (expect "FOO"
4686 (let (v)
4687 (anything-test-candidates '(((name . "FOO")
4688 (candidates "ok")
4689 (candidate-transformer
4690 . (lambda (c)
4691 (setq v anything-source-name)
4692 c)))))
4694 (expect "FOO"
4695 (let (v)
4696 (anything-test-candidates '(((name . "FOO")
4697 (candidates "ok")
4698 (filtered-candidate-transformer
4699 . (lambda (c s)
4700 (setq v anything-source-name)
4701 c)))))
4703 (expect "FOO"
4704 (let (v)
4705 (anything-test-candidates '(((name . "FOO")
4706 (candidates "ok")
4707 (display-to-real
4708 . (lambda (c)
4709 (setq v anything-source-name)
4711 (action . identity))))
4712 (anything-execute-selection-action)
4714 (desc "anything-candidate-buffer create")
4715 (expect " *anything candidates:FOO*"
4716 (let* (anything-candidate-buffer-alist
4717 (anything-source-name "FOO")
4718 (buf (anything-candidate-buffer 'global)))
4719 (prog1 (buffer-name buf)
4720 (kill-buffer buf))))
4721 (expect " *anything candidates:FOO*aTestBuffer"
4722 (let* (anything-candidate-buffer-alist
4723 (anything-source-name "FOO")
4724 (anything-current-buffer (get-buffer-create "aTestBuffer"))
4725 (buf (anything-candidate-buffer 'local)))
4726 (prog1 (buffer-name buf)
4727 (kill-buffer anything-current-buffer)
4728 (kill-buffer buf))))
4729 (expect 0
4730 (let (anything-candidate-buffer-alist
4731 (anything-source-name "FOO") buf)
4732 (with-current-buffer (anything-candidate-buffer 'global)
4733 (insert "1"))
4734 (setq buf (anything-candidate-buffer 'global))
4735 (prog1 (buffer-size buf)
4736 (kill-buffer buf))))
4737 (desc "anything-candidate-buffer get-buffer")
4738 (expect " *anything candidates:FOO*"
4739 (let* (anything-candidate-buffer-alist
4740 (anything-source-name "FOO")
4741 (buf (anything-candidate-buffer 'global)))
4742 (prog1 (buffer-name (anything-candidate-buffer))
4743 (kill-buffer buf))))
4744 (expect " *anything candidates:FOO*aTestBuffer"
4745 (let* (anything-candidate-buffer-alist
4746 (anything-source-name "FOO")
4747 (anything-current-buffer (get-buffer-create "aTestBuffer"))
4748 (buf (anything-candidate-buffer 'local)))
4749 (prog1 (buffer-name (anything-candidate-buffer))
4750 (kill-buffer anything-current-buffer)
4751 (kill-buffer buf))))
4752 (expect " *anything candidates:FOO*"
4753 (let* (anything-candidate-buffer-alist
4754 (anything-source-name "FOO")
4755 (buf-local (anything-candidate-buffer 'local))
4756 (buf-global (anything-candidate-buffer 'global)))
4757 (prog1 (buffer-name (anything-candidate-buffer))
4758 (kill-buffer buf-local)
4759 (kill-buffer buf-global))))
4760 (expect " *anything candidates:FOO*aTestBuffer"
4761 (let* (anything-candidate-buffer-alist
4762 (anything-source-name "FOO")
4763 (anything-current-buffer (get-buffer-create "aTestBuffer"))
4764 (buf-global (anything-candidate-buffer 'global))
4765 (buf-local (anything-candidate-buffer 'local)))
4766 (prog1 (buffer-name (anything-candidate-buffer))
4767 (kill-buffer buf-local)
4768 (kill-buffer buf-global))))
4769 (expect nil
4770 (let* (anything-candidate-buffer-alist
4771 (anything-source-name "NOP__"))
4772 (anything-candidate-buffer)))
4773 (desc "anything-candidate-buffer register-buffer")
4774 (expect " *anything test candidates*"
4775 (let (anything-candidate-buffer-alist
4776 (buf (get-buffer-create " *anything test candidates*")))
4777 (with-current-buffer buf
4778 (insert "1\n2\n")
4779 (prog1 (buffer-name (anything-candidate-buffer buf))
4780 (kill-buffer (current-buffer))))))
4781 (expect " *anything test candidates*"
4782 (let (anything-candidate-buffer-alist
4783 (buf (get-buffer-create " *anything test candidates*")))
4784 (with-current-buffer buf
4785 (insert "1\n2\n")
4786 (anything-candidate-buffer buf)
4787 (prog1 (buffer-name (anything-candidate-buffer))
4788 (kill-buffer (current-buffer))))))
4789 (expect "1\n2\n"
4790 (let (anything-candidate-buffer-alist
4791 (buf (get-buffer-create " *anything test candidates*")))
4792 (with-current-buffer buf
4793 (insert "1\n2\n")
4794 (anything-candidate-buffer buf)
4795 (prog1 (buffer-string)
4796 (kill-buffer (current-buffer))))))
4797 (expect "buf1"
4798 (let (anything-candidate-buffer-alist
4799 (anything-source-name "foo")
4800 (buf1 (get-buffer-create "buf1"))
4801 (buf2 (get-buffer-create "buf2")))
4802 (anything-candidate-buffer buf1)
4803 (anything-candidate-buffer buf2)
4804 (prog1 (buffer-name (anything-candidate-buffer buf1))
4805 (kill-buffer buf1)
4806 (kill-buffer buf2))))
4807 (desc "action attribute")
4808 (expect "foo"
4809 (anything-test-candidates
4810 '(((name . "TEST")
4811 (candidates "foo")
4812 (action ("identity" . identity)))))
4813 (anything-execute-selection-action))
4814 (expect "foo"
4815 (anything-test-candidates
4816 '(((name . "TEST")
4817 (candidates "foo")
4818 (action ("identity" . (lambda (c) (identity c)))))))
4819 (anything-execute-selection-action))
4820 (desc "anything-get-default-action")
4821 (expect 'upcase
4822 (anything-get-default-action '(("upcase" . upcase))))
4823 (expect 'downcase
4824 (anything-get-default-action '(("downcase" . downcase))))
4825 (expect (lambda (x) (capitalize x))
4826 (anything-get-default-action (lambda (x) (capitalize x))))
4827 (expect 'identity
4828 (anything-get-default-action 'identity))
4829 (desc "anything-execute-selection-action")
4830 (expect "FOO"
4831 (anything-execute-selection-action
4832 "foo" '(("upcase" . upcase)) nil))
4833 (expect "FOO"
4834 (anything-execute-selection-action
4835 "foo" '(("upcase" . (lambda (c) (upcase c)))) nil))
4837 (desc "display-to-real attribute")
4838 (expect "FOO"
4839 (anything-test-candidates
4840 '(((name . "TEST")
4841 (candidates "foo")
4842 (display-to-real . upcase)
4843 (action ("identity" . identity)))))
4844 (anything-execute-selection-action))
4845 (desc "cleanup test")
4846 (expect 'cleaned
4847 (let (v)
4848 (anything-test-candidates
4849 '(((name . "TEST")
4850 (cleanup . (lambda () (setq v 'cleaned))))))
4852 (desc "anything-get-current-source")
4853 ;; in init/candidates/action/candidate-transformer/filtered-candidate-transformer
4854 ;; display-to-real/cleanup function
4855 (expect "FOO"
4856 (assoc-default
4857 'name
4858 (anything-funcall-with-source '((name . "FOO")) 'anything-get-current-source)))
4859 ;; init
4860 (expect "FOO"
4861 (let (v)
4862 (anything-test-candidates
4863 '(((name . "FOO")
4864 (init . (lambda () (setq v (anything-get-current-source)))))))
4865 (assoc-default 'name v)))
4866 ;; candidates
4867 (expect "FOO"
4868 (let (v)
4869 (anything-test-candidates
4870 '(((name . "FOO")
4871 (candidates . (lambda () (setq v (anything-get-current-source)) '("a"))))))
4872 (assoc-default 'name v)))
4873 ;; action
4874 (expect "FOO"
4875 (let (v)
4876 (anything-test-candidates
4877 '(((name . "FOO")
4878 (candidates "a")
4879 (action
4880 . (lambda (c) (setq v (anything-get-current-source)) c)))))
4881 (anything-execute-selection-action)
4882 (assoc-default 'name v)))
4883 ;; candidate-transformer
4884 (expect "FOO"
4885 (let (v)
4886 (anything-test-candidates
4887 '(((name . "FOO")
4888 (candidates "a")
4889 (candidate-transformer
4890 . (lambda (c) (setq v (anything-get-current-source)) c)))))
4891 (assoc-default 'name v)))
4892 ;; filtered-candidate-transformer
4893 (expect "FOO"
4894 (let (v)
4895 (anything-test-candidates
4896 '(((name . "FOO")
4897 (candidates "a")
4898 (filtered-candidate-transformer
4899 . (lambda (c s) (setq v (anything-get-current-source)) c)))))
4900 (assoc-default 'name v)))
4901 ;; action-transformer
4902 (expect "FOO"
4903 (let (v)
4904 (anything-test-candidates
4905 '(((name . "FOO")
4906 (candidates "a")
4907 (action-transformer
4908 . (lambda (a c) (setq v (anything-get-current-source)) a))
4909 (action . identity))))
4910 (anything-execute-selection-action)
4911 (assoc-default 'name v)))
4912 ;; display-to-real
4913 (expect "FOO"
4914 (let (v)
4915 (anything-test-candidates
4916 '(((name . "FOO")
4917 (init . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4918 (insert "a\n"))))
4919 (candidates-in-buffer)
4920 (display-to-real
4921 . (lambda (c) (setq v (anything-get-current-source)) c))
4922 (action . identity))))
4923 (anything-execute-selection-action)
4924 (assoc-default 'name v)))
4925 ;; cleanup
4926 (expect "FOO"
4927 (let (v)
4928 (anything-test-candidates
4929 '(((name . "FOO")
4930 (candidates "a")
4931 (cleanup
4932 . (lambda () (setq v (anything-get-current-source)))))))
4933 (assoc-default 'name v)))
4934 ;; candidates are displayed
4935 (expect "TEST"
4936 (anything-test-candidates
4937 '(((name . "TEST")
4938 (candidates "foo")
4939 (action ("identity" . identity)))))
4940 (assoc-default 'name (anything-get-current-source)))
4941 (desc "anything-attr")
4942 (expect "FOO"
4943 (anything-funcall-with-source
4944 '((name . "FOO"))
4945 (lambda ()
4946 (anything-attr 'name))))
4947 (expect 'fuga
4948 (let (v)
4949 (anything-test-candidates
4950 '(((name . "FOO")
4951 (hoge . fuga)
4952 (init . (lambda () (setq v (anything-attr 'hoge))))
4953 (candidates "a"))))
4955 (expect nil
4956 (let (v)
4957 (anything-test-candidates
4958 '(((name . "FOO")
4959 (init . (lambda () (setq v (anything-attr 'hoge))))
4960 (candidates "a"))))
4962 (expect nil
4963 (let (v)
4964 (anything-test-candidates
4965 '(((name . "FOO")
4966 (hoge) ;INCOMPATIBLE!
4967 (init . (lambda () (setq v (anything-attr 'hoge))))
4968 (candidates "a"))))
4970 (desc "anything-attr*")
4971 (expect "generic"
4972 (let (v (value1 "generic"))
4973 (anything-test-candidates
4974 '(((name . "FOO")
4975 (hoge . value1)
4976 (init . (lambda () (setq v (anything-attr* 'hoge)))))))
4978 (desc "anything-attr-defined")
4979 (expect (non-nil)
4980 (let (v)
4981 (anything-test-candidates
4982 '(((name . "FOO")
4983 (hoge)
4984 (init . (lambda () (setq v (anything-attr-defined 'hoge))))
4985 (candidates "a"))))
4986 v))
4987 (expect nil
4988 (let (v)
4989 (anything-test-candidates
4990 '(((name . "FOO")
4991 (init . (lambda () (setq v (anything-attr-defined 'hoge))))
4992 (candidates "a"))))
4993 v))
4994 (desc "anything-attrset")
4995 (expect '((name . "FOO") (hoge . 77))
4996 (let ((src '((name . "FOO") (hoge))))
4997 (anything-attrset 'hoge 77 src)
4998 src))
4999 (expect 77
5000 (anything-attrset 'hoge 77 '((name . "FOO") (hoge))))
5002 (expect '((name . "FOO") (hoge . 77))
5003 (let ((src '((name . "FOO") (hoge . 1))))
5004 (anything-attrset 'hoge 77 src)
5005 src))
5007 (expect '((name . "FOO") (hoge . 77) (x))
5008 (let ((src '((name . "FOO") (x))))
5009 (anything-attrset 'hoge 77 src)
5010 src))
5011 (expect 77
5012 (anything-attrset 'hoge 77 '((name . "FOO"))))
5013 (desc "anything-preselect")
5014 ;; entire candidate
5015 (expect "foo"
5016 (with-current-buffer (anything-create-anything-buffer t)
5017 (let ((anything-pattern "")
5018 (anything-test-mode t))
5019 (anything-process-source '((name . "test")
5020 (candidates "hoge" "foo" "bar")))
5021 (anything-preselect "foo")
5022 (anything-get-selection))))
5023 ;; regexp
5024 (expect "foo"
5025 (with-current-buffer (anything-create-anything-buffer t)
5026 (let ((anything-pattern "")
5027 (anything-test-mode t))
5028 (anything-process-source '((name . "test")
5029 (candidates "hoge" "foo" "bar")))
5030 (anything-preselect "fo+")
5031 (anything-get-selection))))
5032 ;; no match -> first entry
5033 (expect "hoge"
5034 (with-current-buffer (anything-create-anything-buffer t)
5035 (let ((anything-pattern "")
5036 (anything-test-mode t))
5037 (anything-process-source '((name . "test")
5038 (candidates "hoge" "foo" "bar")))
5039 (anything-preselect "not found")
5040 (anything-get-selection))))
5041 (desc "anything-check-new-input")
5042 (expect "newpattern"
5043 (stub anything-update)
5044 (stub anything-action-window)
5045 (let ((anything-pattern "pattern"))
5046 (anything-check-new-input "newpattern")
5047 anything-pattern))
5048 ;; anything-input == nil when action window is available
5049 (expect nil
5050 (stub anything-update)
5051 (stub anything-action-window => t)
5052 (let ((anything-pattern "pattern")
5053 anything-input)
5054 (anything-check-new-input "newpattern")
5055 anything-input))
5056 ;; anything-input == anything-pattern unless action window is available
5057 (expect "newpattern"
5058 (stub anything-update)
5059 (stub anything-action-window => nil)
5060 (let ((anything-pattern "pattern")
5061 anything-input)
5062 (anything-check-new-input "newpattern")
5063 anything-input))
5064 (expect (mock (anything-update))
5065 (stub anything-action-window)
5066 (let (anything-pattern)
5067 (anything-check-new-input "foo")))
5068 (desc "anything-update")
5069 (expect (mock (anything-process-source '((name . "1"))))
5070 (anything-test-update '(((name . "1"))) ""))
5071 ;; (find-function 'anything-update)
5072 ;; TODO el-mock.el should express 2nd call of function.
5073 ;; (expect (mock (anything-process-source '((name . "2"))))
5074 ;; (stub anything-get-sources => '(((name . "1")) ((name . "2"))))
5075 ;; (stub anything-log-run-hook)
5076 ;; (stub anything-maybe-fit-frame)
5077 ;; (stub run-with-idle-timer)
5078 ;; (anything-update))
5079 (expect (mock (run-with-idle-timer * nil 'anything-process-delayed-sources
5080 '(((name . "2") (delayed)))))
5081 (stub anything-get-sources => '(((name . "1"))
5082 ((name . "2") (delayed))))
5083 (stub anything-log-run-hook)
5084 (stub anything-maybe-fit-frame)
5085 (let ((anything-pattern "") anything-test-mode)
5086 (anything-update)))
5088 (desc "requires-pattern attribute")
5089 (expect (not-called anything-process-source)
5090 (anything-test-update '(((name . "1") (requires-pattern))) ""))
5091 (expect (not-called anything-process-source)
5092 (anything-test-update '(((name . "1") (requires-pattern . 3))) "xx"))
5094 (desc "delay")
5095 (expect 0.25
5096 (let ((anything-idle-delay 1.0)
5097 (anything-input-idle-delay 0.75))
5098 (anything-get-delay-time-for-delayed-sources)))
5099 (expect 0.0
5100 (let ((anything-idle-delay 0.2)
5101 (anything-input-idle-delay 0.5))
5102 (anything-get-delay-time-for-delayed-sources)))
5103 (expect 0.5
5104 (let ((anything-idle-delay 0.5)
5105 (anything-input-idle-delay nil))
5106 (anything-get-delay-time-for-delayed-sources)))
5107 (desc "anything-normalize-sources")
5108 (expect '(anything-c-source-test)
5109 (anything-normalize-sources 'anything-c-source-test))
5110 (expect '(anything-c-source-test)
5111 (anything-normalize-sources '(anything-c-source-test)))
5112 (expect '(anything-c-source-test)
5113 (let ((anything-sources '(anything-c-source-test)))
5114 (anything-normalize-sources nil)))
5115 (expect '(((name . "test")))
5116 (anything-normalize-sources '((name . "test"))))
5117 (expect '(((name . "test")))
5118 (anything-normalize-sources '(((name . "test")))))
5119 (desc "anything-get-action")
5120 (expect '(("identity" . identity))
5121 (stub buffer-size => 1)
5122 (stub anything-get-current-source => '((name . "test")
5123 (action ("identity" . identity))))
5124 (anything-get-action))
5125 (expect '((("identity" . identity)) "action-transformer is called")
5126 (stub buffer-size => 1)
5127 (stub anything-get-current-source
5128 => '((name . "test")
5129 (action ("identity" . identity))
5130 (action-transformer
5131 . (lambda (actions selection)
5132 (list actions selection)))))
5133 (stub anything-get-selection => "action-transformer is called")
5134 (anything-get-action))
5135 (desc "anything-select-nth-action")
5136 (expect "selection"
5137 (stub anything-get-selection => "selection")
5138 (stub anything-exit-minibuffer)
5139 (let (anything-saved-selection)
5140 (anything-select-nth-action 1)
5141 anything-saved-selection))
5142 (expect 'cadr
5143 (stub anything-get-action => '(("0" . car) ("1" . cdr) ("2" . cadr)))
5144 (stub anything-exit-minibuffer)
5145 (stub anything-get-selection => "selection")
5146 (let (anything-saved-action)
5147 (anything-select-nth-action 2)
5148 anything-saved-action))
5149 (desc "anything-funcall-foreach")
5150 (expect (mock (upcase "foo"))
5151 (stub anything-get-sources => '(((init . (lambda () (upcase "foo"))))))
5152 (anything-funcall-foreach 'init))
5153 (expect (mock (downcase "bar"))
5154 (stub anything-get-sources => '(((init . (lambda () (upcase "foo"))))
5155 ((init . (lambda () (downcase "bar"))))))
5156 (anything-funcall-foreach 'init))
5157 (expect (not-called anything-funcall-with-source)
5158 (stub anything-get-sources => '(((init . (lambda () (upcase "foo"))))))
5159 (anything-funcall-foreach 'not-found))
5160 ;; TODO anything-select-with-digit-shortcut test
5161 (desc "anything-get-cached-candidates")
5162 (expect '("cached" "version")
5163 (let ((anything-candidate-cache '(("test" "cached" "version"))))
5164 (anything-get-cached-candidates '((name . "test")
5165 (candidates "new")))))
5166 (expect '("new")
5167 (let ((anything-candidate-cache '(("other" "cached" "version"))))
5168 (anything-get-cached-candidates '((name . "test")
5169 (candidates "new")))))
5170 (expect '(("test" "new")
5171 ("other" "cached" "version"))
5172 (let ((anything-candidate-cache '(("other" "cached" "version"))))
5173 (anything-get-cached-candidates '((name . "test")
5174 (candidates "new")))
5175 anything-candidate-cache))
5176 (expect '(("other" "cached" "version"))
5177 (let ((anything-candidate-cache '(("other" "cached" "version"))))
5178 (anything-get-cached-candidates '((name . "test")
5179 (candidates "new")
5180 (volatile)))
5181 anything-candidate-cache))
5182 ;; TODO when candidates == process
5183 ;; TODO anything-output-filter
5184 (desc "candidate-number-limit attribute")
5185 (expect '("a" "b")
5186 (let ((anything-pattern "")
5187 (anything-candidate-number-limit 20))
5188 (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c")
5189 (candidate-number-limit . 2) (volatile)))))
5190 (expect '("a" "b")
5191 (let ((anything-pattern "[abc]")
5192 (anything-candidate-number-limit 20))
5193 (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c")
5194 (candidate-number-limit . 2) (volatile)))))
5195 (expect '("a" "b" "c" "d")
5196 (let ((anything-pattern "[abcd]")
5197 (anything-candidate-number-limit 2))
5198 (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c" "d")
5199 (candidate-number-limit) (volatile)))))
5200 (expect '(("TEST" ("a" "b" "c")))
5201 (let ((anything-candidate-number-limit 2))
5202 (anything-test-candidates
5203 '(((name . "TEST")
5204 (init
5205 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5206 (insert "a\nb\nc\nd\n"))))
5207 (candidates . anything-candidates-in-buffer)
5208 (match identity)
5209 (candidate-number-limit . 3)
5210 (volatile))))))
5211 (expect '(("TEST" ("a" "b" "c")))
5212 (let ((anything-candidate-number-limit 2))
5213 (anything-test-candidates
5214 '(((name . "TEST")
5215 (init
5216 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5217 (insert "a\nb\nc\nd\n"))))
5218 (candidates . anything-candidates-in-buffer)
5219 (match identity)
5220 (candidate-number-limit . 3)
5221 (volatile)))
5222 ".")))
5223 (desc "multiple init")
5224 (expect '(1 . 2)
5225 (let (a b)
5226 (anything-test-candidates
5227 '(((name . "test")
5228 (init (lambda () (setq a 1))
5229 (lambda () (setq b 2))))))
5230 (cons a b)))
5231 (expect 1
5232 (let (a)
5233 (anything-test-candidates
5234 '(((name . "test")
5235 (init (lambda () (setq a 1))))))
5237 (desc "multiple cleanup")
5238 (expect '(1 . 2)
5239 (let (a b)
5240 (anything-test-candidates
5241 '(((name . "test")
5242 (cleanup (lambda () (setq a 1))
5243 (lambda () (setq b 2))))))
5244 (cons a b)))
5245 (desc "anything-mklist")
5246 (expect '(1)
5247 (anything-mklist 1))
5248 (expect '(2)
5249 (anything-mklist '(2)))
5250 (expect '((lambda ()))
5251 (anything-mklist (lambda ())))
5252 (desc "anything-before-initialize-hook")
5253 (expect 'called
5254 (let ((anything-before-initialize-hook '((lambda () (setq v 'called))))
5256 (anything-initialize)
5258 (desc "anything-after-initialize-hook")
5259 (expect '(b a)
5260 (let ((anything-before-initialize-hook
5261 '((lambda () (setq v '(a)))))
5262 (anything-after-initialize-hook
5263 '((lambda () (setq v (cons 'b v)))))
5265 (anything-initialize)
5267 (expect 0
5268 (let ((anything-after-initialize-hook
5269 '((lambda () (setq v (buffer-size (get-buffer anything-buffer))))))
5271 (anything-initialize)
5273 (desc "get-line attribute")
5274 (expect '(("TEST" ("FOO+")))
5275 (anything-test-candidates
5276 '(((name . "TEST")
5277 (init
5278 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5279 (insert "foo+\nbar+\nbaz+\n"))))
5280 (candidates-in-buffer)
5281 (get-line . (lambda (s e) (upcase (buffer-substring-no-properties s e))))))
5282 "oo\\+"))
5283 (desc "with-anything-restore-variables")
5284 (expect '(7 8)
5285 (let ((a 7) (b 8)
5286 (anything-restored-variables '(a b)))
5287 (with-anything-restore-variables
5288 (setq a 0 b 0))
5289 (list a b)))
5290 (desc "anything-cleanup-hook")
5291 (expect 'called
5292 (let ((anything-cleanup-hook
5293 '((lambda () (setq v 'called))))
5295 (anything-cleanup)
5297 (desc "with-anything-display-same-window")
5298 (expect (non-nil)
5299 (save-window-excursion
5300 (delete-other-windows)
5301 (split-window)
5303 (let ((buf (get-buffer-create " tmp"))
5304 (win (selected-window)))
5305 (with-anything-display-same-window
5306 (display-buffer buf)
5307 (eq win (get-buffer-window buf))))))
5308 (expect (non-nil)
5309 (save-window-excursion
5310 (delete-other-windows)
5311 (split-window)
5313 (let ((buf (get-buffer-create " tmp"))
5314 (win (selected-window)))
5315 (with-anything-display-same-window
5316 (pop-to-buffer buf)
5317 (eq win (get-buffer-window buf))))))
5318 (expect (non-nil)
5319 (save-window-excursion
5320 (delete-other-windows)
5321 (split-window)
5323 (let ((buf (get-buffer-create " tmp"))
5324 (win (selected-window)))
5325 (with-anything-display-same-window
5326 (switch-to-buffer buf)
5327 (eq win (get-buffer-window buf))))))
5328 (expect (non-nil)
5329 (save-window-excursion
5330 (delete-other-windows)
5331 (let ((buf (get-buffer-create " tmp"))
5332 (win (selected-window)))
5333 (with-anything-display-same-window
5334 (display-buffer buf)
5335 (eq win (get-buffer-window buf))))))
5336 (expect (non-nil)
5337 (save-window-excursion
5338 (delete-other-windows)
5339 (let ((buf (get-buffer-create " tmp"))
5340 (win (selected-window)))
5341 (with-anything-display-same-window
5342 (pop-to-buffer buf)
5343 (eq win (get-buffer-window buf))))))
5344 (desc "search-from-end attribute")
5345 (expect '(("TEST" ("baz+" "bar+" "foo+")))
5346 (anything-test-candidates
5347 '(((name . "TEST")
5348 (init
5349 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5350 (insert "foo+\nbar+\nbaz+\n"))))
5351 (candidates-in-buffer)
5352 (search-from-end)))))
5353 (expect '(("TEST" ("baz+" "bar+" "foo+")))
5354 (anything-test-candidates
5355 '(((name . "TEST")
5356 (init
5357 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5358 (insert "foo+\nbar+\nbaz+\n"))))
5359 (candidates-in-buffer)
5360 (search-from-end)))
5361 "\\+"))
5362 (expect '(("TEST" ("baz+" "bar+")))
5363 (anything-test-candidates
5364 '(((name . "TEST")
5365 (init
5366 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5367 (insert "foo+\nbar+\nbaz+\n"))))
5368 (candidates-in-buffer)
5369 (search-from-end)
5370 (candidate-number-limit . 2)))))
5371 (expect '(("TEST" ("baz+" "bar+")))
5372 (anything-test-candidates
5373 '(((name . "TEST")
5374 (init
5375 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5376 (insert "foo+\nbar+\nbaz+\n"))))
5377 (candidates-in-buffer)
5378 (search-from-end)
5379 (candidate-number-limit . 2)))
5380 "\\+"))
5382 (desc "header-name attribute")
5383 (expect "original is transformed"
5384 (anything-test-update '(((name . "original")
5385 (candidates "1")
5386 (header-name
5387 . (lambda (name)
5388 (format "%s is transformed" name)))))
5390 (with-current-buffer (anything-buffer-get)
5391 (buffer-string)
5392 (overlay-get (car (overlays-at (1+(point-min)))) 'display)))
5393 (desc "volatile and match attribute")
5394 ;; candidates function is called once per `anything-process-delayed-sources'
5395 (expect 1
5396 (let ((v 0))
5397 (anything-test-candidates '(((name . "test")
5398 (candidates . (lambda () (incf v) '("ok")))
5399 (volatile)
5400 (match identity identity identity)))
5401 "o")
5403 (desc "accept-empty attribute")
5404 (expect nil
5405 (anything-test-candidates
5406 '(((name . "test") (candidates "") (action . identity))))
5407 (anything-execute-selection-action))
5408 (expect ""
5409 (anything-test-candidates
5410 '(((name . "test") (candidates "") (action . identity) (accept-empty))))
5411 (anything-execute-selection-action))
5412 (desc "anything-tick-hash")
5413 (expect nil
5414 (with-current-buffer (get-buffer-create " *00create+*")
5415 (puthash " *00create+*/xxx" 1 anything-tick-hash)
5416 (kill-buffer (current-buffer)))
5417 (gethash " *00create+*/xxx" anything-tick-hash))
5418 (desc "anything-execute-action-at-once-if-once")
5419 (expect "HOGE"
5420 (let ((anything-execute-action-at-once-if-one t))
5421 (anything '(((name . "one test1")
5422 (candidates "hoge")
5423 (action . upcase))))))
5424 (expect "ANY"
5425 (let ((anything-execute-action-at-once-if-one t))
5426 (anything '(((name . "one test2")
5427 (candidates "hoge" "any")
5428 (action . upcase)))
5429 "an")))
5430 ;; candidates > 1
5431 (expect (mock (read-string "word: " nil))
5432 (let ((anything-execute-action-at-once-if-one t))
5433 (anything '(((name . "one test3")
5434 (candidates "hoge" "foo" "bar")
5435 (action . identity)))
5436 nil "word: ")))
5437 (desc "anything-quit-if-no-candidate")
5438 (expect nil
5439 (let ((anything-quit-if-no-candidate t))
5440 (anything '(((name . "zero test1") (candidates) (action . upcase))))))
5441 (expect 'called
5442 (let (v (anything-quit-if-no-candidate (lambda () (setq v 'called))))
5443 (anything '(((name . "zero test2") (candidates) (action . upcase))))
5445 (desc "real-to-display attribute")
5446 (expect '(("test" (("DDD" . "ddd"))))
5447 (anything-test-candidates '(((name . "test")
5448 (candidates "ddd")
5449 (real-to-display . upcase)
5450 (action . identity)))))
5451 (expect '(("test" (("DDD" . "ddd"))))
5452 (anything-test-candidates '(((name . "test")
5453 (candidates ("ignored" . "ddd"))
5454 (real-to-display . upcase)
5455 (action . identity)))))
5456 (expect '(("Commands" (("xxxhoge" . "hoge") ("xxxboke" . "boke"))))
5457 (anything-test-candidates '(((name . "Commands")
5458 (candidates
5459 "hoge" "boke")
5460 (real-to-display . (lambda (x) (concat "xxx" x)))
5461 (action . identity)))
5462 "xxx"))
5463 (expect "test\nDDD\n"
5464 (anything-test-update '(((name . "test")
5465 (candidates "ddd")
5466 (real-to-display . upcase)
5467 (action . identity)))
5469 (with-current-buffer (anything-buffer-get) (buffer-string)))
5470 (desc "real-to-display and candidate-transformer attribute")
5471 (expect '(("test" (("DDD" . "ddd"))))
5472 (anything-test-candidates
5473 '(((name . "test")
5474 (candidates "ddd")
5475 (candidate-transformer (lambda (cands) (mapcar (lambda (c) (cons "X" c)) cands)))
5476 (real-to-display . upcase)
5477 (action . identity)))))
5478 (expect "test\nDDD\n"
5479 (anything-test-update
5480 '(((name . "test")
5481 (candidates "ddd")
5482 (candidate-transformer (lambda (cands) (mapcar (lambda (c) (cons "X" c)) cands)))
5483 (real-to-display . upcase)
5484 (action . identity)))
5486 (with-current-buffer (anything-buffer-get) (buffer-string)))
5487 (desc "real-to-display and candidates-in-buffer")
5488 (expect '(("test" (("A" . "a") ("B" . "b"))))
5489 (anything-test-candidates
5490 '(((name . "test")
5491 (init
5492 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5493 (erase-buffer)
5494 (insert "a\nb\n"))))
5495 (candidates-in-buffer)
5496 (real-to-display . upcase)
5497 (action . identity)))))
5498 (expect "test\nA\nB\n"
5499 (stub read-string)
5500 (anything
5501 '(((name . "test")
5502 (init
5503 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5504 (erase-buffer)
5505 (insert "a\nb\n"))))
5506 (candidates-in-buffer)
5507 (real-to-display . upcase)
5508 (action . identity))))
5509 (with-current-buffer (anything-buffer-get) (buffer-string)))
5510 (desc "Symbols are acceptable as candidate.")
5511 (expect '(("test" (sym "str")))
5512 (anything-test-candidates
5513 '(((name . "test")
5514 (candidates sym "str")))))
5515 (expect '(("test" ((sym . realsym) ("str" . "realstr"))))
5516 (anything-test-candidates
5517 '(((name . "test")
5518 (candidates (sym . realsym) ("str" . "realstr"))))))
5519 (expect '(("test" (sym)))
5520 (anything-test-candidates
5521 '(((name . "test")
5522 (candidates sym "str")))
5523 "sym"))
5524 (expect '(("test" ("str")))
5525 (anything-test-candidates
5526 '(((name . "test")
5527 (candidates sym "str")))
5528 "str"))
5529 (expect '(("test" ((sym . realsym))))
5530 (anything-test-candidates
5531 '(((name . "test")
5532 (candidates (sym . realsym) ("str" . "realstr"))))
5533 "sym"))
5534 (expect '(("test" (("str" . "realstr"))))
5535 (anything-test-candidates
5536 '(((name . "test")
5537 (candidates (sym . realsym) ("str" . "realstr"))))
5538 "str"))
5539 (desc "multiple transformers")
5540 (expect '(("test" ("<FOO>")))
5541 (anything-test-candidates
5542 '(((name . "test")
5543 (candidates "foo")
5544 (candidate-transformer
5545 . (lambda (cands)
5546 (anything-compose (list cands)
5547 (list (lambda (c) (mapcar 'upcase c))
5548 (lambda (c) (list (concat "<" (car c) ">")))))))))))
5549 (expect '("<FOO>")
5550 (anything-composed-funcall-with-source
5551 '((name . "test"))
5552 (list (lambda (c) (mapcar 'upcase c))
5553 (lambda (c) (list (concat "<" (car c) ">"))))
5554 '("foo"))
5556 (expect '(("test" ("<FOO>")))
5557 (anything-test-candidates
5558 '(((name . "test")
5559 (candidates "foo")
5560 (candidate-transformer
5561 (lambda (c) (mapcar 'upcase c))
5562 (lambda (c) (list (concat "<" (car c) ">"))))))))
5563 (expect '(("test" ("<BAR>")))
5564 (anything-test-candidates
5565 '(((name . "test")
5566 (candidates "bar")
5567 (filtered-candidate-transformer
5568 (lambda (c s) (mapcar 'upcase c))
5569 (lambda (c s) (list (concat "<" (car c) ">"))))))))
5570 (expect '(("find-file" . find-file)
5571 ("view-file" . view-file))
5572 (stub zerop => nil)
5573 (stub anything-get-current-source
5574 => '((name . "test")
5575 (action)
5576 (action-transformer
5577 . (lambda (a s)
5578 (anything-compose
5579 (list a s)
5580 (list (lambda (a s) (push '("view-file" . view-file) a))
5581 (lambda (a s) (push '("find-file" . find-file) a))))))))
5582 (anything-get-action))
5583 (expect '(("find-file" . find-file)
5584 ("view-file" . view-file))
5585 (stub zerop => nil)
5586 (stub anything-get-current-source
5587 => '((name . "test")
5588 (action)
5589 (action-transformer
5590 (lambda (a s) (push '("view-file" . view-file) a))
5591 (lambda (a s) (push '("find-file" . find-file) a)))))
5592 (anything-get-action))
5593 (desc "define-anything-type-attribute")
5594 (expect '((file (action . find-file)))
5595 (let (anything-type-attributes)
5596 (define-anything-type-attribute 'file '((action . find-file)))
5597 anything-type-attributes))
5598 (expect '((file (action . find-file)))
5599 (let ((anything-type-attributes '((file (action . view-file)))))
5600 (define-anything-type-attribute 'file '((action . find-file)))
5601 anything-type-attributes))
5602 (expect '((file (action . find-file))
5603 (buffer (action . switch-to-buffer)))
5604 (let (anything-type-attributes)
5605 (define-anything-type-attribute 'buffer '((action . switch-to-buffer)))
5606 (define-anything-type-attribute 'file '((action . find-file)))
5607 anything-type-attributes))
5608 (desc "anything-approximate-candidate-number")
5609 (expect 0
5610 (with-temp-buffer
5611 (let ((anything-buffer (current-buffer)))
5612 (anything-approximate-candidate-number))))
5613 (expect 1
5614 (with-temp-buffer
5615 (let ((anything-buffer (current-buffer)))
5616 (insert "Title\n"
5617 "candiate1\n")
5618 (anything-approximate-candidate-number))))
5619 (expect t
5620 (with-temp-buffer
5621 (let ((anything-buffer (current-buffer)))
5622 (insert "Title\n"
5623 "candiate1\n"
5624 "candiate2\n")
5625 (<= 2 (anything-approximate-candidate-number)))))
5626 (expect 1
5627 (with-temp-buffer
5628 (let ((anything-buffer (current-buffer)))
5629 (insert "Title\n"
5630 (propertize "multi\nline\n" 'anything-multiline t))
5631 (anything-approximate-candidate-number))))
5632 (expect t
5633 (with-temp-buffer
5634 (let ((anything-buffer (current-buffer))
5635 (anything-candidate-separator "-----"))
5636 (insert "Title\n"
5637 (propertize "multi\nline1\n" 'anything-multiline t)
5638 "-----\n"
5639 (propertize "multi\nline2\n" 'anything-multiline t))
5640 (<= 2 (anything-approximate-candidate-number)))))
5641 (desc "delayed-init attribute")
5642 (expect 0
5643 (let ((value 0))
5644 (anything-test-candidates '(((name . "test")
5645 (delayed-init . (lambda () (incf value)))
5646 (candiates "abc")
5647 (requires-pattern . 2)))
5649 value))
5650 (expect 1
5651 (let ((value 0))
5652 (anything-test-candidates '(((name . "test")
5653 (delayed-init . (lambda () (incf value)))
5654 (candiates "abc")
5655 (requires-pattern . 2)))
5656 "abc")
5657 value))
5658 (expect 2
5659 (let ((value 0))
5660 (anything-test-candidates '(((name . "test")
5661 (delayed-init (lambda () (incf value))
5662 (lambda () (incf value)))
5663 (candiates "abc")
5664 (requires-pattern . 2)))
5665 "abc")
5666 value))
5667 (expect t
5668 (let (value)
5669 (with-temp-buffer
5670 (anything-test-candidates '(((name . "test")
5671 (delayed-init
5672 . (lambda () (setq value
5673 (eq anything-current-buffer (current-buffer)))))
5674 (candiates "abc")
5675 (requires-pattern . 2)))
5676 "abc")
5677 value)))
5678 (desc "pattern-transformer attribute")
5679 (expect '(("test2" ("foo")) ("test3" ("bar")))
5680 (anything-test-candidates '(((name . "test1")
5681 (candidates "foo" "bar"))
5682 ((name . "test2")
5683 (pattern-transformer . (lambda (pat) (substring pat 1)))
5684 (candidates "foo" "bar"))
5685 ((name . "test3")
5686 (pattern-transformer . (lambda (pat) "bar"))
5687 (candidates "foo" "bar")))
5688 "xfoo"))
5689 (expect '(("test2" ("foo")) ("test3" ("bar")))
5690 (anything-test-candidates '(((name . "test1")
5691 (candidates "foo" "bar"))
5692 ((name . "test2")
5693 (pattern-transformer (lambda (pat) (substring pat 1)))
5694 (candidates "foo" "bar"))
5695 ((name . "test3")
5696 (pattern-transformer (lambda (pat) "bar"))
5697 (candidates "foo" "bar")))
5698 "xfoo"))
5699 (expect '(("test2" ("foo")) ("test3" ("bar")))
5700 (anything-test-candidates '(((name . "test1")
5701 (init
5702 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5703 (insert "foo\nbar\n"))))
5704 (candidates-in-buffer))
5705 ((name . "test2")
5706 (pattern-transformer . (lambda (pat) (substring pat 1)))
5707 (init
5708 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5709 (insert "foo\nbar\n"))))
5710 (candidates-in-buffer))
5711 ((name . "test3")
5712 (pattern-transformer . (lambda (pat) "bar"))
5713 (init
5714 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5715 (insert "foo\nbar\n"))))
5716 (candidates-in-buffer)))
5717 "xfoo"))
5718 (desc "anything-recent-push")
5719 (expect '("foo" "bar" "baz")
5720 (let ((lst '("bar" "baz")))
5721 (anything-recent-push "foo" 'lst)))
5722 (expect '("foo" "bar" "baz")
5723 (let ((lst '("foo" "bar" "baz")))
5724 (anything-recent-push "foo" 'lst)))
5725 (expect '("foo" "bar" "baz")
5726 (let ((lst '("bar" "foo" "baz")))
5727 (anything-recent-push "foo" 'lst)))
5728 (desc "anything-require-at-least-version")
5729 (expect nil
5730 (anything-require-at-least-version "1.1"))
5731 (expect nil
5732 (anything-require-at-least-version "1.200"))
5733 (expect nil
5734 (anything-require-at-least-version
5735 (and (string-match "1\.\\([0-9]+\\)" anything-version)
5736 (match-string 0 anything-version))))
5737 (expect (error)
5738 (anything-require-at-least-version "1.999"))
5739 (expect (error)
5740 (anything-require-at-least-version "1.2000"))
5741 (desc "anything-once")
5742 (expect 2
5743 (let ((i 0))
5744 (anything-test-candidates
5745 '(((name . "1")
5746 (init . (lambda () (incf i))))
5747 ((name . "2")
5748 (init . (lambda () (incf i))))))
5750 (expect 1
5751 (let ((i 0))
5752 (anything-test-candidates
5753 '(((name . "1")
5754 (init . (lambda () (anything-once (lambda () (incf i))))))
5755 ((name . "2")
5756 (init . (lambda () (anything-once (lambda () (incf i))))))))
5758 (expect 1
5759 (let ((i 0))
5760 (flet ((init1 () (anything-once (lambda () (incf i)))))
5761 (anything-test-candidates
5762 '(((name . "1")
5763 (init . init1))
5764 ((name . "2")
5765 (init . init1)))))
5767 (desc "anything-marked-candidates")
5768 (expect '("mark3" "mark1")
5769 (let* ((source '((name . "mark test")))
5770 (anything-marked-candidates
5771 `((,source . "mark1")
5772 (((name . "other")) . "mark2")
5773 (,source . "mark3"))))
5774 (stub anything-buffer-get => (current-buffer))
5775 (stub anything-get-current-source => source)
5776 (anything-marked-candidates)))
5777 (expect '("current")
5778 (let* ((source '((name . "mark test")))
5779 (anything-marked-candidates nil))
5780 (stub anything-get-current-source => source)
5781 (stub anything-get-selection => "current")
5782 (anything-marked-candidates)))
5783 (desc "anything-let")
5784 (expect '(1 10000 nil)
5785 (let ((a 9999)
5786 (b 8)
5788 (anything-buffer (exps-tmpbuf)))
5789 (anything-let ((a 1)
5790 (b (1+ a))
5792 (anything-create-anything-buffer))
5793 (with-current-buffer anything-buffer
5794 (list a b c))))
5795 (expect (non-nil)
5796 (let ((a 9999)
5797 (b 8)
5799 (anything-buffer (exps-tmpbuf)))
5800 (anything-let ((a 1)
5801 (b (1+ a))
5803 (anything-create-anything-buffer))
5804 (with-current-buffer anything-buffer
5805 (and (assq 'a (buffer-local-variables))
5806 (assq 'b (buffer-local-variables))
5807 (assq 'c (buffer-local-variables))))))
5808 (expect 'retval
5809 (let ((a 9999)
5810 (b 8)
5812 (anything-buffer (exps-tmpbuf)))
5813 (anything-let ((a 1)
5814 (b (1+ a))
5816 'retval)))
5817 (desc "anything-let*")
5818 (expect '(1 2 nil)
5819 (let ((a 9999)
5820 (b 8)
5822 (anything-buffer (exps-tmpbuf)))
5823 (anything-let* ((a 1)
5824 (b (1+ a))
5826 (anything-create-anything-buffer))
5827 (with-current-buffer anything-buffer
5828 (list a b c))))
5829 (expect (non-nil)
5830 (let ((a 9999)
5831 (b 8)
5833 (anything-buffer (exps-tmpbuf)))
5834 (anything-let* ((a 1)
5835 (b (1+ a))
5837 (anything-create-anything-buffer))
5838 (with-current-buffer anything-buffer
5839 (and (assq 'a (buffer-local-variables))
5840 (assq 'b (buffer-local-variables))
5841 (assq 'c (buffer-local-variables))))))
5842 (expect 'retval*
5843 (let ((a 9999)
5844 (b 8)
5846 (anything-buffer (exps-tmpbuf)))
5847 (anything-let* ((a 1)
5848 (b (1+ a))
5850 'retval*)))
5851 (desc "anything with keyword")
5852 (expect (mock (anything-internal 'test-source "input" "prompt: " nil "preselect" "*test*" nil))
5853 (anything :sources 'test-source
5854 :input "input"
5855 :prompt "prompt: "
5856 :resume nil
5857 :preselect "preselect"
5858 :buffer "*test*"
5859 :keymap nil))
5860 (expect (mock (anything-internal 'test-source nil nil nil nil "*test*" nil))
5861 (anything :sources 'test-source
5862 :buffer "*test*"
5863 :candidate-number-limit 20))
5864 (expect (mock (anything-internal 'test-source nil nil nil nil "*test*" nil))
5865 (anything 'test-source nil nil nil nil "*test*" nil))
5866 (desc "anything-log-eval-internal")
5867 (expect (mock (anything-log "%S = %S" '(+ 1 2) 3))
5868 (anything-log-eval-internal '((+ 1 2))))
5869 (expect (mock (anything-log "%S = ERROR!" 'unDeFined))
5870 (anything-log-eval-internal '(unDeFined)))
5872 (desc "anything-output-filter--collect-candidates")
5873 (expect '("a" "b" "")
5874 (split-string "a\nb\n" "\n"))
5875 (expect '("a" "b")
5876 (anything-output-filter--collect-candidates
5877 '("a" "b" "") (cons 'incomplete-line "")))
5878 (expect '("a" "b")
5879 (split-string "a\nb" "\n"))
5880 (expect '("a")
5881 (anything-output-filter--collect-candidates
5882 '("a" "b") (cons 'incomplete-line "")))
5883 (expect '(incomplete-line . "b")
5884 (let ((incomplete-line-info (cons 'incomplete-line "")))
5885 (anything-output-filter--collect-candidates
5886 '("a" "b") incomplete-line-info)
5887 incomplete-line-info))
5888 (expect '("" "c" "")
5889 (split-string "\nc\n" "\n"))
5890 (expect '("b" "c")
5891 ;; "a\nb" + "\nc\n"
5892 (let ((incomplete-line-info (cons 'incomplete-line "")))
5893 (anything-output-filter--collect-candidates
5894 '("a" "b") incomplete-line-info)
5895 (anything-output-filter--collect-candidates
5896 '("" "c" "") incomplete-line-info)))
5897 (desc "anything-next-point-in-list")
5898 (expect 10
5899 (anything-next-point-in-list 5 '(10 20) nil))
5900 (expect 20
5901 (anything-next-point-in-list 15 '(10 20) nil))
5902 (expect 25
5903 (anything-next-point-in-list 25 '(10 20) nil))
5904 (expect 5
5905 (anything-next-point-in-list 5 '(10 20) t))
5906 (expect 10
5907 (anything-next-point-in-list 15 '(10 20) t))
5908 (expect 20
5909 (anything-next-point-in-list 25 '(10 20) t))
5910 (expect 5
5911 (anything-next-point-in-list 5 '() nil))
5912 (expect 5
5913 (anything-next-point-in-list 5 '() t))
5914 (expect 10
5915 (anything-next-point-in-list 5 '(10) nil))
5916 (expect 10
5917 (anything-next-point-in-list 15 '(10) t))
5918 (expect 20
5919 (anything-next-point-in-list 10 '(10 20) nil))
5920 (expect 10
5921 (anything-next-point-in-list 20 '(10 20) t))
5922 (expect 20
5923 (anything-next-point-in-list 30 '(10 20 30) t))
5927 (provide 'anything)
5928 ;; How to save (DO NOT REMOVE!!)
5929 ;; (progn (magit-push) (emacswiki-post "anything.el"))
5930 ;;; anything.el ends here