revert to v9 patch while deep accessibility rewrite in progress

v10-v12 made VoiceOver worse. Reverting to v9 (Zoom works, typing echo works).
Full accessibility tree implementation in progress.
This commit is contained in:
2026-02-25 22:41:28 +01:00
parent 2c78f04089
commit f5ecc44871

View File

@@ -1,63 +1,54 @@
From: Martin Sukany <martin@sukany.cz> From: Martin Sukany <martin@sukany.cz>
Date: Tue, 25 Feb 2026 22:10:00 +0100 Date: Tue, 25 Feb 2026 21:00:00 +0100
Subject: [PATCH] ns: macOS Zoom cursor tracking and VoiceOver support Subject: [PATCH] ns: implement macOS Zoom cursor tracking and VoiceOver
support
Comprehensive accessibility for the macOS NS port: Add comprehensive accessibility support for macOS:
1. UAZoomChangeFocus() for Zoom viewport tracking (on_p && active_p guard). 1. UAZoomChangeFocus() from ApplicationServices/UniversalAccess.h:
Ref: developer.apple.com/documentation/applicationservices/1458830-uazoomchangefocus Directly tells macOS Zoom where the cursor is. Only fires for the
active window cursor draw (on_p && active_p guard).
Ref: https://developer.apple.com/documentation/applicationservices/1458830-uazoomchangefocus
2. NSAccessibility protocol on EmacsView for VoiceOver: 2. NSAccessibility protocol on EmacsView (macOS 10.10+):
- Visual (screen) line numbering via compute_motion/vmotion from indent.c. Full text area protocol for VoiceOver:
Handles line wrapping correctly — VoiceOver reads full visual lines on - Rich typing echo via NSAccessibilityPostNotificationWithUserInfo
up/down arrow, including within wrapped logical lines. with kAXTextEditTypeTyping (requires macOS 10.11+, falls back to
- Bare SelectedTextChanged + SelectedColumnsChanged on every cursor move; bare notification on 10.10)
SelectedRowsChanged only when visual line changes (prevents re-reading - BUF_MODIFF tracking to distinguish content edits from cursor movement
same line on horizontal movement). Matches iTerm2's notification pattern. - Text content: accessibilityValue, accessibilitySelectedText,
- Rich ValueChanged with typing echo (kAXTextEditTypeTyping, macOS 10.11+). accessibilityNumberOfCharacters, accessibilityStringForRange:
Bare ValueChanged on cursor-only moves for text model refresh. - Text navigation: accessibilityLineForIndex:, accessibilityRangeForLine:,
- Buffer-aware data access: BUF_BYTE_ADDRESS (not BYTE_POS_ADDR), accessibilityInsertionPointLineNumber, accessibilityVisibleCharacterRange
set_buffer_internal_1 wrappers around compute_motion/vmotion calls. - Cursor geometry: accessibilityBoundsForRange:, accessibilityFrameForRange:
- accessibilityLabel returns "Emacs — <buffer-name>" for window switch - Notifications: ValueChanged (edit), SelectedTextChanged (cursor),
announcements. FocusedUIElementChanged (window focus)
- 10000-char cap on accessibilityValue, multibyte-safe ranges. Ref: https://developer.apple.com/documentation/appkit/nsaccessibilityprotocol
Uses raw string literals for semi-private AX keys to avoid type Uses raw string literals ("AXTextStateChangeType" etc.) for semi-private
conflicts with the macOS 26 SDK. notification keys to avoid type conflicts between SDK versions (these
symbols were added to public AppKit headers in macOS 26).
3 iterations of pipeline review (researcher → analyzer → coder → reviewer),
final QA score 95/100.
--- ---
diff --git a/src/nsterm.h b/src/nsterm.h diff --git a/src/nsterm.h b/src/nsterm.h
index 7c1ee4c..0c95943 100644 index 7c1ee4c..5e5f61b 100644
--- a/src/nsterm.h --- a/src/nsterm.h
+++ b/src/nsterm.h +++ b/src/nsterm.h
@@ -485,6 +485,12 @@ enum ns_return_frame_mode @@ -485,6 +485,10 @@ enum ns_return_frame_mode
struct frame *emacsframe; struct frame *emacsframe;
int scrollbarsNeedingUpdate; int scrollbarsNeedingUpdate;
NSRect ns_userRect; NSRect ns_userRect;
+#ifdef NS_IMPL_COCOA +#ifdef NS_IMPL_COCOA
+ NSRect lastAccessibilityCursorRect; + NSRect lastAccessibilityCursorRect;
+ ptrdiff_t lastAccessibilityModiff; + ptrdiff_t lastAccessibilityModiff;
+ ptrdiff_t lastAccessibilityCursorPos;
+ NSInteger lastAccessibilityVisualLine;
+#endif +#endif
} }
/* AppKit-side interface. */ /* AppKit-side interface. */
diff --git a/src/nsterm.m b/src/nsterm.m diff --git a/src/nsterm.m b/src/nsterm.m
index 932d209..40e0546 100644 index 932d209..6bfc080 100644
--- a/src/nsterm.m --- a/src/nsterm.m
+++ b/src/nsterm.m +++ b/src/nsterm.m
@@ -57,6 +57,7 @@ Updated by Christian Limpach (chris@nice.ch) @@ -3232,6 +3232,115 @@ Note that CURSOR_WIDTH is meaningful only for (h)bar cursors.
#include "termchar.h"
#include "menu.h"
#include "window.h"
+#include "indent.h"
#include "keyboard.h"
#include "buffer.h"
#include "font.h"
@@ -3232,6 +3233,167 @@ Note that CURSOR_WIDTH is meaningful only for (h)bar cursors.
/* Prevent the cursor from being drawn outside the text area. */ /* Prevent the cursor from being drawn outside the text area. */
r = NSIntersectionRect (r, ns_row_rect (w, glyph_row, TEXT_AREA)); r = NSIntersectionRect (r, ns_row_rect (w, glyph_row, TEXT_AREA));
@@ -98,10 +89,7 @@ index 932d209..40e0546 100644
+ struct buffer *curbuf + struct buffer *curbuf
+ = XBUFFER (XWINDOW (f->selected_window)->contents); + = XBUFFER (XWINDOW (f->selected_window)->contents);
+ +
+ bool content_changed = (curbuf + if (curbuf && BUF_MODIFF (curbuf) != view->lastAccessibilityModiff)
+ && BUF_MODIFF (curbuf) != view->lastAccessibilityModiff);
+
+ if (content_changed)
+ { + {
+ /* Buffer content changed since last cursor draw — this is + /* Buffer content changed since last cursor draw — this is
+ an edit (typing, yank, undo, etc.). Post ValueChanged + an edit (typing, yank, undo, etc.). Post ValueChanged
@@ -149,58 +137,9 @@ index 932d209..40e0546 100644
+ } + }
+ } + }
+ +
+ /* Notify AT that cursor position (selection) changed. + /* Always notify that cursor position (selection) changed. */
+ Post bare notifications WITHOUT userInfo — iTerm2 proves + NSAccessibilityPostNotification (
+ this is sufficient for VoiceOver. Rich userInfo with + view, NSAccessibilitySelectedTextChangedNotification);
+ semi-private AXTextStateChangeType keys can cause VoiceOver
+ to silently ignore notifications if any value is unexpected.
+
+ Post three notifications together (matching iTerm2):
+ 1. SelectedTextChanged — cursor/selection moved
+ 2. SelectedRowsChanged — triggers line announcement
+ 3. SelectedColumnsChanged — triggers column tracking
+
+ Also post ValueChanged on every cursor draw (not just edits)
+ so VoiceOver refreshes its internal text model. iTerm2 does
+ this on every dirty screen refresh. */
+ {
+ ptrdiff_t pt = curbuf ? BUF_PT (curbuf) : 0;
+ ptrdiff_t old_pt = view->lastAccessibilityCursorPos;
+
+ /* Post bare ValueChanged so VoiceOver re-queries
+ accessibilityValue and keeps its text model current.
+ Skip if we already posted a rich ValueChanged above
+ (on content change) to avoid double notification. */
+ if (!content_changed)
+ NSAccessibilityPostNotification (
+ view, NSAccessibilityValueChangedNotification);
+
+ if (pt != old_pt)
+ {
+ /* Cursor actually moved — post selection notifications.
+ Query current visual line to decide whether to post
+ SelectedRowsChanged. Suppressing it on same-line
+ movement (e.g. left/right arrow) prevents VoiceOver
+ from re-reading the entire line on horizontal moves. */
+ NSInteger cur_line
+ = [view accessibilityInsertionPointLineNumber];
+ NSInteger old_line = view->lastAccessibilityVisualLine;
+
+ NSAccessibilityPostNotification (
+ view, NSAccessibilitySelectedTextChangedNotification);
+
+ if (cur_line != old_line)
+ NSAccessibilityPostNotification (
+ view, NSAccessibilitySelectedRowsChangedNotification);
+
+ NSAccessibilityPostNotification (
+ view, NSAccessibilitySelectedColumnsChangedNotification);
+
+ view->lastAccessibilityVisualLine = cur_line;
+ }
+
+ view->lastAccessibilityCursorPos = pt;
+ }
+ +
+ /* Tell macOS Zoom where the cursor is. UAZoomChangeFocus() + /* Tell macOS Zoom where the cursor is. UAZoomChangeFocus()
+ expects top-left origin (CG coordinate space). */ + expects top-left origin (CG coordinate space). */
@@ -225,7 +164,7 @@ index 932d209..40e0546 100644
ns_focus (f, NULL, 0); ns_focus (f, NULL, 0);
NSGraphicsContext *ctx = [NSGraphicsContext currentContext]; NSGraphicsContext *ctx = [NSGraphicsContext currentContext];
@@ -8237,6 +8399,14 @@ - (void)windowDidBecomeKey /* for direct calls */ @@ -8237,6 +8346,14 @@ - (void)windowDidBecomeKey /* for direct calls */
XSETFRAME (event.frame_or_window, emacsframe); XSETFRAME (event.frame_or_window, emacsframe);
kbd_buffer_store_event (&event); kbd_buffer_store_event (&event);
ns_send_appdefined (-1); // Kick main loop ns_send_appdefined (-1); // Kick main loop
@@ -240,7 +179,7 @@ index 932d209..40e0546 100644
} }
@@ -9474,6 +9644,538 @@ - (int) fullscreenState @@ -9474,6 +9591,421 @@ - (int) fullscreenState
return fs_state; return fs_state;
} }
@@ -332,7 +271,7 @@ index 932d209..40e0546 100644
+ str = make_uninit_multibyte_string (range, byte_range); + str = make_uninit_multibyte_string (range, byte_range);
+ else + else
+ str = make_uninit_string (range); + str = make_uninit_string (range);
+ memcpy (SDATA (str), BUF_BYTE_ADDRESS (curbuf, start_byte), byte_range); + memcpy (SDATA (str), BYTE_POS_ADDR (start_byte), byte_range);
+ +
+ return [NSString stringWithLispString:str]; + return [NSString stringWithLispString:str];
+} +}
@@ -394,12 +333,8 @@ index 932d209..40e0546 100644
+ +
+- (NSInteger)accessibilityInsertionPointLineNumber +- (NSInteger)accessibilityInsertionPointLineNumber
+{ +{
+ /* Return the VISUAL (screen) line number at point. VoiceOver uses + /* Return the visual line number of the cursor (vpos = line within
+ this to detect line changes: if the number differs from last query, + the window). VoiceOver uses this for line navigation. */
+ it reads the full line; if same, it reads just one character.
+ We must count display lines (respecting wrapping, continuation,
+ window width) — not logical buffer lines delimited by newlines.
+ This matches iTerm2, which returns the screen row coordinate. */
+ if (!emacsframe) + if (!emacsframe)
+ return 0; + return 0;
+ +
@@ -407,28 +342,7 @@ index 932d209..40e0546 100644
+ if (!w) + if (!w)
+ return 0; + return 0;
+ +
+ struct buffer *b = XBUFFER (w->contents); + return (NSInteger) (w->cursor.vpos);
+ if (!b)
+ return 0;
+
+ struct buffer *old = current_buffer;
+ set_buffer_internal_1 (b);
+
+ /* Pass width = -1 so compute_motion determines the effective window
+ width exactly as vmotion does (consistent with accessibilityRangeForLine:
+ which uses vmotion). On GUI frames with fringes, this uses the full
+ window_body_width; on TTY frames, it subtracts 1 for continuation marks. */
+ struct position *pos = compute_motion (
+ BUF_BEGV (b), BUF_BEGV_BYTE (b),
+ 0, 0, 0, /* fromvpos, fromhpos, did_motion */
+ BUF_PT (b), /* to */
+ MOST_POSITIVE_FIXNUM, /* tovpos — large enough to not stop early */
+ 0, /* tohpos */
+ -1, /* width — let compute_motion use window width */
+ w->hscroll, 0, w);
+
+ set_buffer_internal_1 (old);
+ return (NSInteger) pos->vpos;
+} +}
+ +
+- (NSRange)accessibilityVisibleCharacterRange +- (NSRange)accessibilityVisibleCharacterRange
@@ -493,100 +407,67 @@ index 932d209..40e0546 100644
+ +
+- (NSInteger)accessibilityLineForIndex:(NSInteger)index +- (NSInteger)accessibilityLineForIndex:(NSInteger)index
+{ +{
+ /* Convert character index to VISUAL line number. Must be consistent + /* Convert character index to visual line number. Used by VoiceOver
+ with accessibilityInsertionPointLineNumber (both use visual lines + for line-by-line reading (VO+Up/Down).
+ via compute_motion). */ +
+ We walk the window's glyph matrix to find which row contains the
+ given buffer position. Falls back to 0 if not found. */
+ if (!emacsframe) + if (!emacsframe)
+ return 0; + return 0;
+ +
+ struct window *w = XWINDOW (emacsframe->selected_window); + struct window *w = XWINDOW (emacsframe->selected_window);
+ if (!w) + if (!w || !w->current_matrix)
+ return 0; + return 0;
+ +
+ struct buffer *b = XBUFFER (w->contents); + struct buffer *curbuf = XBUFFER (w->contents);
+ if (!b) + if (!curbuf)
+ return 0; + return 0;
+ +
+ ptrdiff_t charpos = BUF_BEGV (b) + (ptrdiff_t) index; + ptrdiff_t charpos = BUF_BEGV (curbuf) + (ptrdiff_t) index;
+ if (charpos > BUF_ZV (b)) + struct glyph_matrix *matrix = w->current_matrix;
+ charpos = BUF_ZV (b);
+ if (charpos < BUF_BEGV (b))
+ charpos = BUF_BEGV (b);
+ +
+ struct buffer *old = current_buffer; + for (int i = 0; i < matrix->nrows; i++)
+ set_buffer_internal_1 (b); + {
+ struct glyph_row *row = matrix->rows + i;
+ if (!row->enabled_p)
+ continue;
+ if (MATRIX_ROW_START_CHARPOS (row) <= charpos
+ && charpos < MATRIX_ROW_END_CHARPOS (row))
+ return (NSInteger) i;
+ }
+ +
+ /* Pass width = -1 for consistency with vmotion (used by + return 0;
+ accessibilityRangeForLine:). See comment in
+ accessibilityInsertionPointLineNumber. */
+ struct position *pos = compute_motion (
+ BUF_BEGV (b), BUF_BEGV_BYTE (b),
+ 0, 0, 0,
+ charpos,
+ MOST_POSITIVE_FIXNUM, 0,
+ -1, w->hscroll, 0, w);
+
+ set_buffer_internal_1 (old);
+ return (NSInteger) pos->vpos;
+} +}
+ +
+- (NSRange)accessibilityRangeForLine:(NSInteger)line +- (NSRange)accessibilityRangeForLine:(NSInteger)line
+{ +{
+ /* Return the character range for VISUAL line N. VoiceOver uses + /* Return the character range for a visual line. VoiceOver uses
+ this to read an entire line when navigating by line. Must be + this to read an entire line when navigating by line. */
+ consistent with accessibilityLineForIndex: (both use visual lines).
+ We use vmotion to find the start of visual line N, then advance
+ one more visual line to find the end. Trailing newlines are
+ excluded from the range. */
+ if (!emacsframe) + if (!emacsframe)
+ return NSMakeRange (0, 0); + return NSMakeRange (0, 0);
+ +
+ struct window *w = XWINDOW (emacsframe->selected_window); + struct window *w = XWINDOW (emacsframe->selected_window);
+ if (!w) + if (!w || !w->current_matrix)
+ return NSMakeRange (0, 0); + return NSMakeRange (0, 0);
+ +
+ struct buffer *b = XBUFFER (w->contents); + struct buffer *curbuf = XBUFFER (w->contents);
+ if (!b) + if (!curbuf)
+ return NSMakeRange (0, 0); + return NSMakeRange (0, 0);
+ +
+ ptrdiff_t begv = BUF_BEGV (b); + struct glyph_matrix *matrix = w->current_matrix;
+ ptrdiff_t zv = BUF_ZV (b); + if (line < 0 || line >= matrix->nrows)
+ return NSMakeRange (0, 0);
+ +
+ struct buffer *old = current_buffer; + struct glyph_row *row = matrix->rows + line;
+ set_buffer_internal_1 (b); + if (!row->enabled_p)
+ return NSMakeRange (0, 0);
+ +
+ /* Move N visual lines from BEGV to reach the start of line N. */ + ptrdiff_t start = MATRIX_ROW_START_CHARPOS (row) - BUF_BEGV (curbuf);
+ struct position *start_pos = vmotion (begv, BUF_BEGV_BYTE (b), + ptrdiff_t end = MATRIX_ROW_END_CHARPOS (row) - BUF_BEGV (curbuf);
+ (EMACS_INT) line, w); + if (start < 0) start = 0;
+ ptrdiff_t line_start = start_pos->bufpos; + if (end < start) end = start;
+ +
+ /* Move 1 more visual line to find where this line ends. */ + return NSMakeRange ((NSUInteger) start, (NSUInteger) (end - start));
+ ptrdiff_t start_byte = buf_charpos_to_bytepos (b, line_start);
+ struct position *end_pos = vmotion (line_start, start_byte, 1, w);
+ ptrdiff_t line_end = end_pos->bufpos;
+
+ /* If vmotion didn't move (last line in buffer), extend to ZV. */
+ if (line_end <= line_start)
+ line_end = zv;
+
+ /* Exclude trailing newline — VoiceOver doesn't need it and would
+ announce "new line" at the end of each spoken line. */
+ if (line_end > line_start)
+ {
+ ptrdiff_t last_byte = buf_charpos_to_bytepos (b, line_end - 1);
+ if (*BUF_BYTE_ADDRESS (b, last_byte) == '\n')
+ line_end--;
+ }
+
+ set_buffer_internal_1 (old);
+
+ ptrdiff_t start_idx = line_start - begv;
+ ptrdiff_t end_idx = line_end - begv;
+ if (start_idx < 0) start_idx = 0;
+ if (end_idx < start_idx) end_idx = start_idx;
+
+ return NSMakeRange ((NSUInteger) start_idx,
+ (NSUInteger) (end_idx - start_idx));
+} +}
+ +
+- (NSRect)accessibilityFrameForRange:(NSRange)range +- (NSRect)accessibilityFrameForRange:(NSRange)range
@@ -607,65 +488,6 @@ index 932d209..40e0546 100644
+ return NSMakeRange (0, 0); + return NSMakeRange (0, 0);
+} +}
+ +
+- (NSRange)accessibilityRangeForIndex:(NSInteger)index
+{
+ /* Return the range of the character at the given index. VoiceOver
+ uses this for character-by-character navigation. */
+ if (!emacsframe)
+ return NSMakeRange (0, 0);
+
+ struct buffer *curbuf
+ = XBUFFER (XWINDOW (emacsframe->selected_window)->contents);
+ if (!curbuf)
+ return NSMakeRange (0, 0);
+
+ ptrdiff_t range = BUF_ZV (curbuf) - BUF_BEGV (curbuf);
+ ptrdiff_t maxrange = MIN (range, 10000);
+
+ if (index < 0 || index >= maxrange)
+ return NSMakeRange (0, 0);
+
+ return NSMakeRange ((NSUInteger) index, 1);
+}
+
+- (NSArray *)accessibilitySelectedTextRanges
+{
+ /* Return array of selected ranges. VoiceOver may query this
+ (plural) form instead of the singular selectedTextRange. */
+ NSRange r = [self accessibilitySelectedTextRange];
+ return @[[NSValue valueWithRange:r]];
+}
+
+- (BOOL)isAccessibilityFocused
+{
+ /* Always report focused — matches iTerm2 behavior. */
+ return YES;
+}
+
+- (NSString *)accessibilityLabel
+{
+ /* Provide an identifying label for VoiceOver. Include the current
+ buffer name so VoiceOver announces it on window/frame switch
+ (triggered by FocusedUIElementChangedNotification). */
+ if (!emacsframe)
+ return @"Emacs";
+
+ struct window *w = XWINDOW (emacsframe->selected_window);
+ if (!w)
+ return @"Emacs";
+
+ struct buffer *b = XBUFFER (w->contents);
+ if (!b)
+ return @"Emacs";
+
+ Lisp_Object name = BVAR (b, name);
+ if (STRINGP (name))
+ return [NSString stringWithFormat:@"Emacs — %@",
+ [NSString stringWithLispString:name]];
+
+ return @"Emacs";
+}
+
+/* ---- Cursor position for Zoom (via accessibilityBoundsForRange:) ---- +/* ---- Cursor position for Zoom (via accessibilityBoundsForRange:) ----
+ +
+ accessibilityFrame intentionally returns the VIEW's frame (standard + accessibilityFrame intentionally returns the VIEW's frame (standard