*** empty log message ***
[bpt/emacs.git] / man / building.texi
CommitLineData
6bf7aab6 1@c This is part of the Emacs manual.
739a80b3 2@c Copyright (C) 1985,86,87,93,94,95,97,2000,2001 Free Software Foundation, Inc.
6bf7aab6 3@c See file emacs.texi for copying conditions.
ffb1af2b 4@node Building, Maintaining, Programs, Top
6bf7aab6
DL
5@chapter Compiling and Testing Programs
6@cindex building programs
7@cindex program building
8@cindex running Lisp functions
9
10 The previous chapter discusses the Emacs commands that are useful for
11making changes in programs. This chapter deals with commands that assist
12in the larger process of developing and maintaining programs.
13
14@menu
15* Compilation:: Compiling programs in languages other
16 than Lisp (C, Pascal, etc.).
17* Grep Searching:: Running grep as if it were a compiler.
18* Compilation Mode:: The mode for visiting compiler errors.
19* Compilation Shell:: Customizing your shell properly
20 for use in the compilation buffer.
21* Debuggers:: Running symbolic debuggers for non-Lisp programs.
22* Executing Lisp:: Various modes for editing Lisp programs,
23 with different facilities for running
24 the Lisp programs.
25* Libraries: Lisp Libraries. Creating Lisp programs to run in Emacs.
26* Interaction: Lisp Interaction. Executing Lisp in an Emacs buffer.
27* Eval: Lisp Eval. Executing a single Lisp expression in Emacs.
28* External Lisp:: Communicating through Emacs with a separate Lisp.
29@end menu
30
31@node Compilation
32@section Running Compilations under Emacs
33@cindex inferior process
34@cindex make
35@cindex compilation errors
36@cindex error log
37
38 Emacs can run compilers for noninteractive languages such as C and
39Fortran as inferior processes, feeding the error log into an Emacs buffer.
40It can also parse the error messages and show you the source lines where
41compilation errors occurred.
42
43@table @kbd
44@item M-x compile
74b1aac1 45Run a compiler asynchronously under Emacs, with error messages going to
58fa012d 46the @samp{*compilation*} buffer.
6bf7aab6
DL
47@item M-x grep
48Run @code{grep} asynchronously under Emacs, with matching lines
49listed in the buffer named @samp{*grep*}.
50@item M-x grep-find
51Run @code{grep} via @code{find}, with user-specified arguments, and
52collect output in the buffer named @samp{*grep*}.
53@item M-x kill-compilation
54@itemx M-x kill-grep
55Kill the running compilation or @code{grep} subprocess.
56@end table
57
58@findex compile
59 To run @code{make} or another compilation command, do @kbd{M-x
60compile}. This command reads a shell command line using the minibuffer,
61and then executes the command in an inferior shell, putting output in
62the buffer named @samp{*compilation*}. The current buffer's default
63directory is used as the working directory for the execution of the
64command; normally, therefore, the compilation happens in this
65directory.
66
67@vindex compile-command
68 When the shell command line is read, the minibuffer appears containing
69a default command line, which is the command you used the last time you
70did @kbd{M-x compile}. If you type just @key{RET}, the same command
71line is used again. For the first @kbd{M-x compile}, the default is
72@samp{make -k}. The default compilation command comes from the variable
73@code{compile-command}; if the appropriate compilation command for a
74file is something other than @samp{make -k}, it can be useful for the
75file to specify a local value for @code{compile-command} (@pxref{File
76Variables}).
77
78 Starting a compilation displays the buffer @samp{*compilation*} in
79another window but does not select it. The buffer's mode line tells you
80whether compilation is finished, with the word @samp{run} or @samp{exit}
81inside the parentheses. You do not have to keep this buffer visible;
82compilation continues in any case. While a compilation is going on, the
83string @samp{Compiling} appears in the mode lines of all windows. When
84this string disappears, the compilation is finished.
85
86 If you want to watch the compilation transcript as it appears, switch
87to the @samp{*compilation*} buffer and move point to the end of the
88buffer. When point is at the end, new compilation output is inserted
89above point, which remains at the end. If point is not at the end of
90the buffer, it remains fixed while more compilation output is added at
91the end of the buffer.
92
09e58ba6 93@cindex compilation buffer, keeping current position at the end
6bf7aab6
DL
94@vindex compilation-scroll-output
95 If you set the variable @code{compilation-scroll-output} to a
96non-@code{nil} value, then the compilation buffer always scrolls to
97follow output as it comes in.
98
99@findex kill-compilation
100 To kill the compilation process, do @kbd{M-x kill-compilation}. When
101the compiler process terminates, the mode line of the
102@samp{*compilation*} buffer changes to say @samp{signal} instead of
103@samp{run}. Starting a new compilation also kills any running
104compilation, as only one can exist at any time. However, @kbd{M-x
105compile} asks for confirmation before actually killing a compilation
106that is running.
107
108@node Grep Searching
109@section Searching with Grep under Emacs
110
111@findex grep
112 Just as you can run a compiler from Emacs and then visit the lines
113where there were compilation errors, you can also run @code{grep} and
114then visit the lines on which matches were found. This works by
115treating the matches reported by @code{grep} as if they were ``errors.''
116
117 To do this, type @kbd{M-x grep}, then enter a command line that
118specifies how to run @code{grep}. Use the same arguments you would give
119@code{grep} when running it normally: a @code{grep}-style regexp
120(usually in single-quotes to quote the shell's special characters)
121followed by file names, which may use wildcards. The output from
122@code{grep} goes in the @samp{*grep*} buffer. You can find the
123corresponding lines in the original files using @kbd{C-x `} and
124@key{RET}, as with compilation errors.
125
126 If you specify a prefix argument for @kbd{M-x grep}, it figures out
127the tag (@pxref{Tags}) around point, and puts that into the default
128@code{grep} command.
129
130@findex grep-find
131 The command @kbd{M-x grep-find} is similar to @kbd{M-x grep}, but it
132supplies a different initial default for the command---one that runs
133both @code{find} and @code{grep}, so as to search every file in a
134directory tree. See also the @code{find-grep-dired} command,
135in @ref{Dired and Find}.
136
137@node Compilation Mode
138@section Compilation Mode
139
140@findex compile-goto-error
141@cindex Compilation mode
142@cindex mode, Compilation
143 The @samp{*compilation*} buffer uses a special major mode, Compilation
144mode, whose main feature is to provide a convenient way to look at the
145source line where the error happened.
146
09e58ba6
EZ
147 If you set the variable @code{compilation-scroll-output} to a
148non-@code{nil} value, then the compilation buffer always scrolls to
149follow output as it comes in.
150
6bf7aab6
DL
151@table @kbd
152@item C-x `
153Visit the locus of the next compiler error message or @code{grep} match.
154@item @key{RET}
155Visit the locus of the error message that point is on.
156This command is used in the compilation buffer.
157@item Mouse-2
158Visit the locus of the error message that you click on.
159@end table
160
161@kindex C-x `
162@findex next-error
163 You can visit the source for any particular error message by moving
58fa012d
EZ
164point in the @samp{*compilation*} buffer to that error message and
165typing @key{RET} (@code{compile-goto-error}). Alternatively, you can
166click @kbd{Mouse-2} on the error message; you need not switch to the
167@samp{*compilation*} buffer first.
6bf7aab6
DL
168
169 To parse the compiler error messages sequentially, type @kbd{C-x `}
170(@code{next-error}). The character following the @kbd{C-x} is the
171backquote or ``grave accent,'' not the single-quote. This command is
172available in all buffers, not just in @samp{*compilation*}; it displays
173the next error message at the top of one window and source location of
174the error in another window.
175
176 The first time @kbd{C-x `} is used after the start of a compilation,
177it moves to the first error's location. Subsequent uses of @kbd{C-x `}
178advance down to subsequent errors. If you visit a specific error
179message with @key{RET} or @kbd{Mouse-2}, subsequent @kbd{C-x `}
180commands advance from there. When @kbd{C-x `} gets to the end of the
181buffer and finds no more error messages to visit, it fails and signals
182an Emacs error.
183
184 @kbd{C-u C-x `} starts scanning from the beginning of the compilation
185buffer. This is one way to process the same set of errors again.
186
0825052e
EZ
187@vindex compilation-error-regexp-alist
188@vindex grep-regexp-alist
189 To parse messages from the compiler, Compilation mode uses the
190variable @code{compilation-error-regexp-alist} which lists various
191formats of error messages and tells Emacs how to extract the source file
192and the line number from the text of a message. If your compiler isn't
193supported, you can tailor Compilation mode to it by adding elements to
194that list. A similar variable @code{grep-regexp-alist} tells Emacs how
195to parse output of a @code{grep} command.
196
6bf7aab6
DL
197 Compilation mode also redefines the keys @key{SPC} and @key{DEL} to
198scroll by screenfuls, and @kbd{M-n} and @kbd{M-p} to move to the next or
199previous error message. You can also use @kbd{M-@{} and @kbd{M-@}} to
200move up or down to an error message for a different source file.
201
202 The features of Compilation mode are also available in a minor mode
203called Compilation Minor mode. This lets you parse error messages in
204any buffer, not just a normal compilation output buffer. Type @kbd{M-x
205compilation-minor-mode} to enable the minor mode. This defines the keys
206@key{RET} and @kbd{Mouse-2}, as in the Compilation major mode.
207
208 Compilation minor mode works in any buffer, as long as the contents
209are in a format that it understands. In an Rlogin buffer (@pxref{Remote
210Host}), Compilation minor mode automatically accesses remote source
211files by FTP (@pxref{File Names}).
212
213@node Compilation Shell
214@section Subshells for Compilation
215
216 Emacs uses a shell to run the compilation command, but specifies
217the option for a noninteractive shell. This means, in particular, that
218the shell should start with no prompt. If you find your usual shell
219prompt making an unsightly appearance in the @samp{*compilation*}
220buffer, it means you have made a mistake in your shell's init file by
221setting the prompt unconditionally. (This init file's name may be
222@file{.bashrc}, @file{.profile}, @file{.cshrc}, @file{.shrc}, or various
223other things, depending on the shell you use.) The shell init file
224should set the prompt only if there already is a prompt. In csh, here
225is how to do it:
226
227@example
228if ($?prompt) set prompt = @dots{}
229@end example
230
231@noindent
232And here's how to do it in bash:
233
234@example
235if [ "$@{PS1+set@}" = set ]
236then PS1=@dots{}
237fi
238@end example
239
240 There may well be other things that your shell's init file
241ought to do only for an interactive shell. You can use the same
242method to conditionalize them.
243
244 The MS-DOS ``operating system'' does not support asynchronous
245subprocesses; to work around this lack, @kbd{M-x compile} runs the
246compilation command synchronously on MS-DOS. As a consequence, you must
247wait until the command finishes before you can do anything else in
248Emacs. @xref{MS-DOS}.
249
250@node Debuggers
251@section Running Debuggers Under Emacs
252@cindex debuggers
253@cindex GUD library
254@cindex GDB
255@cindex DBX
256@cindex SDB
257@cindex XDB
258@cindex Perldb
259@cindex JDB
260@cindex PDB
261
262@c Do you believe in GUD?
263The GUD (Grand Unified Debugger) library provides an interface to
264various symbolic debuggers from within Emacs. We recommend the debugger
265GDB, which is free software, but you can also run DBX, SDB or XDB if you
266have them. GUD can also serve as an interface to the Perl's debugging
267mode, the Python debugger PDB, and to JDB, the Java Debugger.
d952abde
RS
268@xref{Debugger,, The Lisp Debugger, elisp, the Emacs Lisp Reference Manual},
269for information on debugging Emacs Lisp programs.
6bf7aab6
DL
270
271@menu
272* Starting GUD:: How to start a debugger subprocess.
273* Debugger Operation:: Connection between the debugger and source buffers.
274* Commands of GUD:: Key bindings for common commands.
275* GUD Customization:: Defining your own commands for GUD.
c0d8ceaa 276* GUD Tooltips:: Showing variable values by pointing with the mouse.
6bf7aab6
DL
277@end menu
278
279@node Starting GUD
280@subsection Starting GUD
281
282 There are several commands for starting a debugger, each corresponding
283to a particular debugger program.
284
285@table @kbd
286@item M-x gdb @key{RET} @var{file} @key{RET}
287@findex gdb
4125ceb0 288Run GDB as a subprocess of Emacs. This command creates a buffer
58fa012d
EZ
289for input and output to GDB, and switches to it. If a GDB buffer
290already exists, it just switches to that buffer.
6bf7aab6
DL
291
292@item M-x dbx @key{RET} @var{file} @key{RET}
293@findex dbx
4125ceb0 294Similar, but run DBX instead of GDB.
6bf7aab6
DL
295
296@item M-x xdb @key{RET} @var{file} @key{RET}
297@findex xdb
298@vindex gud-xdb-directories
4125ceb0 299Similar, but run XDB instead of GDB. Use the variable
6bf7aab6
DL
300@code{gud-xdb-directories} to specify directories to search for source
301files.
302
303@item M-x sdb @key{RET} @var{file} @key{RET}
304@findex sdb
4125ceb0 305Similar, but run SDB instead of GDB.
6bf7aab6
DL
306
307 Some versions of SDB do not mention source file names in their
308messages. When you use them, you need to have a valid tags table
309(@pxref{Tags}) in order for GUD to find functions in the source code.
310If you have not visited a tags table or the tags table doesn't list one
311of the functions, you get a message saying @samp{The sdb support
312requires a valid tags table to work}. If this happens, generate a valid
313tags table in the working directory and try again.
314
315@item M-x perldb @key{RET} @var{file} @key{RET}
316@findex perldb
317Run the Perl interpreter in debug mode to debug @var{file}, a Perl program.
318
319@item M-x jdb @key{RET} @var{file} @key{RET}
320@findex jdb
321Run the Java debugger to debug @var{file}.
322
323@item M-x pdb @key{RET} @var{file} @key{RET}
324@findex pdb
325Run the Python debugger to debug @var{file}.
326@end table
327
328 Each of these commands takes one argument: a command line to invoke
329the debugger. In the simplest case, specify just the name of the
330executable file you want to debug. You may also use options that the
331debugger supports. However, shell wildcards and variables are not
332allowed. GUD assumes that the first argument not starting with a
333@samp{-} is the executable file name.
334
335 Emacs can only run one debugger process at a time.
336
337@node Debugger Operation
338@subsection Debugger Operation
339
340 When you run a debugger with GUD, the debugger uses an Emacs buffer
341for its ordinary input and output. This is called the GUD buffer. The
342debugger displays the source files of the program by visiting them in
343Emacs buffers. An arrow (@samp{=>}) in one of these buffers indicates
9c8599ca
DL
344the current execution line.@footnote{Under a window system the arrow is
345displayed in the marginal area of the Emacs window.} Moving point in
346this buffer does not move the arrow.
6bf7aab6
DL
347
348 You can start editing these source files at any time in the buffers
58fa012d 349that display them. The arrow is not part of the file's
6bf7aab6
DL
350text; it appears only on the screen. If you do modify a source file,
351keep in mind that inserting or deleting lines will throw off the arrow's
352positioning; GUD has no way of figuring out which line corresponded
353before your changes to the line number in a debugger message. Also,
354you'll typically have to recompile and restart the program for your
355changes to be reflected in the debugger's tables.
356
357 If you wish, you can control your debugger process entirely through the
358debugger buffer, which uses a variant of Shell mode. All the usual
359commands for your debugger are available, and you can use the Shell mode
360history commands to repeat them. @xref{Shell Mode}.
361
362@node Commands of GUD
363@subsection Commands of GUD
364
365 The GUD interaction buffer uses a variant of Shell mode, so the
366commands of Shell mode are available (@pxref{Shell Mode}). GUD mode
367also provides commands for setting and clearing breakpoints, for
368selecting stack frames, and for stepping through the program. These
369commands are available both in the GUD buffer and globally, but with
370different key bindings.
371
58fa012d
EZ
372 The breakpoint commands are normally used in source file buffers,
373because that is the easiest way to specify where to set or clear the
374breakpoint. Here's the global command to set a breakpoint:
6bf7aab6
DL
375
376@table @kbd
377@item C-x @key{SPC}
378@kindex C-x SPC
379Set a breakpoint on the source line that point is on.
380@end table
381
382@kindex C-x C-a @r{(GUD)}
383 Here are the other special commands provided by GUD. The keys
384starting with @kbd{C-c} are available only in the GUD interaction
385buffer. The key bindings that start with @kbd{C-x C-a} are available in
386the GUD interaction buffer and also in source files.
387
388@table @kbd
389@item C-c C-l
390@kindex C-c C-l @r{(GUD)}
391@itemx C-x C-a C-l
392@findex gud-refresh
393Display in another window the last line referred to in the GUD
394buffer (that is, the line indicated in the last location message).
395This runs the command @code{gud-refresh}.
396
397@item C-c C-s
398@kindex C-c C-s @r{(GUD)}
399@itemx C-x C-a C-s
400@findex gud-step
401Execute a single line of code (@code{gud-step}). If the line contains
402a function call, execution stops after entering the called function.
403
404@item C-c C-n
405@kindex C-c C-n @r{(GUD)}
406@itemx C-x C-a C-n
407@findex gud-next
408Execute a single line of code, stepping across entire function calls
409at full speed (@code{gud-next}).
410
411@item C-c C-i
412@kindex C-c C-i @r{(GUD)}
413@itemx C-x C-a C-i
414@findex gud-stepi
415Execute a single machine instruction (@code{gud-stepi}).
416
417@need 3000
418@item C-c C-r
419@kindex C-c C-r @r{(GUD)}
420@itemx C-x C-a C-r
421@findex gud-cont
422Continue execution without specifying any stopping point. The program
423will run until it hits a breakpoint, terminates, or gets a signal that
424the debugger is checking for (@code{gud-cont}).
425
426@need 1000
427@item C-c C-d
428@kindex C-c C-d @r{(GUD)}
429@itemx C-x C-a C-d
430@findex gud-remove
431Delete the breakpoint(s) on the current source line, if any
432(@code{gud-remove}). If you use this command in the GUD interaction
433buffer, it applies to the line where the program last stopped.
434
435@item C-c C-t
436@kindex C-c C-t @r{(GUD)}
437@itemx C-x C-a C-t
438@findex gud-tbreak
439Set a temporary breakpoint on the current source line, if any.
440If you use this command in the GUD interaction buffer,
441it applies to the line where the program last stopped.
442@end table
443
444 The above commands are common to all supported debuggers. If you are
445using GDB or (some versions of) DBX, these additional commands are available:
446
447@table @kbd
448@item C-c <
449@kindex C-c < @r{(GUD)}
450@itemx C-x C-a <
451@findex gud-up
452Select the next enclosing stack frame (@code{gud-up}). This is
453equivalent to the @samp{up} command.
454
455@item C-c >
456@kindex C-c > @r{(GUD)}
457@itemx C-x C-a >
458@findex gud-down
459Select the next inner stack frame (@code{gud-down}). This is
460equivalent to the @samp{down} command.
461@end table
462
463 If you are using GDB, these additional key bindings are available:
464
465@table @kbd
466@item @key{TAB}
467@kindex TAB @r{(GUD)}
468@findex gud-gdb-complete-command
469With GDB, complete a symbol name (@code{gud-gdb-complete-command}).
470This key is available only in the GUD interaction buffer, and requires
471GDB versions 4.13 and later.
472
473@item C-c C-f
474@kindex C-c C-f @r{(GUD)}
475@itemx C-x C-a C-f
476@findex gud-finish
477Run the program until the selected stack frame returns (or until it
478stops for some other reason).
479@end table
480
481 These commands interpret a numeric argument as a repeat count, when
482that makes sense.
483
484 Because @key{TAB} serves as a completion command, you can't use it to
485enter a tab as input to the program you are debugging with GDB.
486Instead, type @kbd{C-q @key{TAB}} to enter a tab.
487
488@node GUD Customization
489@subsection GUD Customization
490
491@vindex gdb-mode-hook
492@vindex dbx-mode-hook
493@vindex sdb-mode-hook
494@vindex xdb-mode-hook
495@vindex perldb-mode-hook
496@vindex pdb-mode-hook
497@vindex jdb-mode-hook
498 On startup, GUD runs one of the following hooks: @code{gdb-mode-hook},
499if you are using GDB; @code{dbx-mode-hook}, if you are using DBX;
500@code{sdb-mode-hook}, if you are using SDB; @code{xdb-mode-hook}, if you
501are using XDB; @code{perldb-mode-hook}, for Perl debugging mode;
74b1aac1 502@code{pdb-mode-hook}, for PDB; @code{jdb-mode-hook}, for JDB. You can
6bf7aab6
DL
503use these hooks to define custom key bindings for the debugger
504interaction buffer. @xref{Hooks}.
505
506 Here is a convenient way to define a command that sends a particular
507command string to the debugger, and set up a key binding for it in the
508debugger interaction buffer:
509
510@findex gud-def
511@example
512(gud-def @var{function} @var{cmdstring} @var{binding} @var{docstring})
513@end example
514
515 This defines a command named @var{function} which sends
516@var{cmdstring} to the debugger process, and gives it the documentation
7fb4961c 517string @var{docstring}. You can then use the command @var{function} in any
6bf7aab6
DL
518buffer. If @var{binding} is non-@code{nil}, @code{gud-def} also binds
519the command to @kbd{C-c @var{binding}} in the GUD buffer's mode and to
520@kbd{C-x C-a @var{binding}} generally.
521
522 The command string @var{cmdstring} may contain certain
523@samp{%}-sequences that stand for data to be filled in at the time
524@var{function} is called:
525
526@table @samp
527@item %f
528The name of the current source file. If the current buffer is the GUD
529buffer, then the ``current source file'' is the file that the program
530stopped in.
531@c This said, ``the name of the file the program counter was in at the last breakpoint.''
532@c But I suspect it is really the last stop file.
533
534@item %l
535The number of the current source line. If the current buffer is the GUD
536buffer, then the ``current source line'' is the line that the program
537stopped in.
538
539@item %e
540The text of the C lvalue or function-call expression at or adjacent to point.
541
542@item %a
543The text of the hexadecimal address at or adjacent to point.
544
545@item %p
546The numeric argument of the called function, as a decimal number. If
547the command is used without a numeric argument, @samp{%p} stands for the
548empty string.
549
550If you don't use @samp{%p} in the command string, the command you define
551ignores any numeric argument.
552@end table
553
c0d8ceaa
DL
554@node GUD Tooltips
555@subsection GUD Tooltips
556
557@cindex tooltips with GUD
558The Tooltip facility (@pxref{Tooltips}) provides support for GUD@. If
559GUD support is activated by customizing the @code{tooltip} group,
560variable values can be displayed in tooltips by pointing at them with
561the mouse in the GUD buffer or in source buffers with major modes in the
562customizable list @code{tooltip-gud-modes}.
563
6bf7aab6
DL
564@node Executing Lisp
565@section Executing Lisp Expressions
566
567 Emacs has several different major modes for Lisp and Scheme. They are
568the same in terms of editing commands, but differ in the commands for
569executing Lisp expressions. Each mode has its own purpose.
570
571@table @asis
572@item Emacs-Lisp mode
573The mode for editing source files of programs to run in Emacs Lisp.
574This mode defines @kbd{C-M-x} to evaluate the current defun.
575@xref{Lisp Libraries}.
576@item Lisp Interaction mode
577The mode for an interactive session with Emacs Lisp. It defines
578@kbd{C-j} to evaluate the sexp before point and insert its value in the
579buffer. @xref{Lisp Interaction}.
580@item Lisp mode
581The mode for editing source files of programs that run in Lisps other
582than Emacs Lisp. This mode defines @kbd{C-M-x} to send the current defun
583to an inferior Lisp process. @xref{External Lisp}.
584@item Inferior Lisp mode
585The mode for an interactive session with an inferior Lisp process.
586This mode combines the special features of Lisp mode and Shell mode
587(@pxref{Shell Mode}).
588@item Scheme mode
589Like Lisp mode but for Scheme programs.
590@item Inferior Scheme mode
591The mode for an interactive session with an inferior Scheme process.
592@end table
593
594 Most editing commands for working with Lisp programs are in fact
595available globally. @xref{Programs}.
596
597@node Lisp Libraries
598@section Libraries of Lisp Code for Emacs
599@cindex libraries
600@cindex loading Lisp code
601
602 Lisp code for Emacs editing commands is stored in files whose names
603conventionally end in @file{.el}. This ending tells Emacs to edit them in
604Emacs-Lisp mode (@pxref{Executing Lisp}).
605
606@findex load-file
607 To execute a file of Emacs Lisp code, use @kbd{M-x load-file}. This
608command reads a file name using the minibuffer and then executes the
609contents of that file as Lisp code. It is not necessary to visit the
610file first; in any case, this command reads the file as found on disk,
611not text in an Emacs buffer.
612
613@findex load
614@findex load-library
615 Once a file of Lisp code is installed in the Emacs Lisp library
616directories, users can load it using @kbd{M-x load-library}. Programs can
617load it by calling @code{load-library}, or with @code{load}, a more primitive
618function that is similar but accepts some additional arguments.
619
620 @kbd{M-x load-library} differs from @kbd{M-x load-file} in that it
621searches a sequence of directories and tries three file names in each
622directory. Suppose your argument is @var{lib}; the three names are
623@file{@var{lib}.elc}, @file{@var{lib}.el}, and lastly just
624@file{@var{lib}}. If @file{@var{lib}.elc} exists, it is by convention
625the result of compiling @file{@var{lib}.el}; it is better to load the
626compiled file, since it will load and run faster.
627
628 If @code{load-library} finds that @file{@var{lib}.el} is newer than
48dbca2c 629@file{@var{lib}.elc} file, it issues a warning, because it's likely that
6bf7aab6
DL
630somebody made changes to the @file{.el} file and forgot to recompile
631it.
632
633 Because the argument to @code{load-library} is usually not in itself
634a valid file name, file name completion is not available. Indeed, when
635using this command, you usually do not know exactly what file name
636will be used.
637
638@vindex load-path
639 The sequence of directories searched by @kbd{M-x load-library} is
640specified by the variable @code{load-path}, a list of strings that are
641directory names. The default value of the list contains the directory where
642the Lisp code for Emacs itself is stored. If you have libraries of
643your own, put them in a single directory and add that directory
644to @code{load-path}. @code{nil} in this list stands for the current default
645directory, but it is probably not a good idea to put @code{nil} in the
646list. If you find yourself wishing that @code{nil} were in the list,
647most likely what you really want to do is use @kbd{M-x load-file}
648this once.
649
650@cindex autoload
651 Often you do not have to give any command to load a library, because
652the commands defined in the library are set up to @dfn{autoload} that
653library. Trying to run any of those commands calls @code{load} to load
654the library; this replaces the autoload definitions with the real ones
655from the library.
656
657@cindex byte code
658 Emacs Lisp code can be compiled into byte-code which loads faster,
659takes up less space when loaded, and executes faster. @xref{Byte
660Compilation,, Byte Compilation, elisp, the Emacs Lisp Reference Manual}.
661By convention, the compiled code for a library goes in a separate file
662whose name consists of the library source file with @samp{c} appended.
663Thus, the compiled code for @file{foo.el} goes in @file{foo.elc}.
664That's why @code{load-library} searches for @samp{.elc} files first.
665
493c59e0
EZ
666@vindex load-dangerous-libraries
667@cindex Lisp files byte-compiled by XEmacs
a50c7a80
RS
668 By default, Emacs refuses to load compiled Lisp files which were
669compiled with XEmacs, a modified versions of Emacs---they can cause
670Emacs to crash. Set the variable @code{load-dangerous-libraries} to
671@code{t} if you want to try loading them.
493c59e0 672
6bf7aab6
DL
673@node Lisp Eval
674@section Evaluating Emacs-Lisp Expressions
675@cindex Emacs-Lisp mode
676@cindex mode, Emacs-Lisp
677
678@findex emacs-lisp-mode
679 Lisp programs intended to be run in Emacs should be edited in
680Emacs-Lisp mode; this happens automatically for file names ending in
681@file{.el}. By contrast, Lisp mode itself is used for editing Lisp
682programs intended for other Lisp systems. To switch to Emacs-Lisp mode
683explicitly, use the command @kbd{M-x emacs-lisp-mode}.
684
685 For testing of Lisp programs to run in Emacs, it is often useful to
686evaluate part of the program as it is found in the Emacs buffer. For
687example, after changing the text of a Lisp function definition,
688evaluating the definition installs the change for future calls to the
689function. Evaluation of Lisp expressions is also useful in any kind of
690editing, for invoking noninteractive functions (functions that are
691not commands).
692
693@table @kbd
694@item M-:
695Read a single Lisp expression in the minibuffer, evaluate it, and print
696the value in the echo area (@code{eval-expression}).
697@item C-x C-e
698Evaluate the Lisp expression before point, and print the value in the
699echo area (@code{eval-last-sexp}).
700@item C-M-x
701Evaluate the defun containing or after point, and print the value in
702the echo area (@code{eval-defun}).
703@item M-x eval-region
704Evaluate all the Lisp expressions in the region.
705@item M-x eval-current-buffer
706Evaluate all the Lisp expressions in the buffer.
707@end table
708
709@kindex M-:
710@findex eval-expression
711 @kbd{M-:} (@code{eval-expression}) is the most basic command for evaluating
712a Lisp expression interactively. It reads the expression using the
713minibuffer, so you can execute any expression on a buffer regardless of
714what the buffer contains. When the expression is evaluated, the current
715buffer is once again the buffer that was current when @kbd{M-:} was
716typed.
717
718@kindex C-M-x @r{(Emacs-Lisp mode)}
719@findex eval-defun
720 In Emacs-Lisp mode, the key @kbd{C-M-x} is bound to the command
721@code{eval-defun}, which parses the defun containing or following point
722as a Lisp expression and evaluates it. The value is printed in the echo
723area. This command is convenient for installing in the Lisp environment
724changes that you have just made in the text of a function definition.
725
726 @kbd{C-M-x} treats @code{defvar} expressions specially. Normally,
727evaluating a @code{defvar} expression does nothing if the variable it
728defines already has a value. But @kbd{C-M-x} unconditionally resets the
729variable to the initial value specified in the @code{defvar} expression.
9c8599ca 730@code{defcustom} expressions are treated similarly.
6bf7aab6
DL
731This special feature is convenient for debugging Lisp programs.
732
733@kindex C-x C-e
734@findex eval-last-sexp
735 The command @kbd{C-x C-e} (@code{eval-last-sexp}) evaluates the Lisp
736expression preceding point in the buffer, and displays the value in the
737echo area. It is available in all major modes, not just Emacs-Lisp
738mode. It does not treat @code{defvar} specially.
739
740 If @kbd{C-M-x}, @kbd{C-x C-e}, or @kbd{M-:} is given a numeric
741argument, it inserts the value into the current buffer at point, rather
742than displaying it in the echo area. The argument's value does not
743matter.
744
745@findex eval-region
746@findex eval-current-buffer
747 The most general command for evaluating Lisp expressions from a buffer
748is @code{eval-region}. @kbd{M-x eval-region} parses the text of the
749region as one or more Lisp expressions, evaluating them one by one.
750@kbd{M-x eval-current-buffer} is similar but evaluates the entire
751buffer. This is a reasonable way to install the contents of a file of
58fa012d 752Lisp code that you are ready to test. Later, as you find bugs and
6bf7aab6
DL
753change individual functions, use @kbd{C-M-x} on each function that you
754change. This keeps the Lisp world in step with the source file.
755
9c8599ca
DL
756@vindex eval-expression-print-level
757@vindex eval-expression-print-length
758@vindex eval-expression-debug-on-error
759The customizable variables @code{eval-expression-print-level} and
760@code{eval-expression-print-length} control the maximum depth and length
761of lists to print in the result of the evaluation commands before
762abbreviating them. @code{eval-expression-debug-on-error} controls
763whether evaluation errors invoke the debugger when these commands are
764used.
765
6bf7aab6
DL
766@node Lisp Interaction
767@section Lisp Interaction Buffers
768
769 The buffer @samp{*scratch*} which is selected when Emacs starts up is
770provided for evaluating Lisp expressions interactively inside Emacs.
771
772 The simplest way to use the @samp{*scratch*} buffer is to insert Lisp
773expressions and type @kbd{C-j} after each expression. This command
774reads the Lisp expression before point, evaluates it, and inserts the
775value in printed representation before point. The result is a complete
776typescript of the expressions you have evaluated and their values.
777
778 The @samp{*scratch*} buffer's major mode is Lisp Interaction mode, which
779is the same as Emacs-Lisp mode except for the binding of @kbd{C-j}.
780
781@findex lisp-interaction-mode
782 The rationale for this feature is that Emacs must have a buffer when
783it starts up, but that buffer is not useful for editing files since a
784new buffer is made for every file that you visit. The Lisp interpreter
785typescript is the most useful thing I can think of for the initial
786buffer to do. Type @kbd{M-x lisp-interaction-mode} to put the current
787buffer in Lisp Interaction mode.
788
789@findex ielm
790 An alternative way of evaluating Emacs Lisp expressions interactively
791is to use Inferior Emacs-Lisp mode, which provides an interface rather
792like Shell mode (@pxref{Shell Mode}) for evaluating Emacs Lisp
793expressions. Type @kbd{M-x ielm} to create an @samp{*ielm*} buffer
794which uses this mode.
795
796@node External Lisp
797@section Running an External Lisp
798
799 Emacs has facilities for running programs in other Lisp systems. You can
800run a Lisp process as an inferior of Emacs, and pass expressions to it to
801be evaluated. You can also pass changed function definitions directly from
802the Emacs buffers in which you edit the Lisp programs to the inferior Lisp
803process.
804
805@findex run-lisp
806@vindex inferior-lisp-program
807@kindex C-x C-z
808 To run an inferior Lisp process, type @kbd{M-x run-lisp}. This runs
809the program named @code{lisp}, the same program you would run by typing
810@code{lisp} as a shell command, with both input and output going through
811an Emacs buffer named @samp{*lisp*}. That is to say, any ``terminal
812output'' from Lisp will go into the buffer, advancing point, and any
813``terminal input'' for Lisp comes from text in the buffer. (You can
814change the name of the Lisp executable file by setting the variable
815@code{inferior-lisp-program}.)
816
817 To give input to Lisp, go to the end of the buffer and type the input,
818terminated by @key{RET}. The @samp{*lisp*} buffer is in Inferior Lisp
819mode, which combines the special characteristics of Lisp mode with most
820of the features of Shell mode (@pxref{Shell Mode}). The definition of
821@key{RET} to send a line to a subprocess is one of the features of Shell
822mode.
823
824@findex lisp-mode
825 For the source files of programs to run in external Lisps, use Lisp
826mode. This mode can be selected with @kbd{M-x lisp-mode}, and is used
827automatically for files whose names end in @file{.l}, @file{.lsp}, or
828@file{.lisp}, as most Lisp systems usually expect.
829
830@kindex C-M-x @r{(Lisp mode)}
831@findex lisp-eval-defun
832 When you edit a function in a Lisp program you are running, the easiest
833way to send the changed definition to the inferior Lisp process is the key
834@kbd{C-M-x}. In Lisp mode, this runs the function @code{lisp-eval-defun},
835which finds the defun around or following point and sends it as input to
836the Lisp process. (Emacs can send input to any inferior process regardless
837of what buffer is current.)
838
839 Contrast the meanings of @kbd{C-M-x} in Lisp mode (for editing programs
840to be run in another Lisp system) and Emacs-Lisp mode (for editing Lisp
841programs to be run in Emacs): in both modes it has the effect of installing
842the function definition that point is in, but the way of doing so is
843different according to where the relevant Lisp environment is found.
844@xref{Executing Lisp}.