patches: fix C-n/C-p VoiceOver regression - exclude isCtrlNP from re-anchor

When Emacs moves the cursor (emacsMovedCursor=YES), we post
FocusedUIElementChanged on the NSWindow to re-anchor VoiceOver's
browse cursor.  For C-n/C-p this notification races with
AXSelectedTextChanged(granularity=line) and causes VoiceOver to
drop the line-read speech.

Arrow key movement works because VoiceOver intercepts those as AX
selection changes (setAccessibilitySelectedTextRange:), making
voiceoverSetPoint=YES and emacsMovedCursor=NO, so no
FocusedUIElementChanged is posted.

Fix: skip FocusedUIElementChanged for sequential C-n/C-p moves
(isCtrlNP).  AXSelectedTextChanged with direction=next/previous +
granularity=line is sufficient for VoiceOver to read the new line.
FocusedUIElementChanged is only needed for discontiguous jumps
(]], M-<, isearch, xref etc.) where VoiceOver must re-anchor.

Also merge duplicate comment blocks and fix two compile errors
from a64d24c that Martin caught during testing.
This commit is contained in:
2026-03-02 20:48:57 +01:00
parent a64d24cbd9
commit 7a0b4f6cf2
9 changed files with 84 additions and 92 deletions

View File

@@ -1,4 +1,4 @@
From 13da736610fc631a9ea420d6918eefd2791940d3 Mon Sep 17 00:00:00 2001
From 33333f637c51c1ee2080c780fd623e67d3a85545 Mon Sep 17 00:00:00 2001
From: Daneel <daneel@sukany.cz>
Date: Mon, 2 Mar 2026 18:49:13 +0100
Subject: [PATCH 8/8] ns: announce child frame completion candidates for
@@ -33,8 +33,8 @@ area announcements.
doc/emacs/macos.texi | 14 +-
etc/NEWS | 18 +-
src/nsterm.h | 20 ++
src/nsterm.m | 508 ++++++++++++++++++++++++++++++++++++++++---
4 files changed, 512 insertions(+), 48 deletions(-)
src/nsterm.m | 456 +++++++++++++++++++++++++++++++++++++++----
4 files changed, 460 insertions(+), 48 deletions(-)
diff --git a/doc/emacs/macos.texi b/doc/emacs/macos.texi
index 8d4a7825d8..03a657f970 100644
@@ -149,7 +149,7 @@ index 21a93bc799..bdd40b8eb7 100644
@end
diff --git a/src/nsterm.m b/src/nsterm.m
index 523f79d235..45f83f3ac6 100644
index 3d8a5dd0fc..7555ae3e95 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -1126,24 +1126,19 @@ Uses CFAbsoluteTimeGetCurrent() (~5 ns, a VDSO read) for timing. */
@@ -483,66 +483,7 @@ index 523f79d235..45f83f3ac6 100644
NSInteger direction = ns_ax_text_selection_direction_discontiguous;
if (point > oldPoint)
direction = ns_ax_text_selection_direction_next;
@@ -9530,6 +9698,58 @@ frameworks like Vertico bump BOTH BUF_MODIFF (via text property
granularity = ns_ax_text_selection_granularity_line;
}
+ /* Programmatic jumps that cross a line boundary (]], [[, M-<,
+ xref, imenu, …) are discontiguous: the cursor teleported to an
+ arbitrary position, not one sequential step forward/backward.
+ Reporting AXTextSelectionDirectionDiscontiguous causes VoiceOver
+ to re-anchor its rotor browse cursor at the new
+ accessibilitySelectedTextRange rather than advancing linearly
+ from its previous internal position. */
+ if (!isCtrlNP && granularity == ns_ax_text_selection_granularity_line)
+ direction = ns_ax_text_selection_direction_discontiguous;
+
+ /* If Emacs moved the cursor (not VoiceOver), force discontiguous
+ so VoiceOver re-anchors its browse cursor to the current
+ accessibilitySelectedTextRange. This covers all Emacs-initiated
+ moves: editing commands, ELisp, isearch, etc.
+ Exception: C-n/C-p (isCtrlNP) already uses next/previous with
+ line granularity; those are already sequential and VoiceOver
+ handles them correctly. */
+ if (emacsMovedCursor && !isCtrlNP)
+ direction = ns_ax_text_selection_direction_discontiguous;
+
+ /* When Emacs moves the cursor (not VoiceOver-initiated),
+ VoiceOver's browse cursor must re-anchor to the new
+ insertion point. Posting FocusedUIElementChanged on self
+ (a custom NSObject-based element, not an NSView) is
+ insufficient \u2014 VoiceOver only re-anchors its browse cursor
+ when it receives FocusedUIElementChanged from the NSWindow,
+ which triggers accessibilityFocusedUIElement to walk the
+ hierarchy and re-anchor at the returned element.
+
+ Post on the window so VoiceOver calls
+ accessibilityFocusedUIElement on it, receives our buffer
+ element, and re-anchors its rotor browse cursor. Also
+ post LayoutChanged with UIElementsKey on the parent view
+ so VoiceOver re-examines our element's properties
+ (including accessibilitySelectedTextRange). */
+ if (emacsMovedCursor && [self isAccessibilityFocused])
+ {
+ NSWindow *win = [self.emacsView window];
+ if (win)
+ ns_ax_post_notification (
+ win,
+ NSAccessibilityFocusedUIElementChangedNotification);
+
+ NSDictionary *layoutInfo = @{
+ NSAccessibilityUIElementsKey: @[self]
+ };
+ ns_ax_post_notification_with_info (
+ self.emacsView,
+ NSAccessibilityLayoutChangedNotification,
+ layoutInfo);
+ }
+
/* Post notifications for focused and non-focused elements. */
if ([self isAccessibilityFocused])
[self postFocusedCursorNotification:point
@@ -9672,6 +9892,13 @@ - (NSRect)accessibilityFrame
@@ -9716,6 +9884,13 @@ - (NSRect)accessibilityFrame
if (vis_start >= vis_end)
return @[];
@@ -556,7 +497,7 @@ index 523f79d235..45f83f3ac6 100644
block_input ();
specpdl_ref blk_count = SPECPDL_INDEX ();
record_unwind_protect_void (unblock_input);
@@ -9796,6 +10023,7 @@ than O(chars). Fall back to pos+1 as safety net. */
@@ -9840,6 +10015,7 @@ than O(chars). Fall back to pos+1 as safety net. */
pos = span_end;
}
@@ -564,7 +505,7 @@ index 523f79d235..45f83f3ac6 100644
return [[spans copy] autorelease];
}
@@ -9977,6 +10205,10 @@ - (void)dealloc
@@ -10021,6 +10197,10 @@ - (void)dealloc
#endif
[accessibilityElements release];
@@ -575,7 +516,7 @@ index 523f79d235..45f83f3ac6 100644
[[self menu] release];
[super dealloc];
}
@@ -11426,6 +11658,9 @@ - (instancetype) initFrameFromEmacs: (struct frame *)f
@@ -11470,6 +11650,9 @@ - (instancetype) initFrameFromEmacs: (struct frame *)f
windowClosing = NO;
processingCompose = NO;
@@ -585,7 +526,7 @@ index 523f79d235..45f83f3ac6 100644
scrollbarsNeedingUpdate = 0;
fs_state = FULLSCREEN_NONE;
fs_before_fs = next_maximized = -1;
@@ -12734,6 +12969,154 @@ - (id)accessibilityFocusedUIElement
@@ -12778,6 +12961,154 @@ - (id)accessibilityFocusedUIElement
The existing elements carry cached state (modiff, point) from the
previous redisplay cycle. Rebuilding first would create fresh
elements with current values, making change detection impossible. */
@@ -740,7 +681,7 @@ index 523f79d235..45f83f3ac6 100644
- (void)postAccessibilityUpdates
{
NSTRACE ("[EmacsView postAccessibilityUpdates]");
@@ -12744,11 +13127,64 @@ - (void)postAccessibilityUpdates
@@ -12788,11 +13119,64 @@ - (void)postAccessibilityUpdates
/* Re-entrance guard: VoiceOver callbacks during notification posting
can trigger redisplay, which calls ns_update_end, which calls us