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

From: Alvaro Ramirez <[email-masked]>
Date: Wed, 23 Jul 2025 18:06:46 +0100

Eli Zaretskii <[email-masked]> writes:

> Are you sure you sent the right patch?  The text in PROBLEMS 
> looks the
> same to me.

Yup, but there were separate PROBLEMS entries and worded 
differently. I like your proposed format better. Changed in 
0007-Enabled-macOS-dictation-post-NSTextInputClient-migration.patch

> What I had in mind is something like this:
>
>  ** Text dictation doesn't work on macOS
>
>  The indication is that <describe here what users will see>.
>
>  One reason for this is that the Emacs permissions for using the
>  microphone were rejected in the past.  To reset the 
>  permissions, run
>  this command:
>
> etc.

Looks good to me. Adopted in attached patch.

> How about explaining some of this in NEWS?  If the UI looks a 
> bit
> different, users could be confused, so maybe we should tell them 
> it's
> a bit different?

Sounds good. Mentioned in NEWS now.

>From a873b25b63225d38173bfd39c12f9b72e16e0c84 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     |  6 ++++++
 etc/PROBLEMS | 12 ++++++++++++
 src/nsterm.m | 19 ++++++++++++++++++-
 3 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/etc/NEWS b/etc/NEWS
index b469b22aacf..6bbfa95cb7f 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -2786,6 +2786,12 @@ 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.
+You may notice a slight change in dictation UI provided by macOS.
+
 +++
 ** On Mac OS X, stipples now render with color.
 
diff --git a/etc/PROBLEMS b/etc/PROBLEMS
index 20fe9134da5..2d0a5870be9 100644
--- a/etc/PROBLEMS
+++ b/etc/PROBLEMS
@@ -3566,6 +3566,18 @@ 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") ":"))
 
+** Text dictation doesn't work on macOS
+
+The indication is that the macOS keyboard shortcut for dictation is ignored.
+
+One reason for this is that the Emacs permissions for using the
+microphone were rejected in the past.  To reset the permissions, run
+this command:
+
+  tccutil reset Microphone org.gnu.Emacs
+
+Another reason may be an unsupported macOS version. For example: macOS 12.
+
 * 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,7 +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