* anything-config.el (anything-M-x): Fall back to *comp-read to add must-match functi...
[anything-config.git] / anything.el
blob3984fc3995f313cfa527704308c304e2aa904929
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.
383 ;; You can extend `anything' by writing plug-ins. As soon as
384 ;; `anything' is invoked, `anything-sources' is compiled into basic
385 ;; attributes, then compiled one is used during invocation.
387 ;; The oldest built-in plug-in is `type' attribute: appends
388 ;; appropriate element of `anything-type-attributes'. Second built-in
389 ;; plug-in is `candidates-in-buffer': selecting a line from candidates
390 ;; buffer.
392 ;; To write a plug-in:
393 ;; 1. Define a compiler: anything-compile-source--*
394 ;; 2. Add compier function to `anything-compile-source-functions'.
395 ;; 3. (optional) Write helper functions.
397 ;; Anything plug-ins are found in the EmacsWiki.
399 ;; http://www.emacswiki.org/cgi-bin/emacs/AnythingPlugins
401 ;; Tested on Emacs 22/23/24.
404 ;; Thanks to Vagn Johansen for ideas.
405 ;; Thanks to Stefan Kamphausen for fixes and XEmacs support.
406 ;; Thanks to Tassilo Horn for fixes.
407 ;; Thanks to Drew Adams for various fixes
408 ;; Thanks to IMAKADO for candidates-in-buffer idea.
409 ;; Thanks to Tomohiro MATSUYAMA for multiline patch.
412 ;;; (@* "Index")
414 ;; If you have library `linkd.el', load
415 ;; `linkd.el' and turn on `linkd-mode' now. It lets you easily
416 ;; navigate around the sections Linkd mode will
417 ;; highlight this Index. You can get `linkd.el' here:
418 ;; http://www.emacswiki.org/cgi-bin/wiki/download/linkd.el
422 ;;; (@* "Tips")
425 ;; `anything' accepts keyword arguments. See docstring.
426 ;; [EVAL IT] (describe-function 'anything)
429 ;; `anything-enable-shortcuts' enables us to select candidate easily.
430 ;; If 'prefix then they can be selected using <prefix-key> <alnum>.
431 ;; The prefix key is `anything-select-with-prefix-shortcut'.
432 ;; If the <prefix-key> is a letter, pressing twice inputs the letter itself.
433 ;; e.g.
434 ;; (setq anything-enable-shortcuts 'prefix)
435 ;; (define-key anything-map \"@\" 'anything-select-with-prefix-shortcut)
438 ;; You can edit current selection using `anything-edit-current-selection'.
439 ;; It is useful after persistent-action.
442 ;; For `anything' users, setting `anything-sources' directly and
443 ;; invoke M-x anything is obsolete way for now. Try M-x
444 ;; `anything-migrate-sources'!
447 ;; If you want to create anything sources, yasnippet would help you.
448 ;; http://yasnippet.googlecode.com/
450 ;; Then get the snippet from
451 ;; http://www.emacswiki.org/cgi-bin/wiki/download/anything-source.yasnippet
453 ;; Put it in ~/.emacs.d/plugins/yasnippet/snippets/text-mode/emacs-lisp-mode/
457 ;; `anything-interpret-value' is useful function to interpret value
458 ;; like `candidates' attribute.
460 ;; (anything-interpret-value "literal") ; => "literal"
461 ;; (anything-interpret-value (lambda () "lambda")) ; => "lambda"
462 ;; (let ((source '((name . "lambda with source name"))))
463 ;; (anything-interpret-value
464 ;; (lambda () anything-source-name)
465 ;; source)) ; => "lambda with source name"
466 ;; (flet ((f () "function symbol"))
467 ;; (anything-interpret-value 'f)) ; => "function symbol"
468 ;; (let ((v "variable symbol"))
469 ;; (anything-interpret-value 'v)) ; => "variable symbol"
470 ;; (anything-interpret-value 'unbounded-1) ; error
473 ;; Now symbols are acceptable as candidates. So you do not have to use
474 ;; `symbol-name' function. The source is much simpler. For example,
475 ;; `apropos-internal' returns a list of symbols.
477 ;; (anything
478 ;; '(((name . "Commands")
479 ;; (candidates . (lambda () (apropos-internal anything-pattern 'commandp)))
480 ;; (volatile)
481 ;; (action . describe-function))))
484 ;; To mark a candidate, press C-SPC as normal Emacs marking. To go to
485 ;; marked candidate, press M-[ or M-].
488 ;; `anything-map' is now Emacs-standard key bindings by default.
490 ;; There are many `anything' applications, using `anything' for
491 ;; selecting candidate. In this case, if there is one candidate or no
492 ;; candidate, popping up *anything* buffer is irritating. If one
493 ;; candidate, you want to select it at once. If no candidate, you want
494 ;; to quit `anything'. Set `anything-execute-action-at-once-if-one'
495 ;; and `anything-quit-if-no-candidate' to non-nil to remedy it. Note
496 ;; that setting these variables GLOBALLY is bad idea because of
497 ;; delayed sources. These are meant to be let-binded.
498 ;; See anything-etags.el for example.
500 ;; [EVAL IT] (install-elisp "http://www.emacswiki.org/cgi-bin/wiki/download/anything-etags.el")
502 ;; ex.
503 ;; (let ((anything-execute-action-at-once-if-one t)
504 ;; (anything-quit-if-no-candidate (lambda () (message "No candidate"))))
505 ;; (anything temporary-sources input))
508 ;; `set-frame-configuration' arises flickering. If you hate
509 ;; flickering, eval:
510 ;; (setq anything-save-configuration-functions
511 ;; '(set-window-configuration . current-window-configuration))
512 ;; at the cost of restoring frame configuration (only window configuration).
515 ;; `anything-delete-current-selection' deletes the current line.
516 ;; It is useful when deleting a candidate in persistent action.
517 ;; eg. `kill-buffer'.
519 ;; [EVAL IT] (describe-function 'anything-delete-current-selection)
522 ;; `anything-attr' gets the attribute. `anything-attrset' sets the
523 ;; attribute. `anything-attr-defined' tests whether the attribute is
524 ;; defined. They handles source-local variables.
526 ;; [EVAL IT] (describe-function 'anything-attr)
527 ;; [EVAL IT] (describe-function 'anything-attrset)
528 ;; [EVAL IT] (describe-function 'anything-attr-defined)
531 ;; `anything-sources' accepts many attributes to make your life easier.
532 ;; Now `anything-sources' accepts a list of symbols.
534 ;; [EVAL IT] (describe-variable 'anything-sources)
537 ;; `anything' has optional arguments. Now you do not have to let-bind
538 ;; `anything-sources'.
540 ;; [EVAL IT] (describe-function 'anything)
543 ;; `anything-resume' resumes last `anything' session. Now you do not
544 ;; have to retype pattern.
546 ;; [EVAL IT] (describe-function 'anything-resume)
549 ;; `anything-execute-persistent-action' executes action without
550 ;; quitting `anything'. When popping up a buffer in other window by
551 ;; persistent action, you can scroll with `anything-scroll-other-window' and
552 ;; `anything-scroll-other-window-down'. See also `anything-sources' docstring.
554 ;; [EVAL IT] (describe-function 'anything-execute-persistent-action)
555 ;; [EVAL IT] (describe-variable 'anything-sources)
558 ;; `anything-select-2nd-action', `anything-select-3rd-action' and
559 ;; `anything-select-4th-action' select other than default action
560 ;; without pressing Tab.
563 ;; Using `anything-candidate-buffer' and the candidates-in-buffer
564 ;; attribute is much faster than traditional "candidates and match"
565 ;; way. And `anything-current-buffer-is-modified' avoids to
566 ;; recalculate candidates for unmodified buffer. See docstring of
567 ;; them.
569 ;; [EVAL IT] (describe-function 'anything-candidate-buffer)
570 ;; [EVAL IT] (describe-function 'anything-candidates-in-buffer)
571 ;; [EVAL IT] (describe-function 'anything-current-buffer-is-modified)
574 ;; `anything-current-buffer' and `anything-buffer-file-name' stores
575 ;; `(current-buffer)' and `buffer-file-name' in the buffer `anything'
576 ;; is invoked. Use them freely.
578 ;; [EVAL IT] (describe-variable 'anything-current-buffer)
579 ;; [EVAL IT] (describe-variable 'anything-buffer-file-name)
582 ;; `anything-completing-read' and `anything-read-file-name' are
583 ;; experimental implementation. If you are curious, type M-x
584 ;; anything-read-string-mode. It is a minor mode and toggles on/off.
587 ;; Use `anything-test-candidates' to test your handmade anything
588 ;; sources. It simulates contents of *anything* buffer with pseudo
589 ;; `anything-sources' and `anything-pattern', without side-effect. So
590 ;; you can unit-test your anything sources! Let's TDD!
592 ;; [EVAL IT] (describe-function 'anything-test-candidates)
594 ;; There are many unit-testing framework in Emacs Lisp. See the EmacsWiki.
595 ;; http://www.emacswiki.org/cgi-bin/emacs/UnitTesting
597 ;; There is an unit-test by Emacs Lisp Expectations at the tail of this file.
598 ;; http://www.emacswiki.org/cgi-bin/wiki/download/el-expectations.el
599 ;; http://www.emacswiki.org/cgi-bin/wiki/download/el-mock.el
602 ;; If you want to create anything sources, see anything-config.el.
603 ;; It is huge collection of sources. You can learn from examples.
606 ;; (@* "TODO")
608 ;; - process status indication
610 ;; - async sources doesn't honor digit-shortcut-count
612 ;; - anything-candidate-number-limit can't be nil everywhere
614 ;; (@* "HISTORY")
616 ;; Change log of this file is found at
617 ;; http://repo.or.cz/w/anything-config.git/history/master:/anything.el
619 ;; Change log of this project is found at
620 ;; http://repo.or.cz/w/anything-config.git?a=shortlog
622 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
625 ;;; Code:
627 (require 'cl)
629 (defvar anything-version "1.3.5")
631 ;; (@* "User Configuration")
634 ;;; Variables
637 ;; [DEPRECATED]
638 ;; A default value is provided in anything-config.el
639 (defvar anything-sources nil
640 "A list of sources to use with `anything'.
641 It is deprecated, you should not use this.
642 Use instead individual sources or list of sources of your choice.")
644 ;; Default values are provided in anything-config.el.
645 (defvar anything-type-attributes nil
646 "It's a list of \(TYPE ATTRIBUTES ...\).
647 ATTRIBUTES are the same as attributes for `anything-sources'.
648 TYPE connects the value to the appropriate sources.
649 Don't set this directly, use instead `define-anything-type-attribute'.
651 This allows specifying common attributes for several sources.
652 For example, sources which provide files can specify
653 common attributes with a `file' type.")
655 (defvaralias 'anything-enable-digit-shortcuts 'anything-enable-shortcuts
656 "Same as `anything-enable-shortcuts'.
657 Alphabet shortcuts are available now in `anything-enable-shortcuts'.
658 `anything-enable-digit-shortcuts' is retained for compatibility.")
660 (defvar anything-enable-shortcuts nil
661 "*Whether to use digit/alphabet shortcut to select the first nine matches.
662 If t then they can be selected using Ctrl+<number>.
664 If 'prefix then they can be selected using <prefix-key> <alnum>.
665 The prefix key is `anything-select-with-prefix-shortcut'.
666 If the <prefix-key> is a letter, pressing twice inputs the letter itself.
667 e.g.
668 (setq anything-enable-shortcuts 'prefix)
669 (define-key anything-map \"@\" 'anything-select-with-prefix-shortcut)
671 If 'alphabet then they can be selected using Shift+<alphabet> (deprecated).
672 It is not recommended because you cannot input capital letters in pattern.
674 Keys (digit/alphabet) are listed in `anything-shortcut-keys-alist'.")
676 (defvar anything-shortcut-keys-alist
677 '((alphabet . "asdfghjklzxcvbnmqwertyuiop")
678 (prefix . "asdfghjklzxcvbnmqwertyuiop1234567890")
679 (t . "123456789")))
681 (defvar anything-display-source-at-screen-top t
682 "*Display candidates at the top of screen.
683 This happen when using `anything-next-source' and `anything-previous-source'.")
685 (defvar anything-candidate-number-limit 50
686 "*Limit candidate number globally.
687 Do not show more candidates than this limit from individual sources.
688 It is usually pointless to show hundreds of matches
689 when the pattern is empty, because it is much simpler to type a
690 few characters to narrow down the list of potential candidates.
692 Set it to nil if you don't want this limit.")
694 (defvar anything-idle-delay 0.3
695 "*Be idle for this many seconds, before updating in delayed sources.
696 This is useful for sources involving heavy operations
697 \(like launching external programs\), so that candidates
698 from the source are not retrieved unnecessarily if the user keeps typing.
700 It also can be used to declutter the results anything displays,
701 so that results from certain sources are not shown with every
702 character typed, only if the user hesitates a bit.")
704 (defvar anything-input-idle-delay 0.3
705 "Be idle for this many seconds, before updating.
707 Unlike `anything-idle-delay', it is also effective for non-delayed sources.
708 If nil, candidates are collected immediately.
710 Note: If this value is too low compared to `anything-idle-delay',
711 you may have duplicated sources when using multiples sources.
712 Safe value is always >= `anything-idle-delay'.
713 Default settings are equal value for both.")
715 (defvar anything-samewindow nil
716 "Use current window to show the candidates.
717 If t then Anything doesn't pop up a new window.")
719 (defvar anything-source-filter nil
720 "A list of source names to be displayed.
721 Other sources won't appear in the search results.
722 If nil then there is no filtering.
723 See also `anything-set-source-filter'.")
726 (defvar anything-map
727 (let ((map (copy-keymap minibuffer-local-map)))
728 (define-key map (kbd "<down>") 'anything-next-line)
729 (define-key map (kbd "<up>") 'anything-previous-line)
730 (define-key map (kbd "C-n") 'anything-next-line)
731 (define-key map (kbd "C-p") 'anything-previous-line)
732 (define-key map (kbd "<prior>") 'anything-previous-page)
733 (define-key map (kbd "<next>") 'anything-next-page)
734 (define-key map (kbd "M-v") 'anything-previous-page)
735 (define-key map (kbd "C-v") 'anything-next-page)
736 (define-key map (kbd "M-<") 'anything-beginning-of-buffer)
737 (define-key map (kbd "M->") 'anything-end-of-buffer)
738 (define-key map (kbd "C-g") 'anything-keyboard-quit)
739 (define-key map (kbd "<right>") 'anything-next-source)
740 (define-key map (kbd "<left>") 'anything-previous-source)
741 (define-key map (kbd "<RET>") 'anything-exit-minibuffer)
742 (define-key map (kbd "C-1") 'anything-select-with-digit-shortcut)
743 (define-key map (kbd "C-2") 'anything-select-with-digit-shortcut)
744 (define-key map (kbd "C-3") 'anything-select-with-digit-shortcut)
745 (define-key map (kbd "C-4") 'anything-select-with-digit-shortcut)
746 (define-key map (kbd "C-5") 'anything-select-with-digit-shortcut)
747 (define-key map (kbd "C-6") 'anything-select-with-digit-shortcut)
748 (define-key map (kbd "C-7") 'anything-select-with-digit-shortcut)
749 (define-key map (kbd "C-8") 'anything-select-with-digit-shortcut)
750 (define-key map (kbd "C-9") 'anything-select-with-digit-shortcut)
751 (loop for c from ?A to ?Z do
752 (define-key map (make-string 1 c) 'anything-select-with-digit-shortcut))
753 (define-key map (kbd "C-i") 'anything-select-action)
754 (define-key map (kbd "C-z") 'anything-execute-persistent-action)
755 (define-key map (kbd "C-e") 'anything-select-2nd-action-or-end-of-line)
756 (define-key map (kbd "C-j") 'anything-select-3rd-action)
757 (define-key map (kbd "C-o") 'anything-next-source)
758 (define-key map (kbd "C-M-v") 'anything-scroll-other-window)
759 (define-key map (kbd "M-<next>") 'anything-scroll-other-window)
760 (define-key map (kbd "C-M-y") 'anything-scroll-other-window-down)
761 (define-key map (kbd "C-M-S-v") 'anything-scroll-other-window-down)
762 (define-key map (kbd "M-<prior>") 'anything-scroll-other-window-down)
763 (define-key map (kbd "<C-M-down>") 'anything-scroll-other-window)
764 (define-key map (kbd "<C-M-up>") 'anything-scroll-other-window-down)
765 (define-key map (kbd "C-SPC") 'anything-toggle-visible-mark)
766 (define-key map (kbd "M-[") 'anything-prev-visible-mark)
767 (define-key map (kbd "M-]") 'anything-next-visible-mark)
768 (define-key map (kbd "C-k") 'anything-delete-minibuffer-contents)
770 (define-key map (kbd "C-r") 'undefined)
771 (define-key map (kbd "C-t") 'anything-toggle-resplit-window)
772 (define-key map (kbd "C-}") 'anything-narrow-window)
773 (define-key map (kbd "C-{") 'anything-enlarge-window)
775 (define-key map (kbd "C-c C-d") 'anything-delete-current-selection)
776 (define-key map (kbd "C-c C-y") 'anything-yank-selection)
777 (define-key map (kbd "C-c C-k") 'anything-kill-selection-and-quit)
778 (define-key map (kbd "C-c C-f") 'anything-follow-mode)
779 (define-key map (kbd "C-c C-u") 'anything-force-update)
780 (define-key map (kbd "M-p") 'previous-history-element)
781 (define-key map (kbd "M-n") 'next-history-element)
782 ;; Debugging command
783 (define-key map "\C-c\C-x\C-d" 'anything-debug-output)
784 (define-key map "\C-c\C-x\C-m" 'anything-display-all-visible-marks)
785 (define-key map "\C-c\C-x\C-b" 'anything-send-bug-report-from-anything)
786 ;; Use `describe-mode' key in `global-map'.
787 (define-key map [f1] nil) ; Allow to eval keymap whithout errors.
788 (dolist (k (where-is-internal 'describe-mode global-map))
789 (define-key map k 'anything-help))
790 map)
791 "Keymap for anything.")
794 (defgroup anything nil
795 "Open anything."
796 :prefix "anything-" :group 'convenience)
798 (defface anything-header
799 '((t (:inherit header-line)))
800 "Face for header lines in the anything buffer."
801 :group 'anything)
803 (defvar anything-header-face 'anything-header
804 "*Face for header lines in the anything buffer.")
806 (defface anything-candidate-number
807 '((t (:background "Yellow" :foreground "black")))
808 "Face for candidate number in mode-line." :group 'anything)
810 (defvar anything-selection-face 'highlight
811 "*Face for currently selected item.")
813 (defvar anything-buffer "*anything*"
814 "Buffer showing completions.")
816 (defvar anything-action-buffer "*anything action*"
817 "Buffer showing actions.")
819 (defvar anything-selection-overlay nil
820 "Overlay used to highlight the currently selected item.")
822 (defvar anything-digit-overlays nil
823 "Overlays for digit shortcuts. See `anything-enable-shortcuts'.")
825 (defvar anything-candidate-cache nil
826 "Holds the available candidate withing a single anything invocation.")
828 (defvar anything-pattern
829 "The input pattern used to update the anything buffer.")
831 (defvar anything-input
832 "The input typed in the candidates panel.")
834 (defvar anything-async-processes nil
835 "List of information about asynchronous processes managed by anything.")
837 (defvar anything-digit-shortcut-count 0
838 "Number of digit shortcuts shown in the anything buffer.")
840 (defvar anything-before-initialize-hook nil
841 "Run before anything initialization.
842 This hook is run before init functions in `anything-sources'.")
844 (defvar anything-after-initialize-hook nil
845 "Run after anything initialization.
846 Global variables are initialized and the anything buffer is created.
847 But the anything buffer has no contents.")
849 (defvar anything-update-hook nil
850 "Run after the anything buffer was updated according the new input pattern.
851 This hook is run at the beginning of buffer.
852 The first candidate is selected after running this hook.
853 See also `anything-after-update-hook'.")
855 (defvar anything-after-update-hook nil
856 "Run after the anything buffer was updated according the new input pattern.
857 This is very similar to `anything-update-hook' but selection is not moved.
858 It is useful to select a particular object instead of the first one.")
860 (defvar anything-cleanup-hook nil
861 "Run after anything minibuffer is closed.
862 IOW this hook is executed BEFORE performing action.")
864 (defvar anything-select-action-hook nil
865 "Run when opening the action buffer.")
867 (defvar anything-before-action-hook nil
868 "Run before executing action.
869 Contrarily to `anything-cleanup-hook',
870 this hook run before anything minibuffer is closed
871 and before performing action.")
873 (defvar anything-after-action-hook nil
874 "Run after executing action.")
876 (defvar anything-after-persistent-action-hook nil
877 "Run after executing persistent action.")
879 (defvar anything-move-selection-before-hook nil
880 "Run before moving selection in `anything-buffer'.")
882 (defvar anything-move-selection-after-hook nil
883 "Run after moving selection in `anything-buffer'.")
885 (defvar anything-restored-variables
886 '(anything-candidate-number-limit
887 anything-source-filter
888 anything-source-in-each-line-flag
889 anything-map
890 anything-sources)
891 "Variables which are restored after `anything' invocation.")
893 (defvar anything-saved-selection nil
894 "Value of the currently selected object when the action list is shown.")
896 (defvar anything-current-prefix-arg nil
897 "Record `current-prefix-arg' when exiting minibuffer.")
899 (defvar anything-candidate-separator
900 "--------------------"
901 "Candidates separator of `multiline' source.")
903 (defvar anything-current-buffer nil
904 "Current buffer when `anything' is invoked.")
906 (defvar anything-buffer-file-name nil
907 "Variable `buffer-file-name' when `anything' is invoked.")
909 (defvar anything-saved-action nil
910 "Saved value of the currently selected action by key.")
912 (defvar anything-last-sources nil
913 "OBSOLETE!! Sources of previously invoked `anything'.")
915 (defvar anything-saved-current-source nil
916 "Value of the current source when the action list is shown.")
918 (defvar anything-compiled-sources nil
919 "Compiled version of `anything-sources'.")
921 (defvar anything-in-persistent-action nil
922 "Flag whether in persistent-action or not.")
924 (defvar anything-quick-update nil
925 "If non-nil, suppress displaying sources which are out of screen at first.
926 They are treated as delayed sources at this input.
927 This flag makes `anything' a bit faster with many sources.")
929 (defvar anything-last-sources-local nil
930 "Buffer local value of `anything-sources'.")
932 (defvar anything-last-buffer nil
933 "`anything-buffer' of previously `anything' session.")
935 (defvar anything-save-configuration-functions
936 '(set-window-configuration . current-window-configuration)
937 "The functions used to restore/save window or frame configurations.
938 It is a pair where the car is the function to restore window or frame config,
939 and the cdr is the function to save the window or frame config.
941 If you want to save and restore frame configuration, set this variable to
942 '\(set-frame-configuration . current-frame-configuration\)
944 Older version saves/restores frame configuration, but the default is changed now
945 because flickering can occur in some environment. ")
947 (defvar anything-persistent-action-use-special-display nil
948 "If non-nil, use `special-display-function' in persistent action.")
950 (defvar anything-execute-action-at-once-if-one nil
951 "Execute default action and exit when only one candidate is remaining.
952 It is useful for `anything' applications.")
954 (defvar anything-quit-if-no-candidate nil
955 "Quit when there is no candidates when non--nil.
956 This variable accepts a function, which is executed if no candidate.
958 It is useful for `anything' applications.")
960 (defvar anything-scroll-amount nil
961 "Scroll amount when scrolling other window in an anything session.
962 It is used by `anything-scroll-other-window'
963 and `anything-scroll-other-window-down'.
965 If you prefer scrolling line by line, set this value to 1.")
967 (defvar anything-display-function 'anything-default-display-buffer
968 "Function to display *anything* buffer.
969 It is `anything-default-display-buffer' by default,
970 which affects `anything-samewindow'.")
972 (defvar anything-delayed-init-executed nil)
974 (defvar anything-mode-line-string "\\<anything-map>\\[anything-help]:help \
975 \\[anything-select-action]:Acts \
976 \\[anything-exit-minibuffer]/\\[anything-select-2nd-action-or-end-of-line]/\
977 \\[anything-select-3rd-action]:NthAct \
978 \\[anything-send-bug-report-from-anything]:BugReport"
979 "Help string displayed in mode-line in `anything'.
980 It can be a string or a list of two args, in this case,
981 first arg is a string that will be used as name for candidates number,
982 second arg any string to display in mode line.
983 If nil, use default `mode-line-format'.")
985 (defvar anything-help-message
986 "\\<anything-map>The keys that are defined for `anything' are:
987 \\{anything-map}"
988 "Detailed help message string for `anything'.
989 It also accepts function or variable symbol.")
991 ;; FIXME: What is this
992 ;(put 'anything 'timid-completion 'disabled)
994 (defvar anything-source-in-each-line-flag nil
995 "Non-nil means add anything-source text-property in each candidate.
996 experimental feature.")
998 (defvaralias 'anything-debug-variables 'anything-debug-forms)
1000 (defvar anything-debug-forms nil
1001 "Forms to show in `anything-debug-output'.
1002 Otherwise all variables started with `anything-' are shown.
1003 It is useful for debug.")
1005 (defvar anything-debug nil
1006 "If non-nil, write log message into *Anything Log* buffer.
1007 If `debug-on-error' is non-nil, write log message regardless of this variable.
1008 It is disabled by default because *Anything Log* grows quickly.")
1011 ;; (@* "Internal Variables")
1012 (defvar anything-test-candidate-list nil)
1013 (defvar anything-test-mode nil)
1014 (defvar anything-source-name nil)
1015 (defvar anything-candidate-buffer-alist nil)
1016 (defvar anything-check-minibuffer-input-timer nil)
1017 (defvar anything-match-hash (make-hash-table :test 'equal))
1018 (defvar anything-cib-hash (make-hash-table :test 'equal))
1019 (defvar anything-tick-hash (make-hash-table :test 'equal))
1020 (defvar anything-issued-errors nil)
1021 (defvar anything-shortcut-keys nil)
1022 (defvar anything-once-called-functions nil)
1023 (defvar anything-follow-mode nil)
1024 (defvar anything-let-variables nil)
1025 (defvar anything-split-window-state nil)
1026 (defvar anything-selection-point nil)
1029 ;; (@* "Utility: logging")
1030 (defun anything-log (format-string &rest args)
1031 "Log message if `debug-on-error' or `anything-debug' is non-nil.
1032 Messages are written to the *Anything Log* buffer.
1034 Argument FORMAT-STRING is a string to use with `format'.
1035 Use optional arguments ARGS like in `format'."
1036 (when (or debug-on-error anything-debug)
1037 (with-current-buffer (get-buffer-create "*Anything Log*")
1038 (buffer-disable-undo)
1039 (set (make-local-variable 'inhibit-read-only) t)
1040 (goto-char (point-max))
1041 (insert (let ((tm (current-time)))
1042 (format "%s.%06d (%s) %s\n"
1043 (format-time-string "%H:%M:%S" tm)
1044 (nth 2 tm)
1045 (anything-log-get-current-function)
1046 (apply #'format (cons format-string args))))))))
1048 (defmacro anything-log-eval (&rest exprs)
1049 "Write each EXPRS evaluation result to the *Anything Log* buffer."
1050 `(anything-log-eval-internal ',exprs))
1052 (defun anything-log-run-hook (hook)
1053 "Run HOOK like `run-hooks' but write these actions to anything log buffer."
1054 (anything-log "executing %s" hook)
1055 (when (boundp hook)
1056 (anything-log-eval (symbol-value hook))
1057 (anything-log-eval (default-value hook)))
1058 (run-hooks hook)
1059 (anything-log "executed %s" hook))
1061 (defun anything-log-eval-internal (exprs)
1062 "Eval EXPRS and write results to anything log buffer."
1063 (dolist (expr exprs)
1064 (condition-case err
1065 (anything-log "%S = %S" expr (eval expr))
1066 (error (anything-log "%S = ERROR!" expr)))))
1068 (defun anything-log-get-current-function ()
1069 "Get function name calling `anything-log'.
1070 The original idea is from `tramp-debug-message'."
1071 (loop with exclude-func-re = "^anything-\\(?:interpret\\|log\\|.*funcall\\)"
1072 for btn from 1 to 40 ;avoid inf-loop
1073 for btf = (second (backtrace-frame btn))
1074 for fn = (if (symbolp btf) (symbol-name btf) "")
1075 if (and (string-match "^anything" fn)
1076 (not (string-match exclude-func-re fn)))
1077 return fn))
1079 (defun anything-log-error (&rest args)
1080 "Accumulate error messages into `anything-issued-errors'.
1081 ARGS are args given to `format'."
1082 (apply 'anything-log (concat "ERROR: " (car args)) (cdr args))
1083 (let ((msg (apply 'format args)))
1084 (unless (member msg anything-issued-errors)
1085 (add-to-list 'anything-issued-errors msg))))
1087 (defvar anything-last-log-file nil)
1088 (defun anything-log-save-maybe ()
1089 "May be save log buffer to `anything-last-log-file'."
1090 (when (stringp anything-debug)
1091 (let ((logdir (expand-file-name (format-time-string "%Y%m%d")
1092 anything-debug)))
1093 (make-directory logdir t)
1094 (with-current-buffer (get-buffer-create "*Anything Log*")
1095 (write-region (point-min) (point-max)
1096 (setq anything-last-log-file
1097 (expand-file-name (format-time-string "%Y%m%d-%H%M%S")
1098 logdir))
1099 nil 'silent)
1100 (erase-buffer)))))
1102 (defun anything-open-last-log ()
1103 "Open anything log file of last anything session."
1104 (interactive)
1105 (if anything-last-log-file
1106 (view-file anything-last-log-file)
1107 (switch-to-buffer "*Anything Log*")))
1109 (defun anything-print-error-messages ()
1110 "Print error messages in `anything-issued-errors'."
1111 (message "%s" (mapconcat 'identity (reverse anything-issued-errors) "\n")))
1113 ;; (anything-log "test")
1114 ;; (switch-to-buffer-other-window "*Anything Log*")
1117 ;; (@* "Programming Tools")
1118 (defmacro anything-aif (test-form then-form &rest else-forms)
1119 "Like `if' but set the result of TEST-FORM in a temprary variable called `it'.
1120 THEN-FORM and ELSE-FORMS are then excuted just like in `if'."
1121 (declare (indent 2) (debug t))
1122 `(let ((it ,test-form))
1123 (if it ,then-form ,@else-forms)))
1125 (defun anything-mklist (obj)
1126 "If OBJ is a list \(but not lambda\), return itself.
1127 Otherwise make a list with one element."
1128 (if (and (listp obj) (not (functionp obj)))
1130 (list obj)))
1133 ;; (@* "Anything API")
1135 (defun anything-buffer-get ()
1136 "Return `anything-action-buffer' if shown otherwise `anything-buffer'."
1137 (if (anything-action-window)
1138 anything-action-buffer
1139 anything-buffer))
1141 (defun anything-window ()
1142 "Window of `anything-buffer'."
1143 (get-buffer-window (anything-buffer-get) 'visible))
1145 (defun anything-action-window ()
1146 "Window of `anything-action-buffer'."
1147 (get-buffer-window anything-action-buffer 'visible))
1149 (defmacro with-anything-window (&rest body)
1150 "Be sure BODY is excuted in the anything window."
1151 (declare (indent 0) (debug t))
1152 `(if anything-test-mode
1153 (with-current-buffer (anything-buffer-get)
1154 ,@body)
1155 (with-selected-window (anything-window)
1156 ,@body)))
1158 (defmacro with-anything-current-buffer (&rest body)
1159 "Eval BODY inside `anything-current-buffer'."
1160 (declare (indent 0) (debug t))
1161 `(with-current-buffer anything-current-buffer
1162 ,@body))
1164 (defmacro with-anything-restore-variables(&rest body)
1165 "Restore `anything-restored-variables' after executing BODY.
1166 `post-command-hook' is handled specially."
1167 (declare (indent 0) (debug t))
1168 `(let ((--orig-vars (mapcar (lambda (v)
1169 (cons v (symbol-value v)))
1170 anything-restored-variables))
1171 (--post-command-hook-pair (cons post-command-hook
1172 (default-value 'post-command-hook))))
1173 (setq post-command-hook '(t))
1174 (setq-default post-command-hook nil)
1175 (unwind-protect (progn ,@body)
1176 (loop for (var . value) in --orig-vars
1177 do (set var value))
1178 (setq post-command-hook (car --post-command-hook-pair))
1179 (setq-default post-command-hook (cdr --post-command-hook-pair))
1180 (anything-log "restore variables"))))
1182 (defun* anything-attr (attribute-name &optional
1183 (src (anything-get-current-source)))
1184 "Get the value of ATTRIBUTE-NAME of SRC.
1185 if SRC is omitted, use current source.
1186 It is useful to write your sources."
1187 (anything-aif (assq attribute-name src)
1188 (cdr it)))
1190 (defun* anything-attr* (attribute-name
1191 &optional (src (anything-get-current-source)))
1192 "Pass the value of ATTRIBUTE-NAME of SRC to `anything-interpret-value'.
1193 if SRC is omitted, use current source.
1194 It is useful to write your sources."
1195 (anything-interpret-value (anything-attr attribute-name src)))
1197 (defun* anything-attr-defined (attribute-name
1198 &optional (src (anything-get-current-source)))
1199 "Return non-nil if ATTRIBUTE-NAME of SRC is defined.
1200 if SRC is omitted, use current source.
1201 It is useful to write your sources."
1202 (and (assq attribute-name src) t))
1204 (defun* anything-attrset (attribute-name value
1205 &optional
1206 (src (anything-get-current-source)))
1207 "Set the value of ATTRIBUTE-NAME of SRC to VALUE.
1208 if SRC is omitted, use current source.
1209 It is useful to write your sources."
1210 (anything-aif (assq attribute-name src)
1211 (setcdr it value)
1212 (setcdr src (cons (cons attribute-name value) (cdr src))))
1213 value)
1215 (defun anything-set-source-filter (sources)
1216 "Set the value of `anything-source-filter' to SOURCES and update.
1218 This function sets a filter for anything sources and it may be
1219 called while anything is running. It can be used to toggle
1220 displaying of sources dinamically. For example, additional keys
1221 can be bound into `anything-map' to display only the file-related
1222 results if there are too many matches from other sources and
1223 you're after files only:
1225 Shift+F shows only file results from some sources:
1227 \(define-key anything-map \"F\" 'anything-my-show-files-only)
1229 \(defun anything-my-show-files-only ()
1230 (interactive)
1231 (anything-set-source-filter '(\"File Name History\"
1232 \"Files from Current Directory\")))
1234 Shift+A shows all results:
1236 \(define-key anything-map \"A\" 'anything-my-show-all)
1238 \(defun anything-my-show-all ()
1239 (interactive)
1240 (anything-set-source-filter nil))
1242 Note that you have to prefix the functions with anything- prefix,
1243 otherwise they won't be bound when Anything is used under
1244 Iswitchb. The -my- part is added to avoid collisions with
1245 existing Anything function names."
1246 (unless (and (listp sources)
1247 (loop for name in sources always (stringp name)))
1248 (error "Invalid data in `anything-set-source-filter': %S" sources))
1249 (setq anything-source-filter sources)
1250 (anything-log-eval anything-source-filter)
1251 (anything-update))
1253 (defun anything-set-sources (sources &optional no-init no-update)
1254 "Set SOURCES during `anything' invocation.
1255 If NO-INIT is non-nil, skip executing init functions of SOURCES.
1256 If NO-UPDATE is non-nil, skip executing `anything-update'."
1257 (with-current-buffer anything-buffer
1258 (setq anything-compiled-sources nil
1259 anything-sources sources
1260 anything-last-sources-local sources)
1261 (anything-log-eval anything-compiled-sources anything-sources))
1262 (unless no-init (anything-funcall-foreach 'init))
1263 (unless no-update (anything-update)))
1265 (defvar anything-compile-source-functions
1266 '(anything-compile-source--type
1267 anything-compile-source--dummy
1268 anything-compile-source--disable-shortcuts
1269 anything-compile-source--candidates-in-buffer)
1270 "Functions to compile elements of `anything-sources' (plug-in).")
1272 (defun anything-get-sources ()
1273 "Return compiled `anything-sources', which is memoized.
1275 Attributes:
1277 - type
1278 `anything-type-attributes' are merged in.
1279 - candidates-buffer
1280 candidates, volatile and match attrubute are created."
1281 (cond
1282 ;; action
1283 ((anything-action-window)
1284 anything-sources)
1285 ;; memoized
1286 (anything-compiled-sources)
1287 ;; first time
1289 (prog1
1290 (setq anything-compiled-sources
1291 (anything-compile-sources
1292 anything-sources anything-compile-source-functions))
1293 (anything-log-eval anything-compiled-sources)))))
1295 (defun* anything-get-selection (&optional (buffer nil buffer-s) (force-display-part))
1296 "Return the currently selected item or nil.
1297 if BUFFER is nil or unspecified, use anything-buffer as default value.
1298 If FORCE-DISPLAY-PART is non-nil, return the display string."
1299 (setq buffer (if (and buffer buffer-s) buffer anything-buffer))
1300 (unless (anything-empty-buffer-p buffer)
1301 (with-current-buffer buffer
1302 (let ((selection
1303 (or (and (not force-display-part)
1304 (get-text-property (overlay-start
1305 anything-selection-overlay)
1306 'anything-realvalue))
1307 (let ((disp (buffer-substring-no-properties
1308 (overlay-start anything-selection-overlay)
1309 (1- (overlay-end anything-selection-overlay))))
1310 (source (anything-get-current-source)))
1311 (anything-aif (and (not force-display-part)
1312 (assoc-default 'display-to-real source))
1313 (anything-funcall-with-source source it disp)
1314 disp)))))
1315 (unless (equal selection "")
1316 (anything-log-eval selection)
1317 selection)))))
1319 (defun anything-get-action ()
1320 "Return the associated action for the selected candidate.
1321 It is a function symbol \(sole action\) or list
1322 of \(action-display . function\)."
1323 (unless (anything-empty-buffer-p (anything-buffer-get))
1324 (anything-aif (anything-attr 'action-transformer)
1325 (anything-composed-funcall-with-source
1326 (anything-get-current-source) it
1327 (anything-attr 'action) (anything-get-selection))
1328 (anything-attr 'action))))
1330 (defun anything-get-current-source ()
1331 "Return the source for the current selection.
1332 Use it in init, candidates, action, candidate-transformer,
1333 filtered-candidate-transformer functions."
1334 (declare (special source))
1335 ;; The name `anything-get-current-source' should be used in init function etc.
1336 (if (and (boundp 'anything-source-name) (stringp anything-source-name))
1337 source
1338 (with-current-buffer (anything-buffer-get)
1339 (or (get-text-property (point) 'anything-source)
1340 (block exit
1341 ;; This goto-char shouldn't be necessary, but point is moved to
1342 ;; point-min somewhere else which shouldn't happen.
1343 (goto-char (overlay-start anything-selection-overlay))
1344 (let* ((header-pos (or (anything-get-previous-header-pos)
1345 (anything-get-next-header-pos)))
1346 (source-name
1347 (save-excursion
1348 (unless header-pos
1349 ;(message "No candidates")
1350 (return-from exit nil))
1351 (goto-char header-pos)
1352 (anything-current-line-contents))))
1353 (some (lambda (source)
1354 (if (equal (assoc-default 'name source) source-name)
1355 source))
1356 (anything-get-sources))))))))
1358 (defun anything-buffer-is-modified (buffer)
1359 "Return non-nil when BUFFER is modified since `anything' was invoked."
1360 (let* ((b (get-buffer buffer))
1361 (key (concat (buffer-name b) "/" (anything-attr 'name)))
1362 (source-tick (or (gethash key anything-tick-hash) 0))
1363 (buffer-tick (buffer-chars-modified-tick b))
1364 (modifiedp (/= source-tick buffer-tick)))
1365 (puthash key buffer-tick anything-tick-hash)
1366 (anything-log-eval buffer modifiedp)
1367 modifiedp))
1369 (defun anything-current-buffer-is-modified ()
1370 "Check if `anything-current-buffer' is modified since `anything' was invoked."
1371 (anything-buffer-is-modified anything-current-buffer))
1373 (defvar anything-quit nil)
1374 (defun anything-run-after-quit (function &rest args)
1375 "Perform an action after quitting `anything'.
1376 The action is to call FUNCTION with arguments ARGS."
1377 (setq anything-quit t)
1378 (anything-log-eval function args)
1379 (apply 'run-with-idle-timer 0 nil function args)
1380 (anything-exit-minibuffer))
1383 (defun define-anything-type-attribute (type definition &optional doc)
1384 "Register type attribute of TYPE as DEFINITION with DOC.
1385 DOC is displayed in `anything-type-attributes' docstring.
1387 Use this function is better than setting `anything-type-attributes' directly."
1388 (loop for i in definition do
1389 ;; without `ignore-errors', error at emacs22
1390 (ignore-errors (setf i (delete nil i))))
1391 (anything-add-type-attribute type definition)
1392 (and doc (anything-document-type-attribute type doc))
1393 nil)
1395 (defvaralias 'anything-attributes 'anything-additional-attributes)
1396 (defvar anything-additional-attributes nil
1397 "List of all `anything' attributes.")
1398 (defun anything-document-attribute (attribute short-doc &optional long-doc)
1399 "Register ATTRIBUTE documentation introduced by plug-in.
1400 SHORT-DOC is displayed beside attribute name.
1401 LONG-DOC is displayed below attribute name and short documentation."
1402 (declare (indent 2) (debug t))
1403 (if long-doc
1404 (setq short-doc (concat "(" short-doc ")"))
1405 (setq long-doc short-doc
1406 short-doc ""))
1407 (add-to-list 'anything-additional-attributes attribute t)
1408 (put attribute 'anything-attrdoc
1409 (concat "- " (symbol-name attribute)
1410 " " short-doc "\n\n" long-doc "\n")))
1413 (defun anything-interpret-value (value &optional source)
1414 "Interpret VALUE as variable, function or literal.
1415 If VALUE is a function, call it with no arguments and return the value.
1416 If SOURCE is `anything' source, `anything-source-name' is source name.
1418 If VALUE is a variable, return the value.
1420 If VALUE is a symbol, but it is not a function or a variable, cause an error.
1422 Otherwise, return VALUE itself."
1423 (cond ((and source (functionp value))
1424 (anything-funcall-with-source source value))
1425 ((functionp value)
1426 (funcall value))
1427 ((and (symbolp value) (boundp value))
1428 (symbol-value value))
1429 ((symbolp value)
1430 (error "anything-interpret-value: Symbol must be a function or a variable"))
1432 value)))
1434 (defun anything-once (function &rest args)
1435 "Ensure FUNCTION with ARGS to be called once in `anything' session."
1436 (let ((spec (cons function args)))
1437 (unless (member spec anything-once-called-functions)
1438 (apply function args)
1439 (push spec anything-once-called-functions))))
1442 ;; (@* "Core: API helper")
1443 (defun* anything-empty-buffer-p (&optional (buffer anything-buffer))
1444 "Check if BUFFER have candidates.
1445 Default value for BUFFER is `anything-buffer'."
1446 (zerop (buffer-size (and buffer (get-buffer buffer)))))
1448 (defun anything-let-eval-varlist (varlist)
1449 "Return the list of pairs VARLIST with each cdr of pair evaluated.
1450 If VARLIST contain single elements, those are returned
1451 as a list of one element."
1452 (mapcar (lambda (pair)
1453 (if (listp pair)
1454 (cons (car pair) (eval (cadr pair)))
1455 (cons pair nil)))
1456 varlist))
1458 ;; [NOT USED]
1459 ;; (defun anything-let*-eval-varlist (varlist)
1460 ;; (let ((vars (mapcar (lambda (pair)
1461 ;; (or (car-safe pair) pair))
1462 ;; varlist)))
1463 ;; (eval `(let ,vars
1464 ;; ,@(mapcar (lambda (pair)
1465 ;; (if (listp pair)
1466 ;; `(setq ,(car pair) ,(cadr pair))
1467 ;; `(setq ,pair nil)))
1468 ;; varlist)
1469 ;; (mapcar (lambda (v)
1470 ;; (cons v (symbol-value v)))
1471 ;; ',vars)))))
1473 (defun anything-let-internal (binding bodyfunc)
1474 "Set BINDING to anything buffer-local variables and Evaluate BODYFUNC.
1475 BINDING is a list of \(VARNAME . VALUE\) pair."
1476 (setq anything-let-variables binding)
1477 (unwind-protect
1478 (funcall bodyfunc)
1479 (setq anything-let-variables nil)))
1482 ;; (@* "Core: tools")
1483 (defun anything-current-line-contents ()
1484 "Current line string without properties."
1485 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))
1487 (defun anything-funcall-with-source (source func &rest args)
1488 "Call from SOURCE FUNC list or single function FUNC with ARGS.
1489 FUNC can be a symbol or a list of functions.
1490 Return the result of last function call."
1491 (let ((anything-source-name (assoc-default 'name source))
1492 result)
1493 (anything-log-eval anything-source-name func args)
1494 (dolist (func (if (functionp func) (list func) func) result)
1495 (setq result (apply func args)))))
1497 (defun anything-funcall-foreach (sym)
1498 "Call the function SYM for each source if any."
1499 (dolist (source (anything-get-sources))
1500 (anything-aif (assoc-default sym source)
1501 (anything-funcall-with-source source it))))
1503 (defun anything-normalize-sources (sources)
1504 "If SOURCES is only one source, make a list of one element."
1505 (cond ((or (and sources
1506 (symbolp sources))
1507 (and (listp sources) (assq 'name sources)))
1508 (list sources))
1509 (sources)
1510 (t anything-sources)))
1512 (defun anything-approximate-candidate-number (&optional in-current-source)
1513 "Return approximate number of candidates in `anything-buffer'.
1514 If IN-CURRENT-SOURCE is provided return number of candidates
1515 in the source where point is.
1516 It is used to check if candidate number is 0, 1, or 2+."
1517 (with-current-buffer anything-buffer
1518 (save-excursion
1519 (if in-current-source
1520 (goto-char (anything-get-previous-header-pos))
1521 (goto-char (point-min)))
1522 (forward-line 1)
1523 (let ((count-multi 1))
1524 (if (anything-pos-multiline-p)
1525 (save-excursion
1526 (loop while (and (not (if in-current-source
1527 (save-excursion
1528 (forward-line 2)
1529 (or (anything-pos-header-line-p) (eobp)))
1530 (eobp)))
1531 (search-forward anything-candidate-separator nil t))
1532 do (incf count-multi)
1533 finally return count-multi))
1534 (save-excursion
1535 (loop with ln = 0
1536 while (not (if in-current-source
1537 (or (anything-pos-header-line-p) (eobp))
1538 (eobp)))
1539 unless (anything-pos-header-line-p)
1540 do (incf ln)
1541 do (forward-line 1) finally return ln)))))))
1543 (defmacro with-anything-quittable (&rest body)
1544 "If an error occur in execution of BODY, quit anything safely."
1545 (declare (indent 0) (debug t))
1546 `(let (inhibit-quit)
1547 (condition-case v
1548 (progn ,@body)
1549 (quit (setq anything-quit t)
1550 (exit-minibuffer)
1551 (keyboard-quit)))))
1553 (defun anything-compose (arg-lst func-lst)
1554 "Apply arguments specified in ARG-LST with each function of FUNC-LST.
1555 The result of each function will be the new `car' of ARG-LST.
1557 This function allows easy sequencing of transformer functions."
1558 (dolist (func func-lst)
1559 (setcar arg-lst (apply func arg-lst)))
1560 (car arg-lst))
1562 (defun anything-composed-funcall-with-source (source funcs &rest args)
1563 "With SOURCE apply `anything-funcall-with-source' with each FUNCS and ARGS.
1564 This is used in transformers to modify candidates list."
1565 (if (functionp funcs)
1566 (apply 'anything-funcall-with-source source funcs args)
1567 (apply 'anything-funcall-with-source source
1568 (lambda (&rest args)
1569 (anything-compose args funcs))
1570 args)))
1572 (defun anything-new-timer (variable timer)
1573 "Give VARIABLE value to TIMER and cancel old timer."
1574 (anything-aif (symbol-value variable)
1575 (cancel-timer it))
1576 (set variable timer))
1579 ;; (@* "Core: entry point")
1580 (defconst anything-argument-keys
1581 '(:sources :input :prompt :resume :preselect :buffer :keymap :default :history))
1583 ;;;###autoload
1584 (defun anything (&rest plist)
1585 "Main function to execute anything sources.
1587 Keywords supported:
1588 :sources :input :prompt :resume :preselect :buffer :keymap :default :history
1589 Extra keywords are supported and can be added, see below.
1591 When call interactively with no arguments deprecated `anything-sources'
1592 will be used if non--nil.
1594 PLIST is a list like \(:key1 val1 :key2 val2 ...\) or
1595 \(&optional sources input prompt resume preselect buffer keymap default history\).
1597 Basic keywords are the following:
1599 \:sources
1601 Temporary value of `anything-sources'. It also accepts a
1602 symbol, interpreted as a variable of an anything source. It
1603 also accepts an alist representing an anything source, which is
1604 detected by \(assq 'name ANY-SOURCES\)
1606 \:input
1608 Temporary value of `anything-pattern', ie. initial input of minibuffer.
1610 \:prompt
1612 Prompt other than \"pattern: \".
1614 \:resume
1616 If t, Resurrect previously instance of `anything'. Skip the initialization.
1617 If 'noresume, this instance of `anything' cannot be resumed.
1619 \:preselect
1621 Initially selected candidate. Specified by exact candidate or a regexp.
1622 Note that it is not working with delayed sources.
1624 \:buffer
1626 `anything-buffer' instead of *anything*.
1628 \:keymap
1630 `anything-map' for current `anything' session.
1632 \:default
1634 A default argument that will be inserted in minibuffer \
1635 with \\<minibuffer-local-map>\\[next-history-element].
1636 When nil of not present `thing-at-point' will be used instead.
1638 \:history
1640 By default all minibuffer input is pushed to `minibuffer-history',
1641 if an argument HISTORY is provided, input will be pushed to HISTORY.
1642 History element should be a symbol.
1644 Of course, conventional arguments are supported, the two are same.
1646 \(anything :sources sources :input input :prompt prompt :resume resume
1647 :preselect preselect :buffer buffer :keymap keymap :default default
1648 :history history\)
1649 \(anything sources input prompt resume preselect buffer keymap default history\)
1651 Other keywords are interpreted as local variables of this anything session.
1652 The `anything-' prefix can be omitted. For example,
1654 \(anything :sources 'anything-c-source-buffers
1655 :buffer \"*buffers*\" :candidate-number-limit 10\)
1657 means starting anything session with `anything-c-source-buffers'
1658 source in *buffers* buffer and set variable `anything-candidate-number-limit'
1659 to 10 as session local variable."
1660 (interactive)
1661 (if (keywordp (car plist))
1662 (anything-let-internal
1663 (anything-parse-keys plist)
1664 (lambda ()
1665 (apply 'anything
1666 (mapcar (lambda (key) (plist-get plist key))
1667 anything-argument-keys))))
1668 (apply 'anything-internal plist)))
1670 (defun anything-parse-keys (keys)
1671 "Parse the KEYS arguments of `anything'.
1672 Return only the keys that are not in `anything-argument-keys'.
1673 It is used to set local variables via `anything-let-internal'.
1674 This allow to add arguments that are not part of `anything-argument-keys',
1675 but are valid anything attributes.
1676 i.e :candidate-number-limit will be bound to `anything-candidate-number-limit'
1677 in source."
1678 ;; (anything-parse-keys '(:sources ((name . "test")
1679 ;; (candidates . (a b c)))
1680 ;; :buffer "toto"
1681 ;; :candidate-number-limit 4))
1682 ;; ==> ((anything-candidate-number-limit . 4))
1683 (loop for (key value &rest _) on keys by #'cddr
1684 for symname = (substring (symbol-name key) 1)
1685 for sym = (intern (if (string-match "^anything-" symname)
1686 symname
1687 (concat "anything-" symname)))
1688 unless (memq key anything-argument-keys)
1689 collect (cons sym value)))
1691 ;;; (@* "Core: entry point helper")
1692 (defun anything-internal (&optional
1693 any-sources any-input
1694 any-prompt any-resume
1695 any-preselect any-buffer
1696 any-keymap any-default any-history)
1697 "The internal anything function called by `anything'.
1698 For ANY-SOURCES ANY-INPUT ANY-PROMPT ANY-RESUME ANY-PRESELECT ANY-BUFFER and
1699 ANY-KEYMAP ANY-DEFAULT ANY-HISTORY See `anything'."
1700 (anything-log (concat "[Start session] " (make-string 41 ?+)))
1701 (anything-log-eval any-prompt any-preselect
1702 any-buffer any-keymap any-default)
1703 (unwind-protect
1704 (condition-case v
1705 (let (;; It is needed because `anything-source-name' is non-nil
1706 ;; when `anything' is invoked by action. Awful global scope.
1707 anything-source-name
1708 anything-in-persistent-action
1709 anything-quit
1710 (case-fold-search t)
1711 (anything-buffer (or any-buffer anything-buffer))
1712 ;; cua-mode ; avoid error when region is selected
1714 (with-anything-restore-variables
1715 (anything-initialize any-resume any-input any-sources)
1716 (anything-display-buffer anything-buffer)
1717 (anything-log "show prompt")
1718 (unwind-protect
1719 (anything-read-pattern-maybe
1720 any-prompt any-input any-preselect
1721 any-resume any-keymap any-default
1722 (when (and any-history (symbolp any-history)) any-history))
1723 (anything-cleanup)))
1724 (prog1 (unless anything-quit
1725 (anything-execute-selection-action-1))
1726 (anything-log (concat "[End session] " (make-string 41 ?-)))))
1727 (quit
1728 (anything-restore-position-on-quit)
1729 (anything-log (concat "[End session (quit)] " (make-string 34 ?-)))
1730 nil))
1731 (anything-log-save-maybe)))
1734 ;;; Anything resume
1737 (defun* anything-resume (&optional
1738 (any-buffer anything-last-buffer)
1739 buffer-pattern (any-resume t))
1740 "Resurrect previously invoked `anything'.
1741 Called with a prefix arg, allow choosing among all existing
1742 anything buffers. i.e choose among various anything sessions."
1743 (interactive)
1744 (when (or current-prefix-arg buffer-pattern)
1745 (setq any-buffer (anything-resume-select-buffer buffer-pattern)))
1746 (setq anything-compiled-sources nil)
1747 (anything
1748 :sources (or (buffer-local-value 'anything-last-sources-local (get-buffer any-buffer))
1749 anything-last-sources anything-sources)
1750 :input (buffer-local-value 'anything-input-local (get-buffer any-buffer))
1751 :resume any-resume
1752 :buffer any-buffer))
1754 ;;; rubikitch: experimental
1755 ;;; I use this and check it whether I am convenient.
1756 ;;; I may introduce an option to control the behavior.
1757 (defun* anything-resume-window-only (&optional
1758 (any-buffer anything-last-buffer)
1759 buffer-pattern)
1760 (interactive)
1761 (anything-resume any-buffer buffer-pattern 'window-only))
1763 (defun anything-resume-p (any-resume)
1764 "Whether current anything session is resumed or not.
1765 Just check if ANY-RESUME value is t or window-only."
1766 (memq any-resume '(t window-only)))
1768 (defun anything-resume-select-buffer (input)
1769 "Resume precedent anything session with initial input INPUT."
1770 (or (anything :sources '(((name . "Resume anything buffer")
1771 (candidates . anything-buffers)
1772 (action . identity)))
1773 :input input
1774 :resume 'noresume
1775 :buffer "*anything resume*")
1776 (keyboard-quit)))
1779 ;;;###autoload
1780 (defun anything-at-point (&optional
1781 any-sources any-input
1782 any-prompt any-resume
1783 any-preselect any-buffer)
1784 "Call anything with symbol at point as initial input.
1785 ANY-SOURCES ANY-INPUT ANY-PROMPT ANY-RESUME ANY-PRESELECT and ANY-BUFFER
1786 are same args as in `anything'."
1787 (interactive)
1788 (anything :sources any-sources
1789 :input (if current-prefix-arg
1790 (concat "\\b" (thing-at-point 'symbol) "\\b"
1791 (if (featurep 'anything-match-plugin) " " ""))
1792 any-input)
1793 :prompt any-prompt
1794 :resume any-resume
1795 :preselect any-preselect
1796 :buffer any-buffer))
1798 ;;;###autoload
1799 (defun anything-other-buffer (any-sources any-buffer)
1800 "Simplified interface of `anything' with other `anything-buffer'.
1801 Call `anything' with only ANY-SOURCES and ANY-BUFFER as args."
1802 (anything :sources any-sources :buffer any-buffer))
1804 (defun anything-nest (&rest same-as-anything)
1805 "Nested `anything'. If you use `anything' within `anything', use it."
1806 (with-anything-window
1807 (let (anything-current-position
1808 anything-current-buffer
1809 (orig-anything-current-buffer anything-current-buffer)
1810 (orig-anything-buffer anything-buffer)
1811 (orig-anything-last-frame-or-window-configuration
1812 anything-last-frame-or-window-configuration)
1813 anything-pattern
1814 (anything-buffer (or (getf same-as-anything :buffer)
1815 (nth 5 same-as-anything)
1816 "*Anything*"))
1817 anything-sources
1818 anything-compiled-sources
1819 (anything-samewindow t)
1820 (enable-recursive-minibuffers t))
1821 (unwind-protect
1822 (apply #'anything same-as-anything)
1823 (with-current-buffer orig-anything-buffer
1824 (anything-initialize-overlays orig-anything-buffer)
1825 (setq anything-buffer (current-buffer))
1826 (anything-mark-current-line)
1827 (setq anything-last-frame-or-window-configuration
1828 orig-anything-last-frame-or-window-configuration)
1829 (setq cursor-type t)
1830 (setq anything-current-buffer orig-anything-current-buffer))))))
1833 ;;; Initialize
1836 (defvar anything-buffers nil
1837 "All of `anything-buffer' in most recently used order.")
1838 (defun anything-initialize (any-resume any-input any-sources)
1839 "Start initialization of `anything' session.
1840 For ANY-RESUME ANY-INPUT and ANY-SOURCES See `anything'."
1841 (anything-log "start initialization: any-resume=%S any-input=%S" any-resume any-input)
1842 (anything-frame-or-window-configuration 'save)
1843 (setq anything-sources (anything-normalize-sources any-sources))
1844 (anything-log "sources = %S" anything-sources)
1845 (anything-hooks 'setup)
1846 (anything-current-position 'save)
1847 (if (anything-resume-p any-resume)
1848 (anything-initialize-overlays (anything-buffer-get))
1849 (anything-initial-setup))
1850 (unless (eq any-resume 'noresume)
1851 (anything-recent-push anything-buffer 'anything-buffers)
1852 (setq anything-last-buffer anything-buffer))
1853 (when any-input (setq anything-input any-input anything-pattern any-input))
1854 (and (anything-resume-p any-resume) (anything-funcall-foreach 'resume))
1855 (anything-log "end initialization"))
1857 (defun anything-execute-selection-action-1 ()
1858 "Execute current action."
1859 (anything-log-run-hook 'anything-before-action-hook)
1860 (unwind-protect
1861 (anything-execute-selection-action)
1862 (anything-aif (get-buffer anything-action-buffer)
1863 (kill-buffer it))
1864 (anything-log-run-hook 'anything-after-action-hook)
1865 ;; Depending the keymap used, i.e `minibuffer-local-must-match-map'
1866 ;; we can exit minibuffer with `exit-minibuffer'
1867 ;; instead of `anything-exit-minibuffer',
1868 ;; so set exit status here instead of in `anything-exit-minibuffer'.
1869 (setq anything-exit-status 0)))
1871 (defun anything-restore-position-on-quit ()
1872 "Restore position in `anything-current-buffer' when quitting."
1873 (anything-current-position 'restore))
1875 (defun anything-recent-push (elt list-var)
1876 "Add ELT to the value of LIST-VAR as most recently used value."
1877 (let ((m (member elt (symbol-value list-var))))
1878 (and m (set list-var (delq (car m) (symbol-value list-var))))
1879 (push elt (symbol-value list-var))))
1882 ;;; (@* "Core: Accessors")
1883 ;;; rubikitch: I love to create functions to control variables.
1884 (defvar anything-current-position nil
1885 "Cons of \(point . window-start\) when `anything' is invoked.
1886 It is needed to restore position in `anything-current-buffer'
1887 when `anything' is keyboard-quitted.")
1888 (defun anything-current-position (save-or-restore)
1889 "Restore or save current position in `anything-current-buffer'.
1890 Argument SAVE-OR-RESTORE is one of save or restore."
1891 (case save-or-restore
1892 (save
1893 (anything-log "Save position at %S" (cons (point) (window-start)))
1894 (setq anything-current-position (cons (point) (window-start))))
1895 (restore
1896 (anything-log "Restore position at %S in buffer %s"
1897 anything-current-position
1898 (buffer-name (current-buffer)))
1899 (goto-char (car anything-current-position))
1900 ;; Fix this position with the NOFORCE arg of `set-window-start'
1901 ;; otherwise, if there is some other buffer than `anything-current-buffer'
1902 ;; one, position will be lost.
1903 (set-window-start (selected-window) (cdr anything-current-position) t))))
1906 ;; Internal.
1907 (defvar anything-last-frame-or-window-configuration nil
1908 "Used to store window or frame configuration when anything start.")
1909 (defun anything-frame-or-window-configuration (save-or-restore)
1910 "Save or restore last frame or window configuration.
1911 Possible value of SAVE-OR-RESTORE are 'save and 'restore.
1912 window or frame configuration is saved/restored according to values of
1913 `anything-save-configuration-functions'."
1914 (anything-log-eval anything-save-configuration-functions)
1915 (case save-or-restore
1916 (save (setq anything-last-frame-or-window-configuration
1917 (funcall (cdr anything-save-configuration-functions))))
1918 (restore (funcall (car anything-save-configuration-functions)
1919 anything-last-frame-or-window-configuration)
1920 ;; Restore frame focus.
1921 (let ((frame (and (listp anything-last-frame-or-window-configuration)
1922 (caadr anything-last-frame-or-window-configuration))))
1923 ;; If `anything-save-configuration-functions' are window functions
1924 ;; frame should be nil, use current frame.
1925 (unless (framep frame)
1926 (setq frame (selected-frame)))
1927 (select-frame-set-input-focus frame)))))
1930 ;; (@* "Core: Display *anything* buffer")
1931 (defun anything-display-buffer (buf)
1932 "Display *anything* buffer BUF."
1933 (let (pop-up-frames)
1934 (funcall (with-current-buffer buf anything-display-function) buf)))
1936 (defun anything-default-display-buffer (buf)
1937 "Default function to display BUF.
1938 Where BUF is generally `anything-buffer'.
1939 It use `switch-to-buffer' or `pop-to-buffer' depending of value of
1940 `anything-samewindow'."
1941 (funcall (if anything-samewindow 'switch-to-buffer 'pop-to-buffer) buf))
1944 ;; (@* "Core: initialize")
1945 (defun anything-initial-setup ()
1946 "Initialize anything settings and set up the anything buffer."
1947 (anything-log-run-hook 'anything-before-initialize-hook)
1948 (setq anything-current-prefix-arg nil)
1949 (setq anything-once-called-functions nil)
1950 (setq anything-delayed-init-executed nil)
1951 (setq anything-current-buffer
1952 (if (minibuffer-window-active-p (minibuffer-window))
1953 ;; If minibuffer is active be sure to use it's buffer
1954 ;; as `anything-current-buffer'.
1955 (window-buffer (active-minibuffer-window))
1956 (current-buffer)))
1957 (setq anything-buffer-file-name buffer-file-name)
1958 (setq anything-issued-errors nil)
1959 (setq anything-compiled-sources nil)
1960 (setq anything-saved-current-source nil)
1961 (if (or (not split-width-threshold)
1962 (and (integerp split-width-threshold)
1963 (>= split-width-threshold (+ (frame-width) 4))))
1964 (setq anything-split-window-state 'vertical)
1965 (setq anything-split-window-state 'horizontal))
1966 ;; Call the init function for sources where appropriate
1967 (anything-funcall-foreach 'init)
1968 (setq anything-pattern "")
1969 (setq anything-input "")
1970 (setq anything-candidate-cache nil)
1971 (setq anything-last-sources anything-sources)
1972 (anything-create-anything-buffer)
1973 (anything-log-run-hook 'anything-after-initialize-hook))
1975 (defvar anything-reading-pattern nil
1976 "Whether in `read-string' in anything or not.")
1977 (defun anything-read-pattern-maybe (any-prompt any-input
1978 any-preselect any-resume any-keymap
1979 any-default any-history)
1980 "Read pattern with prompt ANY-PROMPT and initial input ANY-INPUT.
1981 For ANY-PRESELECT ANY-RESUME ANY-KEYMAP, See `anything'."
1982 (if (anything-resume-p any-resume)
1983 (anything-mark-current-line t)
1984 (anything-update any-preselect))
1985 (with-current-buffer (anything-buffer-get)
1986 (and any-keymap (set (make-local-variable 'anything-map) any-keymap))
1987 (let ((minibuffer-local-map
1988 anything-map))
1989 (anything-log-eval (anything-approximate-candidate-number)
1990 anything-execute-action-at-once-if-one
1991 anything-quit-if-no-candidate)
1992 (cond ((and anything-execute-action-at-once-if-one
1993 (= (anything-approximate-candidate-number) 1))
1994 (ignore))
1995 ((and anything-quit-if-no-candidate
1996 (= (anything-approximate-candidate-number) 0))
1997 (setq anything-quit t)
1998 (and (functionp anything-quit-if-no-candidate)
1999 (funcall anything-quit-if-no-candidate)))
2001 (let ((anything-reading-pattern t)
2002 (tap (or any-default
2003 (with-anything-current-buffer
2004 (thing-at-point 'symbol)))))
2005 (read-string (or any-prompt "pattern: ") any-input any-history tap)))))))
2007 (defun anything-create-anything-buffer (&optional test-mode)
2008 "Create newly created `anything-buffer'.
2009 If TEST-MODE is non-nil, clear `anything-candidate-cache'."
2010 (when test-mode
2011 (setq anything-candidate-cache nil))
2012 (with-current-buffer (get-buffer-create anything-buffer)
2013 (anything-log "kill local variables: %S" (buffer-local-variables))
2014 (kill-all-local-variables)
2015 (set (make-local-variable 'inhibit-read-only) t)
2016 (buffer-disable-undo)
2017 (erase-buffer)
2018 (set (make-local-variable 'inhibit-read-only) t)
2019 (set (make-local-variable 'anything-last-sources-local) anything-sources)
2020 (set (make-local-variable 'anything-follow-mode) nil)
2021 (set (make-local-variable 'anything-display-function) anything-display-function)
2022 (set (make-local-variable 'anything-selection-point) nil)
2023 (anything-initialize-persistent-action)
2024 (anything-log-eval anything-display-function anything-let-variables)
2025 (loop for (var . val) in anything-let-variables
2026 do (set (make-local-variable var) val))
2027 (setq cursor-type nil)
2028 (setq mode-name "Anything"))
2029 (anything-initialize-overlays anything-buffer)
2030 (get-buffer anything-buffer))
2032 (defun anything-initialize-overlays (buffer)
2033 "Initialize anything overlays in BUFFER."
2034 (anything-log "overlay setup")
2035 (if anything-selection-overlay
2036 ;; make sure the overlay belongs to the anything buffer if
2037 ;; it's newly created
2038 (move-overlay anything-selection-overlay (point-min) (point-min)
2039 (get-buffer buffer))
2041 (setq anything-selection-overlay
2042 (make-overlay (point-min) (point-min) (get-buffer buffer)))
2043 (overlay-put anything-selection-overlay 'face anything-selection-face))
2045 (cond (anything-enable-shortcuts
2046 (setq anything-shortcut-keys
2047 (assoc-default anything-enable-shortcuts anything-shortcut-keys-alist))
2048 (unless anything-digit-overlays
2049 (setq anything-digit-overlays
2050 (loop for key across anything-shortcut-keys
2051 for overlay = (make-overlay (point-min) (point-min)
2052 (get-buffer buffer))
2053 do (overlay-put overlay 'before-string
2054 (format "%s - " (upcase (make-string 1 key))))
2055 collect overlay))))
2056 (anything-digit-overlays
2057 (mapc 'delete-overlay anything-digit-overlays)
2058 (setq anything-digit-overlays nil))))
2060 (defun anything-hooks (setup-or-cleanup)
2061 "Add or remove hooks according to SETUP-OR-CLEANUP value.
2062 if SETUP-OR-CLEANUP value is setup add hooks, any other value
2063 will remove hooks.
2064 hooks concerned are `post-command-hook' and `minibuffer-setup-hook'."
2065 (let ((hooks '((post-command-hook anything-check-minibuffer-input)
2066 (minibuffer-setup-hook anything-print-error-messages))))
2067 (if (eq setup-or-cleanup 'setup)
2068 (dolist (args hooks) (apply 'add-hook args))
2069 (dolist (args (reverse hooks)) (apply 'remove-hook args)))))
2072 ;; (@* "Core: clean up")
2073 ;;; TODO move
2074 (defun anything-cleanup ()
2075 "Clean up the mess when anything exit or quit."
2076 (anything-log "start cleanup")
2077 (with-current-buffer anything-buffer
2078 ;; rubikitch: I think it is not needed.
2079 ;; (setq cursor-type t)
2081 ;; Call burry-buffer whithout arg
2082 ;; to remove anything-buffer from window.
2083 (bury-buffer)
2084 ;; Be sure we call this from anything-buffer.
2085 (anything-funcall-foreach 'cleanup))
2086 (anything-new-timer 'anything-check-minibuffer-input-timer nil)
2087 (anything-kill-async-processes)
2088 (anything-log-run-hook 'anything-cleanup-hook)
2089 (anything-hooks 'cleanup)
2090 (anything-frame-or-window-configuration 'restore)
2091 ;; This is needed in some cases where last input
2092 ;; is yielded infinitely in minibuffer after anything session.
2093 (anything-clean-up-minibuffer))
2095 (defun anything-clean-up-minibuffer ()
2096 "Remove contents of minibuffer."
2097 (let ((miniwin (minibuffer-window)))
2098 ;; Clean only current minibuffer used by anything.
2099 ;; i.e The precedent one is active.
2100 (unless (minibuffer-window-active-p miniwin)
2101 (with-current-buffer (window-buffer miniwin)
2102 (delete-minibuffer-contents)))))
2105 ;; (@* "Core: input handling")
2106 (defun anything-check-minibuffer-input ()
2107 "Extract input string from the minibuffer and check if it needs to be handled."
2108 (let ((delay (with-current-buffer anything-buffer
2109 (and anything-input-idle-delay
2110 (max anything-input-idle-delay 0.1)))))
2111 (if (or (not delay) (anything-action-window))
2112 (anything-check-minibuffer-input-1)
2113 (anything-new-timer
2114 'anything-check-minibuffer-input-timer
2115 (run-with-idle-timer delay nil 'anything-check-minibuffer-input-1)))))
2117 (defun anything-check-minibuffer-input-1 ()
2118 "Check minibuffer content."
2119 (with-anything-quittable
2120 (with-selected-window (or (active-minibuffer-window) (minibuffer-window))
2121 (anything-check-new-input (minibuffer-contents)))))
2123 (defun anything-check-new-input (input)
2124 "Check INPUT string and update the anything buffer if necessary."
2125 (unless (equal input anything-pattern)
2126 (setq anything-pattern input)
2127 (unless (anything-action-window)
2128 (setq anything-input anything-pattern))
2129 (anything-log-eval anything-pattern anything-input)
2130 (anything-update)))
2133 ;; (@* "Core: source compiler")
2134 (defvar anything-compile-source-functions-default anything-compile-source-functions
2135 "Plug-ins this file provides.")
2136 (defun anything-compile-sources (sources funcs)
2137 "Compile SOURCES with FUNCS.
2138 See `anything-compile-source-functions'.
2139 Anything plug-ins are realized by this function."
2140 (mapcar
2141 (lambda (source)
2142 (loop with source = (if (listp source) source (symbol-value source))
2143 for f in funcs
2144 do (setq source (funcall f source))
2145 finally (return source)))
2146 sources))
2149 ;; (@* "Core: plug-in attribute documentation hack")
2151 ;; `anything-document-attribute' is public API.
2152 (defadvice documentation-property (after anything-document-attribute activate)
2153 "Display plug-in attributes' documentation as `anything-sources' docstring."
2154 (when (eq (ad-get-arg 0) 'anything-sources)
2155 (setq ad-return-value
2156 (concat ad-return-value "\n"
2157 (mapconcat (lambda (sym) (get sym 'anything-attrdoc))
2158 anything-additional-attributes
2159 "\n")))))
2160 ;; (describe-variable 'anything-sources)
2161 ;; (documentation-property 'anything-sources 'variable-documentation)
2162 ;; (progn (ad-disable-advice 'documentation-property 'after 'anything-document-attribute) (ad-update 'documentation-property))
2165 ;; (@* "Core: all candidates")
2166 (defun anything-process-delayed-init (source)
2167 "Initialize delayed SOURCE."
2168 (let ((name (assoc-default 'name source)))
2169 (unless (member name anything-delayed-init-executed)
2170 (anything-aif (assoc-default 'delayed-init source)
2171 (with-current-buffer anything-current-buffer
2172 (anything-funcall-with-source source it)
2173 (dolist (f (if (functionp it) (list it) it))
2174 (add-to-list 'anything-delayed-init-executed name)))))))
2176 (defun anything-get-candidates (source)
2177 "Retrieve and return the list of candidates from SOURCE."
2178 (anything-process-delayed-init source)
2179 (let* ((candidate-source (assoc-default 'candidates source))
2180 (type-error (lambda ()
2181 (error (concat "Candidates must either be a function, "
2182 " a variable or a list: %s")
2183 candidate-source)))
2184 (candidates (condition-case err
2185 (anything-interpret-value candidate-source source)
2186 (error (funcall type-error)))))
2187 (cond ((processp candidates) candidates)
2188 ((listp candidates) (anything-transform-candidates candidates source))
2189 (t (funcall type-error)))))
2192 (defun anything-get-cached-candidates (source)
2193 "Return the cached value of candidates for SOURCE.
2194 Cache the candidates if there is not yet a cached value."
2195 (let* ((name (assoc-default 'name source))
2196 (candidate-cache (assoc name anything-candidate-cache)))
2197 (cond (candidate-cache
2198 (anything-log "use cached candidates")
2199 (cdr candidate-cache))
2201 (anything-log "calculate candidates")
2202 (let ((candidates (anything-get-candidates source)))
2203 (cond ((processp candidates)
2204 (push (cons candidates
2205 (append source
2206 (list (cons 'item-count 0)
2207 (cons 'incomplete-line ""))))
2208 anything-async-processes)
2209 (set-process-filter candidates 'anything-output-filter)
2210 (setq candidates nil))
2211 ((not (assoc 'volatile source))
2212 (setq candidate-cache (cons name candidates))
2213 (push candidate-cache anything-candidate-cache)))
2214 candidates)))))
2217 ;;; (@* "Core: candidate transformers")
2218 (defun anything-process-candidate-transformer (candidates source)
2219 "Execute candidate-transformer function on all CANDIDATES of SOURCE."
2220 (anything-aif (assoc-default 'candidate-transformer source)
2221 (anything-composed-funcall-with-source source it candidates)
2222 candidates))
2224 (defun anything-process-filtered-candidate-transformer (candidates source)
2225 "Execute filtered-candidate-transformer function on all CANDIDATES of SOURCE."
2226 (anything-aif (assoc-default 'filtered-candidate-transformer source)
2227 (anything-composed-funcall-with-source source it candidates source)
2228 candidates))
2230 (defun anything-process-filtered-candidate-transformer-maybe (candidates source process-p)
2231 "Execute filtered-candidate-transformer function on all CANDIDATES of SOURCE.
2232 This happen if PROCESS-P is non-nil."
2233 (if process-p
2234 (anything-process-filtered-candidate-transformer candidates source)
2235 candidates))
2237 (defun anything-process-real-to-display (candidates source)
2238 "Execute real-to-display function on all CANDIDATES of SOURCE."
2239 (anything-aif (assoc-default 'real-to-display source)
2240 (setq candidates (anything-funcall-with-source
2241 source 'mapcar
2242 (lambda (cand_)
2243 (if (consp cand_)
2244 ;; override DISPLAY from candidate-transformer
2245 (cons (funcall it (cdr cand_)) (cdr cand_))
2246 (cons (funcall it cand_) cand_)))
2247 candidates))
2248 candidates))
2250 (defun anything-transform-candidates (candidates source &optional process-p)
2251 "Transform CANDIDATES of SOURCE according to candidate transformers.
2252 This happen if PROCESS-P is non-nil."
2253 (anything-process-real-to-display
2254 (anything-process-filtered-candidate-transformer-maybe
2255 (anything-process-candidate-transformer candidates source) source process-p)
2256 source))
2259 ;; (@* "Core: narrowing candidates")
2260 (defun anything-candidate-number-limit (source)
2261 "Apply candidate-number-limit attribute value.
2262 This overhide variable `anything-candidate-number-limit'.
2264 e.g:
2265 If \(candidate-number-limit\) is in SOURCE, show all candidates in SOURCE.
2266 If \(candidate-number-limit . 123\) is in SOURCE limit candidate to 123."
2267 (anything-aif (assq 'candidate-number-limit source)
2268 (or (cdr it) 99999999)
2269 (or anything-candidate-number-limit 99999999)))
2271 ;; FIXME: Why a defconst here
2272 (defconst anything-default-match-functions
2273 (list (lambda (candidate)
2274 (string-match anything-pattern candidate)))
2275 "Default functions to match candidates according to `anything-pattern'.")
2277 (defun anything-compute-matches (source)
2278 "Compute matched results from SOURCE according to its settings."
2279 (if debug-on-error
2280 (anything-compute-matches-internal source)
2281 (condition-case v
2282 (anything-compute-matches-internal source)
2283 (error (anything-log-error
2284 "anything-compute-matches: error when processing source: %s"
2285 (assoc-default 'name source))
2286 nil))))
2288 (defun anything-candidate-get-display (candidate)
2289 "Get searched display part from CANDIDATE.
2290 CANDIDATE is a string, a symbol, or \(DISPLAY . REAL\) cons cell."
2291 (format "%s" (or (car-safe candidate) candidate)))
2293 (defun anything-process-pattern-transformer (pattern source)
2294 "Execute pattern-transformer attribute PATTERN function in SOURCE."
2295 (anything-aif (assoc-default 'pattern-transformer source)
2296 (anything-composed-funcall-with-source source it pattern)
2297 pattern))
2299 (defun anything-match-functions (source)
2300 (or (assoc-default 'match source)
2301 anything-default-match-functions))
2303 (defmacro anything-accumulate-candidates-internal (cand newmatches
2304 hash item-count limit)
2305 "Internal, add CAND into NEWMATCHES.
2306 Use HASH to uniq NEWMATCHES.
2307 Argument ITEM-COUNT count the matches.
2308 if ITEM-COUNT reaches LIMIT, exit from inner loop."
2309 `(unless (gethash ,cand ,hash)
2310 (puthash ,cand t ,hash)
2311 (push ,cand ,newmatches)
2312 (incf ,item-count)
2313 (when (= ,item-count ,limit)
2314 (setq exit t)
2315 (return))))
2317 (defun anything-take-first-elements (seq n)
2318 (if (> (length seq) n)
2319 (setq seq (subseq seq 0 n))
2320 seq))
2322 (defun anything-match-from-candidates (cands matchfns limit)
2323 (let (matches)
2324 (condition-case nil
2325 (let ((item-count 0) exit)
2326 (clrhash anything-match-hash)
2327 (dolist (match matchfns)
2328 (let (newmatches)
2329 (dolist (candidate cands)
2330 (when (funcall match (anything-candidate-get-display candidate))
2331 (anything-accumulate-candidates-internal
2332 candidate newmatches anything-match-hash item-count limit)))
2333 (setq matches (append matches (reverse newmatches)))
2334 (if exit (return)))))
2335 (invalid-regexp (setq matches nil)))
2336 matches))
2338 (defun anything-compute-matches-internal (source)
2339 (save-current-buffer
2340 (let ((matchfns (anything-match-functions source))
2341 (anything-source-name (assoc-default 'name source))
2342 (limit (anything-candidate-number-limit source))
2343 (anything-pattern (anything-process-pattern-transformer
2344 anything-pattern source)))
2345 (anything-process-filtered-candidate-transformer
2346 (if (or (equal anything-pattern "") (equal matchfns '(identity)))
2347 (anything-take-first-elements
2348 (anything-get-cached-candidates source) limit)
2349 (anything-match-from-candidates
2350 (anything-get-cached-candidates source) matchfns limit))
2351 source))))
2353 ;; (anything '(((name . "error")(candidates . (lambda () (hage))) (action . identity))))
2355 (defun anything-process-source (source)
2356 "Display matched results from SOURCE according to its settings."
2357 (anything-log-eval (assoc-default 'name source))
2358 (if (assq 'direct-insert-match source) ;experimental
2359 (anything-process-source--direct-insert-match source)
2360 (let ((matches (anything-compute-matches source)))
2361 (when matches
2362 (when anything-test-mode
2363 (setq anything-test-candidate-list
2364 `(,@anything-test-candidate-list
2365 (,(assoc-default 'name source)
2366 ,matches))))
2367 (anything-insert-header-from-source source)
2368 (if (not (assq 'multiline source))
2369 (mapc 'anything-insert-match-with-digit-overlay matches)
2370 (let ((start (point)) separate)
2371 (dolist (match matches)
2372 (if separate
2373 (anything-insert-candidate-separator)
2374 (setq separate t))
2375 (anything-insert-match-with-digit-overlay match))
2376 (put-text-property start (point) 'anything-multiline t)))))))
2378 (defun anything-insert-match-with-digit-overlay (match)
2379 (declare (special source))
2380 (anything-put-digit-overlay-maybe)
2381 (anything-insert-match match 'insert source))
2383 (defun anything-put-digit-overlay-maybe ()
2384 (when (and anything-enable-shortcuts
2385 (not (eq anything-digit-shortcut-count
2386 (length anything-digit-overlays))))
2387 (move-overlay (nth anything-digit-shortcut-count
2388 anything-digit-overlays)
2389 (point-at-bol)
2390 (point-at-bol))
2391 (incf anything-digit-shortcut-count)))
2393 (defun anything-process-source--direct-insert-match (source)
2394 "[EXPERIMENTAL] Insert candidates from `anything-candidate-buffer' in SOURCE."
2395 (anything-log-eval (assoc-default 'name source))
2396 (let ((anything-source-name (assoc-default 'name source))
2397 content-buf)
2398 (funcall (assoc-default 'candidates source))
2399 (setq content-buf (anything-candidate-buffer))
2400 (unless (anything-empty-buffer-p content-buf)
2401 (anything-insert-header-from-source source)
2402 (insert-buffer-substring content-buf)
2403 ;; TODO call anything-put-digit-overlay-maybe with loop
2406 (defun anything-process-delayed-sources (delayed-sources &optional preselect)
2407 "Process anything DELAYED-SOURCES.
2408 Move selection to string or regexp PRESELECT if non--nil.
2409 This function is called in `anything-process-delayed-sources-timer'
2410 when emacs is idle for `anything-idle-delay'."
2411 (with-anything-quittable
2412 (anything-log-eval (mapcar (lambda (s) (assoc-default 'name s)) delayed-sources))
2413 (with-current-buffer anything-buffer
2414 (save-excursion
2415 (goto-char (point-max))
2416 (mapc 'anything-process-source delayed-sources)
2417 (when (and (not (anything-empty-buffer-p))
2418 ;; No selection yet.
2419 (= (overlay-start anything-selection-overlay)
2420 (overlay-end anything-selection-overlay)))
2421 (anything-update-move-first-line 'without-hook)))
2422 (when preselect (anything-preselect preselect))
2423 (save-excursion
2424 (goto-char (point-min))
2425 (anything-log-run-hook 'anything-update-hook))
2426 (anything-log-run-hook 'anything-after-update-hook))))
2429 ;; (@* "Core: *anything* buffer contents")
2430 (defvar anything-input-local nil)
2431 (defvar anything-process-delayed-sources-timer nil)
2432 (defun anything-update (&optional preselect)
2433 "Update candidates list in `anything-buffer' according to `anything-pattern'.
2434 Argument PRESELECT is a string or regexp used to move selection to a particular
2435 place once updating is done. It should be used on single source because search
2436 is done on whole `anything-buffer' and not on current source."
2437 (anything-log "start update")
2438 (setq anything-digit-shortcut-count 0)
2439 (anything-kill-async-processes)
2440 (with-current-buffer (anything-buffer-get)
2441 (set (make-local-variable 'anything-input-local) anything-pattern)
2442 (erase-buffer)
2443 (when anything-enable-shortcuts
2444 (mapc 'delete-overlay anything-digit-overlays))
2445 (let (delayed-sources
2446 normal-sources)
2447 (unwind-protect ; Process normal sources and store delayed one's.
2448 (setq delayed-sources
2449 (loop for source in (remove-if-not 'anything-update-source-p
2450 (anything-get-sources))
2451 if (anything-delayed-source-p source)
2452 collect source
2453 else do (progn (push source normal-sources)
2454 (anything-process-source source))))
2455 (anything-log-eval
2456 (mapcar (lambda (s) (assoc-default 'name s)) delayed-sources))
2457 (if anything-test-mode ; Need only to process sources.
2458 (mapc 'anything-process-source delayed-sources)
2459 (cond ((and preselect delayed-sources normal-sources)
2460 ;; Preselection run here when there is
2461 ;; normal AND delayed sources.
2462 (anything-log "Update preselect candidate %s" preselect)
2463 (anything-preselect preselect))
2464 (delayed-sources ; Preselection and hooks will run later.
2465 (anything-update-move-first-line 'without-hook))
2466 (t ; No delayed sources, run the hooks now.
2467 (anything-update-move-first-line)
2468 (anything-log-run-hook 'anything-after-update-hook)
2469 (when preselect
2470 (anything-log "Update preselect candidate %s" preselect)
2471 (anything-preselect preselect))))
2472 (when delayed-sources
2473 (anything-new-timer
2474 'anything-process-delayed-sources-timer
2475 (run-with-idle-timer
2476 ;; Be sure anything-idle-delay is >
2477 ;; to anything-input-idle-delay
2478 ;; otherwise use value of anything-input-idle-delay
2479 ;; or 0.1 if == to 0.
2480 (max anything-idle-delay anything-input-idle-delay 0.1) nil
2481 'anything-process-delayed-sources delayed-sources preselect))))
2482 (anything-log "end update")))))
2484 (defun anything-update-source-p (source)
2485 "Wheter SOURCE need updating or not."
2486 (and (or (not anything-source-filter)
2487 (member (assoc-default 'name source) anything-source-filter))
2488 (>= (length anything-pattern)
2489 (anything-aif (assoc 'requires-pattern source)
2490 (or (cdr it) 1)
2491 0))))
2493 (defun anything-delayed-source-p (source)
2494 "Wheter SOURCE is a delayed source or not."
2495 (or (assoc 'delayed source)
2496 (and anything-quick-update
2497 (< (window-height (get-buffer-window (current-buffer)))
2498 (line-number-at-pos (point-max))))))
2500 (defun anything-update-move-first-line (&optional without-hook)
2501 "Goto first line of `anything-buffer'."
2502 (goto-char (point-min))
2503 (unless without-hook
2504 (save-excursion (anything-log-run-hook 'anything-update-hook)))
2505 (anything-next-line))
2507 (defun anything-force-update (&optional preselect)
2508 "Force recalculation and update of candidates.
2509 If current source has `update' attribute, a function without argument,
2510 call it before update."
2511 (interactive)
2512 (let ((source (anything-get-current-source))
2513 (selection (anything-get-selection nil t)))
2514 (when source
2515 (mapc 'anything-force-update--reinit
2516 (anything-get-sources)))
2517 (anything-update preselect)
2518 ;; If preselect arg exists, `anything-update' should
2519 ;; have moved to selection, otherwise do it now.
2520 (unless preselect
2521 (anything-keep-selection (assoc-default 'name source) selection))
2522 (with-anything-window (recenter))))
2524 (defun anything-force-update--reinit (source)
2525 "Reinit SOURCE by calling his update and/or init functions."
2526 (anything-aif (anything-funcall-with-source
2527 source 'anything-candidate-buffer)
2528 (kill-buffer it))
2529 (dolist (attr '(update init))
2530 (anything-aif (assoc-default attr source)
2531 (anything-funcall-with-source source it)))
2532 (anything-remove-candidate-cache source))
2534 (defun anything-keep-selection (source selection)
2535 "Switch to SOURCE and goto SELECTION."
2536 (when (and source selection)
2537 (with-anything-window
2538 (anything-goto-source source)
2539 (forward-char -1)
2540 (if (search-forward selection nil t)
2541 (forward-line 0)
2542 (goto-char (point-min))
2543 (forward-line 1))
2544 (anything-mark-current-line))))
2546 (defun anything-remove-candidate-cache (source)
2547 "Remove SOURCE from `anything-candidate-cache'."
2548 (setq anything-candidate-cache
2549 (delete (assoc (assoc-default 'name source)
2550 anything-candidate-cache)
2551 anything-candidate-cache)))
2553 (defun anything-insert-match (match insert-function source)
2554 "Insert MATCH into `anything-buffer' with INSERT-FUNCTION for SOURCE.
2555 If MATCH is a list then insert the string intended to appear on the display
2556 and store the real value in a text property."
2557 (let ((start (point-at-bol (point)))
2558 (string (or (car-safe match) match))
2559 (realvalue (cdr-safe match)))
2560 (when (symbolp string) (setq string (symbol-name string)))
2561 (when (stringp string)
2562 (funcall insert-function string)
2563 ;; Some sources with candidates-in-buffer have already added
2564 ;; 'anything-realvalue property when creating candidate buffer.
2565 (unless (get-text-property start 'anything-realvalue)
2566 (and realvalue
2567 (put-text-property start (point-at-eol)
2568 'anything-realvalue realvalue)))
2569 (when anything-source-in-each-line-flag
2570 (put-text-property start (point-at-eol) 'anything-source source))
2571 (funcall insert-function "\n"))))
2573 (defun anything-insert-header-from-source (source)
2574 "Insert SOURCE name in `anything-buffer' header.
2575 Maybe insert by overlay additional info after source name if SOURCE have
2576 header-name attribute."
2577 (let ((name (assoc-default 'name source)))
2578 (anything-insert-header
2579 name
2580 (anything-aif (assoc-default 'header-name source)
2581 (anything-funcall-with-source source it name)))))
2583 (defun anything-insert-header (name &optional display-string)
2584 "Insert header of source NAME into the anything buffer.
2585 If DISPLAY-STRING is non--nil and a string, display this additional info
2586 after the source name by overlay."
2587 (unless (bobp)
2588 (let ((start (point)))
2589 (insert "\n")
2590 (put-text-property start (point) 'anything-header-separator t)))
2591 (let ((start (point)))
2592 (insert name)
2593 (put-text-property (point-at-bol)
2594 (point-at-eol) 'anything-header t)
2595 (when display-string
2596 (overlay-put (make-overlay (point-at-bol) (point-at-eol))
2597 'display display-string))
2598 (insert "\n")
2599 (put-text-property start (point) 'face anything-header-face)))
2601 (defun anything-insert-candidate-separator ()
2602 "Insert separator of candidates into the anything buffer."
2603 (insert anything-candidate-separator)
2604 (put-text-property (point-at-bol)
2605 (point-at-eol) 'anything-candidate-separator t)
2606 (insert "\n"))
2609 ;; (@* "Core: async process")
2610 (defun anything-output-filter (process string)
2611 "From PROCESS process output STRING."
2612 (anything-output-filter-1 (assoc process anything-async-processes) string))
2614 (defun anything-output-filter-1 (process-assoc string)
2615 (anything-log-eval string)
2616 (with-current-buffer anything-buffer
2617 (let ((source (cdr process-assoc)))
2618 (save-excursion
2619 (anything-aif (assoc-default 'insertion-marker source)
2620 (goto-char it)
2621 (goto-char (point-max))
2622 (anything-insert-header-from-source source)
2623 (setcdr process-assoc
2624 (append source `((insertion-marker . ,(point-marker))))))
2625 (anything-output-filter--process-source
2626 (car process-assoc) string source
2627 (anything-candidate-number-limit source))))
2628 (anything-output-filter--post-process)))
2630 (defun anything-output-filter--process-source (process string source limit)
2631 (dolist (candidate (anything-transform-candidates
2632 (anything-output-filter--collect-candidates
2633 (split-string string "\n")
2634 (assoc 'incomplete-line source))
2635 source t))
2636 (if (not (assq 'multiline source))
2637 (anything-insert-match candidate 'insert-before-markers source)
2638 (let ((start (point)))
2639 (anything-insert-candidate-separator)
2640 (anything-insert-match candidate 'insert-before-markers source)
2641 (put-text-property start (point) 'anything-multiline t)))
2642 (incf (cdr (assoc 'item-count source)))
2643 (when (>= (assoc-default 'item-count source) limit)
2644 (anything-kill-async-process process)
2645 (return))))
2647 (defun anything-output-filter--collect-candidates (lines incomplete-line-info)
2648 (anything-log-eval (cdr incomplete-line-info))
2649 (butlast
2650 (loop for line in lines collect
2651 (if (cdr incomplete-line-info)
2652 (prog1
2653 (concat (cdr incomplete-line-info) line)
2654 (setcdr incomplete-line-info nil))
2655 line)
2656 finally (setcdr incomplete-line-info line))))
2658 (defun anything-output-filter--post-process ()
2659 (anything-log-run-hook 'anything-update-hook)
2660 (anything-aif (get-buffer-window anything-buffer 'visible)
2661 (save-selected-window
2662 (select-window it)
2663 (anything-skip-noncandidate-line 'next)
2664 (anything-mark-current-line))))
2666 (defun anything-kill-async-processes ()
2667 "Kill all known asynchronous processes of `anything-async-processes'."
2668 (mapc 'anything-kill-async-process (mapcar 'car anything-async-processes))
2669 (setq anything-async-processes nil))
2671 (defun anything-kill-async-process (process)
2672 "Kill PROCESS and detach the associated functions."
2673 (set-process-filter process nil)
2674 (delete-process process))
2677 ;; (@* "Core: action")
2678 (defun anything-execute-selection-action (&optional
2679 selection action
2680 preserve-saved-action)
2681 "If a candidate SELECTION is present then perform the associated ACTION on it.
2682 If PRESERVE-SAVED-ACTION is non-nil don't save action."
2683 (anything-log "executing action")
2684 (setq action (anything-get-default-action
2685 (or action
2686 anything-saved-action
2687 (if (get-buffer anything-action-buffer)
2688 (anything-get-selection anything-action-buffer)
2689 (anything-get-action)))))
2690 (let ((source (or anything-saved-current-source
2691 (anything-get-current-source))))
2692 (setq selection (or selection
2693 (anything-get-selection)
2694 (and (assoc 'accept-empty source) "")))
2695 (unless preserve-saved-action (setq anything-saved-action nil))
2696 (if (and selection action)
2697 (anything-funcall-with-source
2698 source action
2699 (anything-coerce-selection selection source)))))
2701 (defun anything-coerce-selection (selection source)
2702 "Apply coerce attribute function to SELECTION in SOURCE.
2703 Coerce source with coerce function."
2704 (anything-aif (assoc-default 'coerce source)
2705 (anything-funcall-with-source source it selection)
2706 selection))
2708 (defun anything-get-default-action (action)
2709 "Get the first ACTION value of action list in source."
2710 (if (and (listp action) (not (functionp action)))
2711 (cdar action)
2712 action))
2714 (defun anything-select-action ()
2715 "Select an action for the currently selected candidate.
2716 If action buffer is selected, back to the anything buffer."
2717 (interactive)
2718 (anything-log-run-hook 'anything-select-action-hook)
2719 (cond ((get-buffer-window anything-action-buffer 'visible)
2720 (set-window-buffer (get-buffer-window anything-action-buffer)
2721 anything-buffer)
2722 (kill-buffer anything-action-buffer)
2723 (anything-set-pattern anything-input 'noupdate))
2725 (setq anything-saved-selection (anything-get-selection))
2726 (unless anything-saved-selection
2727 (error "Nothing is selected"))
2728 (setq anything-saved-current-source (anything-get-current-source))
2729 (let ((actions (anything-get-action)))
2730 (if (functionp actions)
2731 (message "Sole action: %s" actions)
2732 (anything-show-action-buffer actions)
2733 (anything-delete-minibuffer-contents)
2734 (setq anything-pattern 'dummy) ; so that it differs from the previous one
2735 (anything-check-minibuffer-input))))))
2737 (defun anything-show-action-buffer (actions)
2738 (with-current-buffer (get-buffer-create anything-action-buffer)
2739 (erase-buffer)
2740 (buffer-disable-undo)
2741 (set-window-buffer (get-buffer-window anything-buffer) anything-action-buffer)
2742 (set (make-local-variable 'anything-sources)
2743 `(((name . "Actions")
2744 (volatile)
2745 (candidates . ,actions)
2746 (candidate-number-limit))))
2747 (set (make-local-variable 'anything-source-filter) nil)
2748 (set (make-local-variable 'anything-selection-overlay) nil)
2749 (set (make-local-variable 'anything-digit-overlays) nil)
2750 (anything-initialize-overlays anything-action-buffer)))
2753 ;; (@* "Core: selection")
2754 (defun anything-move-selection-common (move-func unit direction)
2755 "Move the selection marker to a new position wit function MOVE-FUNC.
2756 It is determined by UNIT and DIRECTION."
2757 (unless (or (anything-empty-buffer-p (anything-buffer-get))
2758 (not (anything-window)))
2759 (with-anything-window
2760 (anything-log-run-hook 'anything-move-selection-before-hook)
2761 (funcall move-func)
2762 (anything-skip-noncandidate-line direction)
2763 (anything-display-source-at-screen-top-maybe unit)
2764 (when (anything-get-previous-header-pos)
2765 (anything-mark-current-line))
2766 (anything-display-mode-line (anything-get-current-source))
2767 (anything-log-run-hook 'anything-move-selection-after-hook))))
2769 (defun anything-display-source-at-screen-top-maybe (unit)
2770 (when (and anything-display-source-at-screen-top (eq unit 'source))
2771 (set-window-start (selected-window)
2772 (save-excursion (forward-line -1) (point)))))
2774 (defun anything-skip-noncandidate-line (direction)
2775 (anything-skip-header-and-separator-line direction)
2776 (and (bobp) (forward-line 1)) ;skip first header
2777 (and (eobp) (forward-line -1))) ;avoid last empty line
2780 (defun anything-skip-header-and-separator-line (direction)
2781 (while (and (not (bobp))
2782 (or (anything-pos-header-line-p)
2783 (anything-pos-candidate-separator-p)))
2784 (forward-line (if (and (eq direction 'previous)
2785 (not (eq (point-at-bol) (point-min))))
2786 -1 1))))
2788 (defvar anything-mode-line-string-real nil)
2789 (defun anything-display-mode-line (source)
2790 (set (make-local-variable 'anything-mode-line-string)
2791 (anything-interpret-value (or (assoc-default 'mode-line source)
2792 (default-value 'anything-mode-line-string))
2793 source))
2794 (if anything-mode-line-string
2795 (setq mode-line-format
2796 '(" " mode-line-buffer-identification " "
2797 (line-number-mode "L%l") " " (anything-follow-mode "(F) ")
2798 (:eval (anything-show-candidate-number
2799 (when (listp anything-mode-line-string)
2800 (car anything-mode-line-string))))
2801 " " anything-mode-line-string-real "-%-")
2802 anything-mode-line-string-real
2803 (substitute-command-keys (if (listp anything-mode-line-string)
2804 (cadr anything-mode-line-string)
2805 anything-mode-line-string)))
2806 (setq mode-line-format
2807 (default-value 'mode-line-format)))
2808 (setq header-line-format
2809 (anything-interpret-value (assoc-default 'header-line source) source)))
2811 (defun anything-show-candidate-number (&optional name)
2812 "Used to display candidate number in mode-line.
2813 You can specify NAME of candidates e.g \"Buffers\" otherwise
2814 it is \"Candidate\(s\)\" by default."
2815 (propertize
2816 (format "[%s %s]"
2817 (anything-approximate-candidate-number 'in-current-source)
2818 (or name "Candidate(s)"))
2819 'face 'anything-candidate-number))
2821 (defun anything-previous-line ()
2822 "Move selection to the previous line."
2823 (interactive)
2824 (anything-move-selection-common
2825 (lambda ()
2826 (if (not (anything-pos-multiline-p))
2827 (forward-line -1) ;double forward-line is meaningful
2828 (forward-line -1) ;because evaluation order is important
2829 (anything-skip-header-and-separator-line 'previous)
2830 (let ((header-pos (anything-get-previous-header-pos))
2831 (separator-pos (anything-get-previous-candidate-separator-pos)))
2832 (when header-pos
2833 (goto-char (if (or (null separator-pos) (< separator-pos header-pos))
2834 header-pos ; first candidate
2835 separator-pos))
2836 (forward-line 1)))))
2837 'line 'previous))
2839 (defun anything-next-line ()
2840 "Move selection to the next line."
2841 (interactive)
2842 (anything-move-selection-common
2843 (lambda ()
2844 (if (not (anything-pos-multiline-p))
2845 (forward-line 1)
2846 (let ((header-pos (anything-get-next-header-pos))
2847 (separator-pos (anything-get-next-candidate-separator-pos)))
2848 (cond ((and separator-pos
2849 (or (null header-pos) (< separator-pos header-pos)))
2850 (goto-char separator-pos))
2851 (header-pos
2852 (goto-char header-pos))))))
2853 'line 'next))
2855 (defun anything-previous-page ()
2856 "Move selection back with a pageful."
2857 (interactive)
2858 (anything-move-selection-common
2859 (lambda ()
2860 (condition-case nil
2861 (scroll-down)
2862 (beginning-of-buffer (goto-char (point-min)))))
2863 'page 'previous))
2865 (defun anything-next-page ()
2866 "Move selection forward with a pageful."
2867 (interactive)
2868 (anything-move-selection-common
2869 (lambda ()
2870 (condition-case nil
2871 (scroll-up)
2872 (end-of-buffer (goto-char (point-max)))))
2873 'page 'next))
2875 (defun anything-beginning-of-buffer ()
2876 "Move selection at the top."
2877 (interactive)
2878 (anything-move-selection-common
2879 (lambda () (goto-char (point-min)))
2880 'edge 'previous))
2882 (defun anything-end-of-buffer ()
2883 "Move selection at the bottom."
2884 (interactive)
2885 (anything-move-selection-common
2886 (lambda () (goto-char (point-max)))
2887 'edge 'next))
2889 (defun anything-previous-source ()
2890 "Move selection to the previous source."
2891 (interactive)
2892 (anything-move-selection-common
2893 (lambda ()
2894 (forward-line -1)
2895 (if (bobp)
2896 (goto-char (point-max))
2897 (anything-skip-header-and-separator-line 'previous))
2898 (goto-char (anything-get-previous-header-pos))
2899 (forward-line 1))
2900 'source 'previous))
2902 (defun anything-next-source ()
2903 "Move selection to the next source."
2904 (interactive)
2905 (anything-move-selection-common
2906 (lambda ()
2907 (goto-char (or (anything-get-next-header-pos) (point-min))))
2908 'source 'next))
2910 (defun anything-goto-source (source-or-name)
2911 "Move the selection to the source SOURCE-OR-NAME."
2912 (anything-move-selection-common
2913 (lambda ()
2914 (goto-char (point-min))
2915 (let ((name (if (stringp source-or-name) source-or-name
2916 (assoc-default 'name source-or-name))))
2917 (condition-case err
2918 (while (not (string= name (anything-current-line-contents)))
2919 (goto-char (anything-get-next-header-pos)))
2920 (error (message "")))))
2921 'source 'next))
2923 (defun anything-mark-current-line (&optional resumep)
2924 "Move `anything-selection-overlay' to current line.
2925 Note that this is not related with visibles marks, which are used
2926 to mark candidates."
2927 (with-anything-window
2928 (when resumep
2929 (goto-char anything-selection-point))
2930 (move-overlay
2931 anything-selection-overlay (point-at-bol)
2932 (if (anything-pos-multiline-p)
2933 (let ((header-pos (anything-get-next-header-pos))
2934 (separator-pos (anything-get-next-candidate-separator-pos)))
2935 (or (and (null header-pos) separator-pos)
2936 (and header-pos separator-pos (< separator-pos header-pos)
2937 separator-pos)
2938 header-pos
2939 (point-max)))
2940 (1+ (point-at-eol))))
2941 (setq anything-selection-point (overlay-start anything-selection-overlay)))
2942 (anything-follow-execute-persistent-action-maybe))
2944 (defun anything-this-command-key ()
2945 (event-basic-type (elt (this-command-keys-vector) 0)))
2946 ;; (progn (read-key-sequence "Key: ") (p (anything-this-command-key)))
2948 (defun anything-select-with-shortcut-internal (types get-key-func)
2949 (if (memq anything-enable-shortcuts types)
2950 (save-selected-window
2951 (select-window (anything-window))
2952 (let* ((key (funcall get-key-func))
2953 (overlay (ignore-errors (nth (position key anything-shortcut-keys)
2954 anything-digit-overlays))))
2955 (if (not (and overlay (overlay-buffer overlay)))
2956 (when (numberp key)
2957 (select-window (minibuffer-window))
2958 (self-insert-command 1))
2959 (goto-char (overlay-start overlay))
2960 (anything-mark-current-line)
2961 (anything-exit-minibuffer))))
2962 (self-insert-command 1)))
2964 (defun anything-select-with-prefix-shortcut ()
2965 "Invoke default action with prefix shortcut."
2966 (interactive)
2967 (anything-select-with-shortcut-internal
2968 '(prefix)
2969 (lambda () (read-event "Select shortcut key: "))))
2971 (defun anything-select-with-digit-shortcut ()
2972 "Invoke default action with digit/alphabet shortcut."
2973 (interactive)
2974 (anything-select-with-shortcut-internal
2975 '(alphabet t) 'anything-this-command-key))
2977 ;; (setq anything-enable-shortcuts 'prefix)
2978 ;; (define-key anything-map "@" 'anything-select-with-prefix-shortcut)
2979 ;; (define-key anything-map (kbd "<f18>") 'anything-select-with-prefix-shortcut)
2981 (defvar anything-exit-status 0
2982 "Flag to inform whether anything have aborted or quitted.
2983 Exit with 0 mean anything exit executing an action.
2984 Exit with 1 mean anything abort with \\[keyboard-quit]
2985 It is useful for example to restore a window config if anything abort
2986 in special cases.
2987 See `anything-exit-minibuffer' and `anything-keyboard-quit'.")
2989 (defun anything-exit-minibuffer ()
2990 "Select the current candidate by exiting the minibuffer."
2991 (interactive)
2992 (unless anything-current-prefix-arg
2993 (setq anything-current-prefix-arg current-prefix-arg))
2994 (exit-minibuffer))
2996 (defun anything-keyboard-quit ()
2997 "Quit minibuffer in anything.
2998 If action buffer is displayed, kill it."
2999 (interactive)
3000 (when (get-buffer-window anything-action-buffer 'visible)
3001 (kill-buffer anything-action-buffer))
3002 (setq anything-exit-status 1)
3003 (abort-recursive-edit))
3005 (defun anything-get-next-header-pos ()
3006 "Return the position of the next header from point."
3007 (next-single-property-change (point) 'anything-header))
3009 (defun anything-get-previous-header-pos ()
3010 "Return the position of the previous header from point."
3011 (previous-single-property-change (point) 'anything-header))
3013 (defun anything-pos-multiline-p ()
3014 "Return non-nil if the current position is in the multiline source region."
3015 (get-text-property (point) 'anything-multiline))
3017 (defun anything-get-next-candidate-separator-pos ()
3018 "Return the position of the next candidate separator from point."
3019 (next-single-property-change (point) 'anything-candidate-separator))
3021 (defun anything-get-previous-candidate-separator-pos ()
3022 "Return the position of the previous candidate separator from point."
3023 (previous-single-property-change (point) 'anything-candidate-separator))
3025 (defun anything-pos-header-line-p ()
3026 "Return t if the current line is a header line."
3027 (or (get-text-property (point-at-bol) 'anything-header)
3028 (get-text-property (point-at-bol) 'anything-header-separator)))
3030 (defun anything-pos-candidate-separator-p ()
3031 "Return t if the current line is a candidate separator."
3032 (get-text-property (point-at-bol) 'anything-candidate-separator))
3035 ;; (@* "Core: help")
3036 (defun anything-help-internal (bufname insert-content-fn)
3037 "Show long message during `anything' session in BUFNAME.
3038 INSERT-CONTENT-FN is the text to be displayed in BUFNAME."
3039 (save-window-excursion
3040 (select-window (anything-window))
3041 (delete-other-windows)
3042 (switch-to-buffer (get-buffer-create bufname))
3043 (erase-buffer)
3044 (funcall insert-content-fn)
3045 (setq mode-line-format "%b (SPC,C-v:NextPage b,M-v:PrevPage other:Exit)")
3046 (setq cursor-type nil)
3047 (goto-char 1)
3048 (anything-help-event-loop)))
3050 (defun anything-help-event-loop ()
3051 (ignore-errors
3052 (loop for event = (read-event) do
3053 (case event
3054 ((?\C-v ? ) (scroll-up))
3055 ((?\M-v ?b) (scroll-down))
3056 (t (return))))))
3058 (defun anything-help ()
3059 "Help of `anything'."
3060 (interactive)
3061 (anything-help-internal
3062 " *Anything Help*"
3063 (lambda ()
3064 (insert (substitute-command-keys
3065 (anything-interpret-value anything-help-message)))
3066 (org-mode))))
3068 (defun anything-debug-output ()
3069 "Show all anything-related variables at this time."
3070 (interactive)
3071 (anything-help-internal " *Anything Debug*" 'anything-debug-output-function))
3073 (defun anything-debug-output-function (&optional vars)
3074 (message "Calculating all anything-related values...")
3075 (insert "If you debug some variables or forms, set `anything-debug-forms'
3076 to a list of forms.\n\n")
3077 (dolist (v (or vars
3078 anything-debug-forms
3079 (apropos-internal "^anything-" 'boundp)))
3080 (insert "** "
3081 (pp-to-string v) "\n"
3082 (pp-to-string (with-current-buffer anything-buffer (eval v))) "\n"))
3083 (message "Calculating all anything-related values...Done"))
3086 ;; (@* "Core: misc")
3087 (defun anything-kill-buffer-hook ()
3088 "Remove tick entry from `anything-tick-hash' when killing a buffer."
3089 (loop for key being the hash-keys in anything-tick-hash
3090 if (string-match (format "^%s/" (regexp-quote (buffer-name))) key)
3091 do (remhash key anything-tick-hash)))
3092 (add-hook 'kill-buffer-hook 'anything-kill-buffer-hook)
3094 (defun anything-preselect (candidate-or-regexp)
3095 "Move `anything-selection-overlay' to CANDIDATE-OR-REGEXP on startup."
3096 (with-anything-window
3097 (when candidate-or-regexp
3098 (goto-char (point-min))
3099 ;; go to first candidate of first source
3100 (forward-line 1)
3101 (let ((start (point)))
3102 (or (re-search-forward
3103 (concat "^" (regexp-quote candidate-or-regexp) "$") nil t)
3104 (re-search-forward candidate-or-regexp nil t)
3105 (search-forward candidate-or-regexp nil t)
3106 (goto-char start))))
3107 (anything-mark-current-line)))
3109 (defun anything-delete-current-selection ()
3110 "Delete the currently selected item."
3111 (interactive)
3112 (with-anything-window
3113 (cond ((anything-pos-multiline-p)
3114 (anything-aif (anything-get-next-candidate-separator-pos)
3115 (delete-region (point-at-bol)
3116 (1+ (progn (goto-char it) (point-at-eol))))
3117 ;; last candidate
3118 (goto-char (anything-get-previous-candidate-separator-pos))
3119 (delete-region (point-at-bol) (point-max)))
3120 (when (anything-end-of-source-p)
3121 (goto-char (or (anything-get-previous-candidate-separator-pos)
3122 (point-min)))
3123 (forward-line 1)))
3125 (delete-region (point-at-bol) (1+ (point-at-eol)))
3126 (when (anything-end-of-source-p) (forward-line -1))))
3127 (anything-mark-current-line)))
3129 (defun anything-end-of-source-p ()
3130 "Return non--nil if we are at eob or end of source."
3131 (save-excursion
3132 (forward-line 1)
3133 (or (eq (point-at-bol) (point-at-eol))
3134 (anything-pos-header-line-p)
3135 (eobp))))
3137 (defun anything-edit-current-selection-internal (func)
3138 (with-anything-window
3139 (beginning-of-line)
3140 (let ((realvalue (get-text-property (point) 'anything-realvalue)))
3141 (funcall func)
3142 (beginning-of-line)
3143 (and realvalue
3144 (put-text-property (point) (point-at-eol)
3145 'anything-realvalue realvalue))
3146 (anything-mark-current-line))))
3148 (defmacro anything-edit-current-selection (&rest forms)
3149 "Evaluate FORMS at current selection in the anything buffer.
3150 You can edit the line."
3151 (declare (indent 0) (debug t))
3152 `(anything-edit-current-selection-internal
3153 (lambda () ,@forms)))
3155 (defun anything-set-pattern (pattern &optional noupdate)
3156 "Set minibuffer contents to PATTERN.
3157 if optional NOUPDATE is non-nil, anything buffer is not changed."
3158 (with-selected-window (or (active-minibuffer-window) (minibuffer-window))
3159 (delete-minibuffer-contents)
3160 (insert pattern))
3161 (when noupdate
3162 (setq anything-pattern pattern)
3163 (anything-hooks 'cleanup)
3164 (run-with-idle-timer 0 nil 'anything-hooks 'setup)))
3166 (defun anything-delete-minibuffer-contents ()
3167 "Same as `delete-minibuffer-contents' but this is a command."
3168 (interactive)
3169 (anything-set-pattern ""))
3170 (defalias 'anything-delete-minibuffer-content 'anything-delete-minibuffer-contents)
3173 ;;; Plugins
3175 ;; (@* "Built-in plug-in: type")
3176 (defun anything-compile-source--type (source)
3177 (anything-aif (assoc-default 'type source)
3178 (append source (assoc-default it anything-type-attributes) nil)
3179 source))
3181 ;; `define-anything-type-attribute' is public API.
3183 (defun anything-add-type-attribute (type definition)
3184 (anything-aif (assq type anything-type-attributes)
3185 (setq anything-type-attributes (delete it anything-type-attributes)))
3186 (push (cons type definition) anything-type-attributes))
3188 (defvar anything-types nil)
3189 (defun anything-document-type-attribute (type doc)
3190 (add-to-list 'anything-types type t)
3191 (put type 'anything-typeattrdoc
3192 (concat "- " (symbol-name type) "\n\n" doc "\n")))
3194 (defadvice documentation-property (after anything-document-type-attribute activate)
3195 "Display type attributes' documentation as `anything-type-attributes' docstring."
3196 (when (eq (ad-get-arg 0) 'anything-type-attributes)
3197 (setq ad-return-value
3198 (concat ad-return-value "\n\n++++ Types currently defined ++++\n"
3199 (mapconcat (lambda (sym) (get sym 'anything-typeattrdoc))
3200 anything-types "\n")))))
3202 ;; (@* "Built-in plug-in: dummy")
3203 (defun anything-dummy-candidate (candidate source)
3204 "Use `anything-pattern' as CANDIDATE in SOURCE."
3205 ;; `source' is defined in filtered-candidate-transformer
3206 (list anything-pattern))
3208 (defun anything-compile-source--dummy (source)
3209 (if (assoc 'dummy source)
3210 (append source
3211 '((candidates "dummy")
3212 (accept-empty)
3213 (match identity)
3214 (filtered-candidate-transformer . anything-dummy-candidate)
3215 (disable-shortcuts)
3216 (volatile)))
3217 source))
3219 ;; (@* "Built-in plug-in: disable-shortcuts")
3220 (defvar anything-orig-enable-shortcuts nil)
3221 (defun anything-save-enable-shortcuts ()
3222 (anything-once
3223 (lambda ()
3224 (setq anything-orig-enable-shortcuts anything-enable-shortcuts
3225 anything-enable-shortcuts nil))))
3227 (defun anything-compile-source--disable-shortcuts (source)
3228 (if (assoc 'disable-shortcuts source)
3229 (append `((init ,@(anything-mklist (assoc-default 'init source))
3230 anything-save-enable-shortcuts)
3231 (resume ,@(anything-mklist (assoc-default 'resume source))
3232 anything-save-enable-shortcuts)
3233 (cleanup ,@(anything-mklist (assoc-default 'cleanup source))
3234 (lambda () (setq anything-enable-shortcuts
3235 anything-orig-enable-shortcuts))))
3236 source)
3237 source))
3239 ;; (@* "Built-in plug-in: candidates-in-buffer")
3240 (defun anything-candidates-in-buffer ()
3241 "Get candidates from the candidates buffer according to `anything-pattern'.
3243 BUFFER is `anything-candidate-buffer' by default. Each
3244 candidate must be placed in one line. This function is meant to
3245 be used in candidates-in-buffer or candidates attribute of an
3246 anything source. Especially fast for many (1000+) candidates.
3249 '((name . \"many files\")
3250 (init . (lambda () (with-current-buffer (anything-candidate-buffer 'local)
3251 (insert-many-filenames))))
3252 (search re-search-forward) ; optional
3253 (candidates-in-buffer)
3254 (type . file))
3256 +===============================================================+
3257 | The new way of making and narrowing candidates: Using buffers |
3258 +===============================================================+
3260 By default, `anything' makes candidates by evaluating the
3261 candidates function, then narrows them by `string-match' for each
3262 candidate.
3264 But this way is very slow for many candidates. The new way is
3265 storing all candidates in a buffer and narrowing them by
3266 `re-search-forward'. Search function is customizable by search
3267 attribute. The important point is that buffer processing is MUCH
3268 FASTER than string list processing and is the Emacs way.
3270 The init function writes all candidates to a newly-created
3271 candidate buffer. The candidates buffer is created or specified
3272 by `anything-candidate-buffer'. Candidates are stored in a line.
3274 The candidates function narrows all candidates, IOW creates a
3275 subset of candidates dynamically. It is the task of
3276 `anything-candidates-in-buffer'. As long as
3277 `anything-candidate-buffer' is used,`(candidates-in-buffer)' is
3278 sufficient in most cases.
3280 Note that `(candidates-in-buffer)' is shortcut of three attributes:
3281 (candidates . anything-candidates-in-buffer)
3282 (volatile)
3283 (match identity)
3284 And `(candidates-in-buffer . func)' is shortcut of three attributes:
3285 (candidates . func)
3286 (volatile)
3287 (match identity)
3288 The expansion is performed in `anything-get-sources'.
3290 The candidates-in-buffer attribute implies the volatile attribute.
3291 The volatile attribute is needed because `anything-candidates-in-buffer'
3292 creates candidates dynamically and need to be called everytime
3293 `anything-pattern' changes.
3295 Because `anything-candidates-in-buffer' plays the role of `match' attribute
3296 function, specifying `(match identity)' makes the source slightly faster.
3298 To customize `anything-candidates-in-buffer' behavior, use search,
3299 get-line and search-from-end attributes. See also `anything-sources' docstring."
3300 (declare (special source))
3301 (anything-candidates-in-buffer-1
3302 (anything-candidate-buffer)
3303 anything-pattern
3304 (or (assoc-default 'get-line source)
3305 #'buffer-substring-no-properties)
3306 ;; use external variable `source'.
3307 (or (assoc-default 'search source)
3308 (if (assoc 'search-from-end source)
3309 '(anything-candidates-in-buffer-search-from-end)
3310 '(anything-candidates-in-buffer-search-from-start)))
3311 (anything-candidate-number-limit source)
3312 (assoc 'search-from-end source)))
3314 (defun anything-candidates-in-buffer-search-from-start (pattern)
3315 "Search PATTERN with `re-search-forward' with bound and noerror args."
3316 (re-search-forward pattern nil t))
3318 (defun anything-candidates-in-buffer-search-from-end (pattern)
3319 "Search PATTERN with `re-search-backward' with bound and noerror args."
3320 (re-search-backward pattern nil t))
3322 (defun anything-candidates-in-buffer-1 (buffer pattern get-line-fn
3323 search-fns limit search-from-end)
3324 ;; buffer == nil when candidates buffer does not exist.
3325 (when buffer
3326 (with-current-buffer buffer
3327 (let ((start-point (if search-from-end (point-max) (point-min)))
3328 (endp (if search-from-end #'bobp #'eobp)))
3329 (goto-char (1- start-point))
3330 (if (string= pattern "")
3331 (anything-initial-candidates-from-candidate-buffer
3332 endp get-line-fn limit search-from-end)
3333 (anything-search-from-candidate-buffer
3334 pattern get-line-fn search-fns limit search-from-end
3335 start-point endp))))))
3337 (defun anything-point-is-moved (proc)
3338 "If point is moved after executing PROC, return t, otherwise nil."
3339 (/= (point) (progn (funcall proc) (point))))
3341 (defun anything-search-from-candidate-buffer (pattern get-line-fn search-fns
3342 limit search-from-end
3343 start-point endp)
3344 (let (buffer-read-only
3345 matches exit newmatches)
3346 (anything-search-from-candidate-buffer-internal
3347 (lambda ()
3348 (clrhash anything-cib-hash)
3349 (dolist (searcher search-fns)
3350 (goto-char start-point)
3351 (setq newmatches nil)
3352 (loop with item-count = 0
3353 while (funcall searcher pattern)
3354 for cand = (funcall get-line-fn (point-at-bol) (point-at-eol))
3355 do (anything-accumulate-candidates-internal
3356 cand newmatches anything-cib-hash item-count limit)
3357 unless (anything-point-is-moved
3358 (lambda ()
3359 (if search-from-end
3360 (goto-char (1- (point-at-bol)))
3361 (forward-line 1))))
3362 return nil)
3363 (setq matches (append matches (nreverse newmatches)))
3364 (if exit (return)))
3365 (delq nil matches)))))
3367 (defun anything-initial-candidates-from-candidate-buffer (endp get-line-fn limit search-from-end)
3368 (delq nil (loop with next-line-fn =
3369 (if search-from-end
3370 (lambda (x) (goto-char (max (1- (point-at-bol)) 1)))
3371 #'forward-line)
3372 until (funcall endp)
3373 for i from 1 to limit
3374 collect (funcall get-line-fn (point-at-bol) (point-at-eol))
3375 do (funcall next-line-fn 1))))
3377 (defun anything-search-from-candidate-buffer-internal (search-fn)
3378 (goto-char (point-min))
3379 (insert "\n")
3380 (goto-char (point-max))
3381 (insert "\n")
3382 (unwind-protect
3383 (funcall search-fn)
3384 (goto-char (point-min))
3385 (delete-char 1)
3386 (goto-char (1- (point-max)))
3387 (delete-char 1)
3389 (set-buffer-modified-p nil)))
3391 (defun anything-candidate-buffer (&optional create-or-buffer)
3392 "Register and return a buffer containing candidates of current source.
3393 `anything-candidate-buffer' searches buffer-local candidates buffer first,
3394 then global candidates buffer.
3396 Acceptable values of CREATE-OR-BUFFER:
3398 - nil (omit)
3399 Only return the candidates buffer.
3400 - a buffer
3401 Register a buffer as a candidates buffer.
3402 - 'global
3403 Create a new global candidates buffer,
3404 named \" *anything candidates:SOURCE*\".
3405 - other non-nil value
3406 Create a new local candidates buffer,
3407 named \" *anything candidates:SOURCE*ANYTHING-CURRENT-BUFFER\"."
3408 (let* ((global-bname (format " *anything candidates:%s*" anything-source-name))
3409 (local-bname (format " *anything candidates:%s*%s"
3410 anything-source-name
3411 (buffer-name anything-current-buffer)))
3412 (register-func
3413 (lambda ()
3414 (setq anything-candidate-buffer-alist
3415 (cons (cons anything-source-name create-or-buffer)
3416 (delete (assoc anything-source-name
3417 anything-candidate-buffer-alist)
3418 anything-candidate-buffer-alist)))))
3419 (kill-buffers-func
3420 (lambda ()
3421 (loop for b in (buffer-list)
3422 if (string-match (format "^%s" (regexp-quote global-bname))
3423 (buffer-name b))
3424 do (kill-buffer b))))
3425 (create-func
3426 (lambda ()
3427 (with-current-buffer
3428 (get-buffer-create (if (eq create-or-buffer 'global)
3429 global-bname
3430 local-bname))
3431 (buffer-disable-undo)
3432 (erase-buffer)
3433 (font-lock-mode -1))))
3434 (return-func
3435 (lambda ()
3436 (or (get-buffer local-bname)
3437 (get-buffer global-bname)
3438 (anything-aif (assoc-default anything-source-name
3439 anything-candidate-buffer-alist)
3440 (and (buffer-live-p it) it))))))
3441 (when create-or-buffer
3442 (funcall register-func)
3443 (unless (bufferp create-or-buffer)
3444 (and (eq create-or-buffer 'global) (funcall kill-buffers-func))
3445 (funcall create-func)))
3446 (funcall return-func)))
3448 (defun anything-compile-source--candidates-in-buffer (source)
3449 (anything-aif (assoc 'candidates-in-buffer source)
3450 (append source
3451 `((candidates . ,(or (cdr it) 'anything-candidates-in-buffer))
3452 (volatile) (match identity)))
3453 source))
3456 ;; (@* "Utility: resplit anything window")
3457 (defun anything-toggle-resplit-window ()
3458 "Toggle resplit anything window, vertically or horizontally."
3459 (interactive)
3460 (with-anything-window
3461 (let ((before-height (window-height)))
3462 (delete-window)
3463 (set-window-buffer
3464 (select-window (if (= (window-height) before-height)
3465 (prog1
3466 (split-window-vertically)
3467 (setq anything-split-window-state 'vertical))
3468 (setq anything-split-window-state 'horizontal)
3469 (split-window-horizontally)))
3470 anything-buffer))))
3472 ;; (@* "Utility: Resize anything window.")
3473 (defun anything-enlarge-window-1 (n)
3474 "Enlarge or narrow anything window.
3475 If N is positive enlarge, if negative narrow."
3476 (unless anything-samewindow
3477 (let ((horizontal-p (eq anything-split-window-state 'horizontal)))
3478 (with-anything-window
3479 (enlarge-window n horizontal-p)))))
3481 (defun anything-narrow-window ()
3482 "Narrow anything window."
3483 (interactive)
3484 (anything-enlarge-window-1 -1))
3486 (defun anything-enlarge-window ()
3487 "Enlarge anything window."
3488 (interactive)
3489 (anything-enlarge-window-1 1))
3491 ;; (@* "Utility: select another action by key")
3492 (defun anything-select-nth-action (n)
3493 "Select the N nth action for the currently selected candidate."
3494 (setq anything-saved-selection (anything-get-selection))
3495 (unless anything-saved-selection
3496 (error "Nothing is selected"))
3497 (setq anything-saved-action (anything-get-nth-action n (anything-get-action)))
3498 (anything-exit-minibuffer))
3500 (defun anything-get-nth-action (n action)
3501 (cond ((and (zerop n) (functionp action))
3502 action)
3503 ((listp action)
3504 (or (cdr (elt action n))
3505 (error "No such action")))
3506 ((and (functionp action) (< 0 n))
3507 (error "Sole action"))
3509 (error "Error in `anything-select-nth-action'"))))
3511 (defun anything-select-2nd-action ()
3512 "Select the 2nd action for the currently selected candidate."
3513 (interactive)
3514 (anything-select-nth-action 1))
3516 (defun anything-select-3rd-action ()
3517 "Select the 3rd action for the currently selected candidate."
3518 (interactive)
3519 (anything-select-nth-action 2))
3521 (defun anything-select-4th-action ()
3522 "Select the 4th action for the currently selected candidate."
3523 (interactive)
3524 (anything-select-nth-action 3))
3526 (defun anything-select-2nd-action-or-end-of-line ()
3527 "Select the 2nd action for the currently selected candidate.
3528 This happen when point is at the end of minibuffer.
3529 Otherwise goto the end of minibuffer."
3530 (interactive)
3531 (if (eolp)
3532 (anything-select-nth-action 1)
3533 (end-of-line)))
3535 ;; (@* "Utility: Persistent Action")
3536 (defmacro with-anything-display-same-window (&rest body)
3537 "Execute BODY in the window used for persistent action.
3538 Make `pop-to-buffer' and `display-buffer' display in the same window."
3539 (declare (indent 0) (debug t))
3540 `(let ((display-buffer-function 'anything-persistent-action-display-buffer))
3541 ,@body))
3543 (defvar anything-persistent-action-display-window nil)
3544 (defun anything-initialize-persistent-action ()
3545 (set (make-local-variable 'anything-persistent-action-display-window) nil))
3547 (defun* anything-execute-persistent-action (&optional (attr 'persistent-action) onewindow)
3548 "Perform the associated action ATTR without quitting anything.
3549 ATTR default is 'persistent-action', but it can be anything else.
3550 In this case you have to add this new attribute to your source.
3551 When `anything-samewindow' and ONEWINDOW are non--nil,
3552 the anything window is never split in persistent action."
3553 (interactive)
3554 (anything-log "executing persistent-action")
3555 (with-anything-window
3556 (save-selected-window
3557 (anything-select-persistent-action-window onewindow)
3558 (anything-log-eval (current-buffer))
3559 (let ((anything-in-persistent-action t))
3560 (with-anything-display-same-window
3561 (anything-execute-selection-action
3563 (or (assoc-default attr (anything-get-current-source))
3564 (anything-get-action))
3566 (anything-log-run-hook 'anything-after-persistent-action-hook))))))
3569 (defun anything-persistent-action-display-window (&optional onewindow)
3570 "Return the window that will be used for presistent action.
3571 If ONEWINDOW is non--nil window will not be splitted in persistent action
3572 if `anything-samewindow' is non--nil also."
3573 (with-anything-window
3574 (setq anything-persistent-action-display-window
3575 (cond ((window-live-p anything-persistent-action-display-window)
3576 anything-persistent-action-display-window)
3577 ((and anything-samewindow (one-window-p t) (not onewindow))
3578 (split-window))
3579 ((get-buffer-window anything-current-buffer))
3581 (next-window (selected-window) 1))))))
3583 (defun anything-select-persistent-action-window (&optional onewindow)
3584 "Select the window that will be used for persistent action.
3585 See `anything-persistent-action-display-window' for how to use ONEWINDOW."
3586 (select-window (get-buffer-window (anything-buffer-get)))
3587 (select-window
3588 (setq minibuffer-scroll-window
3589 (anything-persistent-action-display-window onewindow))))
3591 (defun anything-persistent-action-display-buffer (buf &optional not-this-window)
3592 "Make `pop-to-buffer' and `display-buffer' display in the same window.
3593 If `anything-persistent-action-use-special-display' is non-nil and
3594 BUF is to be displayed by `special-display-function', use it.
3595 Otherwise ignores `special-display-buffer-names' and `special-display-regexps'.
3596 Argument NOT-THIS-WINDOW if present will be used as
3597 second argument of `display-buffer'."
3598 (let* ((name (buffer-name buf))
3599 display-buffer-function pop-up-windows pop-up-frames
3600 (same-window-regexps
3601 (unless (and anything-persistent-action-use-special-display
3602 (or (member name
3603 (mapcar (lambda (x) (or (car-safe x) x))
3604 special-display-buffer-names))
3605 (remove-if-not
3606 (lambda (x) (string-match (or (car-safe x) x) name))
3607 special-display-regexps)))
3608 '("."))))
3609 (display-buffer buf not-this-window)))
3611 ;; scroll-other-window(-down)? for persistent-action
3612 (defun anything-scroll-other-window-base (command)
3613 (with-selected-window (anything-persistent-action-display-window)
3614 (funcall command anything-scroll-amount)))
3616 (defun anything-scroll-other-window ()
3617 "Scroll other window (not *Anything* window) upward."
3618 (interactive)
3619 (anything-scroll-other-window-base 'scroll-up))
3621 (defun anything-scroll-other-window-down ()
3622 "Scroll other window (not *Anything* window) downward."
3623 (interactive)
3624 (anything-scroll-other-window-base 'scroll-down))
3627 ;; (@* "Utility: Visible Mark")
3628 (defface anything-visible-mark
3629 '((((min-colors 88) (background dark))
3630 (:background "green1" :foreground "black"))
3631 (((background dark)) (:background "green" :foreground "black"))
3632 (((min-colors 88)) (:background "green1"))
3633 (t (:background "green")))
3634 "Face for visible mark."
3635 :group 'anything)
3637 (defvar anything-visible-mark-face 'anything-visible-mark)
3638 (defvar anything-visible-mark-overlays nil)
3640 (defun anything-clear-visible-mark ()
3641 (with-current-buffer (anything-buffer-get)
3642 (mapc 'delete-overlay anything-visible-mark-overlays)
3643 (set (make-local-variable 'anything-visible-mark-overlays) nil)))
3644 (add-hook 'anything-after-initialize-hook 'anything-clear-visible-mark)
3646 (defvar anything-marked-candidates nil
3647 "Marked candadates. List of \(source . real\) pair.")
3649 (defun anything-this-visible-mark ()
3650 (loop for o in anything-visible-mark-overlays
3651 when (equal (point-at-bol) (overlay-start o))
3652 return o))
3654 (defun anything-delete-visible-mark (overlay)
3655 (setq anything-marked-candidates
3656 (remove
3657 (cons (anything-get-current-source) (anything-get-selection))
3658 anything-marked-candidates))
3659 (delete-overlay overlay)
3660 (setq anything-visible-mark-overlays
3661 (delq overlay anything-visible-mark-overlays)))
3663 (defun anything-make-visible-mark ()
3664 (let ((o (make-overlay (point-at-bol) (1+ (point-at-eol)))))
3665 (overlay-put o 'face anything-visible-mark-face)
3666 (overlay-put o 'source (assoc-default 'name (anything-get-current-source)))
3667 (overlay-put o 'string (buffer-substring (overlay-start o) (overlay-end o)))
3668 (overlay-put o 'real (anything-get-selection))
3669 (add-to-list 'anything-visible-mark-overlays o))
3670 (push (cons (anything-get-current-source) (anything-get-selection))
3671 anything-marked-candidates))
3673 (defun anything-toggle-visible-mark ()
3674 "Toggle anything visible mark at point."
3675 (interactive)
3676 (with-anything-window
3677 (anything-aif (anything-this-visible-mark)
3678 (anything-delete-visible-mark it)
3679 (anything-make-visible-mark))
3680 (anything-next-line)))
3682 (defun anything-display-all-visible-marks ()
3683 "Show all `anything' visible marks strings."
3684 (interactive)
3685 (with-anything-window
3686 (lexical-let ((overlays (reverse anything-visible-mark-overlays)))
3687 (anything-run-after-quit
3688 (lambda ()
3689 (with-output-to-temp-buffer "*anything visible marks*"
3690 (dolist (o overlays) (princ (overlay-get o 'string)))))))))
3692 (defun anything-marked-candidates ()
3693 "Return marked candidates of current source if any.
3694 Otherwise one element list of current selection.
3696 It is analogous to `dired-get-marked-files'."
3697 (with-current-buffer (anything-buffer-get)
3698 (let ((cands
3699 (if anything-marked-candidates
3700 (loop with current-src = (anything-get-current-source)
3701 for (source . real) in (reverse anything-marked-candidates)
3702 when (equal current-src source)
3703 collect (anything-coerce-selection real source))
3704 (list (anything-get-selection)))))
3705 (anything-log-eval cands)
3706 cands)))
3708 (defun anything-reset-marked-candidates ()
3709 (with-current-buffer (anything-buffer-get)
3710 (set (make-local-variable 'anything-marked-candidates) nil)))
3712 (add-hook 'anything-after-initialize-hook 'anything-reset-marked-candidates)
3713 ;; (add-hook 'anything-after-action-hook 'anything-reset-marked-candidates)
3715 (defun anything-current-source-name= (name)
3716 (save-excursion
3717 (goto-char (anything-get-previous-header-pos))
3718 (equal name (anything-current-line-contents))))
3720 (defun anything-revive-visible-mark ()
3721 "Restore marked candidates when anything update display."
3722 (with-current-buffer anything-buffer
3723 (dolist (o anything-visible-mark-overlays)
3724 (goto-char (point-min))
3725 (while (and (search-forward (overlay-get o 'string) nil t)
3726 (anything-current-source-name= (overlay-get o 'source)))
3727 ;; Calculate real value of candidate.
3728 ;; It can be nil if candidate have only a display value.
3729 (let ((real (get-text-property (point-at-bol 0) 'anything-realvalue)))
3730 (if real
3731 ;; Check if real value of current candidate is the same
3732 ;; that the one stored in overlay.
3733 (and (string= (overlay-get o 'real) real)
3734 (move-overlay o (point-at-bol 0) (1+ (point-at-eol 0))))
3735 (move-overlay o (point-at-bol 0) (1+ (point-at-eol 0)))))))))
3736 (add-hook 'anything-update-hook 'anything-revive-visible-mark)
3738 (defun anything-next-point-in-list (curpos points &optional prev)
3739 (cond
3740 ;; rule out special cases
3741 ((null points) curpos)
3742 ((and prev (< curpos (car points))) curpos)
3743 ((< (car (last points)) curpos)
3744 (if prev (car (last points)) curpos))
3746 (nth (if prev
3747 (loop for pt in points
3748 for i from 0
3749 if (<= curpos pt)
3750 return (1- i))
3751 (loop for pt in points
3752 for i from 0
3753 if (< curpos pt)
3754 return i))
3755 points))))
3757 (defun anything-next-visible-mark (&optional prev)
3758 "Move next anything visible mark.
3759 If PREV is non-nil move to precedent."
3760 (interactive)
3761 (with-anything-window
3762 (ignore-errors
3763 (goto-char (anything-next-point-in-list
3764 (point)
3765 (sort (mapcar 'overlay-start anything-visible-mark-overlays) '<)
3766 prev)))
3767 (anything-mark-current-line)))
3769 (defun anything-prev-visible-mark ()
3770 "Move previous anything visible mark."
3771 (interactive)
3772 (anything-next-visible-mark t))
3774 ;; (@* "Utility: Selection Paste")
3775 (defun anything-yank-selection ()
3776 "Set minibuffer contents to current selection."
3777 (interactive)
3778 (anything-set-pattern (anything-get-selection nil t)))
3780 (defun anything-kill-selection-and-quit ()
3781 "Store current selection to kill ring.
3782 You can paste it by typing \\[yank]."
3783 (interactive)
3784 (anything-run-after-quit
3785 (lambda (sel)
3786 (kill-new sel)
3787 (message "Killed: %s" sel))
3788 (anything-get-selection nil t)))
3791 ;; (@* "Utility: Automatical execution of persistent-action")
3792 (add-to-list 'minor-mode-alist '(anything-follow-mode " AFollow"))
3793 (defun anything-follow-mode ()
3794 "If this mode is on, persistent action is executed everytime the cursor is moved."
3795 (interactive)
3796 (with-current-buffer anything-buffer
3797 (setq anything-follow-mode (not anything-follow-mode))
3798 (message "anything-follow-mode is %s"
3799 (if anything-follow-mode "enabled" "disabled"))))
3801 (defun anything-follow-execute-persistent-action-maybe ()
3802 "Execute persistent action in mode `anything-follow-mode'.
3803 This happen after `anything-input-idle-delay' secs."
3804 (and (not (get-buffer-window anything-action-buffer 'visible))
3805 (buffer-local-value 'anything-follow-mode
3806 (get-buffer-create anything-buffer))
3807 (sit-for (and anything-input-idle-delay
3808 (max anything-input-idle-delay 0.1)))
3809 (anything-window)
3810 (anything-get-selection)
3811 (save-excursion
3812 (anything-execute-persistent-action))))
3815 ;; (@* "Utility: Migrate `anything-sources' to my-anything command")
3816 (defun anything-migrate-sources ()
3817 "Help to migrate to new `anything' way."
3818 (interactive)
3819 (with-current-buffer (get-buffer-create "*anything migrate*")
3820 (erase-buffer)
3821 (insert (format "\
3822 Setting `anything-sources' directly is not good because
3823 `anything' is not for one command. For now, interactive use of
3824 `anything' (M-x anything) is only for demonstration purpose.
3825 So you should define commands calling `anything'.
3826 I help you to migrate to the new way.
3828 The code below is automatically generated from current
3829 `anything-sources' value. You can use the `my-anything' command
3830 now!
3832 Copy and paste it to your .emacs. Then substitute `my-anything'
3833 for `anything' bindings in all `define-key', `local-set-key' and
3834 `global-set-key' calls.
3836 \(defun my-anything ()
3837 \"Anything command for you.
3839 It is automatically generated by `anything-migrate-sources'.\"
3840 (interactive)
3841 (anything-other-buffer
3843 \"*my-anything*\"))
3844 " anything-sources))
3845 (eval-last-sexp nil)
3846 (substitute-key-definition 'anything 'my-anything global-map)
3847 (pop-to-buffer (current-buffer))))
3850 ;; (@* "Compatibility")
3852 ;; Copied assoc-default from XEmacs version 21.5.12
3853 (unless (fboundp 'assoc-default)
3854 (defun assoc-default (key alist &optional test default)
3855 "Find object KEY in a pseudo-alist ALIST.
3856 ALIST is a list of conses or objects. Each element (or the element's car,
3857 if it is a cons) is compared with KEY by evaluating (TEST (car elt) KEY).
3858 If that is non-nil, the element matches;
3859 then `assoc-default' returns the element's cdr, if it is a cons,
3860 or DEFAULT if the element is not a cons.
3862 If no element matches, the value is nil.
3863 If TEST is omitted or nil, `equal' is used."
3864 (let (found (tail alist) value)
3865 (while (and tail (not found))
3866 (let ((elt (car tail)))
3867 (when (funcall (or test 'equal) (if (consp elt) (car elt) elt) key)
3868 (setq found t value (if (consp elt) (cdr elt) default))))
3869 (setq tail (cdr tail)))
3870 value)))
3872 ;; Function not available in XEmacs,
3873 (unless (fboundp 'minibuffer-contents)
3874 (defun minibuffer-contents ()
3875 "Return the user input in a minbuffer as a string.
3876 The current buffer must be a minibuffer."
3877 (field-string (point-max)))
3879 (defun delete-minibuffer-contents ()
3880 "Delete all user input in a minibuffer.
3881 The current buffer must be a minibuffer."
3882 (delete-field (point-max))))
3884 ;; Function not available in older Emacs (<= 22.1).
3885 (unless (fboundp 'buffer-chars-modified-tick)
3886 (defun buffer-chars-modified-tick (&optional buffer)
3887 "Return BUFFER's character-change tick counter.
3888 Each buffer has a character-change tick counter, which is set to the
3889 value of the buffer's tick counter (see `buffer-modified-tick'), each
3890 time text in that buffer is inserted or deleted. By comparing the
3891 values returned by two individual calls of `buffer-chars-modified-tick',
3892 you can tell whether a character change occurred in that buffer in
3893 between these calls. No argument or nil as argument means use current
3894 buffer as BUFFER."
3895 (with-current-buffer (or buffer (current-buffer))
3896 (if (listp buffer-undo-list)
3897 (length buffer-undo-list)
3898 (buffer-modified-tick)))))
3901 ;; (@* "CUA workaround")
3902 (defadvice cua-delete-region (around anything-avoid-cua activate)
3903 (ignore-errors ad-do-it))
3905 (defadvice copy-region-as-kill (around anything-avoid-cua activate)
3906 (if cua-mode
3907 (ignore-errors ad-do-it)
3908 ad-do-it))
3910 ;;(@* "Attribute Documentation")
3911 (defun anything-describe-anything-attribute (anything-attribute)
3912 "Display the full documentation of ANYTHING-ATTRIBUTE.
3913 ANYTHING-ATTRIBUTE should be a symbol."
3914 (interactive (list (intern
3915 (completing-read
3916 "Describe anything attribute: "
3917 (mapcar 'symbol-name anything-additional-attributes)))))
3918 (with-output-to-temp-buffer "*Help*"
3919 (princ (get anything-attribute 'anything-attrdoc))))
3921 (put 'anything-document-attribute 'lisp-indent-function 2)
3922 (anything-document-attribute 'name "mandatory"
3923 " The name of the source. It is also the heading which appears
3924 above the list of matches from the source. Must be unique. ")
3925 (anything-document-attribute 'header-name "optional"
3926 " A function returning the display string of the header. Its
3927 argument is the name of the source. This attribute is useful to
3928 add an additional information with the source name. ")
3929 (anything-document-attribute 'candidates "mandatory if candidates-in-buffer attribute is not provided"
3930 " Specifies how to retrieve candidates from the source. It can
3931 either be a variable name, a function called with no parameters
3932 or the actual list of candidates.
3934 The list must be a list whose members are strings, symbols
3935 or (DISPLAY . REAL) pairs.
3937 In case of (DISPLAY . REAL) pairs, the DISPLAY string is shown
3938 in the Anything buffer, but the REAL one is used as action
3939 argument when the candidate is selected. This allows a more
3940 readable presentation for candidates which would otherwise be,
3941 for example, too long or have a common part shared with other
3942 candidates which can be safely replaced with an abbreviated
3943 string for display purposes.
3945 Note that if the (DISPLAY . REAL) form is used then pattern
3946 matching is done on the displayed string, not on the real
3947 value.
3949 If the candidates have to be retrieved asynchronously (for
3950 example, by an external command which takes a while to run)
3951 then the function should start the external command
3952 asynchronously and return the associated process object.
3953 Anything will take care of managing the process (receiving the
3954 output from it, killing it if necessary, etc.). The process
3955 should return candidates matching the current pattern (see
3956 variable `anything-pattern'.)
3958 Note that currently results from asynchronous sources appear
3959 last in the anything buffer regardless of their position in
3960 `anything-sources'. ")
3961 (anything-document-attribute 'action "mandatory if type attribute is not provided"
3962 " It is a list of (DISPLAY . FUNCTION) pairs or FUNCTION.
3963 FUNCTION is called with one parameter: the selected candidate.
3965 An action other than the default can be chosen from this list
3966 of actions for the currently selected candidate (by default
3967 with TAB). The DISPLAY string is shown in the completions
3968 buffer and the FUNCTION is invoked when an action is
3969 selected. The first action of the list is the default. ")
3970 (anything-document-attribute 'coerce "optional"
3971 " It's a function called with one argument: the selected candidate.
3973 This function is intended for type convertion.
3974 In normal case, the selected candidate (string) is passed to action function.
3975 If coerce function is specified, it is called just before action function.
3977 Example: converting string to symbol
3978 (coerce . intern) ")
3979 (anything-document-attribute 'type "optional if action attribute is provided"
3980 " Indicates the type of the items the source returns.
3982 Merge attributes not specified in the source itself from
3983 `anything-type-attributes'.
3985 This attribute is implemented by plug-in. ")
3986 (anything-document-attribute 'init "optional"
3987 " Function called with no parameters when anything is started. It
3988 is useful for collecting current state information which can be
3989 used to create the list of candidates later.
3991 For example, if a source needs to work with the current
3992 directory then it can store its value here, because later
3993 anything does its job in the minibuffer and in the
3994 `anything-buffer' and the current directory can be different
3995 there. ")
3996 (anything-document-attribute 'delayed-init "optional"
3997 " Function called with no parameters before candidate function is
3998 called. It is similar with `init' attribute, but its
3999 evaluation is deferred. It is useful to combine with ")
4000 (anything-document-attribute 'match "optional"
4001 " List of functions called with one parameter: a candidate. The
4002 function should return non-nil if the candidate matches the
4003 current pattern (see variable `anything-pattern').
4005 This attribute allows the source to override the default
4006 pattern matching based on `string-match'. It can be used, for
4007 example, to implement a source for file names and do the
4008 pattern matching on the basename of files, since it's more
4009 likely one is typing part of the basename when searching for a
4010 file, instead of some string anywhere else in its path.
4012 If the list contains more than one function then the list of
4013 matching candidates from the source is constructed by appending
4014 the results after invoking the first function on all the
4015 potential candidates, then the next function, and so on. The
4016 matching candidates supplied by the first function appear first
4017 in the list of results and then results from the other
4018 functions, respectively.
4020 This attribute has no effect for asynchronous sources (see
4021 attribute `candidates'), since they perform pattern matching
4022 themselves. ")
4023 (anything-document-attribute 'candidate-transformer "optional"
4024 " It's a function or a list of functions called with one argument
4025 when the completion list from the source is built. The argument
4026 is the list of candidates retrieved from the source. The
4027 function should return a transformed list of candidates which
4028 will be used for the actual completion. If it is a list of
4029 functions, it calls each function sequentially.
4031 This can be used to transform or remove items from the list of
4032 candidates.
4034 Note that `candidates' is run already, so the given transformer
4035 function should also be able to handle candidates with (DISPLAY
4036 . REAL) format. ")
4037 (anything-document-attribute 'filtered-candidate-transformer "optional"
4038 " It has the same format as `candidate-transformer', except the
4039 function is called with two parameters: the candidate list and
4040 the source.
4042 This transformer is run on the candidate list which is already
4043 filtered by the current pattern. While `candidate-transformer'
4044 is run only once, it is run every time the input pattern is
4045 changed.
4047 It can be used to transform the candidate list dynamically, for
4048 example, based on the current pattern.
4050 In some cases it may also be more efficent to perform candidate
4051 transformation here, instead of with `candidate-transformer'
4052 even if this transformation is done every time the pattern is
4053 changed. For example, if a candidate set is very large then
4054 `candidate-transformer' transforms every candidate while only
4055 some of them will actually be dislpayed due to the limit
4056 imposed by `anything-candidate-number-limit'.
4058 Note that `candidates' and `candidate-transformer' is run
4059 already, so the given transformer function should also be able
4060 to handle candidates with (DISPLAY . REAL) format.
4062 This option has no effect for asynchronous sources. (Not yet,
4063 at least. ")
4064 (anything-document-attribute 'action-transformer "optional"
4065 " It's a function or a list of functions called with two
4066 arguments when the action list from the source is
4067 assembled. The first argument is the list of actions, the
4068 second is the current selection. If it is a list of functions,
4069 it calls each function sequentially.
4071 The function should return a transformed action list.
4073 This can be used to customize the list of actions based on the
4074 currently selected candidate. ")
4075 (anything-document-attribute 'pattern-transformer "optional"
4076 " It's a function or a list of functions called with one argument
4077 before computing matches. Its argument is `anything-pattern'.
4078 Functions should return transformed `anything-pattern'.
4080 It is useful to change interpretation of `anything-pattern'. ")
4081 (anything-document-attribute 'delayed "optional"
4082 " Candidates from the source are shown only if the user stops
4083 typing and is idle for `anything-idle-delay' seconds. ")
4084 (anything-document-attribute 'volatile "optional"
4085 " Indicates the source assembles the candidate list dynamically,
4086 so it shouldn't be cached within a single Anything
4087 invocation. It is only applicable to synchronous sources,
4088 because asynchronous sources are not cached. ")
4089 (anything-document-attribute 'requires-pattern "optional"
4090 " If present matches from the source are shown only if the
4091 pattern is not empty. Optionally, it can have an integer
4092 parameter specifying the required length of input which is
4093 useful in case of sources with lots of candidates. ")
4094 (anything-document-attribute 'persistent-action "optional"
4095 " Function called with one parameter; the selected candidate.
4097 An action performed by `anything-execute-persistent-action'.
4098 If none, use the default action. ")
4099 (anything-document-attribute 'candidates-in-buffer "optional"
4100 " Shortcut attribute for making and narrowing candidates using
4101 buffers. This newly-introduced attribute prevents us from
4102 forgetting to add volatile and match attributes.
4104 See docstring of `anything-candidates-in-buffer'.
4106 (candidates-in-buffer) is equivalent of three attributes:
4107 (candidates . anything-candidates-in-buffer)
4108 (volatile)
4109 (match identity)
4111 (candidates-in-buffer . candidates-function) is equivalent of:
4112 (candidates . candidates-function)
4113 (volatile)
4114 (match identity)
4116 This attribute is implemented by plug-in. ")
4117 (anything-document-attribute 'search "optional"
4118 " List of functions like `re-search-forward' or `search-forward'.
4119 Buffer search function used by `anything-candidates-in-buffer'.
4120 By default, `anything-candidates-in-buffer' uses `re-search-forward'.
4121 This attribute is meant to be used with
4122 (candidates . anything-candidates-in-buffer) or
4123 (candidates-in-buffer) in short. ")
4124 (anything-document-attribute 'search-from-end "optional"
4125 " Make `anything-candidates-in-buffer' search from the end of buffer.
4126 If this attribute is specified, `anything-candidates-in-buffer' uses
4127 `re-search-backward' instead. ")
4128 (anything-document-attribute 'get-line "optional"
4129 " A function like `buffer-substring-no-properties' or `buffer-substring'.
4130 This function converts point of line-beginning and point of line-end,
4131 which represents a candidate computed by `anything-candidates-in-buffer'.
4132 By default, `anything-candidates-in-buffer' uses
4133 `buffer-substring-no-properties'. ")
4134 (anything-document-attribute 'display-to-real "optional"
4135 " Function called with one parameter; the selected candidate.
4137 The function transforms the selected candidate, and the result
4138 is passed to the action function. The display-to-real
4139 attribute provides another way to pass other string than one
4140 shown in Anything buffer.
4142 Traditionally, it is possible to make candidates,
4143 candidate-transformer or filtered-candidate-transformer
4144 function return a list with (DISPLAY . REAL) pairs. But if REAL
4145 can be generated from DISPLAY, display-to-real is more
4146 convenient and faster. ")
4147 (anything-document-attribute 'real-to-display "optional"
4148 " Function called with one parameter; the selected candidate.
4150 The inverse of display-to-real attribute.
4152 The function transforms the selected candidate, which is passed
4153 to the action function, for display. The real-to-display
4154 attribute provides the other way to pass other string than one
4155 shown in Anything buffer.
4157 Traditionally, it is possible to make candidates,
4158 candidate-transformer or filtered-candidate-transformer
4159 function return a list with (DISPLAY . REAL) pairs. But if
4160 DISPLAY can be generated from REAL, real-to-display is more
4161 convenient.
4163 Note that DISPLAY parts returned from candidates /
4164 candidate-transformer are IGNORED as the name `display-to-real'
4165 says. ")
4166 (anything-document-attribute 'cleanup "optional"
4167 " Function called with no parameters when *anything* buffer is closed. It
4168 is useful for killing unneeded candidates buffer.
4170 Note that the function is executed BEFORE performing action. ")
4171 (anything-document-attribute 'candidate-number-limit "optional"
4172 " Override `anything-candidate-number-limit' only for this source. ")
4173 (anything-document-attribute 'accept-empty "optional"
4174 " Pass empty string \"\" to action function. ")
4175 (anything-document-attribute 'disable-shortcuts "optional"
4176 " Disable `anything-enable-shortcuts' in current `anything' session.
4178 This attribute is implemented by plug-in. ")
4179 (anything-document-attribute 'dummy "optional"
4180 " Set `anything-pattern' to candidate. If this attribute is
4181 specified, The candidates attribute is ignored.
4183 This attribute is implemented by plug-in.
4184 This plug-in implies disable-shortcuts plug-in. ")
4185 (anything-document-attribute 'multiline "optional"
4186 " Enable to selection multiline candidates. ")
4187 (anything-document-attribute 'update "optional"
4188 ;; FIXME: this is not displayed correctly in help buffer.
4189 " Function called with no parameters when \\<anything-map>\\[anything-force-update] is pressed. ")
4190 (anything-document-attribute 'mode-line "optional"
4191 " source local `anything-mode-line-string'. (included in `mode-line-format')
4192 It accepts also variable/function name. ")
4193 (anything-document-attribute 'header-line "optional"
4194 " source local `header-line-format'.
4195 It accepts also variable/function name. ")
4196 (anything-document-attribute
4197 'resume "optional"
4198 " Function called with no parameters when `anything-resume' is started.")
4201 ;; (@* "Bug Report")
4202 (defvar anything-maintainer-mail-address "emacs-anything@googlegroups.com")
4204 (defvar anything-bug-report-salutation
4205 "Describe bug below, using a precise recipe.
4207 When I executed M-x ...
4209 How to send a bug report:
4210 1) Be sure to use the LATEST version of anything.el.
4211 2) Enable debugger. M-x toggle-debug-on-error or (setq debug-on-error t)
4212 3) Use Lisp version instead of compiled one: (load \"anything.el\")
4213 4) If you got an error, please paste *Backtrace* buffer.
4214 5) Type C-c C-c to send.")
4216 (defvar anything-no-dump-variables
4217 '(anything-candidate-buffer-alist
4218 anything-digit-overlays
4219 anything-help-message
4220 anything-candidate-cache
4222 "Variables not to dump in bug report.")
4224 (defun anything-dumped-variables-in-bug-report ()
4225 (let ((hash (make-hash-table)))
4226 (loop for var in (apropos-internal "anything-" 'boundp)
4227 for vname = (symbol-name var)
4228 unless (or (string-match "-map$" vname)
4229 (string-match "^anything-c-source-" vname)
4230 (string-match "-hash$" vname)
4231 (string-match "-face$" vname)
4232 (memq var anything-no-dump-variables))
4233 collect var)))
4235 (defun anything-send-bug-report ()
4236 "Send a bug report of anything.el."
4237 (interactive)
4238 (with-current-buffer (or anything-last-buffer
4239 (current-buffer))
4240 (reporter-submit-bug-report
4241 anything-maintainer-mail-address
4242 "anything.el"
4243 (anything-dumped-variables-in-bug-report)
4244 nil nil
4245 anything-bug-report-salutation)))
4247 (defun anything-send-bug-report-from-anything ()
4248 "Send a bug report of anything.el in anything session."
4249 (interactive)
4250 (anything-run-after-quit 'anything-send-bug-report))
4252 ;; Debugging function.
4253 (defun* anything-test-candidates
4254 (sources &optional (input "")
4255 (compile-source-functions
4256 anything-compile-source-functions-default))
4257 "Test helper function for anything.
4258 Given pseudo `anything-sources' and `anything-pattern', returns list like
4259 ((\"source name1\" (\"candidate1\" \"candidate2\"))
4260 (\"source name2\" (\"candidate3\" \"candidate4\")))"
4261 (let ((anything-test-mode t)
4262 anything-enable-shortcuts
4263 anything-candidate-cache
4264 (anything-compile-source-functions compile-source-functions)
4265 anything-before-initialize-hook
4266 anything-after-initialize-hook
4267 anything-update-hook
4268 anything-test-candidate-list)
4269 (get-buffer-create anything-buffer)
4270 (anything-initialize nil input sources)
4271 (anything-update)
4272 ;; test-mode spec: select 1st candidate!
4273 (with-current-buffer anything-buffer
4274 (forward-line 1)
4275 (anything-mark-current-line))
4276 (prog1
4277 anything-test-candidate-list
4278 (anything-cleanup))))
4281 ;; (@* "Unit Tests")
4282 ;; See developer-tools/unit-test-anything.el
4284 (provide 'anything)
4286 ;; How to save (DO NOT REMOVE!!)
4287 ;; (progn (magit-push) (emacswiki-post "anything.el"))
4289 ;; Local Variables:
4290 ;; coding: utf-8
4291 ;; End:
4293 ;;; anything.el ends here