GNU bug report logs - #37862
[PATCH] Use alternative C++ source files for determining C/C++ header files.

Previous Next

Packages: cc-mode, emacs;

Reported by: Hong Xu <hong <at> topbug.net>

Date: Tue, 22 Oct 2019 03:14:03 UTC

Severity: wishlist

Tags: patch, wontfix

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

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 37862 in the body.
You can then email your comments to 37862 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#37862; Package emacs. (Tue, 22 Oct 2019 03:14:03 GMT) Full text and rfc822 format available.

Acknowledgement sent to Hong Xu <hong <at> topbug.net>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Tue, 22 Oct 2019 03:14:03 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Hong Xu <hong <at> topbug.net>
To: bug-gnu-emacs <at> gnu.org
Subject: [PATCH] Use alternative C++ source files for determining C/C++
 header files.
Date: Mon, 21 Oct 2019 20:12:51 -0700
* lisp/progmodes/cc-mode.el (c-or-c++-mode): Add check for the
existence of alternative C++ source files.
---
 lisp/progmodes/cc-mode.el | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/lisp/progmodes/cc-mode.el b/lisp/progmodes/cc-mode.el
index 6bdfb170f1ba..940bd4be3f92 100644
--- a/lisp/progmodes/cc-mode.el
+++ b/lisp/progmodes/cc-mode.el
@@ -2492,16 +2492,23 @@ c-or-c++-mode
 matching on file name insufficient for detecting major mode that
 should be used.
 
