From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Jeremy Bryant Newsgroups: gmane.emacs.devel Subject: Re: Incorporate package macrostep into Emacs core Date: Mon, 18 Mar 2024 23:03:16 +0000 Message-ID: <87le6f9m8b.fsf@jeremybryant.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> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="28712"; 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: Philip Kaludercic Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Tue Mar 19 00:04:08 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 1rmM1W-0007AA-La for ged-emacs-devel@m.gmane-mx.org; Tue, 19 Mar 2024 00:04:07 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rmM0r-0005N3-FU; Mon, 18 Mar 2024 19:03:25 -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 1rmM0o-0005Mp-Ox for emacs-devel@gnu.org; Mon, 18 Mar 2024 19:03:22 -0400 Original-Received: from out-188.mta1.migadu.com ([95.215.58.188]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rmM0n-0001bK-7W for emacs-devel@gnu.org; Mon, 18 Mar 2024 19:03:22 -0400 X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jeremybryant.net; s=key1; t=1710802999; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Xs4URfUyeCAatqKZ3DyY+1sOTesDCrAs+7y53tkc3/Y=; b=r54i3GtMa1VrIFSTqvuzlJZ4D9Jhx5vxPWmnNyLF1bripTuHgSB3hiEcS2s/7YBwRTQFkW CWWpTxVYr/AH1NESzCN+4cKW7oXAKFvKHr9pN8Taeb2dngN0EqzSAhxvj6z9P8qwmzKQuY 7wsRo8eOm+vgoHeihM4XYgA2XSnvu0YGVh2Fh6fFdjL1FWuUoOQ6VdabLaq4TN50KMUj+n B2gKpADVi9eY/1PyTnXQlNxYv16JDFivQQk8Wr+zHfR7lcfvqPs/0ICZoOryjNLbayTi7p MvTJorg+dzBrJAlN0RBeI6rfu4JitX544TmbxSGU3EZRPNyCMTH7RHrLmy3Zbw== In-Reply-To: <87il1jdhzd.fsf@posteo.net> (Philip Kaludercic's message of "Mon, 18 Mar 2024 09:09:10 +0000") X-Migadu-Flow: FLOW_OUT Received-SPF: pass client-ip=95.215.58.188; envelope-from=jb@jeremybryant.net; helo=out-188.mta1.migadu.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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, 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:317172 Archived-At: --=-=-= Content-Type: text/plain 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)? --=-=-= Content-Type: application/emacs-lisp Content-Disposition: attachment; filename=macrostep-c.el Content-Transfer-Encoding: quoted-printable ;;; macrostep-c.el --- macrostep interface to C preprocessor -*- lexical-b= inding: t; -*- ;; Copyright (C) 2015 Jon Oddie ;; Author: Jon Oddie ;; Url: https://github.com/emacsorphanage/macrostep ;; 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))) (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) (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 (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)))))) (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=3D 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 (zerop exit-code) (setq expansion (macrostep-c-string-trim (buffer-string)))))) (insert expansion)) (provide 'macrostep-c) ;;; macrostep-c.el ends here --=-=-=--