Commit b293aa91 authored by Alan Mackenzie's avatar Alan Mackenzie
Browse files

CC Mode. Allow fontification of "wrong" style comments with warning face.

This fixes bug #4192.

* etc/NEWS: Add a new entry.

* lisp/progmodes/cc-defs.el (c-font-lock-flush): New macro.

* lisp/progmodes/cc-cmds.el (c-toggle-comment-style): On toggling the comment
style, invoke c-font-lock-flush when c-mark-wrong-style-of-comment is non-nil,
to cause that marking to be done instead on the other style of comment.

* lisp/progmodes/cc-fonts.el (c-maybe-font-lock-wrong-style-comments): New
(c-cpp-matchers): Call c-maybe-font-lock-wrong-style-comments when

* lisp/progmodes/cc-vars.el (c-mark-wrong-style-of-comment): New customizable

* doc/misc/cc-mode.texi (top level, Indentation Commands, Guessing the Style,
Custom Macros): For some opening quote marks, correct '' to ``.
(Minor Modes): Add an xref to the new page "Wrong Comment Style" in a
(Wrong Comment Style): New page.
parent 6daa80d0
Pipeline #3981 failed with stage
in 25 minutes and 26 seconds
......@@ -669,7 +669,7 @@ expression, to some statements, or perhaps to whole functions, the
syntactic recognition can be wrong. @ccmode{} manages to figure it
out correctly most of the time, though.
Some macros, when invoked, ''have their own semicolon''. To get the
Some macros, when invoked, ``have their own semicolon''. To get the
next line indented correctly, rather than as a continuation line,
@xref{Macros with ;}.
......@@ -1149,7 +1149,9 @@ find useful while writing new code or editing old code:
@table @asis
@item comment style
This specifies whether comment commands (such as @kbd{M-;}) insert
block comments or line comments.
block comments or line comments@footnote{You can emphasize
non-default style comments in your code by giving their delimiters
@code{font-lock-warning-face}. @xref{Wrong Comment Style}.}.
@item electric mode
When this is enabled, certain visible characters cause reformatting as
they are typed. This is normally helpful, but can be a nuisance when
......@@ -1866,6 +1868,7 @@ sections apply to the other languages.
* Font Locking Preliminaries::
* Faces::
* Doc Comments::
* Wrong Comment Style::
* Misc Font Locking::
* AWK Mode Font Locking::
@end menu
......@@ -2069,7 +2072,7 @@ since those aren't syntactic errors in themselves.
@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@node Doc Comments, Misc Font Locking, Faces, Font Locking
@node Doc Comments, Wrong Comment Style, Faces, Font Locking
@comment node-name, next, previous, up
@section Documentation Comments
@cindex documentation comments
......@@ -2149,7 +2152,29 @@ If you add support for another doc comment style, please consider
contributing it: send a note to @email{}.
@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@node Misc Font Locking, AWK Mode Font Locking, Doc Comments, Font Locking
@node Wrong Comment Style, Misc Font Locking, Doc Comments, Font Locking
@comment node-name, next, previous, up
@section Marking ``Wrong'' style comments
@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Most languages supported by @ccmode{} have two styles of comments,
namely block comments and line comments. Your project may have such a
strong preference for one of them, that you wish ``wrong'' style
comments to be clearly marked.
You can get @ccmode{} to do this by setting the default comment style,
if necessary, (@pxref{Minor Modes}) and setting the customizable
option @code{c-mark-wrong-style-of-comment} to non-@code{nil}.
@defvar c-mark-wrong-style-of-comment
@vindex mark-wrong-style-of-comment (c-)
When this customizable option is non-@code{nil}, comment delimiters
which aren't of the default style will be fontified with
@end defvar
@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@node Misc Font Locking, AWK Mode Font Locking, Wrong Comment Style, Font Locking
@comment node-name, next, previous, up
@section Miscellaneous Font Locking
@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
......@@ -2885,11 +2910,11 @@ should not be changed directly; use @code{c-add-style} instead.
Instead of specifying a style, you can get @ccmode{} to @dfn{guess}
your style by examining an already formatted code buffer. @ccmode{}
then determines the ''most frequent'' offset (@pxref{c-offsets-alist})
then determines the ``most frequent'' offset (@pxref{c-offsets-alist})
for each of the syntactic symbols (@pxref{Indentation Engine Basics})
encountered in the buffer, and the ''most frequent'' value of
encountered in the buffer, and the ``most frequent'' value of
c-basic-offset (@pxref{Customizing Indentation}), then merges the
current style with these ''guesses'' to form a new style. This
current style with these ``guesses'' to form a new style. This
combined style is known as the @dfn{guessed style}.
To do this, call @code{c-guess} (or one of the other 5 guessing
......@@ -6927,7 +6952,7 @@ is @code{nil}, all lines inside macro definitions are analyzed as
Because a macro can expand into anything at all, near where one is
invoked @ccmode{} can only indent and fontify code heuristically.
Sometimes it gets it wrong. Usually you should try to design your
macros so that they ''look like ordinary code'' when you invoke them.
macros so that they ``look like ordinary code'' when you invoke them.
However, two situations are so common that @ccmode{} handles them
specially: that is when certain macros needn't (or mustn't) be
followed by a @samp{;}, and when certain macros (or compiler
......@@ -2264,6 +2264,14 @@ file-local variable, you may need to update the value.
*** Declare 'define-overload' and 'define-child-mode' as obsolete
*** Rename several internal functions to use a ''mode-local-' prefix
** CC Mode
*** You can now flag "wrong style" comments with font-lock-warning-face.
To do this, use c-toggle-comment-style, if needed, to set the desired
default comment style (block or line); then set the option
c-mark-wrong-style-of-comment to non-nil.
* New Modes and Packages in Emacs 27.1
......@@ -391,6 +391,16 @@ This action does nothing when the mode only has one comment style."
(if c-block-comment-flag
(concat " " c-block-comment-ender)
;; If necessary, invert the sense of fontification of wrong style comments.
(when (and c-mark-wrong-style-of-comment
(goto-char (point-min))
(c-font-lock-flush (point-min) (point-max)))))
......@@ -219,6 +219,13 @@ This variant works around bugs in `eval-when-compile' in various
`(cl-delete-duplicates ,cl-seq ,@cl-keys)
`(delete-duplicates ,cl-seq ,@cl-keys))))
(defmacro c-font-lock-flush (beg end)
"Declare the region BEG...END's fontification as out-of-date.
On XEmacs and older Emacsen, this refontifies that region immediately."
(if (fboundp 'font-lock-flush)
`(font-lock-flush ,beg ,end)
`(font-lock-fontify-region ,beg ,end)))
(defmacro c-point (position &optional point)
"Return the value of certain commonly referenced POSITIONs relative to POINT.
The current point is used if POINT isn't specified. POSITION can be
......@@ -95,6 +95,7 @@
;; during compilation.
(cc-bytecomp-defvar c-preprocessor-face-name)
(cc-bytecomp-defvar c-reference-face-name)
(cc-bytecomp-defvar c-block-comment-flag)
(cc-bytecomp-defun c-fontify-recorded-types-and-refs)
(cc-bytecomp-defun c-font-lock-declarators)
(cc-bytecomp-defun c-font-lock-objc-method)
......@@ -532,7 +533,12 @@ stuff. Used on level 1 and higher."
(sws-depth (c-lang-const c-syntactic-ws-depth))
(nsws-depth (c-lang-const c-nonempty-syntactic-ws-depth)))
`(;; The stuff after #error and #warning is a message, so
`(;; Fontify "invalid" comment delimiters
,@(when (and (c-lang-const c-block-comment-starter)
(c-lang-const c-line-comment-starter))
;; The stuff after #error and #warning is a message, so
;; fontify it as a string.
,@(when (c-lang-const c-cpp-message-directives)
(let* ((re (c-make-keywords-re 'appendable ; nil
......@@ -715,6 +721,59 @@ stuff. Used on level 1 and higher."
(parse-partial-sexp end limit nil nil state 'syntax-table)))
(defun c-maybe-font-lock-wrong-style-comments (limit)
;; This function will be called from font-lock-for a region bounded by POINT
;; and LIMIT, as though it were to identify a keyword for
;; font-lock-keyword-face. It always returns NIL to inhibit this and
;; prevent a repeat invocation. See elisp/lispref page "Search-based
;; Fontification".
;; This function fontifies "invalid" comment delimiters with
;; `font-lock-warning-face'. A delimiter is "invalid" when
;; `c-mark-wrong-style-of-comment' is non-nil, and the delimiter style is
;; not the default specified by `c-block-comment-flag'.
(when c-mark-wrong-style-of-comment
(let* ((lit (c-semi-pp-to-literal (point)))
(s (car lit)) ; parse-partial-sexp state.
;; First, deal with and move out of any literal we start in.
((null (cadr lit))) ; Not in a literal
((eq (cadr lit) 'string)
(setq s (parse-partial-sexp (point) limit nil nil s 'syntax-table)))
((and (not c-block-comment-flag) ; In an "invalid" block comment
(eq (cadr lit) 'c))
(setq s (parse-partial-sexp (point) limit nil nil s 'syntax-table))
;; Font lock the block comment ender with warning face.
(when (not (nth 4 s))
(c-put-font-lock-face (- (point) (length c-block-comment-ender))
(point) font-lock-warning-face)))
(t ; In a line comment, or a "valid" block comment
(setq s (parse-partial-sexp (point) limit nil nil s 'syntax-table))))
(while (< (point) limit)
(setq s (parse-partial-sexp (point) limit nil nil s 'syntax-table))
((or (nth 3 s) ; In a string
(and (nth 4 s) ; In a comment
(eq (nth 7 s) ; Comment style
(if c-block-comment-flag
nil ; Block comment
1)))) ; Line comment
;; Move over a "valid" literal.
(setq s (parse-partial-sexp (point) limit nil nil s 'syntax-table)))
((nth 4 s) ; In an invalid comment
;; Fontify the invalid comment opener.
(c-put-font-lock-face (nth 8 s) (point) font-lock-warning-face)
;; Move to end of comment or LIMIT.
(setq s (parse-partial-sexp (point) limit nil nil s 'syntax-table))
;; Fontify an invalid block comment ender, if that's what we have.
(when (and (not c-block-comment-flag)
(not (nth 4 s))) ; We're outside the comment
(c-put-font-lock-face (- (point) (length c-block-comment-ender))
(point) font-lock-warning-face)))))))
(c-lang-defconst c-basic-matchers-before
"Font lock matchers for basic keywords, labels, references and various
other easily recognizable things that should be fontified before generic
......@@ -1751,6 +1751,14 @@ variables.")
c-macro-names-with-semicolon: %s"
(defcustom c-mark-wrong-style-of-comment nil
"Fontify \"invalid\" comment delims with `font-lock-warning-face' if non-nil.
\"Invalid\" means a line comment when the default comment style (set by
`c-toggle-comment-style') is block, or a block comment otherwise."
:type 'boolean
:group 'c
:version 27.1)
(defvar c-file-style nil
"Variable interface for setting style via File Local Variables.
In a file's Local Variable section, you can set this variable to a
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment