unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Xah <xahlee@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: Applying macro to lines which match regexp
Date: Wed, 15 Oct 2008 14:16:12 -0700 (PDT)	[thread overview]
Message-ID: <2bb04c85-b1ae-443c-9e1d-c442e1799a79@s20g2000prd.googlegroups.com> (raw)
In-Reply-To: b107cb8f-51b0-4da5-ab1b-c33af250c9fa@b2g2000prf.googlegroups.com

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/

  reply	other threads:[~2008-10-15 21:16 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [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 [this message]
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

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

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2bb04c85-b1ae-443c-9e1d-c442e1799a79@s20g2000prd.googlegroups.com \
    --to=xahlee@gmail.com \
    --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.
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).