* anything-config.el (anything-c-read-file-name): Handle also confirm-after-completion.
[anything-config.git] / anything.el
blobc15d084d0ade93d6f84f3c5821b846ea17757b1a
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))
1805 ;;; Initialize
1808 (defvar anything-buffers nil
1809 "All of `anything-buffer' in most recently used order.")
1810 (defun anything-initialize (any-resume any-input any-sources)
1811 "Start initialization of `anything' session.
1812 For ANY-RESUME ANY-INPUT and ANY-SOURCES See `anything'."
1813 (anything-log "start initialization: any-resume=%S any-input=%S" any-resume any-input)
1814 (anything-frame-or-window-configuration 'save)
1815 (setq anything-sources (anything-normalize-sources any-sources))
1816 (anything-log "sources = %S" anything-sources)
1817 (anything-hooks 'setup)
1818 (anything-current-position 'save)
1819 (if (anything-resume-p any-resume)
1820 (anything-initialize-overlays (anything-buffer-get))
1821 (anything-initial-setup))
1822 (unless (eq any-resume 'noresume)
1823 (anything-recent-push anything-buffer 'anything-buffers)
1824 (setq anything-last-buffer anything-buffer))
1825 (when any-input (setq anything-input any-input anything-pattern any-input))
1826 (and (anything-resume-p any-resume) (anything-funcall-foreach 'resume))
1827 (anything-log "end initialization"))
1829 (defun anything-execute-selection-action-1 ()
1830 "Execute current action."
1831 (anything-log-run-hook 'anything-before-action-hook)
1832 (unwind-protect
1833 (anything-execute-selection-action)
1834 (anything-aif (get-buffer anything-action-buffer)
1835 (kill-buffer it))
1836 (anything-log-run-hook 'anything-after-action-hook)
1837 ;; Depending the keymap used, i.e `minibuffer-local-must-match-map'
1838 ;; we can exit minibuffer with `exit-minibuffer'
1839 ;; instead of `anything-exit-minibuffer',
1840 ;; so set exit status here instead of in `anything-exit-minibuffer'.
1841 (setq anything-exit-status 0)))
1843 (defun anything-restore-position-on-quit ()
1844 "Restore position in `anything-current-buffer' when quitting."
1845 (anything-current-position 'restore))
1847 (defun anything-recent-push (elt list-var)
1848 "Add ELT to the value of LIST-VAR as most recently used value."
1849 (let ((m (member elt (symbol-value list-var))))
1850 (and m (set list-var (delq (car m) (symbol-value list-var))))
1851 (push elt (symbol-value list-var))))
1854 ;;; (@* "Core: Accessors")
1855 ;;; rubikitch: I love to create functions to control variables.
1856 (defvar anything-current-position nil
1857 "Cons of \(point . window-start\) when `anything' is invoked.
1858 It is needed to restore position in `anything-current-buffer'
1859 when `anything' is keyboard-quitted.")
1860 (defun anything-current-position (save-or-restore)
1861 "Restore or save current position in `anything-current-buffer'.
1862 Argument SAVE-OR-RESTORE is one of save or restore."
1863 (case save-or-restore
1864 (save
1865 (anything-log "Save position at %S" (cons (point) (window-start)))
1866 (setq anything-current-position (cons (point) (window-start))))
1867 (restore
1868 (anything-log "Restore position at %S in buffer %s"
1869 anything-current-position
1870 (buffer-name (current-buffer)))
1871 (goto-char (car anything-current-position))
1872 ;; Fix this position with the NOFORCE arg of `set-window-start'
1873 ;; otherwise, if there is some other buffer than `anything-current-buffer'
1874 ;; one, position will be lost.
1875 (set-window-start (selected-window) (cdr anything-current-position) t))))
1878 ;; Internal.
1879 (defvar anything-last-frame-or-window-configuration nil
1880 "Used to store window or frame configuration when anything start.")
1881 (defun anything-frame-or-window-configuration (save-or-restore)
1882 "Save or restore last frame or window configuration.
1883 Possible value of SAVE-OR-RESTORE are 'save and 'restore.
1884 window or frame configuration is saved/restored according to values of
1885 `anything-save-configuration-functions'."
1886 (anything-log-eval anything-save-configuration-functions)
1887 (case save-or-restore
1888 (save (setq anything-last-frame-or-window-configuration
1889 (funcall (cdr anything-save-configuration-functions))))
1890 (restore (funcall (car anything-save-configuration-functions)
1891 anything-last-frame-or-window-configuration)
1892 ;; Restore frame focus.
1893 (let ((frame (and (listp anything-last-frame-or-window-configuration)
1894 (caadr anything-last-frame-or-window-configuration))))
1895 ;; If `anything-save-configuration-functions' are window functions
1896 ;; frame should be nil, use current frame.
1897 (unless (framep frame)
1898 (setq frame (selected-frame)))
1899 (select-frame-set-input-focus frame)))))
1902 ;; (@* "Core: Display *anything* buffer")
1903 (defun anything-display-buffer (buf)
1904 "Display *anything* buffer BUF."
1905 (let (pop-up-frames)
1906 (funcall (with-current-buffer buf anything-display-function) buf)))
1908 (defun anything-default-display-buffer (buf)
1909 "Default function to display BUF.
1910 Where BUF is generally `anything-buffer'.
1911 It use `switch-to-buffer' or `pop-to-buffer' depending of value of
1912 `anything-samewindow'."
1913 (funcall (if anything-samewindow 'switch-to-buffer 'pop-to-buffer) buf))
1916 ;; (@* "Core: initialize")
1917 (defun anything-initial-setup ()
1918 "Initialize anything settings and set up the anything buffer."
1919 (anything-log-run-hook 'anything-before-initialize-hook)
1920 (setq anything-current-prefix-arg nil)
1921 (setq anything-once-called-functions nil)
1922 (setq anything-delayed-init-executed nil)
1923 (setq anything-current-buffer
1924 (if (minibuffer-window-active-p (minibuffer-window))
1925 ;; If minibuffer is active be sure to use it's buffer
1926 ;; as `anything-current-buffer'.
1927 (window-buffer (active-minibuffer-window))
1928 (current-buffer)))
1929 (setq anything-buffer-file-name buffer-file-name)
1930 (setq anything-issued-errors nil)
1931 (setq anything-compiled-sources nil)
1932 (setq anything-saved-current-source nil)
1933 (if (or (not split-width-threshold)
1934 (and (integerp split-width-threshold)
1935 (>= split-width-threshold (+ (frame-width) 4))))
1936 (setq anything-split-window-state 'vertical)
1937 (setq anything-split-window-state 'horizontal))
1938 ;; Call the init function for sources where appropriate
1939 (anything-funcall-foreach 'init)
1940 (setq anything-pattern "")
1941 (setq anything-input "")
1942 (setq anything-candidate-cache nil)
1943 (setq anything-last-sources anything-sources)
1944 (anything-create-anything-buffer)
1945 (anything-log-run-hook 'anything-after-initialize-hook))
1947 (defvar anything-reading-pattern nil
1948 "Whether in `read-string' in anything or not.")
1949 (defun anything-read-pattern-maybe (any-prompt any-input
1950 any-preselect any-resume any-keymap
1951 any-default any-history)
1952 "Read pattern with prompt ANY-PROMPT and initial input ANY-INPUT.
1953 For ANY-PRESELECT ANY-RESUME ANY-KEYMAP, See `anything'."
1954 (if (anything-resume-p any-resume)
1955 (anything-mark-current-line t)
1956 (anything-update any-preselect))
1957 (with-current-buffer (anything-buffer-get)
1958 (and any-keymap (set (make-local-variable 'anything-map) any-keymap))
1959 (let ((minibuffer-local-map
1960 anything-map))
1961 (anything-log-eval (anything-approximate-candidate-number)
1962 anything-execute-action-at-once-if-one
1963 anything-quit-if-no-candidate)
1964 (cond ((and anything-execute-action-at-once-if-one
1965 (= (anything-approximate-candidate-number) 1))
1966 (ignore))
1967 ((and anything-quit-if-no-candidate
1968 (= (anything-approximate-candidate-number) 0))
1969 (setq anything-quit t)
1970 (and (functionp anything-quit-if-no-candidate)
1971 (funcall anything-quit-if-no-candidate)))
1973 (let ((anything-reading-pattern t)
1974 (tap (or any-default
1975 (with-anything-current-buffer
1976 (thing-at-point 'symbol)))))
1977 (read-string (or any-prompt "pattern: ") any-input any-history tap)))))))
1979 (defun anything-create-anything-buffer (&optional test-mode)
1980 "Create newly created `anything-buffer'.
1981 If TEST-MODE is non-nil, clear `anything-candidate-cache'."
1982 (when test-mode
1983 (setq anything-candidate-cache nil))
1984 (with-current-buffer (get-buffer-create anything-buffer)
1985 (anything-log "kill local variables: %S" (buffer-local-variables))
1986 (kill-all-local-variables)
1987 (set (make-local-variable 'inhibit-read-only) t)
1988 (buffer-disable-undo)
1989 (erase-buffer)
1990 (set (make-local-variable 'inhibit-read-only) t)
1991 (set (make-local-variable 'anything-last-sources-local) anything-sources)
1992 (set (make-local-variable 'anything-follow-mode) nil)
1993 (set (make-local-variable 'anything-display-function) anything-display-function)
1994 (set (make-local-variable 'anything-selection-point) nil)
1995 (anything-initialize-persistent-action)
1996 (anything-log-eval anything-display-function anything-let-variables)
1997 (loop for (var . val) in anything-let-variables
1998 do (set (make-local-variable var) val))
1999 (setq cursor-type nil)
2000 (setq mode-name "Anything"))
2001 (anything-initialize-overlays anything-buffer)
2002 (get-buffer anything-buffer))
2004 (defun anything-initialize-overlays (buffer)
2005 "Initialize anything overlays in BUFFER."
2006 (anything-log "overlay setup")
2007 (if anything-selection-overlay
2008 ;; make sure the overlay belongs to the anything buffer if
2009 ;; it's newly created
2010 (move-overlay anything-selection-overlay (point-min) (point-min)
2011 (get-buffer buffer))
2013 (setq anything-selection-overlay
2014 (make-overlay (point-min) (point-min) (get-buffer buffer)))
2015 (overlay-put anything-selection-overlay 'face anything-selection-face))
2017 (cond (anything-enable-shortcuts
2018 (setq anything-shortcut-keys
2019 (assoc-default anything-enable-shortcuts anything-shortcut-keys-alist))
2020 (unless anything-digit-overlays
2021 (setq anything-digit-overlays
2022 (loop for key across anything-shortcut-keys
2023 for overlay = (make-overlay (point-min) (point-min)
2024 (get-buffer buffer))
2025 do (overlay-put overlay 'before-string
2026 (format "%s - " (upcase (make-string 1 key))))
2027 collect overlay))))
2028 (anything-digit-overlays
2029 (mapc 'delete-overlay anything-digit-overlays)
2030 (setq anything-digit-overlays nil))))
2032 (defun anything-hooks (setup-or-cleanup)
2033 "Add or remove hooks according to SETUP-OR-CLEANUP value.
2034 if SETUP-OR-CLEANUP value is setup add hooks, any other value
2035 will remove hooks.
2036 hooks concerned are `post-command-hook' and `minibuffer-setup-hook'."
2037 (let ((hooks '((post-command-hook anything-check-minibuffer-input)
2038 (minibuffer-setup-hook anything-print-error-messages))))
2039 (if (eq setup-or-cleanup 'setup)
2040 (dolist (args hooks) (apply 'add-hook args))
2041 (dolist (args (reverse hooks)) (apply 'remove-hook args)))))
2044 ;; (@* "Core: clean up")
2045 ;;; TODO move
2046 (defun anything-cleanup ()
2047 "Clean up the mess when anything exit or quit."
2048 (anything-log "start cleanup")
2049 (with-current-buffer anything-buffer
2050 ;; rubikitch: I think it is not needed.
2051 ;; (setq cursor-type t)
2053 ;; Call burry-buffer whithout arg
2054 ;; to remove anything-buffer from window.
2055 (bury-buffer)
2056 ;; Be sure we call this from anything-buffer.
2057 (anything-funcall-foreach 'cleanup))
2058 (anything-new-timer 'anything-check-minibuffer-input-timer nil)
2059 (anything-kill-async-processes)
2060 (anything-log-run-hook 'anything-cleanup-hook)
2061 (anything-hooks 'cleanup)
2062 (anything-frame-or-window-configuration 'restore)
2063 ;; This is needed in some cases where last input
2064 ;; is yielded infinitely in minibuffer after anything session.
2065 (anything-clean-up-minibuffer))
2067 (defun anything-clean-up-minibuffer ()
2068 "Remove contents of minibuffer."
2069 (let ((miniwin (minibuffer-window)))
2070 ;; Clean only current minibuffer used by anything.
2071 ;; i.e The precedent one is active.
2072 (unless (minibuffer-window-active-p miniwin)
2073 (with-current-buffer (window-buffer miniwin)
2074 (delete-minibuffer-contents)))))
2077 ;; (@* "Core: input handling")
2078 (defun anything-check-minibuffer-input ()
2079 "Extract input string from the minibuffer and check if it needs to be handled."
2080 (let ((delay (with-current-buffer anything-buffer
2081 (and anything-input-idle-delay
2082 (max anything-input-idle-delay 0.1)))))
2083 (if (or (not delay) (anything-action-window))
2084 (anything-check-minibuffer-input-1)
2085 (anything-new-timer
2086 'anything-check-minibuffer-input-timer
2087 (run-with-idle-timer delay nil 'anything-check-minibuffer-input-1)))))
2089 (defun anything-check-minibuffer-input-1 ()
2090 "Check minibuffer content."
2091 (with-anything-quittable
2092 (with-selected-window (or (active-minibuffer-window) (minibuffer-window))
2093 (anything-check-new-input (minibuffer-contents)))))
2095 (defun anything-check-new-input (input)
2096 "Check INPUT string and update the anything buffer if necessary."
2097 (unless (equal input anything-pattern)
2098 (setq anything-pattern input)
2099 (unless (anything-action-window)
2100 (setq anything-input anything-pattern))
2101 (anything-log-eval anything-pattern anything-input)
2102 (anything-update)))
2105 ;; (@* "Core: source compiler")
2106 (defvar anything-compile-source-functions-default anything-compile-source-functions
2107 "Plug-ins this file provides.")
2108 (defun anything-compile-sources (sources funcs)
2109 "Compile SOURCES with FUNCS.
2110 See `anything-compile-source-functions'.
2111 Anything plug-ins are realized by this function."
2112 (mapcar
2113 (lambda (source)
2114 (loop with source = (if (listp source) source (symbol-value source))
2115 for f in funcs
2116 do (setq source (funcall f source))
2117 finally (return source)))
2118 sources))
2121 ;; (@* "Core: plug-in attribute documentation hack")
2123 ;; `anything-document-attribute' is public API.
2124 (defadvice documentation-property (after anything-document-attribute activate)
2125 "Display plug-in attributes' documentation as `anything-sources' docstring."
2126 (when (eq (ad-get-arg 0) 'anything-sources)
2127 (setq ad-return-value
2128 (concat ad-return-value "\n"
2129 (mapconcat (lambda (sym) (get sym 'anything-attrdoc))
2130 anything-additional-attributes
2131 "\n")))))
2132 ;; (describe-variable 'anything-sources)
2133 ;; (documentation-property 'anything-sources 'variable-documentation)
2134 ;; (progn (ad-disable-advice 'documentation-property 'after 'anything-document-attribute) (ad-update 'documentation-property))
2137 ;; (@* "Core: all candidates")
2138 (defun anything-process-delayed-init (source)
2139 "Initialize delayed SOURCE."
2140 (let ((name (assoc-default 'name source)))
2141 (unless (member name anything-delayed-init-executed)
2142 (anything-aif (assoc-default 'delayed-init source)
2143 (with-current-buffer anything-current-buffer
2144 (anything-funcall-with-source source it)
2145 (dolist (f (if (functionp it) (list it) it))
2146 (add-to-list 'anything-delayed-init-executed name)))))))
2148 (defun anything-get-candidates (source)
2149 "Retrieve and return the list of candidates from SOURCE."
2150 (anything-process-delayed-init source)
2151 (let* ((candidate-source (assoc-default 'candidates source))
2152 (type-error (lambda ()
2153 (error (concat "Candidates must either be a function, "
2154 " a variable or a list: %s")
2155 candidate-source)))
2156 (candidates (condition-case err
2157 (anything-interpret-value candidate-source source)
2158 (error (funcall type-error)))))
2159 (cond ((processp candidates) candidates)
2160 ((listp candidates) (anything-transform-candidates candidates source))
2161 (t (funcall type-error)))))
2164 (defun anything-get-cached-candidates (source)
2165 "Return the cached value of candidates for SOURCE.
2166 Cache the candidates if there is not yet a cached value."
2167 (let* ((name (assoc-default 'name source))
2168 (candidate-cache (assoc name anything-candidate-cache)))
2169 (cond (candidate-cache
2170 (anything-log "use cached candidates")
2171 (cdr candidate-cache))
2173 (anything-log "calculate candidates")
2174 (let ((candidates (anything-get-candidates source)))
2175 (cond ((processp candidates)
2176 (push (cons candidates
2177 (append source
2178 (list (cons 'item-count 0)
2179 (cons 'incomplete-line ""))))
2180 anything-async-processes)
2181 (set-process-filter candidates 'anything-output-filter)
2182 (setq candidates nil))
2183 ((not (assoc 'volatile source))
2184 (setq candidate-cache (cons name candidates))
2185 (push candidate-cache anything-candidate-cache)))
2186 candidates)))))
2189 ;;; (@* "Core: candidate transformers")
2190 (defun anything-process-candidate-transformer (candidates source)
2191 "Execute candidate-transformer function on all CANDIDATES of SOURCE."
2192 (anything-aif (assoc-default 'candidate-transformer source)
2193 (anything-composed-funcall-with-source source it candidates)
2194 candidates))
2196 (defun anything-process-filtered-candidate-transformer (candidates source)
2197 "Execute filtered-candidate-transformer function on all CANDIDATES of SOURCE."
2198 (anything-aif (assoc-default 'filtered-candidate-transformer source)
2199 (anything-composed-funcall-with-source source it candidates source)
2200 candidates))
2202 (defun anything-process-filtered-candidate-transformer-maybe (candidates source process-p)
2203 "Execute filtered-candidate-transformer function on all CANDIDATES of SOURCE.
2204 This happen if PROCESS-P is non-nil."
2205 (if process-p
2206 (anything-process-filtered-candidate-transformer candidates source)
2207 candidates))
2209 (defun anything-process-real-to-display (candidates source)
2210 "Execute real-to-display function on all CANDIDATES of SOURCE."
2211 (anything-aif (assoc-default 'real-to-display source)
2212 (setq candidates (anything-funcall-with-source
2213 source 'mapcar
2214 (lambda (cand_)
2215 (if (consp cand_)
2216 ;; override DISPLAY from candidate-transformer
2217 (cons (funcall it (cdr cand_)) (cdr cand_))
2218 (cons (funcall it cand_) cand_)))
2219 candidates))
2220 candidates))
2222 (defun anything-transform-candidates (candidates source &optional process-p)
2223 "Transform CANDIDATES of SOURCE according to candidate transformers.
2224 This happen if PROCESS-P is non-nil."
2225 (anything-process-real-to-display
2226 (anything-process-filtered-candidate-transformer-maybe
2227 (anything-process-candidate-transformer candidates source) source process-p)
2228 source))
2231 ;; (@* "Core: narrowing candidates")
2232 (defun anything-candidate-number-limit (source)
2233 "Apply candidate-number-limit attribute value.
2234 This overhide variable `anything-candidate-number-limit'.
2236 e.g:
2237 If \(candidate-number-limit\) is in SOURCE, show all candidates in SOURCE.
2238 If \(candidate-number-limit . 123\) is in SOURCE limit candidate to 123."
2239 (anything-aif (assq 'candidate-number-limit source)
2240 (or (cdr it) 99999999)
2241 (or anything-candidate-number-limit 99999999)))
2243 ;; FIXME: Why a defconst here
2244 (defconst anything-default-match-functions
2245 (list (lambda (candidate)
2246 (string-match anything-pattern candidate)))
2247 "Default functions to match candidates according to `anything-pattern'.")
2249 (defun anything-compute-matches (source)
2250 "Compute matched results from SOURCE according to its settings."
2251 (if debug-on-error
2252 (anything-compute-matches-internal source)
2253 (condition-case v
2254 (anything-compute-matches-internal source)
2255 (error (anything-log-error
2256 "anything-compute-matches: error when processing source: %s"
2257 (assoc-default 'name source))
2258 nil))))
2260 (defun anything-candidate-get-display (candidate)
2261 "Get searched display part from CANDIDATE.
2262 CANDIDATE is a string, a symbol, or \(DISPLAY . REAL\) cons cell."
2263 (format "%s" (or (car-safe candidate) candidate)))
2265 (defun anything-process-pattern-transformer (pattern source)
2266 "Execute pattern-transformer attribute PATTERN function in SOURCE."
2267 (anything-aif (assoc-default 'pattern-transformer source)
2268 (anything-composed-funcall-with-source source it pattern)
2269 pattern))
2271 (defun anything-match-functions (source)
2272 (or (assoc-default 'match source)
2273 anything-default-match-functions))
2275 (defmacro anything-accumulate-candidates-internal (cand newmatches
2276 hash item-count limit)
2277 "Internal, add CAND into NEWMATCHES.
2278 Use HASH to uniq NEWMATCHES.
2279 Argument ITEM-COUNT count the matches.
2280 if ITEM-COUNT reaches LIMIT, exit from inner loop."
2281 `(unless (gethash ,cand ,hash)
2282 (puthash ,cand t ,hash)
2283 (push ,cand ,newmatches)
2284 (incf ,item-count)
2285 (when (= ,item-count ,limit)
2286 (setq exit t)
2287 (return))))
2289 (defun anything-take-first-elements (seq n)
2290 (if (> (length seq) n)
2291 (setq seq (subseq seq 0 n))
2292 seq))
2294 (defun anything-match-from-candidates (cands matchfns limit)
2295 (let (matches)
2296 (condition-case nil
2297 (let ((item-count 0) exit)
2298 (clrhash anything-match-hash)
2299 (dolist (match matchfns)
2300 (let (newmatches)
2301 (dolist (candidate cands)
2302 (when (funcall match (anything-candidate-get-display candidate))
2303 (anything-accumulate-candidates-internal
2304 candidate newmatches anything-match-hash item-count limit)))
2305 (setq matches (append matches (reverse newmatches)))
2306 (if exit (return)))))
2307 (invalid-regexp (setq matches nil)))
2308 matches))
2310 (defun anything-compute-matches-internal (source)
2311 (save-current-buffer
2312 (let ((matchfns (anything-match-functions source))
2313 (anything-source-name (assoc-default 'name source))
2314 (limit (anything-candidate-number-limit source))
2315 (anything-pattern (anything-process-pattern-transformer
2316 anything-pattern source)))
2317 (anything-process-filtered-candidate-transformer
2318 (if (or (equal anything-pattern "") (equal matchfns '(identity)))
2319 (anything-take-first-elements
2320 (anything-get-cached-candidates source) limit)
2321 (anything-match-from-candidates
2322 (anything-get-cached-candidates source) matchfns limit))
2323 source))))
2325 ;; (anything '(((name . "error")(candidates . (lambda () (hage))) (action . identity))))
2327 (defun anything-process-source (source)
2328 "Display matched results from SOURCE according to its settings."
2329 (anything-log-eval (assoc-default 'name source))
2330 (if (assq 'direct-insert-match source) ;experimental
2331 (anything-process-source--direct-insert-match source)
2332 (let ((matches (anything-compute-matches source)))
2333 (when matches
2334 (when anything-test-mode
2335 (setq anything-test-candidate-list
2336 `(,@anything-test-candidate-list
2337 (,(assoc-default 'name source)
2338 ,matches))))
2339 (anything-insert-header-from-source source)
2340 (if (not (assq 'multiline source))
2341 (mapc 'anything-insert-match-with-digit-overlay matches)
2342 (let ((start (point)) separate)
2343 (dolist (match matches)
2344 (if separate
2345 (anything-insert-candidate-separator)
2346 (setq separate t))
2347 (anything-insert-match-with-digit-overlay match))
2348 (put-text-property start (point) 'anything-multiline t)))))))
2350 (defun anything-insert-match-with-digit-overlay (match)
2351 (declare (special source))
2352 (anything-put-digit-overlay-maybe)
2353 (anything-insert-match match 'insert source))
2355 (defun anything-put-digit-overlay-maybe ()
2356 (when (and anything-enable-shortcuts
2357 (not (eq anything-digit-shortcut-count
2358 (length anything-digit-overlays))))
2359 (move-overlay (nth anything-digit-shortcut-count
2360 anything-digit-overlays)
2361 (point-at-bol)
2362 (point-at-bol))
2363 (incf anything-digit-shortcut-count)))
2365 (defun anything-process-source--direct-insert-match (source)
2366 "[EXPERIMENTAL] Insert candidates from `anything-candidate-buffer' in SOURCE."
2367 (anything-log-eval (assoc-default 'name source))
2368 (let ((anything-source-name (assoc-default 'name source))
2369 content-buf)
2370 (funcall (assoc-default 'candidates source))
2371 (setq content-buf (anything-candidate-buffer))
2372 (unless (anything-empty-buffer-p content-buf)
2373 (anything-insert-header-from-source source)
2374 (insert-buffer-substring content-buf)
2375 ;; TODO call anything-put-digit-overlay-maybe with loop
2378 (defun anything-process-delayed-sources (delayed-sources &optional preselect)
2379 "Process anything DELAYED-SOURCES.
2380 Move selection to string or regexp PRESELECT if non--nil.
2381 This function is called in `anything-process-delayed-sources-timer'
2382 when emacs is idle for `anything-idle-delay'."
2383 (with-anything-quittable
2384 (anything-log-eval (mapcar (lambda (s) (assoc-default 'name s)) delayed-sources))
2385 (with-current-buffer anything-buffer
2386 (save-excursion
2387 (goto-char (point-max))
2388 (mapc 'anything-process-source delayed-sources)
2389 (when (and (not (anything-empty-buffer-p))
2390 ;; No selection yet.
2391 (= (overlay-start anything-selection-overlay)
2392 (overlay-end anything-selection-overlay)))
2393 (anything-update-move-first-line 'without-hook)))
2394 (when preselect (anything-preselect preselect))
2395 (save-excursion
2396 (goto-char (point-min))
2397 (anything-log-run-hook 'anything-update-hook))
2398 (anything-log-run-hook 'anything-after-update-hook))))
2401 ;; (@* "Core: *anything* buffer contents")
2402 (defvar anything-input-local nil)
2403 (defvar anything-process-delayed-sources-timer nil)
2404 (defun anything-update (&optional preselect)
2405 "Update candidates list in `anything-buffer' according to `anything-pattern'.
2406 Argument PRESELECT is a string or regexp used to move selection to a particular
2407 place once updating is done. It should be used on single source because search
2408 is done on whole `anything-buffer' and not on current source."
2409 (anything-log "start update")
2410 (setq anything-digit-shortcut-count 0)
2411 (anything-kill-async-processes)
2412 (with-current-buffer (anything-buffer-get)
2413 (set (make-local-variable 'anything-input-local) anything-pattern)
2414 (erase-buffer)
2415 (when anything-enable-shortcuts
2416 (mapc 'delete-overlay anything-digit-overlays))
2417 (let (delayed-sources
2418 normal-sources)
2419 (unwind-protect ; Process normal sources and store delayed one's.
2420 (setq delayed-sources
2421 (loop for source in (remove-if-not 'anything-update-source-p
2422 (anything-get-sources))
2423 if (anything-delayed-source-p source)
2424 collect source
2425 else do (progn (push source normal-sources)
2426 (anything-process-source source))))
2427 (anything-log-eval
2428 (mapcar (lambda (s) (assoc-default 'name s)) delayed-sources))
2429 (if anything-test-mode ; Need only to process sources.
2430 (mapc 'anything-process-source delayed-sources)
2431 (cond ((and preselect delayed-sources normal-sources)
2432 ;; Preselection run here when there is
2433 ;; normal AND delayed sources.
2434 (anything-log "Update preselect candidate %s" preselect)
2435 (anything-preselect preselect))
2436 (delayed-sources ; Preselection and hooks will run later.
2437 (anything-update-move-first-line 'without-hook))
2438 (t ; No delayed sources, run the hooks now.
2439 (anything-update-move-first-line)
2440 (anything-log-run-hook 'anything-after-update-hook)
2441 (when preselect
2442 (anything-log "Update preselect candidate %s" preselect)
2443 (anything-preselect preselect))))
2444 (when delayed-sources
2445 (anything-new-timer
2446 'anything-process-delayed-sources-timer
2447 (run-with-idle-timer
2448 ;; Be sure anything-idle-delay is >
2449 ;; to anything-input-idle-delay
2450 ;; otherwise use value of anything-input-idle-delay
2451 ;; or 0.1 if == to 0.
2452 (max anything-idle-delay anything-input-idle-delay 0.1) nil
2453 'anything-process-delayed-sources delayed-sources preselect))))
2454 (anything-log "end update")))))
2456 (defun anything-update-source-p (source)
2457 "Wheter SOURCE need updating or not."
2458 (and (or (not anything-source-filter)
2459 (member (assoc-default 'name source) anything-source-filter))
2460 (>= (length anything-pattern)
2461 (anything-aif (assoc 'requires-pattern source)
2462 (or (cdr it) 1)
2463 0))))
2465 (defun anything-delayed-source-p (source)
2466 "Wheter SOURCE is a delayed source or not."
2467 (or (assoc 'delayed source)
2468 (and anything-quick-update
2469 (< (window-height (get-buffer-window (current-buffer)))
2470 (line-number-at-pos (point-max))))))
2472 (defun anything-update-move-first-line (&optional without-hook)
2473 "Goto first line of `anything-buffer'."
2474 (goto-char (point-min))
2475 (unless without-hook
2476 (save-excursion (anything-log-run-hook 'anything-update-hook)))
2477 (anything-next-line))
2479 (defun anything-force-update (&optional preselect)
2480 "Force recalculation and update of candidates.
2481 If current source has `update' attribute, a function without argument,
2482 call it before update."
2483 (interactive)
2484 (let ((source (anything-get-current-source))
2485 (selection (anything-get-selection nil t)))
2486 (when source
2487 (mapc 'anything-force-update--reinit
2488 (anything-get-sources)))
2489 (anything-update preselect)
2490 ;; If preselect arg exists, `anything-update' should
2491 ;; have moved to selection, otherwise do it now.
2492 (unless preselect
2493 (anything-keep-selection (assoc-default 'name source) selection))
2494 (with-anything-window (recenter))))
2496 (defun anything-force-update--reinit (source)
2497 "Reinit SOURCE by calling his update and/or init functions."
2498 (anything-aif (anything-funcall-with-source
2499 source 'anything-candidate-buffer)
2500 (kill-buffer it))
2501 (dolist (attr '(update init))
2502 (anything-aif (assoc-default attr source)
2503 (anything-funcall-with-source source it)))
2504 (anything-remove-candidate-cache source))
2506 (defun anything-keep-selection (source selection)
2507 "Switch to SOURCE and goto SELECTION."
2508 (when (and source selection)
2509 (with-anything-window
2510 (anything-goto-source source)
2511 (forward-char -1)
2512 (if (search-forward selection nil t)
2513 (forward-line 0)
2514 (goto-char (point-min))
2515 (forward-line 1))
2516 (anything-mark-current-line))))
2518 (defun anything-remove-candidate-cache (source)
2519 "Remove SOURCE from `anything-candidate-cache'."
2520 (setq anything-candidate-cache
2521 (delete (assoc (assoc-default 'name source)
2522 anything-candidate-cache)
2523 anything-candidate-cache)))
2525 (defun anything-insert-match (match insert-function source)
2526 "Insert MATCH into `anything-buffer' with INSERT-FUNCTION for SOURCE.
2527 If MATCH is a list then insert the string intended to appear on the display
2528 and store the real value in a text property."
2529 (let ((start (point-at-bol (point)))
2530 (string (or (car-safe match) match))
2531 (realvalue (cdr-safe match)))
2532 (when (symbolp string) (setq string (symbol-name string)))
2533 (when (stringp string)
2534 (funcall insert-function string)
2535 ;; Some sources with candidates-in-buffer have already added
2536 ;; 'anything-realvalue property when creating candidate buffer.
2537 (unless (get-text-property start 'anything-realvalue)
2538 (and realvalue
2539 (put-text-property start (point-at-eol)
2540 'anything-realvalue realvalue)))
2541 (when anything-source-in-each-line-flag
2542 (put-text-property start (point-at-eol) 'anything-source source))
2543 (funcall insert-function "\n"))))
2545 (defun anything-insert-header-from-source (source)
2546 "Insert SOURCE name in `anything-buffer' header.
2547 Maybe insert by overlay additional info after source name if SOURCE have
2548 header-name attribute."
2549 (let ((name (assoc-default 'name source)))
2550 (anything-insert-header
2551 name
2552 (anything-aif (assoc-default 'header-name source)
2553 (anything-funcall-with-source source it name)))))
2555 (defun anything-insert-header (name &optional display-string)
2556 "Insert header of source NAME into the anything buffer.
2557 If DISPLAY-STRING is non--nil and a string, display this additional info
2558 after the source name by overlay."
2559 (unless (bobp)
2560 (let ((start (point)))
2561 (insert "\n")
2562 (put-text-property start (point) 'anything-header-separator t)))
2563 (let ((start (point)))
2564 (insert name)
2565 (put-text-property (point-at-bol)
2566 (point-at-eol) 'anything-header t)
2567 (when display-string
2568 (overlay-put (make-overlay (point-at-bol) (point-at-eol))
2569 'display display-string))
2570 (insert "\n")
2571 (put-text-property start (point) 'face anything-header-face)))
2573 (defun anything-insert-candidate-separator ()
2574 "Insert separator of candidates into the anything buffer."
2575 (insert anything-candidate-separator)
2576 (put-text-property (point-at-bol)
2577 (point-at-eol) 'anything-candidate-separator t)
2578 (insert "\n"))
2581 ;; (@* "Core: async process")
2582 (defun anything-output-filter (process string)
2583 "From PROCESS process output STRING."
2584 (anything-output-filter-1 (assoc process anything-async-processes) string))
2586 (defun anything-output-filter-1 (process-assoc string)
2587 (anything-log-eval string)
2588 (with-current-buffer anything-buffer
2589 (let ((source (cdr process-assoc)))
2590 (save-excursion
2591 (anything-aif (assoc-default 'insertion-marker source)
2592 (goto-char it)
2593 (goto-char (point-max))
2594 (anything-insert-header-from-source source)
2595 (setcdr process-assoc
2596 (append source `((insertion-marker . ,(point-marker))))))
2597 (anything-output-filter--process-source
2598 (car process-assoc) string source
2599 (anything-candidate-number-limit source))))
2600 (anything-output-filter--post-process)))
2602 (defun anything-output-filter--process-source (process string source limit)
2603 (dolist (candidate (anything-transform-candidates
2604 (anything-output-filter--collect-candidates
2605 (split-string string "\n")
2606 (assoc 'incomplete-line source))
2607 source t))
2608 (if (not (assq 'multiline source))
2609 (anything-insert-match candidate 'insert-before-markers source)
2610 (let ((start (point)))
2611 (anything-insert-candidate-separator)
2612 (anything-insert-match candidate 'insert-before-markers source)
2613 (put-text-property start (point) 'anything-multiline t)))
2614 (incf (cdr (assoc 'item-count source)))
2615 (when (>= (assoc-default 'item-count source) limit)
2616 (anything-kill-async-process process)
2617 (return))))
2619 (defun anything-output-filter--collect-candidates (lines incomplete-line-info)
2620 (anything-log-eval (cdr incomplete-line-info))
2621 (butlast
2622 (loop for line in lines collect
2623 (if (cdr incomplete-line-info)
2624 (prog1
2625 (concat (cdr incomplete-line-info) line)
2626 (setcdr incomplete-line-info nil))
2627 line)
2628 finally (setcdr incomplete-line-info line))))
2630 (defun anything-output-filter--post-process ()
2631 (anything-log-run-hook 'anything-update-hook)
2632 (anything-aif (get-buffer-window anything-buffer 'visible)
2633 (save-selected-window
2634 (select-window it)
2635 (anything-skip-noncandidate-line 'next)
2636 (anything-mark-current-line))))
2638 (defun anything-kill-async-processes ()
2639 "Kill all known asynchronous processes of `anything-async-processes'."
2640 (mapc 'anything-kill-async-process (mapcar 'car anything-async-processes))
2641 (setq anything-async-processes nil))
2643 (defun anything-kill-async-process (process)
2644 "Kill PROCESS and detach the associated functions."
2645 (set-process-filter process nil)
2646 (delete-process process))
2649 ;; (@* "Core: action")
2650 (defun anything-execute-selection-action (&optional
2651 selection action
2652 preserve-saved-action)
2653 "If a candidate SELECTION is present then perform the associated ACTION on it.
2654 If PRESERVE-SAVED-ACTION is non-nil don't save action."
2655 (anything-log "executing action")
2656 (setq action (anything-get-default-action
2657 (or action
2658 anything-saved-action
2659 (if (get-buffer anything-action-buffer)
2660 (anything-get-selection anything-action-buffer)
2661 (anything-get-action)))))
2662 (let ((source (or anything-saved-current-source
2663 (anything-get-current-source))))
2664 (setq selection (or selection
2665 (anything-get-selection)
2666 (and (assoc 'accept-empty source) "")))
2667 (unless preserve-saved-action (setq anything-saved-action nil))
2668 (if (and selection action)
2669 (anything-funcall-with-source
2670 source action
2671 (anything-coerce-selection selection source)))))
2673 (defun anything-coerce-selection (selection source)
2674 "Apply coerce attribute function to SELECTION in SOURCE.
2675 Coerce source with coerce function."
2676 (anything-aif (assoc-default 'coerce source)
2677 (anything-funcall-with-source source it selection)
2678 selection))
2680 (defun anything-get-default-action (action)
2681 "Get the first ACTION value of action list in source."
2682 (if (and (listp action) (not (functionp action)))
2683 (cdar action)
2684 action))
2686 (defun anything-select-action ()
2687 "Select an action for the currently selected candidate.
2688 If action buffer is selected, back to the anything buffer."
2689 (interactive)
2690 (anything-log-run-hook 'anything-select-action-hook)
2691 (cond ((get-buffer-window anything-action-buffer 'visible)
2692 (set-window-buffer (get-buffer-window anything-action-buffer)
2693 anything-buffer)
2694 (kill-buffer anything-action-buffer)
2695 (anything-set-pattern anything-input 'noupdate))
2697 (setq anything-saved-selection (anything-get-selection))
2698 (unless anything-saved-selection
2699 (error "Nothing is selected"))
2700 (setq anything-saved-current-source (anything-get-current-source))
2701 (let ((actions (anything-get-action)))
2702 (if (functionp actions)
2703 (message "Sole action: %s" actions)
2704 (anything-show-action-buffer actions)
2705 (anything-delete-minibuffer-contents)
2706 (setq anything-pattern 'dummy) ; so that it differs from the previous one
2707 (anything-check-minibuffer-input))))))
2709 (defun anything-show-action-buffer (actions)
2710 (with-current-buffer (get-buffer-create anything-action-buffer)
2711 (erase-buffer)
2712 (buffer-disable-undo)
2713 (set-window-buffer (get-buffer-window anything-buffer) anything-action-buffer)
2714 (set (make-local-variable 'anything-sources)
2715 `(((name . "Actions")
2716 (volatile)
2717 (candidates . ,actions)
2718 (candidate-number-limit))))
2719 (set (make-local-variable 'anything-source-filter) nil)
2720 (set (make-local-variable 'anything-selection-overlay) nil)
2721 (set (make-local-variable 'anything-digit-overlays) nil)
2722 (anything-initialize-overlays anything-action-buffer)))
2725 ;; (@* "Core: selection")
2726 (defun anything-move-selection-common (move-func unit direction)
2727 "Move the selection marker to a new position wit function MOVE-FUNC.
2728 It is determined by UNIT and DIRECTION."
2729 (unless (or (anything-empty-buffer-p (anything-buffer-get))
2730 (not (anything-window)))
2731 (with-anything-window
2732 (anything-log-run-hook 'anything-move-selection-before-hook)
2733 (funcall move-func)
2734 (anything-skip-noncandidate-line direction)
2735 (anything-display-source-at-screen-top-maybe unit)
2736 (when (anything-get-previous-header-pos)
2737 (anything-mark-current-line))
2738 (anything-display-mode-line (anything-get-current-source))
2739 (anything-log-run-hook 'anything-move-selection-after-hook))))
2741 (defun anything-display-source-at-screen-top-maybe (unit)
2742 (when (and anything-display-source-at-screen-top (eq unit 'source))
2743 (set-window-start (selected-window)
2744 (save-excursion (forward-line -1) (point)))))
2746 (defun anything-skip-noncandidate-line (direction)
2747 (anything-skip-header-and-separator-line direction)
2748 (and (bobp) (forward-line 1)) ;skip first header
2749 (and (eobp) (forward-line -1))) ;avoid last empty line
2752 (defun anything-skip-header-and-separator-line (direction)
2753 (while (and (not (bobp))
2754 (or (anything-pos-header-line-p)
2755 (anything-pos-candidate-separator-p)))
2756 (forward-line (if (and (eq direction 'previous)
2757 (not (eq (point-at-bol) (point-min))))
2758 -1 1))))
2760 (defvar anything-mode-line-string-real nil)
2761 (defun anything-display-mode-line (source)
2762 (set (make-local-variable 'anything-mode-line-string)
2763 (anything-interpret-value (or (assoc-default 'mode-line source)
2764 (default-value 'anything-mode-line-string))
2765 source))
2766 (if anything-mode-line-string
2767 (setq mode-line-format
2768 '(" " mode-line-buffer-identification " "
2769 (line-number-mode "L%l") " " (anything-follow-mode "(F) ")
2770 (:eval (anything-show-candidate-number
2771 (when (listp anything-mode-line-string)
2772 (car anything-mode-line-string))))
2773 " " anything-mode-line-string-real "-%-")
2774 anything-mode-line-string-real
2775 (substitute-command-keys (if (listp anything-mode-line-string)
2776 (cadr anything-mode-line-string)
2777 anything-mode-line-string)))
2778 (setq mode-line-format
2779 (default-value 'mode-line-format)))
2780 (setq header-line-format
2781 (anything-interpret-value (assoc-default 'header-line source) source)))
2783 (defun anything-show-candidate-number (&optional name)
2784 "Used to display candidate number in mode-line.
2785 You can specify NAME of candidates e.g \"Buffers\" otherwise
2786 it is \"Candidate\(s\)\" by default."
2787 (propertize
2788 (format "[%s %s]"
2789 (anything-approximate-candidate-number 'in-current-source)
2790 (or name "Candidate(s)"))
2791 'face 'anything-candidate-number))
2793 (defun anything-previous-line ()
2794 "Move selection to the previous line."
2795 (interactive)
2796 (anything-move-selection-common
2797 (lambda ()
2798 (if (not (anything-pos-multiline-p))
2799 (forward-line -1) ;double forward-line is meaningful
2800 (forward-line -1) ;because evaluation order is important
2801 (anything-skip-header-and-separator-line 'previous)
2802 (let ((header-pos (anything-get-previous-header-pos))
2803 (separator-pos (anything-get-previous-candidate-separator-pos)))
2804 (when header-pos
2805 (goto-char (if (or (null separator-pos) (< separator-pos header-pos))
2806 header-pos ; first candidate
2807 separator-pos))
2808 (forward-line 1)))))
2809 'line 'previous))
2811 (defun anything-next-line ()
2812 "Move selection to the next line."
2813 (interactive)
2814 (anything-move-selection-common
2815 (lambda ()
2816 (if (not (anything-pos-multiline-p))
2817 (forward-line 1)
2818 (let ((header-pos (anything-get-next-header-pos))
2819 (separator-pos (anything-get-next-candidate-separator-pos)))
2820 (cond ((and separator-pos
2821 (or (null header-pos) (< separator-pos header-pos)))
2822 (goto-char separator-pos))
2823 (header-pos
2824 (goto-char header-pos))))))
2825 'line 'next))
2827 (defun anything-previous-page ()
2828 "Move selection back with a pageful."
2829 (interactive)
2830 (anything-move-selection-common
2831 (lambda ()
2832 (condition-case nil
2833 (scroll-down)
2834 (beginning-of-buffer (goto-char (point-min)))))
2835 'page 'previous))
2837 (defun anything-next-page ()
2838 "Move selection forward with a pageful."
2839 (interactive)
2840 (anything-move-selection-common
2841 (lambda ()
2842 (condition-case nil
2843 (scroll-up)
2844 (end-of-buffer (goto-char (point-max)))))
2845 'page 'next))
2847 (defun anything-beginning-of-buffer ()
2848 "Move selection at the top."
2849 (interactive)
2850 (anything-move-selection-common
2851 (lambda () (goto-char (point-min)))
2852 'edge 'previous))
2854 (defun anything-end-of-buffer ()
2855 "Move selection at the bottom."
2856 (interactive)
2857 (anything-move-selection-common
2858 (lambda () (goto-char (point-max)))
2859 'edge 'next))
2861 (defun anything-previous-source ()
2862 "Move selection to the previous source."
2863 (interactive)
2864 (anything-move-selection-common
2865 (lambda ()
2866 (forward-line -1)
2867 (if (bobp)
2868 (goto-char (point-max))
2869 (anything-skip-header-and-separator-line 'previous))
2870 (goto-char (anything-get-previous-header-pos))
2871 (forward-line 1))
2872 'source 'previous))
2874 (defun anything-next-source ()
2875 "Move selection to the next source."
2876 (interactive)
2877 (anything-move-selection-common
2878 (lambda ()
2879 (goto-char (or (anything-get-next-header-pos) (point-min))))
2880 'source 'next))
2882 (defun anything-goto-source (source-or-name)
2883 "Move the selection to the source SOURCE-OR-NAME."
2884 (anything-move-selection-common
2885 (lambda ()
2886 (goto-char (point-min))
2887 (let ((name (if (stringp source-or-name) source-or-name
2888 (assoc-default 'name source-or-name))))
2889 (condition-case err
2890 (while (not (string= name (anything-current-line-contents)))
2891 (goto-char (anything-get-next-header-pos)))
2892 (error (message "")))))
2893 'source 'next))
2895 (defun anything-mark-current-line (&optional resumep)
2896 "Move `anything-selection-overlay' to current line.
2897 Note that this is not related with visibles marks, which are used
2898 to mark candidates."
2899 (with-anything-window
2900 (when resumep
2901 (goto-char anything-selection-point))
2902 (move-overlay
2903 anything-selection-overlay (point-at-bol)
2904 (if (anything-pos-multiline-p)
2905 (let ((header-pos (anything-get-next-header-pos))
2906 (separator-pos (anything-get-next-candidate-separator-pos)))
2907 (or (and (null header-pos) separator-pos)
2908 (and header-pos separator-pos (< separator-pos header-pos)
2909 separator-pos)
2910 header-pos
2911 (point-max)))
2912 (1+ (point-at-eol))))
2913 (setq anything-selection-point (overlay-start anything-selection-overlay)))
2914 (anything-follow-execute-persistent-action-maybe))
2916 (defun anything-this-command-key ()
2917 (event-basic-type (elt (this-command-keys-vector) 0)))
2918 ;; (progn (read-key-sequence "Key: ") (p (anything-this-command-key)))
2920 (defun anything-select-with-shortcut-internal (types get-key-func)
2921 (if (memq anything-enable-shortcuts types)
2922 (save-selected-window
2923 (select-window (anything-window))
2924 (let* ((key (funcall get-key-func))
2925 (overlay (ignore-errors (nth (position key anything-shortcut-keys)
2926 anything-digit-overlays))))
2927 (if (not (and overlay (overlay-buffer overlay)))
2928 (when (numberp key)
2929 (select-window (minibuffer-window))
2930 (self-insert-command 1))
2931 (goto-char (overlay-start overlay))
2932 (anything-mark-current-line)
2933 (anything-exit-minibuffer))))
2934 (self-insert-command 1)))
2936 (defun anything-select-with-prefix-shortcut ()
2937 "Invoke default action with prefix shortcut."
2938 (interactive)
2939 (anything-select-with-shortcut-internal
2940 '(prefix)
2941 (lambda () (read-event "Select shortcut key: "))))
2943 (defun anything-select-with-digit-shortcut ()
2944 "Invoke default action with digit/alphabet shortcut."
2945 (interactive)
2946 (anything-select-with-shortcut-internal
2947 '(alphabet t) 'anything-this-command-key))
2949 ;; (setq anything-enable-shortcuts 'prefix)
2950 ;; (define-key anything-map "@" 'anything-select-with-prefix-shortcut)
2951 ;; (define-key anything-map (kbd "<f18>") 'anything-select-with-prefix-shortcut)
2953 (defvar anything-exit-status 0
2954 "Flag to inform whether anything have aborted or quitted.
2955 Exit with 0 mean anything exit executing an action.
2956 Exit with 1 mean anything abort with \\[keyboard-quit]
2957 It is useful for example to restore a window config if anything abort
2958 in special cases.
2959 See `anything-exit-minibuffer' and `anything-keyboard-quit'.")
2961 (defun anything-exit-minibuffer ()
2962 "Select the current candidate by exiting the minibuffer."
2963 (interactive)
2964 (unless anything-current-prefix-arg
2965 (setq anything-current-prefix-arg current-prefix-arg))
2966 (exit-minibuffer))
2968 (defun anything-keyboard-quit ()
2969 "Quit minibuffer in anything.
2970 If action buffer is displayed, kill it."
2971 (interactive)
2972 (when (get-buffer-window anything-action-buffer 'visible)
2973 (kill-buffer anything-action-buffer))
2974 (setq anything-exit-status 1)
2975 (abort-recursive-edit))
2977 (defun anything-get-next-header-pos ()
2978 "Return the position of the next header from point."
2979 (next-single-property-change (point) 'anything-header))
2981 (defun anything-get-previous-header-pos ()
2982 "Return the position of the previous header from point."
2983 (previous-single-property-change (point) 'anything-header))
2985 (defun anything-pos-multiline-p ()
2986 "Return non-nil if the current position is in the multiline source region."
2987 (get-text-property (point) 'anything-multiline))
2989 (defun anything-get-next-candidate-separator-pos ()
2990 "Return the position of the next candidate separator from point."
2991 (next-single-property-change (point) 'anything-candidate-separator))
2993 (defun anything-get-previous-candidate-separator-pos ()
2994 "Return the position of the previous candidate separator from point."
2995 (previous-single-property-change (point) 'anything-candidate-separator))
2997 (defun anything-pos-header-line-p ()
2998 "Return t if the current line is a header line."
2999 (or (get-text-property (point-at-bol) 'anything-header)
3000 (get-text-property (point-at-bol) 'anything-header-separator)))
3002 (defun anything-pos-candidate-separator-p ()
3003 "Return t if the current line is a candidate separator."
3004 (get-text-property (point-at-bol) 'anything-candidate-separator))
3007 ;; (@* "Core: help")
3008 (defun anything-help-internal (bufname insert-content-fn)
3009 "Show long message during `anything' session in BUFNAME.
3010 INSERT-CONTENT-FN is the text to be displayed in BUFNAME."
3011 (save-window-excursion
3012 (select-window (anything-window))
3013 (delete-other-windows)
3014 (switch-to-buffer (get-buffer-create bufname))
3015 (erase-buffer)
3016 (funcall insert-content-fn)
3017 (setq mode-line-format "%b (SPC,C-v:NextPage b,M-v:PrevPage other:Exit)")
3018 (setq cursor-type nil)
3019 (goto-char 1)
3020 (anything-help-event-loop)))
3022 (defun anything-help-event-loop ()
3023 (ignore-errors
3024 (loop for event = (read-event) do
3025 (case event
3026 ((?\C-v ? ) (scroll-up))
3027 ((?\M-v ?b) (scroll-down))
3028 (t (return))))))
3030 (defun anything-help ()
3031 "Help of `anything'."
3032 (interactive)
3033 (anything-help-internal
3034 " *Anything Help*"
3035 (lambda ()
3036 (insert (substitute-command-keys
3037 (anything-interpret-value anything-help-message)))
3038 (org-mode))))
3040 (defun anything-debug-output ()
3041 "Show all anything-related variables at this time."
3042 (interactive)
3043 (anything-help-internal " *Anything Debug*" 'anything-debug-output-function))
3045 (defun anything-debug-output-function (&optional vars)
3046 (message "Calculating all anything-related values...")
3047 (insert "If you debug some variables or forms, set `anything-debug-forms'
3048 to a list of forms.\n\n")
3049 (dolist (v (or vars
3050 anything-debug-forms
3051 (apropos-internal "^anything-" 'boundp)))
3052 (insert "** "
3053 (pp-to-string v) "\n"
3054 (pp-to-string (with-current-buffer anything-buffer (eval v))) "\n"))
3055 (message "Calculating all anything-related values...Done"))
3058 ;; (@* "Core: misc")
3059 (defun anything-kill-buffer-hook ()
3060 "Remove tick entry from `anything-tick-hash' when killing a buffer."
3061 (loop for key being the hash-keys in anything-tick-hash
3062 if (string-match (format "^%s/" (regexp-quote (buffer-name))) key)
3063 do (remhash key anything-tick-hash)))
3064 (add-hook 'kill-buffer-hook 'anything-kill-buffer-hook)
3066 (defun anything-preselect (candidate-or-regexp)
3067 "Move `anything-selection-overlay' to CANDIDATE-OR-REGEXP on startup."
3068 (with-anything-window
3069 (when candidate-or-regexp
3070 (goto-char (point-min))
3071 ;; go to first candidate of first source
3072 (forward-line 1)
3073 (let ((start (point)))
3074 (or (re-search-forward
3075 (concat "^" (regexp-quote candidate-or-regexp) "$") nil t)
3076 (re-search-forward candidate-or-regexp nil t)
3077 (search-forward candidate-or-regexp nil t)
3078 (goto-char start))))
3079 (anything-mark-current-line)))
3081 (defun anything-delete-current-selection ()
3082 "Delete the currently selected item."
3083 (interactive)
3084 (with-anything-window
3085 (cond ((anything-pos-multiline-p)
3086 (anything-aif (anything-get-next-candidate-separator-pos)
3087 (delete-region (point-at-bol)
3088 (1+ (progn (goto-char it) (point-at-eol))))
3089 ;; last candidate
3090 (goto-char (anything-get-previous-candidate-separator-pos))
3091 (delete-region (point-at-bol) (point-max)))
3092 (when (anything-end-of-source-p)
3093 (goto-char (or (anything-get-previous-candidate-separator-pos)
3094 (point-min)))
3095 (forward-line 1)))
3097 (delete-region (point-at-bol) (1+ (point-at-eol)))
3098 (when (anything-end-of-source-p) (forward-line -1))))
3099 (anything-mark-current-line)))
3101 (defun anything-end-of-source-p ()
3102 "Return non--nil if we are at eob or end of source."
3103 (save-excursion
3104 (forward-line 1)
3105 (or (eq (point-at-bol) (point-at-eol))
3106 (anything-pos-header-line-p)
3107 (eobp))))
3109 (defun anything-edit-current-selection-internal (func)
3110 (with-anything-window
3111 (beginning-of-line)
3112 (let ((realvalue (get-text-property (point) 'anything-realvalue)))
3113 (funcall func)
3114 (beginning-of-line)
3115 (and realvalue
3116 (put-text-property (point) (point-at-eol)
3117 'anything-realvalue realvalue))
3118 (anything-mark-current-line))))
3120 (defmacro anything-edit-current-selection (&rest forms)
3121 "Evaluate FORMS at current selection in the anything buffer.
3122 You can edit the line."
3123 (declare (indent 0) (debug t))
3124 `(anything-edit-current-selection-internal
3125 (lambda () ,@forms)))
3127 (defun anything-set-pattern (pattern &optional noupdate)
3128 "Set minibuffer contents to PATTERN.
3129 if optional NOUPDATE is non-nil, anything buffer is not changed."
3130 (with-selected-window (or (active-minibuffer-window) (minibuffer-window))
3131 (delete-minibuffer-contents)
3132 (insert pattern))
3133 (when noupdate
3134 (setq anything-pattern pattern)
3135 (anything-hooks 'cleanup)
3136 (run-with-idle-timer 0 nil 'anything-hooks 'setup)))
3138 (defun anything-delete-minibuffer-contents ()
3139 "Same as `delete-minibuffer-contents' but this is a command."
3140 (interactive)
3141 (anything-set-pattern ""))
3142 (defalias 'anything-delete-minibuffer-content 'anything-delete-minibuffer-contents)
3145 ;;; Plugins
3147 ;; (@* "Built-in plug-in: type")
3148 (defun anything-compile-source--type (source)
3149 (anything-aif (assoc-default 'type source)
3150 (append source (assoc-default it anything-type-attributes) nil)
3151 source))
3153 ;; `define-anything-type-attribute' is public API.
3155 (defun anything-add-type-attribute (type definition)
3156 (anything-aif (assq type anything-type-attributes)
3157 (setq anything-type-attributes (delete it anything-type-attributes)))
3158 (push (cons type definition) anything-type-attributes))
3160 (defvar anything-types nil)
3161 (defun anything-document-type-attribute (type doc)
3162 (add-to-list 'anything-types type t)
3163 (put type 'anything-typeattrdoc
3164 (concat "- " (symbol-name type) "\n\n" doc "\n")))
3166 (defadvice documentation-property (after anything-document-type-attribute activate)
3167 "Display type attributes' documentation as `anything-type-attributes' docstring."
3168 (when (eq (ad-get-arg 0) 'anything-type-attributes)
3169 (setq ad-return-value
3170 (concat ad-return-value "\n\n++++ Types currently defined ++++\n"
3171 (mapconcat (lambda (sym) (get sym 'anything-typeattrdoc))
3172 anything-types "\n")))))
3174 ;; (@* "Built-in plug-in: dummy")
3175 (defun anything-dummy-candidate (candidate source)
3176 "Use `anything-pattern' as CANDIDATE in SOURCE."
3177 ;; `source' is defined in filtered-candidate-transformer
3178 (list anything-pattern))
3180 (defun anything-compile-source--dummy (source)
3181 (if (assoc 'dummy source)
3182 (append source
3183 '((candidates "dummy")
3184 (accept-empty)
3185 (match identity)
3186 (filtered-candidate-transformer . anything-dummy-candidate)
3187 (disable-shortcuts)
3188 (volatile)))
3189 source))
3191 ;; (@* "Built-in plug-in: disable-shortcuts")
3192 (defvar anything-orig-enable-shortcuts nil)
3193 (defun anything-save-enable-shortcuts ()
3194 (anything-once
3195 (lambda ()
3196 (setq anything-orig-enable-shortcuts anything-enable-shortcuts
3197 anything-enable-shortcuts nil))))
3199 (defun anything-compile-source--disable-shortcuts (source)
3200 (if (assoc 'disable-shortcuts source)
3201 (append `((init ,@(anything-mklist (assoc-default 'init source))
3202 anything-save-enable-shortcuts)
3203 (resume ,@(anything-mklist (assoc-default 'resume source))
3204 anything-save-enable-shortcuts)
3205 (cleanup ,@(anything-mklist (assoc-default 'cleanup source))
3206 (lambda () (setq anything-enable-shortcuts
3207 anything-orig-enable-shortcuts))))
3208 source)
3209 source))
3211 ;; (@* "Built-in plug-in: candidates-in-buffer")
3212 (defun anything-candidates-in-buffer ()
3213 "Get candidates from the candidates buffer according to `anything-pattern'.
3215 BUFFER is `anything-candidate-buffer' by default. Each
3216 candidate must be placed in one line. This function is meant to
3217 be used in candidates-in-buffer or candidates attribute of an
3218 anything source. Especially fast for many (1000+) candidates.
3221 '((name . \"many files\")
3222 (init . (lambda () (with-current-buffer (anything-candidate-buffer 'local)
3223 (insert-many-filenames))))
3224 (search re-search-forward) ; optional
3225 (candidates-in-buffer)
3226 (type . file))
3228 +===============================================================+
3229 | The new way of making and narrowing candidates: Using buffers |
3230 +===============================================================+
3232 By default, `anything' makes candidates by evaluating the
3233 candidates function, then narrows them by `string-match' for each
3234 candidate.
3236 But this way is very slow for many candidates. The new way is
3237 storing all candidates in a buffer and narrowing them by
3238 `re-search-forward'. Search function is customizable by search
3239 attribute. The important point is that buffer processing is MUCH
3240 FASTER than string list processing and is the Emacs way.
3242 The init function writes all candidates to a newly-created
3243 candidate buffer. The candidates buffer is created or specified
3244 by `anything-candidate-buffer'. Candidates are stored in a line.
3246 The candidates function narrows all candidates, IOW creates a
3247 subset of candidates dynamically. It is the task of
3248 `anything-candidates-in-buffer'. As long as
3249 `anything-candidate-buffer' is used,`(candidates-in-buffer)' is
3250 sufficient in most cases.
3252 Note that `(candidates-in-buffer)' is shortcut of three attributes:
3253 (candidates . anything-candidates-in-buffer)
3254 (volatile)
3255 (match identity)
3256 And `(candidates-in-buffer . func)' is shortcut of three attributes:
3257 (candidates . func)
3258 (volatile)
3259 (match identity)
3260 The expansion is performed in `anything-get-sources'.
3262 The candidates-in-buffer attribute implies the volatile attribute.
3263 The volatile attribute is needed because `anything-candidates-in-buffer'
3264 creates candidates dynamically and need to be called everytime
3265 `anything-pattern' changes.
3267 Because `anything-candidates-in-buffer' plays the role of `match' attribute
3268 function, specifying `(match identity)' makes the source slightly faster.
3270 To customize `anything-candidates-in-buffer' behavior, use search,
3271 get-line and search-from-end attributes. See also `anything-sources' docstring."
3272 (declare (special source))
3273 (anything-candidates-in-buffer-1
3274 (anything-candidate-buffer)
3275 anything-pattern
3276 (or (assoc-default 'get-line source)
3277 #'buffer-substring-no-properties)
3278 ;; use external variable `source'.
3279 (or (assoc-default 'search source)
3280 (if (assoc 'search-from-end source)
3281 '(anything-candidates-in-buffer-search-from-end)
3282 '(anything-candidates-in-buffer-search-from-start)))
3283 (anything-candidate-number-limit source)
3284 (assoc 'search-from-end source)))
3286 (defun anything-candidates-in-buffer-search-from-start (pattern)
3287 "Search PATTERN with `re-search-forward' with bound and noerror args."
3288 (re-search-forward pattern nil t))
3290 (defun anything-candidates-in-buffer-search-from-end (pattern)
3291 "Search PATTERN with `re-search-backward' with bound and noerror args."
3292 (re-search-backward pattern nil t))
3294 (defun anything-candidates-in-buffer-1 (buffer pattern get-line-fn
3295 search-fns limit search-from-end)
3296 ;; buffer == nil when candidates buffer does not exist.
3297 (when buffer
3298 (with-current-buffer buffer
3299 (let ((start-point (if search-from-end (point-max) (point-min)))
3300 (endp (if search-from-end #'bobp #'eobp)))
3301 (goto-char (1- start-point))
3302 (if (string= pattern "")
3303 (anything-initial-candidates-from-candidate-buffer
3304 endp get-line-fn limit search-from-end)
3305 (anything-search-from-candidate-buffer
3306 pattern get-line-fn search-fns limit search-from-end
3307 start-point endp))))))
3309 (defun anything-point-is-moved (proc)
3310 "If point is moved after executing PROC, return t, otherwise nil."
3311 (/= (point) (progn (funcall proc) (point))))
3313 (defun anything-search-from-candidate-buffer (pattern get-line-fn search-fns
3314 limit search-from-end
3315 start-point endp)
3316 (let (buffer-read-only
3317 matches exit newmatches)
3318 (anything-search-from-candidate-buffer-internal
3319 (lambda ()
3320 (clrhash anything-cib-hash)
3321 (dolist (searcher search-fns)
3322 (goto-char start-point)
3323 (setq newmatches nil)
3324 (loop with item-count = 0
3325 while (funcall searcher pattern)
3326 for cand = (funcall get-line-fn (point-at-bol) (point-at-eol))
3327 do (anything-accumulate-candidates-internal
3328 cand newmatches anything-cib-hash item-count limit)
3329 unless (anything-point-is-moved
3330 (lambda ()
3331 (if search-from-end
3332 (goto-char (1- (point-at-bol)))
3333 (forward-line 1))))
3334 return nil)
3335 (setq matches (append matches (nreverse newmatches)))
3336 (if exit (return)))
3337 (delq nil matches)))))
3339 (defun anything-initial-candidates-from-candidate-buffer (endp get-line-fn limit search-from-end)
3340 (delq nil (loop with next-line-fn =
3341 (if search-from-end
3342 (lambda (x) (goto-char (max (1- (point-at-bol)) 1)))
3343 #'forward-line)
3344 until (funcall endp)
3345 for i from 1 to limit
3346 collect (funcall get-line-fn (point-at-bol) (point-at-eol))
3347 do (funcall next-line-fn 1))))
3349 (defun anything-search-from-candidate-buffer-internal (search-fn)
3350 (goto-char (point-min))
3351 (insert "\n")
3352 (goto-char (point-max))
3353 (insert "\n")
3354 (unwind-protect
3355 (funcall search-fn)
3356 (goto-char (point-min))
3357 (delete-char 1)
3358 (goto-char (1- (point-max)))
3359 (delete-char 1)
3361 (set-buffer-modified-p nil)))
3363 (defun anything-candidate-buffer (&optional create-or-buffer)
3364 "Register and return a buffer containing candidates of current source.
3365 `anything-candidate-buffer' searches buffer-local candidates buffer first,
3366 then global candidates buffer.
3368 Acceptable values of CREATE-OR-BUFFER:
3370 - nil (omit)
3371 Only return the candidates buffer.
3372 - a buffer
3373 Register a buffer as a candidates buffer.
3374 - 'global
3375 Create a new global candidates buffer,
3376 named \" *anything candidates:SOURCE*\".
3377 - other non-nil value
3378 Create a new local candidates buffer,
3379 named \" *anything candidates:SOURCE*ANYTHING-CURRENT-BUFFER\"."
3380 (let* ((global-bname (format " *anything candidates:%s*" anything-source-name))
3381 (local-bname (format " *anything candidates:%s*%s"
3382 anything-source-name
3383 (buffer-name anything-current-buffer)))
3384 (register-func
3385 (lambda ()
3386 (setq anything-candidate-buffer-alist
3387 (cons (cons anything-source-name create-or-buffer)
3388 (delete (assoc anything-source-name
3389 anything-candidate-buffer-alist)
3390 anything-candidate-buffer-alist)))))
3391 (kill-buffers-func
3392 (lambda ()
3393 (loop for b in (buffer-list)
3394 if (string-match (format "^%s" (regexp-quote global-bname))
3395 (buffer-name b))
3396 do (kill-buffer b))))
3397 (create-func
3398 (lambda ()
3399 (with-current-buffer
3400 (get-buffer-create (if (eq create-or-buffer 'global)
3401 global-bname
3402 local-bname))
3403 (buffer-disable-undo)
3404 (erase-buffer)
3405 (font-lock-mode -1))))
3406 (return-func
3407 (lambda ()
3408 (or (get-buffer local-bname)
3409 (get-buffer global-bname)
3410 (anything-aif (assoc-default anything-source-name
3411 anything-candidate-buffer-alist)
3412 (and (buffer-live-p it) it))))))
3413 (when create-or-buffer
3414 (funcall register-func)
3415 (unless (bufferp create-or-buffer)
3416 (and (eq create-or-buffer 'global) (funcall kill-buffers-func))
3417 (funcall create-func)))
3418 (funcall return-func)))
3420 (defun anything-compile-source--candidates-in-buffer (source)
3421 (anything-aif (assoc 'candidates-in-buffer source)
3422 (append source
3423 `((candidates . ,(or (cdr it) 'anything-candidates-in-buffer))
3424 (volatile) (match identity)))
3425 source))
3428 ;; (@* "Utility: resplit anything window")
3429 (defun anything-toggle-resplit-window ()
3430 "Toggle resplit anything window, vertically or horizontally."
3431 (interactive)
3432 (with-anything-window
3433 (let ((before-height (window-height)))
3434 (delete-window)
3435 (set-window-buffer
3436 (select-window (if (= (window-height) before-height)
3437 (prog1
3438 (split-window-vertically)
3439 (setq anything-split-window-state 'vertical))
3440 (setq anything-split-window-state 'horizontal)
3441 (split-window-horizontally)))
3442 anything-buffer))))
3444 ;; (@* "Utility: Resize anything window.")
3445 (defun anything-enlarge-window-1 (n)
3446 "Enlarge or narrow anything window.
3447 If N is positive enlarge, if negative narrow."
3448 (unless anything-samewindow
3449 (let ((horizontal-p (eq anything-split-window-state 'horizontal)))
3450 (with-anything-window
3451 (enlarge-window n horizontal-p)))))
3453 (defun anything-narrow-window ()
3454 "Narrow anything window."
3455 (interactive)
3456 (anything-enlarge-window-1 -1))
3458 (defun anything-enlarge-window ()
3459 "Enlarge anything window."
3460 (interactive)
3461 (anything-enlarge-window-1 1))
3463 ;; (@* "Utility: select another action by key")
3464 (defun anything-select-nth-action (n)
3465 "Select the N nth action for the currently selected candidate."
3466 (setq anything-saved-selection (anything-get-selection))
3467 (unless anything-saved-selection
3468 (error "Nothing is selected"))
3469 (setq anything-saved-action (anything-get-nth-action n (anything-get-action)))
3470 (anything-exit-minibuffer))
3472 (defun anything-get-nth-action (n action)
3473 (cond ((and (zerop n) (functionp action))
3474 action)
3475 ((listp action)
3476 (or (cdr (elt action n))
3477 (error "No such action")))
3478 ((and (functionp action) (< 0 n))
3479 (error "Sole action"))
3481 (error "Error in `anything-select-nth-action'"))))
3483 (defun anything-select-2nd-action ()
3484 "Select the 2nd action for the currently selected candidate."
3485 (interactive)
3486 (anything-select-nth-action 1))
3488 (defun anything-select-3rd-action ()
3489 "Select the 3rd action for the currently selected candidate."
3490 (interactive)
3491 (anything-select-nth-action 2))
3493 (defun anything-select-4th-action ()
3494 "Select the 4th action for the currently selected candidate."
3495 (interactive)
3496 (anything-select-nth-action 3))
3498 (defun anything-select-2nd-action-or-end-of-line ()
3499 "Select the 2nd action for the currently selected candidate.
3500 This happen when point is at the end of minibuffer.
3501 Otherwise goto the end of minibuffer."
3502 (interactive)
3503 (if (eolp)
3504 (anything-select-nth-action 1)
3505 (end-of-line)))
3507 ;; (@* "Utility: Persistent Action")
3508 (defmacro with-anything-display-same-window (&rest body)
3509 "Execute BODY in the window used for persistent action.
3510 Make `pop-to-buffer' and `display-buffer' display in the same window."
3511 (declare (indent 0) (debug t))
3512 `(let ((display-buffer-function 'anything-persistent-action-display-buffer))
3513 ,@body))
3515 (defvar anything-persistent-action-display-window nil)
3516 (defun anything-initialize-persistent-action ()
3517 (set (make-local-variable 'anything-persistent-action-display-window) nil))
3519 (defun* anything-execute-persistent-action (&optional (attr 'persistent-action) onewindow)
3520 "Perform the associated action ATTR without quitting anything.
3521 ATTR default is 'persistent-action', but it can be anything else.
3522 In this case you have to add this new attribute to your source.
3523 When `anything-samewindow' and ONEWINDOW are non--nil,
3524 the anything window is never split in persistent action."
3525 (interactive)
3526 (anything-log "executing persistent-action")
3527 (with-anything-window
3528 (save-selected-window
3529 (anything-select-persistent-action-window onewindow)
3530 (anything-log-eval (current-buffer))
3531 (let ((anything-in-persistent-action t))
3532 (with-anything-display-same-window
3533 (anything-execute-selection-action
3535 (or (assoc-default attr (anything-get-current-source))
3536 (anything-get-action))
3538 (anything-log-run-hook 'anything-after-persistent-action-hook))))))
3541 (defun anything-persistent-action-display-window (&optional onewindow)
3542 "Return the window that will be used for presistent action.
3543 If ONEWINDOW is non--nil window will not be splitted in persistent action
3544 if `anything-samewindow' is non--nil also."
3545 (with-anything-window
3546 (setq anything-persistent-action-display-window
3547 (cond ((window-live-p anything-persistent-action-display-window)
3548 anything-persistent-action-display-window)
3549 ((and anything-samewindow (one-window-p t) (not onewindow))
3550 (split-window))
3551 ((get-buffer-window anything-current-buffer))
3553 (next-window (selected-window) 1))))))
3555 (defun anything-select-persistent-action-window (&optional onewindow)
3556 "Select the window that will be used for persistent action.
3557 See `anything-persistent-action-display-window' for how to use ONEWINDOW."
3558 (select-window (get-buffer-window (anything-buffer-get)))
3559 (select-window
3560 (setq minibuffer-scroll-window
3561 (anything-persistent-action-display-window onewindow))))
3563 (defun anything-persistent-action-display-buffer (buf &optional not-this-window)
3564 "Make `pop-to-buffer' and `display-buffer' display in the same window.
3565 If `anything-persistent-action-use-special-display' is non-nil and
3566 BUF is to be displayed by `special-display-function', use it.
3567 Otherwise ignores `special-display-buffer-names' and `special-display-regexps'.
3568 Argument NOT-THIS-WINDOW if present will be used as
3569 second argument of `display-buffer'."
3570 (let* ((name (buffer-name buf))
3571 display-buffer-function pop-up-windows pop-up-frames
3572 (same-window-regexps
3573 (unless (and anything-persistent-action-use-special-display
3574 (or (member name
3575 (mapcar (lambda (x) (or (car-safe x) x))
3576 special-display-buffer-names))
3577 (remove-if-not
3578 (lambda (x) (string-match (or (car-safe x) x) name))
3579 special-display-regexps)))
3580 '("."))))
3581 (display-buffer buf not-this-window)))
3583 ;; scroll-other-window(-down)? for persistent-action
3584 (defun anything-scroll-other-window-base (command)
3585 (with-selected-window (anything-persistent-action-display-window)
3586 (funcall command anything-scroll-amount)))
3588 (defun anything-scroll-other-window ()
3589 "Scroll other window (not *Anything* window) upward."
3590 (interactive)
3591 (anything-scroll-other-window-base 'scroll-up))
3593 (defun anything-scroll-other-window-down ()
3594 "Scroll other window (not *Anything* window) downward."
3595 (interactive)
3596 (anything-scroll-other-window-base 'scroll-down))
3599 ;; (@* "Utility: Visible Mark")
3600 (defface anything-visible-mark
3601 '((((min-colors 88) (background dark))
3602 (:background "green1" :foreground "black"))
3603 (((background dark)) (:background "green" :foreground "black"))
3604 (((min-colors 88)) (:background "green1"))
3605 (t (:background "green")))
3606 "Face for visible mark."
3607 :group 'anything)
3609 (defvar anything-visible-mark-face 'anything-visible-mark)
3610 (defvar anything-visible-mark-overlays nil)
3612 (defun anything-clear-visible-mark ()
3613 (with-current-buffer (anything-buffer-get)
3614 (mapc 'delete-overlay anything-visible-mark-overlays)
3615 (set (make-local-variable 'anything-visible-mark-overlays) nil)))
3616 (add-hook 'anything-after-initialize-hook 'anything-clear-visible-mark)
3618 (defvar anything-marked-candidates nil
3619 "Marked candadates. List of \(source . real\) pair.")
3621 (defun anything-this-visible-mark ()
3622 (loop for o in anything-visible-mark-overlays
3623 when (equal (point-at-bol) (overlay-start o))
3624 return o))
3626 (defun anything-delete-visible-mark (overlay)
3627 (setq anything-marked-candidates
3628 (remove
3629 (cons (anything-get-current-source) (anything-get-selection))
3630 anything-marked-candidates))
3631 (delete-overlay overlay)
3632 (setq anything-visible-mark-overlays
3633 (delq overlay anything-visible-mark-overlays)))
3635 (defun anything-make-visible-mark ()
3636 (let ((o (make-overlay (point-at-bol) (1+ (point-at-eol)))))
3637 (overlay-put o 'face anything-visible-mark-face)
3638 (overlay-put o 'source (assoc-default 'name (anything-get-current-source)))
3639 (overlay-put o 'string (buffer-substring (overlay-start o) (overlay-end o)))
3640 (overlay-put o 'real (anything-get-selection))
3641 (add-to-list 'anything-visible-mark-overlays o))
3642 (push (cons (anything-get-current-source) (anything-get-selection))
3643 anything-marked-candidates))
3645 (defun anything-toggle-visible-mark ()
3646 "Toggle anything visible mark at point."
3647 (interactive)
3648 (with-anything-window
3649 (anything-aif (anything-this-visible-mark)
3650 (anything-delete-visible-mark it)
3651 (anything-make-visible-mark))
3652 (anything-next-line)))
3654 (defun anything-display-all-visible-marks ()
3655 "Show all `anything' visible marks strings."
3656 (interactive)
3657 (with-anything-window
3658 (lexical-let ((overlays (reverse anything-visible-mark-overlays)))
3659 (anything-run-after-quit
3660 (lambda ()
3661 (with-output-to-temp-buffer "*anything visible marks*"
3662 (dolist (o overlays) (princ (overlay-get o 'string)))))))))
3664 (defun anything-marked-candidates ()
3665 "Return marked candidates of current source if any.
3666 Otherwise one element list of current selection.
3668 It is analogous to `dired-get-marked-files'."
3669 (with-current-buffer (anything-buffer-get)
3670 (let ((cands
3671 (if anything-marked-candidates
3672 (loop with current-src = (anything-get-current-source)
3673 for (source . real) in (reverse anything-marked-candidates)
3674 when (equal current-src source)
3675 collect (anything-coerce-selection real source))
3676 (list (anything-get-selection)))))
3677 (anything-log-eval cands)
3678 cands)))
3680 (defun anything-reset-marked-candidates ()
3681 (with-current-buffer (anything-buffer-get)
3682 (set (make-local-variable 'anything-marked-candidates) nil)))
3684 (add-hook 'anything-after-initialize-hook 'anything-reset-marked-candidates)
3685 ;; (add-hook 'anything-after-action-hook 'anything-reset-marked-candidates)
3687 (defun anything-current-source-name= (name)
3688 (save-excursion
3689 (goto-char (anything-get-previous-header-pos))
3690 (equal name (anything-current-line-contents))))
3692 (defun anything-revive-visible-mark ()
3693 "Restore marked candidates when anything update display."
3694 (with-current-buffer anything-buffer
3695 (dolist (o anything-visible-mark-overlays)
3696 (goto-char (point-min))
3697 (while (and (search-forward (overlay-get o 'string) nil t)
3698 (anything-current-source-name= (overlay-get o 'source)))
3699 ;; Calculate real value of candidate.
3700 ;; It can be nil if candidate have only a display value.
3701 (let ((real (get-text-property (point-at-bol 0) 'anything-realvalue)))
3702 (if real
3703 ;; Check if real value of current candidate is the same
3704 ;; that the one stored in overlay.
3705 (and (string= (overlay-get o 'real) real)
3706 (move-overlay o (point-at-bol 0) (1+ (point-at-eol 0))))
3707 (move-overlay o (point-at-bol 0) (1+ (point-at-eol 0)))))))))
3708 (add-hook 'anything-update-hook 'anything-revive-visible-mark)
3710 (defun anything-next-point-in-list (curpos points &optional prev)
3711 (cond
3712 ;; rule out special cases
3713 ((null points) curpos)
3714 ((and prev (< curpos (car points))) curpos)
3715 ((< (car (last points)) curpos)
3716 (if prev (car (last points)) curpos))
3718 (nth (if prev
3719 (loop for pt in points
3720 for i from 0
3721 if (<= curpos pt)
3722 return (1- i))
3723 (loop for pt in points
3724 for i from 0
3725 if (< curpos pt)
3726 return i))
3727 points))))
3729 (defun anything-next-visible-mark (&optional prev)
3730 "Move next anything visible mark.
3731 If PREV is non-nil move to precedent."
3732 (interactive)
3733 (with-anything-window
3734 (ignore-errors
3735 (goto-char (anything-next-point-in-list
3736 (point)
3737 (sort (mapcar 'overlay-start anything-visible-mark-overlays) '<)
3738 prev)))
3739 (anything-mark-current-line)))
3741 (defun anything-prev-visible-mark ()
3742 "Move previous anything visible mark."
3743 (interactive)
3744 (anything-next-visible-mark t))
3746 ;; (@* "Utility: Selection Paste")
3747 (defun anything-yank-selection ()
3748 "Set minibuffer contents to current selection."
3749 (interactive)
3750 (anything-set-pattern (anything-get-selection nil t)))
3752 (defun anything-kill-selection-and-quit ()
3753 "Store current selection to kill ring.
3754 You can paste it by typing \\[yank]."
3755 (interactive)
3756 (anything-run-after-quit
3757 (lambda (sel)
3758 (kill-new sel)
3759 (message "Killed: %s" sel))
3760 (anything-get-selection nil t)))
3763 ;; (@* "Utility: Automatical execution of persistent-action")
3764 (add-to-list 'minor-mode-alist '(anything-follow-mode " AFollow"))
3765 (defun anything-follow-mode ()
3766 "If this mode is on, persistent action is executed everytime the cursor is moved."
3767 (interactive)
3768 (with-current-buffer anything-buffer
3769 (setq anything-follow-mode (not anything-follow-mode))
3770 (message "anything-follow-mode is %s"
3771 (if anything-follow-mode "enabled" "disabled"))))
3773 (defun anything-follow-execute-persistent-action-maybe ()
3774 "Execute persistent action in mode `anything-follow-mode'.
3775 This happen after `anything-input-idle-delay' secs."
3776 (and (not (get-buffer-window anything-action-buffer 'visible))
3777 (buffer-local-value 'anything-follow-mode
3778 (get-buffer-create anything-buffer))
3779 (sit-for (and anything-input-idle-delay
3780 (max anything-input-idle-delay 0.1)))
3781 (anything-window)
3782 (anything-get-selection)
3783 (save-excursion
3784 (anything-execute-persistent-action))))
3787 ;; (@* "Utility: Migrate `anything-sources' to my-anything command")
3788 (defun anything-migrate-sources ()
3789 "Help to migrate to new `anything' way."
3790 (interactive)
3791 (with-current-buffer (get-buffer-create "*anything migrate*")
3792 (erase-buffer)
3793 (insert (format "\
3794 Setting `anything-sources' directly is not good because
3795 `anything' is not for one command. For now, interactive use of
3796 `anything' (M-x anything) is only for demonstration purpose.
3797 So you should define commands calling `anything'.
3798 I help you to migrate to the new way.
3800 The code below is automatically generated from current
3801 `anything-sources' value. You can use the `my-anything' command
3802 now!
3804 Copy and paste it to your .emacs. Then substitute `my-anything'
3805 for `anything' bindings in all `define-key', `local-set-key' and
3806 `global-set-key' calls.
3808 \(defun my-anything ()
3809 \"Anything command for you.
3811 It is automatically generated by `anything-migrate-sources'.\"
3812 (interactive)
3813 (anything-other-buffer
3815 \"*my-anything*\"))
3816 " anything-sources))
3817 (eval-last-sexp nil)
3818 (substitute-key-definition 'anything 'my-anything global-map)
3819 (pop-to-buffer (current-buffer))))
3822 ;; (@* "Compatibility")
3824 ;; Copied assoc-default from XEmacs version 21.5.12
3825 (unless (fboundp 'assoc-default)
3826 (defun assoc-default (key alist &optional test default)
3827 "Find object KEY in a pseudo-alist ALIST.
3828 ALIST is a list of conses or objects. Each element (or the element's car,
3829 if it is a cons) is compared with KEY by evaluating (TEST (car elt) KEY).
3830 If that is non-nil, the element matches;
3831 then `assoc-default' returns the element's cdr, if it is a cons,
3832 or DEFAULT if the element is not a cons.
3834 If no element matches, the value is nil.
3835 If TEST is omitted or nil, `equal' is used."
3836 (let (found (tail alist) value)
3837 (while (and tail (not found))
3838 (let ((elt (car tail)))
3839 (when (funcall (or test 'equal) (if (consp elt) (car elt) elt) key)
3840 (setq found t value (if (consp elt) (cdr elt) default))))
3841 (setq tail (cdr tail)))
3842 value)))
3844 ;; Function not available in XEmacs,
3845 (unless (fboundp 'minibuffer-contents)
3846 (defun minibuffer-contents ()
3847 "Return the user input in a minbuffer as a string.
3848 The current buffer must be a minibuffer."
3849 (field-string (point-max)))
3851 (defun delete-minibuffer-contents ()
3852 "Delete all user input in a minibuffer.
3853 The current buffer must be a minibuffer."
3854 (delete-field (point-max))))
3856 ;; Function not available in older Emacs (<= 22.1).
3857 (unless (fboundp 'buffer-chars-modified-tick)
3858 (defun buffer-chars-modified-tick (&optional buffer)
3859 "Return BUFFER's character-change tick counter.
3860 Each buffer has a character-change tick counter, which is set to the
3861 value of the buffer's tick counter (see `buffer-modified-tick'), each
3862 time text in that buffer is inserted or deleted. By comparing the
3863 values returned by two individual calls of `buffer-chars-modified-tick',
3864 you can tell whether a character change occurred in that buffer in
3865 between these calls. No argument or nil as argument means use current
3866 buffer as BUFFER."
3867 (with-current-buffer (or buffer (current-buffer))
3868 (if (listp buffer-undo-list)
3869 (length buffer-undo-list)
3870 (buffer-modified-tick)))))
3873 ;; (@* "CUA workaround")
3874 (defadvice cua-delete-region (around anything-avoid-cua activate)
3875 (ignore-errors ad-do-it))
3877 (defadvice copy-region-as-kill (around anything-avoid-cua activate)
3878 (if cua-mode
3879 (ignore-errors ad-do-it)
3880 ad-do-it))
3882 ;;(@* "Attribute Documentation")
3883 (defun anything-describe-anything-attribute (anything-attribute)
3884 "Display the full documentation of ANYTHING-ATTRIBUTE.
3885 ANYTHING-ATTRIBUTE should be a symbol."
3886 (interactive (list (intern
3887 (completing-read
3888 "Describe anything attribute: "
3889 (mapcar 'symbol-name anything-additional-attributes)))))
3890 (with-output-to-temp-buffer "*Help*"
3891 (princ (get anything-attribute 'anything-attrdoc))))
3893 (put 'anything-document-attribute 'lisp-indent-function 2)
3894 (anything-document-attribute 'name "mandatory"
3895 " The name of the source. It is also the heading which appears
3896 above the list of matches from the source. Must be unique. ")
3897 (anything-document-attribute 'header-name "optional"
3898 " A function returning the display string of the header. Its
3899 argument is the name of the source. This attribute is useful to
3900 add an additional information with the source name. ")
3901 (anything-document-attribute 'candidates "mandatory if candidates-in-buffer attribute is not provided"
3902 " Specifies how to retrieve candidates from the source. It can
3903 either be a variable name, a function called with no parameters
3904 or the actual list of candidates.
3906 The list must be a list whose members are strings, symbols
3907 or (DISPLAY . REAL) pairs.
3909 In case of (DISPLAY . REAL) pairs, the DISPLAY string is shown
3910 in the Anything buffer, but the REAL one is used as action
3911 argument when the candidate is selected. This allows a more
3912 readable presentation for candidates which would otherwise be,
3913 for example, too long or have a common part shared with other
3914 candidates which can be safely replaced with an abbreviated
3915 string for display purposes.
3917 Note that if the (DISPLAY . REAL) form is used then pattern
3918 matching is done on the displayed string, not on the real
3919 value.
3921 If the candidates have to be retrieved asynchronously (for
3922 example, by an external command which takes a while to run)
3923 then the function should start the external command
3924 asynchronously and return the associated process object.
3925 Anything will take care of managing the process (receiving the
3926 output from it, killing it if necessary, etc.). The process
3927 should return candidates matching the current pattern (see
3928 variable `anything-pattern'.)
3930 Note that currently results from asynchronous sources appear
3931 last in the anything buffer regardless of their position in
3932 `anything-sources'. ")
3933 (anything-document-attribute 'action "mandatory if type attribute is not provided"
3934 " It is a list of (DISPLAY . FUNCTION) pairs or FUNCTION.
3935 FUNCTION is called with one parameter: the selected candidate.
3937 An action other than the default can be chosen from this list
3938 of actions for the currently selected candidate (by default
3939 with TAB). The DISPLAY string is shown in the completions
3940 buffer and the FUNCTION is invoked when an action is
3941 selected. The first action of the list is the default. ")
3942 (anything-document-attribute 'coerce "optional"
3943 " It's a function called with one argument: the selected candidate.
3945 This function is intended for type convertion.
3946 In normal case, the selected candidate (string) is passed to action function.
3947 If coerce function is specified, it is called just before action function.
3949 Example: converting string to symbol
3950 (coerce . intern) ")
3951 (anything-document-attribute 'type "optional if action attribute is provided"
3952 " Indicates the type of the items the source returns.
3954 Merge attributes not specified in the source itself from
3955 `anything-type-attributes'.
3957 This attribute is implemented by plug-in. ")
3958 (anything-document-attribute 'init "optional"
3959 " Function called with no parameters when anything is started. It
3960 is useful for collecting current state information which can be
3961 used to create the list of candidates later.
3963 For example, if a source needs to work with the current
3964 directory then it can store its value here, because later
3965 anything does its job in the minibuffer and in the
3966 `anything-buffer' and the current directory can be different
3967 there. ")
3968 (anything-document-attribute 'delayed-init "optional"
3969 " Function called with no parameters before candidate function is
3970 called. It is similar with `init' attribute, but its
3971 evaluation is deferred. It is useful to combine with ")
3972 (anything-document-attribute 'match "optional"
3973 " List of functions called with one parameter: a candidate. The
3974 function should return non-nil if the candidate matches the
3975 current pattern (see variable `anything-pattern').
3977 This attribute allows the source to override the default
3978 pattern matching based on `string-match'. It can be used, for
3979 example, to implement a source for file names and do the
3980 pattern matching on the basename of files, since it's more
3981 likely one is typing part of the basename when searching for a
3982 file, instead of some string anywhere else in its path.
3984 If the list contains more than one function then the list of
3985 matching candidates from the source is constructed by appending
3986 the results after invoking the first function on all the
3987 potential candidates, then the next function, and so on. The
3988 matching candidates supplied by the first function appear first
3989 in the list of results and then results from the other
3990 functions, respectively.
3992 This attribute has no effect for asynchronous sources (see
3993 attribute `candidates'), since they perform pattern matching
3994 themselves. ")
3995 (anything-document-attribute 'candidate-transformer "optional"
3996 " It's a function or a list of functions called with one argument
3997 when the completion list from the source is built. The argument
3998 is the list of candidates retrieved from the source. The
3999 function should return a transformed list of candidates which
4000 will be used for the actual completion. If it is a list of
4001 functions, it calls each function sequentially.
4003 This can be used to transform or remove items from the list of
4004 candidates.
4006 Note that `candidates' is run already, so the given transformer
4007 function should also be able to handle candidates with (DISPLAY
4008 . REAL) format. ")
4009 (anything-document-attribute 'filtered-candidate-transformer "optional"
4010 " It has the same format as `candidate-transformer', except the
4011 function is called with two parameters: the candidate list and
4012 the source.
4014 This transformer is run on the candidate list which is already
4015 filtered by the current pattern. While `candidate-transformer'
4016 is run only once, it is run every time the input pattern is
4017 changed.
4019 It can be used to transform the candidate list dynamically, for
4020 example, based on the current pattern.
4022 In some cases it may also be more efficent to perform candidate
4023 transformation here, instead of with `candidate-transformer'
4024 even if this transformation is done every time the pattern is
4025 changed. For example, if a candidate set is very large then
4026 `candidate-transformer' transforms every candidate while only
4027 some of them will actually be dislpayed due to the limit
4028 imposed by `anything-candidate-number-limit'.
4030 Note that `candidates' and `candidate-transformer' is run
4031 already, so the given transformer function should also be able
4032 to handle candidates with (DISPLAY . REAL) format.
4034 This option has no effect for asynchronous sources. (Not yet,
4035 at least. ")
4036 (anything-document-attribute 'action-transformer "optional"
4037 " It's a function or a list of functions called with two
4038 arguments when the action list from the source is
4039 assembled. The first argument is the list of actions, the
4040 second is the current selection. If it is a list of functions,
4041 it calls each function sequentially.
4043 The function should return a transformed action list.
4045 This can be used to customize the list of actions based on the
4046 currently selected candidate. ")
4047 (anything-document-attribute 'pattern-transformer "optional"
4048 " It's a function or a list of functions called with one argument
4049 before computing matches. Its argument is `anything-pattern'.
4050 Functions should return transformed `anything-pattern'.
4052 It is useful to change interpretation of `anything-pattern'. ")
4053 (anything-document-attribute 'delayed "optional"
4054 " Candidates from the source are shown only if the user stops
4055 typing and is idle for `anything-idle-delay' seconds. ")
4056 (anything-document-attribute 'volatile "optional"
4057 " Indicates the source assembles the candidate list dynamically,
4058 so it shouldn't be cached within a single Anything
4059 invocation. It is only applicable to synchronous sources,
4060 because asynchronous sources are not cached. ")
4061 (anything-document-attribute 'requires-pattern "optional"
4062 " If present matches from the source are shown only if the
4063 pattern is not empty. Optionally, it can have an integer
4064 parameter specifying the required length of input which is
4065 useful in case of sources with lots of candidates. ")
4066 (anything-document-attribute 'persistent-action "optional"
4067 " Function called with one parameter; the selected candidate.
4069 An action performed by `anything-execute-persistent-action'.
4070 If none, use the default action. ")
4071 (anything-document-attribute 'candidates-in-buffer "optional"
4072 " Shortcut attribute for making and narrowing candidates using
4073 buffers. This newly-introduced attribute prevents us from
4074 forgetting to add volatile and match attributes.
4076 See docstring of `anything-candidates-in-buffer'.
4078 (candidates-in-buffer) is equivalent of three attributes:
4079 (candidates . anything-candidates-in-buffer)
4080 (volatile)
4081 (match identity)
4083 (candidates-in-buffer . candidates-function) is equivalent of:
4084 (candidates . candidates-function)
4085 (volatile)
4086 (match identity)
4088 This attribute is implemented by plug-in. ")
4089 (anything-document-attribute 'search "optional"
4090 " List of functions like `re-search-forward' or `search-forward'.
4091 Buffer search function used by `anything-candidates-in-buffer'.
4092 By default, `anything-candidates-in-buffer' uses `re-search-forward'.
4093 This attribute is meant to be used with
4094 (candidates . anything-candidates-in-buffer) or
4095 (candidates-in-buffer) in short. ")
4096 (anything-document-attribute 'search-from-end "optional"
4097 " Make `anything-candidates-in-buffer' search from the end of buffer.
4098 If this attribute is specified, `anything-candidates-in-buffer' uses
4099 `re-search-backward' instead. ")
4100 (anything-document-attribute 'get-line "optional"
4101 " A function like `buffer-substring-no-properties' or `buffer-substring'.
4102 This function converts point of line-beginning and point of line-end,
4103 which represents a candidate computed by `anything-candidates-in-buffer'.
4104 By default, `anything-candidates-in-buffer' uses
4105 `buffer-substring-no-properties'. ")
4106 (anything-document-attribute 'display-to-real "optional"
4107 " Function called with one parameter; the selected candidate.
4109 The function transforms the selected candidate, and the result
4110 is passed to the action function. The display-to-real
4111 attribute provides another way to pass other string than one
4112 shown in Anything buffer.
4114 Traditionally, it is possible to make candidates,
4115 candidate-transformer or filtered-candidate-transformer
4116 function return a list with (DISPLAY . REAL) pairs. But if REAL
4117 can be generated from DISPLAY, display-to-real is more
4118 convenient and faster. ")
4119 (anything-document-attribute 'real-to-display "optional"
4120 " Function called with one parameter; the selected candidate.
4122 The inverse of display-to-real attribute.
4124 The function transforms the selected candidate, which is passed
4125 to the action function, for display. The real-to-display
4126 attribute provides the other way to pass other string than one
4127 shown in Anything buffer.
4129 Traditionally, it is possible to make candidates,
4130 candidate-transformer or filtered-candidate-transformer
4131 function return a list with (DISPLAY . REAL) pairs. But if
4132 DISPLAY can be generated from REAL, real-to-display is more
4133 convenient.
4135 Note that DISPLAY parts returned from candidates /
4136 candidate-transformer are IGNORED as the name `display-to-real'
4137 says. ")
4138 (anything-document-attribute 'cleanup "optional"
4139 " Function called with no parameters when *anything* buffer is closed. It
4140 is useful for killing unneeded candidates buffer.
4142 Note that the function is executed BEFORE performing action. ")
4143 (anything-document-attribute 'candidate-number-limit "optional"
4144 " Override `anything-candidate-number-limit' only for this source. ")
4145 (anything-document-attribute 'accept-empty "optional"
4146 " Pass empty string \"\" to action function. ")
4147 (anything-document-attribute 'disable-shortcuts "optional"
4148 " Disable `anything-enable-shortcuts' in current `anything' session.
4150 This attribute is implemented by plug-in. ")
4151 (anything-document-attribute 'dummy "optional"
4152 " Set `anything-pattern' to candidate. If this attribute is
4153 specified, The candidates attribute is ignored.
4155 This attribute is implemented by plug-in.
4156 This plug-in implies disable-shortcuts plug-in. ")
4157 (anything-document-attribute 'multiline "optional"
4158 " Enable to selection multiline candidates. ")
4159 (anything-document-attribute 'update "optional"
4160 ;; FIXME: this is not displayed correctly in help buffer.
4161 " Function called with no parameters when \\<anything-map>\\[anything-force-update] is pressed. ")
4162 (anything-document-attribute 'mode-line "optional"
4163 " source local `anything-mode-line-string'. (included in `mode-line-format')
4164 It accepts also variable/function name. ")
4165 (anything-document-attribute 'header-line "optional"
4166 " source local `header-line-format'.
4167 It accepts also variable/function name. ")
4168 (anything-document-attribute
4169 'resume "optional"
4170 " Function called with no parameters when `anything-resume' is started.")
4173 ;; (@* "Bug Report")
4174 (defvar anything-maintainer-mail-address "emacs-anything@googlegroups.com")
4176 (defvar anything-bug-report-salutation
4177 "Describe bug below, using a precise recipe.
4179 When I executed M-x ...
4181 How to send a bug report:
4182 1) Be sure to use the LATEST version of anything.el.
4183 2) Enable debugger. M-x toggle-debug-on-error or (setq debug-on-error t)
4184 3) Use Lisp version instead of compiled one: (load \"anything.el\")
4185 4) If you got an error, please paste *Backtrace* buffer.
4186 5) Type C-c C-c to send.")
4188 (defvar anything-no-dump-variables
4189 '(anything-candidate-buffer-alist
4190 anything-digit-overlays
4191 anything-help-message
4192 anything-candidate-cache
4194 "Variables not to dump in bug report.")
4196 (defun anything-dumped-variables-in-bug-report ()
4197 (let ((hash (make-hash-table)))
4198 (loop for var in (apropos-internal "anything-" 'boundp)
4199 for vname = (symbol-name var)
4200 unless (or (string-match "-map$" vname)
4201 (string-match "^anything-c-source-" vname)
4202 (string-match "-hash$" vname)
4203 (string-match "-face$" vname)
4204 (memq var anything-no-dump-variables))
4205 collect var)))
4207 (defun anything-send-bug-report ()
4208 "Send a bug report of anything.el."
4209 (interactive)
4210 (with-current-buffer (or anything-last-buffer
4211 (current-buffer))
4212 (reporter-submit-bug-report
4213 anything-maintainer-mail-address
4214 "anything.el"
4215 (anything-dumped-variables-in-bug-report)
4216 nil nil
4217 anything-bug-report-salutation)))
4219 (defun anything-send-bug-report-from-anything ()
4220 "Send a bug report of anything.el in anything session."
4221 (interactive)
4222 (anything-run-after-quit 'anything-send-bug-report))
4224 ;; Debugging function.
4225 (defun* anything-test-candidates
4226 (sources &optional (input "")
4227 (compile-source-functions
4228 anything-compile-source-functions-default))
4229 "Test helper function for anything.
4230 Given pseudo `anything-sources' and `anything-pattern', returns list like
4231 ((\"source name1\" (\"candidate1\" \"candidate2\"))
4232 (\"source name2\" (\"candidate3\" \"candidate4\")))"
4233 (let ((anything-test-mode t)
4234 anything-enable-shortcuts
4235 anything-candidate-cache
4236 (anything-compile-source-functions compile-source-functions)
4237 anything-before-initialize-hook
4238 anything-after-initialize-hook
4239 anything-update-hook
4240 anything-test-candidate-list)
4241 (get-buffer-create anything-buffer)
4242 (anything-initialize nil input sources)
4243 (anything-update)
4244 ;; test-mode spec: select 1st candidate!
4245 (with-current-buffer anything-buffer
4246 (forward-line 1)
4247 (anything-mark-current-line))
4248 (prog1
4249 anything-test-candidate-list
4250 (anything-cleanup))))
4253 ;; (@* "Unit Tests")
4254 ;; See developer-tools/unit-test-anything.el
4256 (provide 'anything)
4258 ;; How to save (DO NOT REMOVE!!)
4259 ;; (progn (magit-push) (emacswiki-post "anything.el"))
4261 ;; Local Variables:
4262 ;; coding: utf-8
4263 ;; End:
4265 ;;; anything.el ends here