Importing Liece 1.4.3.
[elisp/liece.git] / lisp / liece-url.el
1 ;;; liece-url.el --- URL browsing.
2 ;; Copyright (C) 1998-2000 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Created: 1998-09-28
6 ;; Revised: 1998-11-25
7 ;; Keywords: IRC, liece
8
9 ;; This file is part of Liece.
10
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26
27 ;;; Commentary:
28 ;; 
29
30 ;;; Code:
31
32 (eval-when-compile
33   (require 'liece-inlines)
34   (require 'liece-intl)
35   (require 'browse-url)
36   (require 'liece-menu))
37
38 (defvar-maybe browse-url-browser-function nil)
39
40 (defgroup liece-url nil
41   "URL Browsing in IRC buffer."
42   :group 'liece-vars)
43
44 (defcustom liece-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_=#$@~`%&*+|\\/]"
45   "Regular expression that matches URLs."
46   :group 'liece-url
47   :type 'regexp)
48
49 (defcustom liece-url-mail-regexp "\\bmailto:\\([-a-zA-Z0-9_=?#$@~`%&*+|\\/.,]*[-a-zA-Z0-9_=#$@~`%&*+|\\/]\\)"
50   "Regular expression that matches mailto:."
51   :group 'liece-url
52   :type 'regexp)
53
54 (defcustom liece-url-browser-name
55   (let (url-func url-prog len)
56     (if (featurep 'browse-url)
57         (and (setq url-func browse-url-browser-function)
58              (setq url-prog (symbol-name url-func))
59              (stringp url-prog)
60              (setq len (length url-prog))
61              (setq url-prog (substring url-prog 11 len)))
62       "netscape"))
63   "Default URL Browser Name.
64 netscape   Netscape    1.1b1
65 mosaic     XMosaic     <= 2.4
66 cci        XMosaic     2.5
67 w3         w3          0
68 iximosaic  IXI Mosaic  ?
69 lynx-*     Lynx        0
70 grail      Grail       0.3b1"
71   :type 'string
72   :group 'liece-url)
73
74 (defvar liece-url-add-hook nil)
75
76 (defmacro liece-url-prepare-browser-function ()
77   (if (featurep 'browse-url)
78       '(intern (concat "browse-url-" liece-url-browser-name))
79     '(intern (concat "liece-url-browser-" liece-url-browser-name))))
80
81 (defcustom liece-url-browser-function
82   (liece-url-prepare-browser-function)
83   "Default URL Browser."
84   :type 'function
85   :group 'liece-url)
86
87 (defcustom liece-url-alist nil
88   "Alist for URL completion."
89   :type 'alist
90   :group 'liece-url)
91
92 (define-widget 'liece-url-link 'link
93   "A link to an www page."
94   :help-echo 'widget-url-link-help-echo
95   :action 'liece-url-link-action)
96
97 (defun liece-url-link-action (widget &optional event)
98   (let ((func (liece-url-prepare-browser-function)))
99     (if (fboundp func)
100         (setq liece-url-browser-function func)
101       (setq func liece-url-browser-function))
102     (funcall func (widget-value widget))))
103
104 (defun liece-url-add-buttons (start end)
105   (save-excursion
106     (goto-char start)
107     (while (re-search-forward liece-url-regexp end t)
108       (let* ((url-start (match-beginning 0))
109              (url-end (match-end 0))
110              (url (buffer-substring url-start url-end)))
111         (if liece-highlight-mode
112             (liece-widget-convert-button 'url-link url-start url-end url))
113         (unless (assoc url liece-url-alist)
114           (push (list url) liece-url-alist)
115           (run-hook-with-args 'liece-url-add-hook url))))))
116
117 (defun liece-command-browse-url (&optional url)
118   (interactive
119    (let (url)
120      (if (and current-prefix-arg (eq current-prefix-arg '-))
121          (setq url (caar liece-url-alist))
122        (setq url (liece-minibuffer-completing-default-read
123                   (_ "Open URL:") liece-url-alist)))
124      (list url)))
125   (let ((func (liece-url-prepare-browser-function)))
126     (if (fboundp func)
127         (setq liece-url-browser-function func)
128       (setq func liece-url-browser-function))
129     (funcall func url)))
130
131 (defun url-irc-liece (host port channel user password)
132   (let ((liece-server
133          `(:host ,host :service ,port :password ,password))
134         (liece-startup-channel channel))
135     (liece)))
136
137 (provide 'liece-url)
138
139 ;;; liece-url.el ends here