emacs evaluating

3 views
Skip to first unread message

Haris Bogdanović

unread,
Feb 6, 2009, 9:20:59 PM2/6/09
to
Hi.

When I write a function in emacs, how can I try it ?
Can I open some window (buffer) for evaluating ?

Thanks


Xah Lee

unread,
Feb 6, 2009, 10:29:16 PM2/6/09
to

select the text, and press Alt+x eval-region. Then, you can call it by
Alt+x ‹function name›. (Alt+x is shortcut for execute-extended-
command)

if your function does not have the “(interactive)” clause, then it
won't be visible to user thru execute-extended-command , but you can
still call the function by other fuction.

• Emacs Lisp Basics
http://xahlee.org/emacs/elisp_basics.html

Xah
http://xahlee.org/


Barry Margolin

unread,
Feb 6, 2009, 10:39:42 PM2/6/09
to
In article <gmir6e$cno$1...@gregory.bnet.hr>,
"Haris Bogdanović" <fbogd...@xnet.hr> wrote:

You can use the *scratch* buffer, which is in lisp-interactive-mode by
default. You can also use M-: to type an expression in the minibuffer
and evaluate it.

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***

Michael Ekstrand

unread,
Feb 6, 2009, 10:43:10 PM2/6/09
to
"Haris Bogdanoviæ" <fbogd...@xnet.hr> writes:
> When I write a function in emacs, how can I try it ?
> Can I open some window (buffer) for evaluating ?

Look at the *scratch* buffer. It defaults to a mode called 'Lisp
Interaction', where you can enter forms and then evaluate them with C-j.

- Michael

--
mouse, n: A device for pointing at the xterm in which you want to type.

Andreas Politz

unread,
Feb 7, 2009, 4:28:53 AM2/7/09
to
Michael Ekstrand wrote:
> "Haris Bogdanoviæ" <fbogd...@xnet.hr> writes:
>> When I write a function in emacs, how can I try it ?
>> Can I open some window (buffer) for evaluating ?
>
> Look at the *scratch* buffer. It defaults to a mode called 'Lisp
> Interaction', where you can enter forms and then evaluate them with C-j.
>
> - Michael
>

There is also a repl :

M-x ielm RET

-a

Haris Bogdanović

unread,
Feb 7, 2009, 7:48:52 AM2/7/09
to
When I try to compile function it gives me "make -k" command, I press enter
and I get error message.
Does emacs have lisp interpreter or I have to install it separatelly ?

"Haris Bogdanović" <fbogd...@xnet.hr> wrote in message
news:gmir6e$cno$1...@gregory.bnet.hr...

Pascal J. Bourguignon

unread,
Feb 7, 2009, 9:18:23 AM2/7/09
to
"Haris Bogdanović" <fbogd...@xnet.hr> writes:
> "Haris Bogdanović" <fbogd...@xnet.hr> wrote in message
> news:gmir6e$cno$1...@gregory.bnet.hr...
>> Hi.
>>
>> When I write a function in emacs, how can I try it ?

Emacs questions are better asked on news:gnu.emacs.help


Type C-x C-e with the point after the function source to enter it into
the emacs lisp image.

They type (your-function your-argument) C-x C-e to evaluate it.


If you type C-y C-x C-e instead, the result of the expression will be
inserted in the buffer at the point.

(+ 1 2) C-u C-x C-e --> 3
(defun f (x) (if (< x 1) 1 (* x (f (1- x))))) C-x C-e
(f 9) C-u C-x C-e --> 362880

You don't need to compile your emacs lisp functions to use them. At
least not until they're debugged.


>> Can I open some window (buffer) for evaluating ?

Yes. M-x ielm RET gives you an interactive REPL to emacs lisp.


> When I try to compile function it gives me "make -k" command, I press enter
> and I get error message.
> Does emacs have lisp interpreter or I have to install it separatelly ?


You can get information about what an emacs command purpose is with:

C-h f compile RET

