Added Copyright notice.
[bpt/guile.git] / doc / ref / program.texi
CommitLineData
2da09c3f
MV
1@c -*-texinfo-*-
2@c This is part of the GNU Guile Reference Manual.
3@c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004
4@c Free Software Foundation, Inc.
5@c See the file guile.texi for copying conditions.
6
9401323e 7@page
226297eb
NJ
8@node Programming Overview
9@chapter An Overview of Guile Programming
10
11Guile is designed as an extension language interpreter that is
12straightforward to integrate with applications written in C (and C++).
13The big win here for the application developer is that Guile
14integration, as the Guile web page says, ``lowers your project's
15hacktivation energy.'' Lowering the hacktivation energy means that you,
16as the application developer, @emph{and your users}, reap the benefits
17that flow from being able to extend the application in a high level
18extension language rather than in plain old C.
19
1982a56a
NJ
20In abstract terms, it's difficult to explain what this really means and
21what the integration process involves, so instead let's begin by jumping
22straight into an example of how you might integrate Guile into an
23existing program, and what you could expect to gain by so doing. With
24that example under our belts, we'll then return to a more general
25analysis of the arguments involved and the range of programming options
26available.
27
28@menu
29* Extending Dia:: How one might extend Dia using Guile.
30* Scheme vs C:: Why Scheme is more hackable than C.
31* Testbed Example:: Example: using Guile in a testbed.
32* Programming Options:: Options for Guile programming.
33* User Programming:: How about application users?
34@end menu
35
36
37@node Extending Dia
38@section How One Might Extend Dia Using Guile
39
40Dia is a free software program for drawing schematic diagrams like flow
41charts and floor plans (REFFIXME). This section conducts the thought
42experiment of adding Guile to Dia. In so doing, it aims to illustrate
43several of the steps and considerations involved in adding Guile to
44applications in general.
45
46@menu
47* Dia Objective:: Deciding why you want to add Guile.
48* Dia Steps:: Four steps required to add Guile.
49* Dia Smobs:: How to represent Dia data in Scheme.
50* Dia Primitives:: Writing Guile primitives for Dia.
51* Dia Hook:: Providing a hook for Scheme evaluation.
52* Dia Structure:: Overall structure for adding Guile.
53* Dia Advanced:: Going further with Dia and Guile.
54@end menu
55
56
57@node Dia Objective
58@subsection Deciding Why You Want to Add Guile
59
60First off, you should understand why you want to add Guile to Dia at
61all, and that means forming a picture of what Dia does and how it does
62it. So, what are the constituents of the Dia application?
63
64@itemize @bullet
65@item
66Most importantly, the @dfn{application domain objects} --- in other
67words, the concepts that differentiate Dia from another application such
68as a word processor or spreadsheet: shapes, templates, connectors,
69pages, plus the properties of all these things.
70
71@item
72The code that manages the graphical face of the application, including
73the layout and display of the objects above.
74
75@item
76The code that handles input events, which indicate that the application
77user is wanting to do something.
78@end itemize
79
80@noindent
81(In other words, a textbook example of the @dfn{model - view -
82controller} paradigm.)
83
84Next question: how will Dia benefit once the Guile integration is
85complete? Several (positive!) answers are possible here, and the choice
86is obviously up to the application developers. Still, one answer is
87that the main benefit will be the ability to manipulate Dia's
88application domain objects from Scheme.
89
90Suppose that Dia made a set of procedures available in Scheme,
91representing the most basic operations on objects such as shapes,
92connectors, and so on. Using Scheme, the application user could then
93write code that builds upon these basic operations to create more
94complex procedures. For example, given basic procedures to enumerate
95the objects on a page, to determine whether an object is a square, and
96to change the fill pattern of a single shape, the user can write a
97Scheme procedure to change the fill pattern of all squares on the
98current page:
99
100@lisp
101(define (change-squares'-fill-pattern new-pattern)
102 (for-each-shape current-page
103 (lambda (shape)
104 (if (square? shape)
105 (change-fill-pattern shape new-pattern)))))
106@end lisp
107
108
109@node Dia Steps
110@subsection Four Steps Required to Add Guile
111
112Assuming this objective, four steps are needed to achieve it.
113
114First, you need a way of representing your application-specific objects
115--- such as @code{shape} in the previous example --- when they are
116passed into the Scheme world. Unless your objects are so simple that
117they map naturally into builtin Scheme data types like numbers and
118strings, you will probably want to use Guile's @dfn{SMOB} interface to
119create a new Scheme data type for your objects.
120
121Second, you need to write code for the basic operations like
122@code{for-each-shape} and @code{square?} such that they access and
123manipulate your existing data structures correctly, and then make these
124operations available as @dfn{primitives} on the Scheme level.
125
126Third, you need to provide some mechanism within the Dia application
127that a user can hook into to cause arbitrary Scheme code to be
128evaluated.
129
130Finally, you need to restructure your top-level application C code a
131little so that it initializes the Guile interpreter correctly and
132declares your @dfn{SMOBs} and @dfn{primitives} to the Scheme world.
133
134The following subsections expand on these four points in turn.
135
136
137@node Dia Smobs
138@subsection How to Represent Dia Data in Scheme
139
140For all but the most trivial applications, you will probably want to
141allow some representation of your domain objects to exist on the Scheme
142level. This is where the idea of SMOBs comes in, and with it issues of
143lifetime management and garbage collection.
144
145To get more concrete about this, let's look again at the example we gave
146earlier of how application users can use Guile to build higher-level
147functions from the primitives that Dia itself provides.
148
149@lisp
150(define (change-squares'-fill-pattern new-pattern)
151 (for-each-shape current-page
152 (lambda (shape)
153 (if (square? shape)
154 (change-fill-pattern shape new-pattern)))))
155@end lisp
156
157Consider what is stored here in the variable @code{shape}. For each
158shape on the current page, the @code{for-each-shape} primitive calls
159@code{(lambda (shape) @dots{})} with an argument representing that
160shape. Question is: how is that argument represented on the Scheme
161level? The issues are as follows.
162
163@itemize @bullet
164@item
165Whatever the representation, it has to be decodable again by the C code
166for the @code{square?} and @code{change-fill-pattern} primitives. In
167other words, a primitive like @code{square?} has somehow to be able to
168turn the value that it receives back into something that points to the
169underlying C structure describing a shape.
170
171@item
172The representation must also cope with Scheme code holding on to the
173value for later use. What happens if the Scheme code stores
174@code{shape} in a global variable, but then that shape is deleted (in a
175way that the Scheme code is not aware of), and later on some other
176Scheme code uses that global variable again in a call to, say,
177@code{square?}?
178
179@item
180The lifetime and memory allocation of objects that exist @emph{only} in
181the Scheme world is managed automatically by Guile's garbage collector
182using one simple rule: when there are no remaining references to an
183object, the object is considered dead and so its memory is freed. But
184for objects that exist in both C and Scheme, the picture is more
185complicated; in the case of Dia, where the @code{shape} argument passes
186transiently in and out of the Scheme world, it would be quite wrong the
187@strong{delete} the underlying C shape just because the Scheme code has
188finished evaluation. How do we avoid this happening?
189@end itemize
190
191One resolution of these issues is for the Scheme-level representation of
192a shape to be a new, Scheme-specific C structure wrapped up as a SMOB.
193The SMOB is what is passed into and out of Scheme code, and the
194Scheme-specific C structure inside the SMOB points to Dia's underlying C
195structure so that the code for primitives like @code{square?} can get at
196it.
197
198To cope with an underlying shape being deleted while Scheme code is
199still holding onto a Scheme shape value, the underlying C structure
200should have a new field that points to the Scheme-specific SMOB. When a
201shape is deleted, the relevant code chains through to the
202Scheme-specific structure and sets its pointer back to the underlying
203structure to NULL. Thus the SMOB value for the shape continues to
204exist, but any primitive code that tries to use it will detect that the
205underlying shape has been deleted because the underlying structure
206pointer is NULL.
207
208So, to summarize the steps involved in this resolution of the problem
209(and assuming that the underlying C structure for a shape is
210@code{struct dia_shape}):
211
212@itemize @bullet
213@item
214Define a new Scheme-specific structure that @emph{points} to the
215underlying C structure:
216
217@lisp
218struct dia_guile_shape
219@{
220 struct dia_shape * c_shape; /* NULL => deleted */
221@}
222@end lisp
223
224@item
225Add a field to @code{struct dia_shape} that points to its @code{struct
226dia_guile_shape} if it has one ---
227
228@lisp
229struct dia_shape
230@{
231 @dots{}
232 struct dia_guile_shape * guile_shape;
233@}
234@end lisp
235
236@noindent
237--- so that C code can set @code{guile_shape->c_shape} to NULL when the
238underlying shape is deleted.
239
240@item
241Wrap @code{struct dia_guile_shape} as a SMOB type.
242
243@item
244Whenever you need to represent a C shape onto the Scheme level, create a
245SMOB instance for it, and pass that.
246
247@item
248In primitive code that receives a shape SMOB instance, check the
249@code{c_shape} field when decoding it, to find out whether the
250underlying C shape is still there.
251@end itemize
252
253As far as memory management is concerned, the SMOB values and their
254Scheme-specific structures are under the control of the garbage
255collector, whereas the underlying C structures are explicitly managed in
256exactly the same way that Dia managed them before we thought of adding
257Guile.
258
259When the garbage collector decides to free a shape SMOB value, it calls
260the @dfn{SMOB free} function that was specified when defining the shape
261SMOB type. To maintain the correctness of the @code{guile_shape} field
262in the underlying C structure, this function should chain through to the
263underlying C structure (if it still exists) and set its
264@code{guile_shape} field to NULL.
265
266For full documentation on defining and using SMOB types, see
267@ref{Defining New Types (Smobs)}.
268
269
270@node Dia Primitives
271@subsection Writing Guile Primitives for Dia
272
273Once the details of object representation are decided, writing the
274primitive function code that you need is usually straightforward.
275
276A primitive is simply a C function whose arguments and return value are
277all of type @code{SCM}, and whose body does whatever you want it to do.
278As an example, here is a possible implementation of the @code{square?}
279primitive:
280
281@lisp
282#define FUNC_NAME "square?"
283static SCM square_p (SCM shape)
284@{
285 struct dia_guile_shape * guile_shape;
286
287 /* Check that arg is really a shape SMOB. */
288 SCM_VALIDATE_SHAPE (SCM_ARG1, shape);
289
290 /* Access Scheme-specific shape structure. */
291 guile_shape = SCM_SMOB_DATA (shape);
292
293 /* Find out if underlying shape exists and is a
294 square; return answer as a Scheme boolean. */
295 return SCM_BOOL (guile_shape->c_shape &&
296 (guile_shape->c_shape->type == DIA_SQUARE));
297@}
298#undef FUNC_NAME
299@end lisp
300
301Notice how easy it is to chain through from the @code{SCM shape}
302parameter that @code{square_p} receives --- which is a SMOB --- to the
303Scheme-specific structure inside the SMOB, and thence to the underlying
304C structure for the shape.
305
306In this code, @code{SCM_SMOB_DATA} and @code{SCM_BOOL} are macros from
307the standard Guile API. @code{SCM_VALIDATE_SHAPE} is a macro that you
308should define as part of your SMOB definition: it checks that the passed
309parameter is of the expected type. This is needed to guard against
310Scheme code using the @code{square?} procedure incorrectly, as in
311@code{(square? "hello")}; Scheme's latent typing means that usage errors
312like this must be caught at run time.
313
314Having written the C code for your primitives, you need to make them
315available as Scheme procedures by calling the @code{scm_c_define_gsubr}
316function. @code{scm_c_define_gsubr} (REFFIXME) takes arguments that
317specify the Scheme-level name for the primitive and how many required,
318optional and rest arguments it can accept. The @code{square?} primitive
319always requires exactly one argument, so the call to make it available
320in Scheme reads like this:
321
322@lisp
323scm_c_define_gsubr ("square?", 1, 0, 0, square_p);
324@end lisp
325
326For where to put this call, see the subsection after next on the
327structure of Guile-enabled code (@pxref{Dia Structure}).
328
329
330@node Dia Hook
331@subsection Providing a Hook for the Evaluation of Scheme Code
332
333To make the Guile integration useful, you have to design some kind of
334hook into your application that application users can use to cause their
335Scheme code to be evaluated.
336
337Technically, this is straightforward; you just have to decide on a
338mechanism that is appropriate for your application. Think of Emacs, for
339example: when you type @kbd{@key{ESC} :}, you get a prompt where you can
340type in any Elisp code, which Emacs will then evaluate. Or, again like
341Emacs, you could provide a mechanism (such as an init file) to allow
342Scheme code to be associated with a particular key sequence, and
343evaluate the code when that key sequence is entered.
344
345In either case, once you have the Scheme code that you want to evaluate,
346as a null terminated string, you can tell Guile to evaluate it by
347calling the @code{scm_c_eval_string} function.
348
349
350@node Dia Structure
351@subsection Top-level Structure of Guile-enabled Dia
352
353Let's assume that the pre-Guile Dia code looks structurally like this:
354
355@itemize @bullet
356@item
357@code{main ()}
358
359@itemize @bullet
360@item
361do lots of initialization and setup stuff
362@item
363enter Gtk main loop
364@end itemize
365@end itemize
366
367When you add Guile to a program, one (rather technical) requirement is
368that Guile's garbage collector needs to know where the bottom of the C
369stack is. The easiest way to ensure this is to use
370@code{scm_boot_guile} like this:
371
372@itemize @bullet
373@item
374@code{main ()}
375
376@itemize @bullet
377@item
378do lots of initialization and setup stuff
379@item
380@code{scm_boot_guile (argc, argv, inner_main, NULL)}
381@end itemize
382
383@item
384@code{inner_main ()}
385
386@itemize @bullet
387@item
388define all SMOB types
389@item
390export primitives to Scheme using @code{scm_c_define_gsubr}
391@item
392enter Gtk main loop
393@end itemize
394@end itemize
395
396In other words, you move the guts of what was previously in your
397@code{main} function into a new function called @code{inner_main}, and
398then add a @code{scm_boot_guile} call, with @code{inner_main} as a
399parameter, to the end of @code{main}.
400
401Assuming that you are using SMOBs and have written primitive code as
402described in the preceding subsections, you also need to insert calls to
403declare your new SMOBs and export the primitives to Scheme. These
404declarations must happen @emph{inside} the dynamic scope of the
405@code{scm_boot_guile} call, but also @emph{before} any code is run that
406could possibly use them --- the beginning of @code{inner_main} is an
407ideal place for this.
408
409
410@node Dia Advanced
411@subsection Going Further with Dia and Guile
412
413The steps described so far implement an initial Guile integration that
414already gives a lot of additional power to Dia application users. But
415there are further steps that you could take, and it's interesting to
416consider a few of these.
417
418In general, you could progressively move more of Dia's source code from
419C into Scheme. This might make the code more maintainable and
420extensible, and it could open the door to new programming paradigms that
421are tricky to effect in C but straightforward in Scheme.
422
423A specific example of this is that you could use the guile-gtk package,
424which provides Scheme-level procedures for most of the Gtk+ library, to
425move the code that lays out and displays Dia objects from C to Scheme.
426
427As you follow this path, it naturally becomes less useful to maintain a
428distinction between Dia's original non-Guile-related source code, and
429its later code implementing SMOBs and primitives for the Scheme world.
430
431For example, suppose that the original source code had a
432@code{dia_change_fill_pattern} function:
433
434@lisp
435void dia_change_fill_pattern (struct dia_shape * shape,
436 struct dia_pattern * pattern)
437@{
438 /* real pattern change work */
439@}
440@end lisp
441
442During initial Guile integration, you add a @code{change_fill_pattern}
443primitive for Scheme purposes, which accesses the underlying structures
444from its SMOB values and uses @code{dia_change_fill_pattern} to do the
445real work:
446
447@lisp
448SCM change_fill_pattern (SCM shape, SCM pattern)
449@{
450 struct dia_shape * d_shape;
451 struct dia_pattern * d_pattern;
452
453 @dots{}
454
455 dia_change_fill_pattern (d_shape, d_pattern);
456
457 return SCM_UNSPECIFIED;
458@}
459@end lisp
460
461At this point, it makes sense to keep @code{dia_change_fill_pattern} and
462@code{change_fill_pattern} separate, because
463@code{dia_change_fill_pattern} can also be called without going through
464Scheme at all, say because the user clicks a button which causes a
465C-registered Gtk+ callback to be called.
466
467But, if the code for creating buttons and registering their callbacks is
468moved into Scheme (using guile-gtk), it may become true that
469@code{dia_change_fill_pattern} can no longer be called other than
470through Scheme. In which case, it makes sense to abolish it and move
471its contents directly into @code{change_fill_pattern}, like this:
472
473@lisp
474SCM change_fill_pattern (SCM shape, SCM pattern)
475@{
476 struct dia_shape * d_shape;
477 struct dia_pattern * d_pattern;
478
479 @dots{}
480
481 /* real pattern change work */
482
483 return SCM_UNSPECIFIED;
484@}
485@end lisp
486
487So further Guile integration progressively @emph{reduces} the amount of
488functional C code that you have to maintain over the long term.
489
490A similar argument applies to data representation. In the discussion of
491SMOBs earlier, issues arose because of the different memory management
492and lifetime models that normally apply to data structures in C and in
493Scheme. However, with further Guile integration, you can resolve this
494issue in a more radical way by allowing all your data structures to be
495under the control of the garbage collector, and kept alive by references
496from the Scheme world. Instead of maintaining an array or linked list
497of shapes in C, you would instead maintain a list in Scheme.
498
499Rather like the coalescing of @code{dia_change_fill_pattern} and
500@code{change_fill_pattern}, the practical upshot of such a change is
501that you would no longer have to keep the @code{dia_shape} and
502@code{dia_guile_shape} structures separate, and so wouldn't need to
503worry about the pointers between them. Instead, you could change the
504SMOB definition to wrap the @code{dia_shape} structure directly, and
505send @code{dia_guile_shape} off to the scrap yard. Cut out the middle
506man!
507
508Finally, we come to the holy grail of Guile's free software / extension
509language approach. Once you have a Scheme representation for
510interesting Dia data types like shapes, and a handy bunch of primitives
511for manipulating them, it suddenly becomes clear that you have a bundle
512of functionality that could have far-ranging use beyond Dia itself. In
513other words, the data types and primitives could now become a library,
514and Dia becomes just one of the many possible applications using that
515library --- albeit, at this early stage, a rather important one!
516
517In this model, Guile becomes just the glue that binds everything
518together. Imagine an application that usefully combined functionality
519from Dia, Gnumeric and GnuCash --- it's tricky right now, because no
520such application yet exists; but it'll happen some day @dots{}
521
522
523@node Scheme vs C
524@section Why Scheme is More Hackable Than C
525
526Underlying Guile's value proposition is the assumption that programming
527in a high level language, specifically Guile's implementation of Scheme,
528is necessarily better in some way than programming in C. What do we
529mean by this claim, and how can we be so sure?
226297eb
NJ
530
531One class of advantages applies not only to Scheme, but more generally
532to any interpretable, high level, scripting language, such as Emacs
533Lisp, Python, Ruby, or @TeX{}'s macro language. Common features of all
534such languages, when compared to C, are that:
9401323e
NJ
535
536@itemize @bullet
537@item
226297eb
NJ
538They lend themselves to rapid and experimental development cycles,
539owing usually to a combination of their interpretability and the
540integrated development environment in which they are used.
9401323e
NJ
541
542@item
226297eb
NJ
543They free developers from some of the low level bookkeeping tasks
544associated with C programming, notably memory management.
9401323e
NJ
545
546@item
226297eb
NJ
547They provide high level features such as container objects and exception
548handling that make common programming tasks easier.
9401323e
NJ
549@end itemize
550
1982a56a
NJ
551In the case of Scheme, particular features that make programming easier
552--- and more fun! --- are its powerful mechanisms for abstracting parts
553of programs (closures --- @pxref{About Closure}) and for iteration
226297eb
NJ
554(@pxref{while do}).
555
556The evidence in support of this argument is empirical: the huge amount
557of code that has been written in extension languages for applications
558that support this mechanism. Most notable are extensions written in
559Emacs Lisp for GNU Emacs, in @TeX{}'s macro language for @TeX{}, and in
560Script-Fu for the Gimp, but there is increasingly now a significant code
561eco-system for Guile-based applications as well, such as Lilypond and
562GnuCash. It is close to inconceivable that similar amounts of
563functionality could have been added to these applications just by
564writing new code in their base implementation languages.
565
226297eb
NJ
566
567@node Testbed Example
568@section Example: Using Guile for an Application Testbed
569
570As an example of what this means in practice, imagine writing a testbed
571for an application that is tested by submitting various requests (via a
572C interface) and validating the output received. Suppose further that
573the application keeps an idea of its current state, and that the
574``correct'' output for a given request may depend on the current
575application state. A complete ``white box''@footnote{A @dfn{white box}
576test plan is one that incorporates knowledge of the internal design of
577the application under test.} test plan for this application would aim to
578submit all possible requests in each distinguishable state, and validate
579the output for all request/state combinations.
580
581To write all this test code in C would be very tedious. Suppose instead
582that the testbed code adds a single new C function, to submit an
583arbitrary request and return the response, and then uses Guile to export
584this function as a Scheme procedure. The rest of the testbed can then
585be written in Scheme, and so benefits from all the advantages of
586programming in Scheme that were described in the previous section.
587
588(In this particular example, there is an additional benefit of writing
589most of the testbed in Scheme. A common problem for white box testing
590is that mistakes and mistaken assumptions in the application under test
591can easily be reproduced in the testbed code. It is more difficult to
592copy mistakes like this when the testbed is written in a different
593language from the application.)
594
595
596@node Programming Options
597@section A Choice of Programming Options
598
599The preceding arguments and example point to a model of Guile
600programming that is applicable in many cases. According to this model,
601Guile programming involves a balance between C and Scheme programming,
602with the aim being to extract the greatest possible Scheme level benefit
603from the least amount of C level work.
604
605The C level work required in this model usually consists of packaging
606and exporting functions and application objects such that they can be
607seen and manipulated on the Scheme level. To help with this, Guile's C
608language interface includes utility features that aim to make this kind
609of integration very easy for the application developer. These features
610are documented later in this part of the manual: see REFFIXME.
611
612This model, though, is really just one of a range of possible
613programming options. If all of the functionality that you need is
614available from Scheme, you could choose instead to write your whole
615application in Scheme (or one of the other high level languages that
616Guile supports through translation), and simply use Guile as an
617interpreter for Scheme. (In the future, we hope that Guile will also be
618able to compile Scheme code, so lessening the performance gap between C
619and Scheme code.) Or, at the other end of the C--Scheme scale, you
620could write the majority of your application in C, and only call out to
621Guile occasionally for specific actions such as reading a configuration
622file or executing a user-specified extension. The choices boil down to
623two basic questions:
9401323e
NJ
624
625@itemize @bullet
626@item
627Which parts of the application do you write in C, and which in Scheme
628(or another high level translated language)?
629
630@item
631How do you design the interface between the C and Scheme parts of your
632application?
633@end itemize
634
635These are of course design questions, and the right design for any given
636application will always depend upon the particular requirements that you
226297eb
NJ
637are trying to meet. In the context of Guile, however, there are some
638generally applicable considerations that can help you when designing
639your answers.
9401323e
NJ
640
641@menu
642* Available Functionality:: What functionality is already available?
643* Basic Constraints:: Functional and performance constraints.
644* Style Choices:: Your preferred programming style.
645* Program Control:: What controls program execution?
9401323e
NJ
646@end menu
647
648
649@node Available Functionality
226297eb 650@subsection What Functionality is Already Available?
9401323e
NJ
651
652Suppose, for the sake of argument, that you would prefer to write your
653whole application in Scheme. Then the API available to you consists of:
654
655@itemize @bullet
656@item
657standard Scheme
658
659@item
660plus the extensions to standard Scheme provided by
661Guile in its core distribution
662
663@item
664plus any additional functionality that you or others have packaged so
665that it can be loaded as a Guile Scheme module.
666@end itemize
667
668A module in the last category can either be a pure Scheme module --- in
669other words a collection of utility procedures coded in Scheme --- or a
670module that provides a Scheme interface to an extension library coded in
671C --- in other words a nice package where someone else has done the work
672of wrapping up some useful C code for you. The set of available modules
673is growing quickly and already includes such useful examples as
674@code{(gtk gtk)}, which makes Gtk+ drawing functions available in
675Scheme, and @code{(database postgres)}, which provides SQL access to a
676Postgres database.
677
a7a7bb95
NJ
678Given the growing collection of pre-existing modules, it is quite
679feasible that your application could be implemented by combining a
680selection of these modules together with new application code written in
681Scheme.
682
683If this approach is not enough, because the functionality that your
226297eb
NJ
684application needs is not already available in this form, and it is
685impossible to write the new functionality in Scheme, you will need to
686write some C code. If the required function is already available in C
687(e.g. in a library), all you need is a little glue to connect it to the
688world of Guile. If not, you need both to write the basic code and to
689plumb it into Guile.
9401323e
NJ
690
691In either case, two general considerations are important. Firstly, what
692is the interface by which the functionality is presented to the Scheme
693world? Does the interface consist only of function calls (for example,
694a simple drawing interface), or does it need to include @dfn{objects} of
695some kind that can be passed between C and Scheme and manipulated by
696both worlds. Secondly, how does the lifetime and memory management of
697objects in the C code relate to the garbage collection governed approach
698of Scheme objects? In the case where the basic C code is not already
699written, most of the difficulties of memory management can be avoided by
700using Guile's C interface features from the start.
701
226297eb
NJ
702For the full documentation on writing C code for Guile and connecting
703existing C code to the Guile world, see REFFIXME.
9401323e
NJ
704
705
706@node Basic Constraints
226297eb 707@subsection Functional and Performance Constraints
9401323e
NJ
708
709
710@node Style Choices
226297eb 711@subsection Your Preferred Programming Style
9401323e
NJ
712
713
714@node Program Control
226297eb 715@subsection What Controls Program Execution?
9401323e 716
9401323e
NJ
717
718@node User Programming
719@section How About Application Users?
720
721So far we have considered what Guile programming means for an
722application developer. But what if you are instead @emph{using} an
723existing Guile-based application, and want to know what your
724options are for programming and extending this application?
725
726The answer to this question varies from one application to another,
727because the options available depend inevitably on whether the
728application developer has provided any hooks for you to hang your own
729code on and, if there are such hooks, what they allow you to
730do.@footnote{Of course, in the world of free software, you always have
731the freedom to modify the application's source code to your own
732requirements. Here we are concerned with the extension options that the
733application has provided for without your needing to modify its source
734code.} For example@dots{}
735
736@itemize @bullet
737@item
738If the application permits you to load and execute any Guile code, the
739world is your oyster. You can extend the application in any way that
740you choose.
741
742@item
743A more cautious application might allow you to load and execute Guile
744code, but only in a @dfn{safe} environment, where the interface
745available is restricted by the application from the standard Guile API.
746
747@item
748Or a really fearful application might not provide a hook to really
749execute user code at all, but just use Scheme syntax as a convenient way
750for users to specify application data or configuration options.
751@end itemize
752
753In the last two cases, what you can do is, by definition, restricted by
754the application, and you should refer to the application's own manual to
755find out your options.
756
757The most well known example of the first case is Emacs, with its
758extension language Emacs Lisp: as well as being a text editor, Emacs
759supports the loading and execution of arbitrary Emacs Lisp code. The
760result of such openness has been dramatic: Emacs now benefits from
761user-contributed Emacs Lisp libraries that extend the basic editing
762function to do everything from reading news to psychoanalysis and
763playing adventure games. The only limitation is that extensions are
764restricted to the functionality provided by Emacs's built-in set of
765primitive operations. For example, you can interact and display data by
85a9b4ed 766manipulating the contents of an Emacs buffer, but you can't pop-up and
9401323e
NJ
767draw a window with a layout that is totally different to the Emacs
768standard.
769
770This situation with a Guile application that supports the loading of
771arbitrary user code is similar, except perhaps even more so, because
772Guile also supports the loading of extension libraries written in C.
773This last point enables user code to add new primitive operations to
774Guile, and so to bypass the limitation present in Emacs Lisp.
775
776At this point, the distinction between an application developer and an
777application user becomes rather blurred. Instead of seeing yourself as
778a user extending an application, you could equally well say that you are
779developing a new application of your own using some of the primitive
780functionality provided by the original application. As such, all the
781discussions of the preceding sections of this chapter are relevant to
782how you can proceed with developing your extension.