* riece.el (riece-create-buffers): Suppress byte-compile
[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., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, 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 (require 'easymenu)
34
35 (autoload 'browse-url "browse-url")
36 (defvar browse-url-browser-function)
37
38 (defgroup riece-url nil
39   "URL Browsing in IRC buffer."
40   :prefix "riece-"
41   :group 'riece)
42
43 (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_=#$@~`%&*+|\\/;]"
44   "Regular expression that matches URLs."
45   :group 'riece-url
46   :type 'regexp)
47
48 (defcustom riece-url-regexp-alist nil
49   "An alist mapping regexp to URL.
50 For example:
51   (setq riece-url-regexp-alist
52         '((\"\\\\bBug#\\\\([0-9]+\\\\)\\\\b\" .
53            \"http://bugs.debian.org/\\\\1\")))
54
55 This maps a string \"Bug#12345\" to a URL
56 \"http://bugs.debian.org/12345\"."
57   :type 'alist
58   :group 'riece-url)
59
60 (defvar riece-urls nil
61   "A list of URL which appears in Riece buffers.")
62
63 (defconst riece-url-description
64   "Collect URL in IRC buffers.")
65
66 (autoload 'widget-convert-button "wid-edit")
67
68 (defun riece-url-replace-match (string)
69   (let ((match-data (match-data))
70         (index 0)
71         number
72         replacement)
73     (while (string-match "\\\\[&1-9\\\\]" string index)
74       (if (eq (aref string (1+ (match-beginning 0))) ?&)
75           (setq number 0)
76         (unless (eq (aref string (1+ (match-beginning 0))) ?\\)
77           (setq number (string-to-number (substring (match-string 0 string)
78                                                     1)))))
79       (if number
80           (setq replacement
81                 (buffer-substring (nth (* number 2) match-data)
82                                   (nth (1+ (* number 2)) match-data)))
83         (setq replacement "\\"))
84       (setq string (concat (substring string 0 (match-beginning 0))
85                            replacement
86                            (substring string (match-end 0)))
87             index (+ index (length replacement))))
88     string))
89
90 (defun riece-url-scan-region (start end)
91   (let ((alist (cons (cons riece-url-regexp "\\&")
92                      riece-url-regexp-alist)))
93     (while alist
94       (save-excursion
95         (goto-char start)
96         (while (re-search-forward (car (car alist)) end t)
97           (let ((url (save-match-data
98                        (riece-url-replace-match (cdr (car alist))))))
99             (if (memq 'riece-highlight riece-addons)
100                 (widget-convert-button
101                  'url-link (match-beginning 0) (match-end 0) url))
102             (unless (member url riece-urls)
103               (setq riece-urls (cons url riece-urls))))))
104       (setq alist (cdr alist)))))
105
106 (defun riece-command-browse-url (&optional url)
107   (interactive
108    (list (completing-read (riece-mcat "Open URL: ")
109                           (mapcar #'list riece-urls))))
110   (browse-url url))
111
112 (defun riece-url-create-menu (menu)
113   (mapcar (lambda (url)
114             (vector url (list 'browse-url url)))
115           riece-urls))
116
117 (defvar riece-dialogue-mode-map)
118
119 (defun riece-url-requires ()
120   (append (if (memq 'riece-highlight riece-addons)
121               '(riece-highlight))
122           (if (memq 'riece-menu riece-addons)
123               '(riece-menu))))
124
125 (defun riece-url-command-mode-hook ()
126   (easy-menu-add-item
127    nil (list (car riece-menu-items))
128    (list (if (featurep 'xemacs)
129              "Open URL..."
130            (riece-mcat "Open URL..."))
131          :filter 'riece-url-create-menu)))
132
133 (defun riece-url-insinuate ()
134   (add-hook 'riece-after-insert-functions 'riece-url-scan-region)
135   (if (memq 'riece-menu riece-addons)
136       (add-hook 'riece-command-mode-hook
137                 'riece-url-command-mode-hook
138                 t)))
139
140 (defun riece-url-uninstall ()
141   (easy-menu-remove-item
142    nil (list (car riece-menu-items))
143    (if (featurep 'xemacs)
144        "Open URL..."
145      (riece-mcat "Open URL...")))
146   (remove-hook 'riece-after-insert-functions 'riece-url-scan-region)
147   (remove-hook 'riece-command-mode-hook
148                'riece-url-command-mode-hook))
149
150 (defun riece-url-enable ()
151   (define-key riece-dialogue-mode-map "U" 'riece-command-browse-url))
152
153 (defun riece-url-disable ()
154   (define-key riece-dialogue-mode-map "U" nil))
155
156 (provide 'riece-url)
157
158 ;;; riece-url.el ends here