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

From: Alvaro Ramirez <[email-masked]>
Date: Mon, 21 Jul 2025 20:29:36 +0100

Gerd Möllmann <[email-masked]> writes:

> Alvaro Ramirez <[email-masked]> writes:
>
>>> On 21 Jul 2025, at 18:25, Gerd Möllmann 
>>> <[email-masked]> wrote:
>>>
>>> Hold on! After the tcutil..., the dictation indicator, the 
>>> blue little
>>> thingy, is displayed with your patch. So it works!
>>
>> Success! Thanks for troubleshooting.
>
> Congrats! Maybe that's something for etc/PROBLEMS.

Good idea. Included in latest patch 
(0003-Enabled-macOS-dictation-post-NSTextInputClient-migration.patch)

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

* src/nsterm.m (selectedRange): Implement to fix dictation.
* etc/NEWS: Announce regression fix.
* etc/PROBLEMS: Help users recover from rejected permission.
---
 etc/NEWS     |  5 +++++
 etc/PROBLEMS |  8 ++++++++
 src/nsterm.m | 19 ++++++++++++++++++-
 3 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/etc/NEWS b/etc/NEWS
index b469b22aacf..4e6bfc45f1e 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -2786,6 +2786,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/etc/PROBLEMS b/etc/PROBLEMS
index 20fe9134da5..1323f02a1e9 100644
--- a/etc/PROBLEMS
+++ b/etc/PROBLEMS
@@ -3566,6 +3566,14 @@ file; for example:
       "/usr/local/opt/libgccjit/lib/gcc/11"
       "/usr/local/opt/gcc/lib/gcc/11/gcc/x86_64-apple-darwin20/11.2.0") ":"))
 
+** macOS text dictation isn't recognising keyboard shortcut
+
+There's a chance that microphone permission may have been rejected in the past.
+
+Try clearing this permission with:
+
+  tccutil reset Microphone org.gnu.Emacs
+
 * Runtime problems specific to PGTK build
 
 ** Whether a display server is available cannot be automatically detected.
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