Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Select Text Inside Parentheses
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  8 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Esben Stien  
View profile  
 More options Sep 1 2012, 7:34 am
Newsgroups: gnu.emacs.help
From: Esben Stien <b...@esben-stien.name>
Date: Sat, 01 Sep 2012 14:11:27 +0200
Local: Sat, Sep 1 2012 8:11 am
Subject: Select Text Inside Parentheses

I'm trying to select text between parentheses. This text is not
code. This is my block of text:

(
foo
bar
baz
)

Problem is that it selects the whole first line after the first
parentheses, so I get a whole line of white space in front of the first
character. I'd like the selection to start at the first character after
the first parentheses and end at the last character before the last
parentheses. I tried adding (delete-horizontal-space).

Any pointers as to how I can do this?.

(require 'simple)
(defun set-selection-around-parens()
  (interactive)
  (let ( (right-paren (save-excursion ; using save-excursion because
                                      ; we don't want to move the
                                      ; point.
                        (re-search-forward ")" nil t))) ; bound nil
                                                        ; no-error t
         (left-paren (save-excursion (re-search-backward "(" nil t))))
    (when (and right-paren left-paren)
      ;; this is actually a way to activate a mark
      ;; you have to move your point to one side
      (push-mark (- right-paren 1))
      (goto-char (+ left-paren 1))
      (delete-horizontal-space)
      (activate-mark)
)))

--
Esben Stien is b0ef@e     s      a            
         http://www. s     t    n m
          irc://irc.  b  -  i  .   e/%23contact
           sip:b0ef@   e     e
           jid:b0ef@    n     n


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Drew Adams  
View profile  
 More options Sep 1 2012, 12:34 pm
Newsgroups: gnu.emacs.help
From: "Drew Adams" <drew.ad...@oracle.com>
Date: Sat, 1 Sep 2012 09:34:17 -0700
Local: Sat, Sep 1 2012 12:34 pm
Subject: RE: Select Text Inside Parentheses

You never need to require `simple.el[c]'.  It is preloaded.

> (defun set-selection-around-parens()
>   (interactive)
>   (let ( (right-paren (save-excursion
>                         (re-search-forward ")" nil t)))
>          (left-paren (save-excursion (re-search-backward "(" nil t))))
>     (when (and right-paren left-paren)
>       (push-mark (- right-paren 1))
>       (goto-char (+ left-paren 1))
>       (delete-horizontal-space)
>       (activate-mark))))

Below is a quick start.  It does not try to take care of whether a parenthesized
sexp might be inside a string.

(defun foo ()
  "..."
  (interactive)
  (when (re-search-forward
         "(\\(\s-\\|[\n]\\)*\\(.+\\)\\(\s-\\|[\n]\\)*)" nil t)
    (goto-char (match-beginning 2))
    (push-mark nil 'nomsg 'activate)
    (goto-char (match-end 2))
    (setq deactivate-mark  nil)))

Someone else might have another suggestion.  Or you can tweak this.

If you do not want to select only whitespace between parens - e.g.,
for `(        )', then you might change \\(.+\\) to \\(\\S-+.*\\).
That is, "(\\(\\s-\\|[\n]\\)*\\(\\S-+.*\\)\\(\\s-\\|[\n]\\)*)".

The regexp matches `(' followed by perhaps some whitespace (including newlines):
"(\\(\\s-\\|[\n]\\)*"

Followed by a non-empty stretch of any chars "\\(.+\\)"
(or perhaps "\\(\\S-+.*\\)").

Followed by perhaps some whitespace (maybe newlines)
followed by `)': "(\\(\\s-\\|[\n]\\)*)".

The regexp's first subgroup matches the possible first stretch of whitespace.
Its second subgroup matches the text you want.  So you pick up the text from
(match-beginning 2) to (match-end 2).

