gnu-emacs-sources
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

anything.el The Next Generation!


From: rubikitch
Subject: anything.el The Next Generation!
Date: Mon, 25 Aug 2008 02:55:07 +0900 (JST)

Hi,

Finally I release the next generation of anything.el!
See Commentary and Tips.

http://www.emacswiki.org/cgi-bin/wiki/download/anything.el

Sample plug-in: Humane match plug-in
http://www.emacswiki.org/cgi-bin/wiki/download/anything-match-plugin.el

GNU GLOBAL anything interface
http://www.emacswiki.org/cgi-bin/wiki/download/anything-gtags.el

Comments are welcome.
Happy Hacking!!
--
rubikitch
Blog: http://d.hatena.ne.jp/rubikitch/
Site: http://www.rubyist.net/~rubikitch/
;;; anything-gtags.el --- GNU GLOBAL anything.el interface
;; $Id: anything-gtags.el,v 1.9 2008/08/24 08:22:48 rubikitch Exp $

;; Copyright (C) 2008  rubikitch

;; Author: rubikitch <address@hidden>
;; Keywords: global, languages
;; URL: http://www.emacswiki.org/cgi-bin/wiki/download/anything-gtags.el

;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Commentary:

;; * `anything-gtags-select' is `anything' interface of `gtags-find-tag'.
;; * `anything-c-source-gtags-select' is a source for `gtags-find-tag'.
;; * Replace *GTAGS SELECT* buffer with `anything' interface.

;;; History:

;; $Log: anything-gtags.el,v $
;; Revision 1.9  2008/08/24 08:22:48  rubikitch
;; Rename `anything-candidates-buffer' -> `anything-candidate-buffer'
;;
;; Revision 1.8  2008/08/23 23:01:53  rubikitch
;; *** empty log message ***
;;
;; Revision 1.7  2008/08/20 19:00:36  rubikitch
;; *** empty log message ***
;;
;; Revision 1.6  2008/08/20 18:58:42  rubikitch
;; preselect entry of current line of source code.
;;
;; Revision 1.5  2008/08/19 21:50:00  rubikitch
;; adjust to new `search' spec.
;;
;; Revision 1.4  2008/08/18 17:20:23  rubikitch
;; save c source buffer's position
;; silence byte compiler
;;
;; Revision 1.3  2008/08/16 10:26:56  rubikitch
;; adjust to argument change of `anything-candidates-in-buffer-1'
;;
;; Revision 1.2  2008/08/14 20:47:14  rubikitch
;; ag-hijack-gtags-select-mode: cleanup
;;
;; Revision 1.1  2008/08/13 14:17:41  rubikitch
;; Initial revision
;;

;;; Code:

(defvar anything-c-source-gtags-select
  '((name . "GTAGS")
    (init
     . (lambda ()
         (call-process-shell-command
          "global -c" nil (anything-candidate-buffer 'global))))
    (candidates-in-buffer)
    (action
     ("Goto the location" . (lambda (candidate)
                              (gtags-goto-tag candidate ""))))))
;; (setq anything-sources (list anything-c-source-gtags-select))

(defun anything-gtags-select ()
  "Tag jump using gtags and `anything'."
  (interactive)
  (anything '(anything-c-source-gtags-select) nil "Find Tag: "))

;;;; `gtags-select-mode' replacement
(defvar anything-gtags-hijack-gtags-select-mode t
    "Use `anything' instead of `gtags-select-mode'.")
(defun aggs-candidate-display (s e)
  ;; 16 = length of symbol
  (buffer-substring-no-properties (+ s 16) e))
(defun ag-hijack-gtags-select-mode ()
  ;; `save': C source file / `buffer': gtags-select-mode buffer
  ;; They are defined at `gtags-goto-tag'.
  (declare (special save buffer))
  (let* ((anything-candidate-number-limit 9999)
         (pwd (with-current-buffer buffer (expand-file-name default-directory)))
         (basename (substring (with-current-buffer save buffer-file-name)
                              (length pwd)))
         (lineno (with-current-buffer save
                   (save-restriction
                     (widen)
                     (line-number-at-pos)))))
    (anything
     '(((name . "GTAGS SELECT")
        (init
         . (lambda ()
             ;; It's needed because `anything' saves
             ;; *GTAGS SELECT* buffer's position,
             (save-window-excursion
               (switch-to-buffer save)
               (setq anything-current-position (cons (point) (window-start))))
             (anything-candidate-buffer buffer)))
        (candidates-in-buffer)
        (get-line . aggs-candidate-display)
        (display-to-real
         . (lambda (c) (if (string-match "^ " c) (concat "_ " c) c)))
        (action
         ("Goto the location"
          . (lambda (c) (aggs-select-it c t))))
        (persistent-action . aggs-select-it)
        (cleanup . (lambda () (kill-buffer buffer)))))
     nil nil nil (format "\\(\\(%d\\) +%s\\)" lineno (regexp-quote basename) 
))))

(defun aggs-select-it (candidate &optional delete)
  (with-temp-buffer
    (declare (special pwd buffer))
    ;; `pwd' is defined at `ag-hijack-gtags-select-mode'.
    (setq default-directory pwd)
    (insert candidate "\n")
    (forward-line -1)
    (gtags-select-it nil)
    ;; `buffer' is defined at `gtags-goto-tag'.
    (and delete (kill-buffer buffer))))


