6a0035a44ef2eeb3c6d0ccd760cc8e9670f66208
[riece] / lisp / riece-url.el
1 ;;; riece-url.el --- URL collector add-on
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 ;; To use, add the following line to your ~/.riece/init.el:
28 ;; (add-to-list 'riece-addons 'riece-url)
29
30 ;;; Code:
31
32 (require 'riece-options)
33 (require 'riece-menu)                   ;riece-menu-items
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 (defvar riece-url-enabled nil)
64
65 (defconst riece-url-description
66   "Collect URL in IRC buffers")
67
68 (autoload 'widget-convert-button "wid-edit")
69
70 (defun riece-url-replace-match (string)
71   (let ((match-data (match-data))
72         (index 0)
73         number
74         replacement)
75     (while (string-match "\\\\[&1-9\\\\]" string index)
76       (if (eq (aref string (1+ (match-beginning 0))) ?&)
77           (setq number 0)
78         (unless (eq (aref string (1+ (match-beginning 0))) ?\\)
79           (setq number (string-to-number (substring (match-string 0 string)
80                                                     1)))))
81       (if number
82           (setq replacement
83                 (buffer-substring (nth (* number 2) match-data)
84                                   (nth (1+ (* number 2)) match-data)))
85         (setq replacement "\\"))
86       (setq string (concat (substring string 0 (match-beginning 0))
87                            replacement
88                            (substring string (match-end 0)))
89             index (+ index (length replacement))))
90     string))
91
92 (defun riece-url-scan-region (start end)
93   (let ((alist (cons (cons riece-url-regexp "\\&")
94                      riece-url-regexp-alist)))
95     (while alist
96       (save-excursion
97         (goto-char start)
98         (while (re-search-forward (car (car alist)) end t)
99           (let ((url (save-match-data
100                        (riece-url-replace-match (cdr (car alist))))))
101             (if (memq 'riece-highlight riece-addons)
102                 (widget-convert-button
103                  'url-link (match-beginning 0) (match-end 0) url))
104             (unless (member url riece-urls)
105               (setq riece-urls (cons url riece-urls))))))
106       (setq alist (cdr alist)))))
107
108 (defun riece-command-browse-url (&optional url)
109   (interactive
110    (list (completing-read "Open URL: " (mapcar #'list riece-urls))))
111   (browse-url url))
112
113 (defun riece-url-create-menu (menu)
114   (mapcar (lambda (url)
115             (vector url (list 'browse-url url)))
116           riece-urls))
117
118 (defvar riece-dialogue-mode-map)
119
120 (defun riece-url-requires ()
121   (append (if (memq 'riece-highlight riece-addons)
122               '(riece-highlight))
123           (if (memq 'riece-menu riece-addons)
124               '(riece-menu))))
125
126 (defun riece-url-insinuate ()
127   (add-hook 'riece-after-insert-functions 'riece-url-scan-region)
128   (if (memq 'riece-menu riece-addons)
129       (add-hook 'riece-command-mode-hook
130                 (lambda ()
131                   (easy-menu-add-item
132                    nil (list (car riece-menu-items))
133                    '("Open URL..." :filter riece-url-create-menu)))
134                 t)))
135
136 (defun riece-url-enable ()
137   (define-key riece-dialogue-mode-map "U" 'riece-command-browse-url)
138   (setq riece-url-enabled t))
139
140 (defun riece-url-disable ()
141   (define-key riece-dialogue-mode-map "U" nil)
142   (setq riece-url-enabled nil))
143
144 (provide 'riece-url)
145
146 ;;; riece-url.el ends here