在Emacs中获取鼠标下的字体名称

109

我一直在开发自己的定制化颜色主题,如果能够获得一个列表,列出影响光标下文本的字体集将非常有用。

类似于Textmate的“显示当前范围”命令。

这将节省我通过执行M-x customize-face和查看可用选项并猜测哪个选项会影响我正在编辑的单词的时间。

有什么想法吗?


如果您正在寻找使用鼠标指针执行相同功能的方法(例如,无法在相关文本上获取“point”),请参见:https://emacs.stackexchange.com/a/35449/13444 - Braham Snyder
7个回答

193

what-cursor-position 带有前缀参数时,会显示光标下的 face 属性等相关信息。

键盘快捷键为 C-u C-x =

示例输出(face 属性显示在最后一段):

             position: 5356 of 25376 (21%), column: 4
            character: r (displayed as r) (codepoint 114, #o162, #x72)
    preferred charset: ascii (ASCII (ISO646 IRV))
code point in charset: 0x72
               syntax: w    which means: word
             category: .:Base, L:Left-to-right (strong), a:ASCII, l:Latin, r:Roman
          buffer code: #x72
            file code: #x72 (encoded by coding system undecided-unix)
              display: by this font (glyph code)
    nil:-apple-Monaco-medium-normal-normal-*-12-*-*-*-m-0-iso10646-1 (#x55)

Character code properties: customize what to show
  name: LATIN SMALL LETTER R
  general-category: Ll (Letter, Lowercase)
  decomposition: (114) ('r')

There are text properties here:
  face                 org-level-2
  fontified            t

[back]

13
触发 what-cursor-position - viam0Zah
有时它会调用what-cursor-position命令,有时它会显示缓冲区属性列表(包括字体)。如果我得到前一种行为,移动光标并重复操作会带来后者。 - davidA
2
我很高兴找到了它,通过一些未知的命令和按键组合,我让“emacs”以我喜欢的方式显示,并且不知道如何在下次重启时还原。 - Miserable Variable
2
它在Emacs GUI上显示字体名称。在终端上,Emacs不负责设置字体,因此当在终端上运行的Emacs(如emacs -nw file.txt)中执行C-u C-x =时,此类信息是不可用的。 - Fernando Basso
奶酪式的助记符“用户体验等于”……呻吟 - Jeremy Field

74

M-x describe-face


5
这也包括了美好的链接,使得在光标下立即自定义面部成为可能。 - Ev Dolzhenko
4
大多数情况下这个功能运作良好,但有时候由于我无法理解的原因,它无法提供我想要的面孔。例如,在eshell中有ansi颜色时,它只会显示“默认”。 - Samuel Edwin Ward
2
这会显示一个提示,我可以在其中输入内容。为了描述光标下的字体,我需要输入什么? - Zelphir Kaltstahl
1
这对我来说很有效,可以在org-mode中自定义代码块字体。@Zelphir,在提示之前的文本显示了face,在我的情况下至少是这样的。你可能只需要按return键。例如,我的结果读取“描述face(默认为'org-block-background'):”。 - Mallory-Erik

44

您可以使用以下代码定义what-face

(defun what-face (pos)
  (interactive "d")
  (let ((face (or (get-char-property (pos) 'read-face-name)
                  (get-char-property (pos) 'face))))
    (if face (message "Face: %s" face) (message "No face at %d" pos))))

之后,

M-x what-face

将打印找到的当前点的面孔。

(感谢thedz指出what-face未内置。)


3
如果启用了hl-line-mode,您将只看到“hl-line”作为面的hl-line ,而不是其他面。这忽略了被设置为文本属性的面。请参考https://gist.github.com/Wilfred/f7d61b7cdf9fdbb1d11c。 - Wilfred Hughes
2
Karl Fogel另一个答案中指出了这段代码中的一个错误:输出消息说它正在描述pos参数处的面部,但实际上读取面部的是(point)而不是pos - Rory O'Kane
1
这个不起作用,你可以使用“M-x describe-face”代替。 - luochen1990
4
“pos不是一个函数”,为了让这段代码片段正常工作,你需要在第三行和第四行将(pos)替换为pos - cebola

8

Trey的想法是正确的。他引导我看到了邮件列表中的一封电子邮件,内容如下:

(defun what-face (pos)
    (interactive "d")
        (let ((face (or (get-char-property (point) 'read-face-name)
            (get-char-property (point) 'face))))
    (if face (message "Face: %s" face) (message "No face at %d" pos))))

哎呀,忘了它不是与Emacs捆绑在一起的。我可以在我的答案中附上来源吗? :) - Trey Jackson

2

`what-face' 函数存在一个 bug:它将 "pos" 作为参数传入,但在获取 face 时并没有使用它,而是使用了 "(point)",尽管后面的消息在 "No face at %d" 情况下声称使用了 pos。


2
这个建议最好作为对那个答案的评论。 - Adam Spiers
1
如果它有一个修复,那就更好了...感谢你发现了它。 - rath

0

我尝试了@tray函数,但它没有起作用,@thedz的定义确实有效:

(defun what-face (pos)
  (interactive "d")
  (let ((face (or (get-char-property (point) 'read-face-name)
                  (get-char-property (point) 'face))))
    (if face (message "Face: %s" face) (message "No face at %d" pos))))

经过一些研究,我发现了原因:
  • (point) 是一个函数,它将 point 的值作为整数返回。
  • pos 获取由 (interactive "d") 返回的值,这个值将是 point 的位置,作为一个整数。
  • get-char-property 需要一个位置,在这种情况下由函数 (point) 给出。

0
在 Emacs Lisp 中,
(face-at-point t)

这就是M-x describe-face使用的内容。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接