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