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

"google feeling lucky" to "feeling searchy"



Hello.

Currently the "google feeling lucky" functionality appears to lead to
search results instead of redirecting to the first result's URL, and the
limitation to Google seems to be unnecessary. A common practice among
web browsers is to use the default search engine in such a case, and I
think it would be nicer to do that in emacs-w3m as well.

Here is a patch that replaces the "feeling lucky" functionality with
"feeling searchy", though it wouldn't work for search engines that
require POST data -- so I'm not sure whether it should be merged this
way (or at all; maybe just the "feeling lucky" functionality should be
fixed).
*** w3m.el	2017-07-27 16:30:30.597520960 +0300
--- w3m-searchy.el	2017-07-27 17:35:12.731623823 +0300
***************
*** 2017,2025 ****
  		 function
  		 (repeat :tag "Args" :extra-offset 1 (sexp :format "%v"))))))
  
! (defcustom w3m-enable-google-feeling-lucky t
    "Non-nil enables you to enter any words as well as a url when prompted.
! In that case, emacs-w3m uses Google to search for the words."
    :group 'w3m
    :type 'boolean)
  
--- 2017,2026 ----
  		 function
  		 (repeat :tag "Args" :extra-offset 1 (sexp :format "%v"))))))
  
! (defcustom w3m-enable-feeling-searchy t
    "Non-nil enables you to enter any words as well as a url when prompted.
! In that case, emacs-w3m uses the default search engine to search
! for the words."
    :group 'w3m
    :type 'boolean)
  
***************
*** 4548,4558 ****
  		       (concat w3m-current-url "#" name)
  		     w3m-current-url))))))))
  
! (defun w3m-canonicalize-url (url &optional feeling-lucky)
    "Fix URL that does not look like a valid url.
! For URL having no scheme part, return a url that leaves it to chance if
! FEELING-LUCKY is non-nil (what is called \"I'm Feeling Lucky\" on Google).
! Also fix URL that fails to have put a separator following a domain name."
    (if (string-match "\\`\\(https?://[-.0-9a-z]+\\)\\([#?].*\\)" url)
        (concat (match-string 1 url) "/" (match-string 2 url))
      (let (replaced)
--- 4549,4560 ----
  		       (concat w3m-current-url "#" name)
  		     w3m-current-url))))))))
  
! (defun w3m-canonicalize-url (url &optional feeling-searchy)
    "Fix URL that does not look like a valid url.
! For a query that neither has a scheme part nor is an existing
! local file, return a search url for the default search engine.
! Also fix URL that fails to have put a separator following a
! domain name."
    (if (string-match "\\`\\(https?://[-.0-9a-z]+\\)\\([#?].*\\)" url)
        (concat (match-string 1 url) "/" (match-string 2 url))
      (let (replaced)
***************
*** 4564,4573 ****
  	(concat "file://" (expand-file-name url)))
         ((progn (w3m-string-match-url-components url) (match-beginning 1))
  	url)
!        (feeling-lucky
! 	(concat "\
! http://www.google.com/search?btnI=I%%27m+Feeling+Lucky&ie=UTF-8&oe=UTF-8&q="
! 		(w3m-url-encode-string url nil t)))
         (t
  	(concat "https://" url))))))
  
--- 4566,4574 ----
  	(concat "file://" (expand-file-name url)))
         ((progn (w3m-string-match-url-components url) (match-beginning 1))
  	url)
!        (feeling-searchy
!         (w3m-search-do-search (lambda (url &rest rest) url)
!                               w3m-search-default-engine url))
         (t
  	(concat "https://" url))))))
  
