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