gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 06:38:33 25 4
gpt4 key购买 nike

有没有办法在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com