stardiviner
stardiviner

Reputation: 1130

How to syntax highlight for Org-mode inline source code src_lang{}?

Is there a way to syntax highlight Org-mode inline source code which is marked with src_ruby{Array.new} ?

Does Org-mode has default option for this ? Or Is there other method to do this ?

Upvotes: 14

Views: 11049

Answers (4)

d4gg4d
d4gg4d

Reputation: 305

UPDATE:

You mean like syntax-highlighting source blocks in buffer?

#+BEGIN_SRC ruby
Array.new
#+END_SRC

You need to set (setq org-src-fontify-natively t)

Ref: http://orgmode.org/worg/org-contrib/babel/examples/fontify-src-code-blocks.html

Upvotes: 13

Tobias
Tobias

Reputation: 5198

Stardiviner gave a nice answer that leads the way. Nevertheless, there are problems when two inline source blocks are on the same line. The second inline source block is engulfed into the first one.

In the following refined solution, the regexp MATCHER in font-lock-keywords is replaced by a function org+-fontify-inline-src-code that does not have this problem.

The matcher org+-fontify-inline-src-code is partially adopted from org-element-inline-src-block-parser.

(defun org+-fontify-inline-src-code (limit)
  "Match inline source blocks from point to LIMIT."
  (when (re-search-forward "\\_<src_\\([^ \t\n[{]+\\)[{[]" limit t) ;; stolen from `org-element-inline-src-block-parser'
    ;; This especially clarifies that the square brackets are optional.
    (let ((beg (match-beginning 0))
      pt
      (lang-beg (match-beginning 1))
      (lang-end (match-end 1))
      header-args-beg header-args-end
      code-beg code-end)
      (setq pt (goto-char lang-end))
      (when (org-element--parse-paired-brackets ?\[)
    (setq header-args-beg pt
          header-args-end (point)
          pt (point)))
      (when (org-element--parse-paired-brackets ?\{)
    (setq code-beg pt
          code-end (point))
    (set-match-data
     (list beg (point)
           beg lang-beg
           lang-beg
           lang-end
           header-args-beg
           header-args-end
           code-beg
           code-end
           (current-buffer)))
    code-end))))

(defun org+-config-fontify-inline-src-code ()
  "Fontify inline source code, such as src_html[:exports code]{<em>to be emphased</em>}."
  (font-lock-add-keywords nil
              '((org+-fontify-inline-src-code
                 (1 '(:foreground "black" :weight normal :height 10) t) ; src_ part
                 (2 '(:foreground "cyan" :weight bold :height 75 :underline "red") t) ; "lang" part.
                 (3 '(:foreground "#555555" :height 70) t) ; [:header arguments] part.
                 (4 'org-code t) ; "code..." part.
                 ))
              'append))

(add-hook 'org-mode-hook #'org+-config-fontify-inline-src-code)

Tested with Emacs 26.3 and org-mode 9.2.6.

Upvotes: 2

stardiviner
stardiviner

Reputation: 1130

enter image description here

(font-lock-add-keywords 'org-mode
                    '(("\\(src_\\)\\([^[{]+\\)\\(\\[:.*\\]\\){\\([^}]*\\)}"
                       (1 '(:foreground "black" :weight 'normal :height 10)) ; src_ part
                       (2 '(:foreground "cyan" :weight 'bold :height 75 :underline "red")) ; "lang" part.
                       (3 '(:foreground "#555555" :height 70)) ; [:header arguments] part.
                       (4 'org-code) ; "code..." part.
                       )))

Upvotes: 11

bowen.li
bowen.li

Reputation: 449

(defun org-fontify-inline-src-block (limit)
  "Fontify inline source block."
  (when (re-search-forward org-babel-inline-src-block-regexp limit t)
    (add-text-properties
     (match-beginning 1) (match-end 0)
     '(font-lock-fontified t face (t (:foreground "#008ED1" :background "#FFFFEA"))))
    (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
    t))

add to function org-set-font-lock-defaults in org.el

;; Drawers
'(org-fontify-drawers)
;; Inline source block
'(org-fontify-inline-src-block)

Upvotes: 3

Related Questions