patches: fix org fold/unfold VoiceOver refresh; revert to BUF_MODIFF

REGRESSION: fold/unfold in org-mode, outline-mode and hideshow-mode did
not refresh VoiceOver text because ensureTextCache used BUF_CHARS_MODIFF
which is NOT bumped by (put-text-property ... 'invisible), the mechanism
used by modern org-fold-core (org >= 29) and outline-mode to hide text.

VoiceOver would continue reading folded content as if visible, or miss
newly unfolded content entirely, because the text cache was considered
valid despite the visible-text having changed.

Revert ensureTextCache to BUF_MODIFF with an explanatory comment:

- BUF_CHARS_MODIFF is bumped only on character insertions/deletions, not
  text-property changes.  Fold/unfold uses text properties for visibility.
- BUF_OVERLAY_MODIFF alone is also insufficient: org >= 29 uses text
  properties, not overlays, for folding.  Also hl-line-mode bumps
  BUF_OVERLAY_MODIFF every post-command-hook --- same per-keystroke cost
  as BUF_MODIFF, with none of its correctness guarantee.
- BUF_MODIFF cost is acceptable: ensureTextCache is called only when
  VoiceOver queries AX properties (human interaction speed, not redisplay
  speed).  Rebuild cost is O(visible-buffer-text).

Also retain C-n/C-p line-read fix from previous commit (7a0b4f6):
FocusedUIElementChanged excluded for sequential isCtrlNP moves.
This commit is contained in:
2026-03-02 20:57:32 +01:00
parent 7a0b4f6cf2
commit a5ff8d391b
9 changed files with 94 additions and 81 deletions

View File

@@ -1,4 +1,4 @@
From 8c99359156443223d13905de4cfbca58fb3e1177 Mon Sep 17 00:00:00 2001
From 1a3dea1876ecde2e625d3a2b8f41d688568ecbf8 Mon Sep 17 00:00:00 2001
From: Daneel <daneel@sukany.cz>
Date: Mon, 2 Mar 2026 18:39:46 +0100
Subject: [PATCH 7/8] ns: announce overlay completion candidates for VoiceOver
@@ -18,8 +18,8 @@ ValueChanged; keep overlay_modiff out of ensureTextCache to prevent a
race where an AX query consumes the change before notification.
---
src/nsterm.h | 1 +
src/nsterm.m | 318 +++++++++++++++++++++++++++++++++++++++++++++------
2 files changed, 283 insertions(+), 36 deletions(-)
src/nsterm.m | 348 ++++++++++++++++++++++++++++++++++++++++++++-------
2 files changed, 307 insertions(+), 42 deletions(-)
diff --git a/src/nsterm.h b/src/nsterm.h
index 5746e9e9bd..21a93bc799 100644
@@ -34,7 +34,7 @@ index 5746e9e9bd..21a93bc799 100644
@property (nonatomic, assign) BOOL cachedMarkActive;
@property (nonatomic, copy) NSString *cachedCompletionAnnouncement;
diff --git a/src/nsterm.m b/src/nsterm.m
index a0598a73c2..3d8a5dd0fc 100644
index a0598a73c2..8f744d1bf3 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -7263,11 +7263,154 @@ Accessibility virtual elements (macOS / Cocoa only)
@@ -228,7 +228,7 @@ index a0598a73c2..3d8a5dd0fc 100644
write section at the end needs synchronization to protect
against concurrent reads from AX server thread. */
eassert ([NSThread isMainThread]);
@@ -8005,24 +8149,15 @@ - (void)ensureTextCache
@@ -8005,25 +8149,34 @@ - (void)ensureTextCache
if (!b)
return;
@@ -247,19 +247,62 @@ index a0598a73c2..3d8a5dd0fc 100644
- explicit announcements in postAccessibilityNotificationsForFrame).
- Including overlay_modiff would silently update cachedOverlayModiff
- and prevent the notification dispatch from detecting changes. */
ptrdiff_t chars_modiff = BUF_CHARS_MODIFF (b);
- ptrdiff_t chars_modiff = BUF_CHARS_MODIFF (b);
+ /* Use BUF_MODIFF, not BUF_CHARS_MODIFF, for cache validity.
+
+ Fold/unfold commands (org-mode, outline-mode, hideshow-mode) change
+ text visibility by modifying the 'invisible text property via
+ `put-text-property' or `add-text-properties'. These bump BUF_MODIFF
+ but NOT BUF_CHARS_MODIFF, because no characters are inserted or
+ deleted. Using only BUF_CHARS_MODIFF would serve stale AX text
+ across fold/unfold: VoiceOver would continue reading hidden content
+ as if it were visible, or miss newly revealed content entirely.
+
+ BUF_MODIFF is bumped by all buffer modifications including
+ text-property changes (e.g. font-lock face assignments), causing a
+ full text-cache rebuild on each redisplay cycle. This is acceptable
+ because `ensureTextCache' is only called when VoiceOver queries
+ accessibilityValue or related AX properties --- which happens at
+ human interaction speed, not at redisplay speed. The per-rebuild
+ cost is O(visible-buffer-text).
+
+ Do NOT use BUF_OVERLAY_MODIFF alone: org-mode >= 29 (org-fold-core)
+ uses text properties, not overlays, for folding, so
+ BUF_OVERLAY_MODIFF would miss those changes. Additionally, modes
+ like hl-line-mode bump BUF_OVERLAY_MODIFF on every
+ post-command-hook, yielding the same per-keystroke rebuild cost as
+ BUF_MODIFF, with none of its correctness guarantee. */
+ ptrdiff_t modiff = BUF_MODIFF (b);
ptrdiff_t pt = BUF_PT (b);
NSUInteger textLen = cachedText ? [cachedText length] : 0;
+ /* Cache validity: track BUF_MODIFF and buffer narrowing.
+ Do NOT track BUF_OVERLAY_MODIFF here --- overlay text is not
+ included in the cached AX text (it is handled separately via
+ explicit announcements). Including overlay_modiff would
+ silently update cachedOverlayModiff and prevent the
+ notification dispatch from detecting overlay changes. */
if (cachedText && cachedTextModiff == chars_modiff
- if (cachedText && cachedTextModiff == chars_modiff
+ if (cachedText && cachedTextModiff == modiff
&& cachedTextStart == BUF_BEGV (b)
&& pt >= cachedTextStart
@@ -8108,7 +8243,7 @@ - (NSUInteger)accessibilityIndexForCharpos:(ptrdiff_t)charpos
&& (textLen == 0
@@ -8039,7 +8192,7 @@ included in the cached AX text (it is handled separately via
{
[cachedText release];
cachedText = [text retain];
- cachedTextModiff = chars_modiff;
+ cachedTextModiff = modiff;
cachedTextStart = start;
if (visibleRuns)
@@ -8051,9 +8204,9 @@ included in the cached AX text (it is handled separately via
Walk the cached text once, recording the start offset of each
line. Uses NSString lineRangeForRange: --- O(N) in the total
text --- but this loop runs only on cache rebuild, which is
- gated on BUF_CHARS_MODIFF: actual character insertions or
- deletions. Font-lock (text property changes) does not trigger
- a rebuild, so the hot path (cursor movement, redisplay) never
+ gated on BUF_MODIFF changes. Rebuilds happen when any buffer
+ modification occurs (including fold/unfold), ensuring the line
+ index always matches the currently visible text.
enters this code. */
if (lineStartOffsets)
xfree (lineStartOffsets);
@@ -8108,7 +8261,7 @@ - (NSUInteger)accessibilityIndexForCharpos:(ptrdiff_t)charpos
/* Binary search: runs are sorted by charpos (ascending). Find the
run whose [charpos, charpos+length) range contains the target,
or the nearest run after an invisible gap. O(log n) instead of
@@ -268,7 +311,7 @@ index a0598a73c2..3d8a5dd0fc 100644
NSUInteger lo = 0, hi = visibleRunCount;
while (lo < hi)
{
@@ -8157,10 +8292,10 @@ by run length (visible window), not total buffer size. */
@@ -8157,10 +8310,10 @@ by run length (visible window), not total buffer size. */
/* Convert accessibility string index to buffer charpos.
Safe to call from any thread: uses only cachedText (NSString) and
@@ -281,7 +324,7 @@ index a0598a73c2..3d8a5dd0fc 100644
@synchronized (self)
{
if (visibleRunCount == 0)
@@ -8202,7 +8337,7 @@ the slow path (composed character sequence walk), which is
@@ -8202,7 +8355,7 @@ the slow path (composed character sequence walk), which is
return cp;
}
}
@@ -290,7 +333,7 @@ index a0598a73c2..3d8a5dd0fc 100644
if (lo > 0)
{
ns_ax_visible_run *last = &visibleRuns[visibleRunCount - 1];
@@ -8224,7 +8359,7 @@ the slow path (composed character sequence walk), which is
@@ -8224,7 +8377,7 @@ the slow path (composed character sequence walk), which is
deadlocking the AX server thread. This is prevented by:
1. validWindow checks WINDOW_LIVE_P and BUFFERP before every
@@ -299,7 +342,7 @@ index a0598a73c2..3d8a5dd0fc 100644
2. All dispatch_sync blocks run on the main thread where no
concurrent Lisp code can modify state between checks.
3. block_input prevents timer events and process output from
@@ -8570,6 +8705,50 @@ - (NSInteger)accessibilityInsertionPointLineNumber
@@ -8570,6 +8723,50 @@ - (NSInteger)accessibilityInsertionPointLineNumber
return [self lineForAXIndex:point_idx];
}
@@ -350,7 +393,7 @@ index a0598a73c2..3d8a5dd0fc 100644
- (NSRange)accessibilityRangeForLine:(NSInteger)line
{
if (![NSThread isMainThread])
@@ -8792,7 +8971,7 @@ - (NSRect)accessibilityFrame
@@ -8792,7 +8989,7 @@ - (NSRect)accessibilityFrame
/* ===================================================================
@@ -359,7 +402,7 @@ index a0598a73c2..3d8a5dd0fc 100644
These methods notify VoiceOver of text and selection changes.
Called from the redisplay cycle (postAccessibilityUpdates).
@@ -8807,7 +8986,7 @@ - (void)postTextChangedNotification:(ptrdiff_t)point
@@ -8807,7 +9004,7 @@ - (void)postTextChangedNotification:(ptrdiff_t)point
if (point > self.cachedPoint
&& point - self.cachedPoint == 1)
{
@@ -368,7 +411,7 @@ index a0598a73c2..3d8a5dd0fc 100644
[self invalidateTextCache];
[self ensureTextCache];
if (cachedText)
@@ -8826,7 +9005,7 @@ - (void)postTextChangedNotification:(ptrdiff_t)point
@@ -8826,7 +9023,7 @@ - (void)postTextChangedNotification:(ptrdiff_t)point
/* Update cachedPoint here so the selection-move branch does NOT
fire for point changes caused by edits. WebKit and Chromium
never send both ValueChanged and SelectedTextChanged for the
@@ -377,7 +420,7 @@ index a0598a73c2..3d8a5dd0fc 100644
self.cachedPoint = point;
NSDictionary *change = @{
@@ -9220,16 +9399,83 @@ - (void)postAccessibilityNotificationsForFrame:(struct frame *)f
@@ -9220,16 +9417,83 @@ - (void)postAccessibilityNotificationsForFrame:(struct frame *)f
BOOL markActive = !NILP (BVAR (b, mark_active));
/* --- Text changed (edit) --- */
@@ -465,7 +508,7 @@ index a0598a73c2..3d8a5dd0fc 100644
{
ptrdiff_t oldPoint = self.cachedPoint;
BOOL oldMarkActive = self.cachedMarkActive;
@@ -12402,7 +12648,7 @@ - (int) fullscreenState
@@ -12402,7 +12666,7 @@ - (int) fullscreenState
if (WINDOW_LEAF_P (w))
{
@@ -474,7 +517,7 @@ index a0598a73c2..3d8a5dd0fc 100644
EmacsAccessibilityBuffer *elem
= [existing objectForKey:[NSValue valueWithPointer:w]];
if (!elem)
@@ -12436,7 +12682,7 @@ - (int) fullscreenState
@@ -12436,7 +12700,7 @@ - (int) fullscreenState
}
else
{
@@ -483,7 +526,7 @@ index a0598a73c2..3d8a5dd0fc 100644
Lisp_Object child = w->contents;
while (!NILP (child))
{
@@ -12548,7 +12794,7 @@ - (void)postAccessibilityUpdates
@@ -12548,7 +12812,7 @@ - (void)postAccessibilityUpdates
accessibilityUpdating = YES;
/* Detect window tree change (split, delete, new buffer). Compare
@@ -492,7 +535,7 @@ index a0598a73c2..3d8a5dd0fc 100644
Lisp_Object curRoot = FRAME_ROOT_WINDOW (emacsframe);
if (!EQ (curRoot, lastRootWindow))
{
@@ -12557,12 +12803,12 @@ - (void)postAccessibilityUpdates
@@ -12557,12 +12821,12 @@ - (void)postAccessibilityUpdates
}
/* If tree is stale, rebuild FIRST so we don't iterate freed