unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Applying macro to lines which match regexp
@ 2008-10-15 19:21 Corey Foote
  0 siblings, 0 replies; 6+ messages in thread
From: Corey Foote @ 2008-10-15 19:21 UTC (permalink / raw)
  To: help-gnu-emacs

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


I was reading through the Emacs manual today, and came across C-x C-k r (apply-macro-to-region-lines) which applies the last keyboard macro to each line that begins in the region. How would I apply the last keyboard macro to each line that begins in the region which match a certain regular expression. For example, say I was editing a Perl script and wanted to apply the last macro to all line which consist solely of a comment. For example:

1    # Print some text
2    print "foo";
3    print "foobar";
4
5    # And then a while later
6    print "foobarbaz";

I would want to apply the macro to lines 1 and 6 which match the regular expression ^\s-*#. Thanks a bunch!

Corey Foote
Toby Software
_________________________________________________________________
Stay up to date on your PC, the Web, and your mobile phone with Windows Live.
http://clk.atdmt.com/MRT/go/msnnkwxp1020093185mrt/direct/01/

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

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

* Re: Applying macro to lines which match regexp
       [not found] <mailman.1136.1224098481.25473.help-gnu-emacs@gnu.org>
@ 2008-10-15 19:54 ` Xah
  2008-10-15 21:16   ` Xah
  2008-10-15 23:36 ` Andreas Politz
  2008-10-16  0:38 ` Tim X
  2 siblings, 1 reply; 6+ messages in thread
From: Xah @ 2008-10-15 19:54 UTC (permalink / raw)
  To: help-gnu-emacs

On Oct 15, 12:21 pm, Corey Foote <coreyfo...@hotmail.com> wrote:
> I was reading through the Emacs manual today, and came across C-x C-k r (apply-macro-to-region-lines) which applies the last keyboard macro to each line that begins in the region. How would I apply the last keyboard macro to each line that begins in the region which match a certain regular expression. For example, say I was editing a Perl script and wanted to apply the last macro to all line which consist solely of a comment. For example:
>
> 1    # Print some text
> 2    print "foo";
> 3    print "foobar";
> 4
> 5    # And then a while later
> 6    print "foobarbaz";
>
> I would want to apply the macro to lines 1 and 6 which match the regular expression ^\s-*#. Thanks a bunch!

I'm not sure there's a way.

From my experience, once you knew a little elisp you almost never use
keyboard macros...

what you could do in your case is assign a keyboard shortcut to the
macro, then just use arrow keys to move to the lines you want then
press the key.

... but if you'd describe what you want to do, i'm sure me or someone
can easily create a elisp function so that you can apply it to a
region and it process just the comment lines.

Since you are familiar with perl, with a little elisp knowledge you
can write any text processing code in perl, then wrap it in elisp, so
that the elisp function works on the region by calling your perl
script for the processing. Let us know and i'll write up on how you do
this so that you could use all your existing knowledge in perl/php/
ruby/python/bash/awk and use them in emacs.

  Xah
