unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* :align-to space spec and line wrap
@ 2023-04-24 19:23 Qiantan Hong
  2023-04-25  7:22 ` Eli Zaretskii
  0 siblings, 1 reply; 4+ messages in thread
From: Qiantan Hong @ 2023-04-24 19:23 UTC (permalink / raw)
  To: emacs-devel@gnu.org

Hi,

I use the :align-to space spec to right align texts, like this:

(defun k-insert-fill-right (string)
  ;; More correct than `k-fill-right' in some cases, respect current
  ;; buffer settings (e.g. invisibility spec)
  (let ((from (point)))
    (insert " " string)
    (save-restriction
      (narrow-to-region (1+ from) (point))
      (let ((width (car (buffer-text-pixel-size))))
        (widen)
        (put-text-property from (1+ from)
                           'display
                           `(space :align-to (- right-fringe (,width))))))
    nil))

It works when there is no line wrap. However, when there is line wrap,
the space width shrink to zero rather than fill to (- right-fringe
(,width)) position on the new line. Seem that the width calculation
does not take line wrap into account. Is this intended? Is the
behavior that align to the right position on the new line possible to
implement?

I'm on GNU Emacs 29.0.50.


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

* Re: :align-to space spec and line wrap
  2023-04-24 19:23 :align-to space spec and line wrap Qiantan Hong
@ 2023-04-25  7:22 ` Eli Zaretskii
  2023-04-30 10:48   ` Qiantan Hong
  0 siblings, 1 reply; 4+ messages in thread
From: Eli Zaretskii @ 2023-04-25  7:22 UTC (permalink / raw)
  To: Qiantan Hong; +Cc: emacs-devel

> From: Qiantan Hong <qthong@stanford.edu>
> Date: Mon, 24 Apr 2023 19:23:48 +0000
> 
> I use the :align-to space spec to right align texts, like this:
> 
> (defun k-insert-fill-right (string)
>   ;; More correct than `k-fill-right' in some cases, respect current
>   ;; buffer settings (e.g. invisibility spec)
>   (let ((from (point)))
>     (insert " " string)
>     (save-restriction
>       (narrow-to-region (1+ from) (point))
>       (let ((width (car (buffer-text-pixel-size))))
>         (widen)
>         (put-text-property from (1+ from)
>                            'display
>                            `(space :align-to (- right-fringe (,width))))))
>     nil))
> 
> It works when there is no line wrap. However, when there is line wrap,
> the space width shrink to zero rather than fill to (- right-fringe
> (,width)) position on the new line. Seem that the width calculation
> does not take line wrap into account. Is this intended?

The :align-to display spec counts columns, and column number doesn't
get reset to zero when the line is continued on the next screen line.
The column numbers on the continuation lines keep counting from the
last column displayed on the previous screen line.

So you are in effect trying to align to a column whose number is
smaller than the column where the text is displayed, and thus the
space width indeed comes out as zero, because you are already past the
target column.

> Is the behavior that align to the right position on the new line
> possible to implement?

You need to dynamically calculate the value of the column for
continuation lines.



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

* Re: :align-to space spec and line wrap
  2023-04-25  7:22 ` Eli Zaretskii
@ 2023-04-30 10:48   ` Qiantan Hong
  2023-04-30 11:29     ` Eli Zaretskii
  0 siblings, 1 reply; 4+ messages in thread
From: Qiantan Hong @ 2023-04-30 10:48 UTC (permalink / raw)
  To: Eli Zaretskii, emacs-devel@gnu.org

Hi Eli,

It turns out that similar same thing also happen for horiontally
scroll. Once I horizontally scroll to the right, the space align to
somewhere in the middle of the screen, rather than visually to the
right.

I understand how current implementation will exhibit such behavior.
Is such behavior desired? The info node "40.16.3 Pixel Specification
for Spaces" says that `:align-to' specifies positions relative to
"text area", which is defined as a concept about window geometry
("29.1 Basic Concepts of Emacs Windows"). Therefore, I think the
desired behavior is to align to visual positions in the window.

I'd also argue this is also more useful in practice as it allows one
to display some text in desired horizontal position in the window.
I've yet to encounter a use case where the current buffer
position-based behavior is desired. It's a display property, after
all, and is probably more commonly used for displaying stuff!

Eli Zaretskii <eliz@gnu.org> writes:

>> From: Qiantan Hong <qthong@stanford.edu>
>> Date: Mon, 24 Apr 2023 19:23:48 +0000
>>
>> I use the :align-to space spec to right align texts, like this:
>>
>> (defun k-insert-fill-right (string)
>>   ;; More correct than `k-fill-right' in some cases, respect current
>>   ;; buffer settings (e.g. invisibility spec)
>>   (let ((from (point)))
>>     (insert " " string)
>>     (save-restriction
>>       (narrow-to-region (1+ from) (point))
>>       (let ((width (car (buffer-text-pixel-size))))
>>         (widen)
>>         (put-text-property from (1+ from)
>>                            'display
>>                            `(space :align-to (- right-fringe (,width))))))
>>     nil))
>>
>> It works when there is no line wrap. However, when there is line wrap,
>> the space width shrink to zero rather than fill to (- right-fringe
>> (,width)) position on the new line. Seem that the width calculation
>> does not take line wrap into account. Is this intended?
>
> The :align-to display spec counts columns, and column number doesn't
> get reset to zero when the line is continued on the next screen line.
> The column numbers on the continuation lines keep counting from the
> last column displayed on the previous screen line.
>
> So you are in effect trying to align to a column whose number is
> smaller than the column where the text is displayed, and thus the
> space width indeed comes out as zero, because you are already past the
> target column.
>
>> Is the behavior that align to the right position on the new line
>> possible to implement?
>
> You need to dynamically calculate the value of the column for
> continuation lines.

-- 
Best,
Qiantan


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

* Re: :align-to space spec and line wrap
  2023-04-30 10:48   ` Qiantan Hong
@ 2023-04-30 11:29     ` Eli Zaretskii
  0 siblings, 0 replies; 4+ messages in thread
From: Eli Zaretskii @ 2023-04-30 11:29 UTC (permalink / raw)
  To: Qiantan Hong; +Cc: emacs-devel

> From: Qiantan Hong <qthong@stanford.edu>
> Date: Sun, 30 Apr 2023 10:48:35 +0000
> 
> It turns out that similar same thing also happen for horiontally
> scroll. Once I horizontally scroll to the right, the space align to
> somewhere in the middle of the screen, rather than visually to the
> right.

Of course.  And for the same reason: the columns are still counted
from the (now invisible) left edge of the window, i.e. from the very
first column of the buffer text.

> I understand how current implementation will exhibit such behavior.
> Is such behavior desired? The info node "40.16.3 Pixel Specification
> for Spaces" says that `:align-to' specifies positions relative to
> "text area", which is defined as a concept about window geometry
> ("29.1 Basic Concepts of Emacs Windows"). Therefore, I think the
> desired behavior is to align to visual positions in the window.

Text area begins where text begins, even if that text is not visible
due to hscroll.  You are interpreting that text in the manual too
literally, and try to apply it to situations where the left edge of
the text area is not visible.

> I'd also argue this is also more useful in practice as it allows one
> to display some text in desired horizontal position in the window.
> I've yet to encounter a use case where the current buffer
> position-based behavior is desired. It's a display property, after
> all, and is probably more commonly used for displaying stuff!

Sorry, it's too late to change these basics of the window geometry, as
they have existed for the last 23 years.



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

end of thread, other threads:[~2023-04-30 11:29 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-04-24 19:23 :align-to space spec and line wrap Qiantan Hong
2023-04-25  7:22 ` Eli Zaretskii
2023-04-30 10:48   ` Qiantan Hong
2023-04-30 11:29     ` Eli Zaretskii

Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).