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