From: Mike Mattie <codermattie@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: Help with keybinding to delete between {}
Date: Thu, 13 Dec 2007 04:46:12 -0800 [thread overview]
Message-ID: <20071213044612.43b371c4@reforged> (raw)
In-Reply-To: <20071213024400.4c65e207@reforged>
[-- Attachment #1.1: Type: text/plain, Size: 6110 bytes --]
On Thu, 13 Dec 2007 02:44:00 -0800
Mike Mattie <codermattie@gmail.com> wrote:
> On Thu, 6 Dec 2007 09:14:12 -0800 (PST)
> Xah Lee <xah@xahlee.org> 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
[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
[-- Attachment #2: Type: text/plain, Size: 152 bytes --]
_______________________________________________
help-gnu-emacs mailing list
help-gnu-emacs@gnu.org
http://lists.gnu.org/mailman/listinfo/help-gnu-emacs
next prev parent reply other threads:[~2007-12-13 12:46 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-12-05 1:33 Help with keybinding to delete between {} lampshade
2007-12-05 10:48 ` Bernardo Bacic
2007-12-05 12:55 ` William Xu
2007-12-05 13:30 ` Andreas Röhler
2007-12-05 23:50 ` Ilya Zakharevich
2007-12-06 6:33 ` Xah Lee
2007-12-06 17:14 ` Xah Lee
2007-12-12 9:20 ` lampshade
2007-12-13 3:59 ` Mike Mattie
[not found] ` <mailman.4961.1197518641.18990.help-gnu-emacs@gnu.org>
2007-12-13 10:39 ` Xah Lee
2007-12-13 10:44 ` Mike Mattie
2007-12-13 12:46 ` Mike Mattie [this message]
2007-12-13 13:01 ` Peter Dyballa
2007-12-14 11:43 ` Xah Lee
2007-12-07 16:30 ` Stefan Monnier
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20071213044612.43b371c4@reforged \
--to=codermattie@gmail.com \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.