Function: standard-display-by-replacement-char

standard-display-by-replacement-char is an autoloaded, interactive and byte-compiled function defined in disp-table.el.gz.

Signature

(standard-display-by-replacement-char &optional REPL FROM TO)

Documentation

Produce code to display characters between FROM and TO using REPL.

This function produces a buffer with code to set up standard-display-table such that characters that cannot be displayed by the terminal, and don't already have their display set up in standard-display-table, will be represented by a replacement character. You can evaluate the produced code to use the setup for the current Emacs session, or copy the code into your init file, to make Emacs use it for subsequent sessions.

Interactively, the produced code arranges for any character in the range [#x100..#x10FFFF] that the terminal cannot display to be represented by the #xFFFD Unicode replacement character.

When called from Lisp, FROM and TO define the range of characters for which to produce the setup code for standard-display-table. If they are omitted, they default to #x100 and #x10FFFF respectively, covering the entire non-ASCII range of Unicode characters. REPL is the replacement character to use. If it's omitted, it defaults to #xFFFD, the Unicode replacement character, usually displayed as a black diamond with a question mark inside. The produced code sets up standard-display-table to show REPL with the homoglyph face, making the replacements stand out on display.

This command is most useful with text-mode terminals, such as the Linux console, for which Emacs has a reliable way of determining which characters can be displayed and which cannot.

Probably introduced at or before Emacs version 29.1.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/disp-table.el.gz
;;;###autoload
(defun standard-display-by-replacement-char (&optional repl from to)
  "Produce code to display characters between FROM and TO using REPL.
This function produces a buffer with code to set up `standard-display-table'
such that characters that cannot be displayed by the terminal, and
don't already have their display set up in `standard-display-table', will
be represented by a replacement character.  You can evaluate the produced
code to use the setup for the current Emacs session, or copy the code
into your init file, to make Emacs use it for subsequent sessions.

Interactively, the produced code arranges for any character in
the range [#x100..#x10FFFF] that the terminal cannot display to
be represented by the #xFFFD Unicode replacement character.

When called from Lisp, FROM and TO define the range of characters for
which to produce the setup code for `standard-display-table'.  If they
are omitted, they default to #x100 and #x10FFFF respectively, covering
the entire non-ASCII range of Unicode characters.
REPL is the replacement character to use.  If it's omitted, it defaults
to #xFFFD, the Unicode replacement character, usually displayed as a
black diamond with a question mark inside.
The produced code sets up `standard-display-table' to show REPL with
the `homoglyph' face, making the replacements stand out on display.

This command is most useful with text-mode terminals, such as the
Linux console, for which Emacs has a reliable way of determining
which characters can be displayed and which cannot."
  (interactive)
  (or repl
      (setq repl #xfffd))
  (or (and from to (<= from to))
      (setq from #x100
	    to (max-char 'unicode)))
  (let ((buf (get-buffer-create "*Display replacements*"))
	(ch from)
        (tbl standard-display-table)
	first)
    (with-current-buffer buf
      (erase-buffer)
      (insert "\
;; This code was produced by `standard-display-by-replacement-char'.
;; Evaluate the Lisp code below to make Emacs show the standard
;; replacement character as a substitute for each undisplayable character.
;; One way to do that is with \"C-x h M-x eval-region RET\".
;; Normally you would put this code in your Emacs initialization file,
;; perhaps conditionally based on the type of terminal, so that
;; this setup happens automatically on each startup.
(let ((tbl (or standard-display-table
               (setq standard-display-table (make-display-table)))))\n")
      (while (<= ch to)
	(cond
	 ((or (char-displayable-p ch)
	      (aref tbl ch))
	  (setq ch (1+ ch)))
	 (t
	  (setq first ch)
	  (while (and (<= ch to)
		      (not (or (char-displayable-p ch)
			       (aref tbl ch))))
	    (setq ch (1+ ch)))
	  (insert
	   "  (set-char-table-range tbl '("
	   (format "#x%x" first)
	   " . "
	   (format "#x%x" (1- ch))
	   ")\n\                        (vconcat (list (make-glyph-code "
	   (format "#x%x" repl) " 'homoglyph))))\n"))))
      (insert ")\n"))
    (pop-to-buffer buf)))