-This function attempts to use file contents to determine whether
-the code is C or C++ and based on that chooses whether to enable
-`c-mode' or `c++-mode'."
-  (if (save-excursion
+This function attempts to use file contents and the existence of
+alternative C++ source files to determine whether the code is C
+or C++ and based on that chooses whether to enable `c-mode' or
+`c++-mode'."
+  (if (or
+       (save-excursion
         (save-restriction
           (save-match-data
             (widen)
             (goto-char (point-min))
             (re-search-forward c-or-c++-mode--regexp
                                (+ (point) c-guess-region-max) t))))
+       ;; We could have gone through auto-mode-alist here, but it
+       ;; might be too slow.
+       (let ((base-name (file-name-sans-extension (buffer-file-name))))
+	 (seq-find (lambda (ext) (file-exists-p (concat base-name ext)))
+		   '(".cc" ".cpp" ".cxx" ".c++"))))
       (c++-mode)
     (c-mode)))
 
-- 
2.20.1






Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#37862; Package emacs. (Sat, 26 Oct 2019 11:07:02 GMT) Full text and rfc822 format available.

Message #8 received at 37862 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Hong Xu <hong <at> topbug.net>, Alan Mackenzie <acm <at> muc.de>
Cc: 37862 <at> debbugs.gnu.org
Subject: Re: bug#37862: [PATCH] Use alternative C++ source files for
 determining C/C++ header files.
Date: Sat, 26 Oct 2019 14:06:39 +0300
> From: Hong Xu <hong <at> topbug.net>
> Date: Mon, 21 Oct 2019 20:12:51 -0700
> 
> 
> * lisp/progmodes/cc-mode.el (c-or-c++-mode): Add check for the
> existence of alternative C++ source files.

Thanks.

Alan, are you looking at this?
 




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#37862; Package emacs. (Sat, 26 Oct 2019 15:20:02 GMT) Full text and rfc822 format available.

Message #11 received at 37862 <at> debbugs.gnu.org (full text, mbox):

From: Alan Mackenzie <acm <at> muc.de>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: 37862 <at> debbugs.gnu.org, Hong Xu <hong <at> topbug.net>
Subject: Re: bug#37862: [PATCH] Use alternative C++ source files for
 determining C/C++ header files.
Date: Sat, 26 Oct 2019 15:19:20 +0000
Hello, Eli.

On Sat, Oct 26, 2019 at 14:06:39 +0300, Eli Zaretskii wrote:
> > From: Hong Xu <hong <at> topbug.net>
> > Date: Mon, 21 Oct 2019 20:12:51 -0700


> > * lisp/progmodes/cc-mode.el (c-or-c++-mode): Add check for the
> > existence of alternative C++ source files.

> Thanks.

> Alan, are you looking at this?

Yes, I've been thinking about it for a few days, now.

I think what we could do with is a function `auto-mode' which, given a
file name, would return the corresponding major mode as recorded in
auto-mode-alist.  At the moment, this functionality is a part of
set-auto-mode, making it more difficult for other functions to do the
same thing.  It shouldn't be difficult.

-- 
Alan Mackenzie (Nuremberg, Germany).




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#37862; Package emacs. (Sun, 27 Oct 2019 13:31:01 GMT) Full text and rfc822 format available.

Message #14 received at 37862 <at> debbugs.gnu.org (full text, mbox):

From: Alan Mackenzie <acm <at> muc.de>
To: Hong Xu <hong <at> topbug.net>
Cc: 37862 <at> debbugs.gnu.org
Subject: Re: bug#37862: [PATCH] Use alternative C++ source files for
 determining C/C++ header files.
Date: Sun, 27 Oct 2019 13:30:38 +0000
Hello, Hong.

On Mon, Oct 21, 2019 at 20:12:51 -0700, Hong Xu wrote:

> * lisp/progmodes/cc-mode.el (c-or-c++-mode): Add check for the
> existence of alternative C++ source files.
> ---
>  lisp/progmodes/cc-mode.el | 15 +++++++++++----
>  1 file changed, 11 insertions(+), 4 deletions(-)

> diff --git a/lisp/progmodes/cc-mode.el b/lisp/progmodes/cc-mode.el
> index 6bdfb170f1ba..940bd4be3f92 100644
> --- a/lisp/progmodes/cc-mode.el
> +++ b/lisp/progmodes/cc-mode.el
> @@ -2492,16 +2492,23 @@ c-or-c++-mode
>  matching on file name insufficient for detecting major mode that
>  should be used.
 
> -This function attempts to use file contents to determine whether
> -the code is C or C++ and based on that chooses whether to enable
> -`c-mode' or `c++-mode'."
> -  (if (save-excursion
> +This function attempts to use file contents and the existence of
> +alternative C++ source files to determine whether the code is C
> +or C++ and based on that chooses whether to enable `c-mode' or
> +`c++-mode'."
> +  (if (or
> +       (save-excursion
>          (save-restriction
>            (save-match-data
>              (widen)
>              (goto-char (point-min))
>              (re-search-forward c-or-c++-mode--regexp
>                                 (+ (point) c-guess-region-max) t))))
> +       ;; We could have gone through auto-mode-alist here, but it
> +       ;; might be too slow.
> +       (let ((base-name (file-name-sans-extension (buffer-file-name))))
> +	 (seq-find (lambda (ext) (file-exists-p (concat base-name ext)))
> +		   '(".cc" ".cpp" ".cxx" ".c++"))))
>        (c++-mode)
>      (c-mode)))
 
> -- 
> 2.20.1

Thanks for writing this proposal.

I think it would be better, given a foo.h, to find all files foo.*, and
test each one in turn for a match in auto-mode-alist.  Searching this
alist is really not that slow, particularly as it is only being done a
few times, and not in a tight loop.

For this, we really want a function in the Emacs core which would find
the match in auto-mode-alist.  Currently, this functionality is open
coded in set-auto-mode (in .../lisp/files.el).

However, I'm a bit sceptical about the use of this proposed facility.
Many projects (possibly most) have .c/.c++ files in a directory called
"src", and .h files in a separate directory called "include".  This
proposed facility wouldn't work in such a set up.  To extend it to cope
with separate "src"/"include" directories would make it messy.

-- 
Alan Mackenzie (Nuremberg, Germany).




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#37862; Package emacs. (Sun, 27 Oct 2019 20:32:02 GMT) Full text and rfc822 format available.

Message #17 received at 37862 <at> debbugs.gnu.org (full text, mbox):

From: Hong Xu <hong <at> topbug.net>
To: Alan Mackenzie <acm <at> muc.de>
Cc: 37862 <at> debbugs.gnu.org
Subject: Re: bug#37862: [PATCH] Use alternative C++ source files for
 determining C/C++ header files.
Date: Sun, 27 Oct 2019 13:31:33 -0700
On 10/27/19 6:30 AM, Alan Mackenzie wrote:
> 
> Thanks for writing this proposal.
> 
> I think it would be better, given a foo.h, to find all files foo.*, and
> test each one in turn for a match in auto-mode-alist.  Searching this
> alist is really not that slow, particularly as it is only being done a
> few times, and not in a tight loop.

I agree with this approach, if it would not be too slow. Perhaps we can set a limit on the number of files that match foo.*.
> 
> For this, we really want a function in the Emacs core which would find
> the match in auto-mode-alist.  Currently, this functionality is open
> coded in set-auto-mode (in .../lisp/files.el).

I like the idea to create a separate function that provides this feature. Do you mean it's somewhat buried in `set-auto-mode'? It's probably also useful in other places.

> 
> However, I'm a bit sceptical about the use of this proposed facility.
> Many projects (possibly most) have .c/.c++ files in a directory called
> "src", and .h files in a separate directory called "include".  This
> proposed facility wouldn't work in such a set up.  To extend it to cope
> with separate "src"/"include" directories would make it messy.


This is true; but I think no guessing is perfect. At least the proposed solution should help with some smaller projects.





bug reassigned from package 'emacs' to 'emacs,cc-mode'. Request was from Stefan Kangas <stefan <at> marxist.se> to control <at> debbugs.gnu.org. (Fri, 08 Nov 2019 01:08:02 GMT) Full text and rfc822 format available.

Severity set to 'wishlist' from 'normal' Request was from Noam Postavsky <npostavs <at> gmail.com> to control <at> debbugs.gnu.org. (Thu, 27 Feb 2020 02:23:01 GMT) Full text and rfc822 format available.

Information forwarded to bug-gnu-emacs <at> gnu.org, bug-cc-mode <at> gnu.org:
bug#37862; Package emacs,cc-mode. (Sat, 04 Jul 2020 17:11:01 GMT) Full text and rfc822 format available.

Message #24 received at 37862 <at> debbugs.gnu.org (full text, mbox):

From: Alan Mackenzie <acm <at> muc.de>
To: Hong Xu <hong <at> topbug.net>
Cc: 37862 <at> debbugs.gnu.org
Subject: Re: bug#37862: [PATCH] Use alternative C++ source files for
 determining C/C++ header files.
Date: Sat, 4 Jul 2020 17:10:22 +0000
Hello, Hong.

On Sun, Oct 27, 2019 at 13:31:33 -0700, Hong Xu wrote:
> On 10/27/19 6:30 AM, Alan Mackenzie wrote:

> > Thanks for writing this proposal.

> > I think it would be better, given a foo.h, to find all files foo.*, and
> > test each one in turn for a match in auto-mode-alist.  Searching this
> > alist is really not that slow, particularly as it is only being done a
> > few times, and not in a tight loop.

> I agree with this approach, if it would not be too slow. Perhaps we can
> set a limit on the number of files that match foo.*.

> > For this, we really want a function in the Emacs core which would find
> > the match in auto-mode-alist.  Currently, this functionality is open
> > coded in set-auto-mode (in .../lisp/files.el).

> I like the idea to create a separate function that provides this
> feature. Do you mean it's somewhat buried in `set-auto-mode'? It's
> probably also useful in other places.

> > However, I'm a bit sceptical about the use of this proposed facility.
> > Many projects (possibly most) have .c/.c++ files in a directory called
> > "src", and .h files in a separate directory called "include".  This
> > proposed facility wouldn't work in such a set up.  To extend it to cope
> > with separate "src"/"include" directories would make it messy.

> This is true; but I think no guessing is perfect. At least the proposed
> solution should help with some smaller projects.

After thinking it over for some time, I'm sorry to say I've decided not
to commit your change to Emacs.  I think the disadvantages would
outweigh the advantages.

But again, thanks for taking the trouble to post the idea on the buglist.

-- 
Alan Mackenzie (Nuremberg, Germany).




Added tag(s) wontfix. Request was from Alan Mackenzie <acm <at> muc.de> to control <at> debbugs.gnu.org. (Sat, 04 Jul 2020 17:18:01 GMT) Full text and rfc822 format available.

bug closed, send any further explanations to 37862 <at> debbugs.gnu.org and Hong Xu <hong <at> topbug.net> Request was from Alan Mackenzie <acm <at> muc.de> to control <at> debbugs.gnu.org. (Sat, 04 Jul 2020 17:18:02 GMT) Full text and rfc822 format available.

bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Sun, 02 Aug 2020 11:24:06 GMT) Full text and rfc822 format available.

This bug report was last modified 3 years and 267 days ago.

Previous Next


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