* riece-xface.el: Support enable/disable.
[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   :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 (defvar riece-urls nil
48   "A list of URL which appears in Riece buffers.")
49
50 (defvar riece-url-enabled nil)
51
52 (defconst riece-url-description
53   "Collect URL in IRC buffers")
54
55 (autoload 'widget-convert-button "wid-edit")
56
57 (defun riece-url-scan-region (start end)
58   (save-excursion
59     (goto-char start)
60     (while (re-search-forward riece-url-regexp end t)
61       (let ((url (match-string 0)))
62         (if (memq 'riece-highlight riece-addons)
63             (widget-convert-button
64              'url-link (match-beginning 0) (match-end 0) url))
65         (unless (member url riece-urls)
66           (setq riece-urls (cons url riece-urls)))))))
67
68 (defun riece-command-browse-url (&optional url)
69   (interactive
70    (list (completing-read "Open URL: " (mapcar #'list riece-urls))))
71   (browse-url url))
72
73 (defun riece-url-create-menu (menu)
74   (mapcar (lambda (url)
75             (vector url (list 'browse-url url)))
76           riece-urls))
77             
78 (defvar riece-dialogue-mode-map)
79
80 (defun riece-url-requires ()
81   (append (if (memq 'riece-highlight riece-addons)
82               '(riece-highlight))
83           (if (memq 'riece-menu riece-addons)
84               '(riece-menu))))
85
86 (defun riece-url-insinuate ()
87   (add-hook 'riece-after-insert-functions 'riece-url-scan-region)
88   (if (memq 'riece-menu riece-addons)
89       (add-hook 'riece-command-mode-hook
90                 (lambda ()
91                   (easy-menu-add-item
92                    nil (list (car riece-menu-items))
93                    '("Open URL..." :filter riece-url-create-menu)))
94                 t)))
95
96 (defun riece-url-enable ()
97   (define-key riece-dialogue-mode-map "U" 'riece-command-browse-url)
98   (setq riece-url-enabled t))
99
100 (defun riece-url-disable ()
101   (define-key riece-dialogue-mode-map "U" nil)
102   (setq riece-url-enabled nil))
103   
104 (provide 'riece-url)
105
106 ;;; riece-url.el ends here