Set `deactivate-mark' to nil at the end of a command where you want the mark to
remain active.

HTH.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Esben Stien  
View profile  
 More options Sep 1 2012, 8:30 pm
Newsgroups: gnu.emacs.help
From: Esben Stien <b...@esben-stien.name>
Date: Sun, 02 Sep 2012 03:06:54 +0200
Local: Sat, Sep 1 2012 9:06 pm
Subject: Re: Select Text Inside Parentheses

"Drew Adams" <drew.ad...@oracle.com> writes:
> (defun foo ()
>   "..."
>   (interactive)
>   (when (re-search-forward
>          "(\\(\s-\\|[\n]\\)*\\(.+\\)\\(\s-\\|[\n]\\)*)" nil t)

This is supposed to be "(\\(\\s-\\|[\n]\\)*\\(.+\\)\\(\\s-\\|[\n]\\)*)" nil t)
, right?. If not, it's telling me:

"Invalid modifier in string"

>     (goto-char (match-beginning 2))
>     (push-mark nil 'nomsg 'activate)
>     (goto-char (match-end 2))
>     (setq deactivate-mark  nil)))

This doesn't seem to do anything.

I go into the block:

(
foo
bar
baz
)

, but nothing seems to happen when I run this code. No selection is
made.

--
Esben Stien is b0ef@e     s      a            
         http://www. s     t    n m
          irc://irc.  b  -  i  .   e/%23contact
           sip:b0ef@   e     e
           jid:b0ef@    n     n


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Drew Adams  
View profile  
 More options Sep 1 2012, 9:02 pm
Newsgroups: gnu.emacs.help
From: "Drew Adams" <drew.ad...@oracle.com>
Date: Sat, 1 Sep 2012 18:01:40 -0700
Local: Sat, Sep 1 2012 9:01 pm
Subject: RE: Select Text Inside Parentheses

> > "(\\(\s-\\|[\n]\\)*\\(.+\\)\\(\s-\\|[\n]\\)*)" nil t)

> This is supposed to be
> "(\\(\\s-\\|[\n]\\)*\\(.+\\)\\(\\s-\\|[\n]\\)*)" nil t)
> , right?

Right, sorry.  I showed it correctly (\\s-, not \s-) in the detailed rundown
that followed.

> >     (goto-char (match-beginning 2))
> >     (push-mark nil 'nomsg 'activate)
> >     (goto-char (match-end 2))
> >     (setq deactivate-mark  nil)))

> This doesn't seem to do anything.

Did you try with \\s- instead of \s-?

What that code, which you say does nothing, does is to set the mark where the
2nd subgroup-match starts, then move point to where the 2nd subgroup-match ends,
then ensure that the mark remains active after the command.

> nothing seems to happen when I run this code.
> No selection is made.

Works for me.  Be sure you corrected the typo from \s- to \\s-.
And be sure you have `transient-mark-mode' turned on.

But again, this is not a real parsing of lists, so there is no treatment of,
say, nested lists.  The \\S-+ matches any non-whitespace chars, including ( and
).  So using it on (foo (bar))) selects "foo (bar))": everything between the
first ( and the last ).

If you want to handle lists correctly (e.g. nesting) then use the Elisp
functions for traversing or parsing lists/sexps.

And as I say, perhaps someone else will help you more, or differently.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Esben Stien  
View profile  
 More options Sep 2 2012, 8:41 am
Newsgroups: gnu.emacs.help
From: Esben Stien <b...@esben-stien.name>
Date: Sun, 02 Sep 2012 15:17:39 +0200
Local: Sun, Sep 2 2012 9:17 am
Subject: Re: Select Text Inside Parentheses

"Drew Adams" <drew.ad...@oracle.com> writes:
> I showed it correctly (\\s-, not \s-) in the detailed rundown
> that followed.

Right, this is what I do:

       (defun everything-of-interest-between-parens ()
         "..."
         (interactive)
         (when (re-search-forward
                "(\\(\\s-\\|[\n]\\)*\\(.+\\)\\(\\s-\\|[\n]\\)*)" nil t)
           (goto-char (match-beginning 2))
           (push-mark nil 'nomsg 'activate)
           (goto-char (match-end 2))
           )
         )

