;; -*- emacs-lisp -*-
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; $Id: www.el,v 1.15 2006/03/06 12:07:06 ole Exp $
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;_*; Wget
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(autoload 'wget "wget" "Fetch an URL via wget" t)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;_* W3M
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


(defconst w3m-package-dir (expand-file-name "emacs-w3m/" emacs-packages-dir)
  "Where the w3m package lives")
(add-to-list 'load-path w3m-package-dir)
(setq w3m-icon-directory (expand-file-name "icons" w3m-package-dir))

;; (autoload 'w3m "w3m" "Interface for w3m on Emacsen." t)
;; (autoload 'w3m-find-file "w3m" "w3m interface function for local file." t)
;; (autoload 'w3m-browse-url "w3m" "Ask a WWW browser to show a URL." t)
;; (autoload 'w3m-search "w3m-search" "Search QUERY using SEARCH-ENGINE." t)
;; (autoload 'w3m-weather "w3m-weather" "Display weather report." t)
;; (autoload 'w3m-antenna "w3m-antenna" "Report change of WEB sites." t)

(require 'w3m)
(require 'w3m-type-ahead)

(setq w3m-fill-column 100
      w3m-mailto-url-function 'message-mail
      w3m-home-page "http://localhost/~ole/sugarshark"
      w3m-command "/usr/bin/w3m"
      w3m-use-cygdrive nil
      w3m-use-cookies t
      )

(defun browse-url-at-mouse-external (event)
  "browse url with windows browser"
  (interactive "e")
  (let ((browse-url-browser-function 'browse-url-mozilla))
    (browse-url-at-mouse event)))

(global-set-key [S-down-mouse-1] 'browse-url-at-mouse-external)
(global-set-key [C-down-mouse-1] 'ffap-at-mouse)

(defun my-w3m-edit-current-url ()
  (interactive)
  (condition-case nil
      (wiki-remote-get-w3m)
    (error (w3m-edit-current-url))))

(defun my-w3m-mode-hook()
  "Do some things when entering w3m"
  (define-key w3m-mode-map [?/] 'w3m-type-ahead)
  (define-key w3m-mode-map [(control ?/)] 'w3m-type-ahead-new-session)
  (define-key w3m-mode-map [?j] 'browse-query)
  (define-key w3m-mode-map [?e] 'my-w3m-edit-current-url))

(defun my-w3m-minormode-hook()
  "Do some things when entering w3m minor mode"
  (define-key w3m-minor-mode-map [(control return)]
    'ole-w3m-this-url-in-mozilla))

(defun ole-w3m-this-url-in-mozilla ()
  (interactive)
  (let ((url (or (w3m-anchor) (w3m-image))))
    (if url (browse-url-mozilla url)
      (message "No URL at point."))))

(add-hook 'w3m-mode-hook 'my-w3m-mode-hook)
(add-hook 'w3m-minor-mode-hook 'my-w3m-minor-mode-hook)



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;_* Browse-Url
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;:*==============================================================
;; save url in a bookmarkfile
;:*==============================================================
(defvar url-stash-file (expand-file-name "~/News/bookmarks.html")
  "File to in which store URLs that are clicked upon.")

(defun local-browse-url-grab-n-save (url &rest ARGS)
  "Given a URL sucked up from a buffer, switch to the saved-url file and insert it as a hyperlink"
  (save-excursion
    (let ((urlbuf (find-file-noselect url-stash-file)))
      (set-buffer urlbuf)
      (goto-char (point-min))
      (search-forward "</UL>" nil t)
      (beginning-of-line 2) (open-line 1)
      (insert (format "<LI><A href=\"%s\">%s</a>" url url))
      (save-buffer)
      (message "Stashed url: %s" url)
      (browse-url-mozilla url)
      )))

(setq browse-url-mozilla-program "/usr/bin/firefox")

(defun w3m-browse-url-other-window (url &optional newwin)
  (interactive "s")
  (switch-to-buffer-other-window "*scratch*" t)
  (w3m-browse-url url newwin)
  (other-window 1))

(setq browse-url-browser-function 
      '(
;;      ("jde_meta" . w3m-browse-url-other-window)
        (".*" . w3m-browse-url-other-window)
))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;_ + Search engines
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defvar browse-query-search-engines
  '(("google" . "http://www.google.com/search?&q=%s")
    ("google-images" . "http://images.google.com/images?q=%s")
    ("google-groups" . "http://groups.google.com/groups?q=%s")
    ("google-news" ."http://news.google.com/news?q=%s")
    ("freedb" . "http://www.freedb.org/freedb_search.php?words=%s")
    ("freshmeat" . "http://freshmeat.net/search/?q=%s")
    ("imdb" . "http://www.imdb.com/find?q=%s")
    ("emacswiki" . "http://www.emacswiki.org/cgi-bin/wiki/%s")
    ("emacswikisearch" . "http://www.emacswiki.org/cgi-bin/wiki?search=%s")
    ("leo" . "http://dict.leo.org/?search=%s")
    ("rfc" . "http://www.ietf.org/rfc/rfc%s.txt")
    ("jdocs" . "http://www.jdocs.com/apis.jsp")
    ("sourceforge" . "http://www.sourceforge.net/projects/%s")
    ("sf.net" . "http://%s.sourceforge.net")
    ("vstrom" . "http://www.sub3-forennet.de/cgi-sub3/v-strom/YaBB.pl")
    ("javagaming" . "http://www.javagaming.org/cgi-bin/JGNetForums/YaBB.cgi")
    ("sharereactor" . "http://www.sharereactor.com/search.php?search=%s")))

(defun browse-query (engine &optional query)
   "Browse the result of querying a search ENGINE for QUERY.
ENGINE must be a key into the `browse-query-search-engines' alist.
QUERY is substituted for \"%s\" in the keyed value, to generate a URL."
   (interactive (list (completing-read "URL: " browse-query-search-engines nil nil)))
   (let* ((url (or (cdr (assoc engine browse-query-search-engines))
                        (if (string= "" engine) (browse-url-url-at-point) engine)))
          (topic (or query (and url (string-match "%s" url)
                                (read-from-minibuffer "Query: " (thing-at-point 'word))))))
     (if url
         (browse-url (format url topic))
       (error "No such URL: %s" engine))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;_ + Keys
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar www-keymap (make-sparse-keymap "WWW")
  "Keymap used to globally access web related functionality.")
(define-key mode-specific-map [?v] www-keymap)

(define-key www-keymap [?v] 'browse-query)
(define-key www-keymap [?b] 'browse-url-of-buffer)
(define-key www-keymap [?f] 'browse-url-of-file)
(define-key www-keymap [?r] 'browse-url-of-region)
(define-key www-keymap [?c] 'browse-url-at-point)
(define-key www-keymap [?m] 'browse-url-mozilla)
(define-key www-keymap [?k] 'browse-url-kde)
(define-key www-keymap [?w] 'w3m-browse-url)

(define-key goto-keymap [?w] 'w3m)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;_* Dictionary
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defvar lookup-keymap (make-sparse-keymap "Lookup")
  "Keymap used to globally access lookup functionality.")
(define-key mode-specific-map [?l] lookup-keymap)

(define-key lookup-keymap [?l] 'dictionary-lookup-definition)
(define-key lookup-keymap [?s] 'dictionary-search)
(define-key lookup-keymap [?m] 'dictionary-match-words)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;_* Whois
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(setq whois-server-name "whois.thur.de")
(setq whois-guess-server nil)