1. Run 'emacs -Q'
2. Open a new Org file 'C-x C-f /tmp/test.org'
3. Insert the following contents:

* TODO this is a test                                                   :tag:
* TODO here is another test                                             :tag:

The tags should align to the right with the standard monospaced font.

4. Run 'M-x variable-pitch-mode'.
5. The tags are now misaligned.

Please see attached screenshots for the before and after states of the
'variable-pitch-mode' activation.

Running 'M-x describe-char' in the space between the heading's title and
the tag informs us that the space's face is the same as the heading's
(e.g. 'org-level-1').

I am also relaying the technical insight of Adam Spiers.  Please see
attached plain text document or visit the comment's URL:
https://gitlab.com/protesilaos/modus-themes/-/issues/85#note_407147422

Overview of my setup:

+ GNU Emacs 27.1 (build 2, x86_64-pc-linux-gnu, GTK+ Version 3.24.22,
  cairo version 1.17.3) of 2020-08-14

+ Org mode version 9.3 (release_9.3 @ /usr/share/emacs/27.1/lisp/org/)

* * *

This message was also posted on debbugs.gnu.org, bug#43272.

-- 
Protesilaos Stavrou
protesilaos.com
I've investigated this and it's not nearly as easy as I had hoped.  It turns 
out that the right alignment supported by display properties can only align 
space, not text.  Something like this works as an experiment:

```lisp
(with-current-buffer "org/TODO.org"
  (put-text-property (region-beginning) (region-end)
                     'display '(space :align-to (- right 10))))
```

If a region of space characters is selected, the above will adjust the region's 
width so that its right hand side is 10 character widths in from the right 
margin.  However if the region contains text, it will make that text invisible.

This could be used to *left*-align the tags, but not *right*-align them, which 
doesn't really work because each heading's tags can be a different number of 
characters wide.

So to get our desired behaviour of adjusting the space in between the heading 
and the tags correctly in order to right-align the tags to [the right margin 
specified by 
`org-tags-column`](https://lists.gnu.org/archive/html/bug-gnu-emacs/2020-09/msg00728.html),
 we'd first have to calculate the display width of the (proportionally spaced) 
tags text, and then subtract that from the right margin.

In theory this width can be retrieved via `pos-visible-in-window-p` with the 
`PARTIALLY` argument set to `t`, but I haven't got that working yet, and even 
if I could, it would need to be done dynamically for each line and then the 
corresponding text property updated whenever that width changed (e.g. any of 
that line was edited, or when the window was scrolled to bring different 
headings into view).

However that raises questions about how well this would perform, since it would 
be creating quite a lot of work to do on each redisplay.  I suspect that the 
emacs gurus would say that support for right-alignment of variable pitch text 
would need to be added to emacs's internal display routines (the ones written 
in C, not Elisp).  But that's only a vague guess.  Hopefully I've 
underestimated what emacs can currently do here and there's an easier solution, 
but there's little chance of finding out either way without asking on the Org 
list, and then falling back to the `help-gnu-emacs` or `emacs-devel` list if 
it's even too difficult a question for any of the Org gurus.

Reply via email to