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