anything-config.el (anything-insert-buffer-name): use `anything-set-pattern' instead...
[anything-config.git] / anything.el
blobb90bf5bf4ad8f40d5bcdcf63ad52e69e59ae1040
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
1180 (src (anything-get-current-source)))
1181 "Get the value of ATTRIBUTE-NAME of SRC.
1182 if SRC is omitted, use current source.
1183 It is useful to write your sources."
1184 (anything-aif (assq attribute-name src)
1185 (cdr it)))
1187 (defun* anything-attr* (attribute-name
1188 &optional (src (anything-get-current-source)))
1189 "Pass the value of ATTRIBUTE-NAME of SRC to `anything-interpret-value'.
1190 if SRC is omitted, use current source.
1191 It is useful to write your sources."
1192 (anything-interpret-value (anything-attr attribute-name src)))
1194 (defun* anything-attr-defined (attribute-name
1195 &optional (src (anything-get-current-source)))
1196 "Return non-nil if ATTRIBUTE-NAME of SRC is defined.
1197 if SRC is omitted, use current source.
1198 It is useful to write your sources."
1199 (and (assq attribute-name src) t))
1201 (defun* anything-attrset (attribute-name value
1202 &optional
1203 (src (anything-get-current-source)))
1204 "Set the value of ATTRIBUTE-NAME of SRC to VALUE.
1205 if SRC is omitted, use current source.
1206 It is useful to write your sources."
1207 (anything-aif (assq attribute-name src)
1208 (setcdr it value)
1209 (setcdr src (cons (cons attribute-name value) (cdr src))))
1210 value)
1212 (defun anything-set-source-filter (sources)
1213 "Set the value of `anything-source-filter' to SOURCES and update.
1215 This function sets a filter for anything sources and it may be
1216 called while anything is running. It can be used to toggle
1217 displaying of sources dinamically. For example, additional keys
1218 can be bound into `anything-map' to display only the file-related
1219 results if there are too many matches from other sources and
1220 you're after files only:
1222 Shift+F shows only file results from some sources:
1224 \(define-key anything-map \"F\" 'anything-my-show-files-only)
1226 \(defun anything-my-show-files-only ()
1227 (interactive)
1228 (anything-set-source-filter '(\"File Name History\"
1229 \"Files from Current Directory\")))
1231 Shift+A shows all results:
1233 \(define-key anything-map \"A\" 'anything-my-show-all)
1235 \(defun anything-my-show-all ()
1236 (interactive)
1237 (anything-set-source-filter nil))
1239 Note that you have to prefix the functions with anything- prefix,
1240 otherwise they won't be bound when Anything is used under
1241 Iswitchb. The -my- part is added to avoid collisions with
1242 existing Anything function names."
1243 (unless (and (listp sources)
1244 (loop for name in sources always (stringp name)))
1245 (error "Invalid data in `anything-set-source-filter': %S" sources))
1246 (setq anything-source-filter sources)
1247 (anything-log-eval anything-source-filter)
1248 (anything-update))
1250 (defun anything-set-sources (sources &optional no-init no-update)
1251 "Set SOURCES during `anything' invocation.
1252 If NO-INIT is non-nil, skip executing init functions of SOURCES.
1253 If NO-UPDATE is non-nil, skip executing `anything-update'."
1254 (with-current-buffer anything-buffer
1255 (setq anything-compiled-sources nil
1256 anything-sources sources
1257 anything-last-sources-local sources)
1258 (anything-log-eval anything-compiled-sources anything-sources))
1259 (unless no-init (anything-funcall-foreach 'init))
1260 (unless no-update (anything-update)))
1262 (defvar anything-compile-source-functions
1263 '(anything-compile-source--type
1264 anything-compile-source--dummy
1265 anything-compile-source--disable-shortcuts
1266 anything-compile-source--candidates-in-buffer)
1267 "Functions to compile elements of `anything-sources' (plug-in).")
1269 (defun anything-get-sources ()
1270 "Return compiled `anything-sources', which is memoized.
1272 Attributes:
1274 - type
1275 `anything-type-attributes' are merged in.
1276 - candidates-buffer
1277 candidates, volatile and match attrubute are created."
1278 (cond
1279 ;; action
1280 ((anything-action-window)
1281 anything-sources)
1282 ;; memoized
1283 (anything-compiled-sources)
1284 ;; first time
1286 (prog1
1287 (setq anything-compiled-sources
1288 (anything-compile-sources
1289 anything-sources anything-compile-source-functions))
1290 (anything-log-eval anything-compiled-sources)))))
1292 (defun* anything-get-selection (&optional (buffer nil buffer-s) (force-display-part))
1293 "Return the currently selected item or nil.
1294 if BUFFER is nil or unspecified, use anything-buffer as default value.
1295 If FORCE-DISPLAY-PART is non-nil, return the display string."
1296 (setq buffer (if (and buffer buffer-s) buffer anything-buffer))
1297 (unless (anything-empty-buffer-p buffer)
1298 (with-current-buffer buffer
1299 (let ((selection
1300 (or (and (not force-display-part)
1301 (get-text-property (overlay-start
1302 anything-selection-overlay)
1303 'anything-realvalue))
1304 (let ((disp (buffer-substring-no-properties
1305 (overlay-start anything-selection-overlay)
1306 (1- (overlay-end anything-selection-overlay))))
1307 (source (anything-get-current-source)))
1308 (anything-aif (and (not force-display-part)
1309 (assoc-default 'display-to-real source))
1310 (anything-funcall-with-source source it disp)
1311 disp)))))
1312 (unless (equal selection "")
1313 (anything-log-eval selection)
1314 selection)))))
1316 (defun anything-get-action ()
1317 "Return the associated action for the selected candidate.
1318 It is a function symbol \(sole action\) or list
1319 of \(action-display . function\)."
1320 (unless (anything-empty-buffer-p (anything-buffer-get))
1321 (anything-aif (anything-attr 'action-transformer)
1322 (anything-composed-funcall-with-source
1323 (anything-get-current-source) it
1324 (anything-attr 'action) (anything-get-selection))
1325 (anything-attr 'action))))
1327 (defun anything-get-current-source ()
1328 "Return the source for the current selection.
1329 Use it in init, candidates, action, candidate-transformer,
1330 filtered-candidate-transformer functions."
1331 (declare (special source))
1332 ;; The name `anything-get-current-source' should be used in init function etc.
1333 (if (and (boundp 'anything-source-name) (stringp anything-source-name))
1334 source
1335 (with-current-buffer (anything-buffer-get)
1336 (or (get-text-property (point) 'anything-source)
1337 (block exit
1338 ;; This goto-char shouldn't be necessary, but point is moved to
1339 ;; point-min somewhere else which shouldn't happen.
1340 (goto-char (overlay-start anything-selection-overlay))
1341 (let* ((header-pos (or (anything-get-previous-header-pos)
1342 (anything-get-next-header-pos)))
1343 (source-name
1344 (save-excursion
1345 (unless header-pos
1346 ;(message "No candidates")
1347 (return-from exit nil))
1348 (goto-char header-pos)
1349 (anything-current-line-contents))))
1350 (some (lambda (source)
1351 (if (equal (assoc-default 'name source) source-name)
1352 source))
1353 (anything-get-sources))))))))
1355 (defun anything-buffer-is-modified (buffer)
1356 "Return non-nil when BUFFER is modified since `anything' was invoked."
1357 (let* ((b (get-buffer buffer))
1358 (key (concat (buffer-name b) "/" (anything-attr 'name)))
1359 (source-tick (or (gethash key anything-tick-hash) 0))
1360 (buffer-tick (buffer-chars-modified-tick b))
1361 (modifiedp (/= source-tick buffer-tick)))
1362 (puthash key buffer-tick anything-tick-hash)
1363 (anything-log-eval buffer modifiedp)
1364 modifiedp))
1366 (defun anything-current-buffer-is-modified ()
1367 "Check if `anything-current-buffer' is modified since `anything' was invoked."
1368 (anything-buffer-is-modified anything-current-buffer))
1370 (defvar anything-quit nil)
1371 (defun anything-run-after-quit (function &rest args)
1372 "Perform an action after quitting `anything'.
1373 The action is to call FUNCTION with arguments ARGS."
1374 (setq anything-quit t)
1375 (anything-log-eval function args)
1376 (apply 'run-with-idle-timer 0 nil function args)
1377 (anything-exit-minibuffer))
1380 (defun define-anything-type-attribute (type definition &optional doc)
1381 "Register type attribute of TYPE as DEFINITION with DOC.
1382 DOC is displayed in `anything-type-attributes' docstring.
1384 Use this function is better than setting `anything-type-attributes' directly."
1385 (loop for i in definition do
1386 ;; without `ignore-errors', error at emacs22
1387 (ignore-errors (setf i (delete nil i))))
1388 (anything-add-type-attribute type definition)
1389 (and doc (anything-document-type-attribute type doc))
1390 nil)
1392 (defvaralias 'anything-attributes 'anything-additional-attributes)
1393 (defvar anything-additional-attributes nil
1394 "List of all `anything' attributes.")
1395 (defun anything-document-attribute (attribute short-doc &optional long-doc)
1396 "Register ATTRIBUTE documentation introduced by plug-in.
1397 SHORT-DOC is displayed beside attribute name.
1398 LONG-DOC is displayed below attribute name and short documentation."
1399 (declare (indent 2) (debug t))
1400 (if long-doc
1401 (setq short-doc (concat "(" short-doc ")"))
1402 (setq long-doc short-doc
1403 short-doc ""))
1404 (add-to-list 'anything-additional-attributes attribute t)
1405 (put attribute 'anything-attrdoc
1406 (concat "- " (symbol-name attribute)
1407 " " short-doc "\n\n" long-doc "\n")))
1410 (defun anything-interpret-value (value &optional source)
1411 "Interpret VALUE as variable, function or literal.
1412 If VALUE is a function, call it with no arguments and return the value.
1413 If SOURCE is `anything' source, `anything-source-name' is source name.
1415 If VALUE is a variable, return the value.
1417 If VALUE is a symbol, but it is not a function or a variable, cause an error.
1419 Otherwise, return VALUE itself."
1420 (cond ((and source (functionp value))
1421 (anything-funcall-with-source source value))
1422 ((functionp value)
1423 (funcall value))
1424 ((and (symbolp value) (boundp value))
1425 (symbol-value value))
1426 ((symbolp value)
1427 (error "anything-interpret-value: Symbol must be a function or a variable"))
1429 value)))
1431 (defun anything-once (function &rest args)
1432 "Ensure FUNCTION with ARGS to be called once in `anything' session."
1433 (let ((spec (cons function args)))
1434 (unless (member spec anything-once-called-functions)
1435 (apply function args)
1436 (push spec anything-once-called-functions))))
1439 ;; (@* "Core: API helper")
1440 (defun* anything-empty-buffer-p (&optional (buffer anything-buffer))
1441 "Check if BUFFER have candidates.
1442 Default value for BUFFER is `anything-buffer'."
1443 (zerop (buffer-size (and buffer (get-buffer buffer)))))
1445 (defun anything-let-eval-varlist (varlist)
1446 "Return the list of pairs VARLIST with each cdr of pair evaluated.
1447 If VARLIST contain single elements, those are returned
1448 as a list of one element."
1449 (mapcar (lambda (pair)
1450 (if (listp pair)
1451 (cons (car pair) (eval (cadr pair)))
1452 (cons pair nil)))
1453 varlist))
1455 ;; [NOT USED]
1456 ;; (defun anything-let*-eval-varlist (varlist)
1457 ;; (let ((vars (mapcar (lambda (pair)
1458 ;; (or (car-safe pair) pair))
1459 ;; varlist)))
1460 ;; (eval `(let ,vars
1461 ;; ,@(mapcar (lambda (pair)
1462 ;; (if (listp pair)
1463 ;; `(setq ,(car pair) ,(cadr pair))
1464 ;; `(setq ,pair nil)))
1465 ;; varlist)
1466 ;; (mapcar (lambda (v)
1467 ;; (cons v (symbol-value v)))
1468 ;; ',vars)))))
1470 (defun anything-let-internal (binding bodyfunc)
1471 "Set BINDING to anything buffer-local variables and Evaluate BODYFUNC.
1472 BINDING is a list of \(VARNAME . VALUE\) pair."
1473 (setq anything-let-variables binding)
1474 (unwind-protect
1475 (funcall bodyfunc)
1476 (setq anything-let-variables nil)))
1479 ;; (@* "Core: tools")
1480 (defun anything-current-line-contents ()
1481 "Current line string without properties."
1482 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))
1484 (defun anything-funcall-with-source (source func &rest args)
1485 "Call from SOURCE FUNC list or single function FUNC with ARGS.
1486 FUNC can be a symbol or a list of functions.
1487 Return the result of last function call."
1488 (let ((anything-source-name (assoc-default 'name source))
1489 result)
1490 (anything-log-eval anything-source-name func args)
1491 (dolist (func (if (functionp func) (list func) func) result)
1492 (setq result (apply func args)))))
1494 (defun anything-funcall-foreach (sym)
1495 "Call the function SYM for each source if any."
1496 (dolist (source (anything-get-sources))
1497 (anything-aif (assoc-default sym source)
1498 (anything-funcall-with-source source it))))
1500 (defun anything-normalize-sources (sources)
1501 "If SOURCES is only one source, make a list of one element."
1502 (cond ((or (and sources
1503 (symbolp sources))
1504 (and (listp sources) (assq 'name sources)))
1505 (list sources))
1506 (sources)
1507 (t anything-sources)))
1509 (defun anything-approximate-candidate-number (&optional in-current-source)
1510 "Return approximate number of candidates in `anything-buffer'.
1511 If IN-CURRENT-SOURCE is provided return number of candidates
1512 in the source where point is.
1513 It is used to check if candidate number is 0, 1, or 2+."
1514 (with-current-buffer anything-buffer
1515 (save-excursion
1516 (if in-current-source
1517 (goto-char (anything-get-previous-header-pos))
1518 (goto-char (point-min)))
1519 (forward-line 1)
1520 (let ((count-multi 1))
1521 (if (anything-pos-multiline-p)
1522 (save-excursion
1523 (loop while (and (not (if in-current-source
1524 (save-excursion
1525 (forward-line 2)
1526 (or (anything-pos-header-line-p) (eobp)))
1527 (eobp)))
1528 (search-forward anything-candidate-separator nil t))
1529 do (incf count-multi)
1530 finally return count-multi))
1531 (save-excursion
1532 (loop with ln = 0
1533 while (not (if in-current-source
1534 (or (anything-pos-header-line-p) (eobp))
1535 (eobp)))
1536 unless (anything-pos-header-line-p)
1537 do (incf ln)
1538 do (forward-line 1) finally return ln)))))))
1540 (defmacro with-anything-quittable (&rest body)
1541 "If an error occur in execution of BODY, quit anything safely."
1542 (declare (indent 0) (debug t))
1543 `(let (inhibit-quit)
1544 (condition-case v
1545 (progn ,@body)
1546 (quit (setq anything-quit t)
1547 (exit-minibuffer)
1548 (keyboard-quit)))))
1550 (defun anything-compose (arg-lst func-lst)
1551 "Apply arguments specified in ARG-LST with each function of FUNC-LST.
1552 The result of each function will be the new `car' of ARG-LST.
1554 This function allows easy sequencing of transformer functions."
1555 (dolist (func func-lst)
1556 (setcar arg-lst (apply func arg-lst)))
1557 (car arg-lst))
1559 (defun anything-composed-funcall-with-source (source funcs &rest args)
1560 "With SOURCE apply `anything-funcall-with-source' with each FUNCS and ARGS.
1561 This is used in transformers to modify candidates list."
1562 (if (functionp funcs)
1563 (apply 'anything-funcall-with-source source funcs args)
1564 (apply 'anything-funcall-with-source source
1565 (lambda (&rest args)
1566 (anything-compose args funcs))
1567 args)))
1569 (defun anything-new-timer (variable timer)
1570 "Give VARIABLE value to TIMER and cancel old timer."
1571 (anything-aif (symbol-value variable)
1572 (cancel-timer it))
1573 (set variable timer))
1576 ;; (@* "Core: entry point")
1577 (defconst anything-argument-keys
1578 '(:sources :input :prompt :resume :preselect :buffer :keymap :default :history))
1580 ;;;###autoload
1581 (defun anything (&rest plist)
1582 "Main function to execute anything sources.
1584 Keywords supported:
1585 :sources :input :prompt :resume :preselect :buffer :keymap :default :history
1586 Extra keywords are supported and can be added, see below.
1588 When call interactively with no arguments deprecated `anything-sources'
1589 will be used if non--nil.
1591 PLIST is a list like \(:key1 val1 :key2 val2 ...\) or
1592 \(&optional sources input prompt resume preselect buffer keymap default history\).
1594 Basic keywords are the following:
1596 \:sources
1598 Temporary value of `anything-sources'. It also accepts a
1599 symbol, interpreted as a variable of an anything source. It
1600 also accepts an alist representing an anything source, which is
1601 detected by \(assq 'name ANY-SOURCES\)
1603 \:input
1605 Temporary value of `anything-pattern', ie. initial input of minibuffer.
1607 \:prompt
1609 Prompt other than \"pattern: \".
1611 \:resume
1613 If t, Resurrect previously instance of `anything'. Skip the initialization.
1614 If 'noresume, this instance of `anything' cannot be resumed.
1616 \:preselect
1618 Initially selected candidate. Specified by exact candidate or a regexp.
1619 Note that it is not working with delayed sources.
1621 \:buffer
1623 `anything-buffer' instead of *anything*.
1625 \:keymap
1627 `anything-map' for current `anything' session.
1629 \:default
1631 A default argument that will be inserted in minibuffer \
1632 with \\<minibuffer-local-map>\\[next-history-element].
1633 When nil of not present `thing-at-point' will be used instead.
1635 \:history
1637 By default all minibuffer input is pushed to `minibuffer-history',
1638 if an argument HISTORY is provided, input will be pushed to HISTORY.
1639 History element should be a symbol.
1641 Of course, conventional arguments are supported, the two are same.
1643 \(anything :sources sources :input input :prompt prompt :resume resume
1644 :preselect preselect :buffer buffer :keymap keymap :default default
1645 :history history\)
1646 \(anything sources input prompt resume preselect buffer keymap default history\)
1648 Other keywords are interpreted as local variables of this anything session.
1649 The `anything-' prefix can be omitted. For example,
1651 \(anything :sources 'anything-c-source-buffers
1652 :buffer \"*buffers*\" :candidate-number-limit 10\)
1654 means starting anything session with `anything-c-source-buffers'
1655 source in *buffers* buffer and set variable `anything-candidate-number-limit'
1656 to 10 as session local variable."
1657 (interactive)
1658 (if (keywordp (car plist))
1659 (anything-let-internal
1660 (anything-parse-keys plist)
1661 (lambda ()
1662 (apply 'anything
1663 (mapcar (lambda (key) (plist-get plist key))
1664 anything-argument-keys))))
1665 (apply 'anything-internal plist)))
1667 (defun anything-parse-keys (keys)
1668 "Parse the KEYS arguments of `anything'.
1669 Return only the keys that are not in `anything-argument-keys'.
1670 It is used to set local variables via `anything-let-internal'.
1671 This allow to add arguments that are not part of `anything-argument-keys',
1672 but are valid anything attributes.
1673 i.e :candidate-number-limit will be bound to `anything-candidate-number-limit'
1674 in source."
1675 ;; (anything-parse-keys '(:sources ((name . "test")
1676 ;; (candidates . (a b c)))
1677 ;; :buffer "toto"
1678 ;; :candidate-number-limit 4))
1679 ;; ==> ((anything-candidate-number-limit . 4))
1680 (loop for (key value &rest _) on keys by #'cddr
1681 for symname = (substring (symbol-name key) 1)
1682 for sym = (intern (if (string-match "^anything-" symname)
1683 symname
1684 (concat "anything-" symname)))
1685 unless (memq key anything-argument-keys)
1686 collect (cons sym value)))
1688 ;;; (@* "Core: entry point helper")
1689 (defun anything-internal (&optional
1690 any-sources any-input
1691 any-prompt any-resume
1692 any-preselect any-buffer
1693 any-keymap any-default any-history)
1694 "The internal anything function called by `anything'.
1695 For ANY-SOURCES ANY-INPUT ANY-PROMPT ANY-RESUME ANY-PRESELECT ANY-BUFFER and
1696 ANY-KEYMAP ANY-DEFAULT ANY-HISTORY See `anything'."
1697 (anything-log (concat "[Start session] " (make-string 41 ?+)))
1698 (anything-log-eval any-prompt any-preselect
1699 any-buffer any-keymap any-default)
1700 (unwind-protect
1701 (condition-case v
1702 (let (;; It is needed because `anything-source-name' is non-nil
1703 ;; when `anything' is invoked by action. Awful global scope.
1704 anything-source-name
1705 anything-in-persistent-action
1706 anything-quit
1707 (case-fold-search t)
1708 (anything-buffer (or any-buffer anything-buffer))
1709 ;; cua-mode ; avoid error when region is selected
1711 (with-anything-restore-variables
1712 (anything-initialize any-resume any-input any-sources)
1713 (anything-display-buffer anything-buffer)
1714 (anything-log "show prompt")
1715 (unwind-protect
1716 (anything-read-pattern-maybe
1717 any-prompt any-input any-preselect
1718 any-resume any-keymap any-default
1719 (when (and any-history (symbolp any-history)) any-history))
1720 (anything-cleanup)))
1721 (prog1 (unless anything-quit
1722 (anything-execute-selection-action-1))
1723 (anything-log (concat "[End session] " (make-string 41 ?-)))))
1724 (quit
1725 (anything-restore-position-on-quit)
1726 (anything-log (concat "[End session (quit)] " (make-string 34 ?-)))
1727 nil))
1728 (anything-log-save-maybe)))
1731 ;;; Anything resume
1734 (defun* anything-resume (&optional
1735 (any-buffer anything-last-buffer)
1736 buffer-pattern (any-resume t))
1737 "Resurrect previously invoked `anything'.
1738 Called with a prefix arg, allow choosing among all existing
1739 anything buffers. i.e choose among various anything sessions."
1740 (interactive)
1741 (when (or current-prefix-arg buffer-pattern)
1742 (setq any-buffer (anything-resume-select-buffer buffer-pattern)))
1743 (setq anything-compiled-sources nil)
1744 (anything
1745 :sources (or (buffer-local-value 'anything-last-sources-local (get-buffer any-buffer))
1746 anything-last-sources anything-sources)
1747 :input (buffer-local-value 'anything-input-local (get-buffer any-buffer))
1748 :resume any-resume
1749 :buffer any-buffer))
1751 ;;; rubikitch: experimental
1752 ;;; I use this and check it whether I am convenient.
1753 ;;; I may introduce an option to control the behavior.
1754 (defun* anything-resume-window-only (&optional
1755 (any-buffer anything-last-buffer)
1756 buffer-pattern)
1757 (interactive)
1758 (anything-resume any-buffer buffer-pattern 'window-only))
1760 (defun anything-resume-p (any-resume)
1761 "Whether current anything session is resumed or not.
1762 Just check if ANY-RESUME value is t or window-only."
1763 (memq any-resume '(t window-only)))
1765 (defun anything-resume-select-buffer (input)
1766 "Resume precedent anything session with initial input INPUT."
1767 (or (anything :sources '(((name . "Resume anything buffer")
1768 (candidates . anything-buffers)
1769 (action . identity)))
1770 :input input
1771 :resume 'noresume
1772 :buffer "*anything resume*")
1773 (keyboard-quit)))
1776 ;;;###autoload
1777 (defun anything-at-point (&optional
1778 any-sources any-input
1779 any-prompt any-resume
1780 any-preselect any-buffer)
1781 "Call anything with symbol at point as initial input.
1782 ANY-SOURCES ANY-INPUT ANY-PROMPT ANY-RESUME ANY-PRESELECT and ANY-BUFFER
1783 are same args as in `anything'."
1784 (interactive)
1785 (anything :sources any-sources
1786 :input (if current-prefix-arg
1787 (concat "\\b" (thing-at-point 'symbol) "\\b"
1788 (if (featurep 'anything-match-plugin) " " ""))
1789 any-input)
1790 :prompt any-prompt
1791 :resume any-resume
1792 :preselect any-preselect
1793 :buffer any-buffer))
1795 ;;;###autoload
1796 (defun anything-other-buffer (any-sources any-buffer)
1797 "Simplified interface of `anything' with other `anything-buffer'.
1798 Call `anything' with only ANY-SOURCES and ANY-BUFFER as args."
1799 (anything :sources any-sources :buffer any-buffer))
1802 ;;; Initialize
1805 (defvar anything-buffers nil
1806 "All of `anything-buffer' in most recently used order.")
1807 (defun anything-initialize (any-resume any-input any-sources)
1808 "Start initialization of `anything' session.
1809 For ANY-RESUME ANY-INPUT and ANY-SOURCES See `anything'."
1810 (anything-log "start initialization: any-resume=%S any-input=%S" any-resume any-input)
1811 (anything-frame-or-window-configuration 'save)
1812 (setq anything-sources (anything-normalize-sources any-sources))
1813 (anything-log "sources = %S" anything-sources)
1814 (anything-hooks 'setup)
1815 (anything-current-position 'save)
1816 (if (anything-resume-p any-resume)
1817 (anything-initialize-overlays (anything-buffer-get))
1818 (anything-initial-setup))
1819 (unless (eq any-resume 'noresume)
1820 (anything-recent-push anything-buffer 'anything-buffers)
1821 (setq anything-last-buffer anything-buffer))
1822 (when any-input (setq anything-input any-input anything-pattern any-input))
1823 (and (anything-resume-p any-resume) (anything-funcall-foreach 'resume))
1824 (anything-log "end initialization"))
1826 ;; Here defun* allow using implicit block `anything-execute-selection-action-1'.
1827 (defun* anything-execute-selection-action-1 ()
1828 "Execute current action.
1829 Push current input to HISTORY if present, otherwise
1830 `minibuffer-history' will be used instead."
1831 (anything-log-run-hook 'anything-before-action-hook)
1832 (unwind-protect
1833 (anything-execute-selection-action)
1834 (anything-aif (get-buffer anything-action-buffer)
1835 (kill-buffer it))
1836 (anything-log-run-hook 'anything-after-action-hook)))
1838 (defun anything-restore-position-on-quit ()
1839 "Restore position in `anything-current-buffer' when quitting."
1840 (anything-current-position 'restore))
1842 (defun anything-recent-push (elt list-var)
1843 "Add ELT to the value of LIST-VAR as most recently used value."
1844 (let ((m (member elt (symbol-value list-var))))
1845 (and m (set list-var (delq (car m) (symbol-value list-var))))
1846 (push elt (symbol-value list-var))))
1849 ;;; (@* "Core: Accessors")
1850 ;;; rubikitch: I love to create functions to control variables.
1851 (defvar anything-current-position nil
1852 "Cons of \(point . window-start\) when `anything' is invoked.
1853 It is needed to restore position in `anything-current-buffer'
1854 when `anything' is keyboard-quitted.")
1855 (defun anything-current-position (save-or-restore)
1856 "Restore or save current position in `anything-current-buffer'.
1857 Argument SAVE-OR-RESTORE is one of save or restore."
1858 (case save-or-restore
1859 (save
1860 (anything-log "Save position at %S" (cons (point) (window-start)))
1861 (setq anything-current-position (cons (point) (window-start))))
1862 (restore
1863 (anything-log "Restore position at %S in buffer %s"
1864 anything-current-position
1865 (buffer-name (current-buffer)))
1866 (goto-char (car anything-current-position))
1867 ;; Fix this position with the NOFORCE arg of `set-window-start'
1868 ;; otherwise, if there is some other buffer than `anything-current-buffer'
1869 ;; one, position will be lost.
1870 (set-window-start (selected-window) (cdr anything-current-position) t))))
1873 ;; Internal.
1874 (defvar anything-last-frame-or-window-configuration nil
1875 "Used to store window or frame configuration when anything start.")
1876 (defun anything-frame-or-window-configuration (save-or-restore)
1877 "Save or restore last frame or window configuration.
1878 Possible value of SAVE-OR-RESTORE are 'save and 'restore.
1879 window or frame configuration is saved/restored according to values of
1880 `anything-save-configuration-functions'."
1881 (anything-log-eval anything-save-configuration-functions)
1882 (case save-or-restore
1883 (save (setq anything-last-frame-or-window-configuration
1884 (funcall (cdr anything-save-configuration-functions))))
1885 (restore (funcall (car anything-save-configuration-functions)
1886 anything-last-frame-or-window-configuration)
1887 ;; Restore frame focus.
1888 (let ((frame (and (listp anything-last-frame-or-window-configuration)
1889 (caadr anything-last-frame-or-window-configuration))))
1890 ;; If `anything-save-configuration-functions' are window functions
1891 ;; frame should be nil, use current frame.
1892 (unless (framep frame)
1893 (setq frame (selected-frame)))
1894 (select-frame-set-input-focus frame)))))
1897 ;; (@* "Core: Display *anything* buffer")
1898 (defun anything-display-buffer (buf)
1899 "Display *anything* buffer BUF."
1900 (let (pop-up-frames)
1901 (funcall (with-current-buffer buf anything-display-function) buf)))
1903 (defun anything-default-display-buffer (buf)
1904 "Default function to display BUF.
1905 Where BUF is generally `anything-buffer'.
1906 It use `switch-to-buffer' or `pop-to-buffer' depending of value of
1907 `anything-samewindow'."
1908 (funcall (if anything-samewindow 'switch-to-buffer 'pop-to-buffer) buf))
1911 ;; (@* "Core: initialize")
1912 (defun anything-initial-setup ()
1913 "Initialize anything settings and set up the anything buffer."
1914 (anything-log-run-hook 'anything-before-initialize-hook)
1915 (setq anything-current-prefix-arg nil)
1916 (setq anything-once-called-functions nil)
1917 (setq anything-delayed-init-executed nil)
1918 (setq anything-current-buffer
1919 (if (minibuffer-window-active-p (minibuffer-window))
1920 ;; If minibuffer is active be sure to use it's buffer
1921 ;; as `anything-current-buffer'.
1922 (window-buffer (active-minibuffer-window))
1923 (current-buffer)))
1924 (setq anything-buffer-file-name buffer-file-name)
1925 (setq anything-issued-errors nil)
1926 (setq anything-compiled-sources nil)
1927 (setq anything-saved-current-source nil)
1928 (if (or (not split-width-threshold)
1929 (and (integerp split-width-threshold)
1930 (>= split-width-threshold (+ (frame-width) 4))))
1931 (setq anything-split-window-state 'vertical)
1932 (setq anything-split-window-state 'horizontal))
1933 ;; Call the init function for sources where appropriate
1934 (anything-funcall-foreach 'init)
1935 (setq anything-pattern "")
1936 (setq anything-input "")
1937 (setq anything-candidate-cache nil)
1938 (setq anything-last-sources anything-sources)
1939 (anything-create-anything-buffer)
1940 (anything-log-run-hook 'anything-after-initialize-hook))
1942 (defvar anything-reading-pattern nil
1943 "Whether in `read-string' in anything or not.")
1944 (defun anything-read-pattern-maybe (any-prompt any-input
1945 any-preselect any-resume any-keymap
1946 any-default any-history)
1947 "Read pattern with prompt ANY-PROMPT and initial input ANY-INPUT.
1948 For ANY-PRESELECT ANY-RESUME ANY-KEYMAP, See `anything'."
1949 (if (anything-resume-p any-resume)
1950 (anything-mark-current-line)
1951 (anything-update any-preselect))
1952 (with-current-buffer (anything-buffer-get)
1953 (and any-keymap (set (make-local-variable 'anything-map) any-keymap))
1954 (let ((minibuffer-local-map
1955 anything-map))
1956 (anything-log-eval (anything-approximate-candidate-number)
1957 anything-execute-action-at-once-if-one
1958 anything-quit-if-no-candidate)
1959 (cond ((and anything-execute-action-at-once-if-one
1960 (= (anything-approximate-candidate-number) 1))
1961 (ignore))
1962 ((and anything-quit-if-no-candidate
1963 (= (anything-approximate-candidate-number) 0))
1964 (setq anything-quit t)
1965 (and (functionp anything-quit-if-no-candidate)
1966 (funcall anything-quit-if-no-candidate)))
1968 (let ((anything-reading-pattern t)
1969 (tap (or any-default
1970 (with-anything-current-buffer
1971 (thing-at-point 'symbol)))))
1972 (read-string (or any-prompt "pattern: ") any-input any-history tap)))))))
1974 (defun anything-create-anything-buffer (&optional test-mode)
1975 "Create newly created `anything-buffer'.
1976 If TEST-MODE is non-nil, clear `anything-candidate-cache'."
1977 (when test-mode
1978 (setq anything-candidate-cache nil))
1979 (with-current-buffer (get-buffer-create anything-buffer)
1980 (anything-log "kill local variables: %S" (buffer-local-variables))
1981 (kill-all-local-variables)
1982 (set (make-local-variable 'inhibit-read-only) t)
1983 (buffer-disable-undo)
1984 (erase-buffer)
1985 (set (make-local-variable 'inhibit-read-only) t)
1986 (set (make-local-variable 'anything-last-sources-local) anything-sources)
1987 (set (make-local-variable 'anything-follow-mode) nil)
1988 (set (make-local-variable 'anything-display-function) anything-display-function)
1989 (anything-initialize-persistent-action)
1990 (anything-log-eval anything-display-function anything-let-variables)
1991 (loop for (var . val) in anything-let-variables
1992 do (set (make-local-variable var) val))
1993 (setq cursor-type nil)
1994 (setq mode-name "Anything"))
1995 (anything-initialize-overlays anything-buffer)
1996 (get-buffer anything-buffer))
1998 (defun anything-initialize-overlays (buffer)
1999 "Initialize anything overlays in BUFFER."
2000 (anything-log "overlay setup")
2001 (if anything-selection-overlay
2002 ;; make sure the overlay belongs to the anything buffer if
2003 ;; it's newly created
2004 (move-overlay anything-selection-overlay (point-min) (point-min)
2005 (get-buffer buffer))
2007 (setq anything-selection-overlay
2008 (make-overlay (point-min) (point-min) (get-buffer buffer)))
2009 (overlay-put anything-selection-overlay 'face anything-selection-face))
2011 (cond (anything-enable-shortcuts
2012 (setq anything-shortcut-keys
2013 (assoc-default anything-enable-shortcuts anything-shortcut-keys-alist))
2014 (unless anything-digit-overlays
2015 (setq anything-digit-overlays
2016 (loop for key across anything-shortcut-keys
2017 for overlay = (make-overlay (point-min) (point-min)
2018 (get-buffer buffer))
2019 do (overlay-put overlay 'before-string
2020 (format "%s - " (upcase (make-string 1 key))))
2021 collect overlay))))
2022 (anything-digit-overlays
2023 (mapc 'delete-overlay anything-digit-overlays)
2024 (setq anything-digit-overlays nil))))
2026 (defun anything-hooks (setup-or-cleanup)
2027 "Add or remove hooks according to SETUP-OR-CLEANUP value.
2028 if SETUP-OR-CLEANUP value is setup add hooks, any other value
2029 will remove hooks.
2030 hooks concerned are `post-command-hook' and `minibuffer-setup-hook'."
2031 (let ((hooks '((post-command-hook anything-check-minibuffer-input)
2032 (minibuffer-setup-hook anything-print-error-messages))))
2033 (if (eq setup-or-cleanup 'setup)
2034 (dolist (args hooks) (apply 'add-hook args))
2035 (dolist (args (reverse hooks)) (apply 'remove-hook args)))))
2038 ;; (@* "Core: clean up")
2039 ;;; TODO move
2040 (defun anything-cleanup ()
2041 "Clean up the mess when anything exit or quit."
2042 (anything-log "start cleanup")
2043 (with-current-buffer anything-buffer
2044 (setq cursor-type t)
2045 ;; Call burry-buffer whithout arg
2046 ;; to remove anything-buffer from window.
2047 (bury-buffer)
2048 ;; Be sure we call this from anything-buffer.
2049 (anything-funcall-foreach 'cleanup))
2050 (anything-new-timer 'anything-check-minibuffer-input-timer nil)
2051 (anything-kill-async-processes)
2052 (anything-log-run-hook 'anything-cleanup-hook)
2053 (anything-hooks 'cleanup)
2054 (anything-frame-or-window-configuration 'restore)
2055 ;; This is needed in some cases where last input
2056 ;; is yielded infinitely in minibuffer after anything session.
2057 (anything-clean-up-minibuffer))
2059 (defun anything-clean-up-minibuffer ()
2060 "Remove contents of minibuffer."
2061 (let ((miniwin (minibuffer-window)))
2062 ;; Clean only current minibuffer used by anything.
2063 ;; i.e The precedent one is active.
2064 (unless (minibuffer-window-active-p miniwin)
2065 (with-current-buffer (window-buffer miniwin)
2066 (delete-minibuffer-contents)))))
2069 ;; (@* "Core: input handling")
2070 (defun anything-check-minibuffer-input ()
2071 "Extract input string from the minibuffer and check if it needs to be handled."
2072 (let ((delay (with-current-buffer anything-buffer
2073 (and anything-input-idle-delay
2074 (max anything-input-idle-delay 0.1)))))
2075 (if (or (not delay) (anything-action-window))
2076 (anything-check-minibuffer-input-1)
2077 (anything-new-timer
2078 'anything-check-minibuffer-input-timer
2079 (run-with-idle-timer delay nil 'anything-check-minibuffer-input-1)))))
2081 (defun anything-check-minibuffer-input-1 ()
2082 "Check minibuffer content."
2083 (with-anything-quittable
2084 (with-selected-window (or (active-minibuffer-window) (minibuffer-window))
2085 (anything-check-new-input (minibuffer-contents)))))
2087 (defun anything-check-new-input (input)
2088 "Check INPUT string and update the anything buffer if necessary."
2089 (unless (equal input anything-pattern)
2090 (setq anything-pattern input)
2091 (unless (anything-action-window)
2092 (setq anything-input anything-pattern))
2093 (anything-log-eval anything-pattern anything-input)
2094 (anything-update)))
2097 ;; (@* "Core: source compiler")
2098 (defvar anything-compile-source-functions-default anything-compile-source-functions
2099 "Plug-ins this file provides.")
2100 (defun anything-compile-sources (sources funcs)
2101 "Compile SOURCES with FUNCS.
2102 See `anything-compile-source-functions'.
2103 Anything plug-ins are realized by this function."
2104 (mapcar
2105 (lambda (source)
2106 (loop with source = (if (listp source) source (symbol-value source))
2107 for f in funcs
2108 do (setq source (funcall f source))
2109 finally (return source)))
2110 sources))
2113 ;; (@* "Core: plug-in attribute documentation hack")
2115 ;; `anything-document-attribute' is public API.
2116 (defadvice documentation-property (after anything-document-attribute activate)
2117 "Display plug-in attributes' documentation as `anything-sources' docstring."
2118 (when (eq (ad-get-arg 0) 'anything-sources)
2119 (setq ad-return-value
2120 (concat ad-return-value "\n"
2121 (mapconcat (lambda (sym) (get sym 'anything-attrdoc))
2122 anything-additional-attributes
2123 "\n")))))
2124 ;; (describe-variable 'anything-sources)
2125 ;; (documentation-property 'anything-sources 'variable-documentation)
2126 ;; (progn (ad-disable-advice 'documentation-property 'after 'anything-document-attribute) (ad-update 'documentation-property))
2129 ;; (@* "Core: all candidates")
2130 (defun anything-process-delayed-init (source)
2131 "Initialize delayed SOURCE."
2132 (let ((name (assoc-default 'name source)))
2133 (unless (member name anything-delayed-init-executed)
2134 (anything-aif (assoc-default 'delayed-init source)
2135 (with-current-buffer anything-current-buffer
2136 (anything-funcall-with-source source it)
2137 (dolist (f (if (functionp it) (list it) it))
2138 (add-to-list 'anything-delayed-init-executed name)))))))
2140 (defun anything-get-candidates (source)
2141 "Retrieve and return the list of candidates from SOURCE."
2142 (anything-process-delayed-init source)
2143 (let* ((candidate-source (assoc-default 'candidates source))
2144 (type-error (lambda ()
2145 (error (concat "Candidates must either be a function, "
2146 " a variable or a list: %s")
2147 candidate-source)))
2148 (candidates (condition-case err
2149 (anything-interpret-value candidate-source source)
2150 (error (funcall type-error)))))
2151 (cond ((processp candidates) candidates)
2152 ((listp candidates) (anything-transform-candidates candidates source))
2153 (t (funcall type-error)))))
2156 (defun anything-get-cached-candidates (source)
2157 "Return the cached value of candidates for SOURCE.
2158 Cache the candidates if there is not yet a cached value."
2159 (let* ((name (assoc-default 'name source))
2160 (candidate-cache (assoc name anything-candidate-cache)))
2161 (cond (candidate-cache
2162 (anything-log "use cached candidates")
2163 (cdr candidate-cache))
2165 (anything-log "calculate candidates")
2166 (let ((candidates (anything-get-candidates source)))
2167 (cond ((processp candidates)
2168 (push (cons candidates
2169 (append source
2170 (list (cons 'item-count 0)
2171 (cons 'incomplete-line ""))))
2172 anything-async-processes)
2173 (set-process-filter candidates 'anything-output-filter)
2174 (setq candidates nil))
2175 ((not (assoc 'volatile source))
2176 (setq candidate-cache (cons name candidates))
2177 (push candidate-cache anything-candidate-cache)))
2178 candidates)))))
2181 ;;; (@* "Core: candidate transformers")
2182 (defun anything-process-candidate-transformer (candidates source)
2183 "Execute candidate-transformer function on all CANDIDATES of SOURCE."
2184 (anything-aif (assoc-default 'candidate-transformer source)
2185 (anything-composed-funcall-with-source source it candidates)
2186 candidates))
2188 (defun anything-process-filtered-candidate-transformer (candidates source)
2189 "Execute filtered-candidate-transformer function on all CANDIDATES of SOURCE."
2190 (anything-aif (assoc-default 'filtered-candidate-transformer source)
2191 (anything-composed-funcall-with-source source it candidates source)
2192 candidates))
2194 (defun anything-process-filtered-candidate-transformer-maybe (candidates source process-p)
2195 "Execute filtered-candidate-transformer function on all CANDIDATES of SOURCE.
2196 This happen if PROCESS-P is non-nil."
2197 (if process-p
2198 (anything-process-filtered-candidate-transformer candidates source)
2199 candidates))
2201 (defun anything-process-real-to-display (candidates source)
2202 "Execute real-to-display function on all CANDIDATES of SOURCE."
2203 (anything-aif (assoc-default 'real-to-display source)
2204 (setq candidates (anything-funcall-with-source
2205 source 'mapcar
2206 (lambda (cand_)
2207 (if (consp cand_)
2208 ;; override DISPLAY from candidate-transformer
2209 (cons (funcall it (cdr cand_)) (cdr cand_))
2210 (cons (funcall it cand_) cand_)))
2211 candidates))
2212 candidates))
2214 (defun anything-transform-candidates (candidates source &optional process-p)
2215 "Transform CANDIDATES of SOURCE according to candidate transformers.
2216 This happen if PROCESS-P is non-nil."
2217 (anything-process-real-to-display
2218 (anything-process-filtered-candidate-transformer-maybe
2219 (anything-process-candidate-transformer candidates source) source process-p)
2220 source))
2223 ;; (@* "Core: narrowing candidates")
2224 (defun anything-candidate-number-limit (source)
2225 "Apply candidate-number-limit attribute value.
2226 This overhide variable `anything-candidate-number-limit'.
2228 e.g:
2229 If \(candidate-number-limit\) is in SOURCE, show all candidates in SOURCE.
2230 If \(candidate-number-limit . 123\) is in SOURCE limit candidate to 123."
2231 (anything-aif (assq 'candidate-number-limit source)
2232 (or (cdr it) 99999999)
2233 (or anything-candidate-number-limit 99999999)))
2235 ;; FIXME: Why a defconst here
2236 (defconst anything-default-match-functions
2237 (list (lambda (candidate)
2238 (string-match anything-pattern candidate)))
2239 "Default functions to match candidates according to `anything-pattern'.")
2241 (defun anything-compute-matches (source)
2242 "Compute matched results from SOURCE according to its settings."
2243 (if debug-on-error
2244 (anything-compute-matches-internal source)
2245 (condition-case v
2246 (anything-compute-matches-internal source)
2247 (error (anything-log-error
2248 "anything-compute-matches: error when processing source: %s"
2249 (assoc-default 'name source))
2250 nil))))
2252 (defun anything-candidate-get-display (candidate)
2253 "Get searched display part from CANDIDATE.
2254 CANDIDATE is a string, a symbol, or \(DISPLAY . REAL\) cons cell."
2255 (format "%s" (or (car-safe candidate) candidate)))
2257 (defun anything-process-pattern-transformer (pattern source)
2258 "Execute pattern-transformer attribute PATTERN function in SOURCE."
2259 (anything-aif (assoc-default 'pattern-transformer source)
2260 (anything-composed-funcall-with-source source it pattern)
2261 pattern))
2263 (defun anything-match-functions (source)
2264 (or (assoc-default 'match source)
2265 anything-default-match-functions))
2267 (defmacro anything-accumulate-candidates-internal (cand newmatches
2268 hash item-count limit)
2269 "Internal, add CAND into NEWMATCHES.
2270 Use HASH to uniq NEWMATCHES.
2271 Argument ITEM-COUNT count the matches.
2272 if ITEM-COUNT reaches LIMIT, exit from inner loop."
2273 `(unless (gethash ,cand ,hash)
2274 (puthash ,cand t ,hash)
2275 (push ,cand ,newmatches)
2276 (incf ,item-count)
2277 (when (= ,item-count ,limit)
2278 (setq exit t)
2279 (return))))
2281 (defun anything-take-first-elements (seq n)
2282 (if (> (length seq) n)
2283 (setq seq (subseq seq 0 n))
2284 seq))
2286 (defun anything-match-from-candidates (cands matchfns limit)
2287 (let (matches)
2288 (condition-case nil
2289 (let ((item-count 0) exit)
2290 (clrhash anything-match-hash)
2291 (dolist (match matchfns)
2292 (let (newmatches)
2293 (dolist (candidate cands)
2294 (when (funcall match (anything-candidate-get-display candidate))
2295 (anything-accumulate-candidates-internal
2296 candidate newmatches anything-match-hash item-count limit)))
2297 (setq matches (append matches (reverse newmatches)))
2298 (if exit (return)))))
2299 (invalid-regexp (setq matches nil)))
2300 matches))
2302 (defun anything-compute-matches-internal (source)
2303 (save-current-buffer
2304 (let ((matchfns (anything-match-functions source))
2305 (anything-source-name (assoc-default 'name source))
2306 (limit (anything-candidate-number-limit source))
2307 (anything-pattern (anything-process-pattern-transformer
2308 anything-pattern source)))
2309 (anything-process-filtered-candidate-transformer
2310 (if (or (equal anything-pattern "") (equal matchfns '(identity)))
2311 (anything-take-first-elements
2312 (anything-get-cached-candidates source) limit)
2313 (anything-match-from-candidates
2314 (anything-get-cached-candidates source) matchfns limit))
2315 source))))
2317 ;; (anything '(((name . "error")(candidates . (lambda () (hage))) (action . identity))))
2319 (defun anything-process-source (source)
2320 "Display matched results from SOURCE according to its settings."
2321 (anything-log-eval (assoc-default 'name source))
2322 (if (assq 'direct-insert-match source) ;experimental
2323 (anything-process-source--direct-insert-match source)
2324 (let ((matches (anything-compute-matches source)))
2325 (when matches
2326 (when anything-test-mode
2327 (setq anything-test-candidate-list
2328 `(,@anything-test-candidate-list
2329 (,(assoc-default 'name source)
2330 ,matches))))
2331 (anything-insert-header-from-source source)
2332 (if (not (assq 'multiline source))
2333 (mapc 'anything-insert-match-with-digit-overlay matches)
2334 (let ((start (point)) separate)
2335 (dolist (match matches)
2336 (if separate
2337 (anything-insert-candidate-separator)
2338 (setq separate t))
2339 (anything-insert-match-with-digit-overlay match))
2340 (put-text-property start (point) 'anything-multiline t)))))))
2342 (defun anything-insert-match-with-digit-overlay (match)
2343 (declare (special source))
2344 (anything-put-digit-overlay-maybe)
2345 (anything-insert-match match 'insert source))
2347 (defun anything-put-digit-overlay-maybe ()
2348 (when (and anything-enable-shortcuts
2349 (not (eq anything-digit-shortcut-count
2350 (length anything-digit-overlays))))
2351 (move-overlay (nth anything-digit-shortcut-count
2352 anything-digit-overlays)
2353 (point-at-bol)
2354 (point-at-bol))
2355 (incf anything-digit-shortcut-count)))
2357 (defun anything-process-source--direct-insert-match (source)
2358 "[EXPERIMENTAL] Insert candidates from `anything-candidate-buffer' in SOURCE."
2359 (anything-log-eval (assoc-default 'name source))
2360 (let ((anything-source-name (assoc-default 'name source))
2361 content-buf)
2362 (funcall (assoc-default 'candidates source))
2363 (setq content-buf (anything-candidate-buffer))
2364 (unless (anything-empty-buffer-p content-buf)
2365 (anything-insert-header-from-source source)
2366 (insert-buffer-substring content-buf)
2367 ;; TODO call anything-put-digit-overlay-maybe with loop
2370 (defun anything-process-delayed-sources (delayed-sources &optional preselect)
2371 "Process anything DELAYED-SOURCES.
2372 Move selection to string or regexp PRESELECT if non--nil.
2373 This function is called in `anything-process-delayed-sources-timer'
2374 when emacs is idle for `anything-idle-delay'."
2375 (with-anything-quittable
2376 (anything-log-eval (mapcar (lambda (s) (assoc-default 'name s)) delayed-sources))
2377 (with-current-buffer anything-buffer
2378 (save-excursion
2379 (goto-char (point-max))
2380 (mapc 'anything-process-source delayed-sources)
2381 (when (and (not (anything-empty-buffer-p))
2382 ;; No selection yet.
2383 (= (overlay-start anything-selection-overlay)
2384 (overlay-end anything-selection-overlay)))
2385 (anything-update-move-first-line 'without-hook)))
2386 (when preselect (anything-preselect preselect))
2387 (save-excursion
2388 (goto-char (point-min))
2389 (anything-log-run-hook 'anything-update-hook))
2390 (anything-log-run-hook 'anything-after-update-hook))))
2393 ;; (@* "Core: *anything* buffer contents")
2394 (defvar anything-input-local nil)
2395 (defvar anything-process-delayed-sources-timer nil)
2396 (defun anything-update (&optional preselect)
2397 "Update candidates list in `anything-buffer' according to `anything-pattern'.
2398 Argument PRESELECT is a string or regexp used to move selection to a particular
2399 place once updating is done. It should be used on single source because search
2400 is done on whole `anything-buffer' and not on current source."
2401 (anything-log "start update")
2402 (setq anything-digit-shortcut-count 0)
2403 (anything-kill-async-processes)
2404 (with-current-buffer (anything-buffer-get)
2405 (set (make-local-variable 'anything-input-local) anything-pattern)
2406 (erase-buffer)
2407 (when anything-enable-shortcuts
2408 (mapc 'delete-overlay anything-digit-overlays))
2409 (let (delayed-sources
2410 normal-sources)
2411 (unwind-protect ; Process normal sources and store delayed one's.
2412 (setq delayed-sources
2413 (loop for source in (remove-if-not 'anything-update-source-p
2414 (anything-get-sources))
2415 if (anything-delayed-source-p source)
2416 collect source
2417 else do (progn (push source normal-sources)
2418 (anything-process-source source))))
2419 (anything-log-eval
2420 (mapcar (lambda (s) (assoc-default 'name s)) delayed-sources))
2421 (if anything-test-mode ; Need only to process sources.
2422 (mapc 'anything-process-source delayed-sources)
2423 (cond ((and preselect delayed-sources normal-sources)
2424 ;; Preselection run here when there is
2425 ;; normal AND delayed sources.
2426 (anything-log "Update preselect candidate %s" preselect)
2427 (anything-preselect preselect))
2428 (delayed-sources ; Preselection and hooks will run later.
2429 (anything-update-move-first-line 'without-hook))
2430 (t ; No delayed sources, run the hooks now.
2431 (anything-update-move-first-line)
2432 (anything-log-run-hook 'anything-after-update-hook)
2433 (when preselect
2434 (anything-log "Update preselect candidate %s" preselect)
2435 (anything-preselect preselect))))
2436 (when delayed-sources
2437 (anything-new-timer
2438 'anything-process-delayed-sources-timer
2439 (run-with-idle-timer
2440 ;; Be sure anything-idle-delay is >
2441 ;; to anything-input-idle-delay
2442 ;; otherwise use value of anything-input-idle-delay
2443 ;; or 0.1 if == to 0.
2444 (max anything-idle-delay anything-input-idle-delay 0.1) nil
2445 'anything-process-delayed-sources delayed-sources preselect))))
2446 (anything-log "end update")))))
2448 (defun anything-update-source-p (source)
2449 "Wheter SOURCE need updating or not."
2450 (and (or (not anything-source-filter)
2451 (member (assoc-default 'name source) anything-source-filter))
2452 (>= (length anything-pattern)
2453 (anything-aif (assoc 'requires-pattern source)
2454 (or (cdr it) 1)
2455 0))))
2457 (defun anything-delayed-source-p (source)
2458 "Wheter SOURCE is a delayed source or not."
2459 (or (assoc 'delayed source)
2460 (and anything-quick-update
2461 (< (window-height (get-buffer-window (current-buffer)))
2462 (line-number-at-pos (point-max))))))
2464 (defun anything-update-move-first-line (&optional without-hook)
2465 "Goto first line of `anything-buffer'."
2466 (goto-char (point-min))
2467 (unless without-hook
2468 (save-excursion (anything-log-run-hook 'anything-update-hook)))
2469 (anything-next-line))
2471 (defun anything-force-update (&optional preselect)
2472 "Force recalculation and update of candidates.
2473 If current source has `update' attribute, a function without argument,
2474 call it before update."
2475 (interactive)
2476 (let ((source (anything-get-current-source))
2477 (selection (anything-get-selection nil t)))
2478 (when source
2479 (mapc 'anything-force-update--reinit
2480 (anything-get-sources)))
2481 (anything-update preselect)
2482 ;; If preselect arg exists, `anything-update' should
2483 ;; have moved to selection, otherwise do it now.
2484 (unless preselect
2485 (anything-keep-selection (assoc-default 'name source) selection))
2486 (with-anything-window (recenter))))
2488 (defun anything-force-update--reinit (source)
2489 "Reinit SOURCE by calling his update and/or init functions."
2490 (anything-aif (anything-funcall-with-source
2491 source 'anything-candidate-buffer)
2492 (kill-buffer it))
2493 (dolist (attr '(update init))
2494 (anything-aif (assoc-default attr source)
2495 (anything-funcall-with-source source it)))
2496 (anything-remove-candidate-cache source))
2498 (defun anything-keep-selection (source selection)
2499 "Switch to SOURCE and goto SELECTION."
2500 (when (and source selection)
2501 (with-anything-window
2502 (anything-goto-source source)
2503 (forward-char -1)
2504 (if (search-forward selection nil t)
2505 (forward-line 0)
2506 (goto-char (point-min))
2507 (forward-line 1))
2508 (anything-mark-current-line))))
2510 (defun anything-remove-candidate-cache (source)
2511 "Remove SOURCE from `anything-candidate-cache'."
2512 (setq anything-candidate-cache
2513 (delete (assoc (assoc-default 'name source)
2514 anything-candidate-cache)
2515 anything-candidate-cache)))
2517 (defun anything-insert-match (match insert-function source)
2518 "Insert MATCH into `anything-buffer' with INSERT-FUNCTION for SOURCE.
2519 If MATCH is a list then insert the string intended to appear on the display
2520 and store the real value in a text property."
2521 (let ((start (point-at-bol (point)))
2522 (string (or (car-safe match) match))
2523 (realvalue (cdr-safe match)))
2524 (when (symbolp string) (setq string (symbol-name string)))
2525 (when (stringp string)
2526 (funcall insert-function string)
2527 ;; Some sources with candidates-in-buffer have already added
2528 ;; 'anything-realvalue property when creating candidate buffer.
2529 (unless (get-text-property start 'anything-realvalue)
2530 (and realvalue
2531 (put-text-property start (point-at-eol)
2532 'anything-realvalue realvalue)))
2533 (when anything-source-in-each-line-flag
2534 (put-text-property start (point-at-eol) 'anything-source source))
2535 (funcall insert-function "\n"))))
2537 (defun anything-insert-header-from-source (source)
2538 "Insert SOURCE name in `anything-buffer' header.
2539 Maybe insert by overlay additional info after source name if SOURCE have
2540 header-name attribute."
2541 (let ((name (assoc-default 'name source)))
2542 (anything-insert-header
2543 name
2544 (anything-aif (assoc-default 'header-name source)
2545 (anything-funcall-with-source source it name)))))
2547 (defun anything-insert-header (name &optional display-string)
2548 "Insert header of source NAME into the anything buffer.
2549 If DISPLAY-STRING is non--nil and a string, display this additional info
2550 after the source name by overlay."
2551 (unless (bobp)
2552 (let ((start (point)))
2553 (insert "\n")
2554 (put-text-property start (point) 'anything-header-separator t)))
2555 (let ((start (point)))
2556 (insert name)
2557 (put-text-property (point-at-bol)
2558 (point-at-eol) 'anything-header t)
2559 (when display-string
2560 (overlay-put (make-overlay (point-at-bol) (point-at-eol))
2561 'display display-string))
2562 (insert "\n")
2563 (put-text-property start (point) 'face anything-header-face)))
2565 (defun anything-insert-candidate-separator ()
2566 "Insert separator of candidates into the anything buffer."
2567 (insert anything-candidate-separator)
2568 (put-text-property (point-at-bol)
2569 (point-at-eol) 'anything-candidate-separator t)
2570 (insert "\n"))
2573 ;; (@* "Core: async process")
2574 (defun anything-output-filter (process string)
2575 "From PROCESS process output STRING."
2576 (anything-output-filter-1 (assoc process anything-async-processes) string))
2578 (defun anything-output-filter-1 (process-assoc string)
2579 (anything-log-eval string)
2580 (with-current-buffer anything-buffer
2581 (let ((source (cdr process-assoc)))
2582 (save-excursion
2583 (anything-aif (assoc-default 'insertion-marker source)
2584 (goto-char it)
2585 (goto-char (point-max))
2586 (anything-insert-header-from-source source)
2587 (setcdr process-assoc
2588 (append source `((insertion-marker . ,(point-marker))))))
2589 (anything-output-filter--process-source
2590 (car process-assoc) string source
2591 (anything-candidate-number-limit source))))
2592 (anything-output-filter--post-process)))
2594 (defun anything-output-filter--process-source (process string source limit)
2595 (dolist (candidate (anything-transform-candidates
2596 (anything-output-filter--collect-candidates
2597 (split-string string "\n")
2598 (assoc 'incomplete-line source))
2599 source t))
2600 (if (not (assq 'multiline source))
2601 (anything-insert-match candidate 'insert-before-markers source)
2602 (let ((start (point)))
2603 (anything-insert-candidate-separator)
2604 (anything-insert-match candidate 'insert-before-markers source)
2605 (put-text-property start (point) 'anything-multiline t)))
2606 (incf (cdr (assoc 'item-count source)))
2607 (when (>= (assoc-default 'item-count source) limit)
2608 (anything-kill-async-process process)
2609 (return))))
2611 (defun anything-output-filter--collect-candidates (lines incomplete-line-info)
2612 (anything-log-eval (cdr incomplete-line-info))
2613 (butlast
2614 (loop for line in lines collect
2615 (if (cdr incomplete-line-info)
2616 (prog1
2617 (concat (cdr incomplete-line-info) line)
2618 (setcdr incomplete-line-info nil))
2619 line)
2620 finally (setcdr incomplete-line-info line))))
2622 (defun anything-output-filter--post-process ()
2623 (anything-log-run-hook 'anything-update-hook)
2624 (anything-aif (get-buffer-window anything-buffer 'visible)
2625 (save-selected-window
2626 (select-window it)
2627 (anything-skip-noncandidate-line 'next)
2628 (anything-mark-current-line))))
2630 (defun anything-kill-async-processes ()
2631 "Kill all known asynchronous processes of `anything-async-processes'."
2632 (mapc 'anything-kill-async-process (mapcar 'car anything-async-processes))
2633 (setq anything-async-processes nil))
2635 (defun anything-kill-async-process (process)
2636 "Kill PROCESS and detach the associated functions."
2637 (set-process-filter process nil)
2638 (delete-process process))
2641 ;; (@* "Core: action")
2642 (defun anything-execute-selection-action (&optional
2643 selection action
2644 preserve-saved-action)
2645 "If a candidate SELECTION is present then perform the associated ACTION on it.
2646 If PRESERVE-SAVED-ACTION is non-nil don't save action."
2647 (anything-log "executing action")
2648 (setq action (anything-get-default-action
2649 (or action
2650 anything-saved-action
2651 (if (get-buffer anything-action-buffer)
2652 (anything-get-selection anything-action-buffer)
2653 (anything-get-action)))))
2654 (let ((source (or anything-saved-current-source
2655 (anything-get-current-source))))
2656 (setq selection (or selection
2657 (anything-get-selection)
2658 (and (assoc 'accept-empty source) "")))
2659 (unless preserve-saved-action (setq anything-saved-action nil))
2660 (if (and selection action)
2661 (anything-funcall-with-source
2662 source action
2663 (anything-coerce-selection selection source)))))
2665 (defun anything-coerce-selection (selection source)
2666 "Apply coerce attribute function to SELECTION in SOURCE.
2667 Coerce source with coerce function."
2668 (anything-aif (assoc-default 'coerce source)
2669 (anything-funcall-with-source source it selection)
2670 selection))
2672 (defun anything-get-default-action (action)
2673 "Get the first ACTION value of action list in source."
2674 (if (and (listp action) (not (functionp action)))
2675 (cdar action)
2676 action))
2678 (defun anything-select-action ()
2679 "Select an action for the currently selected candidate.
2680 If action buffer is selected, back to the anything buffer."
2681 (interactive)
2682 (cond ((get-buffer-window anything-action-buffer 'visible)
2683 (set-window-buffer (get-buffer-window anything-action-buffer)
2684 anything-buffer)
2685 (kill-buffer anything-action-buffer)
2686 (anything-set-pattern anything-input 'noupdate))
2688 (setq anything-saved-selection (anything-get-selection))
2689 (unless anything-saved-selection
2690 (error "Nothing is selected"))
2691 (setq anything-saved-current-source (anything-get-current-source))
2692 (let ((actions (anything-get-action)))
2693 (if (functionp actions)
2694 (message "Sole action: %s" actions)
2695 (anything-show-action-buffer actions)
2696 (anything-delete-minibuffer-contents)
2697 (setq anything-pattern 'dummy) ; so that it differs from the previous one
2698 (anything-check-minibuffer-input))))))
2700 (defun anything-show-action-buffer (actions)
2701 (with-current-buffer (get-buffer-create anything-action-buffer)
2702 (erase-buffer)
2703 (buffer-disable-undo)
2704 (set-window-buffer (get-buffer-window anything-buffer) anything-action-buffer)
2705 (set (make-local-variable 'anything-sources)
2706 `(((name . "Actions")
2707 (volatile)
2708 (candidates . ,actions)
2709 (candidate-number-limit))))
2710 (set (make-local-variable 'anything-source-filter) nil)
2711 (set (make-local-variable 'anything-selection-overlay) nil)
2712 (set (make-local-variable 'anything-digit-overlays) nil)
2713 (anything-initialize-overlays anything-action-buffer)))
2716 ;; (@* "Core: selection")
2717 (defun anything-move-selection-common (move-func unit direction)
2718 "Move the selection marker to a new position wit function MOVE-FUNC.
2719 It is determined by UNIT and DIRECTION."
2720 (unless (or (anything-empty-buffer-p (anything-buffer-get))
2721 (not (anything-window)))
2722 (with-anything-window
2723 (anything-log-run-hook 'anything-move-selection-before-hook)
2724 (funcall move-func)
2725 (anything-skip-noncandidate-line direction)
2726 (anything-display-source-at-screen-top-maybe unit)
2727 (when (anything-get-previous-header-pos)
2728 (anything-mark-current-line))
2729 (anything-display-mode-line (anything-get-current-source))
2730 (anything-log-run-hook 'anything-move-selection-after-hook))))
2732 (defun anything-display-source-at-screen-top-maybe (unit)
2733 (when (and anything-display-source-at-screen-top (eq unit 'source))
2734 (set-window-start (selected-window)
2735 (save-excursion (forward-line -1) (point)))))
2737 (defun anything-skip-noncandidate-line (direction)
2738 (anything-skip-header-and-separator-line direction)
2739 (and (bobp) (forward-line 1)) ;skip first header
2740 (and (eobp) (forward-line -1))) ;avoid last empty line
2743 (defun anything-skip-header-and-separator-line (direction)
2744 (while (and (not (bobp))
2745 (or (anything-pos-header-line-p)
2746 (anything-pos-candidate-separator-p)))
2747 (forward-line (if (and (eq direction 'previous)
2748 (not (eq (point-at-bol) (point-min))))
2749 -1 1))))
2751 (defvar anything-mode-line-string-real nil)
2752 (defun anything-display-mode-line (source)
2753 (set (make-local-variable 'anything-mode-line-string)
2754 (anything-interpret-value (or (assoc-default 'mode-line source)
2755 (default-value 'anything-mode-line-string))
2756 source))
2757 (if anything-mode-line-string
2758 (setq mode-line-format
2759 '(" " mode-line-buffer-identification " "
2760 (line-number-mode "L%l") " " (anything-follow-mode "(F) ")
2761 (:eval (anything-show-candidate-number
2762 (when (listp anything-mode-line-string)
2763 (car anything-mode-line-string))))
2764 " " anything-mode-line-string-real "-%-")
2765 anything-mode-line-string-real
2766 (substitute-command-keys (if (listp anything-mode-line-string)
2767 (cadr anything-mode-line-string)
2768 anything-mode-line-string)))
2769 (setq mode-line-format
2770 (default-value 'mode-line-format)))
2771 (setq header-line-format
2772 (anything-interpret-value (assoc-default 'header-line source) source)))
2774 (defun anything-show-candidate-number (&optional name)
2775 "Used to display candidate number in mode-line.
2776 You can specify NAME of candidates e.g \"Buffers\" otherwise
2777 it is \"Candidate\(s\)\" by default."
2778 (propertize
2779 (format "[%s %s]"
2780 (anything-approximate-candidate-number 'in-current-source)
2781 (or name "Candidate(s)"))
2782 'face 'anything-candidate-number))
2784 (defun anything-previous-line ()
2785 "Move selection to the previous line."
2786 (interactive)
2787 (anything-move-selection-common
2788 (lambda ()
2789 (if (not (anything-pos-multiline-p))
2790 (forward-line -1) ;double forward-line is meaningful
2791 (forward-line -1) ;because evaluation order is important
2792 (anything-skip-header-and-separator-line 'previous)
2793 (let ((header-pos (anything-get-previous-header-pos))
2794 (separator-pos (anything-get-previous-candidate-separator-pos)))
2795 (when header-pos
2796 (goto-char (if (or (null separator-pos) (< separator-pos header-pos))
2797 header-pos ; first candidate
2798 separator-pos))
2799 (forward-line 1)))))
2800 'line 'previous))
2802 (defun anything-next-line ()
2803 "Move selection to the next line."
2804 (interactive)
2805 (anything-move-selection-common
2806 (lambda ()
2807 (if (not (anything-pos-multiline-p))
2808 (forward-line 1)
2809 (let ((header-pos (anything-get-next-header-pos))
2810 (separator-pos (anything-get-next-candidate-separator-pos)))
2811 (cond ((and separator-pos
2812 (or (null header-pos) (< separator-pos header-pos)))
2813 (goto-char separator-pos))
2814 (header-pos
2815 (goto-char header-pos))))))
2816 'line 'next))
2818 (defun anything-previous-page ()
2819 "Move selection back with a pageful."
2820 (interactive)
2821 (anything-move-selection-common
2822 (lambda ()
2823 (condition-case nil
2824 (scroll-down)
2825 (beginning-of-buffer (goto-char (point-min)))))
2826 'page 'previous))
2828 (defun anything-next-page ()
2829 "Move selection forward with a pageful."
2830 (interactive)
2831 (anything-move-selection-common
2832 (lambda ()
2833 (condition-case nil
2834 (scroll-up)
2835 (end-of-buffer (goto-char (point-max)))))
2836 'page 'next))
2838 (defun anything-beginning-of-buffer ()
2839 "Move selection at the top."
2840 (interactive)
2841 (anything-move-selection-common
2842 (lambda () (goto-char (point-min)))
2843 'edge 'previous))
2845 (defun anything-end-of-buffer ()
2846 "Move selection at the bottom."
2847 (interactive)
2848 (anything-move-selection-common
2849 (lambda () (goto-char (point-max)))
2850 'edge 'next))
2852 (defun anything-previous-source ()
2853 "Move selection to the previous source."
2854 (interactive)
2855 (anything-move-selection-common
2856 (lambda ()
2857 (forward-line -1)
2858 (if (bobp)
2859 (goto-char (point-max))
2860 (anything-skip-header-and-separator-line 'previous))
2861 (goto-char (anything-get-previous-header-pos))
2862 (forward-line 1))
2863 'source 'previous))
2865 (defun anything-next-source ()
2866 "Move selection to the next source."
2867 (interactive)
2868 (anything-move-selection-common
2869 (lambda ()
2870 (goto-char (or (anything-get-next-header-pos) (point-min))))
2871 'source 'next))
2873 (defun anything-goto-source (source-or-name)
2874 "Move the selection to the source SOURCE-OR-NAME."
2875 (anything-move-selection-common
2876 (lambda ()
2877 (goto-char (point-min))
2878 (let ((name (if (stringp source-or-name) source-or-name
2879 (assoc-default 'name source-or-name))))
2880 (condition-case err
2881 (while (not (string= name (anything-current-line-contents)))
2882 (goto-char (anything-get-next-header-pos)))
2883 (error (message "")))))
2884 'source 'next))
2886 (defun anything-mark-current-line ()
2887 "Move `anything-selection-overlay' to current line.
2888 Note that this is not related with visibles marks, which are used
2889 to mark candidates."
2890 (move-overlay
2891 anything-selection-overlay (point-at-bol)
2892 (if (anything-pos-multiline-p)
2893 (let ((header-pos (anything-get-next-header-pos))
2894 (separator-pos (anything-get-next-candidate-separator-pos)))
2895 (or (and (null header-pos) separator-pos)
2896 (and header-pos separator-pos (< separator-pos header-pos)
2897 separator-pos)
2898 header-pos
2899 (point-max)))
2900 (1+ (point-at-eol))))
2901 (anything-follow-execute-persistent-action-maybe))
2903 (defun anything-this-command-key ()
2904 (event-basic-type (elt (this-command-keys-vector) 0)))
2905 ;; (progn (read-key-sequence "Key: ") (p (anything-this-command-key)))
2907 (defun anything-select-with-shortcut-internal (types get-key-func)
2908 (if (memq anything-enable-shortcuts types)
2909 (save-selected-window
2910 (select-window (anything-window))
2911 (let* ((key (funcall get-key-func))
2912 (overlay (ignore-errors (nth (position key anything-shortcut-keys)
2913 anything-digit-overlays))))
2914 (if (not (and overlay (overlay-buffer overlay)))
2915 (when (numberp key)
2916 (select-window (minibuffer-window))
2917 (self-insert-command 1))
2918 (goto-char (overlay-start overlay))
2919 (anything-mark-current-line)
2920 (anything-exit-minibuffer))))
2921 (self-insert-command 1)))
2923 (defun anything-select-with-prefix-shortcut ()
2924 "Invoke default action with prefix shortcut."
2925 (interactive)
2926 (anything-select-with-shortcut-internal
2927 '(prefix)
2928 (lambda () (read-event "Select shortcut key: "))))
2930 (defun anything-select-with-digit-shortcut ()
2931 "Invoke default action with digit/alphabet shortcut."
2932 (interactive)
2933 (anything-select-with-shortcut-internal
2934 '(alphabet t) 'anything-this-command-key))
2936 ;; (setq anything-enable-shortcuts 'prefix)
2937 ;; (define-key anything-map "@" 'anything-select-with-prefix-shortcut)
2938 ;; (define-key anything-map (kbd "<f18>") 'anything-select-with-prefix-shortcut)
2940 (defvar anything-exit-status 0
2941 "Flag to inform whether anything have aborted or quitted.
2942 Exit with 0 mean anything exit executing an action.
2943 Exit with 1 mean anything abort with \\[keyboard-quit]
2944 It is useful for example to restore a window config if anything abort
2945 in special cases.
2946 See `anything-exit-minibuffer' and `anything-keyboard-quit'.")
2948 (defun anything-exit-minibuffer ()
2949 "Select the current candidate by exiting the minibuffer."
2950 (interactive)
2951 (unless anything-current-prefix-arg
2952 (setq anything-current-prefix-arg current-prefix-arg))
2953 (setq anything-exit-status 0)
2954 (exit-minibuffer))
2956 (defun anything-keyboard-quit ()
2957 "Quit minibuffer in anything.
2958 If action buffer is displayed, kill it."
2959 (interactive)
2960 (when (get-buffer-window anything-action-buffer 'visible)
2961 (kill-buffer anything-action-buffer))
2962 (setq anything-exit-status 1)
2963 (abort-recursive-edit))
2965 (defun anything-get-next-header-pos ()
2966 "Return the position of the next header from point."
2967 (next-single-property-change (point) 'anything-header))
2969 (defun anything-get-previous-header-pos ()
2970 "Return the position of the previous header from point."
2971 (previous-single-property-change (point) 'anything-header))
2973 (defun anything-pos-multiline-p ()
2974 "Return non-nil if the current position is in the multiline source region."
2975 (get-text-property (point) 'anything-multiline))
2977 (defun anything-get-next-candidate-separator-pos ()
2978 "Return the position of the next candidate separator from point."
2979 (next-single-property-change (point) 'anything-candidate-separator))
2981 (defun anything-get-previous-candidate-separator-pos ()
2982 "Return the position of the previous candidate separator from point."
2983 (previous-single-property-change (point) 'anything-candidate-separator))
2985 (defun anything-pos-header-line-p ()
2986 "Return t if the current line is a header line."
2987 (or (get-text-property (point-at-bol) 'anything-header)
2988 (get-text-property (point-at-bol) 'anything-header-separator)))
2990 (defun anything-pos-candidate-separator-p ()
2991 "Return t if the current line is a candidate separator."
2992 (get-text-property (point-at-bol) 'anything-candidate-separator))
2995 ;; (@* "Core: help")
2996 (defun anything-help-internal (bufname insert-content-fn)
2997 "Show long message during `anything' session in BUFNAME.
2998 INSERT-CONTENT-FN is the text to be displayed in BUFNAME."
2999 (save-window-excursion
3000 (select-window (anything-window))
3001 (delete-other-windows)
3002 (switch-to-buffer (get-buffer-create bufname))
3003 (erase-buffer)
3004 (funcall insert-content-fn)
3005 (setq mode-line-format "%b (SPC,C-v:NextPage b,M-v:PrevPage other:Exit)")
3006 (setq cursor-type nil)
3007 (goto-char 1)
3008 (anything-help-event-loop)))
3010 (defun anything-help-event-loop ()
3011 (ignore-errors
3012 (loop for event = (read-event) do
3013 (case event
3014 ((?\C-v ? ) (scroll-up))
3015 ((?\M-v ?b) (scroll-down))
3016 (t (return))))))
3018 (defun anything-help ()
3019 "Help of `anything'."
3020 (interactive)
3021 (anything-help-internal
3022 " *Anything Help*"
3023 (lambda ()
3024 (insert (substitute-command-keys
3025 (anything-interpret-value anything-help-message)))
3026 (org-mode))))
3028 (defun anything-debug-output ()
3029 "Show all anything-related variables at this time."
3030 (interactive)
3031 (anything-help-internal " *Anything Debug*" 'anything-debug-output-function))
3033 (defun anything-debug-output-function (&optional vars)
3034 (message "Calculating all anything-related values...")
3035 (insert "If you debug some variables or forms, set `anything-debug-forms'
3036 to a list of forms.\n\n")
3037 (dolist (v (or vars
3038 anything-debug-forms
3039 (apropos-internal "^anything-" 'boundp)))
3040 (insert "** "
3041 (pp-to-string v) "\n"
3042 (pp-to-string (with-current-buffer anything-buffer (eval v))) "\n"))
3043 (message "Calculating all anything-related values...Done"))
3046 ;; (@* "Core: misc")
3047 (defun anything-kill-buffer-hook ()
3048 "Remove tick entry from `anything-tick-hash' when killing a buffer."
3049 (loop for key being the hash-keys in anything-tick-hash
3050 if (string-match (format "^%s/" (regexp-quote (buffer-name))) key)
3051 do (remhash key anything-tick-hash)))
3052 (add-hook 'kill-buffer-hook 'anything-kill-buffer-hook)
3054 (defun anything-preselect (candidate-or-regexp)
3055 "Move `anything-selection-overlay' to CANDIDATE-OR-REGEXP on startup."
3056 (with-anything-window
3057 (when candidate-or-regexp
3058 (goto-char (point-min))
3059 ;; go to first candidate of first source
3060 (forward-line 1)
3061 (let ((start (point)))
3062 (or (re-search-forward
3063 (concat "^" (regexp-quote candidate-or-regexp) "$") nil t)
3064 (re-search-forward candidate-or-regexp nil t)
3065 (search-forward candidate-or-regexp nil t)
3066 (goto-char start))))
3067 (anything-mark-current-line)))
3069 (defun anything-delete-current-selection ()
3070 "Delete the currently selected item."
3071 (interactive)
3072 (with-anything-window
3073 (cond ((anything-pos-multiline-p)
3074 (anything-aif (anything-get-next-candidate-separator-pos)
3075 (delete-region (point-at-bol)
3076 (1+ (progn (goto-char it) (point-at-eol))))
3077 ;; last candidate
3078 (goto-char (anything-get-previous-candidate-separator-pos))
3079 (delete-region (point-at-bol) (point-max)))
3080 (when (anything-end-of-source-p)
3081 (goto-char (or (anything-get-previous-candidate-separator-pos)
3082 (point-min)))
3083 (forward-line 1)))
3085 (delete-region (point-at-bol) (1+ (point-at-eol)))
3086 (when (anything-end-of-source-p) (forward-line -1))))
3087 (anything-mark-current-line)))
3089 (defun anything-end-of-source-p ()
3090 "Return non--nil if we are at eob or end of source."
3091 (save-excursion
3092 (forward-line 1)
3093 (or (eq (point-at-bol) (point-at-eol))
3094 (anything-pos-header-line-p)
3095 (eobp))))
3097 (defun anything-edit-current-selection-internal (func)
3098 (with-anything-window
3099 (beginning-of-line)
3100 (let ((realvalue (get-text-property (point) 'anything-realvalue)))
3101 (funcall func)
3102 (beginning-of-line)
3103 (and realvalue
3104 (put-text-property (point) (point-at-eol)
3105 'anything-realvalue realvalue))
3106 (anything-mark-current-line))))
3108 (defmacro anything-edit-current-selection (&rest forms)
3109 "Evaluate FORMS at current selection in the anything buffer.
3110 You can edit the line."
3111 (declare (indent 0) (debug t))
3112 `(anything-edit-current-selection-internal
3113 (lambda () ,@forms)))
3115 (defun anything-set-pattern (pattern &optional noupdate)
3116 "Set minibuffer contents to PATTERN.
3117 if optional NOUPDATE is non-nil, anything buffer is not changed."
3118 (with-selected-window (or (active-minibuffer-window) (minibuffer-window))
3119 (delete-minibuffer-contents)
3120 (insert pattern))
3121 (when noupdate
3122 (setq anything-pattern pattern)
3123 (anything-hooks 'cleanup)
3124 (run-with-idle-timer 0 nil 'anything-hooks 'setup)))
3126 (defun anything-delete-minibuffer-contents ()
3127 "Same as `delete-minibuffer-contents' but this is a command."
3128 (interactive)
3129 (anything-set-pattern ""))
3130 (defalias 'anything-delete-minibuffer-content 'anything-delete-minibuffer-contents)
3133 ;;; Plugins
3135 ;; (@* "Built-in plug-in: type")
3136 (defun anything-compile-source--type (source)
3137 (anything-aif (assoc-default 'type source)
3138 (append source (assoc-default it anything-type-attributes) nil)
3139 source))
3141 ;; `define-anything-type-attribute' is public API.
3143 (defun anything-add-type-attribute (type definition)
3144 (anything-aif (assq type anything-type-attributes)
3145 (setq anything-type-attributes (delete it anything-type-attributes)))
3146 (push (cons type definition) anything-type-attributes))
3148 (defvar anything-types nil)
3149 (defun anything-document-type-attribute (type doc)
3150 (add-to-list 'anything-types type t)
3151 (put type 'anything-typeattrdoc
3152 (concat "- " (symbol-name type) "\n\n" doc "\n")))
3154 (defadvice documentation-property (after anything-document-type-attribute activate)
3155 "Display type attributes' documentation as `anything-type-attributes' docstring."
3156 (when (eq (ad-get-arg 0) 'anything-type-attributes)
3157 (setq ad-return-value
3158 (concat ad-return-value "\n\n++++ Types currently defined ++++\n"
3159 (mapconcat (lambda (sym) (get sym 'anything-typeattrdoc))
3160 anything-types "\n")))))
3162 ;; (@* "Built-in plug-in: dummy")
3163 (defun anything-dummy-candidate (candidate source)
3164 "Use `anything-pattern' as CANDIDATE in SOURCE."
3165 ;; `source' is defined in filtered-candidate-transformer
3166 (list anything-pattern))
3168 (defun anything-compile-source--dummy (source)
3169 (if (assoc 'dummy source)
3170 (append source
3171 '((candidates "dummy")
3172 (accept-empty)
3173 (match identity)
3174 (filtered-candidate-transformer . anything-dummy-candidate)
3175 (disable-shortcuts)
3176 (volatile)))
3177 source))
3179 ;; (@* "Built-in plug-in: disable-shortcuts")
3180 (defvar anything-orig-enable-shortcuts nil)
3181 (defun anything-save-enable-shortcuts ()
3182 (anything-once
3183 (lambda ()
3184 (setq anything-orig-enable-shortcuts anything-enable-shortcuts
3185 anything-enable-shortcuts nil))))
3187 (defun anything-compile-source--disable-shortcuts (source)
3188 (if (assoc 'disable-shortcuts source)
3189 (append `((init ,@(anything-mklist (assoc-default 'init source))
3190 anything-save-enable-shortcuts)
3191 (resume ,@(anything-mklist (assoc-default 'resume source))
3192 anything-save-enable-shortcuts)
3193 (cleanup ,@(anything-mklist (assoc-default 'cleanup source))
3194 (lambda () (setq anything-enable-shortcuts
3195 anything-orig-enable-shortcuts))))
3196 source)
3197 source))
3199 ;; (@* "Built-in plug-in: candidates-in-buffer")
3200 (defun anything-candidates-in-buffer ()
3201 "Get candidates from the candidates buffer according to `anything-pattern'.
3203 BUFFER is `anything-candidate-buffer' by default. Each
3204 candidate must be placed in one line. This function is meant to
3205 be used in candidates-in-buffer or candidates attribute of an
3206 anything source. Especially fast for many (1000+) candidates.
3209 '((name . \"many files\")
3210 (init . (lambda () (with-current-buffer (anything-candidate-buffer 'local)
3211 (insert-many-filenames))))
3212 (search re-search-forward) ; optional
3213 (candidates-in-buffer)
3214 (type . file))
3216 +===============================================================+
3217 | The new way of making and narrowing candidates: Using buffers |
3218 +===============================================================+
3220 By default, `anything' makes candidates by evaluating the
3221 candidates function, then narrows them by `string-match' for each
3222 candidate.
3224 But this way is very slow for many candidates. The new way is
3225 storing all candidates in a buffer and narrowing them by
3226 `re-search-forward'. Search function is customizable by search
3227 attribute. The important point is that buffer processing is MUCH
3228 FASTER than string list processing and is the Emacs way.
3230 The init function writes all candidates to a newly-created
3231 candidate buffer. The candidates buffer is created or specified
3232 by `anything-candidate-buffer'. Candidates are stored in a line.
3234 The candidates function narrows all candidates, IOW creates a
3235 subset of candidates dynamically. It is the task of
3236 `anything-candidates-in-buffer'. As long as
3237 `anything-candidate-buffer' is used,`(candidates-in-buffer)' is
3238 sufficient in most cases.
3240 Note that `(candidates-in-buffer)' is shortcut of three attributes:
3241 (candidates . anything-candidates-in-buffer)
3242 (volatile)
3243 (match identity)
3244 And `(candidates-in-buffer . func)' is shortcut of three attributes:
3245 (candidates . func)
3246 (volatile)
3247 (match identity)
3248 The expansion is performed in `anything-get-sources'.
3250 The candidates-in-buffer attribute implies the volatile attribute.
3251 The volatile attribute is needed because `anything-candidates-in-buffer'
3252 creates candidates dynamically and need to be called everytime
3253 `anything-pattern' changes.
3255 Because `anything-candidates-in-buffer' plays the role of `match' attribute
3256 function, specifying `(match identity)' makes the source slightly faster.
3258 To customize `anything-candidates-in-buffer' behavior, use search,
3259 get-line and search-from-end attributes. See also `anything-sources' docstring."
3260 (declare (special source))
3261 (anything-candidates-in-buffer-1
3262 (anything-candidate-buffer)
3263 anything-pattern
3264 (or (assoc-default 'get-line source)
3265 #'buffer-substring-no-properties)
3266 ;; use external variable `source'.
3267 (or (assoc-default 'search source)
3268 (if (assoc 'search-from-end source)
3269 '(anything-candidates-in-buffer-search-from-end)
3270 '(anything-candidates-in-buffer-search-from-start)))
3271 (anything-candidate-number-limit source)
3272 (assoc 'search-from-end source)))
3274 (defun anything-candidates-in-buffer-search-from-start (pattern)
3275 "Search PATTERN with `re-search-forward' with bound and noerror args."
3276 (re-search-forward pattern nil t))
3278 (defun anything-candidates-in-buffer-search-from-end (pattern)
3279 "Search PATTERN with `re-search-backward' with bound and noerror args."
3280 (re-search-backward pattern nil t))
3282 (defun anything-candidates-in-buffer-1 (buffer pattern get-line-fn
3283 search-fns limit search-from-end)
3284 ;; buffer == nil when candidates buffer does not exist.
3285 (when buffer
3286 (with-current-buffer buffer
3287 (let ((start-point (if search-from-end (point-max) (point-min)))
3288 (endp (if search-from-end #'bobp #'eobp)))
3289 (goto-char (1- start-point))
3290 (if (string= pattern "")
3291 (anything-initial-candidates-from-candidate-buffer
3292 endp get-line-fn limit search-from-end)
3293 (anything-search-from-candidate-buffer
3294 pattern get-line-fn search-fns limit search-from-end
3295 start-point endp))))))
3297 (defun anything-point-is-moved (proc)
3298 "If point is moved after executing PROC, return t, otherwise nil."
3299 (/= (point) (progn (funcall proc) (point))))
3301 (defun anything-search-from-candidate-buffer (pattern get-line-fn search-fns
3302 limit search-from-end
3303 start-point endp)
3304 (let (buffer-read-only
3305 matches exit newmatches)
3306 (anything-search-from-candidate-buffer-internal
3307 (lambda ()
3308 (clrhash anything-cib-hash)
3309 (dolist (searcher search-fns)
3310 (goto-char start-point)
3311 (setq newmatches nil)
3312 (loop with item-count = 0
3313 while (funcall searcher pattern)
3314 for cand = (funcall get-line-fn (point-at-bol) (point-at-eol))
3315 do (anything-accumulate-candidates-internal
3316 cand newmatches anything-cib-hash item-count limit)
3317 unless (anything-point-is-moved
3318 (lambda ()
3319 (if search-from-end
3320 (goto-char (1- (point-at-bol)))
3321 (forward-line 1))))
3322 return nil)
3323 (setq matches (append matches (nreverse newmatches)))
3324 (if exit (return)))
3325 (delq nil matches)))))
3327 (defun anything-initial-candidates-from-candidate-buffer (endp get-line-fn limit search-from-end)
3328 (delq nil (loop with next-line-fn =
3329 (if search-from-end
3330 (lambda (x) (goto-char (max (1- (point-at-bol)) 1)))
3331 #'forward-line)
3332 until (funcall endp)
3333 for i from 1 to limit
3334 collect (funcall get-line-fn (point-at-bol) (point-at-eol))
3335 do (funcall next-line-fn 1))))
3337 (defun anything-search-from-candidate-buffer-internal (search-fn)
3338 (goto-char (point-min))
3339 (insert "\n")
3340 (goto-char (point-max))
3341 (insert "\n")
3342 (unwind-protect
3343 (funcall search-fn)
3344 (goto-char (point-min))
3345 (delete-char 1)
3346 (goto-char (1- (point-max)))
3347 (delete-char 1)
3349 (set-buffer-modified-p nil)))
3351 (defun anything-candidate-buffer (&optional create-or-buffer)
3352 "Register and return a buffer containing candidates of current source.
3353 `anything-candidate-buffer' searches buffer-local candidates buffer first,
3354 then global candidates buffer.
3356 Acceptable values of CREATE-OR-BUFFER:
3358 - nil (omit)
3359 Only return the candidates buffer.
3360 - a buffer
3361 Register a buffer as a candidates buffer.
3362 - 'global
3363 Create a new global candidates buffer,
3364 named \" *anything candidates:SOURCE*\".
3365 - other non-nil value
3366 Create a new local candidates buffer,
3367 named \" *anything candidates:SOURCE*ANYTHING-CURRENT-BUFFER\"."
3368 (let* ((global-bname (format " *anything candidates:%s*" anything-source-name))
3369 (local-bname (format " *anything candidates:%s*%s"
3370 anything-source-name
3371 (buffer-name anything-current-buffer)))
3372 (register-func
3373 (lambda ()
3374 (setq anything-candidate-buffer-alist
3375 (cons (cons anything-source-name create-or-buffer)
3376 (delete (assoc anything-source-name
3377 anything-candidate-buffer-alist)
3378 anything-candidate-buffer-alist)))))
3379 (kill-buffers-func
3380 (lambda ()
3381 (loop for b in (buffer-list)
3382 if (string-match (format "^%s" (regexp-quote global-bname))
3383 (buffer-name b))
3384 do (kill-buffer b))))
3385 (create-func
3386 (lambda ()
3387 (with-current-buffer
3388 (get-buffer-create (if (eq create-or-buffer 'global)
3389 global-bname
3390 local-bname))
3391 (buffer-disable-undo)
3392 (erase-buffer)
3393 (font-lock-mode -1))))
3394 (return-func
3395 (lambda ()
3396 (or (get-buffer local-bname)
3397 (get-buffer global-bname)
3398 (anything-aif (assoc-default anything-source-name
3399 anything-candidate-buffer-alist)
3400 (and (buffer-live-p it) it))))))
3401 (when create-or-buffer
3402 (funcall register-func)
3403 (unless (bufferp create-or-buffer)
3404 (and (eq create-or-buffer 'global) (funcall kill-buffers-func))
3405 (funcall create-func)))
3406 (funcall return-func)))
3408 (defun anything-compile-source--candidates-in-buffer (source)
3409 (anything-aif (assoc 'candidates-in-buffer source)
3410 (append source
3411 `((candidates . ,(or (cdr it) 'anything-candidates-in-buffer))
3412 (volatile) (match identity)))
3413 source))
3416 ;; (@* "Utility: resplit anything window")
3417 (defun anything-toggle-resplit-window ()
3418 "Toggle resplit anything window, vertically or horizontally."
3419 (interactive)
3420 (with-anything-window
3421 (let ((before-height (window-height)))
3422 (delete-window)
3423 (set-window-buffer
3424 (select-window (if (= (window-height) before-height)
3425 (prog1
3426 (split-window-vertically)
3427 (setq anything-split-window-state 'vertical))
3428 (setq anything-split-window-state 'horizontal)
3429 (split-window-horizontally)))
3430 anything-buffer))))
3432 ;; (@* "Utility: Resize anything window.")
3433 (defun anything-enlarge-window-1 (n)
3434 "Enlarge or narrow anything window.
3435 If N is positive enlarge, if negative narrow."
3436 (unless anything-samewindow
3437 (let ((horizontal-p (eq anything-split-window-state 'horizontal)))
3438 (with-anything-window
3439 (enlarge-window n horizontal-p)))))
3441 (defun anything-narrow-window ()
3442 "Narrow anything window."
3443 (interactive)
3444 (anything-enlarge-window-1 -1))
3446 (defun anything-enlarge-window ()
3447 "Enlarge anything window."
3448 (interactive)
3449 (anything-enlarge-window-1 1))
3451 ;; (@* "Utility: select another action by key")
3452 (defun anything-select-nth-action (n)
3453 "Select the N nth action for the currently selected candidate."
3454 (setq anything-saved-selection (anything-get-selection))
3455 (unless anything-saved-selection
3456 (error "Nothing is selected"))
3457 (setq anything-saved-action (anything-get-nth-action n (anything-get-action)))
3458 (anything-exit-minibuffer))
3460 (defun anything-get-nth-action (n action)
3461 (cond ((and (zerop n) (functionp action))
3462 action)
3463 ((listp action)
3464 (or (cdr (elt action n))
3465 (error "No such action")))
3466 ((and (functionp action) (< 0 n))
3467 (error "Sole action"))
3469 (error "Error in `anything-select-nth-action'"))))
3471 (defun anything-select-2nd-action ()
3472 "Select the 2nd action for the currently selected candidate."
3473 (interactive)
3474 (anything-select-nth-action 1))
3476 (defun anything-select-3rd-action ()
3477 "Select the 3rd action for the currently selected candidate."
3478 (interactive)
3479 (anything-select-nth-action 2))
3481 (defun anything-select-4th-action ()
3482 "Select the 4th action for the currently selected candidate."
3483 (interactive)
3484 (anything-select-nth-action 3))
3486 (defun anything-select-2nd-action-or-end-of-line ()
3487 "Select the 2nd action for the currently selected candidate.
3488 This happen when point is at the end of minibuffer.
3489 Otherwise goto the end of minibuffer."
3490 (interactive)
3491 (if (eolp)
3492 (anything-select-nth-action 1)
3493 (end-of-line)))
3495 ;; (@* "Utility: Persistent Action")
3496 (defmacro with-anything-display-same-window (&rest body)
3497 "Execute BODY in the window used for persistent action.
3498 Make `pop-to-buffer' and `display-buffer' display in the same window."
3499 (declare (indent 0) (debug t))
3500 `(let ((display-buffer-function 'anything-persistent-action-display-buffer))
3501 ,@body))
3503 (defvar anything-persistent-action-display-window nil)
3504 (defun anything-initialize-persistent-action ()
3505 (set (make-local-variable 'anything-persistent-action-display-window) nil))
3507 (defun* anything-execute-persistent-action (&optional (attr 'persistent-action) onewindow)
3508 "Perform the associated action ATTR without quitting anything.
3509 ATTR default is 'persistent-action', but it can be anything else.
3510 In this case you have to add this new attribute to your source.
3511 When `anything-samewindow' and ONEWINDOW are non--nil,
3512 the anything window is never split in persistent action."
3513 (interactive)
3514 (anything-log "executing persistent-action")
3515 (with-anything-window
3516 (save-selected-window
3517 (anything-select-persistent-action-window onewindow)
3518 (anything-log-eval (current-buffer))
3519 (let ((anything-in-persistent-action t))
3520 (with-anything-display-same-window
3521 (anything-execute-selection-action
3523 (or (assoc-default attr (anything-get-current-source))
3524 (anything-get-action))
3526 (anything-log-run-hook 'anything-after-persistent-action-hook))))))
3529 (defun anything-persistent-action-display-window (&optional onewindow)
3530 "Return the window that will be used for presistent action.
3531 If ONEWINDOW is non--nil window will not be splitted in persistent action
3532 if `anything-samewindow' is non--nil also."
3533 (with-anything-window
3534 (setq anything-persistent-action-display-window
3535 (cond ((window-live-p anything-persistent-action-display-window)
3536 anything-persistent-action-display-window)
3537 ((and anything-samewindow (one-window-p t) (not onewindow))
3538 (split-window))
3539 ((get-buffer-window anything-current-buffer))
3541 (next-window (selected-window) 1))))))
3543 (defun anything-select-persistent-action-window (&optional onewindow)
3544 "Select the window that will be used for persistent action.
3545 See `anything-persistent-action-display-window' for how to use ONEWINDOW."
3546 (select-window (get-buffer-window (anything-buffer-get)))
3547 (select-window
3548 (setq minibuffer-scroll-window
3549 (anything-persistent-action-display-window onewindow))))
3551 (defun anything-persistent-action-display-buffer (buf &optional not-this-window)
3552 "Make `pop-to-buffer' and `display-buffer' display in the same window.
3553 If `anything-persistent-action-use-special-display' is non-nil and
3554 BUF is to be displayed by `special-display-function', use it.
3555 Otherwise ignores `special-display-buffer-names' and `special-display-regexps'.
3556 Argument NOT-THIS-WINDOW if present will be used as
3557 second argument of `display-buffer'."
3558 (let* ((name (buffer-name buf))
3559 display-buffer-function pop-up-windows pop-up-frames
3560 (same-window-regexps
3561 (unless (and anything-persistent-action-use-special-display
3562 (or (member name
3563 (mapcar (lambda (x) (or (car-safe x) x))
3564 special-display-buffer-names))
3565 (remove-if-not
3566 (lambda (x) (string-match (or (car-safe x) x) name))
3567 special-display-regexps)))
3568 '("."))))
3569 (display-buffer buf not-this-window)))
3571 ;; scroll-other-window(-down)? for persistent-action
3572 (defun anything-scroll-other-window-base (command)
3573 (with-selected-window (anything-persistent-action-display-window)
3574 (funcall command anything-scroll-amount)))
3576 (defun anything-scroll-other-window ()
3577 "Scroll other window (not *Anything* window) upward."
3578 (interactive)
3579 (anything-scroll-other-window-base 'scroll-up))
3581 (defun anything-scroll-other-window-down ()
3582 "Scroll other window (not *Anything* window) downward."
3583 (interactive)
3584 (anything-scroll-other-window-base 'scroll-down))
3587 ;; (@* "Utility: Visible Mark")
3588 (defface anything-visible-mark
3589 '((((min-colors 88) (background dark))
3590 (:background "green1" :foreground "black"))
3591 (((background dark)) (:background "green" :foreground "black"))
3592 (((min-colors 88)) (:background "green1"))
3593 (t (:background "green")))
3594 "Face for visible mark."
3595 :group 'anything)
3597 (defvar anything-visible-mark-face 'anything-visible-mark)
3598 (defvar anything-visible-mark-overlays nil)
3600 (defun anything-clear-visible-mark ()
3601 (with-current-buffer (anything-buffer-get)
3602 (mapc 'delete-overlay anything-visible-mark-overlays)
3603 (set (make-local-variable 'anything-visible-mark-overlays) nil)))
3604 (add-hook 'anything-after-initialize-hook 'anything-clear-visible-mark)
3606 (defvar anything-marked-candidates nil
3607 "Marked candadates. List of \(source . real\) pair.")
3609 (defun anything-this-visible-mark ()
3610 (loop for o in anything-visible-mark-overlays
3611 when (equal (point-at-bol) (overlay-start o))
3612 return o))
3614 (defun anything-delete-visible-mark (overlay)
3615 (setq anything-marked-candidates
3616 (remove
3617 (cons (anything-get-current-source) (anything-get-selection))
3618 anything-marked-candidates))
3619 (delete-overlay overlay)
3620 (setq anything-visible-mark-overlays
3621 (delq overlay anything-visible-mark-overlays)))
3623 (defun anything-make-visible-mark ()
3624 (let ((o (make-overlay (point-at-bol) (1+ (point-at-eol)))))
3625 (overlay-put o 'face anything-visible-mark-face)
3626 (overlay-put o 'source (assoc-default 'name (anything-get-current-source)))
3627 (overlay-put o 'string (buffer-substring (overlay-start o) (overlay-end o)))
3628 (overlay-put o 'real (anything-get-selection))
3629 (add-to-list 'anything-visible-mark-overlays o))
3630 (push (cons (anything-get-current-source) (anything-get-selection))
3631 anything-marked-candidates))
3633 (defun anything-toggle-visible-mark ()
3634 "Toggle anything visible mark at point."
3635 (interactive)
3636 (with-anything-window
3637 (anything-aif (anything-this-visible-mark)
3638 (anything-delete-visible-mark it)
3639 (anything-make-visible-mark))
3640 (anything-next-line)))
3642 (defun anything-display-all-visible-marks ()
3643 "Show all `anything' visible marks strings."
3644 (interactive)
3645 (with-anything-window
3646 (lexical-let ((overlays (reverse anything-visible-mark-overlays)))
3647 (anything-run-after-quit
3648 (lambda ()
3649 (with-output-to-temp-buffer "*anything visible marks*"
3650 (dolist (o overlays) (princ (overlay-get o 'string)))))))))
3652 (defun anything-marked-candidates ()
3653 "Return marked candidates of current source if any.
3654 Otherwise one element list of current selection.
3656 It is analogous to `dired-get-marked-files'."
3657 (with-current-buffer (anything-buffer-get)
3658 (let ((cands
3659 (if anything-marked-candidates
3660 (loop with current-src = (anything-get-current-source)
3661 for (source . real) in (reverse anything-marked-candidates)
3662 when (equal current-src source)
3663 collect (anything-coerce-selection real source))
3664 (list (anything-get-selection)))))
3665 (anything-log-eval cands)
3666 cands)))
3668 (defun anything-reset-marked-candidates ()
3669 (with-current-buffer (anything-buffer-get)
3670 (set (make-local-variable 'anything-marked-candidates) nil)))
3672 (add-hook 'anything-after-initialize-hook 'anything-reset-marked-candidates)
3673 ;; (add-hook 'anything-after-action-hook 'anything-reset-marked-candidates)
3675 (defun anything-current-source-name= (name)
3676 (save-excursion
3677 (goto-char (anything-get-previous-header-pos))
3678 (equal name (anything-current-line-contents))))
3680 (defun anything-revive-visible-mark ()
3681 "Restore marked candidates when anything update display."
3682 (with-current-buffer anything-buffer
3683 (dolist (o anything-visible-mark-overlays)
3684 (goto-char (point-min))
3685 (while (and (search-forward (overlay-get o 'string) nil t)
3686 (anything-current-source-name= (overlay-get o 'source)))
3687 ;; Calculate real value of candidate.
3688 ;; It can be nil if candidate have only a display value.
3689 (let ((real (get-text-property (point-at-bol 0) 'anything-realvalue)))
3690 (if real
3691 ;; Check if real value of current candidate is the same
3692 ;; that the one stored in overlay.
3693 (and (string= (overlay-get o 'real) real)
3694 (move-overlay o (point-at-bol 0) (1+ (point-at-eol 0))))
3695 (move-overlay o (point-at-bol 0) (1+ (point-at-eol 0)))))))))
3696 (add-hook 'anything-update-hook 'anything-revive-visible-mark)
3698 (defun anything-next-point-in-list (curpos points &optional prev)
3699 (cond
3700 ;; rule out special cases
3701 ((null points) curpos)
3702 ((and prev (< curpos (car points))) curpos)
3703 ((< (car (last points)) curpos)
3704 (if prev (car (last points)) curpos))
3706 (nth (if prev
3707 (loop for pt in points
3708 for i from 0
3709 if (<= curpos pt)
3710 return (1- i))
3711 (loop for pt in points
3712 for i from 0
3713 if (< curpos pt)
3714 return i))
3715 points))))
3717 (defun anything-next-visible-mark (&optional prev)
3718 "Move next anything visible mark.
3719 If PREV is non-nil move to precedent."
3720 (interactive)
3721 (with-anything-window
3722 (goto-char (anything-next-point-in-list
3723 (point)
3724 (sort (mapcar 'overlay-start anything-visible-mark-overlays) '<)
3725 prev))
3726 (anything-mark-current-line)))
3728 (defun anything-prev-visible-mark ()
3729 "Move previous anything visible mark."
3730 (interactive)
3731 (anything-next-visible-mark t))
3733 ;; (@* "Utility: Selection Paste")
3734 (defun anything-yank-selection ()
3735 "Set minibuffer contents to current selection."
3736 (interactive)
3737 (anything-set-pattern (anything-get-selection nil t)))
3739 (defun anything-kill-selection-and-quit ()
3740 "Store current selection to kill ring.
3741 You can paste it by typing \\[yank]."
3742 (interactive)
3743 (anything-run-after-quit
3744 (lambda (sel)
3745 (kill-new sel)
3746 (message "Killed: %s" sel))
3747 (anything-get-selection nil t)))
3750 ;; (@* "Utility: Automatical execution of persistent-action")
3751 (add-to-list 'minor-mode-alist '(anything-follow-mode " AFollow"))
3752 (defun anything-follow-mode ()
3753 "If this mode is on, persistent action is executed everytime the cursor is moved."
3754 (interactive)
3755 (with-current-buffer anything-buffer
3756 (setq anything-follow-mode (not anything-follow-mode))
3757 (message "anything-follow-mode is %s"
3758 (if anything-follow-mode "enabled" "disabled"))))
3760 (defun anything-follow-execute-persistent-action-maybe ()
3761 "Execute persistent action in mode `anything-follow-mode'.
3762 This happen after `anything-input-idle-delay' secs."
3763 (and (not (get-buffer-window anything-action-buffer 'visible))
3764 (buffer-local-value 'anything-follow-mode
3765 (get-buffer-create anything-buffer))
3766 (sit-for (and anything-input-idle-delay
3767 (max anything-input-idle-delay 0.1)))
3768 (anything-window)
3769 (anything-get-selection)
3770 (save-excursion
3771 (anything-execute-persistent-action))))
3774 ;; (@* "Utility: Migrate `anything-sources' to my-anything command")
3775 (defun anything-migrate-sources ()
3776 "Help to migrate to new `anything' way."
3777 (interactive)
3778 (with-current-buffer (get-buffer-create "*anything migrate*")
3779 (erase-buffer)
3780 (insert (format "\
3781 Setting `anything-sources' directly is not good because
3782 `anything' is not for one command. For now, interactive use of
3783 `anything' (M-x anything) is only for demonstration purpose.
3784 So you should define commands calling `anything'.
3785 I help you to migrate to the new way.
3787 The code below is automatically generated from current
3788 `anything-sources' value. You can use the `my-anything' command
3789 now!
3791 Copy and paste it to your .emacs. Then substitute `my-anything'
3792 for `anything' bindings in all `define-key', `local-set-key' and
3793 `global-set-key' calls.
3795 \(defun my-anything ()
3796 \"Anything command for you.
3798 It is automatically generated by `anything-migrate-sources'.\"
3799 (interactive)
3800 (anything-other-buffer
3802 \"*my-anything*\"))
3803 " anything-sources))
3804 (eval-last-sexp nil)
3805 (substitute-key-definition 'anything 'my-anything global-map)
3806 (pop-to-buffer (current-buffer))))
3809 ;; (@* "Compatibility")
3811 ;; Copied assoc-default from XEmacs version 21.5.12
3812 (unless (fboundp 'assoc-default)
3813 (defun assoc-default (key alist &optional test default)
3814 "Find object KEY in a pseudo-alist ALIST.
3815 ALIST is a list of conses or objects. Each element (or the element's car,
3816 if it is a cons) is compared with KEY by evaluating (TEST (car elt) KEY).
3817 If that is non-nil, the element matches;
3818 then `assoc-default' returns the element's cdr, if it is a cons,
3819 or DEFAULT if the element is not a cons.
3821 If no element matches, the value is nil.
3822 If TEST is omitted or nil, `equal' is used."
3823 (let (found (tail alist) value)
3824 (while (and tail (not found))
3825 (let ((elt (car tail)))
3826 (when (funcall (or test 'equal) (if (consp elt) (car elt) elt) key)
3827 (setq found t value (if (consp elt) (cdr elt) default))))
3828 (setq tail (cdr tail)))
3829 value)))
3831 ;; Function not available in XEmacs,
3832 (unless (fboundp 'minibuffer-contents)
3833 (defun minibuffer-contents ()
3834 "Return the user input in a minbuffer as a string.
3835 The current buffer must be a minibuffer."
3836 (field-string (point-max)))
3838 (defun delete-minibuffer-contents ()
3839 "Delete all user input in a minibuffer.
3840 The current buffer must be a minibuffer."
3841 (delete-field (point-max))))
3843 ;; Function not available in older Emacs (<= 22.1).
3844 (unless (fboundp 'buffer-chars-modified-tick)
3845 (defun buffer-chars-modified-tick (&optional buffer)
3846 "Return BUFFER's character-change tick counter.
3847 Each buffer has a character-change tick counter, which is set to the
3848 value of the buffer's tick counter (see `buffer-modified-tick'), each
3849 time text in that buffer is inserted or deleted. By comparing the
3850 values returned by two individual calls of `buffer-chars-modified-tick',
3851 you can tell whether a character change occurred in that buffer in
3852 between these calls. No argument or nil as argument means use current
3853 buffer as BUFFER."
3854 (with-current-buffer (or buffer (current-buffer))
3855 (if (listp buffer-undo-list)
3856 (length buffer-undo-list)
3857 (buffer-modified-tick)))))
3860 ;; (@* "CUA workaround")
3861 (defadvice cua-delete-region (around anything-avoid-cua activate)
3862 (ignore-errors ad-do-it))
3864 (defadvice copy-region-as-kill (around anything-avoid-cua activate)
3865 (if cua-mode
3866 (ignore-errors ad-do-it)
3867 ad-do-it))
3869 ;;(@* "Attribute Documentation")
3870 (defun anything-describe-anything-attribute (anything-attribute)
3871 "Display the full documentation of ANYTHING-ATTRIBUTE.
3872 ANYTHING-ATTRIBUTE should be a symbol."
3873 (interactive (list (intern
3874 (completing-read
3875 "Describe anything attribute: "
3876 (mapcar 'symbol-name anything-additional-attributes)))))
3877 (with-output-to-temp-buffer "*Help*"
3878 (princ (get anything-attribute 'anything-attrdoc))))
3880 (anything-document-attribute 'name "mandatory"
3881 " The name of the source. It is also the heading which appears
3882 above the list of matches from the source. Must be unique. ")
3883 (anything-document-attribute 'header-name "optional"
3884 " A function returning the display string of the header. Its
3885 argument is the name of the source. This attribute is useful to
3886 add an additional information with the source name. ")
3887 (anything-document-attribute 'candidates "mandatory if candidates-in-buffer attribute is not provided"
3888 " Specifies how to retrieve candidates from the source. It can
3889 either be a variable name, a function called with no parameters
3890 or the actual list of candidates.
3892 The list must be a list whose members are strings, symbols
3893 or (DISPLAY . REAL) pairs.
3895 In case of (DISPLAY . REAL) pairs, the DISPLAY string is shown
3896 in the Anything buffer, but the REAL one is used as action
3897 argument when the candidate is selected. This allows a more
3898 readable presentation for candidates which would otherwise be,
3899 for example, too long or have a common part shared with other
3900 candidates which can be safely replaced with an abbreviated
3901 string for display purposes.
3903 Note that if the (DISPLAY . REAL) form is used then pattern
3904 matching is done on the displayed string, not on the real
3905 value.
3907 If the candidates have to be retrieved asynchronously (for
3908 example, by an external command which takes a while to run)
3909 then the function should start the external command
3910 asynchronously and return the associated process object.
3911 Anything will take care of managing the process (receiving the
3912 output from it, killing it if necessary, etc.). The process
3913 should return candidates matching the current pattern (see
3914 variable `anything-pattern'.)
3916 Note that currently results from asynchronous sources appear
3917 last in the anything buffer regardless of their position in
3918 `anything-sources'. ")
3919 (anything-document-attribute 'action "mandatory if type attribute is not provided"
3920 " It is a list of (DISPLAY . FUNCTION) pairs or FUNCTION.
3921 FUNCTION is called with one parameter: the selected candidate.
3923 An action other than the default can be chosen from this list
3924 of actions for the currently selected candidate (by default
3925 with TAB). The DISPLAY string is shown in the completions
3926 buffer and the FUNCTION is invoked when an action is
3927 selected. The first action of the list is the default. ")
3928 (anything-document-attribute 'coerce "optional"
3929 " It's a function called with one argument: the selected candidate.
3931 This function is intended for type convertion.
3932 In normal case, the selected candidate (string) is passed to action function.
3933 If coerce function is specified, it is called just before action function.
3935 Example: converting string to symbol
3936 (coerce . intern) ")
3937 (anything-document-attribute 'type "optional if action attribute is provided"
3938 " Indicates the type of the items the source returns.
3940 Merge attributes not specified in the source itself from
3941 `anything-type-attributes'.
3943 This attribute is implemented by plug-in. ")
3944 (anything-document-attribute 'init "optional"
3945 " Function called with no parameters when anything is started. It
3946 is useful for collecting current state information which can be
3947 used to create the list of candidates later.
3949 For example, if a source needs to work with the current
3950 directory then it can store its value here, because later
3951 anything does its job in the minibuffer and in the
3952 `anything-buffer' and the current directory can be different
3953 there. ")
3954 (anything-document-attribute 'delayed-init "optional"
3955 " Function called with no parameters before candidate function is
3956 called. It is similar with `init' attribute, but its
3957 evaluation is deferred. It is useful to combine with ")
3958 (anything-document-attribute 'match "optional"
3959 " List of functions called with one parameter: a candidate. The
3960 function should return non-nil if the candidate matches the
3961 current pattern (see variable `anything-pattern').
3963 This attribute allows the source to override the default
3964 pattern matching based on `string-match'. It can be used, for
3965 example, to implement a source for file names and do the
3966 pattern matching on the basename of files, since it's more
3967 likely one is typing part of the basename when searching for a
3968 file, instead of some string anywhere else in its path.
3970 If the list contains more than one function then the list of
3971 matching candidates from the source is constructed by appending
3972 the results after invoking the first function on all the
3973 potential candidates, then the next function, and so on. The
3974 matching candidates supplied by the first function appear first
3975 in the list of results and then results from the other
3976 functions, respectively.
3978 This attribute has no effect for asynchronous sources (see
3979 attribute `candidates'), since they perform pattern matching
3980 themselves. ")
3981 (anything-document-attribute 'candidate-transformer "optional"
3982 " It's a function or a list of functions called with one argument
3983 when the completion list from the source is built. The argument
3984 is the list of candidates retrieved from the source. The
3985 function should return a transformed list of candidates which
3986 will be used for the actual completion. If it is a list of
3987 functions, it calls each function sequentially.
3989 This can be used to transform or remove items from the list of
3990 candidates.
3992 Note that `candidates' is run already, so the given transformer
3993 function should also be able to handle candidates with (DISPLAY
3994 . REAL) format. ")
3995 (anything-document-attribute 'filtered-candidate-transformer "optional"
3996 " It has the same format as `candidate-transformer', except the
3997 function is called with two parameters: the candidate list and
3998 the source.
4000 This transformer is run on the candidate list which is already
4001 filtered by the current pattern. While `candidate-transformer'
4002 is run only once, it is run every time the input pattern is
4003 changed.
4005 It can be used to transform the candidate list dynamically, for
4006 example, based on the current pattern.
4008 In some cases it may also be more efficent to perform candidate
4009 transformation here, instead of with `candidate-transformer'
4010 even if this transformation is done every time the pattern is
4011 changed. For example, if a candidate set is very large then
4012 `candidate-transformer' transforms every candidate while only
4013 some of them will actually be dislpayed due to the limit
4014 imposed by `anything-candidate-number-limit'.
4016 Note that `candidates' and `candidate-transformer' is run
4017 already, so the given transformer function should also be able
4018 to handle candidates with (DISPLAY . REAL) format.
4020 This option has no effect for asynchronous sources. (Not yet,
4021 at least. ")
4022 (anything-document-attribute 'action-transformer "optional"
4023 " It's a function or a list of functions called with two
4024 arguments when the action list from the source is
4025 assembled. The first argument is the list of actions, the
4026 second is the current selection. If it is a list of functions,
4027 it calls each function sequentially.
4029 The function should return a transformed action list.
4031 This can be used to customize the list of actions based on the
4032 currently selected candidate. ")
4033 (anything-document-attribute 'pattern-transformer "optional"
4034 " It's a function or a list of functions called with one argument
4035 before computing matches. Its argument is `anything-pattern'.
4036 Functions should return transformed `anything-pattern'.
4038 It is useful to change interpretation of `anything-pattern'. ")
4039 (anything-document-attribute 'delayed "optional"
4040 " Candidates from the source are shown only if the user stops
4041 typing and is idle for `anything-idle-delay' seconds. ")
4042 (anything-document-attribute 'volatile "optional"
4043 " Indicates the source assembles the candidate list dynamically,
4044 so it shouldn't be cached within a single Anything
4045 invocation. It is only applicable to synchronous sources,
4046 because asynchronous sources are not cached. ")
4047 (anything-document-attribute 'requires-pattern "optional"
4048 " If present matches from the source are shown only if the
4049 pattern is not empty. Optionally, it can have an integer
4050 parameter specifying the required length of input which is
4051 useful in case of sources with lots of candidates. ")
4052 (anything-document-attribute 'persistent-action "optional"
4053 " Function called with one parameter; the selected candidate.
4055 An action performed by `anything-execute-persistent-action'.
4056 If none, use the default action. ")
4057 (anything-document-attribute 'candidates-in-buffer "optional"
4058 " Shortcut attribute for making and narrowing candidates using
4059 buffers. This newly-introduced attribute prevents us from
4060 forgetting to add volatile and match attributes.
4062 See docstring of `anything-candidates-in-buffer'.
4064 (candidates-in-buffer) is equivalent of three attributes:
4065 (candidates . anything-candidates-in-buffer)
4066 (volatile)
4067 (match identity)
4069 (candidates-in-buffer . candidates-function) is equivalent of:
4070 (candidates . candidates-function)
4071 (volatile)
4072 (match identity)
4074 This attribute is implemented by plug-in. ")
4075 (anything-document-attribute 'search "optional"
4076 " List of functions like `re-search-forward' or `search-forward'.
4077 Buffer search function used by `anything-candidates-in-buffer'.
4078 By default, `anything-candidates-in-buffer' uses `re-search-forward'.
4079 This attribute is meant to be used with
4080 (candidates . anything-candidates-in-buffer) or
4081 (candidates-in-buffer) in short. ")
4082 (anything-document-attribute 'search-from-end "optional"
4083 " Make `anything-candidates-in-buffer' search from the end of buffer.
4084 If this attribute is specified, `anything-candidates-in-buffer' uses
4085 `re-search-backward' instead. ")
4086 (anything-document-attribute 'get-line "optional"
4087 " A function like `buffer-substring-no-properties' or `buffer-substring'.
4088 This function converts point of line-beginning and point of line-end,
4089 which represents a candidate computed by `anything-candidates-in-buffer'.
4090 By default, `anything-candidates-in-buffer' uses
4091 `buffer-substring-no-properties'. ")
4092 (anything-document-attribute 'display-to-real "optional"
4093 " Function called with one parameter; the selected candidate.
4095 The function transforms the selected candidate, and the result
4096 is passed to the action function. The display-to-real
4097 attribute provides another way to pass other string than one
4098 shown in Anything buffer.
4100 Traditionally, it is possible to make candidates,
4101 candidate-transformer or filtered-candidate-transformer
4102 function return a list with (DISPLAY . REAL) pairs. But if REAL
4103 can be generated from DISPLAY, display-to-real is more
4104 convenient and faster. ")
4105 (anything-document-attribute 'real-to-display "optional"
4106 " Function called with one parameter; the selected candidate.
4108 The inverse of display-to-real attribute.
4110 The function transforms the selected candidate, which is passed
4111 to the action function, for display. The real-to-display
4112 attribute provides the other way to pass other string than one
4113 shown in Anything buffer.
4115 Traditionally, it is possible to make candidates,
4116 candidate-transformer or filtered-candidate-transformer
4117 function return a list with (DISPLAY . REAL) pairs. But if
4118 DISPLAY can be generated from REAL, real-to-display is more
4119 convenient.
4121 Note that DISPLAY parts returned from candidates /
4122 candidate-transformer are IGNORED as the name `display-to-real'
4123 says. ")
4124 (anything-document-attribute 'cleanup "optional"
4125 " Function called with no parameters when *anything* buffer is closed. It
4126 is useful for killing unneeded candidates buffer.
4128 Note that the function is executed BEFORE performing action. ")
4129 (anything-document-attribute 'candidate-number-limit "optional"
4130 " Override `anything-candidate-number-limit' only for this source. ")
4131 (anything-document-attribute 'accept-empty "optional"
4132 " Pass empty string \"\" to action function. ")
4133 (anything-document-attribute 'disable-shortcuts "optional"
4134 " Disable `anything-enable-shortcuts' in current `anything' session.
4136 This attribute is implemented by plug-in. ")
4137 (anything-document-attribute 'dummy "optional"
4138 " Set `anything-pattern' to candidate. If this attribute is
4139 specified, The candidates attribute is ignored.
4141 This attribute is implemented by plug-in.
4142 This plug-in implies disable-shortcuts plug-in. ")
4143 (anything-document-attribute 'multiline "optional"
4144 " Enable to selection multiline candidates. ")
4145 (anything-document-attribute 'update "optional"
4146 ;; FIXME: this is not displayed correctly in help buffer.
4147 " Function called with no parameters when \\<anything-map>\\[anything-force-update] is pressed. ")
4148 (anything-document-attribute 'mode-line "optional"
4149 " source local `anything-mode-line-string'. (included in `mode-line-format')
4150 It accepts also variable/function name. ")
4151 (anything-document-attribute 'header-line "optional"
4152 " source local `header-line-format'.
4153 It accepts also variable/function name. ")
4154 (anything-document-attribute
4155 'resume "optional"
4156 " Function called with no parameters when `anything-resume' is started.")
4159 ;; (@* "Bug Report")
4160 (defvar anything-maintainer-mail-address "emacs-anything@googlegroups.com")
4162 (defvar anything-bug-report-salutation
4163 "Describe bug below, using a precise recipe.
4165 When I executed M-x ...
4167 How to send a bug report:
4168 1) Be sure to use the LATEST version of anything.el.
4169 2) Enable debugger. M-x toggle-debug-on-error or (setq debug-on-error t)
4170 3) Use Lisp version instead of compiled one: (load \"anything.el\")
4171 4) If you got an error, please paste *Backtrace* buffer.
4172 5) Type C-c C-c to send.")
4174 (defvar anything-no-dump-variables
4175 '(anything-candidate-buffer-alist
4176 anything-digit-overlays
4177 anything-help-message
4178 anything-candidate-cache
4180 "Variables not to dump in bug report.")
4182 (defun anything-dumped-variables-in-bug-report ()
4183 (let ((hash (make-hash-table)))
4184 (loop for var in (apropos-internal "anything-" 'boundp)
4185 for vname = (symbol-name var)
4186 unless (or (string-match "-map$" vname)
4187 (string-match "^anything-c-source-" vname)
4188 (string-match "-hash$" vname)
4189 (string-match "-face$" vname)
4190 (memq var anything-no-dump-variables))
4191 collect var)))
4193 (defun anything-send-bug-report ()
4194 "Send a bug report of anything.el."
4195 (interactive)
4196 (with-current-buffer (or anything-last-buffer
4197 (current-buffer))
4198 (reporter-submit-bug-report
4199 anything-maintainer-mail-address
4200 "anything.el"
4201 (anything-dumped-variables-in-bug-report)
4202 nil nil
4203 anything-bug-report-salutation)))
4205 (defun anything-send-bug-report-from-anything ()
4206 "Send a bug report of anything.el in anything session."
4207 (interactive)
4208 (anything-run-after-quit 'anything-send-bug-report))
4210 ;; Debugging function.
4211 (defun* anything-test-candidates
4212 (sources &optional (input "")
4213 (compile-source-functions
4214 anything-compile-source-functions-default))
4215 "Test helper function for anything.
4216 Given pseudo `anything-sources' and `anything-pattern', returns list like
4217 ((\"source name1\" (\"candidate1\" \"candidate2\"))
4218 (\"source name2\" (\"candidate3\" \"candidate4\")))"
4219 (let ((anything-test-mode t)
4220 anything-enable-shortcuts
4221 anything-candidate-cache
4222 (anything-compile-source-functions compile-source-functions)
4223 anything-before-initialize-hook
4224 anything-after-initialize-hook
4225 anything-update-hook
4226 anything-test-candidate-list)
4227 (get-buffer-create anything-buffer)
4228 (anything-initialize nil input sources)
4229 (anything-update)
4230 ;; test-mode spec: select 1st candidate!
4231 (with-current-buffer anything-buffer
4232 (forward-line 1)
4233 (anything-mark-current-line))
4234 (prog1
4235 anything-test-candidate-list
4236 (anything-cleanup))))
4239 ;; (@* "Unit Tests")
4240 ;; See developer-tools/unit-test-anything.el
4242 (provide 'anything)
4244 ;; How to save (DO NOT REMOVE!!)
4245 ;; (progn (magit-push) (emacswiki-post "anything.el"))
4247 ;; Local Variables:
4248 ;; coding: utf-8
4249 ;; End:
4251 ;;; anything.el ends here