emacs - 在 org-babel 的结果 block 中启用字体粗细和颜色

标签 emacs org-mode org-babel

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

例如,使用 --color 选项调用 ls 会在 shell 中呈现预期的字体粗细和颜色,但在 #+结果 block :

#+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 block 中:

#+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

最佳答案

这是我想出的。当光标位于代码块(而不是结果 block )上时运行它,将其用作交互式函数。您需要安装 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)))))))))))

或者添加一个钩子(Hook)

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

This link是我的灵感。

我还没有弄清楚的一件事是,虽然当您不在代码块上使用 :wrap 参数时它会为文本着色,但 Org Literal Examples 的着色: 似乎覆盖了它。但是,您可以看到应用了颜色,只需删除 : 预期有颜色的行即可。

关于emacs - 在 org-babel 的结果 block 中启用字体粗细和颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45812085/

相关文章:

org-mode - 在组织模式下,我应该使用单个顶级标题还是多个?

emacs - 将错误重定向到 org src 中的结果

emacs - emacs 中的键盘快捷键补全帮助?

emacs - 确定模式行中的窗口焦点?

emacs - 如何将缓冲区重命名为类似的名称

emacs - 如何检查 Emacs Lisp 中是否存在服务器

emacs - 如何使用 scp 或 rsync 使用 Tramp 传输文件

emacs - 使用 org-babel : Debugger entered--Lisp error: (void-function cl-member) 初始化 emacs

emacs - 是否可以从其他代码块调用 Org Babel 代码块?

assembly - 在 Emacs Lisp 中使用 asm-mode 识别 C++ 风格的注释