all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Daniel Mendler <mail@daniel-mendler.de>
To: Eli Zaretskii <eliz@gnu.org>, Lars Ingebrigtsen <larsi@gnus.org>
Cc: 47712@debbugs.gnu.org
Subject: bug#47712: 27.1; Provide `string-display-width` function, which takes properties into account, `substring-width`
Date: Tue, 13 Apr 2021 14:25:18 +0200	[thread overview]
Message-ID: <cd462c82-e278-1313-92cf-ca9471d94f71@daniel-mendler.de> (raw)
In-Reply-To: <83wnt61jj0.fsf@gnu.org>

On 4/13/21 2:00 PM, Eli Zaretskii wrote:
>> From: Lars Ingebrigtsen <larsi@gnus.org>
>>> Note that I would like to have an implementation of
>>> `substring-width`/`string-display-width`/`substring-display-width`
>>> which does not allocate, since this reduces the GC pressure when
>>> formatting many items. How do you think about this?
>>
>> Sure; I think a non-allocating function to count monospaced string width
>> would be useful.
> 
> I won't mount the barricades over this, but please keep in mind that
> even with fixed-pitch fonts this will not always produce correct
> results, because on GUI frames the "double-width" characters not
> always take exactly two columns (it depends on the font).  So this
> function will only work reliably on text-mode (a.k.a. TTY) frames and
> for Latin characters on GUI frames.
> 
> If you still think we should have such a semi-broken function, at
> least include some warning in its doc string, so that users wouldn't
> be unpleasantly surprised.

Yes, `string-width` is broken for face-dependent purposes. However there 
are still these handful of use cases which will probably not go away 
(org-mode table, formatting monospaced text, ...). In those existing 
cases the `string-width` function is often used in combination with 
`substring`, i.e., `(string-width (substring str beg end)`.

Therefore I would be happy with the following resolution:

1. Add two arguments begin and end to `string-width` to improve the 
current uses of `string-width`.

2. Document the caveats of `string-width` in the docstring (works only 
reliable in text mode for multi-width chars) and maybe mention 
`window-text-pixel-size` or the `string-pixel-width` function by Martin.

----

(defun string-pixel-width (string)
  (let ((buffer (get-buffer-create " *string-pixel-width*"))
        (old-buffer (window-buffer)))
    (unwind-protect
        (with-current-buffer buffer
          (erase-buffer)
          (insert string)
          (set-window-buffer (selected-window) buffer)
          (car (window-text-pixel-size nil (point-min) (point-max))))
      (set-window-buffer (selected-window) old-buffer))))

(I don't think it is needed to add the `string-pixel-width` function to 
the core. The function or some variant of it is easy enough for packages 
to include and it cannot be used in this form since it leads to these 
temporary buffers lying around.)





  reply	other threads:[~2021-04-13 12:25 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-11 21:16 bug#47712: 27.1; Provide `string-display-width` function, which takes properties into account, `substring-width` Daniel Mendler
2021-04-12  2:26 ` Eli Zaretskii
2021-04-12  8:45   ` Daniel Mendler
2021-04-12  8:53     ` Lars Ingebrigtsen
2021-04-12  9:08       ` Daniel Mendler
2021-04-13  8:01         ` Lars Ingebrigtsen
2021-04-13 12:00           ` Eli Zaretskii
2021-04-13 12:25             ` Daniel Mendler [this message]
2021-04-14  8:50               ` Eli Zaretskii
2021-04-14 10:49                 ` Daniel Mendler
2021-04-14 11:38                   ` Eli Zaretskii
2021-04-12 12:21     ` Eli Zaretskii
2021-04-12 12:50       ` Daniel Mendler
2021-04-12 13:21         ` Eli Zaretskii
2021-04-12 13:32           ` Daniel Mendler
2021-04-12 13:40             ` Eli Zaretskii
2021-04-12 14:05               ` Daniel Mendler
2021-04-12 14:15                 ` Eli Zaretskii
2021-04-12 14:32                   ` Eli Zaretskii
2021-04-12 14:38                     ` Daniel Mendler
2021-04-12 17:01                       ` Eli Zaretskii
2021-04-12 17:18                         ` Daniel Mendler
2021-04-13  7:06                         ` martin rudalics
2021-04-13 12:23                           ` Eli Zaretskii
2021-04-12 14:36                   ` Daniel Mendler
2021-04-12 17:09                     ` Eli Zaretskii
2021-04-12 17:13                       ` Daniel Mendler

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=cd462c82-e278-1313-92cf-ca9471d94f71@daniel-mendler.de \
    --to=mail@daniel-mendler.de \
    --cc=47712@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    --cc=larsi@gnus.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.