diff options
| author | Craig Jennings <c@cjennings.net> | 2026-05-26 14:00:35 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2026-05-26 14:00:35 -0500 |
| commit | 6319274992aaa88b820c98d8c06b93717105f183 (patch) | |
| tree | 0324cc90a52fc6ede62c4ae19a93499c593e50c3 /modules/org-config.el | |
| parent | db83a4975be50b5c579b0a075acd952f8135a73f (diff) | |
| download | dotemacs-6319274992aaa88b820c98d8c06b93717105f183.tar.gz dotemacs-6319274992aaa88b820c98d8c06b93717105f183.zip | |
feat(org): right-align headline tags to the window edge
org-tags-column only right-aligns tags to a fixed column, and it does so by baking literal spaces into the file, so it can't track window width or splits. I set it to 0 (org keeps a single space, no padding) and add a font-lock rule that stretches that space with a display property pinned to the window's right edge via :align-to. That value resolves at redisplay, so the tags follow the window width and adapt to splits live, and nothing alignment-specific is written to disk.
The tags stop one column short of the edge on purpose. A glyph filling the final column wraps a non-truncated line, which dropped every tagged heading's tags onto the next line in the first cut. org-agenda-tags-column gets 'auto, the native equivalent for agenda views. Tests cover the heading regexp and the align-spec helper.
Diffstat (limited to 'modules/org-config.el')
| -rw-r--r-- | modules/org-config.el | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/modules/org-config.el b/modules/org-config.el index 90dd09b0..abcc35ac 100644 --- a/modules/org-config.el +++ b/modules/org-config.el @@ -90,6 +90,55 @@ (setq org-fontify-whole-heading-line t) ;; fontify the whole line for headings (for face-backgrounds) (add-hook 'org-mode-hook 'prettify-symbols-mode)) +;; ----------------------- Right-Aligned Org Tags ------------------------------ +;; org-tags-column only right-aligns tags to a fixed column by baking literal +;; spaces into the file, so it can't track window width. Instead, set the +;; column to 0 (org keeps a single space, no padding) and stretch that space +;; with a display property pinned to the window's right edge. `:align-to' is +;; resolved at redisplay, so the tags follow window width and splits live and +;; nothing alignment-specific is written to disk. Agenda has a native +;; equivalent (`org-agenda-tags-column' 'auto'). + +(setq org-tags-column 0) +(setq org-agenda-tags-column 'auto) + +(defconst cj/org-tag-line-re + "^\\*+ .*?\\([ \t]\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$" + "Match an org headline ending in tags. +Group 1 is the single gap character before the tags; group 2 is the tag +string itself.") + +(defconst cj/org-tag-right-margin 1 + "Columns of gap left between right-aligned tags and the window's right edge. +At least 1: a glyph filling the final column makes a non-truncated line wrap, +so the tags must stop short of the edge.") + +(defun cj/org--tag-align-spec (tag-string) + "Return a display spec that right-aligns TAG-STRING to the window edge. +The spec stretches a space so the tag ends `cj/org-tag-right-margin' columns +short of the window's right edge, leaving the tag flush right without wrapping +the line." + `(space :align-to (- right ,(+ (string-width tag-string) cj/org-tag-right-margin)))) + +(defconst cj/org-right-align-tags-keyword + `((,cj/org-tag-line-re + (1 (progn + (put-text-property + (match-beginning 1) (match-end 1) + 'display (cj/org--tag-align-spec (match-string 2))) + nil) + t))) + "Font-lock keyword right-aligning org headline tags via a display property. +The stretched space before the tag string is pinned to the window's right +edge, less the tag width.") + +(defun cj/org--manage-tag-display-prop () + "Let font-lock clear the right-align display property on refontify." + (add-to-list 'font-lock-extra-managed-props 'display)) + +(add-hook 'org-mode-hook #'cj/org--manage-tag-display-prop) +(font-lock-add-keywords 'org-mode cj/org-right-align-tags-keyword t) + ;; ----------------------------- Org TODO Settings --------------------------- (defun cj/org-todo-settings () |
