[PRL] Looking for 2 bits of emacs code

Paul Steckler steck at ccs.neu.edu
Wed Apr 23 15:00:45 EDT 2003


> 1.  Something like M-x browse-url-at-point, but which instead
>     interactivly asks for a search string and ships off a request to
>     google (call it M-x google).

Here's a quick-and-dirty implementation.  I just hacked 
`browse-url' to get the code below.  No warranties apply.
It works for me under Windows, though it grabs any 
URL near the point and copies it to the minibuffer with 
the prompt.

-- Paul


(defun google-string-interactive-arg (prompt)
  "Read a string from the minibuffer, prompting with PROMPT.
Default to the string at or before point.  If invoked with a mouse
button,
set point to the position clicked first.  Return a list for use in
`interactive' containing the URL and `browse-url-new-window-p' or its
negation if a prefix argument was given."
  (let ((event (elt (this-command-keys) 0)))
    (and (listp event) (mouse-set-point event)))
  (list (read-string prompt (browse-url-url-at-point))
	(not (eq (null browse-url-new-window-p)
		 (null current-prefix-arg)))))

(defun google (str &rest args)
  "Search for string in Google.
Prompts for the string, defaulting to the string at or before point.
Variable
`browse-url-browser-function' says which browser to use."
  (interactive (browse-url-interactive-arg "Google for: "))
  (if (functionp browse-url-browser-function)
      (apply browse-url-browser-function (concat
 
"http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q="
					  str
					  "&btnG=Google+Searchstr")
	     args)
    ;; The `function' can be an alist; look down it for first match
    ;; and apply the function (which might be a lambda).
    (catch 'done
      (mapcar
       (lambda (bf)
	 (when (string-match (car bf) url)
	   (apply (cdr bf) url args)
	   (throw 'done t)))
       browse-url-browser-function)
      (error "No browser in browse-url-browser-function matching URL %s"
	     url))))



More information about the PRL mailing list