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: > > > 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) > > > > Here's the correct version: > > > > (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 list. > > 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.el ? there must be something like this in the > code-motion code for elisp. > > This sort of function/feature? is dangerous even when implemented > procedurally 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, > probably since I started with scheme. anyways here goes. > > 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-position 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-position (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 lisp list" (bounds-scan-forward "()" (point))) (defun scan-lisp-list-open () "wrapper for bounds-scan that searches for the opening delimiter of a lisp 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)))) > > ;;----- end of elisp > > Cheers, > Mike Mattie