scheme-using.texi 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Guile Reference Manual.
  3. @c Copyright (C) 2006, 2010, 2011, 2012, 2013
  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. scheme@@(guile-user)> (+ 3 4 5)
  15. $1 = 12
  16. scheme@@(guile-user)> (display "Hello world!\n")
  17. Hello world!
  18. scheme@@(guile-user)> (values 'a 'b)
  19. $2 = a
  20. $3 = 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. The prompt shows you what language and module you are in. In this case, the
  28. current language is @code{scheme}, and the current module is
  29. @code{(guile-user)}. @xref{Other Languages}, for more information on Guile's
  30. support for languages other than Scheme.
  31. @menu
  32. * Init File::
  33. * Readline::
  34. * Value History::
  35. * REPL Commands::
  36. * Error Handling::
  37. * Interactive Debugging::
  38. @end menu
  39. @node Init File
  40. @subsection The Init File, @file{~/.guile}
  41. @cindex .guile
  42. When run interactively, Guile will load a local initialization file from
  43. @file{~/.guile}. This file should contain Scheme expressions for
  44. evaluation.
  45. This facility lets the user customize their interactive Guile
  46. environment, pulling in extra modules or parameterizing the REPL
  47. implementation.
  48. To run Guile without loading the init file, use the @code{-q}
  49. command-line option.
  50. @node Readline
  51. @subsection Readline
  52. To make it easier for you to repeat and vary previously entered
  53. expressions, or to edit the expression that you're typing in, Guile
  54. can use the GNU Readline library. This is not enabled by default
  55. because of licensing reasons, but all you need to activate Readline is
  56. the following pair of lines.
  57. @lisp
  58. scheme@@(guile-user)> (use-modules (ice-9 readline))
  59. scheme@@(guile-user)> (activate-readline)
  60. @end lisp
  61. It's a good idea to put these two lines (without the
  62. @code{scheme@@(guile-user)>} prompts) in your @file{.guile} file.
  63. @xref{Init File}, for more on @file{.guile}.
  64. @node Value History
  65. @subsection Value History
  66. Just as Readline helps you to reuse a previous input line, @dfn{value
  67. history} allows you to use the @emph{result} of a previous evaluation in
  68. a new expression. When value history is enabled, each evaluation result
  69. is automatically assigned to the next in the sequence of variables
  70. @code{$1}, @code{$2}, @dots{}. You can then use these variables in
  71. subsequent expressions.
  72. @lisp
  73. scheme@@(guile-user)> (iota 10)
  74. $1 = (0 1 2 3 4 5 6 7 8 9)
  75. scheme@@(guile-user)> (apply * (cdr $1))
  76. $2 = 362880
  77. scheme@@(guile-user)> (sqrt $2)
  78. $3 = 602.3952191045344
  79. scheme@@(guile-user)> (cons $2 $1)
  80. $4 = (362880 0 1 2 3 4 5 6 7 8 9)
  81. @end lisp
  82. Value history is enabled by default, because Guile's REPL imports the
  83. @code{(ice-9 history)} module. Value history may be turned off or on within the
  84. repl, using the options interface:
  85. @lisp
  86. scheme@@(guile-user)> ,option value-history #f
  87. scheme@@(guile-user)> 'foo
  88. foo
  89. scheme@@(guile-user)> ,option value-history #t
  90. scheme@@(guile-user)> 'bar
  91. $5 = bar
  92. @end lisp
  93. Note that previously recorded values are still accessible, even if value history
  94. is off. In rare cases, these references to past computations can cause Guile to
  95. use too much memory. One may clear these values, possibly enabling garbage
  96. collection, via the @code{clear-value-history!} procedure, described below.
  97. The programmatic interface to value history is in a module:
  98. @lisp
  99. (use-modules (ice-9 history))
  100. @end lisp
  101. @deffn {Scheme Procedure} value-history-enabled?
  102. Return true if value history is enabled, or false otherwise.
  103. @end deffn
  104. @deffn {Scheme Procedure} enable-value-history!
  105. Turn on value history, if it was off.
  106. @end deffn
  107. @deffn {Scheme Procedure} disable-value-history!
  108. Turn off value history, if it was on.
  109. @end deffn
  110. @deffn {Scheme Procedure} clear-value-history!
  111. Clear the value history. If the stored values are not captured by some other
  112. data structure or closure, they may then be reclaimed by the garbage collector.
  113. @end deffn
  114. @node REPL Commands
  115. @subsection REPL Commands
  116. @cindex commands
  117. The REPL exists to read expressions, evaluate them, and then print their
  118. results. But sometimes one wants to tell the REPL to evaluate an
  119. expression in a different way, or to do something else altogether. A
  120. user can affect the way the REPL works with a @dfn{REPL command}.
  121. The previous section had an example of a command, in the form of
  122. @code{,option}.
  123. @lisp
  124. scheme@@(guile-user)> ,option value-history #t
  125. @end lisp
  126. @noindent
  127. Commands are distinguished from expressions by their initial comma
  128. (@samp{,}). Since a comma cannot begin an expression in most languages,
  129. it is an effective indicator to the REPL that the following text forms a
  130. command, not an expression.
  131. REPL commands are convenient because they are always there. Even if the
  132. current module doesn't have a binding for @code{pretty-print}, one can
  133. always @code{,pretty-print}.
  134. The following sections document the various commands, grouped together
  135. by functionality. Many of the commands have abbreviations; see the
  136. online help (@code{,help}) for more information.
  137. @menu
  138. * Help Commands::
  139. * Module Commands::
  140. * Language Commands::
  141. * Compile Commands::
  142. * Profile Commands::
  143. * Debug Commands::
  144. * Inspect Commands::
  145. * System Commands::
  146. @end menu
  147. @node Help Commands
  148. @subsubsection Help Commands
  149. When Guile starts interactively, it notifies the user that help can be
  150. had by typing @samp{,help}. Indeed, @code{help} is a command, and a
  151. particularly useful one, as it allows the user to discover the rest of
  152. the commands.
  153. @deffn {REPL Command} help [@code{all} | group | @code{[-c]} command]
  154. Show help.
  155. With one argument, tries to look up the argument as a group name, giving
  156. help on that group if successful. Otherwise tries to look up the
  157. argument as a command, giving help on the command.
  158. If there is a command whose name is also a group name, use the @samp{-c
  159. @var{command}} form to give help on the command instead of the group.
  160. Without any argument, a list of help commands and command groups
  161. are displayed.
  162. @end deffn
  163. @deffn {REPL Command} show [topic]
  164. Gives information about Guile.
  165. With one argument, tries to show a particular piece of information;
  166. currently supported topics are `warranty' (or `w'), `copying' (or `c'),
  167. and `version' (or `v').
  168. Without any argument, a list of topics is displayed.
  169. @end deffn
  170. @deffn {REPL Command} apropos regexp
  171. Find bindings/modules/packages.
  172. @end deffn
  173. @deffn {REPL Command} describe obj
  174. Show description/documentation.
  175. @end deffn
  176. @node Module Commands
  177. @subsubsection Module Commands
  178. @deffn {REPL Command} module [module]
  179. Change modules / Show current module.
  180. @end deffn
  181. @deffn {REPL Command} import module @dots{}
  182. Import modules / List those imported.
  183. @end deffn
  184. @deffn {REPL Command} load file
  185. Load a file in the current module.
  186. @end deffn
  187. @deffn {REPL Command} reload [module]
  188. Reload the given module, or the current module if none was given.
  189. @end deffn
  190. @deffn {REPL Command} binding
  191. List current bindings.
  192. @end deffn
  193. @deffn {REPL Command} in module expression
  194. @deffnx {REPL Command} in module command arg @dots{}
  195. Evaluate an expression, or alternatively, execute another meta-command
  196. in the context of a module. For example, @samp{,in (foo bar) ,binding}
  197. will show the bindings in the module @code{(foo bar)}.
  198. @end deffn
  199. @node Language Commands
  200. @subsubsection Language Commands
  201. @deffn {REPL Command} language language
  202. Change languages.
  203. @end deffn
  204. @node Compile Commands
  205. @subsubsection Compile Commands
  206. @deffn {REPL Command} compile exp
  207. Generate compiled code.
  208. @end deffn
  209. @deffn {REPL Command} compile-file file
  210. Compile a file.
  211. @end deffn
  212. @deffn {REPL Command} expand exp
  213. Expand any macros in a form.
  214. @end deffn
  215. @deffn {REPL Command} optimize exp
  216. Run the optimizer on a piece of code and print the result.
  217. @end deffn
  218. @deffn {REPL Command} disassemble exp
  219. Disassemble a compiled procedure.
  220. @end deffn
  221. @deffn {REPL Command} disassemble-file file
  222. Disassemble a file.
  223. @end deffn
  224. @node Profile Commands
  225. @subsubsection Profile Commands
  226. @deffn {REPL Command} time exp
  227. Time execution.
  228. @end deffn
  229. @deffn {REPL Command} profile exp [#:hz hz=100] @
  230. [#:count-calls? count-calls?=#f] [#:display-style display-style=list]
  231. Profile execution of an expression. This command compiled @var{exp} and
  232. then runs it within the statprof profiler, passing all keyword options
  233. to the @code{statprof} procedure. For more on statprof and on the the
  234. options available to this command, @xref{Statprof}.
  235. @end deffn
  236. @deffn {REPL Command} trace exp [#:width w] [#:max-indent i]
  237. Trace execution.
  238. By default, the trace will limit its width to the width of your
  239. terminal, or @var{width} if specified. Nested procedure invocations
  240. will be printed farther to the right, though if the width of the
  241. indentation passes the @var{max-indent}, the indentation is abbreviated.
  242. @end deffn
  243. These REPL commands can also be called as regular functions in scheme
  244. code on including the @code{(ice-9 time)} module.
  245. @node Debug Commands
  246. @subsubsection Debug Commands
  247. These debugging commands are only available within a recursive REPL;
  248. they do not work at the top level.
  249. @deffn {REPL Command} backtrace [count] [#:width w] [#:full? f]
  250. Print a backtrace.
  251. Print a backtrace of all stack frames, or innermost @var{count} frames.
  252. If @var{count} is negative, the last @var{count} frames will be shown.
  253. @end deffn
  254. @deffn {REPL Command} up [count]
  255. Select a calling stack frame.
  256. Select and print stack frames that called this one.
  257. An argument says how many frames up to go.
  258. @end deffn
  259. @deffn {REPL Command} down [count]
  260. Select a called stack frame.
  261. Select and print stack frames called by this one.
  262. An argument says how many frames down to go.
  263. @end deffn
  264. @deffn {REPL Command} frame [idx]
  265. Show a frame.
  266. Show the selected frame. With an argument, select a frame by index,
  267. then show it.
  268. @end deffn
  269. @deffn {REPL Command} locals
  270. Show local variables.
  271. Show locally-bound variables in the selected frame.
  272. @end deffn
  273. @deffn {REPL Command} error-message
  274. @deffnx {REPL Command} error
  275. Show error message.
  276. Display the message associated with the error that started the current
  277. debugging REPL.
  278. @end deffn
  279. @deffn {REPL Command} registers
  280. Show the VM registers associated with the current frame.
  281. @xref{Stack Layout}, for more information on VM stack frames.
  282. @end deffn
  283. @deffn {REPL Command} width [cols]
  284. Sets the number of display columns in the output of @code{,backtrace}
  285. and @code{,locals} to @var{cols}. If @var{cols} is not given, the width
  286. of the terminal is used.
  287. @end deffn
  288. The next 3 commands work at any REPL.
  289. @deffn {REPL Command} break proc
  290. Set a breakpoint at @var{proc}.
  291. @end deffn
  292. @deffn {REPL Command} break-at-source file line
  293. Set a breakpoint at the given source location.
  294. @end deffn
  295. @deffn {REPL Command} tracepoint proc
  296. Set a tracepoint on the given procedure. This will cause all calls to
  297. the procedure to print out a tracing message. @xref{Tracing Traps}, for
  298. more information.
  299. @end deffn
  300. The rest of the commands in this subsection all apply only when the
  301. stack is @dfn{continuable} --- in other words when it makes sense for
  302. the program that the stack comes from to continue running. Usually this
  303. means that the program stopped because of a trap or a breakpoint.
  304. @deffn {REPL Command} step
  305. Tell the debugged program to step to the next source location.
  306. @end deffn
  307. @deffn {REPL Command} next
  308. Tell the debugged program to step to the next source location in the
  309. same frame. (See @ref{Traps} for the details of how this works.)
  310. @end deffn
  311. @deffn {REPL Command} finish
  312. Tell the program being debugged to continue running until the completion
  313. of the current stack frame, and at that time to print the result and
  314. reenter the REPL.
  315. @end deffn
  316. @node Inspect Commands
  317. @subsubsection Inspect Commands
  318. @deffn {REPL Command} inspect exp
  319. Inspect the result(s) of evaluating @var{exp}.
  320. @end deffn
  321. @deffn {REPL Command} pretty-print exp
  322. Pretty-print the result(s) of evaluating @var{exp}.
  323. @end deffn
  324. @node System Commands
  325. @subsubsection System Commands
  326. @deffn {REPL Command} gc
  327. Garbage collection.
  328. @end deffn
  329. @deffn {REPL Command} statistics
  330. Display statistics.
  331. @end deffn
  332. @deffn {REPL Command} option [name] [exp]
  333. With no arguments, lists all options. With one argument, shows the
  334. current value of the @var{name} option. With two arguments, sets the
  335. @var{name} option to the result of evaluating the Scheme expression
  336. @var{exp}.
  337. @end deffn
  338. @deffn {REPL Command} quit
  339. Quit this session.
  340. @end deffn
  341. Current REPL options include:
  342. @table @code
  343. @item compile-options
  344. The options used when compiling expressions entered at the REPL.
  345. @xref{Compilation}, for more on compilation options.
  346. @item interp
  347. Whether to interpret or compile expressions given at the REPL, if such a
  348. choice is available. Off by default (indicating compilation).
  349. @item prompt
  350. A customized REPL prompt. @code{#f} by default, indicating the default
  351. prompt.
  352. @item print
  353. A procedure of two arguments used to print the result of evaluating each
  354. expression. The arguments are the current REPL and the value to print.
  355. By default, @code{#f}, to use the default procedure.
  356. @item value-history
  357. Whether value history is on or not. @xref{Value History}.
  358. @item on-error
  359. What to do when an error happens. By default, @code{debug}, meaning to
  360. enter the debugger. Other values include @code{backtrace}, to show a
  361. backtrace without entering the debugger, or @code{report}, to simply
  362. show a short error printout.
  363. @end table
  364. Default values for REPL options may be set using
  365. @code{repl-default-option-set!} from @code{(system repl common)}:
  366. @deffn {Scheme Procedure} repl-default-option-set! key value
  367. Set the default value of a REPL option. This function is particularly
  368. useful in a user's init file. @xref{Init File}.
  369. @end deffn
  370. @node Error Handling
  371. @subsection Error Handling
  372. When code being evaluated from the REPL hits an error, Guile enters a
  373. new prompt, allowing you to inspect the context of the error.
  374. @lisp
  375. scheme@@(guile-user)> (map string-append '("a" "b") '("c" #\d))
  376. ERROR: In procedure string-append:
  377. ERROR: Wrong type (expecting string): #\d
  378. Entering a new prompt. Type `,bt' for a backtrace or `,q' to continue.
  379. scheme@@(guile-user) [1]>
  380. @end lisp
  381. The new prompt runs inside the old one, in the dynamic context of the
  382. error. It is a recursive REPL, augmented with a reified representation
  383. of the stack, ready for debugging.
  384. @code{,backtrace} (abbreviated @code{,bt}) displays the Scheme call
  385. stack at the point where the error occurred:
  386. @lisp
  387. scheme@@(guile-user) [1]> ,bt
  388. 1 (map #<procedure string-append _> ("a" "b") ("c" #\d))
  389. 0 (string-append "b" #\d)
  390. @end lisp
  391. In the above example, the backtrace doesn't have much source
  392. information, as @code{map} and @code{string-append} are both
  393. primitives. But in the general case, the space on the left of the
  394. backtrace indicates the line and column in which a given procedure calls
  395. another.
  396. You can exit a recursive REPL in the same way that you exit any REPL:
  397. via @samp{(quit)}, @samp{,quit} (abbreviated @samp{,q}), or
  398. @kbd{C-d}, among other options.
  399. @node Interactive Debugging
  400. @subsection Interactive Debugging
  401. A recursive debugging REPL exposes a number of other meta-commands that
  402. inspect the state of the computation at the time of the error. These
  403. commands allow you to
  404. @itemize @bullet
  405. @item
  406. display the Scheme call stack at the point where the error occurred;
  407. @item
  408. move up and down the call stack, to see in detail the expression being
  409. evaluated, or the procedure being applied, in each @dfn{frame}; and
  410. @item
  411. examine the values of variables and expressions in the context of each
  412. frame.
  413. @end itemize
  414. @noindent
  415. @xref{Debug Commands}, for documentation of the individual
  416. commands. This section aims to give more of a walkthrough of a typical
  417. debugging session.
  418. First, we're going to need a good error. Let's try to macroexpand the
  419. expression @code{(unquote foo)}, outside of a @code{quasiquote} form,
  420. and see how the macroexpander reports this error.
  421. @lisp
  422. scheme@@(guile-user)> (macroexpand '(unquote foo))
  423. ERROR: In procedure macroexpand:
  424. ERROR: unquote: expression not valid outside of quasiquote in (unquote foo)
  425. Entering a new prompt. Type `,bt' for a backtrace or `,q' to continue.
  426. scheme@@(guile-user) [1]>
  427. @end lisp
  428. The @code{backtrace} command, which can also be invoked as @code{bt},
  429. displays the call stack (aka backtrace) at the point where the debugger
  430. was entered:
  431. @lisp
  432. scheme@@(guile-user) [1]> ,bt
  433. In ice-9/psyntax.scm:
  434. 1130:21 3 (chi-top (unquote foo) () ((top)) e (eval) (hygiene #))
  435. 1071:30 2 (syntax-type (unquote foo) () ((top)) #f #f (# #) #f)
  436. 1368:28 1 (chi-macro #<procedure de9360 at ice-9/psyntax.scm...> ...)
  437. In unknown file:
  438. 0 (scm-error syntax-error macroexpand "~a: ~a in ~a" # #f)
  439. @end lisp
  440. A call stack consists of a sequence of stack @dfn{frames}, with each
  441. frame describing one procedure which is waiting to do something with the
  442. values returned by another. Here we see that there are four frames on
  443. the stack.
  444. Note that @code{macroexpand} is not on the stack -- it must have made a
  445. tail call to @code{chi-top}, as indeed we would find if we searched
  446. @code{ice-9/psyntax.scm} for its definition.
  447. When you enter the debugger, the innermost frame is selected, which
  448. means that the commands for getting information about the ``current''
  449. frame, or for evaluating expressions in the context of the current
  450. frame, will do so by default with respect to the innermost frame. To
  451. select a different frame, so that these operations will apply to it
  452. instead, use the @code{up}, @code{down} and @code{frame} commands like
  453. this:
  454. @lisp
  455. scheme@@(guile-user) [1]> ,up
  456. In ice-9/psyntax.scm:
  457. 1368:28 1 (chi-macro #<procedure de9360 at ice-9/psyntax.scm...> ...)
  458. scheme@@(guile-user) [1]> ,frame 3
  459. In ice-9/psyntax.scm:
  460. 1130:21 3 (chi-top (unquote foo) () ((top)) e (eval) (hygiene #))
  461. scheme@@(guile-user) [1]> ,down
  462. In ice-9/psyntax.scm:
  463. 1071:30 2 (syntax-type (unquote foo) () ((top)) #f #f (# #) #f)
  464. @end lisp
  465. Perhaps we're interested in what's going on in frame 2, so we take a
  466. look at its local variables:
  467. @lisp
  468. scheme@@(guile-user) [1]> ,locals
  469. Local variables:
  470. $1 = e = (unquote foo)
  471. $2 = r = ()
  472. $3 = w = ((top))
  473. $4 = s = #f
  474. $5 = rib = #f
  475. $6 = mod = (hygiene guile-user)
  476. $7 = for-car? = #f
  477. $8 = first = unquote
  478. $9 = ftype = macro
  479. $10 = fval = #<procedure de9360 at ice-9/psyntax.scm:2817:2 (x)>
  480. $11 = fe = unquote
  481. $12 = fw = ((top))
  482. $13 = fs = #f
  483. $14 = fmod = (hygiene guile-user)
  484. @end lisp
  485. All of the values are accessible by their value-history names
  486. (@code{$@var{n}}):
  487. @lisp
  488. scheme@@(guile-user) [1]> $10
  489. $15 = #<procedure de9360 at ice-9/psyntax.scm:2817:2 (x)>
  490. @end lisp
  491. We can even invoke the procedure at the REPL directly:
  492. @lisp
  493. scheme@@(guile-user) [1]> ($10 'not-going-to-work)
  494. ERROR: In procedure macroexpand:
  495. ERROR: source expression failed to match any pattern in not-going-to-work
  496. Entering a new prompt. Type `,bt' for a backtrace or `,q' to continue.
  497. @end lisp
  498. Well at this point we've caused an error within an error. Let's just
  499. quit back to the top level:
  500. @lisp
  501. scheme@@(guile-user) [2]> ,q
  502. scheme@@(guile-user) [1]> ,q
  503. scheme@@(guile-user)>
  504. @end lisp
  505. Finally, as a word to the wise: hackers close their REPL prompts with
  506. @kbd{C-d}.
  507. @node Using Guile in Emacs
  508. @section Using Guile in Emacs
  509. @cindex Emacs
  510. Any text editor can edit Scheme, but some are better than others. Emacs
  511. is the best, of course, and not just because it is a fine text editor.
  512. Emacs has good support for Scheme out of the box, with sensible
  513. indentation rules, parenthesis-matching, syntax highlighting, and even a
  514. set of keybindings for structural editing, allowing navigation,
  515. cut-and-paste, and transposition operations that work on balanced
  516. S-expressions.
  517. As good as it is, though, two things will vastly improve your experience
  518. with Emacs and Guile.
  519. @cindex Paredit
  520. The first is Taylor Campbell's
  521. @uref{http://www.emacswiki.org/emacs/ParEdit, Paredit}. You should not
  522. code in any dialect of Lisp without Paredit. (They say that
  523. unopinionated writing is boring---hence this tone---but it's the
  524. truth, regardless.) Paredit is the bee's knees.
  525. @cindex Geiser
  526. The second is
  527. @iftex
  528. Jos@'e
  529. @end iftex
  530. @ifnottex
  531. José
  532. @end ifnottex
  533. Antonio Ortega Ruiz's
  534. @uref{http://www.nongnu.org/geiser/, Geiser}. Geiser complements Emacs'
  535. @code{scheme-mode} with tight integration to running Guile processes via
  536. a @code{comint-mode} REPL buffer.
  537. Of course there are keybindings to switch to the REPL, and a good REPL
  538. environment, but Geiser goes beyond that, providing:
  539. @itemize @bullet
  540. @item
  541. Form evaluation in the context of the current file's module.
  542. @item
  543. Macro expansion.
  544. @item
  545. File/module loading and/or compilation.
  546. @item
  547. Namespace-aware identifier completion (including local bindings, names
  548. visible in the current module, and module names).
  549. @item
  550. Autodoc: the echo area shows information about the signature of the
  551. procedure/macro around point automatically.
  552. @item
  553. Jump to definition of identifier at point.
  554. @item
  555. Access to documentation (including docstrings when the implementation
  556. provides it).
  557. @item
  558. Listings of identifiers exported by a given module.
  559. @item
  560. Listings of callers/callees of procedures.
  561. @item
  562. Rudimentary support for debugging and error navigation.
  563. @item
  564. Support for multiple, simultaneous REPLs.
  565. @end itemize
  566. See Geiser's web page at @uref{http://www.nongnu.org/geiser/}, for more
  567. information.
  568. @node Using Guile Tools
  569. @section Using Guile Tools
  570. @cindex guild
  571. @cindex guile-tools
  572. @cindex wizards
  573. Guile also comes with a growing number of command-line utilities: a
  574. compiler, a disassembler, some module inspectors, and in the future, a
  575. system to install Guile packages from the internet. These tools may be
  576. invoked using the @code{guild} program.
  577. @example
  578. $ guild compile -o foo.go foo.scm
  579. wrote `foo.go'
  580. @end example
  581. This program used to be called @code{guile-tools} up to
  582. Guile version 2.0.1, and for backward
  583. compatibility it still may be called as such. However we changed the
  584. name to @code{guild}, not only because it is pleasantly shorter and
  585. easier to read, but also because this tool will serve to bind Guile
  586. wizards together, by allowing hackers to share code with each other
  587. using a CPAN-like system.
  588. @xref{Compilation}, for more on @code{guild compile}.
  589. A complete list of guild scripts can be had by invoking @code{guild
  590. list}, or simply @code{guild}.
  591. @node Installing Site Packages
  592. @section Installing Site Packages
  593. @cindex site
  594. @cindex site path
  595. @cindex load path
  596. @findex %site-dir
  597. @findex %site-ccache-dir
  598. At some point, you will probably want to share your code with other
  599. people. To do so effectively, it is important to follow a set of common
  600. conventions, to make it easy for the user to install and use your
  601. package.
  602. The first thing to do is to install your Scheme files where Guile can
  603. find them. When Guile goes to find a Scheme file, it will search a
  604. @dfn{load path} to find the file: first in Guile's own path, then in
  605. paths for @dfn{site packages}. A site package is any Scheme code that
  606. is installed and not part of Guile itself. @xref{Load Paths}, for more
  607. on load paths.
  608. There are several site paths, for historical reasons, but the one that
  609. should generally be used can be obtained by invoking the
  610. @code{%site-dir} procedure. @xref{Build Config}. If Guile
  611. @value{EFFECTIVE-VERSION} is installed on your system in @code{/usr/},
  612. then @code{(%site-dir)} will be
  613. @code{/usr/share/guile/site/@value{EFFECTIVE-VERSION}}. Scheme files
  614. should be installed there.
  615. If you do not install compiled @code{.go} files, Guile will compile your
  616. modules and programs when they are first used, and cache them in the
  617. user's home directory. @xref{Compilation}, for more on
  618. auto-compilation. However, it is better to compile the files before
  619. they are installed, and to just copy the files to a place that Guile can
  620. find them.
  621. As with Scheme files, Guile searches a path to find compiled @code{.go}
  622. files, the @code{%load-compiled-path}. By default, this path has two
  623. entries: a path for Guile's files, and a path for site packages. You
  624. should install your @code{.go} files into the latter directory, whose
  625. value is returned by invoking the @code{%site-ccache-dir} procedure. As
  626. in the previous example, if Guile @value{EFFECTIVE-VERSION} is installed
  627. on your system in @code{/usr/}, then @code{(%site-ccache-dir)} site
  628. packages will be
  629. @code{/usr/lib/guile/@value{EFFECTIVE-VERSION}/site-ccache}.
  630. Note that a @code{.go} file will only be loaded in preference to a
  631. @code{.scm} file if it is newer. For that reason, you should install
  632. your Scheme files first, and your compiled files second. @xref{Load
  633. Paths}, for more on the loading process.
  634. Finally, although this section is only about Scheme, sometimes you need
  635. to install C extensions too. Shared libraries should be installed in
  636. the @dfn{extensions dir}. This value can be had from the build config
  637. (@pxref{Build Config}). Again, if Guile @value{EFFECTIVE-VERSION} is
  638. installed on your system in @code{/usr/}, then the extensions dir will
  639. be @code{/usr/lib/guile/@value{EFFECTIVE-VERSION}/extensions}.
  640. @node Distributing Guile Code
  641. @section Distributing Guile Code
  642. @cindex distribution, of Guile projects
  643. There's a tool that doesn't come bundled with Guile and yet can be very
  644. useful in your day to day experience with it. This tool is
  645. @uref{https://gitlab.com/a-sassmannshausen/guile-hall, Hall}.
  646. Hall helps you create, manage, and package your Guile projects through a
  647. simple command-line interface. When you start a new project, Hall
  648. creates a folder containing a scaffold of your new project. It contains
  649. a directory for your tests, for your libraries, for your scripts and for
  650. your documentation. This means you immediately know where to put the
  651. files you are hacking on.
  652. @cindex build system, for Guile code
  653. In addition, the scaffold will include your basic ``Autotools'' setup,
  654. so you don't have to take care of that yourself (@pxref{The GNU Build
  655. System,,, autoconf, Autoconf: Creating Automatic Configuration Scripts},
  656. for more information on the GNU ``Autotools''). Having Autotools set up
  657. with your project means you can immediately start hacking on your
  658. project without worrying about whether your code will work on other
  659. people's computers. Hall can also generate package definitions for the
  660. GNU@tie{}Guix package manager, making it easy for Guix users to install
  661. it.
  662. @c Local Variables:
  663. @c TeX-master: "guile.texi"
  664. @c End: