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