aboutsummaryrefslogtreecommitdiffstats
path: root/src/local
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-11-12 18:52:26 +0100
committerbptato <nincsnevem662@gmail.com>2024-11-12 18:52:26 +0100
commita4133432e47d9e45159f0ed1d4e7f6a2c02dc5b6 (patch)
tree679ee33faf91877685a89e3bfe8743bf2b013eea /src/local
parent66db97a494d4605c66ba0da049accf0375f647b2 (diff)
downloadchawan-a4133432e47d9e45159f0ed1d4e7f6a2c02dc5b6.tar.gz
buffer: fix broken gotoAnchor behavior
23beebe6 introduced a regression that broke gotoAnchor. This fixes that, plus a couple other long-standing gotoAnchor bugs: * If no anchor is found, do not dupe the buffer. Desktop browsers still add a history entry, while w3m prints an error. I've copied the latter because it makes more sense as a user, but this will have to be refined for the navigation API at some point. * If the anchor *is* found, then always jump to it, even if it's not visible. This was a limitation of relying on the line array, so now we rely on the box tree instead. (Sooner or later, the former must go anyway.) Also, fix `U' reload not restoring the position (hopefully this time for good).
Diffstat (limited to 'src/local')
-rw-r--r--src/local/container.nim68
-rw-r--r--src/local/pager.nim20
2 files changed, 44 insertions, 44 deletions
diff --git a/src/local/container.nim b/src/local/container.nim
index a5cdd630..862ae0ed 100644
--- a/src/local/container.nim
+++ b/src/local/container.nim
@@ -46,9 +46,8 @@ type
setxsave: bool
ContainerEventType* = enum
- cetAnchor, cetNoAnchor, cetReadLine, cetReadArea, cetReadFile, cetOpen,
- cetSetLoadInfo, cetStatus, cetAlert, cetLoaded, cetTitle, cetCancel,
- cetMetaRefresh
+ cetReadLine, cetReadArea, cetReadFile, cetOpen, cetSetLoadInfo, cetStatus,
+ cetAlert, cetLoaded, cetTitle, cetCancel, cetMetaRefresh
ContainerEvent* = object
case t*: ContainerEventType
@@ -62,8 +61,6 @@ type
request*: Request
save*: bool
url*: URL
- of cetAnchor, cetNoAnchor:
- anchor*: string
of cetAlert:
msg*: string
of cetMetaRefresh:
@@ -151,7 +148,7 @@ type
# if set, this *overrides* any content type received from the network. (this
# is because it stores the content type from the -T flag.)
contentType* {.jsget.}: Option[string]
- pos: CursorPosition
+ pos*: CursorPosition
bpos: seq[CursorPosition]
highlights: seq[Highlight]
process* {.jsget.}: int
@@ -489,13 +486,16 @@ proc triggerEvent(container: Container; event: ContainerEvent) =
proc triggerEvent(container: Container; t: ContainerEventType) =
container.triggerEvent(ContainerEvent(t: t))
-proc setNumLines(container: Container; lines: int; finish = false) =
+proc gotoStart(container: Container) =
+ container.pos = container.startpos.get
+ container.startpos = none(CursorPosition)
+ container.needslines = true
+
+proc setNumLines(container: Container; lines: int) =
if container.numLines != lines:
container.numLines = lines
- if container.startpos.isSome and finish:
- container.pos = container.startpos.get
- container.startpos = none(CursorPosition)
- container.needslines = true
+ if container.startpos.isSome and lines >= container.startpos.get.cursory:
+ container.gotoStart()
container.updateCursor()
proc queueDraw*(container: Container) =
@@ -514,7 +514,7 @@ proc requestLines(container: Container): EmptyPromise {.discardable.} =
if isBgNew:
container.bgcolor = res.bgcolor
if res.numLines != container.numLines:
- container.setNumLines(res.numLines, true)
+ container.setNumLines(res.numLines)
if container.loadState != lsLoading:
container.triggerEvent(cetStatus)
if res.numLines > 0:
@@ -706,7 +706,7 @@ proc setCursorYCenter(container: Container; y: int; refresh = true)
if fy != container.fromy:
container.centerLine()
-proc setCursorXYCenter(container: Container; x, y: int; refresh = true)
+proc setCursorXYCenter*(container: Container; x, y: int; refresh = true)
{.jsfunc.} =
let fy = container.fromy
let fx = container.fromx
@@ -1102,10 +1102,13 @@ proc popCursorPos*(container: Container; nojump = false) =
container.sendCursorPosition()
container.needslines = true
-proc copyCursorPos*(container, c2: Container) =
- container.startpos = some(c2.pos)
+proc setStartingPos*(container: Container; pos: CursorPosition) =
+ container.startpos = some(pos)
container.flags.incl(cfHasStart)
+proc setStartingPos*(container: Container; x, y: int) =
+ container.setStartingPos(CursorPosition(setx: -1, cursorx: x, cursory: y))
+
proc cursorNextLink*(container: Container; n = 1) {.jsfunc.} =
if container.iface == nil:
return
@@ -1427,18 +1430,21 @@ proc onload(container: Container; res: int) =
container.setLoadInfo("")
container.triggerEvent(cetStatus)
container.triggerEvent(cetLoaded)
- if cfHasStart notin container.flags and (container.url.hash != "" or
- container.config.autofocus):
- container.requestLines().then(proc(): Promise[GotoAnchorResult] =
- return container.iface.gotoAnchor()
- ).then(proc(res: GotoAnchorResult) =
- if res.found:
- container.setCursorXYCenter(res.x, res.y)
- if res.focus != nil:
- container.onReadLine(res.focus)
- )
+ if cfHasStart in container.flags:
+ if container.startpos.isSome:
+ container.gotoStart()
else:
- container.needslines = true
+ let anchor = container.url.hash.substr(1)
+ if anchor != "" or container.config.autofocus:
+ container.requestLines().then(proc(): Promise[GotoAnchorResult] =
+ return container.iface.gotoAnchor(anchor, container.config.autofocus)
+ ).then(proc(res: GotoAnchorResult) =
+ if res.found:
+ container.setCursorXYCenter(res.x, res.y)
+ if res.focus != nil:
+ container.onReadLine(res.focus)
+ )
+ container.needslines = true
if container.config.metaRefresh != mrNever:
container.iface.checkRefresh().then(proc(res: CheckRefreshResult) =
if res.n >= 0:
@@ -1520,14 +1526,6 @@ proc cancel*(container: Container) {.jsfunc.} =
else:
container.triggerEvent(cetCancel)
-proc findAnchor*(container: Container; anchor: string) =
- container.iface.findAnchor(anchor).then(proc(found: bool) =
- if found:
- container.triggerEvent(ContainerEvent(t: cetAnchor, anchor: anchor))
- else:
- container.triggerEvent(ContainerEvent(t: cetNoAnchor, anchor: anchor))
- )
-
proc readCanceled*(container: Container) =
container.iface.readCanceled().then(proc(repaint: bool) =
if repaint:
@@ -1694,7 +1692,7 @@ proc onreadline(container: Container; w: Slice[int];
container.iface.getLines(w).then(proc(res: GetLinesResult) =
container.onreadline(w, handle, res))
else:
- container.setNumLines(res.numLines, true)
+ container.setNumLines(res.numLines)
# Synchronously read all lines in the buffer.
proc readLines*(container: Container; handle: proc(line: SimpleFlexibleLine)) =
diff --git a/src/local/pager.nim b/src/local/pager.nim
index 07244408..ee799705 100644
--- a/src/local/pager.nim
+++ b/src/local/pager.nim
@@ -865,7 +865,7 @@ proc dupeBuffer(pager: Pager; container: Container; url: URL) =
if p == nil:
pager.alert("Failed to duplicate buffer.")
else:
- p.then(proc(container: Container) =
+ p.then(proc(container: Container): Container =
if container == nil:
pager.alert("Failed to duplicate buffer.")
else:
@@ -1423,13 +1423,21 @@ proc gotoURL(pager: Pager; request: Request; prevurl = none(URL);
else:
container.replaceBackup = replaceBackup
replaceBackup.replaceRef = container
- container.copyCursorPos(replace)
+ container.setStartingPos(replace.pos)
else:
pager.addContainer(container)
inc pager.numload
return container
else:
- pager.container.findAnchor(request.url.hash)
+ let container = pager.container
+ let url = request.url
+ let anchor = url.hash.substr(1)
+ container.iface.gotoAnchor(anchor, false).then(proc(res: GotoAnchorResult) =
+ if res.found:
+ pager.dupeBuffer(container, url)
+ else:
+ pager.alert("Anchor " & url.hash & " not found")
+ )
return nil
proc omniRewrite(pager: Pager; s: string): string =
@@ -2231,12 +2239,6 @@ proc handleEvent0(pager: Pager; container: Container; event: ContainerEvent):
case event.t
of cetLoaded:
dec pager.numload
- of cetAnchor:
- let url2 = newURL(container.url)
- url2.setHash(event.anchor)
- pager.dupeBuffer(container, url2)
- of cetNoAnchor:
- pager.alert("Couldn't find anchor " & event.anchor)
of cetReadLine:
if container == pager.container:
pager.setLineEdit(lmBuffer, event.value, hide = event.password,