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