Get font face under cursor in Emacs
Asked Answered
F

7

111

I've been developing my own custom color theme, and it'd be really useful if I could get a list of font-faces affecting the text under the cursor.

Something like Textmate's show current scope command.

That would save me the trouble of doing M-x customize-face and looking through available options, guessing at which one affects the current word I'm on.

Any ideas?

Feingold answered 7/8, 2009 at 0:56 Comment(1)
In case you're looking for the same functionality using the mouse cursor (if, e.g., you cannot get point on the text in question), see: emacs.stackexchange.com/a/35449/13444Alvis
N
45

You can define what-face with this code:

(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))))

After that,

M-x what-face

will print the face found at the current point.

(Thanks to thedz for pointing out that what-face wasn’t built in.)

Nunciature answered 7/8, 2009 at 1:2 Comment(4)
This ignores faces set as text properties. If enable hl-line-mode you will only see hl-line as the face, not the other faces. Consider gist.github.com/Wilfred/f7d61b7cdf9fdbb1d11cLubricity
Karl Fogel pointed out a bug in this code in a separate answer: the output message says it’s describing the face at the pos parameter, but the reading of the face is actually done at (point) rather than at pos.Couchant
This doesn't works, you can use "M-x describe-face" instead.Crawford
pos is not a function; in order make the snippet work, you should replace (pos) with pos on lines 3 and 4Haerr
P
195

what-cursor-position with a prefix argument shows the face under point, among other information.

Keyboard shortcut is C-u C-x =

Example output (the face property is shown in the last paragraph):

             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]
Pithecanthropus answered 7/8, 2009 at 3:36 Comment(5)
Which invokes what-cursor-position.Miscount
hmmm, sometimes it invokes what-cursor-position, sometimes it displays a list of buffer properties (including font). If I get the former behaviour, moving the cursor and repeating brings on the latter.Bellyful
I am so happy I found this, with some unknown combinations of the commands and keystrokes I got emacs to display how I liked it and didn't how to get it back in my next restartRegazzi
It shows the font name on Emacs GUI. On terminal, Emacs is not responsible for setting the font and therefore such information is not available when one does C-u C-x = in Emacs running on the terminal, like emacs -nw file.txt.Bengurion
Cheesy mnemonic "user experience equals"... groanTribulation
A
74

M-x describe-face

Atlantes answered 8/8, 2009 at 18:53 Comment(4)
This also includes the nice link making it possible to customize the face under cursor immediatelyRondeau
This works well most of the time, but sometimes for reasons I can't figure out sometimes it doesn't suggest the face I'm looking for. For example in eshell when there is ansi color it just says "default".Biggerstaff
This shows me a prompt where I can enter something. What would I need to enter, in order to describe the font under cursor?Idonna
This worked for me to customize code block fonts in org-mode. @Zelphir, the text before the prompt showed the face, in my case at least. You might just hit return. For example my result read Describe face (default ‘org-block-background’): .Larrup
N
45

You can define what-face with this code:

(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))))

After that,

M-x what-face

will print the face found at the current point.

(Thanks to thedz for pointing out that what-face wasn’t built in.)

Nunciature answered 7/8, 2009 at 1:2 Comment(4)
This ignores faces set as text properties. If enable hl-line-mode you will only see hl-line as the face, not the other faces. Consider gist.github.com/Wilfred/f7d61b7cdf9fdbb1d11cLubricity
Karl Fogel pointed out a bug in this code in a separate answer: the output message says it’s describing the face at the pos parameter, but the reading of the face is actually done at (point) rather than at pos.Couchant
This doesn't works, you can use "M-x describe-face" instead.Crawford
pos is not a function; in order make the snippet work, you should replace (pos) with pos on lines 3 and 4Haerr
F
8

Trey's what face is on the right track. It led me to an email on a mailing list that had this:

(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))))
Feingold answered 7/8, 2009 at 1:20 Comment(1)
Duh, forgot it wasn't bundled with Emacs. May I put the source in my answer w/attribution? :)Nunciature
V
2

There's a bug in the `what-face' code: the function takes "pos" as an argument but then doesn't use it when getting the face -- instead it uses "(point)", even though the message later claims pos in the "No face at %d" case.

Viridi answered 30/12, 2013 at 17:25 Comment(2)
This would be better placed as a comment on that answer.Spirula
Even better if it had a fix... +1 for spotting it anywayPancratium
K
0

I tried @tray function but it didn't work, @thedz definition does work:

(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))))

After some research I found why:

  • (point) is a function that returns the value of point as an integer.
  • pos gets the value returned by (interactive "d") which will be the position of point, as an integer.
  • get-char-property expects a position, in this case given by the function (point).
Keitt answered 20/2, 2021 at 2:38 Comment(0)
E
0

In emacs-lisp,

(face-at-point t)

which is what M-x describe-face uses.

Energumen answered 28/3, 2023 at 15:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.