Subject: [PATCH] Fix macOS dictation post NSTextInputClient migration in v30

From: Alvaro Ramirez <[email-masked]>
Date: Mon, 21 Jul 2025 13:26:13 +0100

Hi folks!

We lost macOS dictation in Emacs v30 (see 
https://lists.gnu.org/archive/html/bug-gnu-emacs/2025-03/msg00585.html) 
with the NSTextInputClient migration.

Looks like we can get it back using the new subsystem by 
implementing the missing bits for 'selectedRange' in 'nsterm.m'.

Not a NSTextInputClient expert here. Would love for other macOS 
users to give the patch a try and confirm they get dictation 
back. Similarly, edit as normal so we can rule out there are no 
additional side effects.

Álvaro

In GNU Emacs 31.0.50 (build 14, aarch64-apple-darwin24.3.0, NS
 appkit-2575.40 Version 15.3.2 (Build 24D81)) of 2025-07-21 built 
 on jiko.local
Repository revision: e9deec70dac822f2f312497c6d7a7bd6b4e648bb
Repository branch: fixes-macos-dictation-post-v30
Windowing system distributor 'Apple', version 10.3.2575
System Description:  macOS 15.3.2

Configured using:
 'configure --with-ns
 --prefix=.../emacs/nextstep/Emacs.app/Contents/MacOS
 --enable-locallisppath=.../emacs/nextstep/Emacs.app/Contents/MacOS'

>From 41af74ab18b4287c5d20516d39c8405139757158 Mon Sep 17 00:00:00 2001
From: Alvaro Ramirez <[email-masked]>
Date: Mon, 21 Jul 2025 12:53:50 +0100
Subject: [PATCH] Fix macOS dictation post NSTextInputClient migration in v30

* src/nsterm.m (selectedRange): Implement to fix dictation.
---
 etc/NEWS     |  5 +++++
 src/nsterm.m | 19 ++++++++++++++++++-
 2 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/etc/NEWS b/etc/NEWS
index c778b8239b6..883676dbc15 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -2784,6 +2784,11 @@ allowing Emacs users access to speech recognition utilities.
 Note: Accepting this permission allows the use of system APIs, which may
 send user data to Apple's speech recognition servers.
 
+---
+** Re-introduced dictation, lost in Emacs v30 (macOS).
+We lost macOS dictation in v30 when migrating to NSTextInputClient.
+Implemented 'selectedRange' in 'nsterm.m' to enable in new subsystem.
+
 +++
 ** On Mac OS X, stipples now render with color.
 
diff --git a/src/nsterm.m b/src/nsterm.m
index 003aadb9782..2b34894f36e 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -7413,8 +7413,24 @@ - (NSRange)selectedRange
 {
   if (NS_KEYLOG)
     NSLog (@"selectedRange request");
-  return NSMakeRange (NSNotFound, 0);
+
+  struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (emacsframe));
+  struct buffer *buf = XBUFFER (w->contents);
+  ptrdiff_t point = BUF_PT (buf);
+
+  if (NILP (BVAR (buf, mark_active)))
+    {
+      NSUInteger selection_location = point - BUF_BEGV (buf);
+      return NSMakeRange (selection_location, 0);
+    }
+
+  ptrdiff_t mark = marker_position (BVAR (buf, mark));
+  ptrdiff_t region_start = min (point, mark);
+  ptrdiff_t region_end = max (point, mark);
+  NSUInteger selection_location = region_start - BUF_BEGV (buf);
+  NSUInteger selection_length = region_end - region_start;
+
+  return NSMakeRange (selection_location, selection_length);
 }
 
 #if defined (NS_IMPL_COCOA) || GNUSTEP_GUI_MAJOR_VERSION > 0 || \
--
2.50.0