Subject: Re: bug#4192: 23.1; special fontification for "//" in cc-mode

From: Alan Mackenzie <[email-masked]>
Date: Fri, 21 Aug 2009 21:33:23 +0000

Hi, Tom!

On Tue, Aug 18, 2009 at 10:24:02AM -0600, Tom Tromey wrote:

> This is a feature request for cc-mode.

> I work on a number of projects that use C89, not C99.  In these
> projects, it is invalid to use "//"-style comment.  It would be nice if
> cc-mode would optionally flag such comments in a special face, so that
> I can immediately see when I've written a comment incorrectly.

I'm not sure this is important enough to warrant a fully
implemented/documented/QA'd feature, but then again, I'm not sure it's
not.

Here's a quick hack called c-disable-line-comments.el.  Load it into your
Emacs before CC Mode (in particular, before desktop gets loaded).

Toggling c-disable-line-comments doesn't work smoothly with the font
locking, but I'm assuming here you'll not really be wanting to toggle
this.

Criticism is, of course, welcome.  I've set the followup to
[email-masked].

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; c-disable-line-comments.el.
;; 
;; A function which disables line comments in C mode and fontifies the
;; delimiter "//" with font-lock-warning-face.
;;
;; It should be loaded before CC Mode.  It will only work in GNU Emacs, and
;; has only been "properly" tested in Emacs 23.1.
;;
;; Written 2009-08-21 by Alan Mackenzie at the request of Tom Tromey.
;;
;; This file isn't part of Emacs.  It's licence is GPL version 2, or (at your
;; option) any later version.  The copyright has been assigned to the FSF.

(defun c-warning-face-on-// ()
  (font-lock-add-keywords 'c-mode
			  '(("//" . font-lock-warning-face))))
(add-hook 'c-initialization-hook 'c-warning-face-on-//)

(defvar c-disable-line-comments t
  "Is t when line comments in C Mode are disabled, nil otherwise.
Note that this variable is NOT buffer local.")

(defun c-disable-line-comments (arg)
  "Toggle the disablement of line comments in every C Mode buffer.
With a non-nil argument \(which must be numeric), disable
comments when it's positive, reenable them when negative.

\(Incidentally, the delimiter \"//\" gets fontified with
font-lock-warning-face when \"//\" is disabled, but this defun
doesn't do this."
  (interactive "P")
  (setq c-disable-line-comments
	(cond
	 ((null arg) (not c-disable-line-comments))
	 ((> (prefix-numeric-value arg) 0) t)
	 (t nil)))

  (if c-disable-line-comments
      (progn
	(modify-syntax-entry ?/  ". 14" c-mode-syntax-table)
	(modify-syntax-entry ?\n "  "  c-mode-syntax-table)
	(modify-syntax-entry ?\r "  "  c-mode-syntax-table))
    (modify-syntax-entry ?/  ". 124b" c-mode-syntax-table)
    (modify-syntax-entry ?\n "> b"  c-mode-syntax-table)
    (modify-syntax-entry ?\r "> b"  c-mode-syntax-table)))

(c-disable-line-comments 1)
;; End of c-disable-line-comments.el
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

> Tom

-- 
Alan Mackenzie (Nuremberg, Germany).