aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlan Mackenzie2019-11-09 12:09:30 +0000
committerAlan Mackenzie2019-11-09 12:09:30 +0000
commitb293aa91bcc7f553ffbc6c67027f3c86d06ffbd7 (patch)
tree39f019da4ce7a41d1367c01f590487b529bad2a3
parent6daa80d04e575a27f53f60f5fafd7fcba39b4b2a (diff)
downloademacs-b293aa91bcc7f553ffbc6c67027f3c86d06ffbd7.tar.gz
emacs-b293aa91bcc7f553ffbc6c67027f3c86d06ffbd7.zip
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 function. (c-cpp-matchers): Call c-maybe-font-lock-wrong-style-comments when appropriate. * lisp/progmodes/cc-vars.el (c-mark-wrong-style-of-comment): New customizable option. * 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 footnote. (Wrong Comment Style): New page.
-rw-r--r--doc/misc/cc-mode.texi41
-rw-r--r--etc/NEWS8
-rw-r--r--lisp/progmodes/cc-cmds.el10
-rw-r--r--lisp/progmodes/cc-defs.el7
-rw-r--r--lisp/progmodes/cc-fonts.el61
-rw-r--r--lisp/progmodes/cc-vars.el8
6 files changed, 126 insertions, 9 deletions
diff --git a/doc/misc/cc-mode.texi b/doc/misc/cc-mode.texi
index 217261090bf..a802ca9886f 100644
--- a/doc/misc/cc-mode.texi
+++ b/doc/misc/cc-mode.texi
@@ -669,7 +669,7 @@ expression, to some statements, or perhaps to whole functions, the
669syntactic recognition can be wrong. @ccmode{} manages to figure it 669syntactic recognition can be wrong. @ccmode{} manages to figure it
670out correctly most of the time, though. 670out correctly most of the time, though.
671 671
672Some macros, when invoked, ''have their own semicolon''. To get the 672Some macros, when invoked, ``have their own semicolon''. To get the
673next line indented correctly, rather than as a continuation line, 673next line indented correctly, rather than as a continuation line,
674@xref{Macros with ;}. 674@xref{Macros with ;}.
675 675
@@ -1149,7 +1149,9 @@ find useful while writing new code or editing old code:
1149@table @asis 1149@table @asis
1150@item comment style 1150@item comment style
1151This specifies whether comment commands (such as @kbd{M-;}) insert 1151This specifies whether comment commands (such as @kbd{M-;}) insert
1152block comments or line comments. 1152block comments or line comments@footnote{You can emphasize
1153non-default style comments in your code by giving their delimiters
1154@code{font-lock-warning-face}. @xref{Wrong Comment Style}.}.
1153@item electric mode 1155@item electric mode
1154When this is enabled, certain visible characters cause reformatting as 1156When this is enabled, certain visible characters cause reformatting as
1155they are typed. This is normally helpful, but can be a nuisance when 1157they are typed. This is normally helpful, but can be a nuisance when
@@ -1866,6 +1868,7 @@ sections apply to the other languages.
1866* Font Locking Preliminaries:: 1868* Font Locking Preliminaries::
1867* Faces:: 1869* Faces::
1868* Doc Comments:: 1870* Doc Comments::
1871* Wrong Comment Style::
1869* Misc Font Locking:: 1872* Misc Font Locking::
1870* AWK Mode Font Locking:: 1873* AWK Mode Font Locking::
1871@end menu 1874@end menu
@@ -2069,7 +2072,7 @@ since those aren't syntactic errors in themselves.
2069 2072
2070 2073
2071@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 2074@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2072@node Doc Comments, Misc Font Locking, Faces, Font Locking 2075@node Doc Comments, Wrong Comment Style, Faces, Font Locking
2073@comment node-name, next, previous, up 2076@comment node-name, next, previous, up
2074@section Documentation Comments 2077@section Documentation Comments
2075@cindex documentation comments 2078@cindex documentation comments
@@ -2149,7 +2152,29 @@ If you add support for another doc comment style, please consider
2149contributing it: send a note to @email{bug-cc-mode@@gnu.org}. 2152contributing it: send a note to @email{bug-cc-mode@@gnu.org}.
2150 2153
2151@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 2154@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2152@node Misc Font Locking, AWK Mode Font Locking, Doc Comments, Font Locking 2155@node Wrong Comment Style, Misc Font Locking, Doc Comments, Font Locking
2156@comment node-name, next, previous, up
2157@section Marking ``Wrong'' style comments
2158@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2159
2160Most languages supported by @ccmode{} have two styles of comments,
2161namely block comments and line comments. Your project may have such a
2162strong preference for one of them, that you wish ``wrong'' style
2163comments to be clearly marked.
2164
2165You can get @ccmode{} to do this by setting the default comment style,
2166if necessary, (@pxref{Minor Modes}) and setting the customizable
2167option @code{c-mark-wrong-style-of-comment} to non-@code{nil}.
2168
2169@defvar c-mark-wrong-style-of-comment
2170@vindex mark-wrong-style-of-comment (c-)
2171When this customizable option is non-@code{nil}, comment delimiters
2172which aren't of the default style will be fontified with
2173@code{font-lock-warning-face}.
2174@end defvar
2175
2176@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2177@node Misc Font Locking, AWK Mode Font Locking, Wrong Comment Style, Font Locking
2153@comment node-name, next, previous, up 2178@comment node-name, next, previous, up
2154@section Miscellaneous Font Locking 2179@section Miscellaneous Font Locking
2155@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 2180@comment !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@@ -2885,11 +2910,11 @@ should not be changed directly; use @code{c-add-style} instead.
2885 2910
2886Instead of specifying a style, you can get @ccmode{} to @dfn{guess} 2911Instead of specifying a style, you can get @ccmode{} to @dfn{guess}
2887your style by examining an already formatted code buffer. @ccmode{} 2912your style by examining an already formatted code buffer. @ccmode{}
2888then determines the ''most frequent'' offset (@pxref{c-offsets-alist}) 2913then determines the ``most frequent'' offset (@pxref{c-offsets-alist})
2889for each of the syntactic symbols (@pxref{Indentation Engine Basics}) 2914for each of the syntactic symbols (@pxref{Indentation Engine Basics})
2890encountered in the buffer, and the ''most frequent'' value of 2915encountered in the buffer, and the ``most frequent'' value of
2891c-basic-offset (@pxref{Customizing Indentation}), then merges the 2916c-basic-offset (@pxref{Customizing Indentation}), then merges the
2892current style with these ''guesses'' to form a new style. This 2917current style with these ``guesses'' to form a new style. This
2893combined style is known as the @dfn{guessed style}. 2918combined style is known as the @dfn{guessed style}.
2894 2919
2895To do this, call @code{c-guess} (or one of the other 5 guessing 2920To 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
6927Because a macro can expand into anything at all, near where one is 6952Because a macro can expand into anything at all, near where one is
6928invoked @ccmode{} can only indent and fontify code heuristically. 6953invoked @ccmode{} can only indent and fontify code heuristically.
6929Sometimes it gets it wrong. Usually you should try to design your 6954Sometimes it gets it wrong. Usually you should try to design your
6930macros so that they ''look like ordinary code'' when you invoke them. 6955macros so that they ``look like ordinary code'' when you invoke them.
6931However, two situations are so common that @ccmode{} handles them 6956However, two situations are so common that @ccmode{} handles them
6932specially: that is when certain macros needn't (or mustn't) be 6957specially: that is when certain macros needn't (or mustn't) be
6933followed by a @samp{;}, and when certain macros (or compiler 6958followed by a @samp{;}, and when certain macros (or compiler
diff --git a/etc/NEWS b/etc/NEWS
index 95d6b9b5bd4..61b9f933f15 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -2264,6 +2264,14 @@ file-local variable, you may need to update the value.
2264*** Declare 'define-overload' and 'define-child-mode' as obsolete 2264*** Declare 'define-overload' and 'define-child-mode' as obsolete
2265*** Rename several internal functions to use a ''mode-local-' prefix 2265*** Rename several internal functions to use a ''mode-local-' prefix
2266 2266
2267** CC Mode
2268
2269+++
2270*** You can now flag "wrong style" comments with font-lock-warning-face.
2271To do this, use c-toggle-comment-style, if needed, to set the desired
2272default comment style (block or line); then set the option
2273c-mark-wrong-style-of-comment to non-nil.
2274
2267 2275
2268* New Modes and Packages in Emacs 27.1 2276* New Modes and Packages in Emacs 27.1
2269 2277
diff --git a/lisp/progmodes/cc-cmds.el b/lisp/progmodes/cc-cmds.el
index 9aa2019ae60..af2efb63fc3 100644
--- a/lisp/progmodes/cc-cmds.el
+++ b/lisp/progmodes/cc-cmds.el
@@ -391,6 +391,16 @@ This action does nothing when the mode only has one comment style."
391 (if c-block-comment-flag 391 (if c-block-comment-flag
392 (concat " " c-block-comment-ender) 392 (concat " " c-block-comment-ender)
393 "")) 393 ""))
394 ;; If necessary, invert the sense of fontification of wrong style comments.
395 (when (and c-mark-wrong-style-of-comment
396 font-lock-mode
397 c-block-comment-starter
398 c-block-comment-ender)
399 (save-excursion
400 (save-restriction
401 (widen)
402 (goto-char (point-min))
403 (c-font-lock-flush (point-min) (point-max)))))
394 (c-update-modeline) 404 (c-update-modeline)
395 (c-keep-region-active)) 405 (c-keep-region-active))
396 406
diff --git a/lisp/progmodes/cc-defs.el b/lisp/progmodes/cc-defs.el
index e9a3e16db36..6a9371e6f19 100644
--- a/lisp/progmodes/cc-defs.el
+++ b/lisp/progmodes/cc-defs.el
@@ -219,6 +219,13 @@ This variant works around bugs in `eval-when-compile' in various
219 `(cl-delete-duplicates ,cl-seq ,@cl-keys) 219 `(cl-delete-duplicates ,cl-seq ,@cl-keys)
220 `(delete-duplicates ,cl-seq ,@cl-keys)))) 220 `(delete-duplicates ,cl-seq ,@cl-keys))))
221 221
222(defmacro c-font-lock-flush (beg end)
223 "Declare the region BEG...END's fontification as out-of-date.
224On XEmacs and older Emacsen, this refontifies that region immediately."
225 (if (fboundp 'font-lock-flush)
226 `(font-lock-flush ,beg ,end)
227 `(font-lock-fontify-region ,beg ,end)))
228
222(defmacro c-point (position &optional point) 229(defmacro c-point (position &optional point)
223 "Return the value of certain commonly referenced POSITIONs relative to POINT. 230 "Return the value of certain commonly referenced POSITIONs relative to POINT.
224The current point is used if POINT isn't specified. POSITION can be 231The current point is used if POINT isn't specified. POSITION can be
diff --git a/lisp/progmodes/cc-fonts.el b/lisp/progmodes/cc-fonts.el
index c27b70603ed..0daea8c84c9 100644
--- a/lisp/progmodes/cc-fonts.el
+++ b/lisp/progmodes/cc-fonts.el
@@ -95,6 +95,7 @@
95;; during compilation. 95;; during compilation.
96(cc-bytecomp-defvar c-preprocessor-face-name) 96(cc-bytecomp-defvar c-preprocessor-face-name)
97(cc-bytecomp-defvar c-reference-face-name) 97(cc-bytecomp-defvar c-reference-face-name)
98(cc-bytecomp-defvar c-block-comment-flag)
98(cc-bytecomp-defun c-fontify-recorded-types-and-refs) 99(cc-bytecomp-defun c-fontify-recorded-types-and-refs)
99(cc-bytecomp-defun c-font-lock-declarators) 100(cc-bytecomp-defun c-font-lock-declarators)
100(cc-bytecomp-defun c-font-lock-objc-method) 101(cc-bytecomp-defun c-font-lock-objc-method)
@@ -532,7 +533,12 @@ stuff. Used on level 1 and higher."
532 (sws-depth (c-lang-const c-syntactic-ws-depth)) 533 (sws-depth (c-lang-const c-syntactic-ws-depth))
533 (nsws-depth (c-lang-const c-nonempty-syntactic-ws-depth))) 534 (nsws-depth (c-lang-const c-nonempty-syntactic-ws-depth)))
534 535
535 `(;; The stuff after #error and #warning is a message, so 536 `(;; Fontify "invalid" comment delimiters
537 ,@(when (and (c-lang-const c-block-comment-starter)
538 (c-lang-const c-line-comment-starter))
539 `(c-maybe-font-lock-wrong-style-comments))
540
541 ;; The stuff after #error and #warning is a message, so
536 ;; fontify it as a string. 542 ;; fontify it as a string.
537 ,@(when (c-lang-const c-cpp-message-directives) 543 ,@(when (c-lang-const c-cpp-message-directives)
538 (let* ((re (c-make-keywords-re 'appendable ; nil 544 (let* ((re (c-make-keywords-re 'appendable ; nil
@@ -715,6 +721,59 @@ stuff. Used on level 1 and higher."
715 (parse-partial-sexp end limit nil nil state 'syntax-table))) 721 (parse-partial-sexp end limit nil nil state 'syntax-table)))
716 nil) 722 nil)
717 723
724(defun c-maybe-font-lock-wrong-style-comments (limit)
725 ;; This function will be called from font-lock-for a region bounded by POINT
726 ;; and LIMIT, as though it were to identify a keyword for
727 ;; font-lock-keyword-face. It always returns NIL to inhibit this and
728 ;; prevent a repeat invocation. See elisp/lispref page "Search-based
729 ;; Fontification".
730 ;;
731 ;; This function fontifies "invalid" comment delimiters with
732 ;; `font-lock-warning-face'. A delimiter is "invalid" when
733 ;; `c-mark-wrong-style-of-comment' is non-nil, and the delimiter style is
734 ;; not the default specified by `c-block-comment-flag'.
735 (when c-mark-wrong-style-of-comment
736 (let* ((lit (c-semi-pp-to-literal (point)))
737 (s (car lit)) ; parse-partial-sexp state.
738 )
739 ;; First, deal with and move out of any literal we start in.
740 (cond
741 ((null (cadr lit))) ; Not in a literal
742 ((eq (cadr lit) 'string)
743 (setq s (parse-partial-sexp (point) limit nil nil s 'syntax-table)))
744 ((and (not c-block-comment-flag) ; In an "invalid" block comment
745 (eq (cadr lit) 'c))
746 (setq s (parse-partial-sexp (point) limit nil nil s 'syntax-table))
747 ;; Font lock the block comment ender with warning face.
748 (when (not (nth 4 s))
749 (c-put-font-lock-face (- (point) (length c-block-comment-ender))
750 (point) font-lock-warning-face)))
751 (t ; In a line comment, or a "valid" block comment
752 (setq s (parse-partial-sexp (point) limit nil nil s 'syntax-table))))
753
754 (while (< (point) limit)
755 (setq s (parse-partial-sexp (point) limit nil nil s 'syntax-table))
756 (cond
757 ((or (nth 3 s) ; In a string
758 (and (nth 4 s) ; In a comment
759 (eq (nth 7 s) ; Comment style
760 (if c-block-comment-flag
761 nil ; Block comment
762 1)))) ; Line comment
763 ;; Move over a "valid" literal.
764 (setq s (parse-partial-sexp (point) limit nil nil s 'syntax-table)))
765 ((nth 4 s) ; In an invalid comment
766 ;; Fontify the invalid comment opener.
767 (c-put-font-lock-face (nth 8 s) (point) font-lock-warning-face)
768 ;; Move to end of comment or LIMIT.
769 (setq s (parse-partial-sexp (point) limit nil nil s 'syntax-table))
770 ;; Fontify an invalid block comment ender, if that's what we have.
771 (when (and (not c-block-comment-flag)
772 (not (nth 4 s))) ; We're outside the comment
773 (c-put-font-lock-face (- (point) (length c-block-comment-ender))
774 (point) font-lock-warning-face)))))))
775 nil)
776
718(c-lang-defconst c-basic-matchers-before 777(c-lang-defconst c-basic-matchers-before
719 "Font lock matchers for basic keywords, labels, references and various 778 "Font lock matchers for basic keywords, labels, references and various
720other easily recognizable things that should be fontified before generic 779other easily recognizable things that should be fontified before generic
diff --git a/lisp/progmodes/cc-vars.el b/lisp/progmodes/cc-vars.el
index 60ad42f24f4..273174e0413 100644
--- a/lisp/progmodes/cc-vars.el
+++ b/lisp/progmodes/cc-vars.el
@@ -1751,6 +1751,14 @@ variables.")
1751c-macro-names-with-semicolon: %s" 1751c-macro-names-with-semicolon: %s"
1752 c-macro-names-with-semicolon)))))) 1752 c-macro-names-with-semicolon))))))
1753 1753
1754(defcustom c-mark-wrong-style-of-comment nil
1755 "Fontify \"invalid\" comment delims with `font-lock-warning-face' if non-nil.
1756\"Invalid\" means a line comment when the default comment style (set by
1757`c-toggle-comment-style') is block, or a block comment otherwise."
1758 :type 'boolean
1759 :group 'c
1760 :version 27.1)
1761
1754(defvar c-file-style nil 1762(defvar c-file-style nil
1755 "Variable interface for setting style via File Local Variables. 1763 "Variable interface for setting style via File Local Variables.
1756In a file's Local Variable section, you can set this variable to a 1764In a file's Local Variable section, you can set this variable to a