From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id 0B7mG9O0zF4VPwAA0tVLHw (envelope-from ) for ; Tue, 26 May 2020 06:18:59 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0 with LMTPS id eBDnF9O0zF4WBgAA1q6Kng (envelope-from ) for ; Tue, 26 May 2020 06:18:59 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id E85709404CE for ; Tue, 26 May 2020 06:18:58 +0000 (UTC) Received: from localhost ([::1]:44112 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jdSvR-0004bT-U0 for larch@yhetil.org; Tue, 26 May 2020 02:18:57 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33858) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jdSv4-0004bA-Vx for emacs-orgmode@gnu.org; Tue, 26 May 2020 02:18:35 -0400 Received: from mail-pg1-x52d.google.com ([2607:f8b0:4864:20::52d]:34829) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jdSv3-0001ga-LU for emacs-orgmode@gnu.org; Tue, 26 May 2020 02:18:34 -0400 Received: by mail-pg1-x52d.google.com with SMTP id t11so9532876pgg.2 for ; Mon, 25 May 2020 23:18:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:mime-version; bh=wowatgkVnP6gIuvQOZ5RhxApOQCvjqX56GG2fizYRxc=; b=ZwZOgSp7HJbK7+63CyTQT+cYw27l8vdy6l/Izwh4xLtN7KtXhBdAFJRom8Nk5HWZOW WjCIVs81AEwNDOapYh9QjvaGTrgLpeDZxHk4szyprpfPVCpWa4Zg1AgOnGYbJ2/InRz2 DR1ReYp7/MVYCjjI8oeplL8Uoa2NsRc08vV4Dn4ANZUOr+BXiL9DOGwCygqvwnucKZPR DoVyJDnamZbX+BUAXHzIuxmGZcDf8rSKQ3W+hWrb6lkhazoXelJ02w/R0aP2QMArKBXO J/zbEVfBsAKnsBVTdDp+fK5Xxk2Y883X9qbG3jH6vQF8as4Qdx1QZMTBs41CmcLkgANE fvaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:mime-version; bh=wowatgkVnP6gIuvQOZ5RhxApOQCvjqX56GG2fizYRxc=; b=kwcye9lFMD+FiEqB8Y/TyxxUSY4qVX/TN3OhK8Y8wCnvxu/jXjD7DI45YYN1XcUoa2 fD8V1NQXNGQ/GY1uXKKwHJ+EG3lfZPrXrzbiAD8ABWsdIpmhK55dtiGANkH6WBtvOsQX GRvUm8yypvEzmlz93FEkao0BUbc4Et1W8qVQ6vqhIRE3u9gaVPAOVDcDdi4IUNE6g05m i2ulSdo1Xqs6HwF6It9Oo1nkC7L3zfrbfwwg5r2VhxnQp+ubiqpEdfeD+SQjur1NGXjT p1XnhXhen7/pbwh/zcvKLwJhN+pzARqyJxNeSIYz1nkT8j0TpuSKU9a5KrEJrkS/Beig 2C9w== X-Gm-Message-State: AOAM5319MJxPg2UFtIoTkypj32I7tKZC856+WOijoykyuxlip+02e+ZT TOtxtIC5CxRRKj5oxK4f17XAYqErM//YyA== X-Google-Smtp-Source: ABdhPJxd0XTthD1g3wneWiq98PwpDvgq4e6wjjW9SzJ6+drh7Qu6j/xGR/p2LymR6JA9zLHRMxpYWA== X-Received: by 2002:a63:e008:: with SMTP id e8mr29342839pgh.451.1590473911627; Mon, 25 May 2020 23:18:31 -0700 (PDT) Received: from localhost ([23.27.206.164]) by smtp.gmail.com with ESMTPSA id mn19sm14085687pjb.8.2020.05.25.23.18.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 May 2020 23:18:30 -0700 (PDT) From: Ihor Radchenko To: "emacs-orgmode@gnu.org" Subject: Using font-lock to align tags in org buffers Date: Tue, 26 May 2020 14:13:51 +0800 Message-ID: <87r1v71aw0.fsf@localhost> MIME-Version: 1.0 Content-Type: text/plain Received-SPF: pass client-ip=2607:f8b0:4864:20::52d; envelope-from=yantar92@gmail.com; helo=mail-pg1-x52d.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Scanner: scn0 Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20161025 header.b=ZwZOgSp7; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (aspmx1.migadu.com: domain of emacs-orgmode-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=emacs-orgmode-bounces@gnu.org X-Spam-Score: -1.21 X-TUID: CsTSN5sPOfqu 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