As you can see, compile has nothing to do with compiling emacs lisp

code.

It could be used to compile an emacs lisp source, if you defined a
Makefile or used the emacs command to do so. For example, you could
use:

emacs --eval '(byte-compile-file "source.el")'

to compile source.el into source.elc with emacs.


But we prefer to do it more interactively in lisp:
To compile a function: (byte-compile 'your-function) C-x C-e
To compile an emacs lisp source file: M-x byte-compile-file RET source.el RET
To load an emacs lisp source: M-x load-file RET source.el RET
To evaluate a while buffer: M-x eval-buffer RET


--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
----------> http://www.netmeister.org/news/learn2quote.html <-----------
---> http://homepage.ntlworld.com/g.mccaughan/g/remarks/uquote.html <---

__Pascal Bourguignon__ http://www.informatimago.com/

Xah Lee

unread,
Feb 7, 2009, 9:36:28 AM2/7/09
to
On Feb 7, 6:18 am, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
> "Haris Bogdanoviæ" <fbogdano...@xnet.hr> writes:
> > "Haris Bogdanoviæ" <fbogdano...@xnet.hr> wrote in message

> >news:gmir6e$cno$1...@gregory.bnet.hr...
> >> Hi.
>
> >> When I write a function in emacs, how can I try it ?
>
> Emacs questions are better asked on news:gnu.emacs.help

Please feel free to post emacs lisp question in comp.lang.lisp.

There are some Common Lisp fanatics, who often sneer emacs lisp (and
any other lisps) and they often try to drive any non Common Lisp
questions outside of comp.lang.lisp.

Xah
http://xahlee.org/

Evans Winner

unread,
Feb 7, 2009, 4:29:48 PM2/7/09
to
"Haris Bogdanoviæ" <fbogd...@xnet.hr> writes:

When I try to compile function it gives me "make -k"
command, I press enter and I get error message. Does
emacs have lisp interpreter or I have to install it
separatelly ?

Lisp says, L'emacs c'est moi. And Emacs says, L'isp c'est
moi. More or less. At any rate, Emacs is a text editor
first and foremost, so its model of user interaction is
based on that. If you do want an interactive repl in the
same sense as in a scheme or Common Lisp you can, as Andreas
mentioned, do M-x ielm RET and that will give you a repl.
But typically in Emacs you are in an editing buffer so you
would just write your lisp form, and then with point after
the end-parenthesis that closes the form you want to
evaluate, do C-x C-e and that form will be evaluated,
whatever side-effects it produces will be produced, and
typically the return value will be displayed in the
mini-buffer. If you don't like the result, you just edit
the form, and do it again. You don't have to write out the
whole form again at a repl, and once you have produced a
bunch of those forms that all work together the way you
want, they're already there in the buffer, roughly in the
form that you can directly save as a file and load later.

M-x compile is designed for people writing code in C and
other languages that use external tools like `make' to build
an executable. If you run it and the current directory does
not contain a Makefile it will, not finding it, complain.
You may be looking for `byte-compile' which you can use in
lisp to compile a function. Also you can use
`byte-compile-file' interactively to byte-compile a file;
there is a way to use that in dired, as well, which is nice
when you have a lot of files you want to byte-compile. I
have never used `byte-compile' for a function, but I know
that byte-compiling a file produces a file (a .elc file)
that contains object code (or whatever it's called in Emacs
lisp) but that does not load those expressions into the lisp
image. So other useful functions are `load-file' which
actually loads the .el or .elc file into the lisp, and
`load-library' which is similar (see the docs). Finally, if
you are looking at a buffer full of lisp forms you can use
`eval-buffer' to eval all of it at once, or `eval-region'.

I hope that's helpful. Sorry to be so prolix, but this is
something that I found very confusing myself when I was
starting with Emacs, so I thought I'd try to give a full-ish
(hopefully not to be confused with fool-ish) answer.

Reply all
Reply to author
Forward
0 new messages