From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Alan Mackenzie Newsgroups: gmane.emacs.bugs Subject: bug#4192: 23.1; special fontification for "//" in cc-mode Date: Sun, 3 Nov 2019 17:46:18 +0000 Message-ID: <20191103174618.GA11619__38138.7747985841$1572803248$gmane$org@ACM> References: <20090821213323.GC1897@muc.de> <87k2lo193k.fsf@gnus.org> <87wocjzii7.fsf@gnus.org> <20191102163513.GC6710@ACM> <875zk1ar9d.fsf@gnus.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="106337"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Mutt/1.10.1 (2018-07-13) Cc: tom@tromey.com, 4192@debbugs.gnu.org To: Lars Ingebrigtsen Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Sun Nov 03 18:47:22 2019 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1iRJyD-000RYE-N9 for geb-bug-gnu-emacs@m.gmane.org; Sun, 03 Nov 2019 18:47:21 +0100 Original-Received: from localhost ([::1]:55430 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iRJyC-0002BW-5X for geb-bug-gnu-emacs@m.gmane.org; Sun, 03 Nov 2019 12:47:20 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:34401) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iRJy0-0002BP-N4 for bug-gnu-emacs@gnu.org; Sun, 03 Nov 2019 12:47:10 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iRJxy-0006cI-Q3 for bug-gnu-emacs@gnu.org; Sun, 03 Nov 2019 12:47:08 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]:54474) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iRJxt-0006aR-Qh; Sun, 03 Nov 2019 12:47:01 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1iRJxt-0005ao-Jx; Sun, 03 Nov 2019 12:47:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Alan Mackenzie Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org, bug-cc-mode@gnu.org Resent-Date: Sun, 03 Nov 2019 17:47:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 4192 X-GNU-PR-Package: emacs,cc-mode X-GNU-PR-Keywords: patch X-Debbugs-Original-Cc: bug-cc-mode@gnu.org, Tom Tromey , 4192@debbugs.gnu.org Original-Received: via spool by 4192-submit@debbugs.gnu.org id=B4192.157280318921451 (code B ref 4192); Sun, 03 Nov 2019 17:47:01 +0000 Original-Received: (at 4192) by debbugs.gnu.org; 3 Nov 2019 17:46:29 +0000 Original-Received: from localhost ([127.0.0.1]:35062 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1iRJxM-0005Zu-NZ for submit@debbugs.gnu.org; Sun, 03 Nov 2019 12:46:29 -0500 Original-Received: from colin.muc.de ([193.149.48.1]:56649 helo=mail.muc.de) by debbugs.gnu.org with smtp (Exim 4.84_2) (envelope-from ) id 1iRJxH-0005Zj-PZ for 4192@debbugs.gnu.org; Sun, 03 Nov 2019 12:46:27 -0500 Original-Received: (qmail 90103 invoked by uid 3782); 3 Nov 2019 17:46:22 -0000 Original-Received: from acm.muc.de (p2E5D5ADE.dip0.t-ipconnect.de [46.93.90.222]) by colin.muc.de (tmda-ofmipd) with ESMTP; Sun, 03 Nov 2019 18:46:18 +0100 Original-Received: (qmail 11657 invoked by uid 1000); 3 Nov 2019 17:46:18 -0000 Content-Disposition: inline In-Reply-To: <875zk1ar9d.fsf@gnus.org> X-Delivery-Agent: TMDA/1.1.12 (Macallan) X-Primary-Address: acm@muc.de X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.51.188.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:170891 Archived-At: Hello, Lars. On Sun, Nov 03, 2019 at 16:55:42 +0100, Lars Ingebrigtsen wrote: > Alan Mackenzie writes: > > So, yes, sometime in the very near future, I will implement this. > Great! Would you (and anybody else) please review the following patch. Short instructions on how to enable the feature are in the NEWS alteration; longer ones are in the cc-mode.texi bit: diff --git a/doc/misc/cc-mode.texi b/doc/misc/cc-mode.texi index 217261090b..5ee5540284 100644 --- a/doc/misc/cc-mode.texi +++ b/doc/misc/cc-mode.texi @@ -1149,7 +1149,9 @@ Minor Modes @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 @@ Font Locking * Font Locking Preliminaries:: * Faces:: * Doc Comments:: +* Wrong Comment Style:: * Misc Font Locking:: * AWK Mode Font Locking:: @end menu @@ -2069,7 +2072,7 @@ Faces @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 @@ Doc Comments contributing it: send a note to @email{bug-cc-mode@@gnu.org}. @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 +@code{font-lock-warning-face}. +@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 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! diff --git a/etc/NEWS b/etc/NEWS index 033cb48978..56faf13281 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -2215,6 +2215,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 the "wrong style" of 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 diff --git a/lisp/progmodes/cc-fonts.el b/lisp/progmodes/cc-fonts.el index c27b70603e..0daea8c84c 100644 --- a/lisp/progmodes/cc-fonts.el +++ b/lisp/progmodes/cc-fonts.el @@ -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 @@ c-fontify-recorded-types-and-refs (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)) + `(c-maybe-font-lock-wrong-style-comments)) + + ;; 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 @@ c-font-lock-invalid-single-quotes (parse-partial-sexp end limit nil nil state 'syntax-table))) nil) +(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. + (cond + ((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)) + (cond + ((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))))))) + nil) + (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 diff --git a/lisp/progmodes/cc-vars.el b/lisp/progmodes/cc-vars.el index 60ad42f24f..273174e041 100644 --- a/lisp/progmodes/cc-vars.el +++ b/lisp/progmodes/cc-vars.el @@ -1751,6 +1751,14 @@ c-make-macro-with-semi-re c-macro-names-with-semicolon: %s" c-macro-names-with-semicolon)))))) +(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 > -- > (domestic pets only, the antidote for overdose, milk.) > bloggy blog: http://lars.ingebrigtsen.no -- Alan Mackenzie (Nuremberg, Germany).