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

Previous Next

Package: emacs;

Reported by: Arthur Miller <arthur.miller <at> live.com>

Date: Fri, 26 Nov 2021 20:22:01 UTC

Severity: normal

Tags: wontfix

Found in version 29.0.50

Done: Lars Ingebrigtsen <larsi <at> gnus.org>

Bug is archived. No further changes may be made.

Full log


Message #11 received at 52129 <at> debbugs.gnu.org (full text, mbox):

From: Arthur Miller <arthur.miller <at> live.com>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: 52129 <at> debbugs.gnu.org
Subject: Re: bug#52129: 29.0.50; Wish: Extend `:align-to center' to work on
 lines in buffer
Date: Sat, 27 Nov 2021 09:47:58 +0100
Eli Zaretskii <eliz <at> gnu.org> writes:

>> From: Arthur Miller <arthur.miller <at> live.com>
>> 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.




This bug report was last modified 3 years and 176 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.