anything-config.el (anything-show-this-source-only): revived
[anything-config.git] / anything.el
blob29c7f66525880984f5a7d8d9028bb95ce34fd178
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))
1535 ;;;###autoload
1536 (defun anything (&rest plist)
1537 "Main function to execute anything sources.
1538 When call interactively with no arguments deprecated `anything-sources'
1539 will be used if non--nil.
1541 PLIST is a list like \(:key1 val1 :key2 val2 ...\) or
1542 \(&optional sources input prompt resume preselect buffer keymap\).
1544 Basic keywords are the following:
1546 - :sources
1548 Temporary value of `anything-sources'. It also accepts a
1549 symbol, interpreted as a variable of an anything source. It
1550 also accepts an alist representing an anything source, which is
1551 detected by \(assq 'name ANY-SOURCES\)
1553 - :input
1555 Temporary value of `anything-pattern', ie. initial input of minibuffer.
1557 - :prompt
1559 Prompt other than \"pattern: \".
1561 - :resume
1563 If t, Resurrect previously instance of `anything'. Skip the initialization.
1564 If 'noresume, this instance of `anything' cannot be resumed.
1566 - :preselect
1568 Initially selected candidate. Specified by exact candidate or a regexp.
1569 Note that it is not working with delayed sources.
1571 - :buffer
1573 `anything-buffer' instead of *anything*.
1575 - :keymap
1577 `anything-map' for current `anything' session.
1580 Of course, conventional arguments are supported, the two are same.
1582 \(anything :sources sources :input input :prompt prompt :resume resume
1583 :preselect preselect :buffer buffer :keymap keymap\)
1584 \(anything sources input prompt resume preselect buffer keymap\)
1587 Other keywords are interpreted as local variables of this anything session.
1588 The `anything-' prefix can be omitted. For example,
1590 \(anything :sources 'anything-c-source-buffers
1591 :buffer \"*buffers*\" :candidate-number-limit 10\)
1593 means starting anything session with `anything-c-source-buffers'
1594 source in *buffers* buffer and set variable `anything-candidate-number-limit'
1595 to 10 as session local variable."
1596 (interactive)
1597 (if (keywordp (car plist))
1598 (anything-let-internal
1599 (anything-parse-keys plist)
1600 (lambda ()
1601 (apply 'anything
1602 (mapcar (lambda (key) (plist-get plist key))
1603 anything-argument-keys))))
1604 (apply 'anything-internal plist)))
1606 (defun* anything-resume (&optional
1607 (any-buffer anything-last-buffer)
1608 buffer-pattern (any-resume t))
1609 "Resurrect previously invoked `anything'.
1610 Called with a prefix arg, allow choosing among all existing
1611 anything buffers. i.e choose among various anything sessions."
1612 (interactive)
1613 (when (or current-prefix-arg buffer-pattern)
1614 (setq any-buffer (anything-resume-select-buffer buffer-pattern)))
1615 (setq anything-compiled-sources nil)
1616 (anything
1617 (or (buffer-local-value 'anything-last-sources-local (get-buffer any-buffer))
1618 anything-last-sources anything-sources)
1619 (buffer-local-value 'anything-input-local (get-buffer any-buffer))
1620 nil any-resume nil any-buffer))
1622 ;;; rubikitch: experimental
1623 ;;; I use this and check it whether I am convenient.
1624 ;;; I may introduce an option to control the behavior.
1625 (defun* anything-resume-window-only (&optional
1626 (any-buffer anything-last-buffer)
1627 buffer-pattern)
1628 (interactive)
1629 (anything-resume any-buffer buffer-pattern 'window-only))
1631 ;;;###autoload
1632 (defun anything-at-point (&optional
1633 any-sources any-input
1634 any-prompt any-resume
1635 any-preselect any-buffer)
1636 "Call anything with symbol at point as initial input.
1637 ANY-SOURCES ANY-INPUT ANY-PROMPT ANY-RESUME ANY-PRESELECT and ANY-BUFFER
1638 are same args as in `anything'."
1639 (interactive)
1640 (anything any-sources
1641 (if current-prefix-arg
1642 (concat "\\b" (thing-at-point 'symbol) "\\b"
1643 (if (featurep 'anything-match-plugin) " " ""))
1644 any-input)
1645 any-prompt any-resume any-preselect any-buffer))
1647 ;;;###autoload
1648 (defun anything-other-buffer (any-sources any-buffer)
1649 "Simplified interface of `anything' with other `anything-buffer'.
1650 Call `anything' with only ANY-SOURCES and ANY-BUFFER as args."
1651 (anything any-sources nil nil nil nil any-buffer))
1653 ;;; (@* "Core: entry point helper")
1654 (defun anything-internal (&optional
1655 any-sources any-input
1656 any-prompt any-resume
1657 any-preselect any-buffer any-keymap)
1658 "The internal anything function called by `anything'.
1659 For ANY-SOURCES ANY-INPUT ANY-PROMPT ANY-RESUME ANY-PRESELECT ANY-BUFFER and
1660 ANY-KEYMAP See `anything'."
1661 (anything-log "++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
1662 (anything-log-eval any-prompt any-preselect any-buffer any-keymap)
1663 (unwind-protect
1664 (condition-case v
1665 (let ( ;; It is needed because `anything-source-name' is non-nil
1666 ;; when `anything' is invoked by action. Awful global scope.
1667 anything-source-name
1668 anything-in-persistent-action
1669 anything-quit
1670 (case-fold-search t)
1671 (anything-buffer (or any-buffer anything-buffer))
1672 ;; cua-mode ; avoid error when region is selected
1674 (with-anything-restore-variables
1675 (anything-initialize any-resume any-input any-sources)
1676 (anything-display-buffer anything-buffer)
1677 (anything-log "show prompt")
1678 (unwind-protect
1679 (anything-read-pattern-maybe
1680 any-prompt any-input any-preselect any-resume any-keymap)
1681 (anything-cleanup)))
1682 (prog1 (unless anything-quit (anything-execute-selection-action-1))
1683 (anything-log "end session --------------------------------------------")))
1684 (quit
1685 (anything-on-quit)
1686 (anything-log "end session (quit) -------------------------------------")
1687 nil))
1688 (anything-log-save-maybe)))
1690 (defun anything-parse-keys (keys)
1691 "Parse the KEYS arguments of `anything'."
1692 (loop for (key value &rest _) on keys by #'cddr
1693 for symname = (substring (symbol-name key) 1)
1694 for sym = (intern (if (string-match "^anything-" symname)
1695 symname
1696 (concat "anything-" symname)))
1697 unless (memq key anything-argument-keys)
1698 collect (cons sym value)))
1700 (defun anything-resume-p (any-resume)
1701 "Whether current anything session is resumed or not.
1702 Just check if ANY-RESUME value is t or window-only."
1703 (memq any-resume '(t window-only)))
1705 (defvar anything-buffers nil
1706 "All of `anything-buffer' in most recently used order.")
1707 (defun anything-initialize (any-resume any-input any-sources)
1708 "Start initialization of `anything' session.
1709 For ANY-RESUME ANY-INPUT and ANY-SOURCES See `anything'."
1710 (anything-log "start initialization: any-resume=%S any-input=%S" any-resume any-input)
1711 (anything-frame-or-window-configuration 'save)
1712 (setq anything-sources (anything-normalize-sources any-sources))
1713 (anything-log "sources = %S" anything-sources)
1714 (anything-hooks 'setup)
1715 (anything-current-position 'save)
1716 (if (anything-resume-p any-resume)
1717 (anything-initialize-overlays (anything-buffer-get))
1718 (anything-initial-setup))
1719 (unless (eq any-resume 'noresume)
1720 (anything-recent-push anything-buffer 'anything-buffers)
1721 (setq anything-last-buffer anything-buffer))
1722 (when any-input (setq anything-input any-input anything-pattern any-input))
1723 (and (anything-resume-p any-resume) (anything-funcall-foreach 'resume))
1724 (anything-log "end initialization"))
1726 (defun anything-execute-selection-action-1 ()
1727 "Execute current action."
1728 (unwind-protect
1729 (anything-execute-selection-action)
1730 (anything-aif (get-buffer anything-action-buffer)
1731 (kill-buffer it))
1732 (anything-log-run-hook 'anything-after-action-hook)))
1734 (defun anything-on-quit ()
1735 "Save `minibuffer-history' and current position."
1736 (setq minibuffer-history (cons anything-input minibuffer-history))
1737 (anything-current-position 'restore))
1739 (defun anything-resume-select-buffer (input)
1740 "Resume precedent anything session with initial input INPUT."
1741 (anything '(((name . "Resume anything buffer")
1742 (candidates . anything-buffers)
1743 (action . identity)))
1744 input nil 'noresume nil "*anything resume*"))
1746 (defun anything-recent-push (elt list-var)
1747 "Add ELT to the value of LIST-VAR as most recently used value."
1748 (let ((m (member elt (symbol-value list-var))))
1749 (and m (set list-var (delq (car m) (symbol-value list-var))))
1750 (push elt (symbol-value list-var))))
1752 ;;; (@* "Core: Accessors")
1753 ;;; rubikitch: I love to create functions to control variables.
1754 (defvar anything-current-position nil
1755 "Cons of \(point . window-start\) when `anything' is invoked.
1756 It is needed to restore position in `anything-current-buffer'
1757 when `anything' is keyboard-quitted.")
1758 (defun anything-current-position (save-or-restore)
1759 "Restore or save current position in `anything-current-buffer'.
1760 Argument SAVE-OR-RESTORE is one of save or restore."
1761 (case save-or-restore
1762 (save
1763 (anything-log "Save position at %S" (cons (point) (window-start)))
1764 (setq anything-current-position (cons (point) (window-start))))
1765 (restore
1766 (anything-log "Restore position at %S in buffer %s"
1767 anything-current-position
1768 (buffer-name (current-buffer)))
1769 (goto-char (car anything-current-position))
1770 ;; Fix this position with the NOFORCE arg of `set-window-start'
1771 ;; otherwise, if there is some other buffer than `anything-current-buffer'
1772 ;; one, position will be lost.
1773 (set-window-start (selected-window) (cdr anything-current-position) t))))
1776 ;; Internal.
1777 (defvar anything-last-frame-or-window-configuration nil
1778 "Used to store window or frame configuration when anything start.")
1779 (defun anything-frame-or-window-configuration (save-or-restore)
1780 "Save or restore last frame or window configuration.
1781 Possible value of SAVE-OR-RESTORE are 'save and 'restore.
1782 window or frame configuration is saved/restored according to values of
1783 `anything-save-configuration-functions'."
1784 (anything-log-eval anything-save-configuration-functions)
1785 (case save-or-restore
1786 (save (setq anything-last-frame-or-window-configuration
1787 (funcall (cdr anything-save-configuration-functions))))
1788 (restore (funcall (car anything-save-configuration-functions)
1789 anything-last-frame-or-window-configuration)
1790 ;; Restore frame focus.
1791 (let ((frame (and (listp anything-last-frame-or-window-configuration)
1792 (caadr anything-last-frame-or-window-configuration))))
1793 ;; If `anything-save-configuration-functions' are window functions
1794 ;; frame should be nil, use current frame.
1795 (unless (framep frame)
1796 (setq frame (selected-frame)))
1797 (select-frame-set-input-focus frame)))))
1800 ;; (@* "Core: Display *anything* buffer")
1801 (defun anything-display-buffer (buf)
1802 "Display *anything* buffer BUF."
1803 (let (pop-up-frames)
1804 (funcall (with-current-buffer buf anything-display-function) buf)))
1806 (defun anything-default-display-buffer (buf)
1807 "Default function to display BUF.
1808 Where BUF is generally `anything-buffer'.
1809 It use `switch-to-buffer' or `pop-to-buffer' depending of value of
1810 `anything-samewindow'."
1811 (funcall (if anything-samewindow 'switch-to-buffer 'pop-to-buffer) buf))
1813 ;; (@* "Core: initialize")
1814 (defun anything-initial-setup ()
1815 "Initialize anything settings and set up the anything buffer."
1816 (anything-log-run-hook 'anything-before-initialize-hook)
1817 (setq anything-current-prefix-arg nil)
1818 (setq anything-once-called-functions nil)
1819 (setq anything-delayed-init-executed nil)
1820 (setq anything-current-buffer
1821 (if (minibuffer-window-active-p (minibuffer-window))
1822 ;; If minibuffer is active be sure to use it's buffer
1823 ;; as `anything-current-buffer'.
1824 (window-buffer (active-minibuffer-window))
1825 (current-buffer)))
1826 (setq anything-buffer-file-name buffer-file-name)
1827 (setq anything-issued-errors nil)
1828 (setq anything-compiled-sources nil)
1829 (setq anything-saved-current-source nil)
1830 (if (or (not split-width-threshold)
1831 (and (integerp split-width-threshold)
1832 (>= split-width-threshold (+ (frame-width) 4))))
1833 (setq anything-split-window-state 'vertical)
1834 (setq anything-split-window-state 'horizontal))
1835 ;; Call the init function for sources where appropriate
1836 (anything-funcall-foreach 'init)
1837 (setq anything-pattern "")
1838 (setq anything-input "")
1839 (setq anything-candidate-cache nil)
1840 (setq anything-last-sources anything-sources)
1841 (anything-create-anything-buffer)
1842 (anything-log-run-hook 'anything-after-initialize-hook))
1844 (defvar anything-reading-pattern nil
1845 "Whether in `read-string' in anything or not.")
1846 (defun anything-read-pattern-maybe (any-prompt any-input
1847 any-preselect any-resume any-keymap)
1848 "Read pattern with prompt ANY-PROMPT and initial input ANY-INPUT.
1849 For ANY-PRESELECT ANY-RESUME ANY-KEYMAP, See `anything'."
1850 (if (anything-resume-p any-resume)
1851 (anything-mark-current-line)
1852 (anything-update))
1853 ;; FIXME: This make anything write in minibuffer
1854 ;; When using a minibuffer frame independent config.
1855 ;; Is it really needed and why?
1856 ;; I leave it commented for now.
1857 ;(select-frame-set-input-focus (window-frame (minibuffer-window)))
1858 (anything-preselect any-preselect)
1859 (with-current-buffer (anything-buffer-get)
1860 (and any-keymap (set (make-local-variable 'anything-map) any-keymap))
1861 (let ((minibuffer-local-map
1862 anything-map))
1863 (anything-log-eval (anything-approximate-candidate-number)
1864 anything-execute-action-at-once-if-one
1865 anything-quit-if-no-candidate)
1866 (cond ((and anything-execute-action-at-once-if-one
1867 (= (anything-approximate-candidate-number) 1))
1868 (ignore))
1869 ((and anything-quit-if-no-candidate
1870 (= (anything-approximate-candidate-number) 0))
1871 (setq anything-quit t)
1872 (and (functionp anything-quit-if-no-candidate)
1873 (funcall anything-quit-if-no-candidate)))
1875 (let ((anything-reading-pattern t)
1876 (tap (with-anything-current-buffer (thing-at-point 'symbol))))
1877 (read-string (or any-prompt "pattern: ") any-input nil tap)))))))
1879 (defun anything-create-anything-buffer (&optional test-mode)
1880 "Create newly created `anything-buffer'.
1881 If TEST-MODE is non-nil, clear `anything-candidate-cache'."
1882 (when test-mode
1883 (setq anything-candidate-cache nil))
1884 (with-current-buffer (get-buffer-create anything-buffer)
1885 (anything-log "kill local variables: %S" (buffer-local-variables))
1886 (kill-all-local-variables)
1887 (set (make-local-variable 'inhibit-read-only) t)
1888 (buffer-disable-undo)
1889 (erase-buffer)
1890 (set (make-local-variable 'inhibit-read-only) t)
1891 (set (make-local-variable 'anything-last-sources-local) anything-sources)
1892 (set (make-local-variable 'anything-follow-mode) nil)
1893 (set (make-local-variable 'anything-display-function) anything-display-function)
1894 (anything-initialize-persistent-action)
1895 (anything-log-eval anything-display-function anything-let-variables)
1896 (loop for (var . val) in anything-let-variables
1897 do (set (make-local-variable var) val))
1898 (setq cursor-type nil)
1899 (setq mode-name "Anything"))
1900 (anything-initialize-overlays anything-buffer)
1901 (get-buffer anything-buffer))
1903 (defun anything-initialize-overlays (buffer)
1904 "Initialize anything overlays in BUFFER."
1905 (anything-log "overlay setup")
1906 (if anything-selection-overlay
1907 ;; make sure the overlay belongs to the anything buffer if
1908 ;; it's newly created
1909 (move-overlay anything-selection-overlay (point-min) (point-min)
1910 (get-buffer buffer))
1912 (setq anything-selection-overlay
1913 (make-overlay (point-min) (point-min) (get-buffer buffer)))
1914 (overlay-put anything-selection-overlay 'face anything-selection-face))
1916 (cond (anything-enable-shortcuts
1917 (setq anything-shortcut-keys
1918 (assoc-default anything-enable-shortcuts anything-shortcut-keys-alist))
1919 (unless anything-digit-overlays
1920 (setq anything-digit-overlays
1921 (loop for key across anything-shortcut-keys
1922 for overlay = (make-overlay (point-min) (point-min)
1923 (get-buffer buffer))
1924 do (overlay-put overlay 'before-string
1925 (format "%s - " (upcase (make-string 1 key))))
1926 collect overlay))))
1927 (anything-digit-overlays
1928 (mapc 'delete-overlay anything-digit-overlays)
1929 (setq anything-digit-overlays nil))))
1931 (defun anything-hooks (setup-or-cleanup)
1932 "Add or remove hooks according to SETUP-OR-CLEANUP value.
1933 if SETUP-OR-CLEANUP value is setup add hooks, any other value
1934 will remove hooks.
1935 hooks concerned are `post-command-hook' and `minibuffer-setup-hook'."
1936 (let ((hooks '((post-command-hook anything-check-minibuffer-input)
1937 (minibuffer-setup-hook anything-print-error-messages))))
1938 (if (eq setup-or-cleanup 'setup)
1939 (dolist (args hooks) (apply 'add-hook args))
1940 (dolist (args (reverse hooks)) (apply 'remove-hook args)))))
1942 ;; (@* "Core: clean up")
1943 ;;; TODO move
1944 (defun anything-cleanup ()
1945 "Clean up the mess when anything exit or quit."
1946 (anything-log "start cleanup")
1947 (with-current-buffer anything-buffer
1948 (setq cursor-type t)
1949 ;; Call burry-buffer whithout arg
1950 ;; to remove anything-buffer from window.
1951 (bury-buffer)
1952 ;; Be sure we call this from anything-buffer.
1953 (anything-funcall-foreach 'cleanup))
1954 (anything-new-timer 'anything-check-minibuffer-input-timer nil)
1955 (anything-kill-async-processes)
1956 (anything-log-run-hook 'anything-cleanup-hook)
1957 (anything-hooks 'cleanup)
1958 (anything-frame-or-window-configuration 'restore)
1959 ;; This is needed in some cases where last input
1960 ;; is yielded infinitely in minibuffer after anything session.
1961 (anything-clean-up-minibuffer))
1963 (defun anything-clean-up-minibuffer ()
1964 "Remove contents of minibuffer."
1965 (let ((miniwin (minibuffer-window)))
1966 ;; Clean only current minibuffer used by anything.
1967 ;; i.e The precedent one is active.
1968 (unless (minibuffer-window-active-p miniwin)
1969 (with-current-buffer (window-buffer miniwin)
1970 (delete-minibuffer-contents)))))
1972 ;; (@* "Core: input handling")
1973 (defun anything-check-minibuffer-input ()
1974 "Extract input string from the minibuffer and check if it needs to be handled."
1975 (let ((delay (with-current-buffer anything-buffer anything-input-idle-delay)))
1976 (if (or (not delay) (anything-action-window))
1977 (anything-check-minibuffer-input-1)
1978 (anything-new-timer
1979 'anything-check-minibuffer-input-timer
1980 (run-with-idle-timer delay nil 'anything-check-minibuffer-input-1)))))
1982 (defun anything-check-minibuffer-input-1 ()
1983 "Check minibuffer content."
1984 (with-anything-quittable
1985 (with-selected-window (or (active-minibuffer-window) (minibuffer-window))
1986 (anything-check-new-input (minibuffer-contents)))))
1988 (defun anything-check-new-input (input)
1989 "Check INPUT string and update the anything buffer if necessary."
1990 (unless (equal input anything-pattern)
1991 (setq anything-pattern input)
1992 (unless (anything-action-window)
1993 (setq anything-input anything-pattern))
1994 (anything-log-eval anything-pattern anything-input)
1995 (anything-update)))
1997 ;; (@* "Core: source compiler")
1998 (defvar anything-compile-source-functions-default anything-compile-source-functions
1999 "Plug-ins this file provides.")
2000 (defun anything-compile-sources (sources funcs)
2001 "Compile SOURCES with FUNCS.
2002 See `anything-compile-source-functions'.
2003 Anything plug-ins are realized by this function."
2004 (mapcar
2005 (lambda (source)
2006 (loop with source = (if (listp source) source (symbol-value source))
2007 for f in funcs
2008 do (setq source (funcall f source))
2009 finally (return source)))
2010 sources))
2012 ;; (@* "Core: plug-in attribute documentation hack")
2014 ;; `anything-document-attribute' is public API.
2015 (defadvice documentation-property (after anything-document-attribute activate)
2016 "Display plug-in attributes' documentation as `anything-sources' docstring."
2017 (when (eq (ad-get-arg 0) 'anything-sources)
2018 (setq ad-return-value
2019 (concat ad-return-value "\n"
2020 (mapconcat (lambda (sym) (get sym 'anything-attrdoc))
2021 anything-additional-attributes
2022 "\n")))))
2023 ;; (describe-variable 'anything-sources)
2024 ;; (documentation-property 'anything-sources 'variable-documentation)
2025 ;; (progn (ad-disable-advice 'documentation-property 'after 'anything-document-attribute) (ad-update 'documentation-property))
2027 ;; (@* "Core: all candidates")
2028 (defun anything-process-delayed-init (source)
2029 "Initialize delayed SOURCE."
2030 (let ((name (assoc-default 'name source)))
2031 (unless (member name anything-delayed-init-executed)
2032 (anything-aif (assoc-default 'delayed-init source)
2033 (with-current-buffer anything-current-buffer
2034 (anything-funcall-with-source source it)
2035 (dolist (f (if (functionp it) (list it) it))
2036 (add-to-list 'anything-delayed-init-executed name)))))))
2038 (defun anything-get-candidates (source)
2039 "Retrieve and return the list of candidates from SOURCE."
2040 (anything-process-delayed-init source)
2041 (let* ((candidate-source (assoc-default 'candidates source))
2042 (type-error (lambda ()
2043 (error (concat "Candidates must either be a function, "
2044 " a variable or a list: %s")
2045 candidate-source)))
2046 (candidates (condition-case err
2047 (anything-interpret-value candidate-source source)
2048 (error (funcall type-error)))))
2049 (cond ((processp candidates) candidates)
2050 ((listp candidates) (anything-transform-candidates candidates source))
2051 (t (funcall type-error)))))
2054 (defun anything-get-cached-candidates (source)
2055 "Return the cached value of candidates for SOURCE.
2056 Cache the candidates if there is not yet a cached value."
2057 (let* ((name (assoc-default 'name source))
2058 (candidate-cache (assoc name anything-candidate-cache)))
2059 (cond (candidate-cache
2060 (anything-log "use cached candidates")
2061 (cdr candidate-cache))
2063 (anything-log "calculate candidates")
2064 (let ((candidates (anything-get-candidates source)))
2065 (cond ((processp candidates)
2066 (push (cons candidates
2067 (append source
2068 (list (cons 'item-count 0)
2069 (cons 'incomplete-line ""))))
2070 anything-async-processes)
2071 (set-process-filter candidates 'anything-output-filter)
2072 (setq candidates nil))
2073 ((not (assoc 'volatile source))
2074 (setq candidate-cache (cons name candidates))
2075 (push candidate-cache anything-candidate-cache)))
2076 candidates)))))
2078 ;;; (@* "Core: candidate transformers")
2079 (defun anything-process-candidate-transformer (candidates source)
2080 "Execute candidate-transformer function on all CANDIDATES of SOURCE."
2081 (anything-aif (assoc-default 'candidate-transformer source)
2082 (anything-composed-funcall-with-source source it candidates)
2083 candidates))
2085 (defun anything-process-filtered-candidate-transformer (candidates source)
2086 "Execute filtered-candidate-transformer function on all CANDIDATES of SOURCE."
2087 (anything-aif (assoc-default 'filtered-candidate-transformer source)
2088 (anything-composed-funcall-with-source source it candidates source)
2089 candidates))
2091 (defun anything-process-filtered-candidate-transformer-maybe (candidates source process-p)
2092 "Execute filtered-candidate-transformer function on all CANDIDATES of SOURCE.
2093 This happen if PROCESS-P is non-nil."
2094 (if process-p
2095 (anything-process-filtered-candidate-transformer candidates source)
2096 candidates))
2098 (defun anything-process-real-to-display (candidates source)
2099 "Execute real-to-display function on all CANDIDATES of SOURCE."
2100 (anything-aif (assoc-default 'real-to-display source)
2101 (setq candidates (anything-funcall-with-source
2102 source 'mapcar
2103 (lambda (cand_)
2104 (if (consp cand_)
2105 ;; override DISPLAY from candidate-transformer
2106 (cons (funcall it (cdr cand_)) (cdr cand_))
2107 (cons (funcall it cand_) cand_)))
2108 candidates))
2109 candidates))
2111 (defun anything-transform-candidates (candidates source &optional process-p)
2112 "Transform CANDIDATES of SOURCE according to candidate transformers.
2113 This happen if PROCESS-P is non-nil."
2114 (anything-process-real-to-display
2115 (anything-process-filtered-candidate-transformer-maybe
2116 (anything-process-candidate-transformer candidates source) source process-p)
2117 source))
2119 ;; (@* "Core: narrowing candidates")
2120 (defun anything-candidate-number-limit (source)
2121 "Apply candidate-number-limit attribute value.
2122 This overhide variable `anything-candidate-number-limit'.
2124 e.g:
2125 If \(candidate-number-limit\) is in SOURCE, show all candidates in SOURCE.
2126 If \(candidate-number-limit . 123\) is in SOURCE limit candidate to 123."
2127 (anything-aif (assq 'candidate-number-limit source)
2128 (or (cdr it) 99999999)
2129 (or anything-candidate-number-limit 99999999)))
2131 ;; FIXME: Why a defconst here
2132 (defconst anything-default-match-functions
2133 (list (lambda (candidate)
2134 (string-match anything-pattern candidate)))
2135 "Default functions to match candidates according to `anything-pattern'.")
2137 (defun anything-compute-matches (source)
2138 "Compute matched results from SOURCE according to its settings."
2139 (if debug-on-error
2140 (anything-compute-matches-internal source)
2141 (condition-case v
2142 (anything-compute-matches-internal source)
2143 (error (anything-log-error
2144 "anything-compute-matches: error when processing source: %s"
2145 (assoc-default 'name source))
2146 nil))))
2148 (defun anything-candidate-get-display (candidate)
2149 "Get searched display part from CANDIDATE.
2150 CANDIDATE is a string, a symbol, or \(DISPLAY . REAL\) cons cell."
2151 (format "%s" (or (car-safe candidate) candidate)))
2153 (defun anything-process-pattern-transformer (pattern source)
2154 "Execute pattern-transformer attribute PATTERN function in SOURCE."
2155 (anything-aif (assoc-default 'pattern-transformer source)
2156 (anything-composed-funcall-with-source source it pattern)
2157 pattern))
2159 (defun anything-match-functions (source)
2160 (or (assoc-default 'match source)
2161 anything-default-match-functions))
2163 (defmacro anything-accumulate-candidates-internal (cand newmatches
2164 hash item-count limit)
2165 "Internal, add CAND into NEWMATCHES.
2166 Use HASH to uniq NEWMATCHES.
2167 Argument ITEM-COUNT count the matches.
2168 if ITEM-COUNT reaches LIMIT, exit from inner loop."
2169 `(unless (gethash ,cand ,hash)
2170 (puthash ,cand t ,hash)
2171 (push ,cand ,newmatches)
2172 (incf ,item-count)
2173 (when (= ,item-count ,limit)
2174 (setq exit t)
2175 (return))))
2177 (defun anything-take-first-elements (seq n)
2178 (if (> (length seq) n)
2179 (setq seq (subseq seq 0 n))
2180 seq))
2182 (defun anything-match-from-candidates (cands matchfns limit)
2183 (let (matches)
2184 (condition-case nil
2185 (let ((item-count 0) exit)
2186 (clrhash anything-match-hash)
2187 (dolist (match matchfns)
2188 (let (newmatches)
2189 (dolist (candidate cands)
2190 (when (funcall match (anything-candidate-get-display candidate))
2191 (anything-accumulate-candidates-internal
2192 candidate newmatches anything-match-hash item-count limit)))
2193 (setq matches (append matches (reverse newmatches)))
2194 (if exit (return)))))
2195 (invalid-regexp (setq matches nil)))
2196 matches))
2198 (defun anything-compute-matches-internal (source)
2199 (save-current-buffer
2200 (let ((matchfns (anything-match-functions source))
2201 (anything-source-name (assoc-default 'name source))
2202 (limit (anything-candidate-number-limit source))
2203 (anything-pattern (anything-process-pattern-transformer
2204 anything-pattern source)))
2205 (anything-process-filtered-candidate-transformer
2206 (if (or (equal anything-pattern "") (equal matchfns '(identity)))
2207 (anything-take-first-elements
2208 (anything-get-cached-candidates source) limit)
2209 (anything-match-from-candidates
2210 (anything-get-cached-candidates source) matchfns limit))
2211 source))))
2213 ;; (anything '(((name . "error")(candidates . (lambda () (hage))) (action . identity))))
2215 (defun anything-process-source (source)
2216 "Display matched results from SOURCE according to its settings."
2217 (anything-log-eval (assoc-default 'name source))
2218 (if (assq 'direct-insert-match source) ;experimental
2219 (anything-process-source--direct-insert-match source)
2220 (let ((matches (anything-compute-matches source)))
2221 (when matches
2222 (when anything-test-mode
2223 (setq anything-test-candidate-list
2224 `(,@anything-test-candidate-list
2225 (,(assoc-default 'name source)
2226 ,matches))))
2227 (anything-insert-header-from-source source)
2228 (if (not (assq 'multiline source))
2229 (mapc 'anything-insert-match-with-digit-overlay matches)
2230 (let ((start (point)) separate)
2231 (dolist (match matches)
2232 (if separate
2233 (anything-insert-candidate-separator)
2234 (setq separate t))
2235 (anything-insert-match-with-digit-overlay match))
2236 (put-text-property start (point) 'anything-multiline t)))))))
2238 (defun anything-insert-match-with-digit-overlay (match)
2239 (declare (special source))
2240 (anything-put-digit-overlay-maybe)
2241 (anything-insert-match match 'insert source))
2243 (defun anything-put-digit-overlay-maybe ()
2244 (when (and anything-enable-shortcuts
2245 (not (eq anything-digit-shortcut-count
2246 (length anything-digit-overlays))))
2247 (move-overlay (nth anything-digit-shortcut-count
2248 anything-digit-overlays)
2249 (point-at-bol)
2250 (point-at-bol))
2251 (incf anything-digit-shortcut-count)))
2253 (defun anything-process-source--direct-insert-match (source)
2254 "[EXPERIMENTAL] Insert candidates from `anything-candidate-buffer' in SOURCE."
2255 (anything-log-eval (assoc-default 'name source))
2256 (let ((anything-source-name (assoc-default 'name source))
2257 content-buf)
2258 (funcall (assoc-default 'candidates source))
2259 (setq content-buf (anything-candidate-buffer))
2260 (unless (anything-empty-buffer-p content-buf)
2261 (anything-insert-header-from-source source)
2262 (insert-buffer-substring content-buf)
2263 ;; TODO call anything-put-digit-overlay-maybe with loop
2266 (defun anything-process-delayed-sources (delayed-sources)
2267 "Process DELAYED-SOURCES when idle for `anything-idle-delay' seconds."
2268 (with-anything-quittable
2269 (anything-log-eval (mapcar (lambda (s) (assoc-default 'name s)) delayed-sources))
2270 (with-current-buffer anything-buffer
2271 (save-excursion
2272 (goto-char (point-max))
2273 (mapc 'anything-process-source delayed-sources)
2274 (when (and (not (anything-empty-buffer-p))
2275 ;; no selection yet
2276 (= (overlay-start anything-selection-overlay)
2277 (overlay-end anything-selection-overlay)))
2278 (goto-char (point-min))
2279 (anything-next-line)))
2280 (save-excursion
2281 (goto-char (point-min))
2282 (anything-log-run-hook 'anything-update-hook)))))
2284 ;; (@* "Core: *anything* buffer contents")
2285 (defvar anything-input-local nil)
2286 (defvar anything-process-delayed-sources-timer nil)
2287 (defun anything-update ()
2288 "Update candidates list in `anything-buffer' according to `anything-pattern'."
2289 (anything-log "start update")
2290 (setq anything-digit-shortcut-count 0)
2291 (anything-kill-async-processes)
2292 (with-current-buffer (anything-buffer-get)
2293 (set (make-local-variable 'anything-input-local) anything-pattern)
2294 (erase-buffer)
2295 (when anything-enable-shortcuts
2296 (mapc 'delete-overlay anything-digit-overlays))
2297 (let (delayed-sources)
2298 (unwind-protect
2299 (setq delayed-sources
2300 (loop for source in (remove-if-not 'anything-update-source-p
2301 (anything-get-sources))
2302 if (anything-delayed-source-p source)
2303 collect source
2304 else do (anything-process-source source)))
2305 (anything-log-eval
2306 (mapcar (lambda (s) (assoc-default 'name s)) delayed-sources))
2307 (anything-update-move-first-line)
2308 (if anything-test-mode
2309 (mapc 'anything-process-source delayed-sources)
2310 (when delayed-sources
2311 (anything-new-timer
2312 'anything-process-delayed-sources-timer
2313 (run-with-idle-timer
2314 anything-idle-delay nil
2315 'anything-process-delayed-sources delayed-sources)))
2316 ;; FIXME I want to execute anything-after-update-hook
2317 ;; AFTER processing delayed sources
2318 (anything-log-run-hook 'anything-after-update-hook))
2319 (anything-log "end update")))))
2321 (defun anything-update-source-p (source)
2322 (and (or (not anything-source-filter)
2323 (member (assoc-default 'name source) anything-source-filter))
2324 (>= (length anything-pattern)
2325 (anything-aif (assoc 'requires-pattern source)
2326 (or (cdr it) 1)
2327 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 (with-anything-window
2915 (when candidate-or-regexp
2916 (goto-char (point-min))
2917 ;; go to first candidate of first source
2918 (forward-line 1)
2919 (let ((start (point)))
2920 (unless (or (re-search-forward
2921 (concat "^" (regexp-quote candidate-or-regexp) "$")
2922 nil t)
2923 (progn (goto-char start)
2924 (re-search-forward candidate-or-regexp nil t)))
2925 (goto-char start))))
2926 (anything-mark-current-line)))
2928 (defun anything-delete-current-selection ()
2929 "Delete the currently selected item."
2930 (interactive)
2931 (with-anything-window
2932 (cond ((anything-pos-multiline-p)
2933 (anything-aif (anything-get-next-candidate-separator-pos)
2934 (delete-region (point-at-bol)
2935 (1+ (progn (goto-char it) (point-at-eol))))
2936 ;; last candidate
2937 (goto-char (anything-get-previous-candidate-separator-pos))
2938 (delete-region (point-at-bol) (point-max)))
2939 (when (anything-end-of-source-p)
2940 (goto-char (or (anything-get-previous-candidate-separator-pos)
2941 (point-min)))
2942 (forward-line 1)))
2944 (delete-region (point-at-bol) (1+ (point-at-eol)))
2945 (when (anything-end-of-source-p) (forward-line -1))))
2946 (anything-mark-current-line)))
2948 (defun anything-end-of-source-p ()
2949 "Return t if we are at eob or end of source."
2950 (save-excursion
2951 (forward-line 1)
2952 (or (eq (point-at-bol) (point-at-eol))
2953 (anything-pos-header-line-p)
2954 (eobp))))
2956 (defun anything-edit-current-selection-internal (func)
2957 (with-anything-window
2958 (beginning-of-line)
2959 (let ((realvalue (get-text-property (point) 'anything-realvalue)))
2960 (funcall func)
2961 (beginning-of-line)
2962 (and realvalue
2963 (put-text-property (point) (point-at-eol)
2964 'anything-realvalue realvalue))
2965 (anything-mark-current-line))))
2967 (defmacro anything-edit-current-selection (&rest forms)
2968 "Evaluate FORMS at current selection in the anything buffer.
2969 You can edit the line."
2970 (declare (indent 0) (debug t))
2971 `(anything-edit-current-selection-internal
2972 (lambda () ,@forms)))
2974 (defun anything-set-pattern (pattern &optional noupdate)
2975 "Set minibuffer contents to PATTERN.
2976 if optional NOUPDATE is non-nil, anything buffer is not changed."
2977 (with-selected-window (or (active-minibuffer-window) (minibuffer-window))
2978 (delete-minibuffer-contents)
2979 (insert pattern))
2980 (when noupdate
2981 (setq anything-pattern pattern)
2982 (anything-hooks 'cleanup)
2983 (run-with-idle-timer 0 nil 'anything-hooks 'setup)))
2985 (defun anything-delete-minibuffer-contents ()
2986 "Same as `delete-minibuffer-contents' but this is a command."
2987 (interactive)
2988 (anything-set-pattern ""))
2989 (defalias 'anything-delete-minibuffer-content 'anything-delete-minibuffer-contents)
2991 ;; (@* "Built-in plug-in: type")
2992 (defun anything-compile-source--type (source)
2993 (anything-aif (assoc-default 'type source)
2994 (append source (assoc-default it anything-type-attributes) nil)
2995 source))
2997 ;; `define-anything-type-attribute' is public API.
2999 (defun anything-add-type-attribute (type definition)
3000 (anything-aif (assq type anything-type-attributes)
3001 (setq anything-type-attributes (delete it anything-type-attributes)))
3002 (push (cons type definition) anything-type-attributes))
3004 (defvar anything-types nil)
3005 (defun anything-document-type-attribute (type doc)
3006 (add-to-list 'anything-types type t)
3007 (put type 'anything-typeattrdoc
3008 (concat "- " (symbol-name type) "\n\n" doc "\n")))
3010 (defadvice documentation-property (after anything-document-type-attribute activate)
3011 "Display type attributes' documentation as `anything-type-attributes' docstring."
3012 (when (eq (ad-get-arg 0) 'anything-type-attributes)
3013 (setq ad-return-value
3014 (concat ad-return-value "\n\n++++ Types currently defined ++++\n"
3015 (mapconcat (lambda (sym) (get sym 'anything-typeattrdoc))
3016 anything-types "\n")))))
3018 ;; (@* "Built-in plug-in: dummy")
3019 (defun anything-dummy-candidate (candidate source)
3020 "Use `anything-pattern' as CANDIDATE in SOURCE."
3021 ;; `source' is defined in filtered-candidate-transformer
3022 (list anything-pattern))
3024 (defun anything-compile-source--dummy (source)
3025 (if (assoc 'dummy source)
3026 (append source
3027 '((candidates "dummy")
3028 (accept-empty)
3029 (match identity)
3030 (filtered-candidate-transformer . anything-dummy-candidate)
3031 (disable-shortcuts)
3032 (volatile)))
3033 source))
3035 ;; (@* "Built-in plug-in: disable-shortcuts")
3036 (defvar anything-orig-enable-shortcuts nil)
3037 (defun anything-save-enable-shortcuts ()
3038 (anything-once
3039 (lambda ()
3040 (setq anything-orig-enable-shortcuts anything-enable-shortcuts
3041 anything-enable-shortcuts nil))))
3043 (defun anything-compile-source--disable-shortcuts (source)
3044 (if (assoc 'disable-shortcuts source)
3045 (append `((init ,@(anything-mklist (assoc-default 'init source))
3046 anything-save-enable-shortcuts)
3047 (resume ,@(anything-mklist (assoc-default 'resume source))
3048 anything-save-enable-shortcuts)
3049 (cleanup ,@(anything-mklist (assoc-default 'cleanup source))
3050 (lambda () (setq anything-enable-shortcuts
3051 anything-orig-enable-shortcuts))))
3052 source)
3053 source))
3055 ;; (@* "Built-in plug-in: candidates-in-buffer")
3056 (defun anything-candidates-in-buffer ()
3057 "Get candidates from the candidates buffer according to `anything-pattern'.
3059 BUFFER is `anything-candidate-buffer' by default. Each
3060 candidate must be placed in one line. This function is meant to
3061 be used in candidates-in-buffer or candidates attribute of an
3062 anything source. Especially fast for many (1000+) candidates.
3065 '((name . \"many files\")
3066 (init . (lambda () (with-current-buffer (anything-candidate-buffer 'local)
3067 (insert-many-filenames))))
3068 (search re-search-forward) ; optional
3069 (candidates-in-buffer)
3070 (type . file))
3072 +===============================================================+
3073 | The new way of making and narrowing candidates: Using buffers |
3074 +===============================================================+
3076 By default, `anything' makes candidates by evaluating the
3077 candidates function, then narrows them by `string-match' for each
3078 candidate.
3080 But this way is very slow for many candidates. The new way is
3081 storing all candidates in a buffer and narrowing them by
3082 `re-search-forward'. Search function is customizable by search
3083 attribute. The important point is that buffer processing is MUCH
3084 FASTER than string list processing and is the Emacs way.
3086 The init function writes all candidates to a newly-created
3087 candidate buffer. The candidates buffer is created or specified
3088 by `anything-candidate-buffer'. Candidates are stored in a line.
3090 The candidates function narrows all candidates, IOW creates a
3091 subset of candidates dynamically. It is the task of
3092 `anything-candidates-in-buffer'. As long as
3093 `anything-candidate-buffer' is used,`(candidates-in-buffer)' is
3094 sufficient in most cases.
3096 Note that `(candidates-in-buffer)' is shortcut of three attributes:
3097 (candidates . anything-candidates-in-buffer)
3098 (volatile)
3099 (match identity)
3100 And `(candidates-in-buffer . func)' is shortcut of three attributes:
3101 (candidates . func)
3102 (volatile)
3103 (match identity)
3104 The expansion is performed in `anything-get-sources'.
3106 The candidates-in-buffer attribute implies the volatile attribute.
3107 The volatile attribute is needed because `anything-candidates-in-buffer'
3108 creates candidates dynamically and need to be called everytime
3109 `anything-pattern' changes.
3111 Because `anything-candidates-in-buffer' plays the role of `match' attribute
3112 function, specifying `(match identity)' makes the source slightly faster.
3114 To customize `anything-candidates-in-buffer' behavior, use search,
3115 get-line and search-from-end attributes. See also `anything-sources' docstring."
3116 (declare (special source))
3117 (anything-candidates-in-buffer-1 (anything-candidate-buffer)
3118 (concat "\\<" anything-pattern)
3119 (or (assoc-default 'get-line source)
3120 #'buffer-substring-no-properties)
3121 ;; use external variable `source'.
3122 (or (assoc-default 'search source)
3123 (if (assoc 'search-from-end source)
3124 '(re-search-backward)
3125 '(re-search-forward)))
3126 (anything-candidate-number-limit source)
3127 (assoc 'search-from-end source)))
3129 (defun anything-candidates-in-buffer-1 (buffer pattern get-line-fn search-fns limit search-from-end)
3130 ;; buffer == nil when candidates buffer does not exist.
3131 (when buffer
3132 (with-current-buffer buffer
3133 (let ((start-point (if search-from-end (point-max) (point-min)))
3134 (endp (if search-from-end #'bobp #'eobp)))
3135 (goto-char (1- start-point))
3136 (if (string= pattern "")
3137 (anything-initial-candidates-from-candidate-buffer
3138 endp get-line-fn limit search-from-end)
3139 (anything-search-from-candidate-buffer
3140 pattern get-line-fn search-fns limit search-from-end
3141 start-point endp))))))
3143 (defun anything-point-is-moved (proc)
3144 "If point is moved after executing PROC, return t, otherwise nil."
3145 (/= (point) (progn (funcall proc) (point))))
3147 (defun anything-search-from-candidate-buffer (pattern get-line-fn search-fns
3148 limit search-from-end
3149 start-point endp)
3150 (let (buffer-read-only
3151 matches exit newmatches)
3152 (anything-search-from-candidate-buffer-internal
3153 (lambda ()
3154 (clrhash anything-cib-hash)
3155 (dolist (searcher search-fns)
3156 (goto-char start-point)
3157 (setq newmatches nil)
3158 (loop with item-count = 0
3159 while (funcall searcher pattern nil t)
3160 for cand = (funcall get-line-fn (point-at-bol) (point-at-eol))
3161 do (anything-accumulate-candidates-internal
3162 cand newmatches anything-cib-hash item-count limit)
3163 unless (anything-point-is-moved
3164 (lambda ()
3165 (if search-from-end
3166 (goto-char (1- (point-at-bol)))
3167 (forward-line 1))))
3168 return nil)
3169 (setq matches (append matches (nreverse newmatches)))
3170 (if exit (return)))
3171 (delq nil matches)))))
3173 (defun anything-initial-candidates-from-candidate-buffer (endp get-line-fn limit search-from-end)
3174 (delq nil (loop with next-line-fn =
3175 (if search-from-end
3176 (lambda (x) (goto-char (max (1- (point-at-bol)) 1)))
3177 #'forward-line)
3178 until (funcall endp)
3179 for i from 1 to limit
3180 collect (funcall get-line-fn (point-at-bol) (point-at-eol))
3181 do (funcall next-line-fn 1))))
3183 (defun anything-search-from-candidate-buffer-internal (search-fn)
3184 (goto-char (point-min))
3185 (insert "\n")
3186 (goto-char (point-max))
3187 (insert "\n")
3188 (unwind-protect
3189 (funcall search-fn)
3190 (goto-char (point-min))
3191 (delete-char 1)
3192 (goto-char (1- (point-max)))
3193 (delete-char 1)
3195 (set-buffer-modified-p nil)))
3197 (defun anything-candidate-buffer (&optional create-or-buffer)
3198 "Register and return a buffer containing candidates of current source.
3199 `anything-candidate-buffer' searches buffer-local candidates buffer first,
3200 then global candidates buffer.
3202 Acceptable values of CREATE-OR-BUFFER:
3204 - nil (omit)
3205 Only return the candidates buffer.
3206 - a buffer
3207 Register a buffer as a candidates buffer.
3208 - 'global
3209 Create a new global candidates buffer,
3210 named \" *anything candidates:SOURCE*\".
3211 - other non-nil value
3212 Create a new local candidates buffer,
3213 named \" *anything candidates:SOURCE*ANYTHING-CURRENT-BUFFER\"."
3214 (let* ((global-bname (format " *anything candidates:%s*" anything-source-name))
3215 (local-bname (format " *anything candidates:%s*%s"
3216 anything-source-name
3217 (buffer-name anything-current-buffer)))
3218 (register-func
3219 (lambda ()
3220 (setq anything-candidate-buffer-alist
3221 (cons (cons anything-source-name create-or-buffer)
3222 (delete (assoc anything-source-name
3223 anything-candidate-buffer-alist)
3224 anything-candidate-buffer-alist)))))
3225 (kill-buffers-func
3226 (lambda ()
3227 (loop for b in (buffer-list)
3228 if (string-match (format "^%s" (regexp-quote global-bname))
3229 (buffer-name b))
3230 do (kill-buffer b))))
3231 (create-func
3232 (lambda ()
3233 (with-current-buffer
3234 (get-buffer-create (if (eq create-or-buffer 'global)
3235 global-bname
3236 local-bname))
3237 (buffer-disable-undo)
3238 (erase-buffer)
3239 (font-lock-mode -1))))
3240 (return-func
3241 (lambda ()
3242 (or (get-buffer local-bname)
3243 (get-buffer global-bname)
3244 (anything-aif (assoc-default anything-source-name
3245 anything-candidate-buffer-alist)
3246 (and (buffer-live-p it) it))))))
3247 (when create-or-buffer
3248 (funcall register-func)
3249 (unless (bufferp create-or-buffer)
3250 (and (eq create-or-buffer 'global) (funcall kill-buffers-func))
3251 (funcall create-func)))
3252 (funcall return-func)))
3254 (defun anything-compile-source--candidates-in-buffer (source)
3255 (anything-aif (assoc 'candidates-in-buffer source)
3256 (append source
3257 `((candidates . ,(or (cdr it) 'anything-candidates-in-buffer))
3258 (volatile) (match identity)))
3259 source))
3261 ;; (@* "Utility: resplit anything window")
3262 (defun anything-toggle-resplit-window ()
3263 "Toggle resplit anything window, vertically or horizontally."
3264 (interactive)
3265 (with-anything-window
3266 (let ((before-height (window-height)))
3267 (delete-window)
3268 (set-window-buffer
3269 (select-window (if (= (window-height) before-height)
3270 (prog1
3271 (split-window-vertically)
3272 (setq anything-split-window-state 'vertical))
3273 (setq anything-split-window-state 'horizontal)
3274 (split-window-horizontally)))
3275 anything-buffer))))
3277 ;; (@* "Utility: Resize anything window.")
3278 (defun anything-enlarge-window-1 (n)
3279 "Enlarge or narrow anything window.
3280 If N is positive enlarge, if negative narrow."
3281 (unless anything-samewindow
3282 (let ((horizontal-p (eq anything-split-window-state 'horizontal)))
3283 (with-anything-window
3284 (enlarge-window n horizontal-p)))))
3286 (defun anything-narrow-window ()
3287 "Narrow anything window."
3288 (interactive)
3289 (anything-enlarge-window-1 -1))
3291 (defun anything-enlarge-window ()
3292 "Enlarge anything window."
3293 (interactive)
3294 (anything-enlarge-window-1 1))
3296 ;; (@* "Utility: select another action by key")
3297 (defun anything-select-nth-action (n)
3298 "Select the N nth action for the currently selected candidate."
3299 (setq anything-saved-selection (anything-get-selection))
3300 (unless anything-saved-selection
3301 (error "Nothing is selected"))
3302 (setq anything-saved-action (anything-get-nth-action n (anything-get-action)))
3303 (anything-exit-minibuffer))
3305 (defun anything-get-nth-action (n action)
3306 (cond ((and (zerop n) (functionp action))
3307 action)
3308 ((listp action)
3309 (or (cdr (elt action n))
3310 (error "No such action")))
3311 ((and (functionp action) (< 0 n))
3312 (error "Sole action"))
3314 (error "Error in `anything-select-nth-action'"))))
3316 (defun anything-select-2nd-action ()
3317 "Select the 2nd action for the currently selected candidate."
3318 (interactive)
3319 (anything-select-nth-action 1))
3321 (defun anything-select-3rd-action ()
3322 "Select the 3rd action for the currently selected candidate."
3323 (interactive)
3324 (anything-select-nth-action 2))
3326 (defun anything-select-4th-action ()
3327 "Select the 4th action for the currently selected candidate."
3328 (interactive)
3329 (anything-select-nth-action 3))
3331 (defun anything-select-2nd-action-or-end-of-line ()
3332 "Select the 2nd action for the currently selected candidate.
3333 This happen when point is at the end of minibuffer.
3334 Otherwise goto the end of minibuffer."
3335 (interactive)
3336 (if (eolp)
3337 (anything-select-nth-action 1)
3338 (end-of-line)))
3340 ;; (@* "Utility: Persistent Action")
3341 (defmacro with-anything-display-same-window (&rest body)
3342 "Execute BODY in the window used for persistent action.
3343 Make `pop-to-buffer' and `display-buffer' display in the same window."
3344 (declare (indent 0) (debug t))
3345 `(let ((display-buffer-function 'anything-persistent-action-display-buffer))
3346 ,@body))
3348 (defvar anything-persistent-action-display-window nil)
3349 (defun anything-initialize-persistent-action ()
3350 (set (make-local-variable 'anything-persistent-action-display-window) nil))
3352 (defun* anything-execute-persistent-action (&optional (attr 'persistent-action) onewindow)
3353 "Perform the associated action ATTR without quitting anything.
3354 ATTR default is 'persistent-action', but it can be anything else.
3355 In this case you have to add this new attribute to your source.
3356 When `anything-samewindow' and ONEWINDOW are non--nil,
3357 the anything window is never split in persistent action."
3358 (interactive)
3359 (anything-log "executing persistent-action")
3360 (with-anything-window
3361 (save-selected-window
3362 (anything-select-persistent-action-window onewindow)
3363 (anything-log-eval (current-buffer))
3364 (let ((anything-in-persistent-action t))
3365 (with-anything-display-same-window
3366 (anything-execute-selection-action
3368 (or (assoc-default attr (anything-get-current-source))
3369 (anything-get-action))
3371 (anything-log-run-hook 'anything-after-persistent-action-hook))))))
3374 (defun anything-persistent-action-display-window (&optional onewindow)
3375 "Return the window that will be used for presistent action.
3376 If ONEWINDOW is non--nil window will not be splitted in persistent action
3377 if `anything-samewindow' is non--nil also."
3378 (with-anything-window
3379 (setq anything-persistent-action-display-window
3380 (cond ((window-live-p anything-persistent-action-display-window)
3381 anything-persistent-action-display-window)
3382 ((and anything-samewindow (one-window-p t) (not onewindow))
3383 (split-window))
3384 ((get-buffer-window anything-current-buffer))
3386 (next-window (selected-window) 1))))))
3388 (defun anything-select-persistent-action-window (&optional onewindow)
3389 "Select the window that will be used for persistent action.
3390 See `anything-persistent-action-display-window' for how to use ONEWINDOW."
3391 (select-window (get-buffer-window (anything-buffer-get)))
3392 (select-window
3393 (setq minibuffer-scroll-window
3394 (anything-persistent-action-display-window onewindow))))
3396 (defun anything-persistent-action-display-buffer (buf &optional not-this-window)
3397 "Make `pop-to-buffer' and `display-buffer' display in the same window.
3398 If `anything-persistent-action-use-special-display' is non-nil and
3399 BUF is to be displayed by `special-display-function', use it.
3400 Otherwise ignores `special-display-buffer-names' and `special-display-regexps'.
3401 Argument NOT-THIS-WINDOW if present will be used as
3402 second argument of `display-buffer'."
3403 (let* ((name (buffer-name buf))
3404 display-buffer-function pop-up-windows pop-up-frames
3405 (same-window-regexps
3406 (unless (and anything-persistent-action-use-special-display
3407 (or (member name
3408 (mapcar (lambda (x) (or (car-safe x) x))
3409 special-display-buffer-names))
3410 (remove-if-not
3411 (lambda (x) (string-match (or (car-safe x) x) name))
3412 special-display-regexps)))
3413 '("."))))
3414 (display-buffer buf not-this-window)))
3416 ;; scroll-other-window(-down)? for persistent-action
3417 (defun anything-scroll-other-window-base (command)
3418 (with-selected-window (anything-persistent-action-display-window)
3419 (funcall command anything-scroll-amount)))
3421 (defun anything-scroll-other-window ()
3422 "Scroll other window (not *Anything* window) upward."
3423 (interactive)
3424 (anything-scroll-other-window-base 'scroll-up))
3426 (defun anything-scroll-other-window-down ()
3427 "Scroll other window (not *Anything* window) downward."
3428 (interactive)
3429 (anything-scroll-other-window-base 'scroll-down))
3431 ;; (@* "Utility: Visible Mark")
3432 (defface anything-visible-mark
3433 '((((min-colors 88) (background dark))
3434 (:background "green1" :foreground "black"))
3435 (((background dark)) (:background "green" :foreground "black"))
3436 (((min-colors 88)) (:background "green1"))
3437 (t (:background "green")))
3438 "Face for visible mark."
3439 :group 'anything)
3441 (defvar anything-visible-mark-face 'anything-visible-mark)
3442 (defvar anything-visible-mark-overlays nil)
3444 (defun anything-clear-visible-mark ()
3445 (with-current-buffer (anything-buffer-get)
3446 (mapc 'delete-overlay anything-visible-mark-overlays)
3447 (set (make-local-variable 'anything-visible-mark-overlays) nil)))
3448 (add-hook 'anything-after-initialize-hook 'anything-clear-visible-mark)
3450 (defvar anything-marked-candidates nil
3451 "Marked candadates. List of \(source . real\) pair.")
3453 (defun anything-this-visible-mark ()
3454 (loop for o in anything-visible-mark-overlays
3455 when (equal (point-at-bol) (overlay-start o))
3456 do (return o)))
3458 (defun anything-delete-visible-mark (overlay)
3459 (setq anything-marked-candidates
3460 (remove
3461 (cons (anything-get-current-source) (anything-get-selection))
3462 anything-marked-candidates))
3463 (delete-overlay overlay)
3464 (setq anything-visible-mark-overlays
3465 (delq overlay anything-visible-mark-overlays)))
3467 (defun anything-make-visible-mark ()
3468 (let ((o (make-overlay (point-at-bol) (1+ (point-at-eol)))))
3469 (overlay-put o 'face anything-visible-mark-face)
3470 (overlay-put o 'source (assoc-default 'name (anything-get-current-source)))
3471 (overlay-put o 'string (buffer-substring (overlay-start o) (overlay-end o)))
3472 (overlay-put o 'real (anything-get-selection))
3473 (add-to-list 'anything-visible-mark-overlays o))
3474 (push (cons (anything-get-current-source) (anything-get-selection))
3475 anything-marked-candidates))
3477 (defun anything-toggle-visible-mark ()
3478 "Toggle anything visible mark at point."
3479 (interactive)
3480 (with-anything-window
3481 (anything-aif (anything-this-visible-mark)
3482 (anything-delete-visible-mark it)
3483 (anything-make-visible-mark))
3484 (anything-next-line)))
3486 (defun anything-display-all-visible-marks ()
3487 "Show all `anything' visible marks strings."
3488 (interactive)
3489 (with-anything-window
3490 (lexical-let ((overlays (reverse anything-visible-mark-overlays)))
3491 (anything-run-after-quit
3492 (lambda ()
3493 (with-output-to-temp-buffer "*anything visible marks*"
3494 (dolist (o overlays) (princ (overlay-get o 'string)))))))))
3496 (defun anything-marked-candidates ()
3497 "Return marked candidates of current source if any.
3498 Otherwise one element list of current selection.
3500 It is analogous to `dired-get-marked-files'."
3501 (with-current-buffer (anything-buffer-get)
3502 (let ((cands
3503 (if anything-marked-candidates
3504 (loop with current-src = (anything-get-current-source)
3505 for (source . real) in (reverse anything-marked-candidates)
3506 when (equal current-src source)
3507 collect (anything-coerce-selection real source))
3508 (list (anything-get-selection)))))
3509 (anything-log-eval cands)
3510 cands)))
3512 (defun anything-reset-marked-candidates ()
3513 (with-current-buffer (anything-buffer-get)
3514 (set (make-local-variable 'anything-marked-candidates) nil)))
3516 (add-hook 'anything-after-initialize-hook 'anything-reset-marked-candidates)
3517 ;; (add-hook 'anything-after-action-hook 'anything-reset-marked-candidates)
3519 (defun anything-current-source-name= (name)
3520 (save-excursion
3521 (goto-char (anything-get-previous-header-pos))
3522 (equal name (anything-current-line-contents))))
3524 (defun anything-revive-visible-mark ()
3525 "Restore marked candidates when anything update display."
3526 (with-current-buffer anything-buffer
3527 (dolist (o anything-visible-mark-overlays)
3528 (goto-char (point-min))
3529 (while (and (search-forward (overlay-get o 'string) nil t)
3530 (anything-current-source-name= (overlay-get o 'source)))
3531 ;; Calculate real value of candidate.
3532 ;; It can be nil if candidate have only a display value.
3533 (let ((real (get-text-property (point-at-bol 0) 'anything-realvalue)))
3534 (if real
3535 ;; Check if real value of current candidate is the same
3536 ;; that the one stored in overlay.
3537 (and (string= (overlay-get o 'real) real)
3538 (move-overlay o (point-at-bol 0) (1+ (point-at-eol 0))))
3539 (move-overlay o (point-at-bol 0) (1+ (point-at-eol 0)))))))))
3540 (add-hook 'anything-update-hook 'anything-revive-visible-mark)
3542 (defun anything-next-point-in-list (curpos points &optional prev)
3543 (cond
3544 ;; rule out special cases
3545 ((null points) curpos)
3546 ((and prev (< curpos (car points))) curpos)
3547 ((< (car (last points)) curpos)
3548 (if prev (car (last points)) curpos))
3550 (nth (if prev
3551 (loop for pt in points
3552 for i from 0
3553 if (<= curpos pt)
3554 do (return (1- i)))
3555 (loop for pt in points
3556 for i from 0
3557 if (< curpos pt)
3558 do (return i)))
3559 points))))
3561 (defun anything-next-visible-mark (&optional prev)
3562 "Move next anything visible mark.
3563 If PREV is non-nil move to precedent."
3564 (interactive)
3565 (with-anything-window
3566 (goto-char (anything-next-point-in-list
3567 (point)
3568 (sort (mapcar 'overlay-start anything-visible-mark-overlays) '<)
3569 prev))
3570 (anything-mark-current-line)))
3572 (defun anything-prev-visible-mark ()
3573 "Move previous anything visible mark."
3574 (interactive)
3575 (anything-next-visible-mark t))
3577 ;; (@* "Utility: `find-file' integration")
3578 (defun anything-quit-and-find-file ()
3579 "Drop into `find-file' from `anything' like `iswitchb-find-file'.
3580 If current selection is a buffer or a file, `find-file' from its directory."
3581 (interactive)
3582 (anything-run-after-quit
3583 (lambda (f)
3584 (if (file-exists-p f)
3585 (let ((default-directory (file-name-directory f)))
3586 (call-interactively 'find-file))
3587 (call-interactively 'find-file)))
3588 (anything-aif (get-buffer (anything-get-selection))
3589 (buffer-file-name it)
3590 (expand-file-name (anything-get-selection)))))
3592 ;; (@* "Utility: Selection Paste")
3593 (defun anything-yank-selection ()
3594 "Set minibuffer contents to current selection."
3595 (interactive)
3596 (anything-set-pattern (anything-get-selection nil t)))
3598 (defun anything-kill-selection-and-quit ()
3599 "Store current selection to kill ring.
3600 You can paste it by typing \\[yank]."
3601 (interactive)
3602 (anything-run-after-quit
3603 (lambda (sel)
3604 (kill-new sel)
3605 (message "Killed: %s" sel))
3606 (anything-get-selection nil t)))
3609 ;; (@* "Utility: Automatical execution of persistent-action")
3610 (add-to-list 'minor-mode-alist '(anything-follow-mode " AFollow"))
3611 (defun anything-follow-mode ()
3612 "If this mode is on, persistent action is executed everytime the cursor is moved."
3613 (interactive)
3614 (with-current-buffer anything-buffer
3615 (setq anything-follow-mode (not anything-follow-mode))
3616 (message "anything-follow-mode is %s"
3617 (if anything-follow-mode "enabled" "disabled"))))
3619 (defun anything-follow-execute-persistent-action-maybe ()
3620 "Execute persistent action in mode `anything-follow-mode'.
3621 This happen after `anything-input-idle-delay' secs."
3622 (and (not (get-buffer-window anything-action-buffer 'visible))
3623 (buffer-local-value 'anything-follow-mode
3624 (get-buffer-create anything-buffer))
3625 (sit-for anything-input-idle-delay)
3626 (anything-window)
3627 (anything-get-selection)
3628 (save-excursion
3629 (anything-execute-persistent-action))))
3631 ;; (@* "Utility: Migrate `anything-sources' to my-anything command")
3632 (defun anything-migrate-sources ()
3633 "Help to migrate to new `anything' way."
3634 (interactive)
3635 (with-current-buffer (get-buffer-create "*anything migrate*")
3636 (erase-buffer)
3637 (insert (format "\
3638 Setting `anything-sources' directly is not good because
3639 `anything' is not for one command. For now, interactive use of
3640 `anything' (M-x anything) is only for demonstration purpose.
3641 So you should define commands calling `anything'.
3642 I help you to migrate to the new way.
3644 The code below is automatically generated from current
3645 `anything-sources' value. You can use the `my-anything' command
3646 now!
3648 Copy and paste it to your .emacs. Then substitute `my-anything'
3649 for `anything' bindings in all `define-key', `local-set-key' and
3650 `global-set-key' calls.
3652 \(defun my-anything ()
3653 \"Anything command for you.
3655 It is automatically generated by `anything-migrate-sources'.\"
3656 (interactive)
3657 (anything-other-buffer
3659 \"*my-anything*\"))
3660 " anything-sources))
3661 (eval-last-sexp nil)
3662 (substitute-key-definition 'anything 'my-anything global-map)
3663 (pop-to-buffer (current-buffer))))
3665 ;; (@* "Compatibility")
3667 ;; Copied assoc-default from XEmacs version 21.5.12
3668 (unless (fboundp 'assoc-default)
3669 (defun assoc-default (key alist &optional test default)
3670 "Find object KEY in a pseudo-alist ALIST.
3671 ALIST is a list of conses or objects. Each element (or the element's car,
3672 if it is a cons) is compared with KEY by evaluating (TEST (car elt) KEY).
3673 If that is non-nil, the element matches;
3674 then `assoc-default' returns the element's cdr, if it is a cons,
3675 or DEFAULT if the element is not a cons.
3677 If no element matches, the value is nil.
3678 If TEST is omitted or nil, `equal' is used."
3679 (let (found (tail alist) value)
3680 (while (and tail (not found))
3681 (let ((elt (car tail)))
3682 (when (funcall (or test 'equal) (if (consp elt) (car elt) elt) key)
3683 (setq found t value (if (consp elt) (cdr elt) default))))
3684 (setq tail (cdr tail)))
3685 value)))
3687 ;; Function not available in XEmacs,
3688 (unless (fboundp 'minibuffer-contents)
3689 (defun minibuffer-contents ()
3690 "Return the user input in a minbuffer as a string.
3691 The current buffer must be a minibuffer."
3692 (field-string (point-max)))
3694 (defun delete-minibuffer-contents ()
3695 "Delete all user input in a minibuffer.
3696 The current buffer must be a minibuffer."
3697 (delete-field (point-max))))
3699 ;; Function not available in older Emacs (<= 22.1).
3700 (unless (fboundp 'buffer-chars-modified-tick)
3701 (defun buffer-chars-modified-tick (&optional buffer)
3702 "Return BUFFER's character-change tick counter.
3703 Each buffer has a character-change tick counter, which is set to the
3704 value of the buffer's tick counter (see `buffer-modified-tick'), each
3705 time text in that buffer is inserted or deleted. By comparing the
3706 values returned by two individual calls of `buffer-chars-modified-tick',
3707 you can tell whether a character change occurred in that buffer in
3708 between these calls. No argument or nil as argument means use current
3709 buffer as BUFFER."
3710 (with-current-buffer (or buffer (current-buffer))
3711 (if (listp buffer-undo-list)
3712 (length buffer-undo-list)
3713 (buffer-modified-tick)))))
3715 ;; (@* "CUA workaround")
3716 (defadvice cua-delete-region (around anything-avoid-cua activate)
3717 (ignore-errors ad-do-it))
3719 (defadvice copy-region-as-kill (around anything-avoid-cua activate)
3720 (if cua-mode
3721 (ignore-errors ad-do-it)
3722 ad-do-it))
3724 ;;(@* "Attribute Documentation")
3725 (defun anything-describe-anything-attribute (anything-attribute)
3726 "Display the full documentation of ANYTHING-ATTRIBUTE.
3727 ANYTHING-ATTRIBUTE should be a symbol."
3728 (interactive (list (intern
3729 (completing-read
3730 "Describe anything attribute: "
3731 (mapcar 'symbol-name anything-additional-attributes)))))
3732 (with-output-to-temp-buffer "*Help*"
3733 (princ (get anything-attribute 'anything-attrdoc))))
3735 (anything-document-attribute 'name "mandatory"
3736 " The name of the source. It is also the heading which appears
3737 above the list of matches from the source. Must be unique. ")
3738 (anything-document-attribute 'header-name "optional"
3739 " A function returning the display string of the header. Its
3740 argument is the name of the source. This attribute is useful to
3741 add an additional information with the source name. ")
3742 (anything-document-attribute 'candidates "mandatory if candidates-in-buffer attribute is not provided"
3743 " Specifies how to retrieve candidates from the source. It can
3744 either be a variable name, a function called with no parameters
3745 or the actual list of candidates.
3747 The list must be a list whose members are strings, symbols
3748 or (DISPLAY . REAL) pairs.
3750 In case of (DISPLAY . REAL) pairs, the DISPLAY string is shown
3751 in the Anything buffer, but the REAL one is used as action
3752 argument when the candidate is selected. This allows a more
3753 readable presentation for candidates which would otherwise be,
3754 for example, too long or have a common part shared with other
3755 candidates which can be safely replaced with an abbreviated
3756 string for display purposes.
3758 Note that if the (DISPLAY . REAL) form is used then pattern
3759 matching is done on the displayed string, not on the real
3760 value.
3762 If the candidates have to be retrieved asynchronously (for
3763 example, by an external command which takes a while to run)
3764 then the function should start the external command
3765 asynchronously and return the associated process object.
3766 Anything will take care of managing the process (receiving the
3767 output from it, killing it if necessary, etc.). The process
3768 should return candidates matching the current pattern (see
3769 variable `anything-pattern'.)
3771 Note that currently results from asynchronous sources appear
3772 last in the anything buffer regardless of their position in
3773 `anything-sources'. ")
3774 (anything-document-attribute 'action "mandatory if type attribute is not provided"
3775 " It is a list of (DISPLAY . FUNCTION) pairs or FUNCTION.
3776 FUNCTION is called with one parameter: the selected candidate.
3778 An action other than the default can be chosen from this list
3779 of actions for the currently selected candidate (by default
3780 with TAB). The DISPLAY string is shown in the completions
3781 buffer and the FUNCTION is invoked when an action is
3782 selected. The first action of the list is the default. ")
3783 (anything-document-attribute 'coerce "optional"
3784 " It's a function called with one argument: the selected candidate.
3786 This function is intended for type convertion.
3787 In normal case, the selected candidate (string) is passed to action function.
3788 If coerce function is specified, it is called just before action function.
3790 Example: converting string to symbol
3791 (coerce . intern) ")
3792 (anything-document-attribute 'type "optional if action attribute is provided"
3793 " Indicates the type of the items the source returns.
3795 Merge attributes not specified in the source itself from
3796 `anything-type-attributes'.
3798 This attribute is implemented by plug-in. ")
3799 (anything-document-attribute 'init "optional"
3800 " Function called with no parameters when anything is started. It
3801 is useful for collecting current state information which can be
3802 used to create the list of candidates later.
3804 For example, if a source needs to work with the current
3805 directory then it can store its value here, because later
3806 anything does its job in the minibuffer and in the
3807 `anything-buffer' and the current directory can be different
3808 there. ")
3809 (anything-document-attribute 'delayed-init "optional"
3810 " Function called with no parameters before candidate function is
3811 called. It is similar with `init' attribute, but its
3812 evaluation is deferred. It is useful to combine with ")
3813 (anything-document-attribute 'match "optional"
3814 " List of functions called with one parameter: a candidate. The
3815 function should return non-nil if the candidate matches the
3816 current pattern (see variable `anything-pattern').
3818 This attribute allows the source to override the default
3819 pattern matching based on `string-match'. It can be used, for
3820 example, to implement a source for file names and do the
3821 pattern matching on the basename of files, since it's more
3822 likely one is typing part of the basename when searching for a
3823 file, instead of some string anywhere else in its path.
3825 If the list contains more than one function then the list of
3826 matching candidates from the source is constructed by appending
3827 the results after invoking the first function on all the
3828 potential candidates, then the next function, and so on. The
3829 matching candidates supplied by the first function appear first
3830 in the list of results and then results from the other
3831 functions, respectively.
3833 This attribute has no effect for asynchronous sources (see
3834 attribute `candidates'), since they perform pattern matching
3835 themselves. ")
3836 (anything-document-attribute 'candidate-transformer "optional"
3837 " It's a function or a list of functions called with one argument
3838 when the completion list from the source is built. The argument
3839 is the list of candidates retrieved from the source. The
3840 function should return a transformed list of candidates which
3841 will be used for the actual completion. If it is a list of
3842 functions, it calls each function sequentially.
3844 This can be used to transform or remove items from the list of
3845 candidates.
3847 Note that `candidates' is run already, so the given transformer
3848 function should also be able to handle candidates with (DISPLAY
3849 . REAL) format. ")
3850 (anything-document-attribute 'filtered-candidate-transformer "optional"
3851 " It has the same format as `candidate-transformer', except the
3852 function is called with two parameters: the candidate list and
3853 the source.
3855 This transformer is run on the candidate list which is already
3856 filtered by the current pattern. While `candidate-transformer'
3857 is run only once, it is run every time the input pattern is
3858 changed.
3860 It can be used to transform the candidate list dynamically, for
3861 example, based on the current pattern.
3863 In some cases it may also be more efficent to perform candidate
3864 transformation here, instead of with `candidate-transformer'
3865 even if this transformation is done every time the pattern is
3866 changed. For example, if a candidate set is very large then
3867 `candidate-transformer' transforms every candidate while only
3868 some of them will actually be dislpayed due to the limit
3869 imposed by `anything-candidate-number-limit'.
3871 Note that `candidates' and `candidate-transformer' is run
3872 already, so the given transformer function should also be able
3873 to handle candidates with (DISPLAY . REAL) format.
3875 This option has no effect for asynchronous sources. (Not yet,
3876 at least. ")
3877 (anything-document-attribute 'action-transformer "optional"
3878 " It's a function or a list of functions called with two
3879 arguments when the action list from the source is
3880 assembled. The first argument is the list of actions, the
3881 second is the current selection. If it is a list of functions,
3882 it calls each function sequentially.
3884 The function should return a transformed action list.
3886 This can be used to customize the list of actions based on the
3887 currently selected candidate. ")
3888 (anything-document-attribute 'pattern-transformer "optional"
3889 " It's a function or a list of functions called with one argument
3890 before computing matches. Its argument is `anything-pattern'.
3891 Functions should return transformed `anything-pattern'.
3893 It is useful to change interpretation of `anything-pattern'. ")
3894 (anything-document-attribute 'delayed "optional"
3895 " Candidates from the source are shown only if the user stops
3896 typing and is idle for `anything-idle-delay' seconds. ")
3897 (anything-document-attribute 'volatile "optional"
3898 " Indicates the source assembles the candidate list dynamically,
3899 so it shouldn't be cached within a single Anything
3900 invocation. It is only applicable to synchronous sources,
3901 because asynchronous sources are not cached. ")
3902 (anything-document-attribute 'requires-pattern "optional"
3903 " If present matches from the source are shown only if the
3904 pattern is not empty. Optionally, it can have an integer
3905 parameter specifying the required length of input which is
3906 useful in case of sources with lots of candidates. ")
3907 (anything-document-attribute 'persistent-action "optional"
3908 " Function called with one parameter; the selected candidate.
3910 An action performed by `anything-execute-persistent-action'.
3911 If none, use the default action. ")
3912 (anything-document-attribute 'candidates-in-buffer "optional"
3913 " Shortcut attribute for making and narrowing candidates using
3914 buffers. This newly-introduced attribute prevents us from
3915 forgetting to add volatile and match attributes.
3917 See docstring of `anything-candidates-in-buffer'.
3919 (candidates-in-buffer) is equivalent of three attributes:
3920 (candidates . anything-candidates-in-buffer)
3921 (volatile)
3922 (match identity)
3924 (candidates-in-buffer . candidates-function) is equivalent of:
3925 (candidates . candidates-function)
3926 (volatile)
3927 (match identity)
3929 This attribute is implemented by plug-in. ")
3930 (anything-document-attribute 'search "optional"
3931 " List of functions like `re-search-forward' or `search-forward'.
3932 Buffer search function used by `anything-candidates-in-buffer'.
3933 By default, `anything-candidates-in-buffer' uses `re-search-forward'.
3934 This attribute is meant to be used with
3935 (candidates . anything-candidates-in-buffer) or
3936 (candidates-in-buffer) in short. ")
3937 (anything-document-attribute 'search-from-end "optional"
3938 " Make `anything-candidates-in-buffer' search from the end of buffer.
3939 If this attribute is specified, `anything-candidates-in-buffer' uses
3940 `re-search-backward' instead. ")
3941 (anything-document-attribute 'get-line "optional"
3942 " A function like `buffer-substring-no-properties' or `buffer-substring'.
3943 This function converts point of line-beginning and point of line-end,
3944 which represents a candidate computed by `anything-candidates-in-buffer'.
3945 By default, `anything-candidates-in-buffer' uses
3946 `buffer-substring-no-properties'. ")
3947 (anything-document-attribute 'display-to-real "optional"
3948 " Function called with one parameter; the selected candidate.
3950 The function transforms the selected candidate, and the result
3951 is passed to the action function. The display-to-real
3952 attribute provides another way to pass other string than one
3953 shown in Anything buffer.
3955 Traditionally, it is possible to make candidates,
3956 candidate-transformer or filtered-candidate-transformer
3957 function return a list with (DISPLAY . REAL) pairs. But if REAL
3958 can be generated from DISPLAY, display-to-real is more
3959 convenient and faster. ")
3960 (anything-document-attribute 'real-to-display "optional"
3961 " Function called with one parameter; the selected candidate.
3963 The inverse of display-to-real attribute.
3965 The function transforms the selected candidate, which is passed
3966 to the action function, for display. The real-to-display
3967 attribute provides the other way to pass other string than one
3968 shown in Anything buffer.
3970 Traditionally, it is possible to make candidates,
3971 candidate-transformer or filtered-candidate-transformer
3972 function return a list with (DISPLAY . REAL) pairs. But if
3973 DISPLAY can be generated from REAL, real-to-display is more
3974 convenient.
3976 Note that DISPLAY parts returned from candidates /
3977 candidate-transformer are IGNORED as the name `display-to-real'
3978 says. ")
3979 (anything-document-attribute 'cleanup "optional"
3980 " Function called with no parameters when *anything* buffer is closed. It
3981 is useful for killing unneeded candidates buffer.
3983 Note that the function is executed BEFORE performing action. ")
3984 (anything-document-attribute 'candidate-number-limit "optional"
3985 " Override `anything-candidate-number-limit' only for this source. ")
3986 (anything-document-attribute 'accept-empty "optional"
3987 " Pass empty string \"\" to action function. ")
3988 (anything-document-attribute 'disable-shortcuts "optional"
3989 " Disable `anything-enable-shortcuts' in current `anything' session.
3991 This attribute is implemented by plug-in. ")
3992 (anything-document-attribute 'dummy "optional"
3993 " Set `anything-pattern' to candidate. If this attribute is
3994 specified, The candidates attribute is ignored.
3996 This attribute is implemented by plug-in.
3997 This plug-in implies disable-shortcuts plug-in. ")
3998 (anything-document-attribute 'multiline "optional"
3999 " Enable to selection multiline candidates. ")
4000 (anything-document-attribute 'update "optional"
4001 ;; FIXME: this is not displayed correctly in help buffer.
4002 " Function called with no parameters when \\<anything-map>\\[anything-force-update] is pressed. ")
4003 (anything-document-attribute 'mode-line "optional"
4004 " source local `anything-mode-line-string'. (included in `mode-line-format')
4005 It accepts also variable/function name. ")
4006 (anything-document-attribute 'header-line "optional"
4007 " source local `header-line-format'.
4008 It accepts also variable/function name. ")
4009 (anything-document-attribute
4010 'resume "optional"
4011 " Function called with no parameters when `anything-resume' is started.")
4013 ;; (@* "Bug Report")
4014 (defvar anything-maintainer-mail-address
4015 (concat "rubiki" "tch@ru" "by-lang.org"))
4016 (defvar anything-bug-report-salutation
4017 "Describe bug below, using a precise recipe.
4019 When I executed M-x ...
4021 How to send a bug report:
4022 1) Be sure to use the LATEST version of anything.el.
4023 2) Enable debugger. M-x toggle-debug-on-error or (setq debug-on-error t)
4024 3) Use Lisp version instead of compiled one: (load \"anything.el\")
4025 4) If you got an error, please paste *Backtrace* buffer.
4026 5) Type C-c C-c to send.
4027 # If you are a Japanese, please write in Japanese:-)")
4028 (defvar anything-no-dump-variables
4029 '(anything-candidate-buffer-alist
4030 anything-digit-overlays
4031 anything-help-message
4032 anything-candidate-cache
4034 "Variables not to dump in bug report.")
4036 (defun anything-dumped-variables-in-bug-report ()
4037 (let ((hash (make-hash-table)))
4038 (loop for var in (apropos-internal "anything-" 'boundp)
4039 for vname = (symbol-name var)
4040 unless (or (string-match "-map$" vname)
4041 (string-match "^anything-c-source-" vname)
4042 (string-match "-hash$" vname)
4043 (string-match "-face$" vname)
4044 (memq var anything-no-dump-variables))
4045 collect var)))
4047 (defun anything-send-bug-report ()
4048 "Send a bug report of anything.el."
4049 (interactive)
4050 (with-current-buffer (or anything-last-buffer
4051 (current-buffer))
4052 (reporter-submit-bug-report
4053 anything-maintainer-mail-address
4054 "anything.el"
4055 (anything-dumped-variables-in-bug-report)
4056 nil nil
4057 anything-bug-report-salutation)))
4059 (defun anything-send-bug-report-from-anything ()
4060 "Send a bug report of anything.el in anything session."
4061 (interactive)
4062 (anything-run-after-quit 'anything-send-bug-report))
4064 ;; Debugging function.
4065 (defun* anything-test-candidates
4066 (sources &optional (input "")
4067 (compile-source-functions
4068 anything-compile-source-functions-default))
4069 "Test helper function for anything.
4070 Given pseudo `anything-sources' and `anything-pattern', returns list like
4071 ((\"source name1\" (\"candidate1\" \"candidate2\"))
4072 (\"source name2\" (\"candidate3\" \"candidate4\")))"
4073 (let ((anything-test-mode t)
4074 anything-enable-shortcuts
4075 anything-candidate-cache
4076 (anything-compile-source-functions compile-source-functions)
4077 anything-before-initialize-hook
4078 anything-after-initialize-hook
4079 anything-update-hook
4080 anything-test-candidate-list)
4081 (get-buffer-create anything-buffer)
4082 (anything-initialize nil input sources)
4083 (anything-update)
4084 ;; test-mode spec: select 1st candidate!
4085 (with-current-buffer anything-buffer
4086 (forward-line 1)
4087 (anything-mark-current-line))
4088 (prog1
4089 anything-test-candidate-list
4090 (anything-cleanup))))
4092 ;; (@* "Unit Tests")
4093 ;; See developer-tools/unit-test-anything.el
4095 (provide 'anything)
4097 ;; How to save (DO NOT REMOVE!!)
4098 ;; (progn (magit-push) (emacswiki-post "anything.el"))
4100 ;; Local Variables:
4101 ;; coding: utf-8
4102 ;; End:
4104 ;;; anything.el ends here