From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Matt Armstrong Newsgroups: gmane.emacs.devel Subject: Re: Indent lisp files on save -- How? Date: Tue, 22 Nov 2022 21:47:43 -0800 Message-ID: <87sfia9q80.fsf@rfc20.org> References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="6685"; mail-complaints-to="usenet@ciao.gmane.io" To: "T.V Raman" , emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Wed Nov 23 06:48:37 2022 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 1oxicf-0001WD-1h for ged-emacs-devel@m.gmane-mx.org; Wed, 23 Nov 2022 06:48:37 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oxic4-0000iu-BB; Wed, 23 Nov 2022 00:48:00 -0500 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 1oxic3-0000ih-5E for emacs-devel@gnu.org; Wed, 23 Nov 2022 00:47:59 -0500 Original-Received: from relay4-d.mail.gandi.net ([2001:4b98:dc4:8::224]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oxiby-0000JY-K6 for emacs-devel@gnu.org; Wed, 23 Nov 2022 00:47:56 -0500 Original-Received: (Authenticated sender: matt@rfc20.org) by mail.gandi.net (Postfix) with ESMTPSA id BB17BE0003; Wed, 23 Nov 2022 05:47:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rfc20.org; s=gm1; t=1669182469; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=VOTkviAjHrTbR52E1WSTQ/u5QoS+USxZusGdgbaSvNk=; b=MM7WjptDoohS2TZCzj7xhMJQ9HsFtkc7IBCiju7AKlJcIPBC7Kxw1SB7ecskSwYiTLiPrH jbdU0t9RGgs2XMnu/gACIvy9BseKnsaMW6SLjPl2FkNpgOQQpLiEWyst39kGnsGGdRR/Vd Y2nqFXTg3VJY3HZAyN+JGzpKynIHaDISpfMhWQfHPubuSjXVycBn3eN8Gv3vrKbUGmIKxf pk7NRwVt4S8DAjfEah/ltU7jpsuG+N9Y7DrMzb85+zaoag57OvSPr+i1qOj3rqtNgsfQ7R SEbV6FCHB/rwc4V3PBi9C5uDLVLolf+MKWxQ8GUeiKqk8PaX2z9UQPoSJU2V0g== Original-Received: by mac-mini.lan (Postfix) with ESMTPS id 74F153A9FD; Tue, 22 Nov 2022 21:47:43 -0800 (PST) Original-Received: by naz.lan (Postfix, from userid 1000) id 5877043C1EA2; Tue, 22 Nov 2022 21:47:43 -0800 (PST) In-Reply-To: Received-SPF: pass client-ip=2001:4b98:dc4:8::224; envelope-from=matt@rfc20.org; helo=relay4-d.mail.gandi.net X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 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_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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:300378 Archived-At: "T.V Raman" writes: > I'm somewhat surprized I've never setup the above for emacs-lisp or > Common-Lisp in all these years. For Emacs Lisp, I don't think there is a good "format on save" implementation. > But having gotten used to having format on save for Go, Rust and > Python among others I want the same for all lisp dialects -- how do I > achieve this? It's unclear which of the options available is designed > for placing on before-save-hook. I like this kind of thing too, but in the cases of Go, Rust, Python, Ruby, Javascript, etc. there are existing external tools, and projects often enforce strict formatting rules. For these languages Emacs is just one more editor running the tool. Emacs has something subtly different for Emacs Lisp. One issue for an "auto-indenter" for Emacs Lisp is that indentation behavior is dependent on what is currently loaded. In particular, indentation of macros is often wrong until the macro is loaded, since the indenter doesn't know it is a macro with a custom indentation declaration. This is an issue because one can't simply load an .el file, make an edit, and expect the indenter to do the right thing. One must at least make sure to eval whatever that file requires. I don't know how to solve this problem in the general case. Perhaps an Emacs Lisp auto-indenter should run as a subprocess, much like emacs-lisp.el tells flymake to use an emacs subprocess to byte compile the current .el file to surface bytecomp errors. Another problem is that the programming culture for Emacs Lisp doesn't seem to be about a strict adherence to any one indentation tool or algorithm, and the Emacs indenter isn't always perfect. Load up any random .el file shipped with Emacs, then M-x mark-whole-buffer, then M-x indent-region, and you will usually get diffs. Sometimes the diffs should be applied (when the code has misleading or obviously wrong indentation). Sometimes the diffs are the programmer's choice and should not (necessarily) be undone. Then there is the question: does the whole file get indented, or is the indentation limited to just the portions of the buffer that have been edited? In the latter case, what decides when enough of the surrounding sexp has been reindented? These are surprisingly subtle questions, often requiring non-trivial heuristics. I use the clang-format+ package to format C and C++ code on save. It has logic to run the formatter only on portions of the buffer I have edited (it uses lists of ranges and text properties to track the "dirty" areas of the buffer). It might be useful to extract this machinery out into a package that provides a way to track this for formatting purposes, and then experiment with using that in an emacs-lisp-indent-on-save package.