1

I am trying to override the default syntax highlighting in org-mode and org-agenda-mode buffers.

To clarify my intention: my current reason for doing this is to highlight headings (or parts of headings) based on their tags. The built-in variable org-tag-faces only allows customisation of the tag itself, not the heading containing the tag.

With reference to the following related questions:

  1. Emacs font lock mode: provide a custom color instead of a face

  2. https://emacs.stackexchange.com/questions/8211/color-code-a-new-generic-character-combination

  3. https://emacs.stackexchange.com/questions/8222/how-to-make-the-custom-font-lock-keywords-not-override-the-default-major-mode-fo#comment12615_8222

In thread 2 the accepted answer is to use font-lock for this purpose.

In thread 3 I am trying to achieve the exact opposite of the poster. The last comment by Jordon Biondo says:

take out the t from your keywords, what that t specifies is that font-lock should override already colored things.

Since I want to override already coloured things I am adding in the t but as far as I can tell the org-mode highlighting is still overriding my custom face. In org-mode buffers this manifests as the main body of the heading text being changed but any other items such as todo-states, dates, tags etc. retaining there existing faces. In org-agenda-mode buffers it completely fails to modify any aspect of the matched lines.

By way of a simple example here is some code I'm trying to use to set any lines containing :TT: to red in org-mode buffers:

(add-hook 'org-mode-hook
                (lambda ()
                  (font-lock-add-keywords
                   'org-mode
                   '(
                     ("^.*:TT:.*$" 0 '(:foreground "#FF0000") t)
                   ))))
Community
  • 1
  • 1
  • just curious, why would you want to change the default syntax highlighting of `org-mode` and `org-agenda-mode` ? – Chakravarthy Raghunandan Sep 13 '16 at 10:20
  • I want to be able to change the colour of items based on their tags. Using font-lock is the accepted answer in http://emacs.stackexchange.com/questions/8211/color-code-a-new-generic-character-combination. –  Sep 13 '16 at 10:47
  • `org-agenda-mode` does *not* use `font-lock`, so any efforts to incorporate that feature will not work well. Instead, the `org-agenda-mode` involves placement of text-properties with faces as the items are being gathered from the main buffer. – lawlist Sep 13 '16 at 15:54
  • Unfortunately, a modification of the `text-property` placement in `org-agenda-mode` buffers is too complex to place in an answer because there are multiple functions that can be used to gather data and place faces as the `*Org Agenda*` mode buffer is being created. If you are really motivated and want the easiest approach using `text-properties`, you can modify `org-format-item` which is a last ditch effort to change things before the buffer is populated. The best approach, is to locate and modify the functions that gather that data. – lawlist Sep 13 '16 at 16:04
  • @lawlist I think your comments should be the accepted answer - can you re-post as an answer so I can accept it? –  Sep 14 '16 at 20:12
  • The question has different answers for `org-mode` and for `org-agenda-mode`. I haven't played around a whole lot with changing `org-mode` default highlighting, but it's complex -- without investing time, I don't know whether it's better to modify `org-set-font-lock-defaults`, or some of the functions used therein, or the ramifications of simply trying to use `font-lock-add-keywords`. And, as mentioned, `org-agenda-mode` is complicated too. It is a "Pandora's Box" that I'm not prepared to open by posting an answer -- sorry . . . . I'll leave that to someone much braver than myself. :) – lawlist Sep 14 '16 at 20:38
  • @lawlist: _"I'll leave that to someone much braver than myself"_ - you and me both! –  Sep 16 '16 at 08:33

1 Answers1

0

This mostly works for me:

(add-hook 'org-mode-hook
      (lambda ()
        (font-lock-add-keywords
         'org-mode
         '(("^.*:TT:.*$" . font-lock-warning-face)))))

The headline is red, although the tag itself is not.

John Kitchin
  • 2,236
  • 1
  • 17
  • 25
  • Since `org-agenda-mode` does not use `font-lock`, this solution is not ideal for `*Org Agenda*` buffers. If you `(require 'org-agenda)` and then look at the code for `org-agenda-mode` by typing `M-x find-function RET org-agenda-mode RET` , you will see a notation as follows: `;; Keep global-font-lock-mode from turning on font-lock-mode` and the code below is: `(org-set-local 'font-lock-global-modes (list 'not major-mode))` – lawlist Sep 13 '16 at 16:05
  • This code gives the same result as my code in the original post: _"In org-mode buffers this manifests as the main body of the heading text being changed but any other items such as todo-states, dates, tags etc. retaining there existing faces."_ –  Sep 14 '16 at 12:10