Subject: bug#63132: Bug on Darwin MacOS with 'M-x man RET commandname[ ]'

From: Eli Zaretskii <[email-masked]>
Date: Fri, 28 Apr 2023 09:04:50 +0300

> Date: Thu, 27 Apr 2023 14:10:40 -0700
> From: asocial-hired.0l--- via "Bug reports for GNU Emacs,
>  the Swiss army knife of text editors" <[email-masked]>
> 
> Hello,
> 
> I am running latest emacs, built today (./configure —with-native-compilation —without-pop). This is also bug in earlier versions.
> 
> When running `M-x man <RET> command-name ` where this is a command-name followed by a space, MacOS Ventura emacs in some cases lags heavily and some cases crashes.
> 
> I find through investigation, for example, when it is `M-x man <RET> 2[ ]`, 2 followed by a space, it performs the equivalent ‘man -k ^’ and ‘man -k ^2’ internally. On my Linux VM, running 5.14.8-arch1-1, this is a non-issue, and speaking to others on Linux systems, it is also a non-issue there. 
> 
> I have fixed this temporarily on my MacOS machine by giving man advice; however, this same advice causes man not to work on my Linux VM.
> 
> Currently, it is a non-issue for me, but I would like to report it nonetheless.

Thanks.

Does the patch below give good results?

diff --git a/lisp/man.el b/lisp/man.el
index 479bf9f..02be396 100644
--- a/lisp/man.el
+++ b/lisp/man.el
@@ -664,10 +664,9 @@ Man-build-man-command
 (defun Man-translate-cleanup (string)
   "Strip leading, trailing and middle spaces."
   (when (stringp string)
-    ;;  Strip leading and trailing
-    (if (string-match "^[ \t\f\r\n]*\\(.+[^ \t\f\r\n]\\)" string)
-        (setq string (match-string 1 string)))
-    ;; middle spaces
+    ;;  Strip leading and trailing spaces.
+    (setq string (string-trim string "[ \t\f\r\n]" "[ \t\f\r\n]"))
+    ;; Remove middle spaces.
     (setq string (replace-regexp-in-string "[\t\r\n]" " " string))
     (setq string (replace-regexp-in-string "  +" " " string))
     string))