diff options
| -rw-r--r-- | modules/org-config.el | 49 | ||||
| -rw-r--r-- | tests/test-org-config-tag-alignment.el | 48 |
2 files changed, 97 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 () diff --git a/tests/test-org-config-tag-alignment.el b/tests/test-org-config-tag-alignment.el new file mode 100644 index 00000000..e999f261 --- /dev/null +++ b/tests/test-org-config-tag-alignment.el @@ -0,0 +1,48 @@ +;;; test-org-config-tag-alignment.el --- Right-aligned org tags -*- lexical-binding: t; -*- + +;;; Commentary: +;; cj/org-tag-line-re and cj/org--tag-align-spec drive the display-property +;; right-alignment of org headline tags (org-config.el). The regexp isolates +;; the gap before the tags (group 1) and the tag string (group 2); the spec +;; helper turns a tag string into the (space :align-to (- right WIDTH)) display +;; spec that pins the tags to the window's right edge. These exercise the pure +;; logic directly, without driving font-lock. + +;;; Code: + +(require 'ert) +(require 'org-config) + +(ert-deftest test-org-config-tag-align-spec-single-tag () + "Normal: the spec pins a tag to the right edge less its width plus margin." + (should (equal (cj/org--tag-align-spec ":work:") + `(space :align-to (- right ,(+ 6 cj/org-tag-right-margin)))))) + +(ert-deftest test-org-config-tag-align-spec-widths () + "Boundary: shortest and multi-tag strings carry their width plus the margin." + (should (equal (cj/org--tag-align-spec ":a:") + `(space :align-to (- right ,(+ 3 cj/org-tag-right-margin))))) + (should (equal (cj/org--tag-align-spec ":a:b:") + `(space :align-to (- right ,(+ 5 cj/org-tag-right-margin)))))) + +(ert-deftest test-org-config-tag-line-re-matches-tagged-heading () + "Normal: a tagged headline matches with the gap and tags captured." + (let ((line "** TODO Something :work:")) + (should (string-match cj/org-tag-line-re line)) + (should (equal (match-string 1 line) " ")) + (should (equal (match-string 2 line) ":work:")))) + +(ert-deftest test-org-config-tag-line-re-captures-last-gap-with-padding () + "Boundary: with baked padding, group 1 is the single space before the tags." + (let ((line "*** TODO [#C] Foo :a:b:")) + (should (string-match cj/org-tag-line-re line)) + (should (equal (match-string 1 line) " ")) + (should (equal (match-string 2 line) ":a:b:")))) + +(ert-deftest test-org-config-tag-line-re-rejects-untagged-and-non-heading () + "Error: an untagged heading and a non-heading line do not match." + (should-not (string-match cj/org-tag-line-re "** TODO Foo")) + (should-not (string-match cj/org-tag-line-re "not a heading :work:"))) + +(provide 'test-org-config-tag-alignment) +;;; test-org-config-tag-alignment.el ends here |
