;; -*- emacs-lisp -*-
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; $Id: emacs_w3m.el,v 1.11 2006-11-06 21:02:42 rscholz Exp $
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Einstellungen für den w3m-Mode
;;
;; Die offizielle Quelle dieser Datei ist
;;   <http://www.zonix.de/projects/emacs/config>
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Emacs-w3m <http://emacs-w3m.namazu.org/>
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(add-to-list 'load-path (expand-file-name (concat zonix-elisp-dir "w3m")))

(autoload 'w3m "w3m" "Interface for w3m on Emacs." t)
(autoload 'w3m-browse-url "w3m" "w3m interface function for browse-url.el." t)
(autoload 'w3m-find-file "w3m" "w3m Interface function for local file." t)

(setq w3m-coding-system 'iso-8859-1
      w3m-input-coding-system 'iso-8859-1
      w3m-output-coding-system 'iso-8859-1
      w3m-arrived-file-coding-system 'iso-8859-1
      w3m-search-default-coding-system 'iso-8859-1
      w3m-terminal-coding-system 'iso-8859-1
      w3m-file-name-coding-system 'iso-8859-1

      w3m-use-mule-ucs t
      
      w3m-mailto-url-function 'compose-mail
      
      w3m-default-save-directory "~/tmp/down/"
      w3m-icon-directory (expand-file-name (concat zonix-elisp-dir "w3m/icons"))

      w3m-key-binding 'info
      
      w3m-use-form t
      w3m-use-filter t
      w3m-delete-duplicated-empty-lines t
      w3m-async-exec t

      w3m-fill-column 99
      
      )

(standard-display-ascii ?\212 "-")
(standard-display-ascii ?\226 "-")

;; Emacs 21
(when (>= emacs-major-version 21) 
  (setq w3m-display-inline-image t))

(require 'w3m)

;; (defun widget-button-press (pos &optional event)
;;   "Invoke button at POS."
;;   (interactive "@d")
;;   (if (w3m-anchor)
;;       (w3m-view-this-url)
;;     (let ((button (get-char-property pos 'button)))
;;       (if button
;;        (widget-apply-action button event)
;;      (let ((command (lookup-key widget-global-map (this-command-keys))))
;;        (when (commandp command)
;;          (call-interactively command)))))))

;; Christoph Conrad in <9lenld$9ej3v$1@ID-24456.news.dfncis.de>
;; For more beautiful frameborders i use:
(defun cc-w3m-beautify-tables()
  (goto-char (point-min))
  (while (re-search-forward
          "\200\\|\203\\|\206\\|\211\\|\214\\|\202\\|\204\\|\210\\|\201" nil t)
    (replace-match "+"))
  (goto-char (point-min))
  (while (re-search-forward "\205" nil t)
    (replace-match "|"))
  (goto-char (point-min))
  (while (re-search-forward "\212" nil t)
    (replace-match "-")))

(add-hook 'w3m-fontify-after-hook 'cc-w3m-beautify-tables)

;;Removing trailing whitespaces (better display):       
(defun cc-w3m-del-trailing-ws()
  (goto-char (point-min))
  (while (re-search-forward "[ \t]+$" nil t)
    (replace-match "")))

(add-hook 'w3m-fontify-after-hook 'cc-w3m-del-trailing-ws)