[Conkeror] [RFC] Add facility to allow user functions to transform typed URLs.

David Kettler David.Kettler at dsto.defence.gov.au
Tue Nov 4 22:54:52 PST 2008


If the typed input to read-url and friends is not (something like) a
valid URL or webjump then a list of user defined "handler" functions
can be called to try to transform the input into something valid.

A handler is provided to transform a blank input into the current
buffer's URL.  This means that "g RET" will reload the current buffer
and "C-u g RET" will duplicate the current buffer.

A handler is provided to prepend a webjump keyword (defaulting to
"google") to the typed input.  This means that an input of an
arbitrary set of words will search on those words.  There has to be
more than one word though, otherwise it might be the name of a web
site.  And the first word can't be a webjump.

The two provided handlers can be enabled by placing this in your rc:

read_url_handler_list = [read_url_handler_blank_is_current,
                         read_url_handler_default_webjump];
---

Dodgy bits:

  - The list of functions is difficult to view with C-h v.  Maybe the
    names should be quoted then funcalled.

  - The doco for the provided handlers is only a comment; it's not
    user accessible.  Maybe a mechanism like interactive() would solve
    this and the previous point.

  - Maybe handlers should use a modification of the hook mechanism,
    instead.

  - Passing the minibuffer as a second argument to the handlers is
    strange, but it provides context to the handler which might be
    useful.  For instance read_url_handler_blank_is_current() uses it
    to get the current URI.

  - The if() added to read_url() is ugly.

  - possibly_valid_url() is poorly named and is not very discriminating.

  - It doesn't really check if the URL is valid; possibly_valid_url()
    is just a heuristic.  A real validity check would probably need
    support from nsIWebNavigation; probably it would need to attempt
    the load.  But then behaviour would depend on whether some site is
    up.  Maybe the heuristic is best after all.

  - Perhaps read_url_handler_blank_is_current() should be omitted.
---
 modules/content-buffer.js |   31 +++++++++++++++++++++++++++++++
 modules/utils.js          |   10 ++++++++++
 2 files changed, 41 insertions(+), 0 deletions(-)

diff --git a/modules/content-buffer.js b/modules/content-buffer.js
index 4ed6e18..122ecbc 100644
--- a/modules/content-buffer.js
+++ b/modules/content-buffer.js
@@ -287,6 +287,35 @@ add_hook("current_content_buffer_status_change_hook",
          });
 */
 
+
+define_variable("read_url_handler_list", [], "A list of handler functions which transform a typed url into a valid url or webjump.  If the typed input is not valid then each function on this list is tried in turn.  The handler function is called with the input as a string argument and it should return either a string or null.  The handler function can also access the minibuffer as its second argument.  The result of the first function on the list that returns a string is used in place of the input.");
+
+define_variable("default_webjump", "google", "When a typed url is not a valid url or webjump, prepend this webjump keyword to the input.  The function read_url_handler_default_webjump must be on read_url_handler_list to enable this functionality; it should be the last element on the list (because any input is accepted).");
+
+
+/* read_url_handler_default_webjump transforms any input into a given
+ * webjump.  It should be the last handler on the list. */
+function read_url_handler_default_webjump(input) {
+    return default_webjump + " " + input;
+}
+
+/* read_url_handler_blank_is_current transforms a blank input into
+ * the current url of the buffer.  find-url can thus reload the
+ * current page or load a copy in a new buffer. */
+function read_url_handler_blank_is_current(input, minibuffer) {
+    if (input.length == 0)
+        return minibuffer.window.buffers.current.display_URI_string;
+    return null;
+}
+
+minibuffer.prototype.try_read_url_handlers = function(input) {
+    var result;
+    for (var i = 0; i < read_url_handler_list.length; ++i)
+        if (result = read_url_handler_list[i](input, this))
+            return result;
+    return input;
+}
+
 define_variable("url_completion_use_webjumps", true, "Specifies whether URL completion should complete webjumps.");
 define_variable("url_completion_use_bookmarks", true, "Specifies whether URL completion should complete bookmarks.");
 define_variable("url_completion_use_history", false,
@@ -312,6 +341,8 @@ minibuffer.prototype.read_url = function () {
         $auto_complete = "url",
         $select = minibuffer_read_url_select_initial,
         $match_required = false);
+    if (!possibly_valid_url(result) && !getWebJump(result))
+        result = this.try_read_url_handlers(result);
     if (result == "") // well-formedness check. (could be better!)
         throw ("invalid url or webjump (\""+ result +"\")");
     yield co_return(result);
diff --git a/modules/utils.js b/modules/utils.js
index c02671a..2ca17f4 100644
--- a/modules/utils.js
+++ b/modules/utils.js
@@ -1266,6 +1266,16 @@ function compute_url_up_path (url)
 }
 
 
+/* possibly_valid_url returns true if the string might be a valid
+ * thing to pass to nsIWebNavigation.loadURI.  Currently just checks
+ * that there's no whitespace in the middle and that it's not entirely
+ * whitespace; could be improved.
+ */
+function possibly_valid_url (url) {
+    return !(/\S\s+\S/.test(url)) && !(/^\s*$/.test(url));
+}
+
+
 /* remove_duplicates_filter returns a function that can be
  * used in Array.filter.  It removes duplicates.
  */
-- 
1.6.0.3.613.g9f8f13.dirty


-- 
IMPORTANT: This email remains the property of the Australian Defence
Organisation and is subject to the jurisdiction of section 70 of the
CRIMES ACT 1914. If you have received this email in error, you are
requested to contact the sender and delete the email.



More information about the Conkeror mailing list