From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Xah Lee Newsgroups: gmane.emacs.devel Subject: Re: Patch to change just-one-space Date: Thu, 13 Aug 2009 16:12:52 -0700 Message-ID: References: <7b501d5c0908131337g300e8895i465719007e29074c@mail.gmail.com> Reply-To: xahlee@gmail.com NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=00163641789d2d81ae04710e13ba X-Trace: ger.gmane.org 1250205194 9581 80.91.229.12 (13 Aug 2009 23:13:14 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 13 Aug 2009 23:13:14 +0000 (UTC) Cc: Emacs-Devel devel To: Deniz Dogan Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Fri Aug 14 01:13:07 2009 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1MbjTm-0001Hz-04 for ged-emacs-devel@m.gmane.org; Fri, 14 Aug 2009 01:13:06 +0200 Original-Received: from localhost ([127.0.0.1]:46630 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MbjTl-0006ye-AO for ged-emacs-devel@m.gmane.org; Thu, 13 Aug 2009 19:13:05 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MbjTf-0006yV-S0 for emacs-devel@gnu.org; Thu, 13 Aug 2009 19:12:59 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MbjTb-0006y1-8t for emacs-devel@gnu.org; Thu, 13 Aug 2009 19:12:59 -0400 Original-Received: from [199.232.76.173] (port=49658 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MbjTb-0006xy-3v for emacs-devel@gnu.org; Thu, 13 Aug 2009 19:12:55 -0400 Original-Received: from mail-pz0-f197.google.com ([209.85.222.197]:43914) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1MbjTa-0001n8-Ja for emacs-devel@gnu.org; Thu, 13 Aug 2009 19:12:55 -0400 Original-Received: by pzk35 with SMTP id 35so860930pzk.24 for ; Thu, 13 Aug 2009 16:12:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:reply-to:in-reply-to :references:date:message-id:subject:from:to:cc:content-type; bh=1g5H8N12xxRWbFboZKIeZ1ZDVCqCeMtpOCsErcHtP7E=; b=WZo9mEihGmdXBs73xjO6SDJCGco52uAWJ1Cyx1jHT92rCquh8uShbmw5UjligVvVhZ 9q6uk6aHZ8LIS8KDTBGsXSC7DZUk70SXlS8CZtO0C6fiOHeUOYpFNrF2ECp4NbIqKLDj L7j9gjW1WQXXGVhz6hzO3biryklEQ1pcCom8M= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:reply-to:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; b=KqLVRmIBPSGQ/xzX90TmlSOCMMZj8zJll9VElryFk9bY9ip9JuMK6VWqMc5QOemnlJ lLyjmlwx/qbIgFgCtRZeUUxLurCXKzPwIgKC5hXo8MD+YVVFioePlSiqi6m2+W2rThTP 2Hxz73y5N6XCqmN5kt+POB+YzcR9fUiW8uyb0= Original-Received: by 10.115.76.10 with SMTP id d10mr1412064wal.31.1250205172072; Thu, 13 Aug 2009 16:12:52 -0700 (PDT) In-Reply-To: <7b501d5c0908131337g300e8895i465719007e29074c@mail.gmail.com> X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 2) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:114201 Archived-At: --00163641789d2d81ae04710e13ba Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable i share this view. though, in my experiment, i find that there are often situations where shrinking all whitespaces including EOL is not convenient. Here's a example= : ----------------------------- numRep=3D0 for couple in findreplace: c numRep +=3D s.count(couple[0]) outtext=3Ds.replace(couple[0],couple[1]) s=3Douttext ----------------------------- where the c is the cursor point. If you shrink all whitespaces including EOL, the the current line becomes joined with previous line. what i find more ideal is this: when the current line contain non-whitespace chars, then shrink just spaces and tabs, but if the current line does not contain non-whitespace chars (it's all white space), then shrink all all spaces, tab, EOL. the code i've been using is this: (defun shrink-whitespaces () "Remove white spaces around cursor to just one or none. If current line contains non-white space chars, then shrink any whitespace char surrounding cursor to just one space. If current line does not contain non-white space chars, then remove blank lines to just one." (interactive) (let ( cursor-point line-has-meat-p ; current line contains non-white space chars spaceTabNeighbor-p whitespace-begin whitespace-end space-or-tab-begin space-or-tab-end line-begin-pos line-end-pos ) (save-excursion ;; todo: might consider whitespace as defined by syntax table, and also consider whitespace chars in unicode if syntax table doesn't already considered it. (setq cursor-point (point)) (setq spaceTabNeighbor-p (if (or (looking-at " \\|\t") (looking-back = " \\|\t")) t nil) ) (move-beginning-of-line 1) (setq line-begin-pos (point) ) (move-end-of-line 1) (setq line-end-pos (point) ) ;; (re-search-backward "\n$") (setq line-begin-pos (point) ) ;; (re-search-forward "\n$") (setq line-end-pos (point) ) (setq line-has-meat-p (if (< 0 (count-matches "[[:graph:]]" line-begin-pos line-end-pos)) t nil) ) (goto-char cursor-point) (skip-chars-backward "\t ") (setq space-or-tab-begin (point)) (skip-chars-backward "\t \n") (setq whitespace-begin (point)) (goto-char cursor-point) (skip-chars-forward "\t ") (setq space-or-tab-end (point)) (skip-chars-forward "\t \n") (setq whitespace-end (point)) ) (if line-has-meat-p (progn (when spaceTabNeighbor-p (delete-region space-or-tab-begin space-or-tab-end) (insert " ")) ) (progn ;; (delete-region whitespace-begin whitespace-end) ;; (insert "\n") (delete-blank-lines) ) ;; todo: possibly code my own delete-blank-lines here for better efficiency, because delete-blank-lines seems complex. ) ) ) Xah On Thu, Aug 13, 2009 at 1:37 PM, Deniz Dogan wro= te: > Hi > > I have long wanted to change the behavior of just-one-space to not > only delete spaces and tab characters, but newline characters as well. > Attached is the patch for this change. > > I don't think that this is such a controversial modification and I > believe very few user macros will break. If I'm wrong, let me know! > > -- > Deniz Dogan > --=20 Xah =E2=88=91 http://xahlee.org/ --00163641789d2d81ae04710e13ba Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
i share this view.

