Subject: bug#67393: 29.1; Slow to open file if autosave exists

From: Juri Linkov <[email-masked]>
Date: Tue, 16 Jan 2024 18:36:51 +0200

>>> I propose to refactor such code
>>>
>>>   (message "Ispell error: %s" output)
>>>   (sit-for 5)
>>>
>>> to a new separate function, e.g.
>>>
>>>   (important-message 5 "Ispell error: %s" output)
>>>
>>> with a simple implementation
>>>
>>>   (defun important-message (seconds format-string &rest args)
>>>     (apply #'message format-string args)
>>>     (sit-for seconds))
>>>
>>> Then users could easily override such annoying delay.
>>> Or maybe even the default implementation can check
>>> if set-message-functions already contains set-multi-message
>>> that ensures that the important message will not be missed,
>>> and not to use sit-for in this case.
>>
>> I don't mind much, but is this really the best we can do?
>
> This is just the first thing that we could do.
> So the first task is to find all such places,
> and replace them with the single function call.
>
>> Asking users to customize Emacs by overriding functions is not very
>> friendly, and in this case we certainly could do better, for example
>> by making 5 be a defcustom.
>
> A defcustom would be one option.  'set-important-message' like
> implemented by Ihor would be another option.  All this could be
> added after creating the new function 'important-message'.

So here is the new function 'important-message' and its calls
in two discussed places.

I'm not sure why 'after-find-file' uses non-nil NODISP arg
in (sit-for 1 t).  This means is that the message is not
displayed?  Then why 'message' is called?

I'm asking this because if NODISP is unnecessary then
the signature of 'important-message' without NODISP
will be shorter and more nice to use.

diff --git a/lisp/files.el b/lisp/files.el
index 9c8914bfc50..b22bf4a14c6 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -2781,8 +2781,9 @@ after-find-file
 	      (unless (file-directory-p default-directory)
 		"Use M-x make-directory RET RET to create the directory and its parents")))))
       (when (and warn msg)
-	(message "%s" msg)
-	(or not-serious (sit-for 1 t))))
+	(if not-serious
+            (message "%s" msg)
+          (important-message 1 t "%s" msg))))
     (when (and auto-save-default (not noauto))
       (auto-save-mode 1)))
   ;; Make people do a little extra work (C-x C-q)
diff --git a/lisp/simple.el b/lisp/simple.el
index 692c0dacefc..6fa74bde037 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -4794,6 +4794,12 @@ max-mini-window-lines
 	((integerp max-mini-window-height) max-mini-window-height)
 	(t 1)))
 
+(defun important-message (seconds nodisp format-string &rest args)
+  "Display an important MESSAGE in the echo area.
+Make sure that MESSAGE stays displayed for the specified number of SECONDS."
+  (apply #'message format-string args)
+  (sit-for seconds nodisp))
+
 (defun display-message-or-buffer (message &optional buffer-name action frame)
   "Display MESSAGE in the echo area if possible, otherwise in a pop-up buffer.
 MESSAGE may be either a string or a buffer.
diff --git a/lisp/textmodes/ispell.el b/lisp/textmodes/ispell.el
index 17af1f1d926..3c9eda47cbd 100644
--- a/lisp/textmodes/ispell.el
+++ b/lisp/textmodes/ispell.el
@@ -2768,8 +2768,7 @@ ispell-parse-output
     (substring output 2))		; return root word
    ((equal 0 (string-match "[\ra-zA-Z]" output))
     (ding)				; error message from ispell!
-    (message "Ispell error: %s" output)
-    (sit-for 5)
+    (important-message 5 nil "Ispell error: %s" output)
     nil)
    (t					; need to process &, ?, and #'s
     (let ((type (aref output 0))	; &, ?, or #