* anything.el (anything-update): execute *-preselection here. take now one new option...
[anything-config.git] / anything.el
blobd61d9af4822382563c19d0c5f726e853be77e35d
1 ;;;; anything.el --- open anything / QuickSilver-like candidate-selection framework
3 ;; Copyright (C) 2007 Tamas Patrovics
4 ;; 2008 ~ 2011 rubikitch <rubikitch@ruby-lang.org>
5 ;; 2011 Thierry Volpiatto <thierry.volpiatto@gmail.com>
7 ;; Author: Tamas Patrovics
8 ;; Maintainer: rubikitch <rubikitch@ruby-lang.org>
9 ;; Keywords: files, frames, help, matching, outlines, processes, tools, convenience, anything
10 ;; X-URL: http://repo.or.cz/w/anything-config.git
11 ;; Site: http://www.emacswiki.org/cgi-bin/emacs/Anything
13 ;; This file is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
18 ;; This file is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, USA.
29 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
31 ;;; Autodoc documentation:
32 ;; ---------------------
34 ;; * Commands defined here are:
35 ;; [EVAL] (autodoc-document-lisp-buffer :type 'command :prefix "anything-" :docstring t)
36 ;; `anything-open-last-log'
37 ;; Open anything log file of last anything session.
38 ;; `anything-resume'
39 ;; Resurrect previously invoked `anything'.
40 ;; `anything-resume-window-only'
41 ;;
42 ;; `anything-at-point'
43 ;; Call anything with symbol at point as initial input.
44 ;; `anything-force-update'
45 ;; Force recalculation and update of candidates.
46 ;; `anything-select-action'
47 ;; Select an action for the currently selected candidate.
48 ;; `anything-previous-line'
49 ;; Move selection to the previous line.
50 ;; `anything-next-line'
51 ;; Move selection to the next line.
52 ;; `anything-previous-page'
53 ;; Move selection back with a pageful.
54 ;; `anything-next-page'
55 ;; Move selection forward with a pageful.
56 ;; `anything-beginning-of-buffer'
57 ;; Move selection at the top.
58 ;; `anything-end-of-buffer'
59 ;; Move selection at the bottom.
60 ;; `anything-previous-source'
61 ;; Move selection to the previous source.
62 ;; `anything-next-source'
63 ;; Move selection to the next source.
64 ;; `anything-select-with-prefix-shortcut'
65 ;; Invoke default action with prefix shortcut.
66 ;; `anything-select-with-digit-shortcut'
67 ;; Invoke default action with digit/alphabet shortcut.
68 ;; `anything-exit-minibuffer'
69 ;; Select the current candidate by exiting the minibuffer.
70 ;; `anything-keyboard-quit'
71 ;; Quit minibuffer in anything.
72 ;; `anything-help'
73 ;; Help of `anything'.
74 ;; `anything-debug-output'
75 ;; Show all anything-related variables at this time.
76 ;; `anything-delete-current-selection'
77 ;; Delete the currently selected item.
78 ;; `anything-delete-minibuffer-contents'
79 ;; Same as `delete-minibuffer-contents' but this is a command.
80 ;; `anything-toggle-resplit-window'
81 ;; Toggle resplit anything window, vertically or horizontally.
82 ;; `anything-select-2nd-action'
83 ;; Select the 2nd action for the currently selected candidate.
84 ;; `anything-select-3rd-action'
85 ;; Select the 3rd action for the currently selected candidate.
86 ;; `anything-select-4th-action'
87 ;; Select the 4th action for the currently selected candidate.
88 ;; `anything-select-2nd-action-or-end-of-line'
89 ;; Select the 2nd action for the currently selected candidate.
90 ;; `anything-execute-persistent-action'
91 ;; Perform the associated action ATTR without quitting anything.
92 ;; `anything-scroll-other-window'
93 ;; Scroll other window (not *Anything* window) upward.
94 ;; `anything-scroll-other-window-down'
95 ;; Scroll other window (not *Anything* window) downward.
96 ;; `anything-toggle-visible-mark'
97 ;; Toggle anything visible mark at point.
98 ;; `anything-display-all-visible-marks'
99 ;; Show all `anything' visible marks strings.
100 ;; `anything-next-visible-mark'
101 ;; Move next anything visible mark.
102 ;; `anything-prev-visible-mark'
103 ;; Move previous anything visible mark.
104 ;; `anything-quit-and-find-file'
105 ;; Drop into `find-file' from `anything' like `iswitchb-find-file'.
106 ;; `anything-yank-selection'
107 ;; Set minibuffer contents to current selection.
108 ;; `anything-kill-selection-and-quit'
109 ;; Store current selection to kill ring.
110 ;; `anything-follow-mode'
111 ;; If this mode is on, persistent action is executed everytime the cursor is moved.
112 ;; `anything-migrate-sources'
113 ;; Help to migrate to new `anything' way.
114 ;; `anything-describe-anything-attribute'
115 ;; Display the full documentation of ANYTHING-ATTRIBUTE.
116 ;; `anything-send-bug-report'
117 ;; Send a bug report of anything.el.
118 ;; `anything-send-bug-report-from-anything'
119 ;; Send a bug report of anything.el in anything session.
121 ;; * Variables defined here are:
122 ;; [EVAL] (autodoc-document-lisp-buffer :type 'internal-variable :prefix "anything-" :docstring t)
123 ;; `anything-version'
124 ;; Not documented.
125 ;; `anything-sources'
126 ;; The source of candidates for anything.
127 ;; `anything-type-attributes'
128 ;; It's a list of (TYPE ATTRIBUTES ...).
129 ;; `anything-enable-shortcuts'
130 ;; *Whether to use digit/alphabet shortcut to select the first nine matches.
131 ;; `anything-shortcut-keys-alist'
132 ;; Not documented.
133 ;; `anything-display-source-at-screen-top'
134 ;; *Display candidates at the top of screen.
135 ;; `anything-candidate-number-limit'
136 ;; `anything-candidate-number-limit' variable may be overridden by SOURCE.
137 ;; `anything-idle-delay'
138 ;; *Be idle for this many seconds, before updating in delayed sources.
139 ;; `anything-input-idle-delay'
140 ;; Be idle for this many seconds, before updating.
141 ;; `anything-samewindow'
142 ;; Use current window to show the candidates.
143 ;; `anything-source-filter'
144 ;; A list of source names to be displayed.
145 ;; `anything-map'
146 ;; Keymap for anything.
147 ;; `anything-header-face'
148 ;; Face for header lines in the anything buffer.
149 ;; `anything-selection-face'
150 ;; Face for currently selected item.
151 ;; `anything-buffer'
152 ;; Buffer showing completions.
153 ;; `anything-action-buffer'
154 ;; Buffer showing actions.
155 ;; `anything-selection-overlay'
156 ;; Overlay used to highlight the currently selected item.
157 ;; `anything-digit-overlays'
158 ;; Overlays for digit shortcuts. See `anything-enable-shortcuts'.
159 ;; `anything-candidate-cache'
160 ;; Holds the available candidate withing a single anything invocation.
161 ;; `anything-pattern'
162 ;; Not documented.
163 ;; `anything-input'
164 ;; Not documented.
165 ;; `anything-async-processes'
166 ;; List of information about asynchronous processes managed by anything.
167 ;; `anything-digit-shortcut-count'
168 ;; Number of digit shortcuts shown in the anything buffer.
169 ;; `anything-before-initialize-hook'
170 ;; Run before anything initialization.
171 ;; `anything-after-initialize-hook'
172 ;; Run after anything initialization.
173 ;; `anything-update-hook'
174 ;; Run after the anything buffer was updated according the new input pattern.
175 ;; `anything-after-update-hook'
176 ;; Run after the anything buffer was updated according the new input pattern.
177 ;; `anything-cleanup-hook'
178 ;; Run after anything minibuffer is closed.
179 ;; `anything-after-action-hook'
180 ;; Run after executing action.
181 ;; `anything-after-persistent-action-hook'
182 ;; Run after executing persistent action.
183 ;; `anything-move-selection-before-hook'
184 ;; Run before moving selection in `anything-buffer'.
185 ;; `anything-move-selection-after-hook'
186 ;; Run after moving selection in `anything-buffer'.
187 ;; `anything-restored-variables'
188 ;; Variables which are restored after `anything' invocation.
189 ;; `anything-saved-selection'
190 ;; Value of the currently selected object when the action list is shown.
191 ;; `anything-candidate-separator'
192 ;; Candidates separator of `multiline' source.
193 ;; `anything-current-buffer'
194 ;; Current buffer when `anything' is invoked.
195 ;; `anything-buffer-file-name'
196 ;; `buffer-file-name' when `anything' is invoked.
197 ;; `anything-saved-action'
198 ;; Saved value of the currently selected action by key.
199 ;; `anything-last-sources'
200 ;; OBSOLETE!! Sources of previously invoked `anything'.
201 ;; `anything-saved-current-source'
202 ;; Value of the current source when the action list is shown.
203 ;; `anything-compiled-sources'
204 ;; Compiled version of `anything-sources'.
205 ;; `anything-in-persistent-action'
206 ;; Flag whether in persistent-action or not.
207 ;; `anything-quick-update'
208 ;; If non-nil, suppress displaying sources which are out of screen at first.
209 ;; `anything-last-sources-local'
210 ;; Buffer local value of `anything-sources'.
211 ;; `anything-last-buffer'
212 ;; `anything-buffer' of previously `anything' session.
213 ;; `anything-persistent-action-use-special-display'
214 ;; If non-nil, use `special-display-function' in persistent action.
215 ;; `anything-execute-action-at-once-if-one'
216 ;; Execute default action and exit when only one candidate is remaining.
217 ;; `anything-quit-if-no-candidate'
218 ;; Quit when there is no candidates when non--nil.
219 ;; `anything-scroll-amount'
220 ;; Scroll amount when scrolling other window in an anything session.
221 ;; `anything-display-function'
222 ;; Function to display *anything* buffer.
223 ;; `anything-delayed-init-executed'
224 ;; Not documented.
225 ;; `anything-mode-line-string'
226 ;; Help string displayed in mode-line in `anything'.
227 ;; `anything-help-message'
228 ;; Detailed help message string for `anything'.
229 ;; `anything-source-in-each-line-flag'
230 ;; If non-nil, add anything-source text-property in each candidate.
231 ;; `anything-debug-forms'
232 ;; Forms to show in `anything-debug-output'.
233 ;; `anything-debug'
234 ;; If non-nil, write log message into *Anything Log* buffer.
235 ;; `anything-test-candidate-list'
236 ;; Not documented.
237 ;; `anything-test-mode'
238 ;; Not documented.
239 ;; `anything-source-name'
240 ;; Not documented.
241 ;; `anything-candidate-buffer-alist'
242 ;; Not documented.
243 ;; `anything-check-minibuffer-input-timer'
244 ;; Not documented.
245 ;; `anything-match-hash'
246 ;; Not documented.
247 ;; `anything-cib-hash'
248 ;; Not documented.
249 ;; `anything-tick-hash'
250 ;; Not documented.
251 ;; `anything-issued-errors'
252 ;; Not documented.
253 ;; `anything-shortcut-keys'
254 ;; Not documented.
255 ;; `anything-once-called-functions'
256 ;; Not documented.
257 ;; `anything-follow-mode'
258 ;; If this mode is on, persistent action is executed everytime the cursor is moved.
259 ;; `anything-let-variables'
260 ;; Not documented.
261 ;; `anything-last-log-file'
262 ;; Not documented.
263 ;; `anything-compile-source-functions'
264 ;; Functions to compile elements of `anything-sources' (plug-in).
265 ;; `anything-quit'
266 ;; Not documented.
267 ;; `anything-additional-attributes'
268 ;; List of all `anything' attributes.
269 ;; `anything-buffers'
270 ;; All of `anything-buffer' in most recently used order.
271 ;; `anything-current-position'
272 ;; Not documented.
273 ;; `anything-last-frame-or-window-configuration'
274 ;; Used to store window or frame configuration when anything start.
275 ;; `anything-reading-pattern'
276 ;; Whether in `read-string' in anything or not.
277 ;; `anything-compile-source-functions-default'
278 ;; Plug-ins this file provides.
279 ;; `anything-input-local'
280 ;; Not documented.
281 ;; `anything-process-delayed-sources-timer'
282 ;; Not documented.
283 ;; `anything-mode-line-string-real'
284 ;; Not documented.
285 ;; `anything-exit-status'
286 ;; Flag to inform whether anything have aborted or quitted.
287 ;; `anything-types'
288 ;; Not documented.
289 ;; `anything-orig-enable-shortcuts'
290 ;; Not documented.
291 ;; `anything-persistent-action-display-window'
292 ;; Not documented.
293 ;; `anything-visible-mark-face'
294 ;; Not documented.
295 ;; `anything-visible-mark-overlays'
296 ;; Not documented.
297 ;; `anything-marked-candidates'
298 ;; Return marked candidates of current source if any.
299 ;; `anything-maintainer-mail-address'
300 ;; Not documented.
301 ;; `anything-bug-report-salutation'
302 ;; Not documented.
303 ;; `anything-no-dump-variables'
304 ;; Variables not to dump in bug report.
306 ;; *** END auto-documentation
308 ;;; Commentary:
311 ;; Start with M-x anything, narrow the list by typing some pattern,
312 ;; select with up/down/pgup/pgdown/C-p/C-n/C-v/M-v, choose with enter,
313 ;; left/right moves between sources. With TAB actions can be selected
314 ;; if the selected candidate has more than one possible action.
316 ;; Note that anything.el provides only the framework and some example
317 ;; configurations for demonstration purposes. See anything-config.el
318 ;; for practical, polished, easy to use configurations which can be
319 ;; used to assemble a custom personalized configuration. And many
320 ;; other configurations are in the EmacsWiki.
322 ;; http://www.emacswiki.org/cgi-bin/wiki/download/anything-config.el
323 ;; http://www.emacswiki.org/cgi-bin/emacs/AnythingSources
325 ;; Maintainer's configuration is in the EmacsWiki. It would tell you
326 ;; many tips to write smart sources!
328 ;; http://www.emacswiki.org/cgi-bin/emacs/RubikitchAnythingConfiguration
330 ;; Here is Japanese translation of `anything-sources' attributes. Thanks.
331 ;; http://d.hatena.ne.jp/sirocco634/20091012/1255336649
333 ;;; Bug Report:
335 ;; If you have problems, send a bug report via C-c C-x C-b in anything session (best)
336 ;; or M-x anything-send-bug-report outside anything session.
337 ;; I implemented bug report feature because I want to know your current state.
338 ;; It helps me to solve problems easily.
339 ;; The step is:
340 ;; 0) Setup mail in Emacs, the easiest way is:
341 ;; (setq user-mail-address "your@mail.address")
342 ;; (setq user-full-name "Your Full Name")
343 ;; (setq smtpmail-smtp-server "your.smtp.server.jp")
344 ;; (setq mail-user-agent 'message-user-agent)
345 ;; (setq message-send-mail-function 'message-smtpmail-send-it)
346 ;; 1) Be sure to use the LATEST version of anything.el.
347 ;; 2) Enable debugger. M-x toggle-debug-on-error or (setq debug-on-error t)
348 ;; 3) Use Lisp version instead of compiled one: (load "anything.el")
349 ;; 4) Do it!
350 ;; 5) If you got an error, please do not close *Backtrace* buffer.
351 ;; 6) Type C-c C-x C-b (anything session, best!)
352 ;; or M-x anything-send-bug-report (outside)
353 ;; then M-x insert-buffer *Backtrace* (if you got error)
354 ;; 7) Describe the bug using a precise recipe.
355 ;; 8) Type C-c C-c to send.
356 ;; # If you are a Japanese, please write in Japanese:-)
358 ;; You can extend `anything' by writing plug-ins. As soon as
359 ;; `anything' is invoked, `anything-sources' is compiled into basic
360 ;; attributes, then compiled one is used during invocation.
362 ;; The oldest built-in plug-in is `type' attribute: appends
363 ;; appropriate element of `anything-type-attributes'. Second built-in
364 ;; plug-in is `candidates-in-buffer': selecting a line from candidates
365 ;; buffer.
367 ;; To write a plug-in:
368 ;; 1. Define a compiler: anything-compile-source--*
369 ;; 2. Add compier function to `anything-compile-source-functions'.
370 ;; 3. (optional) Write helper functions.
372 ;; Anything plug-ins are found in the EmacsWiki.
374 ;; http://www.emacswiki.org/cgi-bin/emacs/AnythingPlugins
376 ;; Tested on Emacs 22/23/24.
379 ;; Thanks to Vagn Johansen for ideas.
380 ;; Thanks to Stefan Kamphausen for fixes and XEmacs support.
381 ;; Thanks to Tassilo Horn for fixes.
382 ;; Thanks to Drew Adams for various fixes
383 ;; Thanks to IMAKADO for candidates-in-buffer idea.
384 ;; Thanks to Tomohiro MATSUYAMA for multiline patch.
387 ;;; (@* "Index")
389 ;; If you have library `linkd.el', load
390 ;; `linkd.el' and turn on `linkd-mode' now. It lets you easily
391 ;; navigate around the sections Linkd mode will
392 ;; highlight this Index. You can get `linkd.el' here:
393 ;; http://www.emacswiki.org/cgi-bin/wiki/download/linkd.el
397 ;;; (@* "Tips")
400 ;; `anything' accepts keyword arguments. See docstring.
401 ;; [EVAL IT] (describe-function 'anything)
404 ;; `anything-enable-shortcuts' enables us to select candidate easily.
405 ;; If 'prefix then they can be selected using <prefix-key> <alnum>.
406 ;; The prefix key is `anything-select-with-prefix-shortcut'.
407 ;; If the <prefix-key> is a letter, pressing twice inputs the letter itself.
408 ;; e.g.
409 ;; (setq anything-enable-shortcuts 'prefix)
410 ;; (define-key anything-map \"@\" 'anything-select-with-prefix-shortcut)
413 ;; You can edit current selection using `anything-edit-current-selection'.
414 ;; It is useful after persistent-action.
417 ;; For `anything' users, setting `anything-sources' directly and
418 ;; invoke M-x anything is obsolete way for now. Try M-x
419 ;; `anything-migrate-sources'!
422 ;; If you want to create anything sources, yasnippet would help you.
423 ;; http://yasnippet.googlecode.com/
425 ;; Then get the snippet from
426 ;; http://www.emacswiki.org/cgi-bin/wiki/download/anything-source.yasnippet
428 ;; Put it in ~/.emacs.d/plugins/yasnippet/snippets/text-mode/emacs-lisp-mode/
432 ;; `anything-interpret-value' is useful function to interpret value
433 ;; like `candidates' attribute.
435 ;; (anything-interpret-value "literal") ; => "literal"
436 ;; (anything-interpret-value (lambda () "lambda")) ; => "lambda"
437 ;; (let ((source '((name . "lambda with source name"))))
438 ;; (anything-interpret-value
439 ;; (lambda () anything-source-name)
440 ;; source)) ; => "lambda with source name"
441 ;; (flet ((f () "function symbol"))
442 ;; (anything-interpret-value 'f)) ; => "function symbol"
443 ;; (let ((v "variable symbol"))
444 ;; (anything-interpret-value 'v)) ; => "variable symbol"
445 ;; (anything-interpret-value 'unbounded-1) ; error
448 ;; Now symbols are acceptable as candidates. So you do not have to use
449 ;; `symbol-name' function. The source is much simpler. For example,
450 ;; `apropos-internal' returns a list of symbols.
452 ;; (anything
453 ;; '(((name . "Commands")
454 ;; (candidates . (lambda () (apropos-internal anything-pattern 'commandp)))
455 ;; (volatile)
456 ;; (action . describe-function))))
459 ;; To mark a candidate, press C-SPC as normal Emacs marking. To go to
460 ;; marked candidate, press M-[ or M-].
463 ;; `anything-map' is now Emacs-standard key bindings by default.
465 ;; There are many `anything' applications, using `anything' for
466 ;; selecting candidate. In this case, if there is one candidate or no
467 ;; candidate, popping up *anything* buffer is irritating. If one
468 ;; candidate, you want to select it at once. If no candidate, you want
469 ;; to quit `anything'. Set `anything-execute-action-at-once-if-one'
470 ;; and `anything-quit-if-no-candidate' to non-nil to remedy it. Note
471 ;; that setting these variables GLOBALLY is bad idea because of
472 ;; delayed sources. These are meant to be let-binded.
473 ;; See anything-etags.el for example.
475 ;; [EVAL IT] (install-elisp "http://www.emacswiki.org/cgi-bin/wiki/download/anything-etags.el")
477 ;; ex.
478 ;; (let ((anything-execute-action-at-once-if-one t)
479 ;; (anything-quit-if-no-candidate (lambda () (message "No candidate"))))
480 ;; (anything temporary-sources input))
483 ;; `set-frame-configuration' arises flickering. If you hate
484 ;; flickering, eval:
485 ;; (setq anything-save-configuration-functions
486 ;; '(set-window-configuration . current-window-configuration))
487 ;; at the cost of restoring frame configuration (only window configuration).
490 ;; `anything-delete-current-selection' deletes the current line.
491 ;; It is useful when deleting a candidate in persistent action.
492 ;; eg. `kill-buffer'.
494 ;; [EVAL IT] (describe-function 'anything-delete-current-selection)
497 ;; `anything-attr' gets the attribute. `anything-attrset' sets the
498 ;; attribute. `anything-attr-defined' tests whether the attribute is
499 ;; defined. They handles source-local variables.
501 ;; [EVAL IT] (describe-function 'anything-attr)
502 ;; [EVAL IT] (describe-function 'anything-attrset)
503 ;; [EVAL IT] (describe-function 'anything-attr-defined)
506 ;; `anything-sources' accepts many attributes to make your life easier.
507 ;; Now `anything-sources' accepts a list of symbols.
509 ;; [EVAL IT] (describe-variable 'anything-sources)
512 ;; `anything' has optional arguments. Now you do not have to let-bind
513 ;; `anything-sources'.
515 ;; [EVAL IT] (describe-function 'anything)
518 ;; `anything-resume' resumes last `anything' session. Now you do not
519 ;; have to retype pattern.
521 ;; [EVAL IT] (describe-function 'anything-resume)
524 ;; `anything-execute-persistent-action' executes action without
525 ;; quitting `anything'. When popping up a buffer in other window by
526 ;; persistent action, you can scroll with `anything-scroll-other-window' and
527 ;; `anything-scroll-other-window-down'. See also `anything-sources' docstring.
529 ;; [EVAL IT] (describe-function 'anything-execute-persistent-action)
530 ;; [EVAL IT] (describe-variable 'anything-sources)
533 ;; `anything-select-2nd-action', `anything-select-3rd-action' and
534 ;; `anything-select-4th-action' select other than default action
535 ;; without pressing Tab.
538 ;; Using `anything-candidate-buffer' and the candidates-in-buffer
539 ;; attribute is much faster than traditional "candidates and match"
540 ;; way. And `anything-current-buffer-is-modified' avoids to
541 ;; recalculate candidates for unmodified buffer. See docstring of
542 ;; them.
544 ;; [EVAL IT] (describe-function 'anything-candidate-buffer)
545 ;; [EVAL IT] (describe-function 'anything-candidates-in-buffer)
546 ;; [EVAL IT] (describe-function 'anything-current-buffer-is-modified)
549 ;; `anything-current-buffer' and `anything-buffer-file-name' stores
550 ;; `(current-buffer)' and `buffer-file-name' in the buffer `anything'
551 ;; is invoked. Use them freely.
553 ;; [EVAL IT] (describe-variable 'anything-current-buffer)
554 ;; [EVAL IT] (describe-variable 'anything-buffer-file-name)
557 ;; `anything-completing-read' and `anything-read-file-name' are
558 ;; experimental implementation. If you are curious, type M-x
559 ;; anything-read-string-mode. It is a minor mode and toggles on/off.
562 ;; Use `anything-test-candidates' to test your handmade anything
563 ;; sources. It simulates contents of *anything* buffer with pseudo
564 ;; `anything-sources' and `anything-pattern', without side-effect. So
565 ;; you can unit-test your anything sources! Let's TDD!
567 ;; [EVAL IT] (describe-function 'anything-test-candidates)
569 ;; There are many unit-testing framework in Emacs Lisp. See the EmacsWiki.
570 ;; http://www.emacswiki.org/cgi-bin/emacs/UnitTesting
572 ;; There is an unit-test by Emacs Lisp Expectations at the tail of this file.
573 ;; http://www.emacswiki.org/cgi-bin/wiki/download/el-expectations.el
574 ;; http://www.emacswiki.org/cgi-bin/wiki/download/el-mock.el
577 ;; If you want to create anything sources, see anything-config.el.
578 ;; It is huge collection of sources. You can learn from examples.
581 ;; (@* "TODO")
583 ;; - process status indication
585 ;; - async sources doesn't honor digit-shortcut-count
587 ;; - anything-candidate-number-limit can't be nil everywhere
589 ;; (@* "HISTORY")
591 ;; Change log of this file is found at
592 ;; http://repo.or.cz/w/anything-config.git/history/master:/anything.el
594 ;; Change log of this project is found at
595 ;; http://repo.or.cz/w/anything-config.git?a=shortlog
597 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
599 ;;; Code:
601 (defvar anything-version nil)
602 (setq anything-version "1.3")
604 (require 'cl)
606 ;; (@* "User Configuration")
608 ;; [DEPRECATED]
609 ;; A default value is provided in anything-config.el
610 (defvar anything-sources nil
611 "A list of sources to use with `anything'.
612 It is deprecated, you should not use this.
613 Use instead individual sources or list of sources of your choice.")
615 ;; Default values are provided in anything-config.el.
616 (defvar anything-type-attributes nil
617 "It's a list of \(TYPE ATTRIBUTES ...\).
618 ATTRIBUTES are the same as attributes for `anything-sources'.
619 TYPE connects the value to the appropriate sources.
620 Don't set this directly, use instead `define-anything-type-attribute'.
622 This allows specifying common attributes for several sources.
623 For example, sources which provide files can specify
624 common attributes with a `file' type.")
626 (defvaralias 'anything-enable-digit-shortcuts 'anything-enable-shortcuts
627 "Same as `anything-enable-shortcuts'.
628 Alphabet shortcuts are available now in `anything-enable-shortcuts'.
629 `anything-enable-digit-shortcuts' is retained for compatibility.")
631 (defvar anything-enable-shortcuts nil
632 "*Whether to use digit/alphabet shortcut to select the first nine matches.
633 If t then they can be selected using Ctrl+<number>.
635 If 'prefix then they can be selected using <prefix-key> <alnum>.
636 The prefix key is `anything-select-with-prefix-shortcut'.
637 If the <prefix-key> is a letter, pressing twice inputs the letter itself.
638 e.g.
639 (setq anything-enable-shortcuts 'prefix)
640 (define-key anything-map \"@\" 'anything-select-with-prefix-shortcut)
642 If 'alphabet then they can be selected using Shift+<alphabet> (deprecated).
643 It is not recommended because you cannot input capital letters in pattern.
645 Keys (digit/alphabet) are listed in `anything-shortcut-keys-alist'.")
647 (defvar anything-shortcut-keys-alist
648 '((alphabet . "asdfghjklzxcvbnmqwertyuiop")
649 (prefix . "asdfghjklzxcvbnmqwertyuiop1234567890")
650 (t . "123456789")))
652 (defvar anything-display-source-at-screen-top t
653 "*Display candidates at the top of screen.
654 This happen when using `anything-next-source' and `anything-previous-source'.")
656 (defvar anything-candidate-number-limit 50
657 "*Limit candidate number globally.
658 Do not show more candidates than this limit from individual sources.
659 It is usually pointless to show hundreds of matches
660 when the pattern is empty, because it is much simpler to type a
661 few characters to narrow down the list of potential candidates.
663 Set it to nil if you don't want this limit.")
665 (defvar anything-idle-delay 0.3
666 "*Be idle for this many seconds, before updating in delayed sources.
667 This is useful for sources involving heavy operations
668 \(like launching external programs\), so that candidates
669 from the source are not retrieved unnecessarily if the user keeps typing.
671 It also can be used to declutter the results anything displays,
672 so that results from certain sources are not shown with every
673 character typed, only if the user hesitates a bit.")
675 (defvar anything-input-idle-delay 0.3
676 "Be idle for this many seconds, before updating.
678 Unlike `anything-idle-delay', it is also effective for non-delayed sources.
679 If nil, candidates are collected immediately.
681 Note: If this value is too low compared to `anything-idle-delay',
682 you may have duplicated sources when using multiples sources.
683 Safe value is always >= `anything-idle-delay'.
684 Default settings are equal value for both.")
686 (defvar anything-samewindow nil
687 "Use current window to show the candidates.
688 If t then Anything doesn't pop up a new window.")
690 (defvar anything-source-filter nil
691 "A list of source names to be displayed.
692 Other sources won't appear in the search results.
693 If nil then there is no filtering.
694 See also `anything-set-source-filter'.")
696 (defvar anything-map
697 (let ((map (copy-keymap minibuffer-local-map)))
698 (define-key map (kbd "<down>") 'anything-next-line)
699 (define-key map (kbd "<up>") 'anything-previous-line)
700 (define-key map (kbd "C-n") 'anything-next-line)
701 (define-key map (kbd "C-p") 'anything-previous-line)
702 (define-key map (kbd "<prior>") 'anything-previous-page)
703 (define-key map (kbd "<next>") 'anything-next-page)
704 (define-key map (kbd "M-v") 'anything-previous-page)
705 (define-key map (kbd "C-v") 'anything-next-page)
706 (define-key map (kbd "M-<") 'anything-beginning-of-buffer)
707 (define-key map (kbd "M->") 'anything-end-of-buffer)
708 (define-key map (kbd "C-g") 'anything-keyboard-quit)
709 (define-key map (kbd "<right>") 'anything-next-source)
710 (define-key map (kbd "<left>") 'anything-previous-source)
711 (define-key map (kbd "<RET>") 'anything-exit-minibuffer)
712 (define-key map (kbd "C-1") 'anything-select-with-digit-shortcut)
713 (define-key map (kbd "C-2") 'anything-select-with-digit-shortcut)
714 (define-key map (kbd "C-3") 'anything-select-with-digit-shortcut)
715 (define-key map (kbd "C-4") 'anything-select-with-digit-shortcut)
716 (define-key map (kbd "C-5") 'anything-select-with-digit-shortcut)
717 (define-key map (kbd "C-6") 'anything-select-with-digit-shortcut)
718 (define-key map (kbd "C-7") 'anything-select-with-digit-shortcut)
719 (define-key map (kbd "C-8") 'anything-select-with-digit-shortcut)
720 (define-key map (kbd "C-9") 'anything-select-with-digit-shortcut)
721 (loop for c from ?A to ?Z do
722 (define-key map (make-string 1 c) 'anything-select-with-digit-shortcut))
723 (define-key map (kbd "C-i") 'anything-select-action)
724 (define-key map (kbd "C-z") 'anything-execute-persistent-action)
725 (define-key map (kbd "C-e") 'anything-select-2nd-action-or-end-of-line)
726 (define-key map (kbd "C-j") 'anything-select-3rd-action)
727 (define-key map (kbd "C-o") 'anything-next-source)
728 (define-key map (kbd "C-M-v") 'anything-scroll-other-window)
729 (define-key map (kbd "M-<next>") 'anything-scroll-other-window)
730 (define-key map (kbd "C-M-y") 'anything-scroll-other-window-down)
731 (define-key map (kbd "C-M-S-v") 'anything-scroll-other-window-down)
732 (define-key map (kbd "M-<prior>") 'anything-scroll-other-window-down)
733 (define-key map (kbd "<C-M-down>") 'anything-scroll-other-window)
734 (define-key map (kbd "<C-M-up>") 'anything-scroll-other-window-down)
735 (define-key map (kbd "C-SPC") 'anything-toggle-visible-mark)
736 (define-key map (kbd "M-[") 'anything-prev-visible-mark)
737 (define-key map (kbd "M-]") 'anything-next-visible-mark)
738 (define-key map (kbd "C-k") 'anything-delete-minibuffer-contents)
740 (define-key map (kbd "C-r") 'undefined)
741 (define-key map (kbd "C-t") 'anything-toggle-resplit-window)
742 (define-key map (kbd "C-}") 'anything-narrow-window)
743 (define-key map (kbd "C-{") 'anything-enlarge-window)
744 (define-key map (kbd "C-x C-f") 'anything-quit-and-find-file)
746 (define-key map (kbd "C-c C-d") 'anything-delete-current-selection)
747 (define-key map (kbd "C-c C-y") 'anything-yank-selection)
748 (define-key map (kbd "C-c C-k") 'anything-kill-selection-and-quit)
749 (define-key map (kbd "C-c C-f") 'anything-follow-mode)
750 (define-key map (kbd "C-c C-u") 'anything-force-update)
751 (define-key map (kbd "M-p") 'previous-history-element)
752 (define-key map (kbd "M-n") 'next-history-element)
753 ;; Debugging command
754 (define-key map "\C-c\C-x\C-d" 'anything-debug-output)
755 (define-key map "\C-c\C-x\C-m" 'anything-display-all-visible-marks)
756 (define-key map "\C-c\C-x\C-b" 'anything-send-bug-report-from-anything)
757 ;; Use `describe-mode' key in `global-map'.
758 (define-key map [f1] nil) ; Allow to eval keymap whithout errors.
759 (dolist (k (where-is-internal 'describe-mode global-map))
760 (define-key map k 'anything-help))
761 map)
762 "Keymap for anything.")
764 (defgroup anything nil
765 "Open anything."
766 :prefix "anything-" :group 'convenience)
768 (defface anything-header
769 '((t (:inherit header-line)))
770 "Face for header lines in the anything buffer."
771 :group 'anything)
773 (defvar anything-header-face 'anything-header
774 "*Face for header lines in the anything buffer.")
776 (defface anything-candidate-number
777 '((t (:background "Yellow" :foreground "black")))
778 "Face for candidate number in mode-line." :group 'anything)
780 (defvar anything-selection-face 'highlight
781 "*Face for currently selected item.")
783 (defvar anything-buffer "*anything*"
784 "Buffer showing completions.")
786 (defvar anything-action-buffer "*anything action*"
787 "Buffer showing actions.")
789 (defvar anything-selection-overlay nil
790 "Overlay used to highlight the currently selected item.")
792 (defvar anything-digit-overlays nil
793 "Overlays for digit shortcuts. See `anything-enable-shortcuts'.")
795 (defvar anything-candidate-cache nil
796 "Holds the available candidate withing a single anything invocation.")
798 (defvar anything-pattern
799 "The input pattern used to update the anything buffer.")
801 (defvar anything-input
802 "The input typed in the candidates panel.")
804 (defvar anything-async-processes nil
805 "List of information about asynchronous processes managed by anything.")
807 (defvar anything-digit-shortcut-count 0
808 "Number of digit shortcuts shown in the anything buffer.")
810 (defvar anything-before-initialize-hook nil
811 "Run before anything initialization.
812 This hook is run before init functions in `anything-sources'.")
814 (defvar anything-after-initialize-hook nil
815 "Run after anything initialization.
816 Global variables are initialized and the anything buffer is created.
817 But the anything buffer has no contents.")
819 (defvar anything-update-hook nil
820 "Run after the anything buffer was updated according the new input pattern.
821 This hook is run at the beginning of buffer.
822 The first candidate is selected after running this hook.
823 See also `anything-after-update-hook'.")
825 (defvar anything-after-update-hook nil
826 "Run after the anything buffer was updated according the new input pattern.
827 This is very similar to `anything-update-hook' but selection is not moved.
828 It is useful to select a particular object instead of the first one.")
830 (defvar anything-cleanup-hook nil
831 "Run after anything minibuffer is closed.
832 IOW this hook is executed BEFORE performing action.")
834 (defvar anything-after-action-hook nil
835 "Run after executing action.")
837 (defvar anything-after-persistent-action-hook nil
838 "Run after executing persistent action.")
840 (defvar anything-move-selection-before-hook nil
841 "Run before moving selection in `anything-buffer'.")
843 (defvar anything-move-selection-after-hook nil
844 "Run after moving selection in `anything-buffer'.")
846 (defvar anything-restored-variables
847 '( anything-candidate-number-limit
848 anything-source-filter
849 anything-source-in-each-line-flag
850 anything-map
851 anything-sources)
852 "Variables which are restored after `anything' invocation.")
854 (defvar anything-saved-selection nil
855 "Value of the currently selected object when the action list is shown.")
857 (defvar anything-current-prefix-arg nil
858 "Record `current-prefix-arg' when exiting minibuffer.")
860 (defvar anything-candidate-separator
861 "--------------------"
862 "Candidates separator of `multiline' source.")
864 (defvar anything-current-buffer nil
865 "Current buffer when `anything' is invoked.")
867 (defvar anything-buffer-file-name nil
868 "Variable `buffer-file-name' when `anything' is invoked.")
870 (defvar anything-saved-action nil
871 "Saved value of the currently selected action by key.")
873 (defvar anything-last-sources nil
874 "OBSOLETE!! Sources of previously invoked `anything'.")
876 (defvar anything-saved-current-source nil
877 "Value of the current source when the action list is shown.")
879 (defvar anything-compiled-sources nil
880 "Compiled version of `anything-sources'.")
882 (defvar anything-in-persistent-action nil
883 "Flag whether in persistent-action or not.")
885 (defvar anything-quick-update nil
886 "If non-nil, suppress displaying sources which are out of screen at first.
887 They are treated as delayed sources at this input.
888 This flag makes `anything' a bit faster with many sources.")
890 (defvar anything-last-sources-local nil
891 "Buffer local value of `anything-sources'.")
893 (defvar anything-last-buffer nil
894 "`anything-buffer' of previously `anything' session.")
896 (defvar anything-save-configuration-functions
897 '(set-window-configuration . current-window-configuration)
898 "The functions used to restore/save window or frame configurations.
899 It is a pair where the car is the function to restore window or frame config,
900 and the cdr is the function to save the window or frame config.
902 If you want to save and restore frame configuration, set this variable to
903 '\(set-frame-configuration . current-frame-configuration\)
905 Older version saves/restores frame configuration, but the default is changed now
906 because flickering can occur in some environment. ")
908 (defvar anything-persistent-action-use-special-display nil
909 "If non-nil, use `special-display-function' in persistent action.")
911 (defvar anything-execute-action-at-once-if-one nil
912 "Execute default action and exit when only one candidate is remaining.
913 It is useful for `anything' applications.")
915 (defvar anything-quit-if-no-candidate nil
916 "Quit when there is no candidates when non--nil.
917 This variable accepts a function, which is executed if no candidate.
919 It is useful for `anything' applications.")
921 (defvar anything-scroll-amount nil
922 "Scroll amount when scrolling other window in an anything session.
923 It is used by `anything-scroll-other-window'
924 and `anything-scroll-other-window-down'.
926 If you prefer scrolling line by line, set this value to 1.")
928 (defvar anything-display-function 'anything-default-display-buffer
929 "Function to display *anything* buffer.
930 It is `anything-default-display-buffer' by default,
931 which affects `anything-samewindow'.")
933 (defvar anything-delayed-init-executed nil)
935 (defvar anything-mode-line-string "\\<anything-map>\\[anything-help]:help \
936 \\[anything-select-action]:Acts \
937 \\[anything-exit-minibuffer]/\\[anything-select-2nd-action-or-end-of-line]/\
938 \\[anything-select-3rd-action]:NthAct \
939 \\[anything-send-bug-report-from-anything]:BugReport"
940 "Help string displayed in mode-line in `anything'.
941 It can be a string or a list of two args, in this case,
942 first arg is a string that will be used as name for candidates number,
943 second arg any string to display in mode line.
944 If nil, use default `mode-line-format'.")
946 (defvar anything-help-message
947 "\\<anything-map>The keys that are defined for `anything' are:
948 \\{anything-map}"
949 "Detailed help message string for `anything'.
950 It also accepts function or variable symbol.")
952 ;; FIXME: What is this
953 ;(put 'anything 'timid-completion 'disabled)
955 (defvar anything-source-in-each-line-flag nil
956 "Non-nil means add anything-source text-property in each candidate.
957 experimental feature.")
959 (defvaralias 'anything-debug-variables 'anything-debug-forms)
961 (defvar anything-debug-forms nil
962 "Forms to show in `anything-debug-output'.
963 Otherwise all variables started with `anything-' are shown.
964 It is useful for debug.")
966 (defvar anything-debug nil
967 "If non-nil, write log message into *Anything Log* buffer.
968 If `debug-on-error' is non-nil, write log message regardless of this variable.
969 It is disabled by default because *Anything Log* grows quickly.")
971 ;; (@* "Internal Variables")
972 (defvar anything-test-candidate-list nil)
973 (defvar anything-test-mode nil)
974 (defvar anything-source-name nil)
975 (defvar anything-candidate-buffer-alist nil)
976 (defvar anything-check-minibuffer-input-timer nil)
977 (defvar anything-match-hash (make-hash-table :test 'equal))
978 (defvar anything-cib-hash (make-hash-table :test 'equal))
979 (defvar anything-tick-hash (make-hash-table :test 'equal))
980 (defvar anything-issued-errors nil)
981 (defvar anything-shortcut-keys nil)
982 (defvar anything-once-called-functions nil)
983 (defvar anything-follow-mode nil)
984 (defvar anything-let-variables nil)
985 (defvar anything-split-window-state nil)
987 ;; (@* "Utility: logging")
988 (defun anything-log (format-string &rest args)
989 "Log message if `debug-on-error' or `anything-debug' is non-nil.
990 Messages are written to the *Anything Log* buffer.
992 Argument FORMAT-STRING is a string to use with `format'.
993 Use optional arguments ARGS like in `format'."
994 (when (or debug-on-error anything-debug)
995 (with-current-buffer (get-buffer-create "*Anything Log*")
996 (buffer-disable-undo)
997 (set (make-local-variable 'inhibit-read-only) t)
998 (goto-char (point-max))
999 (insert (let ((tm (current-time)))
1000 (format "%s.%06d (%s) %s\n"
1001 (format-time-string "%H:%M:%S" tm)
1002 (nth 2 tm)
1003 (anything-log-get-current-function)
1004 (apply #'format (cons format-string args))))))))
1006 (defmacro anything-log-eval (&rest exprs)
1007 "Write each EXPRS evaluation result to the *Anything Log* buffer."
1008 `(anything-log-eval-internal ',exprs))
1010 (defun anything-log-run-hook (hook)
1011 "Run HOOK like `run-hooks' but write these actions to anything log buffer."
1012 (anything-log "executing %s" hook)
1013 (when (boundp hook)
1014 (anything-log-eval (symbol-value hook))
1015 (anything-log-eval (default-value hook)))
1016 (run-hooks hook)
1017 (anything-log "executed %s" hook))
1019 (defun anything-log-eval-internal (exprs)
1020 "Eval EXPRS and write results to anything log buffer."
1021 (dolist (expr exprs)
1022 (condition-case err
1023 (anything-log "%S = %S" expr (eval expr))
1024 (error (anything-log "%S = ERROR!" expr)))))
1026 (defun anything-log-get-current-function ()
1027 "Get function name calling `anything-log'.
1028 The original idea is from `tramp-debug-message'."
1029 (loop with exclude-func-re = "^anything-\\(?:interpret\\|log\\|.*funcall\\)"
1030 for btn from 1 to 40 ;avoid inf-loop
1031 for btf = (second (backtrace-frame btn))
1032 for fn = (if (symbolp btf) (symbol-name btf) "")
1033 if (and (string-match "^anything" fn)
1034 (not (string-match exclude-func-re fn)))
1035 return fn))
1037 (defun anything-log-error (&rest args)
1038 "Accumulate error messages into `anything-issued-errors'.
1039 ARGS are args given to `format'."
1040 (apply 'anything-log (concat "ERROR: " (car args)) (cdr args))
1041 (let ((msg (apply 'format args)))
1042 (unless (member msg anything-issued-errors)
1043 (add-to-list 'anything-issued-errors msg))))
1045 (defvar anything-last-log-file nil)
1046 (defun anything-log-save-maybe ()
1047 "May be save log buffer to `anything-last-log-file'."
1048 (when (stringp anything-debug)
1049 (let ((logdir (expand-file-name (format-time-string "%Y%m%d")
1050 anything-debug)))
1051 (make-directory logdir t)
1052 (with-current-buffer (get-buffer-create "*Anything Log*")
1053 (write-region (point-min) (point-max)
1054 (setq anything-last-log-file
1055 (expand-file-name (format-time-string "%Y%m%d-%H%M%S")
1056 logdir))
1057 nil 'silent)
1058 (erase-buffer)))))
1060 (defun anything-open-last-log ()
1061 "Open anything log file of last anything session."
1062 (interactive)
1063 (if anything-last-log-file
1064 (view-file anything-last-log-file)
1065 (switch-to-buffer "*Anything Log*")))
1067 (defun anything-print-error-messages ()
1068 "Print error messages in `anything-issued-errors'."
1069 (message "%s" (mapconcat 'identity (reverse anything-issued-errors) "\n")))
1071 ;; (anything-log "test")
1072 ;; (switch-to-buffer-other-window "*Anything Log*")
1074 ;; (@* "Programming Tools")
1075 (defmacro anything-aif (test-form then-form &rest else-forms)
1076 "Like `if' but set the result of TEST-FORM in a temprary variable called `it'.
1077 THEN-FORM and ELSE-FORMS are then excuted just like in `if'."
1078 (declare (indent 2) (debug t))
1079 `(let ((it ,test-form))
1080 (if it ,then-form ,@else-forms)))
1082 (defun anything-mklist (obj)
1083 "If OBJ is a list \(but not lambda\), return itself.
1084 Otherwise make a list with one element."
1085 (if (and (listp obj) (not (functionp obj)))
1087 (list obj)))
1089 ;; (@* "Anything API")
1091 (defun anything-buffer-get ()
1092 "Return `anything-action-buffer' if shown otherwise `anything-buffer'."
1093 (if (anything-action-window)
1094 anything-action-buffer
1095 anything-buffer))
1097 (defun anything-window ()
1098 "Window of `anything-buffer'."
1099 (get-buffer-window (anything-buffer-get) 'visible))
1101 (defun anything-action-window ()
1102 "Window of `anything-action-buffer'."
1103 (get-buffer-window anything-action-buffer 'visible))
1105 (defmacro with-anything-window (&rest body)
1106 "Be sure BODY is excuted in the anything window."
1107 (declare (indent 0) (debug t))
1108 `(if anything-test-mode
1109 (with-current-buffer (anything-buffer-get)
1110 ,@body)
1111 (with-selected-window (anything-window)
1112 ,@body)))
1114 (defmacro with-anything-current-buffer (&rest body)
1115 "Eval BODY inside `anything-current-buffer'."
1116 (declare (indent 0) (debug t))
1117 `(with-current-buffer anything-current-buffer
1118 ,@body))
1120 (defmacro with-anything-restore-variables(&rest body)
1121 "Restore `anything-restored-variables' after executing BODY.
1122 `post-command-hook' is handled specially."
1123 (declare (indent 0) (debug t))
1124 `(let ((--orig-vars (mapcar (lambda (v)
1125 (cons v (symbol-value v)))
1126 anything-restored-variables))
1127 (--post-command-hook-pair (cons post-command-hook
1128 (default-value 'post-command-hook))))
1129 (setq post-command-hook '(t))
1130 (setq-default post-command-hook nil)
1131 (unwind-protect (progn ,@body)
1132 (loop for (var . value) in --orig-vars
1133 do (set var value))
1134 (setq post-command-hook (car --post-command-hook-pair))
1135 (setq-default post-command-hook (cdr --post-command-hook-pair))
1136 (anything-log "restore variables"))))
1138 (defun* anything-attr (attribute-name &optional (src (anything-get-current-source)))
1139 "Get the value of ATTRIBUTE-NAME of SRC.
1140 if SRC is omitted, use current source.
1141 It is useful to write your sources."
1142 (anything-aif (assq attribute-name src)
1143 (cdr it)))
1145 (defun* anything-attr* (attribute-name &optional (src (anything-get-current-source)))
1146 "Pass the value of ATTRIBUTE-NAME of SRC to `anything-interpret-value'.
1147 if SRC is omitted, use current source.
1148 It is useful to write your sources."
1149 (anything-interpret-value (anything-attr attribute-name src)))
1151 (defun* anything-attr-defined (attribute-name &optional (src (anything-get-current-source)))
1152 "Return non-nil if ATTRIBUTE-NAME of SRC is defined.
1153 if SRC is omitted, use current source.
1154 It is useful to write your sources."
1155 (and (assq attribute-name src) t))
1157 (defun* anything-attrset (attribute-name value &optional (src (anything-get-current-source)))
1158 "Set the value of ATTRIBUTE-NAME of SRC to VALUE.
1159 if SRC is omitted, use current source.
1160 It is useful to write your sources."
1161 (anything-aif (assq attribute-name src)
1162 (setcdr it value)
1163 (setcdr src (cons (cons attribute-name value) (cdr src))))
1164 value)
1166 ;; anything-set-source-filter
1168 ;; This function sets a filter for anything sources and it may be
1169 ;; called while anything is running. It can be used to toggle
1170 ;; displaying of sources dinamically. For example, additional keys
1171 ;; can be bound into `anything-map' to display only the file-related
1172 ;; results if there are too many matches from other sources and
1173 ;; you're after files only:
1175 ;; Shift+F shows only file results from some sources:
1177 ;; (define-key anything-map "F" 'anything-my-show-files-only)
1179 ;; (defun anything-my-show-files-only ()
1180 ;; (interactive)
1181 ;; (anything-set-source-filter '("File Name History"
1182 ;; "Files from Current Directory")))
1184 ;; Shift+A shows all results:
1186 ;; (define-key anything-map "A" 'anything-my-show-all)
1188 ;; (defun anything-my-show-all ()
1189 ;; (interactive)
1190 ;; (anything-set-source-filter nil))
1193 ;; Note that you have to prefix the functions with anything- prefix,
1194 ;; otherwise they won't be bound when Anything is used under
1195 ;; Iswitchb. The -my- part is added to avoid collisions with
1196 ;; existing Anything function names.
1198 (defun anything-set-source-filter (sources)
1199 "Set the value of `anything-source-filter' and update the list of results.
1200 This apply only on SOURCES."
1201 (unless (and (listp sources)
1202 (loop for name in sources always (stringp name)))
1203 (error "Invalid data in `anything-set-source-filter': %S" sources))
1204 (setq anything-source-filter sources)
1205 (anything-log-eval anything-source-filter)
1206 (anything-update))
1208 (defun anything-set-sources (sources &optional no-init no-update)
1209 "Set SOURCES during `anything' invocation.
1210 If NO-INIT is non-nil, skip executing init functions of SOURCES.
1211 If NO-UPDATE is non-nil, skip executing `anything-update'."
1212 (with-current-buffer anything-buffer
1213 (setq anything-compiled-sources nil
1214 anything-sources sources
1215 anything-last-sources-local sources)
1216 (anything-log-eval anything-compiled-sources anything-sources))
1217 (unless no-init (anything-funcall-foreach 'init))
1218 (unless no-update (anything-update)))
1220 (defvar anything-compile-source-functions
1221 '(anything-compile-source--type
1222 anything-compile-source--dummy
1223 anything-compile-source--disable-shortcuts
1224 anything-compile-source--candidates-in-buffer)
1225 "Functions to compile elements of `anything-sources' (plug-in).")
1227 (defun anything-get-sources ()
1228 "Return compiled `anything-sources', which is memoized.
1230 Attributes:
1232 - type
1233 `anything-type-attributes' are merged in.
1234 - candidates-buffer
1235 candidates, volatile and match attrubute are created."
1236 (cond
1237 ;; action
1238 ((anything-action-window)
1239 anything-sources)
1240 ;; memoized
1241 (anything-compiled-sources)
1242 ;; first time
1244 (prog1
1245 (setq anything-compiled-sources
1246 (anything-compile-sources
1247 anything-sources anything-compile-source-functions))
1248 (anything-log-eval anything-compiled-sources)))))
1250 (defun* anything-get-selection (&optional (buffer nil buffer-s) (force-display-part))
1251 "Return the currently selected item or nil.
1252 if BUFFER is nil or unspecified, use anything-buffer as default value.
1253 If FORCE-DISPLAY-PART is non-nil, return the display string."
1254 (setq buffer (if (and buffer buffer-s) buffer anything-buffer))
1255 (unless (anything-empty-buffer-p buffer)
1256 (with-current-buffer buffer
1257 (let ((selection
1258 (or (and (not force-display-part)
1259 (get-text-property (overlay-start
1260 anything-selection-overlay)
1261 'anything-realvalue))
1262 (let ((disp (buffer-substring-no-properties
1263 (overlay-start anything-selection-overlay)
1264 (1- (overlay-end anything-selection-overlay))))
1265 (source (anything-get-current-source)))
1266 (anything-aif (and (not force-display-part)
1267 (assoc-default 'display-to-real source))
1268 (anything-funcall-with-source source it disp)
1269 disp)))))
1270 (unless (equal selection "")
1271 (anything-log-eval selection)
1272 selection)))))
1274 (defun anything-get-action ()
1275 "Return the associated action for the selected candidate.
1276 It is a function symbol \(sole action\) or list
1277 of \(action-display . function\)."
1278 (unless (anything-empty-buffer-p (anything-buffer-get))
1279 (anything-aif (anything-attr 'action-transformer)
1280 (anything-composed-funcall-with-source
1281 (anything-get-current-source) it
1282 (anything-attr 'action) (anything-get-selection))
1283 (anything-attr 'action))))
1285 (defun anything-get-current-source ()
1286 "Return the source for the current selection.
1287 Use it in init, candidates, action, candidate-transformer,
1288 filtered-candidate-transformer functions."
1289 (declare (special source))
1290 ;; The name `anything-get-current-source' should be used in init function etc.
1291 (if (and (boundp 'anything-source-name) (stringp anything-source-name))
1292 source
1293 (with-current-buffer (anything-buffer-get)
1294 (or (get-text-property (point) 'anything-source)
1295 (block exit
1296 ;; This goto-char shouldn't be necessary, but point is moved to
1297 ;; point-min somewhere else which shouldn't happen.
1298 (goto-char (overlay-start anything-selection-overlay))
1299 (let* ((header-pos (or (anything-get-previous-header-pos)
1300 (anything-get-next-header-pos)))
1301 (source-name
1302 (save-excursion
1303 (unless header-pos
1304 ;(message "No candidates")
1305 (return-from exit nil))
1306 (goto-char header-pos)
1307 (anything-current-line-contents))))
1308 (some (lambda (source)
1309 (if (equal (assoc-default 'name source) source-name)
1310 source))
1311 (anything-get-sources))))))))
1313 (defun anything-buffer-is-modified (buffer)
1314 "Return non-nil when BUFFER is modified since `anything' was invoked."
1315 (let* ((b (get-buffer buffer))
1316 (key (concat (buffer-name b) "/" (anything-attr 'name)))
1317 (source-tick (or (gethash key anything-tick-hash) 0))
1318 (buffer-tick (buffer-chars-modified-tick b))
1319 (modifiedp (/= source-tick buffer-tick)))
1320 (puthash key buffer-tick anything-tick-hash)
1321 (anything-log-eval buffer modifiedp)
1322 modifiedp))
1324 (defun anything-current-buffer-is-modified ()
1325 "Check if `anything-current-buffer' is modified since `anything' was invoked."
1326 (anything-buffer-is-modified anything-current-buffer))
1328 (defvar anything-quit nil)
1329 (defun anything-run-after-quit (function &rest args)
1330 "Perform an action after quitting `anything'.
1331 The action is to call FUNCTION with arguments ARGS."
1332 (setq anything-quit t)
1333 (anything-log-eval function args)
1334 (apply 'run-with-idle-timer 0 nil function args)
1335 (anything-exit-minibuffer))
1338 (defun define-anything-type-attribute (type definition &optional doc)
1339 "Register type attribute of TYPE as DEFINITION with DOC.
1340 DOC is displayed in `anything-type-attributes' docstring.
1342 Use this function is better than setting `anything-type-attributes' directly."
1343 (loop for i in definition do
1344 ;; without `ignore-errors', error at emacs22
1345 (ignore-errors (setf i (delete nil i))))
1346 (anything-add-type-attribute type definition)
1347 (and doc (anything-document-type-attribute type doc))
1348 nil)
1350 (defvaralias 'anything-attributes 'anything-additional-attributes)
1351 (defvar anything-additional-attributes nil
1352 "List of all `anything' attributes.")
1353 (defun anything-document-attribute (attribute short-doc &optional long-doc)
1354 "Register ATTRIBUTE documentation introduced by plug-in.
1355 SHORT-DOC is displayed beside attribute name.
1356 LONG-DOC is displayed below attribute name and short documentation."
1357 (declare (indent 2) (debug t))
1358 (if long-doc
1359 (setq short-doc (concat "(" short-doc ")"))
1360 (setq long-doc short-doc
1361 short-doc ""))
1362 (add-to-list 'anything-additional-attributes attribute t)
1363 (put attribute 'anything-attrdoc
1364 (concat "- " (symbol-name attribute)
1365 " " short-doc "\n\n" long-doc "\n")))
1368 (defun anything-interpret-value (value &optional source)
1369 "Interpret VALUE as variable, function or literal.
1370 If VALUE is a function, call it with no arguments and return the value.
1371 If SOURCE is `anything' source, `anything-source-name' is source name.
1373 If VALUE is a variable, return the value.
1375 If VALUE is a symbol, but it is not a function or a variable, cause an error.
1377 Otherwise, return VALUE itself."
1378 (cond ((and source (functionp value))
1379 (anything-funcall-with-source source value))
1380 ((functionp value)
1381 (funcall value))
1382 ((and (symbolp value) (boundp value))
1383 (symbol-value value))
1384 ((symbolp value)
1385 (error "anything-interpret-value: Symbol must be a function or a variable"))
1387 value)))
1389 (defun anything-once (function &rest args)
1390 "Ensure FUNCTION with ARGS to be called once in `anything' session."
1391 (let ((spec (cons function args)))
1392 (unless (member spec anything-once-called-functions)
1393 (apply function args)
1394 (push spec anything-once-called-functions))))
1396 ;; (@* "Core: API helper")
1397 (defun* anything-empty-buffer-p (&optional (buffer anything-buffer))
1398 "Check if BUFFER have candidates.
1399 Default value for BUFFER is `anything-buffer'."
1400 (zerop (buffer-size (and buffer (get-buffer buffer)))))
1402 (defun anything-let-eval-varlist (varlist)
1403 "Return the list of pairs VARLIST with each cdr of pair evaluated.
1404 If VARLIST contain single elements, those are returned
1405 as a list of one element."
1406 (mapcar (lambda (pair)
1407 (if (listp pair)
1408 (cons (car pair) (eval (cadr pair)))
1409 (cons pair nil)))
1410 varlist))
1412 ;; [NOT USED]
1413 ;; (defun anything-let*-eval-varlist (varlist)
1414 ;; (let ((vars (mapcar (lambda (pair)
1415 ;; (or (car-safe pair) pair))
1416 ;; varlist)))
1417 ;; (eval `(let ,vars
1418 ;; ,@(mapcar (lambda (pair)
1419 ;; (if (listp pair)
1420 ;; `(setq ,(car pair) ,(cadr pair))
1421 ;; `(setq ,pair nil)))
1422 ;; varlist)
1423 ;; (mapcar (lambda (v)
1424 ;; (cons v (symbol-value v)))
1425 ;; ',vars)))))
1427 (defun anything-let-internal (binding bodyfunc)
1428 "Set BINDING to anything buffer-local variables and Evaluate BODYFUNC.
1429 BINDING is a list of \(VARNAME . VALUE\) pair."
1430 (setq anything-let-variables binding)
1431 (unwind-protect
1432 (funcall bodyfunc)
1433 (setq anything-let-variables nil)))
1436 ;; (@* "Core: tools")
1437 (defun anything-current-line-contents ()
1438 "Current line string without properties."
1439 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))
1441 (defun anything-funcall-with-source (source func &rest args)
1442 "Call from SOURCE FUNC with ARGS.
1443 FUNC can be a function list.
1444 Return the result of last function call."
1445 (let ((anything-source-name (assoc-default 'name source))
1446 result)
1447 (anything-log-eval anything-source-name func args)
1448 (dolist (func (if (functionp func) (list func) func) result)
1449 (setq result (apply func args)))))
1451 (defun anything-funcall-foreach (sym)
1452 "Call the SYM function\(s\) for each source if any."
1453 (dolist (source (anything-get-sources))
1454 (anything-aif (assoc-default sym source)
1455 (anything-funcall-with-source source it))))
1457 (defun anything-normalize-sources (sources)
1458 "If SOURCES is only one source, make a list."
1459 (cond ((or (and sources ; avoid nil
1460 (symbolp sources))
1461 (and (listp sources) (assq 'name sources)))
1462 (list sources))
1463 (sources)
1464 (t anything-sources)))
1466 (defun anything-approximate-candidate-number (&optional in-current-source)
1467 "Return approximate number of candidates in `anything-buffer'.
1468 If IN-CURRENT-SOURCE is provided return number of candidates
1469 in the source where point is.
1470 It is used to check if candidate number is 0, 1, or 2+."
1471 (with-current-buffer anything-buffer
1472 (save-excursion
1473 (if in-current-source
1474 (goto-char (anything-get-previous-header-pos))
1475 (goto-char (point-min)))
1476 (forward-line 1)
1477 (let ((count-multi 1))
1478 (if (anything-pos-multiline-p)
1479 (save-excursion
1480 (loop while (and (not (if in-current-source
1481 (save-excursion
1482 (forward-line 2)
1483 (or (anything-pos-header-line-p) (eobp)))
1484 (eobp)))
1485 (search-forward anything-candidate-separator nil t))
1486 do (incf count-multi)
1487 finally return count-multi))
1488 (save-excursion
1489 (loop with ln = 0
1490 while (not (if in-current-source
1491 (or (anything-pos-header-line-p) (eobp))
1492 (eobp)))
1493 unless (anything-pos-header-line-p)
1494 do (incf ln)
1495 do (forward-line 1) finally return ln)))))))
1497 (defmacro with-anything-quittable (&rest body)
1498 "If an error occur in execution of BODY, quit anything safely."
1499 (declare (indent 0) (debug t))
1500 `(let (inhibit-quit)
1501 (condition-case v
1502 (progn ,@body)
1503 (quit (setq anything-quit t)
1504 (exit-minibuffer)
1505 (keyboard-quit)))))
1507 (defun anything-compose (arg-lst func-lst)
1508 "Apply arguments specified in ARG-LST with each function of FUNC-LST.
1509 The result of each function will be the new `car' of ARG-LST.
1511 This function allows easy sequencing of transformer functions."
1512 (dolist (func func-lst)
1513 (setcar arg-lst (apply func arg-lst)))
1514 (car arg-lst))
1516 (defun anything-composed-funcall-with-source (source funcs &rest args)
1517 "With SOURCE apply `anything-funcall-with-source' with each FUNCS and ARGS.
1518 This is used in transformers to modify candidates list."
1519 (if (functionp funcs)
1520 (apply 'anything-funcall-with-source source funcs args)
1521 (apply 'anything-funcall-with-source source
1522 (lambda (&rest args)
1523 (anything-compose args funcs))
1524 args)))
1526 (defun anything-new-timer (variable timer)
1527 "Give VARIABLE value to TIMER and cancel old timer."
1528 (anything-aif (symbol-value variable)
1529 (cancel-timer it))
1530 (set variable timer))
1532 ;; (@* "Core: entry point")
1533 (defconst anything-argument-keys
1534 '(:sources :input :prompt :resume :preselect :buffer :keymap :default))
1536 ;;;###autoload
1537 (defun anything (&rest plist)
1538 "Main function to execute anything sources.
1539 When call interactively with no arguments deprecated `anything-sources'
1540 will be used if non--nil.
1542 PLIST is a list like \(:key1 val1 :key2 val2 ...\) or
1543 \(&optional sources input prompt resume preselect buffer keymap\).
1545 Basic keywords are the following:
1547 - :sources
1549 Temporary value of `anything-sources'. It also accepts a
1550 symbol, interpreted as a variable of an anything source. It
1551 also accepts an alist representing an anything source, which is
1552 detected by \(assq 'name ANY-SOURCES\)
1554 - :input
1556 Temporary value of `anything-pattern', ie. initial input of minibuffer.
1558 - :prompt
1560 Prompt other than \"pattern: \".
1562 - :resume
1564 If t, Resurrect previously instance of `anything'. Skip the initialization.
1565 If 'noresume, this instance of `anything' cannot be resumed.
1567 - :preselect
1569 Initially selected candidate. Specified by exact candidate or a regexp.
1570 Note that it is not working with delayed sources.
1572 - :buffer
1574 `anything-buffer' instead of *anything*.
1576 - :keymap
1578 `anything-map' for current `anything' session.
1581 Of course, conventional arguments are supported, the two are same.
1583 \(anything :sources sources :input input :prompt prompt :resume resume
1584 :preselect preselect :buffer buffer :keymap keymap\)
1585 \(anything sources input prompt resume preselect buffer keymap\)
1588 Other keywords are interpreted as local variables of this anything session.
1589 The `anything-' prefix can be omitted. For example,
1591 \(anything :sources 'anything-c-source-buffers
1592 :buffer \"*buffers*\" :candidate-number-limit 10\)
1594 means starting anything session with `anything-c-source-buffers'
1595 source in *buffers* buffer and set variable `anything-candidate-number-limit'
1596 to 10 as session local variable."
1597 (interactive)
1598 (if (keywordp (car plist))
1599 (anything-let-internal
1600 (anything-parse-keys plist)
1601 (lambda ()
1602 (apply 'anything
1603 (mapcar (lambda (key) (plist-get plist key))
1604 anything-argument-keys))))
1605 (apply 'anything-internal plist)))
1607 (defun* anything-resume (&optional
1608 (any-buffer anything-last-buffer)
1609 buffer-pattern (any-resume t))
1610 "Resurrect previously invoked `anything'.
1611 Called with a prefix arg, allow choosing among all existing
1612 anything buffers. i.e choose among various anything sessions."
1613 (interactive)
1614 (when (or current-prefix-arg buffer-pattern)
1615 (setq any-buffer (anything-resume-select-buffer buffer-pattern)))
1616 (setq anything-compiled-sources nil)
1617 (anything
1618 (or (buffer-local-value 'anything-last-sources-local (get-buffer any-buffer))
1619 anything-last-sources anything-sources)
1620 (buffer-local-value 'anything-input-local (get-buffer any-buffer))
1621 nil any-resume nil any-buffer))
1623 ;;; rubikitch: experimental
1624 ;;; I use this and check it whether I am convenient.
1625 ;;; I may introduce an option to control the behavior.
1626 (defun* anything-resume-window-only (&optional
1627 (any-buffer anything-last-buffer)
1628 buffer-pattern)
1629 (interactive)
1630 (anything-resume any-buffer buffer-pattern 'window-only))
1632 ;;;###autoload
1633 (defun anything-at-point (&optional
1634 any-sources any-input
1635 any-prompt any-resume
1636 any-preselect any-buffer)
1637 "Call anything with symbol at point as initial input.
1638 ANY-SOURCES ANY-INPUT ANY-PROMPT ANY-RESUME ANY-PRESELECT and ANY-BUFFER
1639 are same args as in `anything'."
1640 (interactive)
1641 (anything any-sources
1642 (if current-prefix-arg
1643 (concat "\\b" (thing-at-point 'symbol) "\\b"
1644 (if (featurep 'anything-match-plugin) " " ""))
1645 any-input)
1646 any-prompt any-resume any-preselect any-buffer))
1648 ;;;###autoload
1649 (defun anything-other-buffer (any-sources any-buffer)
1650 "Simplified interface of `anything' with other `anything-buffer'.
1651 Call `anything' with only ANY-SOURCES and ANY-BUFFER as args."
1652 (anything any-sources nil nil nil nil any-buffer))
1654 ;;; (@* "Core: entry point helper")
1655 (defun anything-internal (&optional
1656 any-sources any-input
1657 any-prompt any-resume
1658 any-preselect any-buffer
1659 any-keymap any-default)
1660 "The internal anything function called by `anything'.
1661 For ANY-SOURCES ANY-INPUT ANY-PROMPT ANY-RESUME ANY-PRESELECT ANY-BUFFER and
1662 ANY-KEYMAP See `anything'."
1663 (anything-log "++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
1664 (anything-log-eval any-prompt any-preselect any-buffer any-keymap any-default)
1665 (unwind-protect
1666 (condition-case v
1667 (let ( ;; It is needed because `anything-source-name' is non-nil
1668 ;; when `anything' is invoked by action. Awful global scope.
1669 anything-source-name
1670 anything-in-persistent-action
1671 anything-quit
1672 (case-fold-search t)
1673 (anything-buffer (or any-buffer anything-buffer))
1674 ;; cua-mode ; avoid error when region is selected
1676 (with-anything-restore-variables
1677 (anything-initialize any-resume any-input any-sources)
1678 (anything-display-buffer anything-buffer)
1679 (anything-log "show prompt")
1680 (unwind-protect
1681 (anything-read-pattern-maybe
1682 any-prompt any-input any-preselect any-resume any-keymap any-default)
1683 (anything-cleanup)))
1684 (prog1 (unless anything-quit (anything-execute-selection-action-1))
1685 (anything-log "end session --------------------------------------------")))
1686 (quit
1687 (anything-on-quit)
1688 (anything-log "end session (quit) -------------------------------------")
1689 nil))
1690 (anything-log-save-maybe)))
1692 (defun anything-parse-keys (keys)
1693 "Parse the KEYS arguments of `anything'."
1694 (loop for (key value &rest _) on keys by #'cddr
1695 for symname = (substring (symbol-name key) 1)
1696 for sym = (intern (if (string-match "^anything-" symname)
1697 symname
1698 (concat "anything-" symname)))
1699 unless (memq key anything-argument-keys)
1700 collect (cons sym value)))
1702 (defun anything-resume-p (any-resume)
1703 "Whether current anything session is resumed or not.
1704 Just check if ANY-RESUME value is t or window-only."
1705 (memq any-resume '(t window-only)))
1707 (defvar anything-buffers nil
1708 "All of `anything-buffer' in most recently used order.")
1709 (defun anything-initialize (any-resume any-input any-sources)
1710 "Start initialization of `anything' session.
1711 For ANY-RESUME ANY-INPUT and ANY-SOURCES See `anything'."
1712 (anything-log "start initialization: any-resume=%S any-input=%S" any-resume any-input)
1713 (anything-frame-or-window-configuration 'save)
1714 (setq anything-sources (anything-normalize-sources any-sources))
1715 (anything-log "sources = %S" anything-sources)
1716 (anything-hooks 'setup)
1717 (anything-current-position 'save)
1718 (if (anything-resume-p any-resume)
1719 (anything-initialize-overlays (anything-buffer-get))
1720 (anything-initial-setup))
1721 (unless (eq any-resume 'noresume)
1722 (anything-recent-push anything-buffer 'anything-buffers)
1723 (setq anything-last-buffer anything-buffer))
1724 (when any-input (setq anything-input any-input anything-pattern any-input))
1725 (and (anything-resume-p any-resume) (anything-funcall-foreach 'resume))
1726 (anything-log "end initialization"))
1728 (defun anything-execute-selection-action-1 ()
1729 "Execute current action."
1730 (unwind-protect
1731 (anything-execute-selection-action)
1732 (anything-aif (get-buffer anything-action-buffer)
1733 (kill-buffer it))
1734 (anything-log-run-hook 'anything-after-action-hook)))
1736 (defun anything-on-quit ()
1737 "Save `minibuffer-history' and current position."
1738 (setq minibuffer-history (cons anything-input minibuffer-history))
1739 (anything-current-position 'restore))
1741 (defun anything-resume-select-buffer (input)
1742 "Resume precedent anything session with initial input INPUT."
1743 (anything '(((name . "Resume anything buffer")
1744 (candidates . anything-buffers)
1745 (action . identity)))
1746 input nil 'noresume nil "*anything resume*"))
1748 (defun anything-recent-push (elt list-var)
1749 "Add ELT to the value of LIST-VAR as most recently used value."
1750 (let ((m (member elt (symbol-value list-var))))
1751 (and m (set list-var (delq (car m) (symbol-value list-var))))
1752 (push elt (symbol-value list-var))))
1754 ;;; (@* "Core: Accessors")
1755 ;;; rubikitch: I love to create functions to control variables.
1756 (defvar anything-current-position nil
1757 "Cons of \(point . window-start\) when `anything' is invoked.
1758 It is needed to restore position in `anything-current-buffer'
1759 when `anything' is keyboard-quitted.")
1760 (defun anything-current-position (save-or-restore)
1761 "Restore or save current position in `anything-current-buffer'.
1762 Argument SAVE-OR-RESTORE is one of save or restore."
1763 (case save-or-restore
1764 (save
1765 (anything-log "Save position at %S" (cons (point) (window-start)))
1766 (setq anything-current-position (cons (point) (window-start))))
1767 (restore
1768 (anything-log "Restore position at %S in buffer %s"
1769 anything-current-position
1770 (buffer-name (current-buffer)))
1771 (goto-char (car anything-current-position))
1772 ;; Fix this position with the NOFORCE arg of `set-window-start'
1773 ;; otherwise, if there is some other buffer than `anything-current-buffer'
1774 ;; one, position will be lost.
1775 (set-window-start (selected-window) (cdr anything-current-position) t))))
1778 ;; Internal.
1779 (defvar anything-last-frame-or-window-configuration nil
1780 "Used to store window or frame configuration when anything start.")
1781 (defun anything-frame-or-window-configuration (save-or-restore)
1782 "Save or restore last frame or window configuration.
1783 Possible value of SAVE-OR-RESTORE are 'save and 'restore.
1784 window or frame configuration is saved/restored according to values of
1785 `anything-save-configuration-functions'."
1786 (anything-log-eval anything-save-configuration-functions)
1787 (case save-or-restore
1788 (save (setq anything-last-frame-or-window-configuration
1789 (funcall (cdr anything-save-configuration-functions))))
1790 (restore (funcall (car anything-save-configuration-functions)
1791 anything-last-frame-or-window-configuration)
1792 ;; Restore frame focus.
1793 (let ((frame (and (listp anything-last-frame-or-window-configuration)
1794 (caadr anything-last-frame-or-window-configuration))))
1795 ;; If `anything-save-configuration-functions' are window functions
1796 ;; frame should be nil, use current frame.
1797 (unless (framep frame)
1798 (setq frame (selected-frame)))
1799 (select-frame-set-input-focus frame)))))
1802 ;; (@* "Core: Display *anything* buffer")
1803 (defun anything-display-buffer (buf)
1804 "Display *anything* buffer BUF."
1805 (let (pop-up-frames)
1806 (funcall (with-current-buffer buf anything-display-function) buf)))
1808 (defun anything-default-display-buffer (buf)
1809 "Default function to display BUF.
1810 Where BUF is generally `anything-buffer'.
1811 It use `switch-to-buffer' or `pop-to-buffer' depending of value of
1812 `anything-samewindow'."
1813 (funcall (if anything-samewindow 'switch-to-buffer 'pop-to-buffer) buf))
1815 ;; (@* "Core: initialize")
1816 (defun anything-initial-setup ()
1817 "Initialize anything settings and set up the anything buffer."
1818 (anything-log-run-hook 'anything-before-initialize-hook)
1819 (setq anything-current-prefix-arg nil)
1820 (setq anything-once-called-functions nil)
1821 (setq anything-delayed-init-executed nil)
1822 (setq anything-current-buffer
1823 (if (minibuffer-window-active-p (minibuffer-window))
1824 ;; If minibuffer is active be sure to use it's buffer
1825 ;; as `anything-current-buffer'.
1826 (window-buffer (active-minibuffer-window))
1827 (current-buffer)))
1828 (setq anything-buffer-file-name buffer-file-name)
1829 (setq anything-issued-errors nil)
1830 (setq anything-compiled-sources nil)
1831 (setq anything-saved-current-source nil)
1832 (if (or (not split-width-threshold)
1833 (and (integerp split-width-threshold)
1834 (>= split-width-threshold (+ (frame-width) 4))))
1835 (setq anything-split-window-state 'vertical)
1836 (setq anything-split-window-state 'horizontal))
1837 ;; Call the init function for sources where appropriate
1838 (anything-funcall-foreach 'init)
1839 (setq anything-pattern "")
1840 (setq anything-input "")
1841 (setq anything-candidate-cache nil)
1842 (setq anything-last-sources anything-sources)
1843 (anything-create-anything-buffer)
1844 (anything-log-run-hook 'anything-after-initialize-hook))
1846 (defvar anything-reading-pattern nil
1847 "Whether in `read-string' in anything or not.")
1848 (defun anything-read-pattern-maybe (any-prompt any-input
1849 any-preselect any-resume any-keymap any-default)
1850 "Read pattern with prompt ANY-PROMPT and initial input ANY-INPUT.
1851 For ANY-PRESELECT ANY-RESUME ANY-KEYMAP, See `anything'."
1852 (if (anything-resume-p any-resume)
1853 (anything-mark-current-line)
1854 (anything-update any-preselect))
1855 (with-current-buffer (anything-buffer-get)
1856 (and any-keymap (set (make-local-variable 'anything-map) any-keymap))
1857 (let ((minibuffer-local-map
1858 anything-map))
1859 (anything-log-eval (anything-approximate-candidate-number)
1860 anything-execute-action-at-once-if-one
1861 anything-quit-if-no-candidate)
1862 (cond ((and anything-execute-action-at-once-if-one
1863 (= (anything-approximate-candidate-number) 1))
1864 (ignore))
1865 ((and anything-quit-if-no-candidate
1866 (= (anything-approximate-candidate-number) 0))
1867 (setq anything-quit t)
1868 (and (functionp anything-quit-if-no-candidate)
1869 (funcall anything-quit-if-no-candidate)))
1871 (let ((anything-reading-pattern t)
1872 (tap (or any-default
1873 (with-anything-current-buffer
1874 (thing-at-point 'symbol)))))
1875 (read-string (or any-prompt "pattern: ") any-input nil tap)))))))
1877 (defun anything-create-anything-buffer (&optional test-mode)
1878 "Create newly created `anything-buffer'.
1879 If TEST-MODE is non-nil, clear `anything-candidate-cache'."
1880 (when test-mode
1881 (setq anything-candidate-cache nil))
1882 (with-current-buffer (get-buffer-create anything-buffer)
1883 (anything-log "kill local variables: %S" (buffer-local-variables))
1884 (kill-all-local-variables)
1885 (set (make-local-variable 'inhibit-read-only) t)
1886 (buffer-disable-undo)
1887 (erase-buffer)
1888 (set (make-local-variable 'inhibit-read-only) t)
1889 (set (make-local-variable 'anything-last-sources-local) anything-sources)
1890 (set (make-local-variable 'anything-follow-mode) nil)
1891 (set (make-local-variable 'anything-display-function) anything-display-function)
1892 (anything-initialize-persistent-action)
1893 (anything-log-eval anything-display-function anything-let-variables)
1894 (loop for (var . val) in anything-let-variables
1895 do (set (make-local-variable var) val))
1896 (setq cursor-type nil)
1897 (setq mode-name "Anything"))
1898 (anything-initialize-overlays anything-buffer)
1899 (get-buffer anything-buffer))
1901 (defun anything-initialize-overlays (buffer)
1902 "Initialize anything overlays in BUFFER."
1903 (anything-log "overlay setup")
1904 (if anything-selection-overlay
1905 ;; make sure the overlay belongs to the anything buffer if
1906 ;; it's newly created
1907 (move-overlay anything-selection-overlay (point-min) (point-min)
1908 (get-buffer buffer))
1910 (setq anything-selection-overlay
1911 (make-overlay (point-min) (point-min) (get-buffer buffer)))
1912 (overlay-put anything-selection-overlay 'face anything-selection-face))
1914 (cond (anything-enable-shortcuts
1915 (setq anything-shortcut-keys
1916 (assoc-default anything-enable-shortcuts anything-shortcut-keys-alist))
1917 (unless anything-digit-overlays
1918 (setq anything-digit-overlays
1919 (loop for key across anything-shortcut-keys
1920 for overlay = (make-overlay (point-min) (point-min)
1921 (get-buffer buffer))
1922 do (overlay-put overlay 'before-string
1923 (format "%s - " (upcase (make-string 1 key))))
1924 collect overlay))))
1925 (anything-digit-overlays
1926 (mapc 'delete-overlay anything-digit-overlays)
1927 (setq anything-digit-overlays nil))))
1929 (defun anything-hooks (setup-or-cleanup)
1930 "Add or remove hooks according to SETUP-OR-CLEANUP value.
1931 if SETUP-OR-CLEANUP value is setup add hooks, any other value
1932 will remove hooks.
1933 hooks concerned are `post-command-hook' and `minibuffer-setup-hook'."
1934 (let ((hooks '((post-command-hook anything-check-minibuffer-input)
1935 (minibuffer-setup-hook anything-print-error-messages))))
1936 (if (eq setup-or-cleanup 'setup)
1937 (dolist (args hooks) (apply 'add-hook args))
1938 (dolist (args (reverse hooks)) (apply 'remove-hook args)))))
1940 ;; (@* "Core: clean up")
1941 ;;; TODO move
1942 (defun anything-cleanup ()
1943 "Clean up the mess when anything exit or quit."
1944 (anything-log "start cleanup")
1945 (with-current-buffer anything-buffer
1946 (setq cursor-type t)
1947 ;; Call burry-buffer whithout arg
1948 ;; to remove anything-buffer from window.
1949 (bury-buffer)
1950 ;; Be sure we call this from anything-buffer.
1951 (anything-funcall-foreach 'cleanup))
1952 (anything-new-timer 'anything-check-minibuffer-input-timer nil)
1953 (anything-kill-async-processes)
1954 (anything-log-run-hook 'anything-cleanup-hook)
1955 (anything-hooks 'cleanup)
1956 (anything-frame-or-window-configuration 'restore)
1957 ;; This is needed in some cases where last input
1958 ;; is yielded infinitely in minibuffer after anything session.
1959 (anything-clean-up-minibuffer))
1961 (defun anything-clean-up-minibuffer ()
1962 "Remove contents of minibuffer."
1963 (let ((miniwin (minibuffer-window)))
1964 ;; Clean only current minibuffer used by anything.
1965 ;; i.e The precedent one is active.
1966 (unless (minibuffer-window-active-p miniwin)
1967 (with-current-buffer (window-buffer miniwin)
1968 (delete-minibuffer-contents)))))
1970 ;; (@* "Core: input handling")
1971 (defun anything-check-minibuffer-input ()
1972 "Extract input string from the minibuffer and check if it needs to be handled."
1973 (let ((delay (with-current-buffer anything-buffer anything-input-idle-delay)))
1974 (if (or (not delay) (anything-action-window))
1975 (anything-check-minibuffer-input-1)
1976 (anything-new-timer
1977 'anything-check-minibuffer-input-timer
1978 (run-with-idle-timer delay nil 'anything-check-minibuffer-input-1)))))
1980 (defun anything-check-minibuffer-input-1 ()
1981 "Check minibuffer content."
1982 (with-anything-quittable
1983 (with-selected-window (or (active-minibuffer-window) (minibuffer-window))
1984 (anything-check-new-input (minibuffer-contents)))))
1986 (defun anything-check-new-input (input)
1987 "Check INPUT string and update the anything buffer if necessary."
1988 (unless (equal input anything-pattern)
1989 (setq anything-pattern input)
1990 (unless (anything-action-window)
1991 (setq anything-input anything-pattern))
1992 (anything-log-eval anything-pattern anything-input)
1993 (anything-update)))
1995 ;; (@* "Core: source compiler")
1996 (defvar anything-compile-source-functions-default anything-compile-source-functions
1997 "Plug-ins this file provides.")
1998 (defun anything-compile-sources (sources funcs)
1999 "Compile SOURCES with FUNCS.
2000 See `anything-compile-source-functions'.
2001 Anything plug-ins are realized by this function."
2002 (mapcar
2003 (lambda (source)
2004 (loop with source = (if (listp source) source (symbol-value source))
2005 for f in funcs
2006 do (setq source (funcall f source))
2007 finally (return source)))
2008 sources))
2010 ;; (@* "Core: plug-in attribute documentation hack")
2012 ;; `anything-document-attribute' is public API.
2013 (defadvice documentation-property (after anything-document-attribute activate)
2014 "Display plug-in attributes' documentation as `anything-sources' docstring."
2015 (when (eq (ad-get-arg 0) 'anything-sources)
2016 (setq ad-return-value
2017 (concat ad-return-value "\n"
2018 (mapconcat (lambda (sym) (get sym 'anything-attrdoc))
2019 anything-additional-attributes
2020 "\n")))))
2021 ;; (describe-variable 'anything-sources)
2022 ;; (documentation-property 'anything-sources 'variable-documentation)
2023 ;; (progn (ad-disable-advice 'documentation-property 'after 'anything-document-attribute) (ad-update 'documentation-property))
2025 ;; (@* "Core: all candidates")
2026 (defun anything-process-delayed-init (source)
2027 "Initialize delayed SOURCE."
2028 (let ((name (assoc-default 'name source)))
2029 (unless (member name anything-delayed-init-executed)
2030 (anything-aif (assoc-default 'delayed-init source)
2031 (with-current-buffer anything-current-buffer
2032 (anything-funcall-with-source source it)
2033 (dolist (f (if (functionp it) (list it) it))
2034 (add-to-list 'anything-delayed-init-executed name)))))))
2036 (defun anything-get-candidates (source)
2037 "Retrieve and return the list of candidates from SOURCE."
2038 (anything-process-delayed-init source)
2039 (let* ((candidate-source (assoc-default 'candidates source))
2040 (type-error (lambda ()
2041 (error (concat "Candidates must either be a function, "
2042 " a variable or a list: %s")
2043 candidate-source)))
2044 (candidates (condition-case err
2045 (anything-interpret-value candidate-source source)
2046 (error (funcall type-error)))))
2047 (cond ((processp candidates) candidates)
2048 ((listp candidates) (anything-transform-candidates candidates source))
2049 (t (funcall type-error)))))
2052 (defun anything-get-cached-candidates (source)
2053 "Return the cached value of candidates for SOURCE.
2054 Cache the candidates if there is not yet a cached value."
2055 (let* ((name (assoc-default 'name source))
2056 (candidate-cache (assoc name anything-candidate-cache)))
2057 (cond (candidate-cache
2058 (anything-log "use cached candidates")
2059 (cdr candidate-cache))
2061 (anything-log "calculate candidates")
2062 (let ((candidates (anything-get-candidates source)))
2063 (cond ((processp candidates)
2064 (push (cons candidates
2065 (append source
2066 (list (cons 'item-count 0)
2067 (cons 'incomplete-line ""))))
2068 anything-async-processes)
2069 (set-process-filter candidates 'anything-output-filter)
2070 (setq candidates nil))
2071 ((not (assoc 'volatile source))
2072 (setq candidate-cache (cons name candidates))
2073 (push candidate-cache anything-candidate-cache)))
2074 candidates)))))
2076 ;;; (@* "Core: candidate transformers")
2077 (defun anything-process-candidate-transformer (candidates source)
2078 "Execute candidate-transformer function on all CANDIDATES of SOURCE."
2079 (anything-aif (assoc-default 'candidate-transformer source)
2080 (anything-composed-funcall-with-source source it candidates)
2081 candidates))
2083 (defun anything-process-filtered-candidate-transformer (candidates source)
2084 "Execute filtered-candidate-transformer function on all CANDIDATES of SOURCE."
2085 (anything-aif (assoc-default 'filtered-candidate-transformer source)
2086 (anything-composed-funcall-with-source source it candidates source)
2087 candidates))
2089 (defun anything-process-filtered-candidate-transformer-maybe (candidates source process-p)
2090 "Execute filtered-candidate-transformer function on all CANDIDATES of SOURCE.
2091 This happen if PROCESS-P is non-nil."
2092 (if process-p
2093 (anything-process-filtered-candidate-transformer candidates source)
2094 candidates))
2096 (defun anything-process-real-to-display (candidates source)
2097 "Execute real-to-display function on all CANDIDATES of SOURCE."
2098 (anything-aif (assoc-default 'real-to-display source)
2099 (setq candidates (anything-funcall-with-source
2100 source 'mapcar
2101 (lambda (cand_)
2102 (if (consp cand_)
2103 ;; override DISPLAY from candidate-transformer
2104 (cons (funcall it (cdr cand_)) (cdr cand_))
2105 (cons (funcall it cand_) cand_)))
2106 candidates))
2107 candidates))
2109 (defun anything-transform-candidates (candidates source &optional process-p)
2110 "Transform CANDIDATES of SOURCE according to candidate transformers.
2111 This happen if PROCESS-P is non-nil."
2112 (anything-process-real-to-display
2113 (anything-process-filtered-candidate-transformer-maybe
2114 (anything-process-candidate-transformer candidates source) source process-p)
2115 source))
2117 ;; (@* "Core: narrowing candidates")
2118 (defun anything-candidate-number-limit (source)
2119 "Apply candidate-number-limit attribute value.
2120 This overhide variable `anything-candidate-number-limit'.
2122 e.g:
2123 If \(candidate-number-limit\) is in SOURCE, show all candidates in SOURCE.
2124 If \(candidate-number-limit . 123\) is in SOURCE limit candidate to 123."
2125 (anything-aif (assq 'candidate-number-limit source)
2126 (or (cdr it) 99999999)
2127 (or anything-candidate-number-limit 99999999)))
2129 ;; FIXME: Why a defconst here
2130 (defconst anything-default-match-functions
2131 (list (lambda (candidate)
2132 (string-match anything-pattern candidate)))
2133 "Default functions to match candidates according to `anything-pattern'.")
2135 (defun anything-compute-matches (source)
2136 "Compute matched results from SOURCE according to its settings."
2137 (if debug-on-error
2138 (anything-compute-matches-internal source)
2139 (condition-case v
2140 (anything-compute-matches-internal source)
2141 (error (anything-log-error
2142 "anything-compute-matches: error when processing source: %s"
2143 (assoc-default 'name source))
2144 nil))))
2146 (defun anything-candidate-get-display (candidate)
2147 "Get searched display part from CANDIDATE.
2148 CANDIDATE is a string, a symbol, or \(DISPLAY . REAL\) cons cell."
2149 (format "%s" (or (car-safe candidate) candidate)))
2151 (defun anything-process-pattern-transformer (pattern source)
2152 "Execute pattern-transformer attribute PATTERN function in SOURCE."
2153 (anything-aif (assoc-default 'pattern-transformer source)
2154 (anything-composed-funcall-with-source source it pattern)
2155 pattern))
2157 (defun anything-match-functions (source)
2158 (or (assoc-default 'match source)
2159 anything-default-match-functions))
2161 (defmacro anything-accumulate-candidates-internal (cand newmatches
2162 hash item-count limit)
2163 "Internal, add CAND into NEWMATCHES.
2164 Use HASH to uniq NEWMATCHES.
2165 Argument ITEM-COUNT count the matches.
2166 if ITEM-COUNT reaches LIMIT, exit from inner loop."
2167 `(unless (gethash ,cand ,hash)
2168 (puthash ,cand t ,hash)
2169 (push ,cand ,newmatches)
2170 (incf ,item-count)
2171 (when (= ,item-count ,limit)
2172 (setq exit t)
2173 (return))))
2175 (defun anything-take-first-elements (seq n)
2176 (if (> (length seq) n)
2177 (setq seq (subseq seq 0 n))
2178 seq))
2180 (defun anything-match-from-candidates (cands matchfns limit)
2181 (let (matches)
2182 (condition-case nil
2183 (let ((item-count 0) exit)
2184 (clrhash anything-match-hash)
2185 (dolist (match matchfns)
2186 (let (newmatches)
2187 (dolist (candidate cands)
2188 (when (funcall match (anything-candidate-get-display candidate))
2189 (anything-accumulate-candidates-internal
2190 candidate newmatches anything-match-hash item-count limit)))
2191 (setq matches (append matches (reverse newmatches)))
2192 (if exit (return)))))
2193 (invalid-regexp (setq matches nil)))
2194 matches))
2196 (defun anything-compute-matches-internal (source)
2197 (save-current-buffer
2198 (let ((matchfns (anything-match-functions source))
2199 (anything-source-name (assoc-default 'name source))
2200 (limit (anything-candidate-number-limit source))
2201 (anything-pattern (anything-process-pattern-transformer
2202 anything-pattern source)))
2203 (anything-process-filtered-candidate-transformer
2204 (if (or (equal anything-pattern "") (equal matchfns '(identity)))
2205 (anything-take-first-elements
2206 (anything-get-cached-candidates source) limit)
2207 (anything-match-from-candidates
2208 (anything-get-cached-candidates source) matchfns limit))
2209 source))))
2211 ;; (anything '(((name . "error")(candidates . (lambda () (hage))) (action . identity))))
2213 (defun anything-process-source (source)
2214 "Display matched results from SOURCE according to its settings."
2215 (anything-log-eval (assoc-default 'name source))
2216 (if (assq 'direct-insert-match source) ;experimental
2217 (anything-process-source--direct-insert-match source)
2218 (let ((matches (anything-compute-matches source)))
2219 (when matches
2220 (when anything-test-mode
2221 (setq anything-test-candidate-list
2222 `(,@anything-test-candidate-list
2223 (,(assoc-default 'name source)
2224 ,matches))))
2225 (anything-insert-header-from-source source)
2226 (if (not (assq 'multiline source))
2227 (mapc 'anything-insert-match-with-digit-overlay matches)
2228 (let ((start (point)) separate)
2229 (dolist (match matches)
2230 (if separate
2231 (anything-insert-candidate-separator)
2232 (setq separate t))
2233 (anything-insert-match-with-digit-overlay match))
2234 (put-text-property start (point) 'anything-multiline t)))))))
2236 (defun anything-insert-match-with-digit-overlay (match)
2237 (declare (special source))
2238 (anything-put-digit-overlay-maybe)
2239 (anything-insert-match match 'insert source))
2241 (defun anything-put-digit-overlay-maybe ()
2242 (when (and anything-enable-shortcuts
2243 (not (eq anything-digit-shortcut-count
2244 (length anything-digit-overlays))))
2245 (move-overlay (nth anything-digit-shortcut-count
2246 anything-digit-overlays)
2247 (point-at-bol)
2248 (point-at-bol))
2249 (incf anything-digit-shortcut-count)))
2251 (defun anything-process-source--direct-insert-match (source)
2252 "[EXPERIMENTAL] Insert candidates from `anything-candidate-buffer' in SOURCE."
2253 (anything-log-eval (assoc-default 'name source))
2254 (let ((anything-source-name (assoc-default 'name source))
2255 content-buf)
2256 (funcall (assoc-default 'candidates source))
2257 (setq content-buf (anything-candidate-buffer))
2258 (unless (anything-empty-buffer-p content-buf)
2259 (anything-insert-header-from-source source)
2260 (insert-buffer-substring content-buf)
2261 ;; TODO call anything-put-digit-overlay-maybe with loop
2264 (defun anything-process-delayed-sources (delayed-sources)
2265 "Process DELAYED-SOURCES when idle for `anything-idle-delay' seconds."
2266 (with-anything-quittable
2267 (anything-log-eval (mapcar (lambda (s) (assoc-default 'name s)) delayed-sources))
2268 (with-current-buffer anything-buffer
2269 (save-excursion
2270 (goto-char (point-max))
2271 (mapc 'anything-process-source delayed-sources)
2272 (when (and (not (anything-empty-buffer-p))
2273 ;; no selection yet
2274 (= (overlay-start anything-selection-overlay)
2275 (overlay-end anything-selection-overlay)))
2276 (goto-char (point-min))
2277 (anything-next-line)))
2278 (save-excursion
2279 (goto-char (point-min))
2280 (anything-log-run-hook 'anything-update-hook)))))
2282 ;; (@* "Core: *anything* buffer contents")
2283 (defvar anything-input-local nil)
2284 (defvar anything-process-delayed-sources-timer nil)
2285 (defun anything-update (&optional preselect)
2286 "Update candidates list in `anything-buffer' according to `anything-pattern'."
2287 (anything-log "start update")
2288 (setq anything-digit-shortcut-count 0)
2289 (anything-kill-async-processes)
2290 (with-current-buffer (anything-buffer-get)
2291 (set (make-local-variable 'anything-input-local) anything-pattern)
2292 (erase-buffer)
2293 (when anything-enable-shortcuts
2294 (mapc 'delete-overlay anything-digit-overlays))
2295 (let (delayed-sources)
2296 (unwind-protect
2297 (setq delayed-sources
2298 (loop for source in (remove-if-not 'anything-update-source-p
2299 (anything-get-sources))
2300 if (anything-delayed-source-p source)
2301 collect source
2302 else do (anything-process-source source)))
2303 (anything-log-eval
2304 (mapcar (lambda (s) (assoc-default 'name s)) delayed-sources))
2305 (anything-update-move-first-line)
2306 (if anything-test-mode
2307 (mapc 'anything-process-source delayed-sources)
2308 (when delayed-sources
2309 (anything-new-timer
2310 'anything-process-delayed-sources-timer
2311 (run-with-idle-timer
2312 anything-idle-delay nil
2313 'anything-process-delayed-sources delayed-sources)))
2314 ;; FIXME I want to execute anything-after-update-hook
2315 ;; AFTER processing delayed sources
2316 (anything-log-run-hook 'anything-after-update-hook))
2317 (and preselect (anything-preselect preselect))
2318 (anything-log "end update")))))
2320 (defun anything-update-source-p (source)
2321 (and (or (not anything-source-filter)
2322 (member (assoc-default 'name source) anything-source-filter))
2323 (>= (length anything-pattern)
2324 (anything-aif (assoc 'requires-pattern source)
2325 (or (cdr it) 1)
2326 0))))
2328 (defun anything-delayed-source-p (source)
2329 (or (assoc 'delayed source)
2330 (and anything-quick-update
2331 (< (window-height (get-buffer-window (current-buffer)))
2332 (line-number-at-pos (point-max))))))
2334 (defun anything-update-move-first-line ()
2335 (goto-char (point-min))
2336 (save-excursion (anything-log-run-hook 'anything-update-hook))
2337 (anything-next-line))
2339 (defun anything-force-update ()
2340 "Force recalculation and update of candidates.
2341 If current source has `update' attribute, a function without argument,
2342 call it before update."
2343 (interactive)
2344 (let ((source (anything-get-current-source)))
2345 (if source
2346 (anything-force-update--reinit source)
2347 (anything-erase-message)
2348 (mapc 'anything-force-update--reinit (anything-get-sources)))
2349 (let ((selection (anything-get-selection nil t)))
2350 (anything-update)
2351 (anything-keep-selection source selection))))
2353 (defun anything-force-update--reinit (source)
2354 (anything-aif (anything-funcall-with-source source 'anything-candidate-buffer)
2355 (kill-buffer it))
2356 (dolist (attr '(update init))
2357 (anything-aif (assoc-default attr source)
2358 (anything-funcall-with-source source it)))
2359 (anything-remove-candidate-cache source))
2361 (defun anything-erase-message ()
2362 (message ""))
2364 (defun anything-keep-selection (source selection)
2365 (when (and source selection)
2366 (with-anything-window
2367 (anything-goto-source source)
2368 (forward-char -1) ;back to \n
2369 (if (search-forward (concat "\n" selection "\n") nil t)
2370 (forward-line -1)
2371 (goto-char (point-min))
2372 (forward-line 1))
2373 (anything-mark-current-line))))
2375 (defun anything-remove-candidate-cache (source)
2376 (setq anything-candidate-cache
2377 (delete (assoc (assoc-default 'name source) anything-candidate-cache)
2378 anything-candidate-cache)))
2380 (defun anything-insert-match (match insert-function source)
2381 "Insert MATCH into `anything-buffer' with INSERT-FUNCTION for SOURCE.
2382 If MATCH is a list then insert the string intended to appear on the display
2383 and store the real value in a text property."
2384 (let ((start (point-at-bol (point)))
2385 (string (or (car-safe match) match))
2386 (realvalue (cdr-safe match)))
2387 (when (symbolp string) (setq string (symbol-name string)))
2388 (when (stringp string)
2389 (funcall insert-function string)
2390 ;; Some sources with candidates-in-buffer have already added
2391 ;; 'anything-realvalue property when creating candidate buffer.
2392 (unless (get-text-property start 'anything-realvalue)
2393 (and realvalue
2394 (put-text-property start (point-at-eol)
2395 'anything-realvalue realvalue)))
2396 (when anything-source-in-each-line-flag
2397 (put-text-property start (point-at-eol) 'anything-source source))
2398 (funcall insert-function "\n"))))
2400 (defun anything-insert-header-from-source (source)
2401 "Insert SOURCE name in `anything-buffer' header.
2402 Maybe insert by overlay additional info after source name if SOURCE have
2403 header-name attribute."
2404 (let ((name (assoc-default 'name source)))
2405 (anything-insert-header
2406 name
2407 (anything-aif (assoc-default 'header-name source)
2408 (anything-funcall-with-source source it name)))))
2410 (defun anything-insert-header (name &optional display-string)
2411 "Insert header of source NAME into the anything buffer.
2412 If DISPLAY-STRING is non--nil and a string, display this additional info
2413 after the source name by overlay."
2414 (unless (bobp)
2415 (let ((start (point)))
2416 (insert "\n")
2417 (put-text-property start (point) 'anything-header-separator t)))
2418 (let ((start (point)))
2419 (insert name)
2420 (put-text-property (point-at-bol)
2421 (point-at-eol) 'anything-header t)
2422 (when display-string
2423 (overlay-put (make-overlay (point-at-bol) (point-at-eol))
2424 'display display-string))
2425 (insert "\n")
2426 (put-text-property start (point) 'face anything-header-face)))
2428 (defun anything-insert-candidate-separator ()
2429 "Insert separator of candidates into the anything buffer."
2430 (insert anything-candidate-separator)
2431 (put-text-property (point-at-bol)
2432 (point-at-eol) 'anything-candidate-separator t)
2433 (insert "\n"))
2435 ;; (@* "Core: async process")
2436 (defun anything-output-filter (process string)
2437 "From PROCESS process output STRING."
2438 (anything-output-filter-1 (assoc process anything-async-processes) string))
2440 (defun anything-output-filter-1 (process-assoc string)
2441 (anything-log-eval string)
2442 (with-current-buffer anything-buffer
2443 (let ((source (cdr process-assoc)))
2444 (save-excursion
2445 (anything-aif (assoc-default 'insertion-marker source)
2446 (goto-char it)
2447 (goto-char (point-max))
2448 (anything-insert-header-from-source source)
2449 (setcdr process-assoc
2450 (append source `((insertion-marker . ,(point-marker))))))
2451 (anything-output-filter--process-source
2452 (car process-assoc) string source
2453 (anything-candidate-number-limit source))))
2454 (anything-output-filter--post-process)))
2456 (defun anything-output-filter--process-source (process string source limit)
2457 (dolist (candidate (anything-transform-candidates
2458 (anything-output-filter--collect-candidates
2459 (split-string string "\n")
2460 (assoc 'incomplete-line source))
2461 source t))
2462 (if (not (assq 'multiline source))
2463 (anything-insert-match candidate 'insert-before-markers source)
2464 (let ((start (point)))
2465 (anything-insert-candidate-separator)
2466 (anything-insert-match candidate 'insert-before-markers source)
2467 (put-text-property start (point) 'anything-multiline t)))
2468 (incf (cdr (assoc 'item-count source)))
2469 (when (>= (assoc-default 'item-count source) limit)
2470 (anything-kill-async-process process)
2471 (return))))
2473 (defun anything-output-filter--collect-candidates (lines incomplete-line-info)
2474 (anything-log-eval (cdr incomplete-line-info))
2475 (butlast
2476 (loop for line in lines collect
2477 (if (cdr incomplete-line-info)
2478 (prog1
2479 (concat (cdr incomplete-line-info) line)
2480 (setcdr incomplete-line-info nil))
2481 line)
2482 finally (setcdr incomplete-line-info line))))
2484 (defun anything-output-filter--post-process ()
2485 (anything-log-run-hook 'anything-update-hook)
2486 (anything-aif (get-buffer-window anything-buffer 'visible)
2487 (save-selected-window
2488 (select-window it)
2489 (anything-skip-noncandidate-line 'next)
2490 (anything-mark-current-line))))
2492 (defun anything-kill-async-processes ()
2493 "Kill all known asynchronous processes of `anything-async-processes'."
2494 "Kill locate process."
2495 (mapc 'anything-kill-async-process (mapcar 'car anything-async-processes))
2496 (setq anything-async-processes nil))
2498 (defun anything-kill-async-process (process)
2499 "Kill PROCESS and detach the associated functions."
2500 (set-process-filter process nil)
2501 (delete-process process))
2503 ;; (@* "Core: action")
2504 (defun anything-execute-selection-action (&optional
2505 selection action
2506 preserve-saved-action)
2507 "If a candidate SELECTION is present then perform the associated ACTION on it.
2508 If PRESERVE-SAVED-ACTION is non-nil don't save action."
2509 (anything-log "executing action")
2510 (setq action (anything-get-default-action
2511 (or action
2512 anything-saved-action
2513 (if (get-buffer anything-action-buffer)
2514 (anything-get-selection anything-action-buffer)
2515 (anything-get-action)))))
2516 (let ((source (or anything-saved-current-source
2517 (anything-get-current-source))))
2518 (setq selection (or selection
2519 (anything-get-selection)
2520 (and (assoc 'accept-empty source) "")))
2521 (unless preserve-saved-action (setq anything-saved-action nil))
2522 (if (and selection action)
2523 (anything-funcall-with-source
2524 source action
2525 (anything-coerce-selection selection source)))))
2527 (defun anything-coerce-selection (selection source)
2528 "Apply coerce attribute function to SELECTION in SOURCE.
2529 Coerce source with coerce function."
2530 (anything-aif (assoc-default 'coerce source)
2531 (anything-funcall-with-source source it selection)
2532 selection))
2534 (defun anything-get-default-action (action)
2535 (if (and (listp action) (not (functionp action)))
2536 (cdar action)
2537 action))
2539 (defun anything-select-action ()
2540 "Select an action for the currently selected candidate.
2541 If action buffer is selected, back to the anything buffer."
2542 (interactive)
2543 (cond ((get-buffer-window anything-action-buffer 'visible)
2544 (set-window-buffer (get-buffer-window anything-action-buffer)
2545 anything-buffer)
2546 (kill-buffer anything-action-buffer)
2547 (anything-set-pattern anything-input 'noupdate))
2549 (setq anything-saved-selection (anything-get-selection))
2550 (unless anything-saved-selection
2551 (error "Nothing is selected"))
2552 (setq anything-saved-current-source (anything-get-current-source))
2553 (let ((actions (anything-get-action)))
2554 (if (functionp actions)
2555 (message "Sole action: %s" actions)
2556 (anything-show-action-buffer actions)
2557 (anything-delete-minibuffer-contents)
2558 (setq anything-pattern 'dummy) ; so that it differs from the previous one
2559 (anything-check-minibuffer-input))))))
2561 (defun anything-show-action-buffer (actions)
2562 (with-current-buffer (get-buffer-create anything-action-buffer)
2563 (erase-buffer)
2564 (buffer-disable-undo)
2565 (set-window-buffer (get-buffer-window anything-buffer) anything-action-buffer)
2566 (set (make-local-variable 'anything-sources)
2567 `(((name . "Actions")
2568 (volatile)
2569 (candidates . ,actions)
2570 (candidate-number-limit))))
2571 (set (make-local-variable 'anything-source-filter) nil)
2572 (set (make-local-variable 'anything-selection-overlay) nil)
2573 (set (make-local-variable 'anything-digit-overlays) nil)
2574 (anything-initialize-overlays anything-action-buffer)))
2576 ;; (@* "Core: selection")
2577 (defun anything-move-selection-common (move-func unit direction)
2578 "Move the selection marker to a new position wit function MOVE-FUNC.
2579 It is determined by UNIT and DIRECTION."
2580 (unless (or (anything-empty-buffer-p (anything-buffer-get))
2581 (not (anything-window)))
2582 (with-anything-window
2583 (anything-log-run-hook 'anything-move-selection-before-hook)
2584 (funcall move-func)
2585 (anything-skip-noncandidate-line direction)
2586 (anything-display-source-at-screen-top-maybe unit)
2587 (when (anything-get-previous-header-pos)
2588 (anything-mark-current-line))
2589 (anything-display-mode-line (anything-get-current-source))
2590 (anything-log-run-hook 'anything-move-selection-after-hook))))
2592 (defun anything-display-source-at-screen-top-maybe (unit)
2593 (when (and anything-display-source-at-screen-top (eq unit 'source))
2594 (set-window-start (selected-window)
2595 (save-excursion (forward-line -1) (point)))))
2597 (defun anything-skip-noncandidate-line (direction)
2598 (anything-skip-header-and-separator-line direction)
2599 (and (bobp) (forward-line 1)) ;skip first header
2600 (and (eobp) (forward-line -1))) ;avoid last empty line
2603 (defun anything-skip-header-and-separator-line (direction)
2604 (while (and (not (bobp))
2605 (or (anything-pos-header-line-p)
2606 (anything-pos-candidate-separator-p)))
2607 (forward-line (if (and (eq direction 'previous)
2608 (not (eq (point-at-bol) (point-min))))
2609 -1 1))))
2611 (defvar anything-mode-line-string-real nil)
2612 (defun anything-display-mode-line (source)
2613 (set (make-local-variable 'anything-mode-line-string)
2614 (anything-interpret-value (or (assoc-default 'mode-line source)
2615 (default-value 'anything-mode-line-string))
2616 source))
2617 (if anything-mode-line-string
2618 (setq mode-line-format
2619 '(" " mode-line-buffer-identification " "
2620 (line-number-mode "L%l") " " (anything-follow-mode "(F) ")
2621 (:eval (anything-show-candidate-number
2622 (when (listp anything-mode-line-string)
2623 (car anything-mode-line-string))))
2624 " " anything-mode-line-string-real "-%-")
2625 anything-mode-line-string-real
2626 (substitute-command-keys (if (listp anything-mode-line-string)
2627 (cadr anything-mode-line-string)
2628 anything-mode-line-string)))
2629 (setq mode-line-format
2630 (default-value 'mode-line-format)))
2631 (setq header-line-format
2632 (anything-interpret-value (assoc-default 'header-line source) source)))
2634 (defun anything-show-candidate-number (&optional name)
2635 "Used to display candidate number in mode-line.
2636 You can specify NAME of candidates e.g \"Buffers\" otherwise
2637 it is \"Candidate\(s\)\" by default."
2638 (propertize
2639 (format "[%s %s]"
2640 (anything-approximate-candidate-number 'in-current-source)
2641 (or name "Candidate(s)"))
2642 'face 'anything-candidate-number))
2644 (defun anything-previous-line ()
2645 "Move selection to the previous line."
2646 (interactive)
2647 (anything-move-selection-common
2648 (lambda ()
2649 (if (not (anything-pos-multiline-p))
2650 (forward-line -1) ;double forward-line is meaningful
2651 (forward-line -1) ;because evaluation order is important
2652 (anything-skip-header-and-separator-line 'previous)
2653 (let ((header-pos (anything-get-previous-header-pos))
2654 (separator-pos (anything-get-previous-candidate-separator-pos)))
2655 (when header-pos
2656 (goto-char (if (or (null separator-pos) (< separator-pos header-pos))
2657 header-pos ; first candidate
2658 separator-pos))
2659 (forward-line 1)))))
2660 'line 'previous))
2662 (defun anything-next-line ()
2663 "Move selection to the next line."
2664 (interactive)
2665 (anything-move-selection-common
2666 (lambda ()
2667 (if (not (anything-pos-multiline-p))
2668 (forward-line 1)
2669 (let ((header-pos (anything-get-next-header-pos))
2670 (separator-pos (anything-get-next-candidate-separator-pos)))
2671 (cond ((and separator-pos
2672 (or (null header-pos) (< separator-pos header-pos)))
2673 (goto-char separator-pos))
2674 (header-pos
2675 (goto-char header-pos))))))
2676 'line 'next))
2678 (defun anything-previous-page ()
2679 "Move selection back with a pageful."
2680 (interactive)
2681 (anything-move-selection-common
2682 (lambda ()
2683 (condition-case nil
2684 (scroll-down)
2685 (beginning-of-buffer (goto-char (point-min)))))
2686 'page 'previous))
2688 (defun anything-next-page ()
2689 "Move selection forward with a pageful."
2690 (interactive)
2691 (anything-move-selection-common
2692 (lambda ()
2693 (condition-case nil
2694 (scroll-up)
2695 (end-of-buffer (goto-char (point-max)))))
2696 'page 'next))
2698 (defun anything-beginning-of-buffer ()
2699 "Move selection at the top."
2700 (interactive)
2701 (anything-move-selection-common
2702 (lambda () (goto-char (point-min)))
2703 'edge 'previous))
2705 (defun anything-end-of-buffer ()
2706 "Move selection at the bottom."
2707 (interactive)
2708 (anything-move-selection-common
2709 (lambda () (goto-char (point-max)))
2710 'edge 'next))
2712 (defun anything-previous-source ()
2713 "Move selection to the previous source."
2714 (interactive)
2715 (anything-move-selection-common
2716 (lambda ()
2717 (forward-line -1)
2718 (if (bobp)
2719 (goto-char (point-max))
2720 (anything-skip-header-and-separator-line 'previous))
2721 (goto-char (anything-get-previous-header-pos))
2722 (forward-line 1))
2723 'source 'previous))
2725 (defun anything-next-source ()
2726 "Move selection to the next source."
2727 (interactive)
2728 (anything-move-selection-common
2729 (lambda ()
2730 (goto-char (or (anything-get-next-header-pos) (point-min))))
2731 'source 'next))
2733 (defun anything-goto-source (source-or-name)
2734 "Move the selection to the source SOURCE-OR-NAME."
2735 (anything-move-selection-common
2736 (lambda ()
2737 (goto-char (point-min))
2738 (let ((name (if (stringp source-or-name) source-or-name
2739 (assoc-default 'name source-or-name))))
2740 (condition-case err
2741 (while (not (string= name (anything-current-line-contents)))
2742 (goto-char (anything-get-next-header-pos)))
2743 (error (message "")))))
2744 'source 'next))
2746 (defun anything-mark-current-line ()
2747 "Move `anything-selection-overlay' to current line.
2748 Note that this is not related with visibles marks, which are used
2749 to mark candidates."
2750 (move-overlay
2751 anything-selection-overlay (point-at-bol)
2752 (if (anything-pos-multiline-p)
2753 (let ((header-pos (anything-get-next-header-pos))
2754 (separator-pos (anything-get-next-candidate-separator-pos)))
2755 (or (and (null header-pos) separator-pos)
2756 (and header-pos separator-pos (< separator-pos header-pos)
2757 separator-pos)
2758 header-pos
2759 (point-max)))
2760 (1+ (point-at-eol))))
2761 (anything-follow-execute-persistent-action-maybe))
2763 (defun anything-this-command-key ()
2764 (event-basic-type (elt (this-command-keys-vector) 0)))
2765 ;; (progn (read-key-sequence "Key: ") (p (anything-this-command-key)))
2767 (defun anything-select-with-shortcut-internal (types get-key-func)
2768 (if (memq anything-enable-shortcuts types)
2769 (save-selected-window
2770 (select-window (anything-window))
2771 (let* ((key (funcall get-key-func))
2772 (overlay (ignore-errors (nth (position key anything-shortcut-keys)
2773 anything-digit-overlays))))
2774 (if (not (and overlay (overlay-buffer overlay)))
2775 (when (numberp key)
2776 (select-window (minibuffer-window))
2777 (self-insert-command 1))
2778 (goto-char (overlay-start overlay))
2779 (anything-mark-current-line)
2780 (anything-exit-minibuffer))))
2781 (self-insert-command 1)))
2783 (defun anything-select-with-prefix-shortcut ()
2784 "Invoke default action with prefix shortcut."
2785 (interactive)
2786 (anything-select-with-shortcut-internal
2787 '(prefix)
2788 (lambda () (read-event "Select shortcut key: "))))
2790 (defun anything-select-with-digit-shortcut ()
2791 "Invoke default action with digit/alphabet shortcut."
2792 (interactive)
2793 (anything-select-with-shortcut-internal
2794 '(alphabet t) 'anything-this-command-key))
2796 ;; (setq anything-enable-shortcuts 'prefix)
2797 ;; (define-key anything-map "@" 'anything-select-with-prefix-shortcut)
2798 ;; (define-key anything-map (kbd "<f18>") 'anything-select-with-prefix-shortcut)
2800 (defvar anything-exit-status 0
2801 "Flag to inform whether anything have aborted or quitted.
2802 Exit with 0 mean anything exit executing an action.
2803 Exit with 1 mean anything abort with \\[keyboard-quit]
2804 It is useful for example to restore a window config if anything abort
2805 in special cases.
2806 See `anything-exit-minibuffer' and `anything-keyboard-quit'.")
2808 (defun anything-exit-minibuffer ()
2809 "Select the current candidate by exiting the minibuffer."
2810 (interactive)
2811 (unless anything-current-prefix-arg
2812 (setq anything-current-prefix-arg current-prefix-arg))
2813 (setq anything-exit-status 0)
2814 (exit-minibuffer))
2816 (defun anything-keyboard-quit ()
2817 "Quit minibuffer in anything.
2819 If action buffer is displayed, kill it."
2820 (interactive)
2821 (when (get-buffer-window anything-action-buffer 'visible)
2822 (kill-buffer anything-action-buffer))
2823 (setq anything-exit-status 1)
2824 (abort-recursive-edit))
2826 (defun anything-get-next-header-pos ()
2827 "Return the position of the next header from point."
2828 (next-single-property-change (point) 'anything-header))
2830 (defun anything-get-previous-header-pos ()
2831 "Return the position of the previous header from point."
2832 (previous-single-property-change (point) 'anything-header))
2834 (defun anything-pos-multiline-p ()
2835 "Return non-nil if the current position is in the multiline source region."
2836 (get-text-property (point) 'anything-multiline))
2838 (defun anything-get-next-candidate-separator-pos ()
2839 "Return the position of the next candidate separator from point."
2840 (next-single-property-change (point) 'anything-candidate-separator))
2842 (defun anything-get-previous-candidate-separator-pos ()
2843 "Return the position of the previous candidate separator from point."
2844 (previous-single-property-change (point) 'anything-candidate-separator))
2846 (defun anything-pos-header-line-p ()
2847 "Return t if the current line is a header line."
2848 (or (get-text-property (point-at-bol) 'anything-header)
2849 (get-text-property (point-at-bol) 'anything-header-separator)))
2851 (defun anything-pos-candidate-separator-p ()
2852 "Return t if the current line is a candidate separator."
2853 (get-text-property (point-at-bol) 'anything-candidate-separator))
2855 ;; (@* "Core: help")
2856 (defun anything-help-internal (bufname insert-content-fn)
2857 "Show long message during `anything' session in BUFNAME.
2858 INSERT-CONTENT-FN is the text to be displayed in BUFNAME."
2859 (save-window-excursion
2860 (select-window (anything-window))
2861 (delete-other-windows)
2862 (switch-to-buffer (get-buffer-create bufname))
2863 (erase-buffer)
2864 (funcall insert-content-fn)
2865 (setq mode-line-format "%b (SPC,C-v:NextPage b,M-v:PrevPage other:Exit)")
2866 (setq cursor-type nil)
2867 (goto-char 1)
2868 (anything-help-event-loop)))
2870 (defun anything-help-event-loop ()
2871 (ignore-errors
2872 (loop for event = (read-event) do
2873 (case event
2874 ((?\C-v ? ) (scroll-up))
2875 ((?\M-v ?b) (scroll-down))
2876 (t (return))))))
2878 (defun anything-help ()
2879 "Help of `anything'."
2880 (interactive)
2881 (anything-help-internal
2882 " *Anything Help*"
2883 (lambda ()
2884 (insert (substitute-command-keys
2885 (anything-interpret-value anything-help-message)))
2886 (org-mode))))
2888 (defun anything-debug-output ()
2889 "Show all anything-related variables at this time."
2890 (interactive)
2891 (anything-help-internal " *Anything Debug*" 'anything-debug-output-function))
2893 (defun anything-debug-output-function (&optional vars)
2894 (message "Calculating all anything-related values...")
2895 (insert "If you debug some variables or forms, set `anything-debug-forms'
2896 to a list of forms.\n\n")
2897 (dolist (v (or vars
2898 anything-debug-forms
2899 (apropos-internal "^anything-" 'boundp)))
2900 (insert "** "
2901 (pp-to-string v) "\n"
2902 (pp-to-string (eval v)) "\n"))
2903 (message "Calculating all anything-related values...Done"))
2905 ;; (@* "Core: misc")
2906 (defun anything-kill-buffer-hook ()
2907 "Remove tick entry from `anything-tick-hash' when killing a buffer."
2908 (loop for key being the hash-keys in anything-tick-hash
2909 if (string-match (format "^%s/" (regexp-quote (buffer-name))) key)
2910 do (remhash key anything-tick-hash)))
2911 (add-hook 'kill-buffer-hook 'anything-kill-buffer-hook)
2913 (defun anything-preselect (candidate-or-regexp)
2914 "Move `anything-selection-overlay' to CANDIDATE-OR-REGEXP on startup."
2915 (with-anything-window
2916 (when candidate-or-regexp
2917 (goto-char (point-min))
2918 ;; go to first candidate of first source
2919 (forward-line 1)
2920 (let ((start (point)))
2921 (unless (or (re-search-forward
2922 (concat "^" (regexp-quote candidate-or-regexp) "$")
2923 nil t)
2924 (progn (goto-char start)
2925 (re-search-forward candidate-or-regexp nil t)))
2926 (goto-char start))))
2927 (anything-mark-current-line)))
2929 (defun anything-delete-current-selection ()
2930 "Delete the currently selected item."
2931 (interactive)
2932 (with-anything-window
2933 (cond ((anything-pos-multiline-p)
2934 (anything-aif (anything-get-next-candidate-separator-pos)
2935 (delete-region (point-at-bol)
2936 (1+ (progn (goto-char it) (point-at-eol))))
2937 ;; last candidate
2938 (goto-char (anything-get-previous-candidate-separator-pos))
2939 (delete-region (point-at-bol) (point-max)))
2940 (when (anything-end-of-source-p)
2941 (goto-char (or (anything-get-previous-candidate-separator-pos)
2942 (point-min)))
2943 (forward-line 1)))
2945 (delete-region (point-at-bol) (1+ (point-at-eol)))
2946 (when (anything-end-of-source-p) (forward-line -1))))
2947 (anything-mark-current-line)))
2949 (defun anything-end-of-source-p ()
2950 "Return t if we are at eob or end of source."
2951 (save-excursion
2952 (forward-line 1)
2953 (or (eq (point-at-bol) (point-at-eol))
2954 (anything-pos-header-line-p)
2955 (eobp))))
2957 (defun anything-edit-current-selection-internal (func)
2958 (with-anything-window
2959 (beginning-of-line)
2960 (let ((realvalue (get-text-property (point) 'anything-realvalue)))
2961 (funcall func)
2962 (beginning-of-line)
2963 (and realvalue
2964 (put-text-property (point) (point-at-eol)
2965 'anything-realvalue realvalue))
2966 (anything-mark-current-line))))
2968 (defmacro anything-edit-current-selection (&rest forms)
2969 "Evaluate FORMS at current selection in the anything buffer.
2970 You can edit the line."
2971 (declare (indent 0) (debug t))
2972 `(anything-edit-current-selection-internal
2973 (lambda () ,@forms)))
2975 (defun anything-set-pattern (pattern &optional noupdate)
2976 "Set minibuffer contents to PATTERN.
2977 if optional NOUPDATE is non-nil, anything buffer is not changed."
2978 (with-selected-window (or (active-minibuffer-window) (minibuffer-window))
2979 (delete-minibuffer-contents)
2980 (insert pattern))
2981 (when noupdate
2982 (setq anything-pattern pattern)
2983 (anything-hooks 'cleanup)
2984 (run-with-idle-timer 0 nil 'anything-hooks 'setup)))
2986 (defun anything-delete-minibuffer-contents ()
2987 "Same as `delete-minibuffer-contents' but this is a command."
2988 (interactive)
2989 (anything-set-pattern ""))
2990 (defalias 'anything-delete-minibuffer-content 'anything-delete-minibuffer-contents)
2992 ;; (@* "Built-in plug-in: type")
2993 (defun anything-compile-source--type (source)
2994 (anything-aif (assoc-default 'type source)
2995 (append source (assoc-default it anything-type-attributes) nil)
2996 source))
2998 ;; `define-anything-type-attribute' is public API.
3000 (defun anything-add-type-attribute (type definition)
3001 (anything-aif (assq type anything-type-attributes)
3002 (setq anything-type-attributes (delete it anything-type-attributes)))
3003 (push (cons type definition) anything-type-attributes))
3005 (defvar anything-types nil)
3006 (defun anything-document-type-attribute (type doc)
3007 (add-to-list 'anything-types type t)
3008 (put type 'anything-typeattrdoc
3009 (concat "- " (symbol-name type) "\n\n" doc "\n")))
3011 (defadvice documentation-property (after anything-document-type-attribute activate)
3012 "Display type attributes' documentation as `anything-type-attributes' docstring."
3013 (when (eq (ad-get-arg 0) 'anything-type-attributes)
3014 (setq ad-return-value
3015 (concat ad-return-value "\n\n++++ Types currently defined ++++\n"
3016 (mapconcat (lambda (sym) (get sym 'anything-typeattrdoc))
3017 anything-types "\n")))))
3019 ;; (@* "Built-in plug-in: dummy")
3020 (defun anything-dummy-candidate (candidate source)
3021 "Use `anything-pattern' as CANDIDATE in SOURCE."
3022 ;; `source' is defined in filtered-candidate-transformer
3023 (list anything-pattern))
3025 (defun anything-compile-source--dummy (source)
3026 (if (assoc 'dummy source)
3027 (append source
3028 '((candidates "dummy")
3029 (accept-empty)
3030 (match identity)
3031 (filtered-candidate-transformer . anything-dummy-candidate)
3032 (disable-shortcuts)
3033 (volatile)))
3034 source))
3036 ;; (@* "Built-in plug-in: disable-shortcuts")
3037 (defvar anything-orig-enable-shortcuts nil)
3038 (defun anything-save-enable-shortcuts ()
3039 (anything-once
3040 (lambda ()
3041 (setq anything-orig-enable-shortcuts anything-enable-shortcuts
3042 anything-enable-shortcuts nil))))
3044 (defun anything-compile-source--disable-shortcuts (source)
3045 (if (assoc 'disable-shortcuts source)
3046 (append `((init ,@(anything-mklist (assoc-default 'init source))
3047 anything-save-enable-shortcuts)
3048 (resume ,@(anything-mklist (assoc-default 'resume source))
3049 anything-save-enable-shortcuts)
3050 (cleanup ,@(anything-mklist (assoc-default 'cleanup source))
3051 (lambda () (setq anything-enable-shortcuts
3052 anything-orig-enable-shortcuts))))
3053 source)
3054 source))
3056 ;; (@* "Built-in plug-in: candidates-in-buffer")
3057 (defun anything-candidates-in-buffer ()
3058 "Get candidates from the candidates buffer according to `anything-pattern'.
3060 BUFFER is `anything-candidate-buffer' by default. Each
3061 candidate must be placed in one line. This function is meant to
3062 be used in candidates-in-buffer or candidates attribute of an
3063 anything source. Especially fast for many (1000+) candidates.
3066 '((name . \"many files\")
3067 (init . (lambda () (with-current-buffer (anything-candidate-buffer 'local)
3068 (insert-many-filenames))))
3069 (search re-search-forward) ; optional
3070 (candidates-in-buffer)
3071 (type . file))
3073 +===============================================================+
3074 | The new way of making and narrowing candidates: Using buffers |
3075 +===============================================================+
3077 By default, `anything' makes candidates by evaluating the
3078 candidates function, then narrows them by `string-match' for each
3079 candidate.
3081 But this way is very slow for many candidates. The new way is
3082 storing all candidates in a buffer and narrowing them by
3083 `re-search-forward'. Search function is customizable by search
3084 attribute. The important point is that buffer processing is MUCH
3085 FASTER than string list processing and is the Emacs way.
3087 The init function writes all candidates to a newly-created
3088 candidate buffer. The candidates buffer is created or specified
3089 by `anything-candidate-buffer'. Candidates are stored in a line.
3091 The candidates function narrows all candidates, IOW creates a
3092 subset of candidates dynamically. It is the task of
3093 `anything-candidates-in-buffer'. As long as
3094 `anything-candidate-buffer' is used,`(candidates-in-buffer)' is
3095 sufficient in most cases.
3097 Note that `(candidates-in-buffer)' is shortcut of three attributes:
3098 (candidates . anything-candidates-in-buffer)
3099 (volatile)
3100 (match identity)
3101 And `(candidates-in-buffer . func)' is shortcut of three attributes:
3102 (candidates . func)
3103 (volatile)
3104 (match identity)
3105 The expansion is performed in `anything-get-sources'.
3107 The candidates-in-buffer attribute implies the volatile attribute.
3108 The volatile attribute is needed because `anything-candidates-in-buffer'
3109 creates candidates dynamically and need to be called everytime
3110 `anything-pattern' changes.
3112 Because `anything-candidates-in-buffer' plays the role of `match' attribute
3113 function, specifying `(match identity)' makes the source slightly faster.
3115 To customize `anything-candidates-in-buffer' behavior, use search,
3116 get-line and search-from-end attributes. See also `anything-sources' docstring."
3117 (declare (special source))
3118 (anything-candidates-in-buffer-1 (anything-candidate-buffer)
3119 anything-pattern
3120 (or (assoc-default 'get-line source)
3121 #'buffer-substring-no-properties)
3122 ;; use external variable `source'.
3123 (or (assoc-default 'search source)
3124 (if (assoc 'search-from-end source)
3125 '(re-search-backward)
3126 '(re-search-forward)))
3127 (anything-candidate-number-limit source)
3128 (assoc 'search-from-end source)))
3130 (defun anything-candidates-in-buffer-1 (buffer pattern get-line-fn search-fns limit search-from-end)
3131 ;; buffer == nil when candidates buffer does not exist.
3132 (when buffer
3133 (with-current-buffer buffer
3134 (let ((start-point (if search-from-end (point-max) (point-min)))
3135 (endp (if search-from-end #'bobp #'eobp)))
3136 (goto-char (1- start-point))
3137 (if (string= pattern "")
3138 (anything-initial-candidates-from-candidate-buffer
3139 endp get-line-fn limit search-from-end)
3140 (anything-search-from-candidate-buffer
3141 pattern get-line-fn search-fns limit search-from-end
3142 start-point endp))))))
3144 (defun anything-point-is-moved (proc)
3145 "If point is moved after executing PROC, return t, otherwise nil."
3146 (/= (point) (progn (funcall proc) (point))))
3148 (defun anything-search-from-candidate-buffer (pattern get-line-fn search-fns
3149 limit search-from-end
3150 start-point endp)
3151 (let (buffer-read-only
3152 matches exit newmatches)
3153 (anything-search-from-candidate-buffer-internal
3154 (lambda ()
3155 (clrhash anything-cib-hash)
3156 (dolist (searcher search-fns)
3157 (goto-char start-point)
3158 (setq newmatches nil)
3159 (loop with item-count = 0
3160 while (funcall searcher pattern nil t)
3161 for cand = (funcall get-line-fn (point-at-bol) (point-at-eol))
3162 do (anything-accumulate-candidates-internal
3163 cand newmatches anything-cib-hash item-count limit)
3164 unless (anything-point-is-moved
3165 (lambda ()
3166 (if search-from-end
3167 (goto-char (1- (point-at-bol)))
3168 (forward-line 1))))
3169 return nil)
3170 (setq matches (append matches (nreverse newmatches)))
3171 (if exit (return)))
3172 (delq nil matches)))))
3174 (defun anything-initial-candidates-from-candidate-buffer (endp get-line-fn limit search-from-end)
3175 (delq nil (loop with next-line-fn =
3176 (if search-from-end
3177 (lambda (x) (goto-char (max (1- (point-at-bol)) 1)))
3178 #'forward-line)
3179 until (funcall endp)
3180 for i from 1 to limit
3181 collect (funcall get-line-fn (point-at-bol) (point-at-eol))
3182 do (funcall next-line-fn 1))))
3184 (defun anything-search-from-candidate-buffer-internal (search-fn)
3185 (goto-char (point-min))
3186 (insert "\n")
3187 (goto-char (point-max))
3188 (insert "\n")
3189 (unwind-protect
3190 (funcall search-fn)
3191 (goto-char (point-min))
3192 (delete-char 1)
3193 (goto-char (1- (point-max)))
3194 (delete-char 1)
3196 (set-buffer-modified-p nil)))
3198 (defun anything-candidate-buffer (&optional create-or-buffer)
3199 "Register and return a buffer containing candidates of current source.
3200 `anything-candidate-buffer' searches buffer-local candidates buffer first,
3201 then global candidates buffer.
3203 Acceptable values of CREATE-OR-BUFFER:
3205 - nil (omit)
3206 Only return the candidates buffer.
3207 - a buffer
3208 Register a buffer as a candidates buffer.
3209 - 'global
3210 Create a new global candidates buffer,
3211 named \" *anything candidates:SOURCE*\".
3212 - other non-nil value
3213 Create a new local candidates buffer,
3214 named \" *anything candidates:SOURCE*ANYTHING-CURRENT-BUFFER\"."
3215 (let* ((global-bname (format " *anything candidates:%s*" anything-source-name))
3216 (local-bname (format " *anything candidates:%s*%s"
3217 anything-source-name
3218 (buffer-name anything-current-buffer)))
3219 (register-func
3220 (lambda ()
3221 (setq anything-candidate-buffer-alist
3222 (cons (cons anything-source-name create-or-buffer)
3223 (delete (assoc anything-source-name
3224 anything-candidate-buffer-alist)
3225 anything-candidate-buffer-alist)))))
3226 (kill-buffers-func
3227 (lambda ()
3228 (loop for b in (buffer-list)
3229 if (string-match (format "^%s" (regexp-quote global-bname))
3230 (buffer-name b))
3231 do (kill-buffer b))))
3232 (create-func
3233 (lambda ()
3234 (with-current-buffer
3235 (get-buffer-create (if (eq create-or-buffer 'global)
3236 global-bname
3237 local-bname))
3238 (buffer-disable-undo)
3239 (erase-buffer)
3240 (font-lock-mode -1))))
3241 (return-func
3242 (lambda ()
3243 (or (get-buffer local-bname)
3244 (get-buffer global-bname)
3245 (anything-aif (assoc-default anything-source-name
3246 anything-candidate-buffer-alist)
3247 (and (buffer-live-p it) it))))))
3248 (when create-or-buffer
3249 (funcall register-func)
3250 (unless (bufferp create-or-buffer)
3251 (and (eq create-or-buffer 'global) (funcall kill-buffers-func))
3252 (funcall create-func)))
3253 (funcall return-func)))
3255 (defun anything-compile-source--candidates-in-buffer (source)
3256 (anything-aif (assoc 'candidates-in-buffer source)
3257 (append source
3258 `((candidates . ,(or (cdr it) 'anything-candidates-in-buffer))
3259 (volatile) (match identity)))
3260 source))
3262 ;; (@* "Utility: resplit anything window")
3263 (defun anything-toggle-resplit-window ()
3264 "Toggle resplit anything window, vertically or horizontally."
3265 (interactive)
3266 (with-anything-window
3267 (let ((before-height (window-height)))
3268 (delete-window)
3269 (set-window-buffer
3270 (select-window (if (= (window-height) before-height)
3271 (prog1
3272 (split-window-vertically)
3273 (setq anything-split-window-state 'vertical))
3274 (setq anything-split-window-state 'horizontal)
3275 (split-window-horizontally)))
3276 anything-buffer))))
3278 ;; (@* "Utility: Resize anything window.")
3279 (defun anything-enlarge-window-1 (n)
3280 "Enlarge or narrow anything window.
3281 If N is positive enlarge, if negative narrow."
3282 (unless anything-samewindow
3283 (let ((horizontal-p (eq anything-split-window-state 'horizontal)))
3284 (with-anything-window
3285 (enlarge-window n horizontal-p)))))
3287 (defun anything-narrow-window ()
3288 "Narrow anything window."
3289 (interactive)
3290 (anything-enlarge-window-1 -1))
3292 (defun anything-enlarge-window ()
3293 "Enlarge anything window."
3294 (interactive)
3295 (anything-enlarge-window-1 1))
3297 ;; (@* "Utility: select another action by key")
3298 (defun anything-select-nth-action (n)
3299 "Select the N nth action for the currently selected candidate."
3300 (setq anything-saved-selection (anything-get-selection))
3301 (unless anything-saved-selection
3302 (error "Nothing is selected"))
3303 (setq anything-saved-action (anything-get-nth-action n (anything-get-action)))
3304 (anything-exit-minibuffer))
3306 (defun anything-get-nth-action (n action)
3307 (cond ((and (zerop n) (functionp action))
3308 action)
3309 ((listp action)
3310 (or (cdr (elt action n))
3311 (error "No such action")))
3312 ((and (functionp action) (< 0 n))
3313 (error "Sole action"))
3315 (error "Error in `anything-select-nth-action'"))))
3317 (defun anything-select-2nd-action ()
3318 "Select the 2nd action for the currently selected candidate."
3319 (interactive)
3320 (anything-select-nth-action 1))
3322 (defun anything-select-3rd-action ()
3323 "Select the 3rd action for the currently selected candidate."
3324 (interactive)
3325 (anything-select-nth-action 2))
3327 (defun anything-select-4th-action ()
3328 "Select the 4th action for the currently selected candidate."
3329 (interactive)
3330 (anything-select-nth-action 3))
3332 (defun anything-select-2nd-action-or-end-of-line ()
3333 "Select the 2nd action for the currently selected candidate.
3334 This happen when point is at the end of minibuffer.
3335 Otherwise goto the end of minibuffer."
3336 (interactive)
3337 (if (eolp)
3338 (anything-select-nth-action 1)
3339 (end-of-line)))
3341 ;; (@* "Utility: Persistent Action")
3342 (defmacro with-anything-display-same-window (&rest body)
3343 "Execute BODY in the window used for persistent action.
3344 Make `pop-to-buffer' and `display-buffer' display in the same window."
3345 (declare (indent 0) (debug t))
3346 `(let ((display-buffer-function 'anything-persistent-action-display-buffer))
3347 ,@body))
3349 (defvar anything-persistent-action-display-window nil)
3350 (defun anything-initialize-persistent-action ()
3351 (set (make-local-variable 'anything-persistent-action-display-window) nil))
3353 (defun* anything-execute-persistent-action (&optional (attr 'persistent-action) onewindow)
3354 "Perform the associated action ATTR without quitting anything.
3355 ATTR default is 'persistent-action', but it can be anything else.
3356 In this case you have to add this new attribute to your source.
3357 When `anything-samewindow' and ONEWINDOW are non--nil,
3358 the anything window is never split in persistent action."
3359 (interactive)
3360 (anything-log "executing persistent-action")
3361 (with-anything-window
3362 (save-selected-window
3363 (anything-select-persistent-action-window onewindow)
3364 (anything-log-eval (current-buffer))
3365 (let ((anything-in-persistent-action t))
3366 (with-anything-display-same-window
3367 (anything-execute-selection-action
3369 (or (assoc-default attr (anything-get-current-source))
3370 (anything-get-action))
3372 (anything-log-run-hook 'anything-after-persistent-action-hook))))))
3375 (defun anything-persistent-action-display-window (&optional onewindow)
3376 "Return the window that will be used for presistent action.
3377 If ONEWINDOW is non--nil window will not be splitted in persistent action
3378 if `anything-samewindow' is non--nil also."
3379 (with-anything-window
3380 (setq anything-persistent-action-display-window
3381 (cond ((window-live-p anything-persistent-action-display-window)
3382 anything-persistent-action-display-window)
3383 ((and anything-samewindow (one-window-p t) (not onewindow))
3384 (split-window))
3385 ((get-buffer-window anything-current-buffer))
3387 (next-window (selected-window) 1))))))
3389 (defun anything-select-persistent-action-window (&optional onewindow)
3390 "Select the window that will be used for persistent action.
3391 See `anything-persistent-action-display-window' for how to use ONEWINDOW."
3392 (select-window (get-buffer-window (anything-buffer-get)))
3393 (select-window
3394 (setq minibuffer-scroll-window
3395 (anything-persistent-action-display-window onewindow))))
3397 (defun anything-persistent-action-display-buffer (buf &optional not-this-window)
3398 "Make `pop-to-buffer' and `display-buffer' display in the same window.
3399 If `anything-persistent-action-use-special-display' is non-nil and
3400 BUF is to be displayed by `special-display-function', use it.
3401 Otherwise ignores `special-display-buffer-names' and `special-display-regexps'.
3402 Argument NOT-THIS-WINDOW if present will be used as
3403 second argument of `display-buffer'."
3404 (let* ((name (buffer-name buf))
3405 display-buffer-function pop-up-windows pop-up-frames
3406 (same-window-regexps
3407 (unless (and anything-persistent-action-use-special-display
3408 (or (member name
3409 (mapcar (lambda (x) (or (car-safe x) x))
3410 special-display-buffer-names))
3411 (remove-if-not
3412 (lambda (x) (string-match (or (car-safe x) x) name))
3413 special-display-regexps)))
3414 '("."))))
3415 (display-buffer buf not-this-window)))
3417 ;; scroll-other-window(-down)? for persistent-action
3418 (defun anything-scroll-other-window-base (command)
3419 (with-selected-window (anything-persistent-action-display-window)
3420 (funcall command anything-scroll-amount)))
3422 (defun anything-scroll-other-window ()
3423 "Scroll other window (not *Anything* window) upward."
3424 (interactive)
3425 (anything-scroll-other-window-base 'scroll-up))
3427 (defun anything-scroll-other-window-down ()
3428 "Scroll other window (not *Anything* window) downward."
3429 (interactive)
3430 (anything-scroll-other-window-base 'scroll-down))
3432 ;; (@* "Utility: Visible Mark")
3433 (defface anything-visible-mark
3434 '((((min-colors 88) (background dark))
3435 (:background "green1" :foreground "black"))
3436 (((background dark)) (:background "green" :foreground "black"))
3437 (((min-colors 88)) (:background "green1"))
3438 (t (:background "green")))
3439 "Face for visible mark."
3440 :group 'anything)
3442 (defvar anything-visible-mark-face 'anything-visible-mark)
3443 (defvar anything-visible-mark-overlays nil)
3445 (defun anything-clear-visible-mark ()
3446 (with-current-buffer (anything-buffer-get)
3447 (mapc 'delete-overlay anything-visible-mark-overlays)
3448 (set (make-local-variable 'anything-visible-mark-overlays) nil)))
3449 (add-hook 'anything-after-initialize-hook 'anything-clear-visible-mark)
3451 (defvar anything-marked-candidates nil
3452 "Marked candadates. List of \(source . real\) pair.")
3454 (defun anything-this-visible-mark ()
3455 (loop for o in anything-visible-mark-overlays
3456 when (equal (point-at-bol) (overlay-start o))
3457 do (return o)))
3459 (defun anything-delete-visible-mark (overlay)
3460 (setq anything-marked-candidates
3461 (remove
3462 (cons (anything-get-current-source) (anything-get-selection))
3463 anything-marked-candidates))
3464 (delete-overlay overlay)
3465 (setq anything-visible-mark-overlays
3466 (delq overlay anything-visible-mark-overlays)))
3468 (defun anything-make-visible-mark ()
3469 (let ((o (make-overlay (point-at-bol) (1+ (point-at-eol)))))
3470 (overlay-put o 'face anything-visible-mark-face)
3471 (overlay-put o 'source (assoc-default 'name (anything-get-current-source)))
3472 (overlay-put o 'string (buffer-substring (overlay-start o) (overlay-end o)))
3473 (overlay-put o 'real (anything-get-selection))
3474 (add-to-list 'anything-visible-mark-overlays o))
3475 (push (cons (anything-get-current-source) (anything-get-selection))
3476 anything-marked-candidates))
3478 (defun anything-toggle-visible-mark ()
3479 "Toggle anything visible mark at point."
3480 (interactive)
3481 (with-anything-window
3482 (anything-aif (anything-this-visible-mark)
3483 (anything-delete-visible-mark it)
3484 (anything-make-visible-mark))
3485 (anything-next-line)))
3487 (defun anything-display-all-visible-marks ()
3488 "Show all `anything' visible marks strings."
3489 (interactive)
3490 (with-anything-window
3491 (lexical-let ((overlays (reverse anything-visible-mark-overlays)))
3492 (anything-run-after-quit
3493 (lambda ()
3494 (with-output-to-temp-buffer "*anything visible marks*"
3495 (dolist (o overlays) (princ (overlay-get o 'string)))))))))
3497 (defun anything-marked-candidates ()
3498 "Return marked candidates of current source if any.
3499 Otherwise one element list of current selection.
3501 It is analogous to `dired-get-marked-files'."
3502 (with-current-buffer (anything-buffer-get)
3503 (let ((cands
3504 (if anything-marked-candidates
3505 (loop with current-src = (anything-get-current-source)
3506 for (source . real) in (reverse anything-marked-candidates)
3507 when (equal current-src source)
3508 collect (anything-coerce-selection real source))
3509 (list (anything-get-selection)))))
3510 (anything-log-eval cands)
3511 cands)))
3513 (defun anything-reset-marked-candidates ()
3514 (with-current-buffer (anything-buffer-get)
3515 (set (make-local-variable 'anything-marked-candidates) nil)))
3517 (add-hook 'anything-after-initialize-hook 'anything-reset-marked-candidates)
3518 ;; (add-hook 'anything-after-action-hook 'anything-reset-marked-candidates)
3520 (defun anything-current-source-name= (name)
3521 (save-excursion
3522 (goto-char (anything-get-previous-header-pos))
3523 (equal name (anything-current-line-contents))))
3525 (defun anything-revive-visible-mark ()
3526 "Restore marked candidates when anything update display."
3527 (with-current-buffer anything-buffer
3528 (dolist (o anything-visible-mark-overlays)
3529 (goto-char (point-min))
3530 (while (and (search-forward (overlay-get o 'string) nil t)
3531 (anything-current-source-name= (overlay-get o 'source)))
3532 ;; Calculate real value of candidate.
3533 ;; It can be nil if candidate have only a display value.
3534 (let ((real (get-text-property (point-at-bol 0) 'anything-realvalue)))
3535 (if real
3536 ;; Check if real value of current candidate is the same
3537 ;; that the one stored in overlay.
3538 (and (string= (overlay-get o 'real) real)
3539 (move-overlay o (point-at-bol 0) (1+ (point-at-eol 0))))
3540 (move-overlay o (point-at-bol 0) (1+ (point-at-eol 0)))))))))
3541 (add-hook 'anything-update-hook 'anything-revive-visible-mark)
3543 (defun anything-next-point-in-list (curpos points &optional prev)
3544 (cond
3545 ;; rule out special cases
3546 ((null points) curpos)
3547 ((and prev (< curpos (car points))) curpos)
3548 ((< (car (last points)) curpos)
3549 (if prev (car (last points)) curpos))
3551 (nth (if prev
3552 (loop for pt in points
3553 for i from 0
3554 if (<= curpos pt)
3555 do (return (1- i)))
3556 (loop for pt in points
3557 for i from 0
3558 if (< curpos pt)
3559 do (return i)))
3560 points))))
3562 (defun anything-next-visible-mark (&optional prev)
3563 "Move next anything visible mark.
3564 If PREV is non-nil move to precedent."
3565 (interactive)
3566 (with-anything-window
3567 (goto-char (anything-next-point-in-list
3568 (point)
3569 (sort (mapcar 'overlay-start anything-visible-mark-overlays) '<)
3570 prev))
3571 (anything-mark-current-line)))
3573 (defun anything-prev-visible-mark ()
3574 "Move previous anything visible mark."
3575 (interactive)
3576 (anything-next-visible-mark t))
3578 ;; (@* "Utility: `find-file' integration")
3579 (defun anything-quit-and-find-file ()
3580 "Drop into `find-file' from `anything' like `iswitchb-find-file'.
3581 If current selection is a buffer or a file, `find-file' from its directory."
3582 (interactive)
3583 (anything-run-after-quit
3584 (lambda (f)
3585 (if (file-exists-p f)
3586 (let ((default-directory (file-name-directory f)))
3587 (call-interactively 'find-file))
3588 (call-interactively 'find-file)))
3589 (anything-aif (get-buffer (anything-get-selection))
3590 (buffer-file-name it)
3591 (expand-file-name (anything-get-selection)))))
3593 ;; (@* "Utility: Selection Paste")
3594 (defun anything-yank-selection ()
3595 "Set minibuffer contents to current selection."
3596 (interactive)
3597 (anything-set-pattern (anything-get-selection nil t)))
3599 (defun anything-kill-selection-and-quit ()
3600 "Store current selection to kill ring.
3601 You can paste it by typing \\[yank]."
3602 (interactive)
3603 (anything-run-after-quit
3604 (lambda (sel)
3605 (kill-new sel)
3606 (message "Killed: %s" sel))
3607 (anything-get-selection nil t)))
3610 ;; (@* "Utility: Automatical execution of persistent-action")
3611 (add-to-list 'minor-mode-alist '(anything-follow-mode " AFollow"))
3612 (defun anything-follow-mode ()
3613 "If this mode is on, persistent action is executed everytime the cursor is moved."
3614 (interactive)
3615 (with-current-buffer anything-buffer
3616 (setq anything-follow-mode (not anything-follow-mode))
3617 (message "anything-follow-mode is %s"
3618 (if anything-follow-mode "enabled" "disabled"))))
3620 (defun anything-follow-execute-persistent-action-maybe ()
3621 "Execute persistent action in mode `anything-follow-mode'.
3622 This happen after `anything-input-idle-delay' secs."
3623 (and (not (get-buffer-window anything-action-buffer 'visible))
3624 (buffer-local-value 'anything-follow-mode
3625 (get-buffer-create anything-buffer))
3626 (sit-for anything-input-idle-delay)
3627 (anything-window)
3628 (anything-get-selection)
3629 (save-excursion
3630 (anything-execute-persistent-action))))
3632 ;; (@* "Utility: Migrate `anything-sources' to my-anything command")
3633 (defun anything-migrate-sources ()
3634 "Help to migrate to new `anything' way."
3635 (interactive)
3636 (with-current-buffer (get-buffer-create "*anything migrate*")
3637 (erase-buffer)
3638 (insert (format "\
3639 Setting `anything-sources' directly is not good because
3640 `anything' is not for one command. For now, interactive use of
3641 `anything' (M-x anything) is only for demonstration purpose.
3642 So you should define commands calling `anything'.
3643 I help you to migrate to the new way.
3645 The code below is automatically generated from current
3646 `anything-sources' value. You can use the `my-anything' command
3647 now!
3649 Copy and paste it to your .emacs. Then substitute `my-anything'
3650 for `anything' bindings in all `define-key', `local-set-key' and
3651 `global-set-key' calls.
3653 \(defun my-anything ()
3654 \"Anything command for you.
3656 It is automatically generated by `anything-migrate-sources'.\"
3657 (interactive)
3658 (anything-other-buffer
3660 \"*my-anything*\"))
3661 " anything-sources))
3662 (eval-last-sexp nil)
3663 (substitute-key-definition 'anything 'my-anything global-map)
3664 (pop-to-buffer (current-buffer))))
3666 ;; (@* "Compatibility")
3668 ;; Copied assoc-default from XEmacs version 21.5.12
3669 (unless (fboundp 'assoc-default)
3670 (defun assoc-default (key alist &optional test default)
3671 "Find object KEY in a pseudo-alist ALIST.
3672 ALIST is a list of conses or objects. Each element (or the element's car,
3673 if it is a cons) is compared with KEY by evaluating (TEST (car elt) KEY).
3674 If that is non-nil, the element matches;
3675 then `assoc-default' returns the element's cdr, if it is a cons,
3676 or DEFAULT if the element is not a cons.
3678 If no element matches, the value is nil.
3679 If TEST is omitted or nil, `equal' is used."
3680 (let (found (tail alist) value)
3681 (while (and tail (not found))
3682 (let ((elt (car tail)))
3683 (when (funcall (or test 'equal) (if (consp elt) (car elt) elt) key)
3684 (setq found t value (if (consp elt) (cdr elt) default))))
3685 (setq tail (cdr tail)))
3686 value)))
3688 ;; Function not available in XEmacs,
3689 (unless (fboundp 'minibuffer-contents)
3690 (defun minibuffer-contents ()
3691 "Return the user input in a minbuffer as a string.
3692 The current buffer must be a minibuffer."
3693 (field-string (point-max)))
3695 (defun delete-minibuffer-contents ()
3696 "Delete all user input in a minibuffer.
3697 The current buffer must be a minibuffer."
3698 (delete-field (point-max))))
3700 ;; Function not available in older Emacs (<= 22.1).
3701 (unless (fboundp 'buffer-chars-modified-tick)
3702 (defun buffer-chars-modified-tick (&optional buffer)
3703 "Return BUFFER's character-change tick counter.
3704 Each buffer has a character-change tick counter, which is set to the
3705 value of the buffer's tick counter (see `buffer-modified-tick'), each
3706 time text in that buffer is inserted or deleted. By comparing the
3707 values returned by two individual calls of `buffer-chars-modified-tick',
3708 you can tell whether a character change occurred in that buffer in
3709 between these calls. No argument or nil as argument means use current
3710 buffer as BUFFER."
3711 (with-current-buffer (or buffer (current-buffer))
3712 (if (listp buffer-undo-list)
3713 (length buffer-undo-list)
3714 (buffer-modified-tick)))))
3716 ;; (@* "CUA workaround")
3717 (defadvice cua-delete-region (around anything-avoid-cua activate)
3718 (ignore-errors ad-do-it))
3720 (defadvice copy-region-as-kill (around anything-avoid-cua activate)
3721 (if cua-mode
3722 (ignore-errors ad-do-it)
3723 ad-do-it))
3725 ;;(@* "Attribute Documentation")
3726 (defun anything-describe-anything-attribute (anything-attribute)
3727 "Display the full documentation of ANYTHING-ATTRIBUTE.
3728 ANYTHING-ATTRIBUTE should be a symbol."
3729 (interactive (list (intern
3730 (completing-read
3731 "Describe anything attribute: "
3732 (mapcar 'symbol-name anything-additional-attributes)))))
3733 (with-output-to-temp-buffer "*Help*"
3734 (princ (get anything-attribute 'anything-attrdoc))))
3736 (anything-document-attribute 'name "mandatory"
3737 " The name of the source. It is also the heading which appears
3738 above the list of matches from the source. Must be unique. ")
3739 (anything-document-attribute 'header-name "optional"
3740 " A function returning the display string of the header. Its
3741 argument is the name of the source. This attribute is useful to
3742 add an additional information with the source name. ")
3743 (anything-document-attribute 'candidates "mandatory if candidates-in-buffer attribute is not provided"
3744 " Specifies how to retrieve candidates from the source. It can
3745 either be a variable name, a function called with no parameters
3746 or the actual list of candidates.
3748 The list must be a list whose members are strings, symbols
3749 or (DISPLAY . REAL) pairs.
3751 In case of (DISPLAY . REAL) pairs, the DISPLAY string is shown
3752 in the Anything buffer, but the REAL one is used as action
3753 argument when the candidate is selected. This allows a more
3754 readable presentation for candidates which would otherwise be,
3755 for example, too long or have a common part shared with other
3756 candidates which can be safely replaced with an abbreviated
3757 string for display purposes.
3759 Note that if the (DISPLAY . REAL) form is used then pattern
3760 matching is done on the displayed string, not on the real
3761 value.
3763 If the candidates have to be retrieved asynchronously (for
3764 example, by an external command which takes a while to run)
3765 then the function should start the external command
3766 asynchronously and return the associated process object.
3767 Anything will take care of managing the process (receiving the
3768 output from it, killing it if necessary, etc.). The process
3769 should return candidates matching the current pattern (see
3770 variable `anything-pattern'.)
3772 Note that currently results from asynchronous sources appear
3773 last in the anything buffer regardless of their position in
3774 `anything-sources'. ")
3775 (anything-document-attribute 'action "mandatory if type attribute is not provided"
3776 " It is a list of (DISPLAY . FUNCTION) pairs or FUNCTION.
3777 FUNCTION is called with one parameter: the selected candidate.
3779 An action other than the default can be chosen from this list
3780 of actions for the currently selected candidate (by default
3781 with TAB). The DISPLAY string is shown in the completions
3782 buffer and the FUNCTION is invoked when an action is
3783 selected. The first action of the list is the default. ")
3784 (anything-document-attribute 'coerce "optional"
3785 " It's a function called with one argument: the selected candidate.
3787 This function is intended for type convertion.
3788 In normal case, the selected candidate (string) is passed to action function.
3789 If coerce function is specified, it is called just before action function.
3791 Example: converting string to symbol
3792 (coerce . intern) ")
3793 (anything-document-attribute 'type "optional if action attribute is provided"
3794 " Indicates the type of the items the source returns.
3796 Merge attributes not specified in the source itself from
3797 `anything-type-attributes'.
3799 This attribute is implemented by plug-in. ")
3800 (anything-document-attribute 'init "optional"
3801 " Function called with no parameters when anything is started. It
3802 is useful for collecting current state information which can be
3803 used to create the list of candidates later.
3805 For example, if a source needs to work with the current
3806 directory then it can store its value here, because later
3807 anything does its job in the minibuffer and in the
3808 `anything-buffer' and the current directory can be different
3809 there. ")
3810 (anything-document-attribute 'delayed-init "optional"
3811 " Function called with no parameters before candidate function is
3812 called. It is similar with `init' attribute, but its
3813 evaluation is deferred. It is useful to combine with ")
3814 (anything-document-attribute 'match "optional"
3815 " List of functions called with one parameter: a candidate. The
3816 function should return non-nil if the candidate matches the
3817 current pattern (see variable `anything-pattern').
3819 This attribute allows the source to override the default
3820 pattern matching based on `string-match'. It can be used, for
3821 example, to implement a source for file names and do the
3822 pattern matching on the basename of files, since it's more
3823 likely one is typing part of the basename when searching for a
3824 file, instead of some string anywhere else in its path.
3826 If the list contains more than one function then the list of
3827 matching candidates from the source is constructed by appending
3828 the results after invoking the first function on all the
3829 potential candidates, then the next function, and so on. The
3830 matching candidates supplied by the first function appear first
3831 in the list of results and then results from the other
3832 functions, respectively.
3834 This attribute has no effect for asynchronous sources (see
3835 attribute `candidates'), since they perform pattern matching
3836 themselves. ")
3837 (anything-document-attribute 'candidate-transformer "optional"
3838 " It's a function or a list of functions called with one argument
3839 when the completion list from the source is built. The argument
3840 is the list of candidates retrieved from the source. The
3841 function should return a transformed list of candidates which
3842 will be used for the actual completion. If it is a list of
3843 functions, it calls each function sequentially.
3845 This can be used to transform or remove items from the list of
3846 candidates.
3848 Note that `candidates' is run already, so the given transformer
3849 function should also be able to handle candidates with (DISPLAY
3850 . REAL) format. ")
3851 (anything-document-attribute 'filtered-candidate-transformer "optional"
3852 " It has the same format as `candidate-transformer', except the
3853 function is called with two parameters: the candidate list and
3854 the source.
3856 This transformer is run on the candidate list which is already
3857 filtered by the current pattern. While `candidate-transformer'
3858 is run only once, it is run every time the input pattern is
3859 changed.
3861 It can be used to transform the candidate list dynamically, for
3862 example, based on the current pattern.
3864 In some cases it may also be more efficent to perform candidate
3865 transformation here, instead of with `candidate-transformer'
3866 even if this transformation is done every time the pattern is
3867 changed. For example, if a candidate set is very large then
3868 `candidate-transformer' transforms every candidate while only
3869 some of them will actually be dislpayed due to the limit
3870 imposed by `anything-candidate-number-limit'.
3872 Note that `candidates' and `candidate-transformer' is run
3873 already, so the given transformer function should also be able
3874 to handle candidates with (DISPLAY . REAL) format.
3876 This option has no effect for asynchronous sources. (Not yet,
3877 at least. ")
3878 (anything-document-attribute 'action-transformer "optional"
3879 " It's a function or a list of functions called with two
3880 arguments when the action list from the source is
3881 assembled. The first argument is the list of actions, the
3882 second is the current selection. If it is a list of functions,
3883 it calls each function sequentially.
3885 The function should return a transformed action list.
3887 This can be used to customize the list of actions based on the
3888 currently selected candidate. ")
3889 (anything-document-attribute 'pattern-transformer "optional"
3890 " It's a function or a list of functions called with one argument
3891 before computing matches. Its argument is `anything-pattern'.
3892 Functions should return transformed `anything-pattern'.
3894 It is useful to change interpretation of `anything-pattern'. ")
3895 (anything-document-attribute 'delayed "optional"
3896 " Candidates from the source are shown only if the user stops
3897 typing and is idle for `anything-idle-delay' seconds. ")
3898 (anything-document-attribute 'volatile "optional"
3899 " Indicates the source assembles the candidate list dynamically,
3900 so it shouldn't be cached within a single Anything
3901 invocation. It is only applicable to synchronous sources,
3902 because asynchronous sources are not cached. ")
3903 (anything-document-attribute 'requires-pattern "optional"
3904 " If present matches from the source are shown only if the
3905 pattern is not empty. Optionally, it can have an integer
3906 parameter specifying the required length of input which is
3907 useful in case of sources with lots of candidates. ")
3908 (anything-document-attribute 'persistent-action "optional"
3909 " Function called with one parameter; the selected candidate.
3911 An action performed by `anything-execute-persistent-action'.
3912 If none, use the default action. ")
3913 (anything-document-attribute 'candidates-in-buffer "optional"
3914 " Shortcut attribute for making and narrowing candidates using
3915 buffers. This newly-introduced attribute prevents us from
3916 forgetting to add volatile and match attributes.
3918 See docstring of `anything-candidates-in-buffer'.
3920 (candidates-in-buffer) is equivalent of three attributes:
3921 (candidates . anything-candidates-in-buffer)
3922 (volatile)
3923 (match identity)
3925 (candidates-in-buffer . candidates-function) is equivalent of:
3926 (candidates . candidates-function)
3927 (volatile)
3928 (match identity)
3930 This attribute is implemented by plug-in. ")
3931 (anything-document-attribute 'search "optional"
3932 " List of functions like `re-search-forward' or `search-forward'.
3933 Buffer search function used by `anything-candidates-in-buffer'.
3934 By default, `anything-candidates-in-buffer' uses `re-search-forward'.
3935 This attribute is meant to be used with
3936 (candidates . anything-candidates-in-buffer) or
3937 (candidates-in-buffer) in short. ")
3938 (anything-document-attribute 'search-from-end "optional"
3939 " Make `anything-candidates-in-buffer' search from the end of buffer.
3940 If this attribute is specified, `anything-candidates-in-buffer' uses
3941 `re-search-backward' instead. ")
3942 (anything-document-attribute 'get-line "optional"
3943 " A function like `buffer-substring-no-properties' or `buffer-substring'.
3944 This function converts point of line-beginning and point of line-end,
3945 which represents a candidate computed by `anything-candidates-in-buffer'.
3946 By default, `anything-candidates-in-buffer' uses
3947 `buffer-substring-no-properties'. ")
3948 (anything-document-attribute 'display-to-real "optional"
3949 " Function called with one parameter; the selected candidate.
3951 The function transforms the selected candidate, and the result
3952 is passed to the action function. The display-to-real
3953 attribute provides another way to pass other string than one
3954 shown in Anything buffer.
3956 Traditionally, it is possible to make candidates,
3957 candidate-transformer or filtered-candidate-transformer
3958 function return a list with (DISPLAY . REAL) pairs. But if REAL
3959 can be generated from DISPLAY, display-to-real is more
3960 convenient and faster. ")
3961 (anything-document-attribute 'real-to-display "optional"
3962 " Function called with one parameter; the selected candidate.
3964 The inverse of display-to-real attribute.
3966 The function transforms the selected candidate, which is passed
3967 to the action function, for display. The real-to-display
3968 attribute provides the other way to pass other string than one
3969 shown in Anything buffer.
3971 Traditionally, it is possible to make candidates,
3972 candidate-transformer or filtered-candidate-transformer
3973 function return a list with (DISPLAY . REAL) pairs. But if
3974 DISPLAY can be generated from REAL, real-to-display is more
3975 convenient.
3977 Note that DISPLAY parts returned from candidates /
3978 candidate-transformer are IGNORED as the name `display-to-real'
3979 says. ")
3980 (anything-document-attribute 'cleanup "optional"
3981 " Function called with no parameters when *anything* buffer is closed. It
3982 is useful for killing unneeded candidates buffer.
3984 Note that the function is executed BEFORE performing action. ")
3985 (anything-document-attribute 'candidate-number-limit "optional"
3986 " Override `anything-candidate-number-limit' only for this source. ")
3987 (anything-document-attribute 'accept-empty "optional"
3988 " Pass empty string \"\" to action function. ")
3989 (anything-document-attribute 'disable-shortcuts "optional"
3990 " Disable `anything-enable-shortcuts' in current `anything' session.
3992 This attribute is implemented by plug-in. ")
3993 (anything-document-attribute 'dummy "optional"
3994 " Set `anything-pattern' to candidate. If this attribute is
3995 specified, The candidates attribute is ignored.
3997 This attribute is implemented by plug-in.
3998 This plug-in implies disable-shortcuts plug-in. ")
3999 (anything-document-attribute 'multiline "optional"
4000 " Enable to selection multiline candidates. ")
4001 (anything-document-attribute 'update "optional"
4002 ;; FIXME: this is not displayed correctly in help buffer.
4003 " Function called with no parameters when \\<anything-map>\\[anything-force-update] is pressed. ")
4004 (anything-document-attribute 'mode-line "optional"
4005 " source local `anything-mode-line-string'. (included in `mode-line-format')
4006 It accepts also variable/function name. ")
4007 (anything-document-attribute 'header-line "optional"
4008 " source local `header-line-format'.
4009 It accepts also variable/function name. ")
4010 (anything-document-attribute
4011 'resume "optional"
4012 " Function called with no parameters when `anything-resume' is started.")
4014 ;; (@* "Bug Report")
4015 (defvar anything-maintainer-mail-address
4016 (concat "rubiki" "tch@ru" "by-lang.org"))
4017 (defvar anything-bug-report-salutation
4018 "Describe bug below, using a precise recipe.
4020 When I executed M-x ...
4022 How to send a bug report:
4023 1) Be sure to use the LATEST version of anything.el.
4024 2) Enable debugger. M-x toggle-debug-on-error or (setq debug-on-error t)
4025 3) Use Lisp version instead of compiled one: (load \"anything.el\")
4026 4) If you got an error, please paste *Backtrace* buffer.
4027 5) Type C-c C-c to send.
4028 # If you are a Japanese, please write in Japanese:-)")
4029 (defvar anything-no-dump-variables
4030 '(anything-candidate-buffer-alist
4031 anything-digit-overlays
4032 anything-help-message
4033 anything-candidate-cache
4035 "Variables not to dump in bug report.")
4037 (defun anything-dumped-variables-in-bug-report ()
4038 (let ((hash (make-hash-table)))
4039 (loop for var in (apropos-internal "anything-" 'boundp)
4040 for vname = (symbol-name var)
4041 unless (or (string-match "-map$" vname)
4042 (string-match "^anything-c-source-" vname)
4043 (string-match "-hash$" vname)
4044 (string-match "-face$" vname)
4045 (memq var anything-no-dump-variables))
4046 collect var)))
4048 (defun anything-send-bug-report ()
4049 "Send a bug report of anything.el."
4050 (interactive)
4051 (with-current-buffer (or anything-last-buffer
4052 (current-buffer))
4053 (reporter-submit-bug-report
4054 anything-maintainer-mail-address
4055 "anything.el"
4056 (anything-dumped-variables-in-bug-report)
4057 nil nil
4058 anything-bug-report-salutation)))
4060 (defun anything-send-bug-report-from-anything ()
4061 "Send a bug report of anything.el in anything session."
4062 (interactive)
4063 (anything-run-after-quit 'anything-send-bug-report))
4065 ;; Debugging function.
4066 (defun* anything-test-candidates
4067 (sources &optional (input "")
4068 (compile-source-functions
4069 anything-compile-source-functions-default))
4070 "Test helper function for anything.
4071 Given pseudo `anything-sources' and `anything-pattern', returns list like
4072 ((\"source name1\" (\"candidate1\" \"candidate2\"))
4073 (\"source name2\" (\"candidate3\" \"candidate4\")))"
4074 (let ((anything-test-mode t)
4075 anything-enable-shortcuts
4076 anything-candidate-cache
4077 (anything-compile-source-functions compile-source-functions)
4078 anything-before-initialize-hook
4079 anything-after-initialize-hook
4080 anything-update-hook
4081 anything-test-candidate-list)
4082 (get-buffer-create anything-buffer)
4083 (anything-initialize nil input sources)
4084 (anything-update)
4085 ;; test-mode spec: select 1st candidate!
4086 (with-current-buffer anything-buffer
4087 (forward-line 1)
4088 (anything-mark-current-line))
4089 (prog1
4090 anything-test-candidate-list
4091 (anything-cleanup))))
4093 ;; (@* "Unit Tests")
4094 ;; See developer-tools/unit-test-anything.el
4096 (provide 'anything)
4098 ;; How to save (DO NOT REMOVE!!)
4099 ;; (progn (magit-push) (emacswiki-post "anything.el"))
4101 ;; Local Variables:
4102 ;; coding: utf-8
4103 ;; End:
4105 ;;; anything.el ends here