【问题标题】:How to syntax highlight for Org-mode inline source code src_lang{}?如何为 Org-mode 内联源代码 src_lang{} 语法高亮?
【发布时间】:2013-12-17 01:43:22
【问题描述】:

有没有办法语法高亮标有src_ruby{Array.new} 的 Org-mode 内联源代码?

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

【问题讨论】:

  • 我认为它目前不存在。也许你应该在 Org 邮件列表上报告这个愿望。
  • 不是那个特定的降价,而是 #BEGIN/END_SRC 代码块是的。

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


【解决方案1】:

更新:这个特定问题的正确答案在 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

【讨论】:

  • 不敢相信大多数人认为内联代码是 babel 代码块。完全不同的东西。并对错误答案投高票。很奇怪。
  • 我认为人们正在寻找答案来质疑stackoverflow.com/questions/10642888/… 并深入研究这个问题。这可以解释这种异常情况。
【解决方案2】:
(defun org-fontify-inline-src-block (限制) “Fontify 内联源代码块。” (当 (re-search-forward org-babel-inline-src-block-regexp 限制 t) (添加文本属性 (比赛开始 1) (比赛结束 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

;;抽屉 '(org-fontify-drawers) ;;内联源代码块 '(org-fontify-inline-src-block)

【讨论】:

    【解决方案3】:

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

    【讨论】:

    • 我将您的解决方案作为更完善的解决方案的基础(见下文)。我注意到您的解决方案在一行上有多个源块的一些问题。
    【解决方案4】:

    Stardiviner 给出了一个很好的答案,引领潮流。然而,当两个内联源代码块在同一行时会出现问题。第二个内联源代码块被第一个代码块吞没了。

    在以下改进的解决方案中,font-lock-keywords 中的正则表达式 MATCHER 被替换为不存在此问题的函数 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 测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-18
      • 2018-05-10
      • 2014-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多