[Date Prev][Date Next][Thread Prev][][Date Index][Thread Index]

Re: w3m-toggle-inline-image needs cursor on image, even for a region



青田です。

Katsumi Yamaoka <yamaoka@xxxxxxx> writes:

>>>>>> In [emacs-w3m : No.10259] jidanni@xxxxxxxxxxx wrote:
>
>> t (w3m-toggle-inline-image) is nice, and can toggle off all images in
>> a region (except ones that have been toggled on individually).
>> However it cannot toggle on all images in a region unless the cursor
>> is on an image!
>
> (日本語なので宛先に jidanni さんを含めていません。)
>
> 領域を指定して t コマンドを実行したときに、領域の最後が画像アン
> カーの中に置かれていると、その画像を 2回トグルするので目に見える
> 変化が起きないという話。パッチ-1 でいかがでしょう?

いいと思います。 ただ、画像領域の先端にカーソルがある時にも toggle-list
に画像をいれてしまうのが少し気になります。

> これに関連して、領域を指定して T (w3m-toggle-inline-images) を実
> 行すると、実行後に領域の開始点が window-start になってしまいます。
> これもテキトーに対策してみたんですが、見栄えがよろしくありません。
> → パッチ-2
> あまり考えていないんですが、narrowing せずにはできないかなあ。

narrowing なしの解法を考えてみました。 ついでに以下のこともしています。
(というか、そっちがメインになってしまいました ^^;)

- w3m-toggle-inline-images(), w3m-toggle-inline-image() のコード整理
- region つき w3m-toggle-inline-image() は本当に region 内の画像だけを
  toggle するように (従来は region 外の画像も region 内に同じ画像があれ
  ば、その画像の状態によって toggle していました)
- w3m-toggle-inline-images() が w3m-display-inline-images => t, force =>
  t で呼ばれた時にも、安全性の確認をするように。

> さらにもう一つ。領域を指定して t コマンドを実行すると XEmacs で
> は無限ループになってしまいます (実際には繰り返し回数の制限にひっ
> かかってエラーで止まります)。たぶんパッチ-3 でいけます。

下のパッチで w3m-toggle-inline-image() の再帰呼出しはなくしましたが、一応
こちらもお願いします。

--
青田
--- w3m.el.~1.1372.~	2008-06-19 21:12:05.000000000 +0900
+++ w3m.el	2008-06-25 08:01:07.000000000 +0900
@@ -3573,7 +3573,7 @@
 						 'help-echo help
 						 'balloon-help balloon))))))))
 
