all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* right aligning a string, using display (space :align-to
@ 2023-10-19 18:05 martianhiatus
  2023-10-20  6:41 ` Eli Zaretskii
  0 siblings, 1 reply; 6+ messages in thread
From: martianhiatus @ 2023-10-19 18:05 UTC (permalink / raw)
  To: help-gnu-emacs

hi emacs helpers,

I'm trying to right-align a string for display as part of mastodon.el, and I would like the alignment to handle text scaling.

currently I have a rough working version that doesn't handle scaling, but it is also confusing me about how pixel-width display specs work. I have studied https://www.gnu.org/software/emacs/manual/html_node/elisp/Pixel-Specification.html for a while, and I still don't understand how it works, as the manual provides almost no examples of how to use and combine the items in from its long list of dsl expressions.

what i'm aiming to do is create a space that is as wide as screen width minus the length of my string.

currently, what works (but doesn't scale) is this (where str is the string to align):

(concat
  (propertize " "
              'display
              `(space :align-to (- right
                                   ,(+ (length stats) 7))))
str)

this returns a display prop of (space :align-to (- right 18)).

whereas working with the pixel-width of str does not work at all:

(concat
  (propertize " "
              'display
              `(space :align-to (- right
                                   ,(string-pixel-width stats))))
  str)

this returns a display prop of: (space :align-to (- right 142))


why does using length in a piexel-width expression work at all, and why doesn't pixel-width? i've also tried using right-margin and ,(car (window-text-pixel-size)) in place of right, and have tried various alternatives in terms of (quasi-)quoting.

if the former works and the latter doesn't, am i not constructing a correct pixel-width expression, such that it is just counting columns? and will using pixel-width correctly solve my scaling problems?

thanks,
marty



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: right aligning a string, using display (space :align-to
  2023-10-19 18:05 martianhiatus
@ 2023-10-20  6:41 ` Eli Zaretskii
  0 siblings, 0 replies; 6+ messages in thread
From: Eli Zaretskii @ 2023-10-20  6:41 UTC (permalink / raw)
  To: help-gnu-emacs

> Date: Thu, 19 Oct 2023 20:05:05 +0200
> From: martianhiatus@riseup.net
> 
> what i'm aiming to do is create a space that is as wide as screen width minus the length of my string.
> 
> currently, what works (but doesn't scale) is this (where str is the string to align):
> 
> (concat
>   (propertize " "
>               'display
>               `(space :align-to (- right
>                                    ,(+ (length stats) 7))))
> str)
> 
> this returns a display prop of (space :align-to (- right 18)).
> 
> whereas working with the pixel-width of str does not work at all:
> 
> (concat
>   (propertize " "
>               'display
>               `(space :align-to (- right
>                                    ,(string-pixel-width stats))))
>   str)
> 
> this returns a display prop of: (space :align-to (- right 142))
> 
> 
> why does using length in a piexel-width expression work at all, and why doesn't pixel-width? i've also tried using right-margin and ,(car (window-text-pixel-size)) in place of right, and have tried various alternatives in terms of (quasi-)quoting.

As the manual says, by default the :align-to expressions are
interpreted as being in character units, i.e. in units of columns, not
pixels.  You need to use (NUMBER) to have NUMBER interpreted as
pixels.  So your second snippet should be changed to

  (concat
    (propertize " "
		'display
		`(space :align-to (- right
				     ,(list (string-pixel-width stats)))))
    str)

and then it will work.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: right aligning a string, using display (space :align-to
@ 2023-10-20 16:06 martianhiatus
  2023-10-20 18:06 ` Eli Zaretskii
  0 siblings, 1 reply; 6+ messages in thread
From: martianhiatus @ 2023-10-20 16:06 UTC (permalink / raw)
  To: help-gnu-emacs


> As the manual says, by default the :align-to expressions are
> interpreted as being in character units, i.e. in units of columns, not
> pixels.  You need to use (NUMBER) to have NUMBER interpreted as
> pixels.  So your second snippet should be changed to
> 
> (concat
>  (propertize " "
>              'display
>              `(space :align-to (- right
>                                   ,(list (string-pixel-width stats)))))
>  str)
> 
> and then it will work.

hi eli, thanks. i had tried to (list) the whole expression following :align-to.

it does work, but it still doesn't work with text-scaling. if i increase text-scale, my right-aligned string is broken over to the next line, same as when not using pixel-width alignment. if i decrease text scaling, my right-aligned string is further from the right margin.

i thought the two might be linked, but the behavior is the same.

do you have any ideas about how to not break :align-to while text scaling?

m



-- 
some writing: https://anarchive.mooo.com
an internets: https://pleasantlybabykid.tumblr.com/
.
xmpp: mousebot@ghost.noho.st
.
gpg pub key: 0x582C8EAF0B0D77C9
fingerprint: DA24 B943 36EF C491 E22F A70B 582C 8EAF 0B0D 77C9



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: right aligning a string, using display (space :align-to
  2023-10-20 16:06 martianhiatus
@ 2023-10-20 18:06 ` Eli Zaretskii
  0 siblings, 0 replies; 6+ messages in thread
From: Eli Zaretskii @ 2023-10-20 18:06 UTC (permalink / raw)
  To: help-gnu-emacs

> Date: Fri, 20 Oct 2023 18:06:31 +0200
> From: martianhiatus@riseup.net
> 
> 
> > (concat
> >  (propertize " "
> >              'display
> >              `(space :align-to (- right
> >                                   ,(list (string-pixel-width stats)))))
> >  str)
> > 
> > and then it will work.
> 
> hi eli, thanks. i had tried to (list) the whole expression following :align-to.
> 
> it does work, but it still doesn't work with text-scaling. if i increase text-scale, my right-aligned string is broken over to the next line, same as when not using pixel-width alignment. if i decrease text scaling, my right-aligned string is further from the right margin.
> 
> i thought the two might be linked, but the behavior is the same.
> 
> do you have any ideas about how to not break :align-to while text scaling?

You assume that string-pixel-width takes text-scaling into account?  I
don't think it does; you should use window-text-pixel-size or
buffer-text-pixel-size instead.  That's because text-scaling is a
buffer-local feature, and string-pixel-width is not told which buffer
you have in mind.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: right aligning a string, using display (space :align-to
@ 2023-10-20 20:05 martianhiatus
  2023-10-21  6:35 ` Eli Zaretskii
  0 siblings, 1 reply; 6+ messages in thread
From: martianhiatus @ 2023-10-20 20:05 UTC (permalink / raw)
  To: help-gnu-emacs

(pls cc me so i can easily respond / quote)

> You assume that string-pixel-width takes text-scaling into account?  I
> don't think it does; you should use window-text-pixel-size or
> buffer-text-pixel-size instead.  That's because text-scaling is a
> buffer-local feature, and string-pixel-width is not told which buffer
> you have in mind.

I'm just asking for any help i can get regarding how to right-align a string in a way that isn't broken by text-scaling. i don't know how to go about it.

i'm only using string-pixel-width because i thought i needed to subtract it from "right" in the pixel-width spec, as quoted previously. i don't see how the window or buffer functions can help with that that, as i'd have to subtract my string's pixel width from them, so they'd need to replace "right", rather than my string width calculation, it seems to me. perhaps you had in mind another way to use them, but didn't say.




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: right aligning a string, using display (space :align-to
  2023-10-20 20:05 right aligning a string, using display (space :align-to martianhiatus
@ 2023-10-21  6:35 ` Eli Zaretskii
  0 siblings, 0 replies; 6+ messages in thread
From: Eli Zaretskii @ 2023-10-21  6:35 UTC (permalink / raw)
  To: help-gnu-emacs

> Date: Fri, 20 Oct 2023 22:05:40 +0200
> From: martianhiatus@riseup.net
> 
> (pls cc me so i can easily respond / quote)

That's not how newsgroups work: the response goes to the newsgroup.

> > You assume that string-pixel-width takes text-scaling into account?  I
> > don't think it does; you should use window-text-pixel-size or
> > buffer-text-pixel-size instead.  That's because text-scaling is a
> > buffer-local feature, and string-pixel-width is not told which buffer
> > you have in mind.
> 
> I'm just asking for any help i can get regarding how to right-align a string in a way that isn't broken by text-scaling. i don't know how to go about it.
> 
> i'm only using string-pixel-width because i thought i needed to subtract it from "right" in the pixel-width spec, as quoted previously. i don't see how the window or buffer functions can help with that that, as i'd have to subtract my string's pixel width from them, so they'd need to replace "right", rather than my string width calculation, it seems to me. perhaps you had in mind another way to use them, but didn't say.

You didn't say enough about your code for me to know that
window-text-pixel-size is not going to help.  If so, another
possibility is to multiply the value returned by string-pixel-width by
the ratio

  (/ (float (default-font-width)) (frame-char-width))

which should scale the string width according to text-scale-mode.



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-10-21  6:35 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-10-20 20:05 right aligning a string, using display (space :align-to martianhiatus
2023-10-21  6:35 ` Eli Zaretskii
  -- strict thread matches above, loose matches on Subject: below --
2023-10-20 16:06 martianhiatus
2023-10-20 18:06 ` Eli Zaretskii
2023-10-19 18:05 martianhiatus
2023-10-20  6:41 ` Eli Zaretskii

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.