all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Drew Adams" <drew.adams@oracle.com>
To: "'Esben Stien'" <b0ef@esben-stien.name>, <help-gnu-emacs@gnu.org>
Subject: RE: Select Text Inside Parentheses
Date: Sat, 1 Sep 2012 09:34:17 -0700	[thread overview]
Message-ID: <FCF3BE5E0D1247A8833B38FDCFFD095F@us.oracle.com> (raw)
In-Reply-To: <87zk5at028.fsf@quasar.esben-stien.name>

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




  reply	other threads:[~2012-09-01 16:34 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-09-01 12:11 Select Text Inside Parentheses Esben Stien
2012-09-01 16:34 ` Drew Adams [this message]
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

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=FCF3BE5E0D1247A8833B38FDCFFD095F@us.oracle.com \
    --to=drew.adams@oracle.com \
    --cc=b0ef@esben-stien.name \
    --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.