-(defsubst w3m-toggle-inline-images-internal (status no-cache url)
+(defsubst w3m-toggle-inline-images-internal (status &optional no-cache url begin-pos end-pos)
   "Toggle displaying of inline images on current buffer.
 STATUS is current image status.
 If NO-CACHE is non-nil, cache is not used.
@@ -3581,14 +3581,16 @@
   (interactive "P")
   (let ((cur-point (point))
 	(buffer-read-only)
-	(end (point-min))
+	(end (or begin-pos (point-min)))
 	start iurl image size)
+    (unless end-pos (setq end-pos (point-max)))
     (save-excursion
       (if (equal status 'off)
-	  (while (setq start
-		       (if (w3m-image end)
-			   end
-			 (next-single-property-change end 'w3m-image)))
+	  (while (< (setq start
+			  (if (w3m-image end)
+			      end
+			    (next-single-property-change end 'w3m-image nil end-pos)))
+		    end-pos)
 	    (setq end (or (next-single-property-change start 'w3m-image)
 			  (point-max))
 		  iurl (w3m-image start)
@@ -3647,9 +3649,10 @@
 			  (set-marker start nil)
 			  (set-marker end nil)))))))))
 	;; Remove.
-	(while (setq start (if (w3m-image end)
-			       end
-			     (next-single-property-change end 'w3m-image)))
+	(while (< (setq start (if (w3m-image end)
+				  end
+				(next-single-property-change end 'w3m-image nil end-pos)))
+		  end-pos)
 	  (setq end (or (next-single-property-change start 'w3m-image)
 			(point-max))
 		iurl (w3m-image start))
@@ -3681,46 +3684,52 @@
   (interactive "P")
   (unless (w3m-display-graphic-p)
     (error "Can't display images in this environment"))
-  (if (w3m-region-active-p)
-      (let ((p (region-beginning))
-	    (end (region-end))
-	    iurl toggle-list)
-	(w3m-deactivate-region)
-	(while (not (eq end
-			(setq p (next-single-property-change
-				 p 'w3m-image nil end))))
-	  (when (and (setq iurl (w3m-image p))
-		     (not (assoc iurl toggle-list)))
-	    (setq toggle-list (cons (cons iurl p) toggle-list))))
-	(when (w3m-image end)
-	  (setq toggle-list (cons (cons iurl end) toggle-list)))
-	(save-excursion
-	  (dolist (item toggle-list)
-	    (goto-char (cdr item))
-	    (w3m-toggle-inline-image force no-cache))))
-    (let ((url (w3m-image))
-	  (status (get-text-property (point) 'w3m-image-status))
-	  safe-regexp)
-      (if (and (get-text-property (point) 'w3m-image-scale) (equal status 'off))
-	  (w3m-zoom-in-image 0)
-	(if (w3m-url-valid url)
-	    (if (eq status 'on)
-		(progn
-		  (if force (setq status 'off))
-		  (w3m-toggle-inline-images-internal status no-cache url))
-	      (setq safe-regexp
-		    (get-text-property (point) 'w3m-safe-url-regexp))
-	      (if (or (not safe-regexp)
-		      (string-match safe-regexp url)
-		      (and force
-			   (or (not (interactive-p))
-			       (yes-or-no-p "\
+  (let (toggle-list begin end)
+    (if (w3m-region-active-p)
+	(let ((p (region-beginning))
+	    iurl)
+	  (setq begin (region-beginning)
+		end (region-end))
+	  (w3m-deactivate-region)
+	  (while (< p end)
+	    (setq p (next-single-property-change p 'w3m-image nil end))
+	    (when (and (setq iurl (w3m-image p))
+		       (not (assoc iurl toggle-list)))
+	    (setq toggle-list (cons (cons iurl p) toggle-list)))))
+      (setq toggle-list (and (w3m-image)
+			     `(,(cons (w3m-image) (point))))))
+    (if toggle-list
+	(dolist (x toggle-list)
+	  (let* ((url (car x))
+		 (pos (cdr x))
+		 (status (get-text-property pos 'w3m-image-status))
+		 safe-regexp)
+	    (if (and (get-text-property pos 'w3m-image-scale) (equal status 'off))
+		(w3m-zoom-in-image 0)
+	      (if (w3m-url-valid url)
+		  (if (eq status 'on)
+		      (progn
+			(if force (setq status 'off))
+			(w3m-toggle-inline-images-internal status no-cache url 
+							   (or begin (point-min))
+							   (or end (point-max))))
+		    (setq safe-regexp
+			  (get-text-property (point) 'w3m-safe-url-regexp))
+		    (if (or (not safe-regexp)
+			    (string-match safe-regexp url)
+			    (and force
+				 (or (not (interactive-p))
+				     (yes-or-no-p "\
 Are you sure you really want to show this image (maybe insecure)? "))))
-		  (w3m-toggle-inline-images-internal status no-cache url)
-		(when (interactive-p)
-		  (w3m-message "\
-This image is considered to be unsafe; use the prefix arg to force display"))))
-	  (w3m-message "No image at point"))))))
+			(w3m-toggle-inline-images-internal status no-cache url 
+							   (or begin (point-min))
+							   (or end (point-max)))
+		      (when (interactive-p)
+			(w3m-message "\
+This image is considered to be unsafe; use the prefix arg to force display"))))))))
+      (if begin
+	  (w3m-message "No images in region")
+	(w3m-message "No image at point")))))
 
 (defun w3m-turnoff-inline-images ()
   "Turn off to display all images in the buffer or in the region."
@@ -3739,25 +3748,22 @@
   (interactive "P")
   (unless (w3m-display-graphic-p)
     (error "Can't display images in this environment"))
-  (let* ((turnoff (eq force 'turnoff))
-	 (status (or w3m-display-inline-images turnoff))
-	 (safe-p t)
-	 safe-regexp pos url)
-    (if turnoff (setq force nil))
-    (if status
+  (let ((status (cond ((eq force 'turnoff) t)
+		      (force nil)
+		      (t w3m-display-inline-images)))
+	(safe-p t)
+	beg end safe-regexp pos url)
+    (if (w3m-region-active-p)
 	(progn
-	  (if force (setq status nil))
-	  (unwind-protect
-	      (w3m-toggle-inline-images-internal (if status 'on 'off)
-						 no-cache nil)
-	    (unless (setq w3m-display-inline-images (not status))
-	      (w3m-process-stop (current-buffer))))
-	  (force-mode-line-update))
-      (when (w3m-region-active-p)
-	(narrow-to-region (region-beginning) (region-end)))
+	  (setq beg (region-beginning)
+		end (region-end))
+	  (w3m-deactivate-region))
+      (setq beg (point-min)
+	    end (point-max)))
+    (unless status
       (when (setq safe-regexp (get-text-property (point) 'w3m-safe-url-regexp))
 	;; Scan the buffer for searching for an insecure image url.
-	(setq pos (point-min))
+	(setq pos beg)
 	(setq
 	 safe-p
 	 (catch 'done
@@ -3765,27 +3771,29 @@
 	     (unless (string-match safe-regexp url)
 	       (throw 'done nil))
 	     (setq pos (next-single-property-change pos 'w3m-image)))
-	   (while (and pos
-		       (setq pos (next-single-property-change pos 'w3m-image))
-		       (setq url (get-text-property pos 'w3m-image)))
-	     (unless (string-match safe-regexp url)
-	       (throw 'done nil))
-	     (setq pos (next-single-property-change pos 'w3m-image)))
-	   t)))
-      (if (or (not safe-regexp)
-	      safe-p
-	      (and force
-		   (or (not (interactive-p))
-		       (yes-or-no-p "\
+	   (while (< pos end)
+	     (when (and (setq pos (next-single-property-change pos 'w3m-image end))
+			(setq url (get-text-property pos 'w3m-image)))
+	       (unless (string-match safe-regexp url)
+		 (throw 'done nil)))
+	     (setq pos (next-single-property-change pos 'w3m-image end)))
+	   t))))
+    (if (or status
+	    (not safe-regexp)
+	    safe-p
+	    (and force
+		 (or (not (interactive-p))
+		     (yes-or-no-p "\
 Are you sure you really want to show all images (maybe insecure)? "))))
-	  (progn
-	    (unwind-protect
-		(w3m-toggle-inline-images-internal 'off no-cache nil)
-	      (widen)
-	      (setq w3m-display-inline-images t))
-	    (force-mode-line-update))
-	(w3m-message "There are some images considered unsafe;\
- use the prefix arg to force display")))))
+	(progn
+	  (unwind-protect
+	      (w3m-toggle-inline-images-internal (if status 'on 'off)
+						 no-cache nil beg end)
+	    (setq w3m-display-inline-images (not status))
+	    (when status (w3m-process-stop (current-buffer)))
+	    (force-mode-line-update)))
+      (w3m-message "There are some images considered unsafe;\
+ use the prefix arg to force display"))))
 
 (defsubst w3m-resize-inline-image-internal (url rate)
   "Resize an inline image on the cursor position.