Subject: bug#16225: 24.3.50; [PATCH] eww: machinery to set character encoding.

From: Kenjiro NAKAYAMA <[email-masked]>
Date: Wed, 25 Dec 2013 16:32:51 +0900

> However, the implementation doesn't seem ideal.  The best way to handle
> this would be to have the `E' command prompt for the charset, and then
> re-render the page immediately without setting any global variables.

Thank you Lars,

I fixed it. Can you please review again? I send the patch again.

Signed-off-by: Kenjiro NAKAYAMA <[email-masked]>

        * net/eww.el (eww-display-html,eww-display-raw): Change to
          enable to set encoding type.
          (eww-mode-map): New key map and easy-menu to set encoding type.
          (eww-set-character-encoding): New funtion to set encoding type.

---
 lisp/net/eww.el | 43 ++++++++++++++++++++++++++++++-------------
 1 file changed, 30 insertions(+), 13 deletions(-)

diff --git a/lisp/net/eww.el b/lisp/net/eww.el
index 02c93a0..8fbf94d 100644
--- a/lisp/net/eww.el
+++ b/lisp/net/eww.el
@@ -172,7 +172,7 @@ word(s) will be searched for via `eww-search-prefix'."
 		    "/")
 	       (expand-file-name file))))
 
-(defun eww-render (status url &optional point)
+(defun eww-render (status url &optional point encode)
   (let ((redirect (plist-get status :redirect)))
     (when redirect
       (setq url redirect)))
@@ -192,7 +192,7 @@ word(s) will be searched for via `eww-search-prefix'."
 		    (or (cdr (assq 'charset (cdr content-type)))
 			(eww-detect-charset (equal (car content-type)
 						   "text/html"))
-			"utf8"))))
+			"utf-8"))))
 	 (data-buffer (current-buffer)))
     (unwind-protect
 	(progn
@@ -203,12 +203,12 @@ word(s) will be searched for via `eww-search-prefix'."
                                  (car content-type)))
             (eww-browse-with-external-browser url))
 	   ((equal (car content-type) "text/html")
-	    (eww-display-html charset url nil point))
+	    (eww-display-html charset url nil point encode))
 	   ((string-match-p "\\`image/" (car content-type))
 	    (eww-display-image)
 	    (eww-update-header-line-format))
 	   (t
-	    (eww-display-raw)
+	    (eww-display-raw encode)
 	    (eww-update-header-line-format)))
 	  (setq eww-current-url url
 		eww-history-position 0))
@@ -243,12 +243,12 @@ word(s) will be searched for via `eww-search-prefix'."
 (declare-function libxml-parse-html-region "xml.c"
 		  (start end &optional base-url))
 
-(defun eww-display-html (charset url &optional document point)
+(defun eww-display-html (charset url &optional document point encode)
   (or (fboundp 'libxml-parse-html-region)
       (error "This function requires Emacs to be compiled with libxml2"))
-  (unless (eq charset 'utf8)
+  (unless (eq charset encode)
     (condition-case nil
-	(decode-coding-region (point) (point-max) charset)
+	(decode-coding-region (point) (point-max) encode)
       (coding-system-error nil)))
   (let ((document
 	 (or document
@@ -363,11 +363,16 @@ word(s) will be searched for via `eww-search-prefix'."
 				  (list :background (car new-colors))
 				  t))))))
 
-(defun eww-display-raw ()
+(defun eww-display-raw (&optional encode)
   (let ((data (buffer-substring (point) (point-max))))
     (eww-setup-buffer)
     (let ((inhibit-read-only t))
-      (insert data))
+      (insert data)
+      (unless (eq encode 'utf-8)
+	(encode-coding-region (point-min) (1+ (length data)) 'utf-8)
+	(condition-case nil
+	    (decode-coding-region (point-min) (1+ (length data)) encode)
+	  (coding-system-error nil))))
     (goto-char (point-min))))
 
 (defun eww-display-image ()
@@ -420,6 +425,7 @@ word(s) will be searched for via `eww-search-prefix'."
     (define-key map "C" 'url-cookie-list)
     (define-key map "v" 'eww-view-source)
     (define-key map "H" 'eww-list-histories)
+    (define-key map "E" 'eww-set-character-encoding)
 
     (define-key map "b" 'eww-add-bookmark)
     (define-key map "B" 'eww-list-bookmarks)
@@ -442,7 +448,8 @@ word(s) will be searched for via `eww-search-prefix'."
 	["List histories" eww-list-histories t]
 	["Add bookmark" eww-add-bookmark t]
 	["List bookmarks" eww-list-bookmarks t]
-	["List cookies" url-cookie-list t]))
+	["List cookies" url-cookie-list t]
+	["Character Encoding" eww-set-character-encoding]))
     map))
 
 (defvar eww-tool-bar-map
@@ -552,11 +559,11 @@ appears in a <link> or <a> tag."
 	(eww-browse-url (shr-expand-url best-url eww-current-url))
       (user-error "No `top' for this page"))))
 
-(defun eww-reload ()
+(defun eww-reload (&optional encode)
   "Reload the current page."
   (interactive)
   (url-retrieve eww-current-url 'eww-render
-		(list eww-current-url (point))))
+		(list eww-current-url (point) encode)))
 
 ;; Form support.
 
@@ -1032,7 +1039,7 @@ If EXTERNAL, browse the URL using `shr-external-browser'."
      ((and (url-target (url-generic-parse-url url))
 	   (eww-same-page-p url eww-current-url))
       (eww-save-history)
-      (eww-display-html 'utf8 url eww-current-dom))
+      (eww-display-html 'utf-8 url eww-current-dom))
      (t
       (eww-browse-url url)))))
 
@@ -1083,6 +1090,16 @@ Differences in #targets are ignored."
 	(setq count (1+ count)))
       (expand-file-name file directory)))
 
+(defun eww-set-character-encoding (encode)
+  "Set character encoding."
+  (interactive "sSet Character Encoding (default utf-8): ")
+  (cond ((zerop (length encode))
+	 (eww-reload 'utf-8))
+	(t
+	 (if (not (coding-system-p (intern encode)))
+	     (user-error "Invalid encodeing type.")
+	   (eww-reload (intern encode))))))
+
 ;;; Bookmarks code
 
 (defvar eww-bookmarks nil)
-- 
1.8.3.1

Regards,

Kenjiro

[email-masked] writes:

> Kenjiro NAKAYAMA <[email-masked]> writes:
>
>> This report includes a patch to the bug. Please, review and install it
>> to the official tree if appreciated.
>>
>> The user can't change encoding type. This patch is to fix it.
>
> Allowing the user to alter the encoding seems like a good idea.
> However, the implementation doesn't seem ideal.  The best way to handle
> this would be to have the `E' command prompt for the charset, and then
> re-render the page immediately without setting any global variables.