Subject: bug#52129: 29.0.50; Wish: Extend `:align-to center' to work on lines in buffer

From: Arthur Miller <[email-masked]>
Date: Sat, 27 Nov 2021 09:47:58 +0100

Eli Zaretskii <[email-masked]> writes:

>> From: Arthur Miller <[email-masked]>
>> Date: Fri, 26 Nov 2021 21:01:11 +0100
>> 
>> I am not sure if the title is descriptive enough, or even correct, but as seen
>> from attached code and image, I am trying to align two strings in a center of
>> each strings line.
>> 
>> It works now when string-pixel-width correctly calculates pixel widths. However,
>> I would prefer if I could just use :align-to center as a spec for a string as
>> property. Currently we have to use space as a spacer as we used to use
>> gif-spacers back in time.
>> 
>> I am not sure about how semantics would work, there are several cases to
>> consider, but generally if a string or a stretch of text in a string were given
>> text property 'display '(:align-to center), then Emacs would align that stretch
>> of text in the middle of the line that text occupies.
>
> I don't understand what exactly are you asking for.  Is your problem
> that you have to prepend some character to the string and put the
> :align-to spec on that character?  Because AFAIU that simple measure
> should achieve your goal.

You mean something like this:

#+begin_src emacs-lisp
(defun evc--time ()
  (propertize
   (time-stamp-string " %H:%M") 'face evc--time-face 'display '(space :align-to center)))

(defun evc--date ()
  (propertize
   (concat
    " "
    (capitalize (time-stamp-string "%A")) ". "
    (capitalize (time-stamp-string "%B %d"))) 'face evc--date-face 'display '(space :align-to center)))

(defun evc--update ()
  (let ((time (evc--time))
        (date (evc--date)))
    (when (frame-live-p evc--frame)
      (select-frame evc--frame))
    (with-current-buffer evc--buffer
      (erase-buffer)
      (insert time "\n" date))))
#+end_src

That "simple measure" does not achieve the goal of aligning involved strings to
center; or I don't know understand how you mean.

This does:

#+begin_src emacs-lisp
(defun evc--time ()
  (propertize
   (time-stamp-string " %H:%M") 'face evc--time-face))

(defun evc--date ()
  (propertize
   (concat
    (capitalize (time-stamp-string "%A")) ". "
    (capitalize (time-stamp-string "%B %d"))) 'face evc--date-face))

(let* ((tlen (* 0.5 (string-pixel-width time)))
             (dlen (* 0.5 (string-pixel-width date)))
             (spct (propertize " " 'display `(space :align-to (- center (,tlen)))))
             (spcd (propertize " " 'display `(space :align-to (- center (,dlen))))))
        (insert spct time "\n" spcd date))
#+end_src

>                         please show some simple code to explain your
> requests.

I hope above illustrates that it is not just to prepend a space, you have to
calculate how much it should extend to. This has to be done for each line.

Also, even if it would be just to prepend a spacer character, the goal is still
to align text in center of a line, so why can't we just renderer to do it for
us, instead of having such procedural way of telling it what to do. Why can we
not just tell:

(defun evc--time ()
  (propertize
   (time-stamp-string " %H:%M") 'face evc--time-face 'display '(line :align-to center)))

or something similar.

Also what if the text is longer then visual line? Would it be possible for the
rendering engine to center part of the tring with 'aligne-to center' property,
and cull extensive part on sides that are outside. That is not acheavable with a spacer.