(defadvice switch-to-buffer (around anything-gtags activate)
  "Use `anything' instead of `gtags-select-mode' when 
`anything-gtags-hijack-gtags-select-mode' is non-nil."
  (unless (and anything-gtags-hijack-gtags-select-mode
           (string-match "*GTAGS SELECT*"
                         (if (bufferp buffer) (buffer-name buffer) buffer)))
    ad-do-it))
;; (progn (ad-disable-advice 'switch-to-buffer 'around 'anything-gtags) 
(ad-update 'switch-to-buffer)) 

(defadvice gtags-select-mode (around anything-gtags activate)
  "Use `anything' instead of `gtags-select-mode' when 
`anything-gtags-hijack-gtags-select-mode' is non-nil."
  (if anything-gtags-hijack-gtags-select-mode
      (ag-hijack-gtags-select-mode)
    ad-do-it))
;; (progn (ad-disable-advice 'gtags-select-mode 'around 'anything-gtags) 
(ad-update 'gtags-select-mode)) 

(provide 'anything-gtags)

;; How to save (DO NOT REMOVE!!)
;; (emacswiki-post "anything-gtags.el")
;;; anything-gtags.el ends here
;;; anything-match-plugin.el --- Humane match plug-in for anything
;; $Id: anything-match-plugin.el,v 1.10 2008/08/24 17:48:53 rubikitch Exp $

;; Copyright (C) 2008  rubikitch

;; Author: rubikitch <address@hidden>
;; Keywords: anything, matching
;; URL: http://www.emacswiki.org/cgi-bin/wiki/download/anything-match-plugin.el

;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Commentary:

;; Change anything.el matching algorithm humanely.
;; It gives anything.el search refinement functionality.
;; exact match -> prefix match -> multiple regexps match

;; Just require it to use.

;;; History:

;; $Log: anything-match-plugin.el,v $
;; Revision 1.10  2008/08/24 17:48:53  rubikitch
;; Add commentary
;;
;; Revision 1.9  2008/08/24 08:23:16  rubikitch
;; Rename `anything-candidates-buffer' -> `anything-candidate-buffer'
;;
;; Revision 1.8  2008/08/22 21:25:44  rubikitch
;; *** empty log message ***
;;
;; Revision 1.7  2008/08/22 21:17:58  rubikitch
;; exact, prefix match: faster
;;
;; Revision 1.6  2008/08/22 19:40:22  rubikitch
;; exact -> prefix -> mp-3 by default because of speed
;;
;; Revision 1.5  2008/08/22 19:04:53  rubikitch
;; reimplemented
;;
;; Revision 1.4  2008/08/20 00:10:15  rubikitch
;; *** empty log message ***
;;
;; Revision 1.3  2008/08/19 23:30:39  rubikitch
;; exact match support
;;
;; Revision 1.2  2008/08/19 23:02:29  rubikitch
;; candidates-in-buffer hack
;;
;; Revision 1.1  2008/08/19 19:45:11  rubikitch
;; Initial revision
;;

;;; Code:

(require 'anything)
(require 'cl)

;;;; multiple patterns
(defvar anything-use-multiple-patterns t
  "If non-nil, enable anything-use-multiple-patterns.")

(defun amp-permute (list)
  (if (null list)
      (list nil)
    (mapcan (lambda (first)
              (mapcar (lambda (rest)
                        (cons first rest))
                      (amp-permute (remove* first list :count 1 :test 
#'equal))))
            list)))
(defun amp-mp-make-regexps (pattern)
  (if (string= pattern "") '("")
    (loop for s in (split-string (replace-regexp-in-string "\\\\ " "\000\000" 
pattern) " " t)
        collect (replace-regexp-in-string "\000\000" " " s))))

(defun amp-mp-1-make-regexp (pattern)
  (mapconcat 'identity (amp-mp-make-regexps pattern) ".*"))

(defmacro amp-define (prefix regexp-expr)
  (let ((pattern-str (intern (concat prefix "pattern-str")))
        (pattern-regexp (intern (concat prefix "pattern-regexp")))
        (get-regexp (intern (concat prefix "get-regexp"))) 
        (match (intern (concat prefix "match")))
        (search (intern (concat prefix "search"))))
    `(progn
       (defvar ,pattern-str nil)
       (defvar ,pattern-regexp nil)
       (defsubst ,get-regexp (pattern)
         (unless (equal pattern ,pattern-str)
           (setq ,pattern-str pattern
                 ,pattern-regexp ,regexp-expr))
         ,pattern-regexp)
       (defun* ,match (str &optional (pattern anything-pattern))
         (string-match (,get-regexp pattern) str))
       (defun ,search (pattern &rest ignore)
         (re-search-forward (,get-regexp pattern) nil t)))))
  
;; exact match
;(amp-define "anything-exact-" (concat (anything-prefix-get-regexp pattern) 
"$"))
(defun anything-exact-match (str &optional pattern)
  (string= str (or pattern anything-pattern)))
(defun anything-exact-search (pattern &rest ignore)
  (and (search-forward (concat "\n" pattern "\n") nil t)
       (forward-line -1)))
;; prefix match
;;(amp-define "anything-prefix-" (concat "^" (regexp-quote pattern)))
(defun anything-prefix-match (str &optional pattern)
  (setq pattern (or pattern anything-pattern))
  (let ((len (length pattern)))
    (and (<= len (length str))
         (string= (substring str 0 len) pattern ))))
(defun anything-prefix-search (pattern &rest ignore)
  (search-forward (concat "\n" pattern) nil t))
;; multiple regexp patterns 1 (order is preserved / prefix)
(amp-define "anything-mp-1-" (concat "^" (amp-mp-1-make-regexp pattern)))
;; multiple regexp patterns 2 (order is preserved / partial)
(amp-define "anything-mp-2-" (concat "^.+" (amp-mp-1-make-regexp pattern)))
;; multiple regexp patterns 3 (permutation)
(amp-define "anything-mp-3-"
            (mapconcat (lambda (regexps)
                         (concat "\\(" (mapconcat #'identity regexps ".*") 
"\\)"))
                       (amp-permute (amp-mp-make-regexps pattern))
                       "\\|"))

                         
;;;; source compier
(defvar anything-default-match-functions
  '(anything-exact-match anything-prefix-match  anything-mp-3-match))
(defvar anything-default-search-functions
  '(anything-exact-search anything-prefix-search anything-mp-3-search))

(defun anything-compile-source--match-plugin (source)
  `(,(if (or (assoc 'candidates-in-buffer source)
             (equal '(identity) (assoc-default 'match source)))
         '(match identity)
       `(match ,@anything-default-match-functions
               ,@(assoc-default 'match source)))
    (search ,@anything-default-search-functions
            ,@(assoc-default 'search source))
    ,@source))

(add-to-list 'anything-compile-source-functions 
'anything-compile-source--match-plugin t)

;;;; unit test
;; (install-elisp 
"http://www.emacswiki.org/cgi-bin/wiki/download/el-expectations.el";)
;; (install-elisp "http://www.emacswiki.org/cgi-bin/wiki/download/el-mock.el";)
(declare (warn (unresolved 0)))
(when (fboundp 'expectations)
  (expectations
    (desc "amp-mp-make-regexps")
    (expect '("")
      (amp-mp-make-regexps ""))
    (expect '("foo" "bar")
      (amp-mp-make-regexps "foo bar"))
    (expect '("foo" "bar")
      (amp-mp-make-regexps " foo bar"))
    (expect '("foo" "bar")
      (amp-mp-make-regexps " foo bar "))
    (expect '("foo bar" "baz")
      (amp-mp-make-regexps "foo\\ bar baz"))
    (desc "anything-exact-match")
    (expect (non-nil)
      (anything-exact-match "thunder" "thunder"))
    (expect nil
      (anything-exact-match "thunder" "fire"))
    (desc "anything-exact-search")
    (expect (non-nil)
      (with-temp-buffer
        (insert "fire\nthunder\n")
        (goto-char 1)
        (anything-exact-search "thunder" nil t)))
    (expect (non-nil)
      (with-temp-buffer
        (insert "\nfire\nthunder\n")
        (goto-char 1)
        (anything-exact-search "fire" nil t)))
    (desc "amp-mp-1-make-regexp")
    (expect "a.*b"
      (amp-mp-1-make-regexp "a b"))
    (expect "a b"
      (amp-mp-1-make-regexp "a\\ b"))
    (expect "a.*b c"
      (amp-mp-1-make-regexp "a b\\ c"))
    (expect ""
      (amp-mp-1-make-regexp ""))
    (desc "anything-mp-1-search")
    (expect (non-nil)
      (with-temp-buffer
        (insert "fire\nthunder\n")
        (goto-char 1)
        (anything-mp-1-search "th+ r" nil t)))
    (desc "anything-mp-2-search")
    (expect (non-nil)
      (with-temp-buffer
        (insert "fire\nthunder\n")
        (goto-char 1)
        (anything-mp-2-search "h+ r" nil t)))
    (expect nil
      (with-temp-buffer
        (insert "fire\nthunder\n")
        (goto-char 1)
        (anything-mp-2-search "th+ r" nil t)))
    (desc "anything-mp-3-search")
    (expect (non-nil)
      (with-temp-buffer
        (insert "fire\nthunder\n")
        (goto-char 1)
        (anything-mp-3-search "h+ r" nil t)))
    (expect (non-nil)
      (with-temp-buffer
        (insert "fire\nthunder\n")
        (goto-char 1)
        (anything-mp-3-search "th+ r" nil t)))
    (expect (non-nil)
      (with-temp-buffer
        (insert "fire\nthunder\n")
        (goto-char 1)
        (anything-mp-3-search "r th+" nil t)))
    (expect nil
      (with-temp-buffer
        (insert "fire\nthunder\n")
        (goto-char 1)
        (anything-mp-3-search "under hue" nil t)))
    (expect (non-nil)
      (with-temp-buffer
        (insert "fire\nthunder\n")
        (goto-char 1)
        (anything-mp-3-search "r th+ n" nil t)))
    (desc "anything-mp-1-match")
    (expect (non-nil)
      (anything-mp-1-match "thunder" "th+ r"))
    (desc "anything-mp-2-match")
    (expect (non-nil)
      (anything-mp-2-match "thunder" "h+ r"))
    (expect nil
      (anything-mp-2-match "thunder" "th+ r"))
    (desc "anything-mp-3-match")
    (expect (non-nil)
      (anything-mp-3-match "thunder" "h+ r"))
    (expect (non-nil)
      (anything-mp-3-match "thunder" "th+ r"))
    (expect (non-nil)
      (anything-mp-3-match "thunder" "r th+"))
    (expect nil
      (anything-mp-3-match "thunder" "under hue"))
    (expect (non-nil)
      (anything-mp-3-match "thunder" "r th+ n"))
    (desc "anything-prefix-match")
    (expect (non-nil)
      (anything-prefix-match "fobar" "fo"))
    (expect nil
      (anything-prefix-match "xfobar" "fo"))
    
    (desc "with identity match")
    (expect '(identity)
      (assoc-default 'match
                     (car (anything-compile-sources
                           '(((name . "FOO")
                              (candidates-in-buffer)))
                           '(anything-compile-source--candidates-in-buffer
                             anything-compile-source--match-plugin)))))
    (expect '(identity)
      (assoc-default 'match
                     (car (anything-compile-sources
                           '(((name . "FOO")
                              (match identity)))
                           '(anything-compile-source--match-plugin)))))
    (desc "functional")
    (expect '(("FOO" ("thunder")))
      (anything-test-candidates '(((name . "FOO")
                                   (candidates "fire" "thunder")))
                                "th+ r"
                                '(anything-compile-source--match-plugin)))
    (expect '(("FOO" ("one two")))
      (anything-test-candidates '(((name . "FOO")
                                   (candidates "one two" "three four")))
                                "e\\ t"
                                '(anything-compile-source--match-plugin)))
    (expect '(("FOO" ("thunder")))
      (anything-test-candidates '(((name . "FOO")
                                   (init
                                    . (lambda ()
                                        (with-current-buffer 
(anything-candidate-buffer 'global)
                                          (insert "fire\nthunder\nthanks\n"))))
                                   (candidates-in-buffer)))
                                "th+ r"
                                '(anything-compile-source--candidates-in-buffer
                                  anything-compile-source--match-plugin)))
    (expect '(("FOO" ("foo" "foobar")))
      (anything-test-candidates '(((name . "FOO")
                                   (candidates "foobar" "foo")))
                                "foo"
                                '(anything-compile-source--match-plugin)))
    (expect '(("FOO" ("foo" "foobar")))
      (anything-test-candidates '(((name . "FOO")
                                   (init
                                    . (lambda ()
                                        (with-current-buffer 
(anything-candidate-buffer 'global)
                                          (insert "foobar\nfoo\n"))))
                                   (candidates-in-buffer)))
                                "foo"
                                '(anything-compile-source--candidates-in-buffer
                                  anything-compile-source--match-plugin)))
    (expect '(("FOO" ("foo")))
      (anything-test-candidates '(((name . "FOO")
                                   (init
                                    . (lambda ()
                                        (with-current-buffer 
(anything-candidate-buffer 'global)
                                          (insert "foo\n"))))
                                   (candidates-in-buffer)))
                                "foo"
                                '(anything-compile-source--candidates-in-buffer
                                  anything-compile-source--match-plugin)))
    (expect '(("FOO" ("foo")))
      (anything-test-candidates '(((name . "FOO")
                                   (init
                                    . (lambda ()
                                        (with-current-buffer 
(anything-candidate-buffer 'global)
                                          (insert "bar\nfoo\ntest\n"))))
                                   (candidates-in-buffer)))
                                "foo"
                                '(anything-compile-source--candidates-in-buffer
                                  anything-compile-source--match-plugin)))
    (expect '(("FOO" ("foobar" "foo")))
      (anything-test-candidates '(((name . "FOO")
                                   (init
                                    . (lambda ()
                                        (with-current-buffer 
(anything-candidate-buffer 'global)
                                          (insert "foobar\nfoo\n"))))
                                   (candidates-in-buffer)))
                                ""
                                '(anything-compile-source--candidates-in-buffer
                                  anything-compile-source--match-plugin)))
    ))




(provide 'anything-match-plugin)

;; How to save (DO NOT REMOVE!!)
;; (emacswiki-post "anything-match-plugin.el")
;;; anything-match-plugin.el ends here
;;; anything.el --- open anything / QuickSilver-like candidate-selection 
framework
;; $Id: anything.el,v 1.89 2008/08/24 08:35:27 rubikitch Exp $

;; Copyright (C) 2007  Tamas Patrovics
;;               2008  rubikitch <address@hidden>

;; Author: Tamas Patrovics
;; Maintainer: rubikitch <address@hidden>
;; Keywords: files, frames, help, matching, outlines, processes, tools, 
convenience, anything
;; URL: http://www.emacswiki.org/cgi-bin/wiki/download/anything.el
;; Site: http://www.emacswiki.org/cgi-bin/emacs/Anything

;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Commentary:

;;
;; Start with M-x anything, narrow the list by typing some pattern,
;; select with up/down/pgup/pgdown, choose with enter, left/right
;; moves between sources. With TAB actions can be selected if the
;; selected candidate has more than one possible action.
;;
;; Note that anything.el provides only the framework and some example
;; configurations for demonstration purposes. See anything-config.el
;; for practical, polished, easy to use configurations which can be
;; used to assemble a custom personalized configuration. And many
;; other configurations are in the EmacsWiki.
;; 
;; http://www.emacswiki.org/cgi-bin/wiki/download/anything-config.el
;; http://www.emacswiki.org/cgi-bin/emacs/AnythingSources
;;
;; Maintainer's configuration is in the EmacsWiki. It would tell you
;; many tips to write smart sources!
;;
;; http://www.emacswiki.org/cgi-bin/emacs/RubikitchAnythingConfiguration

;; You can extend `anything' by writing plug-ins. As soon as
;; `anything' is invoked, `anything-sources' is compiled into basic
;; attributes, then compiled one is used during invocation.
;;
;; The oldest built-in plug-in is `type' attribute: appends
;; appropriate element of `anything-type-attributes'. Second built-in
;; plug-in is `candidates-in-buffer': selecting a line from candidates
;; buffer.
;;
;; To write a plug-in:
;; 1. Define a compiler: anything-compile-source--*
;; 2. Add compier function to `anything-compile-source-functions'.
;; 3. (optional) Write helper functions.
;;
;; Anything plug-ins are found in the EmacsWiki.
;;
;; http://www.emacswiki.org/cgi-bin/emacs/AnythingPlugins

;; Tested on Emacs 22.
;;
;;
;; Thanks to Vagn Johansen for ideas.
;; Thanks to Stefan Kamphausen for fixes and XEmacs support.
;; Thanks to Tassilo Horn for fixes.
;; Thanks to Drew Adams for various fixes (frame, isearch, customization, etc.)
;; Thanks to IMAKADO for candidates-in-buffer idea.
;;

;;; Tips:

;;
;; `anything-sources' accepts many attributes to make your life easier.
;; Now `anything-sources' accepts a list of symbols.
;;
;; [EVAL IT] (describe-variable 'anything-sources)

;;
;; `anything' has optional arguments. Now you do not have to let-bind
;; `anything-sources'.
;;
;; [EVAL IT] (describe-function 'anything)

;;
;; `anything-resume' resumes last `anything' session. Now you do not
;; have to retype pattern.
;;
;; [EVAL IT] (describe-function 'anything-resume)

;;
;; `anything-execute-persistent-action' executes action without
;; quitting `anything'. When popping up a buffer in other window by
;; persistent action, you can scroll with `anything-scroll-other-window' and
;; `anything-scroll-other-window-down'. See also `anything-sources' docstring.
;;
;; [EVAL IT] (describe-function 'anything-execute-persistent-action)
;; [EVAL IT] (describe-variable 'anything-sources)

;;
;; `anything-select-2nd-action', `anything-select-3rd-action' and
;; `anything-select-4th-action' select other than default action
;; without pressing Tab.

;;
;; Using `anything-candidate-buffer' and the candidates-in-buffer
;; attribute is much faster than traditional "candidates and match"
;; way. And `anything-current-buffer-is-modified' avoids to
;; recalculate candidates for unmodified buffer. See docstring of
;; them.
;;
;; [EVAL IT] (describe-function 'anything-candidate-buffer)
;; [EVAL IT] (describe-function 'anything-candidates-in-buffer)
;; [EVAL IT] (describe-function 'anything-current-buffer-is-modified)

;;
;; `anything-current-buffer' and `anything-buffer-file-name' stores
;; `(current-buffer)' and `buffer-file-name' in the buffer `anything'
;; is invoked. Use them freely.
;;
;; [EVAL IT] (describe-variable 'anything-current-buffer)
;; [EVAL IT] (describe-variable 'anything-buffer-file-name)

;;
;; `anything-completing-read' and `anything-read-file-name' are
;; experimental implementation. If you are curious, type M-x
;; anything-read-string-mode. It is a minor mode and toggles on/off.

;;
;; Use `anything-test-candidates' to test your handmade anything
;; sources. It simulates contents of *anything* buffer with pseudo
;; `anything-sources' and `anything-pattern', without side-effect. So
;; you can unit-test your anything sources! Let's TDD!
;;
;; [EVAL IT] (describe-function 'anything-test-candidates)
;;
;; There are many unit-testing framework in Emacs Lisp. See the EmacsWiki.
;; http://www.emacswiki.org/cgi-bin/emacs/UnitTesting
;;
;; There is an unit-test by Emacs Lisp Expectations at the tail of this file.
;; http://www.emacswiki.org/cgi-bin/wiki/download/el-expectations.el
;; http://www.emacswiki.org/cgi-bin/wiki/download/el-mock.el


;; TODO:
;;
;;   - process status indication
;;
;;   - results from async sources should appear in the order they are
;;     specified in anything-sources
;;
;;   - async sources doesn't honor digit-shortcut-count
;;
;;   - anything-candidate-number-limit can't be nil everywhere
;;
;;   - support multi line candidates

;; HISTORY:
;; $Log: anything.el,v $
;; Revision 1.89  2008/08/24 08:35:27  rubikitch
;; *** empty log message ***
;;
;; Revision 1.88  2008/08/24 08:22:19  rubikitch
;; Rename `anything-candidates-buffer' -> `anything-candidate-buffer'
;;
;; Revision 1.87  2008/08/23 22:27:04  rubikitch
;; New hook: `anything-cleanup-hook'
;;
;; Revision 1.86  2008/08/23 22:05:42  rubikitch
;; `anything-original-source-filter' is removed.
;; Now use `anything-restored-variables' and `with-anything-restore-variables'.
;;
;; Revision 1.85  2008/08/23 21:23:21  rubikitch
;; inhibit-read-only = t in anything-buffer
;;
;; Revision 1.84  2008/08/23 21:18:33  rubikitch
;; *** empty log message ***
;;
;; Revision 1.83  2008/08/23 20:44:20  rubikitch
;; `anything-execute-persistent-action': display-to-real bug fix
;;
;; Revision 1.82  2008/08/23 20:19:12  rubikitch
;; New `anything-sources' attribute: get-line
;;
;; Revision 1.81  2008/08/23 19:32:14  rubikitch
;; `anything-attr': Return t in (attribute-name) case.
;;
;; Revision 1.80  2008/08/22 21:25:05  rubikitch
;; anything-candidates-in-buffer-1:
;; Open a line at the BOB to make use of `search-forward' for faster 
exact/prefix match.
;; Of course, restore the buffer contents after search.
;;
;; Revision 1.79  2008/08/22 17:11:00  rubikitch
;; New hook: `anything-before-initialize-hook', `anything-after-initialize-hook'
;;
;; Revision 1.78  2008/08/21 18:37:03  rubikitch
;; Implemented dummy sources as plug-in.
;;
;; Revision 1.77  2008/08/21 17:40:40  rubikitch
;; New function: `anything-set-sources'
;;
;; Revision 1.76  2008/08/21 12:25:02  rubikitch
;; New variable: `anything-version'
;;
;; Revision 1.75  2008/08/21 12:13:46  rubikitch
;; New variable: `anything-in-persistent-action'
;;
;; Revision 1.74  2008/08/21 10:34:22  rubikitch
;; New function `anything-mklist'
;;
;; Revision 1.73  2008/08/21 09:41:38  rubikitch
;; accept multiple init/cleanup functions so that plug-ins can add new function.
;;
;; Revision 1.72  2008/08/20 22:51:53  rubikitch
;; New `anything-sources' attribute: candidate-number-limit
;;
;; Revision 1.71  2008/08/20 21:45:42  rubikitch
;; added many tests.
;;
;; Revision 1.70  2008/08/20 18:51:45  rubikitch
;; `anything-preselect' bug fix.
;; refactoring.
;;
;; Revision 1.69  2008/08/20 17:57:51  rubikitch
;; Extended `anything' optional arguments: preselect
;;
;; Revision 1.68  2008/08/20 16:39:07  rubikitch
;; Nested `anything' invocation support, ie. `anything' can be invoked by 
anything action.
;;
;; (anything '(((name . "nested anything invocation test")
;;              (candidates "anything-c-source-buffers" 
"anything-c-source-man-pages")
;;              (display-to-real . intern)
;;              (action . anything))))
;;
;; Revision 1.67  2008/08/20 00:08:28  rubikitch
;; `anything-candidates-in-buffer-1': add code when pattern == ""
;;
;; Revision 1.66  2008/08/19 23:31:52  rubikitch
;; Removed `anything-show-exact-match-first' because it should be provided as a 
plug-in.
;;
;; Revision 1.65  2008/08/19 23:18:47  rubikitch
;; *** empty log message ***
;;
;; Revision 1.64  2008/08/19 23:15:43  rubikitch
;; `anything-compute-matches': short-cut when match == '(identity)
;;
;; Revision 1.63  2008/08/19 23:06:42  rubikitch
;; Use hash table to speed uniquify candidates.
;;
;; Revision 1.62  2008/08/19 22:40:57  rubikitch
;; `anything-test-candidates': additional optonal argument
;;
;; Revision 1.61  2008/08/19 18:13:39  rubikitch
;; search attribute: multiple search functions
;;
;; Revision 1.60  2008/08/19 15:07:39  rubikitch
;; New function: `anything-attr'
;;
;; Revision 1.59  2008/08/19 15:01:59  rubikitch
;; arranged code
;; added unit tests
;; update doc
;;
;; Revision 1.58  2008/08/19 13:40:52  rubikitch
;; `anything-get-current-source': This function can be used in
;;  init/candidates/action/candidate-transformer/filtered-candidate-transformer
;;  display-to-real/cleanup function.
;;
;; Revision 1.57  2008/08/19 03:43:57  rubikitch
;; `anything-process-delayed-sources': delay = anything-idle-delay - 
anything-input-idle-delay
;;
;; Revision 1.56  2008/08/18 06:37:51  rubikitch
;; Make `anything-input-idle-delay' ineffective when the action list is shown.
;;
;; Revision 1.55  2008/08/18 06:35:00  rubikitch
;; New variable: `anything-show-exact-match-first'
;;
;; Revision 1.54  2008/08/17 23:22:24  rubikitch
;; *** empty log message ***
;;
;; Revision 1.53  2008/08/17 23:15:38  rubikitch
;; bind `anything-source-name' when executing action to enable to use 
`anything-candidate-buffer' in action.
;;
;; Revision 1.52  2008/08/17 15:21:27  rubikitch
;; `anything-test-candidates': accept a symbol for source
;; New variable: `anything-input-idle-delay'
;;
;; Revision 1.51  2008/08/17 12:45:30  rubikitch
;; (buffer-disable-undo) in anything-buffer
;;
;; Revision 1.50  2008/08/16 22:21:37  rubikitch
;; `anything-saved-sources': removed
;; `anything-action-buffer': action selection buffer
;; `anything-select-action': toggle actions <=> candidates
;;
;; Revision 1.49  2008/08/16 19:46:11  rubikitch
;; New function: `anything-action-list-is-shown'
;;
;; Revision 1.48  2008/08/16 17:03:02  rubikitch
;; bugfix: cleanup
;;
;; Revision 1.47  2008/08/16 16:35:24  rubikitch
;; silence byte compiler
;;
;; Revision 1.46  2008/08/16 14:51:27  rubikitch
;; *** empty log message ***
;;
;; Revision 1.45  2008/08/16 11:27:59  rubikitch
;; refactoring
;;  `anything-aif': Anaphoric if.
;;  `anything-compile-source-functions': make `anything-get-sources' 
customizable.
;;
;; Revision 1.44  2008/08/16 09:38:15  rubikitch
;; *** empty log message ***
;;
;; Revision 1.43  2008/08/15 11:44:28  rubikitch
;; `anything-read-string-mode': minor mode for `anything' version of read 
functions. (experimental)
;;
;; Revision 1.42  2008/08/15 11:03:20  rubikitch
;; update docs
;;
;; Revision 1.41  2008/08/14 20:51:28  rubikitch
;; New `anything-sources' attribute: cleanup
;;
;; Revision 1.40  2008/08/14 10:34:04  rubikitch
;; `anything': SOURCES: accept symbols
;;
;; Revision 1.39  2008/08/10 22:46:01  rubikitch
;; `anything-move-selection': avoid infinite loop
;;
;; Revision 1.38  2008/08/09 21:38:25  rubikitch
;; `anything-read-file-name': experimental implementation.
;;
;; Revision 1.37  2008/08/09 17:54:25  rubikitch
;; action test
;;
;; Revision 1.36  2008/08/09 17:13:00  rubikitch
;; fixed test
;;
;; Revision 1.35  2008/08/09 10:43:08  rubikitch
;; New `anything-sources' attribute: display-to-real
;;
;; Revision 1.34  2008/08/07 13:15:44  rubikitch
;; New `anything-sources' attribute: search
;;
;; Revision 1.33  2008/08/05 23:14:20  rubikitch
;; `anything-candidate-buffer': bugfix
;;
;; Revision 1.32  2008/08/05 21:42:15  rubikitch
;; *** empty log message ***
;;
;; Revision 1.31  2008/08/05 21:06:23  rubikitch
;; `anything-candidate-buffer': candidates buffer registration
;;
;; Revision 1.30  2008/08/05 19:46:36  rubikitch
;; New `anything-sources' attribute: candidates-in-buffer
;;
;; Revision 1.29  2008/08/05 17:58:31  rubikitch
;; *** empty log message ***
;;
;; Revision 1.28  2008/08/05 17:46:04  rubikitch
;; memoized `anything-get-sources'
;;
;; Revision 1.27  2008/08/05 17:29:40  rubikitch
;; update doc
;;
;; Revision 1.26  2008/08/05 08:35:45  rubikitch
;; `anything-completing-read': accept obarray
;;
;; Revision 1.25  2008/08/05 07:26:17  rubikitch
;; `anything-completing-read': guard from non-string return value
;;
;; Revision 1.24  2008/08/04 12:05:41  rubikitch
;; Wrote Tips and some docstrings.
;; `anything-candidate-buffer': buffer-local by default
;;
;; Revision 1.23  2008/08/04 05:29:46  rubikitch
;; `anything-buffer-file-name': `buffer-file-name' when `anything' is invoked.
;;
;; Revision 1.22  2008/08/04 00:10:13  rubikitch
;; `anything-candidate-buffer': new API
;;
;; Revision 1.21  2008/08/03 22:05:08  rubikitch
;; `anything-candidate-buffer': Return a buffer containing candidates of 
current source.
;;
;; Revision 1.20  2008/08/03 20:47:56  rubikitch
;; `anything-current-buffer-is-modified': modify checker
;;
;; Revision 1.19  2008/08/03 19:06:18  rubikitch
;; `anything-candidates-in-buffer': use `with-current-buffer' instead.
;;
;; Revision 1.18  2008/08/03 05:55:01  rubikitch
;; `anything-candidates-in-buffer': extract candidates in a buffer for speed.
;;
;; Revision 1.17  2008/08/02 21:31:29  rubikitch
;; Extended `anything' optional arguments.
;; `anything-completing-read': experimental implementation.
;;
;; Revision 1.16  2008/08/02 20:32:54  rubikitch
;; Extended `anything' optional arguments.
;;
;; Revision 1.15  2008/08/02 16:53:40  rubikitch
;; Fixed a small bug of `anything-test-candidates'.
;;
;; Revision 1.14  2008/08/02 16:48:29  rubikitch
;; Refactored to testable code.
;; Added many candidate tests with `anything-test-candidates'.
;;
;; Revision 1.13  2008/08/02 15:08:14  rubikitch
;; *** empty log message ***
;;
;; Revision 1.12  2008/08/02 14:29:31  rubikitch
;; `anything-sources' accepts symbols. (patched by Sugawara)
;;
;; Revision 1.11  2008/08/02 10:20:36  rubikitch
;; `anything-resume' is usable with other (let-binded) `anything-sources'.
;;
;; Revision 1.10  2008/08/01 19:44:01  rubikitch
;; `anything-resume': resurrect previously invoked `anything'.
;;
;; Revision 1.9  2008/07/30 15:44:49  rubikitch
;; *** empty log message ***
;;
;; Revision 1.8  2008/07/30 15:38:51  rubikitch
;; *** empty log message ***
;;
;; Revision 1.7  2008/07/30 15:21:48  rubikitch
;; `anything-scroll-other-window', `anything-scroll-other-window-down':
;; Scroll other window (for persistent action).
;;
;; Revision 1.6  2008/07/30 15:12:36  rubikitch
;; *** empty log message ***
;;
;; Revision 1.5  2008/07/30 15:06:32  rubikitch
;; `anything-select-2nd-action', `anything-select-3rd-action', 
`anything-select-4th-action':
;; Select other than default action without pressing Tab.
;;
;; Revision 1.4  2008/07/30 14:58:27  rubikitch
;; `anything-current-buffer': Store current buffer when `anything' is invoked.
;; `anything-current-position': Restore position when keyboard-quitted.
;;
;; Revision 1.3  2008/07/30 14:38:04  rubikitch
;; Implemented persistent action.
;;
;; Revision 1.2  2008/07/30 13:37:16  rubikitch
;; Update doc.
;;
;; Revision 1.1  2008/07/30 13:22:06  rubikitch
;; New maintainer.
;;

(defvar anything-version "$Id: anything.el,v 1.89 2008/08/24 08:35:27 rubikitch 
Exp $")
(require 'cl)

;; User Configuration 

;; This is only an example. Customize it to your own taste!
(defvar anything-sources `(((name . "Buffers")
                            (candidates
                             . (lambda ()
                                 (remove-if (lambda (name)
                                              (or (equal name anything-buffer)
                                                  (eq ?\  (aref name 0))))
                                            (mapcar 'buffer-name 
(buffer-list)))))
                            (type . buffer))

                           ((name . "File Name History")
                            (candidates . file-name-history)
                            (match (lambda (candidate)
                                     ;; list basename matches first
                                     (string-match 
                                      anything-pattern 
                                      (file-name-nondirectory candidate)))

                                   (lambda (candidate)                          
           
                                     ;; and then directory part matches
                                     (let ((dir (file-name-directory 
candidate)))
                                       (if dir
                                           (string-match anything-pattern 
dir)))))
                            (type . file))

                           ((name . "Files from Current Directory")
                            (init . (lambda ()
                                      (setq anything-default-directory
                                            default-directory)))
                            (candidates . (lambda ()
                                            (directory-files
                                             anything-default-directory)))
                            (type . file))

                           ((name . "Manual Pages")
                            (candidates . ,(progn
                                             ;; XEmacs doesn't have a woman :)
                                             (declare (special woman-file-name
                                                               
woman-topic-all-completions))
                                             (condition-case nil
                                                 (progn
                                                   (require 'woman)
                                                   (woman-file-name "")
                                                   (sort (mapcar 'car
                                                                 
woman-topic-all-completions)
                                                         'string-lessp))
                                               (error nil))))
                            (action . (("Open Manual Page" . woman)))
                            (requires-pattern . 2))

                           ((name . "Complex Command History")
                            (candidates . (lambda ()
                                            (mapcar 'prin1-to-string
                                                    command-history)))
                            (action . (("Repeat Complex Command" . 
                                        (lambda (c)
                                          (eval (read c))))))
                            (delayed)))
  "The source of candidates for anything.
It accepts symbols:
 (setq anything-sources (list anything-c-foo anything-c-bar))
can be written as
 (setq anything-sources '(anything-c-foo anything-c-bar))
The latter is recommended because if you change anything-c-* variable,
you do not have to update `anything-sources'.

If you want to change `anything-sources' during `anything' invocation,
use `anything-set-sources', never use `setq'.

Attributes:

- name (mandatory)

  The name of the source. It is also the heading which appears
  above the list of matches from the source. Must be unique.

- candidates (mandatory if candidates-in-buffer attribute is not provided)

  Specifies how to retrieve candidates from the source. It can
  either be a variable name, a function called with no parameters
  or the actual list of candidates.

  The list must be a list of strings, so it's the responsibility
  of the source to convert candidates to strings if necessary.

  If the candidates have to be retrieved asynchronously (for
  example, by an external command which takes a while to run)
  then the function should start the external command
  asynchronously and return the associated process object.
  Anything will take care of managing the process (receiving the
  output from it, killing it if necessary, etc.). The process
  should return candidates matching the current pattern (see
  variable `anything-pattern'.)

  Note that currently results from asynchronous sources appear
  last in the anything buffer regardless of their position in
  `anything-sources'.

- action (mandatory if type attribute is not provided)

  It is a list of (DISPLAY . FUNCTION) pairs. FUNCTION is called
  with one parameter: the selected candidate.

  An action other than the default can be chosen from this list
  of actions for the currently selected candidate (by default
  with TAB). The DISPLAY string is shown in the completions
  buffer and the FUNCTION is invoked when an action is
  selected. The first action of the list is the default.

- type (optional if action attribute is provided)

  Indicates the type of the items the source returns. 

  Merge attributes not specified in the source itself from
  `anything-type-attributes'.

  This attribute is implemented by plug-in.

- init (optional)

  Function called with no parameters when anything is started. It
  is useful for collecting current state information which can be
  used to create the list of candidates later.

  For example, if a source needs to work with the current
  directory then it can store its value here, because later
  anything does its job in the minibuffer and in the
  `anything-buffer' and the current directory can be different
  there.

- match (optional)

  List of functions called with one parameter: a candidate. The
  function should return non-nil if the candidate matches the
  current pattern (see variable `anything-pattern').

  This attribute allows the source to override the default
  pattern matching based on `string-match'. It can be used, for
  example, to implement a source for file names and do the
  pattern matching on the basename of files, since it's more
  likely one is typing part of the basename when searching for a
  file, instead of some string anywhere else in its path.

  If the list contains more than one function then the list of
  matching candidates from the source is constructed by appending
  the results after invoking the first function on all the
  potential candidates, then the next function, and so on. The
  matching candidates supplied by the first function appear first
  in the list of results and then results from the other
  functions, respectively.

  This attribute has no effect for asynchronous sources (see
  attribute `candidates'), since they perform pattern matching
  themselves.

- candidate-transformer (optional)

  It's a function called with one argument when the completion
  list from the source is built. The argument is the list of
  candidates retrieved from the source. The function should
  return a transformed list of candidates which will be used for
  the actual completion.

  This can be used to transform or remove items from the list of
  candidates.

  The function can also substitute candidates in the returned
  list with (DISPLAY . REAL) pairs. In this case the DISPLAY
  string is shown in the Anything buffer, but the REAL one is
  used as action argument when the candidate is selected. This
  allows a more readable presentation for candidates which would
  otherwise be, for example, too long or have a common part
  shared with other candidates which can be safely replaced with
  an abbreviated string for display purposes.

  Note that if the (DISPLAY . REAL) form is used then pattern
  matching is done on the displayed string, not on the real
  value.

- filtered-candidate-transformer (optional)

  It has the same format as `candidate-transformer', except the
  function is called with two parameters: the candidate list and
  the source.

  This transformer is run on the candidate list which is already
  filtered by the current pattern. While `candidate-transformer'
  is run only once, it is run every time the input pattern is
  changed.

  It can be used to transform the candidate list dynamically, for
  example, based on the current pattern.

  In some cases it may also be more efficent to perform candidate
  transformation here, instead of with `candidate-transformer'
  even if this transformation is done every time the pattern is
  changed.  For example, if a candidate set is very large then
  `candidate-transformer' transforms every candidate while only
  some of them will actually be dislpayed due to the limit
  imposed by `anything-candidate-number-limit'.

  Note that `candidate-transformer' is run already, so the given
  transformer function should also be able to handle candidates
  with (DISPLAY . REAL) format.

  This option has no effect for asynchronous sources. (Not yet,
  at least.

- action-transformer (optional)

  It's a function called with two arguments when the action list
  from the source is assembled. The first argument is the list of
  actions, the second is the current selection.

  The function should return a transformed action list.

  This can be used to customize the list of actions based on the
  currently selected candidate.

- delayed (optional)

  Candidates from the source are shown only if the user stops
  typing and is idle for `anything-idle-delay' seconds.

- volatile (optional)

  Indicates the source assembles the candidate list dynamically,
  so it shouldn't be cached within a single Anything
  invocation. It is only applicable to synchronous sources,
  because asynchronous sources are not cached.

- requires-pattern (optional)

  If present matches from the source are shown only if the
  pattern is not empty. Optionally, it can have an integer
  parameter specifying the required length of input which is
  useful in case of sources with lots of candidates.

- persistent-action (optional)

  Function called with one parameter; the selected candidate.

  An action performed by `anything-execute-persistent-action'.
  If none, use the default action.

- candidates-in-buffer (optional)

  Shortcut attribute for making and narrowing candidates using
  buffers.  This newly-introduced attribute prevents us from
  forgetting to add volatile and match attributes.

  See docstring of `anything-candidates-in-buffer'.

  (candidates-in-buffer) is equivalent of three attributes:
    (candidates . anything-candidates-in-buffer)
    (volatile)
    (match identity)

  (candidates-in-buffer . candidates-function) is equivalent of:
    (candidates . candidates-function)
    (volatile)
    (match identity)

  This attribute is implemented by plug-in.

- search (optional)

  List of functions like `re-search-forward' or `search-forward'.
  Buffer search function used by `anything-candidates-in-buffer'.
  By default, `anything-candidates-in-buffer' uses `re-search-forward'.
  This attribute is meant to be used with
  (candidates . anything-candidates-in-buffer) or
  (candidates-in-buffer) in short.

- get-line (optional)

  A function like `buffer-substring-no-properties' or `buffer-substring'.
  This function converts point of line-beginning and point of line-end,
  which represents a candidate computed by `anything-candidates-in-buffer'.
  By default, `anything-candidates-in-buffer' uses
  `buffer-substring-no-properties'.

- display-to-real (optional)

  Function called with one parameter; the selected candidate.

  The function transforms the selected candidate, and the result
  is passed to the action function.  The display-to-real
  attribute provides another way to pass other string than one
  shown in Anything buffer.

  Traditionally, it is possible to make candidates,
  candidate-transformer or filtered-candidate-transformer
  function return a list with (DISPLAY . REAL) pairs. But if REAL
  can be generated from DISPLAY, display-to-real is more
  convenient and faster.

- cleanup (optional)

  Function called with no parameters when *anything* buffer is closed. It
  is useful for killing unneeded candidates buffer.

- candidate-number-limit (optional)

  Override `anything-candidate-number-limit' only for this source.

- dummy (optional)

  Set `anything-pattern' to candidate. If this attribute is
  specified, The candidates attribute is ignored.

  This attribute is implemented by plug-in.


")


;; This value is only provided as an example. Customize it to your own
;; taste!
(defvar anything-type-attributes
  '((file (action . (("Find File" . find-file)
                     ("Delete File" . (lambda (file)
                                        (if (y-or-n-p (format "Really delete 
file %s? "
                                                              file))
                                            (delete-file file)))))))
    (buffer (action . (("Switch to Buffer" . switch-to-buffer)
                       ("Pop to Buffer"    . pop-to-buffer)
                       ("Display Buffer"   . display-buffer)
                       ("Kill Buffer"      . kill-buffer)))))
  "It's a list of (TYPE ATTRIBUTES ...). ATTRIBUTES are the same
  as attributes for `anything-sources'. TYPE connects the value
  to the appropriate sources in `anything-sources'.

  This allows specifying common attributes for several
  sources. For example, sources which provide files can specify
  common attributes with a `file' type.")


(defvar anything-enable-digit-shortcuts nil
  "*If t then the first nine matches can be selected using
  Ctrl+<number>.")


(defvar anything-candidate-number-limit 50
  "*Do not show more candidates than this limit from inidividual
  sources. It is usually pointless to show hundreds of matches
  when the pattern is empty, because it is much simpler to type a
  few characters to narrow down the list of potential candidates.

  Set it to nil if you don't want this limit.")


(defvar anything-idle-delay 0.5
  "*The user has to be idle for this many seconds, before
  candidates from delayed sources are collected. This is useful
  for sources involving heavy operations (like launching external
  programs), so that candidates from the source are not retrieved
  unnecessarily if the user keeps typing.

  It also can be used to declutter the results anything displays,
  so that results from certain sources are not shown with every
  character typed, only if the user hesitates a bit.")


(defvar anything-input-idle-delay nil
  "The user has to be idle for this many seconds, before ALL candidates are 
collected.
Unlink `anything-input-idle', it is also effective for non-delayed sources.
If nil, candidates are collected immediately. ")


(defvar anything-samewindow nil
  "If t then Anything doesn't pop up a new window, it uses the
current window to show the candidates.")


(defvar anything-source-filter nil
  "A list of source names to be displayed. Other sources won't
appear in the search results. If nil then there is no filtering.
See also `anything-set-source-filter'.")


(defvar anything-map
  (let ((map (copy-keymap minibuffer-local-map)))
    (define-key map (kbd "<down>") 'anything-next-line)
    (define-key map (kbd "<up>") 'anything-previous-line)
    (define-key map (kbd "<prior>") 'anything-previous-page)
    (define-key map (kbd "<next>") 'anything-next-page)
    (define-key map (kbd "<right>") 'anything-next-source)
    (define-key map (kbd "<left>") 'anything-previous-source)
    (define-key map (kbd "<RET>") 'anything-exit-minibuffer)
    (define-key map (kbd "C-1") 'anything-select-with-digit-shortcut)
    (define-key map (kbd "C-2") 'anything-select-with-digit-shortcut)
    (define-key map (kbd "C-3") 'anything-select-with-digit-shortcut)
    (define-key map (kbd "C-4") 'anything-select-with-digit-shortcut)
    (define-key map (kbd "C-5") 'anything-select-with-digit-shortcut)
    (define-key map (kbd "C-6") 'anything-select-with-digit-shortcut)
    (define-key map (kbd "C-7") 'anything-select-with-digit-shortcut)
    (define-key map (kbd "C-8") 'anything-select-with-digit-shortcut)
    (define-key map (kbd "C-9") 'anything-select-with-digit-shortcut)
    (define-key map (kbd "C-i") 'anything-select-action)
    ;; the defalias is needed because commands are bound by name when
    ;; using iswitchb, so only commands having the prefix anything-
    ;; get rebound
    (defalias 'anything-previous-history-element 'previous-history-element)
    ;; C-p is used instead of M-p, because anything uses ESC
    ;; (currently hardcoded) for `anything-iswitchb-cancel-anything' and
    ;; Emacs handles ESC and Meta as synonyms, so ESC overrides
    ;; other commands with Meta prefix.
    ;;
    ;; Note that iswitchb uses M-p and M-n by default for history
    ;; navigation, so you should bind C-p and C-n in
    ;; `iswitchb-mode-map' if you use the history keys and don't want
    ;; to use different keys for iswitchb while anything is not yet
    ;; kicked in. These keys are not bound automatically by anything
    ;; in `iswitchb-mode-map' because they (C-n at least) already have
    ;; a standard iswitchb binding which you might be accustomed to.
    (define-key map (kbd "C-p") 'anything-previous-history-element)
    (defalias 'anything-next-history-element 'next-history-element)
    (define-key map (kbd "C-n") 'anything-next-history-element)
    ;; Binding M-s is used instead of C-s, because C-s has a binding in
    ;; iswitchb.  You can rebind it, of course.
    (define-key map (kbd "M-s") 'anything-isearch)
    ;; unbind C-r to prevent problems during anything-isearch
    (define-key map (kbd "C-r") nil)
    map)
  "Keymap for anything.")


(defvar anything-isearch-map
  (let ((map (copy-keymap (current-global-map))))
    (define-key map (kbd "<return>") 'anything-isearch-default-action)
    (define-key map (kbd "C-i") 'anything-isearch-select-action)
    (define-key map (kbd "C-g") 'anything-isearch-cancel)
    (define-key map (kbd "M-s") 'anything-isearch-again)
    (define-key map (kbd "<backspace>") 'anything-isearch-delete)
    ;; add printing chars
    (let ((i 32))
      (while (< i 256)
        (define-key map (vector i) 'anything-isearch-printing-char)
        (setq i (1+ i))))
    map)
  "Keymap for anything incremental search.")


(defgroup anything nil
  "Open anything." :prefix "anything-" :group 'convenience)

(if (facep 'header-line)
    (copy-face 'header-line 'anything-header)
  
  (defface anything-header 
    '((t (:bold t :underline t))) 
    "Face for header lines in the anything buffer." :group 'anything))

(defvar anything-header-face 'anything-header
  "Face for header lines in the anything buffer.")

(defface anything-isearch-match '((t (:background "Yellow")))
  "Face for isearch in the anything buffer." :group 'anything)

(defvar anything-isearch-match-face 'anything-isearch-match
  "Face for matches during incremental search.")

(defvar anything-iswitchb-idle-delay 1
  "Show anything completions if the user is idle that many
  seconds after typing.")

(defvar anything-iswitchb-dont-touch-iswithcb-keys nil
  "If t then those commands are not bound from `anything-map'
  under iswitchb which would override standard iswithcb keys.

This allows an even more seamless integration with iswitchb for
those who prefer using iswitchb bindings even if the anything
completions buffer is popped up.

Note that you can bind alternative keys for the same command in
`anything-map', so that you can use different keys for anything
under iswitchb. For example, I bind the character \ to
`anything-exit-minibuffer' which key is just above Enter on my
keyboard. This way I can switch buffers with Enter and choose
anything completions with \.")

;;----------------------------------------------------------------------
;; Public functions
;;----------------------------------------------------------------------
;;
;; These functions are the public API of Anything. See their
;; documentation for more information.
;;
;; anything 
;; anything-iswitchb-setup
;;
;; anything-set-source-filter
;;
;;   This function sets a filter for anything sources and it may be
;;   called while anything is running. It can be used to toggle
;;   displaying of sources dinamically. For example, additional keys
;;   can be bound into `anything-map' to display only the file-related
;;   results if there are too many matches from other sources and
;;   you're after files only:
;;
;;   Shift+F shows only file results from some sources:
;;
;;     (define-key anything-map "F" 'anything-my-show-files-only)
;;     
;;     (defun anything-my-show-files-only ()
;;       (interactive)
;;       (anything-set-source-filter '("File Name History"
;;                                     "Files from Current Directory")))
;;
;;   Shift+A shows all results:
;;
;;     (define-key anything-map "A" 'anything-my-show-all)
;;     
;;     (defun anything-my-show-all ()
;;       (interactive)
;;       (anything-set-source-filter nil))
;;  
;;  
;;   Note that you have to prefix the functions with anything- prefix,
;;   otherwise they won't be bound when Anything is used under
;;   Iswitchb. The -my- part is added to avoid collisions with
;;   existing Anything function names.
;;  

;;----------------------------------------------------------------------

(defvar anything-buffer "*anything*"
  "Buffer showing completions.")

(defvar anything-action-buffer "*anything action*"
  "Buffer showing actions.")

(defvar anything-selection-overlay nil
  "Overlay used to highlight the currently selected file.")

(defvar anything-isearch-overlay nil
  "Overlay used to highlight the current match during isearch.")

(defvar anything-digit-overlays nil
  "Overlays for digit shortcuts. See `anything-enable-digit-shortcuts'.")

(defvar anything-candidate-cache nil
  "Holds the available candidate withing a single anything invocation.")

(defvar anything-pattern
  "The input pattern used to update the anything buffer.")

(defvar anything-input
  "The input typed in the candidates panel.")

(defvar anything-async-processes nil
  "List of information about asynchronous processes managed by anything.")

(defvar anything-digit-shortcut-count 0
  "Number of digit shortcuts shown in the anything buffer.")

(defvar anything-before-initialize-hook nil
  "Run before anything initialization.
This hook is run before init functions in `anything-sources'.")

(defvar anything-after-initialize-hook nil
  "Run after anything initialization.
Global variables are initialized and the anything buffer is created.
But the anything buffer has no contents. ")

(defvar anything-update-hook nil
  "Run after the anything buffer was updated according the new
  input pattern.")

(defvar anything-cleanup-hook nil
  "Run after anything invocation.")

(defvar anything-restored-variables
  '( anything-candidate-number-limit
     anything-source-filter)
  "Variables which are restored after `anything' invocation.")
;; `anything-saved-sources' is removed

(defvar anything-saved-selection nil
  "Saved value of the currently selected object when the action
  list is shown.")

;; `anything-original-source-filter' is removed

(defvar anything-current-buffer nil
  "Current buffer when `anything' is invoked.")

(defvar anything-buffer-file-name nil
  "`buffer-file-name' when `anything' is invoked.")

(defvar anything-current-position nil
  "Cons of (point) and (window-start) when `anything' is invoked.
It is needed because restoring position when `anything' is keyboard-quitted.")

(defvar anything-saved-action nil
  "Saved value of the currently selected action by key.")

(defvar anything-last-sources nil
  "Sources of previously invoked `anything'.")

(defvar anything-saved-current-source nil
  "Saved value of the original (anything-get-current-source) when the action
  list is shown.")

(defvar anything-compiled-sources nil
  "Compiled version of `anything-sources'. ")

(defvar anything-in-persistent-action nil
  "Flag whether in persistent-action or not.")

(put 'anything 'timid-completion 'disabled)

;; internal variables
(defvar anything-test-candidate-list nil)
(defvar anything-test-mode nil)
(defvar anything-buffer-chars-modified-tick 0)
(make-variable-buffer-local 'anything-buffer-chars-modified-tick)
(defvar anything-source-name nil)
(defvar anything-candidate-buffer-alist nil)
(defvar anything-check-minibuffer-input-timer nil)
(defvar anything-match-hash (make-hash-table :test 'equal))
(defvar anything-cib-hash (make-hash-table :test 'equal))

(defmacro anything-aif (test-form then-form &optional else-form)
  "Anaphoric if. Temporary variable `it' is the result of test-form."
  `(let ((it ,test-form))
     (if it ,then-form ,else-form)))  
(put 'anything-aif 'lisp-indent-function 2)

(defun anything-mklist (obj)
  "If OBJ is a list, return itself, otherwise make a list with one element."
  (if (listp obj) obj (list obj)))

(defun anything-buffer-get ()
  "If *anything action* buffer is shown, return `anything-action-buffer', 
otherwise `anything-buffer'."
  (if (anything-action-window)
      anything-action-buffer
    anything-buffer))

(defun anything-window ()
  "Window of `anything-buffer'."
  (get-buffer-window (anything-buffer-get) 'visible))

(defun anything-action-window ()
  "Window of `anything-action-buffer'."
  (get-buffer-window anything-action-buffer 'visible))


(defmacro with-anything-window (&rest body)
  `(let ((--tmpfunc-- (lambda () ,@body)))
     (if anything-test-mode
         (with-current-buffer (anything-buffer-get)
           (funcall --tmpfunc--))
       (with-selected-window (anything-window)
         (funcall --tmpfunc--)))))
(put 'with-anything-window 'lisp-indent-function 0)

(defmacro with-anything-restore-variables(&rest body)
  "Restore variables specified by `anything-restored-variables' after executing 
BODY ."
  `(let ,(mapcar (lambda (v) (list v v)) anything-restored-variables)
     ,@body))

(defun anything-attr (attribute-name)
  "Get the value of ATTRIBUTE-NAME of current source.
It is useful to write your sources."
  (anything-aif (assoc attribute-name (anything-get-current-source))
      (anything-aif (cdr it) it t)))    ;second it == (cdr it)

(defun anything-check-minibuffer-input ()
  "Extract input string from the minibuffer and check if it needs
to be handled."
  (if (or (not anything-input-idle-delay) (anything-action-window))
      (anything-check-minibuffer-input-1)
    (if anything-check-minibuffer-input-timer
        (cancel-timer anything-check-minibuffer-input-timer))
    (setq anything-check-minibuffer-input-timer
          (run-with-idle-timer anything-input-idle-delay nil
                               'anything-check-minibuffer-input-1))))

(defun anything-check-minibuffer-input-1 ()
  (with-selected-window (minibuffer-window)
    (anything-check-new-input (minibuffer-contents))))  
(defun anything-check-new-input (input)
  "Check input string and update the anything buffer if
necessary."
  (unless (equal input anything-pattern)
    (setq anything-pattern input)
    (unless (anything-action-window)
      (setq anything-input anything-pattern))
    (anything-update)))


(defun anything-update ()
  "Update the list of matches in the anything buffer according to
the current pattern."
  (setq anything-digit-shortcut-count 0)
  (anything-kill-async-processes)
  (with-current-buffer (anything-buffer-get)
    (erase-buffer)

    (if anything-enable-digit-shortcuts
        (dolist (overlay anything-digit-overlays)
          (delete-overlay overlay)))

    (let (delayed-sources)
      (dolist (source (anything-get-sources))
        (if (or (not anything-source-filter)
                (member (assoc-default 'name source) anything-source-filter))
          (if (equal anything-pattern "")
              (unless (assoc 'requires-pattern source)
                (if (assoc 'delayed source)
                    (push source delayed-sources)
                  (anything-process-source source)))

            (let ((min-pattern-length (assoc-default 'requires-pattern source)))
              (unless (and min-pattern-length
                           (< (length anything-pattern) min-pattern-length))
                (if (assoc 'delayed source)
                    (push source delayed-sources)
                  (anything-process-source source)))))))

      (goto-char (point-min))
      (run-hooks 'anything-update-hook)
      (anything-next-line)


      (if anything-test-mode
          (dolist (source delayed-sources)
            (anything-process-source source))
        (anything-maybe-fit-frame)
        (run-with-idle-timer (if (featurep 'xemacs)
                                 0.1
                               0)
                             nil
                             'anything-process-delayed-sources
                             delayed-sources)))))

(defvar anything-compile-source-functions
  '(anything-compile-source--type anything-compile-source--dummy 
anything-compile-source--candidates-in-buffer)
  "Functions to compile elements of `anything-sources' (plug-in).")
(defvar anything-compile-source-functions-default 
anything-compile-source-functions
  "Plug-ins this file provides.")
(defun anything-get-sources ()
  "Return compiled `anything-sources', which is memoized.

Attributes:

- type
  `anything-type-attributes' are merged in.
- candidates-buffer
  candidates, volatile and match attrubute are created.
"
  (cond
   ;; action
   ((anything-action-window)
    anything-sources)
   ;; memoized
   (anything-compiled-sources)
   ;; first time
   (t
    (setq anything-compiled-sources
          (anything-compile-sources anything-sources 
anything-compile-source-functions)))))

(defun anything-compile-sources (sources funcs)
  "Compile sources (`anything-sources') with funcs 
(`anything-compile-source-functions').
Anything plug-ins are realized by this function."
  (mapcar
   (lambda (source)
     (loop with source = (if (listp source) source (symbol-value source))
           for f in funcs
           do (setq source (funcall f source))
           finally (return source)))
   sources))  


(defun anything-funcall-with-source (source func &rest args)
  (let ((anything-source-name (assoc-default 'name source)))
    (apply func args)))

(defun anything-candidate-number-limit (source)
  "`anything-candidate-number-limit' variable may be overridden by SOURCE."
  (or (assoc-default 'candidate-number-limit source)
      anything-candidate-number-limit
      99999999))

(defun anything-compute-matches (source)
  "Compute matches from SOURCE according to its settings."
  (let ((functions (assoc-default 'match source))
        (limit (anything-candidate-number-limit source))
        matches)
    (if (or (equal anything-pattern "") (equal functions '(identity)))
        (progn
          (setq matches (anything-get-cached-candidates source))
          (if (> (length matches) limit)
              (setq matches 
                    (subseq matches 0 limit))))

      (condition-case nil
          (let ((item-count 0) 
                exit)

            (unless functions
              (setq functions
                    (list (lambda (candidate)
                            (string-match anything-pattern candidate)))))

            (clrhash anything-match-hash)
            (dolist (function functions)
              (let (newmatches)
                (dolist (candidate (anything-get-cached-candidates source))
                  (when (and (not (gethash candidate anything-match-hash))
                             (funcall function (if (listp candidate)
                                                   (car candidate)
                                                 candidate)))
                    (puthash candidate t anything-match-hash)
                    (push candidate newmatches)

                    (when limit
                      (incf item-count)
                      (when (= item-count limit)
                        (setq exit t)
                        (return)))))

                (setq matches (append matches (reverse newmatches)))

                (if exit
                    (return)))))

        (invalid-regexp (setq matches nil))))

    (anything-aif (assoc-default 'filtered-candidate-transformer source)
        (setq matches
              (anything-funcall-with-source source it matches source)))
    matches))

(defun anything-process-source (source)
  "Display matches from SOURCE according to its settings."
  (let ((matches (anything-compute-matches source)))
    (when matches
      (when anything-test-mode
          (setq anything-test-candidate-list
                `(,@anything-test-candidate-list
                  (,(assoc-default 'name source)
                   ,matches))))
      (anything-insert-header (assoc-default 'name source))
      (dolist (match matches)
        (when (and anything-enable-digit-shortcuts
                   (not (eq anything-digit-shortcut-count 9)))
          (move-overlay (nth anything-digit-shortcut-count
                             anything-digit-overlays)
                        (line-beginning-position)
                        (line-beginning-position))
          (incf anything-digit-shortcut-count))

        (anything-insert-match match 'insert)))))

(defun anything-insert-match (match insert-function)
  "Insert MATCH into the anything buffer. If MATCH is a list then
insert the string inteneded to appear on the display and store
the real value in a text property."
  (if (not (listp match))
      (funcall insert-function match)

    (funcall insert-function (car match))
    (put-text-property (line-beginning-position) (line-end-position) 
                       'anything-realvalue (cdr match)))
  (funcall insert-function "\n"))


(defun anything-process-delayed-sources (delayed-sources)
  "Process delayed sources if the user is idle for
`anything-idle-delay' seconds."
  (if (sit-for (if anything-input-idle-delay
                   (max 0 (- anything-idle-delay anything-input-idle-delay))
                 anything-idle-delay))
      (with-current-buffer anything-buffer        
        (save-excursion
          (goto-char (point-max))
          (dolist (source delayed-sources)
            (anything-process-source source))

          (when (and (not (equal (buffer-size) 0))
                     ;; no selection yet
                     (= (overlay-start anything-selection-overlay)
                        (overlay-end anything-selection-overlay)))
            (goto-char (point-min))
            (run-hooks 'anything-update-hook)
            (anything-next-line)))

        (anything-maybe-fit-frame))))

(defun anything-normalize-sources (sources)
  (cond ((and sources (symbolp sources)) (list sources))
        (sources)
        (t anything-sources)))  

(defun anything (&optional sources input prompt resume preselect)
  "Select anything. In Lisp program, some optional arguments can be used.

- SOURCES

  Temporary value of `anything-sources'. SOURCES accepts a
  symbol, interpreted as a variable of an anything source.

- INPUT

  Temporary value of `anything-pattern', ie. initial input of minibuffer.

- PROMPT

  Prompt other than \"pattern: \".

- RESUME

  Resurrect previously instance of `anything'. Skip the initialization.

- PRESELECT

  Initially selected candidate. Specified by exact candidate or a regexp.
"
  ;; TODO more document
  (interactive)
  (condition-case v
      (with-anything-restore-variables
       (let ((frameconfig (current-frame-configuration))
             ;; It is needed because `anything-source-name' is non-nil
             ;; when `anything' is invoked by action. Awful global scope.
             anything-source-name anything-in-persistent-action
             ;; restored variables
             (anything-sources (anything-normalize-sources sources)))
         (add-hook 'post-command-hook 'anything-check-minibuffer-input)

         (unless resume (anything-initialize))

         (if anything-samewindow
             (switch-to-buffer anything-buffer)
           (pop-to-buffer anything-buffer))        

         (unwind-protect
             (progn
               (unless resume (anything-update))
               (select-frame-set-input-focus (window-frame (minibuffer-window)))
               (anything-preselect preselect)
               (let ((minibuffer-local-map anything-map))
                 (read-string (or prompt "pattern: ") (if resume 
anything-pattern input))))

           (anything-cleanup)
           (remove-hook 'post-command-hook 'anything-check-minibuffer-input)
           (set-frame-configuration frameconfig))
         (unwind-protect
             (anything-execute-selection-action)
           (anything-aif (get-buffer anything-action-buffer)
               (kill-buffer it)))))
    (quit
     (goto-char (car anything-current-position))
     (set-window-start (selected-window) (cdr anything-current-position)))))

(defun anything-resume ()
  "Resurrect previously invoked `anything'."
  (interactive)
  (anything (or anything-last-sources anything-sources) nil nil t))

(defun anything-execute-selection-action (&optional selection action 
clear-saved-action display-to-real)
  "If a candidate was selected then perform the associated
action."
  (setq selection (or selection (anything-get-selection)))
  (setq action (or action
                   anything-saved-action
                   (if (get-buffer anything-action-buffer)
                       (anything-get-selection anything-action-buffer)
                     (anything-get-action))))
  (let ((source (or anything-saved-current-source 
(anything-get-current-source))))
    (setq display-to-real
          (or display-to-real (assoc-default 'display-to-real source)
              #'identity))
    (if (and (listp action)
             (not (functionp action)))  ; lambda
        ;;select the default action
        (setq action (cdar action)))
    (unless clear-saved-action (setq anything-saved-action nil))
    (if (and selection action)
        (anything-funcall-with-source
         source  action
         (anything-funcall-with-source source display-to-real selection)))))


(defun* anything-get-selection (&optional (buffer anything-buffer))
  "Return the currently selected item or nil."
  (unless (zerop (buffer-size (get-buffer buffer)))
    (with-current-buffer buffer
      (let ((selection
             (or (get-text-property (overlay-start
                                     anything-selection-overlay)
                                    'anything-realvalue)
                 (buffer-substring-no-properties
                  (overlay-start anything-selection-overlay)
                  (1- (overlay-end anything-selection-overlay))))))
        (unless (equal selection "")
          selection)))))


(defun anything-get-action ()
  "Return the associated action for the selected candidate."
  (unless (zerop (buffer-size (get-buffer (anything-buffer-get))))
    (let* ((source (anything-get-current-source))
           (actions (assoc-default 'action source)))

      (anything-aif (assoc-default 'action-transformer source)
          (funcall it actions (anything-get-selection))
        actions))))

(defun anything-select-action ()
  "Select an action for the currently selected candidate.
If action buffer is selected, back to the anything buffer."
  (interactive)
  (cond ((get-buffer-window anything-action-buffer 'visible)
         (set-window-buffer (get-buffer-window anything-action-buffer) 
anything-buffer)
         (kill-buffer anything-action-buffer))
        (t
         (setq anything-saved-selection (anything-get-selection))
         (unless anything-saved-selection
           (error "Nothing is selected."))
         (setq anything-saved-current-source (anything-get-current-source))
         (let ((actions (anything-get-action)))
           (with-current-buffer (get-buffer-create anything-action-buffer)
             (erase-buffer)
             (buffer-disable-undo)
             (set-window-buffer (get-buffer-window anything-buffer) 
anything-action-buffer)
             (set (make-local-variable 'anything-sources)
                  `(((name . "Actions")
                     (candidates . ,actions))))
             (set (make-local-variable 'anything-source-filter) nil)
             (set (make-local-variable 'anything-selection-overlay) nil)
             (set (make-local-variable 'anything-digit-overlays) nil)
             (anything-initialize-overlays anything-action-buffer))
           (with-selected-window (minibuffer-window)
             (delete-minibuffer-contents))
           (setq anything-pattern 'dummy) ; so that it differs from the
                                        ; previous one
           
           (anything-check-minibuffer-input)))))

(defun anything-select-nth-action (n)
  "Select the nth action for the currently selected candidate."
  (setq anything-saved-selection (anything-get-selection))
  (unless anything-saved-selection
    (error "Nothing is selected."))
  (setq anything-saved-action (cdr (elt (anything-get-action) n)))
  (anything-exit-minibuffer))

(defun anything-select-2nd-action ()
  "Select the 2nd action for the currently selected candidate."
  (interactive)
  (anything-select-nth-action 1))

(defun anything-select-3rd-action ()
  "Select the 3rd action for the currently selected candidate."
  (interactive)
  (anything-select-nth-action 2))

(defun anything-select-4th-action ()
  "Select the 4th action for the currently selected candidate."
  (interactive)
  (anything-select-nth-action 3))

(defun anything-funcall-foreach (sym)
  "Call the sym function(s) for each source if any."
  (dolist (source (anything-get-sources))
    (when (symbolp source)
      (setq source (symbol-value source)))
    (anything-aif (assoc-default sym source)
        (dolist (func (if (functionp it) (list it) it))
          (anything-funcall-with-source source func)))))

(defun anything-initialize ()
  "Initialize anything settings and set up the anything buffer."
  (run-hooks 'anything-before-initialize-hook)
  (setq anything-current-buffer (current-buffer))
  (setq anything-buffer-file-name buffer-file-name)
  (setq anything-current-position (cons (point) (window-start)))
  (setq anything-compiled-sources nil)
  (setq anything-saved-current-source nil)
  ;; Call the init function for sources where appropriate
  (anything-funcall-foreach 'init)

  (setq anything-pattern "")
  (setq anything-input "")
  (setq anything-candidate-cache nil)
  (setq anything-last-sources anything-sources)

  (anything-create-anything-buffer)
  (run-hooks 'anything-after-initialize-hook))

(defun anything-create-anything-buffer (&optional test-mode)
  "Create newly created `anything-buffer'.
If TEST-MODE is non-nil, clear `anything-candidate-cache'."
  (when test-mode
    (setq anything-candidate-cache nil))
  (with-current-buffer (get-buffer-create anything-buffer)
    (buffer-disable-undo)
    (erase-buffer)
    (set (make-local-variable  'inhibit-read-only) t)
    (setq cursor-type nil)
    (setq mode-name "Anything"))
  (anything-initialize-overlays anything-buffer)
  (get-buffer anything-buffer))

(defun anything-initialize-overlays (buffer)
  (if anything-selection-overlay
      ;; make sure the overlay belongs to the anything buffer if
      ;; it's newly created
      (move-overlay anything-selection-overlay (point-min) (point-min)
                    (get-buffer buffer))

    (setq anything-selection-overlay 
          (make-overlay (point-min) (point-min) (get-buffer buffer)))
    (overlay-put anything-selection-overlay 'face 'highlight))

  (if anything-enable-digit-shortcuts
      (unless anything-digit-overlays
        (dotimes (i 9)
          (push (make-overlay (point-min) (point-min)
                              (get-buffer buffer))
                anything-digit-overlays)
          (overlay-put (car anything-digit-overlays)
                       'before-string (concat (int-to-string (1+ i)) " - ")))
        (setq anything-digit-overlays (nreverse anything-digit-overlays)))

    (when anything-digit-overlays
      (dolist (overlay anything-digit-overlays)
        (delete-overlay overlay))
      (setq anything-digit-overlays nil))))

(defun anything-cleanup ()
  "Clean up the mess."
  (with-current-buffer anything-buffer
    (setq cursor-type t))
  (with-current-buffer anything-current-buffer
    (setq anything-buffer-chars-modified-tick (buffer-chars-modified-tick)))
  (bury-buffer anything-buffer)
  (anything-funcall-foreach 'cleanup)
  (anything-kill-async-processes)
  (run-hooks 'anything-cleanup-hook))


(defun anything-previous-line ()
  "Move selection to the previous line."
  (interactive)
  (anything-move-selection 'line 'previous))


(defun anything-next-line ()
  "Move selection to the next line."
  (interactive)
  (anything-move-selection 'line 'next))


(defun anything-previous-page ()
  "Move selection back with a pageful."
  (interactive)
  (anything-move-selection 'page 'previous))


(defun anything-next-page ()
  "Move selection forward with a pageful."
  (interactive)
  (anything-move-selection 'page 'next))


(defun anything-previous-source ()
  "Move selection to the previous source."
  (interactive)
  (anything-move-selection 'source 'previous))


(defun anything-next-source ()
  "Move selection to the next source."
  (interactive)
  (anything-move-selection 'source 'next))


(defun anything-move-selection (unit direction)
  "Move the selection marker to a new position determined by
UNIT and DIRECTION."
  (unless (or (zerop (buffer-size (get-buffer (anything-buffer-get))))
              (not (anything-window)))
    (with-anything-window
      (case unit
        (line (forward-line (case direction
                              (next 1)
                              (previous -1)
                              (t (error "Invalid direction.")))))

        (page (case direction
                (next (condition-case nil
                          (scroll-up)
                        (end-of-buffer (goto-char (point-max)))))
                (previous (condition-case nil
                              (scroll-down)
                            (beginning-of-buffer (goto-char (point-min)))))
                (t (error "Invalid direction."))))

        (source (case direction
                   (next (goto-char (or (anything-get-next-header-pos)
                                        (point-min))))
                   (previous (progn
                               (forward-line -1)
                               (if (bobp)
                                   (goto-char (point-max))
                                 (if (anything-pos-header-line-p)
                                     (forward-line -1)
                                   (forward-line 1)))
                               (goto-char (anything-get-previous-header-pos))
                               (forward-line 1)))
                   (t (error "Invalid direction."))))

        (t (error "Invalid unit.")))

      (while (and (not (bobp)) (anything-pos-header-line-p))
        (forward-line (if (and (eq direction 'previous)
                               (not (eq (line-beginning-position)
                                        (point-min))))
                          -1
                        1)))
      (if (bobp)
          (forward-line 1))
      (if (eobp)
          (forward-line -1))

      (anything-mark-current-line))))


(defun anything-mark-current-line ()
  "Move selection overlay to current line."
  (move-overlay anything-selection-overlay
                (line-beginning-position)
                (1+ (line-end-position))))


(defun anything-select-with-digit-shortcut ()
  (interactive)
  (if anything-enable-digit-shortcuts
      (save-selected-window
        (select-window (anything-window))          
        (let* ((index (- (event-basic-type (elt (this-command-keys-vector) 0)) 
?1))
               (overlay (nth index anything-digit-overlays)))
          (when (overlay-buffer overlay)
            (goto-char (overlay-start overlay))
            (anything-mark-current-line)
            (anything-exit-minibuffer))))))


(defun anything-exit-minibuffer ()
  "Select the current candidate by exiting the minibuffer."
  (interactive)
  (declare (special anything-iswitchb-candidate-selected))
  (setq anything-iswitchb-candidate-selected (anything-get-selection))
  (exit-minibuffer))


(defun anything-get-current-source ()
  "Return the source for the current selection / in 
init/candidates/action/candidate-transformer/filtered-candidate-transformer 
function."
  (declare (special source))
  ;; The name `anything-get-current-source' should be used in init function etc.
  (if (and (boundp 'anything-source-name) (stringp anything-source-name))
      source
    (with-current-buffer (anything-buffer-get)
      ;; This goto-char shouldn't be necessary, but point is moved to
      ;; point-min somewhere else which shouldn't happen.
      (goto-char (overlay-start anything-selection-overlay))
      (let* ((header-pos (anything-get-previous-header-pos))
             (source-name
              (save-excursion
                (assert header-pos)
                (goto-char header-pos)
                (buffer-substring-no-properties
                 (line-beginning-position) (line-end-position)))))
        (some (lambda (source)
                (if (equal (assoc-default 'name source)
                           source-name)
                    source))
              (anything-get-sources))))))


(defun anything-get-next-header-pos ()
  "Return the position of the next header from point."
  (next-single-property-change (point) 'anything-header))


(defun anything-get-previous-header-pos ()
  "Return the position of the previous header from point"
  (previous-single-property-change (point) 'anything-header))


(defun anything-pos-header-line-p ()
  "Return t if the current line is a header line."
  (or (get-text-property (line-beginning-position) 'anything-header)
      (get-text-property (line-beginning-position) 'anything-header-separator)))


(defun anything-get-candidates (source)
  "Retrieve and return the list of candidates from
SOURCE."
  (let* ((candidate-source (assoc-default 'candidates source))
         (candidates
          (if (functionp candidate-source)
                (anything-funcall-with-source source candidate-source)
            (if (listp candidate-source)
                candidate-source
              (if (and (symbolp candidate-source)
                       (boundp candidate-source))
                  (symbol-value candidate-source)
                (error (concat "Candidates must either be a function, "
                               " a variable or a list: %s")
                       candidate-source))))))
    (if (processp candidates)
        candidates
      (anything-transform-candidates candidates source))))
         

(defun anything-transform-candidates (candidates source)
  "Transform CANDIDATES according to candidate transformers."
  (anything-aif (assoc-default 'candidate-transformer source)
      (anything-funcall-with-source source it candidates)
    candidates))


(defun anything-get-cached-candidates (source)
  "Return the cached value of candidates for SOURCE.
Cache the candidates if there is not yet a cached value."
  (let* ((name (assoc-default 'name source))
         (candidate-cache (assoc name anything-candidate-cache))
         candidates)

    (if candidate-cache
        (setq candidates (cdr candidate-cache))

      (setq candidates (anything-get-candidates source))

      (if (processp candidates)
          (progn
            (push (cons candidates
                        (append source 
                                (list (cons 'item-count 0)
                                      (cons 'incomplete-line ""))))
                  anything-async-processes)
            (set-process-filter candidates 'anything-output-filter)
            (setq candidates nil))

        (unless (assoc 'volatile source)
          (setq candidate-cache (cons name candidates))
          (push candidate-cache anything-candidate-cache))))

    candidates))

(defun anything-current-buffer-is-modified ()
  "Return non-nil when `anything-current-buffer' is modified since `anything' 
was invoked."
  (with-current-buffer anything-current-buffer
    (/= anything-buffer-chars-modified-tick (buffer-chars-modified-tick))))

(defun anything-output-filter (process string)
  "Process output from PROCESS."
  (let* ((process-assoc (assoc process anything-async-processes))
         (process-info (cdr process-assoc))
         (insertion-marker (assoc-default 'insertion-marker process-info))
         (incomplete-line-info (assoc 'incomplete-line process-info))
         (item-count-info (assoc 'item-count process-info)))

    (with-current-buffer anything-buffer
      (save-excursion
        (if insertion-marker
            (goto-char insertion-marker)
        
          (goto-char (point-max))
          (anything-insert-header (assoc-default 'name process-info))
          (setcdr process-assoc
                  (append process-info `((insertion-marker . 
,(point-marker))))))

        (let ((lines (split-string string "\n"))
              candidates)
          (while lines
            (if (not (cdr lines))
                ;; store last incomplete line until new output arrives
                (setcdr incomplete-line-info (car lines))

              (if (cdr incomplete-line-info)
                  (progn
                    (push (concat (cdr incomplete-line-info) (car lines))
                          candidates)
                    (setcdr incomplete-line-info nil))

              (push (car lines) candidates)))
                  
            (pop lines))

          (setq candidates (reverse candidates))
          (dolist (candidate (anything-transform-candidates candidates 
process-info))
            (anything-insert-match candidate 'insert-before-markers)
            (incf (cdr item-count-info))
            (when (>= (cdr item-count-info) anything-candidate-number-limit)
              (anything-kill-async-process process)
              (return)))))

      (anything-maybe-fit-frame)

      (run-hooks 'anything-update-hook)

      (if (bobp)
          (anything-next-line)

        (save-selected-window
          (select-window (get-buffer-window anything-buffer 'visible))
          (anything-mark-current-line))))))


(defun anything-kill-async-processes ()
  "Kill all known asynchronous processes according to
`anything-async-processes'."
    "Kill locate process."
    (dolist (process-info anything-async-processes)
      (anything-kill-async-process (car process-info)))
    (setq anything-async-processes nil))


(defun anything-kill-async-process (process)
  "Kill PROCESS and detach the associated functions."
  (set-process-filter process nil)
  (delete-process process))
  

(defun anything-insert-header (name)
  "Insert header of source NAME into the anything buffer."
  (unless (bobp)
    (let ((start (point)))
      (insert "\n")
      (put-text-property start (point) 'anything-header-separator t)))

  (let ((start (point)))
    (insert name)
    (put-text-property (line-beginning-position)
                       (line-end-position) 'anything-header t)
    (insert "\n")
    (put-text-property start (point) 'face anything-header-face)))


(defun anything-set-source-filter (sources)
  "Sets the value of `anything-source-filter' and updates the list of results."
  (setq anything-source-filter sources)
  (anything-update))


(defun anything-maybe-fit-frame ()
  "Fit anything frame to its buffer, and put it at top right of display.
 To inhibit fitting, set `fit-frame-inhibit-fitting-flag' to t.
 You can set user options `fit-frame-max-width-percent' and
 `fit-frame-max-height-percent' to control max frame size."
  (declare (warn (unresolved 0)))
  (when (and (require 'fit-frame nil t)
             (boundp 'fit-frame-inhibit-fitting-flag)
             (not fit-frame-inhibit-fitting-flag)
             (anything-window))
    (with-anything-window
      (fit-frame nil nil nil t)
      (modify-frame-parameters
       (selected-frame)
       `((left . ,(- (x-display-pixel-width) (+ (frame-pixel-width) 7)))
         (top . 0)))))) ; The (top . 0) shouldn't be necessary (Emacs bug).

(defun anything-preselect (candidate-or-regexp)
  (when candidate-or-regexp
    (with-anything-window
      (goto-char (point-min))
      ;; go to first candidate of first source
      (forward-line 1)
      (let ((start (point)))
        (unless (or (re-search-forward (concat "^" (regexp-quote 
candidate-or-regexp) "$") nil t)
                (progn (goto-char start)
                       (re-search-forward candidate-or-regexp nil t)))
          (goto-char start))
        (anything-mark-current-line)))))

(defun anything-set-sources (sources)
  "Set `anything-sources' during `anything' invocation."
  (setq anything-compiled-sources nil
        anything-sources sources))

;;---------------------------------------------------------------------
;; The smallest plug-in: type (built-in)
;;----------------------------------------------------------------------
(defun anything-compile-source--type (source)
  (anything-aif (assoc-default 'type source)
      (append source (assoc-default it anything-type-attributes) nil)
    source))

;;----------------------------------------------------------------------
;; dummy plug-in (built-in)
;;----------------------------------------------------------------------
(defun anything-dummy-candidate (candidate source)
  ;; `source' is defined in filtered-candidate-transformer
  (list anything-pattern))  

(defun anything-compile-source--dummy (source)
  (if (assoc 'dummy source)
      (append '((candidates "dummy")
                (match identity)
                (filtered-candidate-transformer . anything-dummy-candidate)
                (volatile))
              source)
    source))

;;---------------------------------------------------------------------
;; candidates-in-buffer plug-in (built-in)
;;----------------------------------------------------------------------
(defun anything-candidates-in-buffer ()
  "Get candidates from the candidates buffer according to `anything-pattern'.

BUFFER is `anything-candidate-buffer' by default.  Each
candidate must be placed in one line.  This function is meant to
be used in candidates-in-buffer or candidates attribute of an
anything source.  Especially fast for many (1000+) candidates.

eg.
 '((name . \"many files\")
   (init . (lambda () (with-current-buffer (anything-candidate-buffer 'local)
                        (insert-many-filenames))))
   (search re-search-forward)  ; optional
   (candidates-in-buffer)
   (type . file))

+===============================================================+
| The new way of making and narrowing candidates: Using buffers |
+===============================================================+

By default, `anything' makes candidates by evaluating the
candidates function, then narrows them by `string-match' for each
candidate.

But this way is very slow for many candidates. The new way is
storing all candidates in a buffer and narrowing them by
`re-search-forward'. Search function is customizable by search
attribute. The important point is that buffer processing is MUCH
FASTER than string list processing and is the Emacs way.

The init function writes all candidates to a newly-created
candidate buffer.  The candidates buffer is created or specified
by `anything-candidate-buffer'.  Candidates are stored in a line.

The candidates function narrows all candidates, IOW creates a
subset of candidates dynamically. It is the task of
`anything-candidates-in-buffer'.  As long as
`anything-candidate-buffer' is used,`(candidates-in-buffer)' is
sufficient in most cases.

Note that `(candidates-in-buffer)' is shortcut of three attributes:
  (candidates . anything-candidates-in-buffer)
  (volatile)
  (match identity)
And `(candidates-in-buffer . func)' is shortcut of three attributes:
  (candidates . func)
  (volatile)
  (match identity)
The expansion is performed in `anything-get-sources'.

The candidates-in-buffer attribute implies the volatile attribute.
The volatile attribute is needed because `anything-candidates-in-buffer'
creates candidates dynamically and need to be called everytime
`anything-pattern' changes.

Because `anything-candidates-in-buffer' plays the role of `match' attribute
function, specifying `(match identity)' makes the source slightly faster.

To customize `anything-candidates-in-buffer' behavior, use search
and get-line attributes. See also `anything-sources' docstring.
"
  (declare (special source))
  (anything-candidates-in-buffer-1 (anything-candidate-buffer)
                                   anything-pattern
                                   (or (assoc-default 'get-line source)
                                       #'buffer-substring-no-properties)
                                   ;; use external variable `source'.
                                   (or (assoc-default 'search source)
                                       '(re-search-forward))
                                   (anything-candidate-number-limit source)))

(defmacro anything-with-open-first-line (&rest body)
  `(progn
     (goto-char (point-min))
     (let (buffer-read-only)
       (insert "\n")
       (forward-line -1)
       ,@body
       (goto-char (point-min))
       (delete-char 1)
       (set-buffer-modified-p nil))))
(put 'anything-with-open-first-line 'lisp-indent-function 0)

(defun* anything-candidates-in-buffer-1 (buffer &optional (pattern 
anything-pattern) (get-line-fn 'buffer-substring-no-properties) (search-fns 
'(re-search-forward)) (limit anything-candidate-number-limit))
  ;; buffer == nil when candidates buffer does not exist.
  (when buffer
    (with-current-buffer buffer
      (goto-char (point-min))
      (if (string= pattern "")
          (delq nil (loop until (eobp)
                          for i from 1 to limit
                          collecting (funcall get-line-fn (point-at-bol) 
(point-at-eol))
                          do (forward-line 1)))
        (let ((i 1) matches exit newmatches)
          (anything-with-open-first-line
            (clrhash anything-cib-hash)
            (dolist (searcher search-fns)
              (goto-char (point-min))
              (setq newmatches nil)
              (loop while (funcall searcher pattern nil t)
                    if (or (eobp) (< limit i))
                    do (setq exit t) (return)
                    else do
                    (let ((cand (funcall get-line-fn (point-at-bol) 
(point-at-eol))))
                      (unless (gethash cand anything-cib-hash)
                        (puthash cand t anything-cib-hash)
                        (incf i)
                        (push cand newmatches)))
                    (forward-line 1))
              (setq matches (append matches (nreverse newmatches)))
              (if exit (return))))
          (delq nil matches))))))


(defun anything-candidate-buffer (&optional create-or-buffer)
  "Register and return a buffer containing candidates of current source.
`anything-candidate-buffer' searches buffer-local candidates buffer first,
then global candidates buffer.

Acceptable values of CREATE-OR-BUFFER:

- nil (omit)
  Only return the candidates buffer.
- a buffer
  Register a buffer as a candidates buffer.
- 'global
  Create a new global candidates buffer,
  named \" *anything candidates:SOURCE*\".
- other non-nil value
  Create a new global candidates buffer,
  named \" *anything candidates:SOURCE*ANYTHING-CURRENT-BUFFER\".
"
  (let* ((gbufname (format " *anything candidates:%s*" anything-source-name))
         (lbufname (concat gbufname (buffer-name anything-current-buffer)))
         buf)
    (when create-or-buffer
      (if (bufferp create-or-buffer)
          (add-to-list 'anything-candidate-buffer-alist
                       (cons anything-source-name create-or-buffer))
        (with-current-buffer
            (get-buffer-create (if (eq create-or-buffer 'global) gbufname 
lbufname))
          (buffer-disable-undo)
          (erase-buffer)
          (font-lock-mode -1))))
    (or (get-buffer lbufname)
        (get-buffer gbufname)
        (assoc-default anything-source-name anything-candidate-buffer-alist))))

(defun anything-compile-source--candidates-in-buffer (source)
  (anything-aif (assoc 'candidates-in-buffer source)
      (append source `((candidates . ,(or (cdr it) 
'anything-candidates-in-buffer))
                       (volatile) (match identity)))
    source))

;;---------------------------------------------------------------------
;; Persistent Action
;;----------------------------------------------------------------------
(defun anything-execute-persistent-action ()
  "If a candidate is selected then perform the associated action without 
quitting anything."
  (interactive)
  (save-selected-window
    (select-window (get-buffer-window (anything-buffer-get)))
    (select-window (setq minibuffer-scroll-window
                         (if (one-window-p t) (split-window)
                           (next-window (selected-window) 1))))
    (let ((anything-in-persistent-action t))
      (anything-execute-selection-action
       nil
       (or (assoc-default 'persistent-action (anything-get-current-source))
           (anything-get-action))
       t))))

;; scroll-other-window(-down)? for persistent-action
(defun anything-scroll-other-window-base (command)
  (save-selected-window
    (other-window 2)
    (call-interactively command)))

(defun anything-scroll-other-window ()
  "Scroll other window (not *Anything* window) upward."
  (interactive)
  (anything-scroll-other-window-base 'scroll-other-window))
(defun anything-scroll-other-window-down ()
  "Scroll other window (not *Anything* window) downward."
  (interactive)
  (anything-scroll-other-window-base 'scroll-other-window-down))

;;---------------------------------------------------------------------
;; Incremental search within results
;;----------------------------------------------------------------------

(defvar anything-isearch-original-global-map nil
  "Original global map before Anything isearch is started.")

(defvar anything-isearch-original-message-timeout nil
  "Original message timeout before Anything isearch is started.")

(defvar anything-isearch-pattern nil
  "The current isearch pattern.")

(defvar anything-isearch-message-suffix ""
  "Message suffix indicating the current state of the search.")

(defvar anything-isearch-original-point nil
  "Original position of point before isearch is started.")

(defvar anything-isearch-original-window nil
  "Original selected window before isearch is started.")

(defvar anything-isearch-original-cursor-in-non-selected-windows nil
  "Original value of cursor-in-non-selected-windows before isearch is started.")

(defvar anything-isearch-original-post-command-hook nil
  "Original value of post-command-hook before isearch is started.")

(defvar anything-isearch-match-positions nil
  "Stack of positions of matches or non-matches.

It's a list of plists with two properties: `event', the last user
 event, `start', the start position of the current match, and
 `pos', the position of point after that event.

The value of `event' can be the following symbols: `char' if a
character was typed, `error' if a non-matching character was
typed, `search' if a forward search had to be done after a
character, and `search-again' if a search was done for the next
occurrence of the current pattern.")

(defvar anything-isearch-match-start nil
  "Start position of the current match.")


(defun anything-isearch ()
  "Start incremental search within results."
  (interactive)
  (if (zerop (buffer-size (get-buffer (anything-buffer-get))))
      (message "There are no results.")

    (setq anything-isearch-original-message-timeout minibuffer-message-timeout)
    (setq minibuffer-message-timeout nil)

    (setq anything-isearch-original-global-map global-map)

    (condition-case nil
        (progn
          (setq anything-isearch-original-window (selected-window))
          (select-window (anything-window))
          (setq cursor-type t)

          (setq anything-isearch-original-post-command-hook
                (default-value 'post-command-hook))
          (setq-default post-command-hook nil)
          (add-hook 'post-command-hook 'anything-isearch-post-command)

          (use-global-map anything-isearch-map)
          (setq overriding-terminal-local-map anything-isearch-map)

          (setq anything-isearch-pattern "")

          (setq anything-isearch-original-cursor-in-non-selected-windows
                cursor-in-non-selected-windows)
          (setq cursor-in-non-selected-windows nil) 

          (setq anything-isearch-original-point (point-marker))
          (goto-char (point-min))
          (forward-line)
          (anything-mark-current-line)

          (setq anything-isearch-match-positions nil)
          (setq anything-isearch-match-start (point-marker))

          (if anything-isearch-overlay
              ;; make sure the overlay belongs to the anything buffer
              (move-overlay anything-isearch-overlay (point-min) (point-min)
                            (get-buffer (anything-buffer-get)))

            (setq anything-isearch-overlay (make-overlay (point-min) 
(point-min)))
            (overlay-put anything-isearch-overlay 'face 
anything-isearch-match-face))

          (setq anything-isearch-message-suffix
                (substitute-command-keys "cancel with 
\\[anything-isearch-cancel]")))

      (error (anything-isearch-cleanup)))))


(defun anything-isearch-post-command ()
  "Print the current pattern after every command."
  (anything-isearch-message)
  (when (anything-window)
    (with-anything-window
      (move-overlay anything-isearch-overlay anything-isearch-match-start 
(point)
                    (get-buffer (anything-buffer-get))))))


(defun anything-isearch-printing-char ()
  "Add printing char to the pattern."
  (interactive)
  (let ((char (char-to-string last-command-char)))
    (setq anything-isearch-pattern (concat anything-isearch-pattern char))

    (with-anything-window
      (if (looking-at char)
          (progn
            (push (list 'event 'char
                        'start anything-isearch-match-start
                        'pos (point-marker))
                  anything-isearch-match-positions)
            (forward-char))

        (let ((start (point)))
          (while (and (re-search-forward anything-isearch-pattern nil t)
                      (anything-pos-header-line-p)))
          (if (or (anything-pos-header-line-p)
                  (eq start (point)))
              (progn
                (goto-char start)
                (push (list 'event 'error
                            'start anything-isearch-match-start
                            'pos (point-marker))
                      anything-isearch-match-positions))

            (push (list 'event 'search
                        'start anything-isearch-match-start
                        'pos (copy-marker start))
                  anything-isearch-match-positions)
            (setq anything-isearch-match-start (copy-marker (match-beginning 
0))))))
  
      (anything-mark-current-line))))


(defun anything-isearch-again ()
  "Search again for the current pattern"
  (interactive)
  (if (equal anything-isearch-pattern "")
      (setq anything-isearch-message-suffix "no pattern yet")

    (with-anything-window
      (let ((start (point)))
        (while (and (re-search-forward anything-isearch-pattern nil t)
                    (anything-pos-header-line-p)))
        (if (or (anything-pos-header-line-p)
                (eq start (point)))
            (progn
              (goto-char start)
              (unless (eq 'error (plist-get (car 
anything-isearch-match-positions)
                                            'event))
                (setq anything-isearch-message-suffix "no more matches")))

          (push (list 'event 'search-again
                      'start anything-isearch-match-start
                      'pos (copy-marker start))
                anything-isearch-match-positions)
          (setq anything-isearch-match-start (copy-marker (match-beginning 0)))

          (anything-mark-current-line))))))


(defun anything-isearch-delete ()
  "Undo last event."
  (interactive)
  (unless (equal anything-isearch-pattern "")
    (let ((last (pop anything-isearch-match-positions)))
      (unless (eq 'search-again (plist-get last 'event))
        (setq anything-isearch-pattern
              (substring anything-isearch-pattern 0 -1)))

      (with-anything-window      
        (goto-char (plist-get last 'pos))
        (setq anything-isearch-match-start (plist-get last 'start))
        (anything-mark-current-line)))))


(defun anything-isearch-default-action ()
  "Execute the default action for the selected candidate."
  (interactive)
  (anything-isearch-cleanup)
  (with-current-buffer (anything-buffer-get) (anything-exit-minibuffer)))


(defun anything-isearch-select-action ()
  "Choose an action for the selected candidate."
  (interactive)
  (anything-isearch-cleanup)
  (with-anything-window
    (anything-select-action)))


(defun anything-isearch-cancel ()
  "Cancel Anything isearch."
  (interactive)
  (anything-isearch-cleanup)
  (when (anything-window)
    (with-anything-window
      (goto-char anything-isearch-original-point)
      (anything-mark-current-line))))


(defun anything-isearch-cleanup ()
  "Clean up the mess."
  (setq minibuffer-message-timeout anything-isearch-original-message-timeout)
  (with-current-buffer (anything-buffer-get)
    (setq overriding-terminal-local-map nil)
    (setq cursor-type nil)
    (setq cursor-in-non-selected-windows
          anything-isearch-original-cursor-in-non-selected-windows))
  (when anything-isearch-original-window
    (select-window anything-isearch-original-window))

  (use-global-map anything-isearch-original-global-map)
  (setq-default post-command-hook anything-isearch-original-post-command-hook)
  (when (overlayp anything-isearch-overlay) 
    (delete-overlay anything-isearch-overlay)))


(defun anything-isearch-message ()
  "Print prompt."
  (if (and (equal anything-isearch-message-suffix "")
           (eq (plist-get (car anything-isearch-match-positions) 'event)
               'error))
      (setq anything-isearch-message-suffix "failing"))

  (unless (equal anything-isearch-message-suffix "")
    (setq anything-isearch-message-suffix 
          (concat " [" anything-isearch-message-suffix "]")))

  (message (concat "Search within results: "
                   anything-isearch-pattern
                   anything-isearch-message-suffix))

  (setq anything-isearch-message-suffix ""))


;;---------------------------------------------------------------------
;; Iswitchb integration
;;----------------------------------------------------------------------

(defvar anything-iswitchb-candidate-selected nil
  "Indicates whether an anything candidate is selected from iswitchb.")

(defvar anything-iswitchb-frame-configuration nil
  "Saved frame configuration, before anything buffer was displayed.")

(defvar anything-iswitchb-saved-keys nil
  "The original in iswitchb before binding anything keys.")


(defun anything-iswitchb-setup ()
  "Integrate anything completion into iswitchb.

If the user is idle for `anything-iswitchb-idle-delay' seconds
after typing something into iswitchb then anything candidates are
shown for the current iswitchb input.

ESC cancels anything completion and returns to normal iswitchb."
  (interactive)

  (require 'iswitchb)

  ;; disable timid completion during iswitchb
  (put 'iswitchb-buffer 'timid-completion 'disabled)
  (add-hook 'minibuffer-setup-hook  'anything-iswitchb-minibuffer-setup)

  (defadvice iswitchb-visit-buffer
    (around anything-iswitchb-visit-buffer activate)
    (if anything-iswitchb-candidate-selected
        (anything-execute-selection-action)
      ad-do-it))

  (defadvice iswitchb-possible-new-buffer
    (around anything-iswitchb-possible-new-buffer activate)
    (if anything-iswitchb-candidate-selected
        (anything-execute-selection-action)
      ad-do-it))

  (message "Iswitchb integration is activated."))


(defun anything-iswitchb-minibuffer-setup ()
  (when (eq this-command 'iswitchb-buffer)
    (add-hook 'minibuffer-exit-hook  'anything-iswitchb-minibuffer-exit)

    (setq anything-iswitchb-frame-configuration nil)
    (setq anything-iswitchb-candidate-selected nil)
    (add-hook 'anything-update-hook 'anything-iswitchb-handle-update)

    (anything-initialize)
    
    (add-hook 'post-command-hook 'anything-iswitchb-check-input)))


(defun anything-iswitchb-minibuffer-exit ()
  (remove-hook 'minibuffer-exit-hook  'anything-iswitchb-minibuffer-exit)
  (remove-hook 'post-command-hook 'anything-iswitchb-check-input)
  (remove-hook 'anything-update-hook 'anything-iswitchb-handle-update)

  (anything-cleanup)

  (when anything-iswitchb-frame-configuration
    (set-frame-configuration anything-iswitchb-frame-configuration)
    (setq anything-iswitchb-frame-configuration nil)))


(defun anything-iswitchb-check-input ()
  "Extract iswitchb input and check if it needs to be handled."
  (declare (special iswitchb-text))
  (if (or anything-iswitchb-frame-configuration
          (sit-for anything-iswitchb-idle-delay))
      (anything-check-new-input iswitchb-text)))


(defun anything-iswitchb-handle-update ()
  "Pop up the anything buffer if it's not empty and it's not
shown yet and bind anything commands in iswitchb."
  (unless (or (equal (buffer-size (get-buffer anything-buffer)) 0)
              anything-iswitchb-frame-configuration)
    (setq anything-iswitchb-frame-configuration (current-frame-configuration))

    (save-selected-window 
      (if (not anything-samewindow)
          (pop-to-buffer anything-buffer)

        (select-window (get-lru-window))
        (switch-to-buffer anything-buffer)))

    (with-current-buffer (window-buffer (active-minibuffer-window))
      (let* ((anything-prefix "anything-")
             (prefix-length (length anything-prefix))
             (commands 
              (delete-dups
               (remove-if 'null
                          (mapcar 
                           (lambda (binding)
                             (let ((command (cdr binding)))
                               (when (and (symbolp command)
                                          (eq (compare-strings 
                                               anything-prefix 
                                               0 prefix-length
                                               (symbol-name command)
                                               0 prefix-length)
                                              t))
                                 command)))
                           (cdr anything-map)))))
             (bindings (mapcar (lambda (command)
                                 (cons command 
                                       (where-is-internal command 
anything-map)))
                               commands)))

        (push (list 'anything-iswitchb-cancel-anything (kbd "<ESC>"))
              bindings)

        (setq anything-iswitchb-saved-keys nil)

      (let* ((iswitchb-prefix "iswitchb-")
             (prefix-length (length iswitchb-prefix)))
        (dolist (binding bindings)
          (dolist (key (cdr binding))
            (let ((old-command (lookup-key (current-local-map) key)))
              (unless (and anything-iswitchb-dont-touch-iswithcb-keys
                           (symbolp old-command)
                           (eq (compare-strings iswitchb-prefix 
                                                0 prefix-length
                                                (symbol-name old-command)
                                                0 prefix-length)
                               t))
                (push (cons key old-command)
                      anything-iswitchb-saved-keys)
                (define-key (current-local-map) key (car binding)))))))))))


(defun anything-iswitchb-cancel-anything ()
  "Cancel anything completion and return to standard iswitchb."
  (interactive)
  (save-excursion
    (dolist (binding anything-iswitchb-saved-keys)
      (define-key (current-local-map) (car binding) (cdr binding)))
    (anything-iswitchb-minibuffer-exit)))


;;----------------------------------------------------------------------
;; `completing-read' compatible read function (experimental)
;;----------------------------------------------------------------------
(defun anything-completing-read (prompt collection &optional predicate 
require-match initial hist default inherit-input-method)
  (let ((result (anything (acr-sources
                           prompt
                           (if (arrayp collection)
                               (all-completions "" collection)
                             collection)
                           predicate require-match initial
                          hist default inherit-input-method)
                         initial prompt)))
    (when (stringp result)
      (prog1 result
        (add-to-list (or hist 'minibuffer-history) result)))))

(defun acr-sources (prompt collection predicate require-match initial hist 
default inherit-input-method)
  "`anything' replacement for `completing-read'."
  (let ((transformer-func
         (if predicate
             `(candidate-transformer
               . (lambda (cands)
                   (remove-if-not (lambda (c) (,predicate
                                               (if (listp c) (car c) c))) 
cands)))))
        (new-input-source (unless require-match
                            `((name . prompt) (dummy) (action . identity))))
        (history-source (unless require-match
                          `((name . "History")
                            (candidates . ,(or hist 'minibuffer-history))
                            (action . identity))))
        (default-source (when default
                          `((name . "Default")
                            (candidates  ,default)
                            (filtered-candidate-transformer
                             . (lambda (cands source)
                                 (if (string= anything-pattern "")  cands nil)))
                            (action . identity)))))
  `(,default-source
    ((name . "Completions")
     (candidates . ,collection)
     (action . identity)
     ,transformer-func)
    ,history-source
    ,new-input-source)))
;; (anything-completing-read "Command: " obarray 'commandp t)
;; (anything-completing-read "Test: " '(("hoge")("foo")("bar")) nil t)
;; (completing-read "Test: " '(("hoge")("foo")("bar")) nil t)
;; (anything-completing-read "Test: " '(("hoge")("foo")("bar")) nil nil "f" nil)
;; (completing-read "Test: " '(("hoge")("foo")("bar")) nil nil "f" nil nil t)
;; (anything-completing-read "Test: " '(("hoge")("foo")("bar")) nil nil nil nil 
"nana")
;; (anything-completing-read "Test: " '("hoge" "foo" "bar"))

;;----------------------------------------------------------------------
;; `read-file-name' compatible read function (experimental)
;;----------------------------------------------------------------------
(defvar anything-read-file-name-map (copy-keymap anything-map))
(define-key anything-read-file-name-map "/" 
'anything-read-file-name-follow-directory)

(defun anything-read-file-name-follow-directory ()
  (interactive)
  (declare (special dir prompt default-filename require-match predicate))
  (let* ((sel (anything-get-selection))
         (f (expand-file-name sel dir)))
    (cond ((and (file-directory-p f) (not (string-match "/\\.$" sel)))
           (with-selected-window (minibuffer-window) 
(delete-minibuffer-contents))
           (setq anything-pattern "")
           (setq dir f)
           (anything-set-sources
            (arfn-sources
             prompt f default-filename require-match nil predicate))
           (anything-update))
          (t
           (insert "/")))))

(defun anything-read-file-name (prompt &optional dir default-filename 
require-match initial-input predicate)
  "`anything' replacement for `read-file-name'."
  (let* ((anything-map anything-read-file-name-map)
         (result (anything (arfn-sources
                           prompt dir default-filename require-match
                           initial-input predicate)
                          initial-input prompt)))
    (when (stringp result)
      (prog1 result
        (add-to-list 'minibuffer-history result)))))

(defun arfn-candidates (dir)
  (loop for (f _ _ _ _ _ _ _ _ perm _ _ _) in (directory-files-and-attributes 
dir t)
        for basename = (file-name-nondirectory f)
        when (string= "d" (substring perm 0 1))
        collect (cons (concat basename "/") f)
        else collect (cons basename f)))

(defun arfn-sources (prompt dir default-filename require-match initial-input 
predicate)
  (let* ((dir (or dir default-directory))
         (transformer-func
          (if predicate
              `(candidate-transformer
                . (lambda (cands)
                    (remove-if-not
                     (lambda (c) (,predicate (if (listp c) (car c) c))) 
cands)))))
         (new-input-source (unless require-match
                             `((name . ,prompt) (dummy) '(action . identity))))
         (history-source (unless require-match
                           `((name . "History")
                             (candidates . minibuffer-history)
                             (action . identity))))
         (d2r `(display-to-real . (lambda (f) (expand-file-name f ,dir))))
         (default-source (when default-filename
                           `((name . "Default")
                             (candidates  ,default-filename)
                             (filtered-candidate-transformer
                              . (lambda (cands source)
                                  (if (string= anything-pattern "")  cands 
nil)))
                             ,d2r
                             (action . identity)))))
    `(,default-source
       ((name . ,dir)
        (candidates . (lambda () (arfn-candidates dir)))
        (action . identity)
        ,transformer-func)
       ,new-input-source
       ,history-source)))
;; (anything-read-file-name "file: " "~" ".emacs")
;; (read-file-name "file: " "/tmp")

(defvar anything-read-string-mode nil)
(unless anything-read-string-mode
  (defalias 'anything-old-completing-read (symbol-function 'completing-read))
  (defalias 'anything-old-read-file-name (symbol-function 'read-file-name)))
  
;; (anything-read-string-mode -1)
;; (anything-read-string-mode 1)
;; (anything-read-string-mode 0)
(defun anything-read-string-mode (arg)
  "If this minor mode is on, use `anything' version of `completing-read' and 
`read-file-name'."
  (interactive "P")
  (setq anything-read-string-mode (if arg (> (prefix-numeric-value arg) 0) (not 
anything-read-string-mode)))
  (cond (anything-read-string-mode
         ;; redefine to anything version
         (defalias 'completing-read (symbol-function 'anything-completing-read))
         (defalias 'read-file-name (symbol-function 'anything-read-file-name))
         (message "Installed anything version of read functions."))
        (t
         ;; restore to original version
         (defalias 'completing-read (symbol-function 
'anything-old-completing-read))
         (defalias 'read-file-name (symbol-function 
'anything-old-read-file-name))
         (message "Uninstalled anything version of read functions."))))

;;----------------------------------------------------------------------
;; XEmacs compatibility
;;----------------------------------------------------------------------

;; Copied assoc-default from XEmacs version 21.5.12
(unless (fboundp 'assoc-default)
  (defun assoc-default (key alist &optional test default)
    "Find object KEY in a pseudo-alist ALIST.
ALIST is a list of conses or objects.  Each element (or the element's car,
if it is a cons) is compared with KEY by evaluating (TEST (car elt) KEY).
If that is non-nil, the element matches;
then `assoc-default' returns the element's cdr, if it is a cons,
or DEFAULT if the element is not a cons.

If no element matches, the value is nil.
If TEST is omitted or nil, `equal' is used."
    (let (found (tail alist) value)
      (while (and tail (not found))
        (let ((elt (car tail)))
          (when (funcall (or test 'equal) (if (consp elt) (car elt) elt) key)
            (setq found t value (if (consp elt) (cdr elt) default))))
        (setq tail (cdr tail)))
      value)))

;; Function not available in XEmacs, 
(unless (fboundp 'minibuffer-contents)
  (defun minibuffer-contents ()
    "Return the user input in a minbuffer as a string.
The current buffer must be a minibuffer."
    (field-string (point-max)))

  (defun delete-minibuffer-contents  ()
    "Delete all user input in a minibuffer.
The current buffer must be a minibuffer."
    (delete-field (point-max))))

;;----------------------------------------------------------------------
;; Unit Tests
;;----------------------------------------------------------------------

(defun* anything-test-candidates (sources &optional (input "") 
(compile-source-functions anything-compile-source-functions-default))
  "Test helper function for anything.
Given pseudo `anything-sources' and `anything-pattern', returns list like
  ((\"source name1\" (\"candidate1\" \"candidate2\"))
   (\"source name2\" (\"candidate3\" \"candidate4\")))
"
  (let ((anything-test-mode t)
        anything-enable-digit-shortcuts
        anything-candidate-cache
        (anything-sources (anything-normalize-sources sources))
        (anything-compile-source-functions compile-source-functions)
        anything-before-initialize-hook
        anything-after-initialize-hook
        anything-update-hook
        anything-test-candidate-list)
    (get-buffer-create anything-buffer)

    (anything-initialize)
    (setq anything-input input anything-pattern input)
    (anything-update)
    ;; test-mode spec: select 1st candidate!
    (with-current-buffer anything-buffer
      (forward-line 1)
      (anything-mark-current-line))
    (prog1
        anything-test-candidate-list
      (anything-cleanup))))

;;;; unit test
;; (install-elisp 
"http://www.emacswiki.org/cgi-bin/wiki/download/el-expectations.el";)
;; (install-elisp "http://www.emacswiki.org/cgi-bin/wiki/download/el-mock.el";)
(when (fboundp 'expectations)
  (expectations
    (desc "anything-current-buffer")
    (expect "__a_buffer"
      (with-current-buffer (get-buffer-create "__a_buffer")
        (anything-test-candidates '(((name . "FOO"))) "")
        (prog1
            (buffer-name anything-current-buffer)
          (kill-buffer (current-buffer)))))
    (desc "anything-buffer-file-name")
    (expect (regexp "/__a_file__")
      (with-current-buffer (find-file-noselect "__a_file__")
        (anything-test-candidates '(((name . "FOO"))) "")
        (prog1
            anything-buffer-file-name
          (kill-buffer (current-buffer)))))
    (desc "anything-compile-sources")
    (expect '(((name . "foo")))
      (anything-compile-sources '(((name . "foo"))) nil)
      )
    (expect '(((name . "foo") (type . test) (action . identity)))
      (let ((anything-type-attributes '((test (action . identity)))))
        (anything-compile-sources '(((name . "foo") (type . test)))
                                  '(anything-compile-source--type))))
    (desc "anything-sources accepts symbols")
    (expect '(((name . "foo")))
      (let* ((foo '((name . "foo"))))
        (anything-compile-sources '(foo) nil)))
    (desc "anything-get-sources action")
    (expect '(((name . "Actions") (candidates . actions)))
      (stub anything-action-window => t)
      (let (anything-compiled-sources
            (anything-sources '(((name . "Actions") (candidates . actions)))))
        (anything-get-sources)))
    (desc "get-buffer-create candidates-buffer")
    (expect '(((name . "many") (init . many-init)
               (candidates-in-buffer . anything-candidates-in-buffer)
               (candidates . anything-candidates-in-buffer)
               (volatile) (match identity)))
      (anything-compile-sources
       '(((name . "many") (init . many-init)
          (candidates-in-buffer . anything-candidates-in-buffer)))
       '(anything-compile-source--candidates-in-buffer)))
    (expect '(((name . "many") (init . many-init)
               (candidates-in-buffer)
               (candidates . anything-candidates-in-buffer)
               (volatile) (match identity)))
      (anything-compile-sources
       '(((name . "many") (init . many-init)
          (candidates-in-buffer)))
       '(anything-compile-source--candidates-in-buffer)))
    (expect '(((name . "many") (init . many-init)
               (candidates-in-buffer)
               (type . test)
               (action . identity)
               (candidates . anything-candidates-in-buffer)
               (volatile) (match identity)))
      (let ((anything-type-attributes '((test (action . identity)))))
        (anything-compile-sources
         '(((name . "many") (init . many-init)
            (candidates-in-buffer)
            (type . test)))
         '(anything-compile-source--type
           anything-compile-source--candidates-in-buffer))))

    (desc "anything-get-candidates")
    (expect '("foo" "bar")
      (anything-get-candidates '((name . "foo") (candidates "foo" "bar"))))
    (expect '("FOO" "BAR")
      (anything-get-candidates '((name . "foo") (candidates "foo" "bar")
                                 (candidate-transformer
                                  . (lambda (cands) (mapcar 'upcase cands))))))
    (expect '("foo" "bar")
      (anything-get-candidates '((name . "foo")
                                 (candidates . (lambda () '("foo" "bar"))))))
    (desc "anything-compute-matches")
    (expect '("foo" "bar")
      (let ((anything-pattern ""))
        (anything-compute-matches '((name . "FOO") (candidates "foo" "bar") 
(volatile)))))
    (expect '("foo")
      (let ((anything-pattern "oo"))
        (anything-compute-matches '((name . "FOO") (candidates "foo" "bar") 
(volatile)))))
    (expect '("bar")
      (let ((anything-pattern "^b"))
        (anything-compute-matches '((name . "FOO") (candidates "foo" "bar") 
(volatile)))))
    (expect '("a" "b")
      (let ((anything-pattern "")
            (anything-candidate-number-limit 2))
        (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c") 
(volatile)))))
    (expect '("a" "b")
      (let ((anything-pattern ".")
            (anything-candidate-number-limit 2))
        (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c") 
(volatile)))))
    (expect '("a" "b" "c")
      (let ((anything-pattern "")
            anything-candidate-number-limit)
        (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c") 
(volatile)))))
    (expect '("a" "b" "c")
      (let ((anything-pattern "[abc]")
            anything-candidate-number-limit)
        (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c") 
(volatile)))))
    ;; using anything-test-candidate-list
    (desc "anything-test-candidates")
    (expect '(("FOO" ("foo" "bar")))
      (anything-test-candidates '(((name . "FOO") (candidates "foo" "bar")))))
    (expect '(("FOO" ("bar")))
      (anything-test-candidates '(((name . "FOO") (candidates "foo" "bar"))) 
"ar"))
    (expect '(("T1" ("hoge" "aiue"))
              ("T2" ("test" "boke")))
      (anything-test-candidates '(((name . "T1") (candidates "hoge" "aiue"))
                                  ((name . "T2") (candidates "test" "boke")))))
    (expect '(("T1" ("hoge"))
              ("T2" ("boke")))
      (anything-test-candidates '(((name . "T1") (candidates "hoge" "aiue"))
                                  ((name . "T2") (candidates "test" "boke"))) 
"o"))
    (desc "requires-pattern attribute")
    (expect nil
      (anything-test-candidates '(((name . "FOO") (candidates "foo" "bar")
                                   (requires-pattern . 1)))))
    (expect '(("FOO" ("bar")))
      (anything-test-candidates '(((name . "FOO") (candidates "foo" "bar")
                                   (requires-pattern . 1))) "b"))
    (desc "delayed attribute(for test)")
    (expect '(("T2" ("boke"))
              ("T1" ("hoge")))
      (anything-test-candidates
       '(((name . "T1") (candidates "hoge" "aiue") (delayed))
         ((name . "T2") (candidates "test" "boke")))
       "o"))
    (desc "match attribute(prefix search)")
    (expect '(("FOO" ("bar")))
      (anything-test-candidates
       '(((name . "FOO") (candidates "foo" "bar")
          (match (lambda (c) (string-match (concat "^" anything-pattern) c)))))
       "ba"))
    (expect nil
      (anything-test-candidates
       '(((name . "FOO") (candidates "foo" "bar")
          (match (lambda (c) (string-match (concat "^" anything-pattern) c)))))
       "ar"))
    (desc "init attribute")
    (expect '(("FOO" ("bar")))
      (let (v)
        (anything-test-candidates
         '(((name . "FOO") (init . (lambda () (setq v '("foo" "bar"))))
            (candidates . v)))
         "ar")))
    (desc "candidate-transformer attribute")
    (expect '(("FOO" ("BAR")))
      (anything-test-candidates '(((name . "FOO") (candidates "foo" "bar")
                                   (candidate-transformer
                                    . (lambda (cands) (mapcar 'upcase cands)))))
                                "ar"))
    (desc "filtered-candidate-transformer attribute")
    ;; needs more tests
    (expect '(("FOO" ("BAR")))
      (anything-test-candidates '(((name . "FOO") (candidates "foo" "bar")
                                   (filtered-candidate-transformer
                                    . (lambda (cands src) (mapcar 'upcase 
cands)))))
                                "ar"))
    (desc "anything-candidates-in-buffer-1")
    (expect nil
      (anything-candidates-in-buffer-1 nil))
    (expect '("foo+" "bar+" "baz+")
      (with-temp-buffer
        (insert "foo+\nbar+\nbaz+\n")
        (let ((anything-candidate-number-limit 5))
          (anything-candidates-in-buffer-1 (current-buffer) ""))))
    (expect '("foo+" "bar+")
      (with-temp-buffer
        (insert "foo+\nbar+\nbaz+\n")
        (let ((anything-candidate-number-limit 2))
          (anything-candidates-in-buffer-1 (current-buffer) ""))))
    (expect '("foo+")
      (with-temp-buffer
        (insert "foo+\nbar+\nbaz+\n")
        (anything-candidates-in-buffer-1 (current-buffer) "oo\\+")))
    (expect '("foo+")
      (with-temp-buffer
        (insert "foo+\nbar+\nbaz+\n")
        (anything-candidates-in-buffer-1 
         (current-buffer) "oo+"
         #'buffer-substring-no-properties '(search-forward))))
    (expect '(("foo+" "FOO+"))
      (with-temp-buffer
        (insert "foo+\nbar+\nbaz+\n")
        (anything-candidates-in-buffer-1
         (current-buffer) "oo\\+"
         (lambda (s e)
           (let ((l (buffer-substring-no-properties s e)))
             (list l (upcase l)))))))
    (desc "anything-candidates-in-buffer")
    (expect '(("TEST" ("foo+" "bar+" "baz+")))
      (anything-test-candidates
       '(((name . "TEST")
          (init
           . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
                          (insert "foo+\nbar+\nbaz+\n"))))
          (candidates . anything-candidates-in-buffer)
          (match identity)
          (volatile)))))
    (expect '(("TEST" ("foo+" "bar+" "baz+")))
      (let (anything-candidate-number-limit)
        (anything-test-candidates
         '(((name . "TEST")
            (init
             . (lambda () (with-current-buffer (anything-candidate-buffer 
'global)
                            (insert "foo+\nbar+\nbaz+\n"))))
            (candidates . anything-candidates-in-buffer)
            (match identity)
            (volatile))))))
    (expect '(("TEST" ("foo+")))
      (anything-test-candidates
       '(((name . "TEST")
          (init
           . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
                          (insert "foo+\nbar+\nbaz+\n"))))
          (candidates . anything-candidates-in-buffer)
          (match identity)
          (volatile)))
       "oo\\+"))
    (desc "search attribute")
    (expect '(("TEST" ("foo+")))
      (anything-test-candidates
       '(((name . "TEST")
          (init
           . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
                          (insert "foo+\nbar+\nbaz+\nooo\n"))))
          (search search-forward)
          (candidates . anything-candidates-in-buffer)
          (match identity)
          (volatile)))
       "oo+"))
    (expect '(("TEST" ("foo+" "ooo")))
      (anything-test-candidates
       '(((name . "TEST")
          (init
           . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
                          (insert "foo+\nbar+\nbaz+\nooo\n"))))
          (search search-forward re-search-forward)
          (candidates . anything-candidates-in-buffer)
          (match identity)
          (volatile)))
       "oo+"))
    (expect '(("TEST" ("foo+" "ooo")))
      (anything-test-candidates
       '(((name . "TEST")
          (init
           . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
                          (insert "foo+\nbar+\nbaz+\nooo\n"))))
          (search re-search-forward search-forward)
          (candidates . anything-candidates-in-buffer)
          (match identity)
          (volatile)))
       "oo+"))
    (expect '(("TEST" ("ooo" "foo+")))
      (anything-test-candidates
       '(((name . "TEST")
          (init
           . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
                          (insert "bar+\nbaz+\nooo\nfoo+\n"))))
          (search re-search-forward search-forward)
          (candidates . anything-candidates-in-buffer)
          (match identity)
          (volatile)))
       "oo+"))
    ;; faster exact match
    (expect '(("TEST" ("bar+")))
      (anything-test-candidates
       '(((name . "TEST")
          (init
           . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
                          (insert "bar+\nbaz+\nooo\nfoo+\n"))))
          (search (lambda (pattern &rest _)
                    (and (search-forward (concat "\n" pattern "\n") nil t)
                         (forward-line -1))))
          (candidates . anything-candidates-in-buffer)
          (match identity)
          (volatile)))
       "bar+"))
    ;; faster prefix match
    (expect '(("TEST" ("bar+")))
      (anything-test-candidates
       '(((name . "TEST")
          (init
           . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
                          (insert "bar+\nbaz+\nooo\nfoo+\n"))))
          (search (lambda (pattern &rest _)
                    (search-forward (concat "\n" pattern) nil t)))
          (candidates . anything-candidates-in-buffer)
          (match identity)
          (volatile)))
       "ba"))
    (desc "anything-current-buffer-is-modified")
    (expect nil
      (with-temp-buffer
        (insert "1")
        (setq anything-current-buffer (current-buffer))
        (anything-cleanup)
        (anything-current-buffer-is-modified)))
    (expect t
      (with-temp-buffer
        (insert "1")
        (setq anything-current-buffer (current-buffer))
        (anything-cleanup)
        (insert "2")
        (anything-current-buffer-is-modified)))
    (expect '(("FOO" ("modified")))
      (let ((sources '(((name . "FOO")
                        (candidates
                         . (lambda ()
                             (if (anything-current-buffer-is-modified)
                                 '("modified")
                               '("unmodified"))))))))
        (with-temp-buffer
          (insert "1")
          (anything-test-candidates sources))))
    (expect '(("FOO" ("unmodified")))
      (let ((sources '(((name . "FOO")
                        (candidates
                         . (lambda ()
                             (if (anything-current-buffer-is-modified)
                                 '("modified")
                               '("unmodified"))))))))
        (with-temp-buffer
          (insert "1")
          (anything-test-candidates sources)
          (anything-test-candidates sources))))
    (desc "anything-source-name")
    (expect "FOO"
      (let (v)
        (anything-test-candidates '(((name . "FOO")
                                     (init
                                      . (lambda () (setq v 
anything-source-name)))
                                     (candidates "ok"))))
        v))
    (expect "FOO"
      (let (v)
        (anything-test-candidates '(((name . "FOO")
                                     (candidates
                                      . (lambda ()
                                          (setq v anything-source-name)
                                          '("ok"))))))
        v))
    (expect "FOO"
      (let (v)
        (anything-test-candidates '(((name . "FOO")
                                     (candidates "ok")
                                     (candidate-transformer
                                      . (lambda (c)
                                          (setq v anything-source-name)
                                          c)))))
        v))
    (expect "FOO"
      (let (v)
        (anything-test-candidates '(((name . "FOO")
                                     (candidates "ok")
                                     (filtered-candidate-transformer
                                      . (lambda (c s)
                                          (setq v anything-source-name)
                                          c)))))
        v))
    (expect "FOO"
      (let (v)
        (anything-test-candidates '(((name . "FOO")
                                     (candidates "ok")
                                     (display-to-real
                                      . (lambda (c)
                                          (setq v anything-source-name)
                                          c))
                                     (action . identity))))
        (anything-execute-selection-action)
        v))
    (desc "anything-candidate-buffer create")
    (expect " *anything candidates:FOO*"
      (let* (anything-candidate-buffer-alist
             (anything-source-name "FOO")
             (buf (anything-candidate-buffer 'global)))
        (prog1 (buffer-name buf)
          (kill-buffer buf))))
    (expect " *anything candidates:FOO*aTestBuffer"
      (let* (anything-candidate-buffer-alist
             (anything-source-name "FOO")
             (anything-current-buffer (get-buffer-create "aTestBuffer"))
             (buf (anything-candidate-buffer 'local)))
        (prog1 (buffer-name buf)
          (kill-buffer anything-current-buffer)
          (kill-buffer buf))))
    (expect 0
      (let (anything-candidate-buffer-alist
            (anything-source-name "FOO") buf)
        (with-current-buffer  (anything-candidate-buffer 'global)
          (insert "1"))
        (setq buf  (anything-candidate-buffer 'global))
        (prog1 (buffer-size buf)
          (kill-buffer buf))))
    (desc "anything-candidate-buffer get-buffer")
    (expect " *anything candidates:FOO*"
      (let* (anything-candidate-buffer-alist
             (anything-source-name "FOO")
             (buf (anything-candidate-buffer 'global)))
        (prog1 (buffer-name (anything-candidate-buffer))
          (kill-buffer buf))))
    (expect " *anything candidates:FOO*aTestBuffer"
      (let* (anything-candidate-buffer-alist
             (anything-source-name "FOO")
             (anything-current-buffer (get-buffer-create "aTestBuffer"))
             (buf (anything-candidate-buffer 'local)))
        (prog1 (buffer-name (anything-candidate-buffer))
          (kill-buffer anything-current-buffer)
          (kill-buffer buf))))
    (expect nil
      (let* (anything-candidate-buffer-alist
             (anything-source-name "NOP__"))
        (anything-candidate-buffer)))
    (desc "anything-candidate-buffer register-buffer")
    (expect " *anything test candidates*"
      (let (anything-candidate-buffer-alist
            (buf (get-buffer-create " *anything test candidates*")))
        (with-current-buffer buf
          (insert "1\n2\n")
          (prog1 (buffer-name (anything-candidate-buffer buf))
            (kill-buffer (current-buffer))))))
    (expect " *anything test candidates*"
      (let (anything-candidate-buffer-alist
            (buf (get-buffer-create " *anything test candidates*")))
        (with-current-buffer buf
          (insert "1\n2\n")
          (anything-candidate-buffer buf)
          (prog1 (buffer-name (anything-candidate-buffer))
            (kill-buffer (current-buffer))))))
    (expect "1\n2\n"
      (let (anything-candidate-buffer-alist
            (buf (get-buffer-create " *anything test candidates*")))
        (with-current-buffer buf
          (insert "1\n2\n")
          (anything-candidate-buffer buf)
          (prog1 (buffer-string)
            (kill-buffer (current-buffer))))))
    (desc "action attribute")
    (expect "foo"
      (anything-test-candidates
       '(((name . "TEST")
          (candidates "foo")
          (action ("identity" . identity)))))
      (anything-execute-selection-action))
    (expect "foo"
      (anything-test-candidates
       '(((name . "TEST")
          (candidates "foo")
          (action ("identity" . (lambda (c) (identity c)))))))
      (anything-execute-selection-action))
    (desc "anything-execute-selection-action")
    (expect "FOO"
      (anything-execute-selection-action
       "foo" '(("upcase" . upcase))  nil #'identity))
    (expect "FOO"
      (anything-execute-selection-action
       "foo" '(("upcase" . (lambda (c) (upcase c)))) nil #'identity))
    (desc "display-to-real attribute")
    (expect "FOO"
      (anything-execute-selection-action
       "foo"
       '(("identity" . identity))
       nil
       #'upcase
       ))
    (expect "FOO"
      (anything-test-candidates
       '(((name . "TEST")
          (candidates "foo")
          (display-to-real . upcase)
          (action ("identity" . identity)))))
      (anything-execute-selection-action))
    (desc "cleanup test")
    (expect 'cleaned
      (let (v)
        (anything-test-candidates
         '(((name . "TEST")
            (cleanup . (lambda () (setq v 'cleaned))))))
        v))
    (desc "anything-get-current-source")
    ;; in 
init/candidates/action/candidate-transformer/filtered-candidate-transformer
    ;; display-to-real/cleanup function
    (expect "FOO"
      (assoc-default
       'name
       (anything-funcall-with-source '((name . "FOO")) 
'anything-get-current-source)))
    ;; init
    (expect "FOO"
      (let (v)
        (anything-test-candidates
         '(((name . "FOO")
            (init . (lambda () (setq v (anything-get-current-source)))))))
        (assoc-default 'name v)))
    ;; candidates
    (expect "FOO"
      (let (v)
        (anything-test-candidates
         '(((name . "FOO")
            (candidates . (lambda () (setq v (anything-get-current-source)) 
'("a"))))))
        (assoc-default 'name v)))
    ;; action
    (expect "FOO"
      (let (v)
        (anything-test-candidates
         '(((name . "FOO")
            (candidates "a")
            (action
             . (lambda (c) (setq v (anything-get-current-source)) c)))))
        (anything-execute-selection-action)
        (assoc-default 'name v)))
    ;; candidate-transformer
    (expect "FOO"
      (let (v)
        (anything-test-candidates
         '(((name . "FOO")
            (candidates "a")
            (candidate-transformer
             . (lambda (c) (setq v (anything-get-current-source)) c)))))
        (assoc-default 'name v)))
    ;; filtered-candidate-transformer
    (expect "FOO"
      (let (v)
        (anything-test-candidates
         '(((name . "FOO")
            (candidates "a")
            (filtered-candidate-transformer
             . (lambda (c s) (setq v (anything-get-current-source)) c)))))
        (assoc-default 'name v)))
    ;; action-transformer
    (expect "FOO"
      (let (v)
        (anything-test-candidates
         '(((name . "FOO")
            (candidates "a")
            (action-transformer
             . (lambda (a c) (setq v (anything-get-current-source)) a))
            (action . identity))))
        (anything-execute-selection-action)
        (assoc-default 'name v)))
    ;; display-to-real
    (expect "FOO"
      (let (v)
        (anything-test-candidates
         '(((name . "FOO")
            (init . (lambda () (with-current-buffer (anything-candidate-buffer 
'global)
                                 (insert "a\n"))))
            (candidates-in-buffer)
            (display-to-real
             . (lambda (c) (setq v (anything-get-current-source)) c))
            (action . identity))))
        (anything-execute-selection-action)
        (assoc-default 'name v)))
    ;; cleanup
    (expect "FOO"
      (let (v)
        (anything-test-candidates
         '(((name . "FOO")
            (candidates "a")
            (cleanup
             . (lambda () (setq v (anything-get-current-source)))))))
        (assoc-default 'name v)))
    ;; candidates are displayed
    (expect "TEST"
      (anything-test-candidates
       '(((name . "TEST")
          (candidates "foo")
          (action ("identity" . identity)))))
      (assoc-default 'name (anything-get-current-source)))
    (desc "anything-attr")
    (expect "FOO"
      (anything-funcall-with-source
       '((name . "FOO"))
       (lambda ()
         (anything-attr 'name))))
    (expect 'fuga
      (let (v)
        (anything-test-candidates
         '(((name . "FOO")
            (hoge . fuga)
            (init . (lambda () (setq v (anything-attr 'hoge))))
            (candidates "a"))))
        v))
    (expect t
      (let (v)
        (anything-test-candidates
         '(((name . "FOO")
            (hoge)
            (init . (lambda () (setq v (anything-attr 'hoge))))
            (candidates "a"))))
        v))
    (desc "anything-preselect")
    ;; entire candidate
    (expect "foo"
      (with-current-buffer (anything-create-anything-buffer t)
        (let ((anything-pattern "")
              (anything-test-mode t))
          (anything-process-source '((name . "test")
                                     (candidates "hoge" "foo" "bar")))
          (anything-preselect "foo")
          (anything-get-selection))))
    ;; regexp
    (expect "foo"
      (with-current-buffer (anything-create-anything-buffer t)
        (let ((anything-pattern "")
              (anything-test-mode t))
          (anything-process-source '((name . "test")
                                     (candidates "hoge" "foo" "bar")))
          (anything-preselect "fo+")
          (anything-get-selection))))
    ;; no match -> first entry
    (expect "hoge"
      (with-current-buffer (anything-create-anything-buffer t)
        (let ((anything-pattern "")
              (anything-test-mode t))
          (anything-process-source '((name . "test")
                                     (candidates "hoge" "foo" "bar")))
          (anything-preselect "not found")
          (anything-get-selection))))
    (desc "anything-check-new-input")
    (expect "newpattern"
      (stub anything-update)
      (stub anything-action-window)
      (let ((anything-pattern "pattern"))
        (anything-check-new-input "newpattern")
        anything-pattern))
    ;; anything-input == nil when action window is available
    (expect nil
      (stub anything-update)
      (stub anything-action-window => t)
      (let ((anything-pattern "pattern")
            anything-input)
        (anything-check-new-input "newpattern")
        anything-input))
    ;; anything-input == anything-pattern unless action window is available
    (expect "newpattern"
      (stub anything-update)
      (stub anything-action-window => nil)
      (let ((anything-pattern "pattern")
            anything-input)
        (anything-check-new-input "newpattern")
        anything-input))
    (expect (mock (anything-update))
      (stub anything-action-window)
      (let (anything-pattern)
        (anything-check-new-input "foo")))
    (desc "anything-update")
    (expect (mock (anything-process-source '((name . "1"))))
      (stub anything-get-sources => '(((name . "1"))))
      (stub run-hooks)
      (stub anything-maybe-fit-frame)
      (stub run-with-idle-timer)
      (anything-update))
    ;; (find-function 'anything-update)
    ;; TODO el-mock.el should express 2nd call of function.
    ;; TODO el-mock.el should express no call of function.
    ;;     (expect (mock (anything-process-source '((name . "2"))))
    ;;       (stub anything-get-sources => '(((name . "1")) ((name . "2"))))
    ;;       (stub run-hooks)
    ;;       (stub anything-maybe-fit-frame)
    ;;       (stub run-with-idle-timer)
    ;;       (anything-update))
    (expect (mock (run-with-idle-timer * nil 'anything-process-delayed-sources
                                       '(((name . "2") (delayed)))))
      (stub anything-get-sources => '(((name . "1"))
                                      ((name . "2") (delayed))))
      (stub run-hooks)
      (stub anything-maybe-fit-frame)
      (let ((anything-pattern "") anything-test-mode)
        (anything-update)))
    ;; TODO requires-pattern test
    (desc "delay")
    (expect (mock (sit-for 0.25))
      (stub with-current-buffer)
      (let ((anything-idle-delay 1.0)
            (anything-input-idle-delay 0.75))
        (anything-process-delayed-sources t)))
    (expect (mock (sit-for 0.0))
      (stub with-current-buffer)
      (let ((anything-idle-delay 0.2)
            (anything-input-idle-delay 0.5))
        (anything-process-delayed-sources t)))    
    (expect (mock (sit-for 0.5))
      (stub with-current-buffer)
      (let ((anything-idle-delay 0.5)
            (anything-input-idle-delay nil))
        (anything-process-delayed-sources t)))
    (desc "anything-normalize-sources")
    (expect '(anything-c-source-test)
      (anything-normalize-sources 'anything-c-source-test))
    (expect '(anything-c-source-test)
      (anything-normalize-sources '(anything-c-source-test)))
    (expect '(anything-c-source-test)
      (let ((anything-sources '(anything-c-source-test)))
        (anything-normalize-sources nil)))
    (desc "anything-get-action")
    (expect '(("identity" . identity))
      (stub buffer-size => 1)
      (stub anything-get-current-source => '((name . "test")
                                             (action ("identity" . identity))))
      (anything-get-action))
    (expect '((("identity" . identity)) "action-transformer is called")
      (stub buffer-size => 1)
      (stub anything-get-current-source
            => '((name . "test")
                 (action ("identity" . identity))
                 (action-transformer
                  . (lambda (actions selection)
                      (list actions selection)))))
      (stub anything-get-selection => "action-transformer is called")
      (anything-get-action))
    (desc "anything-select-nth-action")
    (expect "selection"
      (stub anything-get-selection => "selection")
      (stub anything-exit-minibuffer)
      (let (anything-saved-selection)
        (anything-select-nth-action 1)
        anything-saved-selection))
    (expect 'cadr
      (stub anything-get-action => '(("0" . car) ("1" . cdr) ("2" . cadr)))
      (stub anything-exit-minibuffer)
      (stub anything-get-selection => "selection")
      (let (anything-saved-action)
        (anything-select-nth-action 2)
        anything-saved-action))
    (desc "anything-funcall-foreach")
    (expect (mock (upcase "foo"))
      (stub anything-get-sources => '(((init . (lambda () (upcase "foo"))))))
      (anything-funcall-foreach 'init))
    (expect (mock (downcase "bar"))
      (stub anything-get-sources => '(((init . (lambda () (upcase "foo"))))
                                      ((init . (lambda () (downcase "bar"))))))
      (anything-funcall-foreach 'init))
    ;; TODO when symbol is not specified
    ;; TODO anything-select-with-digit-shortcut test
    (desc "anything-get-cached-candidates")
    (expect '("cached" "version")
      (let ((anything-candidate-cache '(("test" "cached" "version"))))
        (anything-get-cached-candidates '((name . "test")
                                          (candidates "new")))))
    (expect '("new")
      (let ((anything-candidate-cache '(("other" "cached" "version"))))
        (anything-get-cached-candidates '((name . "test")
                                          (candidates "new")))))
    (expect '(("test" "new")
              ("other" "cached" "version"))
      (let ((anything-candidate-cache '(("other" "cached" "version"))))
        (anything-get-cached-candidates '((name . "test")
                                          (candidates "new")))
        anything-candidate-cache))
    (expect '(("other" "cached" "version"))
      (let ((anything-candidate-cache '(("other" "cached" "version"))))
        (anything-get-cached-candidates '((name . "test")
                                          (candidates "new")
                                          (volatile)))
        anything-candidate-cache))
    ;; TODO when candidates == process
    ;; TODO anything-output-filter
    (desc "candidate-number-limit attribute")
    (expect '("a" "b")
      (let ((anything-pattern "")
            (anything-candidate-number-limit 20))
        (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c")
                                    (candidate-number-limit . 2) (volatile)))))
    (expect '("a" "b")
      (let ((anything-pattern "[abc]")
            (anything-candidate-number-limit 20))
        (anything-compute-matches '((name . "FOO") (candidates "a" "b" "c")
                                    (candidate-number-limit . 2) (volatile)))))
    (expect '(("TEST" ("a" "b" "c")))
      (let ((anything-candidate-number-limit 2))
        (anything-test-candidates
         '(((name . "TEST")
            (init
             . (lambda () (with-current-buffer (anything-candidate-buffer 
'global)
                            (insert "a\nb\nc\nd\n"))))
            (candidates . anything-candidates-in-buffer)
            (match identity)
            (candidate-number-limit . 3)
            (volatile))))))
    (expect '(("TEST" ("a" "b" "c")))
      (let ((anything-candidate-number-limit 2))
        (anything-test-candidates
         '(((name . "TEST")
            (init
             . (lambda () (with-current-buffer (anything-candidate-buffer 
'global)
                            (insert "a\nb\nc\nd\n"))))
            (candidates . anything-candidates-in-buffer)
            (match identity)
            (candidate-number-limit . 3)
            (volatile)))
         ".")))
    (desc "multiple init")
    (expect '(1 . 2)
      (let (a b)
        (anything-test-candidates
         '(((name . "test")
            (init (lambda () (setq a 1))
                  (lambda () (setq b 2))))))
        (cons a b)))
    (expect 1
      (let (a)
        (anything-test-candidates
         '(((name . "test")
            (init (lambda () (setq a 1))))))
        a))
    (desc "multiple cleanup")
    (expect '(1 . 2)
      (let (a b)
        (anything-test-candidates
         '(((name . "test")
            (cleanup (lambda () (setq a 1))
                     (lambda () (setq b 2))))))
        (cons a b)))
    (desc "anything-mklist")
    (expect '(1)
      (anything-mklist 1))
    (expect '(2)
      (anything-mklist '(2)))
    (desc "anything-before-initialize-hook")
    (expect 'called
      (let ((anything-before-initialize-hook '((lambda () (setq v 'called))))
            v)
        (anything-initialize)
        v))
    (desc "anything-after-initialize-hook")
    (expect '(b a)
      (let ((anything-before-initialize-hook
             '((lambda () (setq v '(a)))))
            (anything-after-initialize-hook
             '((lambda () (setq v (cons 'b v)))))
            v)
        (anything-initialize)
        v))
    (expect 0
      (let ((anything-after-initialize-hook
             '((lambda () (setq v (buffer-size (get-buffer anything-buffer))))))
            v)
        (anything-initialize)
        v))
    (desc "get-line attribute")
    (expect '(("TEST" ("FOO+")))
      (anything-test-candidates
       '(((name . "TEST")
          (init
           . (lambda () (with-current-buffer (anything-candidate-buffer 'global)
                          (insert "foo+\nbar+\nbaz+\n"))))
          (candidates-in-buffer)
          (get-line . (lambda (s e) (upcase (buffer-substring-no-properties s 
e))))))
       "oo\\+"))
    (desc "with-anything-restore-variables")
    (expect 9999
      (let ((a 9999)
            (anything-restored-variables '(a)))
        (with-anything-restore-variables
         (setq a 0))
        a))
    (desc "anything-cleanup-hook")
    (expect 'called
      (let ((anything-cleanup-hook
             '((lambda () (setq v 'called))))
            v)
        (anything-cleanup)
        v))
    ))


(provide 'anything)
;; How to save (DO NOT REMOVE!!)
;; (emacswiki-post "anything.el")
;;; anything.el ends here

reply via email to

[Prev in Thread] Current Thread [Next in Thread]