about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-07-06 21:03:38 +0200
committerbptato <nincsnevem662@gmail.com>2024-07-06 21:14:08 +0200
commita5f98a151207ffe823a8024721e37e320d97f241 (patch)
tree331cf82f24f2956b861686da0d032f0ea179fcf4 /src
parent7184a6758340451b7f8161487b0c4ce1f979510d (diff)
downloadchawan-a5f98a151207ffe823a8024721e37e320d97f241.tar.gz
pager: fix redirection to deleted buffers
Fixes the bug where getting redirected to a buffer that the pager then
deleted (e.g. image display, site no longer available, etc.) would land
you in a buffer detached from the main tree.
Diffstat (limited to 'src')
-rw-r--r--src/local/container.nim1
-rw-r--r--src/local/pager.nim23
2 files changed, 19 insertions, 5 deletions
diff --git a/src/local/container.nim b/src/local/container.nim
index 9f8dbd9e..a103e7a8 100644
--- a/src/local/container.nim
+++ b/src/local/container.nim
@@ -140,6 +140,7 @@ type
     lineshift: int
     numLines*: int
     replace*: Container
+    replaceBackup*: Container # for redirection; when set, we get discarded
     code*: int # note: this is not the status code, but the ConnectErrorCode.
     errorMessage*: string
     retry*: seq[URL]
diff --git a/src/local/pager.nim b/src/local/pager.nim
index f6345dfb..765bb89d 100644
--- a/src/local/pager.nim
+++ b/src/local/pager.nim
@@ -929,6 +929,10 @@ proc replace*(pager: Pager; target, container: Container) =
 proc deleteContainer(pager: Pager; container, setTarget: Container) =
   if container.loadState == lsLoading:
     container.cancel()
+  if container.replaceBackup != nil:
+    pager.setContainer(container.replaceBackup)
+  elif container.replace != nil:
+    pager.replace(container, container.replace)
   if container.sourcepair != nil:
     container.sourcepair.sourcepair = nil
     container.sourcepair = nil
@@ -950,8 +954,9 @@ proc deleteContainer(pager: Pager; container, setTarget: Container) =
   container.parent = nil
   container.children.setLen(0)
   if container.replace != nil:
-    pager.replace(container, container.replace)
     container.replace = nil
+  elif container.replaceBackup != nil:
+    container.replaceBackup = nil
   elif pager.container == container:
     pager.setContainer(setTarget)
   pager.unreg.add(container)
@@ -1186,8 +1191,8 @@ proc applySiteconf(pager: Pager; url: var URL; charsetOverride: Charset;
 # Load request in a new buffer.
 proc gotoURL(pager: Pager; request: Request; prevurl = none(URL);
     contentType = none(string); cs = CHARSET_UNKNOWN; replace: Container = nil;
-    redirectDepth = 0; referrer: Container = nil; save = false;
-    url: URL = nil) =
+    replaceBackup: Container = nil; redirectDepth = 0;
+    referrer: Container = nil; save = false; url: URL = nil) =
   pager.navDirection = ndNext
   if referrer != nil and referrer.config.referer_from:
     request.referrer = referrer.url
@@ -1218,7 +1223,10 @@ proc gotoURL(pager: Pager; request: Request; prevurl = none(URL);
     )
     if replace != nil:
       pager.replace(replace, container)
-      container.replace = replace
+      if replaceBackup == nil:
+        container.replace = replace
+      else:
+        container.replaceBackup = replaceBackup
       container.copyCursorPos(replace)
     else:
       pager.addContainer(container)
@@ -1803,8 +1811,13 @@ proc checkMailcap(pager: Pager; container: Container; stream: SocketStream;
   return CheckMailcapResult(connect: false, fdout: -1, found: true)
 
 proc redirectTo(pager: Pager; container: Container; request: Request) =
+  let replaceBackup = if container.replaceBackup != nil:
+    container.replaceBackup
+  else:
+    container.find(ndAny)
   pager.gotoURL(request, some(container.url), replace = container,
-    redirectDepth = container.redirectDepth + 1, referrer = container)
+    replaceBackup = replaceBackup, redirectDepth = container.redirectDepth + 1,
+    referrer = container)
   pager.container.loadinfo = "Redirecting to " & $request.url
   pager.onSetLoadInfo(pager.container)
   dec pager.numload