Skip to content

Instantly share code, notes, and snippets.

@tungd
Last active May 17, 2022 04:15
Show Gist options
  • Save tungd/6242039 to your computer and use it in GitHub Desktop.
Save tungd/6242039 to your computer and use it in GitHub Desktop.
WIP Highlight `printf` format string specifier token. Used some code from: http://www.emacswiki.org/emacs/AddKeywords#toc7
(defvar font-lock-format-specifier-face
'font-lock-format-specifier-face)
(defface font-lock-format-specifier-face
'((t (:inherit font-lock-preprocessor-face)))
"Font Lock mode face used to highlight format specifiers."
:group 'font-lock-faces)
(defun highlight-format-specifier ()
(font-lock-add-keywords
nil '(("\".*\\(%\\(?:[-+0-9\\$.]+\\)?[bdiuoxXDOUfeEgGcCsSpn]\\)[^\"]*\""
1 font-lock-preprocessor-face t))))
(add-hook 'prog-mode-hook 'highlight-format-specifier)
@tungd
Copy link
Author

tungd commented Aug 15, 2013

Differencies from the Emacs Wiki version:

  • Simpler regex, doesn't do validation of any sort. This is more flexible since the specifier might differ between languages.
  • Only highlight token inside string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment