all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Select Text Inside Parentheses
@ 2012-09-01 12:11 Esben Stien
  2012-09-01 16:34 ` Drew Adams
  2012-09-04 10:17 ` Andreas Röhler
  0 siblings, 2 replies; 8+ messages in thread
From: Esben Stien @ 2012-09-01 12:11 UTC (permalink / raw)
  To: help-gnu-emacs


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



^ permalink raw reply	[flat|nested] 8+ messages in thread

* RE: Select Text Inside Parentheses
  2012-09-01 12:11 Select Text Inside Parentheses Esben Stien
@ 2012-09-01 16:34 ` Drew Adams
  2012-09-02  1:06   ` Esben Stien
  2012-09-04 10:17 ` Andreas Röhler
  1 sibling, 1 reply; 8+ messages in thread
From: Drew Adams @ 2012-09-01 16:34 UTC (permalink / raw)
  To: 'Esben Stien', help-gnu-emacs

> 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)

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.




^ permalink raw reply	[flat|nested] 8+ messages in thread

* RE: Select Text Inside Parentheses
  2012-09-02  1:06   ` Esben Stien
@ 2012-09-02  1:01     ` Drew Adams
  2012-09-02 13:17       ` Esben Stien
  0 siblings, 1 reply; 8+ messages in thread
From: Drew Adams @ 2012-09-02  1:01 UTC (permalink / raw)
  To: 'Esben Stien'; +Cc: help-gnu-emacs

> > "(\\(\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.




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Select Text Inside Parentheses
  2012-09-01 16:34 ` Drew Adams
@ 2012-09-02  1:06   ` Esben Stien
  2012-09-02  1:01     ` Drew Adams
  0 siblings, 1 reply; 8+ messages in thread
From: Esben Stien @ 2012-09-02  1:06 UTC (permalink / raw)
  To: Drew Adams; +Cc: help-gnu-emacs

"Drew Adams" <drew.adams@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



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Select Text Inside Parentheses
  2012-09-02  1:01     ` Drew Adams
@ 2012-09-02 13:17       ` Esben Stien
  2012-09-02 13:29         ` Drew Adams
  2012-09-02 14:40         ` Le Wang
  0 siblings, 2 replies; 8+ messages in thread
From: Esben Stien @ 2012-09-02 13:17 UTC (permalink / raw)
  To: Drew Adams; +Cc: help-gnu-emacs

"Drew Adams" <drew.adams@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



^ permalink raw reply	[flat|nested] 8+ messages in thread

* RE: Select Text Inside Parentheses
  2012-09-02 13:17       ` Esben Stien
@ 2012-09-02 13:29         ` Drew Adams
  2012-09-02 14:40         ` Le Wang
  1 sibling, 0 replies; 8+ messages in thread
From: Drew Adams @ 2012-09-02 13:29 UTC (permalink / raw)
  To: 'Esben Stien'; +Cc: help-gnu-emacs

> 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.




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Select Text Inside Parentheses
  2012-09-02 13:17       ` Esben Stien
  2012-09-02 13:29         ` Drew Adams
@ 2012-09-02 14:40         ` Le Wang
  1 sibling, 0 replies; 8+ messages in thread
From: Le Wang @ 2012-09-02 14:40 UTC (permalink / raw)
  To: Esben Stien; +Cc: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 565 bytes --]

On Sun, Sep 2, 2012 at 9:17 PM, Esben Stien <b0ef@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

[-- Attachment #2: Type: text/html, Size: 1150 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Select Text Inside Parentheses
  2012-09-01 12:11 Select Text Inside Parentheses Esben Stien
  2012-09-01 16:34 ` Drew Adams
@ 2012-09-04 10:17 ` Andreas Röhler
  1 sibling, 0 replies; 8+ messages in thread
From: Andreas Röhler @ 2012-09-04 10:17 UTC (permalink / raw)
  To: help-gnu-emacs

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-Emacs-Werkstatt-1.3.tar.gz



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2012-09-04 10:17 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-09-01 12:11 Select Text Inside Parentheses Esben Stien
2012-09-01 16:34 ` Drew Adams
2012-09-02  1:06   ` Esben Stien
2012-09-02  1:01     ` Drew Adams
2012-09-02 13:17       ` Esben Stien
2012-09-02 13:29         ` Drew Adams
2012-09-02 14:40         ` Le Wang
2012-09-04 10:17 ` Andreas Röhler

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.