> What that code, which you say does nothing, does is to set the mark
> where the 2nd subgroup-match starts, then move point to where the 2nd
> subgroup-match ends, then ensure that the mark remains active after
> the command.

I've tried this on two platforms now; on my own distro, where I run
emacs-22.3.2 and on an Ubuntu 12.04 with emacs-23.3.1

I place my point on "b" in bar in this code:

(
foo
bar
baz
)

, then I M-x everything-of-interest-between-parens RET, but the point
stands at the same place and nothing is selected.

I have transient-mark-mode enabled.

> But again, this is not a real parsing of lists, so there is no treatment of,
> say, nested lists.

Nope. I have no need for that.

> And as I say, perhaps someone else will help you more, or differently.

Thank you very much for your help so far.

--
Esben Stien is b0ef@e     s      a            
         http://www. s     t    n m
          irc://irc.  b  -  i  .   e/%23contact
           sip:b0ef@   e     e
           jid:b0ef@    n     n


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Drew Adams  
View profile  
 More options Sep 2 2012, 9:30 am
Newsgroups: gnu.emacs.help
From: "Drew Adams" <drew.ad...@oracle.com>
Date: Sun, 2 Sep 2012 06:29:41 -0700
Local: Sun, Sep 2 2012 9:29 am
Subject: RE: Select Text Inside Parentheses

> I've tried this on two platforms now; on my own distro, where I run
> emacs-22.3.2 and on an Ubuntu 12.04 with emacs-23.3.1
> I place my point on "b" in bar in this code:
> (
> foo
> bar
> baz
> )

Put the cursor before the (, not after it.  Or redefine the command accordingly.
I did not hear you say that you wanted it to start with point between the
parens.  As I described, the regexp I gave looks first for a (.

Also, . matches any char except a newline.  If you want to pick up newlines
also, and not other whitespace, then change \\(.+\\) to
\\(\\(\\S-\\|[\n]\\)+\\). But if you want to pick up whitespace too, as in (foo
bar baz) or
(
foo bar
baz
)
then use just \\([^)]\\), IOW, just pick up everything up to the first ).

It depends what you want.  Read the Elisp manual's section about regexps.

. matches any char except a newline.
[\n] matches only a newline.
\\s- matches a whitespace char (but not a newline).
\\S- matches a non-whitespace, non-newline char.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Le Wang  
View profile  
 More options Sep 2 2012, 10:40 am
Newsgroups: gnu.emacs.help
From: Le Wang <l26w...@gmail.com>
Date: Sun, 2 Sep 2012 22:40:22 +0800
Local: Sun, Sep 2 2012 10:40 am
Subject: Re: Select Text Inside Parentheses

On Sun, Sep 2, 2012 at 9:17 PM, Esben Stien <b...@esben-stien.name> wrote:
> I place my point on "b" in bar in this code:

> (
> foo
> bar
> baz
> )

Hi Esben,

Someone has already given a lot of thought to the general problem you're
trying to solve, and come up with an elegant generalized solution.

See https://github.com/magnars/expand-region.el and the accompanying
screencast: http://www.youtube.com/watch?v=_RvHz3vJ3kA

My hotkey is <M-r>

With point on "b", as you describe, I press M-r twice and the region is
expanded exactly as you require.

--
Le


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andreas Röhler  
View profile  
 More options Sep 4 2012, 6:18 am
Newsgroups: gnu.emacs.help
From: Andreas Röhler <andreas.roeh...@easy-emacs.de>
Date: Tue, 04 Sep 2012 12:17:50 +0200
Local: Tues, Sep 4 2012 6:17 am
Subject: Re: Select Text Inside Parentheses
On 01.09.2012 14:11, Esben Stien wrote:

> I'm trying to select text between parentheses.

my extension for this:

C-u ar-parentize-or-copy-atpt

get it from

https://launchpad.net/s-x-emacs-werkstatt/trunk/1.3/+download/S-X-Ema...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »