GNU bug report logs - #4192
23.1; special fontification for "//" in cc-mode

Previous Next

Packages: emacs, cc-mode;

Reported by: Tom Tromey <tromey <at> redhat.com>

Date: Thu, 20 Aug 2009 20:06:04 UTC

Severity: wishlist

Tags: patch

Done: Alan Mackenzie <acm <at> muc.de>

Bug is archived. No further changes may be made.

Full log


View this message in rfc822 format

From: Alan Mackenzie <acm <at> muc.de>
To: Tom Tromey <tromey <at> redhat.com>, 4192 <at> debbugs.gnu.org
Cc: bug-gnu-emacs <at> gnu.org, bug-cc-mode <at> gnu.org
Subject: bug#4192: 23.1; special fontification for "//" in cc-mode
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
bug-cc-mode <at> gnu.org.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 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).




This bug report was last modified 5 years and 194 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.