scheme-using.texi 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Guile Reference Manual.
  3. @c Copyright (C) 2006
  4. @c Free Software Foundation, Inc.
  5. @c See the file guile.texi for copying conditions.
  6. @node Using Guile Interactively
  7. @section Using Guile Interactively
  8. When you start up Guile by typing just @code{guile}, without a
  9. @code{-c} argument or the name of a script to execute, you get an
  10. interactive interpreter where you can enter Scheme expressions, and
  11. Guile will evaluate them and print the results for you. Here are some
  12. simple examples.
  13. @lisp
  14. guile> (+ 3 4 5)
  15. 12
  16. guile> (display "Hello world!\n")
  17. Hello world!
  18. guile> (values 'a 'b)
  19. a
  20. b
  21. @end lisp
  22. @noindent
  23. This mode of use is called a @dfn{REPL}, which is short for
  24. ``Read-Eval-Print Loop'', because the Guile interpreter first reads the
  25. expression that you have typed, then evaluates it, and then prints the
  26. result.
  27. @menu
  28. * Readline::
  29. * Value Historyx::
  30. * Error Handling::
  31. * Interactive Debugger:: Using the interactive debugger.
  32. @end menu
  33. @node Readline
  34. @subsection Readline
  35. To make it easier for you to repeat and vary previously entered
  36. expressions, or to edit the expression that you're typing in, Guile
  37. can use the GNU Readline library. This is not enabled by default
  38. because of licensing reasons, but all you need to activate Readline is
  39. the following pair of lines.
  40. @lisp
  41. guile> (use-modules (ice-9 readline))
  42. guile> (activate-readline)
  43. @end lisp
  44. It's a good idea to put these two lines (without the ``guile>''
  45. prompts) in your @file{.guile} file. Guile reads this file when it
  46. starts up interactively, so anything in this file has the same effect
  47. as if you type it in by hand at the ``guile>'' prompt.
  48. @node Value Historyx
  49. @subsection Value History
  50. Just as Readline helps you to reuse a previous input line, @dfn{value
  51. history} allows you to use the @emph{result} of a previous evaluation
  52. in a new expression. When value history is enabled, each evaluation
  53. result is automatically assigned to the next in the sequence of
  54. variables @code{$1}, @code{$2}, @dots{}, and you can then use these
  55. variables in subsequent expressions.
  56. @lisp
  57. guile> (iota 10)
  58. $1 = (0 1 2 3 4 5 6 7 8 9)
  59. guile> (apply * (cdr $1))
  60. $2 = 362880
  61. guile> (sqrt $2)
  62. $3 = 602.3952191045344
  63. guile> (cons $2 $1)
  64. $4 = (362880 0 1 2 3 4 5 6 7 8 9)
  65. @end lisp
  66. To enable value history, type @code{(use-modules (ice-9 history))} at
  67. the Guile prompt, or add this to your @file{.guile} file. (It is not
  68. enabled by default, to avoid the possibility of conflicting with some
  69. other use you may have for the variables @code{$1}, @code{$2},
  70. @dots{}, and also because it prevents the stored evaluation results
  71. from being garbage collected, which some people may not want.)
  72. @node Error Handling
  73. @subsection Error Handling
  74. When code being evaluated from the REPL hits an error, Guile remembers
  75. the execution context where the error occurred and can give you three
  76. levels of information about what the error was and exactly where it
  77. occurred.
  78. By default, Guile displays only the first level, which is the most
  79. immediate information about where and why the error occurred, for
  80. example:
  81. @lisp
  82. (make-string (* 4 (+ 3 #\s)) #\space)
  83. @print{}
  84. standard input:2:19: In procedure + in expression (+ 3 #\s):
  85. standard input:2:19: Wrong type argument: #\s
  86. ABORT: (wrong-type-arg)
  87. Type "(backtrace)" to get more information
  88. or "(debug)" to enter the debugger.
  89. @end lisp
  90. @noindent
  91. However, as the message above says, you can obtain more information
  92. about the context of the error by typing @code{(backtrace)} or
  93. @code{(debug)}.
  94. @code{(backtrace)} displays the Scheme call stack at the point where the
  95. error occurred:
  96. @lisp
  97. (backtrace)
  98. @print{}
  99. Backtrace:
  100. In standard input:
  101. 2: 0* [make-string ...
  102. 2: 1* [* 4 ...
  103. 2: 2* [+ 3 #\s]
  104. Type "(debug-enable 'backtrace)" if you would like a backtrace
  105. automatically if an error occurs in the future.
  106. @end lisp
  107. @noindent
  108. In a more complex scenario than this one, this can be extremely useful
  109. for understanding where and why the error occurred. You can make Guile
  110. show the backtrace automatically by adding @code{(debug-enable
  111. 'backtrace)} to your @file{.guile}.
  112. @code{(debug)} takes you into Guile's interactive debugger, which
  113. provides commands that allow you to
  114. @itemize @bullet
  115. @item
  116. display the Scheme call stack at the point where the error occurred
  117. (the @code{backtrace} command --- see @ref{Display Backtrace})
  118. @item
  119. move up and down the call stack, to see in detail the expression being
  120. evaluated, or the procedure being applied, in each @dfn{frame} (the
  121. @code{up}, @code{down}, @code{frame}, @code{position}, @code{info args}
  122. and @code{info frame} commands --- see @ref{Frame Selection} and
  123. @ref{Frame Information})
  124. @item
  125. examine the values of variables and expressions in the context of each
  126. frame (the @code{evaluate} command --- see @ref{Frame Evaluation}).
  127. @end itemize
  128. @noindent
  129. The interactive debugger is documented further in the following section.
  130. @node Interactive Debugger
  131. @subsection Using the Interactive Debugger
  132. Guile's interactive debugger is a command line application that
  133. accepts commands from you for examining the stack and, if stopped at a
  134. trap, for continuing program execution in various ways. Unlike in the
  135. normal Guile REPL, commands are typed mostly without parentheses.
  136. When you first enter the debugger, it introduces itself with a message
  137. like this:
  138. @lisp
  139. This is the Guile debugger -- for help, type `help'.
  140. There are 3 frames on the stack.
  141. Frame 2 at standard input:36:19
  142. [+ 3 #\s]
  143. debug>
  144. @end lisp
  145. @noindent
  146. ``debug>'' is the debugger's prompt, and a reminder that you are not in
  147. the normal Guile REPL. In case you find yourself in the debugger by
  148. mistake, the @code{quit} command will return you to the REPL.
  149. @deffn {Debugger Command} quit
  150. Exit the debugger.
  151. @end deffn
  152. The other available commands are described in the following subsections.
  153. @menu
  154. * Display Backtrace:: backtrace.
  155. * Frame Selection:: up, down, frame.
  156. * Frame Information:: info args, info frame, position.
  157. * Frame Evaluation:: evaluate.
  158. * Stepping and Continuing:: step, next, (trace-)finish, continue.
  159. @end menu
  160. @node Display Backtrace
  161. @subsubsection Display Backtrace
  162. The @code{backtrace} command, which can also be invoked as @code{bt} or
  163. @code{where}, displays the call stack (aka backtrace) at the point where
  164. the debugger was entered:
  165. @lisp
  166. debug> bt
  167. In standard input:
  168. 36: 0* [make-string ...
  169. 36: 1* [* 4 ...
  170. 36: 2* [+ 3 #\s]
  171. @end lisp
  172. @deffn {Debugger Command} backtrace [count]
  173. @deffnx {Debugger Command} bt [count]
  174. @deffnx {Debugger Command} where [count]
  175. Print backtrace of all stack frames, or of the innermost @var{count}
  176. frames. With a negative argument, print the outermost -@var{count}
  177. frames. If the number of frames isn't explicitly given, the debug
  178. option @code{depth} determines the maximum number of frames printed.
  179. @end deffn
  180. The format of the displayed backtrace is the same as for the
  181. @code{display-backtrace} procedure (@pxref{Examining the Stack}).
  182. @node Frame Selection
  183. @subsubsection Frame Selection
  184. A call stack consists of a sequence of stack @dfn{frames}, with each
  185. frame describing one level of the nested evaluations and applications
  186. that the program was executing when it hit a breakpoint or an error.
  187. Frames are numbered such that frame 0 is the outermost --- i.e. the
  188. operation on the call stack that began least recently --- and frame N-1
  189. the innermost (where N is the total number of frames on the stack).
  190. When you enter the debugger, the innermost frame is selected, which
  191. means that the commands for getting information about the ``current''
  192. frame, or for evaluating expressions in the context of the current
  193. frame, will do so by default with respect to the innermost frame. To
  194. select a different frame, so that these operations will apply to it
  195. instead, use the @code{up}, @code{down} and @code{frame} commands like
  196. this:
  197. @lisp
  198. debug> up
  199. Frame 1 at standard input:36:14
  200. [* 4 ...
  201. debug> frame 0
  202. Frame 0 at standard input:36:1
  203. [make-string ...
  204. debug> down
  205. Frame 1 at standard input:36:14
  206. [* 4 ...
  207. @end lisp
  208. @deffn {Debugger Command} up [n]
  209. Move @var{n} frames up the stack. For positive @var{n}, this
  210. advances toward the outermost frame, to lower frame numbers, to
  211. frames that have existed longer. @var{n} defaults to one.
  212. @end deffn
  213. @deffn {Debugger Command} down [n]
  214. Move @var{n} frames down the stack. For positive @var{n}, this
  215. advances toward the innermost frame, to higher frame numbers, to frames
  216. that were created more recently. @var{n} defaults to one.
  217. @end deffn
  218. @deffn {Debugger Command} frame [n]
  219. Select and print a stack frame. With no argument, print the selected
  220. stack frame. (See also ``info frame''.) An argument specifies the
  221. frame to select; it must be a stack-frame number.
  222. @end deffn
  223. @node Frame Information
  224. @subsubsection Frame Information
  225. The following commands return detailed information about the currently
  226. selected frame.
  227. @deffn {Debugger Command} {info frame}
  228. Display a verbose description of the selected frame. The information
  229. that this command provides is equivalent to what can be deduced from the
  230. one line summary for the frame that appears in a backtrace, but is
  231. presented and explained more clearly.
  232. @end deffn
  233. @deffn {Debugger Command} {info args}
  234. Display the argument variables of the current stack frame. Arguments
  235. can also be seen in the backtrace, but are presented more clearly by
  236. this command.
  237. @end deffn
  238. @deffn {Debugger Command} position
  239. Display the name of the source file that the current expression comes
  240. from, and the line and column number of the expression's opening
  241. parenthesis within that file. This information is only available when
  242. the @code{positions} read option is enabled (@pxref{Reader options}).
  243. @end deffn
  244. @node Frame Evaluation
  245. @subsubsection Frame Evaluation
  246. The @code{evaluate} command is most useful for querying the value of a
  247. variable, either global or local, in the environment of the selected
  248. stack frame, but it can be used more generally to evaluate any
  249. expression.
  250. @deffn {Debugger Command} evaluate expression
  251. Evaluate an expression in the environment of the selected stack frame.
  252. The expression must appear on the same line as the command, however it
  253. may be continued over multiple lines.
  254. @end deffn
  255. @node Stepping and Continuing
  256. @subsubsection Single Stepping and Continuing Execution
  257. The commands in this subsection all apply only when the stack is
  258. @dfn{continuable} --- in other words when it makes sense for the program
  259. that the stack comes from to continue running. Usually this means that
  260. the program stopped because of a trap or a breakpoint.
  261. @deffn {Debugger Command} step [n]
  262. Tell the debugged program to do @var{n} more steps from its current
  263. position. One @dfn{step} means executing until the next frame entry or
  264. exit of any kind. @var{n} defaults to 1.
  265. @end deffn
  266. @deffn {Debugger Command} next [n]
  267. Tell the debugged program to do @var{n} more steps from its current
  268. position, but only counting frame entries and exits where the
  269. corresponding source code comes from the same file as the current stack
  270. frame. (See @ref{Step Traps} for the details of how this works.) If
  271. the current stack frame has no source code, the effect of this command
  272. is the same as of @code{step}. @var{n} defaults to 1.
  273. @end deffn
  274. @deffn {Debugger Command} finish
  275. Tell the program being debugged to continue running until the completion
  276. of the current stack frame, and at that time to print the result and
  277. reenter the command line debugger.
  278. @end deffn
  279. @deffn {Debugger Command} continue
  280. Tell the program being debugged to continue running. (In fact this is
  281. the same as the @code{quit} command, because it exits the debugger
  282. command loop and so allows whatever code it was that invoked the
  283. debugger to continue.)
  284. @end deffn
  285. @node Using Guile in Emacs
  286. @section Using Guile in Emacs
  287. @cindex GDS
  288. @cindex Emacs
  289. There are several options for working on Guile Scheme code in Emacs.
  290. The simplest are to use Emacs's standard @code{scheme-mode} for
  291. editing code, and to run the interpreter when you need it by typing
  292. ``guile'' at the prompt of a @code{*shell*} buffer, but there are
  293. Emacs libraries available which add various bells and whistles to
  294. this. The following diagram shows these libraries and how they relate
  295. to each other, with the arrows indicating ``builds on'' or
  296. ``extends''. For example, the Quack library builds on cmuscheme,
  297. which in turn builds on the standard scheme mode.
  298. @example
  299. scheme
  300. ^
  301. |
  302. .-----+-----.
  303. | |
  304. cmuscheme xscheme
  305. ^
  306. |
  307. .-----+-----.
  308. | |
  309. Quack GDS
  310. @end example
  311. @dfn{scheme}, written by Bill Rozas and Dave Love, is Emacs's standard
  312. mode for Scheme code files. It provides Scheme-sensitive syntax
  313. highlighting, parenthesis matching, indentation and so on.
  314. @dfn{cmuscheme}, written by Olin Shivers, provides a comint-based Scheme
  315. interaction buffer, so that you can run an interpreter more directly
  316. than with the @code{*shell*} buffer approach by typing @kbd{M-x
  317. run-scheme}. It also extends @code{scheme-mode} so that there are key
  318. presses for sending selected bits of code from a Scheme buffer to this
  319. interpreter. This means that when you are writing some code and want to
  320. check what an expression evaluates to, you can easily select that code
  321. and send it to the interpreter for evaluation, then switch to the
  322. interpreter to see what the result is. cmuscheme is included in the
  323. standard Emacs distribution.
  324. @dfn{Quack}, written by Neil Van Dyke, adds a number of incremental
  325. improvements to the scheme/cmuscheme combination: convenient menu
  326. entries for looking up Scheme-related references (such as the SRFIs);
  327. enhanced indentation rules that are customized for particular Scheme
  328. interpreters, including Guile; an enhanced version of the
  329. @code{run-scheme} command that knows the names of the common Scheme
  330. interpreters and remembers which one you used last time; and so on.
  331. Quack is available from @uref{http://www.neilvandyke.org/quack}.
  332. @dfn{GDS}, written by Neil Jerram, also builds on the scheme/cmuscheme
  333. combination, but with a change to the way that Scheme code fragments
  334. are sent to the interpreter for evaluation. cmuscheme and Quack send
  335. code fragments to the interpreter's standard input, on the assumption
  336. that the interpreter is expecting to read Scheme expressions there,
  337. and then monitor the interpreter's standard output to infer what the
  338. result of the evaluation is. GDS doesn't use standard input and
  339. output like this. Instead, it sets up a socket connection between the
  340. Scheme interpreter and Emacs, and sends and receives messages using a
  341. simple protocol through this socket. The messages include requests to
  342. evaluate Scheme code, and responses conveying the results of an
  343. evaluation, thus providing similar function to cmuscheme or Quack.
  344. They also include requests for stack exploration and debugging, which
  345. go beyond what cmuscheme or Quack can do. The price of this extra
  346. power, however, is that GDS is Guile-specific. GDS requires the
  347. Scheme interpreter to run some GDS-specific library code; currently
  348. this code is written as a Guile module and uses features that are
  349. specific to Guile. GDS is now included in the Guile distribution; for
  350. previous Guile releases (1.8.4 and earlier) it can be obtained as part
  351. of the @code{guile-debugging} package from
  352. @uref{http://www.ossau.uklinux.net/guile}.
  353. Finally, @dfn{xscheme} is similar to cmuscheme --- in that it starts up
  354. a Scheme interaction process and sends commands to that process's
  355. standard input --- and to GDS --- in that it has support beyond
  356. cmuscheme or Quack for exploring the Scheme stack when an error has
  357. occurred --- but is implemented specifically for MIT/GNU Scheme. Hence
  358. it isn't really relevant to Guile work in Emacs, except as a reference
  359. for useful features that could be implemented in one of the other
  360. libraries mentioned here.
  361. In summary, the best current choice for working on Guile code in Emacs
  362. is either Quack or GDS, depending on which of these libraries' features
  363. you find most important. For more information on Quack, please see the
  364. website referenced above. GDS is documented further in the rest of this
  365. section.
  366. @menu
  367. * GDS Introduction::
  368. * GDS Architecture::
  369. * GDS Getting Started::
  370. * Working with GDS in Scheme Buffers::
  371. * Displaying the Scheme Stack::
  372. * Continuing Execution::
  373. * Associating Buffers with Clients::
  374. * An Example GDS Session::
  375. @end menu
  376. @node GDS Introduction
  377. @subsection GDS Introduction
  378. GDS aims to allow you to work on Guile Scheme code in the same kind of
  379. way that Emacs allows you to work on Emacs Lisp code: providing easy
  380. access to help, evaluating arbitrary fragments of code, a nice debugging
  381. interface, and so on. The thinking behind the GDS library is that you
  382. will usually be doing one of two things.
  383. @enumerate
  384. @item
  385. Writing or editing code. The code will be in a normal Emacs Scheme mode
  386. buffer, and GDS extends Scheme mode to add keystrokes and menu items for
  387. the things that are likely to be useful to you when working on code:
  388. @itemize
  389. @item
  390. completing the identifier at point, with respect to the set of variable
  391. names that are known to the associated Guile process
  392. @item
  393. accessing Guile's built in ``help'' and ``apropos'' commands
  394. @item
  395. evaluating fragments of code to check what they do, with the results
  396. popping up in a temporary Emacs window.
  397. @end itemize
  398. @item
  399. Debugging a Guile Scheme program. When your program hits an error or
  400. stops at a trap, GDS shows you the relevant code and the Scheme stack,
  401. and makes it easy to
  402. @itemize
  403. @item
  404. look at the values of local variables
  405. @item
  406. see what is happening at all levels of the Scheme stack
  407. @item
  408. continue execution, either normally or step by step.
  409. @end itemize
  410. The presentation makes it very easy to move up and down the stack,
  411. showing whenever possible the source code for each frame in another
  412. Emacs buffer. It also provides convenient keystrokes for telling Guile
  413. what to do next; for example, you can select a stack frame and tell
  414. Guile to run until that frame completes, at which point GDS will display
  415. the frame's return value.
  416. @end enumerate
  417. GDS can provide these facilities for any number of Guile Scheme programs
  418. (which we often refer to as ``clients'') at once, and these programs can
  419. be started either independently of GDS, including outside Emacs, or
  420. specifically @emph{by} GDS.
  421. Communication between each Guile client program and GDS uses a TCP
  422. socket, which means that it is orthogonal to any other interfaces that
  423. the client program has. In particular GDS does not interfere with a
  424. program's standard input and output.
  425. @node GDS Architecture
  426. @subsection GDS Architecture
  427. In order to understand the following documentation fully it will help to
  428. have a picture in mind of how GDS works, so we briefly describe that
  429. here. GDS consists of three components.
  430. @itemize
  431. @item
  432. The GDS @dfn{interface} code is written in Emacs Lisp and runs inside
  433. Emacs. This code, consisting of the installed files @file{gds.el} and
  434. @file{gds-server.el}, is responsible for displaying information from
  435. Guile in Emacs windows, and for responding to Emacs commands and
  436. keystrokes by sending instructions back to the Guile program being
  437. worked on.
  438. @item
  439. The GDS @dfn{server} code is written in Scheme and runs as an Emacs
  440. inferior process. It acts as a multiplexer between the (possibly
  441. multiple) Guile programs being debugged and the interface code running
  442. in Emacs. The server code is the installed file
  443. @file{gds-server.scm}.
  444. @item
  445. The GDS @dfn{client} code is written in Scheme (installed file
  446. @file{gds-client.scm}), and must be loaded as a module by each Guile
  447. program that wants to use GDS in any way.
  448. @end itemize
  449. @noindent
  450. The following diagram shows how these components are connected to each
  451. other.
  452. @example
  453. +----------------+
  454. | Program #1 |
  455. | |
  456. | +------------+ |
  457. | | GDS Client |-_
  458. | +------------+ |-_ +-------------------+
  459. +----------------+ -_TCP | Emacs |
  460. -_ | |
  461. -_+------------+ | +---------------+ |
  462. _| GDS Server |-----| GDS Interface | |
  463. +----------------+ _- +------------+ | +---------------+ |
  464. | Program #2 | _- +-------------------+
  465. | | _- TCP
  466. | +------------+ _-
  467. | | GDS Client |-|
  468. | +------------+ |
  469. +----------------+
  470. @end example
  471. @cindex TCP, use of
  472. The data exchanged between client and server components, and between
  473. server and interface, is a sequence of sexps (parenthesised expressions)
  474. that are designed so as to be directly readable by both Scheme and Emacs
  475. Lisp. The use of a TCP connection means that the server and Emacs
  476. interface can theoretically be on a different computer from the client
  477. programs, but in practice there are currently two problems with
  478. this. Firstly the GDS API doesn't provide any way of specifying a
  479. non-local server to connect to, and secondly there is no security or
  480. authentication mechanism in the GDS protocol. These are issues that
  481. should be addressed in the future.
  482. @node GDS Getting Started
  483. @subsection Getting Started with GDS
  484. To enable the use of GDS in your own Emacs sessions, simply add
  485. @lisp
  486. (require 'gds)
  487. @end lisp
  488. @noindent
  489. somewhere in your @file{.emacs} file. This will cause Emacs to load the
  490. GDS Emacs Lisp code when starting up, and to start the inferior GDS
  491. server process so that it is ready and waiting for any Guile programs
  492. that want to use GDS.
  493. (If GDS's Scheme code is not installed in one of the locations in
  494. Guile's load path, you may find that the server process fails to start.
  495. When this happens you will see an error message from Emacs:
  496. @lisp
  497. error in process filter: Wrong type argument: listp, Backtrace:
  498. @end lisp
  499. @noindent
  500. and the @code{gds-debug} buffer will contain a Scheme backtrace ending
  501. with the message:
  502. @lisp
  503. no code for module (ice-9 gds-server)
  504. @end lisp
  505. @noindent
  506. The solution for this is to customize the Emacs variable
  507. @code{gds-scheme-directory} so that it specifies where the GDS Scheme
  508. code is installed. Then either restart Emacs or type @kbd{M-x
  509. gds-run-debug-server} to try starting the GDS server process again.)
  510. For evaluations, help and completion from Scheme code buffers that you
  511. are working on, this is all you need. The first time you do any of
  512. these things, GDS will automatically start a new Guile client program as
  513. an Emacs subprocess. This Guile program does nothing but wait for and
  514. act on instructions from GDS, and we refer to it as a @dfn{utility}
  515. Guile client. Over time this utility client will accumulate the code
  516. that you ask it to evaluate, and you can also tell it to load complete
  517. files or modules by sending it @code{load} or @code{use-modules}
  518. expressions.
  519. When you want to use GDS to work on an independent Guile
  520. application, you need to add something to that application's Scheme code
  521. to cause it to connect to and interact with GDS at the right times. The
  522. following subsections describe the ways of doing this.
  523. @subsubsection Invoking GDS when an Exception Occurs
  524. One option is to use GDS to catch and display any exceptions that
  525. are thrown by the application's code. If you already have a
  526. @code{lazy-catch} or @code{with-throw-handler} around the area of code
  527. that you want to monitor, you just need to add the following to the
  528. handler code:
  529. @lisp
  530. (gds-debug-trap (throw->trap-context key args))
  531. @end lisp
  532. @noindent
  533. where @code{key} and @code{args} are the first and rest arguments that
  534. Guile passes to the handler. (In other words, they assume the handler
  535. signature @code{(lambda (key . args) @dots{})}.) With Guile 1.8 or
  536. later, you can also do this with a @code{catch}, by adding this same
  537. code to the catch's pre-unwind handler.
  538. If you don't already have any of these, insert a whole
  539. @code{with-throw-handler} expression (or @code{lazy-catch} if your Guile
  540. is pre-1.8) around the code of interest like this:
  541. @lisp
  542. (with-throw-handler #t
  543. (lambda ()
  544. ;; Protected code here.
  545. )
  546. (lambda (key . args)
  547. (gds-debug-trap (throw->trap-context key args))))
  548. @end lisp
  549. Either way, you will need to use the @code{(ice-9 gds-client)} and
  550. @code{(ice-9 debugging traps)} modules.
  551. Two special cases of this are the lazy-catch that the Guile REPL code
  552. uses to catch exceptions in user code, and the lazy-catch inside the
  553. @code{stack-catch} utility procedure that is provided by the
  554. @code{(ice-9 stack-catch)} module. Both of these use a handler called
  555. @code{lazy-handler-dispatch} (defined in @file{boot-9.scm}), which you
  556. can hook into such that it calls GDS to display the stack when an
  557. exception occurs. To do this, use the @code{on-lazy-handler-dispatch}
  558. procedure as follows.
  559. @lisp
  560. (use-modules (ice-9 gds-client)
  561. (ice-9 debugging traps))
  562. (on-lazy-handler-dispatch gds-debug-trap)
  563. @end lisp
  564. @noindent
  565. After this the program will use GDS to display the stack whenever it
  566. hits an exception that is protected by a @code{lazy-catch} using
  567. @code{lazy-handler-dispatch}.
  568. @subsubsection Accepting GDS Instructions at Any Time
  569. In addition to setting an exception handler as described above, a
  570. Guile program can in principle set itself up to accept new
  571. instructions from GDS at any time, not just when it has stopped at an
  572. exception. This would allow the GDS user to evaluate code in the
  573. context of the running program, without having to wait for the program
  574. to stop first.
  575. @lisp
  576. (use-modules (ice-9 gds-client))
  577. (gds-accept-input #t)
  578. @end lisp
  579. @code{gds-accept-input} causes the calling program to loop processing
  580. instructions from GDS, until GDS sends the @code{continue} instruction.
  581. This blocks the thread that calls it, however, so it will normally be
  582. more practical for the program to set up a dedicated GDS thread and call
  583. @code{gds-accept-input} from that thread.
  584. For @code{select}-driven applications, an alternative approach would be
  585. for the GDS client code to provide an API which allowed the application
  586. to
  587. @itemize
  588. @item
  589. discover the file descriptors (or Scheme ports) that are used for
  590. receiving instruction from the GDS front end, so that it could include
  591. these in its @code{select} call
  592. @item
  593. call the GDS instruction handler when @code{select} indicated data
  594. available for reading on those descriptors/ports.
  595. @end itemize
  596. @noindent
  597. This approach is not yet implemented, though.
  598. @subsubsection Utility Guile Implementation
  599. The ``utility'' Guile client mentioned above is a simple combination
  600. of the mechanisms that we have just described. In fact the code for
  601. the utility Guile client is essentially just this:
  602. @lisp
  603. (use-modules (ice-9 gds-client))
  604. (named-module-use! '(guile-user) '(ice-9 session))
  605. (gds-accept-input #f))
  606. @end lisp
  607. The @code{named-module-use!} line ensures that the client can process
  608. @code{help} and @code{apropos} expressions, to implement lookups in
  609. Guile's online help. The @code{#f} parameter to
  610. @code{gds-accept-input} means that the @code{continue} instruction
  611. will not cause the instruction loop to exit, which makes sense here
  612. because the utility client has nothing to do except to process GDS
  613. instructions.
  614. The utility client does not use @code{on-lazy-handler-dispatch} at its
  615. top level, because it has its own mechanism for catching and reporting
  616. exceptions in the code that it is asked to evaluate. This mechanism
  617. summarizes the exception and gives the user a button they can click to
  618. see the full stack, so the end result is very similar to what
  619. @code{on-lazy-handler-dispatch} provides. Deep inside
  620. @code{gds-accept-input}, in the part that handles evaluating
  621. expressions from Emacs, the GDS client code uses
  622. @code{throw->trap-context} and @code{gds-debug-trap} to implement
  623. this.
  624. @node Working with GDS in Scheme Buffers
  625. @subsection Working with GDS in Scheme Buffers
  626. The following subsections describe the facilities and key sequences that
  627. GDS provides for working on code in @code{scheme-mode} buffers.
  628. @menu
  629. * Access to Guile Help and Completion::
  630. * Evaluating Scheme Code::
  631. @end menu
  632. @node Access to Guile Help and Completion
  633. @subsubsection Access to Guile Help and Completion
  634. The following keystrokes provide fast and convenient access to Guile's
  635. built in help, and to completion with respect to the set of defined and
  636. accessible symbols.
  637. @table @kbd
  638. @item C-h g
  639. @findex gds-help-symbol
  640. Get Guile help for a particular symbol, with the same results as if
  641. you had typed @code{(help SYMBOL)} into the Guile REPL
  642. (@code{gds-help-symbol}). The symbol to query defaults to the word at
  643. or before the cursor but can also be entered or edited in the
  644. minibuffer. The available help is popped up in a temporary Emacs
  645. window.
  646. @item C-h G
  647. @findex gds-apropos
  648. List all accessible Guile symbols matching a given regular expression,
  649. with the same results as if you had typed @code{(apropos REGEXP)} into
  650. the Guile REPL (@code{gds-apropos}). The regexp to query defaults to
  651. the word at or before the cursor but can also be entered or edited in
  652. the minibuffer. The list of matching symbols is popped up in a
  653. temporary Emacs window.
  654. @item M-@key{TAB}
  655. @findex gds-complete-symbol
  656. Try to complete the symbol at the cursor by matching it against the
  657. set of all defined and accessible bindings in the associated Guile
  658. process (@code{gds-complete-symbol}). If there are any extra
  659. characters that can be definitively added to the symbol at point, they
  660. are inserted. Otherwise, if there are any completions available, they
  661. are popped up in a temporary Emacs window, where one of them can be
  662. selected using either @kbd{@key{RET}} or the mouse.
  663. @end table
  664. @node Evaluating Scheme Code
  665. @subsubsection Evaluating Scheme Code
  666. The following keystrokes and commands provide various ways of sending
  667. code to a Guile client process for evaluation.
  668. @table @kbd
  669. @item M-C-x
  670. @findex gds-eval-defun
  671. Evaluate the ``top level defun'' that the cursor is in, in other words
  672. the smallest balanced expression which includes the cursor and whose
  673. opening parenthesis is in column 0 (@code{gds-eval-defun}).
  674. @item C-x C-e
  675. @findex gds-eval-last-sexp
  676. Evaluate the expression that ends just before the cursor
  677. (@code{gds-eval-last-sexp}). This is designed so that it is easy to
  678. evaluate an expression that you have just finished typing.
  679. @item C-c C-e
  680. @findex gds-eval-expression
  681. Read a Scheme expression using the minibuffer, and evaluate that
  682. expression (@code{gds-eval-expression}).
  683. @item C-c C-r
  684. @findex gds-eval-region
  685. Evaluate the Scheme code in the marked region of the current buffer
  686. (@code{gds-eval-region}). Note that GDS does not check whether the
  687. region contains a balanced expression, or try to expand the region so
  688. that it does; it uses the region exactly as it is.
  689. @end table
  690. If you type @kbd{C-u} before one of these commands, GDS will
  691. immediately pop up a Scheme stack buffer, showing the requested
  692. evaluation, so that you can single step through it. (This is achieved
  693. by setting a @code{<source-trap>} trap at the start of the requested
  694. evaluation; see @ref{Source Traps} for more on how those work.) The
  695. Scheme stack display, and the options for continuing through the code,
  696. are described in the next two sections.
  697. @node Displaying the Scheme Stack
  698. @subsection Displaying the Scheme Stack
  699. When you specify @code{gds-debug-trap} as the behaviour for a trap and
  700. the Guile program concerned hits that trap, GDS displays the stack and
  701. the relevant Scheme source code in Emacs, allowing you to explore the
  702. state of the program and then decide what to do next. The same
  703. applies if the program calls @code{(on-lazy-handler-dispatch
  704. gds-debug-trap)} and then throws an exception that passes through
  705. @code{lazy-handler-dispatch}, except that in this case you can only
  706. explore; it isn't possible to continue normal execution after an
  707. exception.
  708. The following commands are available in the stack buffer for exploring
  709. the state of the program.
  710. @table @asis
  711. @item @kbd{u}, @kbd{C-p}, @kbd{@key{up}}
  712. @findex gds-up
  713. Select the stack frame one up from the currently selected frame
  714. (@code{gds-up}). GDS displays stack frames with the innermost at the
  715. top, so moving ``up'' means selecting a more ``inner'' frame.
  716. @item @kbd{d}, @kbd{C-n}, @kbd{@key{down}}
  717. @findex gds-down
  718. Select the stack frame one down from the currently selected frame
  719. (@code{gds-down}). GDS displays stack frames with the innermost at the
  720. top, so moving ``down'' means selecting a more ``outer'' frame.
  721. @item @kbd{@key{RET}}
  722. @findex gds-select-stack-frame
  723. Select the stack frame at point (@code{gds-select-stack-frame}). This
  724. is useful after clicking somewhere in the stack trace with the mouse.
  725. @end table
  726. Selecting a frame means that GDS will display the source code
  727. corresponding to that frame in the adjacent window, and that
  728. subsequent frame-sensitive commands, such as @code{gds-evaluate} (see
  729. below) and @code{gds-step-over} (@pxref{Continuing Execution}), will
  730. refer to that frame.
  731. @table @kbd
  732. @item e
  733. @findex gds-evaluate
  734. Evaluate a variable or expression in the local environment of the
  735. selected stack frame (@code{gds-evaluate}). The result is displayed in
  736. the echo area.
  737. @item I
  738. @findex gds-frame-info
  739. Show summary information about the selected stack frame
  740. (@code{gds-frame-info}). This includes what type of frame it is, the
  741. associated expression, and the frame's source location, if any.
  742. @item A
  743. @findex gds-frame-args
  744. For an application frame, display the frame's arguments
  745. (@code{gds-frame-args}).
  746. @item S
  747. @findex gds-proc-source
  748. For an application frame, show the Scheme source code of the procedure
  749. being called (@code{gds-proc-source}). The source code (where
  750. available) is displayed in the echo area.
  751. @end table
  752. @kbd{S} (@code{gds-proc-source}) is useful when the procedure being
  753. called was created by an anonymous @code{(lambda @dots{})} expression.
  754. Such procedures appear in the stack trace as @code{<procedure #f
  755. (@dots{})>}, which doesn't give you much clue as to what will happen
  756. next. @kbd{S} will show you the procedure's code, which is usually
  757. enough for you to identify it.
  758. @node Continuing Execution
  759. @subsection Continuing Execution
  760. If it makes sense to continue execution from the stack which is being
  761. displayed, GDS provides the following further commands in the stack
  762. buffer.
  763. @table @asis
  764. @item @kbd{g}, @kbd{c}, @kbd{q}
  765. @findex gds-go
  766. Tell the program to continue running (@code{gds-go}). It may of course
  767. stop again if it hits another trap, or another occurrence of the same
  768. trap.
  769. The multiple keystrokes reflect that you can think of this as ``going'',
  770. ``continuing'' or ``quitting'' (in the sense of quitting the GDS
  771. display).
  772. @item @kbd{@key{SPC}}
  773. @findex gds-step-file
  774. Tell the program to do a single-step to the next entry or exit of a
  775. frame whose code comes from the same source file as the selected stack
  776. frame (@code{gds-step-file}).
  777. In other words, you can hit @kbd{@key{SPC}} repeatedly to step through
  778. the code in a given file, automatically stepping @emph{over} any
  779. evaluations or procedure calls that use code from other files (or from
  780. no file).
  781. If the selected stack frame has no source, the effect of this command is
  782. the same as that of @kbd{i}, described next.
  783. @item @kbd{i}
  784. @findex gds-step-into
  785. Tell the debugged program to do a single-step to the next frame entry or
  786. exit of any kind (@code{gds-step-into}). @kbd{i} therefore steps
  787. through code at the most detailed level possible.
  788. @item @kbd{o}
  789. @findex gds-step-over
  790. Tell the debugged program to continue running until the selected stack
  791. frame completes, and then to display its result (@code{gds-step-over}).
  792. Note that the program may stop before then if it hits another trap; in
  793. this case the trap telling it to stop when the marked frame completes
  794. remains in place and so will still fire at the appropriate point.
  795. @end table
  796. @node Associating Buffers with Clients
  797. @subsection Associating Buffers with Clients
  798. The first time that you use one of GDS's evaluation, help or completion
  799. commands from a given Scheme mode buffer, GDS will ask which Guile
  800. client program you want to use for the operation, or if you want to
  801. start up a new ``utility'' client. After that GDS considers the buffer
  802. to be ``associated'' with the selected client, and so sends all further
  803. requests to that client, but you can override this by explicitly
  804. associating the buffer with a different client, or by removing the
  805. default association.
  806. @table @kbd
  807. @item M-x gds-associate-buffer
  808. Associate (or re-associate) the current buffer with a particular Guile
  809. client program. The available clients are listed, and you can also
  810. choose to start up a new ``utility'' client for this buffer to associate
  811. with.
  812. @item M-x gds-dissociate-buffer
  813. Dissociate the current buffer from its client, if any. This means that
  814. the next time you use an evaluation, help or completion command, GDS
  815. will ask you again which client to send the request to.
  816. @end table
  817. When a buffer is associated with a client program, the buffer's modeline
  818. shows whether the client is currently able to accept instruction from
  819. GDS. This is done by adding one of the following suffixes to the
  820. ``Scheme'' major mode indicator:
  821. @table @asis
  822. @item :ready
  823. The client program (or one of its threads, if multithreaded) is
  824. currently ready to accept instruction from GDS. In other words, if you
  825. send it a help or evaluation request, you should see the result pretty
  826. much immediately.
  827. @item :running
  828. The client program is not currently able to accept instruction from
  829. GDS. This means that it (or all of its threads, if multithreaded) is
  830. busy, or waiting for input other than from GDS.
  831. @item :debug
  832. The client program (or one of its threads, if multithreaded) is stopped
  833. in ``debugging mode'' with GDS displaying the stack for a trap or
  834. exception. It is waiting for instruction from GDS on what to do next.
  835. @end table
  836. @node An Example GDS Session
  837. @subsection An Example GDS Session
  838. Create a file, @file{testgds.scm} say, for experimenting with GDS and
  839. Scheme code, and type this into it:
  840. @lisp
  841. (use-modules (ice-9 debugging traps)
  842. (ice-9 gds-client)
  843. (ice-9 debugging example-fns))
  844. (install-trap (make <procedure-trap>
  845. #:behaviour gds-debug-trap
  846. #:procedure fact1))
  847. @end lisp
  848. @noindent
  849. Now select all of this code and type @kbd{C-c C-r} to send the selected
  850. region to Guile for evaluation. GDS will ask you which Guile process to
  851. use; unless you know that you already have another Guile application
  852. running and connected to GDS, choose the ``Start a new Guile'' option,
  853. which starts one of the ``utility'' processes described in @ref{GDS
  854. Getting Started}.
  855. The results of the evaluation pop up in a window like this:
  856. @lisp
  857. (use-modules (ice-9 debugging traps)\n @dots{}
  858. ;;; Evaluating subexpression 1 in current module (guile-user)
  859. @result{} no (or unspecified) value
  860. ;;; Evaluating subexpression 2 in current module (guile-user)
  861. @result{} no (or unspecified) value
  862. --:** *Guile Evaluation* (Scheme:ready)--All------------
  863. @end lisp
  864. @noindent
  865. this tells you that the evaluation was successful but that the return
  866. values were unspecified. Its effect was to load a module of example
  867. functions and set a trap on one of these functions, @code{fact1}, that
  868. calculates the factorial of its argument.
  869. If you now call @code{fact1}, you can see the trap and GDS's stack
  870. display in action. To do this add
  871. @lisp
  872. (fact1 4)
  873. @end lisp
  874. @noindent
  875. to your @file{testgds.scm} buffer and type @kbd{C-x C-e} (which
  876. evaluates the expression that the cursor is just after the end of).
  877. The result should be that a GDS stack window like the following
  878. appears:
  879. @lisp
  880. Calling procedure:
  881. => s [fact1 4]
  882. s [primitive-eval (fact1 4)]
  883. --:** PID 28729 (Guile-Debug)--All------------
  884. @end lisp
  885. This stack tells you that Guile is about to call the @code{fact1}
  886. procedure, with argument 4, and you can step through this call in
  887. detail by pressing @kbd{i} once and then @kbd{@key{SPC}}
  888. (@pxref{Continuing Execution}).
  889. (@kbd{i} is needed as the first keystroke rather than @kbd{@key{SPC}},
  890. because the aim here is to step through code in the @code{(ice-9
  891. debugging example-fns)} module, whose source file is
  892. @file{@dots{}/ice-9/debugging/example-fns.scm}, but the initial
  893. @code{(fact1 4)} call comes from the Guile session, whose ``source
  894. file'' Guile presents as @file{standard input}. If the user starts by
  895. pressing @kbd{@key{SPC}} instead of @kbd{i}, the effect is that the
  896. program runs until it hits the first recursive call @code{(fact1 (- n
  897. 1))}, where it stops because of the trap on @code{fact1} firing again.
  898. At this point, the source file @emph{is}
  899. @file{@dots{}/ice-9/debugging/example-fns.scm}, because the recursive
  900. @code{(fact1 (- n 1))} call comes from code in that file, so further
  901. pressing of @kbd{@key{SPC}} successfully single-steps through this
  902. file.)
  903. @c Local Variables:
  904. @c TeX-master: "guile.texi"
  905. @c End: