;;; browse-url-mozilla.el --- browse-url for mozilla

;; Copyright (C) 2000 Free Software Foundation, Inc.

;; Author: Kenichi OKADA <okada@opaopa.org>
;; Keywords: mozilla

;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2, or (at
;; your option) any later version.

;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Commentary:

;;; Code:

(defvar browse-url-mozilla-program "gnome-moz-remote")
(defvar browse-url-mozilla-arguments nil)
(defvar browse-url-mozilla-startup-arguments nil)

(defun browse-url-mozilla (url &optional new-window)
  (interactive (browse-url-interactive-arg "Mozilla URL: "))
  (while (string-match "[,)]" url)
    (setq url (replace-match
	       (format "%%%x" (string-to-char (match-string 0 url))) t t url)))
  (let* ((process-environment (browse-url-process-environment))
         (process (apply 'start-process
			 (concat "mozilla " url) nil
			 browse-url-mozilla-program
			 (append
			  browse-url-mozilla-arguments
			  (if (eq (console-type) 'mswindows)
			      (list url)
			    (append
			     (if new-window '("-noraise"))
			     (list "--remote"
				   (concat "openURL(" url
					   (if new-window ",new-window")
					   ")"))))))))
    (set-process-sentinel process
			  (list 'lambda '(process change)
				(list 'browse-url-mozilla-sentinel 'process url)))))

(defun browse-url-mozilla-sentinel (process url)
  "Handle a change to the process communicating with Mozilla."
  (or (eq (process-exit-status process) 0)
      (let* ((process-environment (browse-url-process-environment)))
	;; Mozilla not running - start it
	(message "Starting Mozilla...")
	(apply 'start-process (concat "mozilla" url) nil
	       browse-url-mozilla-program
	       (append browse-url-mozilla-startup-arguments (list url))))))


(provide 'browse-url-mozilla)

;;; brose-url-mozilla.el ends here