有办法调整开启 truncate-lines 后,折行前的背景颜色吗?

如上图,预期应该是一整块都使用 org-block 的背景色。

从缩进的情况看,应该是启用了 adaptive-wrap,但我试了一下没有背景色不同的问题。

系统环境,Emacs 版本,用了什么配置,什么包。这些都应该交代一下,否则只能猜测了。

我用的自己攒的配置。

版本:GNU Emacs 27.2 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.27, cairo version 1.17.4) of 2021-03-27

没有安装 adaptive-wrap

主题: doom-nord describe-mode

Enabled minor modes: Anzu Auto-Composition Auto-Compression
Auto-Encryption Auto-Sudoedit Blink-Cursor Company-Prescient
Counsel-Projectile Delete-Selection Doom-Modeline Eldoc
Electric-Indent Electric-Pair File-Name-Shadow Font-Lock Gcmh
Global-Anzu Global-Auto-Revert Global-Eldoc Global-Font-Lock
Global-So-Long Global-Undo-Tree Ivy Ivy-Prescient Ivy-Rich Line-Number
Modal Modal-Global Modal-Normal-State Mouse-Wheel Org-Appear
Org-Fragtog Org-Indent Org-Superstar Override-Global Popwin
Prescient-Persist Projectile Recentf Save-Place Savehist
Shell-Dirtrack Show-Paren Sis-Auto-Refresh Sis-Global-Respect Solaire
Solaire-Global Straight-Package-Neutering Straight-Use-Package
Terminal-Cursor Tooltip Transient-Mark Undo-Tree Valign
Visual-Fill-Column Which-Key Winner

其中 Modal Modal-Global Modal-Normal-State Terminal-Cursor 是我自己写的 minor mode ,应该没有涉及到背景颜色的问题。


请问您的主题的 org-block 的背景颜色与 default 的背景颜色是不同的吗?我观察到的现象是折行前的背景颜色使用的是 default 的背景颜色。不管是用 truncate-lines 还是 visual-line-mode 都存在这个问题。

(face-attribute 'org-block :extend) 结果是不是 t?

:extend 是 t。

刚刚测试了下 doom-emacs,好像也存在这个问题。

:sob:

image

把光标移到第一个折行的开始,执行 (print (text-properties-at (1- (point)))),看看结果是真么,我这里:

(face (font-lock-string-face org-block) font-lock-multiline t wrap-prefix
      #("    " 0 4 (face (org-block) font-lock-multiline t src-block t
                         help-echo nil font-lock-fontified t fontified t))
      src-block t help-echo nil font-lock-fontified t fontified t)
1 个赞

输出:

(src-block t help-echo nil face (font-lock-string-face org-block) font-lock-multiline t font-lock-fontified t wrap-prefix #("        " 0 2 (face org-indent) 2 8 (face org-indent)) line-prefix #("  " 0 2 (face org-indent)) fontified t)
(src-block t help-echo nil face (font-lock-string-face org-block) font-lock-multiline t font-lock-fontified t wrap-prefix #("        " 0 2 (face org-indent) 2 8 (face org-indent)) ...)

看起来像是 wrap-prefix 指定了 org-indent 这个 face 导致的,我把 org-indent-mode 关了就好了。

但是 org-indent 这个face并没有设置背景色,对 text-properties 不太了解,不知道怎么改了。。。

(with-eval-after-load 'org-indent
  (set-face-background 'org-indent (face-background 'org-block)))

org-indent 这个face 不仅仅只有 org-block 中有使用,这样改的话,其他地方的背景色也会乱吧。

应该让 org-block 内的缩进自动的应用多个 face 才能正确显示。

像下面这样:

;; 原来的
wrap-prefix #("        " 0 2 (face org-indent) 2 8 (face org-indent))
;; 修改后的(没有测试,只是我的猜测)
wrap-prefix #("        " 0 2 (face org-indent) 2 8 (face org-indent org-block))

0-2 个字符是整个 org-block 的缩进,2-8 是 org-block 内的缩进。

不知道这算不算bug,不管怎么说,现在有一个方向了。有空再研究研究。

那就改 org-indent-set-line-properties 函数,在 block 中就用 org-block 做为 face。

我认为 org-block 还是保持跟 default 一样的背景色比较好。不同的背景会使得整个画面看起来很破碎。可能少量的大块 block 还好,如果是大量的小 block 就很明显。

我之前是想把代码块调整成这样,视觉上文字和背景之间会有边距,所以感觉还好。

我先看看 org-indent-set-line-properties 。感谢您的帮助。 :tulip:

(defun gatsby:org-indent-mode-with-correct-wrap (level indentation &optional heading)
  (let* ((line (aref (pcase heading
                       (`nil org-indent--text-line-prefixes)
                       (`inlinetask org-indent--inlinetask-line-prefixes)
                       (_ org-indent--heading-line-prefixes))
                     level))
         (wrap
          (concat line
                  (org-add-props
                      (if heading (concat (make-string level ?*) " ")
                        (make-string indentation ?\s))
                      nil 'face (if (eq (car (org-element-at-point)) 'src-block)
                                    'org-block
                                  'org-indent)))))
    ;; Add properties down to the next line to indent empty lines.
    (add-text-properties (line-beginning-position) (line-beginning-position 2)
                         `(line-prefix ,line wrap-prefix ,wrap)))
  (forward-line))

(advice-add #'org-indent-set-line-properties :override #'gatsby:org-indent-mode-with-correct-wrap)