From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.ciao.gmane.io!not-for-mail From: Ergus via "Emacs development discussions." Newsgroups: gmane.emacs.devel Subject: Re: c-mode pragma and preproc Date: Mon, 20 Jan 2020 03:15:38 +0100 Message-ID: <20200120021538.wxtxv3vbsorksqdg@Ergus> References: <20191219140738.q5zdmily5ubwnmg3.ref@Ergus> <20191219140738.q5zdmily5ubwnmg3@Ergus> <20200111114402.GA6005@ACM> <20200114140110.hrfqmrfkffwsknse@Ergus> <20200119172647.GB9172@ACM> Reply-To: Ergus Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Injection-Info: ciao.gmane.io; posting-host="ciao.gmane.io:159.69.161.202"; logging-data="37831"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel@gnu.org To: Alan Mackenzie Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Mon Jan 20 03:16:59 2020 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1itMcb-0009ll-OA for ged-emacs-devel@m.gmane-mx.org; Mon, 20 Jan 2020 03:16:57 +0100 Original-Received: from localhost ([::1]:57316 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1itMca-0007tB-MN for ged-emacs-devel@m.gmane-mx.org; Sun, 19 Jan 2020 21:16:56 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:45462) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1itMbS-00072O-FE for emacs-devel@gnu.org; Sun, 19 Jan 2020 21:15:48 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1itMbQ-0007yh-Rk for emacs-devel@gnu.org; Sun, 19 Jan 2020 21:15:46 -0500 Original-Received: from sonic313-15.consmr.mail.bf2.yahoo.com ([74.6.133.125]:42504) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1itMbQ-0007vX-Nd for emacs-devel@gnu.org; Sun, 19 Jan 2020 21:15:44 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aol.com; s=a2048; t=1579486541; bh=lb1CYuxYwV/zP1anzpVPEDFJ9kJ863wsiJT86lgsrd4=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From:Subject; b=cEEuv7MrWXyWwnpXWv2LWgV05dhjYjFJCJfPZ2NI/yKuUzlhPAvGaz9fSuVrclH3oha1BrtUGwBYSTLp0SgwdGAkHXu4AsathBxo8jW08gyrOh13QjeW1MiCLDM2hBVBODPh5Ghy+N9M8rTQnZXNFu5IgtRg3tnEalFWG5PC/KpWdG76Ri03NqHt2h5OjFuArHb0N5U2W8+nxA9vFcG5F+OcBdPvLc+RZrwBwZ/d0kk6AiIL5E/MeWpCyX3t66g8tABMFWEevf8Zhk+mXWGc/x8mHywPhLVI3obi9gzvtY/3f1o/itE+YKNyLac7p+ABLkTaBxsHpOlwK5EaAzcCEg== X-YMail-OSG: tiAz0T4VM1nZWjO8sX4Lis4Us4c3Rt5rsCM2MNobqVRPjT52.AV1sBHqehnvQi9 R_7YEb77yd1IlqNxCk3cvHuv_kDmXIk3YiHHQSyuA4wHfPBBkHaF.iIsZMmz57GXACIRV9fFATwm Ep4MjBG.rw_MG059PoR.9ZEiEQblOOQKNXOboREAYsVVWq8B_7mKzt7Ye3kE2sCeZmawx8nlXwkH BPBOU8YfpR09j3vTG6SFrXq.egwNfI4_2U4_JKmil8N2b5bDK2WVf.v2LodxlkgWkUPu3dWF2BkR WkituL4MsVePnJAY2vTlXouparqMb2EQTl9KdRxCE6B52AugYCzooj5GpJDTSoorU1JNd34G5w6K KOsicgdZYID25R5Deop6KbMBSSB24NbfalVJmYqGVOhwLq6pWciUQruXpOFNBaKpuBGZjMxWtmRP flEC1.DZ5eeQC4NNjDSYP2f21HfCJtI7gdQKe.pIgq6Cywp3xqdi3Kei_6kB_z5Axt.kSaIhxkKv HwtnOVX7vXtNP0AGfWitrVml.P_WACuSqgrZJ3bc3eJknPHUWc2R53pOC8rxsY9InB1OUkjURlDH udD5eVbas9hpkMjAgcriwDXhOHqFb5Zv1.FbLXNBRR_pGc5t5yL4vZoqtYEZDBvmbZtclhBkdPe9 Xg3i.JhyeRpT4VvpRuCOGMZGpK2nMdhwm1RSKZVfz7NyB1coBmszvtbjP7HGFi5n1osNAZ22Rj43 ya1ImigwppDDZaKhypA_ox_Pa7FqnE7LecBQH_w6CcTnNHNfwmE5klPE52sP465VU0.oNHWezQgs TUbninrXJhRlI.OwDcc1qqPGvxKRYoJCQ9Sr6pM72T Original-Received: from sonic.gate.mail.ne1.yahoo.com by sonic313.consmr.mail.bf2.yahoo.com with HTTP; Mon, 20 Jan 2020 02:15:41 +0000 Original-Received: by smtp428.mail.ir2.yahoo.com (Oath Hermes SMTP Server) with ESMTPA ID 594f8a6cca059f1f43746953ddbef63d; Mon, 20 Jan 2020 02:15:39 +0000 (UTC) Content-Disposition: inline In-Reply-To: <20200119172647.GB9172@ACM> X-Mailer: WebService/1.1.14873 hermes Apache-HttpAsyncClient/4.1.4 (Java/1.8.0_181) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 74.6.133.125 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 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-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:244402 Archived-At: Hi Alan: I just tried the patch and it seems to work as expected. I just have 3 observations/suggestions/questions: 1) When I stay in column 0 like |#pragma bla bla and I press tab (to indent the line's) I get: | #pragma bla bla instead of: |#pragma bla bla as it happens with other lines (where | is the cursor position). Is this intended? 2) This new implementation breaks the "normal" way to configure the indentation in c-mode because C-c C-o still returns cpp-macro (as expected) while the indentation is not behaving as specified for cpp-macros. So it seems to be confusing for the user and somehow a bit inconsistent with the rest of cc-mode indentation. Why not just add an extra syntactic symbol for pragmas? Isn't it more consistent that way? Otherwise the user will get false information from C-c C-o and looses somehow a bit of flexibility. Maybe we just need a variable that when not specified indent pragmas as macros else indent pragmas as it specifies...? 3) Why the new function is called c-toggle-cpp-indent*? pragmas are part of C too. Actually in HPC we usually use more C (and Fortran) than C++. In any case the only real "issue" in my opinion is 1). But so far it works pretty fine. Very thanks, Ergus. On Sun, Jan 19, 2020 at 05:26:47PM +0000, Alan Mackenzie wrote: >Hello again, Ergus. > >On Tue, Jan 14, 2020 at 15:01:10 +0100, Ergus wrote: >> On Sat, Jan 11, 2020 at 11:44:02AM +0000, Alan Mackenzie wrote: >> Hi Alan: >> >On Thu, Dec 19, 2019 at 15:07:38 +0100, Ergus wrote: >> >> Hi Alan: > >> >> Recently I have been noticing that many "modern" programming models >> >> (Open-MP, OmpSs, OpenACC, programming for Intel Xeon Phi) use >> >> extensively the #pragma sentence. > >> >> But in general, while the pre-processor sentences are usually in column >> >> zero ([0]), the #pragma, on the other hand, are preferred to be aligned >> >> with text (0). They are more readable that way. > >OK, I have an experimental implementation of M-x >c-toggle-cpp-indent-to-body (see patch below). This can be customised by >changing c-cpp-indent-to-body-directives, the list of directives which >should be indented as statements. It's default value is ("pragma"). >(Yes, I know you said that it would only be needed for "pragma", but it >seemed a bit restrictive so to restrict it.). > >The indentation takes place either on typing (or any other action >which causes indentation) or on typing a space after "# pragma". > >[ .... ] > >> For example the most general example so far is: > >> #pragma omp parallel shared(salaries1, salaries2) >> { >> #pragma omp for reduction(+: salaries1) >> for (int employee = 0; employee < 25000; employee++) >> { >> salaries1 += fetchTheSalary(employee, Co::Company1); >> } > >> #pragma omp single >> { >> std::cout << "Salaries1: " << salaries1 << std::endl; >> } > >> #pragma omp for reduction(+: salaries1) >> for (int employee = 0; employee < 25000; employee++) >> { >> salaries2 += fetchTheSalary(employee, Co::Company2); >> } > >> #pragma omp barrier >> } > >Thanks. This example was helpful. > >Just one thing - I haven't yet amended the CC Mode manual. > >Would you please try the patch out, and let me know how well it satisfies >your needs. The patch should apply cleanly to the master branch. > >Thanks! > >[ .... ] > > > > >diff --git a/lisp/progmodes/cc-cmds.el b/lisp/progmodes/cc-cmds.el >index 1071191775..223266177a 100644 >--- a/lisp/progmodes/cc-cmds.el >+++ b/lisp/progmodes/cc-cmds.el >@@ -48,6 +48,7 @@ > (cc-bytecomp-defvar filladapt-mode) ; c-fill-paragraph contains a kludge > ; which looks at this. > (cc-bytecomp-defun electric-pair-post-self-insert-function) >+(cc-bytecomp-defvar c-indent-to-body-directives) > > ;; Indentation / Display syntax functions > (defvar c-fix-backslashes t) >@@ -1441,6 +1442,78 @@ c-electric-continued-statement > (indent-according-to-mode) > (delete-char -2))))) > >+(defun c-align-cpp-indent-to-body () >+ "Align a \"#pragma\" line under the previous line. >+This function is intented for use as a member of `c-special-indent-hook'." >+ (when (assq 'cpp-macro c-syntactic-context) >+ (save-excursion >+ (save-match-data >+ (back-to-indentation) >+ (when (looking-at "#[ \t]*pragma\\([^[:alnum:]_$]\\|$\\)") >+ (c-indent-line (delete '(cpp-macro) c-syntactic-context))))))) >+ >+(defvar c-cpp-indent-to-body-flag nil) >+;; Non-nil when CPP directives such as "#pragma" should be indented to under >+;; the preceding statement. >+(make-variable-buffer-local 'c-cpp-indent-to-body-flag) >+ >+(defun c-electric-pragma () >+ "Reindent the current line if appropriate. >+ >+This function is used to reindent a preprocessor line when the >+symbol for the directive, typically \"pragma\", triggers this >+function as a hook function of an abbreviation. >+ >+The \"#\" of the preprocessor construct is aligned under the >+first anchor point of the line's syntactic context. >+ >+The line is reindented if the construct is not in a string or >+comment, there is exactly one \"#\" contained in optional >+whitespace before it on the current line, and `c-electric-flag' >+and `c-syntactic-indentation' are both non-nil." >+ (save-excursion >+ (save-match-data >+ (when >+ (and >+ c-cpp-indent-to-body-flag >+ c-electric-flag >+ c-syntactic-indentation >+ last-abbrev-location >+ c-opt-cpp-symbol ; "#" or nil. >+ (progn (back-to-indentation) >+ (looking-at (concat c-opt-cpp-symbol "[ \t]*"))) >+ (>= (match-end 0) last-abbrev-location) >+ (not (c-literal-limits))) >+ (c-indent-line (delete '(cpp-macro) (c-guess-basic-syntax))))))) >+ >+(defun c-add-indent-to-body-to-abbrev-table (d) >+ ;; Create an abbreviation table entry for the directive D, and add it to the >+ ;; current abbreviation table. Existing abbreviation (e.g. for "else") do >+ ;; not get overwritten. >+ (when (and c-buffer-is-cc-mode >+ local-abbrev-table >+ (not (abbrev-symbol d local-abbrev-table))) >+ (define-abbrev local-abbrev-table d d 'c-electric-pragma))) >+ >+(defun c-toggle-cpp-indent-to-body (&optional arg) >+ "Toggle the cpp indent-to-body feature. >+When enabled, when CPP directives which are words in >+`c-indent-to-body-directives', the are indented such that the >+initial \"#\" appears below the previous statement. >+ >+Optional numeric ARG, if supplied, turns on the feature when positive, >+turns it off when negative, and just toggles it when zero or >+left out." >+ (interactive "P") >+ (setq c-cpp-indent-to-body-flag >+ (c-calculate-state arg c-cpp-indent-to-body-flag)) >+ (if c-cpp-indent-to-body-flag >+ (progn >+ (mapc 'c-add-indent-to-body-to-abbrev-table >+ c-cpp-indent-to-body-directives) >+ (add-hook 'c-special-indent-hook 'c-align-cpp-indent-to-body nil t)) >+ (remove-hook 'c-special-indent-hook 'c-align-cpp-indent-to-body t))) >+ > > > (declare-function subword-forward "subword" (&optional arg)) >diff --git a/lisp/progmodes/cc-vars.el b/lisp/progmodes/cc-vars.el >index 861872486c..0391cdccfd 100644 >--- a/lisp/progmodes/cc-vars.el >+++ b/lisp/progmodes/cc-vars.el >@@ -1649,6 +1649,15 @@ c-asymmetry-fontification-flag > :type 'boolean > :group 'c) > >+(defcustom c-cpp-indent-to-body-directives '("pragma") >+ "Preprocessor directives which will be indented as statements. >+ >+A list of Preprocessor directives which when reindented, or newly >+typed in, will cause the \"#\" introducing the directive to be >+indented as a statement." >+ :type '(repeat string) >+ :group 'c) >+ > ;; Initialize the next two to a regexp which never matches. > (defvar c-noise-macro-with-parens-name-re regexp-unmatchable) > (make-variable-buffer-local 'c-noise-macro-with-parens-name-re) > > >> Best, >> Ergus > >-- >Alan Mackenzie (Nuremberg, Germany). >