Colored tags based on regex emacs org-mode

I am using org-mode and all my tags starting with @ will be colored blue. Is it possible and how to do this?

Best wishes

+5
source share
2 answers

The following answer uses the org-mode built-in mechanisms. The variable org-tag-faces accepts a regular expression for the tag, which is the cons cell car . The org-set-tag-faces function sets the global variable org-tags-special-faces-re , which combines the tags of the above cons cell (s). The global variable org-tags-special-faces-re used by org-font-lock-add-tag-faces to re-search-forward through the org-mode buffer - placing the appropriate tags and applying the corresponding face based on the org-get-tag-face function org-get-tag-face . In the original version of the org-get-tag-face function, an exact match was found for the found tag (i.e., the key argument of the assoc function). The revised version of org-get-tag-face adds an additional key search @.* And returns the correct face if key is found - this is necessary because the tag itself will usually look something like @home or @office , while our contextual regexp is @.* .

 (require 'org) (add-to-list 'org-tag-faces '("@.*" . (:foreground "cyan"))) ;; Reset the global variable to nil, just in case org-mode has already beeen used. (when org-tags-special-faces-re (setq org-tags-special-faces-re nil)) (defun org-get-tag-face (kwd) "Get the right face for a TODO keyword KWD. If KWD is a number, get the corresponding match group." (if (numberp kwd) (setq kwd (match-string kwd))) (let ((special-tag-face (or (cdr (assoc kwd org-tag-faces)) (and (string-match "^@.*" kwd) (cdr (assoc "@.*" org-tag-faces)))))) (or (org-face-from-face-or-color 'tag 'org-tag special-tag-face) 'org-tag))) 
+4
source

You can use font-lock-add-keywords , for example, to evaluate the next source of the organizationโ€™s source code, the blue '@' tag should be colored.

 #+TITLE: Tag face #+BEGIN_SRC emacs-lisp (defface org-tag-face '((nil :foreground "blue" :background "#f7f7f7")) "org tag face") (font-lock-add-keywords 'org-mode '((":\\(@[^\:]+\\):" (1 'org-tag-face)))) #+END_SRC * test :@tst: * test2 :tst: 

After evaluating, return the buffer or call font-lock-flush and font-lock-ensure to update the font lock.

+1
source

Source: https://habr.com/ru/post/1260546/


All Articles