1 ;;;; anything.el --- open anything / QuickSilver-like candidate-selection framework
3 ;; Copyright (C) 2007 Tamas Patrovics
4 ;; 2008, 2009, 2010 rubikitch <rubikitch@ruby-lang.org>
6 ;; Author: Tamas Patrovics
7 ;; Maintainer: rubikitch <rubikitch@ruby-lang.org>
8 ;; Keywords: files, frames, help, matching, outlines, processes, tools, convenience, anything
9 ;; URL: http://www.emacswiki.org/cgi-bin/wiki/download/anything.el
10 ;; Site: http://www.emacswiki.org/cgi-bin/emacs/Anything
11 (defvar anything-version nil
)
12 (setq anything-version
"1.285")
14 ;; This file is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
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.
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.
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
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.
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")
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:-)
83 ;; Below are complete command list:
86 ;; Select anything. In Lisp program, some optional arguments can be used.
88 ;; Resurrect previously invoked `anything'.
89 ;; `anything-at-point'
90 ;; Same as `anything' except when C-u is pressed, the initial input is the symbol at point.
91 ;; `anything-force-update'
92 ;; Recalculate and update candidates.
93 ;; `anything-select-action'
94 ;; Select an action for the currently selected candidate.
95 ;; `anything-previous-line'
96 ;; Move selection to the previous line.
97 ;; `anything-next-line'
98 ;; Move selection to the next line.
99 ;; `anything-previous-page'
100 ;; Move selection back with a pageful.
101 ;; `anything-next-page'
102 ;; Move selection forward with a pageful.
103 ;; `anything-beginning-of-buffer'
104 ;; Move selection at the top.
105 ;; `anything-end-of-buffer'
106 ;; Move selection at the bottom.
107 ;; `anything-previous-source'
108 ;; Move selection to the previous source.
109 ;; `anything-next-source'
110 ;; Move selection to the next source.
111 ;; `anything-exit-minibuffer'
112 ;; Select the current candidate by exiting the minibuffer.
114 ;; Help of `anything'.
115 ;; `anything-debug-output'
116 ;; Show all anything-related variables at this time.
117 ;; `anything-delete-current-selection'
118 ;; Delete the currently selected item.
119 ;; `anything-delete-minibuffer-content'
120 ;; Same as `delete-minibuffer-contents' but this is a command.
121 ;; `anything-toggle-resplit-window'
122 ;; Toggle resplit anything window, vertically or horizontally.
123 ;; `anything-select-2nd-action'
124 ;; Select the 2nd action for the currently selected candidate.
125 ;; `anything-select-3rd-action'
126 ;; Select the 3rd action for the currently selected candidate.
127 ;; `anything-select-4th-action'
128 ;; Select the 4th action for the currently selected candidate.
129 ;; `anything-select-2nd-action-or-end-of-line'
130 ;; Select the 2nd action for the currently selected candidate if the point is at the end of minibuffer.
131 ;; `anything-execute-persistent-action'
132 ;; If a candidate is selected then perform the associated action without quitting anything.
133 ;; `anything-scroll-other-window'
134 ;; Scroll other window (not *Anything* window) upward.
135 ;; `anything-scroll-other-window-down'
136 ;; Scroll other window (not *Anything* window) downward.
137 ;; `anything-display-all-visible-marks'
138 ;; Show all `anything' visible marks strings.
139 ;; `anything-quit-and-find-file'
140 ;; Drop into `find-file' from `anything' like `iswitchb-find-file'.
141 ;; `anything-yank-selection'
142 ;; Set minibuffer contents to current selection.
143 ;; `anything-kill-selection-and-quit'
144 ;; Store current selection to kill ring.
145 ;; `anything-follow-mode'
146 ;; If this mode is on, persistent action is executed everytime the cursor is moved.
147 ;; `anything-migrate-sources'
148 ;; Help to migrate to new `anything' way.
149 ;; `anything-isearch'
150 ;; Start incremental search within results. (UNMAINTAINED)
151 ;; `anything-isearch-printing-char'
152 ;; Add printing char to the pattern.
153 ;; `anything-isearch-again'
154 ;; Search again for the current pattern
155 ;; `anything-isearch-delete'
157 ;; `anything-isearch-default-action'
158 ;; Execute the default action for the selected candidate.
159 ;; `anything-isearch-select-action'
160 ;; Choose an action for the selected candidate.
161 ;; `anything-isearch-cancel'
162 ;; Cancel Anything isearch.
163 ;; `anything-iswitchb-setup'
164 ;; Integrate anything completion into iswitchb (UNMAINTAINED).
165 ;; `anything-iswitchb-cancel-anything'
166 ;; Cancel anything completion and return to standard iswitchb.
167 ;; `anything-describe-anything-attribute'
168 ;; Display the full documentation of ANYTHING-ATTRIBUTE (a symbol).
169 ;; `anything-send-bug-report'
170 ;; Send a bug report of anything.el.
171 ;; `anything-send-bug-report-from-anything'
172 ;; Send a bug report of anything.el in anything session.
174 ;;; Customizable Options:
176 ;; Below are customizable option list:
179 ;; You can extend `anything' by writing plug-ins. As soon as
180 ;; `anything' is invoked, `anything-sources' is compiled into basic
181 ;; attributes, then compiled one is used during invocation.
183 ;; The oldest built-in plug-in is `type' attribute: appends
184 ;; appropriate element of `anything-type-attributes'. Second built-in
185 ;; plug-in is `candidates-in-buffer': selecting a line from candidates
188 ;; To write a plug-in:
189 ;; 1. Define a compiler: anything-compile-source--*
190 ;; 2. Add compier function to `anything-compile-source-functions'.
191 ;; 3. (optional) Write helper functions.
193 ;; Anything plug-ins are found in the EmacsWiki.
195 ;; http://www.emacswiki.org/cgi-bin/emacs/AnythingPlugins
197 ;; Tested on Emacs 22/23.
200 ;; Thanks to Vagn Johansen for ideas.
201 ;; Thanks to Stefan Kamphausen for fixes and XEmacs support.
202 ;; Thanks to Tassilo Horn for fixes.
203 ;; Thanks to Drew Adams for various fixes (frame, isearch, customization, etc.)
204 ;; Thanks to IMAKADO for candidates-in-buffer idea.
205 ;; Thanks to Tomohiro MATSUYAMA for multiline patch.
210 ;; If you have library `linkd.el', load
211 ;; `linkd.el' and turn on `linkd-mode' now. It lets you easily
212 ;; navigate around the sections Linkd mode will
213 ;; highlight this Index. You can get `linkd.el' here:
214 ;; http://www.emacswiki.org/cgi-bin/wiki/download/linkd.el
218 ;;; (@* "INCOMPATIBLE CHANGES")
222 ;; Default setting of `anything-save-configuration-functions' is changed.
223 ;; Anything saves/restores window configuration instead of frame configuration now.
224 ;; The default is changed because flickering is occurred in some environment.
226 ;; If you want to save and restore frame configuration, set this variable to
227 ;; '(set-frame-configuration . current-frame-configuration)
231 ;; Fitting frame is disabled by default, because some flickering occurred
232 ;; in some environment. To enable fitting, set both
233 ;; `anything-inhibit-fit-frame-flag' and `fit-frame-inhibit-fitting' to
238 ;; `anything-attr' returns nil when the source attribute is defined
239 ;; but the value of attribute is nil, eg. (volatile) cell. Use
240 ;; `anything-attr-defined' when testing whether the attribute is
246 ;; `anything' accepts keyword arguments. See docstring.
247 ;; [EVAL IT] (describe-function 'anything)
250 ;; `anything-enable-shortcuts' enables us to select candidate easily.
251 ;; If 'prefix then they can be selected using <prefix-key> <alnum>.
252 ;; The prefix key is `anything-select-with-prefix-shortcut'.
253 ;; If the <prefix-key> is a letter, pressing twice inputs the letter itself.
255 ;; (setq anything-enable-shortcuts 'prefix)
256 ;; (define-key anything-map \"@\" 'anything-select-with-prefix-shortcut)
259 ;; You can edit current selection using `anything-edit-current-selection'.
260 ;; It is useful after persistent-action.
263 ;; For `anything' users, setting `anything-sources' directly and
264 ;; invoke M-x anything is obsolete way for now. Try M-x
265 ;; `anything-migrate-sources'!
268 ;; If you want to create anything sources, yasnippet would help you.
269 ;; http://yasnippet.googlecode.com/
271 ;; Then get the snippet from
272 ;; http://www.emacswiki.org/cgi-bin/wiki/download/anything-source.yasnippet
274 ;; Put it in ~/.emacs.d/plugins/yasnippet/snippets/text-mode/emacs-lisp-mode/
278 ;; `anything-interpret-value' is useful function to interpret value
279 ;; like `candidates' attribute.
281 ;; (anything-interpret-value "literal") ; => "literal"
282 ;; (anything-interpret-value (lambda () "lambda")) ; => "lambda"
283 ;; (let ((source '((name . "lambda with source name"))))
284 ;; (anything-interpret-value
285 ;; (lambda () anything-source-name)
286 ;; source)) ; => "lambda with source name"
287 ;; (flet ((f () "function symbol"))
288 ;; (anything-interpret-value 'f)) ; => "function symbol"
289 ;; (let ((v "variable symbol"))
290 ;; (anything-interpret-value 'v)) ; => "variable symbol"
291 ;; (anything-interpret-value 'unbounded-1) ; error
294 ;; Now symbols are acceptable as candidates. So you do not have to use
295 ;; `symbol-name' function. The source is much simpler. For example,
296 ;; `apropos-internal' returns a list of symbols.
299 ;; '(((name . "Commands")
300 ;; (candidates . (lambda () (apropos-internal anything-pattern 'commandp)))
302 ;; (action . describe-function))))
305 ;; To mark a candidate, press C-SPC as normal Emacs marking. To go to
306 ;; marked candidate, press M-[ or M-].
309 ;; `anything-map' is now Emacs-standard key bindings by default. If
310 ;; you are using `iswitchb', execute `anything-iswitchb-setup'. Then
311 ;; some key bindings are adjusted to `iswitchb'. Note that
312 ;; anything-iswitchb is not maintained.
315 ;; There are many `anything' applications, using `anything' for
316 ;; selecting candidate. In this case, if there is one candidate or no
317 ;; candidate, popping up *anything* buffer is irritating. If one
318 ;; candidate, you want to select it at once. If no candidate, you want
319 ;; to quit `anything'. Set `anything-execute-action-at-once-if-one'
320 ;; and `anything-quit-if-no-candidate' to non-nil to remedy it. Note
321 ;; that setting these variables GLOBALLY is bad idea because of
322 ;; delayed sources. These are meant to be let-binded.
323 ;; See anything-etags.el for example.
325 ;; [EVAL IT] (install-elisp "http://www.emacswiki.org/cgi-bin/wiki/download/anything-etags.el")
328 ;; (let ((anything-execute-action-at-once-if-one t)
329 ;; (anything-quit-if-no-candidate (lambda () (message "No candidate"))))
330 ;; (anything temporary-sources input))
333 ;; `set-frame-configuration' arises flickering. If you hate
335 ;; (setq anything-save-configuration-functions
336 ;; '(set-window-configuration . current-window-configuration))
337 ;; at the cost of restoring frame configuration (only window configuration).
340 ;; `anything-delete-current-selection' deletes the current line.
341 ;; It is useful when deleting a candidate in persistent action.
342 ;; eg. `kill-buffer'.
344 ;; [EVAL IT] (describe-function 'anything-delete-current-selection)
347 ;; `anything-attr' gets the attribute. `anything-attrset' sets the
348 ;; attribute. `anything-attr-defined' tests whether the attribute is
349 ;; defined. They handles source-local variables.
351 ;; [EVAL IT] (describe-function 'anything-attr)
352 ;; [EVAL IT] (describe-function 'anything-attrset)
353 ;; [EVAL IT] (describe-function 'anything-attr-defined)
356 ;; `anything-sources' accepts many attributes to make your life easier.
357 ;; Now `anything-sources' accepts a list of symbols.
359 ;; [EVAL IT] (describe-variable 'anything-sources)
362 ;; `anything' has optional arguments. Now you do not have to let-bind
363 ;; `anything-sources'.
365 ;; [EVAL IT] (describe-function 'anything)
368 ;; `anything-resume' resumes last `anything' session. Now you do not
369 ;; have to retype pattern.
371 ;; [EVAL IT] (describe-function 'anything-resume)
374 ;; `anything-execute-persistent-action' executes action without
375 ;; quitting `anything'. When popping up a buffer in other window by
376 ;; persistent action, you can scroll with `anything-scroll-other-window' and
377 ;; `anything-scroll-other-window-down'. See also `anything-sources' docstring.
379 ;; [EVAL IT] (describe-function 'anything-execute-persistent-action)
380 ;; [EVAL IT] (describe-variable 'anything-sources)
383 ;; `anything-select-2nd-action', `anything-select-3rd-action' and
384 ;; `anything-select-4th-action' select other than default action
385 ;; without pressing Tab.
388 ;; Using `anything-candidate-buffer' and the candidates-in-buffer
389 ;; attribute is much faster than traditional "candidates and match"
390 ;; way. And `anything-current-buffer-is-modified' avoids to
391 ;; recalculate candidates for unmodified buffer. See docstring of
394 ;; [EVAL IT] (describe-function 'anything-candidate-buffer)
395 ;; [EVAL IT] (describe-function 'anything-candidates-in-buffer)
396 ;; [EVAL IT] (describe-function 'anything-current-buffer-is-modified)
399 ;; `anything-current-buffer' and `anything-buffer-file-name' stores
400 ;; `(current-buffer)' and `buffer-file-name' in the buffer `anything'
401 ;; is invoked. Use them freely.
403 ;; [EVAL IT] (describe-variable 'anything-current-buffer)
404 ;; [EVAL IT] (describe-variable 'anything-buffer-file-name)
407 ;; `anything-completing-read' and `anything-read-file-name' are
408 ;; experimental implementation. If you are curious, type M-x
409 ;; anything-read-string-mode. It is a minor mode and toggles on/off.
412 ;; Use `anything-test-candidates' to test your handmade anything
413 ;; sources. It simulates contents of *anything* buffer with pseudo
414 ;; `anything-sources' and `anything-pattern', without side-effect. So
415 ;; you can unit-test your anything sources! Let's TDD!
417 ;; [EVAL IT] (describe-function 'anything-test-candidates)
419 ;; There are many unit-testing framework in Emacs Lisp. See the EmacsWiki.
420 ;; http://www.emacswiki.org/cgi-bin/emacs/UnitTesting
422 ;; There is an unit-test by Emacs Lisp Expectations at the tail of this file.
423 ;; http://www.emacswiki.org/cgi-bin/wiki/download/el-expectations.el
424 ;; http://www.emacswiki.org/cgi-bin/wiki/download/el-mock.el
427 ;; If you want to create anything sources, see anything-config.el.
428 ;; It is huge collection of sources. You can learn from examples.
433 ;; - process status indication
435 ;; - async sources doesn't honor digit-shortcut-count
437 ;; - anything-candidate-number-limit can't be nil everywhere
441 ;; Change log of this file is found at
442 ;; http://repo.or.cz/w/anything-config.git/history/master:/anything.el
444 ;; Change log of this project is found at
445 ;; http://repo.or.cz/w/anything-config.git?a=shortlog
448 ;; (require 'anything-match-plugin nil t)
450 ;; (@* "User Configuration")
452 ;; This is only an example. Customize it to your own taste!
453 (defvar anything-sources
`(((name .
"Buffers")
456 (remove-if (lambda (name)
457 (or (equal name anything-buffer
)
458 (eq ?\
(aref name
0))))
459 (mapcar 'buffer-name
(buffer-list)))))
462 ((name .
"File Name History")
463 (candidates . file-name-history
)
464 (match (lambda (candidate)
465 ;; list basename matches first
468 (file-name-nondirectory candidate
)))
471 ;; and then directory part matches
472 (let ((dir (file-name-directory candidate
)))
474 (string-match anything-pattern dir
)))))
477 ((name .
"Files from Current Directory")
479 (setq anything-default-directory
481 (candidates .
(lambda ()
483 anything-default-directory
)))
486 ((name .
"Complex Command History")
487 (candidates .
(lambda ()
488 (mapcar 'prin1-to-string
490 (action .
(("Repeat Complex Command" .
494 "The source of candidates for anything.
496 (setq anything-sources (list anything-c-foo anything-c-bar))
498 (setq anything-sources '(anything-c-foo anything-c-bar))
499 The latter is recommended because if you change anything-c-* variable,
500 you do not have to update `anything-sources'.
502 You are STRONGLY recommended to define a command which calls
503 `anything' or `anything-other-buffer' with argument rather than
504 to set `anything-sources' externally.
506 If you want to change `anything-sources' during `anything' invocation,
507 use `anything-set-sources', never use `setq'.
514 ;; This value is only provided as an example. Customize it to your own
516 (defvar anything-type-attributes
517 '((file (action .
(("Find File" . find-file
)
518 ("Delete File" .
(lambda (file)
519 (if (y-or-n-p (format "Really delete file %s? "
521 (delete-file file
)))))))
522 (buffer (action .
(("Switch to Buffer" . switch-to-buffer
)
523 ("Pop to Buffer" . pop-to-buffer
)
524 ("Display Buffer" . display-buffer
)
525 ("Kill Buffer" . kill-buffer
)))))
526 "It's a list of (TYPE ATTRIBUTES ...). ATTRIBUTES are the same
527 as attributes for `anything-sources'. TYPE connects the value
528 to the appropriate sources in `anything-sources'.
530 This allows specifying common attributes for several
531 sources. For example, sources which provide files can specify
532 common attributes with a `file' type.")
535 (defvaralias 'anything-enable-digit-shortcuts
'anything-enable-shortcuts
536 "Alphabet shortcuts are usable now. Then `anything-enable-digit-shortcuts' should be renamed.
537 `anything-enable-digit-shortcuts' is retained for compatibility.")
538 (defvar anything-enable-shortcuts nil
539 "*Whether to use digit/alphabet shortcut to select the first nine matches.
540 If t then they can be selected using Ctrl+<number>.
542 If 'prefix then they can be selected using <prefix-key> <alnum>.
543 The prefix key is `anything-select-with-prefix-shortcut'.
544 If the <prefix-key> is a letter, pressing twice inputs the letter itself.
546 (setq anything-enable-shortcuts 'prefix)
547 (define-key anything-map \"@\" 'anything-select-with-prefix-shortcut)
549 If 'alphabet then they can be selected using Shift+<alphabet> (deprecated).
550 It is not recommended because you cannot input capital letters in pattern.
552 Keys (digit/alphabet) are listed in `anything-shortcut-keys-alist'.")
554 (defvar anything-shortcut-keys-alist
555 '((alphabet .
"asdfghjklzxcvbnmqwertyuiop")
556 (prefix .
"asdfghjklzxcvbnmqwertyuiop1234567890")
559 (defvar anything-display-source-at-screen-top t
560 "*If t, `anything-next-source' and `anything-previous-source'
561 display candidates at the top of screen.")
563 (defvar anything-candidate-number-limit
50
564 "*Do not show more candidates than this limit from individual
565 sources. It is usually pointless to show hundreds of matches
566 when the pattern is empty, because it is much simpler to type a
567 few characters to narrow down the list of potential candidates.
569 Set it to nil if you don't want this limit.")
572 (defvar anything-idle-delay
0.5
573 "*The user has to be idle for this many seconds, before
574 candidates from delayed sources are collected. This is useful
575 for sources involving heavy operations (like launching external
576 programs), so that candidates from the source are not retrieved
577 unnecessarily if the user keeps typing.
579 It also can be used to declutter the results anything displays,
580 so that results from certain sources are not shown with every
581 character typed, only if the user hesitates a bit.")
584 (defvar anything-input-idle-delay
0.1
585 "The user has to be idle for this many seconds, before ALL candidates are collected.
586 Unlink `anything-input-idle', it is also effective for non-delayed sources.
587 If nil, candidates are collected immediately. ")
590 (defvar anything-samewindow nil
591 "If t then Anything doesn't pop up a new window, it uses the
592 current window to show the candidates.")
595 (defvar anything-source-filter nil
596 "A list of source names to be displayed. Other sources won't
597 appear in the search results. If nil then there is no filtering.
598 See also `anything-set-source-filter'.")
602 (let ((map (copy-keymap minibuffer-local-map
)))
603 (define-key map
(kbd "<down>") 'anything-next-line
)
604 (define-key map
(kbd "<up>") 'anything-previous-line
)
605 (define-key map
(kbd "C-n") 'anything-next-line
)
606 (define-key map
(kbd "C-p") 'anything-previous-line
)
607 (define-key map
(kbd "<prior>") 'anything-previous-page
)
608 (define-key map
(kbd "<next>") 'anything-next-page
)
609 (define-key map
(kbd "M-v") 'anything-previous-page
)
610 (define-key map
(kbd "C-v") 'anything-next-page
)
611 (define-key map
(kbd "M-<") 'anything-beginning-of-buffer
)
612 (define-key map
(kbd "M->") 'anything-end-of-buffer
)
613 (define-key map
(kbd "<right>") 'anything-next-source
)
614 (define-key map
(kbd "<left>") 'anything-previous-source
)
615 (define-key map
(kbd "<RET>") 'anything-exit-minibuffer
)
616 (define-key map
(kbd "C-1") 'anything-select-with-digit-shortcut
)
617 (define-key map
(kbd "C-2") 'anything-select-with-digit-shortcut
)
618 (define-key map
(kbd "C-3") 'anything-select-with-digit-shortcut
)
619 (define-key map
(kbd "C-4") 'anything-select-with-digit-shortcut
)
620 (define-key map
(kbd "C-5") 'anything-select-with-digit-shortcut
)
621 (define-key map
(kbd "C-6") 'anything-select-with-digit-shortcut
)
622 (define-key map
(kbd "C-7") 'anything-select-with-digit-shortcut
)
623 (define-key map
(kbd "C-8") 'anything-select-with-digit-shortcut
)
624 (define-key map
(kbd "C-9") 'anything-select-with-digit-shortcut
)
625 (loop for c from ?A to ?Z do
626 (define-key map
(make-string 1 c
) 'anything-select-with-digit-shortcut
))
627 (define-key map
(kbd "C-i") 'anything-select-action
)
628 (define-key map
(kbd "C-z") 'anything-execute-persistent-action
)
629 (define-key map
(kbd "C-e") 'anything-select-2nd-action-or-end-of-line
)
630 (define-key map
(kbd "C-j") 'anything-select-3rd-action
)
631 (define-key map
(kbd "C-o") 'anything-next-source
)
632 (define-key map
(kbd "C-M-v") 'anything-scroll-other-window
)
633 (define-key map
(kbd "M-<next>") 'anything-scroll-other-window
)
634 (define-key map
(kbd "C-M-y") 'anything-scroll-other-window-down
)
635 (define-key map
(kbd "C-M-S-v") 'anything-scroll-other-window-down
)
636 (define-key map
(kbd "M-<prior>") 'anything-scroll-other-window-down
)
637 (define-key map
(kbd "C-SPC") 'anything-toggle-visible-mark
)
638 (define-key map
(kbd "M-[") 'anything-prev-visible-mark
)
639 (define-key map
(kbd "M-]") 'anything-next-visible-mark
)
640 (define-key map
(kbd "C-k") 'anything-delete-minibuffer-content
)
642 (define-key map
(kbd "C-s") 'anything-isearch
)
643 (define-key map
(kbd "C-r") 'undefined
)
644 (define-key map
(kbd "C-t") 'anything-toggle-resplit-window
)
645 (define-key map
(kbd "C-x C-f") 'anything-quit-and-find-file
)
647 (define-key map
(kbd "C-c C-d") 'anything-delete-current-selection
)
648 (define-key map
(kbd "C-c C-y") 'anything-yank-selection
)
649 (define-key map
(kbd "C-c C-k") 'anything-kill-selection-and-quit
)
650 (define-key map
(kbd "C-c C-f") 'anything-follow-mode
)
651 (define-key map
(kbd "C-c C-u") 'anything-force-update
)
654 (define-key map
"\C-c\C-x\C-d" 'anything-debug-output
)
655 (define-key map
"\C-c\C-x\C-m" 'anything-display-all-visible-marks
)
656 (define-key map
"\C-c\C-x\C-b" 'anything-send-bug-report-from-anything
)
657 ;; Use `describe-mode' key in `global-map'
658 (dolist (k (where-is-internal 'describe-mode global-map
))
659 (define-key map k
'anything-help
))
660 ;; the defalias is needed because commands are bound by name when
661 ;; using iswitchb, so only commands having the prefix anything-
663 (defalias 'anything-previous-history-element
'previous-history-element
)
664 (defalias 'anything-next-history-element
'next-history-element
)
665 (define-key map
(kbd "M-p") 'anything-previous-history-element
)
666 (define-key map
(kbd "M-n") 'anything-next-history-element
)
668 "Keymap for anything.
670 If you execute `anything-iswitchb-setup', some keys are modified.
671 See `anything-iswitchb-setup-keys'.")
673 (defvar anything-isearch-map
674 (let ((map (make-sparse-keymap)))
675 (set-keymap-parent map
(current-global-map))
676 (define-key map
(kbd "<return>") 'anything-isearch-default-action
)
677 (define-key map
(kbd "<RET>") 'anything-isearch-default-action
)
678 (define-key map
(kbd "C-i") 'anything-isearch-select-action
)
679 (define-key map
(kbd "C-g") 'anything-isearch-cancel
)
680 (define-key map
(kbd "M-s") 'anything-isearch-again
)
681 (define-key map
(kbd "<backspace>") 'anything-isearch-delete
)
682 ;; add printing chars
683 (loop for i from
32 below
256 do
684 (define-key map
(vector i
) 'anything-isearch-printing-char
))
686 "Keymap for anything incremental search.")
689 (defgroup anything nil
690 "Open anything." :prefix
"anything-" :group
'convenience
)
692 (defface anything-header
693 '((t (:inherit header-line
)))
694 "Face for header lines in the anything buffer." :group
'anything
)
696 (defvar anything-header-face
'anything-header
697 "Face for header lines in the anything buffer.")
699 (defface anything-isearch-match
'((t (:background
"Yellow")))
700 "Face for isearch in the anything buffer." :group
'anything
)
702 (defvar anything-isearch-match-face
'anything-isearch-match
703 "Face for matches during incremental search.")
705 (defvar anything-selection-face
'highlight
706 "Face for currently selected item.")
708 (defvar anything-iswitchb-idle-delay
1
709 "Show anything completions if the user is idle that many
710 seconds after typing.")
712 (defvar anything-iswitchb-dont-touch-iswithcb-keys nil
713 "If t then those commands are not bound from `anything-map'
714 under iswitchb which would override standard iswithcb keys.
716 This allows an even more seamless integration with iswitchb for
717 those who prefer using iswitchb bindings even if the anything
718 completions buffer is popped up.
720 Note that you can bind alternative keys for the same command in
721 `anything-map', so that you can use different keys for anything
722 under iswitchb. For example, I bind the character \ to
723 `anything-exit-minibuffer' which key is just above Enter on my
724 keyboard. This way I can switch buffers with Enter and choose
725 anything completions with \.")
727 ;;----------------------------------------------------------------------
729 (defvar anything-buffer
"*anything*"
730 "Buffer showing completions.")
732 (defvar anything-action-buffer
"*anything action*"
733 "Buffer showing actions.")
735 (defvar anything-selection-overlay nil
736 "Overlay used to highlight the currently selected item.")
738 (defvar anything-isearch-overlay nil
739 "Overlay used to highlight the current match during isearch.")
741 (defvar anything-digit-overlays nil
742 "Overlays for digit shortcuts. See `anything-enable-shortcuts'.")
744 (defvar anything-candidate-cache nil
745 "Holds the available candidate withing a single anything invocation.")
747 (defvar anything-pattern
748 "The input pattern used to update the anything buffer.")
750 (defvar anything-input
751 "The input typed in the candidates panel.")
753 (defvar anything-async-processes nil
754 "List of information about asynchronous processes managed by anything.")
756 (defvar anything-digit-shortcut-count
0
757 "Number of digit shortcuts shown in the anything buffer.")
759 (defvar anything-before-initialize-hook nil
760 "Run before anything initialization.
761 This hook is run before init functions in `anything-sources'.")
763 (defvar anything-after-initialize-hook nil
764 "Run after anything initialization.
765 Global variables are initialized and the anything buffer is created.
766 But the anything buffer has no contents. ")
768 (defvar anything-update-hook nil
769 "Run after the anything buffer was updated according the new input pattern.
770 This hook is run at the beginning of buffer.
771 The first candidate is selected after running this hook.
772 See also `anything-after-update-hook'.")
774 (defvar anything-after-update-hook nil
775 "Run after the anything buffer was updated according the new input pattern.
776 This is very similar to `anything-update-hook' but selection is not moved.
777 It is useful to select a particular object instead of the first one. ")
779 (defvar anything-cleanup-hook nil
780 "Run after anything minibuffer is closed, IOW this hook is executed BEFORE performing action. ")
782 (defvar anything-after-action-hook nil
783 "Run after executing action.")
785 (defvar anything-after-persistent-action-hook nil
786 "Run after executing persistent action.")
788 (defvar anything-restored-variables
789 '( anything-candidate-number-limit
790 anything-source-filter
791 anything-source-in-each-line-flag
794 deferred-action-list
)
795 "Variables which are restored after `anything' invocation.")
796 ;; `anything-saved-sources' is removed
798 (defvar anything-saved-selection nil
799 "Saved value of the currently selected object when the action
802 ;; `anything-original-source-filter' is removed
804 (defvar anything-candidate-separator
805 "--------------------"
806 "Candidates separator of `multiline' source.")
808 (defvar anything-current-buffer nil
809 "Current buffer when `anything' is invoked.")
811 (defvar anything-buffer-file-name nil
812 "`buffer-file-name' when `anything' is invoked.")
814 (defvar anything-saved-action nil
815 "Saved value of the currently selected action by key.")
817 (defvar anything-last-sources nil
818 "OBSOLETE!! Sources of previously invoked `anything'.")
820 (defvar anything-saved-current-source nil
821 "Saved value of the original (anything-get-current-source) when the action
824 (defvar anything-compiled-sources nil
825 "Compiled version of `anything-sources'. ")
827 (defvar anything-in-persistent-action nil
828 "Flag whether in persistent-action or not.")
830 (defvar anything-quick-update nil
831 "If non-nil, suppress displaying sources which are out of screen at first.
832 They are treated as delayed sources at this input.
833 This flag makes `anything' a bit faster with many sources.")
835 (defvar anything-last-sources-local nil
836 "Buffer local value of `anything-sources'.")
837 (defvar anything-last-buffer nil
838 "`anything-buffer' of previously `anything' session.")
840 (defvar anything-save-configuration-functions
841 '(set-window-configuration . current-window-configuration
)
842 "If you want to save and restore frame configuration, set this variable to
843 '(set-frame-configuration . current-frame-configuration)
845 Older version saves/restores frame configuration, but the default is changed now,
846 because flickering is occurred in some environment.
849 (defvar anything-persistent-action-use-special-display nil
850 "If non-nil, use `special-display-function' in persistent action.")
852 (defvar anything-execute-action-at-once-if-one nil
853 "If non-nil and there is one candidate, execute the first action without selection.
854 It is useful for `anything' applications.")
856 (defvar anything-quit-if-no-candidate nil
857 "if non-nil and there is no candidate, do not display *anything* buffer and quit.
858 This variable accepts a function, which is executed if no candidate.
860 It is useful for `anything' applications.")
862 (defvar anything-scroll-amount nil
863 "Scroll amount used by `anything-scroll-other-window' and `anything-scroll-other-window-down'.
864 If you prefer scrolling line by line, set this value to 1.")
866 (defvar anything-display-function
'anything-default-display-buffer
867 "Function to display *anything* buffer.
868 It is `anything-default-display-buffer' by default, which affects `anything-samewindow'.")
870 (defvar anything-delayed-init-executed nil
)
872 (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"
873 "Help string displayed in mode-line in `anything'.
874 If nil, use default `mode-line-format'.")
876 (defvar anything-help-message
877 "\\<anything-map>The keys that are defined for `anything' are:
879 "Detailed help message string for `anything'.
880 It also accepts function or variable symbol.")
882 (put 'anything
'timid-completion
'disabled
)
884 (defvar anything-inhibit-fit-frame-flag t
885 "If non-nil, inhibit fitting anything frame to its buffer.
886 It is nil by default because some flickering occurred in some environment.
888 To enable fitting, set both `anything-inhibit-fit-frame-flag' and
889 `fit-frame-inhibit-fitting' to nil.")
891 (defvar anything-source-in-each-line-flag nil
892 "If non-nil, add anything-source text-property in each candidate.
893 experimental feature.")
895 (defvaralias 'anything-debug-variables
'anything-debug-forms
)
896 (defvar anything-debug-forms nil
897 "Forms to show in `anything-debug-output'.
898 Otherwise all variables started with `anything-' are shown.
899 It is useful for debug.")
901 (defvar anything-debug nil
902 "If non-nil, write log message into *Anything Log* buffer.
903 If `debug-on-error' is non-nil, write log message regardless of this variable.
904 It is disabled by default because *Anything Log* grows quickly.")
906 ;; (@* "Internal Variables")
907 (defvar anything-test-candidate-list nil
)
908 (defvar anything-test-mode nil
)
909 (defvar anything-source-name nil
)
910 (defvar anything-candidate-buffer-alist nil
)
911 (defvar anything-check-minibuffer-input-timer nil
)
912 (defvar anything-match-hash
(make-hash-table :test
'equal
))
913 (defvar anything-cib-hash
(make-hash-table :test
'equal
))
914 (defvar anything-tick-hash
(make-hash-table :test
'equal
))
915 (defvar anything-issued-errors nil
)
916 (defvar anything-shortcut-keys nil
)
917 (defvar anything-once-called-functions nil
)
918 (defvar anything-follow-mode nil
)
919 (defvar anything-let-variables nil
)
921 ;; (@* "Utility: logging")
922 (defun anything-log (format-string &rest args
)
923 "Log message if `debug-on-error' or `anything-debug' is non-nil.
924 Messages are written to the *Anythingn Log* buffer.
925 Arguments are same as `format'."
926 (when (or debug-on-error anything-debug
)
927 (with-current-buffer (get-buffer-create "*Anything Log*")
928 (buffer-disable-undo)
929 (set (make-local-variable 'inhibit-read-only
) t
)
930 (goto-char (point-max))
931 (insert (let ((tm (current-time)))
932 (format "%s.%06d (%s) %s\n"
933 (format-time-string "%H:%M:%S" tm
)
935 (anything-log-get-current-function)
936 (apply #'format
(cons format-string args
))))))))
937 (defmacro anything-log-eval
(&rest exprs
)
938 "Write each EXPR evaluation result to the *Anything Log* buffer."
939 `(anything-log-eval-internal ',exprs
))
940 (defun anything-log-run-hook (hook)
941 (anything-log "executing %s" hook
)
943 (anything-log-eval (symbol-value hook
))
944 (anything-log-eval (default-value hook
)))
946 (anything-log "executed %s" hook
))
947 (defun anything-log-eval-internal (exprs)
948 (mapc (lambda (expr) (anything-log "%S = %S" expr
(eval expr
))) exprs
))
949 (defun anything-log-get-current-function ()
950 "Get function name calling `anything-log'.
951 The original idea is from `tramp-debug-message'."
952 (loop for btn from
1 to
40 ;avoid inf-loop
953 for btf
= (second (backtrace-frame btn
))
954 for fn
= (if (symbolp btf
) (symbol-name btf
) "")
955 do
(when (and (string-match "^anything" fn
)
956 (not (string-match "^anything-\\(?:interpret\\|log\\|.*funcall\\)" fn
)))
959 ;; (anything-log "test")
960 ;; (switch-to-buffer-other-window "*Anything Log*")
962 ;; (@* "Programming Tools")
963 (defmacro anything-aif
(test-form then-form
&rest else-forms
)
964 "Anaphoric if. Temporary variable `it' is the result of test-form."
965 `(let ((it ,test-form
))
966 (if it
,then-form
,@else-forms
)))
967 (put 'anything-aif
'lisp-indent-function
2)
969 (defun anything-mklist (obj)
970 "If OBJ is a list (but not lambda), return itself, otherwise make a list with one element."
971 (if (and (listp obj
) (not (functionp obj
)))
975 ;; (@* "Anything API")
976 (defmacro anything-let
(varlist &rest body
)
977 "[OBSOLETE] Like `let'. Bind anything buffer local variables according to VARLIST then eval BODY."
978 `(anything-let-internal (anything-let-eval-varlist ',varlist
)
980 (put 'anything-let
'lisp-indent-function
1)
982 (defmacro anything-let
* (varlist &rest body
)
983 "[OBSOLETE] Like `let*'. Bind anything buffer local variables according to VARLIST then eval BODY."
984 `(anything-let-internal (anything-let*-eval-varlist
',varlist
)
986 (put 'anything-let
* 'lisp-indent-function
1)
988 (defun anything-buffer-get ()
989 "If *anything action* buffer is shown, return `anything-action-buffer', otherwise `anything-buffer'."
990 (if (anything-action-window)
991 anything-action-buffer
994 (defun anything-window ()
995 "Window of `anything-buffer'."
996 (get-buffer-window (anything-buffer-get) 'visible
))
998 (defun anything-action-window ()
999 "Window of `anything-action-buffer'."
1000 (get-buffer-window anything-action-buffer
'visible
))
1002 (defmacro with-anything-window
(&rest body
)
1003 `(let ((--tmpfunc-- (lambda () ,@body
)))
1004 (if anything-test-mode
1005 (with-current-buffer (anything-buffer-get)
1006 (funcall --tmpfunc--
))
1007 (with-selected-window (anything-window)
1008 (funcall --tmpfunc--
)))))
1009 (put 'with-anything-window
'lisp-indent-function
0)
1011 (defun anything-deferred-action-function ()
1012 (dolist (f deferred-action-list
) (funcall f
)))
1013 (defmacro with-anything-restore-variables
(&rest body
)
1014 "Restore variables specified by `anything-restored-variables' after executing BODY . "
1015 `(let ((--orig-vars (mapcar (lambda (v) (cons v
(symbol-value v
))) anything-restored-variables
))
1016 (deferred-action-function 'anything-deferred-action-function
))
1017 (anything-log "save variables: %S" --orig-vars
)
1018 (unwind-protect (progn ,@body
)
1019 (loop for
(var . value
) in --orig-vars
1021 (anything-log "restore variables"))))
1022 (put 'with-anything-restore-variables
'lisp-indent-function
0)
1024 (defun* anything-attr
(attribute-name &optional
(src (anything-get-current-source)))
1025 "Get the value of ATTRIBUTE-NAME of SRC (source).
1026 if SRC is omitted, use current source.
1027 It is useful to write your sources."
1028 (anything-aif (assq attribute-name src
)
1031 (defun* anything-attr
* (attribute-name &optional
(src (anything-get-current-source)))
1032 "Get the value of ATTRIBUTE-NAME of SRC (source) and pass to `anything-interpret-value'.
1033 if SRC is omitted, use current source.
1034 It is useful to write your sources."
1035 (anything-interpret-value (anything-attr attribute-name src
)))
1037 (defun* anything-attr-defined
(attribute-name &optional
(src (anything-get-current-source)))
1038 "Return non-nil if ATTRIBUTE-NAME of SRC (source) is defined.
1039 if SRC is omitted, use current source.
1040 It is useful to write your sources."
1041 (and (assq attribute-name src
) t
))
1043 (defun* anything-attrset
(attribute-name value
&optional
(src (anything-get-current-source)))
1044 "Set the value of ATTRIBUTE-NAME of SRC (source) to VALUE.
1045 if SRC is omitted, use current source.
1046 It is useful to write your sources."
1047 (anything-aif (assq attribute-name src
)
1049 (setcdr src
(cons (cons attribute-name value
) (cdr src
))))
1052 ;; anything-set-source-filter
1054 ;; This function sets a filter for anything sources and it may be
1055 ;; called while anything is running. It can be used to toggle
1056 ;; displaying of sources dinamically. For example, additional keys
1057 ;; can be bound into `anything-map' to display only the file-related
1058 ;; results if there are too many matches from other sources and
1059 ;; you're after files only:
1061 ;; Shift+F shows only file results from some sources:
1063 ;; (define-key anything-map "F" 'anything-my-show-files-only)
1065 ;; (defun anything-my-show-files-only ()
1067 ;; (anything-set-source-filter '("File Name History"
1068 ;; "Files from Current Directory")))
1070 ;; Shift+A shows all results:
1072 ;; (define-key anything-map "A" 'anything-my-show-all)
1074 ;; (defun anything-my-show-all ()
1076 ;; (anything-set-source-filter nil))
1079 ;; Note that you have to prefix the functions with anything- prefix,
1080 ;; otherwise they won't be bound when Anything is used under
1081 ;; Iswitchb. The -my- part is added to avoid collisions with
1082 ;; existing Anything function names.
1084 (defun anything-set-source-filter (sources)
1085 "Sets the value of `anything-source-filter' and updates the list of results."
1086 (unless (and (listp sources
)
1087 (loop for name in sources always
(stringp name
)))
1088 (error "invalid data in `anything-set-source-filter': %S" sources
))
1089 (setq anything-source-filter sources
)
1090 (anything-log-eval anything-source-filter
)
1093 (defun anything-set-sources (sources &optional no-init no-update
)
1094 "Set `anything-sources' during `anything' invocation.
1095 If NO-INIT is non-nil, skip executing init functions of SOURCES.
1096 If NO-UPDATE is non-nil, skip executing `anything-update'."
1097 (with-current-buffer anything-buffer
1098 (setq anything-compiled-sources nil
1099 anything-sources sources
1100 anything-last-sources-local sources
)
1101 (anything-log-eval anything-compiled-sources anything-sources
))
1102 (unless no-init
(anything-funcall-foreach 'init
))
1103 (unless no-update
(anything-update)))
1105 (defvar anything-compile-source-functions
1106 '(anything-compile-source--type
1107 anything-compile-source--dummy
1108 anything-compile-source--disable-shortcuts
1109 anything-compile-source--candidates-in-buffer
)
1110 "Functions to compile elements of `anything-sources' (plug-in).")
1112 (defun anything-get-sources ()
1113 "Return compiled `anything-sources', which is memoized.
1118 `anything-type-attributes' are merged in.
1120 candidates, volatile and match attrubute are created.
1124 ((anything-action-window)
1127 (anything-compiled-sources)
1131 (setq anything-compiled-sources
1132 (anything-compile-sources anything-sources anything-compile-source-functions
))
1133 (anything-log-eval anything-compiled-sources
)))))
1135 (defun* anything-get-selection
(&optional
(buffer nil buffer-s
) (force-display-part))
1136 "Return the currently selected item or nil.
1137 if BUFFER is nil or unspecified, use anything-buffer as default value.
1138 If FORCE-DISPLAY-PART is non-nil, return the display string."
1139 (setq buffer
(if (and buffer buffer-s
) buffer anything-buffer
))
1140 (unless (zerop (buffer-size (get-buffer buffer
)))
1141 (with-current-buffer buffer
1143 (or (and (not force-display-part
)
1144 (get-text-property (overlay-start
1145 anything-selection-overlay
)
1146 'anything-realvalue
))
1147 (let ((disp (buffer-substring-no-properties
1148 (overlay-start anything-selection-overlay
)
1149 (1- (overlay-end anything-selection-overlay
))))
1150 (source (anything-get-current-source)))
1151 (anything-aif (and (not force-display-part
)
1152 (assoc-default 'display-to-real source
))
1153 (anything-funcall-with-source source it disp
)
1155 (unless (equal selection
"")
1156 (anything-log-eval selection
)
1159 (defun anything-get-action ()
1160 "Return the associated action for the selected candidate."
1161 (unless (zerop (buffer-size (get-buffer (anything-buffer-get))))
1162 (let* ((source (anything-get-current-source))
1163 (actions (assoc-default 'action source
)))
1165 (anything-aif (assoc-default 'action-transformer source
)
1166 ;; (funcall it actions (anything-get-selection))
1167 (anything-composed-funcall-with-source source it actions
(anything-get-selection))
1170 (defun anything-get-current-source ()
1171 "Return the source for the current selection / in init/candidates/action/candidate-transformer/filtered-candidate-transformer function."
1172 (declare (special source
))
1173 ;; The name `anything-get-current-source' should be used in init function etc.
1174 (if (and (boundp 'anything-source-name
) (stringp anything-source-name
))
1177 (with-current-buffer (anything-buffer-get)
1178 (get-text-property (point) 'anything-source
))
1180 (with-current-buffer (anything-buffer-get)
1181 ;; This goto-char shouldn't be necessary, but point is moved to
1182 ;; point-min somewhere else which shouldn't happen.
1183 (goto-char (overlay-start anything-selection-overlay
))
1184 (let* ((header-pos (or (anything-get-previous-header-pos)
1185 (anything-get-next-header-pos)))
1189 (message "No candidates")
1190 (return-from exit nil
))
1191 (goto-char header-pos
)
1192 (buffer-substring-no-properties
1193 (line-beginning-position) (line-end-position)))))
1194 (some (lambda (source)
1195 (if (equal (assoc-default 'name source
)
1198 (anything-get-sources))))))))
1200 (defun anything-buffer-is-modified (buffer)
1201 "Return non-nil when BUFFER is modified since `anything' was invoked."
1202 (let* ((b (get-buffer buffer
))
1203 (key (concat (buffer-name b
)
1205 (anything-attr 'name
)))
1206 (source-tick (or (gethash key anything-tick-hash
) 0))
1207 (buffer-tick (buffer-chars-modified-tick b
))
1208 (modifiedp (/= source-tick buffer-tick
)))
1209 (puthash key buffer-tick anything-tick-hash
)
1210 (anything-log-eval buffer modifiedp
)
1212 (defun anything-current-buffer-is-modified ()
1213 "Return non-nil when `anything-current-buffer' is modified since `anything' was invoked."
1214 (anything-buffer-is-modified anything-current-buffer
))
1216 (defvar anything-quit nil
)
1217 (defun anything-run-after-quit (function &rest args
)
1218 "Perform an action after quitting `anything'.
1219 The action is to call FUNCTION with arguments ARGS."
1220 (setq anything-quit t
)
1221 (anything-log-eval function args
)
1222 (apply 'run-with-idle-timer
0 nil function args
)
1223 (anything-exit-minibuffer))
1225 (defun define-anything-type-attribute (type definition
&optional doc
)
1226 "Register type attribute of TYPE as DEFINITION with DOC.
1227 DOC is displayed in `anything-type-attributes' docstring.
1229 Use this function is better than setting `anything-type-attributes' directly."
1230 (anything-add-type-attribute type definition
)
1231 (and doc
(anything-document-type-attribute type doc
))
1234 (defvaralias 'anything-attributes
'anything-additional-attributes
)
1235 (defvar anything-additional-attributes nil
1236 "List of all `anything' attributes.")
1237 (defun anything-document-attribute (attribute short-doc
&optional long-doc
)
1238 "Register ATTRIBUTE documentation introduced by plug-in.
1239 SHORT-DOC is displayed beside attribute name.
1240 LONG-DOC is displayed below attribute name and short documentation."
1242 (setq short-doc
(concat "(" short-doc
")"))
1243 (setq long-doc short-doc
1245 (add-to-list 'anything-additional-attributes attribute t
)
1246 (put attribute
'anything-attrdoc
1247 (concat "- " (symbol-name attribute
) " " short-doc
"\n\n" long-doc
"\n")))
1248 (put 'anything-document-attribute
'lisp-indent-function
2)
1250 (defun anything-require-at-least-version (version)
1251 "Output error message unless anything.el is older than VERSION.
1252 This is suitable for anything applications."
1253 (when (and (string= "1." (substring version
0 2))
1254 (string-match "1\.\\([0-9]+\\)" anything-version
)
1255 (< (string-to-number (match-string 1 anything-version
))
1256 (string-to-number (substring version
2))))
1257 (error "Please update anything.el!!
1259 M-x auto-install-batch anything
1261 You must have auto-install.el too.
1262 http://www.emacswiki.org/cgi-bin/wiki/download/auto-install.el
1265 (defun anything-interpret-value (value &optional source
)
1266 "interpret VALUE as variable, function or literal.
1267 If VALUE is a function, call it with no arguments and return the value.
1268 If SOURCE is `anything' source, `anything-source-name' is source name.
1270 If VALUE is a variable, return the value.
1272 If VALUE is a symbol, but it is not a function or a variable, cause an error.
1274 Otherwise, return VALUE itself."
1275 (cond ((and source
(functionp value
))
1276 (anything-funcall-with-source source value
))
1279 ((and (symbolp value
) (boundp value
))
1280 (symbol-value value
))
1282 (error "anything-interpret-value: Symbol must be a function or a variable"))
1286 (defun anything-once (function &rest args
)
1287 "Ensure FUNCTION with ARGS to be called once in `anything' session."
1288 (let ((spec (cons function args
)))
1289 (unless (member spec anything-once-called-functions
)
1290 (apply function args
)
1291 (push spec anything-once-called-functions
))))
1293 ;; (@* "Core: API helper")
1294 (defun anything-let-eval-varlist (varlist)
1295 (mapcar (lambda (pair)
1297 (cons (car pair
) (eval (cadr pair
)))
1300 (defun anything-let*-eval-varlist
(varlist)
1301 (let ((vars (mapcar (lambda (pair) (or (car-safe pair
) pair
)) varlist
)))
1303 ,@(mapcar (lambda (pair)
1305 `(setq ,(car pair
) ,(cadr pair
))
1308 (mapcar (lambda (v) (cons v
(symbol-value v
))) ',vars
)))))
1309 (defun anything-let-internal (binding bodyfunc
)
1310 (setq anything-let-variables binding
)
1313 (setq anything-let-variables nil
)))
1316 ;; (@* "Core: tools")
1317 (defun anything-funcall-with-source (source func
&rest args
)
1318 "Call FUNC with ARGS with variable `anything-source-name' and `source' is bound.
1319 FUNC can be function list. Return the result of last function call."
1320 (let ((anything-source-name (assoc-default 'name source
))
1322 (anything-log-eval anything-source-name func args
)
1323 (dolist (func (if (functionp func
) (list func
) func
) result
)
1324 (setq result
(apply func args
)))))
1326 (defun anything-funcall-foreach (sym)
1327 "Call the sym function(s) for each source if any."
1328 (dolist (source (anything-get-sources))
1329 (when (symbolp source
)
1330 (setq source
(symbol-value source
)))
1331 (anything-aif (assoc-default sym source
)
1332 (anything-funcall-with-source source it
))))
1334 (defun anything-normalize-sources (sources)
1335 "If SOURCES is only one source, make a list."
1336 (cond ((or (and sources
; avoid nil
1338 (and (listp sources
) (assq 'name sources
)))
1341 (t anything-sources
)))
1343 (defun anything-approximate-candidate-number ()
1344 "Approximate Number of candidates.
1345 It is used to check if candidate number is 0, 1, or 2+."
1346 (with-current-buffer anything-buffer
1347 (let ((lines (1- (line-number-at-pos (1- (point-max))))))
1351 (goto-char (point-min))
1353 (if (anything-pos-multiline-p)
1354 (if (search-forward anything-candidate-separator nil t
) 2 1)
1357 (defmacro with-anything-quittable
(&rest body
)
1358 `(let (inhibit-quit)
1361 (quit (setq anything-quit t
)
1364 (put 'with-anything-quittable
'lisp-indent-function
0)
1366 (defun anything-compose (arg-lst func-lst
)
1367 "Call each function in FUNC-LST with the arguments specified in ARG-LST.
1368 The result of each function will be the new `car' of ARG-LST.
1370 This function allows easy sequencing of transformer functions."
1371 (dolist (func func-lst
)
1372 (setcar arg-lst
(apply func arg-lst
)))
1375 (defun anything-composed-funcall-with-source (source funcs
&rest args
)
1376 (if (functionp funcs
)
1377 (apply 'anything-funcall-with-source source funcs args
)
1378 (apply 'anything-funcall-with-source
1379 source
(lambda (&rest args
) (anything-compose args funcs
)) args
)))
1381 ;; (@* "Core: entry point")
1382 (defvar anything-buffers nil
1383 "All of `anything-buffer' in most recently used order.")
1385 (defun anything-internal (&optional any-sources any-input any-prompt any-resume any-preselect any-buffer any-keymap
)
1386 "Older interface of `anything'. It is called by `anything'."
1387 (anything-log "++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
1388 (anything-log-eval any-prompt any-preselect any-buffer any-keymap
)
1390 (let ( ;; It is needed because `anything-source-name' is non-nil
1391 ;; when `anything' is invoked by action. Awful global scope.
1392 anything-source-name anything-in-persistent-action
1394 (case-fold-search t
)
1395 (anything-buffer (or any-buffer anything-buffer
))
1396 ;; cua-mode ; avoid error when region is selected
1398 (with-anything-restore-variables
1399 (anything-initialize-1 any-resume any-input any-sources
)
1400 (anything-display-buffer anything-buffer
)
1401 (anything-log "show prompt")
1403 (anything-read-pattern-maybe any-prompt any-input any-preselect any-resume any-keymap
)
1404 (anything-cleanup)))
1405 (prog1 (unless anything-quit
(anything-execute-selection-action-1))
1406 (anything-log "end session --------------------------------------------")))
1409 (anything-log "end session (quit) -------------------------------------")
1412 (defconst anything-argument-keys
'(:sources
:input
:prompt
:resume
:preselect
:buffer
:keymap
))
1414 (defun anything (&rest plist
)
1415 "Select anything. In Lisp program, some optional arguments can be used.
1417 PLIST is a list like (:key1 val1 :key2 val2 ...) or
1418 (&optional sources input prompt resume preselect buffer keymap).
1420 Basic keywords are the following:
1424 Temporary value of `anything-sources'. It also accepts a
1425 symbol, interpreted as a variable of an anything source. It
1426 also accepts an alist representing an anything source, which is
1427 detected by (assq 'name ANY-SOURCES)
1431 Temporary value of `anything-pattern', ie. initial input of minibuffer.
1435 Prompt other than \"pattern: \".
1439 If t, Resurrect previously instance of `anything'. Skip the initialization.
1440 If 'noresume, this instance of `anything' cannot be resumed.
1444 Initially selected candidate. Specified by exact candidate or a regexp.
1445 Note that it is not working with delayed sources.
1449 `anything-buffer' instead of *anything*.
1453 `anything-map' for current `anything' session.
1456 Of course, conventional arguments are supported, the two are same.
1458 (anything :sources sources :input input :prompt prompt :resume resume
1459 :preselect preselect :buffer buffer :keymap keymap)
1460 (anything sources input prompt resume preselect buffer keymap)
1463 Other keywords are interpreted as local variables of this anything session.
1464 The `anything-' prefix can be omitted. For example,
1466 (anything :sources 'anything-c-source-buffers
1467 :buffer \"*buffers*\" :candidate-number-limit 10)
1469 means starting anything session with `anything-c-source-buffers'
1470 source in *buffers* buffer and set
1471 `anything-candidate-number-limit' to 10 as session local variable. "
1473 (if (keywordp (car plist
))
1474 (anything-let-internal
1475 (anything-parse-keys plist
)
1478 (mapcar (lambda (key) (plist-get plist key
))
1479 anything-argument-keys
))))
1480 (apply 'anything-internal plist
)))
1482 (defun anything-parse-keys (keys)
1483 (loop for
(key value
&rest _
) on keys by
#'cddr
1484 for symname
= (substring (symbol-name key
) 1)
1485 for sym
= (intern (if (string-match "^anything-" symname
)
1487 (concat "anything-" symname
)))
1488 unless
(memq key anything-argument-keys
)
1489 collect
(cons sym value
)))
1491 (defun anything-resume-p (any-resume)
1492 "Whethre current anything session is resumed or not."
1493 (memq any-resume
'(t window-only
)))
1495 (defun anything-initialize-1 (any-resume any-input any-sources
)
1496 "The real initialization of `anything'.
1498 This function name should be `anything-initialize', but anything
1499 extensions may advice `anything-initalize'. I cannot rename, sigh."
1500 (anything-log "start initialization: any-resume=%S any-input=%S" any-resume any-input
)
1501 (anything-frame/window-configuration
'save
)
1502 (setq anything-sources
(anything-normalize-sources any-sources
))
1503 (anything-log "sources = %S" anything-sources
)
1504 (anything-hooks 'setup
)
1505 (anything-current-position 'save
)
1506 (if (anything-resume-p any-resume
)
1507 (anything-initialize-overlays (anything-buffer-get))
1508 (anything-initialize))
1509 (unless (eq any-resume
'noresume
)
1510 (anything-recent-push anything-buffer
'anything-buffers
)
1511 (setq anything-last-buffer anything-buffer
))
1512 (when any-input
(setq anything-input any-input anything-pattern any-input
))
1513 (and (anything-resume-p any-resume
) (anything-funcall-foreach 'resume
))
1514 (anything-log "end initialization"))
1516 (defun anything-execute-selection-action-1 ()
1518 (anything-execute-selection-action)
1519 (anything-aif (get-buffer anything-action-buffer
)
1521 (anything-log-run-hook 'anything-after-action-hook
)))
1523 (defun anything-on-quit ()
1524 (setq minibuffer-history
(cons anything-input minibuffer-history
))
1525 (anything-current-position 'restore
))
1527 (defun anything-resume-select-buffer (input)
1528 (anything '(((name .
"Resume anything buffer")
1529 (candidates . anything-buffers
)
1530 (action . identity
)))
1531 input nil
'noresume nil
"*anything resume*"))
1533 (defun* anything-resume
(&optional
(any-buffer anything-last-buffer
) buffer-pattern
(any-resume t
))
1534 "Resurrect previously invoked `anything'."
1536 (when (or current-prefix-arg buffer-pattern
)
1537 (setq any-buffer
(anything-resume-select-buffer buffer-pattern
)))
1538 (setq anything-compiled-sources nil
)
1540 (or (buffer-local-value 'anything-last-sources-local
(get-buffer any-buffer
))
1541 anything-last-sources anything-sources
)
1542 (buffer-local-value 'anything-input-local
(get-buffer any-buffer
)) nil any-resume nil any-buffer
))
1544 ;;; rubikitch: experimental
1545 ;;; I use this and check it whether I am convenient.
1546 ;;; I may introduce an option to control the behavior.
1547 (defun* anything-resume-window-only
(&optional
(any-buffer anything-last-buffer
) buffer-pattern
)
1549 (anything-resume any-buffer buffer-pattern
'window-only
))
1551 (defun anything-recent-push (elt list-var
)
1552 "Add ELT to the value of LIST-VAR as most recently used value."
1553 (let ((m (member elt
(symbol-value list-var
))))
1554 (and m
(set list-var
(delq (car m
) (symbol-value list-var
))))
1555 (push elt
(symbol-value list-var
))))
1557 ;;; (@* "Core: Accessors")
1558 (defvar anything-current-position nil
1559 "Cons of (point) and (window-start) when `anything' is invoked.
1560 It is needed because restoring position when `anything' is keyboard-quitted.")
1561 (defun anything-current-position (save-or-restore)
1562 (case save-or-restore
1564 (setq anything-current-position
(cons (point) (window-start))))
1566 (goto-char (car anything-current-position
))
1567 (set-window-start (selected-window) (cdr anything-current-position
)))))
1569 ;;; FIXME I want to remove them. But anything-iswitchb uses them.
1570 (defun anything-current-frame/window-configuration
()
1571 (funcall (cdr anything-save-configuration-functions
)))
1572 (defun anything-set-frame/window-configuration
(conf)
1573 (funcall (car anything-save-configuration-functions
) conf
))
1575 (declare-function 'anything-frame
/window-configuration
"anything")
1577 (defun anything-frame/window-configuration
(save-or-restore)
1578 (anything-log-eval anything-save-configuration-functions
)
1579 (case save-or-restore
1580 (save (setq conf
(funcall (cdr anything-save-configuration-functions
))))
1581 (restore (funcall (car anything-save-configuration-functions
) conf
)))))
1586 (defun anything-at-point (&optional any-sources any-input any-prompt any-resume any-preselect any-buffer
)
1587 "Same as `anything' except when C-u is pressed, the initial input is the symbol at point."
1589 (anything any-sources
1590 (if current-prefix-arg
1591 (concat "\\b" (thing-at-point 'symbol
) "\\b"
1592 (if (featurep 'anything-match-plugin
) " " ""))
1594 any-prompt any-resume any-preselect any-buffer
))
1596 (defun anything-other-buffer (any-sources any-buffer
)
1597 "Simplified interface of `anything' with other `anything-buffer'"
1598 (anything any-sources nil nil nil nil any-buffer
))
1600 ;; (@* "Core: Display *anything* buffer")
1601 (defun anything-display-buffer (buf)
1602 "Display *anything* buffer."
1603 (funcall (with-current-buffer buf anything-display-function
) buf
))
1605 (defun anything-default-display-buffer (buf)
1606 (funcall (if anything-samewindow
'switch-to-buffer
'pop-to-buffer
) buf
))
1608 ;; (@* "Core: initialize")
1609 (defun anything-initialize ()
1610 "Initialize anything settings and set up the anything buffer."
1611 (anything-log-run-hook 'anything-before-initialize-hook
)
1612 (setq anything-once-called-functions nil
)
1613 (setq anything-delayed-init-executed nil
)
1614 (setq anything-current-buffer
(current-buffer))
1615 (setq anything-buffer-file-name buffer-file-name
)
1616 (setq anything-issued-errors nil
)
1617 (setq anything-compiled-sources nil
)
1618 (setq anything-saved-current-source nil
)
1619 ;; Call the init function for sources where appropriate
1620 (anything-funcall-foreach 'init
)
1622 (setq anything-pattern
"")
1623 (setq anything-input
"")
1624 (setq anything-candidate-cache nil
)
1625 (setq anything-last-sources anything-sources
)
1627 (anything-create-anything-buffer)
1628 (anything-log-run-hook 'anything-after-initialize-hook
))
1630 (defun anything-read-pattern-maybe (any-prompt any-input any-preselect any-resume any-keymap
)
1631 (if (anything-resume-p any-resume
) (anything-mark-current-line) (anything-update))
1632 (select-frame-set-input-focus (window-frame (minibuffer-window)))
1633 (anything-preselect any-preselect
)
1634 (let ((ncandidate (anything-approximate-candidate-number))
1635 (minibuffer-local-map
1636 (with-current-buffer (anything-buffer-get)
1637 (and any-keymap
(set (make-local-variable 'anything-map
) any-keymap
))
1640 (anything-log-eval ncandidate anything-execute-action-at-once-if-one
1641 anything-quit-if-no-candidate
)
1642 (cond ((and anything-execute-action-at-once-if-one
1645 ((and anything-quit-if-no-candidate
(= ncandidate
0))
1646 (setq anything-quit t
)
1647 (and (functionp anything-quit-if-no-candidate
)
1648 (funcall anything-quit-if-no-candidate
)))
1650 (read-string (or any-prompt
"pattern: ") any-input
)))))
1652 (defun anything-create-anything-buffer (&optional test-mode
)
1653 "Create newly created `anything-buffer'.
1654 If TEST-MODE is non-nil, clear `anything-candidate-cache'."
1656 (setq anything-candidate-cache nil
))
1657 (with-current-buffer (get-buffer-create anything-buffer
)
1658 (anything-log "kill local variables: %S" (buffer-local-variables))
1659 (kill-all-local-variables)
1660 (buffer-disable-undo)
1662 (set (make-local-variable 'inhibit-read-only
) t
)
1663 (set (make-local-variable 'anything-last-sources-local
) anything-sources
)
1664 (set (make-local-variable 'anything-follow-mode
) nil
)
1665 (set (make-local-variable 'anything-display-function
) anything-display-function
)
1666 (anything-log-eval anything-display-function anything-let-variables
)
1667 (loop for
(var . val
) in anything-let-variables
1668 do
(set (make-local-variable var
) val
))
1670 (setq cursor-type nil
)
1671 (setq mode-name
"Anything"))
1672 (anything-initialize-overlays anything-buffer
)
1673 (get-buffer anything-buffer
))
1675 (defun anything-initialize-overlays (buffer)
1676 (anything-log "overlay setup")
1677 (if anything-selection-overlay
1678 ;; make sure the overlay belongs to the anything buffer if
1679 ;; it's newly created
1680 (move-overlay anything-selection-overlay
(point-min) (point-min)
1681 (get-buffer buffer
))
1683 (setq anything-selection-overlay
1684 (make-overlay (point-min) (point-min) (get-buffer buffer
)))
1685 (overlay-put anything-selection-overlay
'face anything-selection-face
))
1687 (when anything-enable-shortcuts
1688 (setq anything-shortcut-keys
(assoc-default anything-enable-shortcuts anything-shortcut-keys-alist
)))
1690 (if anything-enable-shortcuts
1691 (unless anything-digit-overlays
1692 (setq anything-digit-overlays
1693 (loop for key across anything-shortcut-keys
1694 for overlay
= (make-overlay (point-min) (point-min) (get-buffer buffer
))
1695 do
(overlay-put overlay
'before-string
1696 (format "%s - " (upcase (make-string 1 key
))))
1699 (when anything-digit-overlays
1700 (dolist (overlay anything-digit-overlays
)
1701 (delete-overlay overlay
))
1702 (setq anything-digit-overlays nil
))))
1704 (defun anything-hooks (setup-or-cleanup)
1705 (let ((hooks '((deferred-action-list anything-check-minibuffer-input
)
1706 (minibuffer-setup-hook anything-print-error-messages
))))
1707 (if (eq setup-or-cleanup
'setup
)
1708 (dolist (args hooks
) (apply 'add-hook args
))
1709 (dolist (args (reverse hooks
)) (apply 'remove-hook args
)))))
1711 ;; (@* "Core: clean up")
1712 (defun anything-cleanup ()
1713 "Clean up the mess."
1714 (anything-log "start cleanup")
1715 (with-current-buffer anything-buffer
1716 (setq cursor-type t
))
1717 (bury-buffer anything-buffer
)
1718 (anything-funcall-foreach 'cleanup
)
1719 (if anything-check-minibuffer-input-timer
1720 (cancel-timer anything-check-minibuffer-input-timer
))
1721 (anything-kill-async-processes)
1722 (anything-log-run-hook 'anything-cleanup-hook
)
1723 (anything-hooks 'cleanup
)
1724 (anything-frame/window-configuration
'restore
))
1726 ;; (@* "Core: input handling")
1727 (defun anything-check-minibuffer-input ()
1728 "Extract input string from the minibuffer and check if it needs
1730 (if (or (not anything-input-idle-delay
) (anything-action-window))
1731 (anything-check-minibuffer-input-1)
1732 (if anything-check-minibuffer-input-timer
1733 (cancel-timer anything-check-minibuffer-input-timer
))
1734 (setq anything-check-minibuffer-input-timer
1735 (run-with-idle-timer anything-input-idle-delay nil
1736 'anything-check-minibuffer-input-1
))))
1738 (defun anything-check-minibuffer-input-1 ()
1739 (with-anything-quittable
1740 (with-selected-window (minibuffer-window)
1741 (anything-check-new-input (minibuffer-contents)))))
1743 (defun anything-check-new-input (input)
1744 "Check input string and update the anything buffer if
1746 (unless (equal input anything-pattern
)
1747 (setq anything-pattern input
)
1748 (unless (anything-action-window)
1749 (setq anything-input anything-pattern
))
1750 (anything-log-eval anything-pattern anything-input
)
1753 ;; (@* "Core: source compiler")
1754 (defvar anything-compile-source-functions-default anything-compile-source-functions
1755 "Plug-ins this file provides.")
1756 (defun anything-compile-sources (sources funcs
)
1757 "Compile sources (`anything-sources') with funcs (`anything-compile-source-functions').
1758 Anything plug-ins are realized by this function."
1761 (loop with source
= (if (listp source
) source
(symbol-value source
))
1763 do
(setq source
(funcall f source
))
1764 finally
(return source
)))
1767 ;; (@* "Core: plug-in attribute documentation hack")
1769 ;; `anything-document-attribute' is public API.
1770 (defadvice documentation-property
(after anything-document-attribute activate
)
1771 "Hack to display plug-in attributes' documentation as `anything-sources' docstring."
1772 (when (eq symbol
'anything-sources
)
1773 (setq ad-return-value
1774 (concat ad-return-value
"\n"
1775 (mapconcat (lambda (sym) (get sym
'anything-attrdoc
))
1776 anything-additional-attributes
1778 ;; (describe-variable 'anything-sources)
1779 ;; (documentation-property 'anything-sources 'variable-documentation)
1780 ;; (progn (ad-disable-advice 'documentation-property 'after 'anything-document-attribute) (ad-update 'documentation-property))
1782 ;; (@* "Core: all candidates")
1783 (defun anything-process-delayed-init (source)
1784 (let ((name (assoc-default 'name source
)))
1785 (unless (member name anything-delayed-init-executed
)
1786 (anything-aif (assoc-default 'delayed-init source
)
1787 (with-current-buffer anything-current-buffer
1788 (anything-funcall-with-source source it
)
1789 (dolist (f (if (functionp it
) (list it
) it
))
1790 (add-to-list 'anything-delayed-init-executed name
)))))))
1792 (defun anything-get-candidates (source)
1793 "Retrieve and return the list of candidates from
1795 (anything-process-delayed-init source
)
1796 (let* ((candidate-source (assoc-default 'candidates source
))
1797 (type-error (lambda ()
1798 (error (concat "Candidates must either be a function, "
1799 " a variable or a list: %s")
1801 (candidates (condition-case err
1802 (anything-interpret-value candidate-source source
)
1803 (error (funcall type-error
)))))
1804 (cond ((processp candidates
) candidates
)
1805 ((listp candidates
) (anything-transform-candidates candidates source
))
1806 (t (funcall type-error
)))))
1809 (defun anything-transform-candidates (candidates source
&optional process-p
)
1810 "Transform CANDIDATES according to candidate transformers."
1811 (anything-aif (assoc-default 'candidate-transformer source
)
1812 (setq candidates
(anything-composed-funcall-with-source source it candidates
)))
1813 (anything-aif (and process-p
(assoc-default 'filtered-candidate-transformer source
))
1814 (setq candidates
(anything-composed-funcall-with-source source it candidates source
)))
1815 (anything-aif (assoc-default 'real-to-display source
)
1816 (setq candidates
(anything-funcall-with-source
1820 ;; override DISPLAY from candidate-transformer
1821 (cons (funcall it
(cdr cand_
)) (cdr cand_
))
1822 (cons (funcall it cand_
) cand_
)))
1827 (defun anything-get-cached-candidates (source)
1828 "Return the cached value of candidates for SOURCE.
1829 Cache the candidates if there is not yet a cached value."
1830 (let* ((name (assoc-default 'name source
))
1831 (candidate-cache (assoc name anything-candidate-cache
))
1833 (cond (candidate-cache
1834 (anything-log "use cached candidates")
1835 (setq candidates
(cdr candidate-cache
)))
1838 (anything-log "calculate candidates")
1839 (setq candidates
(anything-get-candidates source
))
1840 (if (processp candidates
)
1842 (push (cons candidates
1844 (list (cons 'item-count
0)
1845 (cons 'incomplete-line
""))))
1846 anything-async-processes
)
1847 (set-process-filter candidates
'anything-output-filter
)
1848 (setq candidates nil
))
1850 (unless (assoc 'volatile source
)
1851 (setq candidate-cache
(cons name candidates
))
1852 (push candidate-cache anything-candidate-cache
)))))
1856 ;; (@* "Core: narrowing candidates")
1857 (defun anything-candidate-number-limit (source)
1858 "`anything-candidate-number-limit' variable may be overridden by SOURCE.
1859 If (candidate-number-limit) is in SOURCE, show all candidates in SOURCE,
1860 ie. cancel the effect of `anything-candidate-number-limit'."
1861 (anything-aif (assq 'candidate-number-limit source
)
1862 (or (cdr it
) 99999999)
1863 (or anything-candidate-number-limit
99999999)))
1865 (defun anything-compute-matches (source)
1866 "Compute matches from SOURCE according to its settings."
1867 (let ((doit (lambda ()
1868 (let ((functions (assoc-default 'match source
))
1869 (anything-source-name (assoc-default 'name source
))
1870 (limit (anything-candidate-number-limit source
))
1872 (anything-aif (assoc-default 'pattern-transformer source
)
1873 (anything-composed-funcall-with-source source it anything-pattern
)
1876 (cond ((or (equal anything-pattern
"") (equal functions
'(identity)))
1877 (setq matches
(anything-get-cached-candidates source
))
1878 (if (> (length matches
) limit
)
1880 (subseq matches
0 limit
))))
1883 (let ((item-count 0)
1884 (cands (anything-get-cached-candidates source
))
1889 (list (lambda (candidate)
1890 (string-match anything-pattern candidate
)))))
1892 (clrhash anything-match-hash
)
1893 (dolist (function functions
)
1894 (let (newmatches c cc
)
1895 (dolist (candidate cands
)
1896 (when (and (not (gethash candidate anything-match-hash
))
1897 (setq c
(if (listp candidate
)
1900 (setq cc
(cond ((stringp c
) c
)
1901 ((symbolp c
) (symbol-name c
))))
1902 (funcall function cc
))
1903 (puthash candidate t anything-match-hash
)
1904 (push candidate newmatches
)
1908 (when (= item-count limit
)
1912 (setq matches
(append matches
(reverse newmatches
)))
1917 (invalid-regexp (setq matches nil
)))))
1919 (anything-aif (assoc-default 'filtered-candidate-transformer source
)
1921 (anything-composed-funcall-with-source source it matches source
)))
1927 (error (anything-log-error
1928 "anything-compute-matches: error when processing source: %s"
1929 (assoc-default 'name source
))
1932 ;; (anything '(((name . "error")(candidates . (lambda () (hage))) (action . identity))))
1934 (defun anything-process-source (source)
1935 "Display matches from SOURCE according to its settings."
1936 (anything-log-eval (assoc-default 'name source
))
1937 (if (assq 'direct-insert-match source
) ;experimental
1938 (anything-process-source--direct-insert-match source
)
1939 (let ((matches (anything-compute-matches source
)))
1941 (when anything-test-mode
1942 (setq anything-test-candidate-list
1943 `(,@anything-test-candidate-list
1944 (,(assoc-default 'name source
)
1946 (let ((multiline (assq 'multiline source
))
1949 (anything-insert-header-from-source source
)
1950 (dolist (match matches
)
1951 (if (and multiline separate
)
1952 (anything-insert-candidate-separator)
1955 (when (and anything-enable-shortcuts
1956 (not (eq anything-digit-shortcut-count
1957 (length anything-digit-overlays
))))
1958 (move-overlay (nth anything-digit-shortcut-count
1959 anything-digit-overlays
)
1960 (line-beginning-position)
1961 (line-beginning-position))
1962 (incf anything-digit-shortcut-count
))
1963 (anything-insert-match match
'insert source
))
1966 (put-text-property start
(point) 'anything-multiline t
)))))))
1968 (defun anything-process-source--direct-insert-match (source)
1969 "[EXPERIMENTAL] Insert candidates from `anything-candidate-buffer'"
1970 (anything-log-eval (assoc-default 'name source
))
1971 (let ((anything-source-name (assoc-default 'name source
))
1973 (funcall (assoc-default 'candidates source
))
1974 (setq content-buf
(anything-candidate-buffer))
1975 (unless (zerop (buffer-size content-buf
))
1976 (anything-insert-header-from-source source
)
1977 (insert-buffer-substring content-buf
))))
1979 (defun anything-process-delayed-sources (delayed-sources)
1980 "Process delayed sources if the user is idle for
1981 `anything-idle-delay' seconds."
1982 (with-anything-quittable
1983 (anything-log-eval (ignore-errors (mapcar (lambda (s) (assoc-default 'name s
)) delayed-sources
)))
1984 (if (sit-for (if anything-input-idle-delay
1985 (max 0 (- anything-idle-delay anything-input-idle-delay
))
1986 anything-idle-delay
))
1987 (with-current-buffer anything-buffer
1989 (goto-char (point-max))
1990 (dolist (source delayed-sources
)
1991 (anything-process-source source
))
1993 (when (and (not (equal (buffer-size) 0))
1995 (= (overlay-start anything-selection-overlay
)
1996 (overlay-end anything-selection-overlay
)))
1997 (goto-char (point-min))
1998 (anything-next-line)))
2000 (goto-char (point-min))
2001 (anything-log-run-hook 'anything-update-hook
))
2002 (anything-maybe-fit-frame)))))
2004 ;; (@* "Core: *anything* buffer contents")
2005 (defvar anything-input-local nil
)
2006 (defun anything-update ()
2007 "Update the list of matches in the anything buffer according to
2008 the current pattern."
2009 (anything-log "start update")
2010 (setq anything-digit-shortcut-count
0)
2011 (anything-kill-async-processes)
2012 (with-current-buffer (anything-buffer-get)
2013 (set (make-local-variable 'anything-input-local
) anything-pattern
)
2016 (if anything-enable-shortcuts
2017 (dolist (overlay anything-digit-overlays
)
2018 (delete-overlay overlay
)))
2020 (let (delayed-sources)
2022 (dolist (source (anything-get-sources))
2023 (when (and (or (not anything-source-filter
)
2024 (member (assoc-default 'name source
) anything-source-filter
))
2025 (>= (length anything-pattern
)
2026 (anything-aif (assoc 'requires-pattern source
)
2029 (if (or (assoc 'delayed source
)
2030 (and anything-quick-update
2031 (< (window-height (get-buffer-window (current-buffer)))
2032 (line-number-at-pos (point-max)))))
2033 (push source delayed-sources
)
2034 (anything-process-source source
))))
2036 (goto-char (point-min))
2037 (save-excursion (anything-log-run-hook 'anything-update-hook
))
2038 (anything-next-line)
2039 (setq delayed-sources
(nreverse delayed-sources
))
2040 (if anything-test-mode
2041 (dolist (source delayed-sources
)
2042 (anything-process-source source
))
2043 (anything-maybe-fit-frame)
2044 (when delayed-sources
2045 (run-with-idle-timer (if (featurep 'xemacs
)
2049 'anything-process-delayed-sources
2051 ;; FIXME I want to execute anything-after-update-hook
2052 ;; AFTER processing delayed sources
2053 (anything-log-run-hook 'anything-after-update-hook
))
2054 (anything-log "end update")))))
2056 (defun anything-force-update ()
2057 "Recalculate and update candidates.
2058 If current source has `update' attribute, a function without argument, call it before update."
2060 (let ((source (anything-get-current-source)))
2061 (anything-aif (anything-funcall-with-source source
'anything-candidate-buffer
)
2063 (anything-aif (assoc-default 'update source
)
2064 (anything-funcall-with-source source it
))
2065 (anything-aif (assoc-default 'init source
)
2066 (anything-funcall-with-source source it
))
2067 ;; Remove from candidate cache to recalculate candidates
2068 (setq anything-candidate-cache
2069 (delete (assoc (assoc-default 'name source
) anything-candidate-cache
)
2070 anything-candidate-cache
))
2071 ;; Go to original selection after update
2072 (let ((selection (anything-get-selection nil t
)))
2074 (with-anything-window
2075 (anything-goto-source source
)
2076 (forward-char -
1) ;back to \n
2077 (if (search-forward (concat "\n" selection
"\n") nil t
)
2079 (goto-char (point-min))
2081 (anything-mark-current-line)))))
2083 (defun anything-insert-match (match insert-function source
)
2084 "Insert MATCH into the anything buffer. If MATCH is a list then
2085 insert the string inteneded to appear on the display and store
2086 the real value in a text property."
2087 (let ((start (line-beginning-position (point)))
2088 (string (if (listp match
) (car match
) match
))
2089 (realvalue (if (listp match
) (cdr match
))))
2090 (when (symbolp string
) (setq string
(symbol-name string
)))
2091 (when (stringp string
)
2092 (funcall insert-function string
)
2093 ;; Some sources with candidates-in-buffer have already added
2094 ;; 'anything-realvalue property when creating candidate buffer.
2095 (unless (get-text-property start
'anything-realvalue
)
2097 (put-text-property start
(line-end-position)
2098 'anything-realvalue realvalue
)))
2099 (when anything-source-in-each-line-flag
2100 (put-text-property start
(line-end-position)
2101 'anything-source source
))
2102 (funcall insert-function
"\n"))))
2104 (defun anything-insert-header-from-source (source)
2105 (let ((name (assoc-default 'name source
)))
2106 (anything-insert-header name
2107 (anything-aif (assoc-default 'header-name source
)
2108 (anything-funcall-with-source source it name
)))))
2110 (defun anything-insert-header (name &optional display-string
)
2111 "Insert header of source NAME into the anything buffer."
2113 (let ((start (point)))
2115 (put-text-property start
(point) 'anything-header-separator t
)))
2117 (let ((start (point)))
2119 (put-text-property (line-beginning-position)
2120 (line-end-position) 'anything-header t
)
2121 (when display-string
2122 (overlay-put (make-overlay (line-beginning-position) (line-end-position))
2123 'display display-string
))
2125 (put-text-property start
(point) 'face anything-header-face
)))
2128 (defun anything-insert-candidate-separator ()
2129 "Insert separator of candidates into the anything buffer."
2130 (insert anything-candidate-separator
)
2131 (put-text-property (line-beginning-position)
2132 (line-end-position) 'anything-candidate-separator t
)
2138 ;; (@* "Core: async process")
2139 (defun anything-output-filter (process string
)
2140 "Process output from PROCESS."
2141 (let* ((process-assoc (assoc process anything-async-processes
))
2142 (process-info (cdr process-assoc
))
2143 (insertion-marker (assoc-default 'insertion-marker process-info
))
2144 (incomplete-line-info (assoc 'incomplete-line process-info
))
2145 (item-count-info (assoc 'item-count process-info
))
2146 (limit (anything-candidate-number-limit process-info
)))
2147 (anything-log-eval string
(cdr incomplete-line-info
))
2148 (with-current-buffer anything-buffer
2150 (if insertion-marker
2151 (goto-char insertion-marker
)
2153 (goto-char (point-max))
2154 (anything-insert-header-from-source process-info
)
2155 (setcdr process-assoc
2156 (append process-info
`((insertion-marker .
,(point-marker))))))
2158 (let ((lines (split-string string
"\n"))
2159 (multiline (assq 'multiline process-info
))
2161 candidates separate
)
2163 (if (not (cdr lines
))
2164 ;; store last incomplete line until new output arrives
2165 (setcdr incomplete-line-info
(car lines
))
2167 (if (cdr incomplete-line-info
)
2169 (push (concat (cdr incomplete-line-info
) (car lines
))
2171 (setcdr incomplete-line-info nil
))
2173 (push (car lines
) candidates
)))
2177 (setq candidates
(reverse candidates
))
2178 (dolist (candidate (anything-transform-candidates candidates process-info t
))
2180 ;; (if (and multiline separate)
2181 ;; (anything-insert-candidate-separator)
2182 ;; (setq separate t))
2183 (anything-insert-match candidate
'insert-before-markers process-info
)
2184 (incf (cdr item-count-info
))
2187 ;; (put-text-property start (point) 'anything-multiline t))
2188 (when (>= (cdr item-count-info
) limit
)
2189 (anything-kill-async-process process
)
2192 (anything-maybe-fit-frame)
2194 (anything-log-run-hook 'anything-update-hook
)
2197 (anything-next-line)
2199 (save-selected-window
2200 (select-window (get-buffer-window anything-buffer
'visible
))
2201 (anything-mark-current-line))))))
2204 (defun anything-kill-async-processes ()
2205 "Kill all known asynchronous processes according to
2206 `anything-async-processes'."
2207 "Kill locate process."
2208 (dolist (process-info anything-async-processes
)
2209 (anything-kill-async-process (car process-info
)))
2210 (setq anything-async-processes nil
))
2213 (defun anything-kill-async-process (process)
2214 "Kill PROCESS and detach the associated functions."
2215 (set-process-filter process nil
)
2216 (delete-process process
))
2219 ;; (@* "Core: action")
2220 (defun anything-execute-selection-action (&optional selection action clear-saved-action
)
2221 "If a candidate was selected then perform the associated
2223 (anything-log "executing action")
2224 (setq selection
(or selection
(anything-get-selection)))
2225 (setq action
(or action
2226 anything-saved-action
2227 (if (get-buffer anything-action-buffer
)
2228 (anything-get-selection anything-action-buffer
)
2229 (anything-get-action))))
2230 (let ((source (or anything-saved-current-source
(anything-get-current-source))))
2231 (if (and (not selection
) (assoc 'accept-empty source
))
2232 (setq selection
""))
2233 (if (and (listp action
)
2234 (not (functionp action
))) ; lambda
2235 ;;select the default action
2236 (setq action
(cdar action
)))
2237 (unless clear-saved-action
(setq anything-saved-action nil
))
2238 (if (and selection action
)
2239 (anything-funcall-with-source source action selection
))))
2241 (defun anything-select-action ()
2242 "Select an action for the currently selected candidate.
2243 If action buffer is selected, back to the anything buffer."
2245 (cond ((get-buffer-window anything-action-buffer
'visible
)
2246 (set-window-buffer (get-buffer-window anything-action-buffer
) anything-buffer
)
2247 (kill-buffer anything-action-buffer
)
2248 (insert anything-input
))
2250 (setq anything-saved-selection
(anything-get-selection))
2251 (unless anything-saved-selection
2252 (error "Nothing is selected."))
2253 (setq anything-saved-current-source
(anything-get-current-source))
2254 (let ((actions (anything-get-action)))
2255 (if (functionp actions
)
2256 (message "Sole action: %s" actions
)
2257 (with-current-buffer (get-buffer-create anything-action-buffer
)
2259 (buffer-disable-undo)
2260 (set-window-buffer (get-buffer-window anything-buffer
) anything-action-buffer
)
2261 (set (make-local-variable 'anything-sources
)
2262 `(((name .
"Actions")
2264 (candidates .
,actions
)
2265 ;; Override `anything-candidate-number-limit'
2266 (candidate-number-limit .
9999))))
2267 (set (make-local-variable 'anything-source-filter
) nil
)
2268 (set (make-local-variable 'anything-selection-overlay
) nil
)
2269 (set (make-local-variable 'anything-digit-overlays
) nil
)
2270 (anything-initialize-overlays anything-action-buffer
))
2271 (with-selected-window (minibuffer-window)
2272 (delete-minibuffer-contents))
2273 (setq anything-pattern
'dummy
) ; so that it differs from the
2276 (anything-check-minibuffer-input))))))
2278 ;; (@* "Core: selection")
2279 (defun anything-move-selection-common (move-func unit direction
)
2280 "Move the selection marker to a new position determined by
2281 UNIT and DIRECTION."
2282 (unless (or (zerop (buffer-size (get-buffer (anything-buffer-get))))
2283 (not (anything-window)))
2284 (with-anything-window
2286 (while (and (not (bobp))
2287 (or (anything-pos-header-line-p)
2288 (anything-pos-candidate-separator-p)))
2289 (forward-line (if (and (eq direction
'previous
)
2290 (not (eq (line-beginning-position) (point-min))))
2293 (and (bobp) (forward-line 1)) ;skip first header
2294 (and (eobp) (forward-line -
1)) ;avoid last empty line
2295 (when (and anything-display-source-at-screen-top
(eq unit
'source
))
2296 (set-window-start (selected-window)
2297 (save-excursion (forward-line -
1) (point))))
2298 (when (anything-get-previous-header-pos)
2299 (anything-mark-current-line))
2300 (anything-display-mode-line (anything-get-current-source)))))
2302 (defvar anything-mode-line-string-real nil
)
2303 (defun anything-display-mode-line (source)
2304 (set (make-local-variable 'anything-mode-line-string
)
2305 (anything-interpret-value (or (assoc-default 'mode-line source
)
2306 (default-value 'anything-mode-line-string
))
2308 (if anything-mode-line-string
2309 (setq mode-line-format
2310 '(" " mode-line-buffer-identification
" "
2311 (line-number-mode "%l") " " (anything-follow-mode "(F)")
2312 " " anything-mode-line-string-real
"-%-")
2313 anything-mode-line-string-real
2314 (substitute-command-keys anything-mode-line-string
))
2315 (setq mode-line-format
2316 (default-value 'mode-line-format
)))
2317 (setq header-line-format
2318 (anything-interpret-value (assoc-default 'header-line source
) source
)))
2320 (defun anything-previous-line ()
2321 "Move selection to the previous line."
2323 (anything-move-selection-common
2326 (when (anything-pos-multiline-p)
2327 (if (or (anything-pos-header-line-p)
2328 (anything-pos-candidate-separator-p))
2331 (let ((header-pos (anything-get-previous-header-pos))
2332 (candidate-pos (anything-get-previous-candidate-separator-pos)))
2334 (if (or (null candidate-pos
) (< candidate-pos header-pos
))
2335 (goto-char header-pos
)
2336 (goto-char candidate-pos
))
2337 (forward-line 1)))))
2340 (defun anything-next-line ()
2341 "Move selection to the next line."
2343 (anything-move-selection-common
2345 (if (not (anything-pos-multiline-p))
2347 (let ((header-pos (anything-get-next-header-pos))
2348 (candidate-pos (anything-get-next-candidate-separator-pos)))
2349 (if (and candidate-pos
2350 (or (null header-pos
)
2351 (< candidate-pos header-pos
)))
2352 (goto-char candidate-pos
)
2354 (goto-char header-pos
)))
2356 (forward-line 1)))))
2359 (defun anything-previous-page ()
2360 "Move selection back with a pageful."
2362 (anything-move-selection-common
2366 (beginning-of-buffer (goto-char (point-min)))))
2369 (defun anything-next-page ()
2370 "Move selection forward with a pageful."
2372 (anything-move-selection-common
2376 (end-of-buffer (goto-char (point-max)))))
2379 (defun anything-beginning-of-buffer ()
2380 "Move selection at the top."
2382 (anything-move-selection-common (lambda () (goto-char (point-min)))
2385 (defun anything-end-of-buffer ()
2386 "Move selection at the bottom."
2388 (anything-move-selection-common (lambda () (goto-char (point-max)))
2391 (defun anything-previous-source ()
2392 "Move selection to the previous source."
2394 (anything-move-selection-common
2398 (goto-char (point-max))
2399 (if (anything-pos-header-line-p)
2402 (goto-char (anything-get-previous-header-pos))
2406 (defun anything-next-source ()
2407 "Move selection to the next source."
2409 (anything-move-selection-common
2410 (lambda () (goto-char (or (anything-get-next-header-pos) (point-min))))
2413 (defun anything-goto-source (source-or-name)
2414 "Move the selection to the source (SOURCE-OR-NAME)."
2415 (anything-move-selection-common
2417 (goto-char (point-min))
2418 (let ((name (if (stringp source-or-name
) source-or-name
2419 (assoc-default 'name source-or-name
))))
2420 (while (not (string= name
(buffer-substring (point-at-bol) (point-at-eol))))
2421 (goto-char (anything-get-next-header-pos)))))
2424 (defun anything-mark-current-line ()
2425 "Move selection overlay to current line."
2426 (move-overlay anything-selection-overlay
2427 (line-beginning-position)
2428 (if (anything-pos-multiline-p)
2429 (let ((header-pos (anything-get-next-header-pos))
2430 (candidate-pos (anything-get-next-candidate-separator-pos)))
2431 (or (and (null header-pos
) candidate-pos candidate-pos
)
2432 (and header-pos candidate-pos
(< candidate-pos header-pos
) candidate-pos
)
2435 (1+ (line-end-position))))
2436 (anything-follow-execute-persistent-action-maybe))
2438 (defun anything-this-command-key ()
2439 (event-basic-type (elt (this-command-keys-vector) 0)))
2440 ;; (progn (read-key-sequence "Key: ") (p (anything-this-command-key)))
2442 (defun anything-select-with-digit-shortcut ()
2444 (if (eq anything-enable-shortcuts
'alphabet
)
2445 (save-selected-window
2446 (select-window (anything-window))
2447 (let* ((index (position (anything-this-command-key) anything-shortcut-keys
))
2448 (overlay (nth index anything-digit-overlays
)))
2449 (when (overlay-buffer overlay
)
2450 (goto-char (overlay-start overlay
))
2451 (anything-mark-current-line)
2452 (anything-exit-minibuffer))))
2453 (self-insert-command 1)))
2455 ;;; EXPERIMENTAL TODO documentation
2456 (defun anything-select-with-prefix-shortcut ()
2458 (if (eq anything-enable-shortcuts
'prefix
)
2459 (save-selected-window
2460 (select-window (anything-window))
2461 (let* ((key (read-event "Select shortcut key: "))
2462 (index (position key anything-shortcut-keys
))
2463 (overlay (ignore-errors (nth index anything-digit-overlays
))))
2464 (if (not (and overlay
(overlay-buffer overlay
)))
2466 (select-window (minibuffer-window))
2467 (self-insert-command 1))
2468 (goto-char (overlay-start overlay
))
2469 (anything-mark-current-line)
2470 (anything-exit-minibuffer))))
2471 (self-insert-command 1)))
2472 ;; (setq anything-enable-shortcuts 'prefix)
2473 ;; (define-key anything-map "@" 'anything-select-with-prefix-shortcut)
2474 ;; (define-key anything-map (kbd "<f18>") 'anything-select-with-prefix-shortcut)
2476 (defun anything-exit-minibuffer ()
2477 "Select the current candidate by exiting the minibuffer."
2479 (declare (special anything-iswitchb-candidate-selected
))
2480 (setq anything-iswitchb-candidate-selected
(anything-get-selection))
2484 (defun anything-get-next-header-pos ()
2485 "Return the position of the next header from point."
2486 (next-single-property-change (point) 'anything-header
))
2489 (defun anything-get-previous-header-pos ()
2490 "Return the position of the previous header from point"
2491 (previous-single-property-change (point) 'anything-header
))
2494 (defun anything-pos-multiline-p ()
2495 "Return non-nil if the current position is in the multiline source region."
2496 (get-text-property (point) 'anything-multiline
))
2499 (defun anything-get-next-candidate-separator-pos ()
2500 "Return the position of the next candidate separator from point."
2501 (next-single-property-change (point) 'anything-candidate-separator
))
2504 (defun anything-get-previous-candidate-separator-pos ()
2505 "Return the position of the previous candidate separator from point."
2506 (previous-single-property-change (point) 'anything-candidate-separator
))
2509 (defun anything-pos-header-line-p ()
2510 "Return t if the current line is a header line."
2511 (or (get-text-property (line-beginning-position) 'anything-header
)
2512 (get-text-property (line-beginning-position) 'anything-header-separator
)))
2514 (defun anything-pos-candidate-separator-p ()
2515 "Return t if the current line is a candidate separator."
2516 (get-text-property (line-beginning-position) 'anything-candidate-separator
))
2518 ;; (@* "Core: error handling")
2519 (defun anything-log-error (&rest args
)
2520 "Accumulate error messages into `anything-issued-errors'."
2521 (let ((msg (apply 'format args
)))
2522 (unless (member msg anything-issued-errors
)
2523 (add-to-list 'anything-issued-errors msg
))))
2525 (defun anything-print-error-messages ()
2526 "Print error messages in `anything-issued-errors'."
2527 (message "%s" (mapconcat 'identity
(reverse anything-issued-errors
) "\n")))
2530 ;; (@* "Core: help")
2531 (defun anything-help-internal (bufname insert-content-fn
)
2532 "Show long message during `anything' session."
2533 (save-window-excursion
2534 (select-window (anything-window))
2535 (delete-other-windows)
2536 (switch-to-buffer (get-buffer-create bufname
))
2538 (funcall insert-content-fn
)
2539 (setq mode-line-format
"%b (SPC,C-v:NextPage b,M-v:PrevPage other:Exit)")
2540 (setq cursor-type nil
)
2543 (loop for event
= (read-event) do
2545 ((?\C-v ?
) (scroll-up))
2546 ((?\M-v ?b
) (scroll-down))
2549 (defun anything-help ()
2550 "Help of `anything'."
2552 (anything-help-internal
2555 (insert (substitute-command-keys
2556 (anything-interpret-value anything-help-message
)))
2559 (defun anything-debug-output ()
2560 "Show all anything-related variables at this time."
2562 (anything-help-internal " *Anything Debug*" 'anything-debug-output-function
))
2564 (defun anything-debug-output-function (&optional vars
)
2565 (message "Calculating all anything-related values...")
2566 (insert "If you debug some variables or forms, set `anything-debug-forms'
2567 to a list of forms.\n\n")
2569 anything-debug-forms
2570 (apropos-internal "^anything-" 'boundp
)))
2572 (pp-to-string v
) "\n"
2573 (pp-to-string (eval v
)) "\n"))
2574 (message "Calculating all anything-related values...Done")
2577 ;; (@* "Core: misc")
2578 (defun anything-kill-buffer-hook ()
2579 "Remove tick entry from `anything-tick-hash' when killing a buffer."
2580 (loop for key being the hash-keys in anything-tick-hash
2581 if
(string-match (format "^%s/" (regexp-quote (buffer-name))) key
)
2582 do
(remhash key anything-tick-hash
)))
2583 (add-hook 'kill-buffer-hook
'anything-kill-buffer-hook
)
2585 (defun anything-maybe-fit-frame ()
2586 "Fit anything frame to its buffer, and put it at top right of display.
2588 It is disabled by default because some flickering occurred in some environment.
2589 To enable fitting, set both `anything-inhibit-fit-frame-flag' and
2590 `fit-frame-inhibit-fitting' to nil.
2591 You can set user options `fit-frame-max-width-percent' and
2592 `fit-frame-max-height-percent' to control max frame size."
2593 (declare (warn (unresolved 0)))
2594 (when (and (require 'fit-frame nil t
)
2595 (boundp 'fit-frame-inhibit-fitting-flag
)
2596 (not anything-inhibit-fit-frame-flag
)
2597 (not fit-frame-inhibit-fitting-flag
)
2600 (with-anything-window
2601 (fit-frame nil nil nil t
)
2602 (modify-frame-parameters
2604 `((left .
,(- (x-display-pixel-width) (+ (frame-pixel-width) 7)))
2605 (top .
0))))))) ; The (top . 0) shouldn't be necessary (Emacs bug).
2607 (defun anything-preselect (candidate-or-regexp)
2608 (with-anything-window
2609 (when candidate-or-regexp
2610 (goto-char (point-min))
2611 ;; go to first candidate of first source
2613 (let ((start (point)))
2614 (unless (or (re-search-forward (concat "^" (regexp-quote candidate-or-regexp
) "$") nil t
)
2615 (progn (goto-char start
)
2616 (re-search-forward candidate-or-regexp nil t
)))
2617 (goto-char start
))))
2618 (anything-mark-current-line)))
2620 (defun anything-delete-current-selection ()
2621 "Delete the currently selected item."
2623 (with-anything-window
2624 (cond ((anything-pos-multiline-p)
2625 (anything-aif (anything-get-next-candidate-separator-pos)
2626 (delete-region (point-at-bol)
2627 (1+ (progn (goto-char it
) (point-at-eol))))
2629 (goto-char (anything-get-previous-candidate-separator-pos))
2630 (delete-region (point-at-bol) (point-max)))
2632 (goto-char (or (anything-get-previous-candidate-separator-pos)
2636 (delete-region (point-at-bol) (1+ (point-at-eol)))
2637 (when (eobp) (forward-line -
1))))
2638 (anything-mark-current-line)))
2640 (defun anything-edit-current-selection-1 (func)
2641 (with-anything-window
2643 (let ((realvalue (get-text-property (point) 'anything-realvalue
)))
2647 (put-text-property (point) (point-at-eol) 'anything-realvalue realvalue
))
2648 (anything-mark-current-line))))
2650 (defmacro anything-edit-current-selection
(&rest forms
)
2651 "Evaluate FORMS at current selection in the anything buffer.
2652 You can edit the line."
2653 `(anything-edit-current-selection-1
2654 (lambda () ,@forms
)))
2655 (put 'anything-edit-current-selection
'lisp-indent-function
0)
2657 (defun anything-set-pattern (pattern)
2658 "Set minibuffer contents to PATTERN."
2659 (with-selected-window (minibuffer-window)
2660 (delete-minibuffer-contents)
2663 (defun anything-delete-minibuffer-content ()
2664 "Same as `delete-minibuffer-contents' but this is a command."
2666 (anything-set-pattern ""))
2668 ;; (@* "Built-in plug-in: type")
2669 (defun anything-compile-source--type (source)
2670 (anything-aif (assoc-default 'type source
)
2671 (append source
(assoc-default it anything-type-attributes
) nil
)
2674 ;; `define-anything-type-attribute' is public API.
2676 (defun anything-add-type-attribute (type definition
)
2677 (anything-aif (assq type anything-type-attributes
)
2678 (setq anything-type-attributes
(delete it anything-type-attributes
)))
2679 (push (cons type definition
) anything-type-attributes
))
2681 (defvar anything-types nil
)
2682 (defun anything-document-type-attribute (type doc
)
2683 (add-to-list 'anything-types type t
)
2684 (put type
'anything-typeattrdoc
2685 (concat "- " (symbol-name type
) "\n\n" doc
"\n")))
2687 (defadvice documentation-property
(after anything-document-type-attribute activate
)
2688 "Hack to display type attributes' documentation as `anything-type-attributes' docstring."
2689 (when (eq symbol
'anything-type-attributes
)
2690 (setq ad-return-value
2691 (concat ad-return-value
"\n\n++++ Types currently defined ++++\n"
2692 (mapconcat (lambda (sym) (get sym
'anything-typeattrdoc
))
2693 anything-types
"\n")))))
2695 ;; (@* "Built-in plug-in: dummy")
2696 (defun anything-dummy-candidate (candidate source
)
2697 ;; `source' is defined in filtered-candidate-transformer
2698 (list anything-pattern
))
2700 (defun anything-compile-source--dummy (source)
2701 (if (assoc 'dummy source
)
2703 '((candidates "dummy")
2706 (filtered-candidate-transformer . anything-dummy-candidate
)
2711 ;; (@* "Built-in plug-in: disable-shortcuts")
2712 (defvar anything-orig-enable-shortcuts nil
)
2713 (defun anything-save-enable-shortcuts ()
2715 (lambda () (setq anything-orig-enable-shortcuts anything-enable-shortcuts
2716 anything-enable-shortcuts nil
))))
2717 (defun anything-compile-source--disable-shortcuts (source)
2718 (if (assoc 'disable-shortcuts source
)
2719 (append `((init ,@(anything-mklist (assoc-default 'init source
))
2720 anything-save-enable-shortcuts
)
2721 (resume ,@(anything-mklist (assoc-default 'resume source
))
2722 anything-save-enable-shortcuts
)
2723 (cleanup ,@(anything-mklist (assoc-default 'cleanup source
))
2724 (lambda () (setq anything-enable-shortcuts anything-orig-enable-shortcuts
))))
2728 ;; (@* "Built-in plug-in: candidates-in-buffer")
2729 (defun anything-candidates-in-buffer ()
2730 "Get candidates from the candidates buffer according to `anything-pattern'.
2732 BUFFER is `anything-candidate-buffer' by default. Each
2733 candidate must be placed in one line. This function is meant to
2734 be used in candidates-in-buffer or candidates attribute of an
2735 anything source. Especially fast for many (1000+) candidates.
2738 '((name . \"many files\")
2739 (init . (lambda () (with-current-buffer (anything-candidate-buffer 'local)
2740 (insert-many-filenames))))
2741 (search re-search-forward) ; optional
2742 (candidates-in-buffer)
2745 +===============================================================+
2746 | The new way of making and narrowing candidates: Using buffers |
2747 +===============================================================+
2749 By default, `anything' makes candidates by evaluating the
2750 candidates function, then narrows them by `string-match' for each
2753 But this way is very slow for many candidates. The new way is
2754 storing all candidates in a buffer and narrowing them by
2755 `re-search-forward'. Search function is customizable by search
2756 attribute. The important point is that buffer processing is MUCH
2757 FASTER than string list processing and is the Emacs way.
2759 The init function writes all candidates to a newly-created
2760 candidate buffer. The candidates buffer is created or specified
2761 by `anything-candidate-buffer'. Candidates are stored in a line.
2763 The candidates function narrows all candidates, IOW creates a
2764 subset of candidates dynamically. It is the task of
2765 `anything-candidates-in-buffer'. As long as
2766 `anything-candidate-buffer' is used,`(candidates-in-buffer)' is
2767 sufficient in most cases.
2769 Note that `(candidates-in-buffer)' is shortcut of three attributes:
2770 (candidates . anything-candidates-in-buffer)
2773 And `(candidates-in-buffer . func)' is shortcut of three attributes:
2777 The expansion is performed in `anything-get-sources'.
2779 The candidates-in-buffer attribute implies the volatile attribute.
2780 The volatile attribute is needed because `anything-candidates-in-buffer'
2781 creates candidates dynamically and need to be called everytime
2782 `anything-pattern' changes.
2784 Because `anything-candidates-in-buffer' plays the role of `match' attribute
2785 function, specifying `(match identity)' makes the source slightly faster.
2787 To customize `anything-candidates-in-buffer' behavior, use search,
2788 get-line and search-from-end attributes. See also `anything-sources' docstring.
2790 (declare (special source
))
2791 (anything-candidates-in-buffer-1 (anything-candidate-buffer)
2793 (or (assoc-default 'get-line source
)
2794 #'buffer-substring-no-properties
)
2795 ;; use external variable `source'.
2796 (or (assoc-default 'search source
)
2797 (if (assoc 'search-from-end source
)
2798 '(re-search-backward)
2799 '(re-search-forward)))
2800 (anything-candidate-number-limit source
)
2801 (assoc 'search-from-end source
)))
2803 (defun* anything-candidates-in-buffer-1
(buffer &optional
(pattern anything-pattern
) (get-line-fn 'buffer-substring-no-properties
) (search-fns '(re-search-forward)) (limit anything-candidate-number-limit
) search-from-end
)
2804 ;; buffer == nil when candidates buffer does not exist.
2806 (with-current-buffer buffer
2807 (let ((start-point (if search-from-end
(point-max) (point-min)))
2808 (next-line-fn (if search-from-end
2809 (lambda (x) (goto-char (max (1- (point-at-bol)) 1)))
2811 (endp (if search-from-end
#'bobp
#'eobp
)))
2812 (goto-char (1- start-point
))
2813 (if (string= pattern
"")
2814 (delq nil
(loop until
(funcall endp
)
2815 for i from
1 to limit
2816 collecting
(funcall get-line-fn
(point-at-bol) (point-at-eol))
2817 do
(funcall next-line-fn
1)))
2820 (next-line-fn (if search-from-end
2821 (lambda (x) (goto-char (max (point-at-bol) 1)))
2824 matches exit newmatches
)
2826 (goto-char (point-min))
2828 (goto-char (point-max))
2830 (setq start-point
(if search-from-end
(point-max) (point-min)))
2831 (clrhash anything-cib-hash
)
2833 (dolist (searcher search-fns
)
2834 (goto-char start-point
)
2835 (setq newmatches nil
)
2836 (loop while
(funcall searcher pattern nil t
)
2837 if
(or (funcall endp
) (< limit i
))
2838 do
(setq exit t
) (return)
2840 (let ((cand (funcall get-line-fn
(point-at-bol) (point-at-eol))))
2841 (unless (gethash cand anything-cib-hash
)
2842 (puthash cand t anything-cib-hash
)
2844 (push cand newmatches
)))
2845 (funcall next-line-fn
1))
2846 (setq matches
(append matches
(nreverse newmatches
)))
2848 (goto-char (point-min))
2850 (goto-char (1- (point-max)))
2853 (set-buffer-modified-p nil
)))
2854 (delq nil matches
)))))))
2857 (defun anything-candidate-buffer (&optional create-or-buffer
)
2858 "Register and return a buffer containing candidates of current source.
2859 `anything-candidate-buffer' searches buffer-local candidates buffer first,
2860 then global candidates buffer.
2862 Acceptable values of CREATE-OR-BUFFER:
2865 Only return the candidates buffer.
2867 Register a buffer as a candidates buffer.
2869 Create a new global candidates buffer,
2870 named \" *anything candidates:SOURCE*\".
2871 - other non-nil value
2872 Create a new global candidates buffer,
2873 named \" *anything candidates:SOURCE*ANYTHING-CURRENT-BUFFER\".
2875 (let* ((gbufname (format " *anything candidates:%s*" anything-source-name
))
2876 (lbufname (concat gbufname
(buffer-name anything-current-buffer
)))
2878 (when create-or-buffer
2879 (if (bufferp create-or-buffer
)
2880 (setq anything-candidate-buffer-alist
2881 (cons (cons anything-source-name create-or-buffer
)
2882 (delete (assoc anything-source-name anything-candidate-buffer-alist
)
2883 anything-candidate-buffer-alist
)))
2884 (add-to-list 'anything-candidate-buffer-alist
2885 (cons anything-source-name create-or-buffer
))
2886 (when (eq create-or-buffer
'global
)
2887 (loop for b in
(buffer-list)
2888 if
(string-match (format "^%s" (regexp-quote gbufname
)) (buffer-name b
))
2889 do
(kill-buffer b
)))
2890 (with-current-buffer
2891 (get-buffer-create (if (eq create-or-buffer
'global
) gbufname lbufname
))
2892 (buffer-disable-undo)
2894 (font-lock-mode -
1))))
2895 (or (get-buffer lbufname
)
2896 (get-buffer gbufname
)
2897 (anything-aif (assoc-default anything-source-name anything-candidate-buffer-alist
)
2898 (and (buffer-live-p it
) it
)))))
2900 (defun anything-compile-source--candidates-in-buffer (source)
2901 (anything-aif (assoc 'candidates-in-buffer source
)
2902 (append source
`((candidates .
,(or (cdr it
) 'anything-candidates-in-buffer
))
2903 (volatile) (match identity
)))
2906 ;; (@* "Utility: resplit anything window")
2907 (defun anything-toggle-resplit-window ()
2908 "Toggle resplit anything window, vertically or horizontally."
2910 (with-anything-window
2911 (let ((before-height (window-height)))
2914 (select-window (if (= (window-height) before-height
)
2915 (split-window-vertically)
2916 (split-window-horizontally)))
2919 ;; (@* "Utility: select another action by key")
2920 (defun anything-select-nth-action (n)
2921 "Select the nth action for the currently selected candidate."
2922 (setq anything-saved-selection
(anything-get-selection))
2923 (unless anything-saved-selection
2924 (error "Nothing is selected."))
2925 (setq anything-saved-action
(cdr (elt (anything-get-action) n
)))
2926 (anything-exit-minibuffer))
2928 (defun anything-select-2nd-action ()
2929 "Select the 2nd action for the currently selected candidate."
2931 (anything-select-nth-action 1))
2933 (defun anything-select-3rd-action ()
2934 "Select the 3rd action for the currently selected candidate."
2936 (anything-select-nth-action 2))
2938 (defun anything-select-4th-action ()
2939 "Select the 4th action for the currently selected candidate."
2941 (anything-select-nth-action 3))
2943 (defun anything-select-2nd-action-or-end-of-line ()
2944 "Select the 2nd action for the currently selected candidate if the point is at the end of minibuffer.
2945 Otherwise goto the end of minibuffer."
2948 (anything-select-nth-action 1)
2951 ;; (@* "Utility: Persistent Action")
2952 (defmacro with-anything-display-same-window
(&rest body
)
2953 "Make `pop-to-buffer' and `display-buffer' display in the same window."
2954 `(let ((display-buffer-function 'anything-persistent-action-display-buffer
))
2956 (put 'with-anything-display-same-window
'lisp-indent-function
0)
2958 (defun* anything-execute-persistent-action
(&optional
(attr 'persistent-action
))
2959 "If a candidate is selected then perform the associated action without quitting anything."
2961 (anything-log "executing persistent-action")
2962 (save-selected-window
2963 (select-window (get-buffer-window (anything-buffer-get)))
2964 (select-window (setq minibuffer-scroll-window
2965 (if (one-window-p t
) (split-window)
2966 (next-window (selected-window) 1))))
2967 (anything-log-eval (current-buffer))
2968 (let ((anything-in-persistent-action t
))
2969 (with-anything-display-same-window
2970 (anything-execute-selection-action
2972 (or (assoc-default attr
(anything-get-current-source))
2973 (anything-get-action))
2975 (anything-log-run-hook 'anything-after-persistent-action-hook
)))))
2977 (defun anything-persistent-action-display-buffer (buf &optional not-this-window
)
2978 "Make `pop-to-buffer' and `display-buffer' display in the same window in persistent action.
2979 If `anything-persistent-action-use-special-display' is non-nil and
2980 BUF is to be displayed by `special-display-function', use it.
2981 Otherwise ignores `special-display-buffer-names' and `special-display-regexps'."
2982 (let* ((name (buffer-name buf
))
2983 display-buffer-function pop-up-windows
2984 (same-window-regexps
2985 (unless (and anything-persistent-action-use-special-display
2987 (mapcar (lambda (x) (or (car-safe x
) x
)) special-display-buffer-names
))
2989 (lambda (x) (string-match (or (car-safe x
) x
) name
))
2990 special-display-regexps
)))
2992 (display-buffer buf not-this-window
)))
2994 ;; scroll-other-window(-down)? for persistent-action
2995 (defun anything-scroll-other-window-base (command)
2996 (save-selected-window
2999 (lambda (w) (not (string= anything-buffer
(buffer-name (window-buffer w
)))))
3000 'no-minibuffer
'current-frame
))
3001 (call-interactively command
)))
3003 (defun anything-scroll-other-window ()
3004 "Scroll other window (not *Anything* window) upward."
3006 (anything-scroll-other-window-base (lambda ()
3008 (scroll-up anything-scroll-amount
))))
3009 (defun anything-scroll-other-window-down ()
3010 "Scroll other window (not *Anything* window) downward."
3012 (anything-scroll-other-window-base (lambda ()
3014 (scroll-down anything-scroll-amount
))))
3016 ;; (@* "Utility: Visible Mark")
3017 (defface anything-visible-mark
3018 '((((min-colors 88) (background dark
))
3019 (:background
"green1" :foreground
"black"))
3020 (((background dark
)) (:background
"green" :foreground
"black"))
3021 (((min-colors 88)) (:background
"green1"))
3022 (t (:background
"green")))
3023 "Face for visible mark."
3025 (defvar anything-visible-mark-face
'anything-visible-mark
)
3026 (defvar anything-visible-mark-overlays nil
)
3028 (defun anything-clear-visible-mark ()
3029 (with-current-buffer (anything-buffer-get)
3030 (mapc 'delete-overlay anything-visible-mark-overlays
)
3031 (set (make-local-variable 'anything-visible-mark-overlays
)
3033 (add-hook 'anything-after-initialize-hook
'anything-clear-visible-mark
)
3035 (defvar anything-c-marked-candidate-list nil
3036 "[OBSOLETE] DO NOT USE!!")
3037 (defvar anything-marked-candidates nil
3038 "Marked candadates. List of (source . real) pair.")
3039 (defun anything-toggle-visible-mark ()
3041 (with-anything-window
3042 (let ((display (buffer-substring-no-properties (point-at-bol) (point-at-eol)))
3043 (source (anything-get-current-source))
3044 (selection (anything-get-selection)))
3045 (anything-aif (loop for o in anything-visible-mark-overlays
3046 when
(equal (line-beginning-position) (overlay-start o
))
3050 (setq anything-c-marked-candidate-list
3052 display anything-c-marked-candidate-list
))
3053 (setq anything-marked-candidates
3055 (cons source selection
)
3056 anything-marked-candidates
))
3058 (setq anything-visible-mark-overlays
(delq it anything-visible-mark-overlays
)))
3059 (let ((o (make-overlay (line-beginning-position) (1+ (line-end-position)))))
3060 (overlay-put o
'face anything-visible-mark-face
)
3061 (overlay-put o
'source
(assoc-default 'name source
))
3062 (overlay-put o
'string
(buffer-substring (overlay-start o
) (overlay-end o
)))
3063 (add-to-list 'anything-visible-mark-overlays o
)
3064 (push display anything-c-marked-candidate-list
)
3065 (push (cons source selection
) anything-marked-candidates
)))
3066 (anything-next-line))))
3068 (defun anything-display-all-visible-marks ()
3069 "Show all `anything' visible marks strings."
3071 (lexical-let ((overlays (reverse anything-visible-mark-overlays
)))
3072 (anything-run-after-quit
3074 (with-output-to-temp-buffer "*anything visible marks*"
3075 (dolist (o overlays
) (princ (overlay-get o
'string
))))))))
3077 (defun anything-marked-candidates ()
3078 "Marked candidates (real value) of current source if any,
3079 otherwise 1-element list of current selection.
3081 It is analogous to `dired-get-marked-files'."
3082 (with-current-buffer (anything-buffer-get)
3083 (let ((cands (if anything-marked-candidates
3084 (loop with current-src
= (anything-get-current-source)
3085 for
(source . real
) in
(reverse anything-marked-candidates
)
3086 when
(equal current-src source
)
3088 (list (anything-get-selection)))))
3089 (anything-log-eval cands
)
3092 (defun anything-reset-marked-candidates ()
3093 (with-current-buffer (anything-buffer-get)
3094 (set (make-local-variable 'anything-c-marked-candidate-list
) nil
)
3095 (set (make-local-variable 'anything-marked-candidates
) nil
)))
3097 (add-hook 'anything-after-initialize-hook
'anything-reset-marked-candidates
)
3098 ;; (add-hook 'anything-after-action-hook 'anything-reset-marked-candidates)
3100 (defun anything-revive-visible-mark ()
3102 (with-current-buffer anything-buffer
3103 (loop for o in anything-visible-mark-overlays do
3104 (goto-char (point-min))
3106 (while (and (not moved
)
3107 (search-forward (overlay-get o
'string
) nil t
))
3109 (when (and (save-excursion
3110 (goto-char (anything-get-previous-header-pos))
3111 (equal (overlay-get o
'source
)
3112 (buffer-substring (line-beginning-position) (line-end-position))))
3113 (not (find-if (lambda (x)
3114 (memq x anything-visible-mark-overlays
))
3115 (overlays-at (point)))))
3116 (move-overlay o
(line-beginning-position) (1+ (line-end-position)))
3118 (forward-line 1))))))
3119 (add-hook 'anything-update-hook
'anything-revive-visible-mark
)
3121 (defun anything-next-visible-mark (&optional prev
)
3123 (with-anything-window
3125 (setq anything-visible-mark-overlays
3126 (sort* anything-visible-mark-overlays
3127 '< :key
'overlay-start
))
3128 (let ((i (position-if (lambda (o) (< (point) (overlay-start o
)))
3129 anything-visible-mark-overlays
)))
3131 (unless anything-visible-mark-overlays
(return-from 'exit nil
))
3132 (if (not i
) (setq i
(length anything-visible-mark-overlays
)))
3133 (if (equal (point) (overlay-start (nth (1- i
) anything-visible-mark-overlays
)))
3136 (goto-char (overlay-start (nth (if prev
(1- i
) i
) anything-visible-mark-overlays
)))
3137 (anything-mark-current-line))))))
3139 (defun anything-prev-visible-mark ()
3141 (anything-next-visible-mark t
))
3143 ;; (@* "Utility: `find-file' integration")
3144 (defun anything-quit-and-find-file ()
3145 "Drop into `find-file' from `anything' like `iswitchb-find-file'.
3146 If current selection is a buffer or a file, `find-file' from its directory."
3148 (anything-run-after-quit
3150 (if (file-exists-p f
)
3151 (let ((default-directory (file-name-directory f
)))
3152 (call-interactively 'find-file
))
3153 (call-interactively 'find-file
)))
3154 (anything-aif (get-buffer (anything-get-selection))
3155 (buffer-file-name it
)
3156 (expand-file-name (anything-get-selection)))))
3158 ;; (@* "Utility: Selection Paste")
3159 (defun anything-yank-selection ()
3160 "Set minibuffer contents to current selection."
3162 (anything-set-pattern (anything-get-selection nil t
)))
3164 (defun anything-kill-selection-and-quit ()
3165 "Store current selection to kill ring.
3166 You can paste it by typing C-y."
3168 (anything-run-after-quit
3171 (message "Killed: %s" sel
))
3172 (anything-get-selection nil t
)))
3175 ;; (@* "Utility: Automatical execution of persistent-action")
3176 (add-to-list 'minor-mode-alist
'(anything-follow-mode " AFollow"))
3177 (defun anything-follow-mode ()
3178 "If this mode is on, persistent action is executed everytime the cursor is moved."
3180 (with-current-buffer anything-buffer
3181 (setq anything-follow-mode
(not anything-follow-mode
))
3182 (message "anything-follow-mode is %s"
3183 (if anything-follow-mode
"enabled" "disabled"))))
3185 (defun anything-follow-execute-persistent-action-maybe ()
3186 "Execute persistent action after `anything-input-idle-delay' secs when `anything-follow-mode' is enabled."
3187 (and (buffer-local-value 'anything-follow-mode
3188 (get-buffer-create anything-buffer
))
3189 (sit-for anything-input-idle-delay
)
3191 (anything-get-selection)
3193 (anything-execute-persistent-action))))
3195 ;; (@* "Utility: Migrate `anything-sources' to my-anything command")
3196 (defun anything-migrate-sources ()
3197 "Help to migrate to new `anything' way."
3199 (with-current-buffer (get-buffer-create "*anything migrate*")
3202 Setting `anything-sources' directly is not good because
3203 `anything' is not for one command. For now, interactive use of
3204 `anything' (M-x anything) is only for demonstration purpose.
3205 So you should define commands calling `anything'.
3206 I help you to migrate to the new way.
3208 The code below is automatically generated from current
3209 `anything-sources' value. You can use the `my-anything' command
3212 Copy and paste it to your .emacs. Then substitute `my-anything'
3213 for `anything' bindings in all `define-key', `local-set-key' and
3214 `global-set-key' calls.
3216 \(defun my-anything ()
3217 \"Anything command for you.
3219 It is automatically generated by `anything-migrate-sources'.\"
3221 (anything-other-buffer
3224 " anything-sources
))
3225 (eval-last-sexp nil
)
3226 (substitute-key-definition 'anything
'my-anything global-map
)
3227 (pop-to-buffer (current-buffer))))
3229 ;; (@* "Utility: Incremental search within results (unmaintained)")
3231 (defvar anything-isearch-original-global-map nil
3232 "Original global map before Anything isearch is started.")
3234 (defvar anything-isearch-original-message-timeout nil
3235 "Original message timeout before Anything isearch is started.")
3237 (defvar anything-isearch-pattern nil
3238 "The current isearch pattern.")
3240 (defvar anything-isearch-message-suffix
""
3241 "Message suffix indicating the current state of the search.")
3243 (defvar anything-isearch-original-point nil
3244 "Original position of point before isearch is started.")
3246 (defvar anything-isearch-original-window nil
3247 "Original selected window before isearch is started.")
3249 (defvar anything-isearch-original-cursor-in-non-selected-windows nil
3250 "Original value of cursor-in-non-selected-windows before isearch is started.")
3252 (defvar anything-isearch-original-deferred-action-list nil
3253 "Original value of deferred-action-list before isearch is started.")
3255 (defvar anything-isearch-match-positions nil
3256 "Stack of positions of matches or non-matches.
3258 It's a list of plists with two properties: `event', the last user
3259 event, `start', the start position of the current match, and
3260 `pos', the position of point after that event.
3262 The value of `event' can be the following symbols: `char' if a
3263 character was typed, `error' if a non-matching character was
3264 typed, `search' if a forward search had to be done after a
3265 character, and `search-again' if a search was done for the next
3266 occurrence of the current pattern.")
3268 (defvar anything-isearch-match-start nil
3269 "Start position of the current match.")
3272 (defun anything-isearch ()
3273 "Start incremental search within results. (UNMAINTAINED)"
3275 (if (zerop (buffer-size (get-buffer (anything-buffer-get))))
3276 (message "There are no results.")
3278 (setq anything-isearch-original-message-timeout minibuffer-message-timeout
)
3279 (setq minibuffer-message-timeout nil
)
3281 (setq anything-isearch-original-global-map global-map
)
3285 (setq anything-isearch-original-window
(selected-window))
3286 (select-window (anything-window))
3287 (setq cursor-type t
)
3289 (setq anything-isearch-original-deferred-action-list
3290 (default-value 'deferred-action-list
))
3291 (setq-default deferred-action-list nil
)
3292 (add-hook 'deferred-action-list
'anything-isearch-post-command
)
3294 (use-global-map anything-isearch-map
)
3295 (setq overriding-terminal-local-map anything-isearch-map
)
3297 (setq anything-isearch-pattern
"")
3299 (setq anything-isearch-original-cursor-in-non-selected-windows
3300 cursor-in-non-selected-windows
)
3301 (setq cursor-in-non-selected-windows nil
)
3303 (setq anything-isearch-original-point
(point-marker))
3304 (goto-char (point-min))
3306 (anything-mark-current-line)
3308 (setq anything-isearch-match-positions nil
)
3309 (setq anything-isearch-match-start
(point-marker))
3311 (if anything-isearch-overlay
3312 ;; make sure the overlay belongs to the anything buffer
3313 (move-overlay anything-isearch-overlay
(point-min) (point-min)
3314 (get-buffer (anything-buffer-get)))
3316 (setq anything-isearch-overlay
(make-overlay (point-min) (point-min)))
3317 (overlay-put anything-isearch-overlay
'face anything-isearch-match-face
))
3319 (setq anything-isearch-message-suffix
3320 (substitute-command-keys "cancel with \\[anything-isearch-cancel]")))
3322 (error (anything-isearch-cleanup)))))
3325 (defun anything-isearch-post-command ()
3326 "Print the current pattern after every command."
3327 (anything-isearch-message)
3328 (when (anything-window)
3329 (with-anything-window
3330 (move-overlay anything-isearch-overlay anything-isearch-match-start
(point)
3331 (get-buffer (anything-buffer-get))))))
3334 (defun anything-isearch-printing-char ()
3335 "Add printing char to the pattern."
3337 (let ((char (char-to-string last-command-event
)))
3338 (setq anything-isearch-pattern
(concat anything-isearch-pattern char
))
3340 (with-anything-window
3341 (if (looking-at char
)
3343 (push (list 'event
'char
3344 'start anything-isearch-match-start
3345 'pos
(point-marker))
3346 anything-isearch-match-positions
)
3349 (let ((start (point)))
3350 (while (and (re-search-forward anything-isearch-pattern nil t
)
3351 (anything-pos-header-line-p)))
3352 (if (or (anything-pos-header-line-p)
3356 (push (list 'event
'error
3357 'start anything-isearch-match-start
3358 'pos
(point-marker))
3359 anything-isearch-match-positions
))
3361 (push (list 'event
'search
3362 'start anything-isearch-match-start
3363 'pos
(copy-marker start
))
3364 anything-isearch-match-positions
)
3365 (setq anything-isearch-match-start
(copy-marker (match-beginning 0))))))
3367 (anything-mark-current-line))))
3370 (defun anything-isearch-again ()
3371 "Search again for the current pattern"
3373 (if (equal anything-isearch-pattern
"")
3374 (setq anything-isearch-message-suffix
"no pattern yet")
3376 (with-anything-window
3377 (let ((start (point)))
3378 (while (and (re-search-forward anything-isearch-pattern nil t
)
3379 (anything-pos-header-line-p)))
3380 (if (or (anything-pos-header-line-p)
3384 (unless (eq 'error
(plist-get (car anything-isearch-match-positions
)
3386 (setq anything-isearch-message-suffix
"no more matches")))
3388 (push (list 'event
'search-again
3389 'start anything-isearch-match-start
3390 'pos
(copy-marker start
))
3391 anything-isearch-match-positions
)
3392 (setq anything-isearch-match-start
(copy-marker (match-beginning 0)))
3394 (anything-mark-current-line))))))
3397 (defun anything-isearch-delete ()
3400 (unless (equal anything-isearch-pattern
"")
3401 (let ((last (pop anything-isearch-match-positions
)))
3402 (unless (eq 'search-again
(plist-get last
'event
))
3403 (setq anything-isearch-pattern
3404 (substring anything-isearch-pattern
0 -
1)))
3406 (with-anything-window
3407 (goto-char (plist-get last
'pos
))
3408 (setq anything-isearch-match-start
(plist-get last
'start
))
3409 (anything-mark-current-line)))))
3412 (defun anything-isearch-default-action ()
3413 "Execute the default action for the selected candidate."
3415 (anything-isearch-cleanup)
3416 (with-current-buffer (anything-buffer-get) (anything-exit-minibuffer)))
3419 (defun anything-isearch-select-action ()
3420 "Choose an action for the selected candidate."
3422 (anything-isearch-cleanup)
3423 (with-anything-window
3424 (anything-select-action)))
3427 (defun anything-isearch-cancel ()
3428 "Cancel Anything isearch."
3430 (anything-isearch-cleanup)
3431 (when (anything-window)
3432 (with-anything-window
3433 (goto-char anything-isearch-original-point
)
3434 (anything-mark-current-line))))
3437 (defun anything-isearch-cleanup ()
3438 "Clean up the mess."
3439 (setq minibuffer-message-timeout anything-isearch-original-message-timeout
)
3440 (with-current-buffer (anything-buffer-get)
3441 (setq overriding-terminal-local-map nil
)
3442 (setq cursor-type nil
)
3443 (setq cursor-in-non-selected-windows
3444 anything-isearch-original-cursor-in-non-selected-windows
))
3445 (when anything-isearch-original-window
3446 (select-window anything-isearch-original-window
))
3448 (use-global-map anything-isearch-original-global-map
)
3449 (setq-default deferred-action-list anything-isearch-original-deferred-action-list
)
3450 (when (overlayp anything-isearch-overlay
)
3451 (delete-overlay anything-isearch-overlay
)))
3454 (defun anything-isearch-message ()
3456 (if (and (equal anything-isearch-message-suffix
"")
3457 (eq (plist-get (car anything-isearch-match-positions
) 'event
)
3459 (setq anything-isearch-message-suffix
"failing"))
3461 (unless (equal anything-isearch-message-suffix
"")
3462 (setq anything-isearch-message-suffix
3463 (concat " [" anything-isearch-message-suffix
"]")))
3465 (message (concat "Search within results: "
3466 anything-isearch-pattern
3467 anything-isearch-message-suffix
))
3469 (setq anything-isearch-message-suffix
""))
3472 ;; (@* "Utility: Iswitchb integration (unmaintained)")
3474 (defvar anything-iswitchb-candidate-selected nil
3475 "Indicates whether an anything candidate is selected from iswitchb.")
3477 (defvar anything-iswitchb-frame-configuration nil
3478 "Saved frame configuration, before anything buffer was displayed.")
3480 (defvar anything-iswitchb-saved-keys nil
3481 "The original in iswitchb before binding anything keys.")
3484 (defun anything-iswitchb-setup ()
3485 "Integrate anything completion into iswitchb (UNMAINTAINED).
3487 If the user is idle for `anything-iswitchb-idle-delay' seconds
3488 after typing something into iswitchb then anything candidates are
3489 shown for the current iswitchb input.
3491 ESC cancels anything completion and returns to normal iswitchb.
3493 Some key bindings in `anything-map' are modified.
3494 See also `anything-iswitchb-setup-keys'."
3499 ;; disable timid completion during iswitchb
3500 (put 'iswitchb-buffer
'timid-completion
'disabled
)
3501 (add-hook 'minibuffer-setup-hook
'anything-iswitchb-minibuffer-setup
)
3503 (defadvice iswitchb-visit-buffer
3504 (around anything-iswitchb-visit-buffer activate
)
3505 (if anything-iswitchb-candidate-selected
3506 (anything-execute-selection-action)
3509 (defadvice iswitchb-possible-new-buffer
3510 (around anything-iswitchb-possible-new-buffer activate
)
3511 (if anything-iswitchb-candidate-selected
3512 (anything-execute-selection-action)
3514 (anything-iswitchb-setup-keys)
3515 (message "Iswitchb integration is activated."))
3517 (defun anything-iswitchb-setup-keys ()
3518 "Modify `anything-map' for anything-iswitchb users.
3520 C-p is used instead of M-p, because anything uses ESC
3521 (currently hardcoded) for `anything-iswitchb-cancel-anything' and
3522 Emacs handles ESC and Meta as synonyms, so ESC overrides
3523 other commands with Meta prefix.
3525 Note that iswitchb uses M-p and M-n by default for history
3526 navigation, so you should bind C-p and C-n in
3527 `iswitchb-mode-map' if you use the history keys and don't want
3528 to use different keys for iswitchb while anything is not yet
3529 kicked in. These keys are not bound automatically by anything
3530 in `iswitchb-mode-map' because they (C-n at least) already have
3531 a standard iswitchb binding which you might be accustomed to.
3533 Binding M-s is used instead of C-s, because C-s has a binding in
3534 iswitchb. You can rebind it AFTER `anything-iswitchb-setup'.
3536 Unbind C-r to prevent problems during anything-isearch."
3537 (define-key anything-map
(kbd "C-s") nil
)
3538 (define-key anything-map
(kbd "M-p") nil
)
3539 (define-key anything-map
(kbd "M-n") nil
)
3540 (define-key anything-map
(kbd "M-v") nil
)
3541 (define-key anything-map
(kbd "C-v") nil
)
3542 (define-key anything-map
(kbd "C-p") 'anything-previous-history-element
)
3543 (define-key anything-map
(kbd "C-n") 'anything-next-history-element
)
3544 (define-key anything-map
(kbd "M-s") nil
)
3545 (define-key anything-map
(kbd "M-s") 'anything-isearch
)
3546 (define-key anything-map
(kbd "C-r") nil
))
3548 (defun anything-iswitchb-minibuffer-setup ()
3549 (when (eq this-command
'iswitchb-buffer
)
3550 (add-hook 'minibuffer-exit-hook
'anything-iswitchb-minibuffer-exit
)
3552 (setq anything-iswitchb-frame-configuration nil
)
3553 (setq anything-iswitchb-candidate-selected nil
)
3554 (add-hook 'anything-update-hook
'anything-iswitchb-handle-update
)
3556 (anything-initialize)
3558 (add-hook 'deferred-action-list
'anything-iswitchb-check-input
)))
3561 (defun anything-iswitchb-minibuffer-exit ()
3562 (remove-hook 'minibuffer-exit-hook
'anything-iswitchb-minibuffer-exit
)
3563 (remove-hook 'deferred-action-list
'anything-iswitchb-check-input
)
3564 (remove-hook 'anything-update-hook
'anything-iswitchb-handle-update
)
3568 (when anything-iswitchb-frame-configuration
3569 (anything-set-frame/window-configuration anything-iswitchb-frame-configuration
)
3570 (setq anything-iswitchb-frame-configuration nil
)))
3573 (defun anything-iswitchb-check-input ()
3574 "Extract iswitchb input and check if it needs to be handled."
3575 (declare (special iswitchb-text
))
3576 (if (or anything-iswitchb-frame-configuration
3577 (sit-for anything-iswitchb-idle-delay
))
3578 (anything-check-new-input iswitchb-text
)))
3581 (defun anything-iswitchb-handle-update ()
3582 "Pop up the anything buffer if it's not empty and it's not
3583 shown yet and bind anything commands in iswitchb."
3584 (unless (or (equal (buffer-size (get-buffer anything-buffer
)) 0)
3585 anything-iswitchb-frame-configuration
)
3586 (setq anything-iswitchb-frame-configuration
(anything-current-frame/window-configuration
))
3588 (save-selected-window
3589 (if (not anything-samewindow
)
3590 (pop-to-buffer anything-buffer
)
3592 (select-window (get-lru-window))
3593 (switch-to-buffer anything-buffer
)))
3595 (with-current-buffer (window-buffer (active-minibuffer-window))
3596 (let* ((anything-prefix "anything-")
3597 (prefix-length (length anything-prefix
))
3603 (let ((command (cdr binding
)))
3604 (when (and (symbolp command
)
3605 (eq (compare-strings
3608 (symbol-name command
)
3612 (cdr anything-map
)))))
3613 (bindings (mapcar (lambda (command)
3615 (where-is-internal command anything-map
)))
3618 (push (list 'anything-iswitchb-cancel-anything
(kbd "<ESC>"))
3621 (setq anything-iswitchb-saved-keys nil
)
3623 (let* ((iswitchb-prefix "iswitchb-")
3624 (prefix-length (length iswitchb-prefix
)))
3625 (dolist (binding bindings
)
3626 (dolist (key (cdr binding
))
3627 (let ((old-command (lookup-key (current-local-map) key
)))
3628 (unless (and anything-iswitchb-dont-touch-iswithcb-keys
3629 (symbolp old-command
)
3630 (eq (compare-strings iswitchb-prefix
3632 (symbol-name old-command
)
3635 (push (cons key old-command
)
3636 anything-iswitchb-saved-keys
)
3637 (define-key (current-local-map) key
(car binding
)))))))))))
3640 (defun anything-iswitchb-cancel-anything ()
3641 "Cancel anything completion and return to standard iswitchb."
3644 (dolist (binding anything-iswitchb-saved-keys
)
3645 (define-key (current-local-map) (car binding
) (cdr binding
)))
3646 (anything-iswitchb-minibuffer-exit)))
3648 ;; (@* "Compatibility")
3650 ;; Copied assoc-default from XEmacs version 21.5.12
3651 (unless (fboundp 'assoc-default
)
3652 (defun assoc-default (key alist
&optional test default
)
3653 "Find object KEY in a pseudo-alist ALIST.
3654 ALIST is a list of conses or objects. Each element (or the element's car,
3655 if it is a cons) is compared with KEY by evaluating (TEST (car elt) KEY).
3656 If that is non-nil, the element matches;
3657 then `assoc-default' returns the element's cdr, if it is a cons,
3658 or DEFAULT if the element is not a cons.
3660 If no element matches, the value is nil.
3661 If TEST is omitted or nil, `equal' is used."
3662 (let (found (tail alist
) value
)
3663 (while (and tail
(not found
))
3664 (let ((elt (car tail
)))
3665 (when (funcall (or test
'equal
) (if (consp elt
) (car elt
) elt
) key
)
3666 (setq found t value
(if (consp elt
) (cdr elt
) default
))))
3667 (setq tail
(cdr tail
)))
3670 ;; Function not available in XEmacs,
3671 (unless (fboundp 'minibuffer-contents
)
3672 (defun minibuffer-contents ()
3673 "Return the user input in a minbuffer as a string.
3674 The current buffer must be a minibuffer."
3675 (field-string (point-max)))
3677 (defun delete-minibuffer-contents ()
3678 "Delete all user input in a minibuffer.
3679 The current buffer must be a minibuffer."
3680 (delete-field (point-max))))
3682 ;; Function not available in older Emacs (<= 22.1).
3683 (unless (fboundp 'buffer-chars-modified-tick
)
3684 (defun buffer-chars-modified-tick (&optional buffer
)
3685 "Return BUFFER's character-change tick counter.
3686 Each buffer has a character-change tick counter, which is set to the
3687 value of the buffer's tick counter (see `buffer-modified-tick'), each
3688 time text in that buffer is inserted or deleted. By comparing the
3689 values returned by two individual calls of `buffer-chars-modified-tick',
3690 you can tell whether a character change occurred in that buffer in
3691 between these calls. No argument or nil as argument means use current
3693 (with-current-buffer (or buffer
(current-buffer))
3694 (if (listp buffer-undo-list
)
3695 (length buffer-undo-list
)
3696 (buffer-modified-tick)))))
3698 ;; (@* "CUA workaround")
3699 (defadvice cua-delete-region
(around anything-avoid-cua activate
)
3700 (ignore-errors ad-do-it
))
3701 (defadvice copy-region-as-kill
(around anything-avoid-cua activate
)
3703 (ignore-errors ad-do-it
)
3707 ;;(@* "Attribute Documentation")
3708 (defun anything-describe-anything-attribute (anything-attribute)
3709 "Display the full documentation of ANYTHING-ATTRIBUTE (a symbol)."
3710 (interactive (list (intern
3712 "Describe anything attribute: "
3713 (mapcar 'symbol-name anything-additional-attributes
)))))
3714 (with-output-to-temp-buffer "*Help*"
3715 (princ (get anything-attribute
'anything-attrdoc
))))
3717 (anything-document-attribute 'name
"mandatory"
3718 " The name of the source. It is also the heading which appears
3719 above the list of matches from the source. Must be unique. ")
3720 (anything-document-attribute 'header-name
"optional"
3721 " A function returning the display string of the header. Its
3722 argument is the name of the source. This attribute is useful to
3723 add an additional information with the source name. ")
3724 (anything-document-attribute 'candidates
"mandatory if candidates-in-buffer attribute is not provided"
3725 " Specifies how to retrieve candidates from the source. It can
3726 either be a variable name, a function called with no parameters
3727 or the actual list of candidates.
3729 The list must be a list whose members are strings, symbols
3730 or (DISPLAY . REAL) pairs.
3732 In case of (DISPLAY . REAL) pairs, the DISPLAY string is shown
3733 in the Anything buffer, but the REAL one is used as action
3734 argument when the candidate is selected. This allows a more
3735 readable presentation for candidates which would otherwise be,
3736 for example, too long or have a common part shared with other
3737 candidates which can be safely replaced with an abbreviated
3738 string for display purposes.
3740 Note that if the (DISPLAY . REAL) form is used then pattern
3741 matching is done on the displayed string, not on the real
3744 If the candidates have to be retrieved asynchronously (for
3745 example, by an external command which takes a while to run)
3746 then the function should start the external command
3747 asynchronously and return the associated process object.
3748 Anything will take care of managing the process (receiving the
3749 output from it, killing it if necessary, etc.). The process
3750 should return candidates matching the current pattern (see
3751 variable `anything-pattern'.)
3753 Note that currently results from asynchronous sources appear
3754 last in the anything buffer regardless of their position in
3755 `anything-sources'. ")
3756 (anything-document-attribute 'action
"mandatory if type attribute is not provided"
3757 " It is a list of (DISPLAY . FUNCTION) pairs or FUNCTION.
3758 FUNCTION is called with one parameter: the selected candidate.
3760 An action other than the default can be chosen from this list
3761 of actions for the currently selected candidate (by default
3762 with TAB). The DISPLAY string is shown in the completions
3763 buffer and the FUNCTION is invoked when an action is
3764 selected. The first action of the list is the default. ")
3765 (anything-document-attribute 'type
"optional if action attribute is provided"
3766 " Indicates the type of the items the source returns.
3768 Merge attributes not specified in the source itself from
3769 `anything-type-attributes'.
3771 This attribute is implemented by plug-in. ")
3772 (anything-document-attribute 'init
"optional"
3773 " Function called with no parameters when anything is started. It
3774 is useful for collecting current state information which can be
3775 used to create the list of candidates later.
3777 For example, if a source needs to work with the current
3778 directory then it can store its value here, because later
3779 anything does its job in the minibuffer and in the
3780 `anything-buffer' and the current directory can be different
3782 (anything-document-attribute 'delayed-init
"optional"
3783 " Function called with no parameters before candidate function is
3784 called. It is similar with `init' attribute, but its
3785 evaluation is deferred. It is useful to combine with ")
3786 (anything-document-attribute 'match
"optional"
3787 " List of functions called with one parameter: a candidate. The
3788 function should return non-nil if the candidate matches the
3789 current pattern (see variable `anything-pattern').
3791 This attribute allows the source to override the default
3792 pattern matching based on `string-match'. It can be used, for
3793 example, to implement a source for file names and do the
3794 pattern matching on the basename of files, since it's more
3795 likely one is typing part of the basename when searching for a
3796 file, instead of some string anywhere else in its path.
3798 If the list contains more than one function then the list of
3799 matching candidates from the source is constructed by appending
3800 the results after invoking the first function on all the
3801 potential candidates, then the next function, and so on. The
3802 matching candidates supplied by the first function appear first
3803 in the list of results and then results from the other
3804 functions, respectively.
3806 This attribute has no effect for asynchronous sources (see
3807 attribute `candidates'), since they perform pattern matching
3809 (anything-document-attribute 'candidate-transformer
"optional"
3810 " It's a function or a list of functions called with one argument
3811 when the completion list from the source is built. The argument
3812 is the list of candidates retrieved from the source. The
3813 function should return a transformed list of candidates which
3814 will be used for the actual completion. If it is a list of
3815 functions, it calls each function sequentially.
3817 This can be used to transform or remove items from the list of
3820 Note that `candidates' is run already, so the given transformer
3821 function should also be able to handle candidates with (DISPLAY
3823 (anything-document-attribute 'filtered-candidate-transformer
"optional"
3824 " It has the same format as `candidate-transformer', except the
3825 function is called with two parameters: the candidate list and
3828 This transformer is run on the candidate list which is already
3829 filtered by the current pattern. While `candidate-transformer'
3830 is run only once, it is run every time the input pattern is
3833 It can be used to transform the candidate list dynamically, for
3834 example, based on the current pattern.
3836 In some cases it may also be more efficent to perform candidate
3837 transformation here, instead of with `candidate-transformer'
3838 even if this transformation is done every time the pattern is
3839 changed. For example, if a candidate set is very large then
3840 `candidate-transformer' transforms every candidate while only
3841 some of them will actually be dislpayed due to the limit
3842 imposed by `anything-candidate-number-limit'.
3844 Note that `candidates' and `candidate-transformer' is run
3845 already, so the given transformer function should also be able
3846 to handle candidates with (DISPLAY . REAL) format.
3848 This option has no effect for asynchronous sources. (Not yet,
3850 (anything-document-attribute 'action-transformer
"optional"
3851 " It's a function or a list of functions called with two
3852 arguments when the action list from the source is
3853 assembled. The first argument is the list of actions, the
3854 second is the current selection. If it is a list of functions,
3855 it calls each function sequentially.
3857 The function should return a transformed action list.
3859 This can be used to customize the list of actions based on the
3860 currently selected candidate. ")
3861 (anything-document-attribute 'pattern-transformer
"optional"
3862 " It's a function or a list of functions called with one argument
3863 before computing matches. Its argument is `anything-pattern'.
3864 Functions should return transformed `anything-pattern'.
3866 It is useful to change interpretation of `anything-pattern'. ")
3867 (anything-document-attribute 'delayed
"optional"
3868 " Candidates from the source are shown only if the user stops
3869 typing and is idle for `anything-idle-delay' seconds. ")
3870 (anything-document-attribute 'volatile
"optional"
3871 " Indicates the source assembles the candidate list dynamically,
3872 so it shouldn't be cached within a single Anything
3873 invocation. It is only applicable to synchronous sources,
3874 because asynchronous sources are not cached. ")
3875 (anything-document-attribute 'requires-pattern
"optional"
3876 " If present matches from the source are shown only if the
3877 pattern is not empty. Optionally, it can have an integer
3878 parameter specifying the required length of input which is
3879 useful in case of sources with lots of candidates. ")
3880 (anything-document-attribute 'persistent-action
"optional"
3881 " Function called with one parameter; the selected candidate.
3883 An action performed by `anything-execute-persistent-action'.
3884 If none, use the default action. ")
3885 (anything-document-attribute 'candidates-in-buffer
"optional"
3886 " Shortcut attribute for making and narrowing candidates using
3887 buffers. This newly-introduced attribute prevents us from
3888 forgetting to add volatile and match attributes.
3890 See docstring of `anything-candidates-in-buffer'.
3892 (candidates-in-buffer) is equivalent of three attributes:
3893 (candidates . anything-candidates-in-buffer)
3897 (candidates-in-buffer . candidates-function) is equivalent of:
3898 (candidates . candidates-function)
3902 This attribute is implemented by plug-in. ")
3903 (anything-document-attribute 'search
"optional"
3904 " List of functions like `re-search-forward' or `search-forward'.
3905 Buffer search function used by `anything-candidates-in-buffer'.
3906 By default, `anything-candidates-in-buffer' uses `re-search-forward'.
3907 This attribute is meant to be used with
3908 (candidates . anything-candidates-in-buffer) or
3909 (candidates-in-buffer) in short. ")
3910 (anything-document-attribute 'search-from-end
"optional"
3911 " Make `anything-candidates-in-buffer' search from the end of buffer.
3912 If this attribute is specified, `anything-candidates-in-buffer' uses
3913 `re-search-backward' instead. ")
3914 (anything-document-attribute 'get-line
"optional"
3915 " A function like `buffer-substring-no-properties' or `buffer-substring'.
3916 This function converts point of line-beginning and point of line-end,
3917 which represents a candidate computed by `anything-candidates-in-buffer'.
3918 By default, `anything-candidates-in-buffer' uses
3919 `buffer-substring-no-properties'. ")
3920 (anything-document-attribute 'display-to-real
"optional"
3921 " Function called with one parameter; the selected candidate.
3923 The function transforms the selected candidate, and the result
3924 is passed to the action function. The display-to-real
3925 attribute provides another way to pass other string than one
3926 shown in Anything buffer.
3928 Traditionally, it is possible to make candidates,
3929 candidate-transformer or filtered-candidate-transformer
3930 function return a list with (DISPLAY . REAL) pairs. But if REAL
3931 can be generated from DISPLAY, display-to-real is more
3932 convenient and faster. ")
3933 (anything-document-attribute 'real-to-display
"optional"
3934 " Function called with one parameter; the selected candidate.
3936 The inverse of display-to-real attribute.
3938 The function transforms the selected candidate, which is passed
3939 to the action function, for display. The real-to-display
3940 attribute provides the other way to pass other string than one
3941 shown in Anything buffer.
3943 Traditionally, it is possible to make candidates,
3944 candidate-transformer or filtered-candidate-transformer
3945 function return a list with (DISPLAY . REAL) pairs. But if
3946 DISPLAY can be generated from REAL, real-to-display is more
3949 Note that DISPLAY parts returned from candidates /
3950 candidate-transformer are IGNORED as the name `display-to-real'
3952 (anything-document-attribute 'cleanup
"optional"
3953 " Function called with no parameters when *anything* buffer is closed. It
3954 is useful for killing unneeded candidates buffer.
3956 Note that the function is executed BEFORE performing action. ")
3957 (anything-document-attribute 'candidate-number-limit
"optional"
3958 " Override `anything-candidate-number-limit' only for this source. ")
3959 (anything-document-attribute 'accept-empty
"optional"
3960 " Pass empty string \"\" to action function. ")
3961 (anything-document-attribute 'disable-shortcuts
"optional"
3962 " Disable `anything-enable-shortcuts' in current `anything' session.
3964 This attribute is implemented by plug-in. ")
3965 (anything-document-attribute 'dummy
"optional"
3966 " Set `anything-pattern' to candidate. If this attribute is
3967 specified, The candidates attribute is ignored.
3969 This attribute is implemented by plug-in.
3970 This plug-in implies disable-shortcuts plug-in. ")
3971 (anything-document-attribute 'multiline
"optional"
3972 " Enable to selection multiline candidates. ")
3973 (anything-document-attribute 'update
"optional"
3974 " Function called with no parameters when \\<anything-map>\\[anything-force-update] is pressed. ")
3975 (anything-document-attribute 'mode-line
"optional"
3976 " source local `anything-mode-line-string'. (included in `mode-line-format')
3977 It accepts also variable/function name. ")
3978 (anything-document-attribute 'header-line
"optional"
3979 " source local `header-line-format'.
3980 It accepts also variable/function name. ")
3981 (anything-document-attribute 'resume
"optional" " Function called with no parameters when `anything-resume' is started.")
3983 ;; (@* "Bug Report")
3984 (defvar anything-maintainer-mail-address
3985 (concat "rubiki" "tch@ru" "by-lang.org"))
3986 (defvar anything-bug-report-salutation
3987 "Describe bug below, using a precise recipe.
3989 When I executed M-x ...
3991 How to send a bug report:
3992 1) Be sure to use the LATEST version of anything.el.
3993 2) Enable debugger. M-x toggle-debug-on-error or (setq debug-on-error t)
3994 3) Use Lisp version instead of compiled one: (load \"anything.el\")
3995 4) If you got an error, please paste *Backtrace* buffer.
3996 5) Type C-c C-c to send.
3997 # If you are a Japanese, please write in Japanese:-)")
3998 (defvar anything-no-dump-variables
3999 '(anything-candidate-buffer-alist
4000 anything-digit-overlays
4001 anything-help-message
4002 anything-candidate-cache
4004 "Variables not to dump in bug report.")
4006 (defun anything-dumped-variables-in-bug-report ()
4007 (let ((hash (make-hash-table)))
4008 (loop for var in
(apropos-internal "anything-" 'boundp
)
4009 for vname
= (symbol-name var
)
4010 unless
(or (string-match "-map$" vname
)
4011 (string-match "^anything-c-source-" vname
)
4012 (string-match "-hash$" vname
)
4013 (string-match "-face$" vname
)
4014 (memq var anything-no-dump-variables
))
4017 (defun anything-send-bug-report ()
4018 "Send a bug report of anything.el."
4020 (with-current-buffer (or anything-last-buffer
4022 (reporter-submit-bug-report
4023 anything-maintainer-mail-address
4025 (anything-dumped-variables-in-bug-report)
4027 anything-bug-report-salutation
)))
4029 (defun anything-send-bug-report-from-anything ()
4030 "Send a bug report of anything.el in anything session."
4032 (anything-run-after-quit 'anything-send-bug-report
))
4034 ;; (@* "Unit Tests")
4036 (defun* anything-test-candidates
(sources &optional
(input "") (compile-source-functions anything-compile-source-functions-default
))
4037 "Test helper function for anything.
4038 Given pseudo `anything-sources' and `anything-pattern', returns list like
4039 ((\"source name1\" (\"candidate1\" \"candidate2\"))
4040 (\"source name2\" (\"candidate3\" \"candidate4\")))
4042 (let ((anything-test-mode t
)
4043 anything-enable-shortcuts
4044 anything-candidate-cache
4045 (anything-compile-source-functions compile-source-functions
)
4046 anything-before-initialize-hook
4047 anything-after-initialize-hook
4048 anything-update-hook
4049 anything-test-candidate-list
)
4050 (get-buffer-create anything-buffer
)
4052 (anything-initialize-1 nil input sources
)
4054 ;; test-mode spec: select 1st candidate!
4055 (with-current-buffer anything-buffer
4057 (anything-mark-current-line))
4059 anything-test-candidate-list
4060 (anything-cleanup))))
4062 (defmacro anything-test-update
(sources pattern
)
4063 "Test helper macro for anything. It is meant for testing *anything* buffer contents."
4064 `(progn (stub anything-get-sources
=> ,sources
)
4065 (stub anything-log-run-hook
=> nil
)
4066 (stub anything-maybe-fit-frame
=> nil
)
4067 (stub run-with-idle-timer
=> nil
)
4068 (let (anything-test-mode (anything-pattern ,pattern
))
4069 (anything-update))))
4072 ;; (install-elisp "http://www.emacswiki.org/cgi-bin/wiki/download/el-expectations.el")
4073 ;; (install-elisp "http://www.emacswiki.org/cgi-bin/wiki/download/el-mock.el")
4075 (when (fboundp 'expectations
)
4077 (desc "anything-current-buffer")
4078 (expect "__a_buffer"
4079 (with-current-buffer (get-buffer-create "__a_buffer")
4080 (anything-test-candidates '(((name .
"FOO"))) "")
4082 (buffer-name anything-current-buffer
)
4083 (kill-buffer "__a_buffer")
4085 (desc "anything-buffer-file-name")
4086 (expect (regexp "/__a_file__")
4087 (with-current-buffer (get-buffer-create "__a_file__")
4088 (setq buffer-file-name
"/__a_file__")
4089 (anything-test-candidates '(((name .
"FOO"))) "")
4091 anything-buffer-file-name
4092 ;;(kill-buffer "__a_file__")
4094 (desc "anything-interpret-value")
4096 (anything-interpret-value "literal"))
4098 (anything-interpret-value (lambda () "lambda")))
4099 (expect "lambda with source name"
4100 (let ((source '((name .
"lambda with source name"))))
4101 (anything-interpret-value (lambda () anything-source-name
) source
)))
4102 (expect "function symbol"
4103 (flet ((f () "function symbol"))
4104 (anything-interpret-value 'f
)))
4105 (expect "variable symbol"
4106 (let ((v "variable symbol"))
4107 (anything-interpret-value 'v
)))
4108 (expect (error error
*)
4109 (anything-interpret-value 'unbounded-1
))
4110 (desc "anything-compile-sources")
4111 (expect '(((name .
"foo")))
4112 (anything-compile-sources '(((name .
"foo"))) nil
)
4114 (expect '(((name .
"foo") (type . test
) (action . identity
)))
4115 (let ((anything-type-attributes '((test (action . identity
)))))
4116 (anything-compile-sources '(((name .
"foo") (type . test
)))
4117 '(anything-compile-source--type))))
4118 (desc "anything-sources accepts symbols")
4119 (expect '(((name .
"foo")))
4120 (let* ((foo '((name .
"foo"))))
4121 (anything-compile-sources '(foo) nil
)))
4122 (desc "anything-get-sources action")
4123 (expect '(((name .
"Actions") (candidates . actions
)))
4124 (stub anything-action-window
=> t
)
4125 (let (anything-compiled-sources
4126 (anything-sources '(((name .
"Actions") (candidates . actions
)))))
4127 (anything-get-sources)))
4128 (desc "get-buffer-create candidates-buffer")
4129 (expect '(((name .
"many") (init . many-init
)
4130 (candidates-in-buffer . anything-candidates-in-buffer
)
4131 (candidates . anything-candidates-in-buffer
)
4132 (volatile) (match identity
)))
4133 (anything-compile-sources
4134 '(((name .
"many") (init . many-init
)
4135 (candidates-in-buffer . anything-candidates-in-buffer
)))
4136 '(anything-compile-source--candidates-in-buffer)))
4137 (expect '(((name .
"many") (init . many-init
)
4138 (candidates-in-buffer)
4139 (candidates . anything-candidates-in-buffer
)
4140 (volatile) (match identity
)))
4141 (anything-compile-sources
4142 '(((name .
"many") (init . many-init
)
4143 (candidates-in-buffer)))
4144 '(anything-compile-source--candidates-in-buffer)))
4145 (expect '(((name .
"many") (init . many-init
)
4146 (candidates-in-buffer)
4149 (candidates . anything-candidates-in-buffer
)
4150 (volatile) (match identity
)))
4151 (let ((anything-type-attributes '((test (action . identity
)))))
4152 (anything-compile-sources
4153 '(((name .
"many") (init . many-init
)
4154 (candidates-in-buffer)
4156 '(anything-compile-source--type
4157 anything-compile-source--candidates-in-buffer
))))
4159 (desc "anything-get-candidates")
4160 (expect '("foo" "bar")
4161 (anything-get-candidates '((name .
"foo") (candidates "foo" "bar"))))
4162 (expect '("FOO" "BAR")
4163 (anything-get-candidates '((name .
"foo") (candidates "foo" "bar")
4164 (candidate-transformer
4165 .
(lambda (cands) (mapcar 'upcase cands
))))))
4166 (expect '("foo" "bar")
4167 (anything-get-candidates '((name .
"foo")
4168 (candidates .
(lambda () '("foo" "bar"))))))
4169 (expect '("foo" "bar")
4170 (let ((var '("foo" "bar")))
4171 (anything-get-candidates '((name .
"foo")
4172 (candidates . var
)))))
4173 (expect (error error
*)
4174 (anything-get-candidates '((name .
"foo")
4175 (candidates .
"err"))))
4176 (expect (error error
*)
4178 (anything-get-candidates '((name .
"foo")
4179 (candidates . var
)))))
4180 (expect (error error
*)
4181 (anything-get-candidates '((name .
"foo")
4182 (candidates . unDeFined-syMbol
))))
4183 (desc "anything-compute-matches")
4184 (expect '("foo" "bar")
4185 (let ((anything-pattern ""))
4186 (anything-compute-matches '((name .
"FOO") (candidates "foo" "bar") (volatile)))))
4188 (let ((anything-pattern "oo"))
4189 (anything-compute-matches '((name .
"FOO") (candidates "foo" "bar") (volatile)))))
4191 (let ((anything-pattern "^b"))
4192 (anything-compute-matches '((name .
"FOO") (candidates "foo" "bar") (volatile)))))
4194 (let ((anything-pattern "")
4195 (anything-candidate-number-limit 2))
4196 (anything-compute-matches '((name .
"FOO") (candidates "a" "b" "c") (volatile)))))
4198 (let ((anything-pattern ".")
4199 (anything-candidate-number-limit 2))
4200 (anything-compute-matches '((name .
"FOO") (candidates "a" "b" "c") (volatile)))))
4201 (expect '("a" "b" "c")
4202 (let ((anything-pattern "")
4203 anything-candidate-number-limit
)
4204 (anything-compute-matches '((name .
"FOO") (candidates "a" "b" "c") (volatile)))))
4205 (expect '("a" "b" "c")
4206 (let ((anything-pattern "[abc]")
4207 anything-candidate-number-limit
)
4208 (anything-compute-matches '((name .
"FOO") (candidates "a" "b" "c") (volatile)))))
4209 ;; using anything-test-candidate-list
4210 (desc "anything-test-candidates")
4211 (expect '(("FOO" ("foo" "bar")))
4212 (anything-test-candidates '(((name .
"FOO") (candidates "foo" "bar")))))
4213 (expect '(("FOO" ("bar")))
4214 (anything-test-candidates '(((name .
"FOO") (candidates "foo" "bar"))) "ar"))
4215 (expect '(("T1" ("hoge" "aiue"))
4216 ("T2" ("test" "boke")))
4217 (anything-test-candidates '(((name .
"T1") (candidates "hoge" "aiue"))
4218 ((name .
"T2") (candidates "test" "boke")))))
4219 (expect '(("T1" ("hoge"))
4221 (anything-test-candidates '(((name .
"T1") (candidates "hoge" "aiue"))
4222 ((name .
"T2") (candidates "test" "boke"))) "o"))
4223 (desc "requires-pattern attribute")
4225 (anything-test-candidates '(((name .
"FOO") (candidates "foo" "bar")
4226 (requires-pattern .
1)))))
4227 (expect '(("FOO" ("bar")))
4228 (anything-test-candidates '(((name .
"FOO") (candidates "foo" "bar")
4229 (requires-pattern .
1))) "b"))
4230 (desc "delayed attribute(for test)")
4231 (expect '(("T2" ("boke"))
4233 (anything-test-candidates
4234 '(((name .
"T1") (candidates "hoge" "aiue") (delayed))
4235 ((name .
"T2") (candidates "test" "boke")))
4237 (desc "match attribute(prefix search)")
4238 (expect '(("FOO" ("bar")))
4239 (anything-test-candidates
4240 '(((name .
"FOO") (candidates "foo" "bar")
4241 (match (lambda (c) (string-match (concat "^" anything-pattern
) c
)))))
4244 (anything-test-candidates
4245 '(((name .
"FOO") (candidates "foo" "bar")
4246 (match (lambda (c) (string-match (concat "^" anything-pattern
) c
)))))
4248 (expect "TestSource"
4250 (anything-test-candidates
4251 '(((name .
"TestSource") (candidates "a")
4252 (match (lambda (c) (setq x anything-source-name
)))))
4255 (desc "init attribute")
4256 (expect '(("FOO" ("bar")))
4258 (anything-test-candidates
4259 '(((name .
"FOO") (init .
(lambda () (setq v
'("foo" "bar"))))
4262 (desc "candidate-transformer attribute")
4263 (expect '(("FOO" ("BAR")))
4264 (anything-test-candidates '(((name .
"FOO") (candidates "foo" "bar")
4265 (candidate-transformer
4266 .
(lambda (cands) (mapcar 'upcase cands
)))))
4268 (desc "filtered-candidate-transformer attribute")
4270 (expect '(("FOO" ("BAR")))
4271 (anything-test-candidates '(((name .
"FOO") (candidates "foo" "bar")
4272 (filtered-candidate-transformer
4273 .
(lambda (cands src
) (mapcar 'upcase cands
)))))
4275 (desc "anything-candidates-in-buffer-1")
4277 (anything-candidates-in-buffer-1 nil
))
4278 (expect '("foo+" "bar+" "baz+")
4280 (insert "foo+\nbar+\nbaz+\n")
4281 (let ((anything-candidate-number-limit 5))
4282 (anything-candidates-in-buffer-1 (current-buffer) ""))))
4283 (expect '("foo+" "bar+")
4285 (insert "foo+\nbar+\nbaz+\n")
4286 (let ((anything-candidate-number-limit 2))
4287 (anything-candidates-in-buffer-1 (current-buffer) ""))))
4290 (insert "foo+\nbar+\nbaz+\n")
4291 (anything-candidates-in-buffer-1 (current-buffer) "oo\\+")))
4294 (insert "foo+\nbar+\nbaz+\n")
4295 (anything-candidates-in-buffer-1
4296 (current-buffer) "oo+"
4297 #'buffer-substring-no-properties
'(search-forward))))
4298 (expect '(("foo+" "FOO+"))
4300 (insert "foo+\nbar+\nbaz+\n")
4301 (anything-candidates-in-buffer-1
4302 (current-buffer) "oo\\+"
4304 (let ((l (buffer-substring-no-properties s e
)))
4305 (list l
(upcase l
)))))))
4306 (desc "anything-candidates-in-buffer")
4307 (expect '(("TEST" ("foo+" "bar+" "baz+")))
4308 (anything-test-candidates
4311 .
(lambda () (with-current-buffer (anything-candidate-buffer 'global
)
4312 (insert "foo+\nbar+\nbaz+\n"))))
4313 (candidates . anything-candidates-in-buffer
)
4316 (expect '(("TEST" ("foo+" "bar+" "baz+")))
4317 (let (anything-candidate-number-limit)
4318 (anything-test-candidates
4321 .
(lambda () (with-current-buffer (anything-candidate-buffer 'global
)
4322 (insert "foo+\nbar+\nbaz+\n"))))
4323 (candidates . anything-candidates-in-buffer
)
4326 (expect '(("TEST" ("foo+")))
4327 (anything-test-candidates
4330 .
(lambda () (with-current-buffer (anything-candidate-buffer 'global
)
4331 (insert "foo+\nbar+\nbaz+\n"))))
4332 (candidates . anything-candidates-in-buffer
)
4336 (desc "search attribute")
4337 (expect '(("TEST" ("foo+")))
4338 (anything-test-candidates
4341 .
(lambda () (with-current-buffer (anything-candidate-buffer 'global
)
4342 (insert "foo+\nbar+\nbaz+\nooo\n"))))
4343 (search search-forward
)
4344 (candidates . anything-candidates-in-buffer
)
4348 (expect '(("TEST" ("foo+" "ooo")))
4349 (anything-test-candidates
4352 .
(lambda () (with-current-buffer (anything-candidate-buffer 'global
)
4353 (insert "foo+\nbar+\nbaz+\nooo\n"))))
4354 (search search-forward re-search-forward
)
4355 (candidates . anything-candidates-in-buffer
)
4359 (expect '(("TEST" ("foo+" "ooo")))
4360 (anything-test-candidates
4363 .
(lambda () (with-current-buffer (anything-candidate-buffer 'global
)
4364 (insert "foo+\nbar+\nbaz+\nooo\n"))))
4365 (search re-search-forward search-forward
)
4366 (candidates . anything-candidates-in-buffer
)
4370 (expect '(("TEST" ("ooo" "foo+")))
4371 (anything-test-candidates
4374 .
(lambda () (with-current-buffer (anything-candidate-buffer 'global
)
4375 (insert "bar+\nbaz+\nooo\nfoo+\n"))))
4376 (search re-search-forward search-forward
)
4377 (candidates . anything-candidates-in-buffer
)
4381 ;; faster exact match
4382 (expect '(("TEST" ("bar+")))
4383 (anything-test-candidates
4386 .
(lambda () (with-current-buffer (anything-candidate-buffer 'global
)
4387 (insert "bar+\nbaz+\nooo\nfoo+\n"))))
4388 (search (lambda (pattern &rest _
)
4389 (and (search-forward (concat "\n" pattern
"\n") nil t
)
4390 (forward-line -
1))))
4391 (candidates . anything-candidates-in-buffer
)
4395 ;; faster prefix match
4396 (expect '(("TEST" ("bar+")))
4397 (anything-test-candidates
4400 .
(lambda () (with-current-buffer (anything-candidate-buffer 'global
)
4401 (insert "bar+\nbaz+\nooo\nfoo+\n"))))
4402 (search (lambda (pattern &rest _
)
4403 (search-forward (concat "\n" pattern
) nil t
)))
4404 (candidates . anything-candidates-in-buffer
)
4408 (desc "anything-current-buffer-is-modified")
4409 (expect '(("FOO" ("modified")))
4410 (let ((sources '(((name .
"FOO")
4413 (if (anything-current-buffer-is-modified)
4415 '("unmodified"))))))))
4417 (clrhash anything-tick-hash
)
4419 (anything-test-candidates sources
))))
4420 (expect '(("FOO" ("unmodified")))
4421 (let ((sources '(((name .
"FOO")
4424 (if (anything-current-buffer-is-modified)
4426 '("unmodified"))))))))
4428 (clrhash anything-tick-hash
)
4430 (anything-test-candidates sources
)
4431 (anything-test-candidates sources
))))
4432 (expect '(("FOO" ("modified")))
4433 (let ((sources '(((name .
"FOO")
4436 (if (anything-current-buffer-is-modified)
4438 '("unmodified"))))))))
4440 (clrhash anything-tick-hash
)
4442 (anything-test-candidates sources
)
4444 (anything-test-candidates sources
))))
4445 (expect '(("BAR" ("modified")))
4446 (let ((sources1 '(((name .
"FOO")
4449 (if (anything-current-buffer-is-modified)
4451 '("unmodified")))))))
4452 (sources2 '(((name .
"BAR")
4455 (if (anything-current-buffer-is-modified)
4457 '("unmodified"))))))))
4459 (clrhash anything-tick-hash
)
4461 (anything-test-candidates sources1
)
4462 (anything-test-candidates sources2
))))
4463 (expect '(("FOO" ("unmodified")))
4464 (let ((sources1 '(((name .
"FOO")
4467 (if (anything-current-buffer-is-modified)
4469 '("unmodified")))))))
4470 (sources2 '(((name .
"BAR")
4473 (if (anything-current-buffer-is-modified)
4475 '("unmodified"))))))))
4477 (clrhash anything-tick-hash
)
4479 (anything-test-candidates sources1
)
4480 (anything-test-candidates sources2
)
4481 (anything-test-candidates sources1
))))
4482 (expect '(("BAR" ("unmodified")))
4483 (let ((sources1 '(((name .
"FOO")
4486 (if (anything-current-buffer-is-modified)
4488 '("unmodified")))))))
4489 (sources2 '(((name .
"BAR")
4492 (if (anything-current-buffer-is-modified)
4494 '("unmodified"))))))))
4496 (clrhash anything-tick-hash
)
4498 (anything-test-candidates sources1
)
4499 (anything-test-candidates sources2
)
4500 (anything-test-candidates sources2
))))
4501 (expect '(("BAR" ("modified")))
4502 (let ((sources1 '(((name .
"FOO")
4505 (if (anything-current-buffer-is-modified)
4507 '("unmodified")))))))
4508 (sources2 '(((name .
"BAR")
4511 (if (anything-current-buffer-is-modified)
4513 '("unmodified"))))))))
4515 (clrhash anything-tick-hash
)
4517 (anything-test-candidates sources1
)
4518 (anything-test-candidates sources2
)
4520 (anything-test-candidates sources2
)))))
4521 (desc "anything-source-name")
4524 (anything-test-candidates '(((name .
"FOO")
4526 .
(lambda () (setq v anything-source-name
)))
4527 (candidates "ok"))))
4531 (anything-test-candidates '(((name .
"FOO")
4534 (setq v anything-source-name
)
4539 (anything-test-candidates '(((name .
"FOO")
4541 (candidate-transformer
4543 (setq v anything-source-name
)
4548 (anything-test-candidates '(((name .
"FOO")
4550 (filtered-candidate-transformer
4552 (setq v anything-source-name
)
4557 (anything-test-candidates '(((name .
"FOO")
4561 (setq v anything-source-name
)
4563 (action . identity
))))
4564 (anything-execute-selection-action)
4566 (desc "anything-candidate-buffer create")
4567 (expect " *anything candidates:FOO*"
4568 (let* (anything-candidate-buffer-alist
4569 (anything-source-name "FOO")
4570 (buf (anything-candidate-buffer 'global
)))
4571 (prog1 (buffer-name buf
)
4572 (kill-buffer buf
))))
4573 (expect " *anything candidates:FOO*aTestBuffer"
4574 (let* (anything-candidate-buffer-alist
4575 (anything-source-name "FOO")
4576 (anything-current-buffer (get-buffer-create "aTestBuffer"))
4577 (buf (anything-candidate-buffer 'local
)))
4578 (prog1 (buffer-name buf
)
4579 (kill-buffer anything-current-buffer
)
4580 (kill-buffer buf
))))
4582 (let (anything-candidate-buffer-alist
4583 (anything-source-name "FOO") buf
)
4584 (with-current-buffer (anything-candidate-buffer 'global
)
4586 (setq buf
(anything-candidate-buffer 'global
))
4587 (prog1 (buffer-size buf
)
4588 (kill-buffer buf
))))
4589 (desc "anything-candidate-buffer get-buffer")
4590 (expect " *anything candidates:FOO*"
4591 (let* (anything-candidate-buffer-alist
4592 (anything-source-name "FOO")
4593 (buf (anything-candidate-buffer 'global
)))
4594 (prog1 (buffer-name (anything-candidate-buffer))
4595 (kill-buffer buf
))))
4596 (expect " *anything candidates:FOO*aTestBuffer"
4597 (let* (anything-candidate-buffer-alist
4598 (anything-source-name "FOO")
4599 (anything-current-buffer (get-buffer-create "aTestBuffer"))
4600 (buf (anything-candidate-buffer 'local
)))
4601 (prog1 (buffer-name (anything-candidate-buffer))
4602 (kill-buffer anything-current-buffer
)
4603 (kill-buffer buf
))))
4604 (expect " *anything candidates:FOO*"
4605 (let* (anything-candidate-buffer-alist
4606 (anything-source-name "FOO")
4607 (buf-local (anything-candidate-buffer 'local
))
4608 (buf-global (anything-candidate-buffer 'global
)))
4609 (prog1 (buffer-name (anything-candidate-buffer))
4610 (kill-buffer buf-local
)
4611 (kill-buffer buf-global
))))
4612 (expect " *anything candidates:FOO*aTestBuffer"
4613 (let* (anything-candidate-buffer-alist
4614 (anything-source-name "FOO")
4615 (anything-current-buffer (get-buffer-create "aTestBuffer"))
4616 (buf-global (anything-candidate-buffer 'global
))
4617 (buf-local (anything-candidate-buffer 'local
)))
4618 (prog1 (buffer-name (anything-candidate-buffer))
4619 (kill-buffer buf-local
)
4620 (kill-buffer buf-global
))))
4622 (let* (anything-candidate-buffer-alist
4623 (anything-source-name "NOP__"))
4624 (anything-candidate-buffer)))
4625 (desc "anything-candidate-buffer register-buffer")
4626 (expect " *anything test candidates*"
4627 (let (anything-candidate-buffer-alist
4628 (buf (get-buffer-create " *anything test candidates*")))
4629 (with-current-buffer buf
4631 (prog1 (buffer-name (anything-candidate-buffer buf
))
4632 (kill-buffer (current-buffer))))))
4633 (expect " *anything test candidates*"
4634 (let (anything-candidate-buffer-alist
4635 (buf (get-buffer-create " *anything test candidates*")))
4636 (with-current-buffer buf
4638 (anything-candidate-buffer buf
)
4639 (prog1 (buffer-name (anything-candidate-buffer))
4640 (kill-buffer (current-buffer))))))
4642 (let (anything-candidate-buffer-alist
4643 (buf (get-buffer-create " *anything test candidates*")))
4644 (with-current-buffer buf
4646 (anything-candidate-buffer buf
)
4647 (prog1 (buffer-string)
4648 (kill-buffer (current-buffer))))))
4650 (let (anything-candidate-buffer-alist
4651 (anything-source-name "foo")
4652 (buf1 (get-buffer-create "buf1"))
4653 (buf2 (get-buffer-create "buf2")))
4654 (anything-candidate-buffer buf1
)
4655 (anything-candidate-buffer buf2
)
4656 (prog1 (buffer-name (anything-candidate-buffer buf1
))
4658 (kill-buffer buf2
))))
4659 (desc "action attribute")
4661 (anything-test-candidates
4664 (action ("identity" . identity
)))))
4665 (anything-execute-selection-action))
4667 (anything-test-candidates
4670 (action ("identity" .
(lambda (c) (identity c
)))))))
4671 (anything-execute-selection-action))
4672 (desc "anything-execute-selection-action")
4674 (anything-execute-selection-action
4675 "foo" '(("upcase" . upcase
)) nil
))
4677 (anything-execute-selection-action
4678 "foo" '(("upcase" .
(lambda (c) (upcase c
)))) nil
))
4679 (desc "display-to-real attribute")
4681 (anything-test-candidates
4684 (display-to-real . upcase
)
4685 (action ("identity" . identity
)))))
4686 (anything-execute-selection-action))
4687 (desc "cleanup test")
4690 (anything-test-candidates
4692 (cleanup .
(lambda () (setq v
'cleaned
))))))
4694 (desc "anything-get-current-source")
4695 ;; in init/candidates/action/candidate-transformer/filtered-candidate-transformer
4696 ;; display-to-real/cleanup function
4700 (anything-funcall-with-source '((name .
"FOO")) 'anything-get-current-source
)))
4704 (anything-test-candidates
4706 (init .
(lambda () (setq v
(anything-get-current-source)))))))
4707 (assoc-default 'name v
)))
4711 (anything-test-candidates
4713 (candidates .
(lambda () (setq v
(anything-get-current-source)) '("a"))))))
4714 (assoc-default 'name v
)))
4718 (anything-test-candidates
4722 .
(lambda (c) (setq v
(anything-get-current-source)) c
)))))
4723 (anything-execute-selection-action)
4724 (assoc-default 'name v
)))
4725 ;; candidate-transformer
4728 (anything-test-candidates
4731 (candidate-transformer
4732 .
(lambda (c) (setq v
(anything-get-current-source)) c
)))))
4733 (assoc-default 'name v
)))
4734 ;; filtered-candidate-transformer
4737 (anything-test-candidates
4740 (filtered-candidate-transformer
4741 .
(lambda (c s
) (setq v
(anything-get-current-source)) c
)))))
4742 (assoc-default 'name v
)))
4743 ;; action-transformer
4746 (anything-test-candidates
4750 .
(lambda (a c
) (setq v
(anything-get-current-source)) a
))
4751 (action . identity
))))
4752 (anything-execute-selection-action)
4753 (assoc-default 'name v
)))
4757 (anything-test-candidates
4759 (init .
(lambda () (with-current-buffer (anything-candidate-buffer 'global
)
4761 (candidates-in-buffer)
4763 .
(lambda (c) (setq v
(anything-get-current-source)) c
))
4764 (action . identity
))))
4765 (anything-execute-selection-action)
4766 (assoc-default 'name v
)))
4770 (anything-test-candidates
4774 .
(lambda () (setq v
(anything-get-current-source)))))))
4775 (assoc-default 'name v
)))
4776 ;; candidates are displayed
4778 (anything-test-candidates
4781 (action ("identity" . identity
)))))
4782 (assoc-default 'name
(anything-get-current-source)))
4783 (desc "anything-attr")
4785 (anything-funcall-with-source
4788 (anything-attr 'name
))))
4791 (anything-test-candidates
4794 (init .
(lambda () (setq v
(anything-attr 'hoge
))))
4799 (anything-test-candidates
4801 (init .
(lambda () (setq v
(anything-attr 'hoge
))))
4806 (anything-test-candidates
4808 (hoge) ;INCOMPATIBLE!
4809 (init .
(lambda () (setq v
(anything-attr 'hoge
))))
4812 (desc "anything-attr*")
4814 (let (v (value1 "generic"))
4815 (anything-test-candidates
4818 (init .
(lambda () (setq v
(anything-attr* 'hoge
)))))))
4820 (desc "anything-attr-defined")
4823 (anything-test-candidates
4826 (init .
(lambda () (setq v
(anything-attr-defined 'hoge
))))
4831 (anything-test-candidates
4833 (init .
(lambda () (setq v
(anything-attr-defined 'hoge
))))
4836 (desc "anything-attrset")
4837 (expect '((name .
"FOO") (hoge .
77))
4838 (let ((src '((name .
"FOO") (hoge))))
4839 (anything-attrset 'hoge
77 src
)
4842 (anything-attrset 'hoge
77 '((name .
"FOO") (hoge))))
4844 (expect '((name .
"FOO") (hoge .
77))
4845 (let ((src '((name .
"FOO") (hoge .
1))))
4846 (anything-attrset 'hoge
77 src
)
4849 (expect '((name .
"FOO") (hoge .
77) (x))
4850 (let ((src '((name .
"FOO") (x))))
4851 (anything-attrset 'hoge
77 src
)
4854 (anything-attrset 'hoge
77 '((name .
"FOO"))))
4855 (desc "anything-preselect")
4858 (with-current-buffer (anything-create-anything-buffer t
)
4859 (let ((anything-pattern "")
4860 (anything-test-mode t
))
4861 (anything-process-source '((name .
"test")
4862 (candidates "hoge" "foo" "bar")))
4863 (anything-preselect "foo")
4864 (anything-get-selection))))
4867 (with-current-buffer (anything-create-anything-buffer t
)
4868 (let ((anything-pattern "")
4869 (anything-test-mode t
))
4870 (anything-process-source '((name .
"test")
4871 (candidates "hoge" "foo" "bar")))
4872 (anything-preselect "fo+")
4873 (anything-get-selection))))
4874 ;; no match -> first entry
4876 (with-current-buffer (anything-create-anything-buffer t
)
4877 (let ((anything-pattern "")
4878 (anything-test-mode t
))
4879 (anything-process-source '((name .
"test")
4880 (candidates "hoge" "foo" "bar")))
4881 (anything-preselect "not found")
4882 (anything-get-selection))))
4883 (desc "anything-check-new-input")
4884 (expect "newpattern"
4885 (stub anything-update
)
4886 (stub anything-action-window
)
4887 (let ((anything-pattern "pattern"))
4888 (anything-check-new-input "newpattern")
4890 ;; anything-input == nil when action window is available
4892 (stub anything-update
)
4893 (stub anything-action-window
=> t
)
4894 (let ((anything-pattern "pattern")
4896 (anything-check-new-input "newpattern")
4898 ;; anything-input == anything-pattern unless action window is available
4899 (expect "newpattern"
4900 (stub anything-update
)
4901 (stub anything-action-window
=> nil
)
4902 (let ((anything-pattern "pattern")
4904 (anything-check-new-input "newpattern")
4906 (expect (mock (anything-update))
4907 (stub anything-action-window
)
4908 (let (anything-pattern)
4909 (anything-check-new-input "foo")))
4910 (desc "anything-update")
4911 (expect (mock (anything-process-source '((name .
"1"))))
4912 (anything-test-update '(((name .
"1"))) ""))
4913 ;; (find-function 'anything-update)
4914 ;; TODO el-mock.el should express 2nd call of function.
4915 ;; (expect (mock (anything-process-source '((name . "2"))))
4916 ;; (stub anything-get-sources => '(((name . "1")) ((name . "2"))))
4917 ;; (stub anything-log-run-hook)
4918 ;; (stub anything-maybe-fit-frame)
4919 ;; (stub run-with-idle-timer)
4920 ;; (anything-update))
4921 (expect (mock (run-with-idle-timer * nil
'anything-process-delayed-sources
4922 '(((name .
"2") (delayed)))))
4923 (stub anything-get-sources
=> '(((name .
"1"))
4924 ((name .
"2") (delayed))))
4925 (stub anything-log-run-hook
)
4926 (stub anything-maybe-fit-frame
)
4927 (let ((anything-pattern "") anything-test-mode
)
4930 (desc "requires-pattern attribute")
4931 (expect (not-called anything-process-source
)
4932 (anything-test-update '(((name .
"1") (requires-pattern))) ""))
4933 (expect (not-called anything-process-source
)
4934 (anything-test-update '(((name .
"1") (requires-pattern .
3))) "xx"))
4937 (expect (mock (sit-for 0.25))
4938 (stub with-current-buffer
)
4939 (let ((anything-idle-delay 1.0)
4940 (anything-input-idle-delay 0.75))
4941 (anything-process-delayed-sources t
)))
4942 (expect (mock (sit-for 0.0))
4943 (stub with-current-buffer
)
4944 (let ((anything-idle-delay 0.2)
4945 (anything-input-idle-delay 0.5))
4946 (anything-process-delayed-sources t
)))
4947 (expect (mock (sit-for 0.5))
4948 (stub with-current-buffer
)
4949 (let ((anything-idle-delay 0.5)
4950 (anything-input-idle-delay nil
))
4951 (anything-process-delayed-sources t
)))
4952 (desc "anything-normalize-sources")
4953 (expect '(anything-c-source-test)
4954 (anything-normalize-sources 'anything-c-source-test
))
4955 (expect '(anything-c-source-test)
4956 (anything-normalize-sources '(anything-c-source-test)))
4957 (expect '(anything-c-source-test)
4958 (let ((anything-sources '(anything-c-source-test)))
4959 (anything-normalize-sources nil
)))
4960 (expect '(((name .
"test")))
4961 (anything-normalize-sources '((name .
"test"))))
4962 (expect '(((name .
"test")))
4963 (anything-normalize-sources '(((name .
"test")))))
4964 (desc "anything-get-action")
4965 (expect '(("identity" . identity
))
4966 (stub buffer-size
=> 1)
4967 (stub anything-get-current-source
=> '((name .
"test")
4968 (action ("identity" . identity
))))
4969 (anything-get-action))
4970 (expect '((("identity" . identity
)) "action-transformer is called")
4971 (stub buffer-size
=> 1)
4972 (stub anything-get-current-source
4973 => '((name .
"test")
4974 (action ("identity" . identity
))
4976 .
(lambda (actions selection
)
4977 (list actions selection
)))))
4978 (stub anything-get-selection
=> "action-transformer is called")
4979 (anything-get-action))
4980 (desc "anything-select-nth-action")
4982 (stub anything-get-selection
=> "selection")
4983 (stub anything-exit-minibuffer
)
4984 (let (anything-saved-selection)
4985 (anything-select-nth-action 1)
4986 anything-saved-selection
))
4988 (stub anything-get-action
=> '(("0" . car
) ("1" . cdr
) ("2" . cadr
)))
4989 (stub anything-exit-minibuffer
)
4990 (stub anything-get-selection
=> "selection")
4991 (let (anything-saved-action)
4992 (anything-select-nth-action 2)
4993 anything-saved-action
))
4994 (desc "anything-funcall-foreach")
4995 (expect (mock (upcase "foo"))
4996 (stub anything-get-sources
=> '(((init .
(lambda () (upcase "foo"))))))
4997 (anything-funcall-foreach 'init
))
4998 (expect (mock (downcase "bar"))
4999 (stub anything-get-sources
=> '(((init .
(lambda () (upcase "foo"))))
5000 ((init .
(lambda () (downcase "bar"))))))
5001 (anything-funcall-foreach 'init
))
5002 (expect (not-called anything-funcall-with-source
)
5003 (stub anything-get-sources
=> '(((init .
(lambda () (upcase "foo"))))))
5004 (anything-funcall-foreach 'not-found
))
5005 ;; TODO anything-select-with-digit-shortcut test
5006 (desc "anything-get-cached-candidates")
5007 (expect '("cached" "version")
5008 (let ((anything-candidate-cache '(("test" "cached" "version"))))
5009 (anything-get-cached-candidates '((name .
"test")
5010 (candidates "new")))))
5012 (let ((anything-candidate-cache '(("other" "cached" "version"))))
5013 (anything-get-cached-candidates '((name .
"test")
5014 (candidates "new")))))
5015 (expect '(("test" "new")
5016 ("other" "cached" "version"))
5017 (let ((anything-candidate-cache '(("other" "cached" "version"))))
5018 (anything-get-cached-candidates '((name .
"test")
5019 (candidates "new")))
5020 anything-candidate-cache
))
5021 (expect '(("other" "cached" "version"))
5022 (let ((anything-candidate-cache '(("other" "cached" "version"))))
5023 (anything-get-cached-candidates '((name .
"test")
5026 anything-candidate-cache
))
5027 ;; TODO when candidates == process
5028 ;; TODO anything-output-filter
5029 (desc "candidate-number-limit attribute")
5031 (let ((anything-pattern "")
5032 (anything-candidate-number-limit 20))
5033 (anything-compute-matches '((name .
"FOO") (candidates "a" "b" "c")
5034 (candidate-number-limit .
2) (volatile)))))
5036 (let ((anything-pattern "[abc]")
5037 (anything-candidate-number-limit 20))
5038 (anything-compute-matches '((name .
"FOO") (candidates "a" "b" "c")
5039 (candidate-number-limit .
2) (volatile)))))
5040 (expect '("a" "b" "c" "d")
5041 (let ((anything-pattern "[abcd]")
5042 (anything-candidate-number-limit 2))
5043 (anything-compute-matches '((name .
"FOO") (candidates "a" "b" "c" "d")
5044 (candidate-number-limit) (volatile)))))
5045 (expect '(("TEST" ("a" "b" "c")))
5046 (let ((anything-candidate-number-limit 2))
5047 (anything-test-candidates
5050 .
(lambda () (with-current-buffer (anything-candidate-buffer 'global
)
5051 (insert "a\nb\nc\nd\n"))))
5052 (candidates . anything-candidates-in-buffer
)
5054 (candidate-number-limit .
3)
5056 (expect '(("TEST" ("a" "b" "c")))
5057 (let ((anything-candidate-number-limit 2))
5058 (anything-test-candidates
5061 .
(lambda () (with-current-buffer (anything-candidate-buffer 'global
)
5062 (insert "a\nb\nc\nd\n"))))
5063 (candidates . anything-candidates-in-buffer
)
5065 (candidate-number-limit .
3)
5068 (desc "multiple init")
5071 (anything-test-candidates
5073 (init (lambda () (setq a
1))
5074 (lambda () (setq b
2))))))
5078 (anything-test-candidates
5080 (init (lambda () (setq a
1))))))
5082 (desc "multiple cleanup")
5085 (anything-test-candidates
5087 (cleanup (lambda () (setq a
1))
5088 (lambda () (setq b
2))))))
5090 (desc "anything-mklist")
5092 (anything-mklist 1))
5094 (anything-mklist '(2)))
5095 (expect '((lambda ()))
5096 (anything-mklist (lambda ())))
5097 (desc "anything-before-initialize-hook")
5099 (let ((anything-before-initialize-hook '((lambda () (setq v
'called
))))
5101 (anything-initialize)
5103 (desc "anything-after-initialize-hook")
5105 (let ((anything-before-initialize-hook
5106 '((lambda () (setq v
'(a)))))
5107 (anything-after-initialize-hook
5108 '((lambda () (setq v
(cons 'b v
)))))
5110 (anything-initialize)
5113 (let ((anything-after-initialize-hook
5114 '((lambda () (setq v
(buffer-size (get-buffer anything-buffer
))))))
5116 (anything-initialize)
5118 (desc "get-line attribute")
5119 (expect '(("TEST" ("FOO+")))
5120 (anything-test-candidates
5123 .
(lambda () (with-current-buffer (anything-candidate-buffer 'global
)
5124 (insert "foo+\nbar+\nbaz+\n"))))
5125 (candidates-in-buffer)
5126 (get-line .
(lambda (s e
) (upcase (buffer-substring-no-properties s e
))))))
5128 (desc "with-anything-restore-variables")
5131 (anything-restored-variables '(a b
)))
5132 (with-anything-restore-variables
5135 (desc "anything-cleanup-hook")
5137 (let ((anything-cleanup-hook
5138 '((lambda () (setq v
'called
))))
5142 (desc "with-anything-display-same-window")
5144 (save-window-excursion
5145 (delete-other-windows)
5148 (let ((buf (get-buffer-create " tmp"))
5149 (win (selected-window)))
5150 (with-anything-display-same-window
5151 (display-buffer buf
)
5152 (eq win
(get-buffer-window buf
))))))
5154 (save-window-excursion
5155 (delete-other-windows)
5158 (let ((buf (get-buffer-create " tmp"))
5159 (win (selected-window)))
5160 (with-anything-display-same-window
5162 (eq win
(get-buffer-window buf
))))))
5164 (save-window-excursion
5165 (delete-other-windows)
5168 (let ((buf (get-buffer-create " tmp"))
5169 (win (selected-window)))
5170 (with-anything-display-same-window
5171 (switch-to-buffer buf
)
5172 (eq win
(get-buffer-window buf
))))))
5174 (save-window-excursion
5175 (delete-other-windows)
5176 (let ((buf (get-buffer-create " tmp"))
5177 (win (selected-window)))
5178 (with-anything-display-same-window
5179 (display-buffer buf
)
5180 (eq win
(get-buffer-window buf
))))))
5182 (save-window-excursion
5183 (delete-other-windows)
5184 (let ((buf (get-buffer-create " tmp"))
5185 (win (selected-window)))
5186 (with-anything-display-same-window
5188 (eq win
(get-buffer-window buf
))))))
5189 (desc "search-from-end attribute")
5190 (expect '(("TEST" ("baz+" "bar+" "foo+")))
5191 (anything-test-candidates
5194 .
(lambda () (with-current-buffer (anything-candidate-buffer 'global
)
5195 (insert "foo+\nbar+\nbaz+\n"))))
5196 (candidates-in-buffer)
5197 (search-from-end)))))
5198 (expect '(("TEST" ("baz+" "bar+" "foo+")))
5199 (anything-test-candidates
5202 .
(lambda () (with-current-buffer (anything-candidate-buffer 'global
)
5203 (insert "foo+\nbar+\nbaz+\n"))))
5204 (candidates-in-buffer)
5207 (expect '(("TEST" ("baz+" "bar+")))
5208 (anything-test-candidates
5211 .
(lambda () (with-current-buffer (anything-candidate-buffer 'global
)
5212 (insert "foo+\nbar+\nbaz+\n"))))
5213 (candidates-in-buffer)
5215 (candidate-number-limit .
2)))))
5216 (expect '(("TEST" ("baz+" "bar+")))
5217 (anything-test-candidates
5220 .
(lambda () (with-current-buffer (anything-candidate-buffer 'global
)
5221 (insert "foo+\nbar+\nbaz+\n"))))
5222 (candidates-in-buffer)
5224 (candidate-number-limit .
2)))
5227 (desc "header-name attribute")
5228 (expect "original is transformed"
5229 (anything-test-update '(((name .
"original")
5233 (format "%s is transformed" name
)))))
5235 (with-current-buffer (anything-buffer-get)
5237 (overlay-get (car (overlays-at (1+(point-min)))) 'display
)))
5238 (desc "volatile and match attribute")
5239 ;; candidates function is called once per `anything-process-delayed-sources'
5242 (anything-test-candidates '(((name .
"test")
5243 (candidates .
(lambda () (incf v
) '("ok")))
5245 (match identity identity identity
)))
5248 (desc "accept-empty attribute")
5250 (anything-test-candidates
5251 '(((name .
"test") (candidates "") (action . identity
))))
5252 (anything-execute-selection-action))
5254 (anything-test-candidates
5255 '(((name .
"test") (candidates "") (action . identity
) (accept-empty))))
5256 (anything-execute-selection-action))
5257 (desc "anything-tick-hash")
5259 (with-current-buffer (get-buffer-create " *00create+*")
5260 (puthash " *00create+*/xxx" 1 anything-tick-hash
)
5261 (kill-buffer (current-buffer)))
5262 (gethash " *00create+*/xxx" anything-tick-hash
))
5263 (desc "anything-execute-action-at-once-if-once")
5265 (let ((anything-execute-action-at-once-if-one t
))
5266 (anything '(((name .
"one test1")
5268 (action . upcase
))))))
5270 (let ((anything-execute-action-at-once-if-one t
))
5271 (anything '(((name .
"one test2")
5272 (candidates "hoge" "any")
5276 (expect (mock (read-string "word: " nil
))
5277 (let ((anything-execute-action-at-once-if-one t
))
5278 (anything '(((name .
"one test3")
5279 (candidates "hoge" "foo" "bar")
5280 (action . identity
)))
5282 (desc "anything-quit-if-no-candidate")
5284 (let ((anything-quit-if-no-candidate t
))
5285 (anything '(((name .
"zero test1") (candidates) (action . upcase
))))))
5287 (let (v (anything-quit-if-no-candidate (lambda () (setq v
'called
))))
5288 (anything '(((name .
"zero test2") (candidates) (action . upcase
))))
5290 (desc "real-to-display attribute")
5291 (expect '(("test" (("DDD" .
"ddd"))))
5292 (anything-test-candidates '(((name .
"test")
5294 (real-to-display . upcase
)
5295 (action . identity
)))))
5296 (expect '(("test" (("DDD" .
"ddd"))))
5297 (anything-test-candidates '(((name .
"test")
5298 (candidates ("ignored" .
"ddd"))
5299 (real-to-display . upcase
)
5300 (action . identity
)))))
5301 (expect '(("Commands" (("xxxhoge" .
"hoge") ("xxxboke" .
"boke"))))
5302 (anything-test-candidates '(((name .
"Commands")
5305 (real-to-display .
(lambda (x) (concat "xxx" x
)))
5306 (action . identity
)))
5308 (expect "test\nDDD\n"
5309 (anything-test-update '(((name .
"test")
5311 (real-to-display . upcase
)
5312 (action . identity
)))
5314 (with-current-buffer (anything-buffer-get) (buffer-string)))
5315 (desc "real-to-display and candidate-transformer attribute")
5316 (expect '(("test" (("DDD" .
"ddd"))))
5317 (anything-test-candidates
5320 (candidate-transformer (lambda (cands) (mapcar (lambda (c) (cons "X" c
)) cands
)))
5321 (real-to-display . upcase
)
5322 (action . identity
)))))
5323 (expect "test\nDDD\n"
5324 (anything-test-update
5327 (candidate-transformer (lambda (cands) (mapcar (lambda (c) (cons "X" c
)) cands
)))
5328 (real-to-display . upcase
)
5329 (action . identity
)))
5331 (with-current-buffer (anything-buffer-get) (buffer-string)))
5332 (desc "real-to-display and candidates-in-buffer")
5333 (expect '(("test" (("A" .
"a") ("B" .
"b"))))
5334 (anything-test-candidates
5337 .
(lambda () (with-current-buffer (anything-candidate-buffer 'global
)
5339 (insert "a\nb\n"))))
5340 (candidates-in-buffer)
5341 (real-to-display . upcase
)
5342 (action . identity
)))))
5343 (expect "test\nA\nB\n"
5348 .
(lambda () (with-current-buffer (anything-candidate-buffer 'global
)
5350 (insert "a\nb\n"))))
5351 (candidates-in-buffer)
5352 (real-to-display . upcase
)
5353 (action . identity
))))
5354 (with-current-buffer (anything-buffer-get) (buffer-string)))
5355 (desc "Symbols are acceptable as candidate.")
5356 (expect '(("test" (sym "str")))
5357 (anything-test-candidates
5359 (candidates sym
"str")))))
5360 (expect '(("test" ((sym . realsym
) ("str" .
"realstr"))))
5361 (anything-test-candidates
5363 (candidates (sym . realsym
) ("str" .
"realstr"))))))
5364 (expect '(("test" (sym)))
5365 (anything-test-candidates
5367 (candidates sym
"str")))
5369 (expect '(("test" ("str")))
5370 (anything-test-candidates
5372 (candidates sym
"str")))
5374 (expect '(("test" ((sym . realsym
))))
5375 (anything-test-candidates
5377 (candidates (sym . realsym
) ("str" .
"realstr"))))
5379 (expect '(("test" (("str" .
"realstr"))))
5380 (anything-test-candidates
5382 (candidates (sym . realsym
) ("str" .
"realstr"))))
5384 (desc "multiple transformers")
5385 (expect '(("test" ("<FOO>")))
5386 (anything-test-candidates
5389 (candidate-transformer
5391 (anything-compose (list cands
)
5392 (list (lambda (c) (mapcar 'upcase c
))
5393 (lambda (c) (list (concat "<" (car c
) ">")))))))))))
5395 (anything-composed-funcall-with-source
5397 (list (lambda (c) (mapcar 'upcase c
))
5398 (lambda (c) (list (concat "<" (car c
) ">"))))
5401 (expect '(("test" ("<FOO>")))
5402 (anything-test-candidates
5405 (candidate-transformer
5406 (lambda (c) (mapcar 'upcase c
))
5407 (lambda (c) (list (concat "<" (car c
) ">"))))))))
5408 (expect '(("test" ("<BAR>")))
5409 (anything-test-candidates
5412 (filtered-candidate-transformer
5413 (lambda (c s
) (mapcar 'upcase c
))
5414 (lambda (c s
) (list (concat "<" (car c
) ">"))))))))
5415 (expect '(("find-file" . find-file
)
5416 ("view-file" . view-file
))
5418 (stub anything-get-current-source
5419 => '((name .
"test")
5425 (list (lambda (a s
) (push '("view-file" . view-file
) a
))
5426 (lambda (a s
) (push '("find-file" . find-file
) a
))))))))
5427 (anything-get-action))
5428 (expect '(("find-file" . find-file
)
5429 ("view-file" . view-file
))
5431 (stub anything-get-current-source
5432 => '((name .
"test")
5435 (lambda (a s
) (push '("view-file" . view-file
) a
))
5436 (lambda (a s
) (push '("find-file" . find-file
) a
)))))
5437 (anything-get-action))
5438 (desc "define-anything-type-attribute")
5439 (expect '((file (action . find-file
)))
5440 (let (anything-type-attributes)
5441 (define-anything-type-attribute 'file
'((action . find-file
)))
5442 anything-type-attributes
))
5443 (expect '((file (action . find-file
)))
5444 (let ((anything-type-attributes '((file (action . view-file
)))))
5445 (define-anything-type-attribute 'file
'((action . find-file
)))
5446 anything-type-attributes
))
5447 (expect '((file (action . find-file
))
5448 (buffer (action . switch-to-buffer
)))
5449 (let (anything-type-attributes)
5450 (define-anything-type-attribute 'buffer
'((action . switch-to-buffer
)))
5451 (define-anything-type-attribute 'file
'((action . find-file
)))
5452 anything-type-attributes
))
5453 (desc "anything-approximate-candidate-number")
5456 (let ((anything-buffer (current-buffer)))
5457 (anything-approximate-candidate-number))))
5460 (let ((anything-buffer (current-buffer)))
5463 (anything-approximate-candidate-number))))
5466 (let ((anything-buffer (current-buffer)))
5470 (<= 2 (anything-approximate-candidate-number)))))
5473 (let ((anything-buffer (current-buffer)))
5475 (propertize "multi\nline\n" 'anything-multiline t
))
5476 (anything-approximate-candidate-number))))
5479 (let ((anything-buffer (current-buffer))
5480 (anything-candidate-separator "-----"))
5482 (propertize "multi\nline1\n" 'anything-multiline t
)
5484 (propertize "multi\nline2\n" 'anything-multiline t
))
5485 (<= 2 (anything-approximate-candidate-number)))))
5486 (desc "delayed-init attribute")
5489 (anything-test-candidates '(((name .
"test")
5490 (delayed-init .
(lambda () (incf value
)))
5492 (requires-pattern .
2)))
5497 (anything-test-candidates '(((name .
"test")
5498 (delayed-init .
(lambda () (incf value
)))
5500 (requires-pattern .
2)))
5505 (anything-test-candidates '(((name .
"test")
5506 (delayed-init (lambda () (incf value
))
5507 (lambda () (incf value
)))
5509 (requires-pattern .
2)))
5515 (anything-test-candidates '(((name .
"test")
5517 .
(lambda () (setq value
5518 (eq anything-current-buffer
(current-buffer)))))
5520 (requires-pattern .
2)))
5523 (desc "pattern-transformer attribute")
5524 (expect '(("test2" ("foo")) ("test3" ("bar")))
5525 (anything-test-candidates '(((name .
"test1")
5526 (candidates "foo" "bar"))
5528 (pattern-transformer .
(lambda (pat) (substring pat
1)))
5529 (candidates "foo" "bar"))
5531 (pattern-transformer .
(lambda (pat) "bar"))
5532 (candidates "foo" "bar")))
5534 (expect '(("test2" ("foo")) ("test3" ("bar")))
5535 (anything-test-candidates '(((name .
"test1")
5536 (candidates "foo" "bar"))
5538 (pattern-transformer (lambda (pat) (substring pat
1)))
5539 (candidates "foo" "bar"))
5541 (pattern-transformer (lambda (pat) "bar"))
5542 (candidates "foo" "bar")))
5544 (expect '(("test2" ("foo")) ("test3" ("bar")))
5545 (anything-test-candidates '(((name .
"test1")
5547 .
(lambda () (with-current-buffer (anything-candidate-buffer 'global
)
5548 (insert "foo\nbar\n"))))
5549 (candidates-in-buffer))
5551 (pattern-transformer .
(lambda (pat) (substring pat
1)))
5553 .
(lambda () (with-current-buffer (anything-candidate-buffer 'global
)
5554 (insert "foo\nbar\n"))))
5555 (candidates-in-buffer))
5557 (pattern-transformer .
(lambda (pat) "bar"))
5559 .
(lambda () (with-current-buffer (anything-candidate-buffer 'global
)
5560 (insert "foo\nbar\n"))))
5561 (candidates-in-buffer)))
5563 (desc "anything-recent-push")
5564 (expect '("foo" "bar" "baz")
5565 (let ((lst '("bar" "baz")))
5566 (anything-recent-push "foo" 'lst
)))
5567 (expect '("foo" "bar" "baz")
5568 (let ((lst '("foo" "bar" "baz")))
5569 (anything-recent-push "foo" 'lst
)))
5570 (expect '("foo" "bar" "baz")
5571 (let ((lst '("bar" "foo" "baz")))
5572 (anything-recent-push "foo" 'lst
)))
5573 (desc "anything-require-at-least-version")
5575 (anything-require-at-least-version "1.1"))
5577 (anything-require-at-least-version "1.200"))
5579 (anything-require-at-least-version
5580 (and (string-match "1\.\\([0-9]+\\)" anything-version
)
5581 (match-string 0 anything-version
))))
5583 (anything-require-at-least-version "1.999"))
5585 (anything-require-at-least-version "1.2000"))
5586 (desc "anything-once")
5589 (anything-test-candidates
5591 (init .
(lambda () (incf i
))))
5593 (init .
(lambda () (incf i
))))))
5597 (anything-test-candidates
5599 (init .
(lambda () (anything-once (lambda () (incf i
))))))
5601 (init .
(lambda () (anything-once (lambda () (incf i
))))))))
5605 (flet ((init1 () (anything-once (lambda () (incf i
)))))
5606 (anything-test-candidates
5612 (desc "anything-marked-candidates")
5613 (expect '("mark3" "mark1")
5614 (let* ((source '((name .
"mark test")))
5615 (anything-marked-candidates
5616 `((,source .
"mark1")
5617 (((name .
"other")) .
"mark2")
5618 (,source .
"mark3"))))
5619 (stub anything-buffer-get
=> (current-buffer))
5620 (stub anything-get-current-source
=> source
)
5621 (anything-marked-candidates)))
5622 (expect '("current")
5623 (let* ((source '((name .
"mark test")))
5624 (anything-marked-candidates nil
))
5625 (stub anything-get-current-source
=> source
)
5626 (stub anything-get-selection
=> "current")
5627 (anything-marked-candidates)))
5628 (desc "anything-let")
5629 (expect '(1 10000 nil
)
5633 (anything-buffer (exps-tmpbuf)))
5634 (anything-let ((a 1)
5637 (anything-create-anything-buffer))
5638 (with-current-buffer anything-buffer
5644 (anything-buffer (exps-tmpbuf)))
5645 (anything-let ((a 1)
5648 (anything-create-anything-buffer))
5649 (with-current-buffer anything-buffer
5650 (and (assq 'a
(buffer-local-variables))
5651 (assq 'b
(buffer-local-variables))
5652 (assq 'c
(buffer-local-variables))))))
5657 (anything-buffer (exps-tmpbuf)))
5658 (anything-let ((a 1)
5662 (desc "anything-let*")
5667 (anything-buffer (exps-tmpbuf)))
5668 (anything-let* ((a 1)
5671 (anything-create-anything-buffer))
5672 (with-current-buffer anything-buffer
5678 (anything-buffer (exps-tmpbuf)))
5679 (anything-let* ((a 1)
5682 (anything-create-anything-buffer))
5683 (with-current-buffer anything-buffer
5684 (and (assq 'a
(buffer-local-variables))
5685 (assq 'b
(buffer-local-variables))
5686 (assq 'c
(buffer-local-variables))))))
5691 (anything-buffer (exps-tmpbuf)))
5692 (anything-let* ((a 1)
5696 (desc "anything with keyw")
5697 (expect (mock (anything-internal 'test-source
"input" "prompt: " nil
"preselect" "*test*" nil
))
5698 (anything :sources
'test-source
5702 :preselect
"preselect"
5705 (expect (mock (anything-internal 'test-source nil nil nil nil
"*test*" nil
))
5706 (anything :sources
'test-source
5708 :candidate-number-limit
20))
5709 (expect (mock (anything-internal 'test-source nil nil nil nil
"*test*" nil
))
5710 (anything 'test-source nil nil nil nil
"*test*" nil
))
5715 ;; How to save (DO NOT REMOVE!!)
5716 ;; (progn (magit-push) (emacswiki-post "anything.el"))
5717 ;;; anything.el ends here