* bug#37862: [PATCH] Use alternative C++ source files for determining C/C++ header files.
@ 2019-10-22 3:12 Hong Xu
2019-10-26 11:06 ` Eli Zaretskii
2019-10-27 13:30 ` Alan Mackenzie
0 siblings, 2 replies; 6+ messages in thread
From: Hong Xu @ 2019-10-22 3:12 UTC (permalink / raw)
To: 37862
* 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
^ permalink raw reply related [flat|nested] 6+ messages in thread
* bug#37862: [PATCH] Use alternative C++ source files for determining C/C++ header files.
2019-10-22 3:12 bug#37862: [PATCH] Use alternative C++ source files for determining C/C++ header files Hong Xu
@ 2019-10-26 11:06 ` Eli Zaretskii
2019-10-26 15:19 ` Alan Mackenzie
2019-10-27 13:30 ` Alan Mackenzie
1 sibling, 1 reply; 6+ messages in thread
From: Eli Zaretskii @ 2019-10-26 11:06 UTC (permalink / raw)
To: Hong Xu, Alan Mackenzie; +Cc: 37862
> From: Hong Xu <hong@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?
^ permalink raw reply [flat|nested] 6+ messages in thread
* bug#37862: [PATCH] Use alternative C++ source files for determining C/C++ header files.
2019-10-26 11:06 ` Eli Zaretskii
@ 2019-10-26 15:19 ` Alan Mackenzie
0 siblings, 0 replies; 6+ messages in thread
From: Alan Mackenzie @ 2019-10-26 15:19 UTC (permalink / raw)
To: Eli Zaretskii; +Cc: 37862, Hong Xu
Hello, Eli.
On Sat, Oct 26, 2019 at 14:06:39 +0300, Eli Zaretskii wrote:
> > From: Hong Xu <hong@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).
^ permalink raw reply [flat|nested] 6+ messages in thread
* bug#37862: [PATCH] Use alternative C++ source files for determining C/C++ header files.
2019-10-22 3:12 bug#37862: [PATCH] Use alternative C++ source files for determining C/C++ header files Hong Xu
2019-10-26 11:06 ` Eli Zaretskii
@ 2019-10-27 13:30 ` Alan Mackenzie
2019-10-27 20:31 ` Hong Xu
1 sibling, 1 reply; 6+ messages in thread
From: Alan Mackenzie @ 2019-10-27 13:30 UTC (permalink / raw)
To: Hong Xu; +Cc: 37862
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).
^ permalink raw reply [flat|nested] 6+ messages in thread
* bug#37862: [PATCH] Use alternative C++ source files for determining C/C++ header files.
2019-10-27 13:30 ` Alan Mackenzie
@ 2019-10-27 20:31 ` Hong Xu
2020-07-04 17:10 ` Alan Mackenzie
0 siblings, 1 reply; 6+ messages in thread
From: Hong Xu @ 2019-10-27 20:31 UTC (permalink / raw)
To: Alan Mackenzie; +Cc: 37862
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.
^ permalink raw reply [flat|nested] 6+ messages in thread
* bug#37862: [PATCH] Use alternative C++ source files for determining C/C++ header files.
2019-10-27 20:31 ` Hong Xu
@ 2020-07-04 17:10 ` Alan Mackenzie
0 siblings, 0 replies; 6+ messages in thread
From: Alan Mackenzie @ 2020-07-04 17:10 UTC (permalink / raw)
To: Hong Xu; +Cc: 37862
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).
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2020-07-04 17:10 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-10-22 3:12 bug#37862: [PATCH] Use alternative C++ source files for determining C/C++ header files Hong Xu
2019-10-26 11:06 ` Eli Zaretskii
2019-10-26 15:19 ` Alan Mackenzie
2019-10-27 13:30 ` Alan Mackenzie
2019-10-27 20:31 ` Hong Xu
2020-07-04 17:10 ` Alan Mackenzie
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).