I'm new to Emacs and Org-mode, this is a very basic question. Unfortunately the keywords are too common for me to find the answer.
I would like to create a custom pattern to colour my writing input.
Something like
_theorem: (it would detect _ and a non-whitespace character for the start, and would trace it until the end, or colon)
_theorem 2: (it would be great if this could also include whitespaces)
_theorem on topic 1: (such as this).
The script would thus automatically color all of the above writing that is now in bold. References that would help me do this would also be helpful. Many thanks for helping a beginner!
Update
I've added a follow-up question in the comments on adding multiple such rules that ran into problems. Here's the full code for it. While the first answer to the first question is working perfectly now, the other two new rules seem to be mistakenly formulated or in conflict somehow. On the instances where either of them is found, it breaks the regular coloring of org-mode and does no coloring itself.
(add-hook 'org-mode-hook
(lambda ()
(font-lock-add-keywords nil
'((
"\\(^\\|\\s-+\\)\\(_\\([a-zA-Z0-9]+\\s-?\\)\\{0,4\\}:\\)" 2
font-lock-type-face t)))
(font-lock-add-keywords nil
'((
"\\(\\w+\\(\\s-&\\s-\\w+\\)?,\\s-[0-9]\\{4\\})" 2
font-lock-type-face t)))
(font-lock-add-keywords nil
'((
"^\\w+:" 2
font-lock-type-face t)))
))
regexp
andvisual
in this stackexchange for more examples. – Kaushal Modi Feb 13 '15 at 15:44\(^\|\s-+\)\(_\w+\(?:\s-+\w+\)\{0,3\}\s-*:\)
. Make sure to replace the single back-slashes with double back-slashes when doing so. [Regexp Backslash Reference] – Kaushal Modi Feb 13 '15 at 16:02"\\(^\\|\\s-+\\)\\(_\\w+\\(?:\\s-+\\w+\\)\\{0,3\\}\\s-*:\\)"
Not sure if this may have been due to inadequacies of the interface or me. But this feature is great! When I add more hooks of the same type "font-lock-add-keywords", should I create a new "add-hook" or should I add them under the same hook somehow. Any idea what happens if one of the partially includes the other. In case of conflict, perhaps either the first or last one is used? Many thanks again. – puslet88 Feb 13 '15 at 16:41font-lock-add-keywords
use case, you need to escape those backslashes. So each backslash doubles. I don't use the RE-builder; I test out the regexps visually using thevisual-regexp
oranzu
packages. – Kaushal Modi Feb 13 '15 at 16:44re-builder
with the string version of the regexp with double backslashes and that works. I have updated my answer to show the test text I used for this regexp. For adding second set of rules, you add that other(font-lock-add-keywords ..)
statement withing the(lambda () ..)
. – Kaushal Modi Feb 13 '15 at 16:52font-lock-keywords
alist would have got polluted. – Kaushal Modi Feb 13 '15 at 19:30