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 02:44:00 -0800 Message-ID: <20071213024400.4c65e207@reforged> References: <598472b5-c766-483a-93e5-15b31bfd880f@b40g2000prf.googlegroups.com> <922740c0-88f0-42c0-a373-d7a28c354c4a@s12g2000prg.googlegroups.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="===============1680721270==" X-Trace: ger.gmane.org 1197543466 32696 80.91.229.12 (13 Dec 2007 10:57:46 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 13 Dec 2007 10:57:46 +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 11:57:57 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 1J2llK-0007lv-Ql for geh-help-gnu-emacs@m.gmane.org; Thu, 13 Dec 2007 11:57:55 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1J2ll2-0008O4-9S for geh-help-gnu-emacs@m.gmane.org; Thu, 13 Dec 2007 05:57:36 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1J2lcb-0006B6-Fv for help-gnu-emacs@gnu.org; Thu, 13 Dec 2007 05:48:53 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1J2lcX-00069G-H0 for help-gnu-emacs@gnu.org; Thu, 13 Dec 2007 05:48:50 -0500 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1J2lcW-000690-Ky for help-gnu-emacs@gnu.org; Thu, 13 Dec 2007 05:48:48 -0500 Original-Received: from wa-out-1112.google.com ([209.85.146.180]) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1J2lcV-0006EV-SD for help-gnu-emacs@gnu.org; Thu, 13 Dec 2007 05:48:48 -0500 Original-Received: by wa-out-1112.google.com with SMTP id k34so1017460wah.10 for ; Thu, 13 Dec 2007 02:48:44 -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=CqPTdYFkwJaOstYmfFu56emF5jlTAA/Ko1F33F92WgM=; b=AEAZSttmyKn773X6dw18CDb9/unrcRvUaUukoRwlBovkawsAgZOTX4UpkQj9UuZr78d52I4e6X3p1mkx7eTrCmre0O6f7n3emQVJR071SssMCBDGB5rlwAYD4MoDE6b0lsznYvzSUsxwEP7/VeCXYp8aGgolotqN8Gp4BWeY1Jc= 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=h3wFA7Kcno7zSoXr4/iSWiiW0se/Dhor32Kq478zvHR4MsqbSN6d+s4Zmyluoj8uhcp1Gi/O8gl3+aITom2Kxwanhuzuj151YFElG+j7oHc8kby+cgFvY7102G8x/DOGopHCmdg/0jsTpznJRBQaitq8qPZlapKII1KGdNmoHqY= Original-Received: by 10.114.27.20 with SMTP id a20mr2083934waa.101.1197542924096; Thu, 13 Dec 2007 02:48:44 -0800 (PST) Original-Received: from reforged ( [71.217.198.62]) by mx.google.com with ESMTPS id n37sm1855300wag.2007.12.13.02.48.41 (version=SSLv3 cipher=OTHER); Thu, 13 Dec 2007 02:48:43 -0800 (PST) In-Reply-To: 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:50061 Archived-At: --===============1680721270== Content-Type: multipart/signed; boundary=Sig_sgyWgLFbeMDAGV4yLwKVOnn; protocol="application/pgp-signature"; micalg=PGP-SHA1 --Sig_sgyWgLFbeMDAGV4yLwKVOnn Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable On Thu, 6 Dec 2007 09:14:12 -0800 (PST) Xah Lee wrote: > 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) > ) > ) > ) 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 lis= t. 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. A reliable solution would likely be based off something from thingatpoint.e= l ? there must be something like this in the code-motion code for elisp. This sort of function/feature? is dangerous even when implemented procedura= lly because it will go wild if your delimiters aren't matched correctly. Other flaws: * error path in bounds scan marked by "error ?" not implemented because I= have not studied elisp error handling yet. * does not handle multi-byte characters due to use of aref in bounds-scan= -{forward,backward} 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. I implemented it recursively simply because It looked prettier to me, proba= bly since I started with scheme. anyways here goes. one last big fat warning: this code is for fun, it will eat your children e= ventually. ;; --- 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))) (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))) (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))) (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))) (defun scan-lisp-list-close () "wrapper for bounds-scan that searches for the closing delimiter of a lis= p list" (let* ((start-at (point)) (close-at (bounds-scan-forward "()" start-at))) (if (> close-at start-at) (- close-at 1) start-at) )) (defun scan-lisp-list-open () "wrapper for bounds-scan that searches for the opening delimiter of a lis= p list" (let* ((start-at (point)) (open-at (bounds-scan-backward "()" start-at))) (if (< open-at start-at) (+ open-at 1) start-at) )) (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)))) ;;----- end of elisp Cheers, Mike Mattie --Sig_sgyWgLFbeMDAGV4yLwKVOnn Content-Type: application/pgp-signature; name=signature.asc Content-Disposition: attachment; filename=signature.asc -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (GNU/Linux) iD8DBQFHYQzwdfRchrkBInkRAhmfAJwPQQKkEz+tuTvizoLddFr7jnDl4QCfY8Nb FYib/bfQRf3ZpLU11ITATtA= =Uz45 -----END PGP SIGNATURE----- --Sig_sgyWgLFbeMDAGV4yLwKVOnn-- --===============1680721270== 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 --===============1680721270==--