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