all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Aaron Meurer <asmeurer@gmail.com>
To: Le Wang <l26wang@gmail.com>
Cc: help-gnu-emacs@gnu.org, Thien-Thi Nguyen <ttn@gnuvola.org>
Subject: Re: Clear trailing whitespace on save, but not at the cursor
Date: Thu, 22 Mar 2012 10:56:01 -0600	[thread overview]
Message-ID: <CAKgW=6Ks=BXqzknZT0tkExvQWJHqJQeRcW_b7tihj+sE-bJvLw@mail.gmail.com> (raw)
In-Reply-To: <CAM=K+ioQm-Bmqt=BFujQDgUikQox-AJo-VRS=aM95Ca5S8Y2iA@mail.gmail.com>

Thanks a lot.  This does exactly what I want.  And this also has two
unexpected benefits: first, I can still force a save that includes
trailing whitespace by using M-x save-buffer instead of C-x C-s.
Second, trailing whitespace is cleared even if I haven't yet modified
the file, which was something else that bugged me.

On Thu, Mar 22, 2012 at 9:45 AM, Le Wang <l26wang@gmail.com> wrote:
> On Thu, Mar 22, 2012 at 8:13 AM, Aaron Meurer <asmeurer@gmail.com> wrote:
>>
>> Sorry, I'm still *very* new to emacs lisp (lisp in general, actually).
>>  Does this mean that it's possible to modify the above defadvice
>> function you gave above so that it actually clears it before the save,
>> but then puts it back?  The function works just fine in not clearing
>> at the cursor, but as noted, this is not quite what I want, because I
>> do *not* want to save trailing whitespace to file at all (I would
>> rather have my current annoyance).
>
>
> The defadvice solution is not ideal.  You're changing the fundamental
> behaviour of a function that could be called by other functions.  It's
> better to make your own command, this should fit all your requirements:

As I said, I'm still learning emacs lisp (and lisp in general), so
maybe you could clarify something for me.  I agree that defadvice is
not ideal.  To me, hooking at all is a bad thing.  At one point when
trying to fix this, I was playing around with adding a hook to
kill-buffer-hook.  I messed it up with a simple undefined symbol
error, and found it difficult to even exit emacs.  To me, this sort of
thing typifies why hooking is bad.

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").  It seems that
much of the ways that I find online to do things, even official ways,
involve some kind of hooking: overwriting or adding to some private
variable, defadvice type things, adding things to "hook" variables,
and so on.  Is this view correct?  Is there a reason why this sort of
practice would not be as bad in emacs is it is in other languages
where hooking is possible.

My background is mostly in Python.  In Python, you are allowed by the
language to monkey patch wherever you want (with a few exceptions),
but it's rarely the official way to do things, and it's generally held
in the community that this is a bad way of doing things.

Or am I simply misviewing the way things work with my as of yet
limited knowledge of lisp?

Aaron Meurer

>
> (defun my-save-buffer-dtws (arg)
>   "save buffer delete trailing white space, preserve white space before
> point if point is past text"
>   (interactive "p")
>   (let ((save (when (and (looking-at "\\s-*$")
>                          (looking-back "\\s-+" (line-beginning-position) t))
>                 (match-string 0))))
>     (delete-trailing-whitespace)
>     (save-buffer arg)
>     (when save
>       (insert save)
>       (set-buffer-modified-p nil))))
>
> (global-set-key [remap save-buffer] 'my-save-buffer-dtws)
>
>
>
>>
>> Aaron Meurer
>
>
>
> --
> Le



  reply	other threads:[~2012-03-22 16:56 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-03-05  0:48 Clear trailing whitespace on save, but not at the cursor Aaron Meurer
2012-03-05 11:02 ` Thien-Thi Nguyen
2012-03-05 15:07   ` Juanma Barranquero
2012-03-06  8:23     ` Thien-Thi Nguyen
2012-03-22  0:13       ` Aaron Meurer
2012-03-22 15:45         ` Le Wang
2012-03-22 16:56           ` Aaron Meurer [this message]
2012-03-05 16:04   ` Deniz Dogan
  -- strict thread matches above, loose matches on Subject: below --
2012-03-22 21:08 Doug Lewan
2012-03-23 19:57 ` Aaron Meurer
2012-03-25 14:12   ` Le Wang

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

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

  git send-email \
    --in-reply-to='CAKgW=6Ks=BXqzknZT0tkExvQWJHqJQeRcW_b7tihj+sE-bJvLw@mail.gmail.com' \
    --to=asmeurer@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=l26wang@gmail.com \
    --cc=ttn@gnuvola.org \
    /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 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.