diff --git a/patches/0001-ns-implement-AXBoundsForRange-for-macOS-Zoom-cursor-.patch b/patches/0001-ns-implement-AXBoundsForRange-for-macOS-Zoom-cursor-.patch index 919d265..029edd4 100644 --- a/patches/0001-ns-implement-AXBoundsForRange-for-macOS-Zoom-cursor-.patch +++ b/patches/0001-ns-implement-AXBoundsForRange-for-macOS-Zoom-cursor-.patch @@ -1,63 +1,54 @@ From: Martin Sukany -Date: Tue, 25 Feb 2026 22:10:00 +0100 -Subject: [PATCH] ns: macOS Zoom cursor tracking and VoiceOver support +Date: Tue, 25 Feb 2026 21:00:00 +0100 +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). - Ref: developer.apple.com/documentation/applicationservices/1458830-uazoomchangefocus +1. UAZoomChangeFocus() from ApplicationServices/UniversalAccess.h: + 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: - - Visual (screen) line numbering via compute_motion/vmotion from indent.c. - Handles line wrapping correctly — VoiceOver reads full visual lines on - up/down arrow, including within wrapped logical lines. - - Bare SelectedTextChanged + SelectedColumnsChanged on every cursor move; - SelectedRowsChanged only when visual line changes (prevents re-reading - same line on horizontal movement). Matches iTerm2's notification pattern. - - Rich ValueChanged with typing echo (kAXTextEditTypeTyping, macOS 10.11+). - Bare ValueChanged on cursor-only moves for text model refresh. - - Buffer-aware data access: BUF_BYTE_ADDRESS (not BYTE_POS_ADDR), - set_buffer_internal_1 wrappers around compute_motion/vmotion calls. - - accessibilityLabel returns "Emacs — " for window switch - announcements. - - 10000-char cap on accessibilityValue, multibyte-safe ranges. +2. NSAccessibility protocol on EmacsView (macOS 10.10+): + Full text area protocol for VoiceOver: + - Rich typing echo via NSAccessibilityPostNotificationWithUserInfo + with kAXTextEditTypeTyping (requires macOS 10.11+, falls back to + bare notification on 10.10) + - BUF_MODIFF tracking to distinguish content edits from cursor movement + - Text content: accessibilityValue, accessibilitySelectedText, + accessibilityNumberOfCharacters, accessibilityStringForRange: + - Text navigation: accessibilityLineForIndex:, accessibilityRangeForLine:, + accessibilityInsertionPointLineNumber, accessibilityVisibleCharacterRange + - Cursor geometry: accessibilityBoundsForRange:, accessibilityFrameForRange: + - Notifications: ValueChanged (edit), SelectedTextChanged (cursor), + FocusedUIElementChanged (window focus) + Ref: https://developer.apple.com/documentation/appkit/nsaccessibilityprotocol -Uses raw string literals for semi-private AX keys to avoid type -conflicts with the macOS 26 SDK. - -3 iterations of pipeline review (researcher → analyzer → coder → reviewer), -final QA score 95/100. +Uses raw string literals ("AXTextStateChangeType" etc.) for semi-private +notification keys to avoid type conflicts between SDK versions (these +symbols were added to public AppKit headers in macOS 26). --- diff --git a/src/nsterm.h b/src/nsterm.h -index 7c1ee4c..0c95943 100644 +index 7c1ee4c..5e5f61b 100644 --- a/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; int scrollbarsNeedingUpdate; NSRect ns_userRect; +#ifdef NS_IMPL_COCOA + NSRect lastAccessibilityCursorRect; + ptrdiff_t lastAccessibilityModiff; -+ ptrdiff_t lastAccessibilityCursorPos; -+ NSInteger lastAccessibilityVisualLine; +#endif } /* AppKit-side interface. */ diff --git a/src/nsterm.m b/src/nsterm.m -index 932d209..40e0546 100644 +index 932d209..6bfc080 100644 --- a/src/nsterm.m +++ b/src/nsterm.m -@@ -57,6 +57,7 @@ Updated by Christian Limpach (chris@nice.ch) - #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. +@@ -3232,6 +3232,115 @@ Note that CURSOR_WIDTH is meaningful only for (h)bar cursors. /* Prevent the cursor from being drawn outside the text area. */ r = NSIntersectionRect (r, ns_row_rect (w, glyph_row, TEXT_AREA)); @@ -98,10 +89,7 @@ index 932d209..40e0546 100644 + struct buffer *curbuf + = XBUFFER (XWINDOW (f->selected_window)->contents); + -+ bool content_changed = (curbuf -+ && BUF_MODIFF (curbuf) != view->lastAccessibilityModiff); -+ -+ if (content_changed) ++ if (curbuf && BUF_MODIFF (curbuf) != view->lastAccessibilityModiff) + { + /* Buffer content changed since last cursor draw — this is + an edit (typing, yank, undo, etc.). Post ValueChanged @@ -149,58 +137,9 @@ index 932d209..40e0546 100644 + } + } + -+ /* Notify AT that cursor position (selection) changed. -+ Post bare notifications WITHOUT userInfo — iTerm2 proves -+ this is sufficient for VoiceOver. Rich userInfo with -+ 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; -+ } ++ /* Always notify that cursor position (selection) changed. */ ++ NSAccessibilityPostNotification ( ++ view, NSAccessibilitySelectedTextChangedNotification); + + /* Tell macOS Zoom where the cursor is. UAZoomChangeFocus() + expects top-left origin (CG coordinate space). */ @@ -225,7 +164,7 @@ index 932d209..40e0546 100644 ns_focus (f, NULL, 0); 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); kbd_buffer_store_event (&event); 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; } @@ -332,7 +271,7 @@ index 932d209..40e0546 100644 + str = make_uninit_multibyte_string (range, byte_range); + else + 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]; +} @@ -394,12 +333,8 @@ index 932d209..40e0546 100644 + +- (NSInteger)accessibilityInsertionPointLineNumber +{ -+ /* Return the VISUAL (screen) line number at point. VoiceOver uses -+ this to detect line changes: if the number differs from last query, -+ 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. */ ++ /* Return the visual line number of the cursor (vpos = line within ++ the window). VoiceOver uses this for line navigation. */ + if (!emacsframe) + return 0; + @@ -407,28 +342,7 @@ index 932d209..40e0546 100644 + if (!w) + return 0; + -+ struct buffer *b = XBUFFER (w->contents); -+ 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; ++ return (NSInteger) (w->cursor.vpos); +} + +- (NSRange)accessibilityVisibleCharacterRange @@ -493,100 +407,67 @@ index 932d209..40e0546 100644 + +- (NSInteger)accessibilityLineForIndex:(NSInteger)index +{ -+ /* Convert character index to VISUAL line number. Must be consistent -+ with accessibilityInsertionPointLineNumber (both use visual lines -+ via compute_motion). */ ++ /* Convert character index to visual line number. Used by VoiceOver ++ for line-by-line reading (VO+Up/Down). ++ ++ 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) + return 0; + + struct window *w = XWINDOW (emacsframe->selected_window); -+ if (!w) ++ if (!w || !w->current_matrix) + return 0; + -+ struct buffer *b = XBUFFER (w->contents); -+ if (!b) ++ struct buffer *curbuf = XBUFFER (w->contents); ++ if (!curbuf) + return 0; + -+ ptrdiff_t charpos = BUF_BEGV (b) + (ptrdiff_t) index; -+ if (charpos > BUF_ZV (b)) -+ charpos = BUF_ZV (b); -+ if (charpos < BUF_BEGV (b)) -+ charpos = BUF_BEGV (b); ++ ptrdiff_t charpos = BUF_BEGV (curbuf) + (ptrdiff_t) index; ++ struct glyph_matrix *matrix = w->current_matrix; + -+ struct buffer *old = current_buffer; -+ set_buffer_internal_1 (b); ++ for (int i = 0; i < matrix->nrows; i++) ++ { ++ 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 -+ 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; ++ return 0; +} + +- (NSRange)accessibilityRangeForLine:(NSInteger)line +{ -+ /* Return the character range for VISUAL line N. VoiceOver uses -+ this to read an entire line when navigating by line. Must be -+ 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. */ ++ /* Return the character range for a visual line. VoiceOver uses ++ this to read an entire line when navigating by line. */ + if (!emacsframe) + return NSMakeRange (0, 0); + + struct window *w = XWINDOW (emacsframe->selected_window); -+ if (!w) ++ if (!w || !w->current_matrix) + return NSMakeRange (0, 0); + -+ struct buffer *b = XBUFFER (w->contents); -+ if (!b) ++ struct buffer *curbuf = XBUFFER (w->contents); ++ if (!curbuf) + return NSMakeRange (0, 0); + -+ ptrdiff_t begv = BUF_BEGV (b); -+ ptrdiff_t zv = BUF_ZV (b); ++ struct glyph_matrix *matrix = w->current_matrix; ++ if (line < 0 || line >= matrix->nrows) ++ return NSMakeRange (0, 0); + -+ struct buffer *old = current_buffer; -+ set_buffer_internal_1 (b); ++ struct glyph_row *row = matrix->rows + line; ++ if (!row->enabled_p) ++ return NSMakeRange (0, 0); + -+ /* Move N visual lines from BEGV to reach the start of line N. */ -+ struct position *start_pos = vmotion (begv, BUF_BEGV_BYTE (b), -+ (EMACS_INT) line, w); -+ ptrdiff_t line_start = start_pos->bufpos; ++ ptrdiff_t start = MATRIX_ROW_START_CHARPOS (row) - BUF_BEGV (curbuf); ++ ptrdiff_t end = MATRIX_ROW_END_CHARPOS (row) - BUF_BEGV (curbuf); ++ if (start < 0) start = 0; ++ if (end < start) end = start; + -+ /* Move 1 more visual line to find where this line ends. */ -+ 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)); ++ return NSMakeRange ((NSUInteger) start, (NSUInteger) (end - start)); +} + +- (NSRect)accessibilityFrameForRange:(NSRange)range @@ -607,65 +488,6 @@ index 932d209..40e0546 100644 + 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:) ---- + + accessibilityFrame intentionally returns the VIEW's frame (standard