Commit | Line | Data |
---|---|---|
8cf51b2c | 1 | @c This is part of the Emacs manual. |
ba318903 | 2 | @c Copyright (C) 1985-1987, 1993-1995, 1997, 2000-2014 Free Software |
ab422c4d | 3 | @c Foundation, Inc. |
8cf51b2c | 4 | @c See file emacs.texi for copying conditions. |
abb9615e | 5 | @node Building |
8cf51b2c GM |
6 | @chapter Compiling and Testing Programs |
7 | @cindex building programs | |
8 | @cindex program building | |
9 | @cindex running Lisp functions | |
10 | ||
b09d01da CY |
11 | The previous chapter discusses the Emacs commands that are useful |
12 | for making changes in programs. This chapter deals with commands that | |
13 | assist in the process of compiling and testing programs. | |
8cf51b2c GM |
14 | |
15 | @menu | |
16 | * Compilation:: Compiling programs in languages other | |
17 | than Lisp (C, Pascal, etc.). | |
18 | * Compilation Mode:: The mode for visiting compiler errors. | |
19 | * Compilation Shell:: Customizing your shell properly | |
20 | for use in the compilation buffer. | |
21 | * Grep Searching:: Searching with grep. | |
22 | * Flymake:: Finding syntax errors on the fly. | |
8838673e | 23 | * Debuggers:: Running symbolic debuggers for non-Lisp programs. |
8cf51b2c GM |
24 | * Executing Lisp:: Various modes for editing Lisp programs, |
25 | with different facilities for running | |
26 | the Lisp programs. | |
f3b316df CY |
27 | * Libraries: Lisp Libraries. How Lisp programs are loaded into Emacs. |
28 | * Eval: Lisp Eval. Executing a single Lisp expression in Emacs. | |
29 | * Interaction: Lisp Interaction. Executing Lisp in an Emacs buffer. | |
8838673e | 30 | * External Lisp:: Communicating through Emacs with a separate Lisp. |
8cf51b2c GM |
31 | @end menu |
32 | ||
33 | @node Compilation | |
34 | @section Running Compilations under Emacs | |
35 | @cindex inferior process | |
36 | @cindex make | |
37 | @cindex compilation errors | |
38 | @cindex error log | |
39 | ||
29eabb8e CY |
40 | Emacs can run compilers for languages such as C and Fortran, feeding |
41 | the compilation log into an Emacs buffer. It can also parse the error | |
42 | messages and show you where the errors occurred. | |
8cf51b2c GM |
43 | |
44 | @table @kbd | |
45 | @item M-x compile | |
46 | Run a compiler asynchronously under Emacs, with error messages going to | |
1c64e6ed | 47 | the @file{*compilation*} buffer. |
8cf51b2c GM |
48 | @item M-x recompile |
49 | Invoke a compiler with the same command as in the last invocation of | |
50 | @kbd{M-x compile}. | |
51 | @item M-x kill-compilation | |
52 | Kill the running compilation subprocess. | |
53 | @end table | |
54 | ||
55 | @findex compile | |
b09d01da CY |
56 | To run @code{make} or another compilation command, type @kbd{M-x |
57 | compile}. This reads a shell command line using the minibuffer, and | |
29eabb8e CY |
58 | then executes the command by running a shell as a subprocess (or |
59 | @dfn{inferior process}) of Emacs. The output is inserted in a buffer | |
1c64e6ed | 60 | named @file{*compilation*}. The current buffer's default directory is |
29eabb8e CY |
61 | used as the working directory for the execution of the command; |
62 | normally, therefore, compilation takes place in this directory. | |
8cf51b2c GM |
63 | |
64 | @vindex compile-command | |
b09d01da CY |
65 | The default compilation command is @samp{make -k}, which is usually |
66 | correct for programs compiled using the @command{make} utility (the | |
67 | @samp{-k} flag tells @command{make} to continue compiling as much as | |
68 | possible after an error). @xref{Top,, Make, make, GNU Make Manual}. | |
69 | If you have done @kbd{M-x compile} before, the command that you | |
70 | specified is automatically stored in the variable | |
71 | @code{compile-command}; this is used as the default the next time you | |
72 | type @kbd{M-x compile}. A file can also specify a file-local value | |
73 | for @code{compile-command} (@pxref{File Variables}). | |
74 | ||
1c64e6ed | 75 | Starting a compilation displays the @file{*compilation*} buffer in |
b09d01da CY |
76 | another window but does not select it. While the compilation is |
77 | running, the word @samp{run} is shown in the major mode indicator for | |
1c64e6ed GM |
78 | the @file{*compilation*} buffer, and the word @samp{Compiling} appears |
79 | in all mode lines. You do not have to keep the @file{*compilation*} | |
b09d01da CY |
80 | buffer visible while compilation is running; it continues in any case. |
81 | When the compilation ends, for whatever reason, the mode line of the | |
1c64e6ed | 82 | @file{*compilation*} buffer changes to say @samp{exit} (followed by |
b09d01da CY |
83 | the exit code: @samp{[0]} for a normal exit), or @samp{signal} (if a |
84 | signal terminated the process). | |
85 | ||
86 | If you want to watch the compilation transcript as it appears, | |
1c64e6ed | 87 | switch to the @file{*compilation*} buffer and move point to the end of |
b09d01da CY |
88 | the buffer. When point is at the end, new compilation output is |
89 | inserted above point, which remains at the end. Otherwise, point | |
90 | remains fixed while compilation output is added at the end of the | |
91 | buffer. | |
8cf51b2c GM |
92 | |
93 | @cindex compilation buffer, keeping point at end | |
94 | @vindex compilation-scroll-output | |
07799e9a | 95 | If you change the variable @code{compilation-scroll-output} to a |
1c64e6ed | 96 | non-@code{nil} value, the @file{*compilation*} buffer scrolls |
b09d01da CY |
97 | automatically to follow the output. If the value is |
98 | @code{first-error}, scrolling stops when the first error appears, | |
99 | leaving point at that error. For any other non-@code{nil} value, | |
100 | scrolling continues until there is no more output. | |
8cf51b2c GM |
101 | |
102 | @findex recompile | |
103 | To rerun the last compilation with the same command, type @kbd{M-x | |
b09d01da CY |
104 | recompile}. This reuses the compilation command from the last |
105 | invocation of @kbd{M-x compile}. It also reuses the | |
1c64e6ed | 106 | @file{*compilation*} buffer and starts the compilation in its default |
8cf51b2c GM |
107 | directory, which is the directory in which the previous compilation |
108 | was started. | |
109 | ||
8cf51b2c | 110 | @findex kill-compilation |
05b621a6 | 111 | @vindex compilation-always-kill |
8cf51b2c | 112 | Starting a new compilation also kills any compilation already |
1c64e6ed | 113 | running in @file{*compilation*}, as the buffer can only handle one |
8cf51b2c | 114 | compilation at any time. However, @kbd{M-x compile} asks for |
05b621a6 CY |
115 | confirmation before actually killing a compilation that is running; to |
116 | always automatically kill the compilation without asking, change the | |
117 | variable @code{compilation-always-kill} to @code{t}. You can also | |
118 | kill a compilation process with the command @kbd{M-x | |
8cf51b2c GM |
119 | kill-compilation}. |
120 | ||
bb033b5f | 121 | To run two compilations at once, start the first one, then rename |
1c64e6ed | 122 | the @file{*compilation*} buffer (perhaps using @code{rename-uniquely}; |
bb033b5f | 123 | @pxref{Misc Buffer}), then switch buffers and start the other |
1c64e6ed | 124 | compilation. This will create a new @file{*compilation*} buffer. |
8cf51b2c | 125 | |
8cf51b2c GM |
126 | @vindex compilation-environment |
127 | You can control the environment passed to the compilation command | |
128 | with the variable @code{compilation-environment}. Its value is a list | |
129 | of environment variable settings; each element should be a string of | |
130 | the form @code{"@var{envvarname}=@var{value}"}. These environment | |
131 | variable settings override the usual ones. | |
132 | ||
133 | @node Compilation Mode | |
134 | @section Compilation Mode | |
135 | ||
136 | @cindex Compilation mode | |
137 | @cindex mode, Compilation | |
b09d01da | 138 | @cindex locus |
1c64e6ed | 139 | The @file{*compilation*} buffer uses a major mode called Compilation |
b09d01da CY |
140 | mode. Compilation mode turns each error message in the buffer into a |
141 | hyperlink; you can move point to it and type @key{RET}, or click on it | |
142 | with the mouse (@pxref{Mouse References}), to visit the @dfn{locus} of | |
143 | the error message in a separate window. The locus is the specific | |
144 | position in a file where that error occurred. | |
145 | ||
146 | @findex compile-goto-error | |
147 | @vindex compilation-auto-jump-to-first-error | |
148 | If you change the variable | |
149 | @code{compilation-auto-jump-to-first-error} to a non-@code{nil} value, | |
150 | Emacs automatically visits the locus of the first error message that | |
1c64e6ed | 151 | appears in the @file{*compilation*} buffer. |
b09d01da CY |
152 | |
153 | Compilation mode provides the following additional commands. These | |
1c64e6ed | 154 | commands can also be used in @file{*grep*} buffers, where the |
b09d01da CY |
155 | hyperlinks are search matches rather than error messages (@pxref{Grep |
156 | Searching}). | |
8cf51b2c GM |
157 | |
158 | @table @kbd | |
159 | @item M-g M-n | |
160 | @itemx M-g n | |
161 | @itemx C-x ` | |
b09d01da | 162 | Visit the locus of the next error message or match (@code{next-error}). |
8cf51b2c GM |
163 | @item M-g M-p |
164 | @itemx M-g p | |
b09d01da CY |
165 | Visit the locus of the previous error message or match |
166 | (@code{previous-error}). | |
8cf51b2c | 167 | @item M-n |
b09d01da CY |
168 | Move point to the next error message or match, without visiting its |
169 | locus (@code{compilation-next-error}). | |
8cf51b2c | 170 | @item M-p |
b09d01da CY |
171 | Move point to the previous error message or match, without visiting |
172 | its locus (@code{compilation-previous-error}). | |
8cf51b2c | 173 | @item M-@} |
b09d01da CY |
174 | Move point to the next error message or match occurring in a different |
175 | file (@code{compilation-next-file}). | |
8cf51b2c | 176 | @item M-@{ |
b09d01da CY |
177 | Move point to the previous error message or match occurring in a |
178 | different file (@code{compilation-previous-file}). | |
8cf51b2c GM |
179 | @item C-c C-f |
180 | Toggle Next Error Follow minor mode, which makes cursor motion in the | |
181 | compilation buffer produce automatic source display. | |
182 | @end table | |
183 | ||
8cf51b2c GM |
184 | @kindex M-g M-n |
185 | @kindex M-g n | |
186 | @kindex C-x ` | |
187 | @findex next-error | |
188 | @vindex next-error-highlight | |
b09d01da CY |
189 | To visit errors sequentially, type @w{@kbd{C-x `}} |
190 | (@code{next-error}), or equivalently @kbd{M-g M-n} or @kbd{M-g n}. | |
191 | This command can be invoked from any buffer, not just a Compilation | |
192 | mode buffer. The first time you invoke it after a compilation, it | |
193 | visits the locus of the first error message. Each subsequent | |
194 | @w{@kbd{C-x `}} visits the next error, in a similar fashion. If you | |
195 | visit a specific error with @key{RET} or a mouse click in the | |
1c64e6ed | 196 | @file{*compilation*} buffer, subsequent @w{@kbd{C-x `}} commands |
b09d01da CY |
197 | advance from there. When @w{@kbd{C-x `}} finds no more error messages |
198 | to visit, it signals an error. @w{@kbd{C-u C-x `}} starts again from | |
199 | the beginning of the compilation buffer, and visits the first locus. | |
200 | ||
201 | @kbd{M-g M-p} or @kbd{M-g p} (@code{previous-error}) iterates | |
202 | through errors in the opposite direction. | |
203 | ||
204 | The @code{next-error} and @code{previous-error} commands don't just | |
1c64e6ed GM |
205 | act on the errors or matches listed in @file{*compilation*} and |
206 | @file{*grep*} buffers; they also know how to iterate through error or | |
b09d01da CY |
207 | match lists produced by other commands, such as @kbd{M-x occur} |
208 | (@pxref{Other Repeating Search}). If you are already in a buffer | |
209 | containing error messages or matches, those are the ones that are | |
210 | iterated through; otherwise, Emacs looks for a buffer containing error | |
211 | messages or matches amongst the windows of the selected frame, then | |
212 | for one that @code{next-error} or @code{previous-error} previously | |
213 | iterated through, and finally amongst all other buffers. If the | |
214 | buffer chosen for iterating through is not currently displayed in a | |
215 | window, it will be displayed. | |
8cf51b2c GM |
216 | |
217 | @vindex compilation-skip-threshold | |
b09d01da CY |
218 | By default, the @code{next-error} and @code{previous-error} commands |
219 | skip less important messages. The variable | |
220 | @code{compilation-skip-threshold} controls this. The default value, | |
221 | 1, means to skip anything less important than a warning. A value of 2 | |
222 | means to skip anything less important than an error, while 0 means not | |
223 | to skip any messages. | |
224 | ||
225 | When Emacs visits the locus of an error message, it momentarily | |
226 | highlights the relevant source line. The duration of this highlight | |
227 | is determined by the variable @code{next-error-highlight}. | |
228 | ||
229 | @vindex compilation-context-lines | |
1c64e6ed | 230 | If the @file{*compilation*} buffer is shown in a window with a left |
b09d01da CY |
231 | fringe (@pxref{Fringes}), the locus-visiting commands put an arrow in |
232 | the fringe, pointing to the current error message. If the window has | |
0be641c0 CY |
233 | no left fringe, such as on a text terminal, these commands scroll the |
234 | window so that the current message is at the top of the window. If | |
235 | you change the variable @code{compilation-context-lines} to an integer | |
236 | value @var{n}, these commands scroll the window so that the current | |
237 | error message is @var{n} lines from the top, whether or not there is a | |
238 | fringe; the default value, @code{nil}, gives the behavior described | |
239 | above. | |
8cf51b2c GM |
240 | |
241 | @vindex compilation-error-regexp-alist | |
242 | @vindex grep-regexp-alist | |
243 | To parse messages from the compiler, Compilation mode uses the | |
244 | variable @code{compilation-error-regexp-alist} which lists various | |
b09d01da CY |
245 | error message formats and tells Emacs how to extract the locus from |
246 | each. A similar variable, @code{grep-regexp-alist}, tells Emacs how | |
247 | to parse output from a @code{grep} command (@pxref{Grep Searching}). | |
8cf51b2c GM |
248 | |
249 | @findex compilation-next-error | |
250 | @findex compilation-previous-error | |
251 | @findex compilation-next-file | |
252 | @findex compilation-previous-file | |
b09d01da CY |
253 | Compilation mode also defines the keys @key{SPC} and @key{DEL} to |
254 | scroll by screenfuls; @kbd{M-n} (@code{compilation-next-error}) and | |
255 | @kbd{M-p} (@code{compilation-previous-error}) to move to the next or | |
256 | previous error message; and @kbd{M-@{} (@code{compilation-next-file}) | |
257 | and @kbd{M-@}} (@code{compilation-previous-file}) to move to the next | |
258 | or previous error message for a different source file. | |
8cf51b2c GM |
259 | |
260 | @cindex Next Error Follow mode | |
261 | @findex next-error-follow-minor-mode | |
262 | You can type @kbd{C-c C-f} to toggle Next Error Follow mode. In | |
263 | this minor mode, ordinary cursor motion in the compilation buffer | |
1df7defd | 264 | automatically updates the source buffer, i.e., moving the cursor over |
b09d01da | 265 | an error message causes the locus of that error to be displayed. |
8cf51b2c GM |
266 | |
267 | The features of Compilation mode are also available in a minor mode | |
268 | called Compilation Minor mode. This lets you parse error messages in | |
b09d01da CY |
269 | any buffer, not just a normal compilation output buffer. Type |
270 | @kbd{M-x compilation-minor-mode} to enable the minor mode. For | |
271 | instance, in an Rlogin buffer (@pxref{Remote Host}), Compilation minor | |
272 | mode automatically accesses remote source files by FTP (@pxref{File | |
273 | Names}). | |
8cf51b2c GM |
274 | |
275 | @node Compilation Shell | |
276 | @section Subshells for Compilation | |
277 | ||
b09d01da CY |
278 | The @kbd{M-x compile} command uses a shell to run the compilation |
279 | command, but specifies the option for a noninteractive shell. This | |
280 | means, in particular, that the shell should start with no prompt. If | |
281 | you find your usual shell prompt making an unsightly appearance in the | |
1c64e6ed | 282 | @file{*compilation*} buffer, it means you have made a mistake in your |
b09d01da CY |
283 | shell's init file by setting the prompt unconditionally. (This init |
284 | file may be named @file{.bashrc}, @file{.profile}, @file{.cshrc}, | |
285 | @file{.shrc}, etc., depending on what shell you use.) The shell init | |
8cf51b2c GM |
286 | file should set the prompt only if there already is a prompt. Here's |
287 | how to do it in bash: | |
288 | ||
289 | @example | |
290 | if [ "$@{PS1+set@}" = set ] | |
291 | then PS1=@dots{} | |
292 | fi | |
293 | @end example | |
294 | ||
295 | @noindent | |
296 | And here's how to do it in csh: | |
297 | ||
298 | @example | |
299 | if ($?prompt) set prompt = @dots{} | |
300 | @end example | |
301 | ||
b09d01da CY |
302 | Emacs does not expect a compiler process to launch asynchronous |
303 | subprocesses; if it does, and they keep running after the main | |
304 | compiler process has terminated, Emacs may kill them or their output | |
305 | may not arrive in Emacs. To avoid this problem, make the main | |
306 | compilation process wait for its subprocesses to finish. In a shell | |
307 | script, you can do this using @samp{$!} and @samp{wait}, like this: | |
308 | ||
309 | @example | |
310 | (sleep 10; echo 2nd)& pid=$! # @r{Record pid of subprocess} | |
311 | echo first message | |
312 | wait $pid # @r{Wait for subprocess} | |
313 | @end example | |
314 | ||
315 | @noindent | |
316 | If the background process does not output to the compilation buffer, | |
317 | so you only need to prevent it from being killed when the main | |
318 | compilation process terminates, this is sufficient: | |
319 | ||
320 | @example | |
321 | nohup @var{command}; sleep 1 | |
322 | @end example | |
8cf51b2c | 323 | |
8cf51b2c | 324 | @ifnottex |
b09d01da CY |
325 | On the MS-DOS ``operating system'', asynchronous subprocesses are |
326 | not supported, so @kbd{M-x compile} runs the compilation command | |
1df7defd | 327 | synchronously (i.e., you must wait until the command finishes before |
b09d01da | 328 | you can do anything else in Emacs). @xref{MS-DOS}. |
8cf51b2c GM |
329 | @end ifnottex |
330 | ||
331 | @node Grep Searching | |
332 | @section Searching with Grep under Emacs | |
333 | ||
334 | Just as you can run a compiler from Emacs and then visit the lines | |
b09d01da CY |
335 | with compilation errors, you can also run @command{grep} and then |
336 | visit the lines on which matches were found. This works by treating | |
16152b76 | 337 | the matches reported by @command{grep} as if they were ``errors''. |
b09d01da | 338 | The output buffer uses Grep mode, which is a variant of Compilation |
8cf51b2c GM |
339 | mode (@pxref{Compilation Mode}). |
340 | ||
341 | @table @kbd | |
342 | @item M-x grep | |
467e8d77 | 343 | @itemx M-x lgrep |
b09d01da | 344 | Run @command{grep} asynchronously under Emacs, listing matching lines in |
1c64e6ed | 345 | the buffer named @file{*grep*}. |
8cf51b2c GM |
346 | @item M-x grep-find |
347 | @itemx M-x find-grep | |
348 | @itemx M-x rgrep | |
b09d01da | 349 | Run @command{grep} via @code{find}, and collect output in the |
1c64e6ed | 350 | @file{*grep*} buffer. |
26e533e2 | 351 | @item M-x zrgrep |
1c64e6ed | 352 | Run @code{zgrep} and collect output in the @file{*grep*} buffer. |
8cf51b2c | 353 | @item M-x kill-grep |
b09d01da | 354 | Kill the running @command{grep} subprocess. |
8cf51b2c GM |
355 | @end table |
356 | ||
357 | @findex grep | |
b09d01da CY |
358 | To run @command{grep}, type @kbd{M-x grep}, then enter a command line |
359 | that specifies how to run @command{grep}. Use the same arguments you | |
360 | would give @command{grep} when running it normally: a @command{grep}-style | |
8cf51b2c GM |
361 | regexp (usually in single-quotes to quote the shell's special |
362 | characters) followed by file names, which may use wildcards. If you | |
363 | specify a prefix argument for @kbd{M-x grep}, it finds the tag | |
364 | (@pxref{Tags}) in the buffer around point, and puts that into the | |
b09d01da | 365 | default @command{grep} command. |
8cf51b2c | 366 | |
b09d01da | 367 | Your command need not simply run @command{grep}; you can use any shell |
8cf51b2c | 368 | command that produces output in the same format. For instance, you |
b09d01da | 369 | can chain @command{grep} commands, like this: |
8cf51b2c GM |
370 | |
371 | @example | |
372 | grep -nH -e foo *.el | grep bar | grep toto | |
373 | @end example | |
374 | ||
1c64e6ed | 375 | The output from @command{grep} goes in the @file{*grep*} buffer. You |
8cf51b2c GM |
376 | can find the corresponding lines in the original files using @w{@kbd{C-x |
377 | `}}, @key{RET}, and so forth, just like compilation errors. | |
378 | ||
379 | Some grep programs accept a @samp{--color} option to output special | |
380 | markers around matches for the purpose of highlighting. You can make | |
381 | use of this feature by setting @code{grep-highlight-matches} to | |
382 | @code{t}. When displaying a match in the source buffer, the exact | |
383 | match will be highlighted, instead of the entire source line. | |
384 | ||
385 | @findex grep-find | |
386 | @findex find-grep | |
387 | The command @kbd{M-x grep-find} (also available as @kbd{M-x | |
388 | find-grep}) is similar to @kbd{M-x grep}, but it supplies a different | |
389 | initial default for the command---one that runs both @code{find} and | |
b09d01da | 390 | @command{grep}, so as to search every file in a directory tree. See also |
8cf51b2c GM |
391 | the @code{find-grep-dired} command, in @ref{Dired and Find}. |
392 | ||
393 | @findex lgrep | |
394 | @findex rgrep | |
26e533e2 | 395 | @findex zrgrep |
8cf51b2c | 396 | The commands @kbd{M-x lgrep} (local grep) and @kbd{M-x rgrep} |
b09d01da | 397 | (recursive grep) are more user-friendly versions of @command{grep} and |
8cf51b2c GM |
398 | @code{grep-find}, which prompt separately for the regular expression |
399 | to match, the files to search, and the base directory for the search. | |
26e533e2 CY |
400 | Case sensitivity of the search is controlled by the current value of |
401 | @code{case-fold-search}. The command @kbd{M-x zrgrep} is similar to | |
b09d01da CY |
402 | @kbd{M-x rgrep}, but it calls @command{zgrep} instead of |
403 | @command{grep} to search the contents of gzipped files. | |
8cf51b2c | 404 | |
26e533e2 | 405 | These commands build the shell commands based on the variables |
8cf51b2c | 406 | @code{grep-template} (for @code{lgrep}) and @code{grep-find-template} |
26e533e2 CY |
407 | (for @code{rgrep}). The files to search can use aliases defined in |
408 | the variable @code{grep-files-aliases}. | |
8cf51b2c | 409 | |
b09d01da CY |
410 | @vindex grep-find-ignored-directories |
411 | Directories listed in the variable | |
412 | @code{grep-find-ignored-directories} are automatically skipped by | |
413 | @kbd{M-x rgrep}. The default value includes the data directories used | |
414 | by various version control systems. | |
8cf51b2c GM |
415 | |
416 | @node Flymake | |
417 | @section Finding Syntax Errors On The Fly | |
418 | @cindex checking syntax | |
419 | ||
420 | Flymake mode is a minor mode that performs on-the-fly syntax | |
421 | checking for many programming and markup languages, including C, C++, | |
c1dabff0 | 422 | Perl, HTML, and @TeX{}/@LaTeX{}. It is somewhat analogous to Flyspell |
8cf51b2c GM |
423 | mode, which performs spell checking for ordinary human languages in a |
424 | similar fashion (@pxref{Spelling}). As you edit a file, Flymake mode | |
425 | runs an appropriate syntax checking tool in the background, using a | |
426 | temporary copy of the buffer. It then parses the error and warning | |
427 | messages, and highlights the erroneous lines in the buffer. The | |
428 | syntax checking tool used depends on the language; for example, for | |
429 | C/C++ files this is usually the C compiler. Flymake can also use | |
430 | build tools such as @code{make} for checking complicated projects. | |
431 | ||
166bc0c8 CY |
432 | To enable Flymake mode, type @kbd{M-x flymake-mode}. You can jump |
433 | to the errors that it finds by using @kbd{M-x flymake-goto-next-error} | |
434 | and @kbd{M-x flymake-goto-prev-error}. To display any error messages | |
435 | associated with the current line, type @kbd{M-x | |
436 | flymake-display-err-menu-for-current-line}. | |
8cf51b2c | 437 | |
b09d01da CY |
438 | For more details about using Flymake, |
439 | @ifnottex | |
440 | see @ref{Top, Flymake, Flymake, flymake, The Flymake Manual}. | |
441 | @end ifnottex | |
442 | @iftex | |
443 | see the Flymake Info manual, which is distributed with Emacs. | |
444 | @end iftex | |
8cf51b2c GM |
445 | |
446 | @node Debuggers | |
447 | @section Running Debuggers Under Emacs | |
448 | @cindex debuggers | |
449 | @cindex GUD library | |
450 | @cindex GDB | |
451 | @cindex DBX | |
452 | @cindex SDB | |
453 | @cindex XDB | |
454 | @cindex Perldb | |
455 | @cindex JDB | |
456 | @cindex PDB | |
457 | ||
a03334ca | 458 | The GUD (Grand Unified Debugger) library provides an Emacs interface |
0595bd7e CY |
459 | to a wide variety of symbolic debuggers. It can run the GNU Debugger |
460 | (GDB), as well as DBX, SDB, XDB, Perl's debugging mode, the Python | |
461 | debugger PDB, and the Java Debugger JDB. | |
8cf51b2c | 462 | |
0595bd7e CY |
463 | Emacs provides a special interface to GDB, which uses extra Emacs |
464 | windows to display the state of the debugged program. @xref{GDB | |
465 | Graphical Interface}. | |
466 | ||
467 | Emacs also has a built-in debugger for Emacs Lisp programs. | |
468 | @xref{Debugging,, The Lisp Debugger, elisp, the Emacs Lisp Reference | |
469 | Manual}. | |
d35fdb5b | 470 | |
8cf51b2c | 471 | @menu |
8838673e GM |
472 | * Starting GUD:: How to start a debugger subprocess. |
473 | * Debugger Operation:: Connection between the debugger and source buffers. | |
474 | * Commands of GUD:: Key bindings for common commands. | |
475 | * GUD Customization:: Defining your own commands for GUD. | |
b424697f | 476 | * GDB Graphical Interface:: An enhanced mode that uses GDB features to |
2d2f6581 | 477 | implement a graphical debugging environment. |
8cf51b2c GM |
478 | @end menu |
479 | ||
480 | @node Starting GUD | |
b424697f | 481 | @subsection Starting GUD |
8cf51b2c | 482 | |
0595bd7e | 483 | There are several commands for starting a debugger subprocess, each |
a03334ca | 484 | corresponding to a particular debugger program. |
8cf51b2c GM |
485 | |
486 | @table @kbd | |
0595bd7e | 487 | @item M-x gdb |
b424697f | 488 | @findex gdb |
0595bd7e CY |
489 | Run GDB as a subprocess, and interact with it via an IDE-like Emacs |
490 | interface. @xref{GDB Graphical Interface}, for more information about | |
491 | this command. | |
b424697f | 492 | |
0595bd7e | 493 | @item M-x gud-gdb |
4f45c619 | 494 | @findex gud-gdb |
0595bd7e CY |
495 | Run GDB, using a GUD interaction buffer for input and output to the |
496 | GDB subprocess (@pxref{Debugger Operation}). If such a buffer already | |
497 | exists, switch to it; otherwise, create the buffer and switch to it. | |
8cf51b2c | 498 | |
0595bd7e CY |
499 | The other commands in this list do the same, for other debugger |
500 | programs. | |
8cf51b2c | 501 | |
0595bd7e | 502 | @item M-x perldb |
8cf51b2c | 503 | @findex perldb |
0595bd7e | 504 | Run the Perl interpreter in debug mode. |
8cf51b2c | 505 | |
0595bd7e | 506 | @item M-x jdb |
8cf51b2c | 507 | @findex jdb |
0595bd7e | 508 | Run the Java debugger. |
8cf51b2c | 509 | |
0595bd7e | 510 | @item M-x pdb |
8cf51b2c | 511 | @findex pdb |
0595bd7e CY |
512 | Run the Python debugger. |
513 | ||
514 | @item M-x dbx | |
515 | @findex dbx | |
516 | Run the DBX debugger. | |
517 | ||
518 | @item M-x xdb | |
519 | @findex xdb | |
520 | @vindex gud-xdb-directories | |
521 | Run the XDB debugger. | |
522 | ||
523 | @item M-x sdb | |
524 | @findex sdb | |
525 | Run the SDB debugger. | |
8cf51b2c GM |
526 | @end table |
527 | ||
0595bd7e CY |
528 | Each of these commands reads a command line to invoke the debugger, |
529 | using the minibuffer. The minibuffer's initial contents contain the | |
530 | standard executable name and options for the debugger, and sometimes | |
531 | also a guess for the name of the executable file you want to debug. | |
532 | Shell wildcards and variables are not allowed in this command line. | |
533 | Emacs assumes that the first command argument which does not start | |
534 | with a @samp{-} is the executable file name. | |
8cf51b2c | 535 | |
a03334ca | 536 | @cindex remote host, debugging on |
0595bd7e CY |
537 | Tramp provides a facility for remote debugging, whereby both the |
538 | debugger and the program being debugged are on the same remote host. | |
539 | @xref{Running a debugger on a remote host,,, tramp, The Tramp Manual}, | |
540 | for details. This is separate from GDB's remote debugging feature, | |
541 | where the program and the debugger run on different machines | |
542 | (@pxref{Remote Debugging,, Debugging Remote Programs, gdb, The GNU | |
543 | debugger}). | |
8cf51b2c GM |
544 | |
545 | @node Debugger Operation | |
b424697f | 546 | @subsection Debugger Operation |
0595bd7e | 547 | @cindex GUD interaction buffer |
8cf51b2c | 548 | |
0595bd7e CY |
549 | The @dfn{GUD interaction buffer} is an Emacs buffer which is used to |
550 | send text commands to a debugger subprocess, and record its output. | |
551 | This is the basic interface for interacting with a debugger, used by | |
552 | @kbd{M-x gud-gdb} and other commands listed in | |
553 | @iftex | |
554 | the preceding section. | |
0ab39399 | 555 | @end iftex |
0595bd7e CY |
556 | @ifnottex |
557 | @ref{Starting GUD}. | |
558 | @end ifnottex | |
559 | The @kbd{M-x gdb} command extends this interface with additional | |
560 | specialized buffers for controlling breakpoints, stack frames, and | |
561 | other aspects of the debugger state (@pxref{GDB Graphical Interface}). | |
562 | ||
563 | The GUD interaction buffer uses a variant of Shell mode, so the | |
564 | Emacs commands defined by Shell mode are available (@pxref{Shell | |
565 | Mode}). Completion is available for most debugger commands | |
566 | (@pxref{Completion}), and you can use the usual Shell mode history | |
567 | commands to repeat them. | |
568 | @iftex | |
569 | See the next section | |
2785d024 | 570 | @end iftex |
0595bd7e CY |
571 | @ifnottex |
572 | @xref{Commands of GUD}, | |
573 | @end ifnottex | |
574 | for special commands that can be used in the GUD interaction buffer. | |
575 | ||
576 | As you debug a program, Emacs displays the relevant source files by | |
577 | visiting them in Emacs buffers, with an arrow in the left fringe | |
0be641c0 CY |
578 | indicating the current execution line. (On a text terminal, the arrow |
579 | appears as @samp{=>}, overlaid on the first two text columns.) Moving | |
580 | point in such a buffer does not move the arrow. You are free to edit | |
581 | these source files, but note that inserting or deleting lines will | |
582 | throw off the arrow's positioning, as Emacs has no way to figure out | |
583 | which edited source line corresponds to the line reported by the | |
0595bd7e CY |
584 | debugger subprocess. To update this information, you typically have |
585 | to recompile and restart the program. | |
586 | ||
587 | @cindex GUD Tooltip mode | |
588 | @cindex mode, GUD Tooltip | |
589 | @findex gud-tooltip-mode | |
8cf51b2c | 590 | @vindex gud-tooltip-echo-area |
0595bd7e | 591 | GUD Tooltip mode is a global minor mode that adds tooltip support to |
1df7defd | 592 | GUD@. To toggle this mode, type @kbd{M-x gud-tooltip-mode}. It is |
0595bd7e | 593 | disabled by default. If enabled, you can move the mouse cursor over a |
ba025fbd EZ |
594 | variable, a function, or a macro (collectively called |
595 | @dfn{identifiers}) to show their values in tooltips | |
596 | (@pxref{Tooltips}). Alternatively, mark an identifier or an | |
597 | expression by dragging the mouse over it, then leave the mouse in the | |
598 | marked area to have the value of the expression displayed in a | |
599 | tooltip. The GUD Tooltip mode takes effect in the GUD interaction | |
600 | buffer, and in all source buffers with major modes listed in the | |
601 | variable @code{gud-tooltip-modes}. If the variable | |
602 | @code{gud-tooltip-echo-area} is non-@code{nil}, or if you turned off | |
603 | the tooltip mode, values are shown in the echo area instead of a | |
604 | tooltip. | |
605 | ||
606 | When using GUD Tooltip mode with @kbd{M-x gud-gdb}, displaying an | |
607 | expression's value in GDB can sometimes expand a macro, potentially | |
608 | causing side effects in the debugged program. For that reason, using | |
609 | tooltips in @code{gud-gdb} is disabled. If you use the @kbd{M-x gdb} | |
610 | interface, this problem does not occur, as there is special code to | |
611 | avoid side-effects; furthermore, you can display macro definitions | |
612 | associated with an identifier when the program is not executing. | |
8cf51b2c GM |
613 | |
614 | @node Commands of GUD | |
b424697f | 615 | @subsection Commands of GUD |
8cf51b2c | 616 | |
0595bd7e CY |
617 | GUD provides commands for setting and clearing breakpoints, |
618 | selecting stack frames, and stepping through the program. | |
8cf51b2c GM |
619 | |
620 | @table @kbd | |
1a98c364 BG |
621 | @item C-x C-a C-b |
622 | @kindex C-x C-a C-b | |
8cf51b2c GM |
623 | Set a breakpoint on the source line that point is on. |
624 | @end table | |
625 | ||
1a98c364 | 626 | @kbd{C-x C-a C-b} (@code{gud-break}), when called in a source |
0595bd7e | 627 | buffer, sets a debugger breakpoint on the current source line. This |
1df7defd | 628 | command is available only after starting GUD@. If you call it in a |
0595bd7e CY |
629 | buffer that is not associated with any debugger subprocess, it signals |
630 | a error. | |
631 | ||
8cf51b2c | 632 | @kindex C-x C-a @r{(GUD)} |
0595bd7e CY |
633 | The following commands are available both in the GUD interaction |
634 | buffer and globally, but with different key bindings. The keys | |
8cf51b2c | 635 | starting with @kbd{C-c} are available only in the GUD interaction |
0595bd7e CY |
636 | buffer, while those starting with @kbd{C-x C-a} are available |
637 | globally. Some of these commands are also available via the tool bar; | |
638 | some are not supported by certain debuggers. | |
8cf51b2c GM |
639 | |
640 | @table @kbd | |
641 | @item C-c C-l | |
642 | @kindex C-c C-l @r{(GUD)} | |
643 | @itemx C-x C-a C-l | |
644 | @findex gud-refresh | |
0595bd7e CY |
645 | Display, in another window, the last source line referred to in the |
646 | GUD interaction buffer (@code{gud-refresh}). | |
8cf51b2c GM |
647 | |
648 | @item C-c C-s | |
649 | @kindex C-c C-s @r{(GUD)} | |
650 | @itemx C-x C-a C-s | |
651 | @findex gud-step | |
0595bd7e CY |
652 | Execute the next single line of code (@code{gud-step}). If the line |
653 | contains a function call, execution stops after entering the called | |
654 | function. | |
8cf51b2c GM |
655 | |
656 | @item C-c C-n | |
657 | @kindex C-c C-n @r{(GUD)} | |
658 | @itemx C-x C-a C-n | |
659 | @findex gud-next | |
0595bd7e CY |
660 | Execute the next single line of code, stepping across function calls |
661 | without stopping inside the functions (@code{gud-next}). | |
8cf51b2c GM |
662 | |
663 | @item C-c C-i | |
664 | @kindex C-c C-i @r{(GUD)} | |
665 | @itemx C-x C-a C-i | |
666 | @findex gud-stepi | |
667 | Execute a single machine instruction (@code{gud-stepi}). | |
668 | ||
669 | @item C-c C-p | |
670 | @kindex C-c C-p @r{(GUD)} | |
671 | @itemx C-x C-a C-p | |
672 | @findex gud-print | |
673 | Evaluate the expression at point (@code{gud-print}). If Emacs | |
674 | does not print the exact expression that you want, mark it as a region | |
675 | first. | |
676 | ||
677 | @need 3000 | |
678 | @item C-c C-r | |
679 | @kindex C-c C-r @r{(GUD)} | |
680 | @itemx C-x C-a C-r | |
681 | @findex gud-cont | |
682 | Continue execution without specifying any stopping point. The program | |
683 | will run until it hits a breakpoint, terminates, or gets a signal that | |
684 | the debugger is checking for (@code{gud-cont}). | |
685 | ||
686 | @need 1000 | |
687 | @item C-c C-d | |
688 | @kindex C-c C-d @r{(GUD)} | |
689 | @itemx C-x C-a C-d | |
690 | @findex gud-remove | |
691 | Delete the breakpoint(s) on the current source line, if any | |
692 | (@code{gud-remove}). If you use this command in the GUD interaction | |
693 | buffer, it applies to the line where the program last stopped. | |
694 | ||
695 | @item C-c C-t | |
696 | @kindex C-c C-t @r{(GUD)} | |
697 | @itemx C-x C-a C-t | |
698 | @findex gud-tbreak | |
699 | Set a temporary breakpoint on the current source line, if any | |
700 | (@code{gud-tbreak}). If you use this command in the GUD interaction | |
701 | buffer, it applies to the line where the program last stopped. | |
702 | ||
703 | @item C-c < | |
704 | @kindex C-c < @r{(GUD)} | |
705 | @itemx C-x C-a < | |
706 | @findex gud-up | |
707 | Select the next enclosing stack frame (@code{gud-up}). This is | |
708 | equivalent to the GDB command @samp{up}. | |
709 | ||
710 | @item C-c > | |
711 | @kindex C-c > @r{(GUD)} | |
712 | @itemx C-x C-a > | |
713 | @findex gud-down | |
714 | Select the next inner stack frame (@code{gud-down}). This is | |
715 | equivalent to the GDB command @samp{down}. | |
716 | ||
717 | @item C-c C-u | |
718 | @kindex C-c C-u @r{(GUD)} | |
719 | @itemx C-x C-a C-u | |
720 | @findex gud-until | |
721 | Continue execution to the current line (@code{gud-until}). The | |
722 | program will run until it hits a breakpoint, terminates, gets a signal | |
723 | that the debugger is checking for, or reaches the line on which the | |
724 | cursor currently sits. | |
725 | ||
726 | @item C-c C-f | |
727 | @kindex C-c C-f @r{(GUD)} | |
728 | @itemx C-x C-a C-f | |
729 | @findex gud-finish | |
730 | Run the program until the selected stack frame returns or | |
731 | stops for some other reason (@code{gud-finish}). | |
732 | @end table | |
733 | ||
734 | If you are using GDB, these additional key bindings are available: | |
735 | ||
736 | @table @kbd | |
737 | @item C-x C-a C-j | |
738 | @kindex C-x C-a C-j @r{(GUD)} | |
739 | @findex gud-jump | |
740 | Only useful in a source buffer, @code{gud-jump} transfers the | |
741 | program's execution point to the current line. In other words, the | |
742 | next line that the program executes will be the one where you gave the | |
743 | command. If the new execution line is in a different function from | |
744 | the previously one, GDB prompts for confirmation since the results may | |
745 | be bizarre. See the GDB manual entry regarding @code{jump} for | |
746 | details. | |
747 | ||
748 | @item @key{TAB} | |
749 | @kindex TAB @r{(GUD)} | |
750 | @findex gud-gdb-complete-command | |
751 | With GDB, complete a symbol name (@code{gud-gdb-complete-command}). | |
752 | This key is available only in the GUD interaction buffer. | |
753 | @end table | |
754 | ||
755 | These commands interpret a numeric argument as a repeat count, when | |
756 | that makes sense. | |
757 | ||
758 | Because @key{TAB} serves as a completion command, you can't use it to | |
1df7defd | 759 | enter a tab as input to the program you are debugging with GDB@. |
8cf51b2c GM |
760 | Instead, type @kbd{C-q @key{TAB}} to enter a tab. |
761 | ||
762 | @node GUD Customization | |
b424697f | 763 | @subsection GUD Customization |
8cf51b2c GM |
764 | |
765 | @vindex gdb-mode-hook | |
766 | @vindex dbx-mode-hook | |
767 | @vindex sdb-mode-hook | |
768 | @vindex xdb-mode-hook | |
769 | @vindex perldb-mode-hook | |
770 | @vindex pdb-mode-hook | |
771 | @vindex jdb-mode-hook | |
0595bd7e CY |
772 | On startup, GUD runs one of the following hooks: |
773 | @code{gdb-mode-hook}, if you are using GDB; @code{dbx-mode-hook}, if | |
774 | you are using DBX; @code{sdb-mode-hook}, if you are using SDB; | |
775 | @code{xdb-mode-hook}, if you are using XDB; @code{perldb-mode-hook}, | |
776 | for Perl debugging mode; @code{pdb-mode-hook}, for PDB; | |
1df7defd | 777 | @code{jdb-mode-hook}, for JDB@. @xref{Hooks}. |
0595bd7e CY |
778 | |
779 | The @code{gud-def} Lisp macro (@pxref{Defining Macros,,, elisp, the | |
780 | Emacs Lisp Reference Manual}) provides a convenient way to define an | |
781 | Emacs command that sends a particular command string to the debugger, | |
782 | and set up a key binding for in the GUD interaction buffer: | |
8cf51b2c GM |
783 | |
784 | @findex gud-def | |
785 | @example | |
786 | (gud-def @var{function} @var{cmdstring} @var{binding} @var{docstring}) | |
787 | @end example | |
788 | ||
789 | This defines a command named @var{function} which sends | |
790 | @var{cmdstring} to the debugger process, and gives it the documentation | |
791 | string @var{docstring}. You can then use the command @var{function} in any | |
792 | buffer. If @var{binding} is non-@code{nil}, @code{gud-def} also binds | |
793 | the command to @kbd{C-c @var{binding}} in the GUD buffer's mode and to | |
794 | @kbd{C-x C-a @var{binding}} generally. | |
795 | ||
796 | The command string @var{cmdstring} may contain certain | |
797 | @samp{%}-sequences that stand for data to be filled in at the time | |
798 | @var{function} is called: | |
799 | ||
800 | @table @samp | |
801 | @item %f | |
802 | The name of the current source file. If the current buffer is the GUD | |
803 | buffer, then the ``current source file'' is the file that the program | |
804 | stopped in. | |
805 | ||
806 | @item %l | |
807 | The number of the current source line. If the current buffer is the GUD | |
808 | buffer, then the ``current source line'' is the line that the program | |
809 | stopped in. | |
810 | ||
811 | @item %e | |
812 | In transient-mark-mode the text in the region, if it is active. | |
813 | Otherwise the text of the C lvalue or function-call expression at or | |
814 | adjacent to point. | |
815 | ||
816 | @item %a | |
817 | The text of the hexadecimal address at or adjacent to point. | |
818 | ||
819 | @item %p | |
820 | The numeric argument of the called function, as a decimal number. If | |
821 | the command is used without a numeric argument, @samp{%p} stands for the | |
822 | empty string. | |
823 | ||
824 | If you don't use @samp{%p} in the command string, the command you define | |
825 | ignores any numeric argument. | |
826 | ||
827 | @item %d | |
828 | The name of the directory of the current source file. | |
829 | ||
830 | @item %c | |
831 | Fully qualified class name derived from the expression surrounding point | |
832 | (jdb only). | |
833 | @end table | |
834 | ||
835 | @node GDB Graphical Interface | |
836 | @subsection GDB Graphical Interface | |
837 | ||
0595bd7e CY |
838 | The command @kbd{M-x gdb} starts GDB in an IDE-like interface, with |
839 | specialized buffers for controlling breakpoints, stack frames, and | |
840 | other aspects of the debugger state. It also provides additional ways | |
841 | to control the debugging session with the mouse, such as clicking in | |
842 | the fringe of a source buffer to set a breakpoint there. | |
8cf51b2c GM |
843 | |
844 | @vindex gud-gdb-command-name | |
0595bd7e CY |
845 | To run GDB using just the GUD interaction buffer interface, without |
846 | these additional features, use @kbd{M-x gud-gdb} (@pxref{Starting | |
847 | GUD}). You must use this if you want to debug multiple programs | |
848 | within one Emacs session, as that is currently unsupported by @kbd{M-x | |
849 | gdb}. | |
850 | ||
851 | Internally, @kbd{M-x gdb} informs GDB that its ``screen size'' is | |
852 | unlimited; for correct operation, you must not change GDB's screen | |
853 | height and width values during the debugging session. | |
8cf51b2c GM |
854 | |
855 | @menu | |
691cf4a0 | 856 | * GDB User Interface Layout:: Control the number of displayed buffers. |
8cf51b2c GM |
857 | * Source Buffers:: Use the mouse in the fringe/margin to |
858 | control your program. | |
859 | * Breakpoints Buffer:: A breakpoint control panel. | |
691cf4a0 | 860 | * Threads Buffer:: Displays your threads. |
8cf51b2c | 861 | * Stack Buffer:: Select a frame from the call stack. |
0595bd7e | 862 | * Other GDB Buffers:: Other buffers for controlling the GDB state. |
8cf51b2c | 863 | * Watch Expressions:: Monitor variable values in the speedbar. |
691cf4a0 | 864 | * Multithreaded Debugging:: Debugging programs with several threads. |
8cf51b2c GM |
865 | @end menu |
866 | ||
691cf4a0 | 867 | @node GDB User Interface Layout |
8cf51b2c GM |
868 | @subsubsection GDB User Interface Layout |
869 | @cindex GDB User Interface layout | |
870 | ||
871 | @vindex gdb-many-windows | |
0595bd7e CY |
872 | If the variable @code{gdb-many-windows} is @code{nil} (the default), |
873 | @kbd{M-x gdb} normally displays only the GUD interaction buffer. | |
8cf51b2c | 874 | However, if the variable @code{gdb-show-main} is also non-@code{nil}, |
0595bd7e CY |
875 | it starts with two windows: one displaying the GUD interaction buffer, |
876 | and the other showing the source for the @code{main} function of the | |
877 | program you are debugging. | |
8cf51b2c GM |
878 | |
879 | If @code{gdb-many-windows} is non-@code{nil}, then @kbd{M-x gdb} | |
880 | displays the following frame layout: | |
881 | ||
882 | @smallexample | |
883 | @group | |
884 | +--------------------------------+--------------------------------+ | |
0595bd7e | 885 | | GUD interaction buffer | Locals/Registers buffer | |
8cf51b2c GM |
886 | |--------------------------------+--------------------------------+ |
887 | | Primary Source buffer | I/O buffer for debugged pgm | | |
888 | |--------------------------------+--------------------------------+ | |
0df6175c | 889 | | Stack buffer | Breakpoints/Threads buffer | |
8cf51b2c GM |
890 | +--------------------------------+--------------------------------+ |
891 | @end group | |
892 | @end smallexample | |
893 | ||
8cf51b2c | 894 | @findex gdb-restore-windows |
8cf51b2c | 895 | @findex gdb-many-windows |
0595bd7e CY |
896 | If you ever change the window layout, you can restore the ``many |
897 | windows'' layout by typing @kbd{M-x gdb-restore-windows}. To toggle | |
898 | between the many windows layout and a simple layout with just the GUD | |
899 | interaction buffer and a source file, type @kbd{M-x gdb-many-windows}. | |
8cf51b2c GM |
900 | |
901 | You may also specify additional GDB-related buffers to display, | |
902 | either in the same frame or a different one. Select the buffers you | |
0595bd7e CY |
903 | want by typing @code{M-x gdb-display-@var{buffertype}-buffer} or |
904 | @code{M-x gdb-frame-@var{buffertype}-buffer}, where @var{buffertype} | |
905 | is the relevant buffer type, such as @samp{breakpoints}. You can do | |
906 | the same with the menu bar, with the @samp{GDB-Windows} and | |
907 | @samp{GDB-Frames} sub-menus of the @samp{GUD} menu. | |
908 | ||
909 | When you finish debugging, kill the GUD interaction buffer with | |
910 | @kbd{C-x k}, which will also kill all the buffers associated with the | |
911 | session. However you need not do this if, after editing and | |
912 | re-compiling your source code within Emacs, you wish to continue | |
913 | debugging. When you restart execution, GDB automatically finds the | |
914 | new executable. Keeping the GUD interaction buffer has the advantage | |
915 | of keeping the shell history as well as GDB's breakpoints. You do | |
916 | need to check that the breakpoints in recently edited source files are | |
917 | still in the right places. | |
8cf51b2c GM |
918 | |
919 | @node Source Buffers | |
920 | @subsubsection Source Buffers | |
0595bd7e | 921 | @cindex fringes, for debugging |
8cf51b2c | 922 | |
0595bd7e CY |
923 | @table @asis |
924 | @item @kbd{Mouse-1} (in fringe) | |
925 | Set or clear a breakpoint on that line. | |
8cf51b2c | 926 | |
0595bd7e CY |
927 | @item @kbd{C-Mouse-1} (in fringe) |
928 | Enable or disable a breakpoint on that line. | |
8cf51b2c | 929 | |
0595bd7e CY |
930 | @item @kbd{Mouse-3} (in fringe) |
931 | Continue execution to that line. | |
8cf51b2c | 932 | |
0595bd7e CY |
933 | @item @kbd{C-Mouse-3} (in fringe) |
934 | Jump to that line. | |
8cf51b2c GM |
935 | @end table |
936 | ||
0595bd7e CY |
937 | On a graphical display, you can click @kbd{Mouse-1} in the fringe of |
938 | a source buffer, to set a breakpoint on that line (@pxref{Fringes}). | |
939 | A red dot appears in the fringe, where you clicked. If a breakpoint | |
940 | already exists there, the click removes it. A @kbd{C-Mouse-1} click | |
941 | enables or disables an existing breakpoint; a breakpoint that is | |
942 | disabled, but not unset, is indicated by a gray dot. | |
943 | ||
0be641c0 | 944 | On a text terminal, or when fringes are disabled, enabled |
0595bd7e CY |
945 | breakpoints are indicated with a @samp{B} character in the left margin |
946 | of the window. Disabled breakpoints are indicated with @samp{b}. | |
947 | (The margin is only displayed if a breakpoint is present.) | |
948 | ||
949 | A solid arrow in the left fringe of a source buffer indicates the | |
950 | line of the innermost frame where the debugged program has stopped. A | |
951 | hollow arrow indicates the current execution line of a higher-level | |
952 | frame. If you drag the arrow in the fringe with @kbd{Mouse-1}, that | |
953 | causes execution to advance to the line where you release the button. | |
954 | Alternatively, you can click @kbd{Mouse-3} in the fringe to advance to | |
955 | that line. You can click @kbd{C-Mouse-3} in the fringe to jump to | |
956 | that line without executing the intermediate lines. This command | |
957 | allows you to go backwards, which can be useful for running through | |
958 | code that has already executed, in order to examine its execution in | |
959 | more detail. | |
8cf51b2c GM |
960 | |
961 | @node Breakpoints Buffer | |
962 | @subsubsection Breakpoints Buffer | |
963 | ||
0595bd7e CY |
964 | The GDB Breakpoints buffer shows the breakpoints, watchpoints and |
965 | catchpoints in the debugger session. @xref{Breakpoints,,, gdb, The | |
966 | GNU debugger}. It provides the following commands, which mostly apply | |
967 | to the @dfn{current breakpoint} (the breakpoint which point is on): | |
8cf51b2c GM |
968 | |
969 | @table @kbd | |
970 | @item @key{SPC} | |
0595bd7e | 971 | @kindex SPC @r{(GDB Breakpoints buffer)} |
8cf51b2c | 972 | @findex gdb-toggle-breakpoint |
0595bd7e CY |
973 | Enable/disable current breakpoint (@code{gdb-toggle-breakpoint}). On |
974 | a graphical display, this changes the color of the dot in the fringe | |
975 | of the source buffer at that line. The dot is red when the breakpoint | |
976 | is enabled, and gray when it is disabled. | |
8cf51b2c GM |
977 | |
978 | @item D | |
0595bd7e | 979 | @kindex D @r{(GDB Breakpoints buffer)} |
8cf51b2c GM |
980 | @findex gdb-delete-breakpoint |
981 | Delete the current breakpoint (@code{gdb-delete-breakpoint}). | |
982 | ||
983 | @item @key{RET} | |
0595bd7e | 984 | @kindex RET @r{(GDB Breakpoints buffer)} |
8cf51b2c GM |
985 | @findex gdb-goto-breakpoint |
986 | Visit the source line for the current breakpoint | |
987 | (@code{gdb-goto-breakpoint}). | |
988 | ||
989 | @item Mouse-2 | |
0595bd7e | 990 | @kindex Mouse-2 @r{(GDB Breakpoints buffer)} |
8cf51b2c GM |
991 | Visit the source line for the breakpoint you click on. |
992 | @end table | |
993 | ||
691cf4a0 | 994 | @vindex gdb-show-threads-by-default |
0595bd7e CY |
995 | When @code{gdb-many-windows} is non-@code{nil}, the GDB Breakpoints |
996 | buffer shares its window with the GDB Threads buffer. To switch from | |
997 | one to the other click with @kbd{Mouse-1} on the relevant button in | |
998 | the header line. If @code{gdb-show-threads-by-default} is | |
999 | non-@code{nil}, the GDB Threads buffer is the one shown by default. | |
691cf4a0 NR |
1000 | |
1001 | @node Threads Buffer | |
1002 | @subsubsection Threads Buffer | |
1003 | ||
1004 | @findex gdb-select-thread | |
0595bd7e CY |
1005 | The GDB Threads buffer displays a summary of the threads in the |
1006 | debugged program. @xref{Threads, Threads, Debugging programs with | |
1007 | multiple threads, gdb, The GNU debugger}. To select a thread, move | |
1008 | point there and type @key{RET} (@code{gdb-select-thread}), or click on | |
1009 | it with @kbd{Mouse-2}. This also displays the associated source | |
1010 | buffer, and updates the contents of the other GDB buffers. | |
691cf4a0 NR |
1011 | |
1012 | You can customize variables under @code{gdb-buffers} group to select | |
0595bd7e | 1013 | fields included in GDB Threads buffer. |
691cf4a0 NR |
1014 | |
1015 | @table @code | |
1016 | @item gdb-thread-buffer-verbose-names | |
1017 | @vindex gdb-thread-buffer-verbose-names | |
0595bd7e | 1018 | Show long thread names like @samp{Thread 0x4e2ab70 (LWP 1983)}. |
691cf4a0 NR |
1019 | |
1020 | @item gdb-thread-buffer-arguments | |
1021 | @vindex gdb-thread-buffer-arguments | |
0595bd7e | 1022 | Show arguments of thread top frames. |
691cf4a0 NR |
1023 | |
1024 | @item gdb-thread-buffer-locations | |
1025 | @vindex gdb-thread-buffer-locations | |
0595bd7e | 1026 | Show file information or library names. |
691cf4a0 NR |
1027 | |
1028 | @item gdb-thread-buffer-addresses | |
1029 | @vindex gdb-thread-buffer-addresses | |
1030 | Show addresses for thread frames in threads buffer. | |
1031 | @end table | |
1032 | ||
0595bd7e CY |
1033 | To view information for several threads simultaneously, use the |
1034 | following commands from the GDB Threads buffer. | |
691cf4a0 NR |
1035 | |
1036 | @table @kbd | |
1037 | @item d | |
1038 | @kindex d @r{(GDB threads buffer)} | |
1039 | @findex gdb-display-disassembly-for-thread | |
0595bd7e CY |
1040 | Display disassembly buffer for the thread at current line |
1041 | (@code{gdb-display-disassembly-for-thread}). | |
691cf4a0 NR |
1042 | |
1043 | @item f | |
1044 | @kindex f @r{(GDB threads buffer)} | |
1045 | @findex gdb-display-stack-for-thread | |
0595bd7e | 1046 | Display the GDB Stack buffer for the thread at current line |
691cf4a0 NR |
1047 | (@code{gdb-display-stack-for-thread}). |
1048 | ||
1049 | @item l | |
1050 | @kindex l @r{(GDB threads buffer)} | |
1051 | @findex gdb-display-locals-for-thread | |
0595bd7e | 1052 | Display the GDB Locals buffer for the thread at current line |
691cf4a0 NR |
1053 | (@code{gdb-display-locals-for-thread}). |
1054 | ||
1055 | @item r | |
1056 | @kindex r @r{(GDB threads buffer)} | |
1057 | @findex gdb-display-registers-for-thread | |
0595bd7e | 1058 | Display the GDB Registers buffer for the thread at current line |
691cf4a0 NR |
1059 | (@code{gdb-display-registers-for-thread}). |
1060 | @end table | |
1061 | ||
0595bd7e CY |
1062 | @noindent |
1063 | Their upper-case counterparts, @kbd{D}, @kbd{F} ,@kbd{L} and @kbd{R}, | |
1064 | display the corresponding buffer in a new frame. | |
691cf4a0 NR |
1065 | |
1066 | When you create a buffer showing information about some specific | |
1067 | thread, it becomes bound to that thread and keeps showing actual | |
0595bd7e CY |
1068 | information while you debug your program. The mode indicator for each |
1069 | GDB buffer shows the number of thread it is showing information about. | |
1070 | The thread number is also included in the buffer name of bound | |
1071 | buffers. | |
691cf4a0 | 1072 | |
0595bd7e CY |
1073 | Further commands are available in the GDB Threads buffer which |
1074 | depend on the mode of GDB that is used for controlling execution of | |
1075 | your program. @xref{Multithreaded Debugging}. | |
95dbaaea | 1076 | |
8cf51b2c GM |
1077 | @node Stack Buffer |
1078 | @subsubsection Stack Buffer | |
1079 | ||
0595bd7e CY |
1080 | The GDB Stack buffer displays a @dfn{call stack}, with one line for |
1081 | each of the nested subroutine calls (@dfn{stack frames}) in the | |
1082 | debugger session. @xref{Backtrace,, Backtraces, gdb, The GNU | |
1083 | debugger}. | |
8cf51b2c GM |
1084 | |
1085 | @findex gdb-frames-select | |
0595bd7e | 1086 | On graphical displays, the selected stack frame is indicated by an |
0be641c0 CY |
1087 | arrow in the fringe. On text terminals, or when fringes are disabled, |
1088 | the selected stack frame is displayed in reverse contrast. To select | |
1089 | a stack frame, move point in its line and type @key{RET} | |
0595bd7e CY |
1090 | (@code{gdb-frames-select}), or click @kbd{Mouse-2} on it. Doing so |
1091 | also updates the Locals buffer | |
1092 | @ifnottex | |
1093 | (@pxref{Other GDB Buffers}). | |
1094 | @end ifnottex | |
1095 | @iftex | |
1096 | (described in the next section). | |
1097 | @end iftex | |
8cf51b2c | 1098 | |
691cf4a0 | 1099 | @node Other GDB Buffers |
0595bd7e | 1100 | @subsubsection Other GDB Buffers |
8cf51b2c GM |
1101 | |
1102 | @table @asis | |
8cf51b2c | 1103 | @item Locals Buffer |
0595bd7e CY |
1104 | This buffer displays the values of local variables of the current |
1105 | frame for simple data types (@pxref{Frame Info, Frame Info, | |
0df6175c | 1106 | Information on a frame, gdb, The GNU debugger}). Press @key{RET} or |
8cf51b2c GM |
1107 | click @kbd{Mouse-2} on the value if you want to edit it. |
1108 | ||
1109 | Arrays and structures display their type only. With GDB 6.4 or later, | |
0595bd7e CY |
1110 | you can examine the value of the local variable at point by typing |
1111 | @key{RET}, or with a @kbd{Mouse-2} click. With earlier versions of | |
1112 | GDB, use @key{RET} or @kbd{Mouse-2} on the type description | |
8cf51b2c GM |
1113 | (@samp{[struct/union]} or @samp{[array]}). @xref{Watch Expressions}. |
1114 | ||
1115 | @item Registers Buffer | |
1116 | @findex toggle-gdb-all-registers | |
0595bd7e | 1117 | This buffer displays the values held by the registers |
8cf51b2c | 1118 | (@pxref{Registers,,, gdb, The GNU debugger}). Press @key{RET} or |
0595bd7e CY |
1119 | click @kbd{Mouse-2} on a register if you want to edit its value. With |
1120 | GDB 6.4 or later, recently changed register values display with | |
1121 | @code{font-lock-warning-face}. | |
8cf51b2c | 1122 | |
0df6175c CY |
1123 | @item Assembler Buffer |
1124 | The assembler buffer displays the current frame as machine code. An | |
8cf51b2c GM |
1125 | arrow points to the current instruction, and you can set and remove |
1126 | breakpoints as in a source buffer. Breakpoint icons also appear in | |
1127 | the fringe or margin. | |
1128 | ||
8cf51b2c GM |
1129 | @item Memory Buffer |
1130 | The memory buffer lets you examine sections of program memory | |
1131 | (@pxref{Memory, Memory, Examining memory, gdb, The GNU debugger}). | |
1132 | Click @kbd{Mouse-1} on the appropriate part of the header line to | |
1133 | change the starting address or number of data items that the buffer | |
4a3a621f NR |
1134 | displays. Alternatively, use @kbd{S} or @kbd{N} respectively. Click |
1135 | @kbd{Mouse-3} on the header line to select the display format or unit | |
1136 | size for these data items. | |
8cf51b2c GM |
1137 | @end table |
1138 | ||
691cf4a0 | 1139 | When @code{gdb-many-windows} is non-@code{nil}, the locals buffer |
0595bd7e CY |
1140 | shares its window with the registers buffer, just like breakpoints and |
1141 | threads buffers. To switch from one to the other, click with | |
0df6175c | 1142 | @kbd{Mouse-1} on the relevant button in the header line. |
1a19cb6a | 1143 | |
8cf51b2c GM |
1144 | @node Watch Expressions |
1145 | @subsubsection Watch Expressions | |
1146 | @cindex Watching expressions in GDB | |
1147 | ||
1148 | @findex gud-watch | |
1149 | @kindex C-x C-a C-w @r{(GUD)} | |
1150 | If you want to see how a variable changes each time your program | |
1151 | stops, move point into the variable name and click on the watch icon | |
1152 | in the tool bar (@code{gud-watch}) or type @kbd{C-x C-a C-w}. If you | |
1153 | specify a prefix argument, you can enter the variable name in the | |
1154 | minibuffer. | |
1155 | ||
0595bd7e CY |
1156 | Each watch expression is displayed in the speedbar |
1157 | (@pxref{Speedbar}). Complex data types, such as arrays, structures | |
1158 | and unions are represented in a tree format. Leaves and simple data | |
1159 | types show the name of the expression and its value and, when the | |
1160 | speedbar frame is selected, display the type as a tooltip. Higher | |
1161 | levels show the name, type and address value for pointers and just the | |
1162 | name and type otherwise. Root expressions also display the frame | |
1163 | address as a tooltip to help identify the frame in which they were | |
1164 | defined. | |
8cf51b2c GM |
1165 | |
1166 | To expand or contract a complex data type, click @kbd{Mouse-2} or | |
1167 | press @key{SPC} on the tag to the left of the expression. Emacs asks | |
1168 | for confirmation before expanding the expression if its number of | |
1169 | immediate children exceeds the value of the variable | |
1170 | @code{gdb-max-children}. | |
1171 | ||
1172 | @kindex D @r{(GDB speedbar)} | |
1173 | @findex gdb-var-delete | |
1174 | To delete a complex watch expression, move point to the root | |
1175 | expression in the speedbar and type @kbd{D} (@code{gdb-var-delete}). | |
1176 | ||
1177 | @kindex RET @r{(GDB speedbar)} | |
1178 | @findex gdb-edit-value | |
1179 | To edit a variable with a simple data type, or a simple element of a | |
1180 | complex data type, move point there in the speedbar and type @key{RET} | |
1181 | (@code{gdb-edit-value}). Or you can click @kbd{Mouse-2} on a value to | |
1182 | edit it. Either way, this reads the new value using the minibuffer. | |
1183 | ||
1184 | @vindex gdb-show-changed-values | |
1185 | If you set the variable @code{gdb-show-changed-values} to | |
1186 | non-@code{nil} (the default value), Emacs uses | |
1187 | @code{font-lock-warning-face} to highlight values that have recently | |
1188 | changed and @code{shadow} face to make variables which have gone out of | |
1189 | scope less noticeable. When a variable goes out of scope you can't | |
1190 | edit its value. | |
1191 | ||
975900f9 | 1192 | @vindex gdb-delete-out-of-scope |
32ef39ff NR |
1193 | If the variable @code{gdb-delete-out-of-scope} is non-@code{nil} |
1194 | (the default value), Emacs automatically deletes watch expressions | |
1195 | which go out of scope. Sometimes, when re-entering the same function, | |
de933755 CY |
1196 | it may be useful to set this value to @code{nil} so that you don't |
1197 | need to recreate the watch expression. | |
975900f9 | 1198 | |
8cf51b2c GM |
1199 | @vindex gdb-use-colon-colon-notation |
1200 | If the variable @code{gdb-use-colon-colon-notation} is | |
1201 | non-@code{nil}, Emacs uses the @samp{@var{function}::@var{variable}} | |
1202 | format. This allows the user to display watch expressions which share | |
1203 | the same variable name. The default value is @code{nil}. | |
1204 | ||
1205 | @vindex gdb-speedbar-auto-raise | |
1206 | To automatically raise the speedbar every time the display of watch | |
1207 | expressions updates, set @code{gdb-speedbar-auto-raise} to | |
1208 | non-@code{nil}. This can be useful if you are debugging with a full | |
1209 | screen Emacs frame. | |
1210 | ||
691cf4a0 | 1211 | @node Multithreaded Debugging |
0595bd7e | 1212 | @subsubsection Multithreaded Debugging |
691cf4a0 | 1213 | @cindex Multithreaded debugging in GDB |
691cf4a0 NR |
1214 | @cindex Non-stop debugging in GDB |
1215 | ||
0595bd7e CY |
1216 | In GDB's @dfn{all-stop mode}, whenever your program stops, all |
1217 | execution threads stop. Likewise, whenever you restart the program, | |
1218 | all threads start executing. @xref{All-Stop Mode, , All-Stop Mode, | |
1219 | gdb, The GNU debugger}. For some multi-threaded targets, GDB supports | |
1220 | a further mode of operation, called @dfn{non-stop mode}, in which you | |
1221 | can examine stopped program threads in the debugger while other | |
1222 | threads continue to execute freely. @xref{Non-Stop Mode, , Non-Stop | |
1223 | Mode, gdb, The GNU debugger}. Versions of GDB prior to 7.0 do not | |
1224 | support non-stop mode, and it does not work on all targets. | |
691cf4a0 NR |
1225 | |
1226 | @vindex gdb-non-stop-setting | |
0595bd7e CY |
1227 | The variable @code{gdb-non-stop-setting} determines whether Emacs |
1228 | runs GDB in all-stop mode or non-stop mode. The default is @code{t}, | |
1229 | which means it tries to use non-stop mode if that is available. If | |
1230 | you change the value to @code{nil}, or if non-stop mode is | |
1231 | unavailable, Emacs runs GDB in all-stop mode. The variable takes | |
1232 | effect when Emacs begins a debugging session; if you change its value, | |
1233 | you should restart any active debugging session. | |
691cf4a0 NR |
1234 | |
1235 | @vindex gdb-switch-when-another-stopped | |
0595bd7e CY |
1236 | When a thread stops in non-stop mode, Emacs usually switches to that |
1237 | thread. If you don't want Emacs to do this switch if another stopped | |
1238 | thread is already selected, change the variable | |
1239 | @code{gdb-switch-when-another-stopped} to @code{nil}. | |
691cf4a0 NR |
1240 | |
1241 | @vindex gdb-switch-reasons | |
0595bd7e CY |
1242 | Emacs can decide whether or not to switch to the stopped thread |
1243 | depending on the reason which caused the stop. Customize the variable | |
1244 | @code{gdb-switch-reasons} to select the stop reasons which will cause | |
1245 | a thread switch. | |
691cf4a0 | 1246 | |
0f5414fc CY |
1247 | @vindex gdb-stopped-functions |
1248 | The variable @code{gdb-stopped-functions} allows you to execute your | |
691cf4a0 NR |
1249 | functions whenever some thread stops. |
1250 | ||
1251 | In non-stop mode, you can switch between different modes for GUD | |
1252 | execution control commands. | |
1253 | ||
1254 | @vindex gdb-gud-control-all-threads | |
1255 | @table @dfn | |
1256 | @item Non-stop/A | |
1257 | ||
0595bd7e | 1258 | When @code{gdb-gud-control-all-threads} is @code{t} (the default |
691cf4a0 NR |
1259 | value), interruption and continuation commands apply to all threads, |
1260 | so you can halt or continue all your threads with one command using | |
1261 | @code{gud-stop-subjob} and @code{gud-cont}, respectively. The | |
1262 | @samp{Go} button is shown on the toolbar when at least one thread is | |
1263 | stopped, whereas @samp{Stop} button is shown when at least one thread | |
1264 | is running. | |
1265 | ||
1266 | @item Non-stop/T | |
1267 | ||
1268 | When @code{gdb-gud-control-all-threads} is @code{nil}, only the | |
1269 | current thread is stopped/continued. @samp{Go} and @samp{Stop} | |
1270 | buttons on the GUD toolbar are shown depending on the state of current | |
1271 | thread. | |
1272 | @end table | |
1273 | ||
1274 | You can change the current value of @code{gdb-gud-control-all-threads} | |
1275 | from the tool bar or from @samp{GUD->GDB-MI} menu. | |
1276 | ||
1277 | Stepping commands always apply to the current thread. | |
1278 | ||
691cf4a0 NR |
1279 | In non-stop mode, you can interrupt/continue your threads without |
1280 | selecting them. Hitting @kbd{i} in threads buffer interrupts thread | |
1281 | under point, @kbd{c} continues it, @kbd{s} steps through. More such | |
1282 | commands may be added in the future. | |
1283 | ||
0595bd7e CY |
1284 | Note that when you interrupt a thread, it stops with the |
1285 | @samp{signal received} reason. If that reason is included in your | |
691cf4a0 NR |
1286 | @code{gdb-switch-reasons} (it is by default), Emacs will switch to |
1287 | that thread. | |
8d6bb99e | 1288 | |
8cf51b2c GM |
1289 | @node Executing Lisp |
1290 | @section Executing Lisp Expressions | |
1291 | ||
29eabb8e CY |
1292 | Emacs has major modes for several variants of Lisp. They use the |
1293 | same editing commands as other programming language modes | |
1294 | (@pxref{Programs}). In addition, they provide special commands for | |
1295 | executing Lisp expressions. | |
8cf51b2c GM |
1296 | |
1297 | @table @asis | |
29eabb8e CY |
1298 | @item Emacs Lisp mode |
1299 | The mode for editing Emacs Lisp source files. It defines @kbd{C-M-x} | |
1300 | to evaluate the current top-level Lisp expression. @xref{Lisp Eval}. | |
1301 | ||
8cf51b2c | 1302 | @item Lisp Interaction mode |
29eabb8e CY |
1303 | The mode for an interactive Emacs Lisp session. It defines @kbd{C-j} |
1304 | to evaluate the expression before point and insert its value in the | |
8cf51b2c | 1305 | buffer. @xref{Lisp Interaction}. |
29eabb8e | 1306 | |
8cf51b2c GM |
1307 | @item Lisp mode |
1308 | The mode for editing source files of programs that run in Lisps other | |
29eabb8e CY |
1309 | than Emacs Lisp. It defines @kbd{C-M-x} to evaluate the current |
1310 | top-level expression in an external Lisp. @xref{External Lisp}. | |
1311 | ||
8cf51b2c | 1312 | @item Inferior Lisp mode |
29eabb8e CY |
1313 | The mode for an interactive session with an external Lisp which is |
1314 | being run as a subprocess (or @dfn{inferior process}) of Emacs. | |
1315 | @ifnottex | |
1316 | @xref{External Lisp}. | |
1317 | @end ifnottex | |
1318 | ||
8cf51b2c | 1319 | @item Scheme mode |
29eabb8e CY |
1320 | Like Lisp mode, but for Scheme programs. |
1321 | ||
8cf51b2c | 1322 | @item Inferior Scheme mode |
29eabb8e | 1323 | Like Inferior Lisp mode, but for Scheme. |
8cf51b2c GM |
1324 | @end table |
1325 | ||
8cf51b2c GM |
1326 | @node Lisp Libraries |
1327 | @section Libraries of Lisp Code for Emacs | |
1328 | @cindex libraries | |
1329 | @cindex loading Lisp code | |
1330 | ||
29eabb8e CY |
1331 | Emacs Lisp code is stored in files whose names conventionally end in |
1332 | @file{.el}. Such files are automatically visited in Emacs Lisp mode. | |
8cf51b2c GM |
1333 | |
1334 | @cindex byte code | |
1335 | Emacs Lisp code can be compiled into byte-code, which loads faster, | |
29eabb8e CY |
1336 | takes up less space, and executes faster. By convention, compiled |
1337 | Emacs Lisp code goes in a separate file whose name ends in | |
1338 | @samp{.elc}. For example, the compiled code for @file{foo.el} goes in | |
1339 | @file{foo.elc}. @xref{Byte Compilation,, Byte Compilation, elisp, the | |
1340 | Emacs Lisp Reference Manual}. | |
8cf51b2c GM |
1341 | |
1342 | @findex load-file | |
29eabb8e CY |
1343 | To @dfn{load} an Emacs Lisp file, type @kbd{M-x load-file}. This |
1344 | command reads a file name using the minibuffer, and executes the | |
1345 | contents of that file as Emacs Lisp code. It is not necessary to | |
1346 | visit the file first; this command reads the file directly from disk, | |
1347 | not from an existing Emacs buffer. | |
8cf51b2c GM |
1348 | |
1349 | @findex load | |
1350 | @findex load-library | |
ec918aab | 1351 | @vindex load-prefer-newer |
29eabb8e CY |
1352 | @cindex load path for Emacs Lisp |
1353 | If an Emacs Lisp file is installed in the Emacs Lisp @dfn{load path} | |
1354 | (defined below), you can load it by typing @kbd{M-x load-library}, | |
1355 | instead of using @kbd{M-x load-file}. The @kbd{M-x load-library} | |
1356 | command prompts for a @dfn{library name} rather than a file name; it | |
1357 | searches through each directory in the Emacs Lisp load path, trying to | |
1358 | find a file matching that library name. If the library name is | |
1359 | @samp{@var{foo}}, it tries looking for files named | |
ec918aab RS |
1360 | @file{@var{foo}.elc}, @file{@var{foo}.el}, and @file{@var{foo}}. The |
1361 | default behaviour is to load the first file found. This command | |
1362 | prefers @file{.elc} files over @file{.el} files because compiled files | |
1363 | load and run faster. If it finds that @file{@var{lib}.el} is newer | |
1364 | than @file{@var{lib}.elc}, it issues a warning, in case someone made | |
29eabb8e CY |
1365 | changes to the @file{.el} file and forgot to recompile it, but loads |
1366 | the @file{.elc} file anyway. (Due to this behavior, you can save | |
1367 | unfinished edits to Emacs Lisp source files, and not recompile until | |
97a42888 GM |
1368 | your changes are ready for use.) If you set the option |
1369 | @code{load-prefer-newer} to a non-@code{nil} value, however, then | |
1370 | rather than the procedure described above, Emacs loads whichever | |
1371 | version of the file is newest. | |
29eabb8e CY |
1372 | |
1373 | Emacs Lisp programs usually load Emacs Lisp files using the | |
1374 | @code{load} function. This is similar to @code{load-library}, but is | |
1375 | lower-level and accepts additional arguments. @xref{How Programs Do | |
1376 | Loading,,, elisp, the Emacs Lisp Reference Manual}. | |
8cf51b2c | 1377 | |
8cf51b2c | 1378 | @vindex load-path |
29eabb8e CY |
1379 | The Emacs Lisp load path is specified by the variable |
1380 | @code{load-path}. Its value should be a list of directory names | |
1381 | (strings). These directories are searched, in the specified order, by | |
1382 | the @kbd{M-x load-library} command, the lower-level @code{load} | |
1383 | function, and other Emacs functions that find Emacs Lisp libraries. A | |
1384 | list entry in @code{load-path} can also have the special value | |
1385 | @code{nil}, which stands for the current default directory, but it is | |
1386 | almost always a bad idea to use this. (If you find yourself wishing | |
f3ed4f26 | 1387 | that @code{nil} were in the list, most likely what you really want is |
29eabb8e | 1388 | to use @kbd{M-x load-file}.) |
1557ef4f CY |
1389 | |
1390 | The default value of @code{load-path} is a list of directories where | |
8cf51b2c | 1391 | the Lisp code for Emacs itself is stored. If you have libraries of |
29eabb8e CY |
1392 | your own in another directory, you can add that directory to the load |
1393 | path. Unlike most other variables described in this manual, | |
1394 | @code{load-path} cannot be changed via the Customize interface | |
1395 | (@pxref{Easy Customization}), but you can add a directory to it by | |
1396 | putting a line like this in your init file (@pxref{Init File}): | |
1557ef4f CY |
1397 | |
1398 | @example | |
29eabb8e | 1399 | (add-to-list 'load-path "/path/to/my/lisp/library") |
1557ef4f | 1400 | @end example |
8cf51b2c GM |
1401 | |
1402 | @cindex autoload | |
48de8b12 | 1403 | Some commands are @dfn{autoloaded}; when you run them, Emacs |
29eabb8e CY |
1404 | automatically loads the associated library first. For instance, the |
1405 | @kbd{M-x compile} command (@pxref{Compilation}) is autoloaded; if you | |
1406 | call it, Emacs automatically loads the @code{compile} library first. | |
1407 | In contrast, the command @kbd{M-x recompile} is not autoloaded, so it | |
1408 | is unavailable until you load the @code{compile} library. | |
8cf51b2c | 1409 | |
48de8b12 CY |
1410 | @vindex help-enable-auto-load |
1411 | Automatic loading can also occur when you look up the documentation | |
1412 | of an autoloaded command (@pxref{Name Help}), if the documentation | |
1413 | refers to other functions and variables in its library (loading the | |
1414 | library lets Emacs properly set up the hyperlinks in the @file{*Help*} | |
1415 | buffer). To disable this feature, change the variable | |
1416 | @code{help-enable-auto-load} to @code{nil}. | |
1417 | ||
8cf51b2c GM |
1418 | @vindex load-dangerous-libraries |
1419 | @cindex Lisp files byte-compiled by XEmacs | |
1420 | By default, Emacs refuses to load compiled Lisp files which were | |
1421 | compiled with XEmacs, a modified versions of Emacs---they can cause | |
1422 | Emacs to crash. Set the variable @code{load-dangerous-libraries} to | |
1423 | @code{t} if you want to try loading them. | |
1424 | ||
1425 | @node Lisp Eval | |
1426 | @section Evaluating Emacs Lisp Expressions | |
29eabb8e CY |
1427 | @cindex Emacs Lisp mode |
1428 | @cindex mode, Emacs Lisp | |
595c4578 | 1429 | @cindex evaluation, Emacs Lisp |
8cf51b2c GM |
1430 | |
1431 | @findex emacs-lisp-mode | |
29eabb8e CY |
1432 | Emacs Lisp mode is the major mode for editing Emacs Lisp. Its mode |
1433 | command is @kbd{M-x emacs-lisp-mode}. | |
8cf51b2c | 1434 | |
29eabb8e CY |
1435 | Emacs provides several commands for evaluating Emacs Lisp |
1436 | expressions. You can use these commands in Emacs Lisp mode, to test | |
1437 | your Emacs Lisp code as it is being written. For example, after | |
1438 | re-writing a function, you can evaluate the function definition to | |
1439 | make it take effect for subsequent function calls. These commands are | |
1440 | also available globally, and can be used outside Emacs Lisp mode. | |
1441 | ||
1442 | @table @asis | |
1443 | @item @kbd{M-:} | |
1444 | Read a single Emacs Lisp expression in the minibuffer, evaluate it, | |
1445 | and print the value in the echo area (@code{eval-expression}). | |
1446 | @item @kbd{C-x C-e} | |
1447 | Evaluate the Emacs Lisp expression before point, and print the value | |
1448 | in the echo area (@code{eval-last-sexp}). | |
1449 | @item @kbd{C-M-x} @r{(in Emacs Lisp mode)} | |
1450 | @itemx @kbd{M-x eval-defun} | |
8cf51b2c GM |
1451 | Evaluate the defun containing or after point, and print the value in |
1452 | the echo area (@code{eval-defun}). | |
29eabb8e CY |
1453 | @item @kbd{M-x eval-region} |
1454 | Evaluate all the Emacs Lisp expressions in the region. | |
1455 | @item @kbd{M-x eval-buffer} | |
1456 | Evaluate all the Emacs Lisp expressions in the buffer. | |
8cf51b2c GM |
1457 | @end table |
1458 | ||
1459 | @ifinfo | |
1460 | @c This uses ``colon'' instead of a literal `:' because Info cannot | |
1461 | @c cope with a `:' in a menu | |
1462 | @kindex M-@key{colon} | |
1463 | @end ifinfo | |
1464 | @ifnotinfo | |
1465 | @kindex M-: | |
1466 | @end ifnotinfo | |
1467 | @findex eval-expression | |
29eabb8e CY |
1468 | @kbd{M-:} (@code{eval-expression}) reads an expression using the |
1469 | minibuffer, and evaluates it. (Before evaluating the expression, the | |
1470 | current buffer switches back to the buffer that was current when you | |
1471 | typed @kbd{M-:}, not the minibuffer into which you typed the | |
1472 | expression.) | |
8cf51b2c GM |
1473 | |
1474 | @kindex C-x C-e | |
1475 | @findex eval-last-sexp | |
29eabb8e CY |
1476 | The command @kbd{C-x C-e} (@code{eval-last-sexp}) evaluates the |
1477 | Emacs Lisp expression preceding point in the buffer, and displays the | |
1478 | value in the echo area. When the result of an evaluation is an | |
c51e81ef GM |
1479 | integer, it is displayed together with the value in other formats |
1480 | (octal, hexadecimal, and character). | |
29eabb8e CY |
1481 | |
1482 | If @kbd{M-:} or @kbd{C-x C-e} is given a prefix argument, it inserts | |
1483 | the value into the current buffer at point, rather than displaying it | |
c51e81ef GM |
1484 | in the echo area. If the prefix argument is zero, any integer output |
1485 | is inserted together with its value in other formats (octal, | |
1486 | hexadecimal, and character). Such a prefix argument also prevents | |
1487 | abbreviation of the output according to the variables | |
1488 | @code{eval-expression-print-level} and @code{eval-expression-print-length} | |
1489 | (see below). | |
29eabb8e CY |
1490 | |
1491 | @kindex C-M-x @r{(Emacs Lisp mode)} | |
1492 | @findex eval-defun | |
1493 | The @code{eval-defun} command is bound to @kbd{C-M-x} in Emacs Lisp | |
1494 | mode. It evaluates the top-level Lisp expression containing or | |
1495 | following point, and prints the value in the echo area. In this | |
1496 | context, a top-level expression is referred to as a ``defun'', but it | |
1497 | need not be an actual @code{defun} (function definition). In | |
1498 | particular, this command treats @code{defvar} expressions specially. | |
1499 | Normally, evaluating a @code{defvar} expression does nothing if the | |
1500 | variable it defines already has a value. But this command | |
1501 | unconditionally resets the variable to the initial value specified by | |
1502 | the @code{defvar}; this is convenient for debugging Emacs Lisp | |
1503 | programs. @code{defcustom} and @code{defface} expressions are treated | |
1504 | similarly. Note that the other commands documented in this section do | |
1505 | not have this special feature. | |
1506 | ||
1507 | With a prefix argument, @kbd{C-M-x} instruments the function | |
1508 | definition for Edebug, the Emacs Lisp Debugger. @xref{Instrumenting, | |
1509 | Instrumenting for Edebug,, elisp, the Emacs Lisp Reference Manual}. | |
8cf51b2c GM |
1510 | |
1511 | @findex eval-region | |
1512 | @findex eval-buffer | |
29eabb8e CY |
1513 | The command @kbd{M-x eval-region} parses the text of the region as |
1514 | one or more Lisp expressions, evaluating them one by one. @kbd{M-x | |
1515 | eval-buffer} is similar but evaluates the entire buffer. | |
8cf51b2c GM |
1516 | |
1517 | @vindex eval-expression-print-level | |
1518 | @vindex eval-expression-print-length | |
1519 | @vindex eval-expression-debug-on-error | |
84f4a531 | 1520 | The options @code{eval-expression-print-level} and |
29eabb8e CY |
1521 | @code{eval-expression-print-length} control the maximum depth and |
1522 | length of lists to print in the result of the evaluation commands | |
c51e81ef GM |
1523 | before abbreviating them. Supplying a zero prefix argument to |
1524 | @code{eval-expression} or @code{eval-last-sexp} causes lists to be | |
1525 | printed in full. @code{eval-expression-debug-on-error} controls | |
1526 | whether evaluation errors invoke the debugger when these commands are | |
1527 | used; its default is @code{t}. | |
8cf51b2c GM |
1528 | |
1529 | @node Lisp Interaction | |
1530 | @section Lisp Interaction Buffers | |
1531 | ||
29eabb8e | 1532 | @findex lisp-interaction-mode |
1c64e6ed | 1533 | When Emacs starts up, it contains a buffer named @file{*scratch*}, |
29eabb8e CY |
1534 | which is provided for evaluating Emacs Lisp expressions interactively. |
1535 | Its major mode is Lisp Interaction mode. You can also enable Lisp | |
1536 | Interaction mode by typing @kbd{M-x lisp-interaction-mode}. | |
8cf51b2c | 1537 | |
a03334ca CY |
1538 | @findex eval-print-last-sexp |
1539 | @kindex C-j @r{(Lisp Interaction mode)} | |
1c64e6ed | 1540 | In the @file{*scratch*} buffer, and other Lisp Interaction mode |
29eabb8e CY |
1541 | buffers, @kbd{C-j} (@code{eval-print-last-sexp}) evaluates the Lisp |
1542 | expression before point, and inserts the value at point. Thus, as you | |
1543 | type expressions into the buffer followed by @kbd{C-j} after each | |
1544 | expression, the buffer records a transcript of the evaluated | |
1545 | expressions and their values. All other commands in Lisp Interaction | |
1546 | mode are the same as in Emacs Lisp mode. | |
8cf51b2c | 1547 | |
be77bd45 | 1548 | @vindex initial-scratch-message |
1c64e6ed | 1549 | At startup, the @file{*scratch*} buffer contains a short message, in |
be77bd45 CY |
1550 | the form of a Lisp comment, that explains what it is for. This |
1551 | message is controlled by the variable @code{initial-scratch-message}, | |
29eabb8e CY |
1552 | which should be either a string, or @code{nil} (which means to |
1553 | suppress the message). | |
8cf51b2c GM |
1554 | |
1555 | @findex ielm | |
29eabb8e CY |
1556 | An alternative way of evaluating Emacs Lisp expressions |
1557 | interactively is to use Inferior Emacs Lisp mode, which provides an | |
1558 | interface rather like Shell mode (@pxref{Shell Mode}) for evaluating | |
1559 | Emacs Lisp expressions. Type @kbd{M-x ielm} to create an | |
1c64e6ed | 1560 | @file{*ielm*} buffer which uses this mode. For more information, see |
29eabb8e | 1561 | that command's documentation. |
8cf51b2c GM |
1562 | |
1563 | @node External Lisp | |
1564 | @section Running an External Lisp | |
29eabb8e CY |
1565 | @cindex Lisp mode |
1566 | @cindex mode, Lisp | |
1567 | @cindex Common Lisp | |
8cf51b2c | 1568 | |
29eabb8e CY |
1569 | Lisp mode is the major mode for editing programs written in |
1570 | general-purpose Lisp dialects, such as Common Lisp. Its mode command | |
1571 | is @kbd{M-x lisp-mode}. Emacs uses Lisp mode automatically for files | |
1572 | whose names end in @file{.l}, @file{.lsp}, or @file{.lisp}. | |
8cf51b2c GM |
1573 | |
1574 | @findex run-lisp | |
1575 | @vindex inferior-lisp-program | |
1576 | @kindex C-x C-z | |
29eabb8e CY |
1577 | You can run an external Lisp session as a subprocess or |
1578 | @dfn{inferior process} of Emacs, and pass expressions to it to be | |
1579 | evaluated. To begin an external Lisp session, type @kbd{M-x | |
1580 | run-lisp}. This runs the program named @command{lisp}, and sets it up | |
1581 | so that both input and output go through an Emacs buffer named | |
1c64e6ed | 1582 | @file{*inferior-lisp*}. To change the name of the Lisp program run by |
29eabb8e CY |
1583 | @kbd{M-x run-lisp}, change the variable @code{inferior-lisp-program}. |
1584 | ||
1c64e6ed | 1585 | The major mode for the @file{*lisp*} buffer is Inferior Lisp mode, |
29eabb8e CY |
1586 | which combines the characteristics of Lisp mode and Shell mode |
1587 | (@pxref{Shell Mode}). To send input to the Lisp session, go to the | |
1c64e6ed | 1588 | end of the @file{*lisp*} buffer and type the input, followed by |
29eabb8e CY |
1589 | @key{RET}. Terminal output from the Lisp session is automatically |
1590 | inserted in the buffer. | |
8cf51b2c GM |
1591 | |
1592 | @kindex C-M-x @r{(Lisp mode)} | |
1593 | @findex lisp-eval-defun | |
29eabb8e CY |
1594 | When you edit a Lisp program in Lisp mode, you can type @kbd{C-M-x} |
1595 | (@code{lisp-eval-defun}) to send an expression from the Lisp mode | |
1596 | buffer to a Lisp session that you had started with @kbd{M-x run-lisp}. | |
1597 | The expression sent is the top-level Lisp expression at or following | |
1598 | point. The resulting value goes as usual into the | |
1c64e6ed | 1599 | @file{*inferior-lisp*} buffer. Note that the effect of @kbd{C-M-x} in |
29eabb8e CY |
1600 | Lisp mode is thus very similar to its effect in Emacs Lisp mode |
1601 | (@pxref{Lisp Eval}), except that the expression is sent to a different | |
1602 | Lisp environment instead of being evaluated in Emacs. | |
1603 | ||
1604 | @findex scheme-mode | |
1605 | @findex run-scheme | |
1606 | @cindex Scheme mode | |
1607 | @cindex mode, Scheme | |
1608 | @kindex C-M-x @r{(Scheme mode)} | |
1609 | The facilities for editing Scheme code, and for sending expressions | |
1610 | to a Scheme subprocess, are very similar. Scheme source files are | |
1611 | edited in Scheme mode, which can be explicitly enabled with @kbd{M-x | |
1612 | scheme-mode}. You can initiate a Scheme session by typing @kbd{M-x | |
1613 | run-scheme} (the buffer for interacting with Scheme is named | |
1c64e6ed | 1614 | @file{*scheme*}), and send expressions to it by typing @kbd{C-M-x}. |