Get font face under cursor in Emacs

EmacsFontsEmacs Faces

Emacs Problem Overview


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?

Emacs Solutions


Solution 1 - Emacs

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]

Solution 2 - Emacs

M-x describe-face

Solution 3 - Emacs

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

Solution 4 - Emacs

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

Solution 5 - Emacs

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.

Solution 6 - Emacs

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionthedzView Question on Stackoverflow
Solution 1 - EmacsjlfView Answer on Stackoverflow
Solution 2 - EmacsYooView Answer on Stackoverflow
Solution 3 - EmacsTrey JacksonView Answer on Stackoverflow
Solution 4 - EmacsthedzView Answer on Stackoverflow
Solution 5 - EmacsKarl FogelView Answer on Stackoverflow
Solution 6 - Emacsig-perezView Answer on Stackoverflow