Subject: [PATCH] Add ring-bell functions for mode line and header line.

From: Elijah Gabe Pérez <[email-masked]>
Date: Thu, 10 Apr 2025 11:55:20 -0600

Tags: patch

This feature flashes the current mode-line face (mode-line-active) once,
this is intended to be used in `ring-bell-function', but it can be used
for other purposes.

There have been third-party packages that make this, however many of
them diverge either in features or in faces.

I think Emacs should have had this built-in.

>From e17ea82e1476dd14c3502caa966332453c5513f2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?El=C3=ADas=20Gabriel=20P=C3=A9rez?= <[email-masked]>
Date: Thu, 10 Apr 2025 11:38:21 -0600
Subject: [PATCH] Add ring-bell functions for flash mode line or header line.

New functions for flash the mode line or header line once
whether `ring-bell-function' is called.  (bug#)

* lisp/faces.el (mode-line-flash, header-line-flash): New faces.
* lisp/simple.el (mode-line-ring-bell-delay)
(header-line-ring-bell-delay): New user options.
(mode-line-ring-bell--reset-face, mode-line-ring-bell)
(header-line-ring-bell--reset-face, header-line-ring-bell): New
functions.
---
 lisp/faces.el  | 21 +++++++++++++++++++++
 lisp/simple.el | 30 ++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+)

diff --git a/lisp/faces.el b/lisp/faces.el
index dbc0a2e04f6..b1d84c3dac0 100644
--- a/lisp/faces.el
+++ b/lisp/faces.el
@@ -2745,6 +2745,27 @@ mode-line-inactive
   :group 'mode-line-faces
   :group 'basic-faces)
 
+(defface mode-line-flash
+  '((((class color) (min-colors 88))
+     :box (:line-width -1 :color "brown" :style nil)
+     :background "red3" :foreground "white")
+    (((class color grayscale) (min-colors 88))
+     :box (:line-width -1 :style nil)
+     :background "grey10" :foreground "white")
+    (t
+     :inherit error :inverse-video t))
+  "Basic mode line face for flash it."
+  :version "31.1"
+  :group 'mode-line-faces
+  :group 'basic-faces)
+
+(defface header-line-flash
+  '((t :inherit mode-line-flash))
+  "Basic header line face for flash it."
+  :version "31.1"
+  :group 'mode-line-faces
+  :group 'basic-faces)
+
 (defface mode-line-highlight
   '((((supports :box t) (class color grayscale) (min-colors 88))
      :box (:line-width 2 :color "grey40" :style released-button))
diff --git a/lisp/simple.el b/lisp/simple.el
index 7037158df8d..ee995c35699 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -9383,6 +9383,36 @@ auto-save-mode
   (and (< buffer-saved-size 0)
        (setq buffer-saved-size 0)))
 
+(defcustom mode-line-ring-bell-delay 0.05
+  "Delay used by `mode-line-ring-bell'."
+  :type 'float
+  :version "31.1")
+
+(defun mode-line-ring-bell--reset-face ()
+  (face-remap-reset-base 'mode-line-active))
+
+(defun mode-line-ring-bell ()
+  "Flash the mode line as ring a bell.
+Intended to be used in `ring-bell-function'."
+  (face-remap-set-base 'mode-line-active 'mode-line-flash)
+  (run-with-timer mode-line-ring-bell-delay nil
+                  #'mode-line-ring-bell--reset-face))
+
+(defcustom header-line-ring-bell-delay 0.05
+  "Delay used by `header-line-ring-bell'."
+  :type 'float
+  :version "31.1")
+
+(defun header-line-ring-bell--reset-face ()
+  (face-remap-reset-base 'header-line-active))
+
+(defun header-line-ring-bell ()
+  "Flash the header line as ring a bell.
+Intended to be used in `ring-bell-function'."
+  (face-remap-set-base 'header-line-active 'header-line-flash)
+  (run-with-timer header-line-ring-bell-delay nil
+                  #'header-line-ring-bell--reset-face))
+
 (defgroup paren-blinking nil
   "Blinking matching of parens and expressions."
   :prefix "blink-matching-"
-- 
2.49.0

-- 
                                          - E.G via GNU Emacs and Org.