* anything-match-plugin.el (agp-candidates-synchronous-grep): return nil if the resul...
[anything-config.git] / anything.el
blobdcb073bbcb814ba6f97861866a66a471df3baa67
1 ;;; anything.el --- open anything / QuickSilver-like candidate-selection framework
3 ;; Copyright (C) 2007 Tamas Patrovics
4 ;; 2008 ~ 2012 rubikitch <rubikitch@ruby-lang.org>
5 ;; 2011 ~ 2012 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,
13 ;; processes, tools, convenience, anything
15 ;; X-URL: <http://repo.or.cz/w/anything-config.git>
17 ;; Site: <http://www.emacswiki.org/cgi-bin/emacs/Anything>
19 ;; MailingList: <https://groups.google.com/group/emacs-anything?hl=en>
22 ;;; This file is NOT part of GNU Emacs
24 ;;; License
26 ;; This file is free software; you can redistribute it and/or modify
27 ;; it under the terms of the GNU General Public License as published by
28 ;; the Free Software Foundation; either version 2, or (at your option)
29 ;; any later version.
31 ;; This file is distributed in the hope that it will be useful,
32 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
33 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 ;; GNU General Public License for more details.
37 ;; You should have received a copy of the GNU General Public License
38 ;; along with GNU Emacs; see the file COPYING. If not, write to the
39 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
40 ;; Boston, MA 02110-1301, USA.
43 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
45 ;;; Autodoc documentation:
46 ;; ---------------------
48 ;; * Commands defined here are:
49 ;; [EVAL] (autodoc-document-lisp-buffer :type 'command :prefix "anything" :docstring t)
50 ;; `anything-open-last-log'
51 ;; Open anything log file of last anything session.
52 ;; `anything'
53 ;; Main function to execute anything sources.
54 ;; `anything-resume'
55 ;; Resurrect previously invoked `anything'.
56 ;; `anything-resume-window-only'
57 ;;
58 ;; `anything-at-point'
59 ;; Call anything with symbol at point as initial input.
60 ;; `anything-force-update'
61 ;; Force recalculation and update of candidates.
62 ;; `anything-select-action'
63 ;; Select an action for the currently selected candidate.
64 ;; `anything-previous-line'
65 ;; Move selection to the previous line.
66 ;; `anything-next-line'
67 ;; Move selection to the next line.
68 ;; `anything-previous-page'
69 ;; Move selection back with a pageful.
70 ;; `anything-next-page'
71 ;; Move selection forward with a pageful.
72 ;; `anything-beginning-of-buffer'
73 ;; Move selection at the top.
74 ;; `anything-end-of-buffer'
75 ;; Move selection at the bottom.
76 ;; `anything-previous-source'
77 ;; Move selection to the previous source.
78 ;; `anything-next-source'
79 ;; Move selection to the next source.
80 ;; `anything-select-with-prefix-shortcut'
81 ;; Invoke default action with prefix shortcut.
82 ;; `anything-select-with-digit-shortcut'
83 ;; Invoke default action with digit/alphabet shortcut.
84 ;; `anything-confirm-and-exit-minibuffer'
85 ;; Maybe ask for confirmation when exiting anything.
86 ;; `anything-exit-minibuffer'
87 ;; Select the current candidate by exiting the minibuffer.
88 ;; `anything-keyboard-quit'
89 ;; Quit minibuffer in anything.
90 ;; `anything-help'
91 ;; Help of `anything'.
92 ;; `anything-debug-output'
93 ;; Show all anything-related variables at this time.
94 ;; `anything-delete-current-selection'
95 ;; Delete the currently selected item.
96 ;; `anything-delete-minibuffer-contents'
97 ;; Same as `delete-minibuffer-contents' but this is a command.
98 ;; `anything-toggle-resplit-window'
99 ;; Toggle resplit anything window, vertically or horizontally.
100 ;; `anything-narrow-window'
101 ;; Narrow anything window.
102 ;; `anything-enlarge-window'
103 ;; Enlarge anything window.
104 ;; `anything-select-2nd-action'
105 ;; Select the 2nd action for the currently selected candidate.
106 ;; `anything-select-3rd-action'
107 ;; Select the 3rd action for the currently selected candidate.
108 ;; `anything-select-4th-action'
109 ;; Select the 4th action for the currently selected candidate.
110 ;; `anything-select-2nd-action-or-end-of-line'
111 ;; Select the 2nd action for the currently selected candidate.
112 ;; `anything-execute-persistent-action'
113 ;; Perform the associated action ATTR without quitting anything.
114 ;; `anything-scroll-other-window'
115 ;; Scroll other window (not *Anything* window) upward.
116 ;; `anything-scroll-other-window-down'
117 ;; Scroll other window (not *Anything* window) downward.
118 ;; `anything-toggle-visible-mark'
119 ;; Toggle anything visible mark at point.
120 ;; `anything-display-all-visible-marks'
121 ;; Show all `anything' visible marks strings.
122 ;; `anything-next-visible-mark'
123 ;; Move next anything visible mark.
124 ;; `anything-prev-visible-mark'
125 ;; Move previous anything visible mark.
126 ;; `anything-yank-selection'
127 ;; Set minibuffer contents to current selection.
128 ;; `anything-kill-selection-and-quit'
129 ;; Store current selection to kill ring.
130 ;; `anything-follow-mode'
131 ;; If this mode is on, persistent action is executed everytime the cursor is moved.
132 ;; `anything-migrate-sources'
133 ;; Help to migrate to new `anything' way.
134 ;; `anything-describe-anything-attribute'
135 ;; Display the full documentation of ANYTHING-ATTRIBUTE.
136 ;; `anything-send-bug-report'
137 ;; Send a bug report of anything.el.
138 ;; `anything-send-bug-report-from-anything'
139 ;; Send a bug report of anything.el in anything session.
141 ;; * Variables defined here are:
142 ;; [EVAL] (autodoc-document-lisp-buffer :type 'internal-variable :prefix "anything-" :docstring t)
143 ;; `anything-version'
144 ;; Not documented.
145 ;; `anything-sources'
146 ;; A list of sources to use with `anything'.
147 ;; `anything-type-attributes'
148 ;; It's a list of (TYPE ATTRIBUTES ...).
149 ;; `anything-enable-shortcuts'
150 ;; *Whether to use digit/alphabet shortcut to select the first nine matches.
151 ;; `anything-shortcut-keys-alist'
152 ;; Not documented.
153 ;; `anything-display-source-at-screen-top'
154 ;; *Display candidates at the top of screen.
155 ;; `anything-candidate-number-limit'
156 ;; Apply candidate-number-limit attribute value.
157 ;; `anything-idle-delay'
158 ;; *Be idle for this many seconds, before updating in delayed sources.
159 ;; `anything-input-idle-delay'
160 ;; Be idle for this many seconds, before updating.
161 ;; `anything-samewindow'
162 ;; Use current window to show the candidates.
163 ;; `anything-source-filter'
164 ;; A list of source names to be displayed.
165 ;; `anything-map'
166 ;; Keymap for anything.
167 ;; `anything-header-face'
168 ;; *Face for header lines in the anything buffer.
169 ;; `anything-selection-face'
170 ;; *Face for currently selected item.
171 ;; `anything-buffer'
172 ;; Buffer showing completions.
173 ;; `anything-action-buffer'
174 ;; Buffer showing actions.
175 ;; `anything-selection-overlay'
176 ;; Overlay used to highlight the currently selected item.
177 ;; `anything-digit-overlays'
178 ;; Overlays for digit shortcuts. See `anything-enable-shortcuts'.
179 ;; `anything-candidate-cache'
180 ;; Holds the available candidate withing a single anything invocation.
181 ;; `anything-pattern'
182 ;; Not documented.
183 ;; `anything-input'
184 ;; Not documented.
185 ;; `anything-async-processes'
186 ;; List of information about asynchronous processes managed by anything.
187 ;; `anything-digit-shortcut-count'
188 ;; Number of digit shortcuts shown in the anything buffer.
189 ;; `anything-before-initialize-hook'
190 ;; Run before anything initialization.
191 ;; `anything-after-initialize-hook'
192 ;; Run after anything initialization.
193 ;; `anything-update-hook'
194 ;; Run after the anything buffer was updated according the new input pattern.
195 ;; `anything-after-update-hook'
196 ;; Run after the anything buffer was updated according the new input pattern.
197 ;; `anything-cleanup-hook'
198 ;; Run after anything minibuffer is closed.
199 ;; `anything-select-action-hook'
200 ;; Run when opening the action buffer.
201 ;; `anything-before-action-hook'
202 ;; Run before executing action.
203 ;; `anything-after-action-hook'
204 ;; Run after executing action.
205 ;; `anything-after-persistent-action-hook'
206 ;; Run after executing persistent action.
207 ;; `anything-move-selection-before-hook'
208 ;; Run before moving selection in `anything-buffer'.
209 ;; `anything-move-selection-after-hook'
210 ;; Run after moving selection in `anything-buffer'.
211 ;; `anything-restored-variables'
212 ;; Variables which are restored after `anything' invocation.
213 ;; `anything-saved-selection'
214 ;; Value of the currently selected object when the action list is shown.
215 ;; `anything-current-prefix-arg'
216 ;; Record `current-prefix-arg' when exiting minibuffer.
217 ;; `anything-candidate-separator'
218 ;; Candidates separator of `multiline' source.
219 ;; `anything-current-buffer'
220 ;; Current buffer when `anything' is invoked.
221 ;; `anything-buffer-file-name'
222 ;; Variable `buffer-file-name' when `anything' is invoked.
223 ;; `anything-saved-action'
224 ;; Saved value of the currently selected action by key.
225 ;; `anything-last-sources'
226 ;; OBSOLETE!! Sources of previously invoked `anything'.
227 ;; `anything-saved-current-source'
228 ;; Value of the current source when the action list is shown.
229 ;; `anything-compiled-sources'
230 ;; Compiled version of `anything-sources'.
231 ;; `anything-in-persistent-action'
232 ;; Flag whether in persistent-action or not.
233 ;; `anything-quick-update'
234 ;; If non-nil, suppress displaying sources which are out of screen at first.
235 ;; `anything-last-sources-local'
236 ;; Buffer local value of `anything-sources'.
237 ;; `anything-last-buffer'
238 ;; `anything-buffer' of previously `anything' session.
239 ;; `anything-save-configuration-functions'
240 ;; The functions used to restore/save window or frame configurations.
241 ;; `anything-persistent-action-use-special-display'
242 ;; If non-nil, use `special-display-function' in persistent action.
243 ;; `anything-execute-action-at-once-if-one'
244 ;; Execute default action and exit when only one candidate is remaining.
245 ;; `anything-quit-if-no-candidate'
246 ;; Quit when there is no candidates when non--nil.
247 ;; `anything-scroll-amount'
248 ;; Scroll amount when scrolling other window in an anything session.
249 ;; `anything-display-function'
250 ;; Function to display *anything* buffer.
251 ;; `anything-delayed-init-executed'
252 ;; Not documented.
253 ;; `anything-mode-line-string'
254 ;; Help string displayed in mode-line in `anything'.
255 ;; `anything-help-message'
256 ;; Detailed help message string for `anything'.
257 ;; `anything-source-in-each-line-flag'
258 ;; Non-nil means add anything-source text-property in each candidate.
259 ;; `anything-debug-forms'
260 ;; Forms to show in `anything-debug-output'.
261 ;; `anything-debug'
262 ;; If non-nil, write log message into *Anything Log* buffer.
263 ;; `anything-test-candidate-list'
264 ;; Not documented.
265 ;; `anything-test-mode'
266 ;; Not documented.
267 ;; `anything-source-name'
268 ;; Not documented.
269 ;; `anything-candidate-buffer-alist'
270 ;; Not documented.
271 ;; `anything-check-minibuffer-input-timer'
272 ;; Not documented.
273 ;; `anything-match-hash'
274 ;; Not documented.
275 ;; `anything-cib-hash'
276 ;; Not documented.
277 ;; `anything-tick-hash'
278 ;; Not documented.
279 ;; `anything-issued-errors'
280 ;; Not documented.
281 ;; `anything-shortcut-keys'
282 ;; Not documented.
283 ;; `anything-once-called-functions'
284 ;; Not documented.
285 ;; `anything-follow-mode'
286 ;; If this mode is on, persistent action is executed everytime the cursor is moved.
287 ;; `anything-let-variables'
288 ;; Not documented.
289 ;; `anything-split-window-state'
290 ;; Not documented.
291 ;; `anything-selection-point'
292 ;; Not documented.
293 ;; `anything-last-log-file'
294 ;; Not documented.
295 ;; `anything-compile-source-functions'
296 ;; Functions to compile elements of `anything-sources' (plug-in).
297 ;; `anything-quit'
298 ;; Not documented.
299 ;; `anything-additional-attributes'
300 ;; List of all `anything' attributes.
301 ;; `anything-buffers'
302 ;; All of `anything-buffer' in most recently used order.
303 ;; `anything-current-position'
304 ;; Restore or save current position in `anything-current-buffer'.
305 ;; `anything-last-frame-or-window-configuration'
306 ;; Used to store window or frame configuration when anything start.
307 ;; `anything-reading-pattern'
308 ;; Whether in `read-string' in anything or not.
309 ;; `anything-compile-source-functions-default'
310 ;; Plug-ins this file provides.
311 ;; `anything-input-local'
312 ;; Not documented.
313 ;; `anything-process-delayed-sources-timer'
314 ;; Not documented.
315 ;; `anything-mode-line-string-real'
316 ;; Not documented.
317 ;; `anything-exit-status'
318 ;; Flag to inform whether anything have exited or quitted.
319 ;; `anything-minibuffer-confirm-state'
320 ;; Not documented.
321 ;; `anything-types'
322 ;; Not documented.
323 ;; `anything-orig-enable-shortcuts'
324 ;; Not documented.
325 ;; `anything-persistent-action-display-window'
326 ;; Return the window that will be used for presistent action.
327 ;; `anything-visible-mark-face'
328 ;; Not documented.
329 ;; `anything-visible-mark-overlays'
330 ;; Not documented.
331 ;; `anything-marked-candidates'
332 ;; Return marked candidates of current source if any.
333 ;; `anything-maintainer-mail-address'
334 ;; Not documented.
335 ;; `anything-bug-report-salutation'
336 ;; Not documented.
337 ;; `anything-no-dump-variables'
338 ;; Variables not to dump in bug report.
340 ;; *** END auto-documentation
342 ;; [EVAL] (autodoc-update-all)
345 ;;; Commentary:
348 ;; Start with M-x anything, narrow the list by typing some pattern,
349 ;; select with up/down/pgup/pgdown/C-p/C-n/C-v/M-v, choose with enter,
350 ;; left/right moves between sources. With TAB actions can be selected
351 ;; if the selected candidate has more than one possible action.
353 ;; Note that anything.el provides only the framework and some example
354 ;; configurations for demonstration purposes. See anything-config.el
355 ;; for practical, polished, easy to use configurations which can be
356 ;; used to assemble a custom personalized configuration.
358 ;; NOTE: What you find on Emacswiki is mostly deprecated and not maintained,
359 ;; don't complain if you use such code or configuration and something
360 ;; doesn't work.
362 ;; Here is Japanese translation of `anything-sources' attributes. Thanks.
363 ;; http://d.hatena.ne.jp/sirocco634/20091012/1255336649
366 ;;; Bug Report:
368 ;; If you have problems, send a bug report via C-c C-x C-b in anything session (best)
369 ;; or M-x anything-send-bug-report outside anything session.
370 ;; I implemented bug report feature because I want to know your current state.
371 ;; It helps me to solve problems easily.
372 ;; The step is:
373 ;; 0) Setup mail in Emacs, the easiest way is:
374 ;; (setq user-mail-address "your@mail.address")
375 ;; (setq user-full-name "Your Full Name")
376 ;; (setq smtpmail-smtp-server "your.smtp.server.jp")
377 ;; (setq mail-user-agent 'message-user-agent)
378 ;; (setq message-send-mail-function 'message-smtpmail-send-it)
379 ;; 1) Be sure to use the LATEST version of anything.el.
380 ;; 2) Enable debugger. M-x toggle-debug-on-error or (setq debug-on-error t)
381 ;; 3) Use Lisp version instead of compiled one: (load "anything.el")
382 ;; 4) Do it!
383 ;; 5) If you got an error, please do not close *Backtrace* buffer.
384 ;; 6) Type C-c C-x C-b (anything session, best!)
385 ;; or M-x anything-send-bug-report (outside)
386 ;; then M-x insert-buffer *Backtrace* (if you got error)
387 ;; 7) Describe the bug using a precise recipe.
388 ;; 8) Type C-c C-c to send.
390 ;; You can also just report bug to:
391 ;; https://groups.google.com/group/emacs-anything?hl=en
394 ;; You can extend `anything' by writing plug-ins. As soon as
395 ;; `anything' is invoked, `anything-sources' is compiled into basic
396 ;; attributes, then compiled one is used during invocation.
398 ;; The oldest built-in plug-in is `type' attribute: appends
399 ;; appropriate element of `anything-type-attributes'. Second built-in
400 ;; plug-in is `candidates-in-buffer': selecting a line from candidates
401 ;; buffer.
403 ;; To write a plug-in:
404 ;; 1. Define a compiler: anything-compile-source--*
405 ;; 2. Add compier function to `anything-compile-source-functions'.
406 ;; 3. (optional) Write helper functions.
408 ;; Tested on Emacs 22/23/24.
411 ;; Thanks to Vagn Johansen for ideas.
412 ;; Thanks to Stefan Kamphausen for fixes and XEmacs support.
413 ;; Thanks to Tassilo Horn for fixes.
414 ;; Thanks to Drew Adams for various fixes
415 ;; Thanks to IMAKADO for candidates-in-buffer idea.
416 ;; Thanks to Tomohiro MATSUYAMA for multiline patch.
419 ;;; (@* "Index")
421 ;; If you have library `linkd.el', load
422 ;; `linkd.el' and turn on `linkd-mode' now. It lets you easily
423 ;; navigate around the sections Linkd mode will
424 ;; highlight this Index. You can get `linkd.el' here:
425 ;; http://www.emacswiki.org/cgi-bin/wiki/download/linkd.el
429 ;;; (@* "Tips")
432 ;; `anything' accepts keyword arguments. See docstring.
433 ;; [EVAL IT] (describe-function 'anything)
436 ;; `anything-enable-shortcuts' enables us to select candidate easily.
437 ;; If 'prefix then they can be selected using <prefix-key> <alnum>.
438 ;; The prefix key is `anything-select-with-prefix-shortcut'.
439 ;; If the <prefix-key> is a letter, pressing twice inputs the letter itself.
440 ;; e.g.
441 ;; (setq anything-enable-shortcuts 'prefix)
442 ;; (define-key anything-map \"@\" 'anything-select-with-prefix-shortcut)
445 ;; You can edit current selection using `anything-edit-current-selection'.
446 ;; It is useful after persistent-action.
449 ;; For `anything' users, setting `anything-sources' directly and
450 ;; invoke M-x anything is obsolete way for now. Try M-x
451 ;; `anything-migrate-sources'!
454 ;; If you want to create anything sources, yasnippet would help you.
455 ;; http://yasnippet.googlecode.com/
457 ;; Then get the snippet from
458 ;; http://www.emacswiki.org/cgi-bin/wiki/download/anything-source.yasnippet
460 ;; Put it in ~/.emacs.d/plugins/yasnippet/snippets/text-mode/emacs-lisp-mode/
464 ;; `anything-interpret-value' is useful function to interpret value
465 ;; like `candidates' attribute.
467 ;; (anything-interpret-value "literal") ; => "literal"
468 ;; (anything-interpret-value (lambda () "lambda")) ; => "lambda"
469 ;; (let ((source '((name . "lambda with source name"))))
470 ;; (anything-interpret-value
471 ;; (lambda () anything-source-name)
472 ;; source)) ; => "lambda with source name"
473 ;; (flet ((f () "function symbol"))
474 ;; (anything-interpret-value 'f)) ; => "function symbol"
475 ;; (let ((v "variable symbol"))
476 ;; (anything-interpret-value 'v)) ; => "variable symbol"
477 ;; (anything-interpret-value 'unbounded-1) ; error
480 ;; Now symbols are acceptable as candidates. So you do not have to use
481 ;; `symbol-name' function. The source is much simpler. For example,
482 ;; `apropos-internal' returns a list of symbols.
484 ;; (anything
485 ;; '(((name . "Commands")
486 ;; (candidates . (lambda () (apropos-internal anything-pattern 'commandp)))
487 ;; (volatile)
488 ;; (action . describe-function))))
491 ;; To mark a candidate, press C-SPC as normal Emacs marking. To go to
492 ;; marked candidate, press M-[ or M-].
495 ;; `anything-map' is now Emacs-standard key bindings by default.
497 ;; There are many `anything' applications, using `anything' for
498 ;; selecting candidate. In this case, if there is one candidate or no
499 ;; candidate, popping up *anything* buffer is irritating. If one
500 ;; candidate, you want to select it at once. If no candidate, you want
501 ;; to quit `anything'. Set `anything-execute-action-at-once-if-one'
502 ;; and `anything-quit-if-no-candidate' to non-nil to remedy it. Note
503 ;; that setting these variables GLOBALLY is bad idea because of
504 ;; delayed sources. These are meant to be let-binded.
506 ;; ex.
507 ;; (let ((anything-execute-action-at-once-if-one t)
508 ;; (anything-quit-if-no-candidate (lambda () (message "No candidate"))))
509 ;; (anything temporary-sources input))
512 ;; `set-frame-configuration' arises flickering. If you hate
513 ;; flickering, eval:
514 ;; (setq anything-save-configuration-functions
515 ;; '(set-window-configuration . current-window-configuration))
516 ;; at the cost of restoring frame configuration (only window configuration).
519 ;; `anything-delete-current-selection' deletes the current line.
520 ;; It is useful when deleting a candidate in persistent action.
521 ;; eg. `kill-buffer'.
523 ;; [EVAL IT] (describe-function 'anything-delete-current-selection)
526 ;; `anything-attr' gets the attribute. `anything-attrset' sets the
527 ;; attribute. `anything-attr-defined' tests whether the attribute is
528 ;; defined. They handles source-local variables.
530 ;; [EVAL IT] (describe-function 'anything-attr)
531 ;; [EVAL IT] (describe-function 'anything-attrset)
532 ;; [EVAL IT] (describe-function 'anything-attr-defined)
535 ;; `anything-sources' accepts many attributes to make your life easier.
536 ;; Now `anything-sources' accepts a list of symbols.
538 ;; [EVAL IT] (describe-variable 'anything-sources)
541 ;; `anything' has optional arguments. Now you do not have to let-bind
542 ;; `anything-sources'.
544 ;; [EVAL IT] (describe-function 'anything)
547 ;; `anything-resume' resumes last `anything' session. Now you do not
548 ;; have to retype pattern.
550 ;; [EVAL IT] (describe-function 'anything-resume)
553 ;; `anything-execute-persistent-action' executes action without
554 ;; quitting `anything'. When popping up a buffer in other window by
555 ;; persistent action, you can scroll with `anything-scroll-other-window' and
556 ;; `anything-scroll-other-window-down'. See also `anything-sources' docstring.
558 ;; [EVAL IT] (describe-function 'anything-execute-persistent-action)
559 ;; [EVAL IT] (describe-variable 'anything-sources)
562 ;; `anything-select-2nd-action', `anything-select-3rd-action' and
563 ;; `anything-select-4th-action' select other than default action
564 ;; without pressing Tab.
567 ;; Using `anything-candidate-buffer' and the candidates-in-buffer
568 ;; attribute is much faster than traditional "candidates and match"
569 ;; way. And `anything-current-buffer-is-modified' avoids to
570 ;; recalculate candidates for unmodified buffer. See docstring of
571 ;; them.
573 ;; [EVAL IT] (describe-function 'anything-candidate-buffer)
574 ;; [EVAL IT] (describe-function 'anything-candidates-in-buffer)
575 ;; [EVAL IT] (describe-function 'anything-current-buffer-is-modified)
578 ;; `anything-current-buffer' and `anything-buffer-file-name' stores
579 ;; `(current-buffer)' and `buffer-file-name' in the buffer `anything'
580 ;; is invoked. Use them freely.
582 ;; [EVAL IT] (describe-variable 'anything-current-buffer)
583 ;; [EVAL IT] (describe-variable 'anything-buffer-file-name)
586 ;; `anything-completing-read' and `anything-read-file-name' are
587 ;; experimental implementation. If you are curious, type M-x
588 ;; anything-read-string-mode. It is a minor mode and toggles on/off.
591 ;; Use `anything-test-candidates' to test your handmade anything
592 ;; sources. It simulates contents of *anything* buffer with pseudo
593 ;; `anything-sources' and `anything-pattern', without side-effect. So
594 ;; you can unit-test your anything sources! Let's TDD!
596 ;; [EVAL IT] (describe-function 'anything-test-candidates)
598 ;; For anything developpers:
600 ;; There are many unit-testing framework in Emacs Lisp. See the EmacsWiki.
601 ;; http://www.emacswiki.org/cgi-bin/emacs/UnitTesting
602 ;; There is an unit-test by Emacs Lisp Expectations in developper-tools directory.
603 ;; http://www.emacswiki.org/cgi-bin/wiki/download/el-expectations.el
604 ;; http://www.emacswiki.org/cgi-bin/wiki/download/el-mock.el
607 ;; If you want to create anything sources, see anything-config.el.
608 ;; It is huge collection of sources. You can learn from examples.
611 ;; (@* "TODO")
613 ;; - process status indication
615 ;; - async sources doesn't honor digit-shortcut-count
617 ;; - anything-candidate-number-limit can't be nil everywhere
619 ;; (@* "HISTORY")
621 ;; Change log of this file is found at
622 ;; http://repo.or.cz/w/anything-config.git/history/master:/anything.el
624 ;; Change log of this project is found at
625 ;; http://repo.or.cz/w/anything-config.git?a=shortlog
627 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
630 ;;; Code:
632 (require 'cl)
634 (defvar anything-version "1.3.9")
636 ;; (@* "User Configuration")
639 ;;; Variables
642 ;; [DEPRECATED]
643 ;; A default value is provided in anything-config.el
644 (defvar anything-sources nil
645 "A list of sources to use with `anything'.
646 It is deprecated, you should not use this.
647 Use instead individual sources or list of sources of your choice.")
649 ;; Default values are provided in anything-config.el.
650 (defvar anything-type-attributes nil
651 "It's a list of \(TYPE ATTRIBUTES ...\).
652 ATTRIBUTES are the same as attributes for `anything-sources'.
653 TYPE connects the value to the appropriate sources.
654 Don't set this directly, use instead `define-anything-type-attribute'.
656 This allows specifying common attributes for several sources.
657 For example, sources which provide files can specify
658 common attributes with a `file' type.")
660 (defvaralias 'anything-enable-digit-shortcuts 'anything-enable-shortcuts
661 "Same as `anything-enable-shortcuts'.
662 Alphabet shortcuts are available now in `anything-enable-shortcuts'.
663 `anything-enable-digit-shortcuts' is retained for compatibility.")
665 (defvar anything-enable-shortcuts nil
666 "*Whether to use digit/alphabet shortcut to select the first nine matches.
667 If t then they can be selected using Ctrl+<number>.
669 If 'prefix then they can be selected using <prefix-key> <alnum>.
670 The prefix key is `anything-select-with-prefix-shortcut'.
671 If the <prefix-key> is a letter, pressing twice inputs the letter itself.
672 e.g.
673 (setq anything-enable-shortcuts 'prefix)
674 (define-key anything-map \"@\" 'anything-select-with-prefix-shortcut)
676 If 'alphabet then they can be selected using Shift+<alphabet> (deprecated).
677 It is not recommended because you cannot input capital letters in pattern.
679 Keys (digit/alphabet) are listed in `anything-shortcut-keys-alist'.")
681 (defvar anything-shortcut-keys-alist
682 '((alphabet . "asdfghjklzxcvbnmqwertyuiop")
683 (prefix . "asdfghjklzxcvbnmqwertyuiop1234567890")
684 (t . "123456789")))
686 (defvar anything-display-source-at-screen-top t
687 "*Display candidates at the top of screen.
688 This happen when using `anything-next-source' and `anything-previous-source'.")
690 (defvar anything-candidate-number-limit 50
691 "*Limit candidate number globally.
692 Do not show more candidates than this limit from individual sources.
693 It is usually pointless to show hundreds of matches
694 when the pattern is empty, because it is much simpler to type a
695 few characters to narrow down the list of potential candidates.
697 Set it to nil if you don't want this limit.")
699 (defvar anything-idle-delay 0.3
700 "*Be idle for this many seconds, before updating in delayed sources.
701 This is useful for sources involving heavy operations
702 \(like launching external programs\), so that candidates
703 from the source are not retrieved unnecessarily if the user keeps typing.
705 It also can be used to declutter the results anything displays,
706 so that results from certain sources are not shown with every
707 character typed, only if the user hesitates a bit.")
709 (defvar anything-input-idle-delay 0.3
710 "Be idle for this many seconds, before updating.
712 Unlike `anything-idle-delay', it is also effective for non-delayed sources.
713 If nil, candidates are collected immediately.
715 Note: If this value is too low compared to `anything-idle-delay',
716 you may have duplicated sources when using multiples sources.
717 Safe value is always >= `anything-idle-delay'.
718 Default settings are equal value for both.")
720 (defvar anything-samewindow nil
721 "Use current window to show the candidates.
722 If t then Anything doesn't pop up a new window.")
724 (defvar anything-source-filter nil
725 "A list of source names to be displayed.
726 Other sources won't appear in the search results.
727 If nil then there is no filtering.
728 See also `anything-set-source-filter'.")
731 (defvar anything-map
732 (let ((map (copy-keymap minibuffer-local-map)))
733 (define-key map (kbd "<down>") 'anything-next-line)
734 (define-key map (kbd "<up>") 'anything-previous-line)
735 (define-key map (kbd "C-n") 'anything-next-line)
736 (define-key map (kbd "C-p") 'anything-previous-line)
737 (define-key map (kbd "<prior>") 'anything-previous-page)
738 (define-key map (kbd "<next>") 'anything-next-page)
739 (define-key map (kbd "M-v") 'anything-previous-page)
740 (define-key map (kbd "C-v") 'anything-next-page)
741 (define-key map (kbd "M-<") 'anything-beginning-of-buffer)
742 (define-key map (kbd "M->") 'anything-end-of-buffer)
743 (define-key map (kbd "C-g") 'anything-keyboard-quit)
744 (define-key map (kbd "<right>") 'anything-next-source)
745 (define-key map (kbd "<left>") 'anything-previous-source)
746 (define-key map (kbd "<RET>") 'anything-exit-minibuffer)
747 (define-key map (kbd "C-1") 'anything-select-with-digit-shortcut)
748 (define-key map (kbd "C-2") 'anything-select-with-digit-shortcut)
749 (define-key map (kbd "C-3") 'anything-select-with-digit-shortcut)
750 (define-key map (kbd "C-4") 'anything-select-with-digit-shortcut)
751 (define-key map (kbd "C-5") 'anything-select-with-digit-shortcut)
752 (define-key map (kbd "C-6") 'anything-select-with-digit-shortcut)
753 (define-key map (kbd "C-7") 'anything-select-with-digit-shortcut)
754 (define-key map (kbd "C-8") 'anything-select-with-digit-shortcut)
755 (define-key map (kbd "C-9") 'anything-select-with-digit-shortcut)
756 (loop for c from ?A to ?Z do
757 (define-key map (make-string 1 c) 'anything-select-with-digit-shortcut))
758 (define-key map (kbd "C-i") 'anything-select-action)
759 (define-key map (kbd "C-z") 'anything-execute-persistent-action)
760 (define-key map (kbd "C-e") 'anything-select-2nd-action-or-end-of-line)
761 (define-key map (kbd "C-j") 'anything-select-3rd-action)
762 (define-key map (kbd "C-o") 'anything-next-source)
763 (define-key map (kbd "C-M-v") 'anything-scroll-other-window)
764 (define-key map (kbd "M-<next>") 'anything-scroll-other-window)
765 (define-key map (kbd "C-M-y") 'anything-scroll-other-window-down)
766 (define-key map (kbd "C-M-S-v") 'anything-scroll-other-window-down)
767 (define-key map (kbd "M-<prior>") 'anything-scroll-other-window-down)
768 (define-key map (kbd "<C-M-down>") 'anything-scroll-other-window)
769 (define-key map (kbd "<C-M-up>") 'anything-scroll-other-window-down)
770 (define-key map (kbd "C-SPC") 'anything-toggle-visible-mark)
771 (define-key map (kbd "M-SPC") 'anything-toggle-visible-mark)
772 (define-key map (kbd "M-[") 'anything-prev-visible-mark)
773 (define-key map (kbd "M-]") 'anything-next-visible-mark)
774 (define-key map (kbd "C-k") 'anything-delete-minibuffer-contents)
776 (define-key map (kbd "C-r") 'undefined)
777 (define-key map (kbd "C-t") 'anything-toggle-resplit-window)
778 (define-key map (kbd "C-}") 'anything-narrow-window)
779 (define-key map (kbd "C-{") 'anything-enlarge-window)
781 (define-key map (kbd "C-c C-d") 'anything-delete-current-selection)
782 (define-key map (kbd "C-c C-y") 'anything-yank-selection)
783 (define-key map (kbd "C-c C-k") 'anything-kill-selection-and-quit)
784 (define-key map (kbd "C-c C-f") 'anything-follow-mode)
785 (define-key map (kbd "C-c C-u") 'anything-force-update)
786 (define-key map (kbd "M-p") 'previous-history-element)
787 (define-key map (kbd "M-n") 'next-history-element)
788 ;; Debugging command
789 (define-key map "\C-c\C-x\C-d" 'anything-debug-output)
790 (define-key map "\C-c\C-x\C-m" 'anything-display-all-visible-marks)
791 (define-key map "\C-c\C-x\C-b" 'anything-send-bug-report-from-anything)
792 ;; Use `describe-mode' key in `global-map'.
793 (define-key map [f1] nil) ; Allow to eval keymap whithout errors.
794 (dolist (k (where-is-internal 'describe-mode global-map))
795 (define-key map k 'anything-help))
796 map)
797 "Keymap for anything.")
800 (defgroup anything nil
801 "Open anything."
802 :prefix "anything-" :group 'convenience)
804 (defface anything-header
805 '((t (:inherit header-line)))
806 "Face for header lines in the anything buffer."
807 :group 'anything)
809 (defvar anything-header-face 'anything-header
810 "*Face for header lines in the anything buffer.")
812 (defface anything-candidate-number
813 '((t (:background "Yellow" :foreground "black")))
814 "Face for candidate number in mode-line." :group 'anything)
816 (defvar anything-selection-face 'highlight
817 "*Face for currently selected item.")
819 (defvar anything-buffer "*anything*"
820 "Buffer showing completions.")
822 (defvar anything-action-buffer "*anything action*"
823 "Buffer showing actions.")
825 (defvar anything-selection-overlay nil
826 "Overlay used to highlight the currently selected item.")
828 (defvar anything-digit-overlays nil
829 "Overlays for digit shortcuts. See `anything-enable-shortcuts'.")
831 (defvar anything-candidate-cache nil
832 "Holds the available candidate withing a single anything invocation.")
834 (defvar anything-pattern
835 "The input pattern used to update the anything buffer.")
837 (defvar anything-input
838 "The input typed in the candidates panel.")
840 (defvar anything-async-processes nil
841 "List of information about asynchronous processes managed by anything.")
843 (defvar anything-digit-shortcut-count 0
844 "Number of digit shortcuts shown in the anything buffer.")
846 (defvar anything-before-initialize-hook nil
847 "Run before anything initialization.
848 This hook is run before init functions in `anything-sources'.")
850 (defvar anything-after-initialize-hook nil
851 "Run after anything initialization.
852 Global variables are initialized and the anything buffer is created.
853 But the anything buffer has no contents.")
855 (defvar anything-update-hook nil
856 "Run after the anything buffer was updated according the new input pattern.
857 This hook is run at the beginning of buffer.
858 The first candidate is selected after running this hook.
859 See also `anything-after-update-hook'.")
861 (defvar anything-after-update-hook nil
862 "Run after the anything buffer was updated according the new input pattern.
863 This is very similar to `anything-update-hook' but selection is not moved.
864 It is useful to select a particular object instead of the first one.")
866 (defvar anything-cleanup-hook nil
867 "Run after anything minibuffer is closed.
868 IOW this hook is executed BEFORE performing action.")
870 (defvar anything-select-action-hook nil
871 "Run when opening the action buffer.")
873 (defvar anything-before-action-hook nil
874 "Run before executing action.
875 Contrarily to `anything-cleanup-hook',
876 this hook run before anything minibuffer is closed
877 and before performing action.")
879 (defvar anything-after-action-hook nil
880 "Run after executing action.")
882 (defvar anything-after-persistent-action-hook nil
883 "Run after executing persistent action.")
885 (defvar anything-move-selection-before-hook nil
886 "Run before moving selection in `anything-buffer'.")
888 (defvar anything-move-selection-after-hook nil
889 "Run after moving selection in `anything-buffer'.")
891 (defvar anything-restored-variables
892 '(anything-candidate-number-limit
893 anything-source-filter
894 anything-source-in-each-line-flag
895 anything-map
896 anything-sources)
897 "Variables which are restored after `anything' invocation.")
899 (defvar anything-saved-selection nil
900 "Value of the currently selected object when the action list is shown.")
902 (defvar anything-current-prefix-arg nil
903 "Record `current-prefix-arg' when exiting minibuffer.")
905 (defvar anything-candidate-separator
906 "--------------------"
907 "Candidates separator of `multiline' source.")
909 (defvar anything-current-buffer nil
910 "Current buffer when `anything' is invoked.")
912 (defvar anything-buffer-file-name nil
913 "Variable `buffer-file-name' when `anything' is invoked.")
915 (defvar anything-saved-action nil
916 "Saved value of the currently selected action by key.")
918 (defvar anything-last-sources nil
919 "OBSOLETE!! Sources of previously invoked `anything'.")
921 (defvar anything-saved-current-source nil
922 "Value of the current source when the action list is shown.")
924 (defvar anything-compiled-sources nil
925 "Compiled version of `anything-sources'.")
927 (defvar anything-in-persistent-action nil
928 "Flag whether in persistent-action or not.")
930 (defvar anything-quick-update nil
931 "If non-nil, suppress displaying sources which are out of screen at first.
932 They are treated as delayed sources at this input.
933 This flag makes `anything' a bit faster with many sources.")
935 (defvar anything-last-sources-local nil
936 "Buffer local value of `anything-sources'.")
938 (defvar anything-last-buffer nil
939 "`anything-buffer' of previously `anything' session.")
941 (defvar anything-save-configuration-functions
942 '(set-window-configuration . current-window-configuration)
943 "The functions used to restore/save window or frame configurations.
944 It is a pair where the car is the function to restore window or frame config,
945 and the cdr is the function to save the window or frame config.
947 If you want to save and restore frame configuration, set this variable to
948 '\(set-frame-configuration . current-frame-configuration\)
950 Older version saves/restores frame configuration, but the default is changed now
951 because flickering can occur in some environment. ")
953 (defvar anything-persistent-action-use-special-display nil
954 "If non-nil, use `special-display-function' in persistent action.")
956 (defvar anything-execute-action-at-once-if-one nil
957 "Execute default action and exit when only one candidate is remaining.
958 It is useful for `anything' applications.")
960 (defvar anything-quit-if-no-candidate nil
961 "Quit when there is no candidates when non--nil.
962 This variable accepts a function, which is executed if no candidate.
964 It is useful for `anything' applications.")
966 (defvar anything-scroll-amount nil
967 "Scroll amount when scrolling other window in an anything session.
968 It is used by `anything-scroll-other-window'
969 and `anything-scroll-other-window-down'.
971 If you prefer scrolling line by line, set this value to 1.")
973 (defvar anything-display-function 'anything-default-display-buffer
974 "Function to display *anything* buffer.
975 It is `anything-default-display-buffer' by default,
976 which affects `anything-samewindow'.")
978 (defvar anything-delayed-init-executed nil)
980 (defvar anything-mode-line-string "\\<anything-map>\\[anything-help]:help \
981 \\[anything-select-action]:Acts \
982 \\[anything-exit-minibuffer]/\\[anything-select-2nd-action-or-end-of-line]/\
983 \\[anything-select-3rd-action]:NthAct \
984 \\[anything-send-bug-report-from-anything]:BugReport"
985 "Help string displayed in mode-line in `anything'.
986 It can be a string or a list of two args, in this case,
987 first arg is a string that will be used as name for candidates number,
988 second arg any string to display in mode line.
989 If nil, use default `mode-line-format'.")
991 (defvar anything-help-message
992 "\\<anything-map>The keys that are defined for `anything' are:
993 \\{anything-map}"
994 "Detailed help message string for `anything'.
995 It also accepts function or variable symbol.")
997 (defvar anything-source-in-each-line-flag nil
998 "Non-nil means add anything-source text-property in each candidate.
999 experimental feature.")
1001 (defvaralias 'anything-debug-variables 'anything-debug-forms)
1003 (defvar anything-debug-forms nil
1004 "Forms to show in `anything-debug-output'.
1005 Otherwise all variables started with `anything-' are shown.
1006 It is useful for debug.")
1008 (defvar anything-debug nil
1009 "If non-nil, write log message into *Anything Log* buffer.
1010 If `debug-on-error' is non-nil, write log message regardless of this variable.
1011 It is disabled by default because *Anything Log* grows quickly.")
1013 (defcustom anything-local-map-override-anything-map t
1014 "Override `anything-map' keys with the corresponding ones in source local map.
1015 When non--nil keys in source local map will override same keys in `anything-map'
1016 otherwise same keys in `anything-map' will take precedence."
1017 :group 'anything
1018 :type 'boolean)
1021 ;; (@* "Internal Variables")
1022 (defvar anything-test-candidate-list nil)
1023 (defvar anything-test-mode nil)
1024 (defvar anything-source-name nil)
1025 (defvar anything-candidate-buffer-alist nil)
1026 (defvar anything-check-minibuffer-input-timer nil)
1027 (defvar anything-match-hash (make-hash-table :test 'equal))
1028 (defvar anything-cib-hash (make-hash-table :test 'equal))
1029 (defvar anything-tick-hash (make-hash-table :test 'equal))
1030 (defvar anything-issued-errors nil)
1031 (defvar anything-shortcut-keys nil)
1032 (defvar anything-once-called-functions nil)
1033 (defvar anything-follow-mode nil)
1034 (defvar anything-let-variables nil)
1035 (defvar anything-split-window-state nil)
1036 (defvar anything-selection-point nil)
1037 (defvar anything-alive-p nil)
1040 ;; (@* "Utility: logging")
1041 (defun anything-log (format-string &rest args)
1042 "Log message if `debug-on-error' or `anything-debug' is non-nil.
1043 Messages are written to the *Anything Log* buffer.
1045 Argument FORMAT-STRING is a string to use with `format'.
1046 Use optional arguments ARGS like in `format'."
1047 (when (or debug-on-error anything-debug)
1048 (with-current-buffer (get-buffer-create "*Anything Log*")
1049 (buffer-disable-undo)
1050 (set (make-local-variable 'inhibit-read-only) t)
1051 (goto-char (point-max))
1052 (insert (let ((tm (current-time)))
1053 (format "%s.%06d (%s) %s\n"
1054 (format-time-string "%H:%M:%S" tm)
1055 (nth 2 tm)
1056 (anything-log-get-current-function)
1057 (apply #'format (cons format-string args))))))))
1059 (defmacro anything-log-eval (&rest exprs)
1060 "Write each EXPRS evaluation result to the *Anything Log* buffer."
1061 `(anything-log-eval-internal ',exprs))
1063 (defun anything-log-run-hook (hook)
1064 "Run HOOK like `run-hooks' but write these actions to anything log buffer."
1065 (anything-log "executing %s" hook)
1066 (when (boundp hook)
1067 (anything-log-eval (symbol-value hook))
1068 (anything-log-eval (default-value hook)))
1069 (run-hooks hook)
1070 (anything-log "executed %s" hook))
1072 (defun anything-log-eval-internal (exprs)
1073 "Eval EXPRS and write results to anything log buffer."
1074 (dolist (expr exprs)
1075 (condition-case err
1076 (anything-log "%S = %S" expr (eval expr))
1077 (error (anything-log "%S = ERROR!" expr)))))
1079 (defun anything-log-get-current-function ()
1080 "Get function name calling `anything-log'.
1081 The original idea is from `tramp-debug-message'."
1082 (loop with exclude-func-re = "^anything-\\(?:interpret\\|log\\|.*funcall\\)"
1083 for btn from 1 to 40 ;avoid inf-loop
1084 for btf = (second (backtrace-frame btn))
1085 for fn = (if (symbolp btf) (symbol-name btf) "")
1086 if (and (string-match "^anything" fn)
1087 (not (string-match exclude-func-re fn)))
1088 return fn))
1090 (defun anything-log-error (&rest args)
1091 "Accumulate error messages into `anything-issued-errors'.
1092 ARGS are args given to `format'."
1093 (apply 'anything-log (concat "ERROR: " (car args)) (cdr args))
1094 (let ((msg (apply 'format args)))
1095 (unless (member msg anything-issued-errors)
1096 (add-to-list 'anything-issued-errors msg))))
1098 (defvar anything-last-log-file nil)
1099 (defun anything-log-save-maybe ()
1100 "May be save log buffer to `anything-last-log-file'."
1101 (when (stringp anything-debug)
1102 (let ((logdir (expand-file-name (format-time-string "%Y%m%d")
1103 anything-debug)))
1104 (make-directory logdir t)
1105 (with-current-buffer (get-buffer-create "*Anything Log*")
1106 (write-region (point-min) (point-max)
1107 (setq anything-last-log-file
1108 (expand-file-name (format-time-string "%Y%m%d-%H%M%S")
1109 logdir))
1110 nil 'silent)
1111 (erase-buffer)))))
1113 (defun anything-open-last-log ()
1114 "Open anything log file of last anything session."
1115 (interactive)
1116 (if anything-last-log-file
1117 (view-file anything-last-log-file)
1118 (switch-to-buffer "*Anything Log*")))
1120 (defun anything-print-error-messages ()
1121 "Print error messages in `anything-issued-errors'."
1122 (message "%s" (mapconcat 'identity (reverse anything-issued-errors) "\n")))
1124 ;; (anything-log "test")
1125 ;; (switch-to-buffer-other-window "*Anything Log*")
1128 ;; (@* "Programming Tools")
1129 (defmacro anything-aif (test-form then-form &rest else-forms)
1130 "Like `if' but set the result of TEST-FORM in a temprary variable called `it'.
1131 THEN-FORM and ELSE-FORMS are then excuted just like in `if'."
1132 (declare (indent 2) (debug t))
1133 `(let ((it ,test-form))
1134 (if it ,then-form ,@else-forms)))
1136 (defun anything-mklist (obj)
1137 "If OBJ is a list \(but not lambda\), return itself.
1138 Otherwise make a list with one element."
1139 (if (and (listp obj) (not (functionp obj)))
1141 (list obj)))
1144 ;; (@* "Anything API")
1146 (defun anything-buffer-get ()
1147 "Return `anything-action-buffer' if shown otherwise `anything-buffer'."
1148 (if (anything-action-window)
1149 anything-action-buffer
1150 anything-buffer))
1152 (defun anything-window ()
1153 "Window of `anything-buffer'."
1154 (get-buffer-window (anything-buffer-get) 'visible))
1156 (defun anything-action-window ()
1157 "Window of `anything-action-buffer'."
1158 (get-buffer-window anything-action-buffer 'visible))
1160 (defmacro with-anything-window (&rest body)
1161 "Be sure BODY is excuted in the anything window."
1162 (declare (indent 0) (debug t))
1163 `(if anything-test-mode
1164 (with-current-buffer (anything-buffer-get)
1165 ,@body)
1166 (with-selected-window (anything-window)
1167 ,@body)))
1169 (defmacro with-anything-current-buffer (&rest body)
1170 "Eval BODY inside `anything-current-buffer'."
1171 (declare (indent 0) (debug t))
1172 `(with-current-buffer anything-current-buffer
1173 ,@body))
1175 (defmacro with-anything-restore-variables(&rest body)
1176 "Restore `anything-restored-variables' after executing BODY.
1177 `post-command-hook' is handled specially."
1178 (declare (indent 0) (debug t))
1179 `(let ((--orig-vars (mapcar (lambda (v)
1180 (cons v (symbol-value v)))
1181 anything-restored-variables))
1182 (--post-command-hook-pair (cons post-command-hook
1183 (default-value 'post-command-hook))))
1184 (setq post-command-hook '(t))
1185 (setq-default post-command-hook nil)
1186 (unwind-protect (progn ,@body)
1187 (loop for (var . value) in --orig-vars
1188 do (set var value))
1189 (setq post-command-hook (car --post-command-hook-pair))
1190 (setq-default post-command-hook (cdr --post-command-hook-pair))
1191 (anything-log "restore variables"))))
1193 (defun* anything-attr (attribute-name &optional
1194 (src (anything-get-current-source)))
1195 "Get the value of ATTRIBUTE-NAME of SRC.
1196 if SRC is omitted, use current source.
1197 It is useful to write your sources."
1198 (anything-aif (assq attribute-name src)
1199 (cdr it)))
1201 (defun* anything-attr* (attribute-name
1202 &optional (src (anything-get-current-source)))
1203 "Pass the value of ATTRIBUTE-NAME of SRC to `anything-interpret-value'.
1204 if SRC is omitted, use current source.
1205 It is useful to write your sources."
1206 (anything-interpret-value (anything-attr attribute-name src)))
1208 (defun* anything-attr-defined (attribute-name
1209 &optional (src (anything-get-current-source)))
1210 "Return non-nil if ATTRIBUTE-NAME of SRC is defined.
1211 if SRC is omitted, use current source.
1212 It is useful to write your sources."
1213 (and (assq attribute-name src) t))
1215 (defun* anything-attrset (attribute-name value
1216 &optional
1217 (src (anything-get-current-source)))
1218 "Set the value of ATTRIBUTE-NAME of SRC to VALUE.
1219 if SRC is omitted, use current source.
1220 It is useful to write your sources."
1221 (anything-aif (assq attribute-name src)
1222 (setcdr it value)
1223 (setcdr src (cons (cons attribute-name value) (cdr src))))
1224 value)
1226 (defun anything-set-source-filter (sources)
1227 "Set the value of `anything-source-filter' to SOURCES and update.
1229 This function sets a filter for anything sources and it may be
1230 called while anything is running. It can be used to toggle
1231 displaying of sources dinamically. For example, additional keys
1232 can be bound into `anything-map' to display only the file-related
1233 results if there are too many matches from other sources and
1234 you're after files only:
1236 Shift+F shows only file results from some sources:
1238 \(define-key anything-map \"F\" 'anything-my-show-files-only)
1240 \(defun anything-my-show-files-only ()
1241 (interactive)
1242 (anything-set-source-filter '(\"File Name History\"
1243 \"Files from Current Directory\")))
1245 Shift+A shows all results:
1247 \(define-key anything-map \"A\" 'anything-my-show-all)
1249 \(defun anything-my-show-all ()
1250 (interactive)
1251 (anything-set-source-filter nil))
1253 Note that you have to prefix the functions with anything- prefix,
1254 otherwise they won't be bound when Anything is used under
1255 Iswitchb. The -my- part is added to avoid collisions with
1256 existing Anything function names."
1257 (unless (and (listp sources)
1258 (loop for name in sources always (stringp name)))
1259 (error "Invalid data in `anything-set-source-filter': %S" sources))
1260 (setq anything-source-filter sources)
1261 (anything-log-eval anything-source-filter)
1262 (anything-update))
1264 (defun anything-set-sources (sources &optional no-init no-update)
1265 "Set SOURCES during `anything' invocation.
1266 If NO-INIT is non-nil, skip executing init functions of SOURCES.
1267 If NO-UPDATE is non-nil, skip executing `anything-update'."
1268 (with-current-buffer anything-buffer
1269 (setq anything-compiled-sources nil
1270 anything-sources sources
1271 anything-last-sources-local sources)
1272 (anything-log-eval anything-compiled-sources anything-sources))
1273 (unless no-init (anything-funcall-foreach 'init))
1274 (unless no-update (anything-update)))
1276 (defvar anything-compile-source-functions
1277 '(anything-compile-source--type
1278 anything-compile-source--dummy
1279 anything-compile-source--disable-shortcuts
1280 anything-compile-source--candidates-in-buffer)
1281 "Functions to compile elements of `anything-sources' (plug-in).")
1283 (defun anything-get-sources ()
1284 "Return compiled `anything-sources', which is memoized.
1286 Attributes:
1288 - type
1289 `anything-type-attributes' are merged in.
1290 - candidates-buffer
1291 candidates, volatile and match attribute are created."
1292 (cond
1293 ;; action
1294 ((anything-action-window)
1295 anything-sources)
1296 ;; memoized
1297 (anything-compiled-sources)
1298 ;; first time
1300 (prog1
1301 (setq anything-compiled-sources
1302 (anything-compile-sources
1303 anything-sources anything-compile-source-functions))
1304 (anything-log-eval anything-compiled-sources)))))
1306 (defun* anything-get-selection (&optional (buffer nil buffer-s)
1307 force-display-part)
1308 "Return the currently selected item or nil.
1309 if BUFFER is nil or unspecified, use anything-buffer as default value.
1310 If FORCE-DISPLAY-PART is non-nil, return the display string.
1311 If FORCE-DISPLAY-PART value is 'withprop the display string is returned
1312 with its properties."
1313 (setq buffer (if (and buffer buffer-s) buffer anything-buffer))
1314 (unless (anything-empty-buffer-p buffer)
1315 (with-current-buffer buffer
1316 (let* ((disp-fn (if (eq force-display-part 'withprop)
1317 'buffer-substring
1318 'buffer-substring-no-properties))
1319 (selection
1320 (or (and (not force-display-part)
1321 (get-text-property (overlay-start
1322 anything-selection-overlay)
1323 'anything-realvalue))
1324 ;; It is needed to return properties of DISP in some case,
1325 ;; e.g for `anything-confirm-and-exit-minibuffer',
1326 ;; so use `buffer-substring' here when 'withprop is specified.
1327 (let ((disp (funcall disp-fn
1328 (overlay-start anything-selection-overlay)
1329 (1- (overlay-end anything-selection-overlay))))
1330 (source (anything-get-current-source)))
1331 (anything-aif (and (not force-display-part)
1332 (assoc-default 'display-to-real source))
1333 (anything-funcall-with-source source it disp)
1334 disp)))))
1335 (unless (equal selection "")
1336 (anything-log-eval selection)
1337 selection)))))
1339 (defun anything-get-action ()
1340 "Return the associated action for the selected candidate.
1341 It is a function symbol \(sole action\) or list
1342 of \(action-display . function\)."
1343 (unless (anything-empty-buffer-p (anything-buffer-get))
1344 (anything-aif (anything-attr 'action-transformer)
1345 (anything-composed-funcall-with-source
1346 (anything-get-current-source) it
1347 (anything-attr 'action) (anything-get-selection))
1348 (anything-attr 'action))))
1350 (defun anything-get-current-source ()
1351 "Return the source for the current selection.
1352 Use it in init, candidates, action, candidate-transformer,
1353 filtered-candidate-transformer functions."
1354 (declare (special source))
1355 ;; The name `anything-get-current-source' should be used in init function etc.
1356 (if (and (boundp 'anything-source-name) (stringp anything-source-name))
1357 source
1358 (with-current-buffer (anything-buffer-get)
1360 ;; This happen only when `anything-source-in-each-line-flag'
1361 ;; is non--nil and there is candidates in buffer.
1362 (get-text-property (point) 'anything-source)
1363 ;; Return nil when no--candidates.
1364 (block exit
1365 ;; This goto-char shouldn't be necessary, but point is moved to
1366 ;; point-min somewhere else which shouldn't happen.
1367 (goto-char (overlay-start anything-selection-overlay))
1368 (let* ((header-pos (or (anything-get-previous-header-pos)
1369 (anything-get-next-header-pos)))
1370 (source-name
1371 (save-excursion
1372 (unless header-pos
1373 (return-from exit nil))
1374 (goto-char header-pos)
1375 (anything-current-line-contents))))
1376 (loop for source in (anything-get-sources) thereis
1377 (and (equal (assoc-default 'name source) source-name)
1378 source))))))))
1380 (defun anything-buffer-is-modified (buffer)
1381 "Return non-nil when BUFFER is modified since `anything' was invoked."
1382 (let* ((b (get-buffer buffer))
1383 (key (concat (buffer-name b) "/" (anything-attr 'name)))
1384 (source-tick (or (gethash key anything-tick-hash) 0))
1385 (buffer-tick (buffer-chars-modified-tick b))
1386 (modifiedp (/= source-tick buffer-tick)))
1387 (puthash key buffer-tick anything-tick-hash)
1388 (anything-log-eval buffer modifiedp)
1389 modifiedp))
1391 (defun anything-current-buffer-is-modified ()
1392 "Check if `anything-current-buffer' is modified since `anything' was invoked."
1393 (anything-buffer-is-modified anything-current-buffer))
1395 (defvar anything-quit nil)
1396 (defun anything-run-after-quit (function &rest args)
1397 "Perform an action after quitting `anything'.
1398 The action is to call FUNCTION with arguments ARGS."
1399 (setq anything-quit t)
1400 (anything-kill-async-processes)
1401 (anything-log-eval function args)
1402 (apply 'run-with-idle-timer 0 nil function args)
1403 (anything-exit-minibuffer))
1406 (defun define-anything-type-attribute (type definition &optional doc)
1407 "Register type attribute of TYPE as DEFINITION with DOC.
1408 DOC is displayed in `anything-type-attributes' docstring.
1410 Use this function is better than setting `anything-type-attributes' directly."
1411 (loop for i in definition do
1412 ;; without `ignore-errors', error at emacs22
1413 (ignore-errors (setf i (delete nil i))))
1414 (anything-add-type-attribute type definition)
1415 (and doc (anything-document-type-attribute type doc))
1416 nil)
1418 (defvaralias 'anything-attributes 'anything-additional-attributes)
1419 (defvar anything-additional-attributes nil
1420 "List of all `anything' attributes.")
1422 (defun anything-document-attribute (attribute short-doc &optional long-doc)
1423 "Register ATTRIBUTE documentation introduced by plug-in.
1424 SHORT-DOC is displayed beside attribute name.
1425 LONG-DOC is displayed below attribute name and short documentation."
1426 (if long-doc
1427 (setq short-doc (concat "(" short-doc ")"))
1428 (setq long-doc short-doc
1429 short-doc ""))
1430 (add-to-list 'anything-additional-attributes attribute t)
1431 (put attribute 'anything-attrdoc
1432 (concat "- " (symbol-name attribute)
1433 " " short-doc "\n\n" long-doc "\n")))
1435 (put 'anything-document-attribute 'lisp-indent-function 2)
1437 (defun anything-interpret-value (value &optional source)
1438 "Interpret VALUE as variable, function or literal.
1439 If VALUE is a function, call it with no arguments and return the value.
1440 If SOURCE is `anything' source, `anything-source-name' is source name.
1442 If VALUE is a variable, return the value.
1444 If VALUE is a symbol, but it is not a function or a variable, cause an error.
1446 Otherwise, return VALUE itself."
1447 (cond ((and source (functionp value))
1448 (anything-funcall-with-source source value))
1449 ((functionp value)
1450 (funcall value))
1451 ((and (symbolp value) (boundp value))
1452 (symbol-value value))
1453 ((symbolp value)
1454 (error "anything-interpret-value: Symbol must be a function or a variable"))
1456 value)))
1458 (defun anything-once (function &rest args)
1459 "Ensure FUNCTION with ARGS to be called once in `anything' session."
1460 (let ((spec (cons function args)))
1461 (unless (member spec anything-once-called-functions)
1462 (apply function args)
1463 (push spec anything-once-called-functions))))
1466 ;; (@* "Core: API helper")
1467 (defun* anything-empty-buffer-p (&optional (buffer anything-buffer))
1468 "Check if BUFFER have candidates.
1469 Default value for BUFFER is `anything-buffer'."
1470 (zerop (buffer-size (and buffer (get-buffer buffer)))))
1472 (defun anything-let-eval-varlist (varlist)
1473 "Return the list of pairs VARLIST with each cdr of pair evaluated.
1474 If VARLIST contain single elements, those are returned
1475 as a list of one element."
1476 (mapcar (lambda (pair)
1477 (if (listp pair)
1478 (cons (car pair) (eval (cadr pair)))
1479 (cons pair nil)))
1480 varlist))
1482 ;; [NOT USED]
1483 ;; (defun anything-let*-eval-varlist (varlist)
1484 ;; (let ((vars (mapcar (lambda (pair)
1485 ;; (or (car-safe pair) pair))
1486 ;; varlist)))
1487 ;; (eval `(let ,vars
1488 ;; ,@(mapcar (lambda (pair)
1489 ;; (if (listp pair)
1490 ;; `(setq ,(car pair) ,(cadr pair))
1491 ;; `(setq ,pair nil)))
1492 ;; varlist)
1493 ;; (mapcar (lambda (v)
1494 ;; (cons v (symbol-value v)))
1495 ;; ',vars)))))
1497 (defun anything-let-internal (binding bodyfunc)
1498 "Set BINDING to anything buffer-local variables and Evaluate BODYFUNC.
1499 BINDING is a list of \(VARNAME . VALUE\) pair."
1500 (setq anything-let-variables binding)
1501 (unwind-protect
1502 (funcall bodyfunc)
1503 (setq anything-let-variables nil)))
1506 ;; (@* "Core: tools")
1507 (defun anything-current-line-contents ()
1508 "Current line string without properties."
1509 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))
1511 (defun anything-funcall-with-source (source func &rest args)
1512 "Call from SOURCE FUNC list or single function FUNC with ARGS.
1513 FUNC can be a symbol or a list of functions.
1514 Return the result of last function call."
1515 (let ((anything-source-name (assoc-default 'name source))
1516 result)
1517 (anything-log-eval anything-source-name func args)
1518 (dolist (func (if (functionp func) (list func) func) result)
1519 (setq result (apply func args)))))
1521 (defun anything-funcall-foreach (sym)
1522 "Call the function SYM for each source if any."
1523 (dolist (source (anything-get-sources))
1524 (anything-aif (assoc-default sym source)
1525 (anything-funcall-with-source source it))))
1527 (defun anything-normalize-sources (sources)
1528 "If SOURCES is only one source, make a list of one element."
1529 (cond ((or (and sources
1530 (symbolp sources))
1531 (and (listp sources) (assq 'name sources)))
1532 (list sources))
1533 (sources)
1534 (t anything-sources)))
1536 (defun anything-approximate-candidate-number (&optional in-current-source)
1537 "Return approximate number of candidates in `anything-buffer'.
1538 If IN-CURRENT-SOURCE is provided return number of candidates
1539 in the source where point is.
1540 It is used to check if candidate number is 0, 1, or 2+."
1541 (with-current-buffer anything-buffer
1542 (save-excursion
1543 (if in-current-source
1544 (goto-char (anything-get-previous-header-pos))
1545 (goto-char (point-min)))
1546 (forward-line 1)
1547 (let ((count-multi 1))
1548 (if (anything-pos-multiline-p)
1549 (save-excursion
1550 (loop while (and (not (if in-current-source
1551 (save-excursion
1552 (forward-line 2)
1553 (or (anything-pos-header-line-p) (eobp)))
1554 (eobp)))
1555 (search-forward anything-candidate-separator nil t))
1556 do (incf count-multi)
1557 finally return count-multi))
1558 (save-excursion
1559 (loop with ln = 0
1560 while (not (if in-current-source
1561 (or (anything-pos-header-line-p) (eobp))
1562 (eobp)))
1563 unless (anything-pos-header-line-p)
1564 do (incf ln)
1565 do (forward-line 1) finally return ln)))))))
1567 (defmacro with-anything-quittable (&rest body)
1568 "If an error occur in execution of BODY, quit anything safely."
1569 (declare (indent 0) (debug t))
1570 `(let (inhibit-quit)
1571 (condition-case v
1572 (progn ,@body)
1573 (quit (setq anything-quit t)
1574 (exit-minibuffer)
1575 (keyboard-quit)))))
1577 (defun anything-compose (arg-lst func-lst)
1578 "Apply arguments specified in ARG-LST with each function of FUNC-LST.
1579 The result of each function will be the new `car' of ARG-LST.
1581 This function allows easy sequencing of transformer functions."
1582 (dolist (func func-lst)
1583 (setcar arg-lst (apply func arg-lst)))
1584 (car arg-lst))
1586 (defun anything-composed-funcall-with-source (source funcs &rest args)
1587 "With SOURCE apply `anything-funcall-with-source' with each FUNCS and ARGS.
1588 This is used in transformers to modify candidates list."
1589 (if (functionp funcs)
1590 (apply 'anything-funcall-with-source source funcs args)
1591 (apply 'anything-funcall-with-source source
1592 (lambda (&rest args)
1593 (anything-compose args funcs))
1594 args)))
1596 (defun anything-new-timer (variable timer)
1597 "Give VARIABLE value to TIMER and cancel old timer."
1598 (anything-aif (symbol-value variable)
1599 (cancel-timer it))
1600 (set variable timer))
1603 ;; (@* "Core: entry point")
1604 (defconst anything-argument-keys
1605 '(:sources :input :prompt :resume :preselect :buffer :keymap :default :history))
1607 ;;;###autoload
1608 (defun anything (&rest plist)
1609 "Main function to execute anything sources.
1611 Keywords supported:
1612 :sources :input :prompt :resume :preselect :buffer :keymap :default :history
1613 Extra keywords are supported and can be added, see below.
1615 When call interactively with no arguments deprecated `anything-sources'
1616 will be used if non--nil.
1618 PLIST is a list like \(:key1 val1 :key2 val2 ...\) or
1619 \(&optional sources input prompt resume preselect buffer keymap default history\).
1621 Basic keywords are the following:
1623 \:sources
1625 Temporary value of `anything-sources'. It also accepts a
1626 symbol, interpreted as a variable of an anything source. It
1627 also accepts an alist representing an anything source, which is
1628 detected by \(assq 'name ANY-SOURCES\)
1630 \:input
1632 Temporary value of `anything-pattern', ie. initial input of minibuffer.
1634 \:prompt
1636 Prompt other than \"pattern: \".
1638 \:resume
1640 If t, Resurrect previously instance of `anything'. Skip the initialization.
1641 If 'noresume, this instance of `anything' cannot be resumed.
1643 \:preselect
1645 Initially selected candidate. Specified by exact candidate or a regexp.
1647 \:buffer
1649 `anything-buffer' instead of *anything*.
1651 \:keymap
1653 `anything-map' for current `anything' session.
1655 \:default
1657 A default argument that will be inserted in minibuffer \
1658 with \\<minibuffer-local-map>\\[next-history-element].
1659 When nil of not present `thing-at-point' will be used instead.
1661 \:history
1663 By default all minibuffer input is pushed to `minibuffer-history',
1664 if an argument HISTORY is provided, input will be pushed to HISTORY.
1665 History element should be a symbol.
1667 Of course, conventional arguments are supported, the two are same.
1669 \(anything :sources sources :input input :prompt prompt :resume resume
1670 :preselect preselect :buffer buffer :keymap keymap :default default
1671 :history history\)
1672 \(anything sources input prompt resume preselect buffer keymap default history\)
1674 Other keywords are interpreted as local variables of this anything session.
1675 The `anything-' prefix can be omitted. For example,
1677 \(anything :sources 'anything-c-source-buffers
1678 :buffer \"*buffers*\" :candidate-number-limit 10\)
1680 means starting anything session with `anything-c-source-buffers'
1681 source in *buffers* buffer and set variable `anything-candidate-number-limit'
1682 to 10 as session local variable."
1683 (interactive)
1684 (if (keywordp (car plist))
1685 (anything-let-internal
1686 (anything-parse-keys plist)
1687 (lambda ()
1688 (apply 'anything
1689 (mapcar (lambda (key) (plist-get plist key))
1690 anything-argument-keys))))
1691 (apply 'anything-internal plist)))
1693 (defun anything-parse-keys (keys)
1694 "Parse the KEYS arguments of `anything'.
1695 Return only the keys that are not in `anything-argument-keys'.
1696 It is used to set local variables via `anything-let-internal'.
1697 This allow to add arguments that are not part of `anything-argument-keys',
1698 but are valid anything attributes.
1699 i.e :candidate-number-limit will be bound to `anything-candidate-number-limit'
1700 in source."
1701 ;; (anything-parse-keys '(:sources ((name . "test")
1702 ;; (candidates . (a b c)))
1703 ;; :buffer "toto"
1704 ;; :candidate-number-limit 4))
1705 ;; ==> ((anything-candidate-number-limit . 4))
1706 (loop for (key value) on keys by #'cddr
1707 for symname = (substring (symbol-name key) 1)
1708 for sym = (intern (if (string-match "^anything-" symname)
1709 symname
1710 (concat "anything-" symname)))
1711 unless (memq key anything-argument-keys)
1712 collect (cons sym value)))
1714 ;;; (@* "Core: entry point helper")
1715 (defun anything-internal (&optional
1716 any-sources any-input
1717 any-prompt any-resume
1718 any-preselect any-buffer
1719 any-keymap any-default any-history)
1720 "The internal anything function called by `anything'.
1721 For ANY-SOURCES ANY-INPUT ANY-PROMPT ANY-RESUME ANY-PRESELECT ANY-BUFFER and
1722 ANY-KEYMAP ANY-DEFAULT ANY-HISTORY See `anything'."
1723 (anything-log (concat "[Start session] " (make-string 41 ?+)))
1724 (anything-log-eval any-prompt any-preselect
1725 any-buffer any-keymap any-default)
1726 (let ((old-overridding-local-map overriding-local-map))
1727 (unwind-protect
1728 (condition-case v
1729 (let ( ;; It is needed because `anything-source-name' is non-nil
1730 ;; when `anything' is invoked by action. Awful global scope.
1731 anything-source-name
1732 anything-in-persistent-action
1733 anything-quit
1734 (case-fold-search t)
1735 (anything-buffer (or any-buffer anything-buffer))
1736 ;; cua-mode ; avoid error when region is selected
1738 (with-anything-restore-variables
1739 (anything-initialize any-resume any-input any-sources)
1740 (anything-display-buffer anything-buffer)
1741 (anything-log "show prompt")
1742 (unwind-protect
1743 (anything-read-pattern-maybe
1744 any-prompt any-input any-preselect
1745 any-resume any-keymap any-default
1746 (when (and any-history (symbolp any-history)) any-history))
1747 (anything-cleanup)))
1748 (prog1 (unless anything-quit
1749 (anything-execute-selection-action-1))
1750 (anything-log (concat "[End session] " (make-string 41 ?-)))))
1751 (quit
1752 (anything-restore-position-on-quit)
1753 (anything-log (concat "[End session (quit)] " (make-string 34 ?-)))
1754 nil))
1755 (setq overriding-local-map old-overridding-local-map)
1756 (anything-log-save-maybe))))
1759 ;;; Anything resume
1762 (defun* anything-resume (&optional
1763 (any-buffer anything-last-buffer)
1764 buffer-pattern (any-resume t))
1765 "Resurrect previously invoked `anything'.
1766 Called with a prefix arg, allow choosing among all existing
1767 anything buffers. i.e choose among various anything sessions."
1768 (interactive)
1769 (when (or current-prefix-arg buffer-pattern)
1770 (setq any-buffer (anything-resume-select-buffer buffer-pattern)))
1771 (setq anything-compiled-sources nil)
1772 (anything
1773 :sources (or (buffer-local-value 'anything-last-sources-local (get-buffer any-buffer))
1774 anything-last-sources anything-sources)
1775 :input (buffer-local-value 'anything-input-local (get-buffer any-buffer))
1776 :resume any-resume
1777 :buffer any-buffer))
1779 ;;; rubikitch: experimental
1780 ;;; I use this and check it whether I am convenient.
1781 ;;; I may introduce an option to control the behavior.
1782 (defun* anything-resume-window-only (&optional
1783 (any-buffer anything-last-buffer)
1784 buffer-pattern)
1785 (interactive)
1786 (anything-resume any-buffer buffer-pattern 'window-only))
1788 (defun anything-resume-p (any-resume)
1789 "Whether current anything session is resumed or not.
1790 Just check if ANY-RESUME value is t or window-only."
1791 (memq any-resume '(t window-only)))
1793 (defun anything-resume-select-buffer (input)
1794 "Resume precedent anything session with initial input INPUT."
1795 (or (anything :sources '(((name . "Resume anything buffer")
1796 (candidates . anything-buffers)
1797 (action . identity)))
1798 :input input
1799 :resume 'noresume
1800 :buffer "*anything resume*")
1801 (keyboard-quit)))
1804 ;;;###autoload
1805 (defun anything-at-point (&optional
1806 any-sources any-input
1807 any-prompt any-resume
1808 any-preselect any-buffer)
1809 "Call anything with symbol at point as initial input.
1810 ANY-SOURCES ANY-INPUT ANY-PROMPT ANY-RESUME ANY-PRESELECT and ANY-BUFFER
1811 are same args as in `anything'."
1812 (interactive)
1813 (anything :sources any-sources
1814 :input (if current-prefix-arg
1815 (concat "\\b" (thing-at-point 'symbol) "\\b"
1816 (if (featurep 'anything-match-plugin) " " ""))
1817 any-input)
1818 :prompt any-prompt
1819 :resume any-resume
1820 :preselect any-preselect
1821 :buffer any-buffer))
1823 ;;;###autoload
1824 (defun anything-other-buffer (any-sources any-buffer)
1825 "Simplified interface of `anything' with other `anything-buffer'.
1826 Call `anything' with only ANY-SOURCES and ANY-BUFFER as args."
1827 (anything :sources any-sources :buffer any-buffer))
1829 (defun anything-nest (&rest same-as-anything)
1830 "Allow calling `anything' whithin a running anything session."
1831 (with-anything-window
1832 (let (anything-current-position
1833 anything-current-buffer
1834 (orig-anything-current-buffer anything-current-buffer)
1835 (orig-anything-buffer anything-buffer)
1836 (orig-anything-last-frame-or-window-configuration
1837 anything-last-frame-or-window-configuration)
1838 anything-pattern
1839 (anything-buffer (or (getf same-as-anything :buffer)
1840 (nth 5 same-as-anything)
1841 "*Anything*"))
1842 anything-sources
1843 anything-compiled-sources
1844 (anything-samewindow t)
1845 (enable-recursive-minibuffers t))
1846 (unwind-protect
1847 (apply #'anything same-as-anything)
1848 (with-current-buffer orig-anything-buffer
1849 (anything-initialize-overlays orig-anything-buffer)
1850 (setq anything-buffer (current-buffer))
1851 (anything-mark-current-line)
1852 (setq anything-last-frame-or-window-configuration
1853 orig-anything-last-frame-or-window-configuration)
1854 (setq cursor-type t)
1855 (setq anything-current-buffer orig-anything-current-buffer))))))
1858 ;;; Initialize
1861 (defvar anything-buffers nil
1862 "All of `anything-buffer' in most recently used order.")
1863 (defun anything-initialize (any-resume any-input any-sources)
1864 "Start initialization of `anything' session.
1865 For ANY-RESUME ANY-INPUT and ANY-SOURCES See `anything'."
1866 (anything-log "start initialization: any-resume=%S any-input=%S" any-resume any-input)
1867 (anything-frame-or-window-configuration 'save)
1868 (setq anything-sources (anything-normalize-sources any-sources))
1869 (anything-log "sources = %S" anything-sources)
1870 (anything-hooks 'setup)
1871 (anything-current-position 'save)
1872 (if (anything-resume-p any-resume)
1873 (anything-initialize-overlays (anything-buffer-get))
1874 (anything-initial-setup))
1875 (unless (eq any-resume 'noresume)
1876 (anything-recent-push anything-buffer 'anything-buffers)
1877 (setq anything-last-buffer anything-buffer))
1878 (when any-input (setq anything-input any-input anything-pattern any-input))
1879 (and (anything-resume-p any-resume) (anything-funcall-foreach 'resume))
1880 (anything-log "end initialization"))
1882 (defun anything-execute-selection-action-1 ()
1883 "Execute current action."
1884 (anything-log-run-hook 'anything-before-action-hook)
1885 (unwind-protect
1886 (anything-execute-selection-action)
1887 (anything-aif (get-buffer anything-action-buffer)
1888 (kill-buffer it))
1889 (anything-log-run-hook 'anything-after-action-hook)))
1891 (defun anything-restore-position-on-quit ()
1892 "Restore position in `anything-current-buffer' when quitting."
1893 (anything-current-position 'restore))
1895 (defun anything-recent-push (elt list-var)
1896 "Add ELT to the value of LIST-VAR as most recently used value."
1897 (let ((m (member elt (symbol-value list-var))))
1898 (and m (set list-var (delq (car m) (symbol-value list-var))))
1899 (push elt (symbol-value list-var))))
1902 ;;; (@* "Core: Accessors")
1903 ;;; rubikitch: I love to create functions to control variables.
1904 (defvar anything-current-position nil
1905 "Cons of \(point . window-start\) when `anything' is invoked.
1906 It is needed to restore position in `anything-current-buffer'
1907 when `anything' is keyboard-quitted.")
1908 (defun anything-current-position (save-or-restore)
1909 "Restore or save current position in `anything-current-buffer'.
1910 Argument SAVE-OR-RESTORE is one of save or restore."
1911 (case save-or-restore
1912 (save
1913 (anything-log "Save position at %S" (cons (point) (window-start)))
1914 (setq anything-current-position (cons (point) (window-start))))
1915 (restore
1916 (anything-log "Restore position at %S in buffer %s"
1917 anything-current-position
1918 (buffer-name (current-buffer)))
1919 (goto-char (car anything-current-position))
1920 ;; Fix this position with the NOFORCE arg of `set-window-start'
1921 ;; otherwise, if there is some other buffer than `anything-current-buffer'
1922 ;; one, position will be lost.
1923 (set-window-start (selected-window) (cdr anything-current-position) t))))
1926 ;; Internal.
1927 (defvar anything-last-frame-or-window-configuration nil
1928 "Used to store window or frame configuration when anything start.")
1929 (defun anything-frame-or-window-configuration (save-or-restore)
1930 "Save or restore last frame or window configuration.
1931 Possible value of SAVE-OR-RESTORE are 'save and 'restore.
1932 window or frame configuration is saved/restored according to values of
1933 `anything-save-configuration-functions'."
1934 (anything-log-eval anything-save-configuration-functions)
1935 (case save-or-restore
1936 (save (setq anything-last-frame-or-window-configuration
1937 (funcall (cdr anything-save-configuration-functions))))
1938 (restore (funcall (car anything-save-configuration-functions)
1939 anything-last-frame-or-window-configuration)
1940 ;; Restore frame focus.
1941 (let ((frame (and (listp anything-last-frame-or-window-configuration)
1942 (caadr anything-last-frame-or-window-configuration))))
1943 ;; If `anything-save-configuration-functions' are window functions
1944 ;; frame should be nil, use current frame.
1945 (unless (framep frame)
1946 (setq frame (selected-frame)))
1947 (select-frame-set-input-focus frame)))))
1950 ;; (@* "Core: Display *anything* buffer")
1951 (defun anything-display-buffer (buf)
1952 "Display *anything* buffer BUF."
1953 (let (pop-up-frames)
1954 (funcall (with-current-buffer buf anything-display-function) buf)))
1956 (defun anything-default-display-buffer (buf)
1957 "Default function to display BUF.
1958 Where BUF is generally `anything-buffer'.
1959 It use `switch-to-buffer' or `pop-to-buffer' depending of value of
1960 `anything-samewindow'."
1961 (funcall (if anything-samewindow 'switch-to-buffer 'pop-to-buffer) buf))
1964 ;; (@* "Core: initialize")
1965 (defun anything-initial-setup ()
1966 "Initialize anything settings and set up the anything buffer."
1967 (anything-log-run-hook 'anything-before-initialize-hook)
1968 (setq anything-current-prefix-arg nil)
1969 (setq anything-once-called-functions nil)
1970 (setq anything-delayed-init-executed nil)
1971 (setq anything-alive-p t)
1972 (setq anything-current-buffer
1973 (if (minibuffer-window-active-p (minibuffer-window))
1974 ;; If minibuffer is active be sure to use it's buffer
1975 ;; as `anything-current-buffer'.
1976 (window-buffer (active-minibuffer-window))
1977 (current-buffer)))
1978 (setq anything-buffer-file-name buffer-file-name)
1979 (setq anything-issued-errors nil)
1980 (setq anything-compiled-sources nil)
1981 (setq anything-saved-current-source nil)
1982 (if (or (not (boundp 'split-width-threshold))
1983 (not split-width-threshold)
1984 (and (integerp split-width-threshold)
1985 (>= split-width-threshold (+ (frame-width) 4))))
1986 (setq anything-split-window-state 'vertical)
1987 (setq anything-split-window-state 'horizontal))
1988 ;; Call the init function for sources where appropriate
1989 (anything-funcall-foreach 'init)
1990 (setq anything-pattern "")
1991 (setq anything-input "")
1992 (setq anything-candidate-cache nil)
1993 (setq anything-last-sources anything-sources)
1994 (anything-create-anything-buffer)
1995 (anything-log-run-hook 'anything-after-initialize-hook))
1997 (defvar anything-reading-pattern nil
1998 "Whether in `read-string' in anything or not.")
2000 (defun anything-read-pattern-maybe (any-prompt any-input
2001 any-preselect any-resume any-keymap
2002 any-default any-history)
2003 "Read pattern with prompt ANY-PROMPT and initial input ANY-INPUT.
2004 For ANY-PRESELECT ANY-RESUME ANY-KEYMAP, See `anything'."
2005 (if (anything-resume-p any-resume)
2006 (anything-mark-current-line t)
2007 (anything-update any-preselect))
2008 (with-current-buffer (anything-buffer-get)
2009 (let ((src-keymap (assoc-default 'keymap (anything-get-current-source))))
2010 ;; Startup with the first keymap found either in current source
2011 ;; or anything arg, otherwise use global value of `anything-map'.
2012 ;; This map will be used as a `minibuffer-local-map'.
2013 ;; Maybe it will be overriden when changing source
2014 ;; by `anything-maybe-update-keymap'.
2015 (unless anything-local-map-override-anything-map
2016 (anything-aif (or src-keymap any-keymap)
2017 (ignore-errors
2018 (set-keymap-parent it anything-map))))
2019 (set (make-local-variable 'anything-map)
2020 (or src-keymap any-keymap anything-map))
2021 (anything-log-eval (anything-approximate-candidate-number)
2022 anything-execute-action-at-once-if-one
2023 anything-quit-if-no-candidate)
2024 (cond ((and anything-execute-action-at-once-if-one
2025 (= (anything-approximate-candidate-number) 1))
2026 (ignore))
2027 ((and anything-quit-if-no-candidate
2028 (= (anything-approximate-candidate-number) 0))
2029 (setq anything-quit t)
2030 (and (functionp anything-quit-if-no-candidate)
2031 (funcall anything-quit-if-no-candidate)))
2033 (let ((anything-reading-pattern t)
2034 (tap (or any-default
2035 (with-anything-current-buffer
2036 (thing-at-point 'symbol)))))
2037 (read-from-minibuffer (or any-prompt "pattern: ")
2038 any-input anything-map
2039 nil any-history tap)))))))
2041 (defun anything-maybe-update-keymap ()
2042 "Handle differents keymaps in multiples sources.
2043 This function is meant to be run in `anything-move-selection-after-hook'.
2044 It will override `anything-map' with the keymap attribute of current source
2045 if some when multiples sources are present."
2046 (with-anything-window
2047 (let ((kmap (assoc-default 'keymap (anything-get-current-source))))
2048 (when kmap
2049 (and (not anything-local-map-override-anything-map)
2050 (ignore-errors (set-keymap-parent kmap (default-value 'anything-map))))
2051 (setq overriding-local-map kmap)))))
2052 (add-hook 'anything-move-selection-after-hook 'anything-maybe-update-keymap)
2054 (defun anything-create-anything-buffer (&optional test-mode)
2055 "Create newly created `anything-buffer'.
2056 If TEST-MODE is non-nil, clear `anything-candidate-cache'."
2057 (when test-mode
2058 (setq anything-candidate-cache nil))
2059 (with-current-buffer (get-buffer-create anything-buffer)
2060 (anything-log "kill local variables: %S" (buffer-local-variables))
2061 (kill-all-local-variables)
2062 (set (make-local-variable 'inhibit-read-only) t)
2063 (buffer-disable-undo)
2064 (erase-buffer)
2065 (set (make-local-variable 'inhibit-read-only) t)
2066 (set (make-local-variable 'anything-last-sources-local) anything-sources)
2067 (set (make-local-variable 'anything-follow-mode) nil)
2068 (set (make-local-variable 'anything-display-function) anything-display-function)
2069 (set (make-local-variable 'anything-selection-point) nil)
2070 (anything-initialize-persistent-action)
2071 (anything-log-eval anything-display-function anything-let-variables)
2072 (loop for (var . val) in anything-let-variables
2073 do (set (make-local-variable var) val))
2074 (setq cursor-type nil)
2075 (setq mode-name "Anything"))
2076 (anything-initialize-overlays anything-buffer)
2077 (get-buffer anything-buffer))
2079 (defun anything-initialize-overlays (buffer)
2080 "Initialize anything overlays in BUFFER."
2081 (anything-log "overlay setup")
2082 (if anything-selection-overlay
2083 ;; make sure the overlay belongs to the anything buffer if
2084 ;; it's newly created
2085 (move-overlay anything-selection-overlay (point-min) (point-min)
2086 (get-buffer buffer))
2088 (setq anything-selection-overlay
2089 (make-overlay (point-min) (point-min) (get-buffer buffer)))
2090 (overlay-put anything-selection-overlay 'face anything-selection-face))
2092 (cond (anything-enable-shortcuts
2093 (setq anything-shortcut-keys
2094 (assoc-default anything-enable-shortcuts anything-shortcut-keys-alist))
2095 (unless anything-digit-overlays
2096 (setq anything-digit-overlays
2097 (loop for key across anything-shortcut-keys
2098 for overlay = (make-overlay (point-min) (point-min)
2099 (get-buffer buffer))
2100 do (overlay-put overlay 'before-string
2101 (format "%s - " (upcase (make-string 1 key))))
2102 collect overlay))))
2103 (anything-digit-overlays
2104 (mapc 'delete-overlay anything-digit-overlays)
2105 (setq anything-digit-overlays nil))))
2107 (defun anything-hooks (setup-or-cleanup)
2108 "Add or remove hooks according to SETUP-OR-CLEANUP value.
2109 if SETUP-OR-CLEANUP value is setup add hooks, any other value
2110 will remove hooks.
2111 hooks concerned are `post-command-hook' and `minibuffer-setup-hook'."
2112 (let ((hooks '((post-command-hook anything-check-minibuffer-input)
2113 (minibuffer-setup-hook anything-print-error-messages))))
2114 (if (eq setup-or-cleanup 'setup)
2115 (dolist (args hooks) (apply 'add-hook args))
2116 (dolist (args (reverse hooks)) (apply 'remove-hook args)))))
2119 ;; (@* "Core: clean up")
2120 ;;; TODO move
2121 (defun anything-cleanup ()
2122 "Clean up the mess when anything exit or quit."
2123 (anything-log "start cleanup")
2124 (with-current-buffer anything-buffer
2125 ;; rubikitch: I think it is not needed.
2126 ;; thierry: If you end up for any reasons (error etc...)
2127 ;; with an anything-buffer staying around (visible),
2128 ;; You will have no cursor in this buffer when switching to it,
2129 ;; so I think this is needed.
2130 (setq cursor-type t)
2131 ;; Call burry-buffer whithout arg
2132 ;; to be sure anything-buffer is removed from window.
2133 (bury-buffer)
2134 ;; Be sure we call this from anything-buffer.
2135 (anything-funcall-foreach 'cleanup))
2136 (anything-new-timer 'anything-check-minibuffer-input-timer nil)
2137 (anything-kill-async-processes)
2138 (anything-log-run-hook 'anything-cleanup-hook)
2139 (anything-hooks 'cleanup)
2140 (anything-frame-or-window-configuration 'restore)
2141 (setq anything-alive-p nil)
2142 ;; This is needed in some cases where last input
2143 ;; is yielded infinitely in minibuffer after anything session.
2144 (anything-clean-up-minibuffer))
2146 (defun anything-clean-up-minibuffer ()
2147 "Remove contents of minibuffer."
2148 (let ((miniwin (minibuffer-window)))
2149 ;; Clean only current minibuffer used by anything.
2150 ;; i.e The precedent one is active.
2151 (unless (minibuffer-window-active-p miniwin)
2152 (with-current-buffer (window-buffer miniwin)
2153 (delete-minibuffer-contents)))))
2156 ;; (@* "Core: input handling")
2157 (defun anything-check-minibuffer-input ()
2158 "Extract input string from the minibuffer and check if it needs to be handled."
2159 (let ((delay (with-current-buffer anything-buffer
2160 (and anything-input-idle-delay
2161 (max anything-input-idle-delay 0.1)))))
2162 (if (or (not delay) (anything-action-window))
2163 (anything-check-minibuffer-input-1)
2164 (anything-new-timer
2165 'anything-check-minibuffer-input-timer
2166 (run-with-idle-timer delay nil 'anything-check-minibuffer-input-1)))))
2168 (defun anything-check-minibuffer-input-1 ()
2169 "Check minibuffer content."
2170 (with-anything-quittable
2171 (with-selected-window (or (active-minibuffer-window) (minibuffer-window))
2172 (anything-check-new-input (minibuffer-contents)))))
2174 (defun anything-check-new-input (input)
2175 "Check INPUT string and update the anything buffer if necessary."
2176 (unless (equal input anything-pattern)
2177 (setq anything-pattern input)
2178 (unless (anything-action-window)
2179 (setq anything-input anything-pattern))
2180 (anything-log-eval anything-pattern anything-input)
2181 (anything-update)))
2184 ;; (@* "Core: source compiler")
2185 (defvar anything-compile-source-functions-default anything-compile-source-functions
2186 "Plug-ins this file provides.")
2187 (defun anything-compile-sources (sources funcs)
2188 "Compile SOURCES with FUNCS.
2189 See `anything-compile-source-functions'.
2190 Anything plug-ins are realized by this function."
2191 (mapcar
2192 (lambda (source)
2193 (loop with source = (if (listp source) source (symbol-value source))
2194 for f in funcs
2195 do (setq source (funcall f source))
2196 finally (return source)))
2197 sources))
2200 ;; (@* "Core: plug-in attribute documentation hack")
2202 ;; `anything-document-attribute' is public API.
2203 (defadvice documentation-property (after anything-document-attribute activate)
2204 "Display plug-in attributes' documentation as `anything-sources' docstring."
2205 (when (eq (ad-get-arg 0) 'anything-sources)
2206 (setq ad-return-value
2207 (concat ad-return-value "\n"
2208 (mapconcat (lambda (sym) (get sym 'anything-attrdoc))
2209 anything-additional-attributes
2210 "\n")))))
2211 ;; (describe-variable 'anything-sources)
2212 ;; (documentation-property 'anything-sources 'variable-documentation)
2213 ;; (progn (ad-disable-advice 'documentation-property 'after 'anything-document-attribute) (ad-update 'documentation-property))
2216 ;; (@* "Core: all candidates")
2217 (defun anything-process-delayed-init (source)
2218 "Initialize delayed SOURCE."
2219 (let ((name (assoc-default 'name source)))
2220 (unless (member name anything-delayed-init-executed)
2221 (anything-aif (assoc-default 'delayed-init source)
2222 (with-current-buffer anything-current-buffer
2223 (anything-funcall-with-source source it)
2224 (dolist (f (if (functionp it) (list it) it))
2225 (add-to-list 'anything-delayed-init-executed name)))))))
2227 (defun anything-get-candidates (source)
2228 "Retrieve and return the list of candidates from SOURCE."
2229 (anything-process-delayed-init source)
2230 (let* ((candidate-source (assoc-default 'candidates source))
2231 (type-error (lambda ()
2232 (error (concat "Candidates must either be a function, "
2233 " a variable or a list: %s")
2234 candidate-source)))
2235 (candidates (condition-case err
2236 (anything-interpret-value candidate-source source)
2237 (error (funcall type-error)))))
2238 (cond ((processp candidates) candidates)
2239 ((listp candidates) (anything-transform-candidates candidates source))
2240 (t (funcall type-error)))))
2243 (defun anything-get-cached-candidates (source)
2244 "Return the cached value of candidates for SOURCE.
2245 Cache the candidates if there is not yet a cached value."
2246 (let* ((name (assoc-default 'name source))
2247 (candidate-cache (assoc name anything-candidate-cache)))
2248 (cond (candidate-cache
2249 (anything-log "use cached candidates")
2250 (cdr candidate-cache))
2252 (anything-log "calculate candidates")
2253 (let ((candidates (anything-get-candidates source)))
2254 (cond ((processp candidates)
2255 (push (cons candidates
2256 (append source
2257 (list (cons 'item-count 0)
2258 (cons 'incomplete-line ""))))
2259 anything-async-processes)
2260 (set-process-filter candidates 'anything-output-filter)
2261 (setq candidates nil))
2262 ((not (assoc 'volatile source))
2263 (setq candidate-cache (cons name candidates))
2264 (push candidate-cache anything-candidate-cache)))
2265 candidates)))))
2268 ;;; (@* "Core: candidate transformers")
2269 (defun anything-transform-mapcar (function args)
2270 "`mapcar' for candidate-transformer.
2272 ARGS is (cand1 cand2 ...) or ((disp1 . real1) (disp2 . real2) ...)
2274 \(anything-transform-mapcar 'upcase '(\"foo\" \"bar\"))
2275 => (\"FOO\" \"BAR\")
2276 \(anything-transform-mapcar 'upcase '((\"1st\" . \"foo\") (\"2nd\" . \"bar\")))
2277 => ((\"1st\" . \"FOO\") (\"2nd\" . \"BAR\"))
2279 (loop for arg in args
2280 if (consp arg)
2281 collect (cons (car arg) (funcall function (cdr arg)))
2282 else
2283 collect (funcall function arg)))
2285 (defun anything-process-candidate-transformer (candidates source)
2286 "Execute candidate-transformer function on all CANDIDATES of SOURCE."
2287 (anything-aif (assoc-default 'candidate-transformer source)
2288 (anything-composed-funcall-with-source source it candidates)
2289 candidates))
2291 (defun anything-process-filtered-candidate-transformer (candidates source)
2292 "Execute filtered-candidate-transformer function on all CANDIDATES of SOURCE."
2293 (anything-aif (assoc-default 'filtered-candidate-transformer source)
2294 (anything-composed-funcall-with-source source it candidates source)
2295 candidates))
2297 (defun anything-process-filtered-candidate-transformer-maybe (candidates source process-p)
2298 "Execute filtered-candidate-transformer function on all CANDIDATES of SOURCE.
2299 This happen if PROCESS-P is non-nil."
2300 (if process-p
2301 (anything-process-filtered-candidate-transformer candidates source)
2302 candidates))
2304 (defun anything-process-real-to-display (candidates source)
2305 "Execute real-to-display function on all CANDIDATES of SOURCE."
2306 (anything-aif (assoc-default 'real-to-display source)
2307 (setq candidates (anything-funcall-with-source
2308 source 'mapcar
2309 (lambda (cand_)
2310 (if (consp cand_)
2311 ;; override DISPLAY from candidate-transformer
2312 (cons (funcall it (cdr cand_)) (cdr cand_))
2313 (cons (funcall it cand_) cand_)))
2314 candidates))
2315 candidates))
2317 (defun anything-transform-candidates (candidates source &optional process-p)
2318 "Transform CANDIDATES of SOURCE according to candidate transformers.
2319 This happen if PROCESS-P is non-nil."
2320 (anything-process-real-to-display
2321 (anything-process-filtered-candidate-transformer-maybe
2322 (anything-process-candidate-transformer candidates source) source process-p)
2323 source))
2326 ;; (@* "Core: narrowing candidates")
2327 (defun anything-candidate-number-limit (source)
2328 "Apply candidate-number-limit attribute value.
2329 This overhide variable `anything-candidate-number-limit'.
2331 e.g:
2332 If \(candidate-number-limit\) is in SOURCE, show all candidates in SOURCE.
2333 If \(candidate-number-limit . 123\) is in SOURCE limit candidate to 123."
2334 (anything-aif (assq 'candidate-number-limit source)
2335 (or (cdr it) 99999999)
2336 (or anything-candidate-number-limit 99999999)))
2338 ;; FIXME: Why a defconst here
2339 (defconst anything-default-match-functions
2340 (list (lambda (candidate)
2341 (string-match anything-pattern candidate)))
2342 "Default functions to match candidates according to `anything-pattern'.")
2344 (defun anything-compute-matches (source)
2345 "Compute matched results from SOURCE according to its settings."
2346 (if debug-on-error
2347 (anything-compute-matches-internal source)
2348 (condition-case v
2349 (anything-compute-matches-internal source)
2350 (error (anything-log-error
2351 "anything-compute-matches: error when processing source: %s"
2352 (assoc-default 'name source))
2353 nil))))
2355 (defun anything-candidate-get-display (candidate)
2356 "Get searched display part from CANDIDATE.
2357 CANDIDATE is a string, a symbol, or \(DISPLAY . REAL\) cons cell."
2358 (format "%s" (or (car-safe candidate) candidate)))
2360 (defun anything-process-pattern-transformer (pattern source)
2361 "Execute pattern-transformer attribute PATTERN function in SOURCE."
2362 (anything-aif (assoc-default 'pattern-transformer source)
2363 (anything-composed-funcall-with-source source it pattern)
2364 pattern))
2366 (defun anything-match-functions (source)
2367 (or (assoc-default 'match source)
2368 anything-default-match-functions))
2370 (defmacro anything-accumulate-candidates-internal (cand newmatches
2371 hash item-count limit)
2372 "Internal, add CAND into NEWMATCHES.
2373 Use HASH to uniq NEWMATCHES.
2374 Argument ITEM-COUNT count the matches.
2375 if ITEM-COUNT reaches LIMIT, exit from inner loop."
2376 `(unless (gethash ,cand ,hash)
2377 (puthash ,cand t ,hash)
2378 (push ,cand ,newmatches)
2379 (incf ,item-count)
2380 (when (= ,item-count ,limit)
2381 (setq exit t)
2382 (return))))
2384 (defun anything-take-first-elements (seq n)
2385 (if (> (length seq) n)
2386 (setq seq (subseq seq 0 n))
2387 seq))
2389 (defun anything-match-from-candidates (cands matchfns limit)
2390 (let (matches)
2391 (condition-case nil
2392 (let ((item-count 0) exit)
2393 (clrhash anything-match-hash)
2394 (dolist (match matchfns)
2395 (let (newmatches)
2396 (dolist (candidate cands)
2397 (when (funcall match (anything-candidate-get-display candidate))
2398 (anything-accumulate-candidates-internal
2399 candidate newmatches anything-match-hash item-count limit)))
2400 (setq matches (append matches (reverse newmatches)))
2401 (if exit (return)))))
2402 (invalid-regexp (setq matches nil)))
2403 matches))
2405 (defun anything-compute-matches-internal (source)
2406 (save-current-buffer
2407 (let ((matchfns (anything-match-functions source))
2408 (anything-source-name (assoc-default 'name source))
2409 (limit (anything-candidate-number-limit source))
2410 (anything-pattern (anything-process-pattern-transformer
2411 anything-pattern source)))
2412 (anything-process-filtered-candidate-transformer
2413 (if (or (equal anything-pattern "") (equal matchfns '(identity)))
2414 (anything-take-first-elements
2415 (anything-get-cached-candidates source) limit)
2416 (anything-match-from-candidates
2417 (anything-get-cached-candidates source) matchfns limit))
2418 source))))
2420 ;; (anything '(((name . "error")(candidates . (lambda () (hage))) (action . identity))))
2422 (defun anything-process-source (source)
2423 "Display matched results from SOURCE according to its settings."
2424 (anything-log-eval (assoc-default 'name source))
2425 (if (assq 'direct-insert-match source) ;experimental
2426 (anything-process-source--direct-insert-match source)
2427 (let ((matches (anything-compute-matches source)))
2428 (when matches
2429 (when anything-test-mode
2430 (setq anything-test-candidate-list
2431 `(,@anything-test-candidate-list
2432 (,(assoc-default 'name source)
2433 ,matches))))
2434 (anything-insert-header-from-source source)
2435 (if (not (assq 'multiline source))
2436 (mapc 'anything-insert-match-with-digit-overlay matches)
2437 (let ((start (point)) separate)
2438 (dolist (match matches)
2439 (if separate
2440 (anything-insert-candidate-separator)
2441 (setq separate t))
2442 (anything-insert-match-with-digit-overlay match))
2443 (put-text-property start (point) 'anything-multiline t)))))))
2445 (defun anything-insert-match-with-digit-overlay (match)
2446 (declare (special source))
2447 (anything-put-digit-overlay-maybe)
2448 (anything-insert-match match 'insert source))
2450 (defun anything-put-digit-overlay-maybe ()
2451 (when (and anything-enable-shortcuts
2452 (not (eq anything-digit-shortcut-count
2453 (length anything-digit-overlays))))
2454 (move-overlay (nth anything-digit-shortcut-count
2455 anything-digit-overlays)
2456 (point-at-bol)
2457 (point-at-bol))
2458 (incf anything-digit-shortcut-count)))
2460 (defun anything-process-source--direct-insert-match (source)
2461 "[EXPERIMENTAL] Insert candidates from `anything-candidate-buffer' in SOURCE."
2462 (anything-log-eval (assoc-default 'name source))
2463 (let ((anything-source-name (assoc-default 'name source))
2464 content-buf)
2465 (funcall (assoc-default 'candidates source))
2466 (setq content-buf (anything-candidate-buffer))
2467 (unless (anything-empty-buffer-p content-buf)
2468 (anything-insert-header-from-source source)
2469 (insert-buffer-substring content-buf)
2470 ;; TODO call anything-put-digit-overlay-maybe with loop
2473 (defun anything-process-delayed-sources (delayed-sources &optional preselect)
2474 "Process anything DELAYED-SOURCES.
2475 Move selection to string or regexp PRESELECT if non--nil.
2476 This function is called in `anything-process-delayed-sources-timer'
2477 when emacs is idle for `anything-idle-delay'."
2478 (with-anything-quittable
2479 (anything-log-eval (mapcar (lambda (s) (assoc-default 'name s)) delayed-sources))
2480 (with-current-buffer anything-buffer
2481 (save-excursion
2482 (goto-char (point-max))
2483 (mapc 'anything-process-source delayed-sources)
2484 (when (and (not (anything-empty-buffer-p))
2485 ;; No selection yet.
2486 (= (overlay-start anything-selection-overlay)
2487 (overlay-end anything-selection-overlay)))
2488 (anything-update-move-first-line 'without-hook)))
2489 (when preselect (anything-preselect preselect))
2490 (save-excursion
2491 (goto-char (point-min))
2492 (anything-log-run-hook 'anything-update-hook))
2493 (anything-log-run-hook 'anything-after-update-hook))))
2496 ;; (@* "Core: *anything* buffer contents")
2497 (defvar anything-input-local nil)
2498 (defvar anything-process-delayed-sources-timer nil)
2499 (defun anything-update (&optional preselect)
2500 "Update candidates list in `anything-buffer' according to `anything-pattern'.
2501 Argument PRESELECT is a string or regexp used to move selection to a particular
2502 place once updating is done. It should be used on single source because search
2503 is done on whole `anything-buffer' and not on current source."
2504 (anything-log "start update")
2505 (setq anything-digit-shortcut-count 0)
2506 (anything-kill-async-processes)
2507 (with-current-buffer (anything-buffer-get)
2508 (set (make-local-variable 'anything-input-local) anything-pattern)
2509 (erase-buffer)
2510 (when anything-enable-shortcuts
2511 (mapc 'delete-overlay anything-digit-overlays))
2512 (let (delayed-sources
2513 normal-sources)
2514 (unwind-protect ; Process normal sources and store delayed one's.
2515 (setq delayed-sources
2516 (loop for source in (remove-if-not 'anything-update-source-p
2517 (anything-get-sources))
2518 if (anything-delayed-source-p source)
2519 collect source
2520 else do (progn (push source normal-sources)
2521 (anything-process-source source))))
2522 (anything-log-eval
2523 (mapcar (lambda (s) (assoc-default 'name s)) delayed-sources))
2524 (if anything-test-mode ; Need only to process sources.
2525 (mapc 'anything-process-source delayed-sources)
2526 (cond ((and preselect delayed-sources normal-sources)
2527 ;; Preselection run here when there is
2528 ;; normal AND delayed sources.
2529 (anything-log "Update preselect candidate %s" preselect)
2530 (anything-preselect preselect))
2531 (delayed-sources ; Preselection and hooks will run later.
2532 (anything-update-move-first-line 'without-hook))
2533 (t ; No delayed sources, run the hooks now.
2534 (anything-update-move-first-line)
2535 (anything-log-run-hook 'anything-after-update-hook)
2536 (when preselect
2537 (anything-log "Update preselect candidate %s" preselect)
2538 (anything-preselect preselect))))
2539 (when delayed-sources
2540 (anything-new-timer
2541 'anything-process-delayed-sources-timer
2542 (run-with-idle-timer
2543 ;; Be sure anything-idle-delay is >
2544 ;; to anything-input-idle-delay
2545 ;; otherwise use value of anything-input-idle-delay
2546 ;; or 0.1 if == to 0.
2547 (max anything-idle-delay anything-input-idle-delay 0.1) nil
2548 'anything-process-delayed-sources delayed-sources preselect))))
2549 (anything-log "end update")))))
2551 (defun anything-update-source-p (source)
2552 "Wheter SOURCE need updating or not."
2553 (and (or (not anything-source-filter)
2554 (member (assoc-default 'name source) anything-source-filter))
2555 (>= (length anything-pattern)
2556 (anything-aif (assoc 'requires-pattern source)
2557 (or (cdr it) 1)
2558 0))))
2560 (defun anything-delayed-source-p (source)
2561 "Wheter SOURCE is a delayed source or not."
2562 (or (assoc 'delayed source)
2563 (and anything-quick-update
2564 (< (window-height (get-buffer-window (current-buffer)))
2565 (line-number-at-pos (point-max))))))
2567 (defun anything-update-move-first-line (&optional without-hook)
2568 "Goto first line of `anything-buffer'."
2569 (goto-char (point-min))
2570 (unless without-hook
2571 (save-excursion (anything-log-run-hook 'anything-update-hook)))
2572 (anything-next-line))
2574 (defun anything-force-update (&optional preselect)
2575 "Force recalculation and update of candidates.
2576 If current source has `update' attribute, a function without argument,
2577 call it before update."
2578 (interactive)
2579 (let ((source (anything-get-current-source))
2580 (selection (anything-get-selection nil t)))
2581 (when source
2582 (mapc 'anything-force-update--reinit
2583 (anything-get-sources)))
2584 (anything-update preselect)
2585 ;; If preselect arg exists, `anything-update' should
2586 ;; have moved to selection, otherwise do it now.
2587 (unless preselect
2588 (anything-keep-selection (assoc-default 'name source) selection))
2589 (with-anything-window (recenter))))
2591 (defun anything-force-update--reinit (source)
2592 "Reinit SOURCE by calling his update and/or init functions."
2593 (anything-aif (anything-funcall-with-source
2594 source 'anything-candidate-buffer)
2595 (kill-buffer it))
2596 (dolist (attr '(update init))
2597 (anything-aif (assoc-default attr source)
2598 (anything-funcall-with-source source it)))
2599 (anything-remove-candidate-cache source))
2601 (defun anything-keep-selection (source selection)
2602 "Switch to SOURCE and goto SELECTION."
2603 (when (and source selection)
2604 (with-anything-window
2605 (anything-goto-source source)
2606 (forward-char -1)
2607 (if (search-forward selection nil t)
2608 (forward-line 0)
2609 (goto-char (point-min))
2610 (forward-line 1))
2611 (anything-mark-current-line))))
2613 (defun anything-remove-candidate-cache (source)
2614 "Remove SOURCE from `anything-candidate-cache'."
2615 (setq anything-candidate-cache
2616 (delete (assoc (assoc-default 'name source)
2617 anything-candidate-cache)
2618 anything-candidate-cache)))
2620 (defun anything-insert-match (match insert-function source)
2621 "Insert MATCH into `anything-buffer' with INSERT-FUNCTION for SOURCE.
2622 If MATCH is a list then insert the string intended to appear on the display
2623 and store the real value in a text property."
2624 (let ((start (point-at-bol (point)))
2625 (dispvalue (or (car-safe match) match))
2626 (realvalue (cdr-safe match)))
2627 (setq dispvalue
2628 (cond ((symbolp dispvalue) (symbol-name dispvalue))
2629 ((numberp dispvalue) (number-to-string dispvalue))
2630 (t dispvalue)))
2631 (when (stringp dispvalue)
2632 (funcall insert-function dispvalue)
2633 ;; Some sources with candidates-in-buffer have already added
2634 ;; 'anything-realvalue property when creating candidate buffer.
2635 (unless (get-text-property start 'anything-realvalue)
2636 (and realvalue
2637 (put-text-property start (point-at-eol)
2638 'anything-realvalue realvalue)))
2639 (when anything-source-in-each-line-flag
2640 (put-text-property start (point-at-eol) 'anything-source source))
2641 (funcall insert-function "\n"))))
2643 (defun anything-insert-header-from-source (source)
2644 "Insert SOURCE name in `anything-buffer' header.
2645 Maybe insert by overlay additional info after source name if SOURCE have
2646 header-name attribute."
2647 (let ((name (assoc-default 'name source)))
2648 (anything-insert-header
2649 name
2650 (anything-aif (assoc-default 'header-name source)
2651 (anything-funcall-with-source source it name)))))
2653 (defun anything-insert-header (name &optional display-string)
2654 "Insert header of source NAME into the anything buffer.
2655 If DISPLAY-STRING is non--nil and a string, display this additional info
2656 after the source name by overlay."
2657 (unless (bobp)
2658 (let ((start (point)))
2659 (insert "\n")
2660 (put-text-property start (point) 'anything-header-separator t)))
2661 (let ((start (point)))
2662 (insert name)
2663 (put-text-property (point-at-bol)
2664 (point-at-eol) 'anything-header t)
2665 (when display-string
2666 (overlay-put (make-overlay (point-at-bol) (point-at-eol))
2667 'display display-string))
2668 (insert "\n")
2669 (put-text-property start (point) 'face anything-header-face)))
2671 (defun anything-insert-candidate-separator ()
2672 "Insert separator of candidates into the anything buffer."
2673 (insert anything-candidate-separator)
2674 (put-text-property (point-at-bol)
2675 (point-at-eol) 'anything-candidate-separator t)
2676 (insert "\n"))
2679 ;; (@* "Core: async process")
2680 (defun anything-output-filter (process string)
2681 "From PROCESS process output STRING."
2682 (anything-output-filter-1 (assoc process anything-async-processes) string))
2684 (defun anything-output-filter-1 (process-assoc string)
2685 (anything-log-eval string)
2686 (with-current-buffer anything-buffer
2687 (let ((source (cdr process-assoc)))
2688 (save-excursion
2689 (anything-aif (assoc-default 'insertion-marker source)
2690 (goto-char it)
2691 (goto-char (point-max))
2692 (anything-insert-header-from-source source)
2693 (setcdr process-assoc
2694 (append source `((insertion-marker . ,(point-marker))))))
2695 (anything-output-filter--process-source
2696 (car process-assoc) string source
2697 (anything-candidate-number-limit source))))
2698 (anything-output-filter--post-process)))
2700 (defun anything-output-filter--process-source (process string source limit)
2701 (dolist (candidate (anything-transform-candidates
2702 (anything-output-filter--collect-candidates
2703 (split-string string "\n")
2704 (assoc 'incomplete-line source))
2705 source t))
2706 (if (not (assq 'multiline source))
2707 (anything-insert-match candidate 'insert-before-markers source)
2708 (let ((start (point)))
2709 (anything-insert-candidate-separator)
2710 (anything-insert-match candidate 'insert-before-markers source)
2711 (put-text-property start (point) 'anything-multiline t)))
2712 (incf (cdr (assoc 'item-count source)))
2713 (when (>= (assoc-default 'item-count source) limit)
2714 (anything-kill-async-process process)
2715 (return))))
2717 (defun anything-output-filter--collect-candidates (lines incomplete-line-info)
2718 (anything-log-eval (cdr incomplete-line-info))
2719 (butlast
2720 (loop for line in lines collect
2721 (if (cdr incomplete-line-info)
2722 (prog1
2723 (concat (cdr incomplete-line-info) line)
2724 (setcdr incomplete-line-info nil))
2725 line)
2726 finally (setcdr incomplete-line-info line))))
2728 (defun anything-output-filter--post-process ()
2729 (anything-log-run-hook 'anything-update-hook)
2730 (anything-aif (get-buffer-window anything-buffer 'visible)
2731 (save-selected-window
2732 (select-window it)
2733 (anything-skip-noncandidate-line 'next)
2734 (anything-mark-current-line))))
2736 (defun anything-kill-async-processes ()
2737 "Kill all known asynchronous processes of `anything-async-processes'."
2738 (mapc 'anything-kill-async-process (mapcar 'car anything-async-processes))
2739 (setq anything-async-processes nil))
2741 (defun anything-kill-async-process (process)
2742 "Kill PROCESS and detach the associated functions."
2743 (set-process-filter process nil)
2744 (delete-process process))
2747 ;; (@* "Core: action")
2748 (defun anything-execute-selection-action (&optional
2749 selection action
2750 preserve-saved-action)
2751 "If a candidate SELECTION is present then perform the associated ACTION on it.
2752 If PRESERVE-SAVED-ACTION is non-nil don't save action."
2753 (anything-log "executing action")
2754 (setq action (anything-get-default-action
2755 (or action
2756 anything-saved-action
2757 (if (get-buffer anything-action-buffer)
2758 (anything-get-selection anything-action-buffer)
2759 (anything-get-action)))))
2760 (let ((source (or anything-saved-current-source
2761 (anything-get-current-source))))
2762 (setq selection (or selection
2763 (anything-get-selection)
2764 (and (assoc 'accept-empty source) "")))
2765 (unless preserve-saved-action (setq anything-saved-action nil))
2766 (if (and selection action)
2767 (anything-funcall-with-source
2768 source action
2769 (anything-coerce-selection selection source)))))
2771 (defun anything-coerce-selection (selection source)
2772 "Apply coerce attribute function to SELECTION in SOURCE.
2773 Coerce source with coerce function."
2774 (anything-aif (assoc-default 'coerce source)
2775 (anything-funcall-with-source source it selection)
2776 selection))
2778 (defun anything-get-default-action (action)
2779 "Get the first ACTION value of action list in source."
2780 (if (and (listp action) (not (functionp action)))
2781 (cdar action)
2782 action))
2784 (defun anything-select-action ()
2785 "Select an action for the currently selected candidate.
2786 If action buffer is selected, back to the anything buffer."
2787 (interactive)
2788 (anything-log-run-hook 'anything-select-action-hook)
2789 (cond ((get-buffer-window anything-action-buffer 'visible)
2790 (set-window-buffer (get-buffer-window anything-action-buffer)
2791 anything-buffer)
2792 (kill-buffer anything-action-buffer)
2793 (anything-set-pattern anything-input 'noupdate))
2795 (setq anything-saved-selection (anything-get-selection))
2796 (unless anything-saved-selection
2797 (error "Nothing is selected"))
2798 (setq anything-saved-current-source (anything-get-current-source))
2799 (let ((actions (anything-get-action)))
2800 (if (functionp actions)
2801 (message "Sole action: %s" actions)
2802 (anything-show-action-buffer actions)
2803 (anything-delete-minibuffer-contents)
2804 (setq anything-pattern 'dummy) ; so that it differs from the previous one
2805 (anything-check-minibuffer-input))))))
2807 (defun anything-show-action-buffer (actions)
2808 (with-current-buffer (get-buffer-create anything-action-buffer)
2809 (erase-buffer)
2810 (buffer-disable-undo)
2811 (set-window-buffer (get-buffer-window anything-buffer) anything-action-buffer)
2812 (set (make-local-variable 'anything-sources)
2813 `(((name . "Actions")
2814 (volatile)
2815 (candidates . ,actions)
2816 (candidate-number-limit))))
2817 (set (make-local-variable 'anything-source-filter) nil)
2818 (set (make-local-variable 'anything-selection-overlay) nil)
2819 (set (make-local-variable 'anything-digit-overlays) nil)
2820 (anything-initialize-overlays anything-action-buffer)))
2823 ;; (@* "Core: selection")
2824 (defun anything-move-selection-common (move-func unit direction)
2825 "Move the selection marker to a new position wit function MOVE-FUNC.
2826 It is determined by UNIT and DIRECTION."
2827 (unless (or (anything-empty-buffer-p (anything-buffer-get))
2828 (not (anything-window)))
2829 (with-anything-window
2830 (anything-log-run-hook 'anything-move-selection-before-hook)
2831 (funcall move-func)
2832 (anything-skip-noncandidate-line direction)
2833 (anything-display-source-at-screen-top-maybe unit)
2834 (when (anything-get-previous-header-pos)
2835 (anything-mark-current-line))
2836 (anything-display-mode-line (anything-get-current-source))
2837 (anything-log-run-hook 'anything-move-selection-after-hook))))
2839 (defun anything-display-source-at-screen-top-maybe (unit)
2840 (when (and anything-display-source-at-screen-top (eq unit 'source))
2841 (set-window-start (selected-window)
2842 (save-excursion (forward-line -1) (point)))))
2844 (defun anything-skip-noncandidate-line (direction)
2845 (anything-skip-header-and-separator-line direction)
2846 (and (bobp) (forward-line 1)) ;skip first header
2847 (and (eobp) (forward-line -1))) ;avoid last empty line
2850 (defun anything-skip-header-and-separator-line (direction)
2851 (while (and (not (bobp))
2852 (or (anything-pos-header-line-p)
2853 (anything-pos-candidate-separator-p)))
2854 (forward-line (if (and (eq direction 'previous)
2855 (not (eq (point-at-bol) (point-min))))
2856 -1 1))))
2858 (defvar anything-mode-line-string-real nil)
2859 (defun anything-display-mode-line (source)
2860 (set (make-local-variable 'anything-mode-line-string)
2861 (anything-interpret-value (or (assoc-default 'mode-line source)
2862 (default-value 'anything-mode-line-string))
2863 source))
2864 (if anything-mode-line-string
2865 (setq mode-line-format
2866 '(" " mode-line-buffer-identification " "
2867 (line-number-mode "L%l") " " (anything-follow-mode "(F) ")
2868 (:eval (anything-show-candidate-number
2869 (when (listp anything-mode-line-string)
2870 (car anything-mode-line-string))))
2871 " " anything-mode-line-string-real "-%-")
2872 anything-mode-line-string-real
2873 (substitute-command-keys (if (listp anything-mode-line-string)
2874 (cadr anything-mode-line-string)
2875 anything-mode-line-string)))
2876 (setq mode-line-format
2877 (default-value 'mode-line-format)))
2878 (setq header-line-format
2879 (anything-interpret-value (assoc-default 'header-line source) source)))
2881 (defun anything-show-candidate-number (&optional name)
2882 "Used to display candidate number in mode-line.
2883 You can specify NAME of candidates e.g \"Buffers\" otherwise
2884 it is \"Candidate\(s\)\" by default."
2885 (propertize
2886 (format "[%s %s]"
2887 (anything-approximate-candidate-number 'in-current-source)
2888 (or name "Candidate(s)"))
2889 'face 'anything-candidate-number))
2891 (defun anything-previous-line ()
2892 "Move selection to the previous line."
2893 (interactive)
2894 (anything-move-selection-common
2895 (lambda ()
2896 (if (not (anything-pos-multiline-p))
2897 (forward-line -1) ;double forward-line is meaningful
2898 (forward-line -1) ;because evaluation order is important
2899 (anything-skip-header-and-separator-line 'previous)
2900 (let ((header-pos (anything-get-previous-header-pos))
2901 (separator-pos (anything-get-previous-candidate-separator-pos)))
2902 (when header-pos
2903 (goto-char (if (or (null separator-pos) (< separator-pos header-pos))
2904 header-pos ; first candidate
2905 separator-pos))
2906 (forward-line 1)))))
2907 'line 'previous))
2909 (defun anything-next-line ()
2910 "Move selection to the next line."
2911 (interactive)
2912 (anything-move-selection-common
2913 (lambda ()
2914 (if (not (anything-pos-multiline-p))
2915 (forward-line 1)
2916 (let ((header-pos (anything-get-next-header-pos))
2917 (separator-pos (anything-get-next-candidate-separator-pos)))
2918 (cond ((and separator-pos
2919 (or (null header-pos) (< separator-pos header-pos)))
2920 (goto-char separator-pos))
2921 (header-pos
2922 (goto-char header-pos))))))
2923 'line 'next))
2925 (defun anything-previous-page ()
2926 "Move selection back with a pageful."
2927 (interactive)
2928 (anything-move-selection-common
2929 (lambda ()
2930 (condition-case nil
2931 (scroll-down)
2932 (beginning-of-buffer (goto-char (point-min)))))
2933 'page 'previous))
2935 (defun anything-next-page ()
2936 "Move selection forward with a pageful."
2937 (interactive)
2938 (anything-move-selection-common
2939 (lambda ()
2940 (condition-case nil
2941 (scroll-up)
2942 (end-of-buffer (goto-char (point-max)))))
2943 'page 'next))
2945 (defun anything-beginning-of-buffer ()
2946 "Move selection at the top."
2947 (interactive)
2948 (anything-move-selection-common
2949 (lambda () (goto-char (point-min)))
2950 'edge 'previous))
2952 (defun anything-end-of-buffer ()
2953 "Move selection at the bottom."
2954 (interactive)
2955 (anything-move-selection-common
2956 (lambda () (goto-char (point-max)))
2957 'edge 'next))
2959 (defun anything-previous-source ()
2960 "Move selection to the previous source."
2961 (interactive)
2962 (anything-move-selection-common
2963 (lambda ()
2964 (forward-line -1)
2965 (if (bobp)
2966 (goto-char (point-max))
2967 (anything-skip-header-and-separator-line 'previous))
2968 (goto-char (anything-get-previous-header-pos))
2969 (forward-line 1))
2970 'source 'previous))
2972 (defun anything-next-source ()
2973 "Move selection to the next source."
2974 (interactive)
2975 (anything-move-selection-common
2976 (lambda ()
2977 (goto-char (or (anything-get-next-header-pos) (point-min))))
2978 'source 'next))
2980 (defun anything-goto-source (source-or-name)
2981 "Move the selection to the source SOURCE-OR-NAME."
2982 (anything-move-selection-common
2983 (lambda ()
2984 (goto-char (point-min))
2985 (let ((name (if (stringp source-or-name) source-or-name
2986 (assoc-default 'name source-or-name))))
2987 (condition-case err
2988 (while (not (string= name (anything-current-line-contents)))
2989 (goto-char (anything-get-next-header-pos)))
2990 (error (message "")))))
2991 'source 'next))
2993 (defun anything-mark-current-line (&optional resumep)
2994 "Move `anything-selection-overlay' to current line.
2995 Note that this is not related with visibles marks, which are used
2996 to mark candidates."
2997 (with-anything-window
2998 (when resumep
2999 (goto-char anything-selection-point))
3000 (move-overlay
3001 anything-selection-overlay (point-at-bol)
3002 (if (anything-pos-multiline-p)
3003 (let ((header-pos (anything-get-next-header-pos))
3004 (separator-pos (anything-get-next-candidate-separator-pos)))
3005 (or (and (null header-pos) separator-pos)
3006 (and header-pos separator-pos (< separator-pos header-pos)
3007 separator-pos)
3008 header-pos
3009 (point-max)))
3010 (1+ (point-at-eol))))
3011 (setq anything-selection-point (overlay-start anything-selection-overlay)))
3012 (anything-follow-execute-persistent-action-maybe))
3014 (defun anything-this-command-key ()
3015 (event-basic-type (elt (this-command-keys-vector) 0)))
3016 ;; (progn (read-key-sequence "Key: ") (p (anything-this-command-key)))
3018 (defun anything-select-with-shortcut-internal (types get-key-func)
3019 (if (memq anything-enable-shortcuts types)
3020 (save-selected-window
3021 (select-window (anything-window))
3022 (let* ((key (funcall get-key-func))
3023 (overlay (ignore-errors (nth (position key anything-shortcut-keys)
3024 anything-digit-overlays))))
3025 (if (not (and overlay (overlay-buffer overlay)))
3026 (when (numberp key)
3027 (select-window (minibuffer-window))
3028 (self-insert-command 1))
3029 (goto-char (overlay-start overlay))
3030 (anything-mark-current-line)
3031 (anything-exit-minibuffer))))
3032 (self-insert-command 1)))
3034 (defun anything-select-with-prefix-shortcut ()
3035 "Invoke default action with prefix shortcut."
3036 (interactive)
3037 (anything-select-with-shortcut-internal
3038 '(prefix)
3039 (lambda () (read-event "Select shortcut key: "))))
3041 (defun anything-select-with-digit-shortcut ()
3042 "Invoke default action with digit/alphabet shortcut."
3043 (interactive)
3044 (anything-select-with-shortcut-internal
3045 '(alphabet t) 'anything-this-command-key))
3047 ;; (setq anything-enable-shortcuts 'prefix)
3048 ;; (define-key anything-map "@" 'anything-select-with-prefix-shortcut)
3049 ;; (define-key anything-map (kbd "<f18>") 'anything-select-with-prefix-shortcut)
3051 (defvar anything-exit-status 0
3052 "Flag to inform whether anything have exited or quitted.
3053 Exit with 0 mean anything have exited executing an action.
3054 Exit with 1 mean anything have quitted with \\[keyboard-quit]
3055 It is useful for example to restore a window config if anything abort
3056 in special cases.
3057 See `anything-exit-minibuffer' and `anything-keyboard-quit'.")
3059 (defvar anything-minibuffer-confirm-state nil)
3060 (defun anything-confirm-and-exit-minibuffer ()
3061 "Maybe ask for confirmation when exiting anything.
3062 It is similar to `minibuffer-complete-and-exit' adapted to anything.
3063 If `minibuffer-completion-confirm' value is 'confirm,
3064 send in minibuffer confirm message and exit on next hit.
3065 If `minibuffer-completion-confirm' value is t,
3066 don't exit and send message 'no match'."
3067 (interactive)
3068 (let ((empty-buffer-p (with-current-buffer anything-buffer
3069 (eq (point-min) (point-max))))
3070 (unknow (string= (get-text-property
3071 0 'display (anything-get-selection nil 'withprop))
3072 "[?]")))
3073 (cond ((and (or empty-buffer-p unknow)
3074 (eq minibuffer-completion-confirm 'confirm))
3075 (setq anything-minibuffer-confirm-state
3076 'confirm)
3077 (setq minibuffer-completion-confirm nil)
3078 (minibuffer-message " [confirm]"))
3079 ((and (or empty-buffer-p unknow)
3080 (eq minibuffer-completion-confirm t))
3081 (minibuffer-message " [No match]"))
3083 (setq anything-minibuffer-confirm-state nil)
3084 (anything-exit-minibuffer)))))
3085 (add-hook 'anything-after-update-hook 'anything-confirm-and-exit-hook)
3087 (defun anything-confirm-and-exit-hook ()
3088 "Restore `minibuffer-completion-confirm' when anything update."
3089 (unless (or (eq minibuffer-completion-confirm t)
3090 (not anything-minibuffer-confirm-state))
3091 (setq minibuffer-completion-confirm
3092 anything-minibuffer-confirm-state)))
3094 (defun anything-exit-minibuffer ()
3095 "Select the current candidate by exiting the minibuffer."
3096 (interactive)
3097 (unless anything-current-prefix-arg
3098 (setq anything-current-prefix-arg current-prefix-arg))
3099 (setq anything-exit-status 0)
3100 (exit-minibuffer))
3102 (defun anything-keyboard-quit ()
3103 "Quit minibuffer in anything.
3104 If action buffer is displayed, kill it."
3105 (interactive)
3106 (when (get-buffer-window anything-action-buffer 'visible)
3107 (kill-buffer anything-action-buffer))
3108 (setq anything-exit-status 1)
3109 (abort-recursive-edit))
3111 (defun anything-get-next-header-pos ()
3112 "Return the position of the next header from point."
3113 (next-single-property-change (point) 'anything-header))
3115 (defun anything-get-previous-header-pos ()
3116 "Return the position of the previous header from point."
3117 (previous-single-property-change (point) 'anything-header))
3119 (defun anything-pos-multiline-p ()
3120 "Return non-nil if the current position is in the multiline source region."
3121 (get-text-property (point) 'anything-multiline))
3123 (defun anything-get-next-candidate-separator-pos ()
3124 "Return the position of the next candidate separator from point."
3125 (next-single-property-change (point) 'anything-candidate-separator))
3127 (defun anything-get-previous-candidate-separator-pos ()
3128 "Return the position of the previous candidate separator from point."
3129 (previous-single-property-change (point) 'anything-candidate-separator))
3131 (defun anything-pos-header-line-p ()
3132 "Return t if the current line is a header line."
3133 (or (get-text-property (point-at-bol) 'anything-header)
3134 (get-text-property (point-at-bol) 'anything-header-separator)))
3136 (defun anything-pos-candidate-separator-p ()
3137 "Return t if the current line is a candidate separator."
3138 (get-text-property (point-at-bol) 'anything-candidate-separator))
3141 ;; (@* "Core: help")
3142 (defun anything-help-internal (bufname insert-content-fn)
3143 "Show long message during `anything' session in BUFNAME.
3144 INSERT-CONTENT-FN is the text to be displayed in BUFNAME."
3145 (save-window-excursion
3146 (select-window (anything-window))
3147 (delete-other-windows)
3148 (switch-to-buffer (get-buffer-create bufname))
3149 (erase-buffer)
3150 (funcall insert-content-fn)
3151 (setq mode-line-format "%b (SPC,C-v:NextPage b,M-v:PrevPage other:Exit)")
3152 (setq cursor-type nil)
3153 (goto-char 1)
3154 (anything-help-event-loop)))
3156 (defun anything-help-event-loop ()
3157 (ignore-errors
3158 (loop for event = (read-event) do
3159 (case event
3160 ((?\C-v ? ) (scroll-up))
3161 ((?\M-v ?b) (scroll-down))
3162 (t (return))))))
3164 (defun anything-help ()
3165 "Help of `anything'."
3166 (interactive)
3167 (anything-help-internal
3168 " *Anything Help*"
3169 (lambda ()
3170 (insert (substitute-command-keys
3171 (anything-interpret-value (or (assoc-default
3172 'help-message
3173 (anything-get-current-source))
3174 anything-help-message))))
3175 (org-mode))))
3177 (defun anything-debug-output ()
3178 "Show all anything-related variables at this time."
3179 (interactive)
3180 (anything-help-internal " *Anything Debug*" 'anything-debug-output-function))
3182 (defun anything-debug-output-function (&optional vars)
3183 (message "Calculating all anything-related values...")
3184 (insert "If you debug some variables or forms, set `anything-debug-forms'
3185 to a list of forms.\n\n")
3186 (dolist (v (or vars
3187 anything-debug-forms
3188 (apropos-internal "^anything-" 'boundp)))
3189 (insert "** "
3190 (pp-to-string v) "\n"
3191 (pp-to-string (with-current-buffer anything-buffer (eval v))) "\n"))
3192 (message "Calculating all anything-related values...Done"))
3195 ;; (@* "Core: misc")
3196 (defun anything-kill-buffer-hook ()
3197 "Remove tick entry from `anything-tick-hash' when killing a buffer."
3198 (loop for key being the hash-keys in anything-tick-hash
3199 if (string-match (format "^%s/" (regexp-quote (buffer-name))) key)
3200 do (remhash key anything-tick-hash)))
3201 (add-hook 'kill-buffer-hook 'anything-kill-buffer-hook)
3203 (defun anything-preselect (candidate-or-regexp)
3204 "Move `anything-selection-overlay' to CANDIDATE-OR-REGEXP on startup."
3205 (with-anything-window
3206 (when candidate-or-regexp
3207 (goto-char (point-min))
3208 ;; go to first candidate of first source
3209 (forward-line 1)
3210 (let ((start (point)))
3211 (or (re-search-forward
3212 (concat "^" (regexp-quote candidate-or-regexp) "$") nil t)
3213 (re-search-forward candidate-or-regexp nil t)
3214 (search-forward candidate-or-regexp nil t)
3215 (goto-char start))))
3216 (anything-mark-current-line)))
3218 (defun anything-delete-current-selection ()
3219 "Delete the currently selected item."
3220 (interactive)
3221 (with-anything-window
3222 (cond ((anything-pos-multiline-p)
3223 (anything-aif (anything-get-next-candidate-separator-pos)
3224 (delete-region (point-at-bol)
3225 (1+ (progn (goto-char it) (point-at-eol))))
3226 ;; last candidate
3227 (goto-char (anything-get-previous-candidate-separator-pos))
3228 (delete-region (point-at-bol) (point-max)))
3229 (when (anything-end-of-source-p)
3230 (goto-char (or (anything-get-previous-candidate-separator-pos)
3231 (point-min)))
3232 (forward-line 1)))
3234 (delete-region (point-at-bol) (1+ (point-at-eol)))
3235 (when (anything-end-of-source-p) (forward-line -1))))
3236 (anything-mark-current-line)))
3238 (defun anything-end-of-source-p ()
3239 "Return non--nil if we are at eob or end of source."
3240 (save-excursion
3241 (forward-line 1)
3242 (or (eq (point-at-bol) (point-at-eol))
3243 (anything-pos-header-line-p)
3244 (eobp))))
3246 (defun anything-edit-current-selection-internal (func)
3247 (with-anything-window
3248 (beginning-of-line)
3249 (let ((realvalue (get-text-property (point) 'anything-realvalue)))
3250 (funcall func)
3251 (beginning-of-line)
3252 (and realvalue
3253 (put-text-property (point) (point-at-eol)
3254 'anything-realvalue realvalue))
3255 (anything-mark-current-line))))
3257 (defmacro anything-edit-current-selection (&rest forms)
3258 "Evaluate FORMS at current selection in the anything buffer.
3259 You can edit the line."
3260 (declare (indent 0) (debug t))
3261 `(anything-edit-current-selection-internal
3262 (lambda () ,@forms)))
3264 (defun anything-set-pattern (pattern &optional noupdate)
3265 "Set minibuffer contents to PATTERN.
3266 if optional NOUPDATE is non-nil, anything buffer is not changed."
3267 (with-selected-window (or (active-minibuffer-window) (minibuffer-window))
3268 (delete-minibuffer-contents)
3269 (insert pattern))
3270 (when noupdate
3271 (setq anything-pattern pattern)
3272 (anything-hooks 'cleanup)
3273 (run-with-idle-timer 0 nil 'anything-hooks 'setup)))
3275 (defun anything-delete-minibuffer-contents ()
3276 "Same as `delete-minibuffer-contents' but this is a command."
3277 (interactive)
3278 (anything-set-pattern ""))
3279 (defalias 'anything-delete-minibuffer-content 'anything-delete-minibuffer-contents)
3282 ;;; Plugins
3284 ;; (@* "Built-in plug-in: type")
3285 (defun anything-compile-source--type (source)
3286 (anything-aif (assoc-default 'type source)
3287 (append source (assoc-default it anything-type-attributes) nil)
3288 source))
3290 ;; `define-anything-type-attribute' is public API.
3292 (defun anything-add-type-attribute (type definition)
3293 (anything-aif (assq type anything-type-attributes)
3294 (setq anything-type-attributes (delete it anything-type-attributes)))
3295 (push (cons type definition) anything-type-attributes))
3297 (defvar anything-types nil)
3298 (defun anything-document-type-attribute (type doc)
3299 (add-to-list 'anything-types type t)
3300 (put type 'anything-typeattrdoc
3301 (concat "- " (symbol-name type) "\n\n" doc "\n")))
3303 (defadvice documentation-property (after anything-document-type-attribute activate)
3304 "Display type attributes' documentation as `anything-type-attributes' docstring."
3305 (when (eq (ad-get-arg 0) 'anything-type-attributes)
3306 (setq ad-return-value
3307 (concat ad-return-value "\n\n++++ Types currently defined ++++\n"
3308 (mapconcat (lambda (sym) (get sym 'anything-typeattrdoc))
3309 anything-types "\n")))))
3311 ;; (@* "Built-in plug-in: dummy")
3312 (defun anything-dummy-candidate (candidate source)
3313 "Use `anything-pattern' as CANDIDATE in SOURCE."
3314 ;; `source' is defined in filtered-candidate-transformer
3315 (list anything-pattern))
3317 (defun anything-compile-source--dummy (source)
3318 (if (assoc 'dummy source)
3319 (append source
3320 '((candidates "dummy")
3321 (accept-empty)
3322 (match identity)
3323 (filtered-candidate-transformer . anything-dummy-candidate)
3324 (disable-shortcuts)
3325 (volatile)))
3326 source))
3328 ;; (@* "Built-in plug-in: disable-shortcuts")
3329 (defvar anything-orig-enable-shortcuts nil)
3330 (defun anything-save-enable-shortcuts ()
3331 (anything-once
3332 (lambda ()
3333 (setq anything-orig-enable-shortcuts anything-enable-shortcuts
3334 anything-enable-shortcuts nil))))
3336 (defun anything-compile-source--disable-shortcuts (source)
3337 (if (assoc 'disable-shortcuts source)
3338 (append `((init ,@(anything-mklist (assoc-default 'init source))
3339 anything-save-enable-shortcuts)
3340 (resume ,@(anything-mklist (assoc-default 'resume source))
3341 anything-save-enable-shortcuts)
3342 (cleanup ,@(anything-mklist (assoc-default 'cleanup source))
3343 (lambda () (setq anything-enable-shortcuts
3344 anything-orig-enable-shortcuts))))
3345 source)
3346 source))
3348 ;; (@* "Built-in plug-in: candidates-in-buffer")
3349 (defun anything-candidates-in-buffer ()
3350 "Get candidates from the candidates buffer according to `anything-pattern'.
3352 BUFFER is `anything-candidate-buffer' by default. Each
3353 candidate must be placed in one line. This function is meant to
3354 be used in candidates-in-buffer or candidates attribute of an
3355 anything source. Especially fast for many (1000+) candidates.
3358 '((name . \"many files\")
3359 (init . (lambda () (with-current-buffer (anything-candidate-buffer 'local)
3360 (insert-many-filenames))))
3361 (search re-search-forward) ; optional
3362 (candidates-in-buffer)
3363 (type . file))
3365 +===============================================================+
3366 | The new way of making and narrowing candidates: Using buffers |
3367 +===============================================================+
3369 By default, `anything' makes candidates by evaluating the
3370 candidates function, then narrows them by `string-match' for each
3371 candidate.
3373 But this way is very slow for many candidates. The new way is
3374 storing all candidates in a buffer and narrowing them by
3375 `re-search-forward'. Search function is customizable by search
3376 attribute. The important point is that buffer processing is MUCH
3377 FASTER than string list processing and is the Emacs way.
3379 The init function writes all candidates to a newly-created
3380 candidate buffer. The candidates buffer is created or specified
3381 by `anything-candidate-buffer'. Candidates are stored in a line.
3383 The candidates function narrows all candidates, IOW creates a
3384 subset of candidates dynamically. It is the task of
3385 `anything-candidates-in-buffer'. As long as
3386 `anything-candidate-buffer' is used,`(candidates-in-buffer)' is
3387 sufficient in most cases.
3389 Note that `(candidates-in-buffer)' is shortcut of three attributes:
3390 (candidates . anything-candidates-in-buffer)
3391 (volatile)
3392 (match identity)
3393 And `(candidates-in-buffer . func)' is shortcut of three attributes:
3394 (candidates . func)
3395 (volatile)
3396 (match identity)
3397 The expansion is performed in `anything-get-sources'.
3399 The candidates-in-buffer attribute implies the volatile attribute.
3400 The volatile attribute is needed because `anything-candidates-in-buffer'
3401 creates candidates dynamically and need to be called everytime
3402 `anything-pattern' changes.
3404 Because `anything-candidates-in-buffer' plays the role of `match' attribute
3405 function, specifying `(match identity)' makes the source slightly faster.
3407 To customize `anything-candidates-in-buffer' behavior, use search,
3408 get-line and search-from-end attributes. See also `anything-sources' docstring."
3409 (declare (special source))
3410 (anything-candidates-in-buffer-1
3411 (anything-candidate-buffer)
3412 anything-pattern
3413 (or (assoc-default 'get-line source)
3414 #'buffer-substring-no-properties)
3415 ;; use external variable `source'.
3416 (or (assoc-default 'search source)
3417 (if (assoc 'search-from-end source)
3418 '(anything-candidates-in-buffer-search-from-end)
3419 '(anything-candidates-in-buffer-search-from-start)))
3420 (anything-candidate-number-limit source)
3421 (assoc 'search-from-end source)))
3423 (defun anything-candidates-in-buffer-search-from-start (pattern)
3424 "Search PATTERN with `re-search-forward' with bound and noerror args."
3425 (re-search-forward pattern nil t))
3427 (defun anything-candidates-in-buffer-search-from-end (pattern)
3428 "Search PATTERN with `re-search-backward' with bound and noerror args."
3429 (re-search-backward pattern nil t))
3431 (defun anything-candidates-in-buffer-1 (buffer pattern get-line-fn
3432 search-fns limit search-from-end)
3433 ;; buffer == nil when candidates buffer does not exist.
3434 (when buffer
3435 (with-current-buffer buffer
3436 (let ((start-point (if search-from-end (point-max) (point-min)))
3437 (endp (if search-from-end #'bobp #'eobp)))
3438 (goto-char (1- start-point))
3439 (if (string= pattern "")
3440 (anything-initial-candidates-from-candidate-buffer
3441 endp get-line-fn limit search-from-end)
3442 (anything-search-from-candidate-buffer
3443 pattern get-line-fn search-fns limit search-from-end
3444 start-point endp))))))
3446 (defun anything-point-is-moved (proc)
3447 "If point is moved after executing PROC, return t, otherwise nil."
3448 (/= (point) (progn (funcall proc) (point))))
3450 (defun anything-search-from-candidate-buffer (pattern get-line-fn search-fns
3451 limit search-from-end
3452 start-point endp)
3453 (let (buffer-read-only
3454 matches exit newmatches)
3455 (anything-search-from-candidate-buffer-internal
3456 (lambda ()
3457 (clrhash anything-cib-hash)
3458 (dolist (searcher search-fns)
3459 (goto-char start-point)
3460 (setq newmatches nil)
3461 (loop with item-count = 0
3462 while (funcall searcher pattern)
3463 for cand = (funcall get-line-fn (point-at-bol) (point-at-eol))
3464 do (anything-accumulate-candidates-internal
3465 cand newmatches anything-cib-hash item-count limit)
3466 unless (anything-point-is-moved
3467 (lambda ()
3468 (if search-from-end
3469 (goto-char (1- (point-at-bol)))
3470 (forward-line 1))))
3471 return nil)
3472 (setq matches (append matches (nreverse newmatches)))
3473 (if exit (return)))
3474 (delq nil matches)))))
3476 (defun anything-initial-candidates-from-candidate-buffer (endp get-line-fn limit search-from-end)
3477 (delq nil (loop with next-line-fn =
3478 (if search-from-end
3479 (lambda (x) (goto-char (max (1- (point-at-bol)) 1)))
3480 #'forward-line)
3481 until (funcall endp)
3482 for i from 1 to limit
3483 collect (funcall get-line-fn (point-at-bol) (point-at-eol))
3484 do (funcall next-line-fn 1))))
3486 (defun anything-search-from-candidate-buffer-internal (search-fn)
3487 (goto-char (point-min))
3488 (insert "\n")
3489 (goto-char (point-max))
3490 (insert "\n")
3491 (unwind-protect
3492 (funcall search-fn)
3493 (goto-char (point-min))
3494 (delete-char 1)
3495 (goto-char (1- (point-max)))
3496 (delete-char 1)
3498 (set-buffer-modified-p nil)))
3500 (defun anything-candidate-buffer (&optional create-or-buffer)
3501 "Register and return a buffer containing candidates of current source.
3502 `anything-candidate-buffer' searches buffer-local candidates buffer first,
3503 then global candidates buffer.
3505 Acceptable values of CREATE-OR-BUFFER:
3507 - nil (omit)
3508 Only return the candidates buffer.
3509 - a buffer
3510 Register a buffer as a candidates buffer.
3511 - 'global
3512 Create a new global candidates buffer,
3513 named \" *anything candidates:SOURCE*\".
3514 - other non-nil value
3515 Create a new local candidates buffer,
3516 named \" *anything candidates:SOURCE*ANYTHING-CURRENT-BUFFER\"."
3517 (let* ((global-bname (format " *anything candidates:%s*"
3518 anything-source-name))
3519 (local-bname (format " *anything candidates:%s*%s"
3520 anything-source-name
3521 (buffer-name anything-current-buffer))))
3522 (flet ((register-func ()
3523 (setq anything-candidate-buffer-alist
3524 (cons (cons anything-source-name create-or-buffer)
3525 (delete (assoc anything-source-name
3526 anything-candidate-buffer-alist)
3527 anything-candidate-buffer-alist))))
3528 (kill-buffers-func ()
3529 (loop for b in (buffer-list)
3530 if (string-match (format "^%s" (regexp-quote global-bname))
3531 (buffer-name b))
3532 do (kill-buffer b)))
3533 (create-func ()
3534 (with-current-buffer
3535 (get-buffer-create (if (eq create-or-buffer 'global)
3536 global-bname
3537 local-bname))
3538 (buffer-disable-undo)
3539 (erase-buffer)
3540 (font-lock-mode -1)))
3541 (return-func ()
3542 (or (get-buffer local-bname)
3543 (get-buffer global-bname)
3544 (anything-aif (assoc-default anything-source-name
3545 anything-candidate-buffer-alist)
3546 (and (buffer-live-p it) it)))))
3547 (when create-or-buffer
3548 (register-func)
3549 (unless (bufferp create-or-buffer)
3550 (and (eq create-or-buffer 'global) (kill-buffers-func))
3551 (create-func)))
3552 (return-func))))
3554 (defun anything-compile-source--candidates-in-buffer (source)
3555 (anything-aif (assoc 'candidates-in-buffer source)
3556 (append source
3557 `((candidates . ,(or (cdr it) 'anything-candidates-in-buffer))
3558 (volatile) (match identity)))
3559 source))
3562 ;; (@* "Utility: resplit anything window")
3563 (defun anything-toggle-resplit-window ()
3564 "Toggle resplit anything window, vertically or horizontally."
3565 (interactive)
3566 (with-anything-window
3567 (let ((before-height (window-height)))
3568 (delete-window)
3569 (set-window-buffer
3570 (select-window (if (= (window-height) before-height)
3571 (prog1
3572 (split-window-vertically)
3573 (setq anything-split-window-state 'vertical))
3574 (setq anything-split-window-state 'horizontal)
3575 (split-window-horizontally)))
3576 anything-buffer))))
3578 ;; (@* "Utility: Resize anything window.")
3579 (defun anything-enlarge-window-1 (n)
3580 "Enlarge or narrow anything window.
3581 If N is positive enlarge, if negative narrow."
3582 (unless anything-samewindow
3583 (let ((horizontal-p (eq anything-split-window-state 'horizontal)))
3584 (with-anything-window
3585 (enlarge-window n horizontal-p)))))
3587 (defun anything-narrow-window ()
3588 "Narrow anything window."
3589 (interactive)
3590 (anything-enlarge-window-1 -1))
3592 (defun anything-enlarge-window ()
3593 "Enlarge anything window."
3594 (interactive)
3595 (anything-enlarge-window-1 1))
3597 ;; (@* "Utility: select another action by key")
3598 (defun anything-select-nth-action (n)
3599 "Select the N nth action for the currently selected candidate."
3600 (setq anything-saved-selection (anything-get-selection))
3601 (unless anything-saved-selection
3602 (error "Nothing is selected"))
3603 (setq anything-saved-action (anything-get-nth-action n (anything-get-action)))
3604 (anything-exit-minibuffer))
3606 (defun anything-get-nth-action (n action)
3607 (cond ((and (zerop n) (functionp action))
3608 action)
3609 ((listp action)
3610 (or (cdr (elt action n))
3611 (error "No such action")))
3612 ((and (functionp action) (< 0 n))
3613 (error "Sole action"))
3615 (error "Error in `anything-select-nth-action'"))))
3617 (defun anything-select-2nd-action ()
3618 "Select the 2nd action for the currently selected candidate."
3619 (interactive)
3620 (anything-select-nth-action 1))
3622 (defun anything-select-3rd-action ()
3623 "Select the 3rd action for the currently selected candidate."
3624 (interactive)
3625 (anything-select-nth-action 2))
3627 (defun anything-select-4th-action ()
3628 "Select the 4th action for the currently selected candidate."
3629 (interactive)
3630 (anything-select-nth-action 3))
3632 (defun anything-select-2nd-action-or-end-of-line ()
3633 "Select the 2nd action for the currently selected candidate.
3634 This happen when point is at the end of minibuffer.
3635 Otherwise goto the end of minibuffer."
3636 (interactive)
3637 (if (eolp)
3638 (anything-select-nth-action 1)
3639 (end-of-line)))
3641 ;; (@* "Utility: Persistent Action")
3642 (defmacro with-anything-display-same-window (&rest body)
3643 "Execute BODY in the window used for persistent action.
3644 Make `pop-to-buffer' and `display-buffer' display in the same window."
3645 (declare (indent 0) (debug t))
3646 `(let ((display-buffer-function 'anything-persistent-action-display-buffer))
3647 ,@body))
3649 (defvar anything-persistent-action-display-window nil)
3650 (defun anything-initialize-persistent-action ()
3651 (set (make-local-variable 'anything-persistent-action-display-window) nil))
3653 (defun* anything-execute-persistent-action (&optional (attr 'persistent-action) onewindow)
3654 "Perform the associated action ATTR without quitting anything.
3655 ATTR default is 'persistent-action', but it can be anything else.
3656 In this case you have to add this new attribute to your source.
3657 When `anything-samewindow' and ONEWINDOW are non--nil,
3658 the anything window is never split in persistent action."
3659 (interactive)
3660 (anything-log "executing persistent-action")
3661 (with-anything-window
3662 (save-selected-window
3663 (anything-select-persistent-action-window onewindow)
3664 (anything-log-eval (current-buffer))
3665 (let ((anything-in-persistent-action t))
3666 (with-anything-display-same-window
3667 (anything-execute-selection-action
3669 (or (assoc-default attr (anything-get-current-source))
3670 (anything-get-action))
3672 (anything-log-run-hook 'anything-after-persistent-action-hook))))))
3675 (defun anything-persistent-action-display-window (&optional onewindow)
3676 "Return the window that will be used for presistent action.
3677 If ONEWINDOW is non--nil window will not be splitted in persistent action
3678 if `anything-samewindow' is non--nil also."
3679 (with-anything-window
3680 (setq anything-persistent-action-display-window
3681 (cond ((window-live-p anything-persistent-action-display-window)
3682 anything-persistent-action-display-window)
3683 ((and anything-samewindow (one-window-p t) (not onewindow))
3684 (split-window))
3685 ((get-buffer-window anything-current-buffer))
3687 (next-window (selected-window) 1))))))
3689 (defun anything-select-persistent-action-window (&optional onewindow)
3690 "Select the window that will be used for persistent action.
3691 See `anything-persistent-action-display-window' for how to use ONEWINDOW."
3692 (select-window (get-buffer-window (anything-buffer-get)))
3693 (select-window
3694 (setq minibuffer-scroll-window
3695 (anything-persistent-action-display-window onewindow))))
3697 (defun anything-persistent-action-display-buffer (buf &optional not-this-window)
3698 "Make `pop-to-buffer' and `display-buffer' display in the same window.
3699 If `anything-persistent-action-use-special-display' is non-nil and
3700 BUF is to be displayed by `special-display-function', use it.
3701 Otherwise ignores `special-display-buffer-names' and `special-display-regexps'.
3702 Argument NOT-THIS-WINDOW if present will be used as
3703 second argument of `display-buffer'."
3704 (let* ((name (buffer-name buf))
3705 display-buffer-function pop-up-windows pop-up-frames
3706 (same-window-regexps
3707 (unless (and anything-persistent-action-use-special-display
3708 (or (member name
3709 (mapcar (lambda (x) (or (car-safe x) x))
3710 special-display-buffer-names))
3711 (remove-if-not
3712 (lambda (x) (string-match (or (car-safe x) x) name))
3713 special-display-regexps)))
3714 '("."))))
3715 (display-buffer buf not-this-window)))
3717 ;; scroll-other-window(-down)? for persistent-action
3718 (defun anything-scroll-other-window-base (command)
3719 (with-selected-window (anything-persistent-action-display-window)
3720 (funcall command anything-scroll-amount)))
3722 (defun anything-scroll-other-window ()
3723 "Scroll other window (not *Anything* window) upward."
3724 (interactive)
3725 (anything-scroll-other-window-base 'scroll-up))
3727 (defun anything-scroll-other-window-down ()
3728 "Scroll other window (not *Anything* window) downward."
3729 (interactive)
3730 (anything-scroll-other-window-base 'scroll-down))
3733 ;; (@* "Utility: Visible Mark")
3734 (defface anything-visible-mark
3735 '((((min-colors 88) (background dark))
3736 (:background "green1" :foreground "black"))
3737 (((background dark)) (:background "green" :foreground "black"))
3738 (((min-colors 88)) (:background "green1"))
3739 (t (:background "green")))
3740 "Face for visible mark."
3741 :group 'anything)
3743 (defvar anything-visible-mark-face 'anything-visible-mark)
3744 (defvar anything-visible-mark-overlays nil)
3746 (defun anything-clear-visible-mark ()
3747 (with-current-buffer (anything-buffer-get)
3748 (mapc 'delete-overlay anything-visible-mark-overlays)
3749 (set (make-local-variable 'anything-visible-mark-overlays) nil)))
3750 (add-hook 'anything-after-initialize-hook 'anything-clear-visible-mark)
3752 (defvar anything-marked-candidates nil
3753 "Marked candadates. List of \(source . real\) pair.")
3755 (defun anything-this-visible-mark ()
3756 (loop for o in anything-visible-mark-overlays
3757 when (equal (point-at-bol) (overlay-start o))
3758 return o))
3760 (defun anything-delete-visible-mark (overlay)
3761 (setq anything-marked-candidates
3762 (remove
3763 (cons (anything-get-current-source) (anything-get-selection))
3764 anything-marked-candidates))
3765 (delete-overlay overlay)
3766 (setq anything-visible-mark-overlays
3767 (delq overlay anything-visible-mark-overlays)))
3769 (defun anything-make-visible-mark ()
3770 (let ((o (make-overlay (point-at-bol) (1+ (point-at-eol)))))
3771 (overlay-put o 'face anything-visible-mark-face)
3772 (overlay-put o 'source (assoc-default 'name (anything-get-current-source)))
3773 (overlay-put o 'string (buffer-substring (overlay-start o) (overlay-end o)))
3774 (overlay-put o 'real (anything-get-selection))
3775 (add-to-list 'anything-visible-mark-overlays o))
3776 (push (cons (anything-get-current-source) (anything-get-selection))
3777 anything-marked-candidates))
3779 (defun anything-toggle-visible-mark ()
3780 "Toggle anything visible mark at point."
3781 (interactive)
3782 (with-anything-window
3783 (anything-aif (anything-this-visible-mark)
3784 (anything-delete-visible-mark it)
3785 (anything-make-visible-mark))
3786 (anything-next-line)))
3788 (defun anything-display-all-visible-marks ()
3789 "Show all `anything' visible marks strings."
3790 (interactive)
3791 (with-anything-window
3792 (lexical-let ((overlays (reverse anything-visible-mark-overlays)))
3793 (anything-run-after-quit
3794 (lambda ()
3795 (with-output-to-temp-buffer "*anything visible marks*"
3796 (dolist (o overlays) (princ (overlay-get o 'string)))))))))
3798 (defun anything-marked-candidates ()
3799 "Return marked candidates of current source if any.
3800 Otherwise one element list of current selection.
3802 It is analogous to `dired-get-marked-files'."
3803 (with-current-buffer (anything-buffer-get)
3804 (let ((cands
3805 (if anything-marked-candidates
3806 (loop with current-src = (anything-get-current-source)
3807 for (source . real) in (reverse anything-marked-candidates)
3808 when (equal current-src source)
3809 collect (anything-coerce-selection real source))
3810 (list (anything-get-selection)))))
3811 (anything-log-eval cands)
3812 cands)))
3814 (defun anything-reset-marked-candidates ()
3815 (with-current-buffer (anything-buffer-get)
3816 (set (make-local-variable 'anything-marked-candidates) nil)))
3818 (add-hook 'anything-after-initialize-hook 'anything-reset-marked-candidates)
3819 ;; (add-hook 'anything-after-action-hook 'anything-reset-marked-candidates)
3821 (defun anything-current-source-name= (name)
3822 (save-excursion
3823 (goto-char (anything-get-previous-header-pos))
3824 (equal name (anything-current-line-contents))))
3826 (defun anything-revive-visible-mark ()
3827 "Restore marked candidates when anything update display."
3828 (with-current-buffer anything-buffer
3829 (dolist (o anything-visible-mark-overlays)
3830 (goto-char (point-min))
3831 (while (and (search-forward (overlay-get o 'string) nil t)
3832 (anything-current-source-name= (overlay-get o 'source)))
3833 ;; Calculate real value of candidate.
3834 ;; It can be nil if candidate have only a display value.
3835 (let ((real (get-text-property (point-at-bol 0) 'anything-realvalue)))
3836 (if real
3837 ;; Check if real value of current candidate is the same
3838 ;; that the one stored in overlay.
3839 (and (string= (overlay-get o 'real) real)
3840 (move-overlay o (point-at-bol 0) (1+ (point-at-eol 0))))
3841 (move-overlay o (point-at-bol 0) (1+ (point-at-eol 0)))))))))
3842 (add-hook 'anything-update-hook 'anything-revive-visible-mark)
3844 (defun anything-next-point-in-list (curpos points &optional prev)
3845 (cond
3846 ;; rule out special cases
3847 ((null points) curpos)
3848 ((and prev (< curpos (car points))) curpos)
3849 ((< (car (last points)) curpos)
3850 (if prev (car (last points)) curpos))
3852 (nth (if prev
3853 (loop for pt in points
3854 for i from 0
3855 if (<= curpos pt)
3856 return (1- i))
3857 (loop for pt in points
3858 for i from 0
3859 if (< curpos pt)
3860 return i))
3861 points))))
3863 (defun anything-next-visible-mark (&optional prev)
3864 "Move next anything visible mark.
3865 If PREV is non-nil move to precedent."
3866 (interactive)
3867 (with-anything-window
3868 (ignore-errors
3869 (goto-char (anything-next-point-in-list
3870 (point)
3871 (sort (mapcar 'overlay-start anything-visible-mark-overlays) '<)
3872 prev)))
3873 (anything-mark-current-line)))
3875 (defun anything-prev-visible-mark ()
3876 "Move previous anything visible mark."
3877 (interactive)
3878 (anything-next-visible-mark t))
3880 ;; (@* "Utility: Selection Paste")
3881 (defun anything-yank-selection ()
3882 "Set minibuffer contents to current selection."
3883 (interactive)
3884 (anything-set-pattern (anything-get-selection nil t)))
3886 (defun anything-kill-selection-and-quit ()
3887 "Store current selection to kill ring.
3888 You can paste it by typing \\[yank]."
3889 (interactive)
3890 (anything-run-after-quit
3891 (lambda (sel)
3892 (kill-new sel)
3893 (message "Killed: %s" sel))
3894 (anything-get-selection nil t)))
3897 ;; (@* "Utility: Automatical execution of persistent-action")
3898 (add-to-list 'minor-mode-alist '(anything-follow-mode " AFollow"))
3899 (defun anything-follow-mode ()
3900 "If this mode is on, persistent action is executed everytime the cursor is moved."
3901 (interactive)
3902 (with-current-buffer anything-buffer
3903 (setq anything-follow-mode (not anything-follow-mode))
3904 (message "anything-follow-mode is %s"
3905 (if anything-follow-mode "enabled" "disabled"))))
3907 (defun anything-follow-execute-persistent-action-maybe ()
3908 "Execute persistent action in mode `anything-follow-mode'.
3909 This happen after `anything-input-idle-delay' secs."
3910 (and (not (get-buffer-window anything-action-buffer 'visible))
3911 (buffer-local-value 'anything-follow-mode
3912 (get-buffer-create anything-buffer))
3913 (sit-for (and anything-input-idle-delay
3914 (max anything-input-idle-delay 0.1)))
3915 (anything-window)
3916 (anything-get-selection)
3917 (save-excursion
3918 (anything-execute-persistent-action))))
3921 ;; (@* "Utility: Migrate `anything-sources' to my-anything command")
3922 (defun anything-migrate-sources ()
3923 "Help to migrate to new `anything' way."
3924 (interactive)
3925 (with-current-buffer (get-buffer-create "*anything migrate*")
3926 (erase-buffer)
3927 (insert (format "\
3928 Setting `anything-sources' directly is not good because
3929 `anything' is not for one command. For now, interactive use of
3930 `anything' (M-x anything) is only for demonstration purpose.
3931 So you should define commands calling `anything'.
3932 I help you to migrate to the new way.
3934 The code below is automatically generated from current
3935 `anything-sources' value. You can use the `my-anything' command
3936 now!
3938 Copy and paste it to your .emacs. Then substitute `my-anything'
3939 for `anything' bindings in all `define-key', `local-set-key' and
3940 `global-set-key' calls.
3942 \(defun my-anything ()
3943 \"Anything command for you.
3945 It is automatically generated by `anything-migrate-sources'.\"
3946 (interactive)
3947 (anything-other-buffer
3949 \"*my-anything*\"))
3950 " anything-sources))
3951 (eval-last-sexp nil)
3952 (substitute-key-definition 'anything 'my-anything global-map)
3953 (pop-to-buffer (current-buffer))))
3956 ;; (@* "Compatibility")
3958 ;; Copied assoc-default from XEmacs version 21.5.12
3959 (unless (fboundp 'assoc-default)
3960 (defun assoc-default (key alist &optional test default)
3961 "Find object KEY in a pseudo-alist ALIST.
3962 ALIST is a list of conses or objects. Each element (or the element's car,
3963 if it is a cons) is compared with KEY by evaluating (TEST (car elt) KEY).
3964 If that is non-nil, the element matches;
3965 then `assoc-default' returns the element's cdr, if it is a cons,
3966 or DEFAULT if the element is not a cons.
3968 If no element matches, the value is nil.
3969 If TEST is omitted or nil, `equal' is used."
3970 (let (found (tail alist) value)
3971 (while (and tail (not found))
3972 (let ((elt (car tail)))
3973 (when (funcall (or test 'equal) (if (consp elt) (car elt) elt) key)
3974 (setq found t value (if (consp elt) (cdr elt) default))))
3975 (setq tail (cdr tail)))
3976 value)))
3978 ;; Function not available in XEmacs,
3979 (unless (fboundp 'minibuffer-contents)
3980 (defun minibuffer-contents ()
3981 "Return the user input in a minbuffer as a string.
3982 The current buffer must be a minibuffer."
3983 (field-string (point-max)))
3985 (defun delete-minibuffer-contents ()
3986 "Delete all user input in a minibuffer.
3987 The current buffer must be a minibuffer."
3988 (delete-field (point-max))))
3990 ;; Function not available in older Emacs (<= 22.1).
3991 (unless (fboundp 'buffer-chars-modified-tick)
3992 (defun buffer-chars-modified-tick (&optional buffer)
3993 "Return BUFFER's character-change tick counter.
3994 Each buffer has a character-change tick counter, which is set to the
3995 value of the buffer's tick counter (see `buffer-modified-tick'), each
3996 time text in that buffer is inserted or deleted. By comparing the
3997 values returned by two individual calls of `buffer-chars-modified-tick',
3998 you can tell whether a character change occurred in that buffer in
3999 between these calls. No argument or nil as argument means use current
4000 buffer as BUFFER."
4001 (with-current-buffer (or buffer (current-buffer))
4002 (if (listp buffer-undo-list)
4003 (length buffer-undo-list)
4004 (buffer-modified-tick)))))
4007 ;; (@* "CUA workaround")
4008 (defadvice cua-delete-region (around anything-avoid-cua activate)
4009 (ignore-errors ad-do-it))
4011 (defadvice copy-region-as-kill (around anything-avoid-cua activate)
4012 (if cua-mode
4013 (ignore-errors ad-do-it)
4014 ad-do-it))
4016 ;;(@* "Attribute Documentation")
4017 (defun anything-describe-anything-attribute (anything-attribute)
4018 "Display the full documentation of ANYTHING-ATTRIBUTE.
4019 ANYTHING-ATTRIBUTE should be a symbol."
4020 (interactive (list (intern
4021 (completing-read
4022 "Describe anything attribute: "
4023 (mapcar 'symbol-name anything-additional-attributes)
4024 nil t))))
4025 (with-output-to-temp-buffer "*Help*"
4026 (princ (get anything-attribute 'anything-attrdoc))))
4028 (anything-document-attribute 'name "mandatory"
4029 " The name of the source. It is also the heading which appears
4030 above the list of matches from the source. Must be unique.")
4032 (anything-document-attribute 'header-name "optional"
4033 " A function returning the display string of the header. Its
4034 argument is the name of the source. This attribute is useful to
4035 add an additional information with the source name.")
4037 (anything-document-attribute 'candidates "mandatory if candidates-in-buffer attribute is not provided"
4038 " Specifies how to retrieve candidates from the source. It can
4039 either be a variable name, a function called with no parameters
4040 or the actual list of candidates.
4042 The list must be a list whose members are strings, symbols
4043 or (DISPLAY . REAL) pairs.
4045 In case of (DISPLAY . REAL) pairs, the DISPLAY string is shown
4046 in the Anything buffer, but the REAL one is used as action
4047 argument when the candidate is selected. This allows a more
4048 readable presentation for candidates which would otherwise be,
4049 for example, too long or have a common part shared with other
4050 candidates which can be safely replaced with an abbreviated
4051 string for display purposes.
4053 Note that if the (DISPLAY . REAL) form is used then pattern
4054 matching is done on the displayed string, not on the real
4055 value.
4057 If the candidates have to be retrieved asynchronously (for
4058 example, by an external command which takes a while to run)
4059 then the function should start the external command
4060 asynchronously and return the associated process object.
4061 Anything will take care of managing the process (receiving the
4062 output from it, killing it if necessary, etc.). The process
4063 should return candidates matching the current pattern (see
4064 variable `anything-pattern'.)
4066 Note that currently results from asynchronous sources appear
4067 last in the anything buffer regardless of their position in
4068 `anything-sources'.")
4070 (anything-document-attribute 'action "mandatory if type attribute is not provided"
4071 " It is a list of (DISPLAY . FUNCTION) pairs or FUNCTION.
4072 FUNCTION is called with one parameter: the selected candidate.
4074 An action other than the default can be chosen from this list
4075 of actions for the currently selected candidate (by default
4076 with TAB). The DISPLAY string is shown in the completions
4077 buffer and the FUNCTION is invoked when an action is
4078 selected. The first action of the list is the default.")
4080 (anything-document-attribute 'coerce "optional"
4081 " It's a function called with one argument: the selected candidate.
4083 This function is intended for type convertion.
4084 In normal case, the selected candidate (string) is passed to action function.
4085 If coerce function is specified, it is called just before action function.
4087 Example: converting string to symbol
4088 (coerce . intern)")
4090 (anything-document-attribute 'type "optional if action attribute is provided"
4091 " Indicates the type of the items the source returns.
4093 Merge attributes not specified in the source itself from
4094 `anything-type-attributes'.
4096 This attribute is implemented by plug-in.")
4098 (anything-document-attribute 'init "optional"
4099 " Function called with no parameters when anything is started. It
4100 is useful for collecting current state information which can be
4101 used to create the list of candidates later.
4103 For example, if a source needs to work with the current
4104 directory then it can store its value here, because later
4105 anything does its job in the minibuffer and in the
4106 `anything-buffer' and the current directory can be different
4107 there.")
4109 (anything-document-attribute 'delayed-init "optional"
4110 " Function called with no parameters before candidate function is
4111 called. It is similar with `init' attribute, but its
4112 evaluation is deferred. It is useful to combine with ")
4114 (anything-document-attribute 'match "optional"
4115 " List of functions called with one parameter: a candidate. The
4116 function should return non-nil if the candidate matches the
4117 current pattern (see variable `anything-pattern').
4119 This attribute allows the source to override the default
4120 pattern matching based on `string-match'. It can be used, for
4121 example, to implement a source for file names and do the
4122 pattern matching on the basename of files, since it's more
4123 likely one is typing part of the basename when searching for a
4124 file, instead of some string anywhere else in its path.
4126 If the list contains more than one function then the list of
4127 matching candidates from the source is constructed by appending
4128 the results after invoking the first function on all the
4129 potential candidates, then the next function, and so on. The
4130 matching candidates supplied by the first function appear first
4131 in the list of results and then results from the other
4132 functions, respectively.
4134 This attribute has no effect for asynchronous sources (see
4135 attribute `candidates'), since they perform pattern matching
4136 themselves.")
4138 (anything-document-attribute 'candidate-transformer "optional"
4139 " It's a function or a list of functions called with one argument
4140 when the completion list from the source is built. The argument
4141 is the list of candidates retrieved from the source. The
4142 function should return a transformed list of candidates which
4143 will be used for the actual completion. If it is a list of
4144 functions, it calls each function sequentially.
4146 This can be used to transform or remove items from the list of
4147 candidates.
4149 Note that `candidates' is run already, so the given transformer
4150 function should also be able to handle candidates with (DISPLAY
4151 . REAL) format.")
4153 (anything-document-attribute 'filtered-candidate-transformer "optional"
4154 " It has the same format as `candidate-transformer', except the
4155 function is called with two parameters: the candidate list and
4156 the source.
4158 This transformer is run on the candidate list which is already
4159 filtered by the current pattern. While `candidate-transformer'
4160 is run only once, it is run every time the input pattern is
4161 changed.
4163 It can be used to transform the candidate list dynamically, for
4164 example, based on the current pattern.
4166 In some cases it may also be more efficent to perform candidate
4167 transformation here, instead of with `candidate-transformer'
4168 even if this transformation is done every time the pattern is
4169 changed. For example, if a candidate set is very large then
4170 `candidate-transformer' transforms every candidate while only
4171 some of them will actually be dislpayed due to the limit
4172 imposed by `anything-candidate-number-limit'.
4174 Note that `candidates' and `candidate-transformer' is run
4175 already, so the given transformer function should also be able
4176 to handle candidates with (DISPLAY . REAL) format.
4178 This option has no effect for asynchronous sources. (Not yet,
4179 at least.")
4181 (anything-document-attribute 'action-transformer "optional"
4182 " It's a function or a list of functions called with two
4183 arguments when the action list from the source is
4184 assembled. The first argument is the list of actions, the
4185 second is the current selection. If it is a list of functions,
4186 it calls each function sequentially.
4188 The function should return a transformed action list.
4190 This can be used to customize the list of actions based on the
4191 currently selected candidate.")
4193 (anything-document-attribute 'pattern-transformer "optional"
4194 " It's a function or a list of functions called with one argument
4195 before computing matches. Its argument is `anything-pattern'.
4196 Functions should return transformed `anything-pattern'.
4198 It is useful to change interpretation of `anything-pattern'.")
4200 (anything-document-attribute 'delayed "optional"
4201 " Candidates from the source are shown only if the user stops
4202 typing and is idle for `anything-idle-delay' seconds.")
4204 (anything-document-attribute 'volatile "optional"
4205 " Indicates the source assembles the candidate list dynamically,
4206 so it shouldn't be cached within a single Anything
4207 invocation. It is only applicable to synchronous sources,
4208 because asynchronous sources are not cached.")
4210 (anything-document-attribute 'requires-pattern "optional"
4211 " If present matches from the source are shown only if the
4212 pattern is not empty. Optionally, it can have an integer
4213 parameter specifying the required length of input which is
4214 useful in case of sources with lots of candidates.")
4216 (anything-document-attribute 'persistent-action "optional"
4217 " Function called with one parameter; the selected candidate.
4219 An action performed by `anything-execute-persistent-action'.
4220 If none, use the default action.")
4222 (anything-document-attribute 'candidates-in-buffer "optional"
4223 " Shortcut attribute for making and narrowing candidates using
4224 buffers. This newly-introduced attribute prevents us from
4225 forgetting to add volatile and match attributes.
4227 See docstring of `anything-candidates-in-buffer'.
4229 (candidates-in-buffer) is equivalent of three attributes:
4230 (candidates . anything-candidates-in-buffer)
4231 (volatile)
4232 (match identity)
4234 (candidates-in-buffer . candidates-function) is equivalent of:
4235 (candidates . candidates-function)
4236 (volatile)
4237 (match identity)
4239 This attribute is implemented by plug-in.")
4241 (anything-document-attribute 'search "optional"
4242 " List of functions like `re-search-forward' or `search-forward'.
4243 Buffer search function used by `anything-candidates-in-buffer'.
4244 By default, `anything-candidates-in-buffer' uses `re-search-forward'.
4245 This attribute is meant to be used with
4246 (candidates . anything-candidates-in-buffer) or
4247 (candidates-in-buffer) in short.")
4249 (anything-document-attribute 'search-from-end "optional"
4250 " Make `anything-candidates-in-buffer' search from the end of buffer.
4251 If this attribute is specified, `anything-candidates-in-buffer' uses
4252 `re-search-backward' instead.")
4254 (anything-document-attribute 'get-line "optional"
4255 " A function like `buffer-substring-no-properties' or `buffer-substring'.
4256 This function converts point of line-beginning and point of line-end,
4257 which represents a candidate computed by `anything-candidates-in-buffer'.
4258 By default, `anything-candidates-in-buffer' uses
4259 `buffer-substring-no-properties'.")
4261 (anything-document-attribute 'display-to-real "optional"
4262 " Function called with one parameter; the selected candidate.
4264 The function transforms the selected candidate, and the result
4265 is passed to the action function. The display-to-real
4266 attribute provides another way to pass other string than one
4267 shown in Anything buffer.
4269 Traditionally, it is possible to make candidates,
4270 candidate-transformer or filtered-candidate-transformer
4271 function return a list with (DISPLAY . REAL) pairs. But if REAL
4272 can be generated from DISPLAY, display-to-real is more
4273 convenient and faster.")
4275 (anything-document-attribute 'real-to-display "optional"
4276 " Function called with one parameter; the selected candidate.
4278 The inverse of display-to-real attribute.
4280 The function transforms the selected candidate, which is passed
4281 to the action function, for display. The real-to-display
4282 attribute provides the other way to pass other string than one
4283 shown in Anything buffer.
4285 Traditionally, it is possible to make candidates,
4286 candidate-transformer or filtered-candidate-transformer
4287 function return a list with (DISPLAY . REAL) pairs. But if
4288 DISPLAY can be generated from REAL, real-to-display is more
4289 convenient.
4291 Note that DISPLAY parts returned from candidates /
4292 candidate-transformer are IGNORED as the name `display-to-real'
4293 says.")
4295 (anything-document-attribute 'cleanup "optional"
4296 " Function called with no parameters when *anything* buffer is closed. It
4297 is useful for killing unneeded candidates buffer.
4299 Note that the function is executed BEFORE performing action.")
4301 (anything-document-attribute 'candidate-number-limit "optional"
4302 " Override `anything-candidate-number-limit' only for this source.")
4304 (anything-document-attribute 'accept-empty "optional"
4305 " Pass empty string \"\" to action function.")
4307 (anything-document-attribute 'disable-shortcuts "optional"
4308 " Disable `anything-enable-shortcuts' in current `anything' session.
4310 This attribute is implemented by plug-in.")
4312 (anything-document-attribute 'dummy "optional"
4313 " Set `anything-pattern' to candidate. If this attribute is
4314 specified, The candidates attribute is ignored.
4316 This attribute is implemented by plug-in.
4317 This plug-in implies disable-shortcuts plug-in.")
4319 (anything-document-attribute 'multiline "optional"
4320 " Enable to selection multiline candidates.")
4322 (anything-document-attribute 'update "optional"
4323 (substitute-command-keys
4324 " Function called with no parameters when \
4325 \\<anything-map>\\[anything-force-update] is pressed."))
4327 (anything-document-attribute 'mode-line "optional"
4328 " source local `anything-mode-line-string'. (included in `mode-line-format')
4329 It accepts also variable/function name.")
4331 (anything-document-attribute 'header-line "optional"
4332 " source local `header-line-format'.
4333 It accepts also variable/function name. ")
4335 (anything-document-attribute
4336 'resume "optional"
4337 " Function called with no parameters when `anything-resume' is started.")
4339 (anything-document-attribute 'keymap "optional"
4340 " Specific keymap for this source.
4341 It is useful to have a keymap per source when using more than one source.
4342 Otherwise, a keymap can be set per command with `anything' argument KEYMAP.
4343 NOTE: when a source have `anything-map' as keymap attr,
4344 the global value of `anything-map' will override the actual local one.")
4346 (anything-document-attribute 'help-message "optional"
4347 " Help message for this source.
4348 If not present, `anything-help-message' value will be used.")
4351 ;; (@* "Bug Report")
4352 (defvar anything-maintainer-mail-address "emacs-anything@googlegroups.com")
4354 (defvar anything-bug-report-salutation
4355 "Describe bug below, using a precise recipe.
4357 When I executed M-x ...
4359 How to send a bug report:
4360 1) Be sure to use the LATEST version of anything.el.
4361 2) Enable debugger. M-x toggle-debug-on-error or (setq debug-on-error t)
4362 3) Use Lisp version instead of compiled one: (load \"anything.el\")
4363 4) If you got an error, please paste *Backtrace* buffer.
4364 5) Type C-c C-c to send.")
4366 (defvar anything-no-dump-variables
4367 '(anything-candidate-buffer-alist
4368 anything-digit-overlays
4369 anything-help-message
4370 anything-candidate-cache
4372 "Variables not to dump in bug report.")
4374 (defun anything-dumped-variables-in-bug-report ()
4375 (let ((hash (make-hash-table)))
4376 (loop for var in (apropos-internal "anything-" 'boundp)
4377 for vname = (symbol-name var)
4378 unless (or (string-match "-map$" vname)
4379 (string-match "^anything-c-source-" vname)
4380 (string-match "-hash$" vname)
4381 (string-match "-face$" vname)
4382 (memq var anything-no-dump-variables))
4383 collect var)))
4385 (defun anything-send-bug-report ()
4386 "Send a bug report of anything.el."
4387 (interactive)
4388 (with-current-buffer (or anything-last-buffer
4389 (current-buffer))
4390 (reporter-submit-bug-report
4391 anything-maintainer-mail-address
4392 "anything.el"
4393 (anything-dumped-variables-in-bug-report)
4394 nil nil
4395 anything-bug-report-salutation)))
4397 (defun anything-send-bug-report-from-anything ()
4398 "Send a bug report of anything.el in anything session."
4399 (interactive)
4400 (anything-run-after-quit 'anything-send-bug-report))
4402 ;; Debugging function.
4403 (defun* anything-test-candidates
4404 (sources &optional (input "")
4405 (compile-source-functions
4406 anything-compile-source-functions-default))
4407 "Test helper function for anything.
4408 Given pseudo `anything-sources' and `anything-pattern', returns list like
4409 ((\"source name1\" (\"candidate1\" \"candidate2\"))
4410 (\"source name2\" (\"candidate3\" \"candidate4\")))"
4411 (let ((anything-test-mode t)
4412 anything-enable-shortcuts
4413 anything-candidate-cache
4414 (anything-compile-source-functions compile-source-functions)
4415 anything-before-initialize-hook
4416 anything-after-initialize-hook
4417 anything-update-hook
4418 anything-test-candidate-list)
4419 (get-buffer-create anything-buffer)
4420 (anything-initialize nil input sources)
4421 (anything-update)
4422 ;; test-mode spec: select 1st candidate!
4423 (with-current-buffer anything-buffer
4424 (forward-line 1)
4425 (anything-mark-current-line))
4426 (prog1
4427 anything-test-candidate-list
4428 (anything-cleanup))))
4431 ;; (@* "Unit Tests")
4432 ;; See developer-tools/unit-test-anything.el
4434 (provide 'anything)
4436 ;; Local Variables:
4437 ;; coding: utf-8
4438 ;; End:
4440 ;;; anything.el ends here