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