unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Matt Armstrong <matt@rfc20.org>
To: "T.V Raman" <raman@google.com>, emacs-devel@gnu.org
Subject: Re: Indent lisp files on save -- How?
Date: Tue, 22 Nov 2022 21:47:43 -0800	[thread overview]
Message-ID: <87sfia9q80.fsf@rfc20.org> (raw)
In-Reply-To: <p91r0xxrnix.fsf@google.com>

"T.V Raman" <raman@google.com> 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.



  reply	other threads:[~2022-11-23  5:47 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-21  3:31 Indent lisp files on save -- How? T.V Raman
2022-11-23  5:47 ` Matt Armstrong [this message]
2022-11-23  7:41   ` Stefan Kangas
2022-11-23 12:47   ` Eli Zaretskii
2022-11-23 17:01     ` T.V Raman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87sfia9q80.fsf@rfc20.org \
    --to=matt@rfc20.org \
    --cc=emacs-devel@gnu.org \
    --cc=raman@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).