help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Help with keybinding to delete between {}


From: Xah Lee
Subject: Re: Help with keybinding to delete between {}
Date: Wed, 5 Dec 2007 22:33:20 -0800 (PST)
User-agent: G2/1.0

you wrote:
<<I would really like a keybinding that would allow me to delete any
text between sexp's like {} (), etc no matter where I am between them
and whether or not that text has spaces.>>

I wrote a code few months ago that does exactly what you asked.

(defun delete-enclosed-text ()
  "Delete texts between any pair of delimiters."
  (interactive)
  (skip-chars-backward "^<>()""")
  (delete-char (skip-chars-forward "^<>()""")))

For more, see:
http://xahlee.org/emacs/elisp_examples.html

These days i don't use the above. Instead, i have a extend-selection
that selects text inside matching pairs. (then i can delete or cop or
cut with another keystroke) Here's the code:

(defun extend-selection ()
"Highlight a region between the nearest left and right delimiters.
Delimiters are paired characters: ()[]<><<>>""''「」, including \"\".
When the function is run again, it extends the
selection to the next level of enclosing delimiters. "
 (interactive)
(if
    (or (and (eq last-command this-command) (mark t))
        (and transient-mark-mode mark-active))
    (let ((e1 (region-beginning))  (e2 (region-end)) b1 b2)
      (goto-char (- e1 1))
      (skip-chars-backward "^<>("{[「<<\"")
      (setq b1 (point))

      (goto-char (+ e2 1))
      (skip-chars-forward "^<>)"}]」>>\"")
      (setq b2 (point))
      (push-mark b1 nil t)
      )
  (progn
    (push-mark
     (save-excursion
       (skip-chars-backward "^<>("{[「<<\"")
       (point)
       ) nil t)
    (skip-chars-forward "^<>)"}]」>>\"")
    )
  )
)

This is currently assigned to Alt+7.

In the near future i'll rework the code so that, on repeated press of
Alt+7, it extend to outer matching pairs. (skiping pairs of delimiters
that's siblings) For more detail on this concept, see:

A Text Editor Feature: Syntax Tree Walk
http://xahlee.org/emacs/syntax_tree_walk.html

This concept of code selection is far more powerful (and simpler) than
existing sexp forward/backward or the whole set of paren-edit.el, in
particular when combined with a auto-lisp-formater.
(detailed at:
A Simple Lisp Code Formatter
http://xahlee.org/emacs/lisp_formatter.html
)


PS this message is posted thur groups.google.com. It in the past half
a year has been diddling with the french quote charaters "<<>>" (double
angle quotes; unicode U+00AB and U+00BB) that i use often for quation
in my posts.
For a period google groups will omit them, another period it'll
replace it with << and >>, and in the past week sometimes it'll omit
the closing char. In this post, the elisp code above also contain the
french quote. Let's see what google transformed it to this time.

  Xah
  xah@xahlee.org
\xAD\xF4 http://xahlee.org/

On Dec 4, 5:33 pm, lampshade <mwolff...@gmail.com> wrote:
> Hello,
>
> I would really like a keybinding that would allow me to delete any
> text between sexp's like {} (), etc no matter where I am between them
> and whether or not that text has spaces.
>
> For example
> {asdfdsfa asdfasdf asd}
> I would like to delete between leaving only the {} with my cursor
> inside ready to type.  So far I've been trying
> (defun delete_between ()
>         (interactive)
>         (backward-sexp)
>         (kill-sexp))
> (global-set-key [(control meta k)] 'delete_between)
>
> but that doesn't quite work.  Anyone have any improvements or help
> they could give me?
>
> Thanks in advance,
> lampshade




reply via email to

[Prev in Thread] Current Thread [Next in Thread]