From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Philip Kaludercic Newsgroups: gmane.emacs.devel Subject: Re: Incorporate package macrostep into Emacs core Date: Tue, 19 Mar 2024 06:36:35 +0000 Message-ID: <87o7basp70.fsf@posteo.net> References: <87zfvl8r4e.fsf@jeremybryant.net> <874jdspsqb.fsf@bernoul.li> <877cio8fzf.fsf@jeremybryant.net> <87y1b46vhg.fsf@jeremybryant.net> <878r336lvb.fsf@jeremybryant.net> <86y1b1p1ni.fsf@gnu.org> <87y1b0mi4b.fsf@jeremybryant.net> <8734sobkdj.fsf@jeremybryant.net> <87il1jdhzd.fsf@posteo.net> <87le6f9m8b.fsf@jeremybryant.net> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="23443"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Jeremy Bryant via "Emacs development discussions." , Eli Zaretskii , monnier@iro.umontreal.ca, j.j.oddie@gmail.com, stefankangas@gmail.com, jonas@bernoul.li To: Jeremy Bryant Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Tue Mar 19 07:37:30 2024 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 1rmT6I-0005rJ-Gq for ged-emacs-devel@m.gmane-mx.org; Tue, 19 Mar 2024 07:37:30 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rmT5a-0004Qh-EH; Tue, 19 Mar 2024 02:36:46 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rmT5Y-0004QZ-JN for emacs-devel@gnu.org; Tue, 19 Mar 2024 02:36:44 -0400 Original-Received: from mout02.posteo.de ([185.67.36.66]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rmT5W-00076t-2H for emacs-devel@gnu.org; Tue, 19 Mar 2024 02:36:44 -0400 Original-Received: from submission (posteo.de [185.67.36.169]) by mout02.posteo.de (Postfix) with ESMTPS id 23A6B240104 for ; Tue, 19 Mar 2024 07:36:37 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1710830198; bh=1RuS3NW3xZ9xI0+T4Uh+VbMVfJTg3cWzJMX4r4+lAn8=; h=From:To:Cc:Subject:OpenPGP:Date:Message-ID:MIME-Version: Content-Type:From; b=GxmZZJeD1AyOneDI8MjqJ1f2KmzYnPvtSWDqwsEhQMhugptznj5XPdzhugrU8lwPA QtWATu5kEfN4lVymLaFpFMHnItoGWXWJ+bSE5GsOCl0QdtTWRHd4ggOwDil07Bclzv ltUI/huhpHmtG7oa629Q8l0zCxnprjA5WCdcM+mxZ95MqADmbOXORAJMWidfKtaMcD nxzwOdAy4akfIYMUydYYsuob5+Onft1FmNMbmqHXtUjoAW7XTiiHSc/xa1F4rBgH+8 RnpYIzdLpW/MhX5CrFNMBxrubouSyuC2+yLAQ2bMJDu31laptt5H+FErqkUTwmWnEB mC5jwSYjAgyJw== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4TzMSh2xqXz6twC; Tue, 19 Mar 2024 07:36:36 +0100 (CET) In-Reply-To: <87le6f9m8b.fsf@jeremybryant.net> (Jeremy Bryant's message of "Mon, 18 Mar 2024 23:03:16 +0000") OpenPGP: id=7126E1DE2F0CE35C770BED01F2C3CC513DB89F66; url="https://keys.openpgp.org/vks/v1/by-fingerprint/7126E1DE2F0CE35C770BED01F2C3CC513DB89F66"; preference=signencrypt Received-SPF: pass client-ip=185.67.36.66; envelope-from=philipk@posteo.net; helo=mout02.posteo.de X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 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-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:317178 Archived-At: Jeremy Bryant writes: > Philip Kaludercic writes: > > Philip, > Thanks for all the comments on the macrostep.el code, I'll work on what > I can. > >> Isn't there also a C-preprecossor implementation for macrostep? Would >> the plan be to include that as well? > > Presumably, also. Any comments on macrostep-c.el (attached)? > > ;;; macrostep-c.el --- macrostep interface to C preprocessor -*- lexical-binding: t; -*- > > ;; Copyright (C) 2015 Jon Oddie If included, then this should be updated. > ;; Author: Jon Oddie > ;; Url: https://github.com/emacsorphanage/macrostep And this removed. > ;; Keywords: c, languages, macro, debugging > > ;; SPDX-License-Identifier: GPL-3.0-or-later > > ;; This file is free software: you can redistribute it and/or modify > ;; it under the terms of the GNU General Public License as published > ;; by the Free Software Foundation, either version 3 of the License, > ;; or (at your option) any later version. > ;; > ;; This file is distributed in the hope that it will be useful, > ;; but WITHOUT ANY WARRANTY; without even the implied warranty of > ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > ;; GNU General Public License for more details. > ;; > ;; You should have received a copy of the GNU General Public License > ;; along with this file. If not, see . > > ;;; Commentary: > > ;; A thin wrapper around Emacs's built-in `cmacexp' library to provide > ;; basic support for expanding C macros using the `macrostep' user > ;; interface. To use, position point on a macro use in a C buffer and > ;; type `M-x macrostep-expand'. The variables `c-macro-preprocessor' > ;; and especially `c-macro-cppflags' may need to be set correctly for > ;; accurate expansion. > > ;; This is fairly basic compared to the Emacs Lisp `macrostep'. In > ;; particular, there is no step-by-step expansion, since C macros are > ;; expanded in a single "cpp" pass, and no pretty-printing. > > ;; To hide the buffer containing "cpp" warnings (not recommended), you > ;; could do something like: > ;; > ;; (push `(,(regexp-quote macrostep-c-warning-buffer) > ;; (display-buffer-no-window)) > ;; display-buffer-alist) > > ;;; Code: > > (require 'macrostep) > (require 'cmacexp) > (require 'cl-lib) > > (require 'subr-x nil t) > (defalias 'macrostep-c-string-trim > (if (fboundp 'string-trim) > #'string-trim > (lambda (string) > (when (string-match "\\`[ \t\n\r]+" string) > (setq string (replace-match "" t t string))) > (when (string-match "[ \t\n\r]+\\'" string) > (setq string (replace-match "" t t string))) > string))) We can drop this (or use Compat to provide the function if the package is distributeted on ELPA). > (put 'macrostep-c-non-macro 'error-conditions > '(macrostep-c-non-macro error)) > (put 'macrostep-c-non-macro 'error-message > "Text around point is not a macro call.") > > (put 'macrostep-c-expansion-failed 'error-conditions > '(macrostep-c-expansion-failed error)) > (put 'macrostep-c-expansion-failed 'error-message > "Macro-expansion failed.") > > (defvar macrostep-c-warning-buffer "*Macroexpansion Warnings*") > > ;;;###autoload > (defun macrostep-c-mode-hook () > (setq macrostep-sexp-bounds-function > #'macrostep-c-sexp-bounds) > (setq macrostep-sexp-at-point-function > #'macrostep-c-sexp-at-point) > (setq macrostep-environment-at-point-function > #'ignore) > (setq macrostep-expand-1-function > #'macrostep-c-expand-1) > (setq macrostep-print-function > #'macrostep-c-print-function) > (add-hook 'macrostep-mode-off-hook > #'macrostep-c-mode-off nil t)) > > (defun macrostep-c-mode-off (&rest _ignore) > (when (derived-mode-p 'c-mode) > (let ((warning-window > (get-buffer-window macrostep-c-warning-buffer))) > (when warning-window > (quit-window nil warning-window))))) > > ;;;###autoload > (add-hook 'c-mode-hook #'macrostep-c-mode-hook) This part is suspicious. First of all, it looks like one is adding a hook to a hook, but this would unconditionally modify c-mode-hook, which I don't think is reliable. Can we find some other way to update the variables, depending on the major mode? E.g. in macrostep.el one could try to intern (format "macrostep-%s-init" major-mode) and check if the symbol is fboundp? > (defun macrostep-c-sexp-bounds () > (save-excursion > (cl-loop > (let ((region (macrostep-c-sexp-bounds-1))) > (cond > ((null region) > (signal 'macrostep-c-non-macro nil)) > ((macrostep-c-expandable-p region) > (cl-return region)) > (t > (condition-case nil > (progn > (backward-up-list) > (skip-syntax-backward "-")) > (scan-error > (signal 'macrostep-c-non-macro nil))))))))) > > (defun macrostep-c-sexp-bounds-1 () > (let ((region (bounds-of-thing-at-point 'symbol))) > (when region One could use when-let* in places like this. > (cl-destructuring-bind (symbol-start . symbol-end) region > (save-excursion > (goto-char symbol-end) > (if (looking-at "[[:space:]]*(") > (cons symbol-start (scan-sexps symbol-end 1)) > region)))))) The indentation is off here, right? Might be worth reindenting everything once. > > (defun macrostep-c-expandable-p (region) > (cl-destructuring-bind (start . end) region > (condition-case nil > (cl-destructuring-bind (expansion _warnings) > (macrostep-c-expand-region start end) > (and (cl-plusp (length expansion)) > (not (string= expansion (buffer-substring start end))))) > (macrostep-c-expansion-failed nil)))) > > (defun macrostep-c-sexp-at-point (start end) > (cons start end)) > > (defun macrostep-c-expand-1 (region _ignore) > (cl-destructuring-bind (start . end) region > (cl-destructuring-bind (expansion warnings) > (macrostep-c-expand-region start end) > (when (cl-plusp (length warnings)) > (with-current-buffer > (get-buffer-create macrostep-c-warning-buffer) > (let ((inhibit-read-only t)) > (erase-buffer) > (insert warnings) > (goto-char (point-min))) > (special-mode) > (display-buffer (current-buffer) > '(display-buffer-pop-up-window > (inhibit-same-window . t) > (allow-no-window . t))))) > expansion))) > > (defun macrostep-c-expand-region (start end) > (let ((expansion > (condition-case nil > (c-macro-expansion start end > (concat c-macro-preprocessor " " > c-macro-cppflags)) > (search-failed > (signal 'macrostep-c-expansion-failed nil))))) > (with-temp-buffer > (save-excursion > (insert expansion)) > (when (looking-at (regexp-quote "/*")) > (search-forward "*/")) > (let ((warnings (buffer-substring (point-min) (point))) > (expansion (buffer-substring (point) (point-max)))) > (mapcar #'macrostep-c-string-trim (list expansion warnings)))))) > > (defun macrostep-c-print-function (expansion &rest _ignore) > (with-temp-buffer > (insert expansion) > (let ((exit-code > (shell-command-on-region (point-min) (point-max) "indent" nil t))) When calling indent, it might be nice to provide an option for optional flags, in case someone prefers some other indentation scheme. > (when (zerop exit-code) > (setq expansion (macrostep-c-string-trim (buffer-string)))))) > (insert expansion)) > > (provide 'macrostep-c) > > ;;; macrostep-c.el ends here > -- Philip Kaludercic on peregrine