unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#69232: 30.0.50; [PATCH] EWW history navigation gets caught in a loop
@ 2024-02-18  4:59 Jim Porter
  2024-02-19 12:12 ` Eli Zaretskii
  0 siblings, 1 reply; 30+ messages in thread
From: Jim Porter @ 2024-02-18  4:59 UTC (permalink / raw)
  To: 69232

[-- Attachment #1: Type: text/plain, Size: 1251 bytes --]

If you navigate back in EWW history, and then forward, you can never hit 
the "end": it keeps adding duplicate history elements, even though 
you're not visiting any new pages. To see this in action, start from 
`emacs -Q`, then:

M-x eww RET fsf.org RET
M-x eww RET gnu.org RET
H    ;; Notice that there's one item in the history: the FSF page[1]
q    ;; Close history window
l    ;; Go back one in the history to the FSF page
H    ;; Notice that there are two items in the history
r    ;; Go forward one, back at the GNU page
r    ;; Go forward again, now at the FSF page(?!)
r    ;; Ditto, now at the GNU page
r    ;; Repeat ad infinitum
H    ;; Now there are many entries, alternating between GNU and FSF

Attached is a patch that fixes this. Now, 'eww-save-history' will update 
the history entry in-place when viewing a historical page, and 
'eww-back-url' / 'eww-forward-url' take that into account. I also fixed 
the predicates for when the back/forward menu items were enabled.

I think this is just a straightforward bug fix, so I didn't add a NEWS 
entry. I could add one though if it seems worthwhile.

[1] EWW doesn't immediately add pages to the history when you navigate 
to them. Maybe it should, but that can be addressed another day.

[-- Attachment #2: 0001-When-navigating-through-history-in-EWW-don-t-keep-ad.patch --]
[-- Type: text/plain, Size: 4439 bytes --]

From 2e926fdd83a2eb6b68cc21bf84ea778e923d3e55 Mon Sep 17 00:00:00 2001
From: Jim Porter <jporterbugs@gmail.com>
Date: Sat, 17 Feb 2024 20:49:15 -0800
Subject: [PATCH] When navigating through history in EWW, don't keep adding to
 'eww-history'

This resolves an issue where navigating back and then forward kept
adding new history entries so you could never hit the "end".

* lisp/net/eww.el (eww-history-position): Add docstring.
(eww-mode-map, eww-context-menu): Use correct predicates for when to
enable back/forward.
(eww-save-history): Save history entry in its original place when
viewing a historical page.
(eww-back-url): Set 'eww-history-position' based on the result of
'eww-save-history'.
(eww-forward-url): Set 'eww-history-position' directly, since
'eww-save-history' no longer adds a new entry in this case.
---
 lisp/net/eww.el | 39 ++++++++++++++++++++++++++++-----------
 1 file changed, 28 insertions(+), 11 deletions(-)

diff --git a/lisp/net/eww.el b/lisp/net/eww.el
index 6ae1e6d3d0a..8196f222ad8 100644
--- a/lisp/net/eww.el
+++ b/lisp/net/eww.el
@@ -312,7 +312,10 @@ eww-valid-certificate
 
 (defvar eww-data nil)
 (defvar eww-history nil)
-(defvar eww-history-position 0)
+(defvar eww-history-position 0
+  "The 1-indexed position in `eww-history'.
+If zero, EWW is at the newest page, which isn't yet present in
+`eww-history'.")
 (defvar eww-prompt-history nil)
 
 (defvar eww-local-regex "localhost"
@@ -1129,9 +1132,9 @@ eww-mode-map
           ["Reload" eww-reload t]
           ["Follow URL in new buffer" eww-open-in-new-buffer]
           ["Back to previous page" eww-back-url
-           :active (not (zerop (length eww-history)))]
+           :active (< eww-history-position (length eww-history))]
           ["Forward to next page" eww-forward-url
-           :active (not (zerop eww-history-position))]
+           :active (> eww-history-position 1)]
           ["Browse with external browser" eww-browse-with-external-browser t]
           ["Download" eww-download t]
           ["View page source" eww-view-source]
@@ -1155,9 +1158,9 @@ eww-context-menu
     (easy-menu-define nil easy-menu nil
       '("Eww"
         ["Back to previous page" eww-back-url
-	 :visible (not (zerop (length eww-history)))]
+	 :active (< eww-history-position (length eww-history))]
 	["Forward to next page" eww-forward-url
-	 :visible (not (zerop eww-history-position))]
+	 :active (> eww-history-position 1)]
 	["Reload" eww-reload t]))
     (dolist (item (reverse (lookup-key easy-menu [menu-bar eww])))
       (when (consp item)
@@ -1280,16 +1283,20 @@ eww-back-url
   (interactive nil eww-mode)
   (when (>= eww-history-position (length eww-history))
     (user-error "No previous page"))
-  (eww-save-history)
-  (setq eww-history-position (+ eww-history-position 2))
+  (if (eww-save-history)
+      ;; We were at the latest page (which was just added to the
+      ;; history), so go back two entries.
+      (setq eww-history-position 2)
+    (setq eww-history-position (1+ eww-history-position)))
   (eww-restore-history (elt eww-history (1- eww-history-position))))
 
 (defun eww-forward-url ()
   "Go to the next displayed page."
   (interactive nil eww-mode)
-  (when (zerop eww-history-position)
+  (when (<= eww-history-position 1)
     (user-error "No next page"))
   (eww-save-history)
+  (setq eww-history-position (1- eww-history-position))
   (eww-restore-history (elt eww-history (1- eww-history-position))))
 
 (defun eww-restore-history (elem)
@@ -2289,11 +2296,21 @@ eww-bookmark-mode
 ;;; History code
 
 (defun eww-save-history ()
+  "Save the current page's data to the history.
+If the current page is a historial one loaded from
+`eww-history' (e.g. by calling `eww-back-url'), this will update the
+page's entry in `eww-history' and return nil.  Otherwise, add a new
+entry to `eww-history' and return t."
   (plist-put eww-data :point (point))
   (plist-put eww-data :text (buffer-string))
-  (let ((history-delete-duplicates nil))
-    (add-to-history 'eww-history eww-data eww-history-limit t))
-  (setq eww-data (list :title "")))
+  (prog1
+      (if (zerop eww-history-position)
+          (let ((history-delete-duplicates nil))
+            (add-to-history 'eww-history eww-data eww-history-limit t)
+            t)
+        (setf (elt eww-history (1- eww-history-position)) eww-data)
+        nil)
+    (setq eww-data (list :title ""))))
 
 (defvar eww-current-buffer)
 
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 30+ messages in thread

end of thread, other threads:[~2024-03-07  0:26 UTC | newest]

Thread overview: 30+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-18  4:59 bug#69232: 30.0.50; [PATCH] EWW history navigation gets caught in a loop Jim Porter
2024-02-19 12:12 ` Eli Zaretskii
2024-02-19 18:55   ` Jim Porter
2024-02-19 19:17     ` Eli Zaretskii
2024-02-22 13:22     ` Eli Zaretskii
2024-02-22 17:18       ` Jim Porter
2024-02-22 19:04         ` Eli Zaretskii
2024-02-22 20:10           ` Jim Porter
2024-02-22 20:13             ` Eli Zaretskii
2024-02-24 14:15             ` James Thomas via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-02-24 14:20               ` Eli Zaretskii
2024-02-24 22:29                 ` Jim Porter
2024-02-25  0:50                   ` James Thomas via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-02-24 22:34                 ` James Thomas via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-02-25  5:57                   ` Eli Zaretskii
2024-02-25 19:40                     ` Jim Porter
2024-02-25 19:49                       ` Eli Zaretskii
2024-02-25 22:41                         ` Jim Porter
2024-02-28 23:39                           ` Jim Porter
2024-02-29  7:03                             ` Eli Zaretskii
2024-02-29 17:32                               ` Jim Porter
2024-02-29 23:30                                 ` James Thomas via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-03-01  1:00                                   ` Jim Porter
2024-03-01  2:10                                     ` Jim Porter
2024-03-01  7:26                                       ` Eli Zaretskii
2024-03-01 20:13                                         ` Jim Porter
2024-03-02  7:38                                           ` Eli Zaretskii
2024-03-07  0:26                                             ` Jim Porter
2024-03-01  8:50                                       ` James Thomas via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-03-01 11:56                                         ` James Thomas via Bug reports for GNU Emacs, the Swiss army knife of text editors

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).