14

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 ?

stardiviner
  • 1,090
  • 1
  • 22
  • 33

4 Answers4

13

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

Community
  • 1
  • 1
d4gg4d
  • 305
  • 1
  • 6
  • 8
    Can't believe that most people think inline code is babel code block. Totally different thing. And high vote on wrong answer. Weird. – stardiviner Oct 02 '15 at 01:59
  • 1
    I think people are looking anwsers to question http://stackoverflow.com/questions/10642888/syntax-highlighting-within-begin-src-block-in-emacs-orgmode-not-working and pumb into this question as well. That would explain this anomaly. – d4gg4d Jan 02 '16 at 11:46
11

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.
                       )))
stardiviner
  • 1,090
  • 1
  • 22
  • 33
  • 1
    I took your solution as a basis to a more refined one (see below). I noted some problems of your solution with multiple source blocks on one line. – Tobias Jun 11 '20 at 08:51
3
(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)
bowen.li
  • 449
  • 4
  • 6
1

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.

Tobias
  • 5,038
  • 1
  • 18
  • 39