emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: /* */ style comments in C++ sources


From: Alan Mackenzie
Subject: Re: /* */ style comments in C++ sources
Date: Thu, 25 May 2017 15:48:41 +0000
User-agent: Mutt/1.5.24 (2015-08-30)

On Thu, May 25, 2017 at 05:32:48 +0300, Eli Zaretskii wrote:
> > Date: Wed, 24 May 2017 20:53:16 +0000
> > Cc: address@hidden
> > From: Alan Mackenzie <address@hidden>

> > What do you think of the following:

> > (i) There would be a new "CC Mode minor mode" with an interactive
> > function called `c-toggle-comment-style', which would toggle a new buffer
> > local variable, c-comment-style.  This variable could be set in local
> > variables, or in .emacs in the same way as any other CC Mode buffer
> > setting.
> > (ii) `c-toggle-comment-style' would be bound to an unused CC Mode
> > binding, possibly C-c C-k.
> > (iii) I'm also thinking about putting it on C-c C-;, though terminal
> > keyboards tend not to have C-; (mine doesn't (yet)).
> > (iv) The major mode mode line entry, which currently looks something
> > like "C/l" would be amended to "C/*l" or "C//l", with the obvious meaning.
> > (v) The new buffer-local variable `c-comment-style' would influence all
> > standard Emacs commands which create comments.

> > ?

> Sounds good to me, thanks.

OK, here's a first draught.  I've written some documentation for
cc-mode.texi, but I'm not happy about its state at the moment.

Basically, in a CC Mode buffer, C-c C-k will toggle the comment style.
Set `c-block-comment-flag' to nil or t in, e.g., a mode hook to do the
obvious.

I recommend starting a fresh Emacs with this patch, otherwise the
keymaps, which are defvars, wouldn't get reinitialised on loading the
patched CC Mode.  Etc.  As well as the three patched files, please also
recompile cc-engine.el (or, just recompile CC Mode as a whole).

Comments?



diff --git a/lisp/progmodes/cc-cmds.el b/lisp/progmodes/cc-cmds.el
index a5ddcb4b92..582587a273 100644
--- a/lisp/progmodes/cc-cmds.el
+++ b/lisp/progmodes/cc-cmds.el
@@ -252,7 +252,8 @@ c-syntactic-information-on-region
 
 ;; Minor mode functions.
 (defun c-update-modeline ()
-  (let ((fmt (format "/%s%s%s%s"
+  (let ((fmt (format "/%s%s%s%s%s"
+                    (if c-block-comment-flag "*" "/")
                     (if c-electric-flag "l" "")
                     (if (and c-electric-flag c-auto-newline)
                         "a" "")
@@ -270,9 +271,6 @@ c-update-modeline
        (bare-mode-name (if (string-match "\\(^[^/]*\\)/" mode-name)
                            (match-string 1 mode-name)
                          mode-name)))
-;;     (setq c-submode-indicators
-;;       (if (> (length fmt) 1)
-;;           fmt))
     (setq mode-name
          (if (> (length fmt) 1)
              (concat bare-mode-name fmt)
@@ -362,6 +360,37 @@ c-toggle-electric-state
     (electric-indent-local-mode (if c-electric-flag 1 0)))
   (c-keep-region-active))
 
+;; `c-block-comment-flag' gets initialized to the current mode's default in
+;; c-basic-common-init.
+(defvar c-block-comment-flag nil)
+(make-variable-buffer-local 'c-block-comment-flag)
+
+(defun c-toggle-comment-style (&optional arg)
+  "Toggle the comment style between block and line comments.
+Optional numeric ARG, if supplied, switches to block comment
+style when positive, to line comment style when negative, and
+just toggles it when zero or left out.
+
+This action does nothing when the mode only has one comment style."
+  (interactive "P")
+  (setq c-block-comment-flag
+       (cond
+        ((and c-line-comment-starter c-block-comment-starter)
+         (c-calculate-state arg c-block-comment-flag))
+        (c-line-comment-starter nil)
+        (t t)))
+  (setq comment-start
+       (concat (if c-block-comment-flag
+                   c-block-comment-starter
+                 c-line-comment-starter)
+               " "))
+  (setq comment-end
+       (if c-block-comment-flag
+           (concat " " c-block-comment-ender)
+         ""))
+  (c-update-modeline)
+  (c-keep-region-active))
+
 
 ;; Electric keys
 
diff --git a/lisp/progmodes/cc-langs.el b/lisp/progmodes/cc-langs.el
index 84d4eab75a..a9d5ac34ad 100644
--- a/lisp/progmodes/cc-langs.el
+++ b/lisp/progmodes/cc-langs.el
@@ -1449,6 +1449,7 @@ 'c-opt-op-identitier-prefix
 properly."
   t    "/*"
   awk  nil)
+(c-lang-defvar c-block-comment-starter (c-lang-const c-block-comment-starter))
 
 (c-lang-defconst c-block-comment-ender
   "String that ends block comments, or nil if such don't exist.
@@ -1458,6 +1459,7 @@ 'c-opt-op-identitier-prefix
 properly."
   t    "*/"
   awk  nil)
+(c-lang-defvar c-block-comment-ender (c-lang-const c-block-comment-ender))
 
 (c-lang-defconst c-block-comment-ender-regexp
   ;; Regexp which matches the end of a block comment (if such exists in the
@@ -1515,27 +1517,30 @@ 'c-opt-op-identitier-prefix
 (c-lang-defvar c-doc-comment-start-regexp
   (c-lang-const c-doc-comment-start-regexp))
 
+(c-lang-defconst c-block-comment-is-default
+  "Non-nil when the default comment style is block comment."
+  ;; Note to maintainers of derived modes: You are responsible for ensuring
+  ;; the pertinent `c-block-comment-{starter,ender}' or
+  ;; `c-line-comment-{starter,ender}' are non-nil.
+  t nil
+  c t)
+(c-lang-defvar c-block-comment-is-default
+  (c-lang-const c-block-comment-is-default))
+
 (c-lang-defconst comment-start
   "String that starts comments inserted with M-; etc.
 `comment-start' is initialized from this."
-  ;; Default: Prefer line comments to block comments, and pad with a space.
-  t (concat (or (c-lang-const c-line-comment-starter)
-               (c-lang-const c-block-comment-starter))
-           " ")
-  ;; In C we still default to the block comment style since line
-  ;; comments aren't entirely portable.
-  c "/* ")
+  t (concat
+     (if (c-lang-const c-block-comment-is-default)
+        (c-lang-const c-block-comment-starter)
+       (c-lang-const c-line-comment-starter))
+     " "))
 (c-lang-setvar comment-start (c-lang-const comment-start))
 
 (c-lang-defconst comment-end
   "String that ends comments inserted with M-; etc.
 `comment-end' is initialized from this."
-  ;; Default: Use block comment style if comment-start uses block
-  ;; comments, and pad with a space in that case.
-  t (if (string-match (concat "\\`\\("
-                             (c-lang-const c-block-comment-start-regexp)
-                             "\\)")
-                     (c-lang-const comment-start))
+  t (if (c-lang-const c-block-comment-is-default)
        (concat " " (c-lang-const c-block-comment-ender))
       ""))
 (c-lang-setvar comment-end (c-lang-const comment-end))
diff --git a/lisp/progmodes/cc-mode.el b/lisp/progmodes/cc-mode.el
index 20c63d4dbe..42c75461df 100644
--- a/lisp/progmodes/cc-mode.el
+++ b/lisp/progmodes/cc-mode.el
@@ -398,7 +398,7 @@ c-bind-special-erase-keys
   ;;(define-key c-mode-base-map "\C-c\C-v"  'c-version)
   ;; (define-key c-mode-base-map "\C-c\C-y"  'c-toggle-hungry-state)  
Commented out by ACM, 2005-11-22.
   (define-key c-mode-base-map "\C-c\C-w" 'c-subword-mode)
-  )
+  (define-key c-mode-base-map "\C-c\C-k" 'c-toggle-comment-style))
 
 ;; We don't require the outline package, but we configure it a bit anyway.
 (cc-bytecomp-defvar outline-level)
@@ -547,7 +547,7 @@ c-basic-common-init
        (setq yank-handled-properties (remq yank-cat-handler
                                            yank-handled-properties)))))
 
-  ;; For the benefit of adaptive file, which otherwise mis-fills.
+  ;; For the benefit of adaptive fill, which otherwise mis-fills.
   (setq fill-paragraph-handle-comment nil)
 
   ;; Install `c-fill-paragraph' on `fill-paragraph-function' so that a
@@ -623,6 +623,8 @@ c-basic-common-init
 
   ;; setup the comment indent variable in a Emacs version portable way
   (set (make-local-variable 'comment-indent-function) 'c-comment-indent)
+  ;; What sort of comments are default for M-;?
+  (setq c-block-comment-flag c-block-comment-is-default)
 
   ;; In Emacs 24.4 onwards, prevent Emacs's built in electric indentation from
   ;; messing up CC Mode's, and set `c-electric-flag' if `electric-indent-mode'


-- 
Alan Mackenzie (Nuremberg, Germany).



reply via email to

[Prev in Thread] Current Thread [Next in Thread]