* test/Makefile.am (EXTRA_DIST): Add test-riece-url.el.
authorueno <ueno>
Thu, 25 Nov 2004 08:11:44 +0000 (08:11 +0000)
committerueno <ueno>
Thu, 25 Nov 2004 08:11:44 +0000 (08:11 +0000)
* test/test-riece-url.el: New test cases.

* riece-url.el (riece-url-regexp-alist): Backport from Liece 2.0.
(riece-url-replace-match): New function.
(riece-url-scan-region): Backport from Liece 2.0.

lisp/ChangeLog
lisp/riece-url.el
lisp/test/Makefile.am
lisp/test/test-riece-url.el [new file with mode: 0644]

index fffb718..7d13f80 100644 (file)
@@ -1,5 +1,14 @@
 2004-11-25  Daiki Ueno  <ueno@unixuser.org>
 
+       * test/Makefile.am (EXTRA_DIST): Add test-riece-url.el.
+       * test/test-riece-url.el: New test cases.
+
+       * riece-url.el (riece-url-regexp-alist): Backport from Liece 2.0.
+       (riece-url-replace-match): New function.
+       (riece-url-scan-region): Backport from Liece 2.0.
+
+2004-11-25  Daiki Ueno  <ueno@unixuser.org>
+
        * riece-server.el (riece-server-properties): New function.
 
 2004-11-22  Daiki Ueno  <ueno@unixuser.org>
index 3e72b79..73c7e02 100644 (file)
   :group 'riece-url
   :type 'regexp)
 
+(defcustom riece-url-regexp-alist nil
+  "An alist mapping regexp to URL.
+For example:
+  (setq riece-url-regexp-alist
+        '((\"\\\\bBug#\\\\([0-9]+\\\\)\\\\b\" .
+           \"http://bugs.debian.org/\\\\1\")))
+
+This will map a string \"Bug#12345\" to a URL
+\"http://bugs.debian.org/12345\"."
+  :type 'alist
+  :group 'riece-url)
+
 (defvar riece-urls nil
   "A list of URL which appears in Riece buffers.")
 
 
 (autoload 'widget-convert-button "wid-edit")
 
+(defun riece-url-replace-match (string)
+  (let ((match-data (match-data))
+       (index 0)
+       number
+       replacement)
+    (while (string-match "\\\\[&1-9\\\\]" string index)
+      (if (eq (aref string (1+ (match-beginning 0))) ?&)
+         (setq number 0)
+       (unless (eq (aref string (1+ (match-beginning 0))) ?\\)
+         (setq number (string-to-number (substring (match-string 0 string)
+                                                   1)))))
+      (if number
+         (setq replacement
+               (buffer-substring (nth (* number 2) match-data)
+                                 (nth (1+ (* number 2)) match-data)))
+       (setq replacement "\\"))
+      (setq string (concat (substring string 0 (match-beginning 0))
+                          replacement
+                          (substring string (match-end 0)))
+           index (+ index (length replacement))))
+    string))
+
 (defun riece-url-scan-region (start end)
-  (save-excursion
-    (goto-char start)
-    (while (re-search-forward riece-url-regexp end t)
-      (let ((url (match-string 0)))
-       (if (memq 'riece-highlight riece-addons)
-           (widget-convert-button
-            'url-link (match-beginning 0) (match-end 0) url))
-       (unless (member url riece-urls)
-         (setq riece-urls (cons url riece-urls)))))))
+  (let ((alist (cons (cons riece-url-regexp "\\&")
+                    riece-url-regexp-alist)))
+    (while alist
+      (save-excursion
+       (goto-char start)
+       (while (re-search-forward (car (car alist)) end t)
+         (let ((url (riece-url-replace-match (cdr (car alist)))))
+           (if (memq 'riece-highlight riece-addons)
+               (widget-convert-button
+                'url-link (match-beginning 0) (match-end 0) url))
+           (unless (member url riece-urls)
+             (setq riece-urls (cons url riece-urls))))))
+      (setq alist (cdr alist)))))
 
 (defun riece-command-browse-url (&optional url)
   (interactive
index bc7317a..bf70367 100644 (file)
@@ -1,2 +1,3 @@
 EXTRA_DIST = luna.el lunit.el \
-       test-riece-addon.el test-riece-alias.el test-riece-yank.el
\ No newline at end of file
+       test-riece-addon.el test-riece-alias.el test-riece-url.el \
+       test-riece-yank.el
\ No newline at end of file
diff --git a/lisp/test/test-riece-url.el b/lisp/test/test-riece-url.el
new file mode 100644 (file)
index 0000000..a76a2e8
--- /dev/null
@@ -0,0 +1,37 @@
+(require 'riece-url)
+
+(luna-define-class test-riece-url (lunit-test-case))
+
+(luna-define-method test-riece-url-replace-match ((case test-riece-url))
+  (with-temp-buffer
+    (insert "111\n222\n333\n")
+    (goto-char (point-min))
+    (re-search-forward "2\\(2\\)2")
+    (lunit-assert
+     (equal (save-match-data
+             (riece-url-replace-match "\\&"))
+           "222"))
+    (lunit-assert
+     (equal (save-match-data
+             (riece-url-replace-match "\\0"))
+           "\\0"))
+    (lunit-assert
+     (equal (save-match-data
+             (riece-url-replace-match "\\1"))
+           "2"))
+    (lunit-assert
+     (equal (save-match-data
+             (riece-url-replace-match "\\\\"))
+           "\\"))))
+
+(luna-define-method test-riece-url-regexp-alist ((case test-riece-url))
+  (with-temp-buffer
+    (insert "Bug#12345\n")
+    (let ((riece-url-regexp-alist
+          '(("\\bBug#\\([0-9]+\\)\\b" .
+             "http://bugs.debian.org/\\1")))
+         riece-urls
+         riece-addons)
+      (riece-url-scan-region (point-min) (point-max))
+      (lunit-assert
+       (member "http://bugs.debian.org/12345" riece-urls)))))