bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#57693: 29.0.50; Is there a more reliable version of `char-displayabl


From: Ihor Radchenko
Subject: bug#57693: 29.0.50; Is there a more reliable version of `char-displayable-p'?
Date: Mon, 12 Sep 2022 13:46:50 +0800

Eli Zaretskii <eliz@gnu.org> writes:

> Here's what I suggest for GUI frames:
>
>   (defun insert-char-safely (ch repl)
>     "Insert character CH, if it can be displayed; otherwise insert REPL."
>     (insert ch)
>     (unless (font-at (1- (point)))
>       (delete-char -1)
>       (insert repl)))

Thanks!

> Do you need this to work for TTY frames as well?  If so, it could be a
> problem, since most terminal emulators don't provide a way of
> inquiring whether a certain character can be displayed.
> char-displayable-p currently just checks on most terminals that the
> terminal-coding-system can _encode_ the character, which isn't enough
> if the terminal encoding is UTF-8.

The original code I was referring to unconditionally used a "safe"
fallback on terminals. If there is no technical possibility to determine
whether a character can be displayed, so be it.

I am thinking about something like below (to insert string, not a char),

(defun org-insert-displayable (&rest strings)
  "Insert the first displayable string from STRINGS.
If none of the STRINGS can be displayed, display the last string.
In terminal, always insert the last string."
  (if (not (display-graphic-p))
      (insert (car (last strings)))
    (catch :displayable
      (dolist (string strings)
        (insert string)
        (save-excursion
          (catch :undisplayable
            (dotimes (i (length string))
              (unless (font-at (- (point) i 1))
                (throw :undisplayable t)))
            ;; All chars can be displayed. Keep the inserted string.
            (throw :displayable t)))
        ;; Some char cannot be displayed. Clear the insertion and move ahead to
        ;; the next candidate.
        (delete-char (- (length string))))
      ;; None displayable.
      (insert (car (last strings))))))

-- 
Ihor Radchenko,
Org mode contributor,
Learn more about Org mode at https://orgmode.org/.
Support Org development at https://liberapay.com/org-mode,
or support my work at https://liberapay.com/yantar92





reply via email to

[Prev in Thread] Current Thread [Next in Thread]