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

Re: w3m-search.el



First, I apologize to my *late* reply.

w3m-uri-replace() and w3m-search-quick-search-handler() provide a
similar function; both of them rewrite a URI matching specified
patterns to an other URI.  I worry that this duplication may confuse a
user who intend to adjust the system rewriting URI for his or her
requirement.  Therefore, I propose the attached change that improves
w3m-uri-replace() to support all modifications used in
w3m-search-quick-search-handler().

>> On Thu, 27 Feb 2003 11:17:39 +0900
>> yamaoka@jpl.org (Katsumi Yamaoka) said as follows:

> TSUCHIYA-san, could you see [emacs-w3m:04471]?

Yes, I confirmed it.

> I think it's a real generalization!

I think so, too.  However, it does not ease my worry.

-- 
TSUCHIYA Masatoshi

diff -u -r1.793 w3m.el
--- w3m.el	12 May 2003 09:30:19 -0000	1.793
+++ w3m.el	24 May 2003 04:58:59 -0000
@@ -104,7 +104,7 @@
     "Add link of current page to bookmark." t)
   (autoload 'w3m-search "w3m-search"
     "Search QUERY using SEARCH-ENGINE." t)
-  (autoload 'w3m-search-quick-search-handler "w3m-search")
+  (autoload 'w3m-search-uri-replace "w3m-search")
   (autoload 'w3m-weather "w3m-weather"
     "Display weather report." t)
   (autoload 'w3m-about-weather "w3m-weather")
@@ -1180,34 +1180,44 @@
   :group 'w3m
   :type 'string)
 
-(defcustom w3m-uri-replace-alist nil
-  "*Alist of a regexp matching uri and replacement.
+(defcustom w3m-uri-replace-alist
+  '(("\\`gg:" w3m-search-uri-replace "google")
+    ("\\`ggg:" w3m-search-uri-replace "google groups")
+    ("\\`ya:" w3m-search-uri-replace "yahoo")
+    ("\\`al:" w3m-search-uri-replace "altavista")
+    ("\\`bts:" w3m-search-uri-replace "debian-bts")
+    ("\\`dpkg:" w3m-search-uri-replace "debian-pkg")
+    ("\\`archie:" w3m-search-uri-replace "iij-archie")
+    ("\\`urn:ietf:rfc:\\([0-9]+\\)" w3m-pattern-uri-replace
+     "http://www.ietf.org/rfc/rfc\\1.txt";))
+  "*Alist of a regexp matching a uri and its replacement.
+
+Each element of this alist is (REGEXP FUNCTION OPTIONS...).  FUNCTION
+should take one or more arguments, a uri and OPTIONS.  When this
+FUNCTION is called, sub-strings found in matching REGEXP can be
+refered.  Here are some predefined functions meant for use in this
+way:
+
+`w3m-pattern-uri-replace'
+    Replace a uri with PATTERN.  In PATTERN, `\' is treated as special
+    in the same manner of `replace-match'.
 
-Each element of the alist is (REGEXP . REPLACEMENT) or (REGEXP . FUNCTION).
-
-REGEXP is a regular expression for uri.
-
-Matched string is replaced with REPLACEMENT.
-You can refer matched substring of REGEXP. See `replace-match' for more detail.
-
-If FUNCTION is specified, it will be called with the uri, and replaced with
-the return value.
-
-
-Here is an example of how to set this option:
-
-\(setq w3m-uri-replace-alist
-      '((\"^urn:ietf:rfc:\\\\([0-9]+\\\\)\" . \"http://www.ietf.org/rfc/rfc\\\\1.txt\";)
-	(\"^urn:isbn:\" .
-	 (lambda (uri)
-	   (concat \"http://www.amazon.co.jp/exec/obidos/ASIN/\";
-		   (apply 'concat (split-string (substring uri 9) \"-\"))
-		   \"/\")))))
+`w3m-search-uri-replace'
+    Generate a query from a uri for specified engine.
 "
   :group 'w3m
-  :type '(repeat (cons (string :tag "Regexp")
-		       (choice (string :tag "Replacement")
-			       (function :tag "Function")))))
+  :type '(repeat
+	  (cons
+	   (string :tag "Regexp" :value "")
+	   (choice (list :tag "Replacement Using Pattern"
+			 (function-item :format "" w3m-pattern-uri-replace)
+			 (string :tag "Pattern" :value ""))
+		   (list :tag "Quick Search"
+			 (function-item :format "" w3m-search-uri-replace)
+			 (string :tag "Engine"))
+		   (list :tag "User Defined Function"
+			 (function)
+			 (repeat :tag "Options" sexp))))))
 
 (defconst w3m-entity-alist		; html character entities and values
   (append
@@ -5770,17 +5780,22 @@
   (w3m-set-window-hscroll (selected-window)
 			  (max (- arg (window-width) -2) 0)))
 
+(defun w3m-pattern-uri-replace (uri format)
+  "Replace URI matched by last search with FORMAT."
+  (replace-match format nil nil uri))
+
 (defun w3m-uri-replace (uri)
-  (let ((alist w3m-uri-replace-alist))
-    (while (and alist
-		(not (string-match (car (car alist)) uri)))
-      (setq alist (cdr alist)))
-    (if alist
-	(cond ((functionp (cdr (car alist)))
-	       (funcall (cdr (car alist)) uri))
-	      ((stringp (cdr (car alist)))
-	       (replace-match (cdr (car alist)) nil nil uri)))
-      uri)))
+  (catch 'found-replacement
+    (dolist (elem w3m-uri-replace-alist uri)
+      (when (string-match (car elem) uri)
+	(if (setq uri
+		  (cond
+		   ((functionp (cdr elem))
+		    (apply (cadr elem) uri (cddr elem)))
+		   ((stringp (cdr elem))
+		    (w3m-pattern-uri-replace uri (cdr elem)))))
+	    (throw 'found-replacement uri)
+	  (error "Invalid replacement: %s" elem))))))
 
 (defun w3m-goto-mailto-url (url &optional post-data)
   (let ((before (nreverse (buffer-list)))
@@ -5957,8 +5972,7 @@
 field for this request.
 You can also use \"quicksearch\" url schemes such as \"gg:emacs\" which
 would search for the term \"emacs\" with the Google search engine.  See
-the `w3m-search' function and the variable
-`w3m-search-quick-search-engine-alist'."
+the `w3m-search' function and the variable `w3m-uri-replace-alist'."
   (interactive
    (list
     (w3m-input-url nil
@@ -5977,11 +5991,6 @@
     t)) ;; qsearch
   (set-text-properties 0 (length url) nil url)
   (setq url (w3m-uri-replace url))
-  (when (or qsearch
-	    (equal referer "about://bookmark/"))
-    ;; quicksearch
-    (setq qsearch t
-	  url (w3m-search-quick-search-handler url)))
   (cond
    ;; process mailto: protocol
    ((string-match "\\`mailto:\\(.*\\)" url)
diff -u -r1.23 w3m-search.el
--- w3m-search.el	4 Apr 2003 10:34:50 -0000	1.23
+++ w3m-search.el	17 Apr 2003 03:44:12 -0000
@@ -137,25 +137,6 @@
   :group 'w3m
   :type 'string)
 
-(defcustom w3m-search-quick-search-engine-alist
-  '(("gg"   . "google")
-    ("ggg"  . "google groups")
-    ("ya"   . "yahoo")
-    ("al"   . "altavista")
-    ("bts"  . "debian-bts")
-    ("dpkg" . "debian-pkg")
-    ("archie" . "iij-archie"))
-  "*An alist of short names for defined search engine names.
-Each element follows the scheme (NAME . ENGINE)
-NAME if the short name you want to use for this engine.
-ENGINE is the name of the engine as defined in `w3m-search-engine-alist'.
-Be careful: the names you use here must match exactly the names of the
-engines defined in `w3m-search-engine-alist'."
-  :group 'w3m
-  :type '(repeat (cons :format "%v"
-		       (string :tag "Abbrev")
-		       (string :tag "Engine"))))
-
 (defcustom w3m-search-word-at-point t
   "*Non-nil means that the word at point is used as initial string."
   :group 'w3m
@@ -168,15 +149,6 @@
    (split-string str)
    "+"))
 
-(defun w3m-search-quick-search-engines ()
-  "Return an alist of short names and engines merging the options
-`w3m-search-engine-alist' and `w3m-search-quick-search-engine-alist'."
-  (let ((alist w3m-search-quick-search-engine-alist)
-	engine)
-    (dolist (element w3m-search-engine-alist alist)
-      (unless (rassoc (setq engine (car element)) alist)
-	(push (cons engine engine) alist)))))
-
 ;;;###autoload
 (defun w3m-search (search-engine query)
   "Search QUERY using SEARCH-ENGINE.
@@ -216,22 +188,13 @@
 	(error "Unknown search engine: %s" search-engine)))))
 
 ;;;###autoload
-(defun w3m-search-quick-search-handler (url)
-  "Check if the url is a quicksearch url.
-If URL is a quicksearch url, replace it with the real url needed to access
-the search engine.  If not, leave it alone."
-  (let ((ret url)
-	query engine info)
-    (dolist (quick-search-engine (w3m-search-quick-search-engines) ret)
-      (when (string-match (concat "\\`" (car quick-search-engine) ":") url)
-	(setq query (substring url (match-end 0)))
-	(unless (string= query "")
-	  (setq engine (cdr quick-search-engine)
-		info (assoc engine w3m-search-engine-alist))
-	  (when info
-	    (setq ret (format (cadr info)
-			      (w3m-search-escape-query-string
-			       query (caddr info))))))))))
+(defun w3m-search-uri-replace (uri engine)
+  "Generate query string for ENGINE from URI matched by last search."
+  (let ((query (substring uri (match-end 0)))
+	(info (assoc engine w3m-search-engine-alist)))
+    (when info
+      (format (cadr info)
+	      (w3m-search-escape-query-string query (caddr info))))))
 
 (provide 'w3m-search)