Fixed.
[riece] / lisp / riece-url.el
1 ;;; riece-url.el --- collect URL in IRC buffers
2 ;; Copyright (C) 1998-2003 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Created: 1998-09-28
6 ;; Keywords: IRC, riece
7
8 ;; This file is part of Riece.
9
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)
13 ;; any later version.
14
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.
19
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.
24
25 ;;; Commentary:
26
27 ;; NOTE: This is an add-on module for Riece.
28
29 ;;; Code:
30
31 (require 'riece-options)
32 (require 'riece-menu)                   ;riece-menu-items
33
34 (autoload 'browse-url "browse-url")
35 (defvar browse-url-browser-function)
36
37 (defgroup riece-url nil
38   "URL Browsing in IRC buffer."
39   :prefix "riece-"
40   :group 'riece)
41
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."
44   :group 'riece-url
45   :type 'regexp)
46
47 (defcustom riece-url-regexp-alist nil
48   "An alist mapping regexp to URL.
49 For example:
50   (setq riece-url-regexp-alist
51         '((\"\\\\bBug#\\\\([0-9]+\\\\)\\\\b\" .
52            \"http://bugs.debian.org/\\\\1\")))
53
54 This maps a string \"Bug#12345\" to a URL
55 \"http://bugs.debian.org/12345\"."
56   :type 'alist
57   :group 'riece-url)
58
59 (defvar riece-urls nil
60   "A list of URL which appears in Riece buffers.")
61
62 (defconst riece-url-description
63   "Collect URL in IRC buffers.")
64
65 (autoload 'widget-convert-button "wid-edit")
66
67 (defun riece-url-replace-match (string)
68   (let ((match-data (match-data))
69         (index 0)
70         number
71         replacement)
72     (while (string-match "\\\\[&1-9\\\\]" string index)
73       (if (eq (aref string (1+ (match-beginning 0))) ?&)
74           (setq number 0)
75         (unless (eq (aref string (1+ (match-beginning 0))) ?\\)
76           (setq number (string-to-number (substring (match-string 0 string)
77                                                     1)))))
78       (if number
79           (setq replacement
80                 (buffer-substring (nth (* number 2) match-data)
81                                   (nth (1+ (* number 2)) match-data)))
82         (setq replacement "\\"))
83       (setq string (concat (substring string 0 (match-beginning 0))
84                            replacement
85                            (substring string (match-end 0)))
86             index (+ index (length replacement))))
87     string))
88
89 (defun riece-url-scan-region (start end)
90   (let ((alist (cons (cons riece-url-regexp "\\&")
91                      riece-url-regexp-alist)))
92     (while alist
93       (save-excursion
94         (goto-char start)
95         (while (re-search-forward (car (car alist)) end t)
96           (let ((url (save-match-data
97                        (riece-url-replace-match (cdr (car alist))))))
98             (if (memq 'riece-highlight riece-addons)
99                 (widget-convert-button
100                  'url-link (match-beginning 0) (match-end 0) url))
101             (unless (member url riece-urls)
102               (setq riece-urls (cons url riece-urls))))))
103       (setq alist (cdr alist)))))
104
105 (defun riece-command-browse-url (&optional url)
106   (interactive
107    (list (completing-read "Open URL: " (mapcar #'list riece-urls))))
108   (browse-url url))
109
110 (defun riece-url-create-menu (menu)
111   (mapcar (lambda (url)
112             (vector url (list 'browse-url url)))
113           riece-urls))
114
115 (defvar riece-dialogue-mode-map)
116
117 (defun riece-url-requires ()
118   (append (if (memq 'riece-highlight riece-addons)
119               '(riece-highlight))
120           (if (memq 'riece-menu riece-addons)
121               '(riece-menu))))
122
123 (defun riece-url-command-mode-hook ()
124   (easy-menu-add-item
125    nil (list (car riece-menu-items))
126    '("Open URL..." :filter riece-url-create-menu)))
127
128 (defun riece-url-insinuate ()
129   (add-hook 'riece-after-insert-functions 'riece-url-scan-region)
130   (if (memq 'riece-menu riece-addons)
131       (add-hook 'riece-command-mode-hook
132                 'riece-url-command-mode-hook
133                 t)))
134
135 (defun riece-url-uninstall ()
136   (easy-menu-remove-item
137    nil (list (car riece-menu-items))
138    "Open URL...")
139   (remove-hook 'riece-after-insert-functions 'riece-url-scan-region)
140   (remove-hook 'riece-command-mode-hook
141                'riece-url-command-mode-hook))
142
143 (defun riece-url-enable ()
144   (define-key riece-dialogue-mode-map "U" 'riece-command-browse-url))
145
146 (defun riece-url-disable ()
147   (define-key riece-dialogue-mode-map "U" nil))
148
149 (provide 'riece-url)
150
151 ;;; riece-url.el ends here