From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Aaron Meurer Newsgroups: gmane.emacs.help Subject: Re: Clear trailing whitespace on save, but not at the cursor Date: Thu, 22 Mar 2012 10:56:01 -0600 Message-ID: References: <87aa3vwbsf.fsf@gnuvola.org> <87booarvd6.fsf@gnuvola.org> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: dough.gmane.org 1332435399 19651 80.91.229.3 (22 Mar 2012 16:56:39 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Thu, 22 Mar 2012 16:56:39 +0000 (UTC) Cc: help-gnu-emacs@gnu.org, Thien-Thi Nguyen To: Le Wang Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Mar 22 17:56:38 2012 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1SAlJT-0002jW-1F for geh-help-gnu-emacs@m.gmane.org; Thu, 22 Mar 2012 17:56:35 +0100 Original-Received: from localhost ([::1]:42899 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SAlJS-0000mh-GJ for geh-help-gnu-emacs@m.gmane.org; Thu, 22 Mar 2012 12:56:34 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:38346) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SAlJM-0000m8-Ox for help-gnu-emacs@gnu.org; Thu, 22 Mar 2012 12:56:30 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SAlJH-0000Hp-Oy for help-gnu-emacs@gnu.org; Thu, 22 Mar 2012 12:56:28 -0400 Original-Received: from mail-yx0-f169.google.com ([209.85.213.169]:53707) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SAlJH-0000Hj-I5 for help-gnu-emacs@gnu.org; Thu, 22 Mar 2012 12:56:23 -0400 Original-Received: by yenm8 with SMTP id m8so2304374yen.0 for ; Thu, 22 Mar 2012 09:56:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=Soo4y1FwZrSoD989n8UsxoG3lj4MYW71qwEmcBwYGnI=; b=N2md7ESXEuuhilfkCu27PI6nkS/Xrpz7/L9WjmmeG/aFpFg3tYR1PrMgYAYz99ZVKW PaXSlp1FKuo01KZcxGbbpyW035hy71wY84P0Ly7p1Zr+qwoBnFZRyMtTm5njL0jTcmJM SZtMVRPXF2ksiS7ASAKQrlh0HBoEMUBJPVR/O0BWDw+avif9OKx0Nbhb05jGC/gohLSD X8zHAdJbmYlITqkBazh1HjvrIR0Fv93FRaaoJPjDD9IY9mIBw2/rVtfxksxbR/PBreuC gKZv1wmTZC7rqaWF6TjuTqeNg/m84t9gjDs8irY2VNLUPKu0DIQZuWSt8igbXA0+69eH cQYg== Original-Received: by 10.60.26.166 with SMTP id m6mr10474382oeg.45.1332435381595; Thu, 22 Mar 2012 09:56:21 -0700 (PDT) Original-Received: by 10.182.109.41 with HTTP; Thu, 22 Mar 2012 09:56:01 -0700 (PDT) In-Reply-To: X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.85.213.169 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:84091 Archived-At: 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 wrote: > On Thu, Mar 22, 2012 at 8:13 AM, Aaron Meurer wrote: >> >> Sorry, I'm still *very* new to emacs lisp (lisp in general, actually). >> =A0Does 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? =A0The 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.=A0 You're changing the fundamental > behaviour of a function that could be called by other functions.=A0 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) > =A0 "save buffer delete trailing white space, preserve white space before > point if point is past text" > =A0 (interactive "p") > =A0 (let ((save (when (and (looking-at "\\s-*$") > =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 = (looking-back "\\s-+" (line-beginning-position) t)) > =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 (match-string 0)))) > =A0=A0=A0 (delete-trailing-whitespace) > =A0=A0=A0 (save-buffer arg) > =A0=A0=A0 (when save > =A0=A0=A0=A0=A0 (insert save) > =A0=A0=A0=A0=A0 (set-buffer-modified-p nil)))) > > (global-set-key [remap save-buffer] 'my-save-buffer-dtws) > > > >> >> Aaron Meurer > > > > -- > Le