∑ http://xahlee.org/^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Applying macro to lines which match regexp
  2008-10-15 19:54 ` Applying macro to lines which match regexp Xah
@ 2008-10-15 21:16   ` Xah
  2008-10-15 23:09     ` Parker, Matthew
  0 siblings, 1 reply; 6+ messages in thread
From: Xah @ 2008-10-15 21:16 UTC (permalink / raw)
  To: help-gnu-emacs

alright, i took sometime to write this tutorial about how to wrap
elisp around perl scripts.

It was much easier then i thought. Hope it is useful.

http://xahlee.org/emacs/elisp_perl_wrapper.html

text version follows:
-------------------------------------

Elisp Wrapper For Perl Scripts

Xah Lee, 2008-10

This page shows a example of writing a emacs lisp function that
process text on the current region, by calling a external perl script.
So that you can use your existing knowledge in a scripting language
for text processing as emacs commands.

THE PROBLEM

Elisp is great and powerful, but if you are new, it may take several
months for you to actually become productive in using it for text
processing. However, you are probably familiar with a existing
language, such as Perl, PHP, Python, Ruby. It would be great if you
can use your existing knowledge to write many text processing scripts,
and make them available in emacs as commands, so that you can just
select a section of text, press a key, then the selected text will be
transformed according to one of your script.

SOLUTION

Basically, all your elisp function has to do is to grab the current
region, then pass the text to a external program. The external program
will take the input thru Stdin↗, then produce the processed result in
Stdout. The elisp function will grab the text from the script's
Stdout, then replace the current region by that text. Lucky for us,
the elisp function shell-command-on-region already does this exactly.

For your script, its should takes input from Stdin and oput to Stdout.
For simplicity, let's assume your script is the unix program “wc”,
which takes input from Stdin and output a text to Stdout. (the “wc”
command counts the number of words, lines, chars in the text.) For
example, try this: “cat ‹file name› | wc”.

Here's the elisp wrapper:

(defun my-process-region (startPos endPos)
  "Do some text processing on region.
This command calls the external script “wc”."
(interactive "r")
  (let (scriptName)
    (setq scriptName "/usr/bin/wc") ; full path to your script
    (shell-command-on-region startPos endPos scriptName nil t nil t)
    ))

You can assign a keyboard shortcut to it:

(global-set-key (kbd "<F6>") 'my-process-region)

Put the above code in your “.emacs” then restart emacs. To use your
function, first select a region of text, then press the F6 key.

With the above, you can write many little text processing scripts in
your favorite language, and have them all available in emacs as
commands.

For how to define keyboard shortcuts with other keys, see: How to
Define Keyboard Shortcuts in Emacs.

  Xah
∑ http://xahlee.org/^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: Applying macro to lines which match regexp
  2008-10-15 21:16   ` Xah
@ 2008-10-15 23:09     ` Parker, Matthew
  0 siblings, 0 replies; 6+ messages in thread
From: Parker, Matthew @ 2008-10-15 23:09 UTC (permalink / raw)
  To: Xah, help-gnu-emacs

That will come in very handy. Thanks!

Matthew Parker

SEI  | 1 Freedom Valley Drive | Oaks, PA 19456 | p: 610-676-1279 | f: 484-676-1279 | www.seic.com
> -----Original Message-----
> From: help-gnu-emacs-bounces+mparker=seic.com@gnu.org [mailto:help-gnu-
> emacs-bounces+mparker=seic.com@gnu.org] On Behalf Of Xah
> Sent: Wednesday, October 15, 2008 5:16 PM
> To: help-gnu-emacs@gnu.org
> Subject: Re: Applying macro to lines which match regexp
> 
> alright, i took sometime to write this tutorial about how to wrap
> elisp around perl scripts.
> 
> It was much easier then i thought. Hope it is useful.
> 
> http://xahlee.org/emacs/elisp_perl_wrapper.html
> 
> text version follows:
> -------------------------------------
> 
> Elisp Wrapper For Perl Scripts
> 
> Xah Lee, 2008-10
> 
> This page shows a example of writing a emacs lisp function that
> process text on the current region, by calling a external perl script.
> So that you can use your existing knowledge in a scripting language
> for text processing as emacs commands.
> 
> THE PROBLEM
> 
> Elisp is great and powerful, but if you are new, it may take several
> months for you to actually become productive in using it for text
> processing. However, you are probably familiar with a existing
> language, such as Perl, PHP, Python, Ruby. It would be great if you
> can use your existing knowledge to write many text processing scripts,
> and make them available in emacs as commands, so that you can just
> select a section of text, press a key, then the selected text will be
> transformed according to one of your script.
> 
> SOLUTION
> 
> Basically, all your elisp function has to do is to grab the current
> region, then pass the text to a external program. The external program
> will take the input thru Stdin↗, then produce the processed result in
> Stdout. The elisp function will grab the text from the script's
> Stdout, then replace the current region by that text. Lucky for us,
> the elisp function shell-command-on-region already does this exactly.
> 
> For your script, its should takes input from Stdin and oput to Stdout.
> For simplicity, let's assume your script is the unix program “wc”,
> which takes input from Stdin and output a text to Stdout. (the “wc”
> command counts the number of words, lines, chars in the text.) For
> example, try this: “cat ‹file name› | wc”.
> 
> Here's the elisp wrapper:
> 
> (defun my-process-region (startPos endPos)
>   "Do some text processing on region.
> This command calls the external script “wc”."
> (interactive "r")
>   (let (scriptName)
>     (setq scriptName "/usr/bin/wc") ; full path to your script
>     (shell-command-on-region startPos endPos scriptName nil t nil t)
>     ))
> 
> You can assign a keyboard shortcut to it:
> 
> (global-set-key (kbd "<F6>") 'my-process-region)
> 
> Put the above code in your “.emacs” then restart emacs. To use your
> function, first select a region of text, then press the F6 key.
> 
> With the above, you can write many little text processing scripts in
> your favorite language, and have them all available in emacs as
> commands.
> 
> For how to define keyboard shortcuts with other keys, see: How to
> Define Keyboard Shortcuts in Emacs.
> 
>   Xah
> ∑ http://xahlee.org/
> 
> ☄

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

* Re: Applying macro to lines which match regexp
       [not found] <mailman.1136.1224098481.25473.help-gnu-emacs@gnu.org>
  2008-10-15 19:54 ` Applying macro to lines which match regexp Xah
