13
votes

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 ?

4
I don't think it currently exists. Maybe you should report that wish on the Org mailing list.fniessen
Not with that particular markdown, but with #BEGIN/END_SRC code blocks yes.d4gg4d

4 Answers

13
votes

UPDATE: the correct answer to this particular question is following https://stackoverflow.com/a/28059832/462601 . Answer presented here is related to this question Syntax highlighting within #+begin_src block in emacs orgmode not working

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

10
votes

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.
                       )))
3
votes
(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)
0
votes

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.