From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Alan Mackenzie Newsgroups: gmane.emacs.bugs Subject: bug#37862: [PATCH] Use alternative C++ source files for determining C/C++ header files. Date: Sun, 27 Oct 2019 13:30:38 +0000 Message-ID: <20191027133038.GA27906@ACM> References: <87r235scb0.fsf@topbug.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="255876"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Mutt/1.10.1 (2018-07-13) Cc: 37862@debbugs.gnu.org To: Hong Xu Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Sun Oct 27 14:31:23 2019 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1iOide-0014OL-P3 for geb-bug-gnu-emacs@m.gmane.org; Sun, 27 Oct 2019 14:31:22 +0100 Original-Received: from localhost ([::1]:45318 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iOidd-0002LQ-09 for geb-bug-gnu-emacs@m.gmane.org; Sun, 27 Oct 2019 09:31:21 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:58579) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iOidL-000298-PW for bug-gnu-emacs@gnu.org; Sun, 27 Oct 2019 09:31:08 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iOidJ-0006LM-Sf for bug-gnu-emacs@gnu.org; Sun, 27 Oct 2019 09:31:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:32977) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iOidJ-0006LI-PA for bug-gnu-emacs@gnu.org; Sun, 27 Oct 2019 09:31:01 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1iOidJ-0007ml-LW for bug-gnu-emacs@gnu.org; Sun, 27 Oct 2019 09:31:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Alan Mackenzie Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 27 Oct 2019 13:31:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 37862 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 37862-submit@debbugs.gnu.org id=B37862.157218304529904 (code B ref 37862); Sun, 27 Oct 2019 13:31:01 +0000 Original-Received: (at 37862) by debbugs.gnu.org; 27 Oct 2019 13:30:45 +0000 Original-Received: from localhost ([127.0.0.1]:41798 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1iOid2-0007mF-RO for submit@debbugs.gnu.org; Sun, 27 Oct 2019 09:30:45 -0400 Original-Received: from colin.muc.de ([193.149.48.1]:63743 helo=mail.muc.de) by debbugs.gnu.org with smtp (Exim 4.84_2) (envelope-from ) id 1iOid0-0007m5-G9 for 37862@debbugs.gnu.org; Sun, 27 Oct 2019 09:30:43 -0400 Original-Received: (qmail 47103 invoked by uid 3782); 27 Oct 2019 13:30:40 -0000 Original-Received: from acm.muc.de (p2E5D5E17.dip0.t-ipconnect.de [46.93.94.23]) by colin.muc.de (tmda-ofmipd) with ESMTP; Sun, 27 Oct 2019 14:30:38 +0100 Original-Received: (qmail 28236 invoked by uid 1000); 27 Oct 2019 13:30:38 -0000 Content-Disposition: inline In-Reply-To: <87r235scb0.fsf@topbug.net> X-Delivery-Agent: TMDA/1.1.12 (Macallan) X-Primary-Address: acm@muc.de X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.51.188.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:170247 Archived-At: 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).