Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Color tags based on regex emacs org-mode

Tags:

emacs

org-mode

I'm using org-mode and I want all my tags beginning with @ to be colored in blue. Is it possible and how to do it ?

Best regards

like image 921
boehm_s Avatar asked Nov 29 '16 21:11

boehm_s


2 Answers

The following answer uses the built-in mechanisms of org-mode. The variable org-tag-faces accepts a regexp for the tag, which is the car of the cons cell. The function org-set-tag-faces sets a global variable org-tags-special-faces-re, which combines the tags of the aforementioned cons cell(s). The global variable org-tags-special-faces-re is used by org-font-lock-add-tag-faces to re-search-forward through the org-mode buffer -- locating the matching tags and applying the appropriate face based on the function org-get-tag-face. The original version of the function org-get-tag-face looked for an exact match of the tag found (i.e., the key argument to the function assoc). The revised version of org-get-tag-face adds an additional key search for @.* and returns the proper face if the key is found -- this is necessary because the tag itself will usually look something like @home or @office, whereas our context 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)))
like image 56
lawlist Avatar answered Nov 09 '22 03:11

lawlist


You can use font-lock-add-keywords, for example, evaluating the following org source block should color the '@' tag blue.

#+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, revert the buffer or call font-lock-flush and font-lock-ensure to update font-locking.

like image 43
Rorschach Avatar answered Nov 09 '22 03:11

Rorschach