0

Plain text inherited from ctext.org contain the following syntax: {{...}}

堯年十六,以唐侯升為天子,遂以為號。{{《論語·泰伯》疏

案曰:堯典正義云:徧檢書傳無帝堯卽位之年,則此似非伏生大傳文,疑出書緯。}}

辯章百姓。{{見《毛詩·采菽》正義、《史記·五帝紀》索隱、《後漢書》注}}

辨章百姓,百姓昭明。{{《癸辛雜識》前集引《尚書大傳》第一曰云云}}

Commentary within the braces are displayed as such:

display on ctext

How can this be reproduced in org-mode? My guess is that I would need to work on faces and syntax tables.


Using @xuechunyang's answers below, I'd like to define another set of commentary markup on my own by changing some of the variables. Why doesn't this work?

(defface org-commentary-2
  '((t :foreground "blue2"))
  "Face for blue text in org mode")

(defun org-add-my-extra-markup ()
  (add-to-list 'org-font-lock-extra-keywords
               `(,(rx (group "((")
                      (group (+? anything))
                      (group "))"))
                 (1 '(face default invisible t))
                 (2 '(face org-commentary-2))
                 (3 '(face default invisible t)))))

(add-hook 'org-font-lock-set-keywords-hook #'org-add-my-extra-markup)
  • 1
    }} is missing in the first comment. – xuchunyang Jun 18 at 3:32
  • Yes - that's the way it is. I've not changed anything. If the behavior in line 1-2 is confusing (possibly a bug somewhere), I think we can just focus on lines 3-4 and get that to work for now. – Sati Jun 18 at 6:26
2

Here's my attempt. I learnt org-font-lock-set-keywords-hook on https://www.gonsie.com/blorg/org-highlight.html. The org-green face is defined in @amitp's answer.

(defun org-add-my-extra-markup ()
  (add-to-list 'org-font-lock-extra-keywords
               `(,(rx (group "{{")
                      (group (+? anything))
                      (group "}}"))
                 (1 '(face default invisible t))
                 (2 '(face org-green))
                 (3 '(face default invisible t)))))

(add-hook 'org-font-lock-set-keywords-hook #'org-add-my-extra-markup)
2
+50

There are two things you want.

  1. Apply a face (green text) to the area between {{ and }}.
  2. Hide the {{ and }}.

For the first, you can define a green face and then use font-lock to mark the text:

(defface org-green
  '((t :foreground "green4"))
  "Face for green text in org mode")

(defun my/org-mode-font-lock ()
  (font-lock-add-keywords
   nil
   '(("{{\\(.*\\)}}" 1 'org-green t))))

(add-hook 'org-mode-hook #'my/org-mode-font-lock)

For the second, I am not sure. Font-lock normally applies faces, but invisible is a text property and not a face. An alternative is to use the org-hide face:

(defun my/org-mode-font-lock ()
  (font-lock-add-keywords
   nil
   '(("\\({{\\)\\(.*\\)\\(}}\\)"
      (1 'org-hide t)
      (2 'org-green t)
      (3 'org-hide t)
      ))))
  • I think the green text also needs to be slightly smaller than the usual text. – Sati Jul 7 at 6:39
  • 1
    The size is controlled by :height so you can use :height 0.9 after the :foreground, or do any other customizations to the face. – amitp Jul 7 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.