From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Xah Newsgroups: gmane.emacs.help Subject: Re: Applying macro to lines which match regexp Date: Wed, 15 Oct 2008 14:16:12 -0700 (PDT) Organization: http://groups.google.com Message-ID: <2bb04c85-b1ae-443c-9e1d-c442e1799a79@s20g2000prd.googlegroups.com> References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1224106884 8340 80.91.229.12 (15 Oct 2008 21:41:24 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 15 Oct 2008 21:41:24 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Oct 15 23:42:23 2008 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1KqE86-0003Y7-Mg for geh-help-gnu-emacs@m.gmane.org; Wed, 15 Oct 2008 23:42:06 +0200 Original-Received: from localhost ([127.0.0.1]:49928 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KqE72-0005FB-40 for geh-help-gnu-emacs@m.gmane.org; Wed, 15 Oct 2008 17:41:00 -0400 Original-Path: news.stanford.edu!newsfeed.stanford.edu!postnews.google.com!s20g2000prd.googlegroups.com!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 78 Original-NNTP-Posting-Host: 24.6.185.159 Original-X-Trace: posting.google.com 1224105372 28715 127.0.0.1 (15 Oct 2008 21:16:12 GMT) Original-X-Complaints-To: groups-abuse@google.com Original-NNTP-Posting-Date: Wed, 15 Oct 2008 21:16:12 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: s20g2000prd.googlegroups.com; posting-host=24.6.185.159; posting-account=bRPKjQoAAACxZsR8_VPXCX27T2YcsyMA User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10_4_11; en) AppleWebKit/525.18 (KHTML, like Gecko) Version/3.1.2 Safari/525.22, gzip(gfe), gzip(gfe) Original-Xref: news.stanford.edu gnu.emacs.help:163478 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:58820 Archived-At: 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=E2=86=97, 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 =E2=80=9Cwc=E2= =80=9D, which takes input from Stdin and output a text to Stdout. (the =E2=80=9Cwc= =E2=80=9D command counts the number of words, lines, chars in the text.) For example, try this: =E2=80=9Ccat =E2=80=B9file name=E2=80=BA | wc=E2=80=9D. Here's the elisp wrapper: (defun my-process-region (startPos endPos) "Do some text processing on region. This command calls the external script =E2=80=9Cwc=E2=80=9D." (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 "") 'my-process-region) Put the above code in your =E2=80=9C.emacs=E2=80=9D 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 =E2=88=91 http://xahlee.org/ =E2=98=84