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