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

问题描述 投票:10回答:4

是否有办法用语法高亮显示以 src_ruby{Array.new} ?

Org-mode是否有默认的选项,或者是否有其他方法可以做到这一点?

emacs syntax syntax-highlighting highlight org-mode
4个回答
13
投票

更新:这个问题的正确答案是这样的。https:/stackoverflow.coma28059832462601。 . 这里给出的答案与这个问题有关 在emacs orgmode中,#+begin_src块中的语法高亮显示不起作用。

你是说像在缓冲区中高亮显示源块那样的语法?

#+BEGIN_SRC ruby
Array.new
#+END_SRC

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

参考文献 http:/orgmode.orgworgorg-contribbabelexamplesfontify-src-code-blocks.html。


8
投票

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

0
投票

Stardiviner 给了一个很好的答案,引领了方向。然而,当两个内联源块在同一条线上时,就会出现问题。第二个内联源块会被第一个内联源块吞没。

在下面的细化解决方案中,将MATCHER中的regexp在 font-lock-keywords 被一个函数取代 org+-fontify-inline-src-code 不存在这个问题。

匹配器 org+-fontify-inline-src-code 部分采用的是 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)

用Emacs 26.3和org-mode 9.2.6测试。

© www.soinside.com 2019 - 2024. All rights reserved.