Function: display-warning

display-warning is an autoloaded and byte-compiled function defined in warnings.el.gz.

Signature

(display-warning TYPE MESSAGE &optional LEVEL BUFFER-NAME)

Documentation

Display a warning message, MESSAGE.

TYPE is the warning type: either a custom group name (a symbol), or a list of symbols whose first element is a custom group name.
(The rest of the symbols represent subcategories, for warning purposes
only, and you can use whatever symbols you like.)

LEVEL should be either :debug, :warning, :error, or :emergency
(but see warning-minimum-level and warning-minimum-log-level).
Default is :warning.

:emergency -- a problem that will seriously impair Emacs operation soon
if you do not attend to it promptly.
:error -- data or circumstances that are inherently wrong.
:warning -- data or circumstances that are not inherently wrong,
but raise suspicion of a possible problem.
:debug -- info for debugging only.

BUFFER-NAME, if specified, is the name of the buffer for logging the warning. By default, it is *Warnings*. If this function has to create the buffer, it disables undo in the buffer.

See the warnings custom group for user customization features.

See also warning-series, warning-prefix-function, warning-fill-prefix, and warning-fill-column for additional programming features.

This will also display buttons allowing the user to permanently disable automatic display of the warning or disable the warning entirely by setting warning-suppress-types or warning-suppress-log-types on their behalf.

View in manual

Probably introduced at or before Emacs version 22.1.

Aliases

url-warn (obsolete since 28.1)

Source Code

;; Defined in /usr/src/emacs/lisp/emacs-lisp/warnings.el.gz
;;;###autoload
(defun display-warning (type message &optional level buffer-name)
  "Display a warning message, MESSAGE.
TYPE is the warning type: either a custom group name (a symbol),
or a list of symbols whose first element is a custom group name.
\(The rest of the symbols represent subcategories, for warning purposes
only, and you can use whatever symbols you like.)

LEVEL should be either :debug, :warning, :error, or :emergency
\(but see `warning-minimum-level' and `warning-minimum-log-level').
Default is :warning.

:emergency -- a problem that will seriously impair Emacs operation soon
	      if you do not attend to it promptly.
:error     -- data or circumstances that are inherently wrong.
:warning   -- data or circumstances that are not inherently wrong,
	      but raise suspicion of a possible problem.
:debug     -- info for debugging only.

BUFFER-NAME, if specified, is the name of the buffer for logging
the warning.  By default, it is `*Warnings*'.  If this function
has to create the buffer, it disables undo in the buffer.

See the `warnings' custom group for user customization features.

See also `warning-series', `warning-prefix-function',
`warning-fill-prefix', and `warning-fill-column' for additional
programming features.

This will also display buttons allowing the user to permanently
disable automatic display of the warning or disable the warning
entirely by setting `warning-suppress-types' or
`warning-suppress-log-types' on their behalf."
  (if (not (or after-init-time noninteractive (daemonp)))
      ;; Ensure warnings that happen early in the startup sequence
      ;; are visible when startup completes (bug#20792).
      (delay-warning type message level buffer-name)
    (unless level
      (setq level :warning))
    (unless buffer-name
      (setq buffer-name "*Warnings*"))
    (with-suppressed-warnings ((obsolete warning-level-aliases))
      (when-let ((new (cdr (assq level warning-level-aliases))))
        (warn "Warning level `%s' is obsolete; use `%s' instead" level new)
        (setq level new)))
    (or (< (warning-numeric-level level)
	   (warning-numeric-level warning-minimum-log-level))
	(warning-suppress-p type warning-suppress-log-types)
	(let* ((typename (if (consp type) (car type) type))
	       (old (get-buffer buffer-name))
	       (buffer (or old (get-buffer-create buffer-name)))
	       (level-info (assq level warning-levels))
               ;; `newline' may be unbound during bootstrap.
               (newline (if (fboundp 'newline) #'newline
                          (lambda () (insert "\n"))))
	       start end)
	  (with-current-buffer buffer
	    ;; If we created the buffer, disable undo.
	    (unless old
	      (when (fboundp 'special-mode) ; Undefined during bootstrap.
                (special-mode))
	      (setq buffer-read-only t)
	      (setq buffer-undo-list t))
	    (goto-char (point-max))
	    (when (and warning-series (symbolp warning-series))
	      (setq warning-series
		    (prog1 (point-marker)
		      (unless (eq warning-series t)
			(funcall warning-series)))))
	    (let ((inhibit-read-only t))
	      (unless (bolp)
		(funcall newline))
	      (setq start (point))
              ;; Don't output the button when doing batch compilation
              ;; and similar.
              (unless (or noninteractive (eq type 'bytecomp))
                (insert (buttonize (icon-string 'warnings-suppress)
                                   #'warnings-suppress type)
                        " "))
	      (if warning-prefix-function
		  (setq level-info (funcall warning-prefix-function
					    level level-info)))
	      (insert (format (nth 1 level-info)
			      (format warning-type-format typename))
		      message)
              (funcall newline)
	      (when (and warning-fill-prefix
                         (not (string-search "\n" message))
                         (not noninteractive))
		(let ((fill-prefix warning-fill-prefix)
		      (fill-column warning-fill-column))
		  (fill-region start (point))))
	      (setq end (point)))
	    (when (and (markerp warning-series)
		       (eq (marker-buffer warning-series) buffer))
	      (goto-char warning-series)))
	  (if (nth 2 level-info)
	      (funcall (nth 2 level-info)))
	  (cond (noninteractive
		 ;; Noninteractively, take the text we inserted
		 ;; in the warnings buffer and print it.
		 ;; Do this unconditionally, since there is no way
		 ;; to view logged messages unless we output them.
		 (with-current-buffer buffer
		   (save-excursion
		     ;; Don't include the final newline in the arg
		     ;; to `message', because it adds a newline.
		     (goto-char end)
		     (if (bolp)
			 (forward-char -1))
		     (message "%s" (buffer-substring start (point))))))
		((and (daemonp) (null after-init-time))
		 ;; Warnings assigned during daemon initialization go into
		 ;; the messages buffer.
		 (message "%s"
			  (with-current-buffer buffer
			    (save-excursion
			      (goto-char end)
			      (if (bolp)
				  (forward-char -1))
			      (buffer-substring start (point))))))
		(t
		 ;; Interactively, decide whether the warning merits
		 ;; immediate display.
		 (or (< (warning-numeric-level level)
			(warning-numeric-level warning-minimum-level))
		     (warning-suppress-p type warning-suppress-types)
		     (let ((window (display-buffer buffer)))
		       (when (and (markerp warning-series)
				  (eq (marker-buffer warning-series) buffer))
			 (set-window-start window warning-series))
		       (sit-for 0)))))))))