though, in my experim= ent, i find that there are often situations where shrinking all whitespaces= including EOL is not convenient. Here's a example:

-----------------------------
=C2=A0=C2=A0 numRep=3D0
<= div>=C2=A0=C2=A0 for couple in findreplace:
c =C2=A0 =C2=A0numRep= +=3D s.count(couple[0])
=C2=A0=C2=A0 =C2=A0 =C2=A0outtext=3Ds.re= place(couple[0],couple[1])
=C2=A0=C2=A0 =C2=A0 =C2=A0s=3Douttext<= /div>
-----------------------------

where the c is = the cursor point.
If you shrink all whitespaces including EOL, th= e the current line becomes joined with previous line.

what i find more ideal is this:

when the curr= ent line contain non-whitespace chars, then shrink just spaces and tabs, bu= t if the current line does not contain non-whitespace chars (it's all w= hite space), then shrink all all spaces, tab, EOL.

the code i've been using is this:

(defun shrink-whitespaces ()
=C2=A0=C2=A0"Remove whi= te spaces around cursor to just one or none.
If current line cont= ains non-white space chars, then shrink any whitespace char surrounding cur= sor to just one space.
If current line does not contain non-white space chars, then remove bl= ank lines to just one."
=C2=A0=C2=A0(interactive)
= =C2=A0=C2=A0(let (
=C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0cursor-point<= /div>
=C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0line-has-meat-p =C2=A0; current = line contains non-white space chars
=C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0spaceTabNeighbor-p
=C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0whitespace-begin whitespace-end
=C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0space-or-tab-begin space-or-tab-end
= =C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0line-begin-pos line-end-pos
=C2= =A0=C2=A0 =C2=A0 =C2=A0 =C2=A0)
=C2=A0=C2=A0 =C2=A0(save-excursio= n
=C2=A0=C2=A0 =C2=A0 =C2=A0;; todo: might consider whitespace as define= d by syntax table, and also consider whitespace chars in unicode if syntax = table doesn't already considered it.
=C2=A0=C2=A0 =C2=A0 =C2= =A0(setq cursor-point (point))

=C2=A0=C2=A0 =C2=A0 =C2=A0(setq spaceTabNeighbor-p (if (or (= looking-at " \\|\t") (looking-back " \\|\t")) t nil) )<= /div>
=C2=A0=C2=A0 =C2=A0 =C2=A0(move-beginning-of-line 1) (setq line-b= egin-pos (point) )
=C2=A0=C2=A0 =C2=A0 =C2=A0(move-end-of-line 1)= (setq line-end-pos (point) )
=C2=A0=C2=A0 =C2=A0 =C2=A0;; =C2=A0 =C2=A0 =C2=A0 (re-search-backward = "\n$") (setq line-begin-pos (point) )
=C2=A0=C2=A0 =C2= =A0 =C2=A0;; =C2=A0 =C2=A0 =C2=A0 (re-search-forward "\n$") (setq= line-end-pos (point) )
=C2=A0=C2=A0 =C2=A0 =C2=A0(setq line-has-= meat-p (if (< 0 (count-matches "[[:graph:]]" line-begin-pos li= ne-end-pos)) t nil) )
=C2=A0=C2=A0 =C2=A0 =C2=A0(goto-char cursor-point)

=C2=A0=C2=A0 =C2=A0 =C2=A0(skip-chars-backward "\t ")
=
=C2=A0=C2=A0 =C2=A0 =C2=A0(setq space-or-tab-begin (point))
=
=C2=A0=C2=A0 =C2=A0 =C2=A0(skip-chars-backward "\t \n&q= uot;)
=C2=A0=C2=A0 =C2=A0 =C2=A0(setq whitespace-begin (point))
=C2=A0=C2=A0 =C2=A0 =C2=A0(goto-char cursor-point) =C2=A0 =C2= =A0 =C2=A0(skip-chars-forward "\t ")
=C2=A0=C2=A0 =C2= =A0 =C2=A0(setq space-or-tab-end (point))
=C2=A0=C2=A0 =C2=A0 =C2= =A0(skip-chars-forward "\t \n")
=C2=A0=C2=A0 =C2=A0 =C2=A0(setq whitespace-end (point))
=C2= =A0=C2=A0 =C2=A0 =C2=A0)


=C2=A0=C2= =A0 =C2=A0(if line-has-meat-p
=C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0(p= rogn=C2=A0
=C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(when spaceTab= Neighbor-p
=C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(delete= -region space-or-tab-begin space-or-tab-end)
=C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(insert " "))=
=C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0)

=C2=A0=C2=A0 =C2=A0 =C2=A0(progn
;; =C2=A0 =C2=A0 =C2=A0 =C2= =A0 (delete-region whitespace-begin whitespace-end)
;; =C2=A0 =C2= =A0 =C2=A0 =C2=A0 (insert "\n")
=C2=A0=C2=A0 =C2=A0 =C2=A0 =C2=A0(delete-blank-lines)
=C2=A0=C2= =A0 =C2=A0 =C2=A0 =C2=A0)
=C2=A0=C2=A0 =C2=A0 =C2=A0;; todo: poss= ibly code my own delete-blank-lines here for better efficiency, because del= ete-blank-lines seems complex.
=C2=A0=C2=A0 =C2=A0 =C2=A0)
<= div>=C2=A0=C2=A0 =C2=A0)
=C2=A0=C2=A0)

=C2=A0Xah

On Thu, Aug 13, 2009 at 1:37 PM, Deniz Dogan <den= iz.a.m.dogan@gmail.com> wrote:
Hi

I have long wanted to change the behavior of just-one-space to not
only delete spaces and tab characters, but newline characters as well.
Attached is the patch for this change.

I don't think that this is such a controversial modification and I
believe very few user macros will break. If I'm wrong, let me know!

--
Deniz Dogan



--
=C2=A0Xah
= =E2=88=91 http://xahlee.or= g/
--00163641789d2d81ae04710e13ba--