From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Alan Mackenzie Newsgroups: gmane.emacs.devel Subject: Re: /* */ style comments in C++ sources Date: Thu, 25 May 2017 15:48:41 +0000 Message-ID: <20170525154841.GA5313@acm.fritz.box> References: <83shjvmmeq.fsf@gnu.org> <20170523203144.GB10407@acm.fritz.box> <83k257m1jp.fsf@gnu.org> <20170524205316.GA6009@acm.fritz.box> <83wp95ll7z.fsf@gnu.org> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: blaine.gmane.org 1495727449 23018 195.159.176.226 (25 May 2017 15:50:49 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Thu, 25 May 2017 15:50:49 +0000 (UTC) User-Agent: Mutt/1.5.24 (2015-08-30) Cc: Dani Moncayo , emacs-devel@gnu.org To: Eli Zaretskii Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Thu May 25 17:50:45 2017 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dDv2C-0005sF-PG for ged-emacs-devel@m.gmane.org; Thu, 25 May 2017 17:50:44 +0200 Original-Received: from localhost ([::1]:60613 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dDv2I-0004Iy-6p for ged-emacs-devel@m.gmane.org; Thu, 25 May 2017 11:50:50 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:39859) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dDv1W-0004IT-SE for emacs-devel@gnu.org; Thu, 25 May 2017 11:50:04 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dDv1V-0003Wq-AP for emacs-devel@gnu.org; Thu, 25 May 2017 11:50:02 -0400 Original-Received: from ocolin.muc.de ([193.149.48.4]:37422 helo=mail.muc.de) by eggs.gnu.org with smtp (Exim 4.71) (envelope-from ) id 1dDv1U-0003Wd-W6 for emacs-devel@gnu.org; Thu, 25 May 2017 11:50:01 -0400 Original-Received: (qmail 43484 invoked by uid 3782); 25 May 2017 15:50:00 -0000 Original-Received: from acm.muc.de (p548C73CB.dip0.t-ipconnect.de [84.140.115.203]) by colin.muc.de (tmda-ofmipd) with ESMTP; Thu, 25 May 2017 17:49:42 +0200 Original-Received: (qmail 5541 invoked by uid 1000); 25 May 2017 15:48:41 -0000 Content-Disposition: inline In-Reply-To: <83wp95ll7z.fsf@gnu.org> X-Delivery-Agent: TMDA/1.1.12 (Macallan) X-Primary-Address: acm@muc.de X-detected-operating-system: by eggs.gnu.org: FreeBSD 9.x [fuzzy] X-Received-From: 193.149.48.4 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:215200 Archived-At: On Thu, May 25, 2017 at 05:32:48 +0300, Eli Zaretskii wrote: > > Date: Wed, 24 May 2017 20:53:16 +0000 > > Cc: emacs-devel@gnu.org > > From: Alan Mackenzie > > 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).