1 ;;; riece-url.el --- collect URL in IRC buffers
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., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
27 ;; NOTE: This is an add-on module for Riece.
31 (require 'riece-options)
32 (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."
43 ;; the default value was copied from gnus-button-url-regexp
44 (defcustom riece-url-regexp
46 "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|"
47 "nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)"
48 "\\(//[-a-z0-9_.]+:[0-9]*\\)?"
49 (if (string-match "[[:digit:]]" "1") ;; Support POSIX?
50 (let ((chars "-a-z0-9_=#$@~%&*+\\/[:word:]")
54 ;; Match paired parentheses, e.g. in Wikipedia URLs:
55 ;; http://thread.gmane.org/47B4E3B2.3050402@gmail.com
56 "[" chars punct "]+" "(" "[" chars punct "]+" "[" chars "]*)" "[" chars "]*"
58 "[" chars punct "]+" "[" chars "]"
60 (concat ;; XEmacs 21.4 doesn't support POSIX.
61 "\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+"
62 "\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)"))
64 "Regular expression that matches URLs."
68 (defcustom riece-url-regexp-alist nil
69 "An alist mapping regexp to URL.
71 (setq riece-url-regexp-alist
72 '((\"\\\\bBug#\\\\([0-9]+\\\\)\\\\b\" .
73 \"http://bugs.debian.org/\\\\1\")))
75 This maps a string \"Bug#12345\" to a URL
76 \"http://bugs.debian.org/12345\"."
80 (defvar riece-urls nil
81 "A list of URL which appears in Riece buffers.")
83 (defconst riece-url-description
84 "Collect URL in IRC buffers.")
86 (autoload 'widget-convert-button "wid-edit")
88 (defun riece-url-replace-match (string)
89 (let ((match-data (match-data))
93 (while (string-match "\\\\[&1-9\\\\]" string index)
94 (if (eq (aref string (1+ (match-beginning 0))) ?&)
96 (unless (eq (aref string (1+ (match-beginning 0))) ?\\)
97 (setq number (string-to-number (substring (match-string 0 string)
101 (buffer-substring (nth (* number 2) match-data)
102 (nth (1+ (* number 2)) match-data)))
103 (setq replacement "\\"))
104 (setq string (concat (substring string 0 (match-beginning 0))
106 (substring string (match-end 0)))
107 index (+ index (length replacement))))
110 (defun riece-url-scan-region (start end)
111 (let ((alist (cons (cons riece-url-regexp "\\&")
112 riece-url-regexp-alist)))
116 (while (re-search-forward (car (car alist)) end t)
117 (let ((url (save-match-data
118 (riece-url-replace-match (cdr (car alist))))))
119 (if (memq 'riece-highlight riece-addons)
120 (widget-convert-button
121 'url-link (match-beginning 0) (match-end 0) url))
122 (unless (member url riece-urls)
123 (setq riece-urls (cons url riece-urls))))))
124 (setq alist (cdr alist)))))
126 (defun riece-command-browse-url (&optional url)
128 (list (completing-read (riece-mcat "Open URL: ")
129 (mapcar #'list riece-urls))))
132 (defun riece-url-create-menu (menu)
133 (mapcar (lambda (url)
134 (vector url (list 'browse-url url)))
137 (defvar riece-dialogue-mode-map)
139 (defun riece-url-requires ()
140 (append (if (memq 'riece-highlight riece-addons)
142 (if (memq 'riece-menu riece-addons)
145 (defun riece-url-command-mode-hook ()
147 nil (list (car riece-menu-items))
148 (list (if (featurep 'xemacs)
150 (riece-mcat "Open URL..."))
151 :filter 'riece-url-create-menu)))
153 (defun riece-url-insinuate ()
154 (add-hook 'riece-after-insert-functions 'riece-url-scan-region)
155 (if (memq 'riece-menu riece-addons)
156 (add-hook 'riece-command-mode-hook
157 'riece-url-command-mode-hook
160 (defun riece-url-uninstall ()
161 (easy-menu-remove-item
162 nil (list (car riece-menu-items))
163 (if (featurep 'xemacs)
165 (riece-mcat "Open URL...")))
166 (remove-hook 'riece-after-insert-functions 'riece-url-scan-region)
167 (remove-hook 'riece-command-mode-hook
168 'riece-url-command-mode-hook))
170 (defun riece-url-enable ()
171 (define-key riece-dialogue-mode-map "U" 'riece-command-browse-url))
173 (defun riece-url-disable ()
174 (define-key riece-dialogue-mode-map "U" nil))
178 ;;; riece-url.el ends here