contrib/anything-grep.el: anything-grep-multiline: revert to `t'
[anything-config.git] / anything.el
blob5399a8cbf1b2d23d767b03148aeee48d06b19011
1 ;;; anything.el --- open anything / QuickSilver-like candidate-selection framework
3 ;; Copyright (C) 2007 Tamas Patrovics
4 ;; 2008 ~ 2016 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 'prefix
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 . "asdfghjklzxcvbnmqwertyuiop")
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 (when (get-buffer (anything-buffer-get))
1162 (with-current-buffer (anything-buffer-get)
1163 ,@body))
1164 (when (window-live-p (anything-window))
1165 (with-selected-window (anything-window)
1166 ,@body))))
1168 (defmacro with-anything-current-buffer (&rest body)
1169 "Eval BODY inside `anything-current-buffer'."
1170 (declare (indent 0) (debug t))
1171 `(with-current-buffer anything-current-buffer
1172 ,@body))
1174 (defmacro with-anything-restore-variables(&rest body)
1175 "Restore `anything-restored-variables' after executing BODY.
1176 `post-command-hook' is handled specially."
1177 (declare (indent 0) (debug t))
1178 `(let ((--orig-vars (delq nil
1179 (mapcar (lambda (v)
1180 (and (boundp v) (cons v (symbol-value v))))
1181 (append (mapcar 'car anything-let-variables)
1182 anything-restored-variables))))
1183 (--post-command-hook-pair (cons post-command-hook
1184 (default-value 'post-command-hook))))
1185 (setq post-command-hook '(t))
1186 (setq-default post-command-hook nil)
1187 (unwind-protect (progn ,@body)
1188 (loop for (var . value) in --orig-vars
1189 do (set var value))
1190 (setq post-command-hook (car --post-command-hook-pair))
1191 (setq-default post-command-hook (cdr --post-command-hook-pair))
1192 (anything-log "restore variables"))))
1194 (defun* anything-attr (attribute-name &optional
1195 (src (anything-get-current-source)))
1196 "Get the value of ATTRIBUTE-NAME of SRC.
1197 if SRC is omitted, use current source.
1198 It is useful to write your sources."
1199 (anything-aif (assq attribute-name src)
1200 (cdr it)))
1202 (defun* anything-attr* (attribute-name
1203 &optional (src (anything-get-current-source)))
1204 "Pass the value of ATTRIBUTE-NAME of SRC to `anything-interpret-value'.
1205 if SRC is omitted, use current source.
1206 It is useful to write your sources."
1207 (anything-interpret-value (anything-attr attribute-name src)))
1209 (defun* anything-attr-defined (attribute-name
1210 &optional (src (anything-get-current-source)))
1211 "Return non-nil if ATTRIBUTE-NAME of SRC is defined.
1212 if SRC is omitted, use current source.
1213 It is useful to write your sources."
1214 (and (assq attribute-name src) t))
1216 (defun* anything-attrset (attribute-name value
1217 &optional
1218 (src (anything-get-current-source)))
1219 "Set the value of ATTRIBUTE-NAME of SRC to VALUE.
1220 if SRC is omitted, use current source.
1221 It is useful to write your sources."
1222 (anything-aif (assq attribute-name src)
1223 (setcdr it value)
1224 (setcdr src (cons (cons attribute-name value) (cdr src))))
1225 value)
1227 (defun anything-set-source-filter (sources)
1228 "Set the value of `anything-source-filter' to SOURCES and update.
1230 This function sets a filter for anything sources and it may be
1231 called while anything is running. It can be used to toggle
1232 displaying of sources dinamically. For example, additional keys
1233 can be bound into `anything-map' to display only the file-related
1234 results if there are too many matches from other sources and
1235 you're after files only:
1237 Shift+F shows only file results from some sources:
1239 \(define-key anything-map \"F\" 'anything-my-show-files-only)
1241 \(defun anything-my-show-files-only ()
1242 (interactive)
1243 (anything-set-source-filter '(\"File Name History\"
1244 \"Files from Current Directory\")))
1246 Shift+A shows all results:
1248 \(define-key anything-map \"A\" 'anything-my-show-all)
1250 \(defun anything-my-show-all ()
1251 (interactive)
1252 (anything-set-source-filter nil))
1254 Note that you have to prefix the functions with anything- prefix,
1255 otherwise they won't be bound when Anything is used under
1256 Iswitchb. The -my- part is added to avoid collisions with
1257 existing Anything function names."
1258 (unless (and (listp sources)
1259 (loop for name in sources always (stringp name)))
1260 (error "Invalid data in `anything-set-source-filter': %S" sources))
1261 (setq anything-source-filter sources)
1262 (anything-log-eval anything-source-filter)
1263 (anything-update))
1265 (defun anything-set-sources (sources &optional no-init no-update)
1266 "Set SOURCES during `anything' invocation.
1267 If NO-INIT is non-nil, skip executing init functions of SOURCES.
1268 If NO-UPDATE is non-nil, skip executing `anything-update'."
1269 (with-current-buffer anything-buffer
1270 (setq anything-compiled-sources nil
1271 anything-sources sources
1272 anything-last-sources-local sources)
1273 (anything-log-eval anything-compiled-sources anything-sources))
1274 (unless no-init (anything-funcall-foreach 'init))
1275 (unless no-update (anything-update)))
1277 (defvar anything-compile-source-functions
1278 '(anything-compile-source--type
1279 anything-compile-source--dummy
1280 anything-compile-source--disable-shortcuts
1281 anything-compile-source--candidates-in-buffer)
1282 "Functions to compile elements of `anything-sources' (plug-in).")
1284 (defun anything-get-sources ()
1285 "Return compiled `anything-sources', which is memoized.
1287 Attributes:
1289 - type
1290 `anything-type-attributes' are merged in.
1291 - candidates-buffer
1292 candidates, volatile and match attribute are created."
1293 (cond
1294 ;; action
1295 ((anything-action-window)
1296 anything-sources)
1297 ;; memoized
1298 (anything-compiled-sources)
1299 ;; first time
1301 (prog1
1302 (setq anything-compiled-sources
1303 (anything-compile-sources
1304 anything-sources anything-compile-source-functions))
1305 (anything-log-eval anything-compiled-sources)))))
1307 (defun* anything-get-selection (&optional (buffer nil buffer-s)
1308 force-display-part)
1309 "Return the currently selected item or nil.
1310 if BUFFER is nil or unspecified, use anything-buffer as default value.
1311 If FORCE-DISPLAY-PART is non-nil, return the display string.
1312 If FORCE-DISPLAY-PART value is 'withprop the display string is returned
1313 with its properties."
1314 (setq buffer (if (and buffer buffer-s) buffer anything-buffer))
1315 (unless (anything-empty-buffer-p buffer)
1316 (with-current-buffer buffer
1317 (let* ((disp-fn (if (eq force-display-part 'withprop)
1318 'buffer-substring
1319 'buffer-substring-no-properties))
1320 (selection
1321 (or (and (not force-display-part)
1322 (get-text-property (overlay-start
1323 anything-selection-overlay)
1324 'anything-realvalue))
1325 ;; It is needed to return properties of DISP in some case,
1326 ;; e.g for `anything-confirm-and-exit-minibuffer',
1327 ;; so use `buffer-substring' here when 'withprop is specified.
1328 (let ((disp (funcall disp-fn
1329 (overlay-start anything-selection-overlay)
1330 (1- (overlay-end anything-selection-overlay))))
1331 (source (anything-get-current-source)))
1332 (anything-aif (and (not force-display-part)
1333 (assoc-default 'display-to-real source))
1334 (anything-funcall-with-source source it disp)
1335 disp)))))
1336 (unless (equal selection "")
1337 (anything-log-eval selection)
1338 selection)))))
1340 (defun anything-get-action ()
1341 "Return the associated action for the selected candidate.
1342 It is a function symbol \(sole action\) or list
1343 of \(action-display . function\)."
1344 (unless (anything-empty-buffer-p (anything-buffer-get))
1345 (anything-aif (anything-attr 'action-transformer)
1346 (anything-composed-funcall-with-source
1347 (anything-get-current-source) it
1348 (anything-attr 'action) (anything-get-selection))
1349 (anything-attr 'action))))
1351 (defun anything-get-current-source ()
1352 "Return the source for the current selection.
1353 Use it in init, candidates, action, candidate-transformer,
1354 filtered-candidate-transformer functions."
1355 (declare (special source))
1356 ;; The name `anything-get-current-source' should be used in init function etc.
1357 (cond ((and (boundp 'anything-source-name) (stringp anything-source-name))
1358 source)
1359 ((get-buffer (anything-buffer-get))
1360 (with-current-buffer (anything-buffer-get)
1362 ;; This happen only when `anything-source-in-each-line-flag'
1363 ;; is non--nil and there is candidates in buffer.
1364 (get-text-property (point) 'anything-source)
1365 ;; Return nil when no--candidates.
1366 (block exit
1367 ;; This goto-char shouldn't be necessary, but point is moved to
1368 ;; point-min somewhere else which shouldn't happen.
1369 (goto-char (overlay-start anything-selection-overlay))
1370 (let* ((header-pos (or (anything-get-previous-header-pos)
1371 (anything-get-next-header-pos)))
1372 (source-name
1373 (save-excursion
1374 (unless header-pos
1375 (return-from exit nil))
1376 (goto-char header-pos)
1377 (anything-current-line-contents))))
1378 (loop for source in (anything-get-sources) thereis
1379 (and (equal (assoc-default 'name source) source-name)
1380 source)))))))))
1382 (defun anything-buffer-is-modified (buffer)
1383 "Return non-nil when BUFFER is modified since `anything' was invoked."
1384 (let* ((b (get-buffer buffer))
1385 (key (concat (buffer-name b) "/" (anything-attr 'name)))
1386 (source-tick (or (gethash key anything-tick-hash) 0))
1387 (buffer-tick (buffer-chars-modified-tick b))
1388 (modifiedp (/= source-tick buffer-tick)))
1389 (puthash key buffer-tick anything-tick-hash)
1390 (anything-log-eval buffer modifiedp)
1391 modifiedp))
1393 (defun anything-current-buffer-is-modified ()
1394 "Check if `anything-current-buffer' is modified since `anything' was invoked."
1395 (anything-buffer-is-modified anything-current-buffer))
1397 (defvar anything-quit nil)
1398 (defun anything-run-after-quit (function &rest args)
1399 "Perform an action after quitting `anything'.
1400 The action is to call FUNCTION with arguments ARGS."
1401 (setq anything-quit t)
1402 (anything-kill-async-processes)
1403 (anything-log-eval function args)
1404 (apply 'run-with-idle-timer 0 nil function args)
1405 (anything-exit-minibuffer))
1408 (defun define-anything-type-attribute (type definition &optional doc)
1409 "Register type attribute of TYPE as DEFINITION with DOC.
1410 DOC is displayed in `anything-type-attributes' docstring.
1412 Use this function is better than setting `anything-type-attributes' directly."
1413 (loop for i in definition do
1414 ;; without `ignore-errors', error at emacs22
1415 (ignore-errors (setf i (delete nil i))))
1416 (anything-add-type-attribute type definition)
1417 (and doc (anything-document-type-attribute type doc))
1418 nil)
1420 (defvaralias 'anything-attributes 'anything-additional-attributes)
1421 (defvar anything-additional-attributes nil
1422 "List of all `anything' attributes.")
1424 (defun anything-document-attribute (attribute short-doc &optional long-doc)
1425 "Register ATTRIBUTE documentation introduced by plug-in.
1426 SHORT-DOC is displayed beside attribute name.
1427 LONG-DOC is displayed below attribute name and short documentation."
1428 (if long-doc
1429 (setq short-doc (concat "(" short-doc ")"))
1430 (setq long-doc short-doc
1431 short-doc ""))
1432 (add-to-list 'anything-additional-attributes attribute t)
1433 (put attribute 'anything-attrdoc
1434 (concat "- " (symbol-name attribute)
1435 " " short-doc "\n\n" long-doc "\n")))
1437 (put 'anything-document-attribute 'lisp-indent-function 2)
1439 (defun anything-exit-and-execute-action (action)
1440 "Exit current anything session and execute ACTION.
1441 Argument ACTION is a function called with one arg (candidate)
1442 and part of the actions of current source.
1444 Use this on commands invoked from key-bindings, but not
1445 on action functions invoked as action from the action menu,
1446 i.e functions called with RET."
1447 (setq anything-saved-action action)
1448 (setq anything-saved-selection (or (anything-get-selection) ""))
1449 (anything-exit-minibuffer))
1451 (defun anything-interpret-value (value &optional source)
1452 "Interpret VALUE as variable, function or literal.
1453 If VALUE is a function, call it with no arguments and return the value.
1454 If SOURCE is `anything' source, `anything-source-name' is source name.
1456 If VALUE is a variable, return the value.
1458 If VALUE is a symbol, but it is not a function or a variable, cause an error.
1460 Otherwise, return VALUE itself."
1461 (cond ((and source (functionp value))
1462 (anything-funcall-with-source source value))
1463 ((functionp value)
1464 (funcall value))
1465 ((and (symbolp value) (boundp value))
1466 (symbol-value value))
1467 ((symbolp value)
1468 (error "anything-interpret-value: Symbol must be a function or a variable"))
1470 value)))
1472 (defun anything-once (function &rest args)
1473 "Ensure FUNCTION with ARGS to be called once in `anything' session."
1474 (let ((spec (cons function args)))
1475 (unless (member spec anything-once-called-functions)
1476 (apply function args)
1477 (push spec anything-once-called-functions))))
1480 ;; (@* "Core: API helper")
1481 (defun* anything-empty-buffer-p (&optional (buffer anything-buffer))
1482 "Check if BUFFER have candidates.
1483 Default value for BUFFER is `anything-buffer'."
1484 (zerop (buffer-size (and buffer (get-buffer buffer)))))
1486 (defun anything-let-eval-varlist (varlist)
1487 "Return the list of pairs VARLIST with each cdr of pair evaluated.
1488 If VARLIST contain single elements, those are returned
1489 as a list of one element."
1490 (mapcar (lambda (pair)
1491 (if (listp pair)
1492 (cons (car pair) (eval (cadr pair)))
1493 (cons pair nil)))
1494 varlist))
1496 ;; [NOT USED]
1497 ;; (defun anything-let*-eval-varlist (varlist)
1498 ;; (let ((vars (mapcar (lambda (pair)
1499 ;; (or (car-safe pair) pair))
1500 ;; varlist)))
1501 ;; (eval `(let ,vars
1502 ;; ,@(mapcar (lambda (pair)
1503 ;; (if (listp pair)
1504 ;; `(setq ,(car pair) ,(cadr pair))
1505 ;; `(setq ,pair nil)))
1506 ;; varlist)
1507 ;; (mapcar (lambda (v)
1508 ;; (cons v (symbol-value v)))
1509 ;; ',vars)))))
1511 (defun anything-let-internal (binding bodyfunc)
1512 "Set BINDING to anything buffer-local variables and Evaluate BODYFUNC.
1513 BINDING is a list of \(VARNAME . VALUE\) pair."
1514 (setq anything-let-variables binding)
1515 (unwind-protect
1516 (funcall bodyfunc)
1517 (setq anything-let-variables nil)))
1520 ;; (@* "Core: tools")
1521 (defun anything-current-line-contents ()
1522 "Current line string without properties."
1523 (buffer-substring-no-properties (point-at-bol) (point-at-eol)))
1525 (defun anything-funcall-with-source (source func &rest args)
1526 "Call from SOURCE FUNC list or single function FUNC with ARGS.
1527 FUNC can be a symbol or a list of functions.
1528 Return the result of last function call."
1529 (let ((anything-source-name (assoc-default 'name source))
1530 result)
1531 (anything-log-eval anything-source-name func args)
1532 (dolist (func (if (functionp func) (list func) func) result)
1533 (setq result (apply func args)))))
1535 (defun anything-funcall-foreach (sym)
1536 "Call the function SYM for each source if any."
1537 (dolist (source (anything-get-sources))
1538 (anything-aif (assoc-default sym source)
1539 (anything-funcall-with-source source it))))
1541 (defun anything-normalize-sources (sources)
1542 "If SOURCES is only one source, make a list of one element."
1543 (cond ((or (and sources
1544 (symbolp sources))
1545 (and (listp sources) (assq 'name sources)))
1546 (list sources))
1547 (sources)
1548 (t anything-sources)))
1550 (defun anything-approximate-candidate-number (&optional in-current-source)
1551 "Return approximate number of candidates in `anything-buffer'.
1552 If IN-CURRENT-SOURCE is provided return number of candidates
1553 in the source where point is.
1554 It is used to check if candidate number is 0, 1, or 2+."
1555 (with-current-buffer anything-buffer
1556 (save-excursion
1557 (if in-current-source
1558 (goto-char (anything-get-previous-header-pos))
1559 (goto-char (point-min)))
1560 (forward-line 1)
1561 (let ((count-multi 1))
1562 (if (anything-pos-multiline-p)
1563 (save-excursion
1564 (loop while (and (not (if in-current-source
1565 (save-excursion
1566 (forward-line 2)
1567 (or (anything-pos-header-line-p) (eobp)))
1568 (eobp)))
1569 (search-forward anything-candidate-separator nil t))
1570 do (incf count-multi)
1571 finally return count-multi))
1572 (save-excursion
1573 (loop with ln = 0
1574 while (not (if in-current-source
1575 (or (anything-pos-header-line-p) (eobp))
1576 (eobp)))
1577 unless (anything-pos-header-line-p)
1578 do (incf ln)
1579 do (forward-line 1) finally return ln)))))))
1581 (defmacro with-anything-quittable (&rest body)
1582 "If an error occur in execution of BODY, quit anything safely."
1583 (declare (indent 0) (debug t))
1584 `(let (inhibit-quit)
1585 (condition-case v
1586 (progn ,@body)
1587 (quit (setq anything-quit t)
1588 (exit-minibuffer)
1589 (keyboard-quit)))))
1591 (defun anything-compose (arg-lst func-lst)
1592 "Apply arguments specified in ARG-LST with each function of FUNC-LST.
1593 The result of each function will be the new `car' of ARG-LST.
1595 This function allows easy sequencing of transformer functions."
1596 (dolist (func func-lst)
1597 (setcar arg-lst (apply func arg-lst)))
1598 (car arg-lst))
1600 (defun anything-composed-funcall-with-source (source funcs &rest args)
1601 "With SOURCE apply `anything-funcall-with-source' with each FUNCS and ARGS.
1602 This is used in transformers to modify candidates list."
1603 (if (functionp funcs)
1604 (apply 'anything-funcall-with-source source funcs args)
1605 (apply 'anything-funcall-with-source source
1606 (lambda (&rest args)
1607 (anything-compose args funcs))
1608 args)))
1610 (defun anything-new-timer (variable timer)
1611 "Give VARIABLE value to TIMER and cancel old timer."
1612 (anything-aif (symbol-value variable)
1613 (cancel-timer it))
1614 (set variable timer))
1617 ;; (@* "Core: entry point")
1618 (defconst anything-argument-keys
1619 '(:sources :input :prompt :resume :preselect :buffer :keymap :default :history))
1621 ;;;###autoload
1622 (defun anything (&rest plist)
1623 "Main function to execute anything sources.
1625 Keywords supported:
1626 :sources :input :prompt :resume :preselect :buffer :keymap :default :history
1627 Extra keywords are supported and can be added, see below.
1629 When call interactively with no arguments deprecated `anything-sources'
1630 will be used if non--nil.
1632 PLIST is a list like \(:key1 val1 :key2 val2 ...\) or
1633 \(&optional sources input prompt resume preselect buffer keymap default history\).
1635 Basic keywords are the following:
1637 \:sources
1639 Temporary value of `anything-sources'. It also accepts a
1640 symbol, interpreted as a variable of an anything source. It
1641 also accepts an alist representing an anything source, which is
1642 detected by \(assq 'name ANY-SOURCES\)
1644 \:input
1646 Temporary value of `anything-pattern', ie. initial input of minibuffer.
1648 \:prompt
1650 Prompt other than \"pattern: \".
1652 \:resume
1654 If t, Resurrect previously instance of `anything'. Skip the initialization.
1655 If 'noresume, this instance of `anything' cannot be resumed.
1657 \:preselect
1659 Initially selected candidate. Specified by exact candidate or a regexp.
1661 \:buffer
1663 `anything-buffer' instead of *anything*.
1665 \:keymap
1667 `anything-map' for current `anything' session.
1669 \:default
1671 A default argument that will be inserted in minibuffer \
1672 with \\<minibuffer-local-map>\\[next-history-element].
1673 When nil of not present `thing-at-point' will be used instead.
1675 \:history
1677 By default all minibuffer input is pushed to `minibuffer-history',
1678 if an argument HISTORY is provided, input will be pushed to HISTORY.
1679 History element should be a symbol.
1681 Of course, conventional arguments are supported, the two are same.
1683 \(anything :sources sources :input input :prompt prompt :resume resume
1684 :preselect preselect :buffer buffer :keymap keymap :default default
1685 :history history\)
1686 \(anything sources input prompt resume preselect buffer keymap default history\)
1688 Other keywords are interpreted as local variables of this anything session.
1689 The `anything-' prefix can be omitted. For example,
1691 \(anything :sources 'anything-c-source-buffers
1692 :buffer \"*buffers*\" :candidate-number-limit 10\)
1694 means starting anything session with `anything-c-source-buffers'
1695 source in *buffers* buffer and set variable `anything-candidate-number-limit'
1696 to 10 as session local variable."
1697 (interactive)
1698 (if (keywordp (car plist))
1699 (anything-let-internal
1700 (anything-parse-keys plist)
1701 (lambda ()
1702 (apply 'anything
1703 (mapcar (lambda (key) (plist-get plist key))
1704 anything-argument-keys))))
1705 (apply 'anything-internal plist)))
1707 (defun anything-parse-keys (keys)
1708 "Parse the KEYS arguments of `anything'.
1709 Return only the keys that are not in `anything-argument-keys'.
1710 It is used to set local variables via `anything-let-internal'.
1711 This allow to add arguments that are not part of `anything-argument-keys',
1712 but are valid anything attributes.
1713 i.e :candidate-number-limit will be bound to `anything-candidate-number-limit'
1714 in source.
1715 But the variable is bound, use it.
1717 (anything-parse-keys '(:sources ((name . \"test\")
1718 (candidates . (a b c)))
1719 :buffer \"toto\"
1720 :truncate-lines t
1721 :anything-enable-shortcuts t
1722 :candidate-number-limit 4))
1723 => ((truncate-lines . t)
1724 (anything-enable-shortcuts . t)
1725 (anything-candidate-number-limit . 4))
1727 (loop for (key value) on keys by #'cddr
1728 for symname = (substring (symbol-name key) 1)
1729 for origsym = (intern symname)
1730 for sym = (if (boundp origsym)
1731 origsym
1732 (intern (if (string-match "^anything-" symname)
1733 symname
1734 (concat "anything-" symname))))
1735 unless (memq key anything-argument-keys)
1736 collect (cons sym value)))
1738 ;;; (@* "Core: entry point helper")
1739 (defun anything-internal (&optional
1740 any-sources any-input
1741 any-prompt any-resume
1742 any-preselect any-buffer
1743 any-keymap any-default any-history)
1744 "The internal anything function called by `anything'.
1745 For ANY-SOURCES ANY-INPUT ANY-PROMPT ANY-RESUME ANY-PRESELECT ANY-BUFFER and
1746 ANY-KEYMAP ANY-DEFAULT ANY-HISTORY See `anything'."
1747 (anything-log (concat "[Start session] " (make-string 41 ?+)))
1748 (anything-log-eval any-prompt any-preselect
1749 any-buffer any-keymap any-default)
1750 (let ((old-overridding-local-map overriding-local-map))
1751 (unwind-protect
1752 (condition-case v
1753 (let ( ;; It is needed because `anything-source-name' is non-nil
1754 ;; when `anything' is invoked by action. Awful global scope.
1755 anything-source-name
1756 anything-in-persistent-action
1757 anything-quit
1758 ;; [2012-10-16] move to `anything-update' to show right value of `case-fold-search'
1759 ;; (case-fold-search t)
1760 (anything-buffer (or any-buffer anything-buffer))
1761 ;; cua-mode ; avoid error when region is selected
1763 (with-anything-restore-variables
1764 (anything-initialize any-resume any-input any-sources)
1765 (anything-display-buffer anything-buffer)
1766 (anything-set-margin)
1767 (anything-log "show prompt")
1768 (unwind-protect
1769 (anything-read-pattern-maybe
1770 any-prompt any-input any-preselect
1771 any-resume any-keymap any-default
1772 (when (and any-history (symbolp any-history)) any-history))
1773 (anything-cleanup)))
1774 (prog1 (unless anything-quit
1775 (setq overriding-local-map old-overridding-local-map)
1776 (anything-execute-selection-action-1))
1777 (anything-log (concat "[End session] " (make-string 41 ?-)))))
1778 (quit
1779 (anything-restore-position-on-quit)
1780 (anything-log (concat "[End session (quit)] " (make-string 34 ?-)))
1781 nil))
1782 (setq overriding-local-map old-overridding-local-map)
1783 (anything-log-save-maybe))))
1786 ;;; Anything resume
1789 (defun* anything-resume (&optional
1790 (any-buffer anything-last-buffer)
1791 buffer-pattern (any-resume t))
1792 "Resurrect previously invoked `anything'.
1793 Called with a prefix arg, allow choosing among all existing
1794 anything buffers. i.e choose among various anything sessions."
1795 (interactive)
1796 (when (or current-prefix-arg buffer-pattern)
1797 (setq any-buffer (anything-resume-select-buffer buffer-pattern)))
1798 (setq anything-compiled-sources nil)
1799 (anything
1800 :sources (or (buffer-local-value 'anything-last-sources-local (get-buffer any-buffer))
1801 anything-last-sources anything-sources)
1802 :input (buffer-local-value 'anything-input-local (get-buffer any-buffer))
1803 :resume any-resume
1804 :buffer any-buffer))
1806 ;;; rubikitch: experimental
1807 ;;; I use this and check it whether I am convenient.
1808 ;;; I may introduce an option to control the behavior.
1809 (defun* anything-resume-window-only (&optional
1810 (any-buffer anything-last-buffer)
1811 buffer-pattern)
1812 (interactive)
1813 (anything-resume any-buffer buffer-pattern 'window-only))
1815 (defun anything-resume-p (any-resume)
1816 "Whether current anything session is resumed or not.
1817 Just check if ANY-RESUME value is t or window-only."
1818 (memq any-resume '(t window-only)))
1820 (defun anything-resume-select-buffer (input)
1821 "Resume precedent anything session with initial input INPUT."
1822 (or (anything :sources '(((name . "Resume anything buffer")
1823 (candidates . anything-buffers)
1824 (action . identity)))
1825 :input input
1826 :resume 'noresume
1827 :buffer "*anything resume*")
1828 (keyboard-quit)))
1831 ;;;###autoload
1832 (defun anything-at-point (&optional
1833 any-sources any-input
1834 any-prompt any-resume
1835 any-preselect any-buffer)
1836 "Call anything with symbol at point as initial input.
1837 ANY-SOURCES ANY-INPUT ANY-PROMPT ANY-RESUME ANY-PRESELECT and ANY-BUFFER
1838 are same args as in `anything'."
1839 (interactive)
1840 (anything :sources any-sources
1841 :input (if current-prefix-arg
1842 (concat "\\b" (thing-at-point 'symbol) "\\b"
1843 (if (featurep 'anything-match-plugin) " " ""))
1844 any-input)
1845 :prompt any-prompt
1846 :resume any-resume
1847 :preselect any-preselect
1848 :buffer any-buffer))
1850 ;;;###autoload
1851 (defun anything-other-buffer (any-sources any-buffer)
1852 "Simplified interface of `anything' with other `anything-buffer'.
1853 Call `anything' with only ANY-SOURCES and ANY-BUFFER as args."
1854 (anything :sources any-sources :buffer any-buffer))
1856 (defvar anything-last-frame-or-window-configuration nil)
1858 (defun anything-nest (&rest same-as-anything)
1859 "Allow calling `anything' whithin a running anything session."
1860 (with-anything-window
1861 (let (anything-current-position
1862 anything-current-buffer
1863 (orig-anything-current-buffer anything-current-buffer)
1864 (orig-anything-buffer anything-buffer)
1865 (orig-anything-last-frame-or-window-configuration
1866 anything-last-frame-or-window-configuration)
1867 anything-pattern
1868 (anything-buffer (or (getf same-as-anything :buffer)
1869 (nth 5 same-as-anything)
1870 "*Anything*"))
1871 anything-sources
1872 anything-compiled-sources
1873 (anything-samewindow t)
1874 (enable-recursive-minibuffers t))
1875 (unwind-protect
1876 (apply #'anything same-as-anything)
1877 (with-current-buffer orig-anything-buffer
1878 (anything-initialize-overlays orig-anything-buffer)
1879 (setq anything-buffer (current-buffer))
1880 (anything-mark-current-line)
1881 (setq anything-last-frame-or-window-configuration
1882 orig-anything-last-frame-or-window-configuration)
1883 (setq cursor-type t)
1884 (setq anything-current-buffer orig-anything-current-buffer))))))
1887 ;;; Initialize
1890 (defvar anything-buffers nil
1891 "All of `anything-buffer' in most recently used order.")
1892 (defvar anything-shortcut-update-timer nil)
1894 (defun anything-initialize (any-resume any-input any-sources)
1895 "Start initialization of `anything' session.
1896 For ANY-RESUME ANY-INPUT and ANY-SOURCES See `anything'."
1897 (anything-log "start initialization: any-resume=%S any-input=%S" any-resume any-input)
1898 (when anything-enable-shortcuts
1899 (anything-new-timer
1900 'anything-shortcut-update-timer
1901 (run-with-idle-timer 0.03 0.03 'anything-move-digit-overlay-maybe)))
1902 (loop for (var . val) in anything-let-variables
1903 do (set var val))
1904 (anything-frame-or-window-configuration 'save)
1905 (setq anything-sources (anything-normalize-sources any-sources))
1906 (anything-log "sources = %S" anything-sources)
1907 (anything-hooks 'setup)
1908 (anything-current-position 'save)
1909 (if (anything-resume-p any-resume)
1910 (anything-initialize-overlays (anything-buffer-get))
1911 (anything-initial-setup))
1912 (unless (eq any-resume 'noresume)
1913 (anything-recent-push anything-buffer 'anything-buffers)
1914 (setq anything-last-buffer anything-buffer))
1915 (when any-input (setq anything-input any-input anything-pattern any-input))
1916 (and (anything-resume-p any-resume) (anything-funcall-foreach 'resume))
1917 (anything-log "end initialization"))
1919 (defun anything-execute-selection-action-1 ()
1920 "Execute current action."
1921 (anything-log-run-hook 'anything-before-action-hook)
1922 (unwind-protect
1923 (anything-execute-selection-action)
1924 (anything-aif (get-buffer anything-action-buffer)
1925 (kill-buffer it))
1926 (anything-log-run-hook 'anything-after-action-hook)))
1928 (defun anything-restore-position-on-quit ()
1929 "Restore position in `anything-current-buffer' when quitting."
1930 (anything-current-position 'restore))
1932 (defun anything-recent-push (elt list-var)
1933 "Add ELT to the value of LIST-VAR as most recently used value."
1934 (let ((m (member elt (symbol-value list-var))))
1935 (and m (set list-var (delq (car m) (symbol-value list-var))))
1936 (push elt (symbol-value list-var))))
1939 ;;; (@* "Core: Accessors")
1940 ;;; rubikitch: I love to create functions to control variables.
1941 (defvar anything-current-position nil
1942 "Cons of \(point . window-start\) when `anything' is invoked.
1943 It is needed to restore position in `anything-current-buffer'
1944 when `anything' is keyboard-quitted.")
1945 (defun anything-current-position (save-or-restore)
1946 "Restore or save current position in `anything-current-buffer'.
1947 Argument SAVE-OR-RESTORE is one of save or restore."
1948 (case save-or-restore
1949 (save
1950 (anything-log "Save position at %S" (cons (point) (window-start)))
1951 (setq anything-current-position (cons (point) (window-start))))
1952 (restore
1953 (anything-log "Restore position at %S in buffer %s"
1954 anything-current-position
1955 (buffer-name (current-buffer)))
1956 (goto-char (car anything-current-position))
1957 ;; Fix this position with the NOFORCE arg of `set-window-start'
1958 ;; otherwise, if there is some other buffer than `anything-current-buffer'
1959 ;; one, position will be lost.
1960 (set-window-start (selected-window) (cdr anything-current-position) t))))
1963 ;; Internal.
1964 (defvar anything-last-frame-or-window-configuration nil
1965 "Used to store window or frame configuration when anything start.")
1966 (defun anything-frame-or-window-configuration (save-or-restore)
1967 "Save or restore last frame or window configuration.
1968 Possible value of SAVE-OR-RESTORE are 'save and 'restore.
1969 window or frame configuration is saved/restored according to values of
1970 `anything-save-configuration-functions'."
1971 (anything-log-eval anything-save-configuration-functions)
1972 (case save-or-restore
1973 (save (setq anything-last-frame-or-window-configuration
1974 (funcall (cdr anything-save-configuration-functions))))
1975 (restore (funcall (car anything-save-configuration-functions)
1976 anything-last-frame-or-window-configuration)
1977 ;; Restore frame focus.
1978 (let ((frame (and (listp anything-last-frame-or-window-configuration)
1979 (caadr anything-last-frame-or-window-configuration))))
1980 ;; If `anything-save-configuration-functions' are window functions
1981 ;; frame should be nil, use current frame.
1982 (unless (framep frame)
1983 (setq frame (selected-frame)))
1984 (select-frame-set-input-focus frame)))))
1987 ;; (@* "Core: Display *anything* buffer")
1988 (defun anything-display-buffer (buf)
1989 "Display *anything* buffer BUF."
1990 (let (pop-up-frames)
1991 (funcall (with-current-buffer buf anything-display-function) buf)))
1993 (defun anything-default-display-buffer (buf)
1994 "Default function to display BUF.
1995 Where BUF is generally `anything-buffer'.
1996 It use `switch-to-buffer' or `pop-to-buffer' depending of value of
1997 `anything-samewindow'."
1998 (funcall (if anything-samewindow 'switch-to-buffer 'pop-to-buffer) buf))
2001 ;; (@* "Core: initialize")
2002 (defun anything-initial-setup ()
2003 "Initialize anything settings and set up the anything buffer."
2004 (anything-log-run-hook 'anything-before-initialize-hook)
2005 (setq anything-current-prefix-arg nil)
2006 (setq anything-once-called-functions nil)
2007 (setq anything-delayed-init-executed nil)
2008 (setq anything-alive-p t)
2009 (setq anything-current-buffer
2010 (if (minibuffer-window-active-p (minibuffer-window))
2011 ;; If minibuffer is active be sure to use it's buffer
2012 ;; as `anything-current-buffer'.
2013 (window-buffer (active-minibuffer-window))
2014 (current-buffer)))
2015 (setq anything-buffer-file-name buffer-file-name)
2016 (setq anything-issued-errors nil)
2017 (setq anything-compiled-sources nil)
2018 (setq anything-saved-current-source nil)
2019 (if (or (not (boundp 'split-width-threshold))
2020 (not split-width-threshold)
2021 (and (integerp split-width-threshold)
2022 (>= split-width-threshold (+ (frame-width) 4))))
2023 (setq anything-split-window-state 'vertical)
2024 (setq anything-split-window-state 'horizontal))
2025 ;; Call the init function for sources where appropriate
2026 (anything-funcall-foreach 'init)
2027 (setq anything-pattern "")
2028 (setq anything-input "")
2029 (setq anything-candidate-cache nil)
2030 (setq anything-last-sources anything-sources)
2031 (anything-create-anything-buffer)
2032 (anything-log-run-hook 'anything-after-initialize-hook))
2034 (defvar anything-reading-pattern nil
2035 "Whether in `read-string' in anything or not.")
2037 (defun anything-read-pattern-maybe (any-prompt any-input
2038 any-preselect any-resume any-keymap
2039 any-default any-history)
2040 "Read pattern with prompt ANY-PROMPT and initial input ANY-INPUT.
2041 For ANY-PRESELECT ANY-RESUME ANY-KEYMAP, See `anything'."
2042 (if (anything-resume-p any-resume)
2043 (anything-mark-current-line t)
2044 (anything-update any-preselect))
2045 (with-current-buffer (anything-buffer-get)
2046 (let ((src-keymap (assoc-default 'keymap (anything-get-current-source))))
2047 ;; Startup with the first keymap found either in current source
2048 ;; or anything arg, otherwise use global value of `anything-map'.
2049 ;; This map will be used as a `minibuffer-local-map'.
2050 ;; Maybe it will be overriden when changing source
2051 ;; by `anything-maybe-update-keymap'.
2052 (unless anything-local-map-override-anything-map
2053 (anything-aif (or src-keymap any-keymap)
2054 (ignore-errors
2055 (set-keymap-parent it anything-map))))
2056 (set (make-local-variable 'anything-map)
2057 (or src-keymap any-keymap anything-map))
2058 (anything-log-eval (anything-approximate-candidate-number)
2059 anything-execute-action-at-once-if-one
2060 anything-quit-if-no-candidate)
2061 (cond ((and anything-execute-action-at-once-if-one
2062 (= (anything-approximate-candidate-number) 1))
2063 (ignore))
2064 ((and anything-quit-if-no-candidate
2065 (= (anything-approximate-candidate-number) 0))
2066 (setq anything-quit t)
2067 (and (functionp anything-quit-if-no-candidate)
2068 (funcall anything-quit-if-no-candidate)))
2070 (let ((anything-reading-pattern t)
2071 (tap (or any-default
2072 (with-anything-current-buffer
2073 (thing-at-point 'symbol)))))
2074 (read-from-minibuffer (or any-prompt "pattern: ")
2075 any-input anything-map
2076 nil any-history tap)))))))
2078 (defun anything-maybe-update-keymap ()
2079 "Handle differents keymaps in multiples sources.
2080 This function is meant to be run in `anything-move-selection-after-hook'.
2081 It will override `anything-map' with the keymap attribute of current source
2082 if some when multiples sources are present."
2083 (with-anything-window
2084 (let ((kmap (assoc-default 'keymap (anything-get-current-source))))
2085 (when kmap
2086 (and (not anything-local-map-override-anything-map)
2087 (ignore-errors (set-keymap-parent kmap (default-value 'anything-map))))
2088 (setq overriding-local-map kmap)))))
2089 (add-hook 'anything-move-selection-after-hook 'anything-maybe-update-keymap)
2091 (defun anything-create-anything-buffer (&optional test-mode)
2092 "Create newly created `anything-buffer'.
2093 If TEST-MODE is non-nil, clear `anything-candidate-cache'."
2094 (when test-mode
2095 (setq anything-candidate-cache nil))
2096 (with-current-buffer (get-buffer-create anything-buffer)
2097 (anything-log "kill local variables: %S" (buffer-local-variables))
2098 (kill-all-local-variables)
2099 (set (make-local-variable 'inhibit-read-only) t)
2100 (buffer-disable-undo)
2101 (erase-buffer)
2102 (set (make-local-variable 'inhibit-read-only) t)
2103 (set (make-local-variable 'anything-last-sources-local) anything-sources)
2104 (set (make-local-variable 'anything-follow-mode) nil)
2105 (set (make-local-variable 'anything-display-function) anything-display-function)
2106 (set (make-local-variable 'anything-selection-point) nil)
2107 (anything-initialize-persistent-action)
2108 (anything-log-eval anything-display-function anything-let-variables)
2109 (loop for (var . val) in anything-let-variables
2110 do (set (make-local-variable var) val))
2111 (setq cursor-type nil)
2112 (setq mode-name "Anything"))
2113 (anything-initialize-overlays anything-buffer)
2114 (get-buffer anything-buffer))
2116 (defun anything-initialize-overlays (buffer)
2117 "Initialize anything overlays in BUFFER."
2118 (anything-log "overlay setup")
2119 (if anything-selection-overlay
2120 ;; make sure the overlay belongs to the anything buffer if
2121 ;; it's newly created
2122 (move-overlay anything-selection-overlay (point-min) (point-min)
2123 (get-buffer buffer))
2125 (setq anything-selection-overlay
2126 (make-overlay (point-min) (point-min) (get-buffer buffer)))
2127 (overlay-put anything-selection-overlay 'face anything-selection-face))
2129 (cond (anything-enable-shortcuts
2130 (setq anything-shortcut-keys
2131 (assoc-default anything-enable-shortcuts anything-shortcut-keys-alist))
2132 (unless anything-digit-overlays
2133 (setq anything-digit-overlays
2134 (loop for key across anything-shortcut-keys
2135 for overlay = (make-overlay (point-min) (point-min)
2136 (get-buffer buffer))
2137 do (overlay-put overlay 'before-string
2138 (propertize " " 'display
2139 `((margin left-margin) ,(upcase (make-string 1 key)))))
2140 collect overlay)))
2142 (anything-digit-overlays
2143 (mapc 'delete-overlay anything-digit-overlays)
2144 (setq anything-digit-overlays nil))))
2146 (defun anything-set-margin ()
2147 (when anything-enable-shortcuts
2148 (set-window-margins (get-buffer-window (anything-buffer-get)) 1)))
2150 (defun anything-hooks (setup-or-cleanup)
2151 "Add or remove hooks according to SETUP-OR-CLEANUP value.
2152 if SETUP-OR-CLEANUP value is setup add hooks, any other value
2153 will remove hooks.
2154 hooks concerned are `post-command-hook' and `minibuffer-setup-hook'."
2155 (let ((hooks '((post-command-hook anything-check-minibuffer-input)
2156 (minibuffer-setup-hook anything-print-error-messages))))
2157 (if (eq setup-or-cleanup 'setup)
2158 (dolist (args hooks) (apply 'add-hook args))
2159 (dolist (args (reverse hooks)) (apply 'remove-hook args)))))
2162 ;; (@* "Core: clean up")
2163 ;;; TODO move
2164 (defun anything-cleanup ()
2165 "Clean up the mess when anything exit or quit."
2166 (anything-log "start cleanup")
2167 (with-current-buffer anything-buffer
2168 ;; rubikitch: I think it is not needed.
2169 ;; thierry: If you end up for any reasons (error etc...)
2170 ;; with an anything-buffer staying around (visible),
2171 ;; You will have no cursor in this buffer when switching to it,
2172 ;; so I think this is needed.
2173 (setq cursor-type t)
2174 ;; Call burry-buffer whithout arg
2175 ;; to be sure anything-buffer is removed from window.
2176 (bury-buffer)
2177 ;; Be sure we call this from anything-buffer.
2178 (anything-funcall-foreach 'cleanup))
2179 (anything-new-timer 'anything-shortcut-update-timer nil)
2180 (anything-new-timer 'anything-check-minibuffer-input-timer nil)
2181 (anything-kill-async-processes)
2182 (anything-log-run-hook 'anything-cleanup-hook)
2183 (anything-hooks 'cleanup)
2184 (anything-frame-or-window-configuration 'restore)
2185 (setq anything-alive-p nil)
2186 ;; This is needed in some cases where last input
2187 ;; is yielded infinitely in minibuffer after anything session.
2188 (anything-clean-up-minibuffer))
2190 (defun anything-clean-up-minibuffer ()
2191 "Remove contents of minibuffer."
2192 (let ((miniwin (minibuffer-window)))
2193 ;; Clean only current minibuffer used by anything.
2194 ;; i.e The precedent one is active.
2195 (unless (minibuffer-window-active-p miniwin)
2196 (with-current-buffer (window-buffer miniwin)
2197 (delete-minibuffer-contents)))))
2200 ;; (@* "Core: input handling")
2201 (defun anything-check-minibuffer-input ()
2202 "Extract input string from the minibuffer and check if it needs to be handled."
2203 (when anything-reading-pattern
2204 (let ((delay (with-current-buffer anything-buffer
2205 (and anything-input-idle-delay
2206 (max anything-input-idle-delay 0.1)))))
2207 (if (or (not delay) (anything-action-window))
2208 (anything-check-minibuffer-input-1)
2209 (anything-new-timer
2210 'anything-check-minibuffer-input-timer
2211 (run-with-idle-timer delay nil 'anything-check-minibuffer-input-1))))))
2213 (defun anything-check-minibuffer-input-1 ()
2214 "Check minibuffer content."
2215 (with-anything-quittable
2216 (with-selected-window (or (active-minibuffer-window) (minibuffer-window))
2217 (anything-check-new-input (minibuffer-contents)))))
2219 (defun anything-check-new-input (input)
2220 "Check INPUT string and update the anything buffer if necessary."
2221 (unless (equal input anything-pattern)
2222 (setq anything-pattern input)
2223 (unless (anything-action-window)
2224 (setq anything-input anything-pattern))
2225 (anything-log-eval anything-pattern anything-input)
2226 (anything-update)))
2229 ;; (@* "Core: source compiler")
2230 (defvar anything-compile-source-functions-default anything-compile-source-functions
2231 "Plug-ins this file provides.")
2232 (defun anything-compile-sources (sources funcs)
2233 "Compile SOURCES with FUNCS.
2234 See `anything-compile-source-functions'.
2235 Anything plug-ins are realized by this function."
2236 (mapcar
2237 (lambda (source)
2238 (loop with source = (if (listp source) source (symbol-value source))
2239 for f in funcs
2240 do (setq source (funcall f source))
2241 finally (return source)))
2242 sources))
2245 ;; (@* "Core: plug-in attribute documentation hack")
2247 ;; `anything-document-attribute' is public API.
2248 (defadvice documentation-property (after anything-document-attribute activate)
2249 "Display plug-in attributes' documentation as `anything-sources' docstring."
2250 (when (eq (ad-get-arg 0) 'anything-sources)
2251 (setq ad-return-value
2252 (concat ad-return-value "\n"
2253 (mapconcat (lambda (sym) (get sym 'anything-attrdoc))
2254 anything-additional-attributes
2255 "\n")))))
2256 ;; (describe-variable 'anything-sources)
2257 ;; (documentation-property 'anything-sources 'variable-documentation)
2258 ;; (progn (ad-disable-advice 'documentation-property 'after 'anything-document-attribute) (ad-update 'documentation-property))
2261 ;; (@* "Core: all candidates")
2262 (defun anything-process-delayed-init (source)
2263 "Initialize delayed SOURCE."
2264 (let ((name (assoc-default 'name source)))
2265 (unless (member name anything-delayed-init-executed)
2266 (anything-aif (assoc-default 'delayed-init source)
2267 (with-current-buffer anything-current-buffer
2268 (anything-funcall-with-source source it)
2269 (dolist (f (if (functionp it) (list it) it))
2270 (add-to-list 'anything-delayed-init-executed name)))))))
2272 (defun anything-get-candidates (source)
2273 "Retrieve and return the list of candidates from SOURCE."
2274 (anything-process-delayed-init source)
2275 (let* ((candidate-source (assoc-default 'candidates source))
2276 (type-error (lambda ()
2277 (error (concat "Candidates must either be a function, "
2278 " a variable or a list: %s")
2279 candidate-source)))
2280 (candidates (condition-case err
2281 (anything-interpret-value candidate-source source)
2282 (error (funcall type-error)))))
2283 (cond ((processp candidates) candidates)
2284 ((listp candidates) (anything-transform-candidates candidates source))
2285 (t (funcall type-error)))))
2288 (defun anything-get-cached-candidates (source)
2289 "Return the cached value of candidates for SOURCE.
2290 Cache the candidates if there is not yet a cached value."
2291 (let* ((name (assoc-default 'name source))
2292 (candidate-cache (assoc name anything-candidate-cache)))
2293 (cond (candidate-cache
2294 (anything-log "use cached candidates")
2295 (cdr candidate-cache))
2297 (anything-log "calculate candidates")
2298 (let ((candidates (anything-get-candidates source)))
2299 (cond ((processp candidates)
2300 (push (cons candidates
2301 (append source
2302 (list (cons 'item-count 0)
2303 (cons 'incomplete-line ""))))
2304 anything-async-processes)
2305 (set-process-filter candidates 'anything-output-filter)
2306 (setq candidates nil))
2307 ((not (assoc 'volatile source))
2308 (setq candidate-cache (cons name candidates))
2309 (push candidate-cache anything-candidate-cache)))
2310 candidates)))))
2313 ;;; (@* "Core: candidate transformers")
2314 (defun anything-transform-mapcar (function args)
2315 "`mapcar' for candidate-transformer.
2317 ARGS is (cand1 cand2 ...) or ((disp1 . real1) (disp2 . real2) ...)
2319 \(anything-transform-mapcar 'upcase '(\"foo\" \"bar\"))
2320 => (\"FOO\" \"BAR\")
2321 \(anything-transform-mapcar 'upcase '((\"1st\" . \"foo\") (\"2nd\" . \"bar\")))
2322 => ((\"1st\" . \"FOO\") (\"2nd\" . \"BAR\"))
2324 (loop for arg in args
2325 if (consp arg)
2326 collect (cons (car arg) (funcall function (cdr arg)))
2327 else
2328 collect (funcall function arg)))
2330 (defun anything-process-candidate-transformer (candidates source)
2331 "Execute candidate-transformer function on all CANDIDATES of SOURCE."
2332 (anything-aif (assoc-default 'candidate-transformer source)
2333 (anything-composed-funcall-with-source source it candidates)
2334 candidates))
2336 (defun anything-process-filtered-candidate-transformer (candidates source)
2337 "Execute filtered-candidate-transformer function on all CANDIDATES of SOURCE."
2338 (anything-aif (assoc-default 'filtered-candidate-transformer source)
2339 (anything-composed-funcall-with-source source it candidates source)
2340 candidates))
2342 (defun anything-process-filtered-candidate-transformer-maybe (candidates source process-p)
2343 "Execute filtered-candidate-transformer function on all CANDIDATES of SOURCE.
2344 This happen if PROCESS-P is non-nil."
2345 (if process-p
2346 (anything-process-filtered-candidate-transformer candidates source)
2347 candidates))
2349 (defun anything-process-real-to-display (candidates source)
2350 "Execute real-to-display function on all CANDIDATES of SOURCE."
2351 (anything-aif (assoc-default 'real-to-display source)
2352 (setq candidates (anything-funcall-with-source
2353 source 'mapcar
2354 (lambda (cand_)
2355 (if (consp cand_)
2356 ;; override DISPLAY from candidate-transformer
2357 (cons (funcall it (cdr cand_)) (cdr cand_))
2358 (cons (funcall it cand_) cand_)))
2359 candidates))
2360 candidates))
2362 (defun anything-transform-candidates (candidates source &optional process-p)
2363 "Transform CANDIDATES of SOURCE according to candidate transformers.
2364 This happen if PROCESS-P is non-nil."
2365 (anything-process-real-to-display
2366 (anything-process-filtered-candidate-transformer-maybe
2367 (anything-process-candidate-transformer candidates source) source process-p)
2368 source))
2371 ;; (@* "Core: narrowing candidates")
2372 (defun anything-candidate-number-limit (source)
2373 "Apply candidate-number-limit attribute value.
2374 This overhide variable `anything-candidate-number-limit'.
2376 e.g:
2377 If \(candidate-number-limit\) is in SOURCE, show all candidates in SOURCE.
2378 If \(candidate-number-limit . 123\) is in SOURCE limit candidate to 123."
2379 (anything-aif (assq 'candidate-number-limit source)
2380 (or (cdr it) 99999999)
2381 (or anything-candidate-number-limit 99999999)))
2383 ;; FIXME: Why a defconst here
2384 (defconst anything-default-match-functions
2385 (list (lambda (candidate)
2386 (string-match anything-pattern candidate)))
2387 "Default functions to match candidates according to `anything-pattern'.")
2389 (defun anything-compute-matches (source)
2390 "Compute matched results from SOURCE according to its settings."
2391 (if debug-on-error
2392 (anything-compute-matches-internal source)
2393 (condition-case v
2394 (anything-compute-matches-internal source)
2395 (error (anything-log-error
2396 "anything-compute-matches: error when processing source: %s"
2397 (assoc-default 'name source))
2398 nil))))
2400 (defun anything-candidate-get-display (candidate)
2401 "Get searched display part from CANDIDATE.
2402 CANDIDATE is a string, a symbol, or \(DISPLAY . REAL\) cons cell."
2403 (format "%s" (or (car-safe candidate) candidate)))
2405 (defun anything-process-pattern-transformer (pattern source)
2406 "Execute pattern-transformer attribute PATTERN function in SOURCE."
2407 (anything-aif (assoc-default 'pattern-transformer source)
2408 (anything-composed-funcall-with-source source it pattern)
2409 pattern))
2411 (defun anything-match-functions (source)
2412 (or (assoc-default 'match source)
2413 anything-default-match-functions))
2415 (defmacro anything-accumulate-candidates-internal (cand newmatches
2416 hash item-count limit)
2417 "Internal, add CAND into NEWMATCHES.
2418 Use HASH to uniq NEWMATCHES.
2419 Argument ITEM-COUNT count the matches.
2420 if ITEM-COUNT reaches LIMIT, exit from inner loop."
2421 `(unless (gethash ,cand ,hash)
2422 (puthash ,cand t ,hash)
2423 (push ,cand ,newmatches)
2424 (incf ,item-count)
2425 (when (= ,item-count ,limit)
2426 (setq exit t)
2427 (return))))
2429 (defun anything-take-first-elements (seq n)
2430 (if (> (length seq) n)
2431 (setq seq (subseq seq 0 n))
2432 seq))
2434 (defun anything-match-from-candidates (cands matchfns limit)
2435 (let (matches)
2436 (condition-case nil
2437 (let ((item-count 0) exit)
2438 (clrhash anything-match-hash)
2439 (dolist (match matchfns)
2440 (let (newmatches)
2441 (dolist (candidate cands)
2442 (when (funcall match (anything-candidate-get-display candidate))
2443 (anything-accumulate-candidates-internal
2444 candidate newmatches anything-match-hash item-count limit)))
2445 (setq matches (append matches (reverse newmatches)))
2446 (if exit (return)))))
2447 (invalid-regexp (setq matches nil)))
2448 matches))
2450 (defun anything-compute-matches-internal (source)
2451 (save-current-buffer
2452 (let ((matchfns (anything-match-functions source))
2453 (anything-source-name (assoc-default 'name source))
2454 (limit (anything-candidate-number-limit source))
2455 (anything-pattern (anything-process-pattern-transformer
2456 anything-pattern source)))
2457 (anything-process-filtered-candidate-transformer
2458 (if (or (equal anything-pattern "") (equal matchfns '(identity)))
2459 (anything-take-first-elements
2460 (anything-get-cached-candidates source) limit)
2461 (anything-match-from-candidates
2462 (anything-get-cached-candidates source) matchfns limit))
2463 source))))
2465 ;; (anything '(((name . "error")(candidates . (lambda () (hage))) (action . identity))))
2467 (defun anything-process-source (source)
2468 "Display matched results from SOURCE according to its settings."
2469 (anything-log-eval (assoc-default 'name source))
2470 (if (assq 'direct-insert-match source) ;experimental
2471 (anything-process-source--direct-insert-match source)
2472 (let ((matches (anything-compute-matches source)))
2473 (when matches
2474 (when anything-test-mode
2475 (setq anything-test-candidate-list
2476 `(,@anything-test-candidate-list
2477 (,(assoc-default 'name source)
2478 ,matches))))
2479 (anything-insert-header-from-source source)
2480 (if (not (assq 'multiline source))
2481 (mapc 'anything-insert-match-with-digit-overlay matches)
2482 (let ((start (point)) separate)
2483 (dolist (match matches)
2484 (if separate
2485 (anything-insert-candidate-separator)
2486 (setq separate t))
2487 (anything-insert-match-with-digit-overlay match))
2488 (put-text-property start (point) 'anything-multiline t)))))))
2490 (defun anything-insert-match-with-digit-overlay (match)
2491 (declare (special source))
2492 (anything-insert-match match 'insert source))
2494 (defun anything-put-digit-overlay-maybe ()
2495 (when (and anything-enable-shortcuts
2496 (not (eq anything-digit-shortcut-count
2497 (length anything-digit-overlays))))
2498 (move-overlay (nth anything-digit-shortcut-count
2499 anything-digit-overlays)
2500 (point-at-bol)
2501 (point-at-bol))
2502 (incf anything-digit-shortcut-count)))
2504 (defun anything-move-digit-overlay-maybe ()
2505 (with-anything-window
2506 (save-excursion
2507 (goto-char (window-start))
2508 (loop for overlay in anything-digit-overlays
2509 unless (eobp)
2511 (anything-skip-noncandidate-line 'forward)
2512 (move-overlay overlay (point-at-bol) (point-at-bol)
2513 (get-buffer (anything-buffer-get)))
2514 (anything-next-candidate-internal)))))
2516 (defun anything-process-source--direct-insert-match (source)
2517 "[EXPERIMENTAL] Insert candidates from `anything-candidate-buffer' in SOURCE."
2518 (anything-log-eval (assoc-default 'name source))
2519 (let ((anything-source-name (assoc-default 'name source))
2520 content-buf)
2521 (funcall (assoc-default 'candidates source))
2522 (setq content-buf (anything-candidate-buffer))
2523 (unless (anything-empty-buffer-p content-buf)
2524 (anything-insert-header-from-source source)
2525 (insert-buffer-substring content-buf)
2526 ;; TODO call anything-put-digit-overlay-maybe with loop
2529 (defun anything-process-delayed-sources (delayed-sources &optional preselect)
2530 "Process anything DELAYED-SOURCES.
2531 Move selection to string or regexp PRESELECT if non--nil.
2532 This function is called in `anything-process-delayed-sources-timer'
2533 when emacs is idle for `anything-idle-delay'."
2534 (with-anything-quittable
2535 (anything-log-eval (mapcar (lambda (s) (assoc-default 'name s)) delayed-sources))
2536 (with-current-buffer anything-buffer
2537 (save-excursion
2538 (goto-char (point-max))
2539 (mapc 'anything-process-source delayed-sources)
2540 (when (and (not (anything-empty-buffer-p))
2541 ;; No selection yet.
2542 (= (overlay-start anything-selection-overlay)
2543 (overlay-end anything-selection-overlay)))
2544 (anything-update-move-first-line 'without-hook)))
2545 (when preselect (anything-preselect preselect))
2546 (save-excursion
2547 (goto-char (point-min))
2548 (anything-log-run-hook 'anything-update-hook))
2549 (anything-log-run-hook 'anything-after-update-hook))))
2552 ;; (@* "Core: *anything* buffer contents")
2553 (defvar anything-input-local nil)
2554 (defvar anything-process-delayed-sources-timer nil)
2555 (defun anything-update (&optional preselect)
2556 "Update candidates list in `anything-buffer' according to `anything-pattern'.
2557 Argument PRESELECT is a string or regexp used to move selection to a particular
2558 place once updating is done. It should be used on single source because search
2559 is done on whole `anything-buffer' and not on current source."
2560 (anything-log "start update")
2561 (setq anything-digit-shortcut-count 0)
2562 (anything-kill-async-processes)
2563 (with-current-buffer (anything-buffer-get)
2564 (set (make-local-variable 'anything-input-local) anything-pattern)
2565 (erase-buffer)
2566 (when anything-enable-shortcuts
2567 (mapc 'delete-overlay anything-digit-overlays))
2568 (let (delayed-sources
2569 (case-fold-search t)
2570 normal-sources)
2571 (unwind-protect ; Process normal sources and store delayed one's.
2572 (setq delayed-sources
2573 (loop for source in (remove-if-not 'anything-update-source-p
2574 (anything-get-sources))
2575 if (anything-delayed-source-p source)
2576 collect source
2577 else do (progn (push source normal-sources)
2578 (anything-process-source source))))
2579 (anything-log-eval
2580 (mapcar (lambda (s) (assoc-default 'name s)) delayed-sources))
2581 (if anything-test-mode ; Need only to process sources.
2582 (mapc 'anything-process-source delayed-sources)
2583 (cond ((and preselect delayed-sources normal-sources)
2584 ;; Preselection run here when there is
2585 ;; normal AND delayed sources.
2586 (anything-log "Update preselect candidate %s" preselect)
2587 (anything-preselect preselect))
2588 (delayed-sources ; Preselection and hooks will run later.
2589 (anything-update-move-first-line 'without-hook)
2591 (t ; No delayed sources, run the hooks now.
2592 (anything-update-move-first-line)
2593 (anything-log-run-hook 'anything-after-update-hook)
2594 (when preselect
2595 (anything-log "Update preselect candidate %s" preselect)
2596 (anything-preselect preselect))))
2597 (when delayed-sources
2598 (anything-new-timer
2599 'anything-process-delayed-sources-timer
2600 (run-with-idle-timer
2601 ;; Be sure anything-idle-delay is >
2602 ;; to anything-input-idle-delay
2603 ;; otherwise use value of anything-input-idle-delay
2604 ;; or 0.1 if == to 0.
2605 (max anything-idle-delay anything-input-idle-delay 0.1) nil
2606 'anything-process-delayed-sources delayed-sources preselect))))
2607 (anything-log "end update"))))
2608 (anything-move-digit-overlay-maybe))
2610 (defun anything-update-source-p (source)
2611 "Wheter SOURCE need updating or not."
2612 (and (or (not anything-source-filter)
2613 (member (assoc-default 'name source) anything-source-filter))
2614 (>= (length anything-pattern)
2615 (anything-aif (assoc 'requires-pattern source)
2616 (or (cdr it) 1)
2617 0))))
2619 (defun anything-delayed-source-p (source)
2620 "Wheter SOURCE is a delayed source or not."
2621 (or (assoc 'delayed source)
2622 (and anything-quick-update
2623 (< (window-height (get-buffer-window (current-buffer)))
2624 (line-number-at-pos (point-max))))))
2626 (defun anything-update-move-first-line (&optional without-hook)
2627 "Goto first line of `anything-buffer'."
2628 (goto-char (point-min))
2629 (unless without-hook
2630 (save-excursion (anything-log-run-hook 'anything-update-hook)))
2631 (anything-next-line))
2633 (defun anything-force-update (&optional preselect)
2634 "Force recalculation and update of candidates.
2635 If current source has `update' attribute, a function without argument,
2636 call it before update."
2637 (interactive)
2638 (let ((source (anything-get-current-source))
2639 (selection (anything-get-selection nil t)))
2640 (when source
2641 (mapc 'anything-force-update--reinit
2642 (anything-get-sources)))
2643 (anything-update preselect)
2644 ;; If preselect arg exists, `anything-update' should
2645 ;; have moved to selection, otherwise do it now.
2646 (unless preselect
2647 (anything-keep-selection (assoc-default 'name source) selection))
2648 (with-anything-window (recenter))))
2650 (defun anything-force-update--reinit (source)
2651 "Reinit SOURCE by calling his update and/or init functions."
2652 (anything-aif (anything-funcall-with-source
2653 source 'anything-candidate-buffer)
2654 (kill-buffer it))
2655 (dolist (attr '(update init))
2656 (anything-aif (assoc-default attr source)
2657 (anything-funcall-with-source source it)))
2658 (anything-remove-candidate-cache source))
2660 (defun anything-keep-selection (source selection)
2661 "Switch to SOURCE and goto SELECTION."
2662 (when (and source selection)
2663 (with-anything-window
2664 (anything-goto-source source)
2665 (forward-char -1)
2666 (if (search-forward selection nil t)
2667 (forward-line 0)
2668 (goto-char (point-min))
2669 (forward-line 1))
2670 (anything-mark-current-line))))
2672 (defun anything-remove-candidate-cache (source)
2673 "Remove SOURCE from `anything-candidate-cache'."
2674 (setq anything-candidate-cache
2675 (delete (assoc (assoc-default 'name source)
2676 anything-candidate-cache)
2677 anything-candidate-cache)))
2679 (defun anything-insert-match (match insert-function source)
2680 "Insert MATCH into `anything-buffer' with INSERT-FUNCTION for SOURCE.
2681 If MATCH is a list then insert the string intended to appear on the display
2682 and store the real value in a text property."
2683 (let ((start (point-at-bol (point)))
2684 (dispvalue (or (car-safe match) match))
2685 (realvalue (cdr-safe match)))
2686 (setq dispvalue
2687 (cond ((symbolp dispvalue) (symbol-name dispvalue))
2688 ((numberp dispvalue) (number-to-string dispvalue))
2689 (t dispvalue)))
2690 (when (stringp dispvalue)
2691 (funcall insert-function dispvalue)
2692 ;; Some sources with candidates-in-buffer have already added
2693 ;; 'anything-realvalue property when creating candidate buffer.
2694 (unless (get-text-property start 'anything-realvalue)
2695 (and realvalue
2696 (put-text-property start (point-at-eol)
2697 'anything-realvalue realvalue)))
2698 (when anything-source-in-each-line-flag
2699 (put-text-property start (point-at-eol) 'anything-source source))
2700 (funcall insert-function "\n"))))
2702 (defun anything-insert-header-from-source (source)
2703 "Insert SOURCE name in `anything-buffer' header.
2704 Maybe insert by overlay additional info after source name if SOURCE have
2705 header-name attribute."
2706 (let ((name (assoc-default 'name source)))
2707 (anything-insert-header
2708 name
2709 (anything-aif (assoc-default 'header-name source)
2710 (anything-funcall-with-source source it name)))))
2712 (defun anything-insert-header (name &optional display-string)
2713 "Insert header of source NAME into the anything buffer.
2714 If DISPLAY-STRING is non--nil and a string, display this additional info
2715 after the source name by overlay."
2716 (unless (bobp)
2717 (let ((start (point)))
2718 (insert "\n")
2719 (put-text-property start (point) 'anything-header-separator t)))
2720 (let ((start (point)))
2721 (insert name)
2722 (put-text-property (point-at-bol)
2723 (point-at-eol) 'anything-header t)
2724 (when display-string
2725 (overlay-put (make-overlay (point-at-bol) (point-at-eol))
2726 'display display-string))
2727 (insert "\n")
2728 (put-text-property start (point) 'face anything-header-face)))
2730 (defun anything-insert-candidate-separator ()
2731 "Insert separator of candidates into the anything buffer."
2732 (insert anything-candidate-separator)
2733 (put-text-property (point-at-bol)
2734 (point-at-eol) 'anything-candidate-separator t)
2735 (insert "\n"))
2738 ;; (@* "Core: async process")
2739 (defun anything-output-filter (process string)
2740 "From PROCESS process output STRING."
2741 (anything-output-filter-1 (assoc process anything-async-processes) string))
2743 (defun anything-output-filter-1 (process-assoc string)
2744 (anything-log-eval string)
2745 (with-current-buffer anything-buffer
2746 (let ((source (cdr process-assoc)))
2747 (save-excursion
2748 (anything-aif (assoc-default 'insertion-marker source)
2749 (goto-char it)
2750 (goto-char (point-max))
2751 (anything-insert-header-from-source source)
2752 (setcdr process-assoc
2753 (append source `((insertion-marker . ,(point-marker))))))
2754 (anything-output-filter--process-source
2755 (car process-assoc) string source
2756 (anything-candidate-number-limit source))))
2757 (anything-output-filter--post-process)))
2759 (defun anything-output-filter--process-source (process string source limit)
2760 (dolist (candidate (anything-transform-candidates
2761 (anything-output-filter--collect-candidates
2762 (split-string string "\n")
2763 (assoc 'incomplete-line source))
2764 source t))
2765 (if (not (assq 'multiline source))
2766 (anything-insert-match candidate 'insert-before-markers source)
2767 (let ((start (point)))
2768 (anything-insert-candidate-separator)
2769 (anything-insert-match candidate 'insert-before-markers source)
2770 (put-text-property start (point) 'anything-multiline t)))
2771 (incf (cdr (assoc 'item-count source)))
2772 (when (>= (assoc-default 'item-count source) limit)
2773 (anything-kill-async-process process)
2774 (return))))
2776 (defun anything-output-filter--collect-candidates (lines incomplete-line-info)
2777 (anything-log-eval (cdr incomplete-line-info))
2778 (butlast
2779 (loop for line in lines collect
2780 (if (cdr incomplete-line-info)
2781 (prog1
2782 (concat (cdr incomplete-line-info) line)
2783 (setcdr incomplete-line-info nil))
2784 line)
2785 finally (setcdr incomplete-line-info line))))
2787 (defun anything-output-filter--post-process ()
2788 (anything-log-run-hook 'anything-update-hook)
2789 (anything-aif (get-buffer-window anything-buffer 'visible)
2790 (save-selected-window
2791 (select-window it)
2792 (anything-skip-noncandidate-line 'next)
2793 (anything-mark-current-line))))
2795 (defun anything-kill-async-processes ()
2796 "Kill all known asynchronous processes of `anything-async-processes'."
2797 (mapc 'anything-kill-async-process (mapcar 'car anything-async-processes))
2798 (setq anything-async-processes nil))
2800 (defun anything-kill-async-process (process)
2801 "Kill PROCESS and detach the associated functions."
2802 (set-process-filter process nil)
2803 (delete-process process))
2806 ;; (@* "Core: action")
2807 (defun anything-execute-selection-action (&optional
2808 selection action
2809 preserve-saved-action)
2810 "If a candidate SELECTION is present then perform the associated ACTION on it.
2811 If PRESERVE-SAVED-ACTION is non-nil don't save action."
2812 (anything-log "executing action")
2813 (setq action (anything-get-default-action
2814 (or action
2815 anything-saved-action
2816 (if (get-buffer anything-action-buffer)
2817 (anything-get-selection anything-action-buffer)
2818 (anything-get-action)))))
2819 (let ((source (or anything-saved-current-source
2820 (anything-get-current-source))))
2821 (setq selection (or selection
2822 (anything-get-selection)
2823 (and (assoc 'accept-empty source) "")))
2824 (unless preserve-saved-action (setq anything-saved-action nil))
2825 (if (and selection action)
2826 (anything-funcall-with-source
2827 source action
2828 (anything-coerce-selection selection source)))))
2830 (defun anything-coerce-selection (selection source)
2831 "Apply coerce attribute function to SELECTION in SOURCE.
2832 Coerce source with coerce function."
2833 (anything-aif (assoc-default 'coerce source)
2834 (anything-funcall-with-source source it selection)
2835 selection))
2837 (defun anything-get-default-action (action)
2838 "Get the first ACTION value of action list in source."
2839 (if (and (listp action) (not (functionp action)))
2840 (cdar action)
2841 action))
2843 (defun anything-select-action ()
2844 "Select an action for the currently selected candidate.
2845 If action buffer is selected, back to the anything buffer."
2846 (interactive)
2847 (anything-log-run-hook 'anything-select-action-hook)
2848 (cond ((get-buffer-window anything-action-buffer 'visible)
2849 (set-window-buffer (get-buffer-window anything-action-buffer)
2850 anything-buffer)
2851 (kill-buffer anything-action-buffer)
2852 (anything-set-pattern anything-input 'noupdate))
2854 (setq anything-saved-selection (anything-get-selection))
2855 (unless anything-saved-selection
2856 (error "Nothing is selected"))
2857 (setq anything-saved-current-source (anything-get-current-source))
2858 (let ((actions (anything-get-action)))
2859 (if (functionp actions)
2860 (message "Sole action: %s" actions)
2861 (anything-show-action-buffer actions)
2862 (anything-delete-minibuffer-contents)
2863 (setq anything-pattern 'dummy) ; so that it differs from the previous one
2864 (anything-check-minibuffer-input))))))
2866 (defun anything-fall-back-to-minibuffer-complete ()
2867 (call-interactively 'minibuffer-complete))
2869 (defun anything-show-action-buffer (actions)
2870 (with-current-buffer (get-buffer-create anything-action-buffer)
2871 (erase-buffer)
2872 (buffer-disable-undo)
2873 (set-window-buffer (get-buffer-window anything-buffer) anything-action-buffer)
2874 (set (make-local-variable 'anything-sources)
2875 `(((name . "Actions")
2876 (volatile)
2877 (candidates . ,actions)
2878 (candidate-number-limit))))
2879 (set (make-local-variable 'anything-source-filter) nil)
2880 (set (make-local-variable 'anything-selection-overlay) nil)
2881 (set (make-local-variable 'anything-digit-overlays) nil)
2882 (anything-set-margin)
2883 (anything-initialize-overlays anything-action-buffer)))
2886 ;; (@* "Core: selection")
2887 (defun anything-move-selection-common (move-func unit direction)
2888 "Move the selection marker to a new position wit function MOVE-FUNC.
2889 It is determined by UNIT and DIRECTION."
2890 (unless (or (anything-empty-buffer-p (anything-buffer-get))
2891 (not (anything-window)))
2892 (with-anything-window
2893 (anything-log-run-hook 'anything-move-selection-before-hook)
2894 (funcall move-func)
2895 (anything-skip-noncandidate-line direction)
2896 (anything-display-source-at-screen-top-maybe unit)
2897 (when (anything-get-previous-header-pos)
2898 (anything-mark-current-line))
2899 (anything-display-mode-line (anything-get-current-source))
2900 (anything-log-run-hook 'anything-move-selection-after-hook))))
2902 (defun anything-display-source-at-screen-top-maybe (unit)
2903 (when (and anything-display-source-at-screen-top (eq unit 'source))
2904 (set-window-start (selected-window)
2905 (save-excursion (forward-line -1) (point)))))
2907 (defun anything-skip-noncandidate-line (direction)
2908 (anything-skip-header-and-separator-line direction)
2909 (and (bobp) (forward-line 1)) ;skip first header
2910 (and (eobp) (forward-line -1))) ;avoid last empty line
2913 (defun anything-skip-header-and-separator-line (direction)
2914 (while (and (not (bobp))
2915 (or (anything-pos-header-line-p)
2916 (anything-pos-candidate-separator-p)))
2917 (forward-line (if (and (eq direction 'previous)
2918 (not (eq (point-at-bol) (point-min))))
2919 -1 1))))
2921 (defvar anything-mode-line-string-real nil)
2922 (defun anything-display-mode-line (source)
2923 (set (make-local-variable 'anything-mode-line-string)
2924 (anything-interpret-value (or (assoc-default 'mode-line source)
2925 (default-value 'anything-mode-line-string))
2926 source))
2927 (if anything-mode-line-string
2928 (setq mode-line-format
2929 '(" " mode-line-buffer-identification " "
2930 (line-number-mode "L%l") " " (anything-follow-mode "(F) ")
2931 (:eval (anything-show-candidate-number
2932 (when (listp anything-mode-line-string)
2933 (car anything-mode-line-string))))
2934 " " anything-mode-line-string-real "-%-")
2935 anything-mode-line-string-real
2936 (substitute-command-keys (if (listp anything-mode-line-string)
2937 (cadr anything-mode-line-string)
2938 anything-mode-line-string)))
2939 (setq mode-line-format
2940 (default-value 'mode-line-format)))
2941 (setq header-line-format
2942 (if (anything-action-window)
2943 "Select Action"
2944 (anything-interpret-value (assoc-default 'header-line source) source))))
2946 (defun anything-show-candidate-number (&optional name)
2947 "Used to display candidate number in mode-line.
2948 You can specify NAME of candidates e.g \"Buffers\" otherwise
2949 it is \"Candidate\(s\)\" by default."
2950 (propertize
2951 (format "[%s %s]"
2952 (anything-approximate-candidate-number 'in-current-source)
2953 (or name "Candidate(s)"))
2954 'face 'anything-candidate-number))
2956 (defun anything-previous-line ()
2957 "Move selection to the previous line."
2958 (interactive)
2959 (anything-move-selection-common
2960 (lambda ()
2961 (if (not (anything-pos-multiline-p))
2962 (forward-line -1) ;double forward-line is meaningful
2963 (forward-line -1) ;because evaluation order is important
2964 (anything-skip-header-and-separator-line 'previous)
2965 (let ((header-pos (anything-get-previous-header-pos))
2966 (separator-pos (anything-get-previous-candidate-separator-pos)))
2967 (when header-pos
2968 (goto-char (if (or (null separator-pos) (< separator-pos header-pos))
2969 header-pos ; first candidate
2970 separator-pos))
2971 (forward-line 1)))))
2972 'line 'previous))
2974 (defun anything-next-candidate-internal ()
2975 "Go to next candidate line.
2976 Skip candidate separators for multiline sources.
2977 Do `forward-line' for non-multiline sources."
2978 (if (not (anything-pos-multiline-p))
2979 (forward-line 1)
2980 (let ((header-pos (anything-get-next-header-pos))
2981 (separator-pos (anything-get-next-candidate-separator-pos)))
2982 (cond ((and separator-pos
2983 (or (null header-pos) (< separator-pos header-pos)))
2984 (goto-char separator-pos))
2985 (header-pos
2986 (goto-char header-pos))))))
2988 (defun anything-next-line ()
2989 "Move selection to the next line."
2990 (interactive)
2991 (anything-move-selection-common 'anything-next-candidate-internal 'line 'next))
2993 (defun anything-previous-page ()
2994 "Move selection back with a pageful."
2995 (interactive)
2996 (anything-move-selection-common
2997 (lambda ()
2998 (condition-case nil
2999 (scroll-down)
3000 (beginning-of-buffer (goto-char (point-min)))))
3001 'page 'previous))
3003 (defun anything-next-page ()
3004 "Move selection forward with a pageful."
3005 (interactive)
3006 (anything-move-selection-common
3007 (lambda ()
3008 (condition-case nil
3009 (scroll-up)
3010 (end-of-buffer (goto-char (point-max)))))
3011 'page 'next))
3013 (defun anything-beginning-of-buffer ()
3014 "Move selection at the top."
3015 (interactive)
3016 (anything-move-selection-common
3017 (lambda () (goto-char (point-min)))
3018 'edge 'previous))
3020 (defun anything-end-of-buffer ()
3021 "Move selection at the bottom."
3022 (interactive)
3023 (anything-move-selection-common
3024 (lambda () (goto-char (point-max)))
3025 'edge 'next))
3027 (defun anything-previous-source ()
3028 "Move selection to the previous source."
3029 (interactive)
3030 (anything-move-selection-common
3031 (lambda ()
3032 (forward-line -1)
3033 (if (bobp)
3034 (goto-char (point-max))
3035 (anything-skip-header-and-separator-line 'previous))
3036 (goto-char (anything-get-previous-header-pos))
3037 (forward-line 1))
3038 'source 'previous))
3040 (defun anything-next-source ()
3041 "Move selection to the next source."
3042 (interactive)
3043 (anything-move-selection-common
3044 (lambda ()
3045 (goto-char (or (anything-get-next-header-pos) (point-min))))
3046 'source 'next))
3048 (defun anything-goto-source (source-or-name)
3049 "Move the selection to the source SOURCE-OR-NAME."
3050 (anything-move-selection-common
3051 (lambda ()
3052 (goto-char (point-min))
3053 (let ((name (if (stringp source-or-name) source-or-name
3054 (assoc-default 'name source-or-name))))
3055 (condition-case err
3056 (while (not (string= name (anything-current-line-contents)))
3057 (goto-char (anything-get-next-header-pos)))
3058 (error (message "")))))
3059 'source 'next))
3061 (defun anything-mark-current-line (&optional resumep)
3062 "Move `anything-selection-overlay' to current line.
3063 Note that this is not related with visibles marks, which are used
3064 to mark candidates."
3065 (with-anything-window
3066 (when resumep
3067 (goto-char anything-selection-point))
3068 (move-overlay
3069 anything-selection-overlay (point-at-bol)
3070 (if (anything-pos-multiline-p)
3071 (let ((header-pos (anything-get-next-header-pos))
3072 (separator-pos (anything-get-next-candidate-separator-pos)))
3073 (or (and (null header-pos) separator-pos)
3074 (and header-pos separator-pos (< separator-pos header-pos)
3075 separator-pos)
3076 header-pos
3077 (point-max)))
3078 (1+ (point-at-eol))))
3079 (setq anything-selection-point (overlay-start anything-selection-overlay)))
3080 (anything-follow-execute-persistent-action-maybe))
3082 (defun anything-this-command-key ()
3083 (cons (event-basic-type (elt (this-command-keys-vector) 0)) 0))
3084 ;; (progn (read-key-sequence "Key: ") (p (anything-this-command-key)))
3086 (defun anything-read-prefix-shortcut ()
3087 (let ((key0 (read-event "Select shortcut key (1-9 for action): ")))
3088 (if (<= ?1 key0 ?9)
3089 (cons (read-event "Select shortcut key (candidate): ") (- key0 ?1))
3090 (cons key0 0))))
3092 (defun anything-select-with-shortcut-internal (types get-key-func)
3093 (if (memq anything-enable-shortcuts types)
3094 (save-selected-window
3095 (select-window (anything-window))
3096 (let* ((pair (funcall get-key-func))
3097 (key (car pair))
3098 (action-index (cdr pair))
3099 (overlay (ignore-errors (nth (position key anything-shortcut-keys)
3100 anything-digit-overlays))))
3101 (if (not (and overlay (overlay-buffer overlay)))
3102 (when (numberp key)
3103 (select-window (minibuffer-window))
3104 (self-insert-command 1))
3105 (goto-char (overlay-start overlay))
3106 (anything-mark-current-line)
3107 (if (anything-action-window)
3108 (anything-exit-minibuffer)
3109 (anything-select-nth-action action-index)))))
3110 (self-insert-command 1)))
3112 (defun anything-select-with-prefix-shortcut ()
3113 "Invoke default action with prefix shortcut."
3114 (interactive)
3115 (anything-select-with-shortcut-internal
3116 '(prefix) 'anything-read-prefix-shortcut))
3118 (defun anything-select-with-digit-shortcut ()
3119 "Invoke default action with digit/alphabet shortcut."
3120 (interactive)
3121 (anything-select-with-shortcut-internal
3122 '(alphabet t) 'anything-this-command-key))
3124 ;; (setq anything-enable-shortcuts 'prefix)
3125 ;; (define-key anything-map "@" 'anything-select-with-prefix-shortcut)
3126 ;; (define-key anything-map (kbd "<f18>") 'anything-select-with-prefix-shortcut)
3128 (defvar anything-exit-status 0
3129 "Flag to inform whether anything have exited or quitted.
3130 Exit with 0 mean anything have exited executing an action.
3131 Exit with 1 mean anything have quitted with \\[keyboard-quit]
3132 It is useful for example to restore a window config if anything abort
3133 in special cases.
3134 See `anything-exit-minibuffer' and `anything-keyboard-quit'.")
3136 (defvar anything-minibuffer-confirm-state nil)
3137 (defun anything-confirm-and-exit-minibuffer ()
3138 "Maybe ask for confirmation when exiting anything.
3139 It is similar to `minibuffer-complete-and-exit' adapted to anything.
3140 If `minibuffer-completion-confirm' value is 'confirm,
3141 send in minibuffer confirm message and exit on next hit.
3142 If `minibuffer-completion-confirm' value is t,
3143 don't exit and send message 'no match'."
3144 (interactive)
3145 (let ((empty-buffer-p (with-current-buffer anything-buffer
3146 (eq (point-min) (point-max))))
3147 (unknow (string= (get-text-property
3148 0 'display (anything-get-selection nil 'withprop))
3149 "[?]")))
3150 (cond ((and (or empty-buffer-p unknow)
3151 (eq minibuffer-completion-confirm 'confirm))
3152 (setq anything-minibuffer-confirm-state
3153 'confirm)
3154 (setq minibuffer-completion-confirm nil)
3155 (minibuffer-message " [confirm]"))
3156 ((and (or empty-buffer-p unknow)
3157 (eq minibuffer-completion-confirm t))
3158 (minibuffer-message " [No match]"))
3160 (setq anything-minibuffer-confirm-state nil)
3161 (anything-exit-minibuffer)))))
3162 (add-hook 'anything-after-update-hook 'anything-confirm-and-exit-hook)
3164 (defun anything-confirm-and-exit-hook ()
3165 "Restore `minibuffer-completion-confirm' when anything update."
3166 (unless (or (eq minibuffer-completion-confirm t)
3167 (not anything-minibuffer-confirm-state))
3168 (setq minibuffer-completion-confirm
3169 anything-minibuffer-confirm-state)))
3171 (defun anything-exit-minibuffer ()
3172 "Select the current candidate by exiting the minibuffer."
3173 (interactive)
3174 (unless anything-current-prefix-arg
3175 (setq anything-current-prefix-arg current-prefix-arg))
3176 (setq anything-exit-status 0)
3177 (exit-minibuffer))
3179 (defun anything-keyboard-quit ()
3180 "Quit minibuffer in anything.
3181 If action buffer is displayed, kill it."
3182 (interactive)
3183 (when (get-buffer-window anything-action-buffer 'visible)
3184 (kill-buffer anything-action-buffer))
3185 (setq anything-exit-status 1)
3186 (abort-recursive-edit))
3188 (defun anything-get-next-header-pos ()
3189 "Return the position of the next header from point."
3190 (next-single-property-change (point) 'anything-header))
3192 (defun anything-get-previous-header-pos ()
3193 "Return the position of the previous header from point."
3194 (previous-single-property-change (point) 'anything-header))
3196 (defun anything-pos-multiline-p ()
3197 "Return non-nil if the current position is in the multiline source region."
3198 (get-text-property (point) 'anything-multiline))
3200 (defun anything-get-next-candidate-separator-pos ()
3201 "Return the position of the next candidate separator from point."
3202 (next-single-property-change (point) 'anything-candidate-separator))
3204 (defun anything-get-previous-candidate-separator-pos ()
3205 "Return the position of the previous candidate separator from point."
3206 (previous-single-property-change (point) 'anything-candidate-separator))
3208 (defun anything-pos-header-line-p ()
3209 "Return t if the current line is a header line."
3210 (or (get-text-property (point-at-bol) 'anything-header)
3211 (get-text-property (point-at-bol) 'anything-header-separator)))
3213 (defun anything-pos-candidate-separator-p ()
3214 "Return t if the current line is a candidate separator."
3215 (get-text-property (point-at-bol) 'anything-candidate-separator))
3218 ;; (@* "Core: help")
3219 (defun anything-help-internal (bufname insert-content-fn)
3220 "Show long message during `anything' session in BUFNAME.
3221 INSERT-CONTENT-FN is the text to be displayed in BUFNAME."
3222 (save-window-excursion
3223 (select-window (anything-window))
3224 (delete-other-windows)
3225 (switch-to-buffer (get-buffer-create bufname))
3226 (erase-buffer)
3227 (funcall insert-content-fn)
3228 (setq mode-line-format "%b (SPC,C-v:NextPage b,M-v:PrevPage other:Exit)")
3229 (setq cursor-type nil)
3230 (goto-char 1)
3231 (anything-help-event-loop)))
3233 (defun anything-help-event-loop ()
3234 (ignore-errors
3235 (loop for event = (read-event) do
3236 (case event
3237 ((?\C-v ? ) (scroll-up))
3238 ((?\M-v ?b) (scroll-down))
3239 (t (return))))))
3241 (defun anything-help ()
3242 "Help of `anything'."
3243 (interactive)
3244 (anything-help-internal
3245 " *Anything Help*"
3246 (lambda ()
3247 (insert (substitute-command-keys
3248 (anything-interpret-value (or (assoc-default
3249 'help-message
3250 (anything-get-current-source))
3251 anything-help-message))))
3252 (org-mode))))
3254 (defun anything-debug-output ()
3255 "Show all anything-related variables at this time."
3256 (interactive)
3257 (anything-help-internal " *Anything Debug*" 'anything-debug-output-function))
3259 (defun anything-debug-output-function (&optional vars)
3260 (message "Calculating all anything-related values...")
3261 (insert "If you debug some variables or forms, set `anything-debug-forms'
3262 to a list of forms.\n\n")
3263 (dolist (v (or vars
3264 anything-debug-forms
3265 (apropos-internal "^anything-" 'boundp)))
3266 (insert "** "
3267 (pp-to-string v) "\n"
3268 (pp-to-string (with-current-buffer anything-buffer (eval v))) "\n"))
3269 (message "Calculating all anything-related values...Done"))
3272 ;; (@* "Core: misc")
3273 (defun anything-kill-buffer-hook ()
3274 "Remove tick entry from `anything-tick-hash' when killing a buffer."
3275 (loop for key being the hash-keys in anything-tick-hash
3276 if (string-match (format "^%s/" (regexp-quote (buffer-name))) key)
3277 do (remhash key anything-tick-hash)))
3278 (add-hook 'kill-buffer-hook 'anything-kill-buffer-hook)
3280 (defun anything-preselect (candidate-or-regexp)
3281 "Move `anything-selection-overlay' to CANDIDATE-OR-REGEXP on startup."
3282 (with-anything-window
3283 (when candidate-or-regexp
3284 (goto-char (point-min))
3285 ;; go to first candidate of first source
3286 (forward-line 1)
3287 (let ((start (point)))
3288 (or (re-search-forward
3289 (concat "^" (regexp-quote candidate-or-regexp) "$") nil t)
3290 (re-search-forward candidate-or-regexp nil t)
3291 (search-forward candidate-or-regexp nil t)
3292 (goto-char start))))
3293 (anything-mark-current-line)))
3295 (defun anything-delete-current-selection ()
3296 "Delete the currently selected item."
3297 (interactive)
3298 (with-anything-window
3299 (cond ((anything-pos-multiline-p)
3300 (anything-aif (anything-get-next-candidate-separator-pos)
3301 (delete-region (point-at-bol)
3302 (1+ (progn (goto-char it) (point-at-eol))))
3303 ;; last candidate
3304 (goto-char (anything-get-previous-candidate-separator-pos))
3305 (delete-region (point-at-bol) (point-max)))
3306 (when (anything-end-of-source-p)
3307 (goto-char (or (anything-get-previous-candidate-separator-pos)
3308 (point-min)))
3309 (forward-line 1)))
3311 (delete-region (point-at-bol) (1+ (point-at-eol)))
3312 (when (anything-end-of-source-p) (forward-line -1))))
3313 (anything-mark-current-line)))
3315 (defun anything-end-of-source-p ()
3316 "Return non--nil if we are at eob or end of source."
3317 (save-excursion
3318 (forward-line 1)
3319 (or (eq (point-at-bol) (point-at-eol))
3320 (anything-pos-header-line-p)
3321 (eobp))))
3323 (defun anything-edit-current-selection-internal (func)
3324 (with-anything-window
3325 (beginning-of-line)
3326 (let ((realvalue (get-text-property (point) 'anything-realvalue)))
3327 (funcall func)
3328 (beginning-of-line)
3329 (and realvalue
3330 (put-text-property (point) (point-at-eol)
3331 'anything-realvalue realvalue))
3332 (anything-mark-current-line))))
3334 (defmacro anything-edit-current-selection (&rest forms)
3335 "Evaluate FORMS at current selection in the anything buffer.
3336 You can edit the line."
3337 (declare (indent 0) (debug t))
3338 `(anything-edit-current-selection-internal
3339 (lambda () ,@forms)))
3341 (defun anything-set-pattern (pattern &optional noupdate)
3342 "Set minibuffer contents to PATTERN.
3343 if optional NOUPDATE is non-nil, anything buffer is not changed."
3344 (with-selected-window (or (active-minibuffer-window) (minibuffer-window))
3345 (delete-minibuffer-contents)
3346 (insert pattern))
3347 (when noupdate
3348 (anything-set-margin)
3349 (setq anything-pattern pattern)
3350 (anything-hooks 'cleanup)
3351 (run-with-idle-timer 0 nil 'anything-hooks 'setup)))
3353 (defun anything-delete-minibuffer-contents ()
3354 "Same as `delete-minibuffer-contents' but this is a command."
3355 (interactive)
3356 (anything-set-pattern ""))
3357 (defalias 'anything-delete-minibuffer-content 'anything-delete-minibuffer-contents)
3360 ;;; Plugins
3362 ;; (@* "Built-in plug-in: type")
3363 (defun anything-compile-source--type (source)
3364 (anything-aif (assoc-default 'type source)
3365 (append source (assoc-default it anything-type-attributes) nil)
3366 source))
3368 ;; `define-anything-type-attribute' is public API.
3370 (defun anything-add-type-attribute (type definition)
3371 (anything-aif (assq type anything-type-attributes)
3372 (setq anything-type-attributes (delete it anything-type-attributes)))
3373 (push (cons type definition) anything-type-attributes))
3375 (defvar anything-types nil)
3376 (defun anything-document-type-attribute (type doc)
3377 (add-to-list 'anything-types type t)
3378 (put type 'anything-typeattrdoc
3379 (concat "- " (symbol-name type) "\n\n" doc "\n")))
3381 (defadvice documentation-property (after anything-document-type-attribute activate)
3382 "Display type attributes' documentation as `anything-type-attributes' docstring."
3383 (when (eq (ad-get-arg 0) 'anything-type-attributes)
3384 (setq ad-return-value
3385 (concat ad-return-value "\n\n++++ Types currently defined ++++\n"
3386 (mapconcat (lambda (sym) (get sym 'anything-typeattrdoc))
3387 anything-types "\n")))))
3389 ;; (@* "Built-in plug-in: dummy")
3390 (defun anything-dummy-candidate (candidate source)
3391 "Use `anything-pattern' as CANDIDATE in SOURCE."
3392 ;; `source' is defined in filtered-candidate-transformer
3393 (list anything-pattern))
3395 (defun anything-compile-source--dummy (source)
3396 (if (assoc 'dummy source)
3397 (append source
3398 '((candidates "dummy")
3399 (accept-empty)
3400 (match identity)
3401 (filtered-candidate-transformer . anything-dummy-candidate)
3402 ;;(disable-shortcuts)
3403 (volatile)))
3404 source))
3406 ;; (@* "Built-in plug-in: disable-shortcuts")
3407 (defvar anything-orig-enable-shortcuts nil)
3408 (defun anything-save-enable-shortcuts ()
3409 (anything-once
3410 (lambda ()
3411 (setq anything-orig-enable-shortcuts anything-enable-shortcuts
3412 anything-enable-shortcuts nil))))
3414 (defun anything-compile-source--disable-shortcuts (source)
3415 (if (assoc 'disable-shortcuts source)
3416 (append `((init ,@(anything-mklist (assoc-default 'init source))
3417 anything-save-enable-shortcuts)
3418 (resume ,@(anything-mklist (assoc-default 'resume source))
3419 anything-save-enable-shortcuts)
3420 (cleanup ,@(anything-mklist (assoc-default 'cleanup source))
3421 (lambda () (setq anything-enable-shortcuts
3422 anything-orig-enable-shortcuts))))
3423 source)
3424 source))
3426 ;; (@* "Built-in plug-in: candidates-in-buffer")
3427 (defun anything-candidates-in-buffer ()
3428 "Get candidates from the candidates buffer according to `anything-pattern'.
3430 BUFFER is `anything-candidate-buffer' by default. Each
3431 candidate must be placed in one line. This function is meant to
3432 be used in candidates-in-buffer or candidates attribute of an
3433 anything source. Especially fast for many (1000+) candidates.
3436 '((name . \"many files\")
3437 (init . (lambda () (with-current-buffer (anything-candidate-buffer 'local)
3438 (insert-many-filenames))))
3439 (search re-search-forward) ; optional
3440 (candidates-in-buffer)
3441 (type . file))
3443 +===============================================================+
3444 | The new way of making and narrowing candidates: Using buffers |
3445 +===============================================================+
3447 By default, `anything' makes candidates by evaluating the
3448 candidates function, then narrows them by `string-match' for each
3449 candidate.
3451 But this way is very slow for many candidates. The new way is
3452 storing all candidates in a buffer and narrowing them by
3453 `re-search-forward'. Search function is customizable by search
3454 attribute. The important point is that buffer processing is MUCH
3455 FASTER than string list processing and is the Emacs way.
3457 The init function writes all candidates to a newly-created
3458 candidate buffer. The candidates buffer is created or specified
3459 by `anything-candidate-buffer'. Candidates are stored in a line.
3461 The candidates function narrows all candidates, IOW creates a
3462 subset of candidates dynamically. It is the task of
3463 `anything-candidates-in-buffer'. As long as
3464 `anything-candidate-buffer' is used,`(candidates-in-buffer)' is
3465 sufficient in most cases.
3467 Note that `(candidates-in-buffer)' is shortcut of three attributes:
3468 (candidates . anything-candidates-in-buffer)
3469 (volatile)
3470 (match identity)
3471 And `(candidates-in-buffer . func)' is shortcut of three attributes:
3472 (candidates . func)
3473 (volatile)
3474 (match identity)
3475 The expansion is performed in `anything-get-sources'.
3477 The candidates-in-buffer attribute implies the volatile attribute.
3478 The volatile attribute is needed because `anything-candidates-in-buffer'
3479 creates candidates dynamically and need to be called everytime
3480 `anything-pattern' changes.
3482 Because `anything-candidates-in-buffer' plays the role of `match' attribute
3483 function, specifying `(match identity)' makes the source slightly faster.
3485 To customize `anything-candidates-in-buffer' behavior, use search,
3486 get-line and search-from-end attributes. See also `anything-sources' docstring."
3487 (declare (special source))
3488 (anything-candidates-in-buffer-1
3489 (anything-candidate-buffer)
3490 anything-pattern
3491 (or (assoc-default 'get-line source)
3492 #'buffer-substring-no-properties)
3493 ;; use external variable `source'.
3494 (or (assoc-default 'search source)
3495 (if (assoc 'search-from-end source)
3496 '(anything-candidates-in-buffer-search-from-end)
3497 '(anything-candidates-in-buffer-search-from-start)))
3498 (anything-candidate-number-limit source)
3499 (assoc 'search-from-end source)))
3501 (defun anything-candidates-in-buffer-search-from-start (pattern)
3502 "Search PATTERN with `re-search-forward' with bound and noerror args."
3503 (re-search-forward pattern nil t))
3505 (defun anything-candidates-in-buffer-search-from-end (pattern)
3506 "Search PATTERN with `re-search-backward' with bound and noerror args."
3507 (re-search-backward pattern nil t))
3509 (defun anything-candidates-in-buffer-1 (buffer pattern get-line-fn
3510 search-fns limit search-from-end)
3511 ;; buffer == nil when candidates buffer does not exist.
3512 (when buffer
3513 (with-current-buffer buffer
3514 (let ((start-point (if search-from-end (point-max) (point-min)))
3515 (endp (if search-from-end #'bobp #'eobp)))
3516 (goto-char (1- start-point))
3517 (if (string= pattern "")
3518 (anything-initial-candidates-from-candidate-buffer
3519 endp get-line-fn limit search-from-end)
3520 (anything-search-from-candidate-buffer
3521 pattern get-line-fn search-fns limit search-from-end
3522 start-point endp))))))
3524 (defun anything-point-is-moved (proc)
3525 "If point is moved after executing PROC, return t, otherwise nil."
3526 (/= (point) (progn (funcall proc) (point))))
3528 (defun anything-search-from-candidate-buffer (pattern get-line-fn search-fns
3529 limit search-from-end
3530 start-point endp)
3531 (let (buffer-read-only
3532 matches exit newmatches)
3533 (anything-search-from-candidate-buffer-internal
3534 (lambda ()
3535 (clrhash anything-cib-hash)
3536 (dolist (searcher search-fns)
3537 (goto-char start-point)
3538 (setq newmatches nil)
3539 (loop with item-count = 0
3540 while (funcall searcher pattern)
3541 for cand = (funcall get-line-fn (point-at-bol) (point-at-eol))
3542 do (anything-accumulate-candidates-internal
3543 cand newmatches anything-cib-hash item-count limit)
3544 unless (anything-point-is-moved
3545 (lambda ()
3546 (if search-from-end
3547 (goto-char (1- (point-at-bol)))
3548 (forward-line 1))))
3549 return nil)
3550 (setq matches (append matches (nreverse newmatches)))
3551 (if exit (return)))
3552 (delq nil matches)))))
3554 (defun anything-initial-candidates-from-candidate-buffer (endp get-line-fn limit search-from-end)
3555 (delq nil (loop with next-line-fn =
3556 (if search-from-end
3557 (lambda (x) (goto-char (max (1- (point-at-bol)) 1)))
3558 #'forward-line)
3559 until (funcall endp)
3560 for i from 1 to limit
3561 collect (funcall get-line-fn (point-at-bol) (point-at-eol))
3562 do (funcall next-line-fn 1))))
3564 (defun anything-search-from-candidate-buffer-internal (search-fn)
3565 (goto-char (point-min))
3566 (insert "\n")
3567 (goto-char (point-max))
3568 (insert "\n")
3569 (unwind-protect
3570 (funcall search-fn)
3571 (goto-char (point-min))
3572 (delete-char 1)
3573 (goto-char (1- (point-max)))
3574 (delete-char 1)
3576 (set-buffer-modified-p nil)))
3578 (defun anything-candidate-buffer (&optional create-or-buffer)
3579 "Register and return a buffer containing candidates of current source.
3580 `anything-candidate-buffer' searches buffer-local candidates buffer first,
3581 then global candidates buffer.
3583 Acceptable values of CREATE-OR-BUFFER:
3585 - nil (omit)
3586 Only return the candidates buffer.
3587 - a buffer
3588 Register a buffer as a candidates buffer.
3589 - 'global
3590 Create a new global candidates buffer,
3591 named \" *anything candidates:SOURCE*\".
3592 - other non-nil value
3593 Create a new local candidates buffer,
3594 named \" *anything candidates:SOURCE*ANYTHING-CURRENT-BUFFER\"."
3595 (let* ((global-bname (format " *anything candidates:%s*"
3596 anything-source-name))
3597 (local-bname (format " *anything candidates:%s*%s"
3598 anything-source-name
3599 (buffer-name anything-current-buffer))))
3600 (cl-flet ((register-func ()
3601 (setq anything-candidate-buffer-alist
3602 (cons (cons anything-source-name create-or-buffer)
3603 (delete (assoc anything-source-name
3604 anything-candidate-buffer-alist)
3605 anything-candidate-buffer-alist))))
3606 (kill-buffers-func ()
3607 (loop for b in (buffer-list)
3608 if (string-match (format "^%s" (regexp-quote global-bname))
3609 (buffer-name b))
3610 do (kill-buffer b)))
3611 (create-func ()
3612 (with-current-buffer
3613 (get-buffer-create (if (eq create-or-buffer 'global)
3614 global-bname
3615 local-bname))
3616 (buffer-disable-undo)
3617 (erase-buffer)
3618 (font-lock-mode -1)))
3619 (return-func ()
3620 (or (get-buffer local-bname)
3621 (get-buffer global-bname)
3622 (anything-aif (assoc-default anything-source-name
3623 anything-candidate-buffer-alist)
3624 (and (buffer-live-p it) it)))))
3625 (when create-or-buffer
3626 (register-func)
3627 (unless (bufferp create-or-buffer)
3628 (and (eq create-or-buffer 'global) (kill-buffers-func))
3629 (create-func)))
3630 (return-func))))
3632 (defun anything-compile-source--candidates-in-buffer (source)
3633 (anything-aif (assoc 'candidates-in-buffer source)
3634 (append source
3635 `((candidates . ,(or (cdr it) 'anything-candidates-in-buffer))
3636 (volatile) (match identity)))
3637 source))
3640 ;; (@* "Utility: resplit anything window")
3641 (defun anything-toggle-resplit-window ()
3642 "Toggle resplit anything window, vertically or horizontally."
3643 (interactive)
3644 (with-anything-window
3645 (let ((before-height (window-height)))
3646 (delete-window)
3647 (set-window-buffer
3648 (select-window (if (= (window-height) before-height)
3649 (prog1
3650 (split-window-vertically)
3651 (setq anything-split-window-state 'vertical))
3652 (setq anything-split-window-state 'horizontal)
3653 (split-window-horizontally)))
3654 anything-buffer)
3655 (anything-set-margin))))
3657 ;; (@* "Utility: Resize anything window.")
3658 (defun anything-enlarge-window-1 (n)
3659 "Enlarge or narrow anything window.
3660 If N is positive enlarge, if negative narrow."
3661 (unless anything-samewindow
3662 (let ((horizontal-p (eq anything-split-window-state 'horizontal)))
3663 (with-anything-window
3664 (enlarge-window n horizontal-p)))))
3666 (defun anything-narrow-window ()
3667 "Narrow anything window."
3668 (interactive)
3669 (anything-enlarge-window-1 -1))
3671 (defun anything-enlarge-window ()
3672 "Enlarge anything window."
3673 (interactive)
3674 (anything-enlarge-window-1 1))
3676 ;; (@* "Utility: select another action by key")
3677 (defun anything-select-nth-action (n)
3678 "Select the N nth action for the currently selected candidate."
3679 (setq anything-saved-selection (anything-get-selection))
3680 (unless anything-saved-selection
3681 (error "Nothing is selected"))
3682 (setq anything-saved-action (anything-get-nth-action n (anything-get-action)))
3683 (anything-exit-minibuffer))
3685 (defun anything-get-nth-action (n action)
3686 (cond ((and (zerop n) (functionp action))
3687 action)
3688 ((listp action)
3689 (or (cdr (elt action n))
3690 (error "No such action")))
3691 ((and (functionp action) (< 0 n))
3692 (error "Sole action"))
3694 (error "Error in `anything-select-nth-action'"))))
3696 (defun anything-select-2nd-action ()
3697 "Select the 2nd action for the currently selected candidate."
3698 (interactive)
3699 (anything-select-nth-action 1))
3701 (defun anything-select-3rd-action ()
3702 "Select the 3rd action for the currently selected candidate."
3703 (interactive)
3704 (anything-select-nth-action 2))
3706 (defun anything-select-4th-action ()
3707 "Select the 4th action for the currently selected candidate."
3708 (interactive)
3709 (anything-select-nth-action 3))
3711 (defun anything-select-2nd-action-or-end-of-line ()
3712 "Select the 2nd action for the currently selected candidate.
3713 This happen when point is at the end of minibuffer.
3714 Otherwise goto the end of minibuffer."
3715 (interactive)
3716 (if (eolp)
3717 (anything-select-nth-action 1)
3718 (end-of-line)))
3720 ;; (@* "Utility: Persistent Action")
3721 (defmacro with-anything-display-same-window (&rest body)
3722 "Execute BODY in the window used for persistent action.
3723 Make `pop-to-buffer' and `display-buffer' display in the same window."
3724 (declare (indent 0) (debug t))
3725 `(let ((display-buffer-function 'anything-persistent-action-display-buffer))
3726 ,@body))
3728 (defvar anything-persistent-action-display-window nil)
3729 (defun anything-initialize-persistent-action ()
3730 (set (make-local-variable 'anything-persistent-action-display-window) nil))
3732 (defun* anything-execute-persistent-action (&optional (attr 'persistent-action) onewindow)
3733 "Perform the associated action ATTR without quitting anything.
3734 ATTR default is 'persistent-action', but it can be anything else.
3735 In this case you have to add this new attribute to your source.
3736 When `anything-samewindow' and ONEWINDOW are non--nil,
3737 the anything window is never split in persistent action."
3738 (interactive)
3739 (anything-log "executing persistent-action")
3740 (with-anything-window
3741 (save-selected-window
3742 (anything-select-persistent-action-window onewindow)
3743 (anything-log-eval (current-buffer))
3744 (let ((anything-in-persistent-action t))
3745 (with-anything-display-same-window
3746 (anything-execute-selection-action
3748 (or (assoc-default attr (anything-get-current-source))
3749 (anything-get-action))
3751 (anything-log-run-hook 'anything-after-persistent-action-hook))))))
3754 (defun anything-persistent-action-display-window (&optional onewindow)
3755 "Return the window that will be used for presistent action.
3756 If ONEWINDOW is non--nil window will not be splitted in persistent action
3757 if `anything-samewindow' is non--nil also."
3758 (with-anything-window
3759 (setq anything-persistent-action-display-window
3760 (cond ((window-live-p anything-persistent-action-display-window)
3761 anything-persistent-action-display-window)
3762 ((and anything-samewindow (one-window-p t) (not onewindow))
3763 (split-window))
3764 ((get-buffer-window anything-current-buffer))
3766 (next-window (selected-window) 1))))))
3768 (defun anything-select-persistent-action-window (&optional onewindow)
3769 "Select the window that will be used for persistent action.
3770 See `anything-persistent-action-display-window' for how to use ONEWINDOW."
3771 (select-window (get-buffer-window (anything-buffer-get)))
3772 (select-window
3773 (setq minibuffer-scroll-window
3774 (anything-persistent-action-display-window onewindow))))
3776 (defun anything-persistent-action-display-buffer (buf &optional not-this-window)
3777 "Make `pop-to-buffer' and `display-buffer' display in the same window.
3778 If `anything-persistent-action-use-special-display' is non-nil and
3779 BUF is to be displayed by `special-display-function', use it.
3780 Otherwise ignores `special-display-buffer-names' and `special-display-regexps'.
3781 Argument NOT-THIS-WINDOW if present will be used as
3782 second argument of `display-buffer'."
3783 (let* ((name (buffer-name buf))
3784 display-buffer-function pop-up-windows pop-up-frames
3785 (same-window-regexps
3786 (unless (and anything-persistent-action-use-special-display
3787 (or (member name
3788 (mapcar (lambda (x) (or (car-safe x) x))
3789 special-display-buffer-names))
3790 (remove-if-not
3791 (lambda (x) (string-match (or (car-safe x) x) name))
3792 special-display-regexps)))
3793 '("."))))
3794 (display-buffer buf not-this-window)))
3796 ;; scroll-other-window(-down)? for persistent-action
3797 (defun anything-scroll-other-window-base (command)
3798 (with-selected-window (anything-persistent-action-display-window)
3799 (funcall command anything-scroll-amount)))
3801 (defun anything-scroll-other-window ()
3802 "Scroll other window (not *Anything* window) upward."
3803 (interactive)
3804 (anything-scroll-other-window-base 'scroll-up))
3806 (defun anything-scroll-other-window-down ()
3807 "Scroll other window (not *Anything* window) downward."
3808 (interactive)
3809 (anything-scroll-other-window-base 'scroll-down))
3812 ;; (@* "Utility: Visible Mark")
3813 (defface anything-visible-mark
3814 '((((min-colors 88) (background dark))
3815 (:background "green1" :foreground "black"))
3816 (((background dark)) (:background "green" :foreground "black"))
3817 (((min-colors 88)) (:background "green1"))
3818 (t (:background "green")))
3819 "Face for visible mark."
3820 :group 'anything)
3822 (defvar anything-visible-mark-face 'anything-visible-mark)
3823 (defvar anything-visible-mark-overlays nil)
3825 (defun anything-clear-visible-mark ()
3826 (with-current-buffer (anything-buffer-get)
3827 (mapc 'delete-overlay anything-visible-mark-overlays)
3828 (set (make-local-variable 'anything-visible-mark-overlays) nil)))
3829 (add-hook 'anything-after-initialize-hook 'anything-clear-visible-mark)
3831 (defvar anything-marked-candidates nil
3832 "Marked candadates. List of \(source . real\) pair.")
3834 (defun anything-this-visible-mark ()
3835 (loop for o in anything-visible-mark-overlays
3836 when (equal (point-at-bol) (overlay-start o))
3837 return o))
3839 (defun anything-delete-visible-mark (overlay)
3840 (setq anything-marked-candidates
3841 (remove
3842 (cons (anything-get-current-source) (anything-get-selection))
3843 anything-marked-candidates))
3844 (delete-overlay overlay)
3845 (setq anything-visible-mark-overlays
3846 (delq overlay anything-visible-mark-overlays)))
3848 (defun anything-make-visible-mark ()
3849 (let ((o (make-overlay (point-at-bol) (1+ (point-at-eol)))))
3850 (overlay-put o 'face anything-visible-mark-face)
3851 (overlay-put o 'source (assoc-default 'name (anything-get-current-source)))
3852 (overlay-put o 'string (buffer-substring (overlay-start o) (overlay-end o)))
3853 (overlay-put o 'real (anything-get-selection))
3854 (add-to-list 'anything-visible-mark-overlays o))
3855 (push (cons (anything-get-current-source) (anything-get-selection))
3856 anything-marked-candidates))
3858 (defun anything-toggle-visible-mark ()
3859 "Toggle anything visible mark at point."
3860 (interactive)
3861 (with-anything-window
3862 (anything-aif (anything-this-visible-mark)
3863 (anything-delete-visible-mark it)
3864 (anything-make-visible-mark))
3865 (anything-next-line)))
3867 (defun anything-display-all-visible-marks ()
3868 "Show all `anything' visible marks strings."
3869 (interactive)
3870 (with-anything-window
3871 (lexical-let ((overlays (reverse anything-visible-mark-overlays)))
3872 (anything-run-after-quit
3873 (lambda ()
3874 (with-output-to-temp-buffer "*anything visible marks*"
3875 (dolist (o overlays) (princ (overlay-get o 'string)))))))))
3877 (defun anything-marked-candidates ()
3878 "Return marked candidates of current source if any.
3879 Otherwise one element list of current selection.
3881 It is analogous to `dired-get-marked-files'."
3882 (with-current-buffer (anything-buffer-get)
3883 (let ((cands
3884 (if anything-marked-candidates
3885 (loop with current-src = (anything-get-current-source)
3886 for (source . real) in (reverse anything-marked-candidates)
3887 when (equal current-src source)
3888 collect (anything-coerce-selection real source))
3889 (list (anything-get-selection)))))
3890 (anything-log-eval cands)
3891 cands)))
3893 (defun anything-reset-marked-candidates ()
3894 (with-current-buffer (anything-buffer-get)
3895 (set (make-local-variable 'anything-marked-candidates) nil)))
3897 (add-hook 'anything-after-initialize-hook 'anything-reset-marked-candidates)
3898 ;; (add-hook 'anything-after-action-hook 'anything-reset-marked-candidates)
3900 (defun anything-current-source-name= (name)
3901 (save-excursion
3902 (goto-char (anything-get-previous-header-pos))
3903 (equal name (anything-current-line-contents))))
3905 (defun anything-revive-visible-mark ()
3906 "Restore marked candidates when anything update display."
3907 (with-current-buffer anything-buffer
3908 (dolist (o anything-visible-mark-overlays)
3909 (goto-char (point-min))
3910 (while (and (search-forward (overlay-get o 'string) nil t)
3911 (anything-current-source-name= (overlay-get o 'source)))
3912 ;; Calculate real value of candidate.
3913 ;; It can be nil if candidate have only a display value.
3914 (let ((real (get-text-property (point-at-bol 0) 'anything-realvalue)))
3915 (if real
3916 ;; Check if real value of current candidate is the same
3917 ;; that the one stored in overlay.
3918 (and (string= (overlay-get o 'real) real)
3919 (move-overlay o (point-at-bol 0) (1+ (point-at-eol 0))))
3920 (move-overlay o (point-at-bol 0) (1+ (point-at-eol 0)))))))))
3921 (add-hook 'anything-update-hook 'anything-revive-visible-mark)
3923 (defun anything-next-point-in-list (curpos points &optional prev)
3924 (cond
3925 ;; rule out special cases
3926 ((null points) curpos)
3927 ((and prev (< curpos (car points))) curpos)
3928 ((< (car (last points)) curpos)
3929 (if prev (car (last points)) curpos))
3931 (nth (if prev
3932 (loop for pt in points
3933 for i from 0
3934 if (<= curpos pt)
3935 return (1- i))
3936 (loop for pt in points
3937 for i from 0
3938 if (< curpos pt)
3939 return i))
3940 points))))
3942 (defun anything-next-visible-mark (&optional prev)
3943 "Move next anything visible mark.
3944 If PREV is non-nil move to precedent."
3945 (interactive)
3946 (with-anything-window
3947 (ignore-errors
3948 (goto-char (anything-next-point-in-list
3949 (point)
3950 (sort (mapcar 'overlay-start anything-visible-mark-overlays) '<)
3951 prev)))
3952 (anything-mark-current-line)))
3954 (defun anything-prev-visible-mark ()
3955 "Move previous anything visible mark."
3956 (interactive)
3957 (anything-next-visible-mark t))
3959 ;; (@* "Utility: Selection Paste")
3960 (defun anything-yank-selection ()
3961 "Set minibuffer contents to current selection."
3962 (interactive)
3963 (anything-set-pattern (anything-get-selection nil t)))
3965 (defun anything-kill-selection-and-quit ()
3966 "Store current selection to kill ring.
3967 You can paste it by typing \\[yank]."
3968 (interactive)
3969 (anything-run-after-quit
3970 (lambda (sel)
3971 (kill-new sel)
3972 (message "Killed: %s" sel))
3973 (anything-get-selection nil t)))
3976 ;; (@* "Utility: Automatical execution of persistent-action")
3977 (add-to-list 'minor-mode-alist '(anything-follow-mode " AFollow"))
3978 (defun anything-follow-mode ()
3979 "If this mode is on, persistent action is executed everytime the cursor is moved."
3980 (interactive)
3981 (with-current-buffer anything-buffer
3982 (setq anything-follow-mode (not anything-follow-mode))
3983 (message "anything-follow-mode is %s"
3984 (if anything-follow-mode "enabled" "disabled"))))
3986 (defun anything-follow-execute-persistent-action-maybe ()
3987 "Execute persistent action in mode `anything-follow-mode'.
3988 This happen after `anything-input-idle-delay' secs."
3989 (and (not (get-buffer-window anything-action-buffer 'visible))
3990 (buffer-local-value 'anything-follow-mode
3991 (get-buffer-create anything-buffer))
3992 (sit-for (and anything-input-idle-delay
3993 (max anything-input-idle-delay 0.1)))
3994 (anything-window)
3995 (anything-get-selection)
3996 (save-excursion
3997 (anything-execute-persistent-action))))
4000 ;; (@* "Utility: Migrate `anything-sources' to my-anything command")
4001 (defun anything-migrate-sources ()
4002 "Help to migrate to new `anything' way."
4003 (interactive)
4004 (with-current-buffer (get-buffer-create "*anything migrate*")
4005 (erase-buffer)
4006 (insert (format "\
4007 Setting `anything-sources' directly is not good because
4008 `anything' is not for one command. For now, interactive use of
4009 `anything' (M-x anything) is only for demonstration purpose.
4010 So you should define commands calling `anything'.
4011 I help you to migrate to the new way.
4013 The code below is automatically generated from current
4014 `anything-sources' value. You can use the `my-anything' command
4015 now!
4017 Copy and paste it to your .emacs. Then substitute `my-anything'
4018 for `anything' bindings in all `define-key', `local-set-key' and
4019 `global-set-key' calls.
4021 \(defun my-anything ()
4022 \"Anything command for you.
4024 It is automatically generated by `anything-migrate-sources'.\"
4025 (interactive)
4026 (anything-other-buffer
4028 \"*my-anything*\"))
4029 " anything-sources))
4030 (eval-last-sexp nil)
4031 (substitute-key-definition 'anything 'my-anything global-map)
4032 (pop-to-buffer (current-buffer))))
4035 ;; (@* "Compatibility")
4037 ;; Copied assoc-default from XEmacs version 21.5.12
4038 (unless (fboundp 'assoc-default)
4039 (defun assoc-default (key alist &optional test default)
4040 "Find object KEY in a pseudo-alist ALIST.
4041 ALIST is a list of conses or objects. Each element (or the element's car,
4042 if it is a cons) is compared with KEY by evaluating (TEST (car elt) KEY).
4043 If that is non-nil, the element matches;
4044 then `assoc-default' returns the element's cdr, if it is a cons,
4045 or DEFAULT if the element is not a cons.
4047 If no element matches, the value is nil.
4048 If TEST is omitted or nil, `equal' is used."
4049 (let (found (tail alist) value)
4050 (while (and tail (not found))
4051 (let ((elt (car tail)))
4052 (when (funcall (or test 'equal) (if (consp elt) (car elt) elt) key)
4053 (setq found t value (if (consp elt) (cdr elt) default))))
4054 (setq tail (cdr tail)))
4055 value)))
4057 ;; Function not available in XEmacs,
4058 (unless (fboundp 'minibuffer-contents)
4059 (defun minibuffer-contents ()
4060 "Return the user input in a minbuffer as a string.
4061 The current buffer must be a minibuffer."
4062 (field-string (point-max)))
4064 (defun delete-minibuffer-contents ()
4065 "Delete all user input in a minibuffer.
4066 The current buffer must be a minibuffer."
4067 (delete-field (point-max))))
4069 ;; Function not available in older Emacs (<= 22.1).
4070 (unless (fboundp 'buffer-chars-modified-tick)
4071 (defun buffer-chars-modified-tick (&optional buffer)
4072 "Return BUFFER's character-change tick counter.
4073 Each buffer has a character-change tick counter, which is set to the
4074 value of the buffer's tick counter (see `buffer-modified-tick'), each
4075 time text in that buffer is inserted or deleted. By comparing the
4076 values returned by two individual calls of `buffer-chars-modified-tick',
4077 you can tell whether a character change occurred in that buffer in
4078 between these calls. No argument or nil as argument means use current
4079 buffer as BUFFER."
4080 (with-current-buffer (or buffer (current-buffer))
4081 (if (listp buffer-undo-list)
4082 (length buffer-undo-list)
4083 (buffer-modified-tick)))))
4086 ;; (@* "CUA workaround")
4087 (defadvice cua-delete-region (around anything-avoid-cua activate)
4088 (ignore-errors ad-do-it))
4090 (defadvice copy-region-as-kill (around anything-avoid-cua activate)
4091 (if cua-mode
4092 (ignore-errors ad-do-it)
4093 ad-do-it))
4095 ;;(@* "Attribute Documentation")
4096 (defun anything-describe-anything-attribute (anything-attribute)
4097 "Display the full documentation of ANYTHING-ATTRIBUTE.
4098 ANYTHING-ATTRIBUTE should be a symbol."
4099 (interactive (list (intern
4100 (completing-read
4101 "Describe anything attribute: "
4102 (mapcar 'symbol-name anything-additional-attributes)
4103 nil t))))
4104 (with-output-to-temp-buffer "*Help*"
4105 (princ (get anything-attribute 'anything-attrdoc))))
4107 (anything-document-attribute 'name "mandatory"
4108 " The name of the source. It is also the heading which appears
4109 above the list of matches from the source. Must be unique.")
4111 (anything-document-attribute 'header-name "optional"
4112 " A function returning the display string of the header. Its
4113 argument is the name of the source. This attribute is useful to
4114 add an additional information with the source name.")
4116 (anything-document-attribute 'candidates "mandatory if candidates-in-buffer attribute is not provided"
4117 " Specifies how to retrieve candidates from the source. It can
4118 either be a variable name, a function called with no parameters
4119 or the actual list of candidates.
4121 The list must be a list whose members are strings, symbols
4122 or (DISPLAY . REAL) pairs.
4124 In case of (DISPLAY . REAL) pairs, the DISPLAY string is shown
4125 in the Anything buffer, but the REAL one is used as action
4126 argument when the candidate is selected. This allows a more
4127 readable presentation for candidates which would otherwise be,
4128 for example, too long or have a common part shared with other
4129 candidates which can be safely replaced with an abbreviated
4130 string for display purposes.
4132 Note that if the (DISPLAY . REAL) form is used then pattern
4133 matching is done on the displayed string, not on the real
4134 value.
4136 If the candidates have to be retrieved asynchronously (for
4137 example, by an external command which takes a while to run)
4138 then the function should start the external command
4139 asynchronously and return the associated process object.
4140 Anything will take care of managing the process (receiving the
4141 output from it, killing it if necessary, etc.). The process
4142 should return candidates matching the current pattern (see
4143 variable `anything-pattern'.)
4145 Note that currently results from asynchronous sources appear
4146 last in the anything buffer regardless of their position in
4147 `anything-sources'.")
4149 (anything-document-attribute 'action "mandatory if type attribute is not provided"
4150 " It is a list of (DISPLAY . FUNCTION) pairs or FUNCTION.
4151 FUNCTION is called with one parameter: the selected candidate.
4153 An action other than the default can be chosen from this list
4154 of actions for the currently selected candidate (by default
4155 with TAB). The DISPLAY string is shown in the completions
4156 buffer and the FUNCTION is invoked when an action is
4157 selected. The first action of the list is the default.")
4159 (anything-document-attribute 'coerce "optional"
4160 " It's a function called with one argument: the selected candidate.
4162 This function is intended for type convertion.
4163 In normal case, the selected candidate (string) is passed to action function.
4164 If coerce function is specified, it is called just before action function.
4166 Example: converting string to symbol
4167 (coerce . intern)")
4169 (anything-document-attribute 'type "optional if action attribute is provided"
4170 " Indicates the type of the items the source returns.
4172 Merge attributes not specified in the source itself from
4173 `anything-type-attributes'.
4175 This attribute is implemented by plug-in.")
4177 (anything-document-attribute 'init "optional"
4178 " Function called with no parameters when anything is started. It
4179 is useful for collecting current state information which can be
4180 used to create the list of candidates later.
4182 For example, if a source needs to work with the current
4183 directory then it can store its value here, because later
4184 anything does its job in the minibuffer and in the
4185 `anything-buffer' and the current directory can be different
4186 there.")
4188 (anything-document-attribute 'delayed-init "optional"
4189 " Function called with no parameters before candidate function is
4190 called. It is similar with `init' attribute, but its
4191 evaluation is deferred. It is useful to combine with ")
4193 (anything-document-attribute 'match "optional"
4194 " List of functions called with one parameter: a candidate. The
4195 function should return non-nil if the candidate matches the
4196 current pattern (see variable `anything-pattern').
4198 This attribute allows the source to override the default
4199 pattern matching based on `string-match'. It can be used, for
4200 example, to implement a source for file names and do the
4201 pattern matching on the basename of files, since it's more
4202 likely one is typing part of the basename when searching for a
4203 file, instead of some string anywhere else in its path.
4205 If the list contains more than one function then the list of
4206 matching candidates from the source is constructed by appending
4207 the results after invoking the first function on all the
4208 potential candidates, then the next function, and so on. The
4209 matching candidates supplied by the first function appear first
4210 in the list of results and then results from the other
4211 functions, respectively.
4213 This attribute has no effect for asynchronous sources (see
4214 attribute `candidates'), since they perform pattern matching
4215 themselves.")
4217 (anything-document-attribute 'candidate-transformer "optional"
4218 " It's a function or a list of functions called with one argument
4219 when the completion list from the source is built. The argument
4220 is the list of candidates retrieved from the source. The
4221 function should return a transformed list of candidates which
4222 will be used for the actual completion. If it is a list of
4223 functions, it calls each function sequentially.
4225 This can be used to transform or remove items from the list of
4226 candidates.
4228 Note that `candidates' is run already, so the given transformer
4229 function should also be able to handle candidates with (DISPLAY
4230 . REAL) format.")
4232 (anything-document-attribute 'filtered-candidate-transformer "optional"
4233 " It has the same format as `candidate-transformer', except the
4234 function is called with two parameters: the candidate list and
4235 the source.
4237 This transformer is run on the candidate list which is already
4238 filtered by the current pattern. While `candidate-transformer'
4239 is run only once, it is run every time the input pattern is
4240 changed.
4242 It can be used to transform the candidate list dynamically, for
4243 example, based on the current pattern.
4245 In some cases it may also be more efficent to perform candidate
4246 transformation here, instead of with `candidate-transformer'
4247 even if this transformation is done every time the pattern is
4248 changed. For example, if a candidate set is very large then
4249 `candidate-transformer' transforms every candidate while only
4250 some of them will actually be dislpayed due to the limit
4251 imposed by `anything-candidate-number-limit'.
4253 Note that `candidates' and `candidate-transformer' is run
4254 already, so the given transformer function should also be able
4255 to handle candidates with (DISPLAY . REAL) format.
4257 This option has no effect for asynchronous sources. (Not yet,
4258 at least.")
4260 (anything-document-attribute 'action-transformer "optional"
4261 " It's a function or a list of functions called with two
4262 arguments when the action list from the source is
4263 assembled. The first argument is the list of actions, the
4264 second is the current selection. If it is a list of functions,
4265 it calls each function sequentially.
4267 The function should return a transformed action list.
4269 This can be used to customize the list of actions based on the
4270 currently selected candidate.")
4272 (anything-document-attribute 'pattern-transformer "optional"
4273 " It's a function or a list of functions called with one argument
4274 before computing matches. Its argument is `anything-pattern'.
4275 Functions should return transformed `anything-pattern'.
4277 It is useful to change interpretation of `anything-pattern'.")
4279 (anything-document-attribute 'delayed "optional"
4280 " Candidates from the source are shown only if the user stops
4281 typing and is idle for `anything-idle-delay' seconds.")
4283 (anything-document-attribute 'volatile "optional"
4284 " Indicates the source assembles the candidate list dynamically,
4285 so it shouldn't be cached within a single Anything
4286 invocation. It is only applicable to synchronous sources,
4287 because asynchronous sources are not cached.")
4289 (anything-document-attribute 'requires-pattern "optional"
4290 " If present matches from the source are shown only if the
4291 pattern is not empty. Optionally, it can have an integer
4292 parameter specifying the required length of input which is
4293 useful in case of sources with lots of candidates.")
4295 (anything-document-attribute 'persistent-action "optional"
4296 " Function called with one parameter; the selected candidate.
4298 An action performed by `anything-execute-persistent-action'.
4299 If none, use the default action.")
4301 (anything-document-attribute 'candidates-in-buffer "optional"
4302 " Shortcut attribute for making and narrowing candidates using
4303 buffers. This newly-introduced attribute prevents us from
4304 forgetting to add volatile and match attributes.
4306 See docstring of `anything-candidates-in-buffer'.
4308 (candidates-in-buffer) is equivalent of three attributes:
4309 (candidates . anything-candidates-in-buffer)
4310 (volatile)
4311 (match identity)
4313 (candidates-in-buffer . candidates-function) is equivalent of:
4314 (candidates . candidates-function)
4315 (volatile)
4316 (match identity)
4318 This attribute is implemented by plug-in.")
4320 (anything-document-attribute 'search "optional"
4321 " List of functions like `re-search-forward' or `search-forward'.
4322 Buffer search function used by `anything-candidates-in-buffer'.
4323 By default, `anything-candidates-in-buffer' uses `re-search-forward'.
4324 This attribute is meant to be used with
4325 (candidates . anything-candidates-in-buffer) or
4326 (candidates-in-buffer) in short.")
4328 (anything-document-attribute 'search-from-end "optional"
4329 " Make `anything-candidates-in-buffer' search from the end of buffer.
4330 If this attribute is specified, `anything-candidates-in-buffer' uses
4331 `re-search-backward' instead.")
4333 (anything-document-attribute 'get-line "optional"
4334 " A function like `buffer-substring-no-properties' or `buffer-substring'.
4335 This function converts point of line-beginning and point of line-end,
4336 which represents a candidate computed by `anything-candidates-in-buffer'.
4337 By default, `anything-candidates-in-buffer' uses
4338 `buffer-substring-no-properties'.")
4340 (anything-document-attribute 'display-to-real "optional"
4341 " Function called with one parameter; the selected candidate.
4343 The function transforms the selected candidate, and the result
4344 is passed to the action function. The display-to-real
4345 attribute provides another way to pass other string than one
4346 shown in Anything buffer.
4348 Traditionally, it is possible to make candidates,
4349 candidate-transformer or filtered-candidate-transformer
4350 function return a list with (DISPLAY . REAL) pairs. But if REAL
4351 can be generated from DISPLAY, display-to-real is more
4352 convenient and faster.")
4354 (anything-document-attribute 'real-to-display "optional"
4355 " Function called with one parameter; the selected candidate.
4357 The inverse of display-to-real attribute.
4359 The function transforms the selected candidate, which is passed
4360 to the action function, for display. The real-to-display
4361 attribute provides the other way to pass other string than one
4362 shown in Anything buffer.
4364 Traditionally, it is possible to make candidates,
4365 candidate-transformer or filtered-candidate-transformer
4366 function return a list with (DISPLAY . REAL) pairs. But if
4367 DISPLAY can be generated from REAL, real-to-display is more
4368 convenient.
4370 Note that DISPLAY parts returned from candidates /
4371 candidate-transformer are IGNORED as the name `display-to-real'
4372 says.")
4374 (anything-document-attribute 'cleanup "optional"
4375 " Function called with no parameters when *anything* buffer is closed. It
4376 is useful for killing unneeded candidates buffer.
4378 Note that the function is executed BEFORE performing action.")
4380 (anything-document-attribute 'candidate-number-limit "optional"
4381 " Override `anything-candidate-number-limit' only for this source.")
4383 (anything-document-attribute 'accept-empty "optional"
4384 " Pass empty string \"\" to action function.")
4386 (anything-document-attribute 'disable-shortcuts "optional"
4387 " Disable `anything-enable-shortcuts' in current `anything' session.
4389 This attribute is implemented by plug-in.")
4391 (anything-document-attribute 'dummy "optional"
4392 " Set `anything-pattern' to candidate. If this attribute is
4393 specified, The candidates attribute is ignored.
4395 This attribute is implemented by plug-in.
4396 This plug-in implies disable-shortcuts plug-in.")
4398 (anything-document-attribute 'multiline "optional"
4399 " Enable to selection multiline candidates.")
4401 (anything-document-attribute 'update "optional"
4402 (substitute-command-keys
4403 " Function called with no parameters when \
4404 \\<anything-map>\\[anything-force-update] is pressed."))
4406 (anything-document-attribute 'mode-line "optional"
4407 " source local `anything-mode-line-string'. (included in `mode-line-format')
4408 It accepts also variable/function name.")
4410 (anything-document-attribute 'header-line "optional"
4411 " source local `header-line-format'.
4412 It accepts also variable/function name. ")
4414 (anything-document-attribute
4415 'resume "optional"
4416 " Function called with no parameters when `anything-resume' is started.")
4418 (anything-document-attribute 'keymap "optional"
4419 " Specific keymap for this source.
4420 It is useful to have a keymap per source when using more than one source.
4421 Otherwise, a keymap can be set per command with `anything' argument KEYMAP.
4422 NOTE: when a source have `anything-map' as keymap attr,
4423 the global value of `anything-map' will override the actual local one.")
4425 (anything-document-attribute 'help-message "optional"
4426 " Help message for this source.
4427 If not present, `anything-help-message' value will be used.")
4430 ;; (@* "Bug Report")
4431 (defvar anything-maintainer-mail-address "emacs-anything@googlegroups.com")
4433 (defvar anything-bug-report-salutation
4434 "Describe bug below, using a precise recipe.
4436 When I executed M-x ...
4438 How to send a bug report:
4439 1) Be sure to use the LATEST version of anything.el.
4440 2) Enable debugger. M-x toggle-debug-on-error or (setq debug-on-error t)
4441 3) Use Lisp version instead of compiled one: (load \"anything.el\")
4442 4) If you got an error, please paste *Backtrace* buffer.
4443 5) Type C-c C-c to send.")
4445 (defvar anything-no-dump-variables
4446 '(anything-candidate-buffer-alist
4447 anything-digit-overlays
4448 anything-help-message
4449 anything-candidate-cache
4451 "Variables not to dump in bug report.")
4453 (defun anything-dumped-variables-in-bug-report ()
4454 (let ((hash (make-hash-table)))
4455 (loop for var in (apropos-internal "anything-" 'boundp)
4456 for vname = (symbol-name var)
4457 unless (or (string-match "-map$" vname)
4458 (string-match "^anything-c-source-" vname)
4459 (string-match "-hash$" vname)
4460 (string-match "-face$" vname)
4461 (memq var anything-no-dump-variables))
4462 collect var)))
4464 (defun anything-send-bug-report ()
4465 "Send a bug report of anything.el."
4466 (interactive)
4467 (with-current-buffer (or anything-last-buffer
4468 (current-buffer))
4469 (reporter-submit-bug-report
4470 anything-maintainer-mail-address
4471 "anything.el"
4472 (anything-dumped-variables-in-bug-report)
4473 nil nil
4474 anything-bug-report-salutation)))
4476 (defun anything-send-bug-report-from-anything ()
4477 "Send a bug report of anything.el in anything session."
4478 (interactive)
4479 (anything-run-after-quit 'anything-send-bug-report))
4481 ;; Debugging function.
4482 (defun* anything-test-candidates
4483 (sources &optional (input "")
4484 (compile-source-functions
4485 anything-compile-source-functions-default))
4486 "Test helper function for anything.
4487 Given pseudo `anything-sources' and `anything-pattern', returns list like
4488 ((\"source name1\" (\"candidate1\" \"candidate2\"))
4489 (\"source name2\" (\"candidate3\" \"candidate4\")))"
4490 (let ((anything-test-mode t)
4491 anything-enable-shortcuts
4492 anything-candidate-cache
4493 (anything-compile-source-functions compile-source-functions)
4494 anything-before-initialize-hook
4495 anything-after-initialize-hook
4496 anything-update-hook
4497 anything-test-candidate-list)
4498 (get-buffer-create anything-buffer)
4499 (anything-initialize nil input sources)
4500 (anything-update)
4501 ;; test-mode spec: select 1st candidate!
4502 (with-current-buffer anything-buffer
4503 (forward-line 1)
4504 (anything-mark-current-line))
4505 (prog1
4506 anything-test-candidate-list
4507 (anything-cleanup))))
4510 ;; (@* "Unit Tests")
4511 ;; See developer-tools/unit-test-anything.el
4513 (provide 'anything)
4515 ;; Local Variables:
4516 ;; byte-compile-warnings: (not cl-functions obsolete)
4517 ;; coding: utf-8
4518 ;; End:
4520 ;;; anything.el ends here