473,387 Members | 3,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

writing emacs commands with your fav lang

Here's a little tutorial that lets you write emacs commands for
processing the current text selection in emacs in your favorite lang.

Elisp Wrapper For Perl Scripts
http://xahlee.org/emacs/elisp_perl_wrapper.html

plain 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/


Oct 16 '08 #1
0 1230

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Alan Mackenzie | last post by:
To all those who use (X)Emacs's CC Mode to edit C, C++, Java, Objective-C, Pike, AWK or IDL: To help direct the development of CC Mode, it would be useful to find out how people use the...
10
by: _R | last post by:
I'd love to get Emacs/Epsilon emu mode back for the VS code editor. Is there any way to do this? Apparently 2005b2 has an option for "Apply the following additional keyboard mapping scheme' and...
37
by: Richard G. Riley | last post by:
I am looking for a way to integrate the C library documentation into emacs' C mode. Adding any linux library documentation would be a bonus. e.g hilite word, hotkey to library documentation. ...
1
by: lorinh | last post by:
Hi all, I've written a Python script with functionality similar to the Unix "script" program, which keeps a record of shell commands issued (I capture some additional stuff like timestamps). The...
12
by: Thomas Bartkus | last post by:
Does anyone use emacs together with both WordStar key bindings and python mode? I'm afraid that Wordstar editing key commands are burned R/O into my knuckles! I would like to play with emacs...
7
by: Xah Lee | last post by:
Summary: when encountering ex as a unit in css, FireFox (and iCab) did not take into account the font-family. Detail: http://xahlee.org/js/ff_pre_ex.html Xah xah@xahlee.org ∑...
4
by: emin.shopper | last post by:
Emacs seems to freeze when doing certain shell commands on Microsoft Windows. The following is a simple example with Xemacs: ---------------------------------------------------------- ...
5
by: levander | last post by:
I've been using pdb under emacs on an Ubuntu box to debug python programs. I just upgraded from Ubuntu Edgy to Feisty and this combo has stopped working. Python is at 2.5.1 now, and emacs is at...
331
by: Xah Lee | last post by:
http://xahlee.org/emacs/modernization.html ] The Modernization of Emacs ---------------------------------------- THE PROBLEM Emacs is a great editor. It is perhaps the most powerful and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.