@ 2008-10-15 23:36 ` Andreas Politz
  2008-10-16  0:38 ` Tim X
  2 siblings, 0 replies; 6+ messages in thread
From: Andreas Politz @ 2008-10-15 23:36 UTC (permalink / raw)
  To: help-gnu-emacs

Corey Foote wrote:
> I was reading through the Emacs manual today, and came across C-x C-k r (apply-macro-to-region-lines) which applies the last keyboard macro to each line that begins in the region. How would I apply the last keyboard macro to each line that begins in the region which match a certain regular expression. For example, say I was editing a Perl script and wanted to apply the last macro to all line which consist solely of a comment. For example:
> 
> 1    # Print some text
> 2    print "foo";
> 3    print "foobar";
> 4
> 5    # And then a while later
> 6    print "foobarbaz";
> 
> I would want to apply the macro to lines 1 and 6 which match the regular expression ^\s-*#. Thanks a bunch!
> 
> Corey Foote
> Toby Software

Bind the macro, e.g. with C-x C-k b 1 , and create another new macro
that does the searching part and then calls macro-1.
Or edit your macro (C-x C-k e) and prepend some searching `code'.

-ap


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

* Re: Applying macro to lines which match regexp
       [not found] <mailman.1136.1224098481.25473.help-gnu-emacs@gnu.org>
  2008-10-15 19:54 ` Applying macro to lines which match regexp Xah
  2008-10-15 23:36 ` Andreas Politz
@ 2008-10-16  0:38 ` Tim X
  2 siblings, 0 replies; 6+ messages in thread
From: Tim X @ 2008-10-16  0:38 UTC (permalink / raw)
  To: help-gnu-emacs

Corey Foote <coreyfoote@hotmail.com> writes:

> I was reading through the Emacs manual today, and came across C-x C-k r
> (apply-macro-to-region-lines) which applies the last keyboard macro to each
> line that begins in the region. How would I apply the last keyboard macro to
> each line that begins in the region which match a certain regular expression.
> For example, say I was editing a Perl script and wanted to apply the last
> macro to all line which consist solely of a comment. For example:
>
> 1    # Print some text
> 2    print "foo";
> 3    print "foobar";
> 4
> 5    # And then a while later
> 6    print "foobarbaz";
>
> I would want to apply the macro to lines 1 and 6 which match the regular
> expression ^\s-*#. Thanks a bunch!
>
> Corey Foote
> Toby Software
> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
> Stay up to date on your PC, the Web, and your mobile phone with Windows Live. 
> See Now
>

I'm not sure there is a command to do that 'out of the box'. Normally,
I'd probably write a function to do it. A couple of ideas though -

1. You could include in your macro definition an isearch command to find
the line you are interested in. 

2. See if you can find the package map-lines.el by Andreas Fuchs (part
of the emacs-goodies package in Debian). This allows you to apply a
command to a set of lines matching a regexp. 

3. Write something in elisp using the various map* funcitons.

HTH

Tim



-- 
tcross (at) rapttech dot com dot au


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

end of thread, other threads:[~2008-10-16  0:38 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.1136.1224098481.25473.help-gnu-emacs@gnu.org>
2008-10-15 19:54 ` Applying macro to lines which match regexp Xah
2008-10-15 21:16   ` Xah
2008-10-15 23:09     ` Parker, Matthew
2008-10-15 23:36 ` Andreas Politz
2008-10-16  0:38 ` Tim X
2008-10-15 19:21 Corey Foote

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).