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

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

Re: ;;; anything.el --- open anything


From: address@hidden
Subject: Re: ;;; anything.el --- open anything
Date: Fri, 03 Aug 2007 06:21:44 -0700
User-agent: G2/1.0

Here's a demontstration for the option (anything-filtered-candidate-
transformers) added today.

I didn't want to stuff into anything.el, so I post it here. Maybe it
should go to anything-config.el.

It's a candidate sorter which places those candidates first in the
list of matches which you selected before. The more frequently you use
an item the closer it is to top of the list, so you can select
frequent selections more easily. In addition if the current pattern
matches one of the previous ones used to select an item then those
items are put at the very beginning of the list, so you can have a
sort of automatic bookmarks if you use the same pattern to select the
same item again and again.

So it's an adaptive candidate sorter which learns which candidates you
select frequently and makes  them more accessible for you.

It's only a quick implementation. It surely could be improved. It's
not extensively tested, but it seems to work well.

To activate add the sorter function to the desired types:

(setq anything-filtered-candidate-transformers
   '((file . anything-adaptive-sort)))

and load this code:


(defvar anything-adaptive-done nil)

(defvar anything-adaptive-history nil)

(defvar anything-adaptive-history-file "~/.anything_adaptive_history")

(defvar anything-adaptive-history-length 50
  "Max number of candidates stored for a source.")


(defadvice anything-initialize (before anything-adaptive-initialize
activate)
  (setq anything-adaptive-done nil))

(defadvice anything-exit-minibuffer (before anything-adaptive-exit-
minibuffer activate)
  (anything-adaptive-store-selection))

(defadvice anything-select-action (before anything-adaptive-select-
action activate)
  (anything-adaptive-store-selection))


(defun anything-adaptive-store-selection ()
  (unless anything-adaptive-done
    (setq anything-adaptive-done t)
    (let* ((source-name (assoc-default 'name (anything-get-current-
source)))
           (source-info (or (assoc source-name anything-adaptive-
history)
                            (progn
                              (push (list source-name) anything-
adaptive-history)
                              (car anything-adaptive-history))))
           (selection (anything-get-selection))
           (selection-info (or (let ((found (assoc selection (cdr
source-info))))
                                 (when found
                                   (setcdr source-info
                                           (cons found
                                                 (delete found (cdr
source-info))))
                                   found))
                               (let ((new (list selection)))
                                 (setcdr source-info (cons new (cdr
source-info)))
                                 new)))
           (pattern-info (or (let ((found (assoc anything-pattern (cdr
selection-info))))
                               (when found
                                 (setcdr selection-info
                                         (cons found
                                               (delete found (cdr
selection-info))))
                                 found))
                             (let ((new (cons anything-pattern 0)))
                               (setcdr selection-info
                                       (cons new (cdr selection-
info)))
                               new))))
      (setcdr pattern-info (1+ (cdr pattern-info)))

      (if (> (length (cdr selection-info)) anything-adaptive-history-
length)
          (setcdr selection-info
                  (subseq (cdr selection-info) 0 anything-adaptive-
history-length))))))


(load-file anything-adaptive-history-file)
(add-hook 'kill-emacs-hook 'anything-adaptive-save-history)

(defun anything-adaptive-save-history ()
  (interactive)
  (with-temp-buffer
    (insert
     ";; -*- mode: emacs-lisp -*-\n"
     ";; History entries used for anything adaptive display.\n")
    (prin1 `(setq anything-adaptive-history ',anything-adaptive-
history)
           (current-buffer))
    (insert ?\n)
    (write-region (point-min) (point-max) anything-adaptive-history-
file nil
                  (unless (interactive-p) 'quiet))))


(defun anything-adaptive-sort (source candidates)
  (let* ((source-name (assoc-default 'name source))
         (source-info (assoc source-name anything-adaptive-history)))
    (if source-info
        (let ((usage
               (mapcar (lambda (candidate-info)
                         (let ((count 0))
                           (dolist (pattern-info (cdr candidate-info))
                             (if (not (equal (car pattern-info)
                                             anything-pattern))
                                 (incf count (cdr pattern-info))

                               (setq count (+ 10000 (cdr pattern-
info)))
                               (return)))
                           (cons (car candidate-info) count)))
                       (cdr source-info)))
              sorted)

          (setq usage (sort usage (lambda (first second)
                                    (> (cdr first) (cdr second)))))

          (dolist (info usage)
            (when (member* (car info) candidates
                            :test 'anything-adaptive-compare)
              (push (car info) sorted)
              (setq candidates (remove* (car info) candidates
                                        :test 'anything-adaptive-
compare))))

          (append (reverse sorted) candidates nil)))))


(defun anything-adaptive-compare (x y)
  (equal (if (listp x)
             (cdr x)
           x)
         (if (listp y)
             (cdr y)
           y)))





reply via email to

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