SDL: Enable ApplePressAndHoldEnabled

From caf0348b26d02bc22ba9a0a908c83c43f8e4e6ad Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Fri, 6 Dec 2024 17:33:10 -0800
Subject: [PATCH] Enable ApplePressAndHoldEnabled

Fixes https://github.com/libsdl-org/SDL/issues/3622
---
 src/video/cocoa/SDL_cocoaevents.m   |  2 +-
 src/video/cocoa/SDL_cocoakeyboard.m | 20 +++++++++++++-------
 2 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/src/video/cocoa/SDL_cocoaevents.m b/src/video/cocoa/SDL_cocoaevents.m
index 886e88f2244b3..ea7874a3d218e 100644
--- a/src/video/cocoa/SDL_cocoaevents.m
+++ b/src/video/cocoa/SDL_cocoaevents.m
@@ -110,7 +110,7 @@ + (void)registerUserDefaults
 
     NSDictionary *appDefaults = [[NSDictionary alloc] initWithObjectsAndKeys:
                                                           [NSNumber numberWithBool:momentumScrollSupported], @"AppleMomentumScrollSupported",
-                                                          [NSNumber numberWithBool:NO], @"ApplePressAndHoldEnabled",
+                                                          [NSNumber numberWithBool:YES], @"ApplePressAndHoldEnabled",
                                                           [NSNumber numberWithBool:YES], @"ApplePersistenceIgnoreState",
                                                           nil];
     [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
diff --git a/src/video/cocoa/SDL_cocoakeyboard.m b/src/video/cocoa/SDL_cocoakeyboard.m
index 34c86c402bd92..72614cb10fd2b 100644
--- a/src/video/cocoa/SDL_cocoakeyboard.m
+++ b/src/video/cocoa/SDL_cocoakeyboard.m
@@ -59,11 +59,10 @@ - (void)setInputRect:(const SDL_Rect *)rect
 
 - (void)insertText:(id)aString replacementRange:(NSRange)replacementRange
 {
-    // TODO: Make use of replacementRange?
-
     const char *str;
 
-    DEBUG_IME(@"insertText: %@", aString);
+    DEBUG_IME(@"insertText: %@ replacementRange: (%d, %d)", aString,
+              (int)replacementRange.location, (int)replacementRange.length);
 
     /* Could be NSString or NSAttributedString, so we have
      * to test and convert it before return as SDL event */
@@ -81,6 +80,12 @@ - (void)insertText:(id)aString replacementRange:(NSRange)replacementRange
     // Deliver the raw key event that generated this text
     [self sendPendingKey];
 
+    if ((int)replacementRange.location != -1) {
+        // We're replacing the last character
+        SDL_SendKeyboardKey(0, SDL_GLOBAL_KEYBOARD_ID, 0, SDL_SCANCODE_BACKSPACE, true);
+        SDL_SendKeyboardKey(0, SDL_GLOBAL_KEYBOARD_ID, 0, SDL_SCANCODE_BACKSPACE, false);
+    }
+
     SDL_SendKeyboardText(str);
 }
 
@@ -131,8 +136,9 @@ - (void)setMarkedText:(id)aString selectedRange:(NSRange)selectedRange replaceme
     SDL_SendEditingText([aString UTF8String],
                         (int)selectedRange.location, (int)selectedRange.length);
 
-    DEBUG_IME(@"setMarkedText: %@, (%d, %d)", _markedText,
-              selectedRange.location, selectedRange.length);
+    DEBUG_IME(@"setMarkedText: %@, (%d, %d) replacement range (%d, %d)", _markedText,
+              (int)selectedRange.location, (int)selectedRange.length,
+              (int)replacementRange.location, (int)replacementRange.length);
 }
 
 - (void)unmarkText
@@ -158,7 +164,7 @@ - (NSRect)firstRectForCharacterRange:(NSRange)aRange actualRange:(NSRangePointer
     }
 
     DEBUG_IME(@"firstRectForCharacterRange: (%d, %d): windowHeight = %g, rect = %@",
-              aRange.location, aRange.length, windowHeight,
+              (int)aRange.location, (int)aRange.length, windowHeight,
               NSStringFromRect(rect));
 
     rect = [window convertRectToScreen:rect];
@@ -168,7 +174,7 @@ - (NSRect)firstRectForCharacterRange:(NSRange)aRange actualRange:(NSRangePointer
 
 - (NSAttributedString *)attributedSubstringForProposedRange:(NSRange)aRange actualRange:(NSRangePointer)actualRange
 {
-    DEBUG_IME(@"attributedSubstringFromRange: (%d, %d)", aRange.location, aRange.length);
+    DEBUG_IME(@"attributedSubstringFromRange: (%d, %d)", (int)aRange.location, (int)aRange.length);
     return nil;
 }