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: Thu, 23 Aug 2007 09:54:35 -0700
User-agent: G2/1.0

Here's a Google Suggest source. Requires the latest anything.el.

(setq anything-sources
 '(((name . "Google Suggest")
    (candidates
     . (lambda ()
         (let ((suggestions (anything-google-suggest-fetch anything-
input)))
           (if (some (lambda (suggestion)
                       (equal (cdr suggestion) anything-input))
                     suggestions)
               suggestions
             ;; if there is no suggestion exactly matching the input
             ;; then prepend a Search on Google item to the list
             (append (list (cons (concat "Search for "
                                         "'" anything-input "'"
                                         " on Google")
                                 anything-input))
                     suggestions)))))
    (action . (("Google Search" .
                (lambda (candidate)
                  (browse-url (concat anything-google-suggest-search-
url
                                      (url-hexify-string
candidate)))))))
    (volatile)
    (requires-pattern . 3)
    (delayed))))



(defvar anything-google-suggest-url
  "http://www.google.com/complete/search?hl=en&js=true&qu=";
  "URL used for looking up suggestions.")

(defvar anything-google-suggest-search-url
  "http://www.google.com/search?ie=utf-8&oe=utf-8&q=";
  "URL used for searching.")


(defun anything-google-suggest-fetch (input)
  "Fetch suggestions for INPUT."
  (let* ((result (with-current-buffer
                     (url-retrieve-synchronously
                      (concat anything-google-suggest-url
                              (url-hexify-string input)))
                   (buffer-substring (point-min) (point-max))))
         (split (split-string result "new Array("))
         (suggestions (anything-google-suggest-get-items (second
split)))
         (numbers (anything-google-suggest-get-items (third split)))
         (longest (+ (apply 'max 0 (let (lengths)
                                     (dotimes (i (length suggestions))
                                       (push (+ (length (nth i
suggestions))
                                                (length (nth i
numbers)))
                                             lengths))
                                     lengths))
                     10))
         items)
    (dotimes (i (length suggestions))
      (let ((suggestion (nth i suggestions))
            (number (nth i numbers)))
        (push (cons (concat suggestion
                            (make-string (- longest
                                            (length suggestion)
                                            (length number))
                                         32)
                            number)
                    suggestion)
              items)))
    items))


(defun anything-google-suggest-get-items (str)
  "Extract items from STR returned by Google Suggest."
  (let ((start nil)
        items)
    (while (string-match "\"\\([^\"]+?\\)\"" str start)
      (push (match-string 1 str) items)
      (setq start (1+ (match-end 1))))
    items))



reply via email to

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