Fix byte-compile error
[riece] / lisp / riece-url.el
1 ;;; riece-url.el --- collect URL in IRC buffers -*- lexical-binding: t -*-
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 ;; the default value was copied from gnus-button-url-regexp
44 (defcustom riece-url-regexp
45   (concat
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:]")
51              (punct "!?:;.,"))
52          (concat
53           "\\(?:"
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 "]*"
57           "\\|"
58           "[" chars punct     "]+" "[" chars "]"
59           "\\)"))
60      (concat ;; XEmacs 21.4 doesn't support POSIX.
61       "\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+"
62       "\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)"))
63    "\\)")
64   "Regular expression that matches URLs."
65   :group 'riece-url
66   :type 'regexp)
67
68 (defcustom riece-url-regexp-alist nil
69   "An alist mapping regexp to URL.
70 For example:
71   (setq riece-url-regexp-alist
72         '((\"\\\\bBug#\\\\([0-9]+\\\\)\\\\b\" .
73            \"http://bugs.debian.org/\\\\1\")))
74
75 This maps a string \"Bug#12345\" to a URL
76 \"http://bugs.debian.org/12345\"."
77   :type 'alist
78   :group 'riece-url)
79
80 (defvar riece-urls nil
81   "A list of URL which appears in Riece buffers.")
82
83 (defconst riece-url-description
84   "Collect URL in IRC buffers.")
85
86 (autoload 'widget-convert-button "wid-edit")
87
88 (defun riece-url-replace-match (string)
89   (let ((match-data (match-data))
90         (index 0)
91         number
92         replacement)
93     (while (string-match "\\\\[&1-9\\\\]" string index)
94       (if (eq (aref string (1+ (match-beginning 0))) ?&)
95           (setq number 0)
96         (unless (eq (aref string (1+ (match-beginning 0))) ?\\)
97           (setq number (string-to-number (substring (match-string 0 string)
98                                                     1)))))
99       (if number
100           (setq replacement
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))
105                            replacement
106                            (substring string (match-end 0)))
107             index (+ index (length replacement))))
108     string))
109
110 (defun riece-url-scan-region (start end)
111   (let ((alist (cons (cons riece-url-regexp "\\&")
112                      riece-url-regexp-alist)))
113     (while alist
114       (save-excursion
115         (goto-char start)
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)))))
125
126 (defun riece-command-browse-url (&optional url)
127   (interactive
128    (list (completing-read (riece-mcat "Open URL: ")
129                           (mapcar #'list riece-urls))))
130   (browse-url url))
131
132 (defun riece-url-create-menu (_menu)
133   (mapcar (lambda (url)
134             (vector url (list 'browse-url url)))
135           riece-urls))
136
137 (defvar riece-dialogue-mode-map)
138
139 (defun riece-url-requires ()
140   (append (if (memq 'riece-highlight riece-addons)
141               '(riece-highlight))
142           (if (memq 'riece-menu riece-addons)
143               '(riece-menu))))
144
145 (defun riece-url-command-mode-hook ()
146   (easy-menu-add-item
147    nil (list (car riece-menu-items))
148    (list (if (featurep 'xemacs)
149              "Open URL..."
150            (riece-mcat "Open URL..."))
151          :filter 'riece-url-create-menu)))
152
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
158                 t)))
159
160 (defun riece-url-uninstall ()
161   (easy-menu-remove-item
162    nil (list (car riece-menu-items))
163    (if (featurep 'xemacs)
164        "Open URL..."
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))
169
170 (defun riece-url-enable ()
171   (define-key riece-dialogue-mode-map "U" 'riece-command-browse-url))
172
173 (defun riece-url-disable ()
174   (define-key riece-dialogue-mode-map "U" nil))
175
176 (provide 'riece-url)
177
178 ;;; riece-url.el ends here