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