emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Ihor Radchenko <yantar92@gmail.com>
To: "emacs-orgmode@gnu.org" <emacs-orgmode@gnu.org>
Subject: Using font-lock to align tags in org buffers
Date: Tue, 26 May 2020 14:13:51 +0800	[thread overview]
Message-ID: <87r1v71aw0.fsf@localhost> (raw)

Hello,

Currently, org does tag alignment by adding a number of spaces before
the tags. This becomes messy when one changes the window size and turns
off line truncation.

I came up with an experimental code utilising 'display text property and
font-lock to align tags dynamically, as you resize the Emacs frame.

If anyone is interested, the code is below:

-------------------------------------------------------------------------
  (defun string-display-pixel-width (string &optional mode)
    "Calculate pixel width of STRING.
  Optional MODE specifies major mode used for display."
    (with-temp-buffer
      (with-silent-modifications
        (setf (buffer-string) string))
      (when (fboundp mode)
        (funcall mode)
        (font-lock-fontify-buffer))
      (if (not (get-buffer-window (current-buffer)))
  	(save-window-excursion
            ;; Avoid errors if the selected window is a dedicated one,
            ;; and they just want to insert a document into it.
            (set-window-dedicated-p nil nil)
  	  (set-window-buffer nil (current-buffer))
  	  (car (window-text-pixel-size nil (line-beginning-position) (point))))
        (car (window-text-pixel-size nil (line-beginning-position) (point))))))

  (setq org-auto-align-tags nil)
  (add-hook 'org-mode-hook (lambda () (font-lock-add-keywords nil '(yant/org-align-tags) t)) 100)
  (add-hook 'org-mode-hook (lambda () (add-to-list 'font-lock-extra-managed-props 'org-tag-aligned)))

  (defun yant/org-align-tags (limit)
    "Align all the tags in org buffer."
    (while (re-search-forward "^\\*+ \\(.+?\\)\\([ \t]+\\)\\(:\\(?:[^ \n]+:\\)+\\)$" limit t)
      (when (and (match-string 2)
		 (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 (match-beginning 2)
			     (match-end 2)
			     'display
			     `(space . (:align-to (- right
						     (,(+ (string-display-pixel-width org-ellipsis)
							  (string-display-pixel-width (or (match-string 3)
											  ""))))))))))))
-------------------------------------------------------------------------

The same approach can be used to align tags in agenda:

-------------------------------------------------------------------------
  (defun org-agenda-fix-tag-alignment ()
    "Use 'display :align-to instead of spaces in agenda."
    (goto-char (point-min))
    (setq-local word-wrap nil) ; tags would be moved to next line if `word-wrap'` is non-nil and `truncate-lines' is nil
    (while (re-search-forward org-tag-group-re nil 'noerror)
      (put-text-property (match-beginning 0) (match-beginning 1) 'display `(space . (:align-to (- right (,(string-display-pixel-width (match-string 1)))))))))
  
  (add-hook 'org-agenda-finalize-hook #'org-agenda-fix-tag-alignment)
-------------------------------------------------------------------------

Best,
Ihor


-- 
Ihor Radchenko,
PhD,
Center for Advancing Materials Performance from the Nanoscale (CAMP-nano)
State Key Laboratory for Mechanical Behavior of Materials, Xi'an Jiaotong University, Xi'an, China
Email: yantar92@gmail.com, ihor_radchenko@alumni.sutd.edu.sg



                 reply	other threads:[~2020-05-26  6:18 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87r1v71aw0.fsf@localhost \
    --to=yantar92@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).