The bell rings whenever function ‘ding’ is called. What the bell does depends on the value of these variables:
‘visible-bell’ is ‘nil’, Emacs makes an audible ding.‘visible-bell’ is non-‘nil’, Emacs flashes the screen.‘ring-bell-function’ is a function, Emacs does whatever that function tells it to do.Combinations of these variables can create different alert functions, as described below.
See also: echo-bell-mode.
During a normal editing session Emacs gives a warning signal quite often. Every time you type Ctrl-G, to stop searching, or stop what you were typing and do something else, and so on… you will get the bell. Some people find all the beeping annoying. To get a visual signal instead, put the following in your init file:
(setq visible-bell 1)
If (setq visible-bell t) doesn’t work for you, you can use a different visual indicator:
(setq lexical-binding t
visible-bell nil
ring-bell-function 'asc:flash-background)
(defun asc:flash-background ()
(let ((fg (face-foreground 'default))
(bg (face-background 'default)))
(set-face-background 'default "DodgerBlue")
(set-face-foreground 'default "black")
(run-with-idle-timer
1 nil (lambda ()
(set-face-background 'default bg)
(set-face-foreground 'default fg)))))
This uses a timer to turn the colors back to what they were.
For an non-intrusive but still noticeable visual bell, you can flash the foreground of a face. This is similar to the above screen flashing, but subtly flashes only the mode line foreground, and does not require lexical binding:
(setq ring-bell-function
(lambda ()
(let ((orig-fg (face-foreground 'mode-line)))
(set-face-foreground 'mode-line "#F2804F")
(run-with-idle-timer 0.1 nil
(lambda (fg) (set-face-foreground 'mode-line fg))
orig-fg))))
Some people find the flashing annoying. To turn the alarm off totally, you can use this:
(setq ring-bell-function 'ignore)
Instead of beeping or flashing, Emacs could play a cool sound file, whenever an error occurs (this handles .au or .wav files):
(setq ring-bell-function (lambda () (play-sound-file "/this/is/my/errorsound.au")))
Or
(setq ring-bell-function (lambda () (call-process "audioplay" nil 0 nil "/this/is/my/errorsound.au")))
You might have to replace “audioplay” by “wavplay” or whatever application you have for playing audio files.
The visible bell is great if Emacs is the only program you ever use. However, if you aren’t paying attention to Emacs, or it’s on another desktop, you won’t see the visible bell. In those cases, you probably want the bell to ring instead of blink. Use the following:
(defun unfocused-ding-blink () "Flash the screen when Emacs is focused, ring the bell (ding) if not." (setq ring-bell-function (lambda () (setq visible-bell (fsm-frame-x-active-window-p (selected-frame))) (ding))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Frame-related functions ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; All from http://www.emacswiki.org/emacs-pt/rcircDbusNotification (require 'dbus) (defun fsm-x-active-window () "Return the window ID of the current active window in X, as given by the _NET_ACTIVE_WINDOW of the root window set by the window-manager, or nil if not able to" (and (eq (window-system) 'x) (let ((x-active-window (x-window-property "_NET_ACTIVE_WINDOW" nil "WINDOW" 0 nil t))) (string-to-number (format "%x00%x" (car x-active-window) (cdr x-active-window)) 16)))) (defun fsm-frame-outer-window-id (frame) "Return the frame outer-window-id property, or nil if FRAME not of the correct type" (and (framep frame) (string-to-number (frame-parameter frame 'outer-window-id)))) (defun fsm-frame-x-active-window-p (frame) "Check if FRAME is is the X active windows Returns t if frame has focus or nil if" (and (framep frame) (eq (fsm-frame-outer-window-id frame) (fsm-x-active-window)))
)
(unfocused-ding-blink)
In some cases, you’d like to reduce the number of warnings or eliminate warnings in certain conditions. The following turns off the alarm bell when you hit ‘C-g’ in the minibuffer or during an ‘isearch’.
(setq ring-bell-function
(lambda ()
(unless (memq this-command
'(isearch-abort abort-recursive-edit exit-minibuffer keyboard-quit))
(ding))))I have a younger sister and the bell wakes her up, so I’ve wrote this predicate to control the beeping. – PiotrMieszkowski
(defvar night-start 22 "The hour that people go to sleep.") (defvar night-end 8 "The hour that people wake up.") (defun nightp () "Check if it is night." (let ((hr (nth 2 (decode-time (current-time))))) (unless (< hr night-end) (> hr night-start))))
I dislike the visible bell in terminals (i.e. flashing the whole terminal), so I’m experimenting with the following, which thus far seems to work nicely:
(defun my-terminal-visible-bell () "A friendlier visual bell effect." (invert-face 'mode-line) (run-with-timer 0.1 nil 'invert-face 'mode-line)) (setq visible-bell nil ring-bell-function #'my-terminal-visible-bell)
In GUI Emacs I prefer the default visible-bell however, so I’d ideally like to switch automatically depending on the currently-focused display. Unfortunately these variables aren’t terminal-local, which makes it more awkward. My current approach is to use focus-in-hook, which doesn’t catch all situations, but certainly accounts for many (and if you don’t switch between GUI and terminal frames, it should be fine in any case). FWIW:
(defun my-configure-visible-bell () "Use a nicer visual bell in terminals." (if window-system (setq visible-bell t ring-bell-function nil) (setq visible-bell nil ring-bell-function #'my-terminal-visible-bell))) (defun my-frame-config (frame) "Custom behaviours for new frames." (with-selected-frame frame (my-configure-visible-bell)))
;; Run now, for non-daemon Emacs... (my-frame-config (selected-frame)) ;; ...and later, for new frames / emacsclient (add-hook 'after-make-frame-functions 'my-frame-config) ;; ...and whenever a frame gains input focus. (add-hook 'focus-in-hook 'my-configure-visible-bell)
For XEmacs you can set the visible bell not to flash the whole screen (as is default in XEmacs) but only the top and bottom line (default in GnuEmacs) by setting the visible-bell to anything other than t or nil:
(setq visible-bell 'top-bottom)
XEmacs offers an option to set the variable bell-volume. The following turns off the bell absolutely:
(setq bell-volume 0)
I use the following to make the Scroll Lock LED blink:
(setq ring-bell-function (lambda () (call-process-shell-command "xset led 3; xset -led 3" nil 0 nil)))
Two points. The LED numbers are different on different hardware. And on some systems the Scroll Lock LED doesn’t respond if the command to turn it on and off are sent in the same system call. A more robust way to get the same effect is to write:
(setq ring-bell-function
(lambda ()
(call-process-shell-command "xset led named 'Scroll Lock'")
(call-process-shell-command "xset -led named 'Scroll Lock'"))The following version of caps lock flash uses emacs-deferred for async, timing and error checking.
(defvar flash-scroll-lock-enabled t) (defvar flash-scroll-lock-active nil) (defun flash-scroll-lock () (interactive) (when (and (not flash-scroll-lock-active) flash-scroll-lock-enabled) (setq flash-scroll-lock-active t) (deferred:$ (deferred:$ (deferred:process "xset" "led" "named" "Scroll Lock") (deferred:nextc it (lambda () (deferred:wait 300))) (deferred:nextc it (lambda () (deferred:process "xset" "-led" "named" "Scroll Lock"))) (deferred:nextc it (lambda () (deferred:wait 200))) (deferred:nextc it (lambda () (setq flash-scroll-lock-active nil)))) (deferred:error it (lambda (err) (setq flash-scroll-lock-active nil flash-scroll-lock-enabled nil))))))
Seems like the Scroll Lock LED may be useful after all ;) – mina86