【问题标题】:Enabling font weight and colour in RESULTS block of org-babel在 org-babel 的 RESULTS 块中启用字体粗细和颜色
【发布时间】:2017-08-22 08:01:15
【问题描述】:

有没有办法在org-babel#+RESULTS块中启用字体粗细和颜色?

例如,使用 --color 选项调用 ls 会在 shell 中按预期呈现字体粗细和颜色,但在 #+RESULTS 块中呈现时则不会:

#+BEGIN_SRC shell
ls --color ~/
#+END_SRC

#+RESULTS:
| Applications      |
| Desktop           |
| Documents         |
| Downloads         |
| Library           |
...

Screenshot of ls with and without --color in the terminal

另一个例子是node模块chalk,它以明文形式出现在#+RESULTS块中:

#+BEGIN_SRC js
const chalk = require('chalk');

console.log(chalk.red('Hello world!'));
#+END_SRC

#+RESULTS:
: Hello world!
: undefined

Screenshot of chalk in the terminal

【问题讨论】:

    标签: emacs org-mode org-babel


    【解决方案1】:

    这是我想出的。通过在光标位于代码块(而不是结果块)上时运行它来将其用作交互式功能。您需要安装 xterm-color 软件包。

    (defun org-babel-color-results()
      "Color org babel results.
    Use the :wrap header argument on your code block for best results.
    Without it, org may color overtop with whatever default coloring it
    uses for literal examples.
    Depends on the xterm-color package."
      (interactive)
      (when-let ((result-start (org-babel-where-is-src-block-result nil nil)))
        (save-excursion
          (goto-char result-start)
          (when (looking-at org-babel-result-regexp)
            (let ((element (org-element-at-point)))
              (pcase (org-element-type element)
                (`fixed-width
                 (let ((post-affiliated (org-element-property :post-affiliated element))
                       (end (org-element-property :end element))
                       (contents (org-element-property :value element))
                       (post-blank (org-element-property :post-blank element)))
                   (goto-char post-affiliated)
                   (delete-region post-affiliated end)
                   (insert (xterm-color-filter contents))
                   (org-babel-examplify-region post-affiliated (point))
                   (insert (make-string post-blank ?\n ))
                   ))
                ((or `center-block `quote-block `verse-block `special-block)
                 (let ((contents-begin (org-element-property :contents-begin element))
                       (contents-end (org-element-property :contents-end element)))
                   (goto-char contents-begin)
                   (let ((contents (buffer-substring contents-begin contents-end)))
                     (delete-region contents-begin contents-end)
                     (insert (xterm-color-filter contents)))))))))))
    

    或者添加一个钩子

    (add-hook 'org-babel-after-execute-hook 'org-babel-color-results)
    

    This link 是我的灵感来源。

    我没有想到的一件事是,虽然当您不在代码块上使用 :wrap 参数时它会为文本着色,但 Org Literal Examples : 的着色似乎会覆盖它。您可以通过简单地删除 : 来查看颜色已应用,以显示预期有颜色的行。

    【讨论】:

      猜你喜欢
      • 2021-10-31
      • 1970-01-01
      • 2015-09-07
      • 2021-01-12
      • 1970-01-01
      • 2017-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多