elisp icon indicating copy to clipboard operation
elisp copied to clipboard

org-variable-pitch: Right-justify or align tags

Open ksafford opened this issue 5 years ago • 4 comments

Tags in an org-mode file are jagged when org-variable-pitch-minor-mode is enabled. It would be nicer if they were either aligned or right-justified. I'm not sure if this is within the scope of this project.

Given some guidance, I'd be happy to take a crack at this. Screen Shot 2020-09-04 at 8 39 21 AM

ksafford avatar Sep 02 '20 14:09 ksafford

Hi, thanks for the report. I'll try to reproduce this, but this is happening probably because the headline is in variable pitch too. Org probably aligns tags to the right edge using window width or fill column measured in characters. In that case you could add org-level-* faces to org-variable-pitch-fixed-faces or set org-tags-column to 0 causing them to not be right-justified but simply appended to the title with a preceding space.

I doubt we can both keep the headlines in variable pitch and align tags perfectly, but I'll look into it.

In any case could you add a screenshot for clarity? And maybe relevant parts of your config?

cadadr avatar Sep 03 '20 03:09 cadadr

I've uploaded a screen shot showing a sample org-mode buffer both with org-variable-pitch enabled and disabled. I will try adding the the org-level faces to -org-variable-pitch-fixed-faces some time today. Thanks for the reply!

ksafford avatar Sep 04 '20 13:09 ksafford

You're welcome. I was going to close this issue, but I'll keep it open because it might be possible to handle this in a neater way later, but that'll take a bit of a research.

cadadr avatar Sep 11 '20 12:09 cadadr

Note to self:

The following piece of code could be helpful in resolving this:

(use-package org
  :if init-flag
  :custom
  (org-auto-align-tags nil)
  :config
  ;; (add-hook 'org-mode-hook (lambda () (font-lock-add-keywords 'org-mode '(yant/org-align-tags) t)) 100)
  (add-hook! 'org-mode-hook (add-to-list 'font-lock-extra-managed-props 'org-tag-aligned))

  (add-hook! 'org-mode-hook (setq-local adaptive-fill-function #'org-adaptive-fill-function))
  (defun org-adaptive-fill-function ()
    "Fill headlines to the beginning of headline in org."
    (save-excursion
      (when (org-at-heading-p)
	(beginning-of-line)
        (looking-at org-complex-heading-regexp)
        (goto-char (match-beginning 4)) ;; at headline
        (make-string (current-column) ?\ ))))

  (defun yant/org-align-tags (limit &optional force)
    "Align all the tags in org buffer."
    (save-match-data
      (when (eq major-mode 'org-mode)
	(while (re-search-forward "^\\*+ \\(.+?\\)\\([ \t]+\\)\\(:\\(?:[^ \n]+:\\)+\\)$" limit t)
	  (when (and (match-string 2)
		     (or force
			 (not (get-text-property (match-beginning 2) 'org-tag-aligned))))
	    (with-silent-modifications
              (put-text-property (match-beginning 2) (match-end 2) 'org-tag-aligned t)
	      (put-text-property (if (>= 2 (- (match-end 2) (match-beginning 2)))
				     (match-beginning 2)
				   ;; multiple whitespaces may mean that we are in process of typing
				   (1+ (match-beginning 2)))
				 (match-end 2)
				 'display
				 `(space . (:align-to (- right
							 (,(+ 3 ;; no idea, but otherwise it is sometimes not enough
							      (string-display-pixel-width org-ellipsis)
							      (string-display-pixel-width (or (match-string 3)
											      ""))))))))))))))

Taken with permission from here

cadadr avatar Dec 17 '20 18:12 cadadr

OVP no longer maintained, cf. b28a70665b8db82323b57c2ee2f6e019cb9b92d3

cadadr avatar Mar 18 '23 01:03 cadadr