From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Mike Mattie Newsgroups: gmane.emacs.help Subject: Re: Help with keybinding to delete between {} Date: Thu, 13 Dec 2007 04:46:12 -0800 Message-ID: <20071213044612.43b371c4@reforged> References: <598472b5-c766-483a-93e5-15b31bfd880f@b40g2000prf.googlegroups.com> <922740c0-88f0-42c0-a373-d7a28c354c4a@s12g2000prg.googlegroups.com> <20071213024400.4c65e207@reforged> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="===============1162633401==" X-Trace: ger.gmane.org 1197550291 22947 80.91.229.12 (13 Dec 2007 12:51:31 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 13 Dec 2007 12:51:31 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Dec 13 13:51:41 2007 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1J2nXO-0004aZ-NZ for geh-help-gnu-emacs@m.gmane.org; Thu, 13 Dec 2007 13:51:39 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1J2nX6-0003zI-KR for geh-help-gnu-emacs@m.gmane.org; Thu, 13 Dec 2007 07:51:20 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1J2nWn-0003zC-5W for help-gnu-emacs@gnu.org; Thu, 13 Dec 2007 07:51:01 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1J2nWk-0003xq-B3 for help-gnu-emacs@gnu.org; Thu, 13 Dec 2007 07:51:00 -0500 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1J2nWk-0003xn-5T for help-gnu-emacs@gnu.org; Thu, 13 Dec 2007 07:50:58 -0500 Original-Received: from wa-out-1112.google.com ([209.85.146.176]) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1J2nWj-0001P5-PI for help-gnu-emacs@gnu.org; Thu, 13 Dec 2007 07:50:58 -0500 Original-Received: by wa-out-1112.google.com with SMTP id k34so1077690wah.10 for ; Thu, 13 Dec 2007 04:50:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:date:from:to:subject:message-id:in-reply-to:references:x-mailer:mime-version:content-type; bh=D4DciIfzE4f0UkKiNrjHdAY8R380a+Wcj/vN9VWABP8=; b=ok//vdAho96tiQ48qGOwjSuVihcbRwHOScy8PVnklvmDm2rq5ZHbnNfvsJKuVga4/BeMdO1/b4D+/evitg5SUlfW2Dk9g1xQxVrbkNkdTFHLOf6PoXs9hQOTBeJf5XDkL2KtJVIXXY0ZsbUwplm/jCWUDyYioSu6xbrUByJy//Y= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=date:from:to:subject:message-id:in-reply-to:references:x-mailer:mime-version:content-type; b=iFWZVdvevWR84F9SrR8reqKJXFWyXNun+A1FYrhmEz03dmQU+EvZaivU0vsQ71q9WQvj5o3oFluSzLVf5ILwhmy8NN8zS0a0PT5iQskunc83oxpTndLZnGG3CFjdYLIeF7+8wdkbgpFlAA8RRqO9DANuM9tNCUl8f5W0VFAXdTM= Original-Received: by 10.114.154.1 with SMTP id b1mr2195125wae.118.1197550256205; Thu, 13 Dec 2007 04:50:56 -0800 (PST) Original-Received: from reforged ( [71.217.198.62]) by mx.google.com with ESMTPS id m28sm10258956waf.2007.12.13.04.50.50 (version=SSLv3 cipher=OTHER); Thu, 13 Dec 2007 04:50:50 -0800 (PST) In-Reply-To: <20071213024400.4c65e207@reforged> X-Mailer: Claws Mail 3.0.0 (GTK+ 2.12.1; i686-pc-linux-gnu) X-detected-kernel: by monty-python.gnu.org: Linux 2.6 (newer, 2) X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:50066 Archived-At: --===============1162633401== Content-Type: multipart/signed; boundary="Sig_3snn.derZ=zkzbQCw=En2zR"; protocol="application/pgp-signature"; micalg=PGP-SHA1 --Sig_3snn.derZ=zkzbQCw=En2zR Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable On Thu, 13 Dec 2007 02:44:00 -0800 Mike Mattie wrote: > On Thu, 6 Dec 2007 09:14:12 -0800 (PST) > Xah Lee wrote: >=20 > > for some reason my code in the previous post is completely non- > > functional. (i swear i used it for few months. Perhaps when i put on > > the website i got smart and edited it "for the better" without > > testing) > >=20 > > Here's the correct version: > >=20 > > (defun delete-enclosed-text () > > "Delete texts between any pair of delimiters. > > Note: if you have nested matching pairs, the cursor > > should be inside the inner most one. Else it gets confused. > > This code should to be fixed in the future." > > (interactive) > > (save-excursion > > (let (p1 p2) > > (skip-chars-backward "^(<["<<") (setq p1 (point)) > > (skip-chars-forward "^)>]">>") (setq p2 (point)) > > (delete-region p1 p2) > > ) > > ) > > ) >=20 > I decided to fix it in regards to nested lists. My implementation > works however it is purely for elucidation. It is a recursive > implementation that is nth recursive where n is the number of nested > delimiters in the list. >=20 > that means that if you use this on a list with deep nesting it *will* > crash after exhausting the recursion limit. So don't use it for real. >=20 > A reliable solution would likely be based off something from > thingatpoint.el ? there must be something like this in the > code-motion code for elisp. >=20 > This sort of function/feature? is dangerous even when implemented > procedurally because it will go wild if your delimiters aren't > matched correctly. >=20 > Other flaws: > * error path in bounds scan marked by "error ?" not implemented > because I have not studied elisp error handling yet. >=20 > * does not handle multi-byte characters due to use of aref in > bounds-scan-{forward,backward} >=20 > With that said it does illustrate the value of lexical-let, and > solves the problem in a general way. It would be quite easy to add > variations that kill instead of deleting, or whatever other features > you want. >=20 > I implemented it recursively simply because It looked prettier to me, > probably since I started with scheme. anyways here goes. >=20 > one last big fat warning: this code is for fun, it will eat your > children eventually. a small re-factor I noticed when I read the code a bit later. the check for motion in the scan is hoisted into bounds-scan-{forward,backward} so it isn't duplicated needlessly. > ;; --- start of elisp (defun bounds-scan ( seek-bounds open-bound-p close-bound-p restart-positio= n position level ) "scan for the delimitation of a region. This is a general form of a simple algorithm that counts opening and closing delimiters to scan past nested delimited spans." (progn (goto-char position) ;; move to the starting position before scanning. (funcall seek-bounds) (cond ((funcall open-bound-p) (bounds-scan seek-bounds open-bound-p close-bound-p restart-position (funcall restart-position) (+ level 1))) ((funcall close-bound-p) (if (> level 0) ;; when we have a positive level to start with ;; scan again with a decremented level. (bounds-scan seek-bounds open-bound-p close-bound-p restart-posit= ion (funcall restart-position) (- level 1)) ;; return point as we are done (point) )) ;; error ? ))) (defun bounds-scan-forward ( delimiters position ) "entry point for bounds-scan forward. given delimiters: a string containing a pair of delimiting characters, which must be in \"open close\" order, scan forward for the bounding delimiter returning the position before the delimiter" (lexical-let ((open-delimiter (aref delimiters 0)) (close-delimiter (aref delimiters 1))) (let ((close-at (bounds-scan (lambda () (skip-chars-forward (concat "^" delimiters))) (lambda () (char-equal open-delimiter (char-after))) (lambda () (char-equal close-delimiter (char-after))) (lambda () (+ (point) 1)) position 0))) (if (> close-at position) (- close-at 1) position) ))) (defun bounds-scan-backward ( delimiters position ) "entry point for bounds-scan backward. given delimiters: a string containing a pair of delimiting characters, which must be in \"open close\" order, scan backward for the bounding delimiter returning the position after the delimiter" (lexical-let ;; note the inversion of the order since we are looking backwards ((open-delimiter (aref delimiters 1)) (close-delimiter (aref delimiters 0))) (let ((open-at (bounds-scan (lambda () (skip-chars-backward (concat "^" delimiters))) (lambda () (char-equal open-delimiter (char-before))) (lambda () (char-equal close-delimiter (char-before))) (lambda () (- (point) 1)) position 0))) (if (< open-at position) (+ open-at 1) position) ))) (defun scan-lisp-list-close () "wrapper for bounds-scan that searches for the closing delimiter of a lis= p list" (bounds-scan-forward "()" (point))) (defun scan-lisp-list-open () "wrapper for bounds-scan that searches for the opening delimiter of a lis= p list" (bounds-scan-backward "()" (point))) (defun lisp-list-delete-body () "delete the body of a lisp list including any nested lists" (interactive) (let ((open-pos (scan-lisp-list-open)) (close-pos (scan-lisp-list-close))) (delete-backward-char (- close-pos open-pos)))) >=20 > ;;----- end of elisp >=20 > Cheers, > Mike Mattie --Sig_3snn.derZ=zkzbQCw=En2zR Content-Type: application/pgp-signature; name=signature.asc Content-Disposition: attachment; filename=signature.asc -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (GNU/Linux) iD8DBQFHYSmVdfRchrkBInkRAiBOAKCjgo7SbArTm1MiMMSZ6og4kX1bXwCfbGuM rIi3N+8xHSuwaXskl1IdZgk= =VfZQ -----END PGP SIGNATURE----- --Sig_3snn.derZ=zkzbQCw=En2zR-- --===============1162633401== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ help-gnu-emacs mailing list help-gnu-emacs@gnu.org http://lists.gnu.org/mailman/listinfo/help-gnu-emacs --===============1162633401==--