如何对Org-mode内联源代码src_lang {}进行语法高亮显示?

时间:2013-12-01 07:33:15

标签: emacs syntax syntax-highlighting highlight org-mode

是否有一种方法可以突出显示标有src_ruby{Array.new}的Org-mode内联源代码的语法?

Org-mode是否有默认选项? 或者还有其他方法吗?

4 个答案:

答案 0 :(得分:14)

更新:这个特定问题的正确答案是https://stackoverflow.com/a/28059832/462601。此处提供的答案与此问题Syntax highlighting within #+begin_src block in emacs orgmode not working

有关

你的意思是像缓冲区中的语法高亮源块一样吗?

#+BEGIN_SRC ruby
Array.new
#+END_SRC

您需要设置(setq org-src-fontify-natively t)

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

答案 1 :(得分:7)

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.
                       )))

答案 2 :(得分:2)

(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))

在org.el

中添加函数org-set-font-lock-defaults
;; Drawers
'(org-fontify-drawers)
;; Inline source block
'(org-fontify-inline-src-block)

答案 3 :(得分:0)

Stardiviner提供了一个很好的答案,引领了潮流。然而,当两个串联源块在同一条线上时,仍然存在问题。第二个嵌入式源代码块被吞并到第一个嵌入式源代码块中。

在以下改进的解决方案中,font-lock-keywords中的正则表达式MATCHER被替换为没有问题的函数org+-fontify-inline-src-code

匹配器org+-fontify-inline-src-codeorg-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)

已通过Emacs 26.3和组织模式9.2.6测试。