all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Indent lisp files on save -- How?
@ 2022-11-21  3:31 T.V Raman
  2022-11-23  5:47 ` Matt Armstrong
  0 siblings, 1 reply; 5+ messages in thread
From: T.V Raman @ 2022-11-21  3:31 UTC (permalink / raw)
  To: emacs-devel

I'm somewhat surprized I've never setup the above for emacs-lisp or
Common-Lisp in all these years.

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.

-- 

Thanks,

--Raman(I Search, I Find, I Misplace, I Research)
♉ Id: kg:/m/0285kf1  🦮

-- 

Thanks,

--Raman(I Search, I Find, I Misplace, I Research)
♉ Id: kg:/m/0285kf1  🦮



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Indent lisp files on save -- How?
  2022-11-21  3:31 Indent lisp files on save -- How? T.V Raman
@ 2022-11-23  5:47 ` Matt Armstrong
  2022-11-23  7:41   ` Stefan Kangas
  2022-11-23 12:47   ` Eli Zaretskii
  0 siblings, 2 replies; 5+ messages in thread
From: Matt Armstrong @ 2022-11-23  5:47 UTC (permalink / raw)
  To: T.V Raman, emacs-devel

"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.



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Indent lisp files on save -- How?
  2022-11-23  5:47 ` Matt Armstrong
@ 2022-11-23  7:41   ` Stefan Kangas
  2022-11-23 12:47   ` Eli Zaretskii
  1 sibling, 0 replies; 5+ messages in thread
From: Stefan Kangas @ 2022-11-23  7:41 UTC (permalink / raw)
  To: Matt Armstrong, T.V Raman, emacs-devel

Matt Armstrong <matt@rfc20.org> writes:

> "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.

FWIW, I use `aggressive-indent' (on GNU ELPA), but I do have to disable
it every now and then to avoid getting massive diffs all the time.

It unfortunately also doesn't always play very well with `nameless', but
that is a more general problem.



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Indent lisp files on save -- How?
  2022-11-23  5:47 ` Matt Armstrong
  2022-11-23  7:41   ` Stefan Kangas
@ 2022-11-23 12:47   ` Eli Zaretskii
  2022-11-23 17:01     ` T.V Raman
  1 sibling, 1 reply; 5+ messages in thread
From: Eli Zaretskii @ 2022-11-23 12:47 UTC (permalink / raw)
  To: Matt Armstrong; +Cc: raman, emacs-devel

> From: Matt Armstrong <matt@rfc20.org>
> Date: Tue, 22 Nov 2022 21:47:43 -0800
> 
> "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.

It should be easy to define a before-save-hook that would run indent-region
on the entire buffer.  Would that be a useful solution?



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Indent lisp files on save -- How?
  2022-11-23 12:47   ` Eli Zaretskii
@ 2022-11-23 17:01     ` T.V Raman
  0 siblings, 0 replies; 5+ messages in thread
From: T.V Raman @ 2022-11-23 17:01 UTC (permalink / raw)
  To: eliz; +Cc: matt, raman, emacs-devel

yes, at least from my perspective.
Eli Zaretskii writes:
 > > From: Matt Armstrong <matt@rfc20.org>
 > > Date: Tue, 22 Nov 2022 21:47:43 -0800
 > > 
 > > "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.
 > 
 > It should be easy to define a before-save-hook that would run indent-region
 > on the entire buffer.  Would that be a useful solution?

-- 

Thanks,

--Raman(I Search, I Find, I Misplace, I Research)
♉ Id: kg:/m/0285kf1  🦮

--

Thanks,

--Raman(I Search, I Find, I Misplace, I Research)
♉ Id: kg:/m/0285kf1  🦮



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-11-23 17:01 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-11-21  3:31 Indent lisp files on save -- How? T.V Raman
2022-11-23  5:47 ` Matt Armstrong
2022-11-23  7:41   ` Stefan Kangas
2022-11-23 12:47   ` Eli Zaretskii
2022-11-23 17:01     ` T.V Raman

Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.