* anything-config.el (anything-ff-update-when-only-one-matched): Add / to fname when...
[anything-config.git] / anything.el
blob5b5f3647201cedd56266cf7c65d6783323468209
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-M-down>") 'anything-scroll-other-window)
654 (define-key map (kbd "<C-M-up>") 'anything-scroll-other-window-down)
655 (define-key map (kbd "C-SPC") 'anything-toggle-visible-mark)
656 (define-key map (kbd "M-[") 'anything-prev-visible-mark)
657 (define-key map (kbd "M-]") 'anything-next-visible-mark)
658 (define-key map (kbd "C-k") 'anything-delete-minibuffer-contents)
660 (define-key map (kbd "C-s") 'anything-isearch)
661 (define-key map (kbd "C-r") 'undefined)
662 (define-key map (kbd "C-t") 'anything-toggle-resplit-window)
663 (define-key map (kbd "C-x C-f") 'anything-quit-and-find-file)
665 (define-key map (kbd "C-c C-d") 'anything-delete-current-selection)
666 (define-key map (kbd "C-c C-y") 'anything-yank-selection)
667 (define-key map (kbd "C-c C-k") 'anything-kill-selection-and-quit)
668 (define-key map (kbd "C-c C-f") 'anything-follow-mode)
669 (define-key map (kbd "C-c C-u") 'anything-force-update)
671 ;; Debugging command
672 (define-key map "\C-c\C-x\C-d" 'anything-debug-output)
673 (define-key map "\C-c\C-x\C-m" 'anything-display-all-visible-marks)
674 (define-key map "\C-c\C-x\C-b" 'anything-send-bug-report-from-anything)
675 ;; Use `describe-mode' key in `global-map'
676 (define-key map [f1] nil) ; Allow to eval keymap whithout errors.
677 (dolist (k (where-is-internal 'describe-mode global-map))
678 (define-key map k 'anything-help))
679 ;; the defalias is needed because commands are bound by name when
680 ;; using iswitchb, so only commands having the prefix anything-
681 ;; get rebound
682 (defalias 'anything-previous-history-element 'previous-history-element)
683 (defalias 'anything-next-history-element 'next-history-element)
684 (define-key map (kbd "M-p") 'anything-previous-history-element)
685 (define-key map (kbd "M-n") 'anything-next-history-element)
686 map)
687 "Keymap for anything.
689 If you execute `anything-iswitchb-setup', some keys are modified.
690 See `anything-iswitchb-setup-keys'.")
692 (defvar anything-isearch-map
693 (let ((map (make-sparse-keymap)))
694 (set-keymap-parent map (current-global-map))
695 (define-key map (kbd "<return>") 'anything-isearch-default-action)
696 (define-key map (kbd "<RET>") 'anything-isearch-default-action)
697 (define-key map (kbd "C-i") 'anything-isearch-select-action)
698 (define-key map (kbd "C-g") 'anything-isearch-cancel)
699 (define-key map (kbd "M-s") 'anything-isearch-again)
700 (define-key map (kbd "<backspace>") 'anything-isearch-delete)
701 ;; add printing chars
702 (loop for i from 32 below 256 do
703 (define-key map (vector i) 'anything-isearch-printing-char))
704 map)
705 "Keymap for anything incremental search.")
708 (defgroup anything nil
709 "Open anything." :prefix "anything-" :group 'convenience)
711 (defface anything-header
712 '((t (:inherit header-line)))
713 "Face for header lines in the anything buffer." :group 'anything)
715 (defvar anything-header-face 'anything-header
716 "Face for header lines in the anything buffer.")
718 (defface anything-isearch-match '((t (:background "Yellow")))
719 "Face for isearch in the anything buffer." :group 'anything)
721 (defface anything-candidate-number '((t (:background "Yellow" :foreground "black")))
722 "Face for candidate number in mode-line." :group 'anything)
724 (defvar anything-isearch-match-face 'anything-isearch-match
725 "Face for matches during incremental search.")
727 (defvar anything-selection-face 'highlight
728 "Face for currently selected item.")
730 (defvar anything-iswitchb-idle-delay 1
731 "Show anything completions if the user is idle that many
732 seconds after typing.")
734 (defvar anything-iswitchb-dont-touch-iswithcb-keys nil
735 "If t then those commands are not bound from `anything-map'
736 under iswitchb which would override standard iswithcb keys.
738 This allows an even more seamless integration with iswitchb for
739 those who prefer using iswitchb bindings even if the anything
740 completions buffer is popped up.
742 Note that you can bind alternative keys for the same command in
743 `anything-map', so that you can use different keys for anything
744 under iswitchb. For example, I bind the character \ to
745 `anything-exit-minibuffer' which key is just above Enter on my
746 keyboard. This way I can switch buffers with Enter and choose
747 anything completions with \.")
749 ;;----------------------------------------------------------------------
751 (defvar anything-buffer "*anything*"
752 "Buffer showing completions.")
754 (defvar anything-action-buffer "*anything action*"
755 "Buffer showing actions.")
757 (defvar anything-selection-overlay nil
758 "Overlay used to highlight the currently selected item.")
760 (defvar anything-isearch-overlay nil
761 "Overlay used to highlight the current match during isearch.")
763 (defvar anything-digit-overlays nil
764 "Overlays for digit shortcuts. See `anything-enable-shortcuts'.")
766 (defvar anything-candidate-cache nil
767 "Holds the available candidate withing a single anything invocation.")
769 (defvar anything-pattern
770 "The input pattern used to update the anything buffer.")
772 (defvar anything-input
773 "The input typed in the candidates panel.")
775 (defvar anything-async-processes nil
776 "List of information about asynchronous processes managed by anything.")
778 (defvar anything-digit-shortcut-count 0
779 "Number of digit shortcuts shown in the anything buffer.")
781 (defvar anything-before-initialize-hook nil
782 "Run before anything initialization.
783 This hook is run before init functions in `anything-sources'.")
785 (defvar anything-after-initialize-hook nil
786 "Run after anything initialization.
787 Global variables are initialized and the anything buffer is created.
788 But the anything buffer has no contents. ")
790 (defvar anything-update-hook nil
791 "Run after the anything buffer was updated according the new input pattern.
792 This hook is run at the beginning of buffer.
793 The first candidate is selected after running this hook.
794 See also `anything-after-update-hook'.")
796 (defvar anything-after-update-hook nil
797 "Run after the anything buffer was updated according the new input pattern.
798 This is very similar to `anything-update-hook' but selection is not moved.
799 It is useful to select a particular object instead of the first one. ")
801 (defvar anything-cleanup-hook nil
802 "Run after anything minibuffer is closed, IOW this hook is executed BEFORE performing action. ")
804 (defvar anything-after-action-hook nil
805 "Run after executing action.")
807 (defvar anything-after-persistent-action-hook nil
808 "Run after executing persistent action.")
810 (defvar anything-move-selection-before-hook nil
811 "Run before moving selection in `anything-buffer'.")
813 (defvar anything-move-selection-after-hook nil
814 "Run after moving selection in `anything-buffer'.")
816 (defvar anything-restored-variables
817 '( anything-candidate-number-limit
818 anything-source-filter
819 anything-source-in-each-line-flag
820 anything-map
821 anything-sources
822 deferred-action-list)
823 "Variables which are restored after `anything' invocation.")
824 ;; `anything-saved-sources' is removed
826 (defvar anything-saved-selection nil
827 "Saved value of the currently selected object when the action
828 list is shown.")
830 ;; `anything-original-source-filter' is removed
832 (defvar anything-candidate-separator
833 "--------------------"
834 "Candidates separator of `multiline' source.")
836 (defvar anything-current-buffer nil
837 "Current buffer when `anything' is invoked.")
839 (defvar anything-buffer-file-name nil
840 "`buffer-file-name' when `anything' is invoked.")
842 (defvar anything-saved-action nil
843 "Saved value of the currently selected action by key.")
845 (defvar anything-last-sources nil
846 "OBSOLETE!! Sources of previously invoked `anything'.")
848 (defvar anything-saved-current-source nil
849 "Saved value of the original (anything-get-current-source) when the action
850 list is shown.")
852 (defvar anything-compiled-sources nil
853 "Compiled version of `anything-sources'. ")
855 (defvar anything-in-persistent-action nil
856 "Flag whether in persistent-action or not.")
858 (defvar anything-quick-update nil
859 "If non-nil, suppress displaying sources which are out of screen at first.
860 They are treated as delayed sources at this input.
861 This flag makes `anything' a bit faster with many sources.")
863 (defvar anything-last-sources-local nil
864 "Buffer local value of `anything-sources'.")
865 (defvar anything-last-buffer nil
866 "`anything-buffer' of previously `anything' session.")
868 (defvar anything-save-configuration-functions
869 '(set-window-configuration . current-window-configuration)
870 "The functions used to restore/save window or frame configurations.
871 It is a pair where the car is the function to restore window or frame config,
872 and the cdr is the function to save the window or frame config.
874 If you want to save and restore frame configuration, set this variable to
875 '\(set-frame-configuration . current-frame-configuration\)
877 Older version saves/restores frame configuration, but the default is changed now
878 because flickering can occur in some environment. ")
880 (defvar anything-persistent-action-use-special-display nil
881 "If non-nil, use `special-display-function' in persistent action.")
883 (defvar anything-execute-action-at-once-if-one nil
884 "If non-nil and there is one candidate, execute the first action without selection.
885 It is useful for `anything' applications.")
887 (defvar anything-quit-if-no-candidate nil
888 "if non-nil and there is no candidate, do not display *anything* buffer and quit.
889 This variable accepts a function, which is executed if no candidate.
891 It is useful for `anything' applications.")
893 (defvar anything-scroll-amount nil
894 "Scroll amount used by `anything-scroll-other-window' and `anything-scroll-other-window-down'.
895 If you prefer scrolling line by line, set this value to 1.")
897 (defvar anything-display-function 'anything-default-display-buffer
898 "Function to display *anything* buffer.
899 It is `anything-default-display-buffer' by default, which affects `anything-samewindow'.")
901 (defvar anything-delayed-init-executed nil)
903 (defvar anything-mode-line-string "\\<anything-map>\\[anything-help]:help \
904 \\[anything-select-action]:Acts \
905 \\[anything-exit-minibuffer]/\\[anything-select-2nd-action-or-end-of-line]/\
906 \\[anything-select-3rd-action]:NthAct \
907 \\[anything-send-bug-report-from-anything]:BugReport"
908 "Help string displayed in mode-line in `anything'.
909 It can be a string or a list of two args, in this case,
910 first arg is a string that will be used as name for candidates number,
911 second arg any string to display in mode line.
912 If nil, use default `mode-line-format'.")
914 (defvar anything-help-message
915 "\\<anything-map>The keys that are defined for `anything' are:
916 \\{anything-map}"
917 "Detailed help message string for `anything'.
918 It also accepts function or variable symbol.")
920 (put 'anything 'timid-completion 'disabled)
922 (defvar anything-inhibit-fit-frame-flag t
923 "If non-nil, inhibit fitting anything frame to its buffer.
924 It is disabled by default because some flickering occurred in some environment.
926 To enable fitting, set both `anything-inhibit-fit-frame-flag' and
927 `fit-frame-inhibit-fitting-flag' to nil.
928 You will need to install external library fit-frame.el.
929 NOTE: setting this to nil will slow down considerably your anything.
930 It will have no effect when you use a full frame Emacs.")
932 (defvar anything-source-in-each-line-flag nil
933 "If non-nil, add anything-source text-property in each candidate.
934 experimental feature.")
936 (defvaralias 'anything-debug-variables 'anything-debug-forms)
937 (defvar anything-debug-forms nil
938 "Forms to show in `anything-debug-output'.
939 Otherwise all variables started with `anything-' are shown.
940 It is useful for debug.")
942 (defvar anything-debug nil
943 "If non-nil, write log message into *Anything Log* buffer.
944 If `debug-on-error' is non-nil, write log message regardless of this variable.
945 It is disabled by default because *Anything Log* grows quickly.")
947 ;; (@* "Internal Variables")
948 (defvar anything-test-candidate-list nil)
949 (defvar anything-test-mode nil)
950 (defvar anything-source-name nil)
951 (defvar anything-candidate-buffer-alist nil)
952 (defvar anything-check-minibuffer-input-timer nil)
953 (defvar anything-match-hash (make-hash-table :test 'equal))
954 (defvar anything-cib-hash (make-hash-table :test 'equal))
955 (defvar anything-tick-hash (make-hash-table :test 'equal))
956 (defvar anything-issued-errors nil)
957 (defvar anything-shortcut-keys nil)
958 (defvar anything-once-called-functions nil)
959 (defvar anything-follow-mode nil)
960 (defvar anything-let-variables nil)
962 ;; (@* "Utility: logging")
963 (defun anything-log (format-string &rest args)
964 "Log message if `debug-on-error' or `anything-debug' is non-nil.
965 Messages are written to the *Anythingn Log* buffer.
966 Arguments are same as `format'."
967 (when (or debug-on-error anything-debug)
968 (with-current-buffer (get-buffer-create "*Anything Log*")
969 (buffer-disable-undo)
970 (set (make-local-variable 'inhibit-read-only) t)
971 (goto-char (point-max))
972 (insert (let ((tm (current-time)))
973 (format "%s.%06d (%s) %s\n"
974 (format-time-string "%H:%M:%S" tm)
975 (nth 2 tm)
976 (anything-log-get-current-function)
977 (apply #'format (cons format-string args))))))))
978 (defmacro anything-log-eval (&rest exprs)
979 "Write each EXPR evaluation result to the *Anything Log* buffer."
980 `(anything-log-eval-internal ',exprs))
981 (defun anything-log-run-hook (hook)
982 (anything-log "executing %s" hook)
983 (when (boundp hook)
984 (anything-log-eval (symbol-value hook))
985 (anything-log-eval (default-value hook)))
986 (run-hooks hook)
987 (anything-log "executed %s" hook))
988 (defun anything-log-eval-internal (exprs)
989 (dolist (expr exprs)
990 (condition-case err
991 (anything-log "%S = %S" expr (eval expr))
992 (error (anything-log "%S = ERROR!" expr)))))
993 (defun anything-log-get-current-function ()
994 "Get function name calling `anything-log'.
995 The original idea is from `tramp-debug-message'."
996 (loop with exclude-func-re = "^anything-\\(?:interpret\\|log\\|.*funcall\\)"
997 for btn from 1 to 40 ;avoid inf-loop
998 for btf = (second (backtrace-frame btn))
999 for fn = (if (symbolp btf) (symbol-name btf) "")
1000 if (and (string-match "^anything" fn)
1001 (not (string-match exclude-func-re fn)))
1002 return fn))
1004 (defun anything-log-error (&rest args)
1005 "Accumulate error messages into `anything-issued-errors'."
1006 (apply 'anything-log (concat "ERROR: " (car args)) (cdr args))
1007 (let ((msg (apply 'format args)))
1008 (unless (member msg anything-issued-errors)
1009 (add-to-list 'anything-issued-errors msg))))
1011 (defvar anything-last-log-file nil)
1012 (defun anything-log-save-maybe ()
1013 (when (stringp anything-debug)
1014 (let ((logdir (expand-file-name (format-time-string "%Y%m%d")
1015 anything-debug)))
1016 (make-directory logdir t)
1017 (with-current-buffer (get-buffer-create "*Anything Log*")
1018 (write-region (point-min) (point-max)
1019 (setq anything-last-log-file
1020 (expand-file-name (format-time-string "%Y%m%d-%H%M%S")
1021 logdir))
1022 nil 'silent)
1023 (erase-buffer)))))
1025 (defun anything-open-last-log ()
1026 "Open anything log file of last anything session."
1027 (interactive)
1028 (if anything-last-log-file
1029 (view-file anything-last-log-file)
1030 (switch-to-buffer "*Anything Log*")))
1032 (defun anything-print-error-messages ()
1033 "Print error messages in `anything-issued-errors'."
1034 (message "%s" (mapconcat 'identity (reverse anything-issued-errors) "\n")))
1039 ;; (anything-log "test")
1040 ;; (switch-to-buffer-other-window "*Anything Log*")
1042 ;; (@* "Programming Tools")
1043 (defmacro anything-aif (test-form then-form &rest else-forms)
1044 "Anaphoric if. Temporary variable `it' is the result of test-form."
1045 `(let ((it ,test-form))
1046 (if it ,then-form ,@else-forms)))
1047 (put 'anything-aif 'lisp-indent-function 2)
1049 (defun anything-mklist (obj)
1050 "If OBJ is a list (but not lambda), return itself, otherwise make a list with one element."
1051 (if (and (listp obj) (not (functionp obj)))
1053 (list obj)))
1055 ;; (@* "Anything API")
1056 (defmacro anything-let (varlist &rest body)
1057 "[OBSOLETE] Like `let'. Bind anything buffer local variables according to VARLIST then eval BODY."
1058 `(anything-let-internal (anything-let-eval-varlist ',varlist)
1059 (lambda () ,@body)))
1060 (put 'anything-let 'lisp-indent-function 1)
1062 (defmacro anything-let* (varlist &rest body)
1063 "[OBSOLETE] Like `let*'. Bind anything buffer local variables according to VARLIST then eval BODY."
1064 `(anything-let-internal (anything-let*-eval-varlist ',varlist)
1065 (lambda () ,@body)))
1066 (put 'anything-let* 'lisp-indent-function 1)
1068 (defun anything-buffer-get ()
1069 "If *anything action* buffer is shown, return `anything-action-buffer', otherwise `anything-buffer'."
1070 (if (anything-action-window)
1071 anything-action-buffer
1072 anything-buffer))
1074 (defun anything-window ()
1075 "Window of `anything-buffer'."
1076 (get-buffer-window (anything-buffer-get) 'visible))
1078 (defun anything-action-window ()
1079 "Window of `anything-action-buffer'."
1080 (get-buffer-window anything-action-buffer 'visible))
1082 (defmacro with-anything-window (&rest body)
1083 `(let ((--tmpfunc-- (lambda () ,@body)))
1084 (if anything-test-mode
1085 (with-current-buffer (anything-buffer-get)
1086 (funcall --tmpfunc--))
1087 (with-selected-window (anything-window)
1088 (funcall --tmpfunc--)))))
1089 (put 'with-anything-window 'lisp-indent-function 0)
1091 (defun anything-deferred-action-function ()
1092 (dolist (f deferred-action-list) (funcall f)))
1093 (defmacro with-anything-restore-variables(&rest body)
1094 "Restore variables specified by `anything-restored-variables' after executing BODY . "
1095 `(let ((--orig-vars (mapcar (lambda (v) (cons v (symbol-value v)))
1096 anything-restored-variables))
1097 (deferred-action-function 'anything-deferred-action-function))
1098 (anything-log "save variables: %S" --orig-vars)
1099 (unwind-protect (progn ,@body)
1100 (loop for (var . value) in --orig-vars
1101 do (set var value))
1102 (anything-log "restore variables"))))
1103 (put 'with-anything-restore-variables 'lisp-indent-function 0)
1105 (defun* anything-attr (attribute-name &optional (src (anything-get-current-source)))
1106 "Get the value of ATTRIBUTE-NAME of SRC (source).
1107 if SRC is omitted, use current source.
1108 It is useful to write your sources."
1109 (anything-aif (assq attribute-name src)
1110 (cdr it)))
1112 (defun* anything-attr* (attribute-name &optional (src (anything-get-current-source)))
1113 "Get the value of ATTRIBUTE-NAME of SRC (source) and pass to `anything-interpret-value'.
1114 if SRC is omitted, use current source.
1115 It is useful to write your sources."
1116 (anything-interpret-value (anything-attr attribute-name src)))
1118 (defun* anything-attr-defined (attribute-name &optional (src (anything-get-current-source)))
1119 "Return non-nil if ATTRIBUTE-NAME of SRC (source) is defined.
1120 if SRC is omitted, use current source.
1121 It is useful to write your sources."
1122 (and (assq attribute-name src) t))
1124 (defun* anything-attrset (attribute-name value &optional (src (anything-get-current-source)))
1125 "Set the value of ATTRIBUTE-NAME of SRC (source) to VALUE.
1126 if SRC is omitted, use current source.
1127 It is useful to write your sources."
1128 (anything-aif (assq attribute-name src)
1129 (setcdr it value)
1130 (setcdr src (cons (cons attribute-name value) (cdr src))))
1131 value)
1133 ;; anything-set-source-filter
1135 ;; This function sets a filter for anything sources and it may be
1136 ;; called while anything is running. It can be used to toggle
1137 ;; displaying of sources dinamically. For example, additional keys
1138 ;; can be bound into `anything-map' to display only the file-related
1139 ;; results if there are too many matches from other sources and
1140 ;; you're after files only:
1142 ;; Shift+F shows only file results from some sources:
1144 ;; (define-key anything-map "F" 'anything-my-show-files-only)
1146 ;; (defun anything-my-show-files-only ()
1147 ;; (interactive)
1148 ;; (anything-set-source-filter '("File Name History"
1149 ;; "Files from Current Directory")))
1151 ;; Shift+A shows all results:
1153 ;; (define-key anything-map "A" 'anything-my-show-all)
1155 ;; (defun anything-my-show-all ()
1156 ;; (interactive)
1157 ;; (anything-set-source-filter nil))
1160 ;; Note that you have to prefix the functions with anything- prefix,
1161 ;; otherwise they won't be bound when Anything is used under
1162 ;; Iswitchb. The -my- part is added to avoid collisions with
1163 ;; existing Anything function names.
1165 (defun anything-set-source-filter (sources)
1166 "Sets the value of `anything-source-filter' and updates the list of results."
1167 (unless (and (listp sources)
1168 (loop for name in sources always (stringp name)))
1169 (error "invalid data in `anything-set-source-filter': %S" sources))
1170 (setq anything-source-filter sources)
1171 (anything-log-eval anything-source-filter)
1172 (anything-update))
1174 (defun anything-set-sources (sources &optional no-init no-update)
1175 "Set `anything-sources' during `anything' invocation.
1176 If NO-INIT is non-nil, skip executing init functions of SOURCES.
1177 If NO-UPDATE is non-nil, skip executing `anything-update'."
1178 (with-current-buffer anything-buffer
1179 (setq anything-compiled-sources nil
1180 anything-sources sources
1181 anything-last-sources-local sources)
1182 (anything-log-eval anything-compiled-sources anything-sources))
1183 (unless no-init (anything-funcall-foreach 'init))
1184 (unless no-update (anything-update)))
1186 (defvar anything-compile-source-functions
1187 '(anything-compile-source--type
1188 anything-compile-source--dummy
1189 anything-compile-source--disable-shortcuts
1190 anything-compile-source--candidates-in-buffer)
1191 "Functions to compile elements of `anything-sources' (plug-in).")
1193 (defun anything-get-sources ()
1194 "Return compiled `anything-sources', which is memoized.
1196 Attributes:
1198 - type
1199 `anything-type-attributes' are merged in.
1200 - candidates-buffer
1201 candidates, volatile and match attrubute are created.
1203 (cond
1204 ;; action
1205 ((anything-action-window)
1206 anything-sources)
1207 ;; memoized
1208 (anything-compiled-sources)
1209 ;; first time
1211 (prog1
1212 (setq anything-compiled-sources
1213 (anything-compile-sources
1214 anything-sources anything-compile-source-functions))
1215 (anything-log-eval anything-compiled-sources)))))
1217 (defun* anything-get-selection (&optional (buffer nil buffer-s) (force-display-part))
1218 "Return the currently selected item or nil.
1219 if BUFFER is nil or unspecified, use anything-buffer as default value.
1220 If FORCE-DISPLAY-PART is non-nil, return the display string."
1221 (setq buffer (if (and buffer buffer-s) buffer anything-buffer))
1222 (unless (anything-empty-buffer-p buffer)
1223 (with-current-buffer buffer
1224 (let ((selection
1225 (or (and (not force-display-part)
1226 (get-text-property (overlay-start
1227 anything-selection-overlay)
1228 'anything-realvalue))
1229 (let ((disp (buffer-substring-no-properties
1230 (overlay-start anything-selection-overlay)
1231 (1- (overlay-end anything-selection-overlay))))
1232 (source (anything-get-current-source)))
1233 (anything-aif (and (not force-display-part)
1234 (assoc-default 'display-to-real source))
1235 (anything-funcall-with-source source it disp)
1236 disp)))))
1237 (unless (equal selection "")
1238 (anything-log-eval selection)
1239 selection)))))
1241 (defun anything-get-action ()
1242 "Return the associated action for the selected candidate.
1243 It is a function symbol (sole action) or list of (action-display . function)."
1244 (unless (anything-empty-buffer-p (anything-buffer-get))
1245 (anything-aif (anything-attr 'action-transformer)
1246 (anything-composed-funcall-with-source
1247 (anything-get-current-source) it
1248 (anything-attr 'action) (anything-get-selection))
1249 (anything-attr 'action))))
1251 (defun anything-get-current-source ()
1252 "Return the source for the current selection / in init/candidates/action/candidate-transformer/filtered-candidate-transformer function."
1253 (declare (special source))
1254 ;; The name `anything-get-current-source' should be used in init function etc.
1255 (if (and (boundp 'anything-source-name) (stringp anything-source-name))
1256 source
1257 (with-current-buffer (anything-buffer-get)
1258 (or (get-text-property (point) 'anything-source)
1259 (block exit
1260 ;; This goto-char shouldn't be necessary, but point is moved to
1261 ;; point-min somewhere else which shouldn't happen.
1262 (goto-char (overlay-start anything-selection-overlay))
1263 (let* ((header-pos (or (anything-get-previous-header-pos)
1264 (anything-get-next-header-pos)))
1265 (source-name
1266 (save-excursion
1267 (unless header-pos
1268 ;(message "No candidates")
1269 (return-from exit nil))
1270 (goto-char header-pos)
1271 (anything-current-line-contents))))
1272 (some (lambda (source)
1273 (if (equal (assoc-default 'name source) source-name)
1274 source))
1275 (anything-get-sources))))))))
1277 (defun anything-buffer-is-modified (buffer)
1278 "Return non-nil when BUFFER is modified since `anything' was invoked."
1279 (let* ((b (get-buffer buffer))
1280 (key (concat (buffer-name b) "/" (anything-attr 'name)))
1281 (source-tick (or (gethash key anything-tick-hash) 0))
1282 (buffer-tick (buffer-chars-modified-tick b))
1283 (modifiedp (/= source-tick buffer-tick)))
1284 (puthash key buffer-tick anything-tick-hash)
1285 (anything-log-eval buffer modifiedp)
1286 modifiedp))
1287 (defun anything-current-buffer-is-modified ()
1288 "Return non-nil when `anything-current-buffer' is modified since `anything' was invoked."
1289 (anything-buffer-is-modified anything-current-buffer))
1291 (defvar anything-quit nil)
1292 (defun anything-run-after-quit (function &rest args)
1293 "Perform an action after quitting `anything'.
1294 The action is to call FUNCTION with arguments ARGS."
1295 (setq anything-quit t)
1296 (anything-log-eval function args)
1297 (apply 'run-with-idle-timer 0 nil function args)
1298 (anything-exit-minibuffer))
1301 (defun define-anything-type-attribute (type definition &optional doc)
1302 "Register type attribute of TYPE as DEFINITION with DOC.
1303 DOC is displayed in `anything-type-attributes' docstring.
1305 Use this function is better than setting `anything-type-attributes' directly."
1306 (loop for i in definition do
1307 ;; without `ignore-errors', error at emacs22
1308 (ignore-errors (setf i (delete nil i))))
1309 (anything-add-type-attribute type definition)
1310 (and doc (anything-document-type-attribute type doc))
1311 nil)
1313 (defvaralias 'anything-attributes 'anything-additional-attributes)
1314 (defvar anything-additional-attributes nil
1315 "List of all `anything' attributes.")
1316 (defun anything-document-attribute (attribute short-doc &optional long-doc)
1317 "Register ATTRIBUTE documentation introduced by plug-in.
1318 SHORT-DOC is displayed beside attribute name.
1319 LONG-DOC is displayed below attribute name and short documentation."
1320 (if long-doc
1321 (setq short-doc (concat "(" short-doc ")"))
1322 (setq long-doc short-doc
1323 short-doc ""))
1324 (add-to-list 'anything-additional-attributes attribute t)
1325 (put attribute 'anything-attrdoc
1326 (concat "- " (symbol-name attribute) " " short-doc "\n\n" long-doc "\n")))
1327 (put 'anything-document-attribute 'lisp-indent-function 2)
1329 (defun anything-require-at-least-version (version)
1330 "Output error message unless anything.el is older than VERSION.
1331 This is suitable for anything applications."
1332 (when (and (string= "1." (substring version 0 2))
1333 (string-match "1\.\\([0-9]+\\)" anything-version)
1334 (< (string-to-number (match-string 1 anything-version))
1335 (string-to-number (substring version 2))))
1336 (error "Please update anything.el!!
1338 M-x auto-install-batch anything
1340 You must have auto-install.el too.
1341 http://www.emacswiki.org/cgi-bin/wiki/download/auto-install.el
1342 ")))
1344 (defun anything-interpret-value (value &optional source)
1345 "interpret VALUE as variable, function or literal.
1346 If VALUE is a function, call it with no arguments and return the value.
1347 If SOURCE is `anything' source, `anything-source-name' is source name.
1349 If VALUE is a variable, return the value.
1351 If VALUE is a symbol, but it is not a function or a variable, cause an error.
1353 Otherwise, return VALUE itself."
1354 (cond ((and source (functionp value))
1355 (anything-funcall-with-source source value))
1356 ((functionp value)
1357 (funcall value))
1358 ((and (symbolp value) (boundp value))
1359 (symbol-value value))
1360 ((symbolp value)
1361 (error "anything-interpret-value: Symbol must be a function or a variable"))
1363 value)))
1365 (defun anything-once (function &rest args)
1366 "Ensure FUNCTION with ARGS to be called once in `anything' session."
1367 (let ((spec (cons function args)))
1368 (unless (member spec anything-once-called-functions)
1369 (apply function args)
1370 (push spec anything-once-called-functions))))
1372 ;; (@* "Core: API helper")
1373 (defun anything-empty-buffer-p (&optional buffer)
1374 (zerop (buffer-size (and buffer (get-buffer buffer)))))
1376 (defun anything-let-eval-varlist (varlist)
1377 (mapcar (lambda (pair)
1378 (if (listp pair)
1379 (cons (car pair) (eval (cadr pair)))
1380 (cons pair nil)))
1381 varlist))
1382 (defun anything-let*-eval-varlist (varlist)
1383 (let ((vars (mapcar (lambda (pair) (or (car-safe pair) pair)) varlist)))
1384 (eval `(let ,vars
1385 ,@(mapcar (lambda (pair)
1386 (if (listp pair)
1387 `(setq ,(car pair) ,(cadr pair))
1388 `(setq ,pair nil)))
1389 varlist)
1390 (mapcar (lambda (v) (cons v (symbol-value v))) ',vars)))))
1391 (defun anything-let-internal (binding bodyfunc)
1392 "Evaluate BODYFUNC and Set BINDING to anything buffer-local variables.
1393 BINDING is a list of (VARNAME . VALUE) pair."
1394 (setq anything-let-variables binding)
1395 (unwind-protect
1396 (funcall bodyfunc)
1397 (setq anything-let-variables nil)))
1400 ;; (@* "Core: tools")
1401 (defun anything-current-line-contents ()
1402 "Current line strig without properties."
1403 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))
1405 (defun anything-funcall-with-source (source func &rest args)
1406 "Call FUNC with ARGS with variable `anything-source-name' and `source' is bound.
1407 FUNC can be function list. Return the result of last function call."
1408 (let ((anything-source-name (assoc-default 'name source))
1409 result)
1410 (anything-log-eval anything-source-name func args)
1411 (dolist (func (if (functionp func) (list func) func) result)
1412 (setq result (apply func args)))))
1414 (defun anything-funcall-foreach (sym)
1415 "Call the sym function(s) for each source if any."
1416 (dolist (source (anything-get-sources))
1417 (anything-aif (assoc-default sym source)
1418 (anything-funcall-with-source source it))))
1420 (defun anything-normalize-sources (sources)
1421 "If SOURCES is only one source, make a list."
1422 (cond ((or (and sources ; avoid nil
1423 (symbolp sources))
1424 (and (listp sources) (assq 'name sources)))
1425 (list sources))
1426 (sources)
1427 (t anything-sources)))
1429 (defun anything-approximate-candidate-number (&optional in-current-source)
1430 "Approximate Number of candidates.
1431 It is used to check if candidate number is 0, 1, or 2+."
1432 (with-current-buffer anything-buffer
1433 (save-excursion
1434 (if in-current-source
1435 (goto-char (anything-get-previous-header-pos))
1436 (goto-char (point-min)))
1437 (forward-line 1)
1438 (let ((lines (save-excursion
1439 (loop with ln = 0
1440 while (not (if in-current-source
1441 (or (anything-pos-header-line-p) (eobp))
1442 (eobp)))
1443 unless (anything-pos-header-line-p)
1444 do (incf ln)
1445 do (forward-line 1) finally return ln)))
1446 (count-multi 1))
1447 (if (anything-pos-multiline-p)
1448 (save-excursion
1449 (loop while (search-forward anything-candidate-separator nil t)
1450 do (incf count-multi) finally return count-multi))
1451 lines)))))
1453 (defmacro with-anything-quittable (&rest body)
1454 `(let (inhibit-quit)
1455 (condition-case v
1456 (progn ,@body)
1457 (quit (setq anything-quit t)
1458 (exit-minibuffer)
1459 (keyboard-quit)))))
1460 (put 'with-anything-quittable 'lisp-indent-function 0)
1462 (defun anything-compose (arg-lst func-lst)
1463 "Call each function in FUNC-LST with the arguments specified in ARG-LST.
1464 The result of each function will be the new `car' of ARG-LST.
1466 This function allows easy sequencing of transformer functions."
1467 (dolist (func func-lst)
1468 (setcar arg-lst (apply func arg-lst)))
1469 (car arg-lst))
1471 (defun anything-composed-funcall-with-source (source funcs &rest args)
1472 (if (functionp funcs)
1473 (apply 'anything-funcall-with-source source funcs args)
1474 (apply 'anything-funcall-with-source
1475 source (lambda (&rest args) (anything-compose args funcs)) args)))
1477 (defun anything-new-timer (variable timer)
1478 "Set new TIMER to VARIABLE. Old timer is cancelled."
1479 (anything-aif (symbol-value variable)
1480 (cancel-timer it))
1481 (set variable timer))
1483 ;; (@* "Core: entry point")
1484 (defconst anything-argument-keys
1485 '(:sources :input :prompt :resume :preselect :buffer :keymap))
1486 ;;;###autoload
1487 (defun anything (&rest plist)
1488 "Select anything. In Lisp program, some optional arguments can be used.
1490 PLIST is a list like (:key1 val1 :key2 val2 ...) or
1491 (&optional sources input prompt resume preselect buffer keymap).
1493 Basic keywords are the following:
1495 - :sources
1497 Temporary value of `anything-sources'. It also accepts a
1498 symbol, interpreted as a variable of an anything source. It
1499 also accepts an alist representing an anything source, which is
1500 detected by (assq 'name ANY-SOURCES)
1502 - :input
1504 Temporary value of `anything-pattern', ie. initial input of minibuffer.
1506 - :prompt
1508 Prompt other than \"pattern: \".
1510 - :resume
1512 If t, Resurrect previously instance of `anything'. Skip the initialization.
1513 If 'noresume, this instance of `anything' cannot be resumed.
1515 - :preselect
1517 Initially selected candidate. Specified by exact candidate or a regexp.
1518 Note that it is not working with delayed sources.
1520 - :buffer
1522 `anything-buffer' instead of *anything*.
1524 - :keymap
1526 `anything-map' for current `anything' session.
1529 Of course, conventional arguments are supported, the two are same.
1531 (anything :sources sources :input input :prompt prompt :resume resume
1532 :preselect preselect :buffer buffer :keymap keymap)
1533 (anything sources input prompt resume preselect buffer keymap)
1536 Other keywords are interpreted as local variables of this anything session.
1537 The `anything-' prefix can be omitted. For example,
1539 (anything :sources 'anything-c-source-buffers
1540 :buffer \"*buffers*\" :candidate-number-limit 10)
1542 means starting anything session with `anything-c-source-buffers'
1543 source in *buffers* buffer and set
1544 `anything-candidate-number-limit' to 10 as session local variable. "
1545 (interactive)
1546 (if (keywordp (car plist))
1547 (anything-let-internal
1548 (anything-parse-keys plist)
1549 (lambda ()
1550 (apply 'anything
1551 (mapcar (lambda (key) (plist-get plist key))
1552 anything-argument-keys))))
1553 (apply 'anything-internal plist)))
1555 (defun* anything-resume (&optional (any-buffer anything-last-buffer) buffer-pattern (any-resume t))
1556 "Resurrect previously invoked `anything'."
1557 (interactive)
1558 (when (or current-prefix-arg buffer-pattern)
1559 (setq any-buffer (anything-resume-select-buffer buffer-pattern)))
1560 (setq anything-compiled-sources nil)
1561 (anything
1562 (or (buffer-local-value 'anything-last-sources-local (get-buffer any-buffer))
1563 anything-last-sources anything-sources)
1564 (buffer-local-value 'anything-input-local (get-buffer any-buffer))
1565 nil any-resume nil any-buffer))
1567 ;;; rubikitch: experimental
1568 ;;; I use this and check it whether I am convenient.
1569 ;;; I may introduce an option to control the behavior.
1570 (defun* anything-resume-window-only (&optional (any-buffer anything-last-buffer) buffer-pattern)
1571 (interactive)
1572 (anything-resume any-buffer buffer-pattern 'window-only))
1574 ;;;###autoload
1575 (defun anything-at-point (&optional any-sources any-input any-prompt any-resume any-preselect any-buffer)
1576 "Same as `anything' except when C-u is pressed, the initial input is the symbol at point."
1577 (interactive)
1578 (anything any-sources
1579 (if current-prefix-arg
1580 (concat "\\b" (thing-at-point 'symbol) "\\b"
1581 (if (featurep 'anything-match-plugin) " " ""))
1582 any-input)
1583 any-prompt any-resume any-preselect any-buffer))
1585 ;;;###autoload
1586 (defun anything-other-buffer (any-sources any-buffer)
1587 "Simplified interface of `anything' with other `anything-buffer'"
1588 (anything any-sources nil nil nil nil any-buffer))
1590 ;;; (@* "Core: entry point helper")
1591 (defun anything-internal (&optional any-sources any-input any-prompt any-resume any-preselect any-buffer any-keymap)
1592 "Older interface of `anything'. It is called by `anything'."
1593 (anything-log "++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
1594 (anything-log-eval any-prompt any-preselect any-buffer any-keymap)
1595 (unwind-protect
1596 (condition-case v
1597 (let ( ;; It is needed because `anything-source-name' is non-nil
1598 ;; when `anything' is invoked by action. Awful global scope.
1599 anything-source-name
1600 anything-in-persistent-action
1601 anything-quit
1602 (case-fold-search t)
1603 (anything-buffer (or any-buffer anything-buffer))
1604 ;; cua-mode ; avoid error when region is selected
1606 (with-anything-restore-variables
1607 (anything-initialize-1 any-resume any-input any-sources)
1608 (anything-display-buffer anything-buffer)
1609 (anything-log "show prompt")
1610 (unwind-protect
1611 (anything-read-pattern-maybe
1612 any-prompt any-input any-preselect any-resume any-keymap)
1613 (anything-cleanup)))
1614 (prog1 (unless anything-quit (anything-execute-selection-action-1))
1615 (anything-log "end session --------------------------------------------")))
1616 (quit
1617 (anything-on-quit)
1618 (anything-log "end session (quit) -------------------------------------")
1619 nil))
1620 (anything-log-save-maybe)))
1624 (defun anything-parse-keys (keys)
1625 (loop for (key value &rest _) on keys by #'cddr
1626 for symname = (substring (symbol-name key) 1)
1627 for sym = (intern (if (string-match "^anything-" symname)
1628 symname
1629 (concat "anything-" symname)))
1630 unless (memq key anything-argument-keys)
1631 collect (cons sym value)))
1633 (defun anything-resume-p (any-resume)
1634 "Whethre current anything session is resumed or not."
1635 (memq any-resume '(t window-only)))
1637 (defvar anything-buffers nil
1638 "All of `anything-buffer' in most recently used order.")
1639 (defun anything-initialize-1 (any-resume any-input any-sources)
1640 "The real initialization of `anything'.
1642 This function name should be `anything-initialize', but anything
1643 extensions may advice `anything-initalize'. I cannot rename, sigh."
1644 (anything-log "start initialization: any-resume=%S any-input=%S" any-resume any-input)
1645 (anything-frame-or-window-configuration 'save)
1646 (setq anything-sources (anything-normalize-sources any-sources))
1647 (anything-log "sources = %S" anything-sources)
1648 (anything-hooks 'setup)
1649 (anything-current-position 'save)
1650 (if (anything-resume-p any-resume)
1651 (anything-initialize-overlays (anything-buffer-get))
1652 (anything-initialize))
1653 (unless (eq any-resume 'noresume)
1654 (anything-recent-push anything-buffer 'anything-buffers)
1655 (setq anything-last-buffer anything-buffer))
1656 (when any-input (setq anything-input any-input anything-pattern any-input))
1657 (and (anything-resume-p any-resume) (anything-funcall-foreach 'resume))
1658 (anything-log "end initialization"))
1660 (defun anything-execute-selection-action-1 ()
1661 (unwind-protect
1662 (anything-execute-selection-action)
1663 (anything-aif (get-buffer anything-action-buffer)
1664 (kill-buffer it))
1665 (anything-log-run-hook 'anything-after-action-hook)))
1667 (defun anything-on-quit ()
1668 (setq minibuffer-history (cons anything-input minibuffer-history))
1669 (anything-current-position 'restore))
1671 (defun anything-resume-select-buffer (input)
1672 (anything '(((name . "Resume anything buffer")
1673 (candidates . anything-buffers)
1674 (action . identity)))
1675 input nil 'noresume nil "*anything resume*"))
1677 (defun anything-recent-push (elt list-var)
1678 "Add ELT to the value of LIST-VAR as most recently used value."
1679 (let ((m (member elt (symbol-value list-var))))
1680 (and m (set list-var (delq (car m) (symbol-value list-var))))
1681 (push elt (symbol-value list-var))))
1683 ;;; (@* "Core: Accessors")
1684 ;;; rubikitch: I love to create functions to control variables.
1685 (defvar anything-current-position nil
1686 "Cons of (point) and (window-start) when `anything' is invoked.
1687 It is needed because restoring position when `anything' is keyboard-quitted.")
1688 (defun anything-current-position (save-or-restore)
1689 (case save-or-restore
1690 (save
1691 (setq anything-current-position (cons (point) (window-start))))
1692 (restore
1693 (goto-char (car anything-current-position))
1694 (set-window-start (selected-window) (cdr anything-current-position)))))
1696 ;;; FIXME I want to remove them. But anything-iswitchb uses them.
1697 (defun anything-current-frame/window-configuration ()
1698 (funcall (cdr anything-save-configuration-functions)))
1699 (defun anything-set-frame/window-configuration (&optional win-or-frame-config)
1700 (funcall (car anything-save-configuration-functions)
1701 (or win-or-frame-config
1702 anything-last-frame-or-window-configuration)))
1704 ;; Internal.
1705 (defvar anything-last-frame-or-window-configuration nil
1706 "Used to store window or frame configuration when anything start.")
1707 (defun anything-frame-or-window-configuration (save-or-restore)
1708 "Save or restore last frame or window configuration.
1709 Possible value of SAVE-OR-RESTORE are 'save and 'restore.
1710 window or frame configuration is saved/restored according to values of
1711 `anything-save-configuration-functions'."
1712 (anything-log-eval anything-save-configuration-functions)
1713 (case save-or-restore
1714 (save (setq anything-last-frame-or-window-configuration
1715 (funcall (cdr anything-save-configuration-functions))))
1716 (restore (funcall (car anything-save-configuration-functions)
1717 anything-last-frame-or-window-configuration)
1718 ;; Restore frame focus.
1719 (let ((frame (and (listp anything-last-frame-or-window-configuration)
1720 (caadr anything-last-frame-or-window-configuration))))
1721 ;; If `anything-save-configuration-functions' are window functions
1722 ;; frame should be nil, use current frame.
1723 (unless (framep frame)
1724 (setq frame (selected-frame)))
1725 (select-frame-set-input-focus frame)))))
1728 ;; (@* "Core: Display *anything* buffer")
1729 (defun anything-display-buffer (buf)
1730 "Display *anything* buffer."
1731 (let (pop-up-frames)
1732 (funcall (with-current-buffer buf anything-display-function) buf)))
1734 (defun anything-default-display-buffer (buf)
1735 (funcall (if anything-samewindow 'switch-to-buffer 'pop-to-buffer) buf))
1737 ;; (@* "Core: initialize")
1738 (defun anything-initialize ()
1739 "Initialize anything settings and set up the anything buffer."
1740 (anything-log-run-hook 'anything-before-initialize-hook)
1741 (setq anything-once-called-functions nil)
1742 (setq anything-delayed-init-executed nil)
1743 (setq anything-current-buffer (current-buffer))
1744 (setq anything-buffer-file-name buffer-file-name)
1745 (setq anything-issued-errors nil)
1746 (setq anything-compiled-sources nil)
1747 (setq anything-saved-current-source nil)
1748 ;; Call the init function for sources where appropriate
1749 (anything-funcall-foreach 'init)
1751 (setq anything-pattern "")
1752 (setq anything-input "")
1753 (setq anything-candidate-cache nil)
1754 (setq anything-last-sources anything-sources)
1756 (anything-create-anything-buffer)
1757 (anything-log-run-hook 'anything-after-initialize-hook))
1759 (defvar anything-reading-pattern nil
1760 "Whether in `read-string' in anything or not.")
1761 (defun anything-read-pattern-maybe (any-prompt any-input any-preselect any-resume any-keymap)
1762 (if (anything-resume-p any-resume)
1763 (anything-mark-current-line)
1764 (anything-update))
1765 ;; FIXME: This make anything write in minibuffer
1766 ;; When using a minibuffer frame independent config.
1767 ;; Is it really needed and why?
1768 ;; I leave it commented for now.
1769 ;(select-frame-set-input-focus (window-frame (minibuffer-window)))
1770 (anything-preselect any-preselect)
1771 (with-current-buffer (anything-buffer-get)
1772 (and any-keymap (set (make-local-variable 'anything-map) any-keymap))
1773 (let ((minibuffer-local-map
1774 anything-map))
1775 (anything-log-eval (anything-approximate-candidate-number)
1776 anything-execute-action-at-once-if-one
1777 anything-quit-if-no-candidate)
1778 (cond ((and anything-execute-action-at-once-if-one
1779 (= (anything-approximate-candidate-number) 1))
1780 (ignore))
1781 ((and anything-quit-if-no-candidate
1782 (= (anything-approximate-candidate-number) 0))
1783 (setq anything-quit t)
1784 (and (functionp anything-quit-if-no-candidate)
1785 (funcall anything-quit-if-no-candidate)))
1787 (let ((anything-reading-pattern t))
1788 (read-string (or any-prompt "pattern: ") any-input)))))))
1790 (defun anything-create-anything-buffer (&optional test-mode)
1791 "Create newly created `anything-buffer'.
1792 If TEST-MODE is non-nil, clear `anything-candidate-cache'."
1793 (when test-mode
1794 (setq anything-candidate-cache nil))
1795 (with-current-buffer (get-buffer-create anything-buffer)
1796 (anything-log "kill local variables: %S" (buffer-local-variables))
1797 (kill-all-local-variables)
1798 (set (make-local-variable 'inhibit-read-only) t)
1799 (buffer-disable-undo)
1800 (erase-buffer)
1801 (set (make-local-variable 'inhibit-read-only) t)
1802 (set (make-local-variable 'anything-last-sources-local) anything-sources)
1803 (set (make-local-variable 'anything-follow-mode) nil)
1804 (set (make-local-variable 'anything-display-function) anything-display-function)
1805 (anything-initialize-persistent-action)
1806 (anything-log-eval anything-display-function anything-let-variables)
1807 (loop for (var . val) in anything-let-variables
1808 do (set (make-local-variable var) val))
1810 (setq cursor-type nil)
1811 (setq mode-name "Anything"))
1812 (anything-initialize-overlays anything-buffer)
1813 (get-buffer anything-buffer))
1815 (defun anything-initialize-overlays (buffer)
1816 (anything-log "overlay setup")
1817 (if anything-selection-overlay
1818 ;; make sure the overlay belongs to the anything buffer if
1819 ;; it's newly created
1820 (move-overlay anything-selection-overlay (point-min) (point-min)
1821 (get-buffer buffer))
1823 (setq anything-selection-overlay
1824 (make-overlay (point-min) (point-min) (get-buffer buffer)))
1825 (overlay-put anything-selection-overlay 'face anything-selection-face))
1827 (cond (anything-enable-shortcuts
1828 (setq anything-shortcut-keys
1829 (assoc-default anything-enable-shortcuts anything-shortcut-keys-alist))
1830 (unless anything-digit-overlays
1831 (setq anything-digit-overlays
1832 (loop for key across anything-shortcut-keys
1833 for overlay = (make-overlay (point-min) (point-min)
1834 (get-buffer buffer))
1835 do (overlay-put overlay 'before-string
1836 (format "%s - " (upcase (make-string 1 key))))
1837 collect overlay))))
1838 (anything-digit-overlays
1839 (mapc 'delete-overlay anything-digit-overlays)
1840 (setq anything-digit-overlays nil))))
1842 (defun anything-hooks (setup-or-cleanup)
1843 (let ((hooks '((deferred-action-list anything-check-minibuffer-input)
1844 (minibuffer-setup-hook anything-print-error-messages))))
1845 (if (eq setup-or-cleanup 'setup)
1846 (dolist (args hooks) (apply 'add-hook args))
1847 (dolist (args (reverse hooks)) (apply 'remove-hook args)))))
1849 ;; (@* "Core: clean up")
1850 ;;; TODO move
1851 (defun anything-cleanup ()
1852 "Clean up the mess."
1853 (anything-log "start cleanup")
1854 (with-current-buffer anything-buffer
1855 (setq cursor-type t)
1856 ;; Call burry-buffer whithout arg
1857 ;; to remove anything-buffer from window.
1858 (bury-buffer)
1859 ;; Be sure we call this from anything-buffer.
1860 (anything-funcall-foreach 'cleanup))
1861 (anything-new-timer 'anything-check-minibuffer-input-timer nil)
1862 (anything-kill-async-processes)
1863 (anything-log-run-hook 'anything-cleanup-hook)
1864 (anything-hooks 'cleanup)
1865 (anything-frame-or-window-configuration 'restore)
1866 ;; For some reasons, text can stay in minibuffer
1867 ;; after an anything session, so erase minibuffer contents
1868 ;; on exit.
1869 (anything-clean-up-minibuffer))
1871 (defun anything-clean-up-minibuffer ()
1872 "Remove contents of minibuffer."
1873 (with-current-buffer (window-buffer (minibuffer-window))
1874 (delete-minibuffer-contents)))
1876 ;; (@* "Core: input handling")
1877 (defun anything-check-minibuffer-input ()
1878 "Extract input string from the minibuffer and check if it needs
1879 to be handled."
1880 (let ((delay (with-current-buffer anything-buffer anything-input-idle-delay)))
1881 (if (or (not delay) (anything-action-window))
1882 (anything-check-minibuffer-input-1)
1883 (anything-new-timer
1884 'anything-check-minibuffer-input-timer
1885 (run-with-idle-timer delay nil 'anything-check-minibuffer-input-1)))))
1887 (defun anything-check-minibuffer-input-1 ()
1888 (with-anything-quittable
1889 (with-selected-window (minibuffer-window)
1890 (anything-check-new-input (minibuffer-contents)))))
1892 (defun anything-check-new-input (input)
1893 "Check input string and update the anything buffer if
1894 necessary."
1895 (unless (equal input anything-pattern)
1896 (setq anything-pattern input)
1897 (unless (anything-action-window)
1898 (setq anything-input anything-pattern))
1899 (anything-log-eval anything-pattern anything-input)
1900 (anything-update)))
1902 ;; (@* "Core: source compiler")
1903 (defvar anything-compile-source-functions-default anything-compile-source-functions
1904 "Plug-ins this file provides.")
1905 (defun anything-compile-sources (sources funcs)
1906 "Compile sources (`anything-sources') with funcs (`anything-compile-source-functions').
1907 Anything plug-ins are realized by this function."
1908 (mapcar
1909 (lambda (source)
1910 (loop with source = (if (listp source) source (symbol-value source))
1911 for f in funcs
1912 do (setq source (funcall f source))
1913 finally (return source)))
1914 sources))
1916 ;; (@* "Core: plug-in attribute documentation hack")
1918 ;; `anything-document-attribute' is public API.
1919 (defadvice documentation-property (after anything-document-attribute activate)
1920 "Hack to display plug-in attributes' documentation as `anything-sources' docstring."
1921 (when (eq (ad-get-arg 0) 'anything-sources)
1922 (setq ad-return-value
1923 (concat ad-return-value "\n"
1924 (mapconcat (lambda (sym) (get sym 'anything-attrdoc))
1925 anything-additional-attributes
1926 "\n")))))
1927 ;; (describe-variable 'anything-sources)
1928 ;; (documentation-property 'anything-sources 'variable-documentation)
1929 ;; (progn (ad-disable-advice 'documentation-property 'after 'anything-document-attribute) (ad-update 'documentation-property))
1931 ;; (@* "Core: all candidates")
1932 (defun anything-process-delayed-init (source)
1933 (let ((name (assoc-default 'name source)))
1934 (unless (member name anything-delayed-init-executed)
1935 (anything-aif (assoc-default 'delayed-init source)
1936 (with-current-buffer anything-current-buffer
1937 (anything-funcall-with-source source it)
1938 (dolist (f (if (functionp it) (list it) it))
1939 (add-to-list 'anything-delayed-init-executed name)))))))
1941 (defun anything-get-candidates (source)
1942 "Retrieve and return the list of candidates from
1943 SOURCE."
1944 (anything-process-delayed-init source)
1945 (let* ((candidate-source (assoc-default 'candidates source))
1946 (type-error (lambda ()
1947 (error (concat "Candidates must either be a function, "
1948 " a variable or a list: %s")
1949 candidate-source)))
1950 (candidates (condition-case err
1951 (anything-interpret-value candidate-source source)
1952 (error (funcall type-error)))))
1953 (cond ((processp candidates) candidates)
1954 ((listp candidates) (anything-transform-candidates candidates source))
1955 (t (funcall type-error)))))
1958 (defun anything-get-cached-candidates (source)
1959 "Return the cached value of candidates for SOURCE.
1960 Cache the candidates if there is not yet a cached value."
1961 (let* ((name (assoc-default 'name source))
1962 (candidate-cache (assoc name anything-candidate-cache)))
1963 (cond (candidate-cache
1964 (anything-log "use cached candidates")
1965 (cdr candidate-cache))
1967 (anything-log "calculate candidates")
1968 (let ((candidates (anything-get-candidates source)))
1969 (cond ((processp candidates)
1970 (push (cons candidates
1971 (append source
1972 (list (cons 'item-count 0)
1973 (cons 'incomplete-line ""))))
1974 anything-async-processes)
1975 (set-process-filter candidates 'anything-output-filter)
1976 (setq candidates nil))
1977 ((not (assoc 'volatile source))
1978 (setq candidate-cache (cons name candidates))
1979 (push candidate-cache anything-candidate-cache)))
1980 candidates)))))
1982 ;;; (@* "Core: candidate transformers")
1983 (defun anything-process-candidate-transformer (candidates source)
1984 (anything-aif (assoc-default 'candidate-transformer source)
1985 (anything-composed-funcall-with-source source it candidates)
1986 candidates))
1987 (defun anything-process-filtered-candidate-transformer (candidates source)
1988 (anything-aif (assoc-default 'filtered-candidate-transformer source)
1989 (anything-composed-funcall-with-source source it candidates source)
1990 candidates))
1991 (defun anything-process-filtered-candidate-transformer-maybe (candidates source process-p)
1992 (if process-p
1993 (anything-process-filtered-candidate-transformer candidates source)
1994 candidates))
1995 (defun anything-process-real-to-display (candidates source)
1996 (anything-aif (assoc-default 'real-to-display source)
1997 (setq candidates (anything-funcall-with-source
1998 source 'mapcar
1999 (lambda (cand_)
2000 (if (consp cand_)
2001 ;; override DISPLAY from candidate-transformer
2002 (cons (funcall it (cdr cand_)) (cdr cand_))
2003 (cons (funcall it cand_) cand_)))
2004 candidates))
2005 candidates))
2006 (defun anything-transform-candidates (candidates source &optional process-p)
2007 "Transform CANDIDATES according to candidate transformers."
2008 (anything-process-real-to-display
2009 (anything-process-filtered-candidate-transformer-maybe
2010 (anything-process-candidate-transformer candidates source) source process-p)
2011 source))
2014 ;; (@* "Core: narrowing candidates")
2015 (defun anything-candidate-number-limit (source)
2016 "`anything-candidate-number-limit' variable may be overridden by SOURCE.
2017 If (candidate-number-limit) is in SOURCE, show all candidates in SOURCE,
2018 ie. cancel the effect of `anything-candidate-number-limit'."
2019 (anything-aif (assq 'candidate-number-limit source)
2020 (or (cdr it) 99999999)
2021 (or anything-candidate-number-limit 99999999)))
2023 (defconst anything-default-match-functions
2024 (list (lambda (candidate)
2025 (string-match anything-pattern candidate))))
2027 (defun anything-compute-matches (source)
2028 "Compute matches from SOURCE according to its settings."
2029 (if debug-on-error
2030 (anything-compute-matches-internal source)
2031 (condition-case v
2032 (anything-compute-matches-internal source)
2033 (error (anything-log-error
2034 "anything-compute-matches: error when processing source: %s"
2035 (assoc-default 'name source))
2036 nil))))
2038 (defun anything-candidate-get-display (candidate)
2039 "Get display part (searched) from CANDIDATE.
2040 CANDIDATE is a string, a symbol, or (DISPLAY . REAL) cons cell."
2041 (format "%s" (or (car-safe candidate) candidate)))
2043 (defun anything-process-pattern-transformer (pattern source)
2044 (anything-aif (assoc-default 'pattern-transformer source)
2045 (anything-composed-funcall-with-source source it pattern)
2046 pattern))
2048 (defun anything-match-functions (source)
2049 (or (assoc-default 'match source)
2050 anything-default-match-functions))
2052 (defmacro anything-accumulate-candidates-internal (cand newmatches hash item-count limit)
2053 "INTERNAL: add CAND (ITEM-COUNT th match) into NEWMATCHES.
2054 Use HASH to uniq NEWMATCHES.
2055 if ITEM-COUNT reaches LIMIT, exit from inner loop."
2056 `(unless (gethash ,cand ,hash)
2057 (puthash ,cand t ,hash)
2058 (push ,cand ,newmatches)
2059 (incf ,item-count)
2060 (when (= ,item-count ,limit)
2061 (setq exit t)
2062 (return))))
2064 (defun anything-take-first-elements (seq n)
2065 (if (> (length seq) n)
2066 (setq seq (subseq seq 0 n))
2067 seq))
2069 (defun anything-match-from-candidates (cands matchfns limit)
2070 (let (matches)
2071 (condition-case nil
2072 (let ((item-count 0) exit)
2073 (clrhash anything-match-hash)
2074 (dolist (match matchfns)
2075 (let (newmatches)
2076 (dolist (candidate cands)
2077 (when (funcall match (anything-candidate-get-display candidate))
2078 (anything-accumulate-candidates-internal
2079 candidate newmatches anything-match-hash item-count limit)))
2080 (setq matches (append matches (reverse newmatches)))
2081 (if exit (return)))))
2082 (invalid-regexp (setq matches nil)))
2083 matches))
2085 (defun anything-compute-matches-internal (source)
2086 (save-current-buffer
2087 (let ((matchfns (anything-match-functions source))
2088 (anything-source-name (assoc-default 'name source))
2089 (limit (anything-candidate-number-limit source))
2090 (anything-pattern (anything-process-pattern-transformer
2091 anything-pattern source)))
2092 (anything-process-filtered-candidate-transformer
2093 (if (or (equal anything-pattern "") (equal matchfns '(identity)))
2094 (anything-take-first-elements
2095 (anything-get-cached-candidates source) limit)
2096 (anything-match-from-candidates
2097 (anything-get-cached-candidates source) matchfns limit))
2098 source))))
2100 ;; (anything '(((name . "error")(candidates . (lambda () (hage))) (action . identity))))
2102 (defun anything-process-source (source)
2103 "Display matches from SOURCE according to its settings."
2104 (anything-log-eval (assoc-default 'name source))
2105 (if (assq 'direct-insert-match source) ;experimental
2106 (anything-process-source--direct-insert-match source)
2107 (let ((matches (anything-compute-matches source)))
2108 (when matches
2109 (when anything-test-mode
2110 (setq anything-test-candidate-list
2111 `(,@anything-test-candidate-list
2112 (,(assoc-default 'name source)
2113 ,matches))))
2114 (anything-insert-header-from-source source)
2115 (if (not (assq 'multiline source))
2116 (mapc 'anything-insert-match-with-digit-overlay matches)
2117 (let ((start (point)) separate)
2118 (dolist (match matches)
2119 (if separate
2120 (anything-insert-candidate-separator)
2121 (setq separate t))
2122 (anything-insert-match-with-digit-overlay match))
2123 (put-text-property start (point) 'anything-multiline t)))))))
2125 (defun anything-insert-match-with-digit-overlay (match)
2126 (declare (special source))
2127 (anything-put-digit-overlay-maybe)
2128 (anything-insert-match match 'insert source))
2130 (defun anything-put-digit-overlay-maybe ()
2131 (when (and anything-enable-shortcuts
2132 (not (eq anything-digit-shortcut-count
2133 (length anything-digit-overlays))))
2134 (move-overlay (nth anything-digit-shortcut-count
2135 anything-digit-overlays)
2136 (point-at-bol)
2137 (point-at-bol))
2138 (incf anything-digit-shortcut-count)))
2140 (defun anything-process-source--direct-insert-match (source)
2141 "[EXPERIMENTAL] Insert candidates from `anything-candidate-buffer'"
2142 (anything-log-eval (assoc-default 'name source))
2143 (let ((anything-source-name (assoc-default 'name source))
2144 content-buf)
2145 (funcall (assoc-default 'candidates source))
2146 (setq content-buf (anything-candidate-buffer))
2147 (unless (anything-empty-buffer-p content-buf)
2148 (anything-insert-header-from-source source)
2149 (insert-buffer-substring content-buf)
2150 ;; TODO call anything-put-digit-overlay-maybe with loop
2153 (defun anything-process-delayed-sources (delayed-sources)
2154 "Process delayed sources if the user is idle for
2155 `anything-idle-delay' seconds."
2156 (with-anything-quittable
2157 (anything-log-eval (mapcar (lambda (s) (assoc-default 'name s)) delayed-sources))
2158 (with-current-buffer anything-buffer
2159 (save-excursion
2160 (goto-char (point-max))
2161 (mapc 'anything-process-source delayed-sources)
2162 (when (and (not (anything-empty-buffer-p))
2163 ;; no selection yet
2164 (= (overlay-start anything-selection-overlay)
2165 (overlay-end anything-selection-overlay)))
2166 (goto-char (point-min))
2167 (anything-next-line)))
2168 (save-excursion
2169 (goto-char (point-min))
2170 (anything-log-run-hook 'anything-update-hook))
2171 (anything-maybe-fit-frame))))
2173 ;; (@* "Core: *anything* buffer contents")
2174 (defvar anything-input-local nil)
2175 (defvar anything-process-delayed-sources-timer nil)
2176 (defun anything-update ()
2177 "Update the list of matches in the anything buffer according to
2178 the current pattern."
2179 (anything-log "start update")
2180 (setq anything-digit-shortcut-count 0)
2181 (anything-kill-async-processes)
2182 (with-current-buffer (anything-buffer-get)
2183 (set (make-local-variable 'anything-input-local) anything-pattern)
2184 (erase-buffer)
2185 (when anything-enable-shortcuts
2186 (mapc 'delete-overlay anything-digit-overlays))
2187 (let (delayed-sources)
2188 (unwind-protect
2189 (setq delayed-sources
2190 (loop for source in (remove-if-not 'anything-update-source-p
2191 (anything-get-sources))
2192 if (anything-delayed-source-p source)
2193 collect source
2194 else do (anything-process-source source)))
2195 (anything-log-eval
2196 (mapcar (lambda (s) (assoc-default 'name s)) delayed-sources))
2197 (anything-update-move-first-line)
2198 (if anything-test-mode
2199 (mapc 'anything-process-source delayed-sources)
2200 (anything-maybe-fit-frame)
2201 (when delayed-sources
2202 (anything-new-timer
2203 'anything-process-delayed-sources-timer
2204 (run-with-idle-timer
2205 anything-idle-delay nil
2206 'anything-process-delayed-sources delayed-sources)))
2207 ;; FIXME I want to execute anything-after-update-hook
2208 ;; AFTER processing delayed sources
2209 (anything-log-run-hook 'anything-after-update-hook))
2210 (anything-log "end update")))))
2212 (defun anything-update-source-p (source)
2213 (and (or (not anything-source-filter)
2214 (member (assoc-default 'name source) anything-source-filter))
2215 (>= (length anything-pattern)
2216 (anything-aif (assoc 'requires-pattern source)
2217 (or (cdr it) 1)
2218 0))))
2219 (defun anything-delayed-source-p (source)
2220 (or (assoc 'delayed source)
2221 (and anything-quick-update
2222 (< (window-height (get-buffer-window (current-buffer)))
2223 (line-number-at-pos (point-max))))))
2225 (defun anything-update-move-first-line ()
2226 (goto-char (point-min))
2227 (save-excursion (anything-log-run-hook 'anything-update-hook))
2228 (anything-next-line))
2230 (defun anything-force-update ()
2231 "Recalculate and update candidates.
2232 If current source has `update' attribute, a function without argument, call it before update."
2233 (interactive)
2234 (let ((source (anything-get-current-source)))
2235 (if source
2236 (anything-force-update--reinit source)
2237 (anything-erase-message)
2238 (mapc 'anything-force-update--reinit (anything-get-sources)))
2239 (let ((selection (anything-get-selection nil t)))
2240 (anything-update)
2241 (anything-keep-selection source selection))))
2243 (defun anything-force-update--reinit (source)
2244 (anything-aif (anything-funcall-with-source source 'anything-candidate-buffer)
2245 (kill-buffer it))
2246 (dolist (attr '(update init))
2247 (anything-aif (assoc-default attr source)
2248 (anything-funcall-with-source source it)))
2249 (anything-remove-candidate-cache source))
2251 (defun anything-erase-message ()
2252 (message ""))
2254 (defun anything-keep-selection (source selection)
2255 (when (and source selection)
2256 (with-anything-window
2257 (anything-goto-source source)
2258 (forward-char -1) ;back to \n
2259 (if (search-forward (concat "\n" selection "\n") nil t)
2260 (forward-line -1)
2261 (goto-char (point-min))
2262 (forward-line 1))
2263 (anything-mark-current-line))))
2265 (defun anything-remove-candidate-cache (source)
2266 (setq anything-candidate-cache
2267 (delete (assoc (assoc-default 'name source) anything-candidate-cache)
2268 anything-candidate-cache)))
2270 (defun anything-insert-match (match insert-function source)
2271 "Insert MATCH into the anything buffer. If MATCH is a list then
2272 insert the string inteneded to appear on the display and store
2273 the real value in a text property."
2274 (let ((start (point-at-bol (point)))
2275 (string (or (car-safe match) match))
2276 (realvalue (cdr-safe match)))
2277 (when (symbolp string) (setq string (symbol-name string)))
2278 (when (stringp string)
2279 (funcall insert-function string)
2280 ;; Some sources with candidates-in-buffer have already added
2281 ;; 'anything-realvalue property when creating candidate buffer.
2282 (unless (get-text-property start 'anything-realvalue)
2283 (and realvalue
2284 (put-text-property start (point-at-eol)
2285 'anything-realvalue realvalue)))
2286 (when anything-source-in-each-line-flag
2287 (put-text-property start (point-at-eol) 'anything-source source))
2288 (funcall insert-function "\n"))))
2290 (defun anything-insert-header-from-source (source)
2291 (let ((name (assoc-default 'name source)))
2292 (anything-insert-header
2293 name
2294 (anything-aif (assoc-default 'header-name source)
2295 (anything-funcall-with-source source it name)))))
2297 (defun anything-insert-header (name &optional display-string)
2298 "Insert header of source NAME into the anything buffer."
2299 (unless (bobp)
2300 (let ((start (point)))
2301 (insert "\n")
2302 (put-text-property start (point) 'anything-header-separator t)))
2304 (let ((start (point)))
2305 (insert name)
2306 (put-text-property (point-at-bol)
2307 (point-at-eol) 'anything-header t)
2308 (when display-string
2309 (overlay-put (make-overlay (point-at-bol) (point-at-eol))
2310 'display display-string))
2311 (insert "\n")
2312 (put-text-property start (point) 'face anything-header-face)))
2315 (defun anything-insert-candidate-separator ()
2316 "Insert separator of candidates into the anything buffer."
2317 (insert anything-candidate-separator)
2318 (put-text-property (point-at-bol)
2319 (point-at-eol) 'anything-candidate-separator t)
2320 (insert "\n"))
2325 ;; (@* "Core: async process")
2326 (defun anything-output-filter (process string)
2327 "Process output from PROCESS."
2328 (anything-output-filter-1 (assoc process anything-async-processes) string))
2330 (defun anything-output-filter-1 (process-assoc string)
2331 (anything-log-eval string)
2332 (with-current-buffer anything-buffer
2333 (let ((source (cdr process-assoc)))
2334 (save-excursion
2335 (anything-aif (assoc-default 'insertion-marker source)
2336 (goto-char it)
2337 (goto-char (point-max))
2338 (anything-insert-header-from-source source)
2339 (setcdr process-assoc
2340 (append source `((insertion-marker . ,(point-marker))))))
2341 (anything-output-filter--process-source
2342 (car process-assoc) string source
2343 (anything-candidate-number-limit source))))
2344 (anything-output-filter--post-process)))
2346 (defun anything-output-filter--process-source (process string source limit)
2347 (dolist (candidate (anything-transform-candidates
2348 (anything-output-filter--collect-candidates
2349 (split-string string "\n")
2350 (assoc 'incomplete-line source))
2351 source t))
2352 (if (not (assq 'multiline source))
2353 (anything-insert-match candidate 'insert-before-markers source)
2354 (let ((start (point)))
2355 (anything-insert-candidate-separator)
2356 (anything-insert-match candidate 'insert-before-markers source)
2357 (put-text-property start (point) 'anything-multiline t)))
2358 (incf (cdr (assoc 'item-count source)))
2359 (when (>= (assoc-default 'item-count source) limit)
2360 (anything-kill-async-process process)
2361 (return))))
2363 (defun anything-output-filter--collect-candidates (lines incomplete-line-info)
2364 (anything-log-eval (cdr incomplete-line-info))
2365 (butlast
2366 (loop for line in lines collect
2367 (if (cdr incomplete-line-info)
2368 (prog1
2369 (concat (cdr incomplete-line-info) line)
2370 (setcdr incomplete-line-info nil))
2371 line)
2372 finally (setcdr incomplete-line-info line))))
2374 (defun anything-output-filter--post-process ()
2375 (anything-maybe-fit-frame)
2376 (anything-log-run-hook 'anything-update-hook)
2377 (anything-aif (get-buffer-window anything-buffer 'visible)
2378 (save-selected-window
2379 (select-window it)
2380 (anything-skip-noncandidate-line 'next)
2381 (anything-mark-current-line))))
2384 (defun anything-kill-async-processes ()
2385 "Kill all known asynchronous processes according to
2386 `anything-async-processes'."
2387 "Kill locate process."
2388 (mapc 'anything-kill-async-process (mapcar 'car anything-async-processes))
2389 (setq anything-async-processes nil))
2392 (defun anything-kill-async-process (process)
2393 "Kill PROCESS and detach the associated functions."
2394 (set-process-filter process nil)
2395 (delete-process process))
2398 ;; (@* "Core: action")
2399 (defun anything-execute-selection-action (&optional selection action preserve-saved-action)
2400 "If a candidate was selected then perform the associated
2401 action."
2402 (anything-log "executing action")
2403 (setq action (anything-get-default-action
2404 (or action
2405 anything-saved-action
2406 (if (get-buffer anything-action-buffer)
2407 (anything-get-selection anything-action-buffer)
2408 (anything-get-action)))))
2409 (let ((source (or anything-saved-current-source (anything-get-current-source))))
2410 (setq selection (or selection
2411 (anything-get-selection)
2412 (and (assoc 'accept-empty source) "")))
2413 (unless preserve-saved-action (setq anything-saved-action nil))
2414 (if (and selection action)
2415 (anything-funcall-with-source
2416 source action
2417 (anything-coerce-selection selection source)))))
2419 (defun anything-coerce-selection (selection source)
2420 "Coerce source with coerce function."
2421 (anything-aif (assoc-default 'coerce source)
2422 (anything-funcall-with-source source it selection)
2423 selection))
2425 (defun anything-get-default-action (action)
2426 (if (and (listp action) (not (functionp action)))
2427 (cdar action)
2428 action))
2430 (defun anything-select-action ()
2431 "Select an action for the currently selected candidate.
2432 If action buffer is selected, back to the anything buffer."
2433 (interactive)
2434 (cond ((get-buffer-window anything-action-buffer 'visible)
2435 (set-window-buffer (get-buffer-window anything-action-buffer)
2436 anything-buffer)
2437 (kill-buffer anything-action-buffer)
2438 (anything-set-pattern anything-input 'noupdate))
2440 (setq anything-saved-selection (anything-get-selection))
2441 (unless anything-saved-selection
2442 (error "Nothing is selected."))
2443 (setq anything-saved-current-source (anything-get-current-source))
2444 (let ((actions (anything-get-action)))
2445 (if (functionp actions)
2446 (message "Sole action: %s" actions)
2447 (anything-show-action-buffer actions)
2448 (anything-delete-minibuffer-contents)
2449 (setq anything-pattern 'dummy) ; so that it differs from the previous one
2450 (anything-check-minibuffer-input))))))
2452 (defun anything-show-action-buffer (actions)
2453 (with-current-buffer (get-buffer-create anything-action-buffer)
2454 (erase-buffer)
2455 (buffer-disable-undo)
2456 (set-window-buffer (get-buffer-window anything-buffer) anything-action-buffer)
2457 (set (make-local-variable 'anything-sources)
2458 `(((name . "Actions")
2459 (volatile)
2460 (candidates . ,actions)
2461 (candidate-number-limit))))
2462 (set (make-local-variable 'anything-source-filter) nil)
2463 (set (make-local-variable 'anything-selection-overlay) nil)
2464 (set (make-local-variable 'anything-digit-overlays) nil)
2465 (anything-initialize-overlays anything-action-buffer)))
2467 ;; (@* "Core: selection")
2468 (defun anything-move-selection-common (move-func unit direction)
2469 "Move the selection marker to a new position determined by
2470 UNIT and DIRECTION."
2471 (unless (or (anything-empty-buffer-p (anything-buffer-get))
2472 (not (anything-window)))
2473 (with-anything-window
2474 (anything-log-run-hook 'anything-move-selection-before-hook)
2475 (funcall move-func)
2476 (anything-skip-noncandidate-line direction)
2477 (anything-display-source-at-screen-top-maybe unit)
2478 (when (anything-get-previous-header-pos)
2479 (anything-mark-current-line))
2480 (anything-display-mode-line (anything-get-current-source))
2481 (anything-log-run-hook 'anything-move-selection-after-hook))))
2483 (defun anything-display-source-at-screen-top-maybe (unit)
2484 (when (and anything-display-source-at-screen-top (eq unit 'source))
2485 (set-window-start (selected-window)
2486 (save-excursion (forward-line -1) (point)))))
2488 (defun anything-skip-noncandidate-line (direction)
2489 (anything-skip-header-and-separator-line direction)
2490 (and (bobp) (forward-line 1)) ;skip first header
2491 (and (eobp) (forward-line -1)) ;avoid last empty line
2494 (defun anything-skip-header-and-separator-line (direction)
2495 (while (and (not (bobp))
2496 (or (anything-pos-header-line-p)
2497 (anything-pos-candidate-separator-p)))
2498 (forward-line (if (and (eq direction 'previous)
2499 (not (eq (point-at-bol) (point-min))))
2501 1))))
2503 (defvar anything-mode-line-string-real nil)
2504 (defun anything-display-mode-line (source)
2505 (set (make-local-variable 'anything-mode-line-string)
2506 (anything-interpret-value (or (assoc-default 'mode-line source)
2507 (default-value 'anything-mode-line-string))
2508 source))
2509 (if anything-mode-line-string
2510 (setq mode-line-format
2511 '(" " mode-line-buffer-identification " "
2512 (line-number-mode "L%l") " " (anything-follow-mode "(F) ")
2513 (:eval (anything-show-candidate-number
2514 (when (listp anything-mode-line-string)
2515 (car anything-mode-line-string))))
2516 " " anything-mode-line-string-real "-%-")
2517 anything-mode-line-string-real
2518 (substitute-command-keys (if (listp anything-mode-line-string)
2519 (cadr anything-mode-line-string)
2520 anything-mode-line-string)))
2521 (setq mode-line-format
2522 (default-value 'mode-line-format)))
2523 (setq header-line-format
2524 (anything-interpret-value (assoc-default 'header-line source) source)))
2526 (defun anything-show-candidate-number (&optional name)
2527 "Used to display candidate number in mode-line."
2528 (propertize
2529 (format "[%s %s]"
2530 (anything-approximate-candidate-number 'in-current-source)
2531 (or name "Candidate(s)"))
2532 'face 'anything-candidate-number))
2534 (defun anything-previous-line ()
2535 "Move selection to the previous line."
2536 (interactive)
2537 (anything-move-selection-common
2538 (lambda ()
2539 (if (not (anything-pos-multiline-p))
2540 (forward-line -1) ;double forward-line is meaningful
2541 (forward-line -1) ;because evaluation order is important
2542 (anything-skip-header-and-separator-line 'previous)
2543 (let ((header-pos (anything-get-previous-header-pos))
2544 (separator-pos (anything-get-previous-candidate-separator-pos)))
2545 (when header-pos
2546 (goto-char (if (or (null separator-pos) (< separator-pos header-pos))
2547 header-pos ; first candidate
2548 separator-pos))
2549 (forward-line 1)))))
2550 'line 'previous))
2552 (defun anything-next-line ()
2553 "Move selection to the next line."
2554 (interactive)
2555 (anything-move-selection-common
2556 (lambda ()
2557 (if (not (anything-pos-multiline-p))
2558 (forward-line 1)
2559 (let ((header-pos (anything-get-next-header-pos))
2560 (separator-pos (anything-get-next-candidate-separator-pos)))
2561 (cond ((and separator-pos
2562 (or (null header-pos) (< separator-pos header-pos)))
2563 (goto-char separator-pos))
2564 (header-pos
2565 (goto-char header-pos))))))
2566 'line 'next))
2568 (defun anything-previous-page ()
2569 "Move selection back with a pageful."
2570 (interactive)
2571 (anything-move-selection-common
2572 (lambda ()
2573 (condition-case nil
2574 (scroll-down)
2575 (beginning-of-buffer (goto-char (point-min)))))
2576 'page 'previous))
2578 (defun anything-next-page ()
2579 "Move selection forward with a pageful."
2580 (interactive)
2581 (anything-move-selection-common
2582 (lambda ()
2583 (condition-case nil
2584 (scroll-up)
2585 (end-of-buffer (goto-char (point-max)))))
2586 'page 'next))
2588 (defun anything-beginning-of-buffer ()
2589 "Move selection at the top."
2590 (interactive)
2591 (anything-move-selection-common (lambda () (goto-char (point-min)))
2592 'edge 'previous))
2594 (defun anything-end-of-buffer ()
2595 "Move selection at the bottom."
2596 (interactive)
2597 (anything-move-selection-common (lambda () (goto-char (point-max)))
2598 'edge 'next))
2600 (defun anything-previous-source ()
2601 "Move selection to the previous source."
2602 (interactive)
2603 (anything-move-selection-common
2604 (lambda ()
2605 (forward-line -1)
2606 (if (bobp)
2607 (goto-char (point-max))
2608 (anything-skip-header-and-separator-line 'previous))
2609 (goto-char (anything-get-previous-header-pos))
2610 (forward-line 1))
2611 'source 'previous))
2613 (defun anything-next-source ()
2614 "Move selection to the next source."
2615 (interactive)
2616 (anything-move-selection-common
2617 (lambda () (goto-char (or (anything-get-next-header-pos) (point-min))))
2618 'source 'next))
2620 (defun anything-goto-source (source-or-name)
2621 "Move the selection to the source (SOURCE-OR-NAME)."
2622 (anything-move-selection-common
2623 (lambda ()
2624 (goto-char (point-min))
2625 (let ((name (if (stringp source-or-name) source-or-name
2626 (assoc-default 'name source-or-name))))
2627 (condition-case err
2628 (while (not (string= name (anything-current-line-contents)))
2629 (goto-char (anything-get-next-header-pos)))
2630 (error (message "")))))
2631 'source 'next))
2633 (defun anything-mark-current-line ()
2634 "Move selection overlay to current line."
2635 (move-overlay
2636 anything-selection-overlay (point-at-bol)
2637 (if (anything-pos-multiline-p)
2638 (let ((header-pos (anything-get-next-header-pos))
2639 (separator-pos (anything-get-next-candidate-separator-pos)))
2640 (or (and (null header-pos) separator-pos)
2641 (and header-pos separator-pos (< separator-pos header-pos)
2642 separator-pos)
2643 header-pos
2644 (point-max)))
2645 (1+ (point-at-eol))))
2646 (anything-follow-execute-persistent-action-maybe))
2648 (defun anything-this-command-key ()
2649 (event-basic-type (elt (this-command-keys-vector) 0)))
2650 ;; (progn (read-key-sequence "Key: ") (p (anything-this-command-key)))
2652 (defun anything-select-with-shortcut-internal (types get-key-func)
2653 (if (memq anything-enable-shortcuts types)
2654 (save-selected-window
2655 (select-window (anything-window))
2656 (let* ((key (funcall get-key-func))
2657 (overlay (ignore-errors (nth (position key anything-shortcut-keys)
2658 anything-digit-overlays))))
2659 (if (not (and overlay (overlay-buffer overlay)))
2660 (when (numberp key)
2661 (select-window (minibuffer-window))
2662 (self-insert-command 1))
2663 (goto-char (overlay-start overlay))
2664 (anything-mark-current-line)
2665 (anything-exit-minibuffer))))
2666 (self-insert-command 1)))
2668 (defun anything-select-with-prefix-shortcut ()
2669 "Invoke default action with prefix shortcut."
2670 (interactive)
2671 (anything-select-with-shortcut-internal
2672 '(prefix)
2673 (lambda () (read-event "Select shortcut key: "))))
2675 (defun anything-select-with-digit-shortcut ()
2676 "Invoke default action with digit/alphabet shortcut."
2677 (interactive)
2678 (anything-select-with-shortcut-internal
2679 '(alphabet t) 'anything-this-command-key))
2681 ;; (setq anything-enable-shortcuts 'prefix)
2682 ;; (define-key anything-map "@" 'anything-select-with-prefix-shortcut)
2683 ;; (define-key anything-map (kbd "<f18>") 'anything-select-with-prefix-shortcut)
2685 (defvar anything-exit-status 0
2686 "Flag to inform whether anything have aborted or quitted.
2687 Exit with 0 mean anything exit executing an action.
2688 Exit with 1 mean anything abort with \\[keyboard-quit]
2689 It is useful for example to restore a window config if anything abort
2690 in special cases.
2691 See `anything-exit-minibuffer' and `anything-keyboard-quit'.")
2693 (defun anything-exit-minibuffer ()
2694 "Select the current candidate by exiting the minibuffer."
2695 (interactive)
2696 (declare (special anything-iswitchb-candidate-selected))
2697 (setq anything-iswitchb-candidate-selected (anything-get-selection))
2698 (setq anything-exit-status 0)
2699 (exit-minibuffer))
2701 (defun anything-keyboard-quit ()
2702 "Quit minibuffer in anything.
2704 If action buffer is displayed, kill it."
2705 (interactive)
2706 (when (get-buffer-window anything-action-buffer 'visible)
2707 (kill-buffer anything-action-buffer))
2708 (setq anything-exit-status 1)
2709 (abort-recursive-edit))
2711 (defun anything-get-next-header-pos ()
2712 "Return the position of the next header from point."
2713 (next-single-property-change (point) 'anything-header))
2716 (defun anything-get-previous-header-pos ()
2717 "Return the position of the previous header from point"
2718 (previous-single-property-change (point) 'anything-header))
2721 (defun anything-pos-multiline-p ()
2722 "Return non-nil if the current position is in the multiline source region."
2723 (get-text-property (point) 'anything-multiline))
2726 (defun anything-get-next-candidate-separator-pos ()
2727 "Return the position of the next candidate separator from point."
2728 (next-single-property-change (point) 'anything-candidate-separator))
2731 (defun anything-get-previous-candidate-separator-pos ()
2732 "Return the position of the previous candidate separator from point."
2733 (previous-single-property-change (point) 'anything-candidate-separator))
2736 (defun anything-pos-header-line-p ()
2737 "Return t if the current line is a header line."
2738 (or (get-text-property (point-at-bol) 'anything-header)
2739 (get-text-property (point-at-bol) 'anything-header-separator)))
2741 (defun anything-pos-candidate-separator-p ()
2742 "Return t if the current line is a candidate separator."
2743 (get-text-property (point-at-bol) 'anything-candidate-separator))
2745 ;; (@* "Core: help")
2746 (defun anything-help-internal (bufname insert-content-fn)
2747 "Show long message during `anything' session."
2748 (save-window-excursion
2749 (select-window (anything-window))
2750 (delete-other-windows)
2751 (switch-to-buffer (get-buffer-create bufname))
2752 (erase-buffer)
2753 (funcall insert-content-fn)
2754 (setq mode-line-format "%b (SPC,C-v:NextPage b,M-v:PrevPage other:Exit)")
2755 (setq cursor-type nil)
2756 (goto-char 1)
2757 (anything-help-event-loop)))
2759 (defun anything-help-event-loop ()
2760 (ignore-errors
2761 (loop for event = (read-event) do
2762 (case event
2763 ((?\C-v ? ) (scroll-up))
2764 ((?\M-v ?b) (scroll-down))
2765 (t (return))))))
2767 (defun anything-help ()
2768 "Help of `anything'."
2769 (interactive)
2770 (anything-help-internal
2771 " *Anything Help*"
2772 (lambda ()
2773 (insert (substitute-command-keys
2774 (anything-interpret-value anything-help-message)))
2775 (org-mode))))
2777 (defun anything-debug-output ()
2778 "Show all anything-related variables at this time."
2779 (interactive)
2780 (anything-help-internal " *Anything Debug*" 'anything-debug-output-function))
2782 (defun anything-debug-output-function (&optional vars)
2783 (message "Calculating all anything-related values...")
2784 (insert "If you debug some variables or forms, set `anything-debug-forms'
2785 to a list of forms.\n\n")
2786 (dolist (v (or vars
2787 anything-debug-forms
2788 (apropos-internal "^anything-" 'boundp)))
2789 (insert "** "
2790 (pp-to-string v) "\n"
2791 (pp-to-string (eval v)) "\n"))
2792 (message "Calculating all anything-related values...Done"))
2794 ;; (@* "Core: misc")
2795 (defun anything-kill-buffer-hook ()
2796 "Remove tick entry from `anything-tick-hash' when killing a buffer."
2797 (loop for key being the hash-keys in anything-tick-hash
2798 if (string-match (format "^%s/" (regexp-quote (buffer-name))) key)
2799 do (remhash key anything-tick-hash)))
2800 (add-hook 'kill-buffer-hook 'anything-kill-buffer-hook)
2802 (defun anything-maybe-fit-frame ()
2803 "Fit anything frame to its buffer, and put it at top right of display.
2805 It is disabled by default because some flickering occurred in some environment.
2806 To enable fitting, set both `anything-inhibit-fit-frame-flag' and
2807 `fit-frame-inhibit-fitting' to nil.
2808 You can set user options `fit-frame-max-width-percent' and
2809 `fit-frame-max-height-percent' to control max frame size."
2810 (declare (warn (unresolved 0)))
2811 (when (and (not anything-inhibit-fit-frame-flag)
2812 (anything-window)
2813 (require 'fit-frame nil t)
2814 (boundp 'fit-frame-inhibit-fitting-flag)
2815 (not fit-frame-inhibit-fitting-flag))
2816 (ignore-errors
2817 (with-anything-window
2818 (fit-frame nil nil nil t)
2819 (modify-frame-parameters
2820 (selected-frame)
2821 `((left . ,(- (x-display-pixel-width) (+ (frame-pixel-width) 7)))
2822 (top . 0))))))) ; The (top . 0) shouldn't be necessary (Emacs bug).
2824 (defun anything-preselect (candidate-or-regexp)
2825 (with-anything-window
2826 (when candidate-or-regexp
2827 (goto-char (point-min))
2828 ;; go to first candidate of first source
2829 (forward-line 1)
2830 (let ((start (point)))
2831 (unless (or (re-search-forward
2832 (concat "^" (regexp-quote candidate-or-regexp) "$")
2833 nil t)
2834 (progn (goto-char start)
2835 (re-search-forward candidate-or-regexp nil t)))
2836 (goto-char start))))
2837 (anything-mark-current-line)))
2839 (defun anything-delete-current-selection ()
2840 "Delete the currently selected item."
2841 (interactive)
2842 (with-anything-window
2843 (cond ((anything-pos-multiline-p)
2844 (anything-aif (anything-get-next-candidate-separator-pos)
2845 (delete-region (point-at-bol)
2846 (1+ (progn (goto-char it) (point-at-eol))))
2847 ;; last candidate
2848 (goto-char (anything-get-previous-candidate-separator-pos))
2849 (delete-region (point-at-bol) (point-max)))
2850 (when (anything-end-of-source-p)
2851 (goto-char (or (anything-get-previous-candidate-separator-pos)
2852 (point-min)))
2853 (forward-line 1)))
2855 (delete-region (point-at-bol) (1+ (point-at-eol)))
2856 (when (anything-end-of-source-p) (forward-line -1))))
2857 (anything-mark-current-line)))
2859 (defun anything-end-of-source-p ()
2860 "Return t if we are at eob or end of source."
2861 (save-excursion
2862 (forward-line 1)
2863 (or (eq (point-at-bol) (point-at-eol))
2864 (anything-pos-header-line-p)
2865 (eobp))))
2867 (defun anything-edit-current-selection-internal (func)
2868 (with-anything-window
2869 (beginning-of-line)
2870 (let ((realvalue (get-text-property (point) 'anything-realvalue)))
2871 (funcall func)
2872 (beginning-of-line)
2873 (and realvalue
2874 (put-text-property (point) (point-at-eol)
2875 'anything-realvalue realvalue))
2876 (anything-mark-current-line))))
2878 (defmacro anything-edit-current-selection (&rest forms)
2879 "Evaluate FORMS at current selection in the anything buffer.
2880 You can edit the line."
2881 `(anything-edit-current-selection-internal
2882 (lambda () ,@forms)))
2883 (put 'anything-edit-current-selection 'lisp-indent-function 0)
2885 (defun anything-set-pattern (pattern &optional noupdate)
2886 "Set minibuffer contents to PATTERN.
2887 if optional NOUPDATE is non-nil, anything buffer is not changed."
2888 (with-selected-window (minibuffer-window)
2889 (delete-minibuffer-contents)
2890 (insert pattern))
2891 (when noupdate
2892 (setq anything-pattern pattern)
2893 (anything-hooks 'cleanup)
2894 (run-with-idle-timer 0 nil 'anything-hooks 'setup)))
2896 (defun anything-delete-minibuffer-contents ()
2897 "Same as `delete-minibuffer-contents' but this is a command."
2898 (interactive)
2899 (anything-set-pattern ""))
2900 (defalias 'anything-delete-minibuffer-content 'anything-delete-minibuffer-contents)
2902 ;; (@* "Built-in plug-in: type")
2903 (defun anything-compile-source--type (source)
2904 (anything-aif (assoc-default 'type source)
2905 (append source (assoc-default it anything-type-attributes) nil)
2906 source))
2908 ;; `define-anything-type-attribute' is public API.
2910 (defun anything-add-type-attribute (type definition)
2911 (anything-aif (assq type anything-type-attributes)
2912 (setq anything-type-attributes (delete it anything-type-attributes)))
2913 (push (cons type definition) anything-type-attributes))
2915 (defvar anything-types nil)
2916 (defun anything-document-type-attribute (type doc)
2917 (add-to-list 'anything-types type t)
2918 (put type 'anything-typeattrdoc
2919 (concat "- " (symbol-name type) "\n\n" doc "\n")))
2921 (defadvice documentation-property (after anything-document-type-attribute activate)
2922 "Hack to display type attributes' documentation as `anything-type-attributes' docstring."
2923 (when (eq (ad-get-arg 0) 'anything-type-attributes)
2924 (setq ad-return-value
2925 (concat ad-return-value "\n\n++++ Types currently defined ++++\n"
2926 (mapconcat (lambda (sym) (get sym 'anything-typeattrdoc))
2927 anything-types "\n")))))
2929 ;; (@* "Built-in plug-in: dummy")
2930 (defun anything-dummy-candidate (candidate source)
2931 ;; `source' is defined in filtered-candidate-transformer
2932 (list anything-pattern))
2934 (defun anything-compile-source--dummy (source)
2935 (if (assoc 'dummy source)
2936 (append source
2937 '((candidates "dummy")
2938 (accept-empty)
2939 (match identity)
2940 (filtered-candidate-transformer . anything-dummy-candidate)
2941 (disable-shortcuts)
2942 (volatile)))
2943 source))
2945 ;; (@* "Built-in plug-in: disable-shortcuts")
2946 (defvar anything-orig-enable-shortcuts nil)
2947 (defun anything-save-enable-shortcuts ()
2948 (anything-once
2949 (lambda () (setq anything-orig-enable-shortcuts anything-enable-shortcuts
2950 anything-enable-shortcuts nil))))
2951 (defun anything-compile-source--disable-shortcuts (source)
2952 (if (assoc 'disable-shortcuts source)
2953 (append `((init ,@(anything-mklist (assoc-default 'init source))
2954 anything-save-enable-shortcuts)
2955 (resume ,@(anything-mklist (assoc-default 'resume source))
2956 anything-save-enable-shortcuts)
2957 (cleanup ,@(anything-mklist (assoc-default 'cleanup source))
2958 (lambda () (setq anything-enable-shortcuts
2959 anything-orig-enable-shortcuts))))
2960 source)
2961 source))
2963 ;; (@* "Built-in plug-in: candidates-in-buffer")
2964 (defun anything-candidates-in-buffer ()
2965 "Get candidates from the candidates buffer according to `anything-pattern'.
2967 BUFFER is `anything-candidate-buffer' by default. Each
2968 candidate must be placed in one line. This function is meant to
2969 be used in candidates-in-buffer or candidates attribute of an
2970 anything source. Especially fast for many (1000+) candidates.
2973 '((name . \"many files\")
2974 (init . (lambda () (with-current-buffer (anything-candidate-buffer 'local)
2975 (insert-many-filenames))))
2976 (search re-search-forward) ; optional
2977 (candidates-in-buffer)
2978 (type . file))
2980 +===============================================================+
2981 | The new way of making and narrowing candidates: Using buffers |
2982 +===============================================================+
2984 By default, `anything' makes candidates by evaluating the
2985 candidates function, then narrows them by `string-match' for each
2986 candidate.
2988 But this way is very slow for many candidates. The new way is
2989 storing all candidates in a buffer and narrowing them by
2990 `re-search-forward'. Search function is customizable by search
2991 attribute. The important point is that buffer processing is MUCH
2992 FASTER than string list processing and is the Emacs way.
2994 The init function writes all candidates to a newly-created
2995 candidate buffer. The candidates buffer is created or specified
2996 by `anything-candidate-buffer'. Candidates are stored in a line.
2998 The candidates function narrows all candidates, IOW creates a
2999 subset of candidates dynamically. It is the task of
3000 `anything-candidates-in-buffer'. As long as
3001 `anything-candidate-buffer' is used,`(candidates-in-buffer)' is
3002 sufficient in most cases.
3004 Note that `(candidates-in-buffer)' is shortcut of three attributes:
3005 (candidates . anything-candidates-in-buffer)
3006 (volatile)
3007 (match identity)
3008 And `(candidates-in-buffer . func)' is shortcut of three attributes:
3009 (candidates . func)
3010 (volatile)
3011 (match identity)
3012 The expansion is performed in `anything-get-sources'.
3014 The candidates-in-buffer attribute implies the volatile attribute.
3015 The volatile attribute is needed because `anything-candidates-in-buffer'
3016 creates candidates dynamically and need to be called everytime
3017 `anything-pattern' changes.
3019 Because `anything-candidates-in-buffer' plays the role of `match' attribute
3020 function, specifying `(match identity)' makes the source slightly faster.
3022 To customize `anything-candidates-in-buffer' behavior, use search,
3023 get-line and search-from-end attributes. See also `anything-sources' docstring.
3025 (declare (special source))
3026 (anything-candidates-in-buffer-1 (anything-candidate-buffer)
3027 anything-pattern
3028 (or (assoc-default 'get-line source)
3029 #'buffer-substring-no-properties)
3030 ;; use external variable `source'.
3031 (or (assoc-default 'search source)
3032 (if (assoc 'search-from-end source)
3033 '(re-search-backward)
3034 '(re-search-forward)))
3035 (anything-candidate-number-limit source)
3036 (assoc 'search-from-end source)))
3038 (defun anything-candidates-in-buffer-1 (buffer pattern get-line-fn search-fns limit search-from-end)
3039 ;; buffer == nil when candidates buffer does not exist.
3040 (when buffer
3041 (with-current-buffer buffer
3042 (let ((start-point (if search-from-end (point-max) (point-min)))
3043 (endp (if search-from-end #'bobp #'eobp)))
3044 (goto-char (1- start-point))
3045 (if (string= pattern "")
3046 (anything-initial-candidates-from-candidate-buffer
3047 endp get-line-fn limit search-from-end)
3048 (anything-search-from-candidate-buffer
3049 pattern get-line-fn search-fns limit search-from-end
3050 start-point endp))))))
3052 (defun anything-point-is-moved (proc)
3053 "If point is moved after executing PROC, return t, otherwise nil."
3054 (/= (point) (progn (funcall proc) (point))))
3056 (defun anything-search-from-candidate-buffer (pattern get-line-fn search-fns
3057 limit search-from-end
3058 start-point endp)
3059 (let (buffer-read-only
3060 matches exit newmatches)
3061 (anything-search-from-candidate-buffer-internal
3062 (lambda ()
3063 (clrhash anything-cib-hash)
3064 (dolist (searcher search-fns)
3065 (goto-char start-point)
3066 (setq newmatches nil)
3067 (loop with item-count = 0
3068 while (funcall searcher pattern nil t)
3069 for cand = (funcall get-line-fn (point-at-bol) (point-at-eol))
3070 do (anything-accumulate-candidates-internal
3071 cand newmatches anything-cib-hash item-count limit)
3072 unless (anything-point-is-moved
3073 (lambda ()
3074 (if search-from-end
3075 (goto-char (1- (point-at-bol)))
3076 (forward-line 1))))
3077 return nil)
3078 (setq matches (append matches (nreverse newmatches)))
3079 (if exit (return)))
3080 (delq nil matches)))))
3082 (defun anything-initial-candidates-from-candidate-buffer (endp get-line-fn limit search-from-end)
3083 (delq nil (loop with next-line-fn =
3084 (if search-from-end
3085 (lambda (x) (goto-char (max (1- (point-at-bol)) 1)))
3086 #'forward-line)
3087 until (funcall endp)
3088 for i from 1 to limit
3089 collect (funcall get-line-fn (point-at-bol) (point-at-eol))
3090 do (funcall next-line-fn 1))))
3092 (defun anything-search-from-candidate-buffer-internal (search-fn)
3093 (goto-char (point-min))
3094 (insert "\n")
3095 (goto-char (point-max))
3096 (insert "\n")
3097 (unwind-protect
3098 (funcall search-fn)
3099 (goto-char (point-min))
3100 (delete-char 1)
3101 (goto-char (1- (point-max)))
3102 (delete-char 1)
3104 (set-buffer-modified-p nil)))
3106 (defun anything-candidate-buffer (&optional create-or-buffer)
3107 "Register and return a buffer containing candidates of current source.
3108 `anything-candidate-buffer' searches buffer-local candidates buffer first,
3109 then global candidates buffer.
3111 Acceptable values of CREATE-OR-BUFFER:
3113 - nil (omit)
3114 Only return the candidates buffer.
3115 - a buffer
3116 Register a buffer as a candidates buffer.
3117 - 'global
3118 Create a new global candidates buffer,
3119 named \" *anything candidates:SOURCE*\".
3120 - other non-nil value
3121 Create a new local candidates buffer,
3122 named \" *anything candidates:SOURCE*ANYTHING-CURRENT-BUFFER\".
3124 (let* ((global-bname (format " *anything candidates:%s*" anything-source-name))
3125 (local-bname (format " *anything candidates:%s*%s"
3126 anything-source-name
3127 (buffer-name anything-current-buffer)))
3128 (register-func
3129 (lambda ()
3130 (setq anything-candidate-buffer-alist
3131 (cons (cons anything-source-name create-or-buffer)
3132 (delete (assoc anything-source-name
3133 anything-candidate-buffer-alist)
3134 anything-candidate-buffer-alist)))))
3135 (kill-buffers-func
3136 (lambda ()
3137 (loop for b in (buffer-list)
3138 if (string-match (format "^%s" (regexp-quote global-bname))
3139 (buffer-name b))
3140 do (kill-buffer b))))
3141 (create-func
3142 (lambda ()
3143 (with-current-buffer
3144 (get-buffer-create (if (eq create-or-buffer 'global)
3145 global-bname
3146 local-bname))
3147 (buffer-disable-undo)
3148 (erase-buffer)
3149 (font-lock-mode -1))))
3150 (return-func
3151 (lambda ()
3152 (or (get-buffer local-bname)
3153 (get-buffer global-bname)
3154 (anything-aif (assoc-default anything-source-name
3155 anything-candidate-buffer-alist)
3156 (and (buffer-live-p it) it))))))
3157 (when create-or-buffer
3158 (funcall register-func)
3159 (unless (bufferp create-or-buffer)
3160 (and (eq create-or-buffer 'global) (funcall kill-buffers-func))
3161 (funcall create-func)))
3162 (funcall return-func)))
3164 (defun anything-compile-source--candidates-in-buffer (source)
3165 (anything-aif (assoc 'candidates-in-buffer source)
3166 (append source
3167 `((candidates . ,(or (cdr it) 'anything-candidates-in-buffer))
3168 (volatile) (match identity)))
3169 source))
3171 ;; (@* "Utility: resplit anything window")
3172 (defun anything-toggle-resplit-window ()
3173 "Toggle resplit anything window, vertically or horizontally."
3174 (interactive)
3175 (with-anything-window
3176 (let ((before-height (window-height)))
3177 (delete-window)
3178 (set-window-buffer
3179 (select-window (if (= (window-height) before-height)
3180 (split-window-vertically)
3181 (split-window-horizontally)))
3182 anything-buffer))))
3184 ;; (@* "Utility: select another action by key")
3185 (defun anything-select-nth-action (n)
3186 "Select the nth action for the currently selected candidate."
3187 (setq anything-saved-selection (anything-get-selection))
3188 (unless anything-saved-selection
3189 (error "Nothing is selected."))
3190 (setq anything-saved-action (anything-get-nth-action n (anything-get-action)))
3191 (anything-exit-minibuffer))
3193 (defun anything-get-nth-action (n action)
3194 (cond ((and (zerop n) (functionp action))
3195 action)
3196 ((listp action)
3197 (or (cdr (elt action n))
3198 (error "No such action")))
3199 ((and (functionp action) (< 0 n))
3200 (error "Sole action."))
3202 (error "Error in `anything-select-nth-action'."))))
3204 (defun anything-select-2nd-action ()
3205 "Select the 2nd action for the currently selected candidate."
3206 (interactive)
3207 (anything-select-nth-action 1))
3209 (defun anything-select-3rd-action ()
3210 "Select the 3rd action for the currently selected candidate."
3211 (interactive)
3212 (anything-select-nth-action 2))
3214 (defun anything-select-4th-action ()
3215 "Select the 4th action for the currently selected candidate."
3216 (interactive)
3217 (anything-select-nth-action 3))
3219 (defun anything-select-2nd-action-or-end-of-line ()
3220 "Select the 2nd action for the currently selected candidate if the point is at the end of minibuffer.
3221 Otherwise goto the end of minibuffer."
3222 (interactive)
3223 (if (eolp)
3224 (anything-select-nth-action 1)
3225 (end-of-line)))
3227 ;; (@* "Utility: Persistent Action")
3228 (defmacro with-anything-display-same-window (&rest body)
3229 "Make `pop-to-buffer' and `display-buffer' display in the same window."
3230 `(let ((display-buffer-function 'anything-persistent-action-display-buffer))
3231 ,@body))
3232 (put 'with-anything-display-same-window 'lisp-indent-function 0)
3234 (defvar anything-persistent-action-display-window nil)
3235 (defun anything-initialize-persistent-action ()
3236 (set (make-local-variable 'anything-persistent-action-display-window) nil))
3238 (defun* anything-execute-persistent-action (&optional (attr 'persistent-action))
3239 "If a candidate is selected then perform the associated action without quitting anything."
3240 (interactive)
3241 (anything-log "executing persistent-action")
3242 (with-anything-window
3243 (save-selected-window
3244 (anything-select-persistent-action-window)
3245 (anything-log-eval (current-buffer))
3246 (let ((anything-in-persistent-action t))
3247 (with-anything-display-same-window
3248 (anything-execute-selection-action
3250 (or (assoc-default attr (anything-get-current-source))
3251 (anything-get-action))
3253 (anything-log-run-hook 'anything-after-persistent-action-hook))))))
3255 (defun anything-persistent-action-display-window ()
3256 (with-anything-window
3257 (setq anything-persistent-action-display-window
3258 (cond ((window-live-p anything-persistent-action-display-window)
3259 anything-persistent-action-display-window)
3260 ((and anything-samewindow (one-window-p t))
3261 (split-window))
3262 ((get-buffer-window anything-current-buffer))
3264 (next-window (selected-window) 1))))))
3266 (defun anything-select-persistent-action-window ()
3267 (select-window (get-buffer-window (anything-buffer-get)))
3268 (select-window
3269 (setq minibuffer-scroll-window (anything-persistent-action-display-window))))
3271 (defun anything-persistent-action-display-buffer (buf &optional not-this-window)
3272 "Make `pop-to-buffer' and `display-buffer' display in the same window in persistent action.
3273 If `anything-persistent-action-use-special-display' is non-nil and
3274 BUF is to be displayed by `special-display-function', use it.
3275 Otherwise ignores `special-display-buffer-names' and `special-display-regexps'."
3276 (let* ((name (buffer-name buf))
3277 display-buffer-function pop-up-windows pop-up-frames
3278 (same-window-regexps
3279 (unless (and anything-persistent-action-use-special-display
3280 (or (member name
3281 (mapcar (lambda (x) (or (car-safe x) x))
3282 special-display-buffer-names))
3283 (remove-if-not
3284 (lambda (x) (string-match (or (car-safe x) x) name))
3285 special-display-regexps)))
3286 '("."))))
3287 (display-buffer buf not-this-window)))
3289 ;; scroll-other-window(-down)? for persistent-action
3290 (defun anything-scroll-other-window-base (command)
3291 (with-selected-window (anything-persistent-action-display-window)
3292 (funcall command anything-scroll-amount)))
3294 (defun anything-scroll-other-window ()
3295 "Scroll other window (not *Anything* window) upward."
3296 (interactive)
3297 (anything-scroll-other-window-base 'scroll-up))
3298 (defun anything-scroll-other-window-down ()
3299 "Scroll other window (not *Anything* window) downward."
3300 (interactive)
3301 (anything-scroll-other-window-base 'scroll-down))
3303 ;; (@* "Utility: Visible Mark")
3304 (defface anything-visible-mark
3305 '((((min-colors 88) (background dark))
3306 (:background "green1" :foreground "black"))
3307 (((background dark)) (:background "green" :foreground "black"))
3308 (((min-colors 88)) (:background "green1"))
3309 (t (:background "green")))
3310 "Face for visible mark."
3311 :group 'anything)
3312 (defvar anything-visible-mark-face 'anything-visible-mark)
3313 (defvar anything-visible-mark-overlays nil)
3315 (defun anything-clear-visible-mark ()
3316 (with-current-buffer (anything-buffer-get)
3317 (mapc 'delete-overlay anything-visible-mark-overlays)
3318 (set (make-local-variable 'anything-visible-mark-overlays) nil)))
3319 (add-hook 'anything-after-initialize-hook 'anything-clear-visible-mark)
3321 (defvar anything-c-marked-candidate-list nil
3322 "[OBSOLETE] DO NOT USE!!")
3323 (defvar anything-marked-candidates nil
3324 "Marked candadates. List of (source . real) pair.")
3326 (defun anything-this-visible-mark ()
3327 (loop for o in anything-visible-mark-overlays
3328 when (equal (point-at-bol) (overlay-start o))
3329 do (return o)))
3331 (defun anything-delete-visible-mark (overlay)
3332 (setq anything-c-marked-candidate-list
3333 (remove (anything-current-line-contents) anything-c-marked-candidate-list))
3334 (setq anything-marked-candidates
3335 (remove
3336 (cons (anything-get-current-source) (anything-get-selection))
3337 anything-marked-candidates))
3338 (delete-overlay overlay)
3339 (setq anything-visible-mark-overlays
3340 (delq overlay anything-visible-mark-overlays)))
3342 (defun anything-make-visible-mark ()
3343 (let ((o (make-overlay (point-at-bol) (1+ (point-at-eol)))))
3344 (overlay-put o 'face anything-visible-mark-face)
3345 (overlay-put o 'source (assoc-default 'name (anything-get-current-source)))
3346 (overlay-put o 'string (buffer-substring (overlay-start o) (overlay-end o)))
3347 (add-to-list 'anything-visible-mark-overlays o))
3348 (push (anything-current-line-contents) anything-c-marked-candidate-list)
3349 (push (cons (anything-get-current-source) (anything-get-selection))
3350 anything-marked-candidates))
3352 (defun anything-toggle-visible-mark ()
3353 "Toggle anything visible mark at point."
3354 (interactive)
3355 (with-anything-window
3356 (anything-aif (anything-this-visible-mark)
3357 (anything-delete-visible-mark it)
3358 (anything-make-visible-mark))
3359 (anything-next-line)))
3361 (defun anything-display-all-visible-marks ()
3362 "Show all `anything' visible marks strings."
3363 (interactive)
3364 (with-anything-window
3365 (lexical-let ((overlays (reverse anything-visible-mark-overlays)))
3366 (anything-run-after-quit
3367 (lambda ()
3368 (with-output-to-temp-buffer "*anything visible marks*"
3369 (dolist (o overlays) (princ (overlay-get o 'string)))))))))
3371 (defun anything-marked-candidates ()
3372 "Marked candidates (real value) of current source if any,
3373 otherwise 1-element list of current selection.
3375 It is analogous to `dired-get-marked-files'."
3376 (with-current-buffer (anything-buffer-get)
3377 (let ((cands
3378 (if anything-marked-candidates
3379 (loop with current-src = (anything-get-current-source)
3380 for (source . real) in (reverse anything-marked-candidates)
3381 when (equal current-src source)
3382 collect (anything-coerce-selection real source))
3383 (list (anything-get-selection)))))
3384 (anything-log-eval cands)
3385 cands)))
3387 (defun anything-reset-marked-candidates ()
3388 (with-current-buffer (anything-buffer-get)
3389 (set (make-local-variable 'anything-c-marked-candidate-list) nil)
3390 (set (make-local-variable 'anything-marked-candidates) nil)))
3392 (add-hook 'anything-after-initialize-hook 'anything-reset-marked-candidates)
3393 ;; (add-hook 'anything-after-action-hook 'anything-reset-marked-candidates)
3395 (defun anything-current-source-name= (name)
3396 (save-excursion
3397 (goto-char (anything-get-previous-header-pos))
3398 (equal name (anything-current-line-contents))))
3400 (defun anything-revive-visible-mark ()
3401 (with-current-buffer anything-buffer
3402 (dolist (o anything-visible-mark-overlays)
3403 (goto-char (point-min))
3404 (while (and (search-forward (overlay-get o 'string) nil t)
3405 (anything-current-source-name= (overlay-get o 'source)))
3406 ;; Now the next line of visible mark
3407 (move-overlay o (point-at-bol 0) (1+ (point-at-eol 0)))))))
3408 (add-hook 'anything-update-hook 'anything-revive-visible-mark)
3410 (defun anything-next-point-in-list (curpos points &optional prev)
3411 (cond
3412 ;; rule out special cases
3413 ((null points) curpos)
3414 ((and prev (< curpos (car points))) curpos)
3415 ((< (car (last points)) curpos)
3416 (if prev (car (last points)) curpos))
3418 (nth (if prev
3419 (loop for pt in points
3420 for i from 0
3421 if (<= curpos pt)
3422 do (return (1- i)))
3423 (loop for pt in points
3424 for i from 0
3425 if (< curpos pt)
3426 do (return i)))
3427 points))))
3429 (defun anything-next-visible-mark (&optional prev)
3430 "Move next anything visible mark."
3431 (interactive)
3432 (with-anything-window
3433 (goto-char (anything-next-point-in-list
3434 (point)
3435 (sort (mapcar 'overlay-start anything-visible-mark-overlays) '<)
3436 prev))
3437 (anything-mark-current-line)))
3439 (defun anything-prev-visible-mark ()
3440 "Move previous anything visible mark."
3441 (interactive)
3442 (anything-next-visible-mark t))
3444 ;; (@* "Utility: `find-file' integration")
3445 (defun anything-quit-and-find-file ()
3446 "Drop into `find-file' from `anything' like `iswitchb-find-file'.
3447 If current selection is a buffer or a file, `find-file' from its directory."
3448 (interactive)
3449 (anything-run-after-quit
3450 (lambda (f)
3451 (if (file-exists-p f)
3452 (let ((default-directory (file-name-directory f)))
3453 (call-interactively 'find-file))
3454 (call-interactively 'find-file)))
3455 (anything-aif (get-buffer (anything-get-selection))
3456 (buffer-file-name it)
3457 (expand-file-name (anything-get-selection)))))
3459 ;; (@* "Utility: Selection Paste")
3460 (defun anything-yank-selection ()
3461 "Set minibuffer contents to current selection."
3462 (interactive)
3463 (anything-set-pattern (anything-get-selection nil t)))
3465 (defun anything-kill-selection-and-quit ()
3466 "Store current selection to kill ring.
3467 You can paste it by typing C-y."
3468 (interactive)
3469 (anything-run-after-quit
3470 (lambda (sel)
3471 (kill-new sel)
3472 (message "Killed: %s" sel))
3473 (anything-get-selection nil t)))
3476 ;; (@* "Utility: Automatical execution of persistent-action")
3477 (add-to-list 'minor-mode-alist '(anything-follow-mode " AFollow"))
3478 (defun anything-follow-mode ()
3479 "If this mode is on, persistent action is executed everytime the cursor is moved."
3480 (interactive)
3481 (with-current-buffer anything-buffer
3482 (setq anything-follow-mode (not anything-follow-mode))
3483 (message "anything-follow-mode is %s"
3484 (if anything-follow-mode "enabled" "disabled"))))
3486 (defun anything-follow-execute-persistent-action-maybe ()
3487 "Execute persistent action after `anything-input-idle-delay' secs when `anything-follow-mode' is enabled."
3488 (and (not (get-buffer-window anything-action-buffer 'visible))
3489 (buffer-local-value 'anything-follow-mode
3490 (get-buffer-create anything-buffer))
3491 (sit-for anything-input-idle-delay)
3492 (anything-window)
3493 (anything-get-selection)
3494 (save-excursion
3495 (anything-execute-persistent-action))))
3497 ;; (@* "Utility: Migrate `anything-sources' to my-anything command")
3498 (defun anything-migrate-sources ()
3499 "Help to migrate to new `anything' way."
3500 (interactive)
3501 (with-current-buffer (get-buffer-create "*anything migrate*")
3502 (erase-buffer)
3503 (insert (format "\
3504 Setting `anything-sources' directly is not good because
3505 `anything' is not for one command. For now, interactive use of
3506 `anything' (M-x anything) is only for demonstration purpose.
3507 So you should define commands calling `anything'.
3508 I help you to migrate to the new way.
3510 The code below is automatically generated from current
3511 `anything-sources' value. You can use the `my-anything' command
3512 now!
3514 Copy and paste it to your .emacs. Then substitute `my-anything'
3515 for `anything' bindings in all `define-key', `local-set-key' and
3516 `global-set-key' calls.
3518 \(defun my-anything ()
3519 \"Anything command for you.
3521 It is automatically generated by `anything-migrate-sources'.\"
3522 (interactive)
3523 (anything-other-buffer
3525 \"*my-anything*\"))
3526 " anything-sources))
3527 (eval-last-sexp nil)
3528 (substitute-key-definition 'anything 'my-anything global-map)
3529 (pop-to-buffer (current-buffer))))
3531 ;; (@* "Utility: Incremental search within results (unmaintained)")
3533 (defvar anything-isearch-original-global-map nil
3534 "Original global map before Anything isearch is started.")
3536 (defvar anything-isearch-original-message-timeout nil
3537 "Original message timeout before Anything isearch is started.")
3539 (defvar anything-isearch-pattern nil
3540 "The current isearch pattern.")
3542 (defvar anything-isearch-message-suffix ""
3543 "Message suffix indicating the current state of the search.")
3545 (defvar anything-isearch-original-point nil
3546 "Original position of point before isearch is started.")
3548 (defvar anything-isearch-original-window nil
3549 "Original selected window before isearch is started.")
3551 (defvar anything-isearch-original-cursor-in-non-selected-windows nil
3552 "Original value of cursor-in-non-selected-windows before isearch is started.")
3554 (defvar anything-isearch-original-deferred-action-list nil
3555 "Original value of deferred-action-list before isearch is started.")
3557 (defvar anything-isearch-match-positions nil
3558 "Stack of positions of matches or non-matches.
3560 It's a list of plists with two properties: `event', the last user
3561 event, `start', the start position of the current match, and
3562 `pos', the position of point after that event.
3564 The value of `event' can be the following symbols: `char' if a
3565 character was typed, `error' if a non-matching character was
3566 typed, `search' if a forward search had to be done after a
3567 character, and `search-again' if a search was done for the next
3568 occurrence of the current pattern.")
3570 (defvar anything-isearch-match-start nil
3571 "Start position of the current match.")
3574 (defun anything-isearch ()
3575 "Start incremental search within results. (UNMAINTAINED)"
3576 (interactive)
3577 (if (anything-empty-buffer-p (anything-buffer-get))
3578 (message "There are no results.")
3580 (setq anything-isearch-original-message-timeout minibuffer-message-timeout)
3581 (setq minibuffer-message-timeout nil)
3583 (setq anything-isearch-original-global-map global-map)
3585 (condition-case nil
3586 (progn
3587 (setq anything-isearch-original-window (selected-window))
3588 (select-window (anything-window))
3589 (setq cursor-type t)
3591 (setq anything-isearch-original-deferred-action-list
3592 (default-value 'deferred-action-list))
3593 (setq-default deferred-action-list nil)
3594 (add-hook 'deferred-action-list 'anything-isearch-post-command)
3596 (use-global-map anything-isearch-map)
3597 (setq overriding-terminal-local-map anything-isearch-map)
3599 (setq anything-isearch-pattern "")
3601 (setq anything-isearch-original-cursor-in-non-selected-windows
3602 cursor-in-non-selected-windows)
3603 (setq cursor-in-non-selected-windows nil)
3605 (setq anything-isearch-original-point (point-marker))
3606 (goto-char (point-min))
3607 (forward-line)
3608 (anything-mark-current-line)
3610 (setq anything-isearch-match-positions nil)
3611 (setq anything-isearch-match-start (point-marker))
3613 (if anything-isearch-overlay
3614 ;; make sure the overlay belongs to the anything buffer
3615 (move-overlay anything-isearch-overlay (point-min) (point-min)
3616 (get-buffer (anything-buffer-get)))
3618 (setq anything-isearch-overlay (make-overlay (point-min) (point-min)))
3619 (overlay-put anything-isearch-overlay 'face anything-isearch-match-face))
3621 (setq anything-isearch-message-suffix
3622 (substitute-command-keys "cancel with \\[anything-isearch-cancel]")))
3624 (error (anything-isearch-cleanup)))))
3627 (defun anything-isearch-post-command ()
3628 "Print the current pattern after every command."
3629 (anything-isearch-message)
3630 (when (anything-window)
3631 (with-anything-window
3632 (move-overlay anything-isearch-overlay anything-isearch-match-start (point)
3633 (get-buffer (anything-buffer-get))))))
3636 (defun anything-isearch-printing-char ()
3637 "Add printing char to the pattern."
3638 (interactive)
3639 (let ((char (char-to-string last-command-event)))
3640 (setq anything-isearch-pattern (concat anything-isearch-pattern char))
3642 (with-anything-window
3643 (if (looking-at char)
3644 (progn
3645 (push (list 'event 'char
3646 'start anything-isearch-match-start
3647 'pos (point-marker))
3648 anything-isearch-match-positions)
3649 (forward-char))
3651 (let ((start (point)))
3652 (while (and (re-search-forward anything-isearch-pattern nil t)
3653 (anything-pos-header-line-p)))
3654 (if (or (anything-pos-header-line-p)
3655 (eq start (point)))
3656 (progn
3657 (goto-char start)
3658 (push (list 'event 'error
3659 'start anything-isearch-match-start
3660 'pos (point-marker))
3661 anything-isearch-match-positions))
3663 (push (list 'event 'search
3664 'start anything-isearch-match-start
3665 'pos (copy-marker start))
3666 anything-isearch-match-positions)
3667 (setq anything-isearch-match-start
3668 (copy-marker (match-beginning 0))))))
3670 (anything-mark-current-line))))
3673 (defun anything-isearch-again ()
3674 "Search again for the current pattern"
3675 (interactive)
3676 (if (equal anything-isearch-pattern "")
3677 (setq anything-isearch-message-suffix "no pattern yet")
3679 (with-anything-window
3680 (let ((start (point)))
3681 (while (and (re-search-forward anything-isearch-pattern nil t)
3682 (anything-pos-header-line-p)))
3683 (if (or (anything-pos-header-line-p)
3684 (eq start (point)))
3685 (progn
3686 (goto-char start)
3687 (unless (eq 'error
3688 (plist-get (car anything-isearch-match-positions)
3689 'event))
3690 (setq anything-isearch-message-suffix "no more matches")))
3692 (push (list 'event 'search-again
3693 'start anything-isearch-match-start
3694 'pos (copy-marker start))
3695 anything-isearch-match-positions)
3696 (setq anything-isearch-match-start (copy-marker (match-beginning 0)))
3698 (anything-mark-current-line))))))
3701 (defun anything-isearch-delete ()
3702 "Undo last event."
3703 (interactive)
3704 (unless (equal anything-isearch-pattern "")
3705 (let ((last (pop anything-isearch-match-positions)))
3706 (unless (eq 'search-again (plist-get last 'event))
3707 (setq anything-isearch-pattern
3708 (substring anything-isearch-pattern 0 -1)))
3710 (with-anything-window
3711 (goto-char (plist-get last 'pos))
3712 (setq anything-isearch-match-start (plist-get last 'start))
3713 (anything-mark-current-line)))))
3716 (defun anything-isearch-default-action ()
3717 "Execute the default action for the selected candidate."
3718 (interactive)
3719 (anything-isearch-cleanup)
3720 (with-current-buffer (anything-buffer-get) (anything-exit-minibuffer)))
3723 (defun anything-isearch-select-action ()
3724 "Choose an action for the selected candidate."
3725 (interactive)
3726 (anything-isearch-cleanup)
3727 (with-anything-window
3728 (anything-select-action)))
3731 (defun anything-isearch-cancel ()
3732 "Cancel Anything isearch."
3733 (interactive)
3734 (anything-isearch-cleanup)
3735 (when (anything-window)
3736 (with-anything-window
3737 (goto-char anything-isearch-original-point)
3738 (anything-mark-current-line))))
3741 (defun anything-isearch-cleanup ()
3742 "Clean up the mess."
3743 (setq minibuffer-message-timeout anything-isearch-original-message-timeout)
3744 (with-current-buffer (anything-buffer-get)
3745 (setq overriding-terminal-local-map nil)
3746 (setq cursor-type nil)
3747 (setq cursor-in-non-selected-windows
3748 anything-isearch-original-cursor-in-non-selected-windows))
3749 (when anything-isearch-original-window
3750 (select-window anything-isearch-original-window))
3752 (use-global-map anything-isearch-original-global-map)
3753 (setq-default deferred-action-list
3754 anything-isearch-original-deferred-action-list)
3755 (when (overlayp anything-isearch-overlay)
3756 (delete-overlay anything-isearch-overlay)))
3759 (defun anything-isearch-message ()
3760 "Print prompt."
3761 (if (and (equal anything-isearch-message-suffix "")
3762 (eq (plist-get (car anything-isearch-match-positions) 'event)
3763 'error))
3764 (setq anything-isearch-message-suffix "failing"))
3766 (unless (equal anything-isearch-message-suffix "")
3767 (setq anything-isearch-message-suffix
3768 (concat " [" anything-isearch-message-suffix "]")))
3770 (message (concat "Search within results: "
3771 anything-isearch-pattern
3772 anything-isearch-message-suffix))
3774 (setq anything-isearch-message-suffix ""))
3777 ;; (@* "Utility: Iswitchb integration (unmaintained)")
3779 (defvar anything-iswitchb-candidate-selected nil
3780 "Indicates whether an anything candidate is selected from iswitchb.")
3782 (defvar anything-iswitchb-frame-configuration nil
3783 "Saved frame configuration, before anything buffer was displayed.")
3785 (defvar anything-iswitchb-saved-keys nil
3786 "The original in iswitchb before binding anything keys.")
3789 (defun anything-iswitchb-setup ()
3790 "Integrate anything completion into iswitchb (UNMAINTAINED).
3792 If the user is idle for `anything-iswitchb-idle-delay' seconds
3793 after typing something into iswitchb then anything candidates are
3794 shown for the current iswitchb input.
3796 ESC cancels anything completion and returns to normal iswitchb.
3798 Some key bindings in `anything-map' are modified.
3799 See also `anything-iswitchb-setup-keys'."
3800 (interactive)
3802 (require 'iswitchb)
3804 ;; disable timid completion during iswitchb
3805 (put 'iswitchb-buffer 'timid-completion 'disabled)
3806 (add-hook 'minibuffer-setup-hook 'anything-iswitchb-minibuffer-setup)
3808 (defadvice iswitchb-visit-buffer
3809 (around anything-iswitchb-visit-buffer activate)
3810 (if anything-iswitchb-candidate-selected
3811 (anything-execute-selection-action)
3812 ad-do-it))
3814 (defadvice iswitchb-possible-new-buffer
3815 (around anything-iswitchb-possible-new-buffer activate)
3816 (if anything-iswitchb-candidate-selected
3817 (anything-execute-selection-action)
3818 ad-do-it))
3819 (anything-iswitchb-setup-keys)
3820 (message "Iswitchb integration is activated."))
3822 (defun anything-iswitchb-setup-keys ()
3823 "Modify `anything-map' for anything-iswitchb users.
3825 C-p is used instead of M-p, because anything uses ESC
3826 (currently hardcoded) for `anything-iswitchb-cancel-anything' and
3827 Emacs handles ESC and Meta as synonyms, so ESC overrides
3828 other commands with Meta prefix.
3830 Note that iswitchb uses M-p and M-n by default for history
3831 navigation, so you should bind C-p and C-n in
3832 `iswitchb-mode-map' if you use the history keys and don't want
3833 to use different keys for iswitchb while anything is not yet
3834 kicked in. These keys are not bound automatically by anything
3835 in `iswitchb-mode-map' because they (C-n at least) already have
3836 a standard iswitchb binding which you might be accustomed to.
3838 Binding M-s is used instead of C-s, because C-s has a binding in
3839 iswitchb. You can rebind it AFTER `anything-iswitchb-setup'.
3841 Unbind C-r to prevent problems during anything-isearch."
3842 (define-key anything-map (kbd "C-s") nil)
3843 (define-key anything-map (kbd "M-p") nil)
3844 (define-key anything-map (kbd "M-n") nil)
3845 (define-key anything-map (kbd "M-v") nil)
3846 (define-key anything-map (kbd "C-v") nil)
3847 (define-key anything-map (kbd "C-p") 'anything-previous-history-element)
3848 (define-key anything-map (kbd "C-n") 'anything-next-history-element)
3849 (define-key anything-map (kbd "M-s") nil)
3850 (define-key anything-map (kbd "M-s") 'anything-isearch)
3851 (define-key anything-map (kbd "C-r") nil))
3853 (defun anything-iswitchb-minibuffer-setup ()
3854 (when (eq this-command 'iswitchb-buffer)
3855 (add-hook 'minibuffer-exit-hook 'anything-iswitchb-minibuffer-exit)
3857 (setq anything-iswitchb-frame-configuration nil)
3858 (setq anything-iswitchb-candidate-selected nil)
3859 (add-hook 'anything-update-hook 'anything-iswitchb-handle-update)
3861 (anything-initialize)
3863 (add-hook 'deferred-action-list 'anything-iswitchb-check-input)))
3866 (defun anything-iswitchb-minibuffer-exit ()
3867 (remove-hook 'minibuffer-exit-hook 'anything-iswitchb-minibuffer-exit)
3868 (remove-hook 'deferred-action-list 'anything-iswitchb-check-input)
3869 (remove-hook 'anything-update-hook 'anything-iswitchb-handle-update)
3871 (anything-cleanup)
3873 (when anything-iswitchb-frame-configuration
3874 (anything-set-frame/window-configuration
3875 anything-iswitchb-frame-configuration)
3876 (setq anything-iswitchb-frame-configuration nil)))
3879 (defun anything-iswitchb-check-input ()
3880 "Extract iswitchb input and check if it needs to be handled."
3881 (declare (special iswitchb-text))
3882 (if (or anything-iswitchb-frame-configuration
3883 (sit-for anything-iswitchb-idle-delay))
3884 (anything-check-new-input iswitchb-text)))
3887 (defun anything-iswitchb-handle-update ()
3888 "Pop up the anything buffer if it's not empty and it's not
3889 shown yet and bind anything commands in iswitchb."
3890 (unless (or (anything-empty-buffer-p anything-buffer)
3891 anything-iswitchb-frame-configuration)
3892 (setq anything-iswitchb-frame-configuration
3893 (anything-current-frame/window-configuration))
3895 (save-selected-window
3896 (if (not anything-samewindow)
3897 (pop-to-buffer anything-buffer)
3899 (select-window (get-lru-window))
3900 (switch-to-buffer anything-buffer)))
3902 (with-current-buffer (window-buffer (active-minibuffer-window))
3903 (let* ((anything-prefix "anything-")
3904 (prefix-length (length anything-prefix))
3905 (commands
3906 (delete-dups
3907 (remove-if 'null
3908 (mapcar
3909 (lambda (binding)
3910 (let ((command (cdr binding)))
3911 (when (and (symbolp command)
3912 (eq (compare-strings
3913 anything-prefix
3914 0 prefix-length
3915 (symbol-name command)
3916 0 prefix-length)
3918 command)))
3919 (cdr anything-map)))))
3920 (bindings (mapcar (lambda (command)
3921 (cons command
3922 (where-is-internal command anything-map)))
3923 commands)))
3925 (push (list 'anything-iswitchb-cancel-anything (kbd "<ESC>"))
3926 bindings)
3928 (setq anything-iswitchb-saved-keys nil)
3930 (let* ((iswitchb-prefix "iswitchb-")
3931 (prefix-length (length iswitchb-prefix)))
3932 (dolist (binding bindings)
3933 (dolist (key (cdr binding))
3934 (let ((old-command (lookup-key (current-local-map) key)))
3935 (unless (and anything-iswitchb-dont-touch-iswithcb-keys
3936 (symbolp old-command)
3937 (eq (compare-strings iswitchb-prefix
3938 0 prefix-length
3939 (symbol-name old-command)
3940 0 prefix-length)
3942 (push (cons key old-command)
3943 anything-iswitchb-saved-keys)
3944 (define-key (current-local-map) key (car binding)))))))))))
3947 (defun anything-iswitchb-cancel-anything ()
3948 "Cancel anything completion and return to standard iswitchb."
3949 (interactive)
3950 (save-excursion
3951 (dolist (binding anything-iswitchb-saved-keys)
3952 (define-key (current-local-map) (car binding) (cdr binding)))
3953 (anything-iswitchb-minibuffer-exit)))
3955 ;; (@* "Compatibility")
3957 ;; Copied assoc-default from XEmacs version 21.5.12
3958 (unless (fboundp 'assoc-default)
3959 (defun assoc-default (key alist &optional test default)
3960 "Find object KEY in a pseudo-alist ALIST.
3961 ALIST is a list of conses or objects. Each element (or the element's car,
3962 if it is a cons) is compared with KEY by evaluating (TEST (car elt) KEY).
3963 If that is non-nil, the element matches;
3964 then `assoc-default' returns the element's cdr, if it is a cons,
3965 or DEFAULT if the element is not a cons.
3967 If no element matches, the value is nil.
3968 If TEST is omitted or nil, `equal' is used."
3969 (let (found (tail alist) value)
3970 (while (and tail (not found))
3971 (let ((elt (car tail)))
3972 (when (funcall (or test 'equal) (if (consp elt) (car elt) elt) key)
3973 (setq found t value (if (consp elt) (cdr elt) default))))
3974 (setq tail (cdr tail)))
3975 value)))
3977 ;; Function not available in XEmacs,
3978 (unless (fboundp 'minibuffer-contents)
3979 (defun minibuffer-contents ()
3980 "Return the user input in a minbuffer as a string.
3981 The current buffer must be a minibuffer."
3982 (field-string (point-max)))
3984 (defun delete-minibuffer-contents ()
3985 "Delete all user input in a minibuffer.
3986 The current buffer must be a minibuffer."
3987 (delete-field (point-max))))
3989 ;; Function not available in older Emacs (<= 22.1).
3990 (unless (fboundp 'buffer-chars-modified-tick)
3991 (defun buffer-chars-modified-tick (&optional buffer)
3992 "Return BUFFER's character-change tick counter.
3993 Each buffer has a character-change tick counter, which is set to the
3994 value of the buffer's tick counter (see `buffer-modified-tick'), each
3995 time text in that buffer is inserted or deleted. By comparing the
3996 values returned by two individual calls of `buffer-chars-modified-tick',
3997 you can tell whether a character change occurred in that buffer in
3998 between these calls. No argument or nil as argument means use current
3999 buffer as BUFFER."
4000 (with-current-buffer (or buffer (current-buffer))
4001 (if (listp buffer-undo-list)
4002 (length buffer-undo-list)
4003 (buffer-modified-tick)))))
4005 ;; (@* "CUA workaround")
4006 (defadvice cua-delete-region (around anything-avoid-cua activate)
4007 (ignore-errors ad-do-it))
4008 (defadvice copy-region-as-kill (around anything-avoid-cua activate)
4009 (if cua-mode
4010 (ignore-errors ad-do-it)
4011 ad-do-it))
4014 ;;(@* "Attribute Documentation")
4015 (defun anything-describe-anything-attribute (anything-attribute)
4016 "Display the full documentation of ANYTHING-ATTRIBUTE (a symbol)."
4017 (interactive (list (intern
4018 (completing-read
4019 "Describe anything attribute: "
4020 (mapcar 'symbol-name anything-additional-attributes)))))
4021 (with-output-to-temp-buffer "*Help*"
4022 (princ (get anything-attribute 'anything-attrdoc))))
4024 (anything-document-attribute 'name "mandatory"
4025 " The name of the source. It is also the heading which appears
4026 above the list of matches from the source. Must be unique. ")
4027 (anything-document-attribute 'header-name "optional"
4028 " A function returning the display string of the header. Its
4029 argument is the name of the source. This attribute is useful to
4030 add an additional information with the source name. ")
4031 (anything-document-attribute 'candidates "mandatory if candidates-in-buffer attribute is not provided"
4032 " Specifies how to retrieve candidates from the source. It can
4033 either be a variable name, a function called with no parameters
4034 or the actual list of candidates.
4036 The list must be a list whose members are strings, symbols
4037 or (DISPLAY . REAL) pairs.
4039 In case of (DISPLAY . REAL) pairs, the DISPLAY string is shown
4040 in the Anything buffer, but the REAL one is used as action
4041 argument when the candidate is selected. This allows a more
4042 readable presentation for candidates which would otherwise be,
4043 for example, too long or have a common part shared with other
4044 candidates which can be safely replaced with an abbreviated
4045 string for display purposes.
4047 Note that if the (DISPLAY . REAL) form is used then pattern
4048 matching is done on the displayed string, not on the real
4049 value.
4051 If the candidates have to be retrieved asynchronously (for
4052 example, by an external command which takes a while to run)
4053 then the function should start the external command
4054 asynchronously and return the associated process object.
4055 Anything will take care of managing the process (receiving the
4056 output from it, killing it if necessary, etc.). The process
4057 should return candidates matching the current pattern (see
4058 variable `anything-pattern'.)
4060 Note that currently results from asynchronous sources appear
4061 last in the anything buffer regardless of their position in
4062 `anything-sources'. ")
4063 (anything-document-attribute 'action "mandatory if type attribute is not provided"
4064 " It is a list of (DISPLAY . FUNCTION) pairs or FUNCTION.
4065 FUNCTION is called with one parameter: the selected candidate.
4067 An action other than the default can be chosen from this list
4068 of actions for the currently selected candidate (by default
4069 with TAB). The DISPLAY string is shown in the completions
4070 buffer and the FUNCTION is invoked when an action is
4071 selected. The first action of the list is the default. ")
4072 (anything-document-attribute 'coerce "optional"
4073 " It's a function called with one argument: the selected candidate.
4075 This function is intended for type convertion.
4076 In normal case, the selected candidate (string) is passed to action function.
4077 If coerce function is specified, it is called just before action function.
4079 Example: converting string to symbol
4080 (coerce . intern)
4082 (anything-document-attribute 'type "optional if action attribute is provided"
4083 " Indicates the type of the items the source returns.
4085 Merge attributes not specified in the source itself from
4086 `anything-type-attributes'.
4088 This attribute is implemented by plug-in. ")
4089 (anything-document-attribute 'init "optional"
4090 " Function called with no parameters when anything is started. It
4091 is useful for collecting current state information which can be
4092 used to create the list of candidates later.
4094 For example, if a source needs to work with the current
4095 directory then it can store its value here, because later
4096 anything does its job in the minibuffer and in the
4097 `anything-buffer' and the current directory can be different
4098 there. ")
4099 (anything-document-attribute 'delayed-init "optional"
4100 " Function called with no parameters before candidate function is
4101 called. It is similar with `init' attribute, but its
4102 evaluation is deferred. It is useful to combine with ")
4103 (anything-document-attribute 'match "optional"
4104 " List of functions called with one parameter: a candidate. The
4105 function should return non-nil if the candidate matches the
4106 current pattern (see variable `anything-pattern').
4108 This attribute allows the source to override the default
4109 pattern matching based on `string-match'. It can be used, for
4110 example, to implement a source for file names and do the
4111 pattern matching on the basename of files, since it's more
4112 likely one is typing part of the basename when searching for a
4113 file, instead of some string anywhere else in its path.
4115 If the list contains more than one function then the list of
4116 matching candidates from the source is constructed by appending
4117 the results after invoking the first function on all the
4118 potential candidates, then the next function, and so on. The
4119 matching candidates supplied by the first function appear first
4120 in the list of results and then results from the other
4121 functions, respectively.
4123 This attribute has no effect for asynchronous sources (see
4124 attribute `candidates'), since they perform pattern matching
4125 themselves. ")
4126 (anything-document-attribute 'candidate-transformer "optional"
4127 " It's a function or a list of functions called with one argument
4128 when the completion list from the source is built. The argument
4129 is the list of candidates retrieved from the source. The
4130 function should return a transformed list of candidates which
4131 will be used for the actual completion. If it is a list of
4132 functions, it calls each function sequentially.
4134 This can be used to transform or remove items from the list of
4135 candidates.
4137 Note that `candidates' is run already, so the given transformer
4138 function should also be able to handle candidates with (DISPLAY
4139 . REAL) format. ")
4140 (anything-document-attribute 'filtered-candidate-transformer "optional"
4141 " It has the same format as `candidate-transformer', except the
4142 function is called with two parameters: the candidate list and
4143 the source.
4145 This transformer is run on the candidate list which is already
4146 filtered by the current pattern. While `candidate-transformer'
4147 is run only once, it is run every time the input pattern is
4148 changed.
4150 It can be used to transform the candidate list dynamically, for
4151 example, based on the current pattern.
4153 In some cases it may also be more efficent to perform candidate
4154 transformation here, instead of with `candidate-transformer'
4155 even if this transformation is done every time the pattern is
4156 changed. For example, if a candidate set is very large then
4157 `candidate-transformer' transforms every candidate while only
4158 some of them will actually be dislpayed due to the limit
4159 imposed by `anything-candidate-number-limit'.
4161 Note that `candidates' and `candidate-transformer' is run
4162 already, so the given transformer function should also be able
4163 to handle candidates with (DISPLAY . REAL) format.
4165 This option has no effect for asynchronous sources. (Not yet,
4166 at least. ")
4167 (anything-document-attribute 'action-transformer "optional"
4168 " It's a function or a list of functions called with two
4169 arguments when the action list from the source is
4170 assembled. The first argument is the list of actions, the
4171 second is the current selection. If it is a list of functions,
4172 it calls each function sequentially.
4174 The function should return a transformed action list.
4176 This can be used to customize the list of actions based on the
4177 currently selected candidate. ")
4178 (anything-document-attribute 'pattern-transformer "optional"
4179 " It's a function or a list of functions called with one argument
4180 before computing matches. Its argument is `anything-pattern'.
4181 Functions should return transformed `anything-pattern'.
4183 It is useful to change interpretation of `anything-pattern'. ")
4184 (anything-document-attribute 'delayed "optional"
4185 " Candidates from the source are shown only if the user stops
4186 typing and is idle for `anything-idle-delay' seconds. ")
4187 (anything-document-attribute 'volatile "optional"
4188 " Indicates the source assembles the candidate list dynamically,
4189 so it shouldn't be cached within a single Anything
4190 invocation. It is only applicable to synchronous sources,
4191 because asynchronous sources are not cached. ")
4192 (anything-document-attribute 'requires-pattern "optional"
4193 " If present matches from the source are shown only if the
4194 pattern is not empty. Optionally, it can have an integer
4195 parameter specifying the required length of input which is
4196 useful in case of sources with lots of candidates. ")
4197 (anything-document-attribute 'persistent-action "optional"
4198 " Function called with one parameter; the selected candidate.
4200 An action performed by `anything-execute-persistent-action'.
4201 If none, use the default action. ")
4202 (anything-document-attribute 'candidates-in-buffer "optional"
4203 " Shortcut attribute for making and narrowing candidates using
4204 buffers. This newly-introduced attribute prevents us from
4205 forgetting to add volatile and match attributes.
4207 See docstring of `anything-candidates-in-buffer'.
4209 (candidates-in-buffer) is equivalent of three attributes:
4210 (candidates . anything-candidates-in-buffer)
4211 (volatile)
4212 (match identity)
4214 (candidates-in-buffer . candidates-function) is equivalent of:
4215 (candidates . candidates-function)
4216 (volatile)
4217 (match identity)
4219 This attribute is implemented by plug-in. ")
4220 (anything-document-attribute 'search "optional"
4221 " List of functions like `re-search-forward' or `search-forward'.
4222 Buffer search function used by `anything-candidates-in-buffer'.
4223 By default, `anything-candidates-in-buffer' uses `re-search-forward'.
4224 This attribute is meant to be used with
4225 (candidates . anything-candidates-in-buffer) or
4226 (candidates-in-buffer) in short. ")
4227 (anything-document-attribute 'search-from-end "optional"
4228 " Make `anything-candidates-in-buffer' search from the end of buffer.
4229 If this attribute is specified, `anything-candidates-in-buffer' uses
4230 `re-search-backward' instead. ")
4231 (anything-document-attribute 'get-line "optional"
4232 " A function like `buffer-substring-no-properties' or `buffer-substring'.
4233 This function converts point of line-beginning and point of line-end,
4234 which represents a candidate computed by `anything-candidates-in-buffer'.
4235 By default, `anything-candidates-in-buffer' uses
4236 `buffer-substring-no-properties'. ")
4237 (anything-document-attribute 'display-to-real "optional"
4238 " Function called with one parameter; the selected candidate.
4240 The function transforms the selected candidate, and the result
4241 is passed to the action function. The display-to-real
4242 attribute provides another way to pass other string than one
4243 shown in Anything buffer.
4245 Traditionally, it is possible to make candidates,
4246 candidate-transformer or filtered-candidate-transformer
4247 function return a list with (DISPLAY . REAL) pairs. But if REAL
4248 can be generated from DISPLAY, display-to-real is more
4249 convenient and faster. ")
4250 (anything-document-attribute 'real-to-display "optional"
4251 " Function called with one parameter; the selected candidate.
4253 The inverse of display-to-real attribute.
4255 The function transforms the selected candidate, which is passed
4256 to the action function, for display. The real-to-display
4257 attribute provides the other way to pass other string than one
4258 shown in Anything buffer.
4260 Traditionally, it is possible to make candidates,
4261 candidate-transformer or filtered-candidate-transformer
4262 function return a list with (DISPLAY . REAL) pairs. But if
4263 DISPLAY can be generated from REAL, real-to-display is more
4264 convenient.
4266 Note that DISPLAY parts returned from candidates /
4267 candidate-transformer are IGNORED as the name `display-to-real'
4268 says. ")
4269 (anything-document-attribute 'cleanup "optional"
4270 " Function called with no parameters when *anything* buffer is closed. It
4271 is useful for killing unneeded candidates buffer.
4273 Note that the function is executed BEFORE performing action. ")
4274 (anything-document-attribute 'candidate-number-limit "optional"
4275 " Override `anything-candidate-number-limit' only for this source. ")
4276 (anything-document-attribute 'accept-empty "optional"
4277 " Pass empty string \"\" to action function. ")
4278 (anything-document-attribute 'disable-shortcuts "optional"
4279 " Disable `anything-enable-shortcuts' in current `anything' session.
4281 This attribute is implemented by plug-in. ")
4282 (anything-document-attribute 'dummy "optional"
4283 " Set `anything-pattern' to candidate. If this attribute is
4284 specified, The candidates attribute is ignored.
4286 This attribute is implemented by plug-in.
4287 This plug-in implies disable-shortcuts plug-in. ")
4288 (anything-document-attribute 'multiline "optional"
4289 " Enable to selection multiline candidates. ")
4290 (anything-document-attribute 'update "optional"
4291 " Function called with no parameters when \\<anything-map>\\[anything-force-update] is pressed. ")
4292 (anything-document-attribute 'mode-line "optional"
4293 " source local `anything-mode-line-string'. (included in `mode-line-format')
4294 It accepts also variable/function name. ")
4295 (anything-document-attribute 'header-line "optional"
4296 " source local `header-line-format'.
4297 It accepts also variable/function name. ")
4298 (anything-document-attribute 'resume "optional" " Function called with no parameters when `anything-resume' is started.")
4300 ;; (@* "Bug Report")
4301 (defvar anything-maintainer-mail-address
4302 (concat "rubiki" "tch@ru" "by-lang.org"))
4303 (defvar anything-bug-report-salutation
4304 "Describe bug below, using a precise recipe.
4306 When I executed M-x ...
4308 How to send a bug report:
4309 1) Be sure to use the LATEST version of anything.el.
4310 2) Enable debugger. M-x toggle-debug-on-error or (setq debug-on-error t)
4311 3) Use Lisp version instead of compiled one: (load \"anything.el\")
4312 4) If you got an error, please paste *Backtrace* buffer.
4313 5) Type C-c C-c to send.
4314 # If you are a Japanese, please write in Japanese:-)")
4315 (defvar anything-no-dump-variables
4316 '(anything-candidate-buffer-alist
4317 anything-digit-overlays
4318 anything-help-message
4319 anything-candidate-cache
4321 "Variables not to dump in bug report.")
4323 (defun anything-dumped-variables-in-bug-report ()
4324 (let ((hash (make-hash-table)))
4325 (loop for var in (apropos-internal "anything-" 'boundp)
4326 for vname = (symbol-name var)
4327 unless (or (string-match "-map$" vname)
4328 (string-match "^anything-c-source-" vname)
4329 (string-match "-hash$" vname)
4330 (string-match "-face$" vname)
4331 (memq var anything-no-dump-variables))
4332 collect var)))
4334 (defun anything-send-bug-report ()
4335 "Send a bug report of anything.el."
4336 (interactive)
4337 (with-current-buffer (or anything-last-buffer
4338 (current-buffer))
4339 (reporter-submit-bug-report
4340 anything-maintainer-mail-address
4341 "anything.el"
4342 (anything-dumped-variables-in-bug-report)
4343 nil nil
4344 anything-bug-report-salutation)))
4346 (defun anything-send-bug-report-from-anything ()
4347 "Send a bug report of anything.el in anything session."
4348 (interactive)
4349 (anything-run-after-quit 'anything-send-bug-report))
4351 ;; (@* "Unit Tests")
4353 (defun* anything-test-candidates (sources &optional (input "") (compile-source-functions anything-compile-source-functions-default))
4354 "Test helper function for anything.
4355 Given pseudo `anything-sources' and `anything-pattern', returns list like
4356 ((\"source name1\" (\"candidate1\" \"candidate2\"))
4357 (\"source name2\" (\"candidate3\" \"candidate4\")))
4359 (let ((anything-test-mode t)
4360 anything-enable-shortcuts
4361 anything-candidate-cache
4362 (anything-compile-source-functions compile-source-functions)
4363 anything-before-initialize-hook
4364 anything-after-initialize-hook
4365 anything-update-hook
4366 anything-test-candidate-list)
4367 (get-buffer-create anything-buffer)
4369 (anything-initialize-1 nil input sources)
4370 (anything-update)
4371 ;; test-mode spec: select 1st candidate!
4372 (with-current-buffer anything-buffer
4373 (forward-line 1)
4374 (anything-mark-current-line))
4375 (prog1
4376 anything-test-candidate-list
4377 (anything-cleanup))))
4379 (defmacro anything-test-update (sources pattern)
4380 "Test helper macro for anything. It is meant for testing *anything* buffer contents."
4381 `(progn (stub anything-get-sources => ,sources)
4382 (stub anything-log-run-hook => nil)
4383 (stub anything-maybe-fit-frame => nil)
4384 (stub run-with-idle-timer => nil)
4385 (let (anything-test-mode (anything-pattern ,pattern))
4386 (anything-update))))
4388 ;;;; unit test
4389 ;; (install-elisp "http://www.emacswiki.org/cgi-bin/wiki/download/el-expectations.el")
4390 ;; (install-elisp "http://www.emacswiki.org/cgi-bin/wiki/download/el-mock.el")
4391 (dont-compile
4392 (when (fboundp 'expectations)
4393 (expectations
4394 (desc "anything-current-buffer")
4395 (expect "__a_buffer"
4396 (with-current-buffer (get-buffer-create "__a_buffer")
4397 (anything-test-candidates '(((name . "FOO"))) "")
4398 (prog1
4399 (buffer-name anything-current-buffer)
4400 (kill-buffer "__a_buffer")
4402 (desc "anything-buffer-file-name")
4403 (expect (regexp "/__a_file__")
4404 (with-current-buffer (get-buffer-create "__a_file__")
4405 (setq buffer-file-name "/__a_file__")
4406 (anything-test-candidates '(((name . "FOO"))) "")
4407 (prog1
4408 anything-buffer-file-name
4409 ;;(kill-buffer "__a_file__")
4411 (desc "anything-interpret-value")
4412 (expect "literal"
4413 (anything-interpret-value "literal"))
4414 (expect "lambda"
4415 (anything-interpret-value (lambda () "lambda")))
4416 (expect "lambda with source name"
4417 (let ((source '((name . "lambda with source name"))))
4418 (anything-interpret-value (lambda () anything-source-name) source)))
4419 (expect "function symbol"
4420 (flet ((f () "function symbol"))
4421 (anything-interpret-value 'f)))
4422 (expect "variable symbol"
4423 (let ((v "variable symbol"))
4424 (anything-interpret-value 'v)))
4425 (expect (error error *)
4426 (anything-interpret-value 'unbounded-1))
4427 (desc "anything-compile-sources")
4428 (expect '(((name . "foo")))
4429 (anything-compile-sources '(((name . "foo"))) nil)
4431 (expect '(((name . "foo") (type . test) (action . identity)))
4432 (let ((anything-type-attributes '((test (action . identity)))))
4433 (anything-compile-sources '(((name . "foo") (type . test)))
4434 '(anything-compile-source--type))))
4435 (desc "anything-sources accepts symbols")
4436 (expect '(((name . "foo")))
4437 (let* ((foo '((name . "foo"))))
4438 (anything-compile-sources '(foo) nil)))
4439 (desc "anything-get-sources action")
4440 (expect '(((name . "Actions") (candidates . actions)))
4441 (stub anything-action-window => t)
4442 (let (anything-compiled-sources
4443 (anything-sources '(((name . "Actions") (candidates . actions)))))
4444 (anything-get-sources)))
4445 (desc "get-buffer-create candidates-buffer")
4446 (expect '(((name . "many") (init . many-init)
4447 (candidates-in-buffer . anything-candidates-in-buffer)
4448 (candidates . anything-candidates-in-buffer)
4449 (volatile) (match identity)))
4450 (anything-compile-sources
4451 '(((name . "many") (init . many-init)
4452 (candidates-in-buffer . anything-candidates-in-buffer)))
4453 '(anything-compile-source--candidates-in-buffer)))
4454 (expect '(((name . "many") (init . many-init)
4455 (candidates-in-buffer)
4456 (candidates . anything-candidates-in-buffer)
4457 (volatile) (match identity)))
4458 (anything-compile-sources
4459 '(((name . "many") (init . many-init)
4460 (candidates-in-buffer)))
4461 '(anything-compile-source--candidates-in-buffer)))
4462 (expect '(((name . "many") (init . many-init)
4463 (candidates-in-buffer)
4464 (type . test)
4465 (action . identity)
4466 (candidates . anything-candidates-in-buffer)
4467 (volatile) (match identity)))
4468 (let ((anything-type-attributes '((test (action . identity)))))
4469 (anything-compile-sources
4470 '(((name . "many") (init . many-init)
4471 (candidates-in-buffer)
4472 (type . test)))
4473 '(anything-compile-source--type
4474 anything-compile-source--candidates-in-buffer))))
4476 (desc "anything-get-candidates")
4477 (expect '("foo" "bar")
4478 (anything-get-candidates '((name . "foo") (candidates "foo" "bar"))))
4479 (expect '("FOO" "BAR")
4480 (anything-get-candidates '((name . "foo") (candidates "foo" "bar")
4481 (candidate-transformer
4482 . (lambda (cands) (mapcar 'upcase cands))))))
4483 (expect '("foo" "bar")
4484 (anything-get-candidates '((name . "foo")
4485 (candidates . (lambda () '("foo" "bar"))))))
4486 (expect '("foo" "bar")
4487 (let ((var '("foo" "bar")))
4488 (anything-get-candidates '((name . "foo")
4489 (candidates . var)))))
4490 (expect (error error *)
4491 (anything-get-candidates '((name . "foo")
4492 (candidates . "err"))))
4493 (expect (error error *)
4494 (let ((var "err"))
4495 (anything-get-candidates '((name . "foo")
4496 (candidates . var)))))
4497 (expect (error error *)
4498 (anything-get-candidates '((name . "foo")
4499 (candidates . unDeFined-syMbol))))
4500 (desc "anything-compute-matches")
4501 (expect '("foo" "bar")
4502 (let ((anything-pattern ""))
4503 (anything-compute-matches '((name . "FOO") (candidates "foo" "bar") (volatile)))))
4504 (expect '("foo")
4505 (let ((anything-pattern "oo"))
4506 (anything-compute-matches '((name . "FOO") (candidates "foo" "bar") (volatile)))))
4507 (expect '("bar")
4508 (let ((anything-pattern "^b"))
4509 (anything-compute-matches '((name . "FOO") (candidates "foo" "bar") (volatile)))))
4510 (expect '("a" "b")
4511 (let ((anything-pattern "")
4512 (anything-candidate-number-limit 2))
4513 (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c") (volatile)))))
4514 (expect '("a" "b")
4515 (let ((anything-pattern ".")
4516 (anything-candidate-number-limit 2))
4517 (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c") (volatile)))))
4518 (expect '("a" "b" "c")
4519 (let ((anything-pattern "")
4520 anything-candidate-number-limit)
4521 (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c") (volatile)))))
4522 (expect '("a" "b" "c")
4523 (let ((anything-pattern "[abc]")
4524 anything-candidate-number-limit)
4525 (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c") (volatile)))))
4526 (expect '(a b c)
4527 (let ((anything-pattern "[abc]")
4528 anything-candidate-number-limit)
4529 (anything-compute-matches '((name . "FOO") (candidates a b c) (volatile)))))
4530 (expect '(("foo" . "FOO") ("bar" . "BAR"))
4531 (let ((anything-pattern ""))
4532 (anything-compute-matches '((name . "FOO") (candidates ("foo" . "FOO") ("bar" . "BAR")) (volatile)))))
4533 (expect '(("foo" . "FOO"))
4534 (let ((anything-pattern "foo"))
4535 (anything-compute-matches '((name . "FOO") (candidates ("foo" . "FOO") ("bar" . "foo")) (volatile)))))
4536 ;; using anything-test-candidate-list
4537 (desc "anything-test-candidates")
4538 (expect '(("FOO" ("foo" "bar")))
4539 (anything-test-candidates '(((name . "FOO") (candidates "foo" "bar")))))
4540 (expect '(("FOO" ("bar")))
4541 (anything-test-candidates '(((name . "FOO") (candidates "foo" "bar"))) "ar"))
4542 (expect '(("T1" ("hoge" "aiue"))
4543 ("T2" ("test" "boke")))
4544 (anything-test-candidates '(((name . "T1") (candidates "hoge" "aiue"))
4545 ((name . "T2") (candidates "test" "boke")))))
4546 (expect '(("T1" ("hoge"))
4547 ("T2" ("boke")))
4548 (anything-test-candidates '(((name . "T1") (candidates "hoge" "aiue"))
4549 ((name . "T2") (candidates "test" "boke"))) "o"))
4550 (desc "requires-pattern attribute")
4551 (expect nil
4552 (anything-test-candidates '(((name . "FOO") (candidates "foo" "bar")
4553 (requires-pattern . 1)))))
4554 (expect '(("FOO" ("bar")))
4555 (anything-test-candidates '(((name . "FOO") (candidates "foo" "bar")
4556 (requires-pattern . 1))) "b"))
4557 (desc "delayed attribute(for test)")
4558 (expect '(("T2" ("boke"))
4559 ("T1" ("hoge")))
4560 (anything-test-candidates
4561 '(((name . "T1") (candidates "hoge" "aiue") (delayed))
4562 ((name . "T2") (candidates "test" "boke")))
4563 "o"))
4564 (desc "match attribute(prefix search)")
4565 (expect '(("FOO" ("bar")))
4566 (anything-test-candidates
4567 '(((name . "FOO") (candidates "foo" "bar")
4568 (match (lambda (c) (string-match (concat "^" anything-pattern) c)))))
4569 "ba"))
4570 (expect nil
4571 (anything-test-candidates
4572 '(((name . "FOO") (candidates "foo" "bar")
4573 (match (lambda (c) (string-match (concat "^" anything-pattern) c)))))
4574 "ar"))
4575 (expect "TestSource"
4576 (let (x)
4577 (anything-test-candidates
4578 '(((name . "TestSource") (candidates "a")
4579 (match (lambda (c) (setq x anything-source-name)))))
4580 "a")
4582 (desc "init attribute")
4583 (expect '(("FOO" ("bar")))
4584 (let (v)
4585 (anything-test-candidates
4586 '(((name . "FOO") (init . (lambda () (setq v '("foo" "bar"))))
4587 (candidates . v)))
4588 "ar")))
4589 (desc "candidate-transformer attribute")
4590 (expect '(("FOO" ("BAR")))
4591 (anything-test-candidates '(((name . "FOO") (candidates "foo" "bar")
4592 (candidate-transformer
4593 . (lambda (cands) (mapcar 'upcase cands)))))
4594 "ar"))
4595 (desc "filtered-candidate-transformer attribute")
4596 ;; needs more tests
4597 (expect '(("FOO" ("BAR")))
4598 (anything-test-candidates '(((name . "FOO") (candidates "foo" "bar")
4599 (filtered-candidate-transformer
4600 . (lambda (cands src) (mapcar 'upcase cands)))))
4601 "ar"))
4602 (desc "anything-transform-candidates in process")
4603 (expect (mock (anything-composed-funcall-with-source
4604 '((name . "FOO") (candidates "foo" "bar")
4605 (filtered-candidate-transformer
4606 . (lambda (cands src) (mapcar 'upcase cands))))
4607 (lambda (cands src) (mapcar 'upcase cands))
4608 '("foo" "bar")
4609 '((name . "FOO") (candidates "foo" "bar")
4610 (filtered-candidate-transformer
4611 . (lambda (cands src) (mapcar 'upcase cands))))
4613 (stub anything-process-candidate-transformer => '("foo" "bar"))
4614 (anything-transform-candidates
4615 '("foo" "bar")
4616 '((name . "FOO") (candidates "foo" "bar")
4617 (filtered-candidate-transformer
4618 . (lambda (cands src) (mapcar 'upcase cands))))
4621 (desc "anything-candidates-in-buffer-1")
4622 (expect nil
4623 (anything-candidates-in-buffer-1
4624 nil ""
4625 'buffer-substring-no-properties '(re-search-forward) 50 nil))
4626 (expect '("foo+" "bar+" "baz+")
4627 (with-temp-buffer
4628 (insert "foo+\nbar+\nbaz+\n")
4629 (anything-candidates-in-buffer-1
4630 (current-buffer) ""
4631 'buffer-substring-no-properties '(re-search-forward) 5 nil)))
4632 (expect '("foo+" "bar+")
4633 (with-temp-buffer
4634 (insert "foo+\nbar+\nbaz+\n")
4635 (anything-candidates-in-buffer-1
4636 (current-buffer) ""
4637 'buffer-substring-no-properties '(re-search-forward) 2 nil)))
4638 (expect '("foo+")
4639 (with-temp-buffer
4640 (insert "foo+\nbar+\nbaz+\n")
4641 (anything-candidates-in-buffer-1
4642 (current-buffer) "oo\\+"
4643 'buffer-substring-no-properties '(re-search-forward) 50 nil)))
4644 (expect '("foo+")
4645 (with-temp-buffer
4646 (insert "foo+\nbar+\nbaz+\n")
4647 (anything-candidates-in-buffer-1
4648 (current-buffer) "oo+"
4649 #'buffer-substring-no-properties '(search-forward) 50 nil)))
4650 (expect '("foo+" "bar+")
4651 (with-temp-buffer
4652 (insert "foo+\nbar+\nbaz+\n")
4653 (anything-candidates-in-buffer-1
4654 (current-buffer) "."
4655 'buffer-substring-no-properties '(re-search-forward) 2 nil)))
4656 (expect '(("foo+" "FOO+"))
4657 (with-temp-buffer
4658 (insert "foo+\nbar+\nbaz+\n")
4659 (anything-candidates-in-buffer-1
4660 (current-buffer) "oo\\+"
4661 (lambda (s e)
4662 (let ((l (buffer-substring-no-properties s e)))
4663 (list l (upcase l))))
4664 '(re-search-forward) 50 nil)))
4665 (desc "anything-candidates-in-buffer")
4666 (expect '(("TEST" ("foo+" "bar+" "baz+")))
4667 (anything-test-candidates
4668 '(((name . "TEST")
4669 (init
4670 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4671 (insert "foo+\nbar+\nbaz+\n"))))
4672 (candidates . anything-candidates-in-buffer)
4673 (match identity)
4674 (volatile)))))
4675 (expect '(("TEST" ("foo+" "bar+" "baz+")))
4676 (let (anything-candidate-number-limit)
4677 (anything-test-candidates
4678 '(((name . "TEST")
4679 (init
4680 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4681 (insert "foo+\nbar+\nbaz+\n"))))
4682 (candidates . anything-candidates-in-buffer)
4683 (match identity)
4684 (volatile))))))
4685 (expect '(("TEST" ("foo+")))
4686 (anything-test-candidates
4687 '(((name . "TEST")
4688 (init
4689 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4690 (insert "foo+\nbar+\nbaz+\n"))))
4691 (candidates . anything-candidates-in-buffer)
4692 (match identity)
4693 (volatile)))
4694 "oo\\+"))
4695 ;; BUG remain empty string, but the pattern is rare case.
4696 (expect '(("a" ("" "a" "b")))
4697 (anything-test-candidates
4698 '(((name . "a")
4699 (init . (lambda ()
4700 (with-current-buffer (anything-candidate-buffer 'global)
4701 (insert "a\nb\n"))))
4702 (candidates-in-buffer)))
4703 "a*"))
4704 (desc "search attribute")
4705 (expect '(("TEST" ("foo+")))
4706 (anything-test-candidates
4707 '(((name . "TEST")
4708 (init
4709 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4710 (insert "foo+\nbar+\nbaz+\nooo\n"))))
4711 (search search-forward)
4712 (candidates . anything-candidates-in-buffer)
4713 (match identity)
4714 (volatile)))
4715 "oo+"))
4716 (expect '(("TEST" ("foo+" "ooo")))
4717 (anything-test-candidates
4718 '(((name . "TEST")
4719 (init
4720 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4721 (insert "foo+\nbar+\nbaz+\nooo\n"))))
4722 (search search-forward re-search-forward)
4723 (candidates . anything-candidates-in-buffer)
4724 (match identity)
4725 (volatile)))
4726 "oo+"))
4727 (expect '(("TEST" ("foo+" "ooo")))
4728 (anything-test-candidates
4729 '(((name . "TEST")
4730 (init
4731 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4732 (insert "foo+\nbar+\nbaz+\nooo\n"))))
4733 (search re-search-forward search-forward)
4734 (candidates . anything-candidates-in-buffer)
4735 (match identity)
4736 (volatile)))
4737 "oo+"))
4738 (expect '(("TEST" ("ooo" "foo+")))
4739 (anything-test-candidates
4740 '(((name . "TEST")
4741 (init
4742 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4743 (insert "bar+\nbaz+\nooo\nfoo+\n"))))
4744 (search re-search-forward search-forward)
4745 (candidates . anything-candidates-in-buffer)
4746 (match identity)
4747 (volatile)))
4748 "oo+"))
4749 ;; faster exact match
4750 (expect '(("TEST" ("bar+")))
4751 (anything-test-candidates
4752 '(((name . "TEST")
4753 (init
4754 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4755 (insert "bar+\nbaz+\nooo\nfoo+\n"))))
4756 (search (lambda (pattern &rest _)
4757 (and (search-forward (concat "\n" pattern "\n") nil t)
4758 (forward-line -1))))
4759 (candidates . anything-candidates-in-buffer)
4760 (match identity)
4761 (volatile)))
4762 "bar+"))
4763 ;; faster prefix match
4764 (expect '(("TEST" ("bar+")))
4765 (anything-test-candidates
4766 '(((name . "TEST")
4767 (init
4768 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4769 (insert "bar+\nbaz+\nooo\nfoo+\n"))))
4770 (search (lambda (pattern &rest _)
4771 (search-forward (concat "\n" pattern) nil t)))
4772 (candidates . anything-candidates-in-buffer)
4773 (match identity)
4774 (volatile)))
4775 "ba"))
4776 (desc "anything-current-buffer-is-modified")
4777 (expect '(("FOO" ("modified")))
4778 (let ((sources '(((name . "FOO")
4779 (candidates
4780 . (lambda ()
4781 (if (anything-current-buffer-is-modified)
4782 '("modified")
4783 '("unmodified"))))))))
4784 (with-temp-buffer
4785 (clrhash anything-tick-hash)
4786 (insert "1")
4787 (anything-test-candidates sources))))
4788 (expect '(("FOO" ("unmodified")))
4789 (let ((sources '(((name . "FOO")
4790 (candidates
4791 . (lambda ()
4792 (if (anything-current-buffer-is-modified)
4793 '("modified")
4794 '("unmodified"))))))))
4795 (with-temp-buffer
4796 (clrhash anything-tick-hash)
4797 (insert "1")
4798 (anything-test-candidates sources)
4799 (anything-test-candidates sources))))
4800 (expect '(("FOO" ("modified")))
4801 (let ((sources '(((name . "FOO")
4802 (candidates
4803 . (lambda ()
4804 (if (anything-current-buffer-is-modified)
4805 '("modified")
4806 '("unmodified"))))))))
4807 (with-temp-buffer
4808 (clrhash anything-tick-hash)
4809 (insert "1")
4810 (anything-test-candidates sources)
4811 (insert "2")
4812 (anything-test-candidates sources))))
4813 (expect '(("BAR" ("modified")))
4814 (let ((sources1 '(((name . "FOO")
4815 (candidates
4816 . (lambda ()
4817 (if (anything-current-buffer-is-modified)
4818 '("modified")
4819 '("unmodified")))))))
4820 (sources2 '(((name . "BAR")
4821 (candidates
4822 . (lambda ()
4823 (if (anything-current-buffer-is-modified)
4824 '("modified")
4825 '("unmodified"))))))))
4826 (with-temp-buffer
4827 (clrhash anything-tick-hash)
4828 (insert "1")
4829 (anything-test-candidates sources1)
4830 (anything-test-candidates sources2))))
4831 (expect '(("FOO" ("unmodified")))
4832 (let ((sources1 '(((name . "FOO")
4833 (candidates
4834 . (lambda ()
4835 (if (anything-current-buffer-is-modified)
4836 '("modified")
4837 '("unmodified")))))))
4838 (sources2 '(((name . "BAR")
4839 (candidates
4840 . (lambda ()
4841 (if (anything-current-buffer-is-modified)
4842 '("modified")
4843 '("unmodified"))))))))
4844 (with-temp-buffer
4845 (clrhash anything-tick-hash)
4846 (insert "1")
4847 (anything-test-candidates sources1)
4848 (anything-test-candidates sources2)
4849 (anything-test-candidates sources1))))
4850 (expect '(("BAR" ("unmodified")))
4851 (let ((sources1 '(((name . "FOO")
4852 (candidates
4853 . (lambda ()
4854 (if (anything-current-buffer-is-modified)
4855 '("modified")
4856 '("unmodified")))))))
4857 (sources2 '(((name . "BAR")
4858 (candidates
4859 . (lambda ()
4860 (if (anything-current-buffer-is-modified)
4861 '("modified")
4862 '("unmodified"))))))))
4863 (with-temp-buffer
4864 (clrhash anything-tick-hash)
4865 (insert "1")
4866 (anything-test-candidates sources1)
4867 (anything-test-candidates sources2)
4868 (anything-test-candidates sources2))))
4869 (expect '(("BAR" ("modified")))
4870 (let ((sources1 '(((name . "FOO")
4871 (candidates
4872 . (lambda ()
4873 (if (anything-current-buffer-is-modified)
4874 '("modified")
4875 '("unmodified")))))))
4876 (sources2 '(((name . "BAR")
4877 (candidates
4878 . (lambda ()
4879 (if (anything-current-buffer-is-modified)
4880 '("modified")
4881 '("unmodified"))))))))
4882 (with-temp-buffer
4883 (clrhash anything-tick-hash)
4884 (insert "1")
4885 (anything-test-candidates sources1)
4886 (anything-test-candidates sources2)
4887 (with-temp-buffer
4888 (anything-test-candidates sources2)))))
4889 (desc "anything-source-name")
4890 (expect "FOO"
4891 (let (v)
4892 (anything-test-candidates '(((name . "FOO")
4893 (init
4894 . (lambda () (setq v anything-source-name)))
4895 (candidates "ok"))))
4897 (expect "FOO"
4898 (let (v)
4899 (anything-test-candidates '(((name . "FOO")
4900 (candidates
4901 . (lambda ()
4902 (setq v anything-source-name)
4903 '("ok"))))))
4905 (expect "FOO"
4906 (let (v)
4907 (anything-test-candidates '(((name . "FOO")
4908 (candidates "ok")
4909 (candidate-transformer
4910 . (lambda (c)
4911 (setq v anything-source-name)
4912 c)))))
4914 (expect "FOO"
4915 (let (v)
4916 (anything-test-candidates '(((name . "FOO")
4917 (candidates "ok")
4918 (filtered-candidate-transformer
4919 . (lambda (c s)
4920 (setq v anything-source-name)
4921 c)))))
4923 (expect "FOO"
4924 (let (v)
4925 (anything-test-candidates '(((name . "FOO")
4926 (candidates "ok")
4927 (display-to-real
4928 . (lambda (c)
4929 (setq v anything-source-name)
4931 (action . identity))))
4932 (anything-execute-selection-action)
4934 (desc "anything-candidate-buffer create")
4935 (expect " *anything candidates:FOO*"
4936 (let* (anything-candidate-buffer-alist
4937 (anything-source-name "FOO")
4938 (buf (anything-candidate-buffer 'global)))
4939 (prog1 (buffer-name buf)
4940 (kill-buffer buf))))
4941 (expect " *anything candidates:FOO*aTestBuffer"
4942 (let* (anything-candidate-buffer-alist
4943 (anything-source-name "FOO")
4944 (anything-current-buffer (get-buffer-create "aTestBuffer"))
4945 (buf (anything-candidate-buffer 'local)))
4946 (prog1 (buffer-name buf)
4947 (kill-buffer anything-current-buffer)
4948 (kill-buffer buf))))
4949 (expect 0
4950 (let (anything-candidate-buffer-alist
4951 (anything-source-name "FOO") buf)
4952 (with-current-buffer (anything-candidate-buffer 'global)
4953 (insert "1"))
4954 (setq buf (anything-candidate-buffer 'global))
4955 (prog1 (buffer-size buf)
4956 (kill-buffer buf))))
4957 (desc "anything-candidate-buffer get-buffer")
4958 (expect " *anything candidates:FOO*"
4959 (let* (anything-candidate-buffer-alist
4960 (anything-source-name "FOO")
4961 (buf (anything-candidate-buffer 'global)))
4962 (prog1 (buffer-name (anything-candidate-buffer))
4963 (kill-buffer buf))))
4964 (expect " *anything candidates:FOO*aTestBuffer"
4965 (let* (anything-candidate-buffer-alist
4966 (anything-source-name "FOO")
4967 (anything-current-buffer (get-buffer-create "aTestBuffer"))
4968 (buf (anything-candidate-buffer 'local)))
4969 (prog1 (buffer-name (anything-candidate-buffer))
4970 (kill-buffer anything-current-buffer)
4971 (kill-buffer buf))))
4972 (expect " *anything candidates:FOO*"
4973 (let* (anything-candidate-buffer-alist
4974 (anything-source-name "FOO")
4975 (buf-local (anything-candidate-buffer 'local))
4976 (buf-global (anything-candidate-buffer 'global)))
4977 (prog1 (buffer-name (anything-candidate-buffer))
4978 (kill-buffer buf-local)
4979 (kill-buffer buf-global))))
4980 (expect " *anything candidates:FOO*aTestBuffer"
4981 (let* (anything-candidate-buffer-alist
4982 (anything-source-name "FOO")
4983 (anything-current-buffer (get-buffer-create "aTestBuffer"))
4984 (buf-global (anything-candidate-buffer 'global))
4985 (buf-local (anything-candidate-buffer 'local)))
4986 (prog1 (buffer-name (anything-candidate-buffer))
4987 (kill-buffer buf-local)
4988 (kill-buffer buf-global))))
4989 (expect nil
4990 (let* (anything-candidate-buffer-alist
4991 (anything-source-name "NOP__"))
4992 (anything-candidate-buffer)))
4993 (desc "anything-candidate-buffer register-buffer")
4994 (expect " *anything test candidates*"
4995 (let (anything-candidate-buffer-alist
4996 (buf (get-buffer-create " *anything test candidates*")))
4997 (with-current-buffer buf
4998 (insert "1\n2\n")
4999 (prog1 (buffer-name (anything-candidate-buffer buf))
5000 (kill-buffer (current-buffer))))))
5001 (expect " *anything test candidates*"
5002 (let (anything-candidate-buffer-alist
5003 (buf (get-buffer-create " *anything test candidates*")))
5004 (with-current-buffer buf
5005 (insert "1\n2\n")
5006 (anything-candidate-buffer buf)
5007 (prog1 (buffer-name (anything-candidate-buffer))
5008 (kill-buffer (current-buffer))))))
5009 (expect "1\n2\n"
5010 (let (anything-candidate-buffer-alist
5011 (buf (get-buffer-create " *anything test candidates*")))
5012 (with-current-buffer buf
5013 (insert "1\n2\n")
5014 (anything-candidate-buffer buf)
5015 (prog1 (buffer-string)
5016 (kill-buffer (current-buffer))))))
5017 (expect "buf1"
5018 (let (anything-candidate-buffer-alist
5019 (anything-source-name "foo")
5020 (buf1 (get-buffer-create "buf1"))
5021 (buf2 (get-buffer-create "buf2")))
5022 (anything-candidate-buffer buf1)
5023 (anything-candidate-buffer buf2)
5024 (prog1 (buffer-name (anything-candidate-buffer buf1))
5025 (kill-buffer buf1)
5026 (kill-buffer buf2))))
5027 (desc "action attribute")
5028 (expect "foo"
5029 (anything-test-candidates
5030 '(((name . "TEST")
5031 (candidates "foo")
5032 (action ("identity" . identity)))))
5033 (anything-execute-selection-action))
5034 (expect "foo"
5035 (anything-test-candidates
5036 '(((name . "TEST")
5037 (candidates "foo")
5038 (action ("identity" . (lambda (c) (identity c)))))))
5039 (anything-execute-selection-action))
5040 (desc "anything-get-default-action")
5041 (expect 'upcase
5042 (anything-get-default-action '(("upcase" . upcase))))
5043 (expect 'downcase
5044 (anything-get-default-action '(("downcase" . downcase))))
5045 (expect (lambda (x) (capitalize x))
5046 (anything-get-default-action (lambda (x) (capitalize x))))
5047 (expect 'identity
5048 (anything-get-default-action 'identity))
5049 (desc "anything-execute-selection-action")
5050 (expect "FOO"
5051 (anything-execute-selection-action
5052 "foo" '(("upcase" . upcase)) nil))
5053 (expect "FOO"
5054 (anything-execute-selection-action
5055 "foo" '(("upcase" . (lambda (c) (upcase c)))) nil))
5057 (desc "display-to-real attribute")
5058 (expect "FOO"
5059 (anything-test-candidates
5060 '(((name . "TEST")
5061 (candidates "foo")
5062 (display-to-real . upcase)
5063 (action ("identity" . identity)))))
5064 (anything-execute-selection-action))
5065 (desc "cleanup test")
5066 (expect 'cleaned
5067 (let (v)
5068 (anything-test-candidates
5069 '(((name . "TEST")
5070 (cleanup . (lambda () (setq v 'cleaned))))))
5072 (desc "anything-get-current-source")
5073 ;; in init/candidates/action/candidate-transformer/filtered-candidate-transformer
5074 ;; display-to-real/cleanup function
5075 (expect "FOO"
5076 (assoc-default
5077 'name
5078 (anything-funcall-with-source '((name . "FOO")) 'anything-get-current-source)))
5079 ;; init
5080 (expect "FOO"
5081 (let (v)
5082 (anything-test-candidates
5083 '(((name . "FOO")
5084 (init . (lambda () (setq v (anything-get-current-source)))))))
5085 (assoc-default 'name v)))
5086 ;; candidates
5087 (expect "FOO"
5088 (let (v)
5089 (anything-test-candidates
5090 '(((name . "FOO")
5091 (candidates . (lambda () (setq v (anything-get-current-source)) '("a"))))))
5092 (assoc-default 'name v)))
5093 ;; action
5094 (expect "FOO"
5095 (let (v)
5096 (anything-test-candidates
5097 '(((name . "FOO")
5098 (candidates "a")
5099 (action
5100 . (lambda (c) (setq v (anything-get-current-source)) c)))))
5101 (anything-execute-selection-action)
5102 (assoc-default 'name v)))
5103 ;; candidate-transformer
5104 (expect "FOO"
5105 (let (v)
5106 (anything-test-candidates
5107 '(((name . "FOO")
5108 (candidates "a")
5109 (candidate-transformer
5110 . (lambda (c) (setq v (anything-get-current-source)) c)))))
5111 (assoc-default 'name v)))
5112 ;; filtered-candidate-transformer
5113 (expect "FOO"
5114 (let (v)
5115 (anything-test-candidates
5116 '(((name . "FOO")
5117 (candidates "a")
5118 (filtered-candidate-transformer
5119 . (lambda (c s) (setq v (anything-get-current-source)) c)))))
5120 (assoc-default 'name v)))
5121 ;; action-transformer
5122 (expect "FOO"
5123 (let (v)
5124 (anything-test-candidates
5125 '(((name . "FOO")
5126 (candidates "a")
5127 (action-transformer
5128 . (lambda (a c) (setq v (anything-get-current-source)) a))
5129 (action . identity))))
5130 (anything-execute-selection-action)
5131 (assoc-default 'name v)))
5132 ;; display-to-real
5133 (expect "FOO"
5134 (let (v)
5135 (anything-test-candidates
5136 '(((name . "FOO")
5137 (init . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5138 (insert "a\n"))))
5139 (candidates-in-buffer)
5140 (display-to-real
5141 . (lambda (c) (setq v (anything-get-current-source)) c))
5142 (action . identity))))
5143 (anything-execute-selection-action)
5144 (assoc-default 'name v)))
5145 ;; cleanup
5146 (expect "FOO"
5147 (let (v)
5148 (anything-test-candidates
5149 '(((name . "FOO")
5150 (candidates "a")
5151 (cleanup
5152 . (lambda () (setq v (anything-get-current-source)))))))
5153 (assoc-default 'name v)))
5154 ;; candidates are displayed
5155 (expect "TEST"
5156 (anything-test-candidates
5157 '(((name . "TEST")
5158 (candidates "foo")
5159 (action ("identity" . identity)))))
5160 (assoc-default 'name (anything-get-current-source)))
5161 (desc "anything-attr")
5162 (expect "FOO"
5163 (anything-funcall-with-source
5164 '((name . "FOO"))
5165 (lambda ()
5166 (anything-attr 'name))))
5167 (expect 'fuga
5168 (let (v)
5169 (anything-test-candidates
5170 '(((name . "FOO")
5171 (hoge . fuga)
5172 (init . (lambda () (setq v (anything-attr 'hoge))))
5173 (candidates "a"))))
5175 (expect nil
5176 (let (v)
5177 (anything-test-candidates
5178 '(((name . "FOO")
5179 (init . (lambda () (setq v (anything-attr 'hoge))))
5180 (candidates "a"))))
5182 (expect nil
5183 (let (v)
5184 (anything-test-candidates
5185 '(((name . "FOO")
5186 (hoge) ;INCOMPATIBLE!
5187 (init . (lambda () (setq v (anything-attr 'hoge))))
5188 (candidates "a"))))
5190 (desc "anything-attr*")
5191 (expect "generic"
5192 (let (v (value1 "generic"))
5193 (anything-test-candidates
5194 '(((name . "FOO")
5195 (hoge . value1)
5196 (init . (lambda () (setq v (anything-attr* 'hoge)))))))
5198 (desc "anything-attr-defined")
5199 (expect (non-nil)
5200 (let (v)
5201 (anything-test-candidates
5202 '(((name . "FOO")
5203 (hoge)
5204 (init . (lambda () (setq v (anything-attr-defined 'hoge))))
5205 (candidates "a"))))
5206 v))
5207 (expect nil
5208 (let (v)
5209 (anything-test-candidates
5210 '(((name . "FOO")
5211 (init . (lambda () (setq v (anything-attr-defined 'hoge))))
5212 (candidates "a"))))
5213 v))
5214 (desc "anything-attrset")
5215 (expect '((name . "FOO") (hoge . 77))
5216 (let ((src '((name . "FOO") (hoge))))
5217 (anything-attrset 'hoge 77 src)
5218 src))
5219 (expect 77
5220 (anything-attrset 'hoge 77 '((name . "FOO") (hoge))))
5222 (expect '((name . "FOO") (hoge . 77))
5223 (let ((src '((name . "FOO") (hoge . 1))))
5224 (anything-attrset 'hoge 77 src)
5225 src))
5227 (expect '((name . "FOO") (hoge . 77) (x))
5228 (let ((src '((name . "FOO") (x))))
5229 (anything-attrset 'hoge 77 src)
5230 src))
5231 (expect 77
5232 (anything-attrset 'hoge 77 '((name . "FOO"))))
5233 (desc "anything-preselect")
5234 ;; entire candidate
5235 (expect "foo"
5236 (with-current-buffer (anything-create-anything-buffer t)
5237 (let ((anything-pattern "")
5238 (anything-test-mode t))
5239 (anything-process-source '((name . "test")
5240 (candidates "hoge" "foo" "bar")))
5241 (anything-preselect "foo")
5242 (anything-get-selection))))
5243 ;; regexp
5244 (expect "foo"
5245 (with-current-buffer (anything-create-anything-buffer t)
5246 (let ((anything-pattern "")
5247 (anything-test-mode t))
5248 (anything-process-source '((name . "test")
5249 (candidates "hoge" "foo" "bar")))
5250 (anything-preselect "fo+")
5251 (anything-get-selection))))
5252 ;; no match -> first entry
5253 (expect "hoge"
5254 (with-current-buffer (anything-create-anything-buffer t)
5255 (let ((anything-pattern "")
5256 (anything-test-mode t))
5257 (anything-process-source '((name . "test")
5258 (candidates "hoge" "foo" "bar")))
5259 (anything-preselect "not found")
5260 (anything-get-selection))))
5261 (desc "anything-check-new-input")
5262 (expect "newpattern"
5263 (stub anything-update)
5264 (stub anything-action-window)
5265 (let ((anything-pattern "pattern"))
5266 (anything-check-new-input "newpattern")
5267 anything-pattern))
5268 ;; anything-input == nil when action window is available
5269 (expect nil
5270 (stub anything-update)
5271 (stub anything-action-window => t)
5272 (let ((anything-pattern "pattern")
5273 anything-input)
5274 (anything-check-new-input "newpattern")
5275 anything-input))
5276 ;; anything-input == anything-pattern unless action window is available
5277 (expect "newpattern"
5278 (stub anything-update)
5279 (stub anything-action-window => nil)
5280 (let ((anything-pattern "pattern")
5281 anything-input)
5282 (anything-check-new-input "newpattern")
5283 anything-input))
5284 (expect (mock (anything-update))
5285 (stub anything-action-window)
5286 (let (anything-pattern)
5287 (anything-check-new-input "foo")))
5288 (desc "anything-update")
5289 (expect (mock (anything-process-source '((name . "1"))))
5290 (anything-test-update '(((name . "1"))) ""))
5291 ;; (find-function 'anything-update)
5292 ;; TODO el-mock.el should express 2nd call of function.
5293 ;; (expect (mock (anything-process-source '((name . "2"))))
5294 ;; (stub anything-get-sources => '(((name . "1")) ((name . "2"))))
5295 ;; (stub anything-log-run-hook)
5296 ;; (stub anything-maybe-fit-frame)
5297 ;; (stub run-with-idle-timer)
5298 ;; (anything-update))
5299 (expect (mock (run-with-idle-timer * nil 'anything-process-delayed-sources
5300 '(((name . "2") (delayed)))))
5301 (stub anything-get-sources => '(((name . "1"))
5302 ((name . "2") (delayed))))
5303 (stub anything-log-run-hook)
5304 (stub anything-maybe-fit-frame)
5305 (let ((anything-pattern "") anything-test-mode)
5306 (anything-update)))
5308 (desc "requires-pattern attribute")
5309 (expect (not-called anything-process-source)
5310 (anything-test-update '(((name . "1") (requires-pattern))) ""))
5311 (expect (not-called anything-process-source)
5312 (anything-test-update '(((name . "1") (requires-pattern . 3))) "xx"))
5314 (desc "anything-normalize-sources")
5315 (expect '(anything-c-source-test)
5316 (anything-normalize-sources 'anything-c-source-test))
5317 (expect '(anything-c-source-test)
5318 (anything-normalize-sources '(anything-c-source-test)))
5319 (expect '(anything-c-source-test)
5320 (let ((anything-sources '(anything-c-source-test)))
5321 (anything-normalize-sources nil)))
5322 (expect '(((name . "test")))
5323 (anything-normalize-sources '((name . "test"))))
5324 (expect '(((name . "test")))
5325 (anything-normalize-sources '(((name . "test")))))
5326 (desc "anything-get-action")
5327 (expect '(("identity" . identity))
5328 (stub buffer-size => 1)
5329 (stub anything-get-current-source => '((name . "test")
5330 (action ("identity" . identity))))
5331 (anything-get-action))
5332 (expect 'identity
5333 (stub buffer-size => 1)
5334 (stub anything-get-current-source => '((name . "test")
5335 (action . identity)))
5336 (anything-get-action))
5337 (expect '((("identity" . identity)) "action-transformer is called")
5338 (stub buffer-size => 1)
5339 (stub anything-get-current-source
5340 => '((name . "test")
5341 (action ("identity" . identity))
5342 (action-transformer
5343 . (lambda (actions selection)
5344 (list actions selection)))))
5345 (stub anything-get-selection => "action-transformer is called")
5346 (anything-get-action))
5347 (desc "anything-select-nth-action")
5348 (expect (error error *)
5349 (stub anything-get-selection => nil)
5350 (anything-select-nth-action 0))
5351 (desc "anything-get-nth-action")
5352 (expect 'cadr
5353 (anything-get-nth-action 2 '(("0" . car) ("1" . cdr) ("2" . cadr))))
5354 (expect (error error *)
5355 (anything-get-nth-action 2 '(("0" . car))))
5356 (expect 'identity
5357 (anything-get-nth-action 0 'identity))
5358 (expect (error error *)
5359 (anything-get-nth-action 1 'identity))
5360 (expect (error error *)
5361 (anything-get-nth-action 0 'unbound-function-xxx))
5362 (expect (error error *)
5363 (anything-get-nth-action 0 "invalid data"))
5364 (desc "anything-funcall-foreach")
5365 (expect (mock (upcase "foo"))
5366 (stub anything-get-sources => '(((init . (lambda () (upcase "foo"))))))
5367 (anything-funcall-foreach 'init))
5368 (expect (mock (downcase "bar"))
5369 (stub anything-get-sources => '(((init . (lambda () (upcase "foo"))))
5370 ((init . (lambda () (downcase "bar"))))))
5371 (anything-funcall-foreach 'init))
5372 (expect (not-called anything-funcall-with-source)
5373 (stub anything-get-sources => '(((init . (lambda () (upcase "foo"))))))
5374 (anything-funcall-foreach 'not-found))
5375 ;; TODO anything-select-with-digit-shortcut test
5376 (desc "anything-get-cached-candidates")
5377 (expect '("cached" "version")
5378 (let ((anything-candidate-cache '(("test" "cached" "version"))))
5379 (anything-get-cached-candidates '((name . "test")
5380 (candidates "new")))))
5381 (expect '("new")
5382 (let ((anything-candidate-cache '(("other" "cached" "version"))))
5383 (anything-get-cached-candidates '((name . "test")
5384 (candidates "new")))))
5385 (expect '(("test" "new")
5386 ("other" "cached" "version"))
5387 (let ((anything-candidate-cache '(("other" "cached" "version"))))
5388 (anything-get-cached-candidates '((name . "test")
5389 (candidates "new")))
5390 anything-candidate-cache))
5391 (expect '(("other" "cached" "version"))
5392 (let ((anything-candidate-cache '(("other" "cached" "version"))))
5393 (anything-get-cached-candidates '((name . "test")
5394 (candidates "new")
5395 (volatile)))
5396 anything-candidate-cache))
5397 ;; TODO when candidates == process
5398 ;; TODO anything-output-filter
5399 (desc "candidate-number-limit attribute")
5400 (expect '("a" "b")
5401 (let ((anything-pattern "")
5402 (anything-candidate-number-limit 20))
5403 (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c")
5404 (candidate-number-limit . 2) (volatile)))))
5405 (expect '("a" "b")
5406 (let ((anything-pattern "[abc]")
5407 (anything-candidate-number-limit 20))
5408 (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c")
5409 (candidate-number-limit . 2) (volatile)))))
5410 (expect '("a" "b" "c" "d")
5411 (let ((anything-pattern "[abcd]")
5412 (anything-candidate-number-limit 2))
5413 (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c" "d")
5414 (candidate-number-limit) (volatile)))))
5415 (expect '(("TEST" ("a" "b" "c")))
5416 (let ((anything-candidate-number-limit 2))
5417 (anything-test-candidates
5418 '(((name . "TEST")
5419 (init
5420 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5421 (insert "a\nb\nc\nd\n"))))
5422 (candidates . anything-candidates-in-buffer)
5423 (match identity)
5424 (candidate-number-limit . 3)
5425 (volatile))))))
5426 (expect '(("TEST" ("a" "b" "c")))
5427 (let ((anything-candidate-number-limit 2))
5428 (anything-test-candidates
5429 '(((name . "TEST")
5430 (init
5431 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5432 (insert "a\nb\nc\nd\n"))))
5433 (candidates . anything-candidates-in-buffer)
5434 (match identity)
5435 (candidate-number-limit . 3)
5436 (volatile)))
5437 ".")))
5438 (desc "multiple init")
5439 (expect '(1 . 2)
5440 (let (a b)
5441 (anything-test-candidates
5442 '(((name . "test")
5443 (init (lambda () (setq a 1))
5444 (lambda () (setq b 2))))))
5445 (cons a b)))
5446 (expect 1
5447 (let (a)
5448 (anything-test-candidates
5449 '(((name . "test")
5450 (init (lambda () (setq a 1))))))
5452 (desc "multiple cleanup")
5453 (expect '(1 . 2)
5454 (let (a b)
5455 (anything-test-candidates
5456 '(((name . "test")
5457 (cleanup (lambda () (setq a 1))
5458 (lambda () (setq b 2))))))
5459 (cons a b)))
5460 (desc "anything-mklist")
5461 (expect '(1)
5462 (anything-mklist 1))
5463 (expect '(2)
5464 (anything-mklist '(2)))
5465 (expect '((lambda ()))
5466 (anything-mklist (lambda ())))
5467 (desc "anything-before-initialize-hook")
5468 (expect 'called
5469 (let ((anything-before-initialize-hook '((lambda () (setq v 'called))))
5471 (anything-initialize)
5473 (desc "anything-after-initialize-hook")
5474 (expect '(b a)
5475 (let ((anything-before-initialize-hook
5476 '((lambda () (setq v '(a)))))
5477 (anything-after-initialize-hook
5478 '((lambda () (setq v (cons 'b v)))))
5480 (anything-initialize)
5482 (expect 0
5483 (let ((anything-after-initialize-hook
5484 '((lambda () (setq v (buffer-size (get-buffer anything-buffer))))))
5486 (anything-initialize)
5488 (desc "get-line attribute")
5489 (expect '(("TEST" ("FOO+")))
5490 (anything-test-candidates
5491 '(((name . "TEST")
5492 (init
5493 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5494 (insert "foo+\nbar+\nbaz+\n"))))
5495 (candidates-in-buffer)
5496 (get-line . (lambda (s e) (upcase (buffer-substring-no-properties s e))))))
5497 "oo\\+"))
5498 (desc "with-anything-restore-variables")
5499 (expect '(7 8)
5500 (let ((a 7) (b 8)
5501 (anything-restored-variables '(a b)))
5502 (with-anything-restore-variables
5503 (setq a 0 b 0))
5504 (list a b)))
5505 (desc "anything-cleanup-hook")
5506 (expect 'called
5507 (let ((anything-cleanup-hook
5508 '((lambda () (setq v 'called))))
5510 (anything-cleanup)
5512 (desc "with-anything-display-same-window")
5513 (expect (non-nil)
5514 (save-window-excursion
5515 (delete-other-windows)
5516 (split-window)
5518 (let ((buf (get-buffer-create " tmp"))
5519 (win (selected-window)))
5520 (with-anything-display-same-window
5521 (display-buffer buf)
5522 (eq win (get-buffer-window buf))))))
5523 (expect (non-nil)
5524 (save-window-excursion
5525 (delete-other-windows)
5526 (split-window)
5528 (let ((buf (get-buffer-create " tmp"))
5529 (win (selected-window)))
5530 (with-anything-display-same-window
5531 (pop-to-buffer buf)
5532 (eq win (get-buffer-window buf))))))
5533 (expect (non-nil)
5534 (save-window-excursion
5535 (delete-other-windows)
5536 (split-window)
5538 (let ((buf (get-buffer-create " tmp"))
5539 (win (selected-window)))
5540 (with-anything-display-same-window
5541 (switch-to-buffer buf)
5542 (eq win (get-buffer-window buf))))))
5543 (expect (non-nil)
5544 (save-window-excursion
5545 (delete-other-windows)
5546 (let ((buf (get-buffer-create " tmp"))
5547 (win (selected-window)))
5548 (with-anything-display-same-window
5549 (display-buffer buf)
5550 (eq win (get-buffer-window buf))))))
5551 (expect (non-nil)
5552 (save-window-excursion
5553 (delete-other-windows)
5554 (let ((buf (get-buffer-create " tmp"))
5555 (win (selected-window)))
5556 (with-anything-display-same-window
5557 (pop-to-buffer buf)
5558 (eq win (get-buffer-window buf))))))
5559 (desc "search-from-end attribute")
5560 (expect '(("TEST" ("baz+" "bar+" "foo+")))
5561 (anything-test-candidates
5562 '(((name . "TEST")
5563 (init
5564 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5565 (insert "foo+\nbar+\nbaz+\n"))))
5566 (candidates-in-buffer)
5567 (search-from-end)))))
5568 (expect '(("TEST" ("baz+" "bar+" "foo+")))
5569 (anything-test-candidates
5570 '(((name . "TEST")
5571 (init
5572 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5573 (insert "foo+\nbar+\nbaz+\n"))))
5574 (candidates-in-buffer)
5575 (search-from-end)))
5576 "\\+"))
5577 (expect '(("TEST" ("baz+" "bar+")))
5578 (anything-test-candidates
5579 '(((name . "TEST")
5580 (init
5581 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5582 (insert "foo+\nbar+\nbaz+\n"))))
5583 (candidates-in-buffer)
5584 (search-from-end)
5585 (candidate-number-limit . 2)))))
5586 (expect '(("TEST" ("baz+" "bar+")))
5587 (anything-test-candidates
5588 '(((name . "TEST")
5589 (init
5590 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5591 (insert "foo+\nbar+\nbaz+\n"))))
5592 (candidates-in-buffer)
5593 (search-from-end)
5594 (candidate-number-limit . 2)))
5595 "\\+"))
5596 (expect '(("a" ("c2" "c1")))
5597 (anything-test-candidates
5598 '(((name . "a")
5599 (init . (lambda ()
5600 (with-current-buffer (anything-candidate-buffer 'global)
5601 (insert "c1\nc2\n"))))
5602 (search-from-end)
5603 (candidates-in-buffer)))))
5604 ;; BUG remain empty string, but the pattern is rare case.
5605 (expect '(("a" ("c" "b" "a" "")))
5606 (anything-test-candidates
5607 '(((name . "a")
5608 (init . (lambda ()
5609 (with-current-buffer (anything-candidate-buffer 'global)
5610 (insert "a\nb\nc\n"))))
5611 (search-from-end)
5612 (candidates-in-buffer)))
5613 "a*"))
5614 (desc "header-name attribute")
5615 (expect "original is transformed"
5616 (anything-test-update '(((name . "original")
5617 (candidates "1")
5618 (header-name
5619 . (lambda (name)
5620 (format "%s is transformed" name)))))
5622 (with-current-buffer (anything-buffer-get)
5623 (buffer-string)
5624 (overlay-get (car (overlays-at (1+(point-min)))) 'display)))
5625 (desc "volatile and match attribute")
5626 ;; candidates function is called once per `anything-process-delayed-sources'
5627 (expect 1
5628 (let ((v 0))
5629 (anything-test-candidates '(((name . "test")
5630 (candidates . (lambda () (incf v) '("ok")))
5631 (volatile)
5632 (match identity identity identity)))
5633 "o")
5635 (desc "accept-empty attribute")
5636 (expect nil
5637 (anything-test-candidates
5638 '(((name . "test") (candidates "") (action . identity))))
5639 (anything-execute-selection-action))
5640 (expect ""
5641 (anything-test-candidates
5642 '(((name . "test") (candidates "") (action . identity) (accept-empty))))
5643 (anything-execute-selection-action))
5644 (desc "anything-tick-hash")
5645 (expect nil
5646 (with-current-buffer (get-buffer-create " *00create+*")
5647 (puthash " *00create+*/xxx" 1 anything-tick-hash)
5648 (kill-buffer (current-buffer)))
5649 (gethash " *00create+*/xxx" anything-tick-hash))
5650 (desc "anything-execute-action-at-once-if-once")
5651 (expect "HOGE"
5652 (let ((anything-execute-action-at-once-if-one t))
5653 (anything '(((name . "one test1")
5654 (candidates "hoge")
5655 (action . upcase))))))
5656 (expect "ANY"
5657 (let ((anything-execute-action-at-once-if-one t))
5658 (anything '(((name . "one test2")
5659 (candidates "hoge" "any")
5660 (action . upcase)))
5661 "an")))
5662 ;; candidates > 1
5663 (expect (mock (read-string "word: " nil))
5664 (let ((anything-execute-action-at-once-if-one t))
5665 (anything '(((name . "one test3")
5666 (candidates "hoge" "foo" "bar")
5667 (action . identity)))
5668 nil "word: ")))
5669 (desc "anything-quit-if-no-candidate")
5670 (expect nil
5671 (let ((anything-quit-if-no-candidate t))
5672 (anything '(((name . "zero test1") (candidates) (action . upcase))))))
5673 (expect 'called
5674 (let (v (anything-quit-if-no-candidate (lambda () (setq v 'called))))
5675 (anything '(((name . "zero test2") (candidates) (action . upcase))))
5677 (desc "real-to-display attribute")
5678 (expect '(("test" (("DDD" . "ddd"))))
5679 (anything-test-candidates '(((name . "test")
5680 (candidates "ddd")
5681 (real-to-display . upcase)
5682 (action . identity)))))
5683 (expect '(("test" (("DDD" . "ddd"))))
5684 (anything-test-candidates '(((name . "test")
5685 (candidates ("ignored" . "ddd"))
5686 (real-to-display . upcase)
5687 (action . identity)))))
5688 (expect '(("Commands" (("xxxhoge" . "hoge") ("xxxboke" . "boke"))))
5689 (anything-test-candidates '(((name . "Commands")
5690 (candidates
5691 "hoge" "boke")
5692 (real-to-display . (lambda (x) (concat "xxx" x)))
5693 (action . identity)))
5694 "xxx"))
5695 (expect "test\nDDD\n"
5696 (anything-test-update '(((name . "test")
5697 (candidates "ddd")
5698 (real-to-display . upcase)
5699 (action . identity)))
5701 (with-current-buffer (anything-buffer-get) (buffer-string)))
5702 (desc "real-to-display and candidate-transformer attribute")
5703 (expect '(("test" (("DDD" . "ddd"))))
5704 (anything-test-candidates
5705 '(((name . "test")
5706 (candidates "ddd")
5707 (candidate-transformer (lambda (cands) (mapcar (lambda (c) (cons "X" c)) cands)))
5708 (real-to-display . upcase)
5709 (action . identity)))))
5710 (expect "test\nDDD\n"
5711 (anything-test-update
5712 '(((name . "test")
5713 (candidates "ddd")
5714 (candidate-transformer (lambda (cands) (mapcar (lambda (c) (cons "X" c)) cands)))
5715 (real-to-display . upcase)
5716 (action . identity)))
5718 (with-current-buffer (anything-buffer-get) (buffer-string)))
5719 (desc "real-to-display and candidates-in-buffer")
5720 (expect '(("test" (("A" . "a") ("B" . "b"))))
5721 (anything-test-candidates
5722 '(((name . "test")
5723 (init
5724 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5725 (erase-buffer)
5726 (insert "a\nb\n"))))
5727 (candidates-in-buffer)
5728 (real-to-display . upcase)
5729 (action . identity)))))
5730 (expect "test\nA\nB\n"
5731 (stub read-string)
5732 (anything
5733 '(((name . "test")
5734 (init
5735 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5736 (erase-buffer)
5737 (insert "a\nb\n"))))
5738 (candidates-in-buffer)
5739 (real-to-display . upcase)
5740 (action . identity))))
5741 (with-current-buffer (anything-buffer-get) (buffer-string)))
5742 (desc "Symbols are acceptable as candidate.")
5743 (expect '(("test" (sym "str")))
5744 (anything-test-candidates
5745 '(((name . "test")
5746 (candidates sym "str")))))
5747 (expect '(("test" ((sym . realsym) ("str" . "realstr"))))
5748 (anything-test-candidates
5749 '(((name . "test")
5750 (candidates (sym . realsym) ("str" . "realstr"))))))
5751 (expect '(("test" (sym)))
5752 (anything-test-candidates
5753 '(((name . "test")
5754 (candidates sym "str")))
5755 "sym"))
5756 (expect '(("test" ("str")))
5757 (anything-test-candidates
5758 '(((name . "test")
5759 (candidates sym "str")))
5760 "str"))
5761 (expect '(("test" ((sym . realsym))))
5762 (anything-test-candidates
5763 '(((name . "test")
5764 (candidates (sym . realsym) ("str" . "realstr"))))
5765 "sym"))
5766 (expect '(("test" (("str" . "realstr"))))
5767 (anything-test-candidates
5768 '(((name . "test")
5769 (candidates (sym . realsym) ("str" . "realstr"))))
5770 "str"))
5771 (desc "multiple transformers")
5772 (expect '(("test" ("<FOO>")))
5773 (anything-test-candidates
5774 '(((name . "test")
5775 (candidates "foo")
5776 (candidate-transformer
5777 . (lambda (cands)
5778 (anything-compose (list cands)
5779 (list (lambda (c) (mapcar 'upcase c))
5780 (lambda (c) (list (concat "<" (car c) ">")))))))))))
5781 (expect '("<FOO>")
5782 (anything-composed-funcall-with-source
5783 '((name . "test"))
5784 (list (lambda (c) (mapcar 'upcase c))
5785 (lambda (c) (list (concat "<" (car c) ">"))))
5786 '("foo"))
5788 (expect '(("test" ("<FOO>")))
5789 (anything-test-candidates
5790 '(((name . "test")
5791 (candidates "foo")
5792 (candidate-transformer
5793 (lambda (c) (mapcar 'upcase c))
5794 (lambda (c) (list (concat "<" (car c) ">"))))))))
5795 (expect '(("test" ("<BAR>")))
5796 (anything-test-candidates
5797 '(((name . "test")
5798 (candidates "bar")
5799 (filtered-candidate-transformer
5800 (lambda (c s) (mapcar 'upcase c))
5801 (lambda (c s) (list (concat "<" (car c) ">"))))))))
5802 (expect '(("find-file" . find-file)
5803 ("view-file" . view-file))
5804 (stub zerop => nil)
5805 (stub anything-get-current-source
5806 => '((name . "test")
5807 (action)
5808 (action-transformer
5809 . (lambda (a s)
5810 (anything-compose
5811 (list a s)
5812 (list (lambda (a s) (push '("view-file" . view-file) a))
5813 (lambda (a s) (push '("find-file" . find-file) a))))))))
5814 (anything-get-action))
5815 (expect '(("find-file" . find-file)
5816 ("view-file" . view-file))
5817 (stub zerop => nil)
5818 (stub anything-get-current-source
5819 => '((name . "test")
5820 (action)
5821 (action-transformer
5822 (lambda (a s) (push '("view-file" . view-file) a))
5823 (lambda (a s) (push '("find-file" . find-file) a)))))
5824 (anything-get-action))
5825 (desc "define-anything-type-attribute")
5826 (expect '((file (action . find-file)))
5827 (let (anything-type-attributes)
5828 (define-anything-type-attribute 'file '((action . find-file)))
5829 anything-type-attributes))
5830 (expect '((file (action . find-file)))
5831 (let ((anything-type-attributes '((file (action . view-file)))))
5832 (define-anything-type-attribute 'file '((action . find-file)))
5833 anything-type-attributes))
5834 (expect '((file (action . find-file))
5835 (buffer (action . switch-to-buffer)))
5836 (let (anything-type-attributes)
5837 (define-anything-type-attribute 'buffer '((action . switch-to-buffer)))
5838 (define-anything-type-attribute 'file '((action . find-file)))
5839 anything-type-attributes))
5840 (desc "anything-approximate-candidate-number")
5841 (expect 0
5842 (with-temp-buffer
5843 (let ((anything-buffer (current-buffer)))
5844 (anything-approximate-candidate-number))))
5845 (expect 1
5846 (with-temp-buffer
5847 (let ((anything-buffer (current-buffer)))
5848 (insert "Title\n"
5849 "candiate1\n")
5850 (anything-approximate-candidate-number))))
5851 (expect t
5852 (with-temp-buffer
5853 (let ((anything-buffer (current-buffer)))
5854 (insert "Title\n"
5855 "candiate1\n"
5856 "candiate2\n")
5857 (<= 2 (anything-approximate-candidate-number)))))
5858 (expect 1
5859 (with-temp-buffer
5860 (let ((anything-buffer (current-buffer)))
5861 (insert "Title\n"
5862 (propertize "multi\nline\n" 'anything-multiline t))
5863 (anything-approximate-candidate-number))))
5864 (expect t
5865 (with-temp-buffer
5866 (let ((anything-buffer (current-buffer))
5867 (anything-candidate-separator "-----"))
5868 (insert "Title\n"
5869 (propertize "multi\nline1\n" 'anything-multiline t)
5870 "-----\n"
5871 (propertize "multi\nline2\n" 'anything-multiline t))
5872 (<= 2 (anything-approximate-candidate-number)))))
5873 (desc "delayed-init attribute")
5874 (expect 0
5875 (let ((value 0))
5876 (anything-test-candidates '(((name . "test")
5877 (delayed-init . (lambda () (incf value)))
5878 (candiates "abc")
5879 (requires-pattern . 2)))
5881 value))
5882 (expect 1
5883 (let ((value 0))
5884 (anything-test-candidates '(((name . "test")
5885 (delayed-init . (lambda () (incf value)))
5886 (candiates "abc")
5887 (requires-pattern . 2)))
5888 "abc")
5889 value))
5890 (expect 2
5891 (let ((value 0))
5892 (anything-test-candidates '(((name . "test")
5893 (delayed-init (lambda () (incf value))
5894 (lambda () (incf value)))
5895 (candiates "abc")
5896 (requires-pattern . 2)))
5897 "abc")
5898 value))
5899 (expect t
5900 (let (value)
5901 (with-temp-buffer
5902 (anything-test-candidates '(((name . "test")
5903 (delayed-init
5904 . (lambda () (setq value
5905 (eq anything-current-buffer (current-buffer)))))
5906 (candiates "abc")
5907 (requires-pattern . 2)))
5908 "abc")
5909 value)))
5910 (desc "pattern-transformer attribute")
5911 (expect '(("test2" ("foo")) ("test3" ("bar")))
5912 (anything-test-candidates '(((name . "test1")
5913 (candidates "foo" "bar"))
5914 ((name . "test2")
5915 (pattern-transformer . (lambda (pat) (substring pat 1)))
5916 (candidates "foo" "bar"))
5917 ((name . "test3")
5918 (pattern-transformer . (lambda (pat) "bar"))
5919 (candidates "foo" "bar")))
5920 "xfoo"))
5921 (expect '(("test2" ("foo")) ("test3" ("bar")))
5922 (anything-test-candidates '(((name . "test1")
5923 (candidates "foo" "bar"))
5924 ((name . "test2")
5925 (pattern-transformer (lambda (pat) (substring pat 1)))
5926 (candidates "foo" "bar"))
5927 ((name . "test3")
5928 (pattern-transformer (lambda (pat) "bar"))
5929 (candidates "foo" "bar")))
5930 "xfoo"))
5931 (expect '(("test2" ("foo")) ("test3" ("bar")))
5932 (anything-test-candidates '(((name . "test1")
5933 (init
5934 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5935 (insert "foo\nbar\n"))))
5936 (candidates-in-buffer))
5937 ((name . "test2")
5938 (pattern-transformer . (lambda (pat) (substring pat 1)))
5939 (init
5940 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5941 (insert "foo\nbar\n"))))
5942 (candidates-in-buffer))
5943 ((name . "test3")
5944 (pattern-transformer . (lambda (pat) "bar"))
5945 (init
5946 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5947 (insert "foo\nbar\n"))))
5948 (candidates-in-buffer)))
5949 "xfoo"))
5950 (desc "anything-recent-push")
5951 (expect '("foo" "bar" "baz")
5952 (let ((lst '("bar" "baz")))
5953 (anything-recent-push "foo" 'lst)))
5954 (expect '("foo" "bar" "baz")
5955 (let ((lst '("foo" "bar" "baz")))
5956 (anything-recent-push "foo" 'lst)))
5957 (expect '("foo" "bar" "baz")
5958 (let ((lst '("bar" "foo" "baz")))
5959 (anything-recent-push "foo" 'lst)))
5960 (desc "anything-require-at-least-version")
5961 (expect nil
5962 (anything-require-at-least-version "1.1"))
5963 (expect nil
5964 (anything-require-at-least-version "1.200"))
5965 (expect nil
5966 (anything-require-at-least-version
5967 (and (string-match "1\.\\([0-9]+\\)" anything-version)
5968 (match-string 0 anything-version))))
5969 (expect (error)
5970 (anything-require-at-least-version "1.999"))
5971 (expect (error)
5972 (anything-require-at-least-version "1.2000"))
5973 (desc "anything-once")
5974 (expect 2
5975 (let ((i 0))
5976 (anything-test-candidates
5977 '(((name . "1")
5978 (init . (lambda () (incf i))))
5979 ((name . "2")
5980 (init . (lambda () (incf i))))))
5982 (expect 1
5983 (let ((i 0))
5984 (anything-test-candidates
5985 '(((name . "1")
5986 (init . (lambda () (anything-once (lambda () (incf i))))))
5987 ((name . "2")
5988 (init . (lambda () (anything-once (lambda () (incf i))))))))
5990 (expect 1
5991 (let ((i 0))
5992 (flet ((init1 () (anything-once (lambda () (incf i)))))
5993 (anything-test-candidates
5994 '(((name . "1")
5995 (init . init1))
5996 ((name . "2")
5997 (init . init1)))))
5999 (desc "anything-marked-candidates")
6000 (expect '("mark3" "mark1")
6001 (let* ((source '((name . "mark test")))
6002 (anything-marked-candidates
6003 `((,source . "mark1")
6004 (((name . "other")) . "mark2")
6005 (,source . "mark3"))))
6006 (stub anything-buffer-get => (current-buffer))
6007 (stub anything-get-current-source => source)
6008 (anything-marked-candidates)))
6009 (expect '("current")
6010 (let* ((source '((name . "mark test")))
6011 (anything-marked-candidates nil))
6012 (stub anything-get-current-source => source)
6013 (stub anything-get-selection => "current")
6014 (anything-marked-candidates)))
6015 (desc "anything-marked-candidates with coerce")
6016 (expect '(mark3 mark1)
6017 (let* ((source '((name . "mark test")
6018 (coerce . intern)))
6019 (anything-marked-candidates
6020 `((,source . "mark1")
6021 (((name . "other")) . "mark2")
6022 (,source . "mark3"))))
6023 (stub anything-buffer-get => (current-buffer))
6024 (stub anything-get-current-source => source)
6025 (anything-marked-candidates)))
6026 (desc "anything-let")
6027 (expect '(1 10000 nil)
6028 (let ((a 9999)
6029 (b 8)
6031 (anything-buffer (exps-tmpbuf)))
6032 (anything-let ((a 1)
6033 (b (1+ a))
6035 (anything-create-anything-buffer))
6036 (with-current-buffer anything-buffer
6037 (list a b c))))
6038 (expect (non-nil)
6039 (let ((a 9999)
6040 (b 8)
6042 (anything-buffer (exps-tmpbuf)))
6043 (anything-let ((a 1)
6044 (b (1+ a))
6046 (anything-create-anything-buffer))
6047 (with-current-buffer anything-buffer
6048 (and (assq 'a (buffer-local-variables))
6049 (assq 'b (buffer-local-variables))
6050 (assq 'c (buffer-local-variables))))))
6051 (expect 'retval
6052 (let ((a 9999)
6053 (b 8)
6055 (anything-buffer (exps-tmpbuf)))
6056 (anything-let ((a 1)
6057 (b (1+ a))
6059 'retval)))
6060 (desc "anything-let*")
6061 (expect '(1 2 nil)
6062 (let ((a 9999)
6063 (b 8)
6065 (anything-buffer (exps-tmpbuf)))
6066 (anything-let* ((a 1)
6067 (b (1+ a))
6069 (anything-create-anything-buffer))
6070 (with-current-buffer anything-buffer
6071 (list a b c))))
6072 (expect (non-nil)
6073 (let ((a 9999)
6074 (b 8)
6076 (anything-buffer (exps-tmpbuf)))
6077 (anything-let* ((a 1)
6078 (b (1+ a))
6080 (anything-create-anything-buffer))
6081 (with-current-buffer anything-buffer
6082 (and (assq 'a (buffer-local-variables))
6083 (assq 'b (buffer-local-variables))
6084 (assq 'c (buffer-local-variables))))))
6085 (expect 'retval*
6086 (let ((a 9999)
6087 (b 8)
6089 (anything-buffer (exps-tmpbuf)))
6090 (anything-let* ((a 1)
6091 (b (1+ a))
6093 'retval*)))
6094 (desc "anything with keyword")
6095 (expect (mock (anything-internal 'test-source "input" "prompt: " nil "preselect" "*test*" nil))
6096 (anything :sources 'test-source
6097 :input "input"
6098 :prompt "prompt: "
6099 :resume nil
6100 :preselect "preselect"
6101 :buffer "*test*"
6102 :keymap nil))
6103 (expect (mock (anything-internal 'test-source nil nil nil nil "*test*" nil))
6104 (anything :sources 'test-source
6105 :buffer "*test*"
6106 :candidate-number-limit 20))
6107 (expect (mock (anything-internal 'test-source nil nil nil nil "*test*" nil))
6108 (anything 'test-source nil nil nil nil "*test*" nil))
6109 (desc "anything-log-eval-internal")
6110 (expect (mock (anything-log "%S = %S" '(+ 1 2) 3))
6111 (anything-log-eval-internal '((+ 1 2))))
6112 (expect (mock (anything-log "%S = ERROR!" 'unDeFined))
6113 (anything-log-eval-internal '(unDeFined)))
6115 (desc "anything-output-filter--collect-candidates")
6116 (expect '("a" "b" "")
6117 (split-string "a\nb\n" "\n"))
6118 (expect '("a" "b")
6119 (anything-output-filter--collect-candidates
6120 '("a" "b" "") (cons 'incomplete-line "")))
6121 (expect '("a" "b")
6122 (split-string "a\nb" "\n"))
6123 (expect '("a")
6124 (anything-output-filter--collect-candidates
6125 '("a" "b") (cons 'incomplete-line "")))
6126 (expect '(incomplete-line . "b")
6127 (let ((incomplete-line-info (cons 'incomplete-line "")))
6128 (anything-output-filter--collect-candidates
6129 '("a" "b") incomplete-line-info)
6130 incomplete-line-info))
6131 (expect '("" "c" "")
6132 (split-string "\nc\n" "\n"))
6133 (expect '("b" "c")
6134 ;; "a\nb" + "\nc\n"
6135 (let ((incomplete-line-info (cons 'incomplete-line "")))
6136 (anything-output-filter--collect-candidates
6137 '("a" "b") incomplete-line-info)
6138 (anything-output-filter--collect-candidates
6139 '("" "c" "") incomplete-line-info)))
6140 (desc "coerce attribute")
6141 (expect "string"
6142 (anything :sources '(((name . "test")
6143 (candidates "string")
6144 (action . identity)))
6145 :execute-action-at-once-if-one t))
6146 (expect 'symbol
6147 (anything :sources '(((name . "test")
6148 (candidates "symbol")
6149 (coerce . intern)
6150 (action . identity)))
6151 :execute-action-at-once-if-one t))
6152 (expect 'real
6153 (anything :sources '(((name . "test")
6154 (candidates ("display" . "real"))
6155 (coerce . intern)
6156 (action . identity)))
6157 :execute-action-at-once-if-one t))
6158 (expect 'real
6159 (anything :sources '(((name . "test")
6160 (candidates)
6161 (candidate-transformer
6162 (lambda (c) '(("display" . "real"))))
6163 (coerce . intern)
6164 (action . identity)))
6165 :execute-action-at-once-if-one t))
6166 (expect 'real
6167 (anything :sources '(((name . "test")
6168 (candidates)
6169 (filtered-candidate-transformer
6170 (lambda (c s) '(("display" . "real"))))
6171 (coerce . intern)
6172 (action . identity)))
6173 :execute-action-at-once-if-one t))
6174 (expect 'real
6175 (anything :sources '(((name . "test")
6176 (candidates "dummy")
6177 (display-to-real (lambda (disp) "real"))
6178 (coerce . intern)
6179 (action . identity)))
6180 :execute-action-at-once-if-one t))
6181 (desc "anything-next-point-in-list")
6182 (expect 10
6183 (anything-next-point-in-list 5 '(10 20) nil))
6184 (expect 20
6185 (anything-next-point-in-list 15 '(10 20) nil))
6186 (expect 25
6187 (anything-next-point-in-list 25 '(10 20) nil))
6188 (expect 5
6189 (anything-next-point-in-list 5 '(10 20) t))
6190 (expect 10
6191 (anything-next-point-in-list 15 '(10 20) t))
6192 (expect 20
6193 (anything-next-point-in-list 25 '(10 20) t))
6194 (expect 5
6195 (anything-next-point-in-list 5 '() nil))
6196 (expect 5
6197 (anything-next-point-in-list 5 '() t))
6198 (expect 10
6199 (anything-next-point-in-list 5 '(10) nil))
6200 (expect 10
6201 (anything-next-point-in-list 15 '(10) t))
6202 (expect 20
6203 (anything-next-point-in-list 10 '(10 20) nil))
6204 (expect 10
6205 (anything-next-point-in-list 20 '(10 20) t))
6206 (expect 20
6207 (anything-next-point-in-list 30 '(10 20 30) t))
6211 (provide 'anything)
6212 ;; How to save (DO NOT REMOVE!!)
6213 ;; (progn (magit-push) (emacswiki-post "anything.el"))
6214 ;; Local Variables:
6215 ;; coding: utf-8
6216 ;; End:
6217 ;;; anything.el ends here