1 ;;; riece-url.el --- URL collector add-on
2 ;; Copyright (C) 1998-2003 Daiki Ueno
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
6 ;; Keywords: IRC, riece
8 ;; This file is part of Riece.
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; To use, add the following line to your ~/.riece/init.el:
28 ;; (add-to-list 'riece-addons 'riece-url)
32 (require 'riece-options)
33 (require 'riece-menu) ;riece-menu-items
35 (autoload 'browse-url "browse-url")
36 (defvar browse-url-browser-function)
38 (defgroup riece-url nil
39 "URL Browsing in IRC buffer."
42 (defcustom riece-url-regexp "\\b\\(s?https?\\|ftp\\|file\\|gopher\\|news\\|telnet\\|wais\\|mailto\\):\\(//[-a-zA-Z0-9_.]+:[0-9]*\\)?[-a-zA-Z0-9_=?#$@~`%&*+|\\/.,;]*[-a-zA-Z0-9_=#$@~`%&*+|\\/;]"
43 "Regular expression that matches URLs."
47 (defcustom riece-url-regexp-alist nil
48 "An alist mapping regexp to URL.
50 (setq riece-url-regexp-alist
51 '((\"\\\\bBug#\\\\([0-9]+\\\\)\\\\b\" .
52 \"http://bugs.debian.org/\\\\1\")))
54 This will map a string \"Bug#12345\" to a URL
55 \"http://bugs.debian.org/12345\"."
59 (defvar riece-urls nil
60 "A list of URL which appears in Riece buffers.")
62 (defvar riece-url-enabled nil)
64 (defconst riece-url-description
65 "Collect URL in IRC buffers")
67 (autoload 'widget-convert-button "wid-edit")
69 (defun riece-url-replace-match (string)
70 (let ((match-data (match-data))
74 (while (string-match "\\\\[&1-9\\\\]" string index)
75 (if (eq (aref string (1+ (match-beginning 0))) ?&)
77 (unless (eq (aref string (1+ (match-beginning 0))) ?\\)
78 (setq number (string-to-number (substring (match-string 0 string)
82 (buffer-substring (nth (* number 2) match-data)
83 (nth (1+ (* number 2)) match-data)))
84 (setq replacement "\\"))
85 (setq string (concat (substring string 0 (match-beginning 0))
87 (substring string (match-end 0)))
88 index (+ index (length replacement))))
91 (defun riece-url-scan-region (start end)
92 (let ((alist (cons (cons riece-url-regexp "\\&")
93 riece-url-regexp-alist)))
97 (while (re-search-forward (car (car alist)) end t)
98 (let ((url (save-match-data
99 (riece-url-replace-match (cdr (car alist))))))
100 (if (memq 'riece-highlight riece-addons)
101 (widget-convert-button
102 'url-link (match-beginning 0) (match-end 0) url))
103 (unless (member url riece-urls)
104 (setq riece-urls (cons url riece-urls))))))
105 (setq alist (cdr alist)))))
107 (defun riece-command-browse-url (&optional url)
109 (list (completing-read "Open URL: " (mapcar #'list riece-urls))))
112 (defun riece-url-create-menu (menu)
113 (mapcar (lambda (url)
114 (vector url (list 'browse-url url)))
117 (defvar riece-dialogue-mode-map)
119 (defun riece-url-requires ()
120 (append (if (memq 'riece-highlight riece-addons)
122 (if (memq 'riece-menu riece-addons)
125 (defun riece-url-insinuate ()
126 (add-hook 'riece-after-insert-functions 'riece-url-scan-region)
127 (if (memq 'riece-menu riece-addons)
128 (add-hook 'riece-command-mode-hook
131 nil (list (car riece-menu-items))
132 '("Open URL..." :filter riece-url-create-menu)))
135 (defun riece-url-enable ()
136 (define-key riece-dialogue-mode-map "U" 'riece-command-browse-url)
137 (setq riece-url-enabled t))
139 (defun riece-url-disable ()
140 (define-key riece-dialogue-mode-map "U" nil)
141 (setq riece-url-enabled nil))
145 ;;; riece-url.el ends here