help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Clear trailing whitespace on save, but not at the cursor


From: Aaron Meurer
Subject: Re: Clear trailing whitespace on save, but not at the cursor
Date: Fri, 23 Mar 2012 13:57:58 -0600

On Thu, Mar 22, 2012 at 3:08 PM, Doug Lewan <dougl@shubertticketing.com> wrote:
> Aaron Meurer writes:
>
>> But it seems to me that the whole emacs lisp system is designed
>> from the ground up to do hooking (by the way, where I come from, "hooking"
>> is given the much auspicious name "monkey patching").
>
> First:
>
> Today I learned "monkey patching" (and, by going to Wikipedia, it's synonym 
> "duck punching"). Thanks for giving me a techno-chuckle.
>
> Second:
>
> (Assuming I understand the meaning of monkey patching) Hooks are /not/ the 
> same thing. Hooks are favors you ask for when something happens. Hooks are 
> easily removed. Often they are if they are badly behaved. See the 
> documentation for after-change-functions.

I guess they're not the same in the sense that they're officially
supported.  This was kind of the whole point of my question, which is,
to what point are these things supposed to be the way you do things?

Like I said, they can be problematic.  For example, take the seemingly
innocent (add-hook 'before-save-hook 'delete-trailing-whitespace),
which is the universally recommended way make emacs to clear
whitespace on save.  As far as I can tell, with this active, it is
impossible to save without clearing whitespace, unless you clear the
hook.  With the global-set-key solution, I can easily save without
clearing by doing M-x save-buffer.

Hooks are fine if all they do is enable some mode, because I can
easily turn that off if I don't want it. But other than that, you run
into the above issue. Or maybe there's an easy way to bypass hooks
that I just don't know about.

There's other potential problems that are shared by hooks and monkey
patching, like expected invariants that are no longer met.  I suppose
the very existence of hooks means that there really can be no expected
invariants about anything. But to me, this is impossible (you have to
expect that what you use will work, or else you can't really say
anything about your program).

And by the way, I wasn't just referring to defadvice for monkey
patching.  That actually seems like a better way to do it, because at
least it warns you.  I was also talking about how in emacs lisp,
pretty much everything is a global variable, so you can often "fix"
something by just changing some internal variable to do what you want
(usually with knowledge of how it is used internally).

Aaron Meurer

>
> Advice seem like they fit the definition of monkey patching. A piece of 
> advice can be enabled and disabled, but it stays once it has been 
> (defadvice)d.
>
> The emacs lisp manual has lots of warnings about advice. It has legitimate 
> uses, but they are rare -- typically there's-no-other-darn-way-to-do-this 
> kind of things. It's usually worthwhile spending time to find another way 
> because it's hard to write interesting advice that doesn't have unexpected 
> consequences.
>
> The emacs lisp manual has no general warnings about hooks.
>
> There's probably something to that.
>
> ,Douglas
> Douglas Lewan
> Shubert Ticketing
> (201) 489-8600 ext 224
>
>
>



reply via email to

[Prev in Thread] Current Thread [Next in Thread]