Subject: bug#6082: [PATCH] rcirc: rcirc-browse-url: Use only URLs before point and exclude consecutive duplicate URLs

From: Deniz Dogan <[email-masked]>
Date: Mon, 3 May 2010 01:09:24 +0200

Attached is a patch which does the following:

Modifies rcirc-browse-url to only complete to buffers that are before
point in the current buffer. To accomplish this, the structure of
rcirc-urls has changed from being just a list of strings to a list of
strings and buffer positions.

Modifies rcirc-markup-urls to conform to the new structure of
rcirc-urls and so that any URL that is already the latest recognized
URL is not added to the list of URLs. This prevents "consecutive
duplicate URLs" from being stored.
=== modified file 'lisp/net/rcirc.el'
--- lisp/net/rcirc.el	2010-01-13 08:35:10 +0000
+++ lisp/net/rcirc.el	2010-05-02 22:52:48 +0000
@@ -350,7 +350,8 @@
   "The channel or user associated with this buffer.")
 
 (defvar rcirc-urls nil
-  "List of urls seen in the current buffer.")
+  "List of URLs seen in the current buffer and the position in
+the buffer where the URL starts.")
 (put 'rcirc-urls 'permanent-local t)
 
 (defvar rcirc-timeout-seconds 600
@@ -2180,12 +2181,21 @@
    "\\)")
   "Regexp matching URLs.  Set to nil to disable URL features in rcirc.")
 
+(defun rcirc-condition-filter (condp lst)
+  "Given a condition and a list, returns the list with elements
+that do not satisfy the condition removed."
+  (delq nil (mapcar (lambda (x) (and (funcall condp x) x)) lst)))
+
 (defun rcirc-browse-url (&optional arg)
-  "Prompt for URL to browse based on URLs in buffer."
+  "Prompt for URL to browse based on URLs in buffer before point.
+
+If ARG is given, opens the URL in a new browser window."
   (interactive "P")
-  (let ((completions (mapcar (lambda (x) (cons x nil)) rcirc-urls))
-        (initial-input (car rcirc-urls))
-        (history (cdr rcirc-urls)))
+  (let* ((point (point))
+         (filtered (rcirc-condition-filter (lambda (x) (>= point (nth 1 x))) rcirc-urls))
+         (completions (mapcar (lambda (x) (car x)) filtered))
+         (initial-input (caar filtered))
+         (history (mapcar (lambda (x) (car x)) (cdr filtered))))
     (browse-url (completing-read "rcirc browse-url: "
                                  completions nil nil initial-input 'history)
                 arg)))
@@ -2242,13 +2252,16 @@
 
 (defun rcirc-markup-urls (sender response)
   (while (re-search-forward rcirc-url-regexp nil t)
-    (let ((start (match-beginning 0))
-	  (end (match-end 0)))
+    (let* ((start (match-beginning 0))
+           (end (match-end 0))
+           (link-text (buffer-substring-no-properties start end)))
       (rcirc-add-face start end 'rcirc-url)
       (add-text-properties start end (list 'mouse-face 'highlight
 					   'keymap rcirc-browse-url-map))
-      ;; record the url
-      (push (buffer-substring-no-properties start end) rcirc-urls))))
+      ;; record the url if it is not already the latest stored url
+      (when (or (not rcirc-urls)
+                (not (string= link-text (caar rcirc-urls))))
+        (push (list link-text start) rcirc-urls)))))
 
 (defun rcirc-markup-keywords (sender response)
   (when (and (string= response "PRIVMSG")