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

New w3m-uri-modify hook?


While I was writing the bookmark-related nodes for the Info
documentation, I realized that one feature that I'd like to have is the
ability to quickly open bookmarks, without having to go to the bookmark
page, find the right link, and open it (I often visit the same
webpages).  So I needed a "quick bookmark" feature, but the current
situation in emacs-w3m doesn't allow easy manipulation of URLs; the
`w3m-uri-replace' approach would work, but it isn't flexible enough for
me (I'd like to reuse my quick bookmarks with other Elisp packages).

The attached patch adds a generic `w3m-uri-modify' function which calls
functions from a hook on the URL, so that users can write simple
functions and use them to modify the URL.  It also removes the
non-interactive call to `w3m-uri-modify', changes the libqsearch
handler and `w3m-uri-modify' to return nil instead of the original url,
and puts them in the default list of functions to call on the URL.

With this patch, my "quick bookmark" feature takes two lines of Lisp;
the following comes from my ~/.emacs{,-w3m}:

* From ~/.emacs:

(setq ore-quick-bookmark-alist
      '(("gnu"  . "http://www.gnu.org/")
        ("iam"  . "http://orebokech.com/")
        ("sf"   . "http://online.securityfocus.com/")
        ("/."   . "http://slashdot.org/")
        ("ell"  . "http://www.anc.ed.ac.uk/~stephen/emacs/ell.html")
        ("news" . "http://fr.news.yahoo.com/")))

* From ~/.emacs-w3m:

(setq ore-quick-bookmark-prefix "qb")

(defun ore-quick-bookmarks-handler (url)
  "Check for a quick bookmark URL type.
If found, return the real URL of the bookmark, otherwise nil."
  (when (string-match (concat "\\`" ore-quick-bookmark-prefix ":") url)
    (cdr (assoc (substring url (match-end 0)) ore-quick-bookmark-alist))))

(add-hook 'w3m-uri-modify-hook 'ore-quick-bookmarks-handler)

I think my approach is better because:
- user configuration is made using hooks
- the URL manipulation is done only if called interactively
- using `w3m-uri-modify' is still possible for people who really want
  to use it, but only for interactive calls (the fact that it was
  called even non-interactively while the qsearch handler wasn't doesn't
  seem really logical to me)

I might be wrong, though.  What do you think?


Romain FRANCOISE <romain@orebokech.com> | I'm going out for a little
it's a miracle -- http://orebokech.com/ | drive, and it may be the last
                                        | time you see me alive.
Index: w3m.el
RCS file: /storage/cvsroot/emacs-w3m/w3m.el,v
retrieving revision 1.774
diff -u -r1.774 w3m.el
--- w3m.el	20 Dec 2002 09:16:19 -0000	1.774
+++ w3m.el	20 Dec 2002 18:23:36 -0000
@@ -740,6 +740,14 @@
   :group 'w3m
   :type 'hook)
+(defcustom w3m-uri-modify-hook nil
+  "*Hook run to maybe modify URLs in `w3m-goto-url'.
+This hook is run with one argument, the URL to be loaded.  All functions
+you add to this hook should return nil if they didn't modify the URL,
+and otherwise return the modified URL."
+  :group 'w3m
+  :type 'hook)
 (defcustom w3m-async-exec (not (memq system-type '(macos)))
   "*If non-nil, w3m is executed as an asynchronous process.  Note that
 setting this option to t is harmful on some platforms.  As far as we
@@ -2218,6 +2226,25 @@
+(defvar w3m-uri-modify-functions '(w3m-uri-replace 
+                                   w3m-search-quick-search-handler)
+  "Functions to call when we want to modify the URL.  Do not use
+this variable for user customizations, use `w3m-uri-modify-hook'
+(defun w3m-uri-modify (url)
+  "Modify a URL, using the functions in `w3m-uri-modify-functions' and
+in `w3m-uri-modify-hook'."
+  ;; Include user functions from w3m-uri-modify-hook,
+  ;; then call each function until one succeeds.
+  (let* ((funcs (if w3m-uri-modify-hook
+                    (append w3m-uri-modify-functions 
+                            w3m-uri-modify-hook)
+                  w3m-uri-modify-functions))
+         (ret (run-hook-with-args-until-success 'funcs url)))
+    ;; If URL has been changed return it, otherwise return
+    ;; our original url...
+    (if ret ret url)))
 ;;; HTML character entity handling:
 (defun w3m-entity-db-setup ()
@@ -5670,7 +5697,7 @@
 	       (funcall (cdr (car alist)) uri))
 	      ((stringp (cdr (car alist)))
 	       (replace-match (cdr (car alist)) nil nil uri)))
-      uri)))
+      nil)))
 (defun w3m-goto-mailto-url (url &optional post-data)
   (let ((before (nreverse (buffer-list)))
@@ -5832,7 +5859,7 @@
 (defun w3m-goto-url
-  (url &optional reload charset post-data referer handler qsearch)
+  (url &optional reload charset post-data referer handler modify-url)
   "Retrieve contents of URL.
 If the second argument RELOAD is non-nil, reload a content of URL.
 Except that if it is 'redisplay, re-display the page without reloading.
@@ -5864,14 +5891,13 @@
     nil ;; post-data
     nil ;; referer
     nil ;; handler
-    t)) ;; qsearch
+    t)) ;; modify-url
   (set-text-properties 0 (length url) nil url)
-  (setq url (w3m-uri-replace url))
-  (when (or qsearch
+  (when (or modify-url
 	    (equal referer "about://bookmark/"))
-    ;; quicksearch
-    (setq qsearch t
-	  url (w3m-search-quick-search-handler url)))
+    ;; Call the URL modification functions
+    (setq modify-url t
+	  url (w3m-uri-modify url)))
    ;; process mailto: protocol
    ((string-match "\\`mailto:\\(.*\\)" url)
@@ -5900,12 +5926,14 @@
 			    (split-string (substring url (match-end 0)) "&"))))
 	      (type (prog1
-			(w3m-goto-url (car urls) nil nil nil nil nil qsearch)
+			(w3m-goto-url (car urls) nil nil nil nil nil 
+                                      modify-url)
 		      (dolist (url (cdr urls))
 			  (set-buffer (w3m-copy-buffer nil nil nil 'empty))
-			    (w3m-goto-url url nil nil nil nil nil qsearch))))))
+			    (w3m-goto-url url nil nil nil nil nil 
+                                          modify-url))))))
       ;; Retrieve the page.
       (lexical-let ((orig url)
Index: w3m-search.el
RCS file: /storage/cvsroot/emacs-w3m/w3m-search.el,v
retrieving revision 1.21
diff -u -r1.21 w3m-search.el
--- w3m-search.el	27 Nov 2002 22:52:40 -0000	1.21
+++ w3m-search.el	20 Dec 2002 18:23:37 -0000
@@ -216,9 +216,8 @@
 (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)
+the search engine.  If not, return nil."
+  (let (ret 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)))