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