***************
*** 4602,4608 ****
  	      (delete def2 (delete def (delete add2 (delete to-add all))))))))
  
  (defun w3m-input-url (&optional prompt initial default quick-start
! 				feeling-lucky no-initial)
    "Read a url from the minibuffer, prompting with string PROMPT."
    (let ((url
  	 ;; Check whether the region is active.
--- 4603,4609 ----
  	      (delete def2 (delete def (delete add2 (delete to-add all))))))))
  
  (defun w3m-input-url (&optional prompt initial default quick-start
! 				feeling-searchy no-initial)
    "Read a url from the minibuffer, prompting with string PROMPT."
    (let ((url
  	 ;; Check whether the region is active.
***************
*** 4637,4644 ****
  	     default
  	     (not initial))
  	default
!       (unless w3m-enable-google-feeling-lucky
! 	(setq feeling-lucky nil))
        (setq url (let ((minibuffer-setup-hook
  		       (append minibuffer-setup-hook
  			       (list (lambda ()
--- 4638,4645 ----
  	     default
  	     (not initial))
  	default
!       (unless w3m-enable-feeling-searchy
! 	(setq feeling-searchy nil))
        (setq url (let ((minibuffer-setup-hook
  		       (append minibuffer-setup-hook
  			       (list (lambda ()
***************
*** 4674,4680 ****
  			 prompt)
  		     (if default
  			 (format "URL %s(default %s): "
! 				 (if feeling-lucky "or Keyword " "")
  				 (if (stringp default)
  				     (cond ((string= default "about:blank")
  					    "BLANK")
--- 4675,4681 ----
  			 prompt)
  		     (if default
  			 (format "URL %s(default %s): "
! 				 (if feeling-searchy "or Keyword " "")
  				 (if (stringp default)
  				     (cond ((string= default "about:blank")
  					    "BLANK")
***************
*** 4682,4688 ****
  					    "HOME")
  					   (t default))
  				   (prin1-to-string default)))
! 		       (if feeling-lucky "URL or Keyword: " "URL: ")))
  		   initial keymap nil 'w3m-input-url-history default)))
        (if (string-equal url "")
  	  (or default "")
--- 4683,4689 ----
  					    "HOME")
  					   (t default))
  				   (prin1-to-string default)))
! 		       (if feeling-searchy "URL or Keyword: " "URL: ")))
  		   initial keymap nil 'w3m-input-url-history default)))
        (if (string-equal url "")
  	  (or default "")
***************
*** 4691,4697 ****
  	      ;; remove duplication
  	      (setq w3m-input-url-history
  		    (cons url (delete url w3m-input-url-history)))
! 	      (w3m-canonicalize-url url feeling-lucky))
  	  ;; It may be `popup'.
  	  url)))))
  
--- 4692,4698 ----
  	      ;; remove duplication
  	      (setq w3m-input-url-history
  		    (cons url (delete url w3m-input-url-history)))
! 	      (w3m-canonicalize-url url feeling-searchy))
  	  ;; It may be `popup'.
  	  url)))))
  
***************
*** 7131,7137 ****
  	(w3m-view-image)))
       ((setq url (w3m-active-region-or-url-at-point 'never))
        (unless (eq 'quit (setq url (w3m-input-url nil url 'quit nil
! 						 'feeling-lucky 'no-initial)))
  	(w3m-view-this-url-1 url arg new-session)))
       (t (w3m-message "No URL at point")))))
  
--- 7132,7138 ----
  	(w3m-view-image)))
       ((setq url (w3m-active-region-or-url-at-point 'never))
        (unless (eq 'quit (setq url (w3m-input-url nil url 'quit nil
! 						 'feeling-searchy 'no-initial)))
  	(w3m-view-this-url-1 url arg new-session)))
       (t (w3m-message "No URL at point")))))
  
***************
*** 9562,9568 ****
  	     (error "%s"
  		    (substitute-command-keys "Cannot run two w3m processes simultaneously \
  \(Type `\\<w3m-mode-map>\\[w3m-process-stop]' to stop asynchronous process)"))
! 	   (w3m-input-url nil nil nil nil 'feeling-lucky 'no-initial))
  	 current-prefix-arg
  	 (w3m-static-if (fboundp 'universal-coding-system-argument)
  	     coding-system-for-read)))
--- 9563,9569 ----
  	     (error "%s"
  		    (substitute-command-keys "Cannot run two w3m processes simultaneously \
  \(Type `\\<w3m-mode-map>\\[w3m-process-stop]' to stop asynchronous process)"))
! 	   (w3m-input-url nil nil nil nil 'feeling-searchy 'no-initial))
  	 current-prefix-arg
  	 (w3m-static-if (fboundp 'universal-coding-system-argument)
  	     coding-system-for-read)))
***************
*** 9908,9914 ****
     (list (w3m-input-url nil nil
  			(or (w3m-active-region-or-url-at-point)
  			    w3m-new-session-url)
! 			nil 'feeling-lucky 'no-initial)
  	 current-prefix-arg
  	 (w3m-static-if (fboundp 'universal-coding-system-argument)
  	     coding-system-for-read)
--- 9909,9915 ----
     (list (w3m-input-url nil nil
  			(or (w3m-active-region-or-url-at-point)
  			    w3m-new-session-url)
! 			nil 'feeling-searchy 'no-initial)
  	 current-prefix-arg
  	 (w3m-static-if (fboundp 'universal-coding-system-argument)
  	     coding-system-for-read)
***************
*** 10181,10187 ****
  	    (setq new (if current-prefix-arg
  			  default
  			(w3m-input-url nil nil default w3m-quick-start
! 				       'feeling-lucky 'no-initial)))))
        ;; new-session
        (and w3m-make-new-session
  	   (w3m-alive-p)
--- 10182,10188 ----
  	    (setq new (if current-prefix-arg
  			  default
  			(w3m-input-url nil nil default w3m-quick-start
! 				       'feeling-searchy 'no-initial)))))
        ;; new-session
        (and w3m-make-new-session
  	   (w3m-alive-p)