anything.el (anything-open-last-log): New command
[anything-config.git] / anything.el
blob538aecff6d206e176b26935c06620bbfea445957
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.286")
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 (declare-function 'anything-frame/window-configuration "anything")
1660 (lexical-let (conf)
1661 (defun anything-frame/window-configuration (save-or-restore)
1662 (anything-log-eval anything-save-configuration-functions)
1663 (case save-or-restore
1664 (save (setq conf (funcall (cdr anything-save-configuration-functions))))
1665 (restore (funcall (car anything-save-configuration-functions) conf)))))
1667 ;; (@* "Core: Display *anything* buffer")
1668 (defun anything-display-buffer (buf)
1669 "Display *anything* buffer."
1670 (funcall (with-current-buffer buf anything-display-function) buf))
1672 (defun anything-default-display-buffer (buf)
1673 (funcall (if anything-samewindow 'switch-to-buffer 'pop-to-buffer) buf))
1675 ;; (@* "Core: initialize")
1676 (defun anything-initialize ()
1677 "Initialize anything settings and set up the anything buffer."
1678 (anything-log-run-hook 'anything-before-initialize-hook)
1679 (setq anything-once-called-functions nil)
1680 (setq anything-delayed-init-executed nil)
1681 (setq anything-current-buffer (current-buffer))
1682 (setq anything-buffer-file-name buffer-file-name)
1683 (setq anything-issued-errors nil)
1684 (setq anything-compiled-sources nil)
1685 (setq anything-saved-current-source nil)
1686 ;; Call the init function for sources where appropriate
1687 (anything-funcall-foreach 'init)
1689 (setq anything-pattern "")
1690 (setq anything-input "")
1691 (setq anything-candidate-cache nil)
1692 (setq anything-last-sources anything-sources)
1694 (anything-create-anything-buffer)
1695 (anything-log-run-hook 'anything-after-initialize-hook))
1697 (defun anything-read-pattern-maybe (any-prompt any-input any-preselect any-resume any-keymap)
1698 (if (anything-resume-p any-resume)
1699 (anything-mark-current-line)
1700 (anything-update))
1701 (select-frame-set-input-focus (window-frame (minibuffer-window)))
1702 (anything-preselect any-preselect)
1703 (let ((minibuffer-local-map
1704 (with-current-buffer (anything-buffer-get)
1705 (and any-keymap (set (make-local-variable 'anything-map) any-keymap))
1706 anything-map)))
1707 (anything-log-eval (anything-approximate-candidate-number)
1708 anything-execute-action-at-once-if-one
1709 anything-quit-if-no-candidate)
1710 (cond ((and anything-execute-action-at-once-if-one
1711 (= (anything-approximate-candidate-number) 1))
1712 (ignore))
1713 ((and anything-quit-if-no-candidate
1714 (= (anything-approximate-candidate-number) 0))
1715 (setq anything-quit t)
1716 (and (functionp anything-quit-if-no-candidate)
1717 (funcall anything-quit-if-no-candidate)))
1719 (read-string (or any-prompt "pattern: ") any-input)))))
1721 (defun anything-create-anything-buffer (&optional test-mode)
1722 "Create newly created `anything-buffer'.
1723 If TEST-MODE is non-nil, clear `anything-candidate-cache'."
1724 (when test-mode
1725 (setq anything-candidate-cache nil))
1726 (with-current-buffer (get-buffer-create anything-buffer)
1727 (anything-log "kill local variables: %S" (buffer-local-variables))
1728 (kill-all-local-variables)
1729 (buffer-disable-undo)
1730 (erase-buffer)
1731 (set (make-local-variable 'inhibit-read-only) t)
1732 (set (make-local-variable 'anything-last-sources-local) anything-sources)
1733 (set (make-local-variable 'anything-follow-mode) nil)
1734 (set (make-local-variable 'anything-display-function) anything-display-function)
1735 (anything-log-eval anything-display-function anything-let-variables)
1736 (loop for (var . val) in anything-let-variables
1737 do (set (make-local-variable var) val))
1739 (setq cursor-type nil)
1740 (setq mode-name "Anything"))
1741 (anything-initialize-overlays anything-buffer)
1742 (get-buffer anything-buffer))
1744 (defun anything-initialize-overlays (buffer)
1745 (anything-log "overlay setup")
1746 (if anything-selection-overlay
1747 ;; make sure the overlay belongs to the anything buffer if
1748 ;; it's newly created
1749 (move-overlay anything-selection-overlay (point-min) (point-min)
1750 (get-buffer buffer))
1752 (setq anything-selection-overlay
1753 (make-overlay (point-min) (point-min) (get-buffer buffer)))
1754 (overlay-put anything-selection-overlay 'face anything-selection-face))
1756 (cond (anything-enable-shortcuts
1757 (setq anything-shortcut-keys
1758 (assoc-default anything-enable-shortcuts anything-shortcut-keys-alist))
1759 (unless anything-digit-overlays
1760 (setq anything-digit-overlays
1761 (loop for key across anything-shortcut-keys
1762 for overlay = (make-overlay (point-min) (point-min)
1763 (get-buffer buffer))
1764 do (overlay-put overlay 'before-string
1765 (format "%s - " (upcase (make-string 1 key))))
1766 collect overlay))))
1767 (anything-digit-overlays
1768 (mapc 'delete-overlay anything-digit-overlays)
1769 (setq anything-digit-overlays nil))))
1771 (defun anything-hooks (setup-or-cleanup)
1772 (let ((hooks '((deferred-action-list anything-check-minibuffer-input)
1773 (minibuffer-setup-hook anything-print-error-messages))))
1774 (if (eq setup-or-cleanup 'setup)
1775 (dolist (args hooks) (apply 'add-hook args))
1776 (dolist (args (reverse hooks)) (apply 'remove-hook args)))))
1778 ;; (@* "Core: clean up")
1779 ;;; TODO move
1780 (defun anything-cleanup ()
1781 "Clean up the mess."
1782 (anything-log "start cleanup")
1783 (with-current-buffer anything-buffer
1784 (setq cursor-type t))
1785 (bury-buffer anything-buffer)
1786 (anything-funcall-foreach 'cleanup)
1787 (anything-new-timer 'anything-check-minibuffer-input-timer nil)
1788 (anything-kill-async-processes)
1789 (anything-log-run-hook 'anything-cleanup-hook)
1790 (anything-hooks 'cleanup)
1791 (anything-frame/window-configuration 'restore))
1793 ;; (@* "Core: input handling")
1794 (defun anything-check-minibuffer-input ()
1795 "Extract input string from the minibuffer and check if it needs
1796 to be handled."
1797 (let ((delay (with-current-buffer anything-buffer anything-input-idle-delay)))
1798 (if (or (not delay) (anything-action-window))
1799 (anything-check-minibuffer-input-1)
1800 (anything-new-timer
1801 'anything-check-minibuffer-input-timer
1802 (run-with-idle-timer delay nil 'anything-check-minibuffer-input-1)))))
1804 (defun anything-check-minibuffer-input-1 ()
1805 (with-anything-quittable
1806 (with-selected-window (minibuffer-window)
1807 (anything-check-new-input (minibuffer-contents)))))
1809 (defun anything-check-new-input (input)
1810 "Check input string and update the anything buffer if
1811 necessary."
1812 (unless (equal input anything-pattern)
1813 (setq anything-pattern input)
1814 (unless (anything-action-window)
1815 (setq anything-input anything-pattern))
1816 (anything-log-eval anything-pattern anything-input)
1817 (anything-update)))
1819 ;; (@* "Core: source compiler")
1820 (defvar anything-compile-source-functions-default anything-compile-source-functions
1821 "Plug-ins this file provides.")
1822 (defun anything-compile-sources (sources funcs)
1823 "Compile sources (`anything-sources') with funcs (`anything-compile-source-functions').
1824 Anything plug-ins are realized by this function."
1825 (mapcar
1826 (lambda (source)
1827 (loop with source = (if (listp source) source (symbol-value source))
1828 for f in funcs
1829 do (setq source (funcall f source))
1830 finally (return source)))
1831 sources))
1833 ;; (@* "Core: plug-in attribute documentation hack")
1835 ;; `anything-document-attribute' is public API.
1836 (defadvice documentation-property (after anything-document-attribute activate)
1837 "Hack to display plug-in attributes' documentation as `anything-sources' docstring."
1838 (when (eq symbol 'anything-sources)
1839 (setq ad-return-value
1840 (concat ad-return-value "\n"
1841 (mapconcat (lambda (sym) (get sym 'anything-attrdoc))
1842 anything-additional-attributes
1843 "\n")))))
1844 ;; (describe-variable 'anything-sources)
1845 ;; (documentation-property 'anything-sources 'variable-documentation)
1846 ;; (progn (ad-disable-advice 'documentation-property 'after 'anything-document-attribute) (ad-update 'documentation-property))
1848 ;; (@* "Core: all candidates")
1849 (defun anything-process-delayed-init (source)
1850 (let ((name (assoc-default 'name source)))
1851 (unless (member name anything-delayed-init-executed)
1852 (anything-aif (assoc-default 'delayed-init source)
1853 (with-current-buffer anything-current-buffer
1854 (anything-funcall-with-source source it)
1855 (dolist (f (if (functionp it) (list it) it))
1856 (add-to-list 'anything-delayed-init-executed name)))))))
1858 (defun anything-get-candidates (source)
1859 "Retrieve and return the list of candidates from
1860 SOURCE."
1861 (anything-process-delayed-init source)
1862 (let* ((candidate-source (assoc-default 'candidates source))
1863 (type-error (lambda ()
1864 (error (concat "Candidates must either be a function, "
1865 " a variable or a list: %s")
1866 candidate-source)))
1867 (candidates (condition-case err
1868 (anything-interpret-value candidate-source source)
1869 (error (funcall type-error)))))
1870 (cond ((processp candidates) candidates)
1871 ((listp candidates) (anything-transform-candidates candidates source))
1872 (t (funcall type-error)))))
1875 (defun anything-get-cached-candidates (source)
1876 "Return the cached value of candidates for SOURCE.
1877 Cache the candidates if there is not yet a cached value."
1878 (let* ((name (assoc-default 'name source))
1879 (candidate-cache (assoc name anything-candidate-cache)))
1880 (cond (candidate-cache
1881 (anything-log "use cached candidates")
1882 (cdr candidate-cache))
1884 (anything-log "calculate candidates")
1885 (let ((candidates (anything-get-candidates source)))
1886 (cond ((processp candidates)
1887 (push (cons candidates
1888 (append source
1889 (list (cons 'item-count 0)
1890 (cons 'incomplete-line ""))))
1891 anything-async-processes)
1892 (set-process-filter candidates 'anything-output-filter)
1893 (setq candidates nil))
1894 ((not (assoc 'volatile source))
1895 (setq candidate-cache (cons name candidates))
1896 (push candidate-cache anything-candidate-cache)))
1897 candidates)))))
1899 ;;; (@* "Core: candidate transformers")
1900 (defun anything-process-candidate-transformer (candidates source)
1901 (anything-aif (assoc-default 'candidate-transformer source)
1902 (anything-composed-funcall-with-source source it candidates)
1903 candidates))
1904 (defun anything-process-filtered-candidate-transformer (candidates source)
1905 (anything-aif (assoc-default 'filtered-candidate-transformer source)
1906 (anything-composed-funcall-with-source source it candidates source)
1907 candidates))
1908 (defun anything-process-filtered-candidate-transformer-maybe (candidates source process-p)
1909 (if process-p
1910 (anything-process-filtered-candidate-transformer candidates source)
1911 candidates))
1912 (defun anything-process-real-to-display (candidates source)
1913 (anything-aif (assoc-default 'real-to-display source)
1914 (setq candidates (anything-funcall-with-source
1915 source 'mapcar
1916 (lambda (cand_)
1917 (if (consp cand_)
1918 ;; override DISPLAY from candidate-transformer
1919 (cons (funcall it (cdr cand_)) (cdr cand_))
1920 (cons (funcall it cand_) cand_)))
1921 candidates))
1922 candidates))
1923 (defun anything-transform-candidates (candidates source &optional process-p)
1924 "Transform CANDIDATES according to candidate transformers."
1925 (anything-process-real-to-display
1926 (anything-process-filtered-candidate-transformer-maybe
1927 (anything-process-candidate-transformer candidates source) source process-p)
1928 source))
1931 ;; (@* "Core: narrowing candidates")
1932 (defun anything-candidate-number-limit (source)
1933 "`anything-candidate-number-limit' variable may be overridden by SOURCE.
1934 If (candidate-number-limit) is in SOURCE, show all candidates in SOURCE,
1935 ie. cancel the effect of `anything-candidate-number-limit'."
1936 (anything-aif (assq 'candidate-number-limit source)
1937 (or (cdr it) 99999999)
1938 (or anything-candidate-number-limit 99999999)))
1940 (defconst anything-default-match-functions
1941 (list (lambda (candidate)
1942 (string-match anything-pattern candidate))))
1944 (defun anything-compute-matches (source)
1945 "Compute matches from SOURCE according to its settings."
1946 (if debug-on-error
1947 (anything-compute-matches-internal source)
1948 (condition-case v
1949 (anything-compute-matches-internal source)
1950 (error (anything-log-error
1951 "anything-compute-matches: error when processing source: %s"
1952 (assoc-default 'name source))
1953 nil))))
1955 (defun anything-candidate-get-display (candidate)
1956 "Get display part (searched) from CANDIDATE.
1957 CANDIDATE is a string, a symbol, or (DISPLAY . REAL) cons cell."
1958 (format "%s" (or (car-safe candidate) candidate)))
1960 (defun anything-process-pattern-transformer (pattern source)
1961 (anything-aif (assoc-default 'pattern-transformer source)
1962 (anything-composed-funcall-with-source source it pattern)
1963 pattern))
1965 (defun anything-match-functions (source)
1966 (or (assoc-default 'match source)
1967 anything-default-match-functions))
1969 (defmacro anything-accumulate-candidates-internal (cand newmatches hash item-count limit)
1970 "INTERNAL: add CAND (ITEM-COUNT th match) into NEWMATCHES.
1971 Use HASH to uniq NEWMATCHES.
1972 if ITEM-COUNT reaches LIMIT, exit from inner loop."
1973 `(unless (gethash ,cand ,hash)
1974 (puthash ,cand t ,hash)
1975 (push ,cand ,newmatches)
1976 (incf ,item-count)
1977 (when (= ,item-count ,limit)
1978 (setq exit t)
1979 (return))))
1981 (defun anything-take-first-elements (seq n)
1982 (if (> (length seq) n)
1983 (setq seq (subseq seq 0 n))
1984 seq))
1986 (defun anything-match-from-candidates (cands matchfns limit)
1987 (let (matches)
1988 (condition-case nil
1989 (let ((item-count 0) exit)
1990 (clrhash anything-match-hash)
1991 (dolist (match matchfns)
1992 (let (newmatches)
1993 (dolist (candidate cands)
1994 (when (funcall match (anything-candidate-get-display candidate))
1995 (anything-accumulate-candidates-internal
1996 candidate newmatches anything-match-hash item-count limit)))
1997 (setq matches (append matches (reverse newmatches)))
1998 (if exit (return)))))
1999 (invalid-regexp (setq matches nil)))
2000 matches))
2002 (defun anything-compute-matches-internal (source)
2003 (let ((matchfns (anything-match-functions source))
2004 (anything-source-name (assoc-default 'name source))
2005 (limit (anything-candidate-number-limit source))
2006 (anything-pattern (anything-process-pattern-transformer
2007 anything-pattern source)))
2008 (anything-process-filtered-candidate-transformer
2009 (if (or (equal anything-pattern "") (equal matchfns '(identity)))
2010 (anything-take-first-elements
2011 (anything-get-cached-candidates source) limit)
2012 (anything-match-from-candidates
2013 (anything-get-cached-candidates source) matchfns limit))
2014 source)))
2016 ;; (anything '(((name . "error")(candidates . (lambda () (hage))) (action . identity))))
2018 (defun anything-process-source (source)
2019 "Display matches from SOURCE according to its settings."
2020 (anything-log-eval (assoc-default 'name source))
2021 (if (assq 'direct-insert-match source) ;experimental
2022 (anything-process-source--direct-insert-match source)
2023 (let ((matches (anything-compute-matches source)))
2024 (when matches
2025 (when anything-test-mode
2026 (setq anything-test-candidate-list
2027 `(,@anything-test-candidate-list
2028 (,(assoc-default 'name source)
2029 ,matches))))
2030 (anything-insert-header-from-source source)
2031 (if (not (assq 'multiline source))
2032 (mapc 'anything-insert-match-with-digit-overlay matches)
2033 (let ((start (point)) separate)
2034 (dolist (match matches)
2035 (if separate
2036 (anything-insert-candidate-separator)
2037 (setq separate t))
2038 (anything-insert-match-with-digit-overlay match))
2039 (put-text-property start (point) 'anything-multiline t)))))))
2041 (defun anything-insert-match-with-digit-overlay (match)
2042 (declare (special source))
2043 (anything-put-digit-overlay-maybe)
2044 (anything-insert-match match 'insert source))
2046 (defun anything-put-digit-overlay-maybe ()
2047 (when (and anything-enable-shortcuts
2048 (not (eq anything-digit-shortcut-count
2049 (length anything-digit-overlays))))
2050 (move-overlay (nth anything-digit-shortcut-count
2051 anything-digit-overlays)
2052 (point-at-bol)
2053 (point-at-bol))
2054 (incf anything-digit-shortcut-count)))
2056 (defun anything-process-source--direct-insert-match (source)
2057 "[EXPERIMENTAL] Insert candidates from `anything-candidate-buffer'"
2058 (anything-log-eval (assoc-default 'name source))
2059 (let ((anything-source-name (assoc-default 'name source))
2060 content-buf)
2061 (funcall (assoc-default 'candidates source))
2062 (setq content-buf (anything-candidate-buffer))
2063 (unless (anything-empty-buffer-p content-buf)
2064 (anything-insert-header-from-source source)
2065 (insert-buffer-substring content-buf)
2066 ;; TODO call anything-put-digit-overlay-maybe with loop
2069 (defun anything-process-delayed-sources (delayed-sources)
2070 "Process delayed sources if the user is idle for
2071 `anything-idle-delay' seconds."
2072 (with-anything-quittable
2073 (anything-log-eval (mapcar (lambda (s) (assoc-default 'name s)) delayed-sources))
2074 (with-current-buffer anything-buffer
2075 (save-excursion
2076 (goto-char (point-max))
2077 (mapc 'anything-process-source delayed-sources)
2078 (when (and (not (anything-empty-buffer-p))
2079 ;; no selection yet
2080 (= (overlay-start anything-selection-overlay)
2081 (overlay-end anything-selection-overlay)))
2082 (goto-char (point-min))
2083 (anything-next-line)))
2084 (save-excursion
2085 (goto-char (point-min))
2086 (anything-log-run-hook 'anything-update-hook))
2087 (anything-maybe-fit-frame))))
2089 ;; (@* "Core: *anything* buffer contents")
2090 (defvar anything-input-local nil)
2091 (defvar anything-process-delayed-sources-timer nil)
2092 (defun anything-update ()
2093 "Update the list of matches in the anything buffer according to
2094 the current pattern."
2095 (anything-log "start update")
2096 (setq anything-digit-shortcut-count 0)
2097 (anything-kill-async-processes)
2098 (with-current-buffer (anything-buffer-get)
2099 (set (make-local-variable 'anything-input-local) anything-pattern)
2100 (erase-buffer)
2101 (when anything-enable-shortcuts
2102 (mapc 'delete-overlay anything-digit-overlays))
2103 (let (delayed-sources)
2104 (unwind-protect
2105 (setq delayed-sources
2106 (loop for source in (remove-if-not 'anything-update-source-p
2107 (anything-get-sources))
2108 if (anything-delayed-source-p source)
2109 collect source
2110 else do (anything-process-source source)))
2111 (anything-log-eval
2112 (mapcar (lambda (s) (assoc-default 'name s)) delayed-sources))
2113 (anything-update-move-first-line)
2114 (if anything-test-mode
2115 (mapc 'anything-process-source delayed-sources)
2116 (anything-maybe-fit-frame)
2117 (when delayed-sources
2118 (anything-new-timer
2119 'anything-process-delayed-sources-timer
2120 (run-with-idle-timer
2121 anything-idle-delay nil
2122 'anything-process-delayed-sources delayed-sources)))
2123 ;; FIXME I want to execute anything-after-update-hook
2124 ;; AFTER processing delayed sources
2125 (anything-log-run-hook 'anything-after-update-hook))
2126 (anything-log "end update")))))
2128 (defun anything-update-source-p (source)
2129 (and (or (not anything-source-filter)
2130 (member (assoc-default 'name source) anything-source-filter))
2131 (>= (length anything-pattern)
2132 (anything-aif (assoc 'requires-pattern source)
2133 (or (cdr it) 1)
2134 0))))
2135 (defun anything-delayed-source-p (source)
2136 (or (assoc 'delayed source)
2137 (and anything-quick-update
2138 (< (window-height (get-buffer-window (current-buffer)))
2139 (line-number-at-pos (point-max))))))
2141 (defun anything-update-move-first-line ()
2142 (goto-char (point-min))
2143 (save-excursion (anything-log-run-hook 'anything-update-hook))
2144 (anything-next-line))
2146 (defun anything-force-update ()
2147 "Recalculate and update candidates.
2148 If current source has `update' attribute, a function without argument, call it before update."
2149 (interactive)
2150 (let ((source (anything-get-current-source)))
2151 (anything-aif (anything-funcall-with-source source 'anything-candidate-buffer)
2152 (kill-buffer it))
2153 (dolist (attr '(update init))
2154 (anything-aif (assoc-default attr source)
2155 (anything-funcall-with-source source it)))
2156 (anything-remove-candidate-cache source)
2157 (let ((selection (anything-get-selection nil t)))
2158 (anything-update)
2159 (anything-keep-selection source selection))))
2161 (defun anything-keep-selection (source selection)
2162 (with-anything-window
2163 (anything-goto-source source)
2164 (forward-char -1) ;back to \n
2165 (if (search-forward (concat "\n" selection "\n") nil t)
2166 (forward-line -1)
2167 (goto-char (point-min))
2168 (forward-line 1))
2169 (anything-mark-current-line)))
2171 (defun anything-remove-candidate-cache (source)
2172 (setq anything-candidate-cache
2173 (delete (assoc (assoc-default 'name source) anything-candidate-cache)
2174 anything-candidate-cache)))
2176 (defun anything-insert-match (match insert-function source)
2177 "Insert MATCH into the anything buffer. If MATCH is a list then
2178 insert the string inteneded to appear on the display and store
2179 the real value in a text property."
2180 (let ((start (point-at-bol (point)))
2181 (string (or (car-safe match) match))
2182 (realvalue (cdr-safe match)))
2183 (when (symbolp string) (setq string (symbol-name string)))
2184 (when (stringp string)
2185 (funcall insert-function string)
2186 ;; Some sources with candidates-in-buffer have already added
2187 ;; 'anything-realvalue property when creating candidate buffer.
2188 (unless (get-text-property start 'anything-realvalue)
2189 (and realvalue
2190 (put-text-property start (point-at-eol)
2191 'anything-realvalue realvalue)))
2192 (when anything-source-in-each-line-flag
2193 (put-text-property start (point-at-eol) 'anything-source source))
2194 (funcall insert-function "\n"))))
2196 (defun anything-insert-header-from-source (source)
2197 (let ((name (assoc-default 'name source)))
2198 (anything-insert-header
2199 name
2200 (anything-aif (assoc-default 'header-name source)
2201 (anything-funcall-with-source source it name)))))
2203 (defun anything-insert-header (name &optional display-string)
2204 "Insert header of source NAME into the anything buffer."
2205 (unless (bobp)
2206 (let ((start (point)))
2207 (insert "\n")
2208 (put-text-property start (point) 'anything-header-separator t)))
2210 (let ((start (point)))
2211 (insert name)
2212 (put-text-property (point-at-bol)
2213 (point-at-eol) 'anything-header t)
2214 (when display-string
2215 (overlay-put (make-overlay (point-at-bol) (point-at-eol))
2216 'display display-string))
2217 (insert "\n")
2218 (put-text-property start (point) 'face anything-header-face)))
2221 (defun anything-insert-candidate-separator ()
2222 "Insert separator of candidates into the anything buffer."
2223 (insert anything-candidate-separator)
2224 (put-text-property (point-at-bol)
2225 (point-at-eol) 'anything-candidate-separator t)
2226 (insert "\n"))
2231 ;; (@* "Core: async process")
2232 (defun anything-output-filter (process string)
2233 "Process output from PROCESS."
2234 (anything-output-filter-1 (assoc process anything-async-processes) string))
2236 (defun anything-output-filter-1 (process-assoc string)
2237 (anything-log-eval string)
2238 (with-current-buffer anything-buffer
2239 (let ((source (cdr process-assoc)))
2240 (save-excursion
2241 (anything-aif (assoc-default 'insertion-marker source)
2242 (goto-char it)
2243 (goto-char (point-max))
2244 (anything-insert-header-from-source source)
2245 (setcdr process-assoc
2246 (append source `((insertion-marker . ,(point-marker))))))
2247 (anything-output-filter--process-source
2248 (car process-assoc) string source
2249 (anything-candidate-number-limit source))))
2250 (anything-output-filter--post-process)))
2252 (defun anything-output-filter--process-source (process string source limit)
2253 (dolist (candidate (anything-transform-candidates
2254 (anything-output-filter--collect-candidates
2255 (split-string string "\n")
2256 (assoc 'incomplete-line source))
2257 source t))
2258 (anything-insert-match candidate 'insert-before-markers source)
2259 (incf (cdr (assoc 'item-count source)))
2260 (when (>= (assoc-default 'item-count source) limit)
2261 (anything-kill-async-process process)
2262 (return))))
2264 (defun anything-output-filter--collect-candidates (lines incomplete-line-info)
2265 (anything-log-eval (cdr incomplete-line-info))
2266 (butlast
2267 (loop for line in lines collect
2268 (if (cdr incomplete-line-info)
2269 (prog1
2270 (concat (cdr incomplete-line-info) line)
2271 (setcdr incomplete-line-info nil))
2272 line)
2273 finally (setcdr incomplete-line-info line))))
2275 (defun anything-output-filter--post-process ()
2276 (anything-maybe-fit-frame)
2277 (anything-log-run-hook 'anything-update-hook)
2278 (save-selected-window
2279 (select-window (get-buffer-window anything-buffer 'visible))
2280 (anything-skip-noncandidate-line 'next)
2281 (anything-mark-current-line)))
2284 (defun anything-kill-async-processes ()
2285 "Kill all known asynchronous processes according to
2286 `anything-async-processes'."
2287 "Kill locate process."
2288 (mapc 'anything-kill-async-process (mapcar 'car anything-async-processes))
2289 (setq anything-async-processes nil))
2292 (defun anything-kill-async-process (process)
2293 "Kill PROCESS and detach the associated functions."
2294 (set-process-filter process nil)
2295 (delete-process process))
2298 ;; (@* "Core: action")
2299 (defun anything-execute-selection-action (&optional selection action preserve-saved-action)
2300 "If a candidate was selected then perform the associated
2301 action."
2302 (anything-log "executing action")
2303 (setq action (anything-get-default-action
2304 (or action
2305 anything-saved-action
2306 (if (get-buffer anything-action-buffer)
2307 (anything-get-selection anything-action-buffer)
2308 (anything-get-action)))))
2309 (let ((source (or anything-saved-current-source (anything-get-current-source))))
2310 (setq selection (or selection
2311 (anything-get-selection)
2312 (and (assoc 'accept-empty source) "")))
2313 (unless preserve-saved-action (setq anything-saved-action nil))
2314 (if (and selection action)
2315 (anything-funcall-with-source source action selection))))
2317 (defun anything-get-default-action (action)
2318 (if (and (listp action) (not (functionp action)))
2319 (cdar action)
2320 action))
2322 (defun anything-select-action ()
2323 "Select an action for the currently selected candidate.
2324 If action buffer is selected, back to the anything buffer."
2325 (interactive)
2326 (cond ((get-buffer-window anything-action-buffer 'visible)
2327 (set-window-buffer (get-buffer-window anything-action-buffer)
2328 anything-buffer)
2329 (kill-buffer anything-action-buffer)
2330 (anything-set-pattern anything-input 'noupdate))
2332 (setq anything-saved-selection (anything-get-selection))
2333 (unless anything-saved-selection
2334 (error "Nothing is selected."))
2335 (setq anything-saved-current-source (anything-get-current-source))
2336 (let ((actions (anything-get-action)))
2337 (if (functionp actions)
2338 (message "Sole action: %s" actions)
2339 (anything-show-action-buffer actions)
2340 (anything-delete-minibuffer-contents)
2341 (setq anything-pattern 'dummy) ; so that it differs from the previous one
2342 (anything-check-minibuffer-input))))))
2344 (defun anything-show-action-buffer (actions)
2345 (with-current-buffer (get-buffer-create anything-action-buffer)
2346 (erase-buffer)
2347 (buffer-disable-undo)
2348 (set-window-buffer (get-buffer-window anything-buffer) anything-action-buffer)
2349 (set (make-local-variable 'anything-sources)
2350 `(((name . "Actions")
2351 (volatile)
2352 (candidates . ,actions)
2353 (candidate-number-limit))))
2354 (set (make-local-variable 'anything-source-filter) nil)
2355 (set (make-local-variable 'anything-selection-overlay) nil)
2356 (set (make-local-variable 'anything-digit-overlays) nil)
2357 (anything-initialize-overlays anything-action-buffer)))
2359 ;; (@* "Core: selection")
2360 (defun anything-move-selection-common (move-func unit direction)
2361 "Move the selection marker to a new position determined by
2362 UNIT and DIRECTION."
2363 (unless (or (anything-empty-buffer-p (anything-buffer-get))
2364 (not (anything-window)))
2365 (with-anything-window
2366 (funcall move-func)
2367 (anything-skip-noncandidate-line direction)
2368 (anything-display-source-at-screen-top-maybe unit)
2369 (when (anything-get-previous-header-pos)
2370 (anything-mark-current-line))
2371 (anything-display-mode-line (anything-get-current-source)))))
2373 (defun anything-display-source-at-screen-top-maybe (unit)
2374 (when (and anything-display-source-at-screen-top (eq unit 'source))
2375 (set-window-start (selected-window)
2376 (save-excursion (forward-line -1) (point)))))
2378 (defun anything-skip-noncandidate-line (direction)
2379 (anything-skip-header-and-separator-line direction)
2380 (and (bobp) (forward-line 1)) ;skip first header
2381 (and (eobp) (forward-line -1)) ;avoid last empty line
2384 (defun anything-skip-header-and-separator-line (direction)
2385 (while (and (not (bobp))
2386 (or (anything-pos-header-line-p)
2387 (anything-pos-candidate-separator-p)))
2388 (forward-line (if (and (eq direction 'previous)
2389 (not (eq (point-at-bol) (point-min))))
2391 1))))
2393 (defvar anything-mode-line-string-real nil)
2394 (defun anything-display-mode-line (source)
2395 (set (make-local-variable 'anything-mode-line-string)
2396 (anything-interpret-value (or (assoc-default 'mode-line source)
2397 (default-value 'anything-mode-line-string))
2398 source))
2399 (if anything-mode-line-string
2400 (setq mode-line-format
2401 '(" " mode-line-buffer-identification " "
2402 (line-number-mode "%l") " " (anything-follow-mode "(F)")
2403 " " anything-mode-line-string-real "-%-")
2404 anything-mode-line-string-real
2405 (substitute-command-keys anything-mode-line-string))
2406 (setq mode-line-format
2407 (default-value 'mode-line-format)))
2408 (setq header-line-format
2409 (anything-interpret-value (assoc-default 'header-line source) source)))
2411 (defun anything-previous-line ()
2412 "Move selection to the previous line."
2413 (interactive)
2414 (anything-move-selection-common
2415 (lambda ()
2416 (if (not (anything-pos-multiline-p))
2417 (forward-line -1) ;double forward-line is meaningful
2418 (forward-line -1) ;because evaluation order is important
2419 (anything-skip-header-and-separator-line 'previous)
2420 (let ((header-pos (anything-get-previous-header-pos))
2421 (separator-pos (anything-get-previous-candidate-separator-pos)))
2422 (when header-pos
2423 (goto-char (if (or (null separator-pos) (< separator-pos header-pos))
2424 header-pos ; first candidate
2425 separator-pos))
2426 (forward-line 1)))))
2427 'line 'previous))
2429 (defun anything-next-line ()
2430 "Move selection to the next line."
2431 (interactive)
2432 (anything-move-selection-common
2433 (lambda ()
2434 (if (not (anything-pos-multiline-p))
2435 (forward-line 1)
2436 (let ((header-pos (anything-get-next-header-pos))
2437 (separator-pos (anything-get-next-candidate-separator-pos)))
2438 (cond ((and separator-pos
2439 (or (null header-pos) (< separator-pos header-pos)))
2440 (goto-char separator-pos))
2441 (header-pos
2442 (goto-char header-pos))))))
2443 'line 'next))
2445 (defun anything-previous-page ()
2446 "Move selection back with a pageful."
2447 (interactive)
2448 (anything-move-selection-common
2449 (lambda ()
2450 (condition-case nil
2451 (scroll-down)
2452 (beginning-of-buffer (goto-char (point-min)))))
2453 'page 'previous))
2455 (defun anything-next-page ()
2456 "Move selection forward with a pageful."
2457 (interactive)
2458 (anything-move-selection-common
2459 (lambda ()
2460 (condition-case nil
2461 (scroll-up)
2462 (end-of-buffer (goto-char (point-max)))))
2463 'page 'next))
2465 (defun anything-beginning-of-buffer ()
2466 "Move selection at the top."
2467 (interactive)
2468 (anything-move-selection-common (lambda () (goto-char (point-min)))
2469 'edge 'previous))
2471 (defun anything-end-of-buffer ()
2472 "Move selection at the bottom."
2473 (interactive)
2474 (anything-move-selection-common (lambda () (goto-char (point-max)))
2475 'edge 'next))
2477 (defun anything-previous-source ()
2478 "Move selection to the previous source."
2479 (interactive)
2480 (anything-move-selection-common
2481 (lambda ()
2482 (forward-line -1)
2483 (if (bobp)
2484 (goto-char (point-max))
2485 (anything-skip-header-and-separator-line 'previous))
2486 (goto-char (anything-get-previous-header-pos))
2487 (forward-line 1))
2488 'source 'previous))
2490 (defun anything-next-source ()
2491 "Move selection to the next source."
2492 (interactive)
2493 (anything-move-selection-common
2494 (lambda () (goto-char (or (anything-get-next-header-pos) (point-min))))
2495 'source 'next))
2497 (defun anything-goto-source (source-or-name)
2498 "Move the selection to the source (SOURCE-OR-NAME)."
2499 (anything-move-selection-common
2500 (lambda ()
2501 (goto-char (point-min))
2502 (let ((name (if (stringp source-or-name) source-or-name
2503 (assoc-default 'name source-or-name))))
2504 (while (not (string= name (anything-current-line-contents)))
2505 (goto-char (anything-get-next-header-pos)))))
2506 'source 'next))
2508 (defun anything-mark-current-line ()
2509 "Move selection overlay to current line."
2510 (move-overlay
2511 anything-selection-overlay (point-at-bol)
2512 (if (anything-pos-multiline-p)
2513 (let ((header-pos (anything-get-next-header-pos))
2514 (separator-pos (anything-get-next-candidate-separator-pos)))
2515 (or (and (null header-pos) separator-pos)
2516 (and header-pos separator-pos (< separator-pos header-pos)
2517 separator-pos)
2518 header-pos
2519 (point-max)))
2520 (1+ (point-at-eol))))
2521 (anything-follow-execute-persistent-action-maybe))
2523 (defun anything-this-command-key ()
2524 (event-basic-type (elt (this-command-keys-vector) 0)))
2525 ;; (progn (read-key-sequence "Key: ") (p (anything-this-command-key)))
2527 (defun anything-select-with-shortcut-internal (types get-key-func)
2528 (if (memq anything-enable-shortcuts types)
2529 (save-selected-window
2530 (select-window (anything-window))
2531 (let* ((key (funcall get-key-func))
2532 (overlay (ignore-errors (nth (position key anything-shortcut-keys)
2533 anything-digit-overlays))))
2534 (if (not (and overlay (overlay-buffer overlay)))
2535 (when (numberp key)
2536 (select-window (minibuffer-window))
2537 (self-insert-command 1))
2538 (goto-char (overlay-start overlay))
2539 (anything-mark-current-line)
2540 (anything-exit-minibuffer))))
2541 (self-insert-command 1)))
2543 (defun anything-select-with-prefix-shortcut ()
2544 "Invoke default action with prefix shortcut."
2545 (interactive)
2546 (anything-select-with-shortcut-internal
2547 '(prefix)
2548 (lambda () (read-event "Select shortcut key: "))))
2550 (defun anything-select-with-digit-shortcut ()
2551 "Invoke default action with digit/alphabet shortcut."
2552 (interactive)
2553 (anything-select-with-shortcut-internal
2554 '(alphabet t) 'anything-this-command-key))
2556 ;; (setq anything-enable-shortcuts 'prefix)
2557 ;; (define-key anything-map "@" 'anything-select-with-prefix-shortcut)
2558 ;; (define-key anything-map (kbd "<f18>") 'anything-select-with-prefix-shortcut)
2560 (defun anything-exit-minibuffer ()
2561 "Select the current candidate by exiting the minibuffer."
2562 (interactive)
2563 (declare (special anything-iswitchb-candidate-selected))
2564 (setq anything-iswitchb-candidate-selected (anything-get-selection))
2565 (exit-minibuffer))
2568 (defun anything-get-next-header-pos ()
2569 "Return the position of the next header from point."
2570 (next-single-property-change (point) 'anything-header))
2573 (defun anything-get-previous-header-pos ()
2574 "Return the position of the previous header from point"
2575 (previous-single-property-change (point) 'anything-header))
2578 (defun anything-pos-multiline-p ()
2579 "Return non-nil if the current position is in the multiline source region."
2580 (get-text-property (point) 'anything-multiline))
2583 (defun anything-get-next-candidate-separator-pos ()
2584 "Return the position of the next candidate separator from point."
2585 (next-single-property-change (point) 'anything-candidate-separator))
2588 (defun anything-get-previous-candidate-separator-pos ()
2589 "Return the position of the previous candidate separator from point."
2590 (previous-single-property-change (point) 'anything-candidate-separator))
2593 (defun anything-pos-header-line-p ()
2594 "Return t if the current line is a header line."
2595 (or (get-text-property (point-at-bol) 'anything-header)
2596 (get-text-property (point-at-bol) 'anything-header-separator)))
2598 (defun anything-pos-candidate-separator-p ()
2599 "Return t if the current line is a candidate separator."
2600 (get-text-property (point-at-bol) 'anything-candidate-separator))
2602 ;; (@* "Core: help")
2603 (defun anything-help-internal (bufname insert-content-fn)
2604 "Show long message during `anything' session."
2605 (save-window-excursion
2606 (select-window (anything-window))
2607 (delete-other-windows)
2608 (switch-to-buffer (get-buffer-create bufname))
2609 (erase-buffer)
2610 (funcall insert-content-fn)
2611 (setq mode-line-format "%b (SPC,C-v:NextPage b,M-v:PrevPage other:Exit)")
2612 (setq cursor-type nil)
2613 (goto-char 1)
2614 (anything-help-event-loop)))
2616 (defun anything-help-event-loop ()
2617 (ignore-errors
2618 (loop for event = (read-event) do
2619 (case event
2620 ((?\C-v ? ) (scroll-up))
2621 ((?\M-v ?b) (scroll-down))
2622 (t (return))))))
2624 (defun anything-help ()
2625 "Help of `anything'."
2626 (interactive)
2627 (anything-help-internal
2628 " *Anything Help*"
2629 (lambda ()
2630 (insert (substitute-command-keys
2631 (anything-interpret-value anything-help-message)))
2632 (org-mode))))
2634 (defun anything-debug-output ()
2635 "Show all anything-related variables at this time."
2636 (interactive)
2637 (anything-help-internal " *Anything Debug*" 'anything-debug-output-function))
2639 (defun anything-debug-output-function (&optional vars)
2640 (message "Calculating all anything-related values...")
2641 (insert "If you debug some variables or forms, set `anything-debug-forms'
2642 to a list of forms.\n\n")
2643 (dolist (v (or vars
2644 anything-debug-forms
2645 (apropos-internal "^anything-" 'boundp)))
2646 (insert "** "
2647 (pp-to-string v) "\n"
2648 (pp-to-string (eval v)) "\n"))
2649 (message "Calculating all anything-related values...Done"))
2651 ;; (@* "Core: misc")
2652 (defun anything-kill-buffer-hook ()
2653 "Remove tick entry from `anything-tick-hash' when killing a buffer."
2654 (loop for key being the hash-keys in anything-tick-hash
2655 if (string-match (format "^%s/" (regexp-quote (buffer-name))) key)
2656 do (remhash key anything-tick-hash)))
2657 (add-hook 'kill-buffer-hook 'anything-kill-buffer-hook)
2659 (defun anything-maybe-fit-frame ()
2660 "Fit anything frame to its buffer, and put it at top right of display.
2662 It is disabled by default because some flickering occurred in some environment.
2663 To enable fitting, set both `anything-inhibit-fit-frame-flag' and
2664 `fit-frame-inhibit-fitting' to nil.
2665 You can set user options `fit-frame-max-width-percent' and
2666 `fit-frame-max-height-percent' to control max frame size."
2667 (declare (warn (unresolved 0)))
2668 (when (and (not anything-inhibit-fit-frame-flag)
2669 (anything-window)
2670 (require 'fit-frame nil t)
2671 (boundp 'fit-frame-inhibit-fitting-flag)
2672 (not fit-frame-inhibit-fitting-flag))
2673 (ignore-errors
2674 (with-anything-window
2675 (fit-frame nil nil nil t)
2676 (modify-frame-parameters
2677 (selected-frame)
2678 `((left . ,(- (x-display-pixel-width) (+ (frame-pixel-width) 7)))
2679 (top . 0))))))) ; The (top . 0) shouldn't be necessary (Emacs bug).
2681 (defun anything-preselect (candidate-or-regexp)
2682 (with-anything-window
2683 (when candidate-or-regexp
2684 (goto-char (point-min))
2685 ;; go to first candidate of first source
2686 (forward-line 1)
2687 (let ((start (point)))
2688 (unless (or (re-search-forward
2689 (concat "^" (regexp-quote candidate-or-regexp) "$")
2690 nil t)
2691 (progn (goto-char start)
2692 (re-search-forward candidate-or-regexp nil t)))
2693 (goto-char start))))
2694 (anything-mark-current-line)))
2696 (defun anything-delete-current-selection ()
2697 "Delete the currently selected item."
2698 (interactive)
2699 (with-anything-window
2700 (cond ((anything-pos-multiline-p)
2701 (anything-aif (anything-get-next-candidate-separator-pos)
2702 (delete-region (point-at-bol)
2703 (1+ (progn (goto-char it) (point-at-eol))))
2704 ;; last candidate
2705 (goto-char (anything-get-previous-candidate-separator-pos))
2706 (delete-region (point-at-bol) (point-max)))
2707 (when (eobp)
2708 (goto-char (or (anything-get-previous-candidate-separator-pos)
2709 (point-min)))
2710 (forward-line 1)))
2712 (delete-region (point-at-bol) (1+ (point-at-eol)))
2713 (when (eobp) (forward-line -1))))
2714 (anything-mark-current-line)))
2716 (defun anything-edit-current-selection-internal (func)
2717 (with-anything-window
2718 (beginning-of-line)
2719 (let ((realvalue (get-text-property (point) 'anything-realvalue)))
2720 (funcall func)
2721 (beginning-of-line)
2722 (and realvalue
2723 (put-text-property (point) (point-at-eol)
2724 'anything-realvalue realvalue))
2725 (anything-mark-current-line))))
2727 (defmacro anything-edit-current-selection (&rest forms)
2728 "Evaluate FORMS at current selection in the anything buffer.
2729 You can edit the line."
2730 `(anything-edit-current-selection-internal
2731 (lambda () ,@forms)))
2732 (put 'anything-edit-current-selection 'lisp-indent-function 0)
2734 (defun anything-set-pattern (pattern &optional noupdate)
2735 "Set minibuffer contents to PATTERN.
2736 if optional NOUPDATE is non-nil, anything buffer is not changed."
2737 (with-selected-window (minibuffer-window)
2738 (delete-minibuffer-contents)
2739 (insert pattern))
2740 (when noupdate
2741 (setq anything-pattern pattern)
2742 (anything-hooks 'cleanup)
2743 (run-with-idle-timer 0 nil 'anything-hooks 'setup)))
2745 (defun anything-delete-minibuffer-contents ()
2746 "Same as `delete-minibuffer-contents' but this is a command."
2747 (interactive)
2748 (anything-set-pattern ""))
2749 (defalias 'anything-delete-minibuffer-content 'anything-delete-minibuffer-contents)
2751 ;; (@* "Built-in plug-in: type")
2752 (defun anything-compile-source--type (source)
2753 (anything-aif (assoc-default 'type source)
2754 (append source (assoc-default it anything-type-attributes) nil)
2755 source))
2757 ;; `define-anything-type-attribute' is public API.
2759 (defun anything-add-type-attribute (type definition)
2760 (anything-aif (assq type anything-type-attributes)
2761 (setq anything-type-attributes (delete it anything-type-attributes)))
2762 (push (cons type definition) anything-type-attributes))
2764 (defvar anything-types nil)
2765 (defun anything-document-type-attribute (type doc)
2766 (add-to-list 'anything-types type t)
2767 (put type 'anything-typeattrdoc
2768 (concat "- " (symbol-name type) "\n\n" doc "\n")))
2770 (defadvice documentation-property (after anything-document-type-attribute activate)
2771 "Hack to display type attributes' documentation as `anything-type-attributes' docstring."
2772 (when (eq symbol 'anything-type-attributes)
2773 (setq ad-return-value
2774 (concat ad-return-value "\n\n++++ Types currently defined ++++\n"
2775 (mapconcat (lambda (sym) (get sym 'anything-typeattrdoc))
2776 anything-types "\n")))))
2778 ;; (@* "Built-in plug-in: dummy")
2779 (defun anything-dummy-candidate (candidate source)
2780 ;; `source' is defined in filtered-candidate-transformer
2781 (list anything-pattern))
2783 (defun anything-compile-source--dummy (source)
2784 (if (assoc 'dummy source)
2785 (append source
2786 '((candidates "dummy")
2787 (accept-empty)
2788 (match identity)
2789 (filtered-candidate-transformer . anything-dummy-candidate)
2790 (disable-shortcuts)
2791 (volatile)))
2792 source))
2794 ;; (@* "Built-in plug-in: disable-shortcuts")
2795 (defvar anything-orig-enable-shortcuts nil)
2796 (defun anything-save-enable-shortcuts ()
2797 (anything-once
2798 (lambda () (setq anything-orig-enable-shortcuts anything-enable-shortcuts
2799 anything-enable-shortcuts nil))))
2800 (defun anything-compile-source--disable-shortcuts (source)
2801 (if (assoc 'disable-shortcuts source)
2802 (append `((init ,@(anything-mklist (assoc-default 'init source))
2803 anything-save-enable-shortcuts)
2804 (resume ,@(anything-mklist (assoc-default 'resume source))
2805 anything-save-enable-shortcuts)
2806 (cleanup ,@(anything-mklist (assoc-default 'cleanup source))
2807 (lambda () (setq anything-enable-shortcuts
2808 anything-orig-enable-shortcuts))))
2809 source)
2810 source))
2812 ;; (@* "Built-in plug-in: candidates-in-buffer")
2813 (defun anything-candidates-in-buffer ()
2814 "Get candidates from the candidates buffer according to `anything-pattern'.
2816 BUFFER is `anything-candidate-buffer' by default. Each
2817 candidate must be placed in one line. This function is meant to
2818 be used in candidates-in-buffer or candidates attribute of an
2819 anything source. Especially fast for many (1000+) candidates.
2822 '((name . \"many files\")
2823 (init . (lambda () (with-current-buffer (anything-candidate-buffer 'local)
2824 (insert-many-filenames))))
2825 (search re-search-forward) ; optional
2826 (candidates-in-buffer)
2827 (type . file))
2829 +===============================================================+
2830 | The new way of making and narrowing candidates: Using buffers |
2831 +===============================================================+
2833 By default, `anything' makes candidates by evaluating the
2834 candidates function, then narrows them by `string-match' for each
2835 candidate.
2837 But this way is very slow for many candidates. The new way is
2838 storing all candidates in a buffer and narrowing them by
2839 `re-search-forward'. Search function is customizable by search
2840 attribute. The important point is that buffer processing is MUCH
2841 FASTER than string list processing and is the Emacs way.
2843 The init function writes all candidates to a newly-created
2844 candidate buffer. The candidates buffer is created or specified
2845 by `anything-candidate-buffer'. Candidates are stored in a line.
2847 The candidates function narrows all candidates, IOW creates a
2848 subset of candidates dynamically. It is the task of
2849 `anything-candidates-in-buffer'. As long as
2850 `anything-candidate-buffer' is used,`(candidates-in-buffer)' is
2851 sufficient in most cases.
2853 Note that `(candidates-in-buffer)' is shortcut of three attributes:
2854 (candidates . anything-candidates-in-buffer)
2855 (volatile)
2856 (match identity)
2857 And `(candidates-in-buffer . func)' is shortcut of three attributes:
2858 (candidates . func)
2859 (volatile)
2860 (match identity)
2861 The expansion is performed in `anything-get-sources'.
2863 The candidates-in-buffer attribute implies the volatile attribute.
2864 The volatile attribute is needed because `anything-candidates-in-buffer'
2865 creates candidates dynamically and need to be called everytime
2866 `anything-pattern' changes.
2868 Because `anything-candidates-in-buffer' plays the role of `match' attribute
2869 function, specifying `(match identity)' makes the source slightly faster.
2871 To customize `anything-candidates-in-buffer' behavior, use search,
2872 get-line and search-from-end attributes. See also `anything-sources' docstring.
2874 (declare (special source))
2875 (anything-candidates-in-buffer-1 (anything-candidate-buffer)
2876 anything-pattern
2877 (or (assoc-default 'get-line source)
2878 #'buffer-substring-no-properties)
2879 ;; use external variable `source'.
2880 (or (assoc-default 'search source)
2881 (if (assoc 'search-from-end source)
2882 '(re-search-backward)
2883 '(re-search-forward)))
2884 (anything-candidate-number-limit source)
2885 (assoc 'search-from-end source)))
2887 (defun anything-candidates-in-buffer-1 (buffer pattern get-line-fn search-fns limit search-from-end)
2888 ;; buffer == nil when candidates buffer does not exist.
2889 (when buffer
2890 (with-current-buffer buffer
2891 (let ((start-point (if search-from-end (point-max) (point-min)))
2892 (endp (if search-from-end #'bobp #'eobp)))
2893 (goto-char (1- start-point))
2894 (if (string= pattern "")
2895 (anything-initial-candidates-from-candidate-buffer
2896 endp get-line-fn limit search-from-end)
2897 (anything-search-from-candidate-buffer
2898 pattern get-line-fn search-fns limit search-from-end
2899 start-point endp))))))
2901 (defun anything-point-is-moved (proc)
2902 "If point is moved after executing PROC, return t, otherwise nil."
2903 (/= (point) (progn (funcall proc) (point))))
2905 (defun anything-search-from-candidate-buffer (pattern get-line-fn search-fns
2906 limit search-from-end
2907 start-point endp)
2908 (let (buffer-read-only
2909 matches exit newmatches)
2910 (anything-search-from-candidate-buffer-internal
2911 (lambda ()
2912 (clrhash anything-cib-hash)
2913 (dolist (searcher search-fns)
2914 (goto-char start-point)
2915 (setq newmatches nil)
2916 (loop with item-count = 0
2917 while (funcall searcher pattern nil t)
2918 for cand = (funcall get-line-fn (point-at-bol) (point-at-eol))
2919 do (anything-accumulate-candidates-internal
2920 cand newmatches anything-cib-hash item-count limit)
2921 unless (anything-point-is-moved
2922 (lambda ()
2923 (if search-from-end
2924 (goto-char (1- (point-at-bol)))
2925 (forward-line 1))))
2926 return nil)
2927 (setq matches (append matches (nreverse newmatches)))
2928 (if exit (return)))
2929 (delq nil matches)))))
2931 (defun anything-initial-candidates-from-candidate-buffer (endp get-line-fn limit search-from-end)
2932 (delq nil (loop with next-line-fn =
2933 (if search-from-end
2934 (lambda (x) (goto-char (max (1- (point-at-bol)) 1)))
2935 #'forward-line)
2936 until (funcall endp)
2937 for i from 1 to limit
2938 collect (funcall get-line-fn (point-at-bol) (point-at-eol))
2939 do (funcall next-line-fn 1))))
2941 (defun anything-search-from-candidate-buffer-internal (search-fn)
2942 (goto-char (point-min))
2943 (insert "\n")
2944 (goto-char (point-max))
2945 (insert "\n")
2946 (unwind-protect
2947 (funcall search-fn)
2948 (goto-char (point-min))
2949 (delete-char 1)
2950 (goto-char (1- (point-max)))
2951 (delete-char 1)
2953 (set-buffer-modified-p nil)))
2955 (defun anything-candidate-buffer (&optional create-or-buffer)
2956 "Register and return a buffer containing candidates of current source.
2957 `anything-candidate-buffer' searches buffer-local candidates buffer first,
2958 then global candidates buffer.
2960 Acceptable values of CREATE-OR-BUFFER:
2962 - nil (omit)
2963 Only return the candidates buffer.
2964 - a buffer
2965 Register a buffer as a candidates buffer.
2966 - 'global
2967 Create a new global candidates buffer,
2968 named \" *anything candidates:SOURCE*\".
2969 - other non-nil value
2970 Create a new local candidates buffer,
2971 named \" *anything candidates:SOURCE*ANYTHING-CURRENT-BUFFER\".
2973 (let* ((global-bname (format " *anything candidates:%s*" anything-source-name))
2974 (local-bname (format " *anything candidates:%s*%s"
2975 anything-source-name
2976 (buffer-name anything-current-buffer)))
2977 (register-func
2978 (lambda ()
2979 (setq anything-candidate-buffer-alist
2980 (cons (cons anything-source-name create-or-buffer)
2981 (delete (assoc anything-source-name
2982 anything-candidate-buffer-alist)
2983 anything-candidate-buffer-alist)))))
2984 (kill-buffers-func
2985 (lambda ()
2986 (loop for b in (buffer-list)
2987 if (string-match (format "^%s" (regexp-quote global-bname))
2988 (buffer-name b))
2989 do (kill-buffer b))))
2990 (create-func
2991 (lambda ()
2992 (with-current-buffer
2993 (get-buffer-create (if (eq create-or-buffer 'global)
2994 global-bname
2995 local-bname))
2996 (buffer-disable-undo)
2997 (erase-buffer)
2998 (font-lock-mode -1))))
2999 (return-func
3000 (lambda ()
3001 (or (get-buffer local-bname)
3002 (get-buffer global-bname)
3003 (anything-aif (assoc-default anything-source-name
3004 anything-candidate-buffer-alist)
3005 (and (buffer-live-p it) it))))))
3006 (when create-or-buffer
3007 (funcall register-func)
3008 (unless (bufferp create-or-buffer)
3009 (and (eq create-or-buffer 'global) (funcall kill-buffers-func))
3010 (funcall create-func)))
3011 (funcall return-func)))
3013 (defun anything-compile-source--candidates-in-buffer (source)
3014 (anything-aif (assoc 'candidates-in-buffer source)
3015 (append source
3016 `((candidates . ,(or (cdr it) 'anything-candidates-in-buffer))
3017 (volatile) (match identity)))
3018 source))
3020 ;; (@* "Utility: resplit anything window")
3021 (defun anything-toggle-resplit-window ()
3022 "Toggle resplit anything window, vertically or horizontally."
3023 (interactive)
3024 (with-anything-window
3025 (let ((before-height (window-height)))
3026 (delete-window)
3027 (set-window-buffer
3028 (select-window (if (= (window-height) before-height)
3029 (split-window-vertically)
3030 (split-window-horizontally)))
3031 anything-buffer))))
3033 ;; (@* "Utility: select another action by key")
3034 (defun anything-select-nth-action (n)
3035 "Select the nth action for the currently selected candidate."
3036 (setq anything-saved-selection (anything-get-selection))
3037 (unless anything-saved-selection
3038 (error "Nothing is selected."))
3039 (setq anything-saved-action (cdr (elt (anything-get-action) n)))
3040 (anything-exit-minibuffer))
3042 (defun anything-select-2nd-action ()
3043 "Select the 2nd action for the currently selected candidate."
3044 (interactive)
3045 (anything-select-nth-action 1))
3047 (defun anything-select-3rd-action ()
3048 "Select the 3rd action for the currently selected candidate."
3049 (interactive)
3050 (anything-select-nth-action 2))
3052 (defun anything-select-4th-action ()
3053 "Select the 4th action for the currently selected candidate."
3054 (interactive)
3055 (anything-select-nth-action 3))
3057 (defun anything-select-2nd-action-or-end-of-line ()
3058 "Select the 2nd action for the currently selected candidate if the point is at the end of minibuffer.
3059 Otherwise goto the end of minibuffer."
3060 (interactive)
3061 (if (eolp)
3062 (anything-select-nth-action 1)
3063 (end-of-line)))
3065 ;; (@* "Utility: Persistent Action")
3066 (defmacro with-anything-display-same-window (&rest body)
3067 "Make `pop-to-buffer' and `display-buffer' display in the same window."
3068 `(let ((display-buffer-function 'anything-persistent-action-display-buffer))
3069 ,@body))
3070 (put 'with-anything-display-same-window 'lisp-indent-function 0)
3072 (defun* anything-execute-persistent-action (&optional (attr 'persistent-action))
3073 "If a candidate is selected then perform the associated action without quitting anything."
3074 (interactive)
3075 (anything-log "executing persistent-action")
3076 (save-selected-window
3077 (select-window (get-buffer-window (anything-buffer-get)))
3078 (select-window (setq minibuffer-scroll-window
3079 (if (one-window-p t) (split-window)
3080 (next-window (selected-window) 1))))
3081 (anything-log-eval (current-buffer))
3082 (let ((anything-in-persistent-action t))
3083 (with-anything-display-same-window
3084 (anything-execute-selection-action
3086 (or (assoc-default attr (anything-get-current-source))
3087 (anything-get-action))
3089 (anything-log-run-hook 'anything-after-persistent-action-hook)))))
3091 (defun anything-persistent-action-display-buffer (buf &optional not-this-window)
3092 "Make `pop-to-buffer' and `display-buffer' display in the same window in persistent action.
3093 If `anything-persistent-action-use-special-display' is non-nil and
3094 BUF is to be displayed by `special-display-function', use it.
3095 Otherwise ignores `special-display-buffer-names' and `special-display-regexps'."
3096 (let* ((name (buffer-name buf))
3097 display-buffer-function pop-up-windows
3098 (same-window-regexps
3099 (unless (and anything-persistent-action-use-special-display
3100 (or (member name
3101 (mapcar (lambda (x) (or (car-safe x) x))
3102 special-display-buffer-names))
3103 (remove-if-not
3104 (lambda (x) (string-match (or (car-safe x) x) name))
3105 special-display-regexps)))
3106 '("."))))
3107 (display-buffer buf not-this-window)))
3109 ;; scroll-other-window(-down)? for persistent-action
3110 (defun anything-scroll-other-window-base (command)
3111 (save-selected-window
3112 (select-window
3113 (some-window
3114 (lambda (w) (not (string= anything-buffer (buffer-name (window-buffer w)))))
3115 'no-minibuffer 'current-frame))
3116 (funcall command anything-scroll-amount)))
3118 (defun anything-scroll-other-window ()
3119 "Scroll other window (not *Anything* window) upward."
3120 (interactive)
3121 (anything-scroll-other-window-base 'scroll-up))
3122 (defun anything-scroll-other-window-down ()
3123 "Scroll other window (not *Anything* window) downward."
3124 (interactive)
3125 (anything-scroll-other-window-base 'scroll-down))
3127 ;; (@* "Utility: Visible Mark")
3128 (defface anything-visible-mark
3129 '((((min-colors 88) (background dark))
3130 (:background "green1" :foreground "black"))
3131 (((background dark)) (:background "green" :foreground "black"))
3132 (((min-colors 88)) (:background "green1"))
3133 (t (:background "green")))
3134 "Face for visible mark."
3135 :group 'anything)
3136 (defvar anything-visible-mark-face 'anything-visible-mark)
3137 (defvar anything-visible-mark-overlays nil)
3139 (defun anything-clear-visible-mark ()
3140 (with-current-buffer (anything-buffer-get)
3141 (mapc 'delete-overlay anything-visible-mark-overlays)
3142 (set (make-local-variable 'anything-visible-mark-overlays) nil)))
3143 (add-hook 'anything-after-initialize-hook 'anything-clear-visible-mark)
3145 (defvar anything-c-marked-candidate-list nil
3146 "[OBSOLETE] DO NOT USE!!")
3147 (defvar anything-marked-candidates nil
3148 "Marked candadates. List of (source . real) pair.")
3150 (defun anything-this-visible-mark ()
3151 (loop for o in anything-visible-mark-overlays
3152 when (equal (point-at-bol) (overlay-start o))
3153 do (return o)))
3155 (defun anything-delete-visible-mark (overlay)
3156 (setq anything-c-marked-candidate-list
3157 (remove (anything-current-line-contents) anything-c-marked-candidate-list))
3158 (setq anything-marked-candidates
3159 (remove
3160 (cons (anything-get-current-source) (anything-get-selection))
3161 anything-marked-candidates))
3162 (delete-overlay overlay)
3163 (setq anything-visible-mark-overlays
3164 (delq overlay anything-visible-mark-overlays)))
3166 (defun anything-make-visible-mark ()
3167 (let ((o (make-overlay (point-at-bol) (1+ (point-at-eol)))))
3168 (overlay-put o 'face anything-visible-mark-face)
3169 (overlay-put o 'source (assoc-default 'name (anything-get-current-source)))
3170 (overlay-put o 'string (buffer-substring (overlay-start o) (overlay-end o)))
3171 (add-to-list 'anything-visible-mark-overlays o))
3172 (push (anything-current-line-contents) anything-c-marked-candidate-list)
3173 (push (cons (anything-get-current-source) (anything-get-selection))
3174 anything-marked-candidates))
3176 (defun anything-toggle-visible-mark ()
3177 "Toggle anything visible bookmark at point."
3178 (interactive)
3179 (with-anything-window
3180 (anything-aif (anything-this-visible-mark)
3181 (anything-delete-visible-mark it)
3182 (anything-make-visible-mark))
3183 (anything-next-line)))
3185 (defun anything-display-all-visible-marks ()
3186 "Show all `anything' visible marks strings."
3187 (interactive)
3188 (lexical-let ((overlays (reverse anything-visible-mark-overlays)))
3189 (anything-run-after-quit
3190 (lambda ()
3191 (with-output-to-temp-buffer "*anything visible marks*"
3192 (dolist (o overlays) (princ (overlay-get o 'string))))))))
3194 (defun anything-marked-candidates ()
3195 "Marked candidates (real value) of current source if any,
3196 otherwise 1-element list of current selection.
3198 It is analogous to `dired-get-marked-files'."
3199 (with-current-buffer (anything-buffer-get)
3200 (let ((cands
3201 (if anything-marked-candidates
3202 (loop with current-src = (anything-get-current-source)
3203 for (source . real) in (reverse anything-marked-candidates)
3204 when (equal current-src source)
3205 collect real)
3206 (list (anything-get-selection)))))
3207 (anything-log-eval cands)
3208 cands)))
3210 (defun anything-reset-marked-candidates ()
3211 (with-current-buffer (anything-buffer-get)
3212 (set (make-local-variable 'anything-c-marked-candidate-list) nil)
3213 (set (make-local-variable 'anything-marked-candidates) nil)))
3215 (add-hook 'anything-after-initialize-hook 'anything-reset-marked-candidates)
3216 ;; (add-hook 'anything-after-action-hook 'anything-reset-marked-candidates)
3218 (defun anything-current-source-name= (name)
3219 (save-excursion
3220 (goto-char (anything-get-previous-header-pos))
3221 (equal name (anything-current-line-contents))))
3223 (defun anything-revive-visible-mark ()
3224 (with-current-buffer anything-buffer
3225 (dolist (o anything-visible-mark-overlays)
3226 (goto-char (point-min))
3227 (while (and (search-forward (overlay-get o 'string) nil t)
3228 (anything-current-source-name= (overlay-get o 'source)))
3229 ;; Now the next line of visible mark
3230 (move-overlay o (point-at-bol 0) (1+ (point-at-eol 0)))))))
3231 (add-hook 'anything-update-hook 'anything-revive-visible-mark)
3233 (defun anything-next-point-in-list (curpos points &optional prev)
3234 (cond
3235 ;; rule out special cases
3236 ((null points) curpos)
3237 ((and prev (< curpos (car points))) curpos)
3238 ((< (car (last points)) curpos)
3239 (if prev (car (last points)) curpos))
3241 (nth (if prev
3242 (loop for pt in points
3243 for i from 0
3244 if (<= curpos pt)
3245 do (return (1- i)))
3246 (loop for pt in points
3247 for i from 0
3248 if (< curpos pt)
3249 do (return i)))
3250 points))))
3252 (defun anything-next-visible-mark (&optional prev)
3253 "Move next anything visible mark."
3254 (interactive)
3255 (with-anything-window
3256 (goto-char (anything-next-point-in-list
3257 (point)
3258 (sort (mapcar 'overlay-start anything-visible-mark-overlays) '<)
3259 prev))
3260 (anything-mark-current-line)))
3262 (defun anything-prev-visible-mark ()
3263 "Move previous anything visible mark."
3264 (interactive)
3265 (anything-next-visible-mark t))
3267 ;; (@* "Utility: `find-file' integration")
3268 (defun anything-quit-and-find-file ()
3269 "Drop into `find-file' from `anything' like `iswitchb-find-file'.
3270 If current selection is a buffer or a file, `find-file' from its directory."
3271 (interactive)
3272 (anything-run-after-quit
3273 (lambda (f)
3274 (if (file-exists-p f)
3275 (let ((default-directory (file-name-directory f)))
3276 (call-interactively 'find-file))
3277 (call-interactively 'find-file)))
3278 (anything-aif (get-buffer (anything-get-selection))
3279 (buffer-file-name it)
3280 (expand-file-name (anything-get-selection)))))
3282 ;; (@* "Utility: Selection Paste")
3283 (defun anything-yank-selection ()
3284 "Set minibuffer contents to current selection."
3285 (interactive)
3286 (anything-set-pattern (anything-get-selection nil t)))
3288 (defun anything-kill-selection-and-quit ()
3289 "Store current selection to kill ring.
3290 You can paste it by typing C-y."
3291 (interactive)
3292 (anything-run-after-quit
3293 (lambda (sel)
3294 (kill-new sel)
3295 (message "Killed: %s" sel))
3296 (anything-get-selection nil t)))
3299 ;; (@* "Utility: Automatical execution of persistent-action")
3300 (add-to-list 'minor-mode-alist '(anything-follow-mode " AFollow"))
3301 (defun anything-follow-mode ()
3302 "If this mode is on, persistent action is executed everytime the cursor is moved."
3303 (interactive)
3304 (with-current-buffer anything-buffer
3305 (setq anything-follow-mode (not anything-follow-mode))
3306 (message "anything-follow-mode is %s"
3307 (if anything-follow-mode "enabled" "disabled"))))
3309 (defun anything-follow-execute-persistent-action-maybe ()
3310 "Execute persistent action after `anything-input-idle-delay' secs when `anything-follow-mode' is enabled."
3311 (and (buffer-local-value 'anything-follow-mode
3312 (get-buffer-create anything-buffer))
3313 (sit-for anything-input-idle-delay)
3314 (anything-window)
3315 (anything-get-selection)
3316 (save-excursion
3317 (anything-execute-persistent-action))))
3319 ;; (@* "Utility: Migrate `anything-sources' to my-anything command")
3320 (defun anything-migrate-sources ()
3321 "Help to migrate to new `anything' way."
3322 (interactive)
3323 (with-current-buffer (get-buffer-create "*anything migrate*")
3324 (erase-buffer)
3325 (insert (format "\
3326 Setting `anything-sources' directly is not good because
3327 `anything' is not for one command. For now, interactive use of
3328 `anything' (M-x anything) is only for demonstration purpose.
3329 So you should define commands calling `anything'.
3330 I help you to migrate to the new way.
3332 The code below is automatically generated from current
3333 `anything-sources' value. You can use the `my-anything' command
3334 now!
3336 Copy and paste it to your .emacs. Then substitute `my-anything'
3337 for `anything' bindings in all `define-key', `local-set-key' and
3338 `global-set-key' calls.
3340 \(defun my-anything ()
3341 \"Anything command for you.
3343 It is automatically generated by `anything-migrate-sources'.\"
3344 (interactive)
3345 (anything-other-buffer
3347 \"*my-anything*\"))
3348 " anything-sources))
3349 (eval-last-sexp nil)
3350 (substitute-key-definition 'anything 'my-anything global-map)
3351 (pop-to-buffer (current-buffer))))
3353 ;; (@* "Utility: Incremental search within results (unmaintained)")
3355 (defvar anything-isearch-original-global-map nil
3356 "Original global map before Anything isearch is started.")
3358 (defvar anything-isearch-original-message-timeout nil
3359 "Original message timeout before Anything isearch is started.")
3361 (defvar anything-isearch-pattern nil
3362 "The current isearch pattern.")
3364 (defvar anything-isearch-message-suffix ""
3365 "Message suffix indicating the current state of the search.")
3367 (defvar anything-isearch-original-point nil
3368 "Original position of point before isearch is started.")
3370 (defvar anything-isearch-original-window nil
3371 "Original selected window before isearch is started.")
3373 (defvar anything-isearch-original-cursor-in-non-selected-windows nil
3374 "Original value of cursor-in-non-selected-windows before isearch is started.")
3376 (defvar anything-isearch-original-deferred-action-list nil
3377 "Original value of deferred-action-list before isearch is started.")
3379 (defvar anything-isearch-match-positions nil
3380 "Stack of positions of matches or non-matches.
3382 It's a list of plists with two properties: `event', the last user
3383 event, `start', the start position of the current match, and
3384 `pos', the position of point after that event.
3386 The value of `event' can be the following symbols: `char' if a
3387 character was typed, `error' if a non-matching character was
3388 typed, `search' if a forward search had to be done after a
3389 character, and `search-again' if a search was done for the next
3390 occurrence of the current pattern.")
3392 (defvar anything-isearch-match-start nil
3393 "Start position of the current match.")
3396 (defun anything-isearch ()
3397 "Start incremental search within results. (UNMAINTAINED)"
3398 (interactive)
3399 (if (anything-empty-buffer-p (anything-buffer-get))
3400 (message "There are no results.")
3402 (setq anything-isearch-original-message-timeout minibuffer-message-timeout)
3403 (setq minibuffer-message-timeout nil)
3405 (setq anything-isearch-original-global-map global-map)
3407 (condition-case nil
3408 (progn
3409 (setq anything-isearch-original-window (selected-window))
3410 (select-window (anything-window))
3411 (setq cursor-type t)
3413 (setq anything-isearch-original-deferred-action-list
3414 (default-value 'deferred-action-list))
3415 (setq-default deferred-action-list nil)
3416 (add-hook 'deferred-action-list 'anything-isearch-post-command)
3418 (use-global-map anything-isearch-map)
3419 (setq overriding-terminal-local-map anything-isearch-map)
3421 (setq anything-isearch-pattern "")
3423 (setq anything-isearch-original-cursor-in-non-selected-windows
3424 cursor-in-non-selected-windows)
3425 (setq cursor-in-non-selected-windows nil)
3427 (setq anything-isearch-original-point (point-marker))
3428 (goto-char (point-min))
3429 (forward-line)
3430 (anything-mark-current-line)
3432 (setq anything-isearch-match-positions nil)
3433 (setq anything-isearch-match-start (point-marker))
3435 (if anything-isearch-overlay
3436 ;; make sure the overlay belongs to the anything buffer
3437 (move-overlay anything-isearch-overlay (point-min) (point-min)
3438 (get-buffer (anything-buffer-get)))
3440 (setq anything-isearch-overlay (make-overlay (point-min) (point-min)))
3441 (overlay-put anything-isearch-overlay 'face anything-isearch-match-face))
3443 (setq anything-isearch-message-suffix
3444 (substitute-command-keys "cancel with \\[anything-isearch-cancel]")))
3446 (error (anything-isearch-cleanup)))))
3449 (defun anything-isearch-post-command ()
3450 "Print the current pattern after every command."
3451 (anything-isearch-message)
3452 (when (anything-window)
3453 (with-anything-window
3454 (move-overlay anything-isearch-overlay anything-isearch-match-start (point)
3455 (get-buffer (anything-buffer-get))))))
3458 (defun anything-isearch-printing-char ()
3459 "Add printing char to the pattern."
3460 (interactive)
3461 (let ((char (char-to-string last-command-event)))
3462 (setq anything-isearch-pattern (concat anything-isearch-pattern char))
3464 (with-anything-window
3465 (if (looking-at char)
3466 (progn
3467 (push (list 'event 'char
3468 'start anything-isearch-match-start
3469 'pos (point-marker))
3470 anything-isearch-match-positions)
3471 (forward-char))
3473 (let ((start (point)))
3474 (while (and (re-search-forward anything-isearch-pattern nil t)
3475 (anything-pos-header-line-p)))
3476 (if (or (anything-pos-header-line-p)
3477 (eq start (point)))
3478 (progn
3479 (goto-char start)
3480 (push (list 'event 'error
3481 'start anything-isearch-match-start
3482 'pos (point-marker))
3483 anything-isearch-match-positions))
3485 (push (list 'event 'search
3486 'start anything-isearch-match-start
3487 'pos (copy-marker start))
3488 anything-isearch-match-positions)
3489 (setq anything-isearch-match-start
3490 (copy-marker (match-beginning 0))))))
3492 (anything-mark-current-line))))
3495 (defun anything-isearch-again ()
3496 "Search again for the current pattern"
3497 (interactive)
3498 (if (equal anything-isearch-pattern "")
3499 (setq anything-isearch-message-suffix "no pattern yet")
3501 (with-anything-window
3502 (let ((start (point)))
3503 (while (and (re-search-forward anything-isearch-pattern nil t)
3504 (anything-pos-header-line-p)))
3505 (if (or (anything-pos-header-line-p)
3506 (eq start (point)))
3507 (progn
3508 (goto-char start)
3509 (unless (eq 'error
3510 (plist-get (car anything-isearch-match-positions)
3511 'event))
3512 (setq anything-isearch-message-suffix "no more matches")))
3514 (push (list 'event 'search-again
3515 'start anything-isearch-match-start
3516 'pos (copy-marker start))
3517 anything-isearch-match-positions)
3518 (setq anything-isearch-match-start (copy-marker (match-beginning 0)))
3520 (anything-mark-current-line))))))
3523 (defun anything-isearch-delete ()
3524 "Undo last event."
3525 (interactive)
3526 (unless (equal anything-isearch-pattern "")
3527 (let ((last (pop anything-isearch-match-positions)))
3528 (unless (eq 'search-again (plist-get last 'event))
3529 (setq anything-isearch-pattern
3530 (substring anything-isearch-pattern 0 -1)))
3532 (with-anything-window
3533 (goto-char (plist-get last 'pos))
3534 (setq anything-isearch-match-start (plist-get last 'start))
3535 (anything-mark-current-line)))))
3538 (defun anything-isearch-default-action ()
3539 "Execute the default action for the selected candidate."
3540 (interactive)
3541 (anything-isearch-cleanup)
3542 (with-current-buffer (anything-buffer-get) (anything-exit-minibuffer)))
3545 (defun anything-isearch-select-action ()
3546 "Choose an action for the selected candidate."
3547 (interactive)
3548 (anything-isearch-cleanup)
3549 (with-anything-window
3550 (anything-select-action)))
3553 (defun anything-isearch-cancel ()
3554 "Cancel Anything isearch."
3555 (interactive)
3556 (anything-isearch-cleanup)
3557 (when (anything-window)
3558 (with-anything-window
3559 (goto-char anything-isearch-original-point)
3560 (anything-mark-current-line))))
3563 (defun anything-isearch-cleanup ()
3564 "Clean up the mess."
3565 (setq minibuffer-message-timeout anything-isearch-original-message-timeout)
3566 (with-current-buffer (anything-buffer-get)
3567 (setq overriding-terminal-local-map nil)
3568 (setq cursor-type nil)
3569 (setq cursor-in-non-selected-windows
3570 anything-isearch-original-cursor-in-non-selected-windows))
3571 (when anything-isearch-original-window
3572 (select-window anything-isearch-original-window))
3574 (use-global-map anything-isearch-original-global-map)
3575 (setq-default deferred-action-list
3576 anything-isearch-original-deferred-action-list)
3577 (when (overlayp anything-isearch-overlay)
3578 (delete-overlay anything-isearch-overlay)))
3581 (defun anything-isearch-message ()
3582 "Print prompt."
3583 (if (and (equal anything-isearch-message-suffix "")
3584 (eq (plist-get (car anything-isearch-match-positions) 'event)
3585 'error))
3586 (setq anything-isearch-message-suffix "failing"))
3588 (unless (equal anything-isearch-message-suffix "")
3589 (setq anything-isearch-message-suffix
3590 (concat " [" anything-isearch-message-suffix "]")))
3592 (message (concat "Search within results: "
3593 anything-isearch-pattern
3594 anything-isearch-message-suffix))
3596 (setq anything-isearch-message-suffix ""))
3599 ;; (@* "Utility: Iswitchb integration (unmaintained)")
3601 (defvar anything-iswitchb-candidate-selected nil
3602 "Indicates whether an anything candidate is selected from iswitchb.")
3604 (defvar anything-iswitchb-frame-configuration nil
3605 "Saved frame configuration, before anything buffer was displayed.")
3607 (defvar anything-iswitchb-saved-keys nil
3608 "The original in iswitchb before binding anything keys.")
3611 (defun anything-iswitchb-setup ()
3612 "Integrate anything completion into iswitchb (UNMAINTAINED).
3614 If the user is idle for `anything-iswitchb-idle-delay' seconds
3615 after typing something into iswitchb then anything candidates are
3616 shown for the current iswitchb input.
3618 ESC cancels anything completion and returns to normal iswitchb.
3620 Some key bindings in `anything-map' are modified.
3621 See also `anything-iswitchb-setup-keys'."
3622 (interactive)
3624 (require 'iswitchb)
3626 ;; disable timid completion during iswitchb
3627 (put 'iswitchb-buffer 'timid-completion 'disabled)
3628 (add-hook 'minibuffer-setup-hook 'anything-iswitchb-minibuffer-setup)
3630 (defadvice iswitchb-visit-buffer
3631 (around anything-iswitchb-visit-buffer activate)
3632 (if anything-iswitchb-candidate-selected
3633 (anything-execute-selection-action)
3634 ad-do-it))
3636 (defadvice iswitchb-possible-new-buffer
3637 (around anything-iswitchb-possible-new-buffer activate)
3638 (if anything-iswitchb-candidate-selected
3639 (anything-execute-selection-action)
3640 ad-do-it))
3641 (anything-iswitchb-setup-keys)
3642 (message "Iswitchb integration is activated."))
3644 (defun anything-iswitchb-setup-keys ()
3645 "Modify `anything-map' for anything-iswitchb users.
3647 C-p is used instead of M-p, because anything uses ESC
3648 (currently hardcoded) for `anything-iswitchb-cancel-anything' and
3649 Emacs handles ESC and Meta as synonyms, so ESC overrides
3650 other commands with Meta prefix.
3652 Note that iswitchb uses M-p and M-n by default for history
3653 navigation, so you should bind C-p and C-n in
3654 `iswitchb-mode-map' if you use the history keys and don't want
3655 to use different keys for iswitchb while anything is not yet
3656 kicked in. These keys are not bound automatically by anything
3657 in `iswitchb-mode-map' because they (C-n at least) already have
3658 a standard iswitchb binding which you might be accustomed to.
3660 Binding M-s is used instead of C-s, because C-s has a binding in
3661 iswitchb. You can rebind it AFTER `anything-iswitchb-setup'.
3663 Unbind C-r to prevent problems during anything-isearch."
3664 (define-key anything-map (kbd "C-s") nil)
3665 (define-key anything-map (kbd "M-p") nil)
3666 (define-key anything-map (kbd "M-n") nil)
3667 (define-key anything-map (kbd "M-v") nil)
3668 (define-key anything-map (kbd "C-v") nil)
3669 (define-key anything-map (kbd "C-p") 'anything-previous-history-element)
3670 (define-key anything-map (kbd "C-n") 'anything-next-history-element)
3671 (define-key anything-map (kbd "M-s") nil)
3672 (define-key anything-map (kbd "M-s") 'anything-isearch)
3673 (define-key anything-map (kbd "C-r") nil))
3675 (defun anything-iswitchb-minibuffer-setup ()
3676 (when (eq this-command 'iswitchb-buffer)
3677 (add-hook 'minibuffer-exit-hook 'anything-iswitchb-minibuffer-exit)
3679 (setq anything-iswitchb-frame-configuration nil)
3680 (setq anything-iswitchb-candidate-selected nil)
3681 (add-hook 'anything-update-hook 'anything-iswitchb-handle-update)
3683 (anything-initialize)
3685 (add-hook 'deferred-action-list 'anything-iswitchb-check-input)))
3688 (defun anything-iswitchb-minibuffer-exit ()
3689 (remove-hook 'minibuffer-exit-hook 'anything-iswitchb-minibuffer-exit)
3690 (remove-hook 'deferred-action-list 'anything-iswitchb-check-input)
3691 (remove-hook 'anything-update-hook 'anything-iswitchb-handle-update)
3693 (anything-cleanup)
3695 (when anything-iswitchb-frame-configuration
3696 (anything-set-frame/window-configuration
3697 anything-iswitchb-frame-configuration)
3698 (setq anything-iswitchb-frame-configuration nil)))
3701 (defun anything-iswitchb-check-input ()
3702 "Extract iswitchb input and check if it needs to be handled."
3703 (declare (special iswitchb-text))
3704 (if (or anything-iswitchb-frame-configuration
3705 (sit-for anything-iswitchb-idle-delay))
3706 (anything-check-new-input iswitchb-text)))
3709 (defun anything-iswitchb-handle-update ()
3710 "Pop up the anything buffer if it's not empty and it's not
3711 shown yet and bind anything commands in iswitchb."
3712 (unless (or (anything-empty-buffer-p anything-buffer)
3713 anything-iswitchb-frame-configuration)
3714 (setq anything-iswitchb-frame-configuration
3715 (anything-current-frame/window-configuration))
3717 (save-selected-window
3718 (if (not anything-samewindow)
3719 (pop-to-buffer anything-buffer)
3721 (select-window (get-lru-window))
3722 (switch-to-buffer anything-buffer)))
3724 (with-current-buffer (window-buffer (active-minibuffer-window))
3725 (let* ((anything-prefix "anything-")
3726 (prefix-length (length anything-prefix))
3727 (commands
3728 (delete-dups
3729 (remove-if 'null
3730 (mapcar
3731 (lambda (binding)
3732 (let ((command (cdr binding)))
3733 (when (and (symbolp command)
3734 (eq (compare-strings
3735 anything-prefix
3736 0 prefix-length
3737 (symbol-name command)
3738 0 prefix-length)
3740 command)))
3741 (cdr anything-map)))))
3742 (bindings (mapcar (lambda (command)
3743 (cons command
3744 (where-is-internal command anything-map)))
3745 commands)))
3747 (push (list 'anything-iswitchb-cancel-anything (kbd "<ESC>"))
3748 bindings)
3750 (setq anything-iswitchb-saved-keys nil)
3752 (let* ((iswitchb-prefix "iswitchb-")
3753 (prefix-length (length iswitchb-prefix)))
3754 (dolist (binding bindings)
3755 (dolist (key (cdr binding))
3756 (let ((old-command (lookup-key (current-local-map) key)))
3757 (unless (and anything-iswitchb-dont-touch-iswithcb-keys
3758 (symbolp old-command)
3759 (eq (compare-strings iswitchb-prefix
3760 0 prefix-length
3761 (symbol-name old-command)
3762 0 prefix-length)
3764 (push (cons key old-command)
3765 anything-iswitchb-saved-keys)
3766 (define-key (current-local-map) key (car binding)))))))))))
3769 (defun anything-iswitchb-cancel-anything ()
3770 "Cancel anything completion and return to standard iswitchb."
3771 (interactive)
3772 (save-excursion
3773 (dolist (binding anything-iswitchb-saved-keys)
3774 (define-key (current-local-map) (car binding) (cdr binding)))
3775 (anything-iswitchb-minibuffer-exit)))
3777 ;; (@* "Compatibility")
3779 ;; Copied assoc-default from XEmacs version 21.5.12
3780 (unless (fboundp 'assoc-default)
3781 (defun assoc-default (key alist &optional test default)
3782 "Find object KEY in a pseudo-alist ALIST.
3783 ALIST is a list of conses or objects. Each element (or the element's car,
3784 if it is a cons) is compared with KEY by evaluating (TEST (car elt) KEY).
3785 If that is non-nil, the element matches;
3786 then `assoc-default' returns the element's cdr, if it is a cons,
3787 or DEFAULT if the element is not a cons.
3789 If no element matches, the value is nil.
3790 If TEST is omitted or nil, `equal' is used."
3791 (let (found (tail alist) value)
3792 (while (and tail (not found))
3793 (let ((elt (car tail)))
3794 (when (funcall (or test 'equal) (if (consp elt) (car elt) elt) key)
3795 (setq found t value (if (consp elt) (cdr elt) default))))
3796 (setq tail (cdr tail)))
3797 value)))
3799 ;; Function not available in XEmacs,
3800 (unless (fboundp 'minibuffer-contents)
3801 (defun minibuffer-contents ()
3802 "Return the user input in a minbuffer as a string.
3803 The current buffer must be a minibuffer."
3804 (field-string (point-max)))
3806 (defun delete-minibuffer-contents ()
3807 "Delete all user input in a minibuffer.
3808 The current buffer must be a minibuffer."
3809 (delete-field (point-max))))
3811 ;; Function not available in older Emacs (<= 22.1).
3812 (unless (fboundp 'buffer-chars-modified-tick)
3813 (defun buffer-chars-modified-tick (&optional buffer)
3814 "Return BUFFER's character-change tick counter.
3815 Each buffer has a character-change tick counter, which is set to the
3816 value of the buffer's tick counter (see `buffer-modified-tick'), each
3817 time text in that buffer is inserted or deleted. By comparing the
3818 values returned by two individual calls of `buffer-chars-modified-tick',
3819 you can tell whether a character change occurred in that buffer in
3820 between these calls. No argument or nil as argument means use current
3821 buffer as BUFFER."
3822 (with-current-buffer (or buffer (current-buffer))
3823 (if (listp buffer-undo-list)
3824 (length buffer-undo-list)
3825 (buffer-modified-tick)))))
3827 ;; (@* "CUA workaround")
3828 (defadvice cua-delete-region (around anything-avoid-cua activate)
3829 (ignore-errors ad-do-it))
3830 (defadvice copy-region-as-kill (around anything-avoid-cua activate)
3831 (if cua-mode
3832 (ignore-errors ad-do-it)
3833 ad-do-it))
3836 ;;(@* "Attribute Documentation")
3837 (defun anything-describe-anything-attribute (anything-attribute)
3838 "Display the full documentation of ANYTHING-ATTRIBUTE (a symbol)."
3839 (interactive (list (intern
3840 (completing-read
3841 "Describe anything attribute: "
3842 (mapcar 'symbol-name anything-additional-attributes)))))
3843 (with-output-to-temp-buffer "*Help*"
3844 (princ (get anything-attribute 'anything-attrdoc))))
3846 (anything-document-attribute 'name "mandatory"
3847 " The name of the source. It is also the heading which appears
3848 above the list of matches from the source. Must be unique. ")
3849 (anything-document-attribute 'header-name "optional"
3850 " A function returning the display string of the header. Its
3851 argument is the name of the source. This attribute is useful to
3852 add an additional information with the source name. ")
3853 (anything-document-attribute 'candidates "mandatory if candidates-in-buffer attribute is not provided"
3854 " Specifies how to retrieve candidates from the source. It can
3855 either be a variable name, a function called with no parameters
3856 or the actual list of candidates.
3858 The list must be a list whose members are strings, symbols
3859 or (DISPLAY . REAL) pairs.
3861 In case of (DISPLAY . REAL) pairs, the DISPLAY string is shown
3862 in the Anything buffer, but the REAL one is used as action
3863 argument when the candidate is selected. This allows a more
3864 readable presentation for candidates which would otherwise be,
3865 for example, too long or have a common part shared with other
3866 candidates which can be safely replaced with an abbreviated
3867 string for display purposes.
3869 Note that if the (DISPLAY . REAL) form is used then pattern
3870 matching is done on the displayed string, not on the real
3871 value.
3873 If the candidates have to be retrieved asynchronously (for
3874 example, by an external command which takes a while to run)
3875 then the function should start the external command
3876 asynchronously and return the associated process object.
3877 Anything will take care of managing the process (receiving the
3878 output from it, killing it if necessary, etc.). The process
3879 should return candidates matching the current pattern (see
3880 variable `anything-pattern'.)
3882 Note that currently results from asynchronous sources appear
3883 last in the anything buffer regardless of their position in
3884 `anything-sources'. ")
3885 (anything-document-attribute 'action "mandatory if type attribute is not provided"
3886 " It is a list of (DISPLAY . FUNCTION) pairs or FUNCTION.
3887 FUNCTION is called with one parameter: the selected candidate.
3889 An action other than the default can be chosen from this list
3890 of actions for the currently selected candidate (by default
3891 with TAB). The DISPLAY string is shown in the completions
3892 buffer and the FUNCTION is invoked when an action is
3893 selected. The first action of the list is the default. ")
3894 (anything-document-attribute 'type "optional if action attribute is provided"
3895 " Indicates the type of the items the source returns.
3897 Merge attributes not specified in the source itself from
3898 `anything-type-attributes'.
3900 This attribute is implemented by plug-in. ")
3901 (anything-document-attribute 'init "optional"
3902 " Function called with no parameters when anything is started. It
3903 is useful for collecting current state information which can be
3904 used to create the list of candidates later.
3906 For example, if a source needs to work with the current
3907 directory then it can store its value here, because later
3908 anything does its job in the minibuffer and in the
3909 `anything-buffer' and the current directory can be different
3910 there. ")
3911 (anything-document-attribute 'delayed-init "optional"
3912 " Function called with no parameters before candidate function is
3913 called. It is similar with `init' attribute, but its
3914 evaluation is deferred. It is useful to combine with ")
3915 (anything-document-attribute 'match "optional"
3916 " List of functions called with one parameter: a candidate. The
3917 function should return non-nil if the candidate matches the
3918 current pattern (see variable `anything-pattern').
3920 This attribute allows the source to override the default
3921 pattern matching based on `string-match'. It can be used, for
3922 example, to implement a source for file names and do the
3923 pattern matching on the basename of files, since it's more
3924 likely one is typing part of the basename when searching for a
3925 file, instead of some string anywhere else in its path.
3927 If the list contains more than one function then the list of
3928 matching candidates from the source is constructed by appending
3929 the results after invoking the first function on all the
3930 potential candidates, then the next function, and so on. The
3931 matching candidates supplied by the first function appear first
3932 in the list of results and then results from the other
3933 functions, respectively.
3935 This attribute has no effect for asynchronous sources (see
3936 attribute `candidates'), since they perform pattern matching
3937 themselves. ")
3938 (anything-document-attribute 'candidate-transformer "optional"
3939 " It's a function or a list of functions called with one argument
3940 when the completion list from the source is built. The argument
3941 is the list of candidates retrieved from the source. The
3942 function should return a transformed list of candidates which
3943 will be used for the actual completion. If it is a list of
3944 functions, it calls each function sequentially.
3946 This can be used to transform or remove items from the list of
3947 candidates.
3949 Note that `candidates' is run already, so the given transformer
3950 function should also be able to handle candidates with (DISPLAY
3951 . REAL) format. ")
3952 (anything-document-attribute 'filtered-candidate-transformer "optional"
3953 " It has the same format as `candidate-transformer', except the
3954 function is called with two parameters: the candidate list and
3955 the source.
3957 This transformer is run on the candidate list which is already
3958 filtered by the current pattern. While `candidate-transformer'
3959 is run only once, it is run every time the input pattern is
3960 changed.
3962 It can be used to transform the candidate list dynamically, for
3963 example, based on the current pattern.
3965 In some cases it may also be more efficent to perform candidate
3966 transformation here, instead of with `candidate-transformer'
3967 even if this transformation is done every time the pattern is
3968 changed. For example, if a candidate set is very large then
3969 `candidate-transformer' transforms every candidate while only
3970 some of them will actually be dislpayed due to the limit
3971 imposed by `anything-candidate-number-limit'.
3973 Note that `candidates' and `candidate-transformer' is run
3974 already, so the given transformer function should also be able
3975 to handle candidates with (DISPLAY . REAL) format.
3977 This option has no effect for asynchronous sources. (Not yet,
3978 at least. ")
3979 (anything-document-attribute 'action-transformer "optional"
3980 " It's a function or a list of functions called with two
3981 arguments when the action list from the source is
3982 assembled. The first argument is the list of actions, the
3983 second is the current selection. If it is a list of functions,
3984 it calls each function sequentially.
3986 The function should return a transformed action list.
3988 This can be used to customize the list of actions based on the
3989 currently selected candidate. ")
3990 (anything-document-attribute 'pattern-transformer "optional"
3991 " It's a function or a list of functions called with one argument
3992 before computing matches. Its argument is `anything-pattern'.
3993 Functions should return transformed `anything-pattern'.
3995 It is useful to change interpretation of `anything-pattern'. ")
3996 (anything-document-attribute 'delayed "optional"
3997 " Candidates from the source are shown only if the user stops
3998 typing and is idle for `anything-idle-delay' seconds. ")
3999 (anything-document-attribute 'volatile "optional"
4000 " Indicates the source assembles the candidate list dynamically,
4001 so it shouldn't be cached within a single Anything
4002 invocation. It is only applicable to synchronous sources,
4003 because asynchronous sources are not cached. ")
4004 (anything-document-attribute 'requires-pattern "optional"
4005 " If present matches from the source are shown only if the
4006 pattern is not empty. Optionally, it can have an integer
4007 parameter specifying the required length of input which is
4008 useful in case of sources with lots of candidates. ")
4009 (anything-document-attribute 'persistent-action "optional"
4010 " Function called with one parameter; the selected candidate.
4012 An action performed by `anything-execute-persistent-action'.
4013 If none, use the default action. ")
4014 (anything-document-attribute 'candidates-in-buffer "optional"
4015 " Shortcut attribute for making and narrowing candidates using
4016 buffers. This newly-introduced attribute prevents us from
4017 forgetting to add volatile and match attributes.
4019 See docstring of `anything-candidates-in-buffer'.
4021 (candidates-in-buffer) is equivalent of three attributes:
4022 (candidates . anything-candidates-in-buffer)
4023 (volatile)
4024 (match identity)
4026 (candidates-in-buffer . candidates-function) is equivalent of:
4027 (candidates . candidates-function)
4028 (volatile)
4029 (match identity)
4031 This attribute is implemented by plug-in. ")
4032 (anything-document-attribute 'search "optional"
4033 " List of functions like `re-search-forward' or `search-forward'.
4034 Buffer search function used by `anything-candidates-in-buffer'.
4035 By default, `anything-candidates-in-buffer' uses `re-search-forward'.
4036 This attribute is meant to be used with
4037 (candidates . anything-candidates-in-buffer) or
4038 (candidates-in-buffer) in short. ")
4039 (anything-document-attribute 'search-from-end "optional"
4040 " Make `anything-candidates-in-buffer' search from the end of buffer.
4041 If this attribute is specified, `anything-candidates-in-buffer' uses
4042 `re-search-backward' instead. ")
4043 (anything-document-attribute 'get-line "optional"
4044 " A function like `buffer-substring-no-properties' or `buffer-substring'.
4045 This function converts point of line-beginning and point of line-end,
4046 which represents a candidate computed by `anything-candidates-in-buffer'.
4047 By default, `anything-candidates-in-buffer' uses
4048 `buffer-substring-no-properties'. ")
4049 (anything-document-attribute 'display-to-real "optional"
4050 " Function called with one parameter; the selected candidate.
4052 The function transforms the selected candidate, and the result
4053 is passed to the action function. The display-to-real
4054 attribute provides another way to pass other string than one
4055 shown in Anything buffer.
4057 Traditionally, it is possible to make candidates,
4058 candidate-transformer or filtered-candidate-transformer
4059 function return a list with (DISPLAY . REAL) pairs. But if REAL
4060 can be generated from DISPLAY, display-to-real is more
4061 convenient and faster. ")
4062 (anything-document-attribute 'real-to-display "optional"
4063 " Function called with one parameter; the selected candidate.
4065 The inverse of display-to-real attribute.
4067 The function transforms the selected candidate, which is passed
4068 to the action function, for display. The real-to-display
4069 attribute provides the other way to pass other string than one
4070 shown in Anything buffer.
4072 Traditionally, it is possible to make candidates,
4073 candidate-transformer or filtered-candidate-transformer
4074 function return a list with (DISPLAY . REAL) pairs. But if
4075 DISPLAY can be generated from REAL, real-to-display is more
4076 convenient.
4078 Note that DISPLAY parts returned from candidates /
4079 candidate-transformer are IGNORED as the name `display-to-real'
4080 says. ")
4081 (anything-document-attribute 'cleanup "optional"
4082 " Function called with no parameters when *anything* buffer is closed. It
4083 is useful for killing unneeded candidates buffer.
4085 Note that the function is executed BEFORE performing action. ")
4086 (anything-document-attribute 'candidate-number-limit "optional"
4087 " Override `anything-candidate-number-limit' only for this source. ")
4088 (anything-document-attribute 'accept-empty "optional"
4089 " Pass empty string \"\" to action function. ")
4090 (anything-document-attribute 'disable-shortcuts "optional"
4091 " Disable `anything-enable-shortcuts' in current `anything' session.
4093 This attribute is implemented by plug-in. ")
4094 (anything-document-attribute 'dummy "optional"
4095 " Set `anything-pattern' to candidate. If this attribute is
4096 specified, The candidates attribute is ignored.
4098 This attribute is implemented by plug-in.
4099 This plug-in implies disable-shortcuts plug-in. ")
4100 (anything-document-attribute 'multiline "optional"
4101 " Enable to selection multiline candidates. ")
4102 (anything-document-attribute 'update "optional"
4103 " Function called with no parameters when \\<anything-map>\\[anything-force-update] is pressed. ")
4104 (anything-document-attribute 'mode-line "optional"
4105 " source local `anything-mode-line-string'. (included in `mode-line-format')
4106 It accepts also variable/function name. ")
4107 (anything-document-attribute 'header-line "optional"
4108 " source local `header-line-format'.
4109 It accepts also variable/function name. ")
4110 (anything-document-attribute 'resume "optional" " Function called with no parameters when `anything-resume' is started.")
4112 ;; (@* "Bug Report")
4113 (defvar anything-maintainer-mail-address
4114 (concat "rubiki" "tch@ru" "by-lang.org"))
4115 (defvar anything-bug-report-salutation
4116 "Describe bug below, using a precise recipe.
4118 When I executed M-x ...
4120 How to send a bug report:
4121 1) Be sure to use the LATEST version of anything.el.
4122 2) Enable debugger. M-x toggle-debug-on-error or (setq debug-on-error t)
4123 3) Use Lisp version instead of compiled one: (load \"anything.el\")
4124 4) If you got an error, please paste *Backtrace* buffer.
4125 5) Type C-c C-c to send.
4126 # If you are a Japanese, please write in Japanese:-)")
4127 (defvar anything-no-dump-variables
4128 '(anything-candidate-buffer-alist
4129 anything-digit-overlays
4130 anything-help-message
4131 anything-candidate-cache
4133 "Variables not to dump in bug report.")
4135 (defun anything-dumped-variables-in-bug-report ()
4136 (let ((hash (make-hash-table)))
4137 (loop for var in (apropos-internal "anything-" 'boundp)
4138 for vname = (symbol-name var)
4139 unless (or (string-match "-map$" vname)
4140 (string-match "^anything-c-source-" vname)
4141 (string-match "-hash$" vname)
4142 (string-match "-face$" vname)
4143 (memq var anything-no-dump-variables))
4144 collect var)))
4146 (defun anything-send-bug-report ()
4147 "Send a bug report of anything.el."
4148 (interactive)
4149 (with-current-buffer (or anything-last-buffer
4150 (current-buffer))
4151 (reporter-submit-bug-report
4152 anything-maintainer-mail-address
4153 "anything.el"
4154 (anything-dumped-variables-in-bug-report)
4155 nil nil
4156 anything-bug-report-salutation)))
4158 (defun anything-send-bug-report-from-anything ()
4159 "Send a bug report of anything.el in anything session."
4160 (interactive)
4161 (anything-run-after-quit 'anything-send-bug-report))
4163 ;; (@* "Unit Tests")
4165 (defun* anything-test-candidates (sources &optional (input "") (compile-source-functions anything-compile-source-functions-default))
4166 "Test helper function for anything.
4167 Given pseudo `anything-sources' and `anything-pattern', returns list like
4168 ((\"source name1\" (\"candidate1\" \"candidate2\"))
4169 (\"source name2\" (\"candidate3\" \"candidate4\")))
4171 (let ((anything-test-mode t)
4172 anything-enable-shortcuts
4173 anything-candidate-cache
4174 (anything-compile-source-functions compile-source-functions)
4175 anything-before-initialize-hook
4176 anything-after-initialize-hook
4177 anything-update-hook
4178 anything-test-candidate-list)
4179 (get-buffer-create anything-buffer)
4181 (anything-initialize-1 nil input sources)
4182 (anything-update)
4183 ;; test-mode spec: select 1st candidate!
4184 (with-current-buffer anything-buffer
4185 (forward-line 1)
4186 (anything-mark-current-line))
4187 (prog1
4188 anything-test-candidate-list
4189 (anything-cleanup))))
4191 (defmacro anything-test-update (sources pattern)
4192 "Test helper macro for anything. It is meant for testing *anything* buffer contents."
4193 `(progn (stub anything-get-sources => ,sources)
4194 (stub anything-log-run-hook => nil)
4195 (stub anything-maybe-fit-frame => nil)
4196 (stub run-with-idle-timer => nil)
4197 (let (anything-test-mode (anything-pattern ,pattern))
4198 (anything-update))))
4200 ;;;; unit test
4201 ;; (install-elisp "http://www.emacswiki.org/cgi-bin/wiki/download/el-expectations.el")
4202 ;; (install-elisp "http://www.emacswiki.org/cgi-bin/wiki/download/el-mock.el")
4203 (dont-compile
4204 (when (fboundp 'expectations)
4205 (expectations
4206 (desc "anything-current-buffer")
4207 (expect "__a_buffer"
4208 (with-current-buffer (get-buffer-create "__a_buffer")
4209 (anything-test-candidates '(((name . "FOO"))) "")
4210 (prog1
4211 (buffer-name anything-current-buffer)
4212 (kill-buffer "__a_buffer")
4214 (desc "anything-buffer-file-name")
4215 (expect (regexp "/__a_file__")
4216 (with-current-buffer (get-buffer-create "__a_file__")
4217 (setq buffer-file-name "/__a_file__")
4218 (anything-test-candidates '(((name . "FOO"))) "")
4219 (prog1
4220 anything-buffer-file-name
4221 ;;(kill-buffer "__a_file__")
4223 (desc "anything-interpret-value")
4224 (expect "literal"
4225 (anything-interpret-value "literal"))
4226 (expect "lambda"
4227 (anything-interpret-value (lambda () "lambda")))
4228 (expect "lambda with source name"
4229 (let ((source '((name . "lambda with source name"))))
4230 (anything-interpret-value (lambda () anything-source-name) source)))
4231 (expect "function symbol"
4232 (flet ((f () "function symbol"))
4233 (anything-interpret-value 'f)))
4234 (expect "variable symbol"
4235 (let ((v "variable symbol"))
4236 (anything-interpret-value 'v)))
4237 (expect (error error *)
4238 (anything-interpret-value 'unbounded-1))
4239 (desc "anything-compile-sources")
4240 (expect '(((name . "foo")))
4241 (anything-compile-sources '(((name . "foo"))) nil)
4243 (expect '(((name . "foo") (type . test) (action . identity)))
4244 (let ((anything-type-attributes '((test (action . identity)))))
4245 (anything-compile-sources '(((name . "foo") (type . test)))
4246 '(anything-compile-source--type))))
4247 (desc "anything-sources accepts symbols")
4248 (expect '(((name . "foo")))
4249 (let* ((foo '((name . "foo"))))
4250 (anything-compile-sources '(foo) nil)))
4251 (desc "anything-get-sources action")
4252 (expect '(((name . "Actions") (candidates . actions)))
4253 (stub anything-action-window => t)
4254 (let (anything-compiled-sources
4255 (anything-sources '(((name . "Actions") (candidates . actions)))))
4256 (anything-get-sources)))
4257 (desc "get-buffer-create candidates-buffer")
4258 (expect '(((name . "many") (init . many-init)
4259 (candidates-in-buffer . anything-candidates-in-buffer)
4260 (candidates . anything-candidates-in-buffer)
4261 (volatile) (match identity)))
4262 (anything-compile-sources
4263 '(((name . "many") (init . many-init)
4264 (candidates-in-buffer . anything-candidates-in-buffer)))
4265 '(anything-compile-source--candidates-in-buffer)))
4266 (expect '(((name . "many") (init . many-init)
4267 (candidates-in-buffer)
4268 (candidates . anything-candidates-in-buffer)
4269 (volatile) (match identity)))
4270 (anything-compile-sources
4271 '(((name . "many") (init . many-init)
4272 (candidates-in-buffer)))
4273 '(anything-compile-source--candidates-in-buffer)))
4274 (expect '(((name . "many") (init . many-init)
4275 (candidates-in-buffer)
4276 (type . test)
4277 (action . identity)
4278 (candidates . anything-candidates-in-buffer)
4279 (volatile) (match identity)))
4280 (let ((anything-type-attributes '((test (action . identity)))))
4281 (anything-compile-sources
4282 '(((name . "many") (init . many-init)
4283 (candidates-in-buffer)
4284 (type . test)))
4285 '(anything-compile-source--type
4286 anything-compile-source--candidates-in-buffer))))
4288 (desc "anything-get-candidates")
4289 (expect '("foo" "bar")
4290 (anything-get-candidates '((name . "foo") (candidates "foo" "bar"))))
4291 (expect '("FOO" "BAR")
4292 (anything-get-candidates '((name . "foo") (candidates "foo" "bar")
4293 (candidate-transformer
4294 . (lambda (cands) (mapcar 'upcase cands))))))
4295 (expect '("foo" "bar")
4296 (anything-get-candidates '((name . "foo")
4297 (candidates . (lambda () '("foo" "bar"))))))
4298 (expect '("foo" "bar")
4299 (let ((var '("foo" "bar")))
4300 (anything-get-candidates '((name . "foo")
4301 (candidates . var)))))
4302 (expect (error error *)
4303 (anything-get-candidates '((name . "foo")
4304 (candidates . "err"))))
4305 (expect (error error *)
4306 (let ((var "err"))
4307 (anything-get-candidates '((name . "foo")
4308 (candidates . var)))))
4309 (expect (error error *)
4310 (anything-get-candidates '((name . "foo")
4311 (candidates . unDeFined-syMbol))))
4312 (desc "anything-compute-matches")
4313 (expect '("foo" "bar")
4314 (let ((anything-pattern ""))
4315 (anything-compute-matches '((name . "FOO") (candidates "foo" "bar") (volatile)))))
4316 (expect '("foo")
4317 (let ((anything-pattern "oo"))
4318 (anything-compute-matches '((name . "FOO") (candidates "foo" "bar") (volatile)))))
4319 (expect '("bar")
4320 (let ((anything-pattern "^b"))
4321 (anything-compute-matches '((name . "FOO") (candidates "foo" "bar") (volatile)))))
4322 (expect '("a" "b")
4323 (let ((anything-pattern "")
4324 (anything-candidate-number-limit 2))
4325 (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c") (volatile)))))
4326 (expect '("a" "b")
4327 (let ((anything-pattern ".")
4328 (anything-candidate-number-limit 2))
4329 (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c") (volatile)))))
4330 (expect '("a" "b" "c")
4331 (let ((anything-pattern "")
4332 anything-candidate-number-limit)
4333 (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c") (volatile)))))
4334 (expect '("a" "b" "c")
4335 (let ((anything-pattern "[abc]")
4336 anything-candidate-number-limit)
4337 (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c") (volatile)))))
4338 (expect '(a b c)
4339 (let ((anything-pattern "[abc]")
4340 anything-candidate-number-limit)
4341 (anything-compute-matches '((name . "FOO") (candidates a b c) (volatile)))))
4342 (expect '(("foo" . "FOO") ("bar" . "BAR"))
4343 (let ((anything-pattern ""))
4344 (anything-compute-matches '((name . "FOO") (candidates ("foo" . "FOO") ("bar" . "BAR")) (volatile)))))
4345 (expect '(("foo" . "FOO"))
4346 (let ((anything-pattern "foo"))
4347 (anything-compute-matches '((name . "FOO") (candidates ("foo" . "FOO") ("bar" . "foo")) (volatile)))))
4348 ;; using anything-test-candidate-list
4349 (desc "anything-test-candidates")
4350 (expect '(("FOO" ("foo" "bar")))
4351 (anything-test-candidates '(((name . "FOO") (candidates "foo" "bar")))))
4352 (expect '(("FOO" ("bar")))
4353 (anything-test-candidates '(((name . "FOO") (candidates "foo" "bar"))) "ar"))
4354 (expect '(("T1" ("hoge" "aiue"))
4355 ("T2" ("test" "boke")))
4356 (anything-test-candidates '(((name . "T1") (candidates "hoge" "aiue"))
4357 ((name . "T2") (candidates "test" "boke")))))
4358 (expect '(("T1" ("hoge"))
4359 ("T2" ("boke")))
4360 (anything-test-candidates '(((name . "T1") (candidates "hoge" "aiue"))
4361 ((name . "T2") (candidates "test" "boke"))) "o"))
4362 (desc "requires-pattern attribute")
4363 (expect nil
4364 (anything-test-candidates '(((name . "FOO") (candidates "foo" "bar")
4365 (requires-pattern . 1)))))
4366 (expect '(("FOO" ("bar")))
4367 (anything-test-candidates '(((name . "FOO") (candidates "foo" "bar")
4368 (requires-pattern . 1))) "b"))
4369 (desc "delayed attribute(for test)")
4370 (expect '(("T2" ("boke"))
4371 ("T1" ("hoge")))
4372 (anything-test-candidates
4373 '(((name . "T1") (candidates "hoge" "aiue") (delayed))
4374 ((name . "T2") (candidates "test" "boke")))
4375 "o"))
4376 (desc "match attribute(prefix search)")
4377 (expect '(("FOO" ("bar")))
4378 (anything-test-candidates
4379 '(((name . "FOO") (candidates "foo" "bar")
4380 (match (lambda (c) (string-match (concat "^" anything-pattern) c)))))
4381 "ba"))
4382 (expect nil
4383 (anything-test-candidates
4384 '(((name . "FOO") (candidates "foo" "bar")
4385 (match (lambda (c) (string-match (concat "^" anything-pattern) c)))))
4386 "ar"))
4387 (expect "TestSource"
4388 (let (x)
4389 (anything-test-candidates
4390 '(((name . "TestSource") (candidates "a")
4391 (match (lambda (c) (setq x anything-source-name)))))
4392 "a")
4394 (desc "init attribute")
4395 (expect '(("FOO" ("bar")))
4396 (let (v)
4397 (anything-test-candidates
4398 '(((name . "FOO") (init . (lambda () (setq v '("foo" "bar"))))
4399 (candidates . v)))
4400 "ar")))
4401 (desc "candidate-transformer attribute")
4402 (expect '(("FOO" ("BAR")))
4403 (anything-test-candidates '(((name . "FOO") (candidates "foo" "bar")
4404 (candidate-transformer
4405 . (lambda (cands) (mapcar 'upcase cands)))))
4406 "ar"))
4407 (desc "filtered-candidate-transformer attribute")
4408 ;; needs more tests
4409 (expect '(("FOO" ("BAR")))
4410 (anything-test-candidates '(((name . "FOO") (candidates "foo" "bar")
4411 (filtered-candidate-transformer
4412 . (lambda (cands src) (mapcar 'upcase cands)))))
4413 "ar"))
4414 (desc "anything-transform-candidates in process")
4415 (expect (mock (anything-composed-funcall-with-source
4416 '((name . "FOO") (candidates "foo" "bar")
4417 (filtered-candidate-transformer
4418 . (lambda (cands src) (mapcar 'upcase cands))))
4419 (lambda (cands src) (mapcar 'upcase cands))
4420 '("foo" "bar")
4421 '((name . "FOO") (candidates "foo" "bar")
4422 (filtered-candidate-transformer
4423 . (lambda (cands src) (mapcar 'upcase cands))))
4425 (stub anything-process-candidate-transformer => '("foo" "bar"))
4426 (anything-transform-candidates
4427 '("foo" "bar")
4428 '((name . "FOO") (candidates "foo" "bar")
4429 (filtered-candidate-transformer
4430 . (lambda (cands src) (mapcar 'upcase cands))))
4433 (desc "anything-candidates-in-buffer-1")
4434 (expect nil
4435 (anything-candidates-in-buffer-1
4436 nil ""
4437 'buffer-substring-no-properties '(re-search-forward) 50 nil))
4438 (expect '("foo+" "bar+" "baz+")
4439 (with-temp-buffer
4440 (insert "foo+\nbar+\nbaz+\n")
4441 (anything-candidates-in-buffer-1
4442 (current-buffer) ""
4443 'buffer-substring-no-properties '(re-search-forward) 5 nil)))
4444 (expect '("foo+" "bar+")
4445 (with-temp-buffer
4446 (insert "foo+\nbar+\nbaz+\n")
4447 (anything-candidates-in-buffer-1
4448 (current-buffer) ""
4449 'buffer-substring-no-properties '(re-search-forward) 2 nil)))
4450 (expect '("foo+")
4451 (with-temp-buffer
4452 (insert "foo+\nbar+\nbaz+\n")
4453 (anything-candidates-in-buffer-1
4454 (current-buffer) "oo\\+"
4455 'buffer-substring-no-properties '(re-search-forward) 50 nil)))
4456 (expect '("foo+")
4457 (with-temp-buffer
4458 (insert "foo+\nbar+\nbaz+\n")
4459 (anything-candidates-in-buffer-1
4460 (current-buffer) "oo+"
4461 #'buffer-substring-no-properties '(search-forward) 50 nil)))
4462 (expect '("foo+" "bar+")
4463 (with-temp-buffer
4464 (insert "foo+\nbar+\nbaz+\n")
4465 (anything-candidates-in-buffer-1
4466 (current-buffer) "."
4467 'buffer-substring-no-properties '(re-search-forward) 2 nil)))
4468 (expect '(("foo+" "FOO+"))
4469 (with-temp-buffer
4470 (insert "foo+\nbar+\nbaz+\n")
4471 (anything-candidates-in-buffer-1
4472 (current-buffer) "oo\\+"
4473 (lambda (s e)
4474 (let ((l (buffer-substring-no-properties s e)))
4475 (list l (upcase l))))
4476 '(re-search-forward) 50 nil)))
4477 (desc "anything-candidates-in-buffer")
4478 (expect '(("TEST" ("foo+" "bar+" "baz+")))
4479 (anything-test-candidates
4480 '(((name . "TEST")
4481 (init
4482 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4483 (insert "foo+\nbar+\nbaz+\n"))))
4484 (candidates . anything-candidates-in-buffer)
4485 (match identity)
4486 (volatile)))))
4487 (expect '(("TEST" ("foo+" "bar+" "baz+")))
4488 (let (anything-candidate-number-limit)
4489 (anything-test-candidates
4490 '(((name . "TEST")
4491 (init
4492 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4493 (insert "foo+\nbar+\nbaz+\n"))))
4494 (candidates . anything-candidates-in-buffer)
4495 (match identity)
4496 (volatile))))))
4497 (expect '(("TEST" ("foo+")))
4498 (anything-test-candidates
4499 '(((name . "TEST")
4500 (init
4501 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4502 (insert "foo+\nbar+\nbaz+\n"))))
4503 (candidates . anything-candidates-in-buffer)
4504 (match identity)
4505 (volatile)))
4506 "oo\\+"))
4507 ;; BUG remain empty string, but the pattern is rare case.
4508 (expect '(("a" ("" "a" "b")))
4509 (anything-test-candidates
4510 '(((name . "a")
4511 (init . (lambda ()
4512 (with-current-buffer (anything-candidate-buffer 'global)
4513 (insert "a\nb\n"))))
4514 (candidates-in-buffer)))
4515 "a*"))
4516 (desc "search attribute")
4517 (expect '(("TEST" ("foo+")))
4518 (anything-test-candidates
4519 '(((name . "TEST")
4520 (init
4521 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4522 (insert "foo+\nbar+\nbaz+\nooo\n"))))
4523 (search search-forward)
4524 (candidates . anything-candidates-in-buffer)
4525 (match identity)
4526 (volatile)))
4527 "oo+"))
4528 (expect '(("TEST" ("foo+" "ooo")))
4529 (anything-test-candidates
4530 '(((name . "TEST")
4531 (init
4532 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4533 (insert "foo+\nbar+\nbaz+\nooo\n"))))
4534 (search search-forward re-search-forward)
4535 (candidates . anything-candidates-in-buffer)
4536 (match identity)
4537 (volatile)))
4538 "oo+"))
4539 (expect '(("TEST" ("foo+" "ooo")))
4540 (anything-test-candidates
4541 '(((name . "TEST")
4542 (init
4543 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4544 (insert "foo+\nbar+\nbaz+\nooo\n"))))
4545 (search re-search-forward search-forward)
4546 (candidates . anything-candidates-in-buffer)
4547 (match identity)
4548 (volatile)))
4549 "oo+"))
4550 (expect '(("TEST" ("ooo" "foo+")))
4551 (anything-test-candidates
4552 '(((name . "TEST")
4553 (init
4554 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4555 (insert "bar+\nbaz+\nooo\nfoo+\n"))))
4556 (search re-search-forward search-forward)
4557 (candidates . anything-candidates-in-buffer)
4558 (match identity)
4559 (volatile)))
4560 "oo+"))
4561 ;; faster exact match
4562 (expect '(("TEST" ("bar+")))
4563 (anything-test-candidates
4564 '(((name . "TEST")
4565 (init
4566 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4567 (insert "bar+\nbaz+\nooo\nfoo+\n"))))
4568 (search (lambda (pattern &rest _)
4569 (and (search-forward (concat "\n" pattern "\n") nil t)
4570 (forward-line -1))))
4571 (candidates . anything-candidates-in-buffer)
4572 (match identity)
4573 (volatile)))
4574 "bar+"))
4575 ;; faster prefix match
4576 (expect '(("TEST" ("bar+")))
4577 (anything-test-candidates
4578 '(((name . "TEST")
4579 (init
4580 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4581 (insert "bar+\nbaz+\nooo\nfoo+\n"))))
4582 (search (lambda (pattern &rest _)
4583 (search-forward (concat "\n" pattern) nil t)))
4584 (candidates . anything-candidates-in-buffer)
4585 (match identity)
4586 (volatile)))
4587 "ba"))
4588 (desc "anything-current-buffer-is-modified")
4589 (expect '(("FOO" ("modified")))
4590 (let ((sources '(((name . "FOO")
4591 (candidates
4592 . (lambda ()
4593 (if (anything-current-buffer-is-modified)
4594 '("modified")
4595 '("unmodified"))))))))
4596 (with-temp-buffer
4597 (clrhash anything-tick-hash)
4598 (insert "1")
4599 (anything-test-candidates sources))))
4600 (expect '(("FOO" ("unmodified")))
4601 (let ((sources '(((name . "FOO")
4602 (candidates
4603 . (lambda ()
4604 (if (anything-current-buffer-is-modified)
4605 '("modified")
4606 '("unmodified"))))))))
4607 (with-temp-buffer
4608 (clrhash anything-tick-hash)
4609 (insert "1")
4610 (anything-test-candidates sources)
4611 (anything-test-candidates sources))))
4612 (expect '(("FOO" ("modified")))
4613 (let ((sources '(((name . "FOO")
4614 (candidates
4615 . (lambda ()
4616 (if (anything-current-buffer-is-modified)
4617 '("modified")
4618 '("unmodified"))))))))
4619 (with-temp-buffer
4620 (clrhash anything-tick-hash)
4621 (insert "1")
4622 (anything-test-candidates sources)
4623 (insert "2")
4624 (anything-test-candidates sources))))
4625 (expect '(("BAR" ("modified")))
4626 (let ((sources1 '(((name . "FOO")
4627 (candidates
4628 . (lambda ()
4629 (if (anything-current-buffer-is-modified)
4630 '("modified")
4631 '("unmodified")))))))
4632 (sources2 '(((name . "BAR")
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 sources1)
4642 (anything-test-candidates sources2))))
4643 (expect '(("FOO" ("unmodified")))
4644 (let ((sources1 '(((name . "FOO")
4645 (candidates
4646 . (lambda ()
4647 (if (anything-current-buffer-is-modified)
4648 '("modified")
4649 '("unmodified")))))))
4650 (sources2 '(((name . "BAR")
4651 (candidates
4652 . (lambda ()
4653 (if (anything-current-buffer-is-modified)
4654 '("modified")
4655 '("unmodified"))))))))
4656 (with-temp-buffer
4657 (clrhash anything-tick-hash)
4658 (insert "1")
4659 (anything-test-candidates sources1)
4660 (anything-test-candidates sources2)
4661 (anything-test-candidates sources1))))
4662 (expect '(("BAR" ("unmodified")))
4663 (let ((sources1 '(((name . "FOO")
4664 (candidates
4665 . (lambda ()
4666 (if (anything-current-buffer-is-modified)
4667 '("modified")
4668 '("unmodified")))))))
4669 (sources2 '(((name . "BAR")
4670 (candidates
4671 . (lambda ()
4672 (if (anything-current-buffer-is-modified)
4673 '("modified")
4674 '("unmodified"))))))))
4675 (with-temp-buffer
4676 (clrhash anything-tick-hash)
4677 (insert "1")
4678 (anything-test-candidates sources1)
4679 (anything-test-candidates sources2)
4680 (anything-test-candidates sources2))))
4681 (expect '(("BAR" ("modified")))
4682 (let ((sources1 '(((name . "FOO")
4683 (candidates
4684 . (lambda ()
4685 (if (anything-current-buffer-is-modified)
4686 '("modified")
4687 '("unmodified")))))))
4688 (sources2 '(((name . "BAR")
4689 (candidates
4690 . (lambda ()
4691 (if (anything-current-buffer-is-modified)
4692 '("modified")
4693 '("unmodified"))))))))
4694 (with-temp-buffer
4695 (clrhash anything-tick-hash)
4696 (insert "1")
4697 (anything-test-candidates sources1)
4698 (anything-test-candidates sources2)
4699 (with-temp-buffer
4700 (anything-test-candidates sources2)))))
4701 (desc "anything-source-name")
4702 (expect "FOO"
4703 (let (v)
4704 (anything-test-candidates '(((name . "FOO")
4705 (init
4706 . (lambda () (setq v anything-source-name)))
4707 (candidates "ok"))))
4709 (expect "FOO"
4710 (let (v)
4711 (anything-test-candidates '(((name . "FOO")
4712 (candidates
4713 . (lambda ()
4714 (setq v anything-source-name)
4715 '("ok"))))))
4717 (expect "FOO"
4718 (let (v)
4719 (anything-test-candidates '(((name . "FOO")
4720 (candidates "ok")
4721 (candidate-transformer
4722 . (lambda (c)
4723 (setq v anything-source-name)
4724 c)))))
4726 (expect "FOO"
4727 (let (v)
4728 (anything-test-candidates '(((name . "FOO")
4729 (candidates "ok")
4730 (filtered-candidate-transformer
4731 . (lambda (c s)
4732 (setq v anything-source-name)
4733 c)))))
4735 (expect "FOO"
4736 (let (v)
4737 (anything-test-candidates '(((name . "FOO")
4738 (candidates "ok")
4739 (display-to-real
4740 . (lambda (c)
4741 (setq v anything-source-name)
4743 (action . identity))))
4744 (anything-execute-selection-action)
4746 (desc "anything-candidate-buffer create")
4747 (expect " *anything candidates:FOO*"
4748 (let* (anything-candidate-buffer-alist
4749 (anything-source-name "FOO")
4750 (buf (anything-candidate-buffer 'global)))
4751 (prog1 (buffer-name buf)
4752 (kill-buffer buf))))
4753 (expect " *anything candidates:FOO*aTestBuffer"
4754 (let* (anything-candidate-buffer-alist
4755 (anything-source-name "FOO")
4756 (anything-current-buffer (get-buffer-create "aTestBuffer"))
4757 (buf (anything-candidate-buffer 'local)))
4758 (prog1 (buffer-name buf)
4759 (kill-buffer anything-current-buffer)
4760 (kill-buffer buf))))
4761 (expect 0
4762 (let (anything-candidate-buffer-alist
4763 (anything-source-name "FOO") buf)
4764 (with-current-buffer (anything-candidate-buffer 'global)
4765 (insert "1"))
4766 (setq buf (anything-candidate-buffer 'global))
4767 (prog1 (buffer-size buf)
4768 (kill-buffer buf))))
4769 (desc "anything-candidate-buffer get-buffer")
4770 (expect " *anything candidates:FOO*"
4771 (let* (anything-candidate-buffer-alist
4772 (anything-source-name "FOO")
4773 (buf (anything-candidate-buffer 'global)))
4774 (prog1 (buffer-name (anything-candidate-buffer))
4775 (kill-buffer buf))))
4776 (expect " *anything candidates:FOO*aTestBuffer"
4777 (let* (anything-candidate-buffer-alist
4778 (anything-source-name "FOO")
4779 (anything-current-buffer (get-buffer-create "aTestBuffer"))
4780 (buf (anything-candidate-buffer 'local)))
4781 (prog1 (buffer-name (anything-candidate-buffer))
4782 (kill-buffer anything-current-buffer)
4783 (kill-buffer buf))))
4784 (expect " *anything candidates:FOO*"
4785 (let* (anything-candidate-buffer-alist
4786 (anything-source-name "FOO")
4787 (buf-local (anything-candidate-buffer 'local))
4788 (buf-global (anything-candidate-buffer 'global)))
4789 (prog1 (buffer-name (anything-candidate-buffer))
4790 (kill-buffer buf-local)
4791 (kill-buffer buf-global))))
4792 (expect " *anything candidates:FOO*aTestBuffer"
4793 (let* (anything-candidate-buffer-alist
4794 (anything-source-name "FOO")
4795 (anything-current-buffer (get-buffer-create "aTestBuffer"))
4796 (buf-global (anything-candidate-buffer 'global))
4797 (buf-local (anything-candidate-buffer 'local)))
4798 (prog1 (buffer-name (anything-candidate-buffer))
4799 (kill-buffer buf-local)
4800 (kill-buffer buf-global))))
4801 (expect nil
4802 (let* (anything-candidate-buffer-alist
4803 (anything-source-name "NOP__"))
4804 (anything-candidate-buffer)))
4805 (desc "anything-candidate-buffer register-buffer")
4806 (expect " *anything test candidates*"
4807 (let (anything-candidate-buffer-alist
4808 (buf (get-buffer-create " *anything test candidates*")))
4809 (with-current-buffer buf
4810 (insert "1\n2\n")
4811 (prog1 (buffer-name (anything-candidate-buffer buf))
4812 (kill-buffer (current-buffer))))))
4813 (expect " *anything test candidates*"
4814 (let (anything-candidate-buffer-alist
4815 (buf (get-buffer-create " *anything test candidates*")))
4816 (with-current-buffer buf
4817 (insert "1\n2\n")
4818 (anything-candidate-buffer buf)
4819 (prog1 (buffer-name (anything-candidate-buffer))
4820 (kill-buffer (current-buffer))))))
4821 (expect "1\n2\n"
4822 (let (anything-candidate-buffer-alist
4823 (buf (get-buffer-create " *anything test candidates*")))
4824 (with-current-buffer buf
4825 (insert "1\n2\n")
4826 (anything-candidate-buffer buf)
4827 (prog1 (buffer-string)
4828 (kill-buffer (current-buffer))))))
4829 (expect "buf1"
4830 (let (anything-candidate-buffer-alist
4831 (anything-source-name "foo")
4832 (buf1 (get-buffer-create "buf1"))
4833 (buf2 (get-buffer-create "buf2")))
4834 (anything-candidate-buffer buf1)
4835 (anything-candidate-buffer buf2)
4836 (prog1 (buffer-name (anything-candidate-buffer buf1))
4837 (kill-buffer buf1)
4838 (kill-buffer buf2))))
4839 (desc "action attribute")
4840 (expect "foo"
4841 (anything-test-candidates
4842 '(((name . "TEST")
4843 (candidates "foo")
4844 (action ("identity" . identity)))))
4845 (anything-execute-selection-action))
4846 (expect "foo"
4847 (anything-test-candidates
4848 '(((name . "TEST")
4849 (candidates "foo")
4850 (action ("identity" . (lambda (c) (identity c)))))))
4851 (anything-execute-selection-action))
4852 (desc "anything-get-default-action")
4853 (expect 'upcase
4854 (anything-get-default-action '(("upcase" . upcase))))
4855 (expect 'downcase
4856 (anything-get-default-action '(("downcase" . downcase))))
4857 (expect (lambda (x) (capitalize x))
4858 (anything-get-default-action (lambda (x) (capitalize x))))
4859 (expect 'identity
4860 (anything-get-default-action 'identity))
4861 (desc "anything-execute-selection-action")
4862 (expect "FOO"
4863 (anything-execute-selection-action
4864 "foo" '(("upcase" . upcase)) nil))
4865 (expect "FOO"
4866 (anything-execute-selection-action
4867 "foo" '(("upcase" . (lambda (c) (upcase c)))) nil))
4869 (desc "display-to-real attribute")
4870 (expect "FOO"
4871 (anything-test-candidates
4872 '(((name . "TEST")
4873 (candidates "foo")
4874 (display-to-real . upcase)
4875 (action ("identity" . identity)))))
4876 (anything-execute-selection-action))
4877 (desc "cleanup test")
4878 (expect 'cleaned
4879 (let (v)
4880 (anything-test-candidates
4881 '(((name . "TEST")
4882 (cleanup . (lambda () (setq v 'cleaned))))))
4884 (desc "anything-get-current-source")
4885 ;; in init/candidates/action/candidate-transformer/filtered-candidate-transformer
4886 ;; display-to-real/cleanup function
4887 (expect "FOO"
4888 (assoc-default
4889 'name
4890 (anything-funcall-with-source '((name . "FOO")) 'anything-get-current-source)))
4891 ;; init
4892 (expect "FOO"
4893 (let (v)
4894 (anything-test-candidates
4895 '(((name . "FOO")
4896 (init . (lambda () (setq v (anything-get-current-source)))))))
4897 (assoc-default 'name v)))
4898 ;; candidates
4899 (expect "FOO"
4900 (let (v)
4901 (anything-test-candidates
4902 '(((name . "FOO")
4903 (candidates . (lambda () (setq v (anything-get-current-source)) '("a"))))))
4904 (assoc-default 'name v)))
4905 ;; action
4906 (expect "FOO"
4907 (let (v)
4908 (anything-test-candidates
4909 '(((name . "FOO")
4910 (candidates "a")
4911 (action
4912 . (lambda (c) (setq v (anything-get-current-source)) c)))))
4913 (anything-execute-selection-action)
4914 (assoc-default 'name v)))
4915 ;; candidate-transformer
4916 (expect "FOO"
4917 (let (v)
4918 (anything-test-candidates
4919 '(((name . "FOO")
4920 (candidates "a")
4921 (candidate-transformer
4922 . (lambda (c) (setq v (anything-get-current-source)) c)))))
4923 (assoc-default 'name v)))
4924 ;; filtered-candidate-transformer
4925 (expect "FOO"
4926 (let (v)
4927 (anything-test-candidates
4928 '(((name . "FOO")
4929 (candidates "a")
4930 (filtered-candidate-transformer
4931 . (lambda (c s) (setq v (anything-get-current-source)) c)))))
4932 (assoc-default 'name v)))
4933 ;; action-transformer
4934 (expect "FOO"
4935 (let (v)
4936 (anything-test-candidates
4937 '(((name . "FOO")
4938 (candidates "a")
4939 (action-transformer
4940 . (lambda (a c) (setq v (anything-get-current-source)) a))
4941 (action . identity))))
4942 (anything-execute-selection-action)
4943 (assoc-default 'name v)))
4944 ;; display-to-real
4945 (expect "FOO"
4946 (let (v)
4947 (anything-test-candidates
4948 '(((name . "FOO")
4949 (init . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
4950 (insert "a\n"))))
4951 (candidates-in-buffer)
4952 (display-to-real
4953 . (lambda (c) (setq v (anything-get-current-source)) c))
4954 (action . identity))))
4955 (anything-execute-selection-action)
4956 (assoc-default 'name v)))
4957 ;; cleanup
4958 (expect "FOO"
4959 (let (v)
4960 (anything-test-candidates
4961 '(((name . "FOO")
4962 (candidates "a")
4963 (cleanup
4964 . (lambda () (setq v (anything-get-current-source)))))))
4965 (assoc-default 'name v)))
4966 ;; candidates are displayed
4967 (expect "TEST"
4968 (anything-test-candidates
4969 '(((name . "TEST")
4970 (candidates "foo")
4971 (action ("identity" . identity)))))
4972 (assoc-default 'name (anything-get-current-source)))
4973 (desc "anything-attr")
4974 (expect "FOO"
4975 (anything-funcall-with-source
4976 '((name . "FOO"))
4977 (lambda ()
4978 (anything-attr 'name))))
4979 (expect 'fuga
4980 (let (v)
4981 (anything-test-candidates
4982 '(((name . "FOO")
4983 (hoge . fuga)
4984 (init . (lambda () (setq v (anything-attr 'hoge))))
4985 (candidates "a"))))
4987 (expect nil
4988 (let (v)
4989 (anything-test-candidates
4990 '(((name . "FOO")
4991 (init . (lambda () (setq v (anything-attr 'hoge))))
4992 (candidates "a"))))
4994 (expect nil
4995 (let (v)
4996 (anything-test-candidates
4997 '(((name . "FOO")
4998 (hoge) ;INCOMPATIBLE!
4999 (init . (lambda () (setq v (anything-attr 'hoge))))
5000 (candidates "a"))))
5002 (desc "anything-attr*")
5003 (expect "generic"
5004 (let (v (value1 "generic"))
5005 (anything-test-candidates
5006 '(((name . "FOO")
5007 (hoge . value1)
5008 (init . (lambda () (setq v (anything-attr* 'hoge)))))))
5010 (desc "anything-attr-defined")
5011 (expect (non-nil)
5012 (let (v)
5013 (anything-test-candidates
5014 '(((name . "FOO")
5015 (hoge)
5016 (init . (lambda () (setq v (anything-attr-defined 'hoge))))
5017 (candidates "a"))))
5018 v))
5019 (expect nil
5020 (let (v)
5021 (anything-test-candidates
5022 '(((name . "FOO")
5023 (init . (lambda () (setq v (anything-attr-defined 'hoge))))
5024 (candidates "a"))))
5025 v))
5026 (desc "anything-attrset")
5027 (expect '((name . "FOO") (hoge . 77))
5028 (let ((src '((name . "FOO") (hoge))))
5029 (anything-attrset 'hoge 77 src)
5030 src))
5031 (expect 77
5032 (anything-attrset 'hoge 77 '((name . "FOO") (hoge))))
5034 (expect '((name . "FOO") (hoge . 77))
5035 (let ((src '((name . "FOO") (hoge . 1))))
5036 (anything-attrset 'hoge 77 src)
5037 src))
5039 (expect '((name . "FOO") (hoge . 77) (x))
5040 (let ((src '((name . "FOO") (x))))
5041 (anything-attrset 'hoge 77 src)
5042 src))
5043 (expect 77
5044 (anything-attrset 'hoge 77 '((name . "FOO"))))
5045 (desc "anything-preselect")
5046 ;; entire candidate
5047 (expect "foo"
5048 (with-current-buffer (anything-create-anything-buffer t)
5049 (let ((anything-pattern "")
5050 (anything-test-mode t))
5051 (anything-process-source '((name . "test")
5052 (candidates "hoge" "foo" "bar")))
5053 (anything-preselect "foo")
5054 (anything-get-selection))))
5055 ;; regexp
5056 (expect "foo"
5057 (with-current-buffer (anything-create-anything-buffer t)
5058 (let ((anything-pattern "")
5059 (anything-test-mode t))
5060 (anything-process-source '((name . "test")
5061 (candidates "hoge" "foo" "bar")))
5062 (anything-preselect "fo+")
5063 (anything-get-selection))))
5064 ;; no match -> first entry
5065 (expect "hoge"
5066 (with-current-buffer (anything-create-anything-buffer t)
5067 (let ((anything-pattern "")
5068 (anything-test-mode t))
5069 (anything-process-source '((name . "test")
5070 (candidates "hoge" "foo" "bar")))
5071 (anything-preselect "not found")
5072 (anything-get-selection))))
5073 (desc "anything-check-new-input")
5074 (expect "newpattern"
5075 (stub anything-update)
5076 (stub anything-action-window)
5077 (let ((anything-pattern "pattern"))
5078 (anything-check-new-input "newpattern")
5079 anything-pattern))
5080 ;; anything-input == nil when action window is available
5081 (expect nil
5082 (stub anything-update)
5083 (stub anything-action-window => t)
5084 (let ((anything-pattern "pattern")
5085 anything-input)
5086 (anything-check-new-input "newpattern")
5087 anything-input))
5088 ;; anything-input == anything-pattern unless action window is available
5089 (expect "newpattern"
5090 (stub anything-update)
5091 (stub anything-action-window => nil)
5092 (let ((anything-pattern "pattern")
5093 anything-input)
5094 (anything-check-new-input "newpattern")
5095 anything-input))
5096 (expect (mock (anything-update))
5097 (stub anything-action-window)
5098 (let (anything-pattern)
5099 (anything-check-new-input "foo")))
5100 (desc "anything-update")
5101 (expect (mock (anything-process-source '((name . "1"))))
5102 (anything-test-update '(((name . "1"))) ""))
5103 ;; (find-function 'anything-update)
5104 ;; TODO el-mock.el should express 2nd call of function.
5105 ;; (expect (mock (anything-process-source '((name . "2"))))
5106 ;; (stub anything-get-sources => '(((name . "1")) ((name . "2"))))
5107 ;; (stub anything-log-run-hook)
5108 ;; (stub anything-maybe-fit-frame)
5109 ;; (stub run-with-idle-timer)
5110 ;; (anything-update))
5111 (expect (mock (run-with-idle-timer * nil 'anything-process-delayed-sources
5112 '(((name . "2") (delayed)))))
5113 (stub anything-get-sources => '(((name . "1"))
5114 ((name . "2") (delayed))))
5115 (stub anything-log-run-hook)
5116 (stub anything-maybe-fit-frame)
5117 (let ((anything-pattern "") anything-test-mode)
5118 (anything-update)))
5120 (desc "requires-pattern attribute")
5121 (expect (not-called anything-process-source)
5122 (anything-test-update '(((name . "1") (requires-pattern))) ""))
5123 (expect (not-called anything-process-source)
5124 (anything-test-update '(((name . "1") (requires-pattern . 3))) "xx"))
5126 (desc "anything-normalize-sources")
5127 (expect '(anything-c-source-test)
5128 (anything-normalize-sources 'anything-c-source-test))
5129 (expect '(anything-c-source-test)
5130 (anything-normalize-sources '(anything-c-source-test)))
5131 (expect '(anything-c-source-test)
5132 (let ((anything-sources '(anything-c-source-test)))
5133 (anything-normalize-sources nil)))
5134 (expect '(((name . "test")))
5135 (anything-normalize-sources '((name . "test"))))
5136 (expect '(((name . "test")))
5137 (anything-normalize-sources '(((name . "test")))))
5138 (desc "anything-get-action")
5139 (expect '(("identity" . identity))
5140 (stub buffer-size => 1)
5141 (stub anything-get-current-source => '((name . "test")
5142 (action ("identity" . identity))))
5143 (anything-get-action))
5144 (expect '((("identity" . identity)) "action-transformer is called")
5145 (stub buffer-size => 1)
5146 (stub anything-get-current-source
5147 => '((name . "test")
5148 (action ("identity" . identity))
5149 (action-transformer
5150 . (lambda (actions selection)
5151 (list actions selection)))))
5152 (stub anything-get-selection => "action-transformer is called")
5153 (anything-get-action))
5154 (desc "anything-select-nth-action")
5155 (expect "selection"
5156 (stub anything-get-selection => "selection")
5157 (stub anything-exit-minibuffer)
5158 (let (anything-saved-selection)
5159 (anything-select-nth-action 1)
5160 anything-saved-selection))
5161 (expect 'cadr
5162 (stub anything-get-action => '(("0" . car) ("1" . cdr) ("2" . cadr)))
5163 (stub anything-exit-minibuffer)
5164 (stub anything-get-selection => "selection")
5165 (let (anything-saved-action)
5166 (anything-select-nth-action 2)
5167 anything-saved-action))
5168 (desc "anything-funcall-foreach")
5169 (expect (mock (upcase "foo"))
5170 (stub anything-get-sources => '(((init . (lambda () (upcase "foo"))))))
5171 (anything-funcall-foreach 'init))
5172 (expect (mock (downcase "bar"))
5173 (stub anything-get-sources => '(((init . (lambda () (upcase "foo"))))
5174 ((init . (lambda () (downcase "bar"))))))
5175 (anything-funcall-foreach 'init))
5176 (expect (not-called anything-funcall-with-source)
5177 (stub anything-get-sources => '(((init . (lambda () (upcase "foo"))))))
5178 (anything-funcall-foreach 'not-found))
5179 ;; TODO anything-select-with-digit-shortcut test
5180 (desc "anything-get-cached-candidates")
5181 (expect '("cached" "version")
5182 (let ((anything-candidate-cache '(("test" "cached" "version"))))
5183 (anything-get-cached-candidates '((name . "test")
5184 (candidates "new")))))
5185 (expect '("new")
5186 (let ((anything-candidate-cache '(("other" "cached" "version"))))
5187 (anything-get-cached-candidates '((name . "test")
5188 (candidates "new")))))
5189 (expect '(("test" "new")
5190 ("other" "cached" "version"))
5191 (let ((anything-candidate-cache '(("other" "cached" "version"))))
5192 (anything-get-cached-candidates '((name . "test")
5193 (candidates "new")))
5194 anything-candidate-cache))
5195 (expect '(("other" "cached" "version"))
5196 (let ((anything-candidate-cache '(("other" "cached" "version"))))
5197 (anything-get-cached-candidates '((name . "test")
5198 (candidates "new")
5199 (volatile)))
5200 anything-candidate-cache))
5201 ;; TODO when candidates == process
5202 ;; TODO anything-output-filter
5203 (desc "candidate-number-limit attribute")
5204 (expect '("a" "b")
5205 (let ((anything-pattern "")
5206 (anything-candidate-number-limit 20))
5207 (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c")
5208 (candidate-number-limit . 2) (volatile)))))
5209 (expect '("a" "b")
5210 (let ((anything-pattern "[abc]")
5211 (anything-candidate-number-limit 20))
5212 (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c")
5213 (candidate-number-limit . 2) (volatile)))))
5214 (expect '("a" "b" "c" "d")
5215 (let ((anything-pattern "[abcd]")
5216 (anything-candidate-number-limit 2))
5217 (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c" "d")
5218 (candidate-number-limit) (volatile)))))
5219 (expect '(("TEST" ("a" "b" "c")))
5220 (let ((anything-candidate-number-limit 2))
5221 (anything-test-candidates
5222 '(((name . "TEST")
5223 (init
5224 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5225 (insert "a\nb\nc\nd\n"))))
5226 (candidates . anything-candidates-in-buffer)
5227 (match identity)
5228 (candidate-number-limit . 3)
5229 (volatile))))))
5230 (expect '(("TEST" ("a" "b" "c")))
5231 (let ((anything-candidate-number-limit 2))
5232 (anything-test-candidates
5233 '(((name . "TEST")
5234 (init
5235 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5236 (insert "a\nb\nc\nd\n"))))
5237 (candidates . anything-candidates-in-buffer)
5238 (match identity)
5239 (candidate-number-limit . 3)
5240 (volatile)))
5241 ".")))
5242 (desc "multiple init")
5243 (expect '(1 . 2)
5244 (let (a b)
5245 (anything-test-candidates
5246 '(((name . "test")
5247 (init (lambda () (setq a 1))
5248 (lambda () (setq b 2))))))
5249 (cons a b)))
5250 (expect 1
5251 (let (a)
5252 (anything-test-candidates
5253 '(((name . "test")
5254 (init (lambda () (setq a 1))))))
5256 (desc "multiple cleanup")
5257 (expect '(1 . 2)
5258 (let (a b)
5259 (anything-test-candidates
5260 '(((name . "test")
5261 (cleanup (lambda () (setq a 1))
5262 (lambda () (setq b 2))))))
5263 (cons a b)))
5264 (desc "anything-mklist")
5265 (expect '(1)
5266 (anything-mklist 1))
5267 (expect '(2)
5268 (anything-mklist '(2)))
5269 (expect '((lambda ()))
5270 (anything-mklist (lambda ())))
5271 (desc "anything-before-initialize-hook")
5272 (expect 'called
5273 (let ((anything-before-initialize-hook '((lambda () (setq v 'called))))
5275 (anything-initialize)
5277 (desc "anything-after-initialize-hook")
5278 (expect '(b a)
5279 (let ((anything-before-initialize-hook
5280 '((lambda () (setq v '(a)))))
5281 (anything-after-initialize-hook
5282 '((lambda () (setq v (cons 'b v)))))
5284 (anything-initialize)
5286 (expect 0
5287 (let ((anything-after-initialize-hook
5288 '((lambda () (setq v (buffer-size (get-buffer anything-buffer))))))
5290 (anything-initialize)
5292 (desc "get-line attribute")
5293 (expect '(("TEST" ("FOO+")))
5294 (anything-test-candidates
5295 '(((name . "TEST")
5296 (init
5297 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5298 (insert "foo+\nbar+\nbaz+\n"))))
5299 (candidates-in-buffer)
5300 (get-line . (lambda (s e) (upcase (buffer-substring-no-properties s e))))))
5301 "oo\\+"))
5302 (desc "with-anything-restore-variables")
5303 (expect '(7 8)
5304 (let ((a 7) (b 8)
5305 (anything-restored-variables '(a b)))
5306 (with-anything-restore-variables
5307 (setq a 0 b 0))
5308 (list a b)))
5309 (desc "anything-cleanup-hook")
5310 (expect 'called
5311 (let ((anything-cleanup-hook
5312 '((lambda () (setq v 'called))))
5314 (anything-cleanup)
5316 (desc "with-anything-display-same-window")
5317 (expect (non-nil)
5318 (save-window-excursion
5319 (delete-other-windows)
5320 (split-window)
5322 (let ((buf (get-buffer-create " tmp"))
5323 (win (selected-window)))
5324 (with-anything-display-same-window
5325 (display-buffer buf)
5326 (eq win (get-buffer-window buf))))))
5327 (expect (non-nil)
5328 (save-window-excursion
5329 (delete-other-windows)
5330 (split-window)
5332 (let ((buf (get-buffer-create " tmp"))
5333 (win (selected-window)))
5334 (with-anything-display-same-window
5335 (pop-to-buffer buf)
5336 (eq win (get-buffer-window buf))))))
5337 (expect (non-nil)
5338 (save-window-excursion
5339 (delete-other-windows)
5340 (split-window)
5342 (let ((buf (get-buffer-create " tmp"))
5343 (win (selected-window)))
5344 (with-anything-display-same-window
5345 (switch-to-buffer buf)
5346 (eq win (get-buffer-window buf))))))
5347 (expect (non-nil)
5348 (save-window-excursion
5349 (delete-other-windows)
5350 (let ((buf (get-buffer-create " tmp"))
5351 (win (selected-window)))
5352 (with-anything-display-same-window
5353 (display-buffer buf)
5354 (eq win (get-buffer-window buf))))))
5355 (expect (non-nil)
5356 (save-window-excursion
5357 (delete-other-windows)
5358 (let ((buf (get-buffer-create " tmp"))
5359 (win (selected-window)))
5360 (with-anything-display-same-window
5361 (pop-to-buffer buf)
5362 (eq win (get-buffer-window buf))))))
5363 (desc "search-from-end attribute")
5364 (expect '(("TEST" ("baz+" "bar+" "foo+")))
5365 (anything-test-candidates
5366 '(((name . "TEST")
5367 (init
5368 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5369 (insert "foo+\nbar+\nbaz+\n"))))
5370 (candidates-in-buffer)
5371 (search-from-end)))))
5372 (expect '(("TEST" ("baz+" "bar+" "foo+")))
5373 (anything-test-candidates
5374 '(((name . "TEST")
5375 (init
5376 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5377 (insert "foo+\nbar+\nbaz+\n"))))
5378 (candidates-in-buffer)
5379 (search-from-end)))
5380 "\\+"))
5381 (expect '(("TEST" ("baz+" "bar+")))
5382 (anything-test-candidates
5383 '(((name . "TEST")
5384 (init
5385 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5386 (insert "foo+\nbar+\nbaz+\n"))))
5387 (candidates-in-buffer)
5388 (search-from-end)
5389 (candidate-number-limit . 2)))))
5390 (expect '(("TEST" ("baz+" "bar+")))
5391 (anything-test-candidates
5392 '(((name . "TEST")
5393 (init
5394 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5395 (insert "foo+\nbar+\nbaz+\n"))))
5396 (candidates-in-buffer)
5397 (search-from-end)
5398 (candidate-number-limit . 2)))
5399 "\\+"))
5400 (expect '(("a" ("c2" "c1")))
5401 (anything-test-candidates
5402 '(((name . "a")
5403 (init . (lambda ()
5404 (with-current-buffer (anything-candidate-buffer 'global)
5405 (insert "c1\nc2\n"))))
5406 (search-from-end)
5407 (candidates-in-buffer)))))
5408 ;; BUG remain empty string, but the pattern is rare case.
5409 (expect '(("a" ("c" "b" "a" "")))
5410 (anything-test-candidates
5411 '(((name . "a")
5412 (init . (lambda ()
5413 (with-current-buffer (anything-candidate-buffer 'global)
5414 (insert "a\nb\nc\n"))))
5415 (search-from-end)
5416 (candidates-in-buffer)))
5417 "a*"))
5418 (desc "header-name attribute")
5419 (expect "original is transformed"
5420 (anything-test-update '(((name . "original")
5421 (candidates "1")
5422 (header-name
5423 . (lambda (name)
5424 (format "%s is transformed" name)))))
5426 (with-current-buffer (anything-buffer-get)
5427 (buffer-string)
5428 (overlay-get (car (overlays-at (1+(point-min)))) 'display)))
5429 (desc "volatile and match attribute")
5430 ;; candidates function is called once per `anything-process-delayed-sources'
5431 (expect 1
5432 (let ((v 0))
5433 (anything-test-candidates '(((name . "test")
5434 (candidates . (lambda () (incf v) '("ok")))
5435 (volatile)
5436 (match identity identity identity)))
5437 "o")
5439 (desc "accept-empty attribute")
5440 (expect nil
5441 (anything-test-candidates
5442 '(((name . "test") (candidates "") (action . identity))))
5443 (anything-execute-selection-action))
5444 (expect ""
5445 (anything-test-candidates
5446 '(((name . "test") (candidates "") (action . identity) (accept-empty))))
5447 (anything-execute-selection-action))
5448 (desc "anything-tick-hash")
5449 (expect nil
5450 (with-current-buffer (get-buffer-create " *00create+*")
5451 (puthash " *00create+*/xxx" 1 anything-tick-hash)
5452 (kill-buffer (current-buffer)))
5453 (gethash " *00create+*/xxx" anything-tick-hash))
5454 (desc "anything-execute-action-at-once-if-once")
5455 (expect "HOGE"
5456 (let ((anything-execute-action-at-once-if-one t))
5457 (anything '(((name . "one test1")
5458 (candidates "hoge")
5459 (action . upcase))))))
5460 (expect "ANY"
5461 (let ((anything-execute-action-at-once-if-one t))
5462 (anything '(((name . "one test2")
5463 (candidates "hoge" "any")
5464 (action . upcase)))
5465 "an")))
5466 ;; candidates > 1
5467 (expect (mock (read-string "word: " nil))
5468 (let ((anything-execute-action-at-once-if-one t))
5469 (anything '(((name . "one test3")
5470 (candidates "hoge" "foo" "bar")
5471 (action . identity)))
5472 nil "word: ")))
5473 (desc "anything-quit-if-no-candidate")
5474 (expect nil
5475 (let ((anything-quit-if-no-candidate t))
5476 (anything '(((name . "zero test1") (candidates) (action . upcase))))))
5477 (expect 'called
5478 (let (v (anything-quit-if-no-candidate (lambda () (setq v 'called))))
5479 (anything '(((name . "zero test2") (candidates) (action . upcase))))
5481 (desc "real-to-display attribute")
5482 (expect '(("test" (("DDD" . "ddd"))))
5483 (anything-test-candidates '(((name . "test")
5484 (candidates "ddd")
5485 (real-to-display . upcase)
5486 (action . identity)))))
5487 (expect '(("test" (("DDD" . "ddd"))))
5488 (anything-test-candidates '(((name . "test")
5489 (candidates ("ignored" . "ddd"))
5490 (real-to-display . upcase)
5491 (action . identity)))))
5492 (expect '(("Commands" (("xxxhoge" . "hoge") ("xxxboke" . "boke"))))
5493 (anything-test-candidates '(((name . "Commands")
5494 (candidates
5495 "hoge" "boke")
5496 (real-to-display . (lambda (x) (concat "xxx" x)))
5497 (action . identity)))
5498 "xxx"))
5499 (expect "test\nDDD\n"
5500 (anything-test-update '(((name . "test")
5501 (candidates "ddd")
5502 (real-to-display . upcase)
5503 (action . identity)))
5505 (with-current-buffer (anything-buffer-get) (buffer-string)))
5506 (desc "real-to-display and candidate-transformer attribute")
5507 (expect '(("test" (("DDD" . "ddd"))))
5508 (anything-test-candidates
5509 '(((name . "test")
5510 (candidates "ddd")
5511 (candidate-transformer (lambda (cands) (mapcar (lambda (c) (cons "X" c)) cands)))
5512 (real-to-display . upcase)
5513 (action . identity)))))
5514 (expect "test\nDDD\n"
5515 (anything-test-update
5516 '(((name . "test")
5517 (candidates "ddd")
5518 (candidate-transformer (lambda (cands) (mapcar (lambda (c) (cons "X" c)) cands)))
5519 (real-to-display . upcase)
5520 (action . identity)))
5522 (with-current-buffer (anything-buffer-get) (buffer-string)))
5523 (desc "real-to-display and candidates-in-buffer")
5524 (expect '(("test" (("A" . "a") ("B" . "b"))))
5525 (anything-test-candidates
5526 '(((name . "test")
5527 (init
5528 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5529 (erase-buffer)
5530 (insert "a\nb\n"))))
5531 (candidates-in-buffer)
5532 (real-to-display . upcase)
5533 (action . identity)))))
5534 (expect "test\nA\nB\n"
5535 (stub read-string)
5536 (anything
5537 '(((name . "test")
5538 (init
5539 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5540 (erase-buffer)
5541 (insert "a\nb\n"))))
5542 (candidates-in-buffer)
5543 (real-to-display . upcase)
5544 (action . identity))))
5545 (with-current-buffer (anything-buffer-get) (buffer-string)))
5546 (desc "Symbols are acceptable as candidate.")
5547 (expect '(("test" (sym "str")))
5548 (anything-test-candidates
5549 '(((name . "test")
5550 (candidates sym "str")))))
5551 (expect '(("test" ((sym . realsym) ("str" . "realstr"))))
5552 (anything-test-candidates
5553 '(((name . "test")
5554 (candidates (sym . realsym) ("str" . "realstr"))))))
5555 (expect '(("test" (sym)))
5556 (anything-test-candidates
5557 '(((name . "test")
5558 (candidates sym "str")))
5559 "sym"))
5560 (expect '(("test" ("str")))
5561 (anything-test-candidates
5562 '(((name . "test")
5563 (candidates sym "str")))
5564 "str"))
5565 (expect '(("test" ((sym . realsym))))
5566 (anything-test-candidates
5567 '(((name . "test")
5568 (candidates (sym . realsym) ("str" . "realstr"))))
5569 "sym"))
5570 (expect '(("test" (("str" . "realstr"))))
5571 (anything-test-candidates
5572 '(((name . "test")
5573 (candidates (sym . realsym) ("str" . "realstr"))))
5574 "str"))
5575 (desc "multiple transformers")
5576 (expect '(("test" ("<FOO>")))
5577 (anything-test-candidates
5578 '(((name . "test")
5579 (candidates "foo")
5580 (candidate-transformer
5581 . (lambda (cands)
5582 (anything-compose (list cands)
5583 (list (lambda (c) (mapcar 'upcase c))
5584 (lambda (c) (list (concat "<" (car c) ">")))))))))))
5585 (expect '("<FOO>")
5586 (anything-composed-funcall-with-source
5587 '((name . "test"))
5588 (list (lambda (c) (mapcar 'upcase c))
5589 (lambda (c) (list (concat "<" (car c) ">"))))
5590 '("foo"))
5592 (expect '(("test" ("<FOO>")))
5593 (anything-test-candidates
5594 '(((name . "test")
5595 (candidates "foo")
5596 (candidate-transformer
5597 (lambda (c) (mapcar 'upcase c))
5598 (lambda (c) (list (concat "<" (car c) ">"))))))))
5599 (expect '(("test" ("<BAR>")))
5600 (anything-test-candidates
5601 '(((name . "test")
5602 (candidates "bar")
5603 (filtered-candidate-transformer
5604 (lambda (c s) (mapcar 'upcase c))
5605 (lambda (c s) (list (concat "<" (car c) ">"))))))))
5606 (expect '(("find-file" . find-file)
5607 ("view-file" . view-file))
5608 (stub zerop => nil)
5609 (stub anything-get-current-source
5610 => '((name . "test")
5611 (action)
5612 (action-transformer
5613 . (lambda (a s)
5614 (anything-compose
5615 (list a s)
5616 (list (lambda (a s) (push '("view-file" . view-file) a))
5617 (lambda (a s) (push '("find-file" . find-file) a))))))))
5618 (anything-get-action))
5619 (expect '(("find-file" . find-file)
5620 ("view-file" . view-file))
5621 (stub zerop => nil)
5622 (stub anything-get-current-source
5623 => '((name . "test")
5624 (action)
5625 (action-transformer
5626 (lambda (a s) (push '("view-file" . view-file) a))
5627 (lambda (a s) (push '("find-file" . find-file) a)))))
5628 (anything-get-action))
5629 (desc "define-anything-type-attribute")
5630 (expect '((file (action . find-file)))
5631 (let (anything-type-attributes)
5632 (define-anything-type-attribute 'file '((action . find-file)))
5633 anything-type-attributes))
5634 (expect '((file (action . find-file)))
5635 (let ((anything-type-attributes '((file (action . view-file)))))
5636 (define-anything-type-attribute 'file '((action . find-file)))
5637 anything-type-attributes))
5638 (expect '((file (action . find-file))
5639 (buffer (action . switch-to-buffer)))
5640 (let (anything-type-attributes)
5641 (define-anything-type-attribute 'buffer '((action . switch-to-buffer)))
5642 (define-anything-type-attribute 'file '((action . find-file)))
5643 anything-type-attributes))
5644 (desc "anything-approximate-candidate-number")
5645 (expect 0
5646 (with-temp-buffer
5647 (let ((anything-buffer (current-buffer)))
5648 (anything-approximate-candidate-number))))
5649 (expect 1
5650 (with-temp-buffer
5651 (let ((anything-buffer (current-buffer)))
5652 (insert "Title\n"
5653 "candiate1\n")
5654 (anything-approximate-candidate-number))))
5655 (expect t
5656 (with-temp-buffer
5657 (let ((anything-buffer (current-buffer)))
5658 (insert "Title\n"
5659 "candiate1\n"
5660 "candiate2\n")
5661 (<= 2 (anything-approximate-candidate-number)))))
5662 (expect 1
5663 (with-temp-buffer
5664 (let ((anything-buffer (current-buffer)))
5665 (insert "Title\n"
5666 (propertize "multi\nline\n" 'anything-multiline t))
5667 (anything-approximate-candidate-number))))
5668 (expect t
5669 (with-temp-buffer
5670 (let ((anything-buffer (current-buffer))
5671 (anything-candidate-separator "-----"))
5672 (insert "Title\n"
5673 (propertize "multi\nline1\n" 'anything-multiline t)
5674 "-----\n"
5675 (propertize "multi\nline2\n" 'anything-multiline t))
5676 (<= 2 (anything-approximate-candidate-number)))))
5677 (desc "delayed-init attribute")
5678 (expect 0
5679 (let ((value 0))
5680 (anything-test-candidates '(((name . "test")
5681 (delayed-init . (lambda () (incf value)))
5682 (candiates "abc")
5683 (requires-pattern . 2)))
5685 value))
5686 (expect 1
5687 (let ((value 0))
5688 (anything-test-candidates '(((name . "test")
5689 (delayed-init . (lambda () (incf value)))
5690 (candiates "abc")
5691 (requires-pattern . 2)))
5692 "abc")
5693 value))
5694 (expect 2
5695 (let ((value 0))
5696 (anything-test-candidates '(((name . "test")
5697 (delayed-init (lambda () (incf value))
5698 (lambda () (incf value)))
5699 (candiates "abc")
5700 (requires-pattern . 2)))
5701 "abc")
5702 value))
5703 (expect t
5704 (let (value)
5705 (with-temp-buffer
5706 (anything-test-candidates '(((name . "test")
5707 (delayed-init
5708 . (lambda () (setq value
5709 (eq anything-current-buffer (current-buffer)))))
5710 (candiates "abc")
5711 (requires-pattern . 2)))
5712 "abc")
5713 value)))
5714 (desc "pattern-transformer attribute")
5715 (expect '(("test2" ("foo")) ("test3" ("bar")))
5716 (anything-test-candidates '(((name . "test1")
5717 (candidates "foo" "bar"))
5718 ((name . "test2")
5719 (pattern-transformer . (lambda (pat) (substring pat 1)))
5720 (candidates "foo" "bar"))
5721 ((name . "test3")
5722 (pattern-transformer . (lambda (pat) "bar"))
5723 (candidates "foo" "bar")))
5724 "xfoo"))
5725 (expect '(("test2" ("foo")) ("test3" ("bar")))
5726 (anything-test-candidates '(((name . "test1")
5727 (candidates "foo" "bar"))
5728 ((name . "test2")
5729 (pattern-transformer (lambda (pat) (substring pat 1)))
5730 (candidates "foo" "bar"))
5731 ((name . "test3")
5732 (pattern-transformer (lambda (pat) "bar"))
5733 (candidates "foo" "bar")))
5734 "xfoo"))
5735 (expect '(("test2" ("foo")) ("test3" ("bar")))
5736 (anything-test-candidates '(((name . "test1")
5737 (init
5738 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5739 (insert "foo\nbar\n"))))
5740 (candidates-in-buffer))
5741 ((name . "test2")
5742 (pattern-transformer . (lambda (pat) (substring pat 1)))
5743 (init
5744 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5745 (insert "foo\nbar\n"))))
5746 (candidates-in-buffer))
5747 ((name . "test3")
5748 (pattern-transformer . (lambda (pat) "bar"))
5749 (init
5750 . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
5751 (insert "foo\nbar\n"))))
5752 (candidates-in-buffer)))
5753 "xfoo"))
5754 (desc "anything-recent-push")
5755 (expect '("foo" "bar" "baz")
5756 (let ((lst '("bar" "baz")))
5757 (anything-recent-push "foo" 'lst)))
5758 (expect '("foo" "bar" "baz")
5759 (let ((lst '("foo" "bar" "baz")))
5760 (anything-recent-push "foo" 'lst)))
5761 (expect '("foo" "bar" "baz")
5762 (let ((lst '("bar" "foo" "baz")))
5763 (anything-recent-push "foo" 'lst)))
5764 (desc "anything-require-at-least-version")
5765 (expect nil
5766 (anything-require-at-least-version "1.1"))
5767 (expect nil
5768 (anything-require-at-least-version "1.200"))
5769 (expect nil
5770 (anything-require-at-least-version
5771 (and (string-match "1\.\\([0-9]+\\)" anything-version)
5772 (match-string 0 anything-version))))
5773 (expect (error)
5774 (anything-require-at-least-version "1.999"))
5775 (expect (error)
5776 (anything-require-at-least-version "1.2000"))
5777 (desc "anything-once")
5778 (expect 2
5779 (let ((i 0))
5780 (anything-test-candidates
5781 '(((name . "1")
5782 (init . (lambda () (incf i))))
5783 ((name . "2")
5784 (init . (lambda () (incf i))))))
5786 (expect 1
5787 (let ((i 0))
5788 (anything-test-candidates
5789 '(((name . "1")
5790 (init . (lambda () (anything-once (lambda () (incf i))))))
5791 ((name . "2")
5792 (init . (lambda () (anything-once (lambda () (incf i))))))))
5794 (expect 1
5795 (let ((i 0))
5796 (flet ((init1 () (anything-once (lambda () (incf i)))))
5797 (anything-test-candidates
5798 '(((name . "1")
5799 (init . init1))
5800 ((name . "2")
5801 (init . init1)))))
5803 (desc "anything-marked-candidates")
5804 (expect '("mark3" "mark1")
5805 (let* ((source '((name . "mark test")))
5806 (anything-marked-candidates
5807 `((,source . "mark1")
5808 (((name . "other")) . "mark2")
5809 (,source . "mark3"))))
5810 (stub anything-buffer-get => (current-buffer))
5811 (stub anything-get-current-source => source)
5812 (anything-marked-candidates)))
5813 (expect '("current")
5814 (let* ((source '((name . "mark test")))
5815 (anything-marked-candidates nil))
5816 (stub anything-get-current-source => source)
5817 (stub anything-get-selection => "current")
5818 (anything-marked-candidates)))
5819 (desc "anything-let")
5820 (expect '(1 10000 nil)
5821 (let ((a 9999)
5822 (b 8)
5824 (anything-buffer (exps-tmpbuf)))
5825 (anything-let ((a 1)
5826 (b (1+ a))
5828 (anything-create-anything-buffer))
5829 (with-current-buffer anything-buffer
5830 (list a b c))))
5831 (expect (non-nil)
5832 (let ((a 9999)
5833 (b 8)
5835 (anything-buffer (exps-tmpbuf)))
5836 (anything-let ((a 1)
5837 (b (1+ a))
5839 (anything-create-anything-buffer))
5840 (with-current-buffer anything-buffer
5841 (and (assq 'a (buffer-local-variables))
5842 (assq 'b (buffer-local-variables))
5843 (assq 'c (buffer-local-variables))))))
5844 (expect 'retval
5845 (let ((a 9999)
5846 (b 8)
5848 (anything-buffer (exps-tmpbuf)))
5849 (anything-let ((a 1)
5850 (b (1+ a))
5852 'retval)))
5853 (desc "anything-let*")
5854 (expect '(1 2 nil)
5855 (let ((a 9999)
5856 (b 8)
5858 (anything-buffer (exps-tmpbuf)))
5859 (anything-let* ((a 1)
5860 (b (1+ a))
5862 (anything-create-anything-buffer))
5863 (with-current-buffer anything-buffer
5864 (list a b c))))
5865 (expect (non-nil)
5866 (let ((a 9999)
5867 (b 8)
5869 (anything-buffer (exps-tmpbuf)))
5870 (anything-let* ((a 1)
5871 (b (1+ a))
5873 (anything-create-anything-buffer))
5874 (with-current-buffer anything-buffer
5875 (and (assq 'a (buffer-local-variables))
5876 (assq 'b (buffer-local-variables))
5877 (assq 'c (buffer-local-variables))))))
5878 (expect 'retval*
5879 (let ((a 9999)
5880 (b 8)
5882 (anything-buffer (exps-tmpbuf)))
5883 (anything-let* ((a 1)
5884 (b (1+ a))
5886 'retval*)))
5887 (desc "anything with keyword")
5888 (expect (mock (anything-internal 'test-source "input" "prompt: " nil "preselect" "*test*" nil))
5889 (anything :sources 'test-source
5890 :input "input"
5891 :prompt "prompt: "
5892 :resume nil
5893 :preselect "preselect"
5894 :buffer "*test*"
5895 :keymap nil))
5896 (expect (mock (anything-internal 'test-source nil nil nil nil "*test*" nil))
5897 (anything :sources 'test-source
5898 :buffer "*test*"
5899 :candidate-number-limit 20))
5900 (expect (mock (anything-internal 'test-source nil nil nil nil "*test*" nil))
5901 (anything 'test-source nil nil nil nil "*test*" nil))
5902 (desc "anything-log-eval-internal")
5903 (expect (mock (anything-log "%S = %S" '(+ 1 2) 3))
5904 (anything-log-eval-internal '((+ 1 2))))
5905 (expect (mock (anything-log "%S = ERROR!" 'unDeFined))
5906 (anything-log-eval-internal '(unDeFined)))
5908 (desc "anything-output-filter--collect-candidates")
5909 (expect '("a" "b" "")
5910 (split-string "a\nb\n" "\n"))
5911 (expect '("a" "b")
5912 (anything-output-filter--collect-candidates
5913 '("a" "b" "") (cons 'incomplete-line "")))
5914 (expect '("a" "b")
5915 (split-string "a\nb" "\n"))
5916 (expect '("a")
5917 (anything-output-filter--collect-candidates
5918 '("a" "b") (cons 'incomplete-line "")))
5919 (expect '(incomplete-line . "b")
5920 (let ((incomplete-line-info (cons 'incomplete-line "")))
5921 (anything-output-filter--collect-candidates
5922 '("a" "b") incomplete-line-info)
5923 incomplete-line-info))
5924 (expect '("" "c" "")
5925 (split-string "\nc\n" "\n"))
5926 (expect '("b" "c")
5927 ;; "a\nb" + "\nc\n"
5928 (let ((incomplete-line-info (cons 'incomplete-line "")))
5929 (anything-output-filter--collect-candidates
5930 '("a" "b") incomplete-line-info)
5931 (anything-output-filter--collect-candidates
5932 '("" "c" "") incomplete-line-info)))
5933 (desc "anything-next-point-in-list")
5934 (expect 10
5935 (anything-next-point-in-list 5 '(10 20) nil))
5936 (expect 20
5937 (anything-next-point-in-list 15 '(10 20) nil))
5938 (expect 25
5939 (anything-next-point-in-list 25 '(10 20) nil))
5940 (expect 5
5941 (anything-next-point-in-list 5 '(10 20) t))
5942 (expect 10
5943 (anything-next-point-in-list 15 '(10 20) t))
5944 (expect 20
5945 (anything-next-point-in-list 25 '(10 20) t))
5946 (expect 5
5947 (anything-next-point-in-list 5 '() nil))
5948 (expect 5
5949 (anything-next-point-in-list 5 '() t))
5950 (expect 10
5951 (anything-next-point-in-list 5 '(10) nil))
5952 (expect 10
5953 (anything-next-point-in-list 15 '(10) t))
5954 (expect 20
5955 (anything-next-point-in-list 10 '(10 20) nil))
5956 (expect 10
5957 (anything-next-point-in-list 20 '(10 20) t))
5958 (expect 20
5959 (anything-next-point-in-list 30 '(10 20 30) t))
5963 (provide 'anything)
5964 ;; How to save (DO NOT REMOVE!!)
5965 ;; (progn (magit-push) (emacswiki-post "anything.el"))
5966 ;;; anything.el ends here