C/C++: improve extra fontification; add modern-cpp-font-lock plugin

This commit is contained in:
Henrik Lissner 2016-06-23 01:32:56 -04:00
parent 04f49981d7
commit 2649f6ffc1
3 changed files with 27 additions and 30 deletions

1
Cask
View File

@ -150,6 +150,7 @@
(depends-on "irony")
(depends-on "irony-eldoc")
(depends-on "opencl-mode")
(depends-on "modern-cpp-font-lock")
;; Crystal -- modules/module-crystal.el
(depends-on "crystal-mode" :git "https://github.com/jpellerin/emacs-crystal-mode")

View File

@ -72,35 +72,12 @@
(set new-face new-face))
;;;###autoload
(defun doom|init-c++-C11-highlights ()
;; C++11 syntax support (until cc-mode is updated)
(require 'font-lock)
;; labels, case, public, private, protected, namespace-tags
(doom--copy-face 'font-lock-label-face 'font-lock-keyword-face)
;; comment markups such as Javadoc-tags
(doom--copy-face 'font-lock-doc-markup-face 'font-lock-doc-face)
;; comment markups
(doom--copy-face 'font-lock-doc-string-face 'font-lock-comment-face)
(setq font-lock-maximum-decoration t)
(defun doom|extra-fontify-c++ ()
;; We could place some regexes into `c-mode-common-hook', but
;; note that their evaluation order matters.
;; NOTE modern-cpp-font-lock will eventually supercede some of these rules
(font-lock-add-keywords
nil '(;; complete some fundamental keywords
("\\<\\(void\\|unsigned\\|signed\\|char\\|short\\|bool\\|int\\|long\\|float\\|double\\)\\>" . font-lock-keyword-face)
;; namespace names and tags - these are rendered as constants by cc-mode
("\\<\\(\\w+::\\)" . font-lock-function-name-face)
;; new C++11 keywords
("\\<\\(alignof\\|alignas\\|constexpr\\|decltype\\|noexcept\\|nullptr\\|static_assert\\|thread_local\\|override\\|final\\)\\>" . font-lock-keyword-face)
("\\<\\(char16_t\\|char32_t\\)\\>" . font-lock-keyword-face)
;; PREPROCESSOR_CONSTANT, PREPROCESSORCONSTANT
("\\<[A-Z]*_[A-Z_]+\\>" . font-lock-constant-face)
("\\<[A-Z]\\{3,\\}\\>" . font-lock-constant-face)
;; hexadecimal numbers
("\\<0[xX][0-9A-Fa-f]+\\>" . font-lock-constant-face)
;; integer/float/scientific numbers
("\\<[\\-+]*[0-9]*\\.?[0-9]+\\([ulUL]+\\|[eE][\\-+]?[0-9]+\\)?\\>" . font-lock-constant-face)
;; c++11 string literals
nil '(;; c++11 string literals
;; L"wide string"
;; L"wide string with UNICODE codepoint: \u2018"
;; u8"UTF-8 string", u"UTF-16 string", U"UTF-32 string"
@ -112,10 +89,23 @@
("\\(\\<[uU8]*R\"[^\\s-\\\\()]\\{0,16\\}(\\)" 1 font-lock-keyword-face t) ; start delimiter
( "\\<[uU8]*R\"[^\\s-\\\\()]\\{0,16\\}(\\(.*?\\))[^\\s-\\\\()]\\{0,16\\}\"" 1 font-lock-string-face t) ; actual string
( "\\<[uU8]*R\"[^\\s-\\\\()]\\{0,16\\}(.*?\\()[^\\s-\\\\()]\\{0,16\\}\"\\)" 1 font-lock-keyword-face t) ; end delimiter
) t))
;; user-defined types (rather project-specific)
("\\<[A-Za-z_]+[A-Za-z_0-9]*_\\(type\\|ptr\\)\\>" . font-lock-type-face)
("\\<\\(xstring\\|xchar\\)\\>" . font-lock-type-face)
;;;###autoload
(defun doom|extra-fontify-c/c++ ()
(font-lock-add-keywords
nil '(;; PREPROCESSOR_CONSTANT, PREPROCESSORCONSTANT
("\\<[A-Z]*_[A-Z_]+\\>" . font-lock-constant-face)
("\\<[A-Z]\\{3,\\}\\>" . font-lock-constant-face)
;; integer/float/scientific numbers
("\\<\\([\\-+]*[0-9\\.]+\\)\\>" 1 font-lock-constant-face t)
("\\<\\([\\-+]*[0-9\\.]+\\)\\(f\\)\\>"
(1 font-lock-constant-face t)
(2 font-lock-keyword-face t))
("\\<\\([\\-+]*[0-9\\.]+\\)\\([eE]\\)\\([\\-+]?[0-9]+\\)\\>"
(1 font-lock-constant-face t)
(2 font-lock-keyword-face t)
(3 font-lock-constant-face t))
) t))
;;;###autoload

View File

@ -4,7 +4,9 @@
:commands (c-mode c++-mode objc-mode java-mode)
:mode ("\\.mm" . objc-mode)
:init
(add-hook! c++-mode '(highlight-numbers-mode doom|init-c++-C11-highlights))
(add-hook! (c-mode c++-mode) 'doom|extra-fontify-c/c++)
(add-hook 'c-mode-hook 'highlight-numbers-mode)
(add-hook 'c++-mode-hook 'doom|extra-fontify-c++)
(add-hook 'c-initialization-hook 'doom|init-c/c++-settings)
;; C++ header files
@ -40,6 +42,10 @@
;; Improve indentation of inline lambdas in C++11
(advice-add 'c-lineup-arglist :around 'doom/c-lineup-arglist))
(use-package modern-cpp-font-lock
:commands modern-c++-font-lock-mode
:init (add-hook 'c++-mode-hook 'modern-c++-font-lock-mode))
(use-package irony
:after cc-mode
:config