1 \input texinfo @c -*-texinfo-*-
3 @setfilename distel.info
4 @settitle Distel User Manual
7 @set EDITION Distel Jungerl
12 @title Distel User Manual
13 @subtitle @value{EDITION}, updated @value{UPDATED}
19 * Distel: (distel). Erlang development environment.
22 @c @setchapternewpage off
26 @node Top, Introduction, (dir), (dir)
27 @comment node-name, next, previous, up
32 This is the user manual for Distel.
41 --- The Detailed Node Listing ---
45 * Principles:: The underlying model of operation.
46 * Conventions:: Common conventions for commands.
50 * Tags:: Looking up function definitions.
51 * Completion:: Completing module and function names.
52 * Evaluation:: Evaluating snippets and reloading modules.
54 * Documentation:: Looking up online documentation.
58 * Process Manager:: See and manipulate Erlang processes.
59 * Debugger:: Debug Erlang programs.
60 * Interactive Sessions:: Hybrid Emacs buffer / Erlang shell.
61 * Profiler:: Profile with fprof.
65 * Basic Commands:: Interpreting modules and setting breakpoints.
66 * Monitor Buffer:: Monitoring interpreted processes.
67 * Attach Buffer:: Single-stepping a process.
68 * Synchronising Breakpoints:: Resynchronising breakpoints after edits.
69 * Saving and Restoring:: Temporarily saving debugging state.
74 @node Introduction, Programming Aids, Top, Top
77 Distel is a set of Emacs-based programs for interacting with running
78 Erlang nodes. The purpose is to make everyday Erlang programming tasks
79 easier -- looking up function definitions, debugging and profiling,
80 experimenting with code snippets, and so on. It builds on the existing
81 @code{erlang-mode} to provide more of the features common to
82 Integrated Development Environments.
84 This manual describes Distel from the user's point of view. For
85 details on how Distel works and how to write your own Distel-based
86 programs, see the paper @cite{Distel: Distributed Emacs Lisp (for
87 Erlang)} from the proceedings of the 2002 Erlang User Conference. The
88 paper is also available from the Distel home page,
89 @url{http://distel.sourceforge.net}
92 The overall principles of Distel's operation and use are described in
93 the following sections.
97 * Principles:: The underlying model of operation.
98 * Conventions:: Common conventions for commands.
101 @node Principles, Conventions, Introduction, Introduction
102 @section Principles of Operation
104 Distel works by talking to Erlang nodes using the Erlang distribution
105 protocol. It creates an ``Emacs node,'' similar to the ``C nodes'' of
106 @code{erl_interface}, and then talks directly to Erlang nodes with RPC
107 and other forms of message-passing.
109 Of course, this means that to use Distel you need to have an Erlang
110 node running. The node should be able to load some supporting modules
111 for Distel to make RPCs to -- setting this up is simple, and described
112 in the @file{INSTALL} file in the distribution. Other aspects of the
113 node's setup, such as which other modules it can find, will also
114 affect Distel's operation. More on this in the relevant sections.
116 @node Conventions, , Principles, Introduction
117 @section Conventions of Use
119 Most Distel commands need to know which Erlang node to talk to.
120 (Distel doesn't start an Erlang node, you have to do that yourself.)
121 The first command you use will prompt in the minibuffer for the name
122 of the node to use. You can answer with either a @code{name@@host}
123 node name, or with just the @code{name} part as an abbreviation for a
124 node on the local machine.
126 As a convenience, the node name you enter is cached and then reused in
127 future commands. If you later want to talk to a different node you can
128 use the command @code{erl-choose-nodename} (@kbd{C-c C-d n}) to select
129 a new node to talk to. The currently cached node name is always shown
132 Some commands accept a prefix argument to alter their behaviour in
133 some specific way. You can give a prefix with @kbd{C-u} followed by
134 the command you want to call. For example, @kbd{C-u M-.} tells the
135 @kbd{M-.} command to prompt for the function to lookup, instead
136 choosing one by looking at the source text in the buffer. The effect,
137 if any, of a prefix on a command is included in the command's
140 @node Programming Aids, Applications, Introduction, Top
141 @chapter Programming Aids
143 Distel includes a few small sets of related commands to automate
144 common programming chores. These are described in the following
148 * Tags:: Looking up function definitions.
149 * Completion:: Completing module and function names.
150 * Evaluation:: Evaluating snippets and reloading modules.
152 * Documentation:: Looking up online documentation.
155 @node Tags, Completion, Programming Aids, Programming Aids
156 @section Cross-Referencing (Tags)
158 A ``dynamic tags'' facility effectively makes each function call in an
159 Erlang source file into a hyperlink to the definition of that
160 function. For example, if you have a line of code like this:
166 You can place the point on the function name, press @kbd{M-.}, and up
167 pops @file{lists.erl} at the definition of @code{keysort/2}. After you
168 have perused the definition to your satisfaction, you press @kbd{M-,}
169 to jump back where you came from. You can also jump through several
170 (sub)function definitions and then use @kbd{M-,} several times to
171 unwind step-by-step back to where you came from.
173 This feature is a dynamic version of a traditional Emacs facility
174 called ``Tags.'' Whereas Tags needs you to maintain a special
175 @file{TAGS} file to keep track of definitions, Distel simply asks an
176 Erlang node, ``Where is the source file for module @code{foo}?'' The
177 Erlang node makes a well-educated guess at which source file we want
178 (based on the location and attributes of the beam file for the same
179 module), and sends back the path. Emacs then opens the file and scans
180 down to the definition of the function with the right arity.
182 If you have several versions of the same source file (perhaps
183 belonging to separate branches in revision control), then Distel will
184 find the one that matches the code in the Erlang node you're talking
185 to. So, to work on a particular source tree you just connect to a node
186 that has the matching code in its code path.
191 Jump from a function call to the definition of the function
192 (@code{erl-find-source-under-point}). If the variable
193 @code{distel-tags-compliant} is non-nil, or a prefix argument is
194 given, this command prompts for the function name to lookup.
197 Jump back from a function definition
198 (@code{erl-find-source-unwind}). This is a multi-level unwind through
199 a stack of positions from which you have jumped with @kbd{M-.} The
200 command is also bound to @kbd{M-*} for consistency with ``etags.''
203 To actually find the source file for a particular module, the Erlang
204 node first ensures that it can load the module, and then tries each of
205 these locations in order:
209 Same directory as the beam file.
211 @file{../src/} from the beam file.
213 @file{../erl/} from the beam file.
215 The directory from which the beam file was compiled. We can find this
216 using @code{module_info/1}, because the compiler records it as an
217 attribute in the beam file.
220 @node Completion, Evaluation, Tags, Programming Aids
221 @section Completion of Modules and Functions
223 Completion allows you to write some part of a module or remote
224 function name and then press @kbd{M-TAB} to have it completed
225 automatically. When multiple completions exist they are displayed in a
226 popup buffer, much like Emacs's normal filename completion. The
227 completion buffer can simply be read to see which completions exist,
228 or either @kbd{RET} or the middle mouse button can be used to select
234 Complete the module or function at point. (@code{erl-complete})
237 Alternative binding for @code{erl-complete}, since @kbd{M-TAB} is
238 often reserved by window managers.
241 @node Evaluation, Refactoring, Completion, Programming Aids
242 @section Evaluting Erlang Code
244 Distel includes some simple ways to evaluate Erlang code, described
245 here. More elaborate interactive evaluation is provided by Interactive
246 Sessions (@pxref{Interactive Sessions}).
251 Read an Erlang expression from the minibuffer, evaluate it on an
252 Erlang node, and show the result. (@code{erl-eval-expression})
255 Read a module name from the minibuffer and reload that module in an
256 Erlang node. (@code{erl-reload-module})
259 @node Refactoring, Documentation, Evaluation, Programming Aids
262 @emph{The Refactoring feature requires the syntax-tools package to be
263 in the Erlang node's code path. You can download syntax-tools from the
264 erlang.org ``User Contributions'' area.}
266 Expressions within functions can be automatically ``refactored'' into
267 their own sub-functions by using the @code{erl-refactor-subfunction}
268 command (@kbd{C-c C-d f}). The command takes the text of the
269 expression, determines which variables it needs from the original
270 function, and then generates the new function and puts it on the kill
271 ring for insertion by hand (with @code{yank}, @kbd{C-y}). The original
272 function is rewritten with a call to the subfunction where the
273 refactored expression used to be.
275 For example, suppose we want to refactor the following function:
278 eval_expression(S) ->
279 case parse_expr(S) of
281 case catch erl_eval:exprs(Parse, []) of
283 @{ok, flatten(io_lib:format("~p", [V]))@};
284 @{'EXIT', Reason@} ->
287 @{error, @{_, erl_parse, Err@}@} ->
292 In this example we will take the inner @code{case} expression and move
293 it into a new function called @code{try_evaluation}. We do this by
294 setting the Emacs region (using the mouse or @kbd{C-SPC}) from the
295 word @code{case} until the end of the word @code{end} -- marking
296 exactly one whole expression. We then enter @kbd{C-c C-d f} to
297 refactor, and when prompted for the function name we respond with
298 ``@code{try_evaluation}''. The original function is then rewritten to:
301 eval_expression(S) ->
302 case parse_expr(S) of
304 try_evaluation(Parse);
305 @{error, @{_, erl_parse, Err@}@} ->
310 And at the front of the kill ring we have the new function definition,
311 which can be pasted into the buffer wherever we want. The actual
312 definition we get is:
315 try_evaluation(Parse) ->
316 case catch erl_eval:exprs(Parse, []) of
318 @{ok, flatten(io_lib:format("~p", [V]))@};
319 @{'EXIT', Reason@} ->
324 @strong{Important note:} This command is not a ``pure'' refactoring,
325 because although it will import variables from the parent function
326 into the subfunction, it will not export new bindings created in the
327 subfunction back to the parent. However, if you follow good
328 programming practice and never ``export'' variables from inner
329 expressions, this is not a problem. An example of @emph{bad} code that
330 will not refactor correctly is this @code{if} expression:
333 if A < B -> X = true;
339 This is in poor style -- a variable created inside the @code{if} is
340 used by code at an outer level of nesting. To work with refactoring,
341 and to be in better style, it should be rewritten like this:
344 X = if A < B -> true;
350 @node Documentation, , Refactoring, Programming Aids
351 @section Documentation
353 Simple online Erlang documentation is provided via an Erlang program
354 called @code{fdoc}. The documentation is automatically scanned out of
355 source files by building a searchable database of the comments
356 appearing before each function. Naturally, the quality of
357 documentation provided by this scheme will depend on the style in
358 which the source files are commented.
362 Describe an Erlang module or function by
363 name. (@code{erl-fdoc-describe})
365 Show apropos information about Erlang functions, by regular
366 expression. All functions whose names or comments match the regexp are
367 displayed. (@code{erl-fdoc-apropos})
370 With a prefix argument, these commands rebuild the @code{fdoc}
371 database before searching. This is useful after (re)loading a lot of
372 modules, since @code{fdoc} only scans the currently loaded modules for
373 documentation when it builds the database.
375 @node Applications, , Programming Aids, Top
376 @chapter Applications
378 This chapter describes the larger applications included with Distel.
381 * Process Manager:: See and manipulate Erlang processes.
382 * Debugger:: Debug Erlang programs.
383 * Interactive Sessions:: Hybrid Emacs buffer / Erlang shell.
384 * Profiler:: Profile with fprof.
387 @node Process Manager, Debugger, Applications, Applications
388 @section Process Manager
390 The process manager displays a list of all the processes running on an
391 Erlang node, and offers commands to manipulate them.
395 Popup a process manager buffer. (@code{erl-process-list})
398 Within the process manager's buffer, the following commands are
403 Quit the process manager, and restore the Emacs windows as they were
406 Update the process list.
410 Pop up a buffer showing all the information about a process. The
411 buffer also continuously traces the process by appending events to the
412 buffer, until the buffer is killed with @kbd{q}.
414 Show a piece of information about the process, specified by name. The
415 name can be any key accepted by the @code{process_info/2} BIF.
417 Show a backtrace for a process. The backtrace is a fairly low-level
418 snapshot of the stack of a process, obtained from
419 @code{process_info(P, backtrace)}. It may take a little pratice to
420 learn how to read them.
422 Show the contents of a process's mailbox.
425 @node Debugger, Interactive Sessions, Process Manager, Applications
427 Distel includes a front-end to the Erlang debugger, using the same
428 backend as the standard Tk-based OTP debugger. The Distel debugger has
429 three parts: commands in Erlang source buffers for interpreting
430 modules and configuring breakpoints, a ``Monitor'' buffer listing
431 processes running interpreted code, and one ``Attach'' buffer for each
432 process that is being single-stepped.
435 * Basic Commands:: Interpreting modules and setting breakpoints.
436 * Monitor Buffer:: Monitoring interpreted processes.
437 * Attach Buffer:: Single-stepping a process.
438 * Synchronising Breakpoints:: Resynchronising breakpoints after edits.
439 * Saving and Restoring:: Temporarily saving debugging state.
442 @node Basic Commands, Monitor Buffer, Debugger, Debugger
443 @subsection Basic Commands
447 Toggle interpretedness of the current buffer's module.
448 (@code{edb-toggle-interpret})
450 Toggle a breakpoint on the current line.
451 (@code{edb-toggle-breakpoint})
453 Popup the Monitor buffer. (@code{edb-monitor})
456 @node Monitor Buffer, Attach Buffer, Basic Commands, Debugger
457 @subsection Monitor Buffer
459 The monitor buffer displays all processes that the debugger knows
460 about, line-by-line. This includes all processes that have run
461 interpreted code, and all that are stopped in breakpoints. The current
462 status of each process is shown -- running, at breakpoint, or
463 exited. You can attach to a debugged process by pressing @kbd{RET} on
468 Popup an attach buffer for a process.
470 Hide the Monitor buffer and restore Emacs' window configuration to the
473 Kill the monitor. This disconnects Emacs from the Erlang node's
474 debugging state and deletes all the local debugging state
475 (e.g. breakpoints in buffers.) The next debugger command will
476 automatically re-attach the monitor.
479 @node Attach Buffer, Synchronising Breakpoints, Monitor Buffer, Debugger
480 @subsection Attach Buffer
482 An attach buffer corresponds to a particular Erlang process that is
483 being debugged. It displays the source to the module currently being
484 executed and, when the process is stopped at a breakpoint, an arrow
485 showing the next line of execution. The attach buffer is accompanied
486 by a buffer showing the variable bindings in the current stack frame.
490 Step into the next expression. If the expression is a function call,
491 the debugger will enter that function. (@code{edb-attach-step})
493 Step over the next expression, without going down into a subfunction.
494 (@code{edb-attach-next})
496 Continue execution until the next breakpoint.
497 (@code{edb-attach-continue})
499 Show the previous stack frame. (@code{edb-attach-up})
501 Show the next stack frame. (@code{edb-attach-down})
503 Toggle a breakpoint on the current line.
504 (@code{edb-toggle-breakpoint})
506 Kill the attach buffer. This does not affect the actual Erlang
509 Display online help, showing essentially this information.
510 (@code{edb-attach-help})
513 @node Synchronising Breakpoints, Saving and Restoring, Attach Buffer, Debugger
514 @subsection Synchronising Breakpoints
516 At any point in time the breakpoints in a particular buffer will be
517 either ``fresh'' or ``stale,'' depending on whether the buffer has
518 been modified. Breakpoints are fresh when, as far as Emacs knows, the
519 buffer's source text (line numbers) correspond with the code in the
520 Erlang node. After the buffer is modified, the breakpoints become
521 stale, because edits may change line numbers so that the breakpoints
522 in Emacs no longer correspond with the actual program. Stale
523 breakpoints are made fresh by using the @code{edb-synch-breakpoints}
524 (@kbd{C-c C-d s}) command to reassert that they are correct. This
525 command is typically used after recompiling and reloading the module.
527 Fresh breakpoints are marked in red, stale breakpoints are marked in
532 Synchronise breakpoints by discarding the ones in the Erlang node and
533 then re-setting them from those in the Emacs buffer.
537 The overall debugger state machine for Erlang-mode buffers is shown in
542 In the ``Normal'' state, no breakpoints exist. In the ``Interpreted''
543 state, all breakpoints are fresh. In the ``Out of sync'' state, all
544 breakpoints are stale. The transitions illustrate how you can navigate
548 Care must be taken to only synchronise breakpoints when the Erlang
549 node is actually running the same code that is in the Emacs
550 buffer. Otherwise, the Erlang processes may break in unexpected
553 When reloading modules during debugging, it is preferable to use the
554 @code{erl-reload-module} command (@kbd{C-c C-d L}, @pxref{Evaluation})
555 than to call @code{l(mymodule)} directly in the Erlang shell. This is
556 because the Distel command is specially coded to make sure reloading
557 interpreted modules keeps them interpreted, but this doesn't appear to
558 work correctly in the Erlang shell.
560 @node Saving and Restoring, , Synchronising Breakpoints, Debugger
561 @subsection Saving and Restoring Debugger State
563 The overall debugger state (set of breakpoints and interpreted
564 modules) can be temporarily saved inside Emacs and then restored to
565 the Erlang node. This is particularly useful when you want to restart
566 the Erlang node and then continue debugging where you left off: you
567 just save the debug state, restart the node, and then restore.
571 Save the set of breakpoints and interpreted modules inside
572 Emacs. (@code{edb-save-dbg-state})
574 Restore Emacs's saved debugger state to the Erlang
575 node. (@code{edb-restore-dbg-state})
578 @node Interactive Sessions, Profiler, Debugger, Applications
579 @section Interactive Sessions
581 Interactive sessions are an Erlang version of the Emacs Lisp
582 @file{*scratch*} buffer. You can enter arbitrary Erlang expressions
583 and function definitions in an interactive session buffer and evaluate
584 them immediately, without creating any files.
588 Display the Interactive Session buffer for an Erlang node, creating it
589 if necessary. (@code{erl-ie-show-session})
592 Within the session buffer, these commands are available:
596 Evaluate the Erlang expression on the current line, and insert the
597 result in-line. (@code{erl-ie-eval-expression})
599 Evaluate the function definition before the point. Once defined, the
600 function can then be called from expressions in the session buffer,
601 and can be redefined later.
604 @node Profiler, , Interactive Sessions, Applications
607 Distel supports profiling function calls via the OTP @code{fprof}
608 application. This is a very convenient profiler, in that it doesn't
609 require any special compiler options or initialisation -- you can use
610 it whenever you want.
614 Prompt for an Erlang expression, evaluate it with profiling, and then
615 summarise the results. (@code{fprof})
617 Load and display prerecorded profiler data, from a file created by
618 @code{fprof:analyse/1}. (@code{fprof-analyse})
621 After an expression is profiled, the results are popped up in
622 ``profiler results'' buffer. The buffer contains one line to describe
623 each function that was called, with the following columns:
627 The total number of times the function was called.
629 The total time (ms) spent in the function, including its callees.
631 The total time (ms) spent by the function itself, excluding time spent
635 Furthermore, pressing @kbd{RET} on a summary line in the results
636 buffer will pop up another buffer showing more information about the
637 function: how much time it spent on behalf of each of its callers, and
638 how much time it spent in each of its subfunctions.