api-utility.texi 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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. @page
  7. @node Utility Functions
  8. @section General Utility Functions
  9. @c FIXME::martin: Review me!
  10. This chapter contains information about procedures which are not cleanly
  11. tied to a specific data type. Because of their wide range of
  12. applications, they are collected in a @dfn{utility} chapter.
  13. @menu
  14. * Equality:: When are two values `the same'?
  15. * Object Properties:: A modern interface to object properties.
  16. * Sorting:: Sort utility procedures.
  17. * Copying:: Copying deep structures.
  18. * General Conversion:: Converting objects to strings.
  19. * Hooks:: User-customizable event lists.
  20. @end menu
  21. @node Equality
  22. @subsection Equality
  23. @cindex sameness
  24. @cindex equality
  25. There are three kinds of core equality predicates in Scheme, described
  26. below. The same kinds of comparisons arise in other functions, like
  27. @code{memq} and friends (@pxref{List Searching}).
  28. For all three tests, objects of different types are never equal. So
  29. for instance a list and a vector are not @code{equal?}, even if their
  30. contents are the same. Exact and inexact numbers are considered
  31. different types too, and are hence not equal even if their values are
  32. the same.
  33. @code{eq?} tests just for the same object (essentially a pointer
  34. comparison). This is fast, and can be used when searching for a
  35. particular object, or when working with symbols or keywords (which are
  36. always unique objects).
  37. @code{eqv?} extends @code{eq?} to look at the value of numbers and
  38. characters. It can for instance be used somewhat like @code{=}
  39. (@pxref{Comparison}) but without an error if one operand isn't a
  40. number.
  41. @code{equal?} goes further, it looks (recursively) into the contents
  42. of lists, vectors, etc. This is good for instance on lists that have
  43. been read or calculated in various places and are the same, just not
  44. made up of the same pairs. Such lists look the same (when printed),
  45. and @code{equal?} will consider them the same.
  46. @sp 1
  47. @deffn {Scheme Procedure} eq? x y
  48. @deffnx {C Function} scm_eq_p (x, y)
  49. @rnindex eq?
  50. Return @code{#t} if @var{x} and @var{y} are the same object, except
  51. for numbers and characters. For example,
  52. @example
  53. (define x (vector 1 2 3))
  54. (define y (vector 1 2 3))
  55. (eq? x x) @result{} #t
  56. (eq? x y) @result{} #f
  57. @end example
  58. Numbers and characters are not equal to any other object, but the
  59. problem is they're not necessarily @code{eq?} to themselves either.
  60. This is even so when the number comes directly from a variable,
  61. @example
  62. (let ((n (+ 2 3)))
  63. (eq? n n)) @result{} *unspecified*
  64. @end example
  65. Generally @code{eqv?} below should be used when comparing numbers or
  66. characters. @code{=} (@pxref{Comparison}) or @code{char=?}
  67. (@pxref{Characters}) can be used too.
  68. It's worth noting that end-of-list @code{()}, @code{#t}, @code{#f}, a
  69. symbol of a given name, and a keyword of a given name, are unique
  70. objects. There's just one of each, so for instance no matter how
  71. @code{()} arises in a program, it's the same object and can be
  72. compared with @code{eq?},
  73. @example
  74. (define x (cdr '(123)))
  75. (define y (cdr '(456)))
  76. (eq? x y) @result{} #t
  77. (define x (string->symbol "foo"))
  78. (eq? x 'foo) @result{} #t
  79. @end example
  80. @end deffn
  81. @deftypefn {C Function} int scm_is_eq (SCM x, SCM y)
  82. Return @code{1} when @var{x} and @var{y} are equal in the sense of
  83. @code{eq?}, otherwise return @code{0}.
  84. @findex ==
  85. The @code{==} operator should not be used on @code{SCM} values, an
  86. @code{SCM} is a C type which cannot necessarily be compared using
  87. @code{==} (@pxref{The SCM Type}).
  88. @end deftypefn
  89. @sp 1
  90. @deffn {Scheme Procedure} eqv? x y
  91. @deffnx {C Function} scm_eqv_p (x, y)
  92. @rnindex eqv?
  93. Return @code{#t} if @var{x} and @var{y} are the same object, or for
  94. characters and numbers the same value.
  95. On objects except characters and numbers, @code{eqv?} is the same as
  96. @code{eq?} above, it's true if @var{x} and @var{y} are the same
  97. object.
  98. If @var{x} and @var{y} are numbers or characters, @code{eqv?} compares
  99. their type and value. An exact number is not @code{eqv?} to an
  100. inexact number (even if their value is the same).
  101. @example
  102. (eqv? 3 (+ 1 2)) @result{} #t
  103. (eqv? 1 1.0) @result{} #f
  104. @end example
  105. @end deffn
  106. @sp 1
  107. @deffn {Scheme Procedure} equal? x y
  108. @deffnx {C Function} scm_equal_p (x, y)
  109. @rnindex equal?
  110. Return @code{#t} if @var{x} and @var{y} are the same type, and their
  111. contents or value are equal.
  112. For a pair, string, vector, array or structure, @code{equal?} compares the
  113. contents, and does so using using the same @code{equal?} recursively,
  114. so a deep structure can be traversed.
  115. @example
  116. (equal? (list 1 2 3) (list 1 2 3)) @result{} #t
  117. (equal? (list 1 2 3) (vector 1 2 3)) @result{} #f
  118. @end example
  119. For other objects, @code{equal?} compares as per @code{eqv?} above,
  120. which means characters and numbers are compared by type and value (and
  121. like @code{eqv?}, exact and inexact numbers are not @code{equal?},
  122. even if their value is the same).
  123. @example
  124. (equal? 3 (+ 1 2)) @result{} #t
  125. (equal? 1 1.0) @result{} #f
  126. @end example
  127. Hash tables are currently only compared as per @code{eq?}, so two
  128. different tables are not @code{equal?}, even if their contents are the
  129. same.
  130. @code{equal?} does not support circular data structures, it may go
  131. into an infinite loop if asked to compare two circular lists or
  132. similar.
  133. New application-defined object types (@pxref{Defining New Types
  134. (Smobs)}) have an @code{equalp} handler which is called by
  135. @code{equal?}. This lets an application traverse the contents or
  136. control what is considered @code{equal?} for two objects of such a
  137. type. If there's no such handler, the default is to just compare as
  138. per @code{eq?}.
  139. @end deffn
  140. @node Object Properties
  141. @subsection Object Properties
  142. It's often useful to associate a piece of additional information with a
  143. Scheme object even though that object does not have a dedicated slot
  144. available in which the additional information could be stored. Object
  145. properties allow you to do just that.
  146. Guile's representation of an object property is a procedure-with-setter
  147. (@pxref{Procedures with Setters}) that can be used with the generalized
  148. form of @code{set!} (REFFIXME) to set and retrieve that property for any
  149. Scheme object. So, setting a property looks like this:
  150. @lisp
  151. (set! (my-property obj1) value-for-obj1)
  152. (set! (my-property obj2) value-for-obj2)
  153. @end lisp
  154. @noindent
  155. And retrieving values of the same property looks like this:
  156. @lisp
  157. (my-property obj1)
  158. @result{}
  159. value-for-obj1
  160. (my-property obj2)
  161. @result{}
  162. value-for-obj2
  163. @end lisp
  164. To create an object property in the first place, use the
  165. @code{make-object-property} procedure:
  166. @lisp
  167. (define my-property (make-object-property))
  168. @end lisp
  169. @deffn {Scheme Procedure} make-object-property
  170. Create and return an object property. An object property is a
  171. procedure-with-setter that can be called in two ways. @code{(set!
  172. (@var{property} @var{obj}) @var{val})} sets @var{obj}'s @var{property}
  173. to @var{val}. @code{(@var{property} @var{obj})} returns the current
  174. setting of @var{obj}'s @var{property}.
  175. @end deffn
  176. A single object property created by @code{make-object-property} can
  177. associate distinct property values with all Scheme values that are
  178. distinguishable by @code{eq?} (including, for example, integers).
  179. Internally, object properties are implemented using a weak key hash
  180. table. This means that, as long as a Scheme value with property values
  181. is protected from garbage collection, its property values are also
  182. protected. When the Scheme value is collected, its entry in the
  183. property table is removed and so the (ex-) property values are no longer
  184. protected by the table.
  185. @menu
  186. * Property Primitives:: Low level property implementation.
  187. * Old-fashioned Properties:: An older approach to properties.
  188. @end menu
  189. @node Property Primitives
  190. @subsubsection Low Level Property Implementation.
  191. @deffn {Scheme Procedure} primitive-make-property not-found-proc
  192. @deffnx {C Function} scm_primitive_make_property (not_found_proc)
  193. Create a @dfn{property token} that can be used with
  194. @code{primitive-property-ref} and @code{primitive-property-set!}.
  195. See @code{primitive-property-ref} for the significance of
  196. @var{not-found-proc}.
  197. @end deffn
  198. @deffn {Scheme Procedure} primitive-property-ref prop obj
  199. @deffnx {C Function} scm_primitive_property_ref (prop, obj)
  200. Return the property @var{prop} of @var{obj}.
  201. When no value has yet been associated with @var{prop} and @var{obj},
  202. the @var{not-found-proc} from @var{prop} is used. A call
  203. @code{(@var{not-found-proc} @var{prop} @var{obj})} is made and the
  204. result set as the property value. If @var{not-found-proc} is
  205. @code{#f} then @code{#f} is the property value.
  206. @end deffn
  207. @deffn {Scheme Procedure} primitive-property-set! prop obj val
  208. @deffnx {C Function} scm_primitive_property_set_x (prop, obj, val)
  209. Set the property @var{prop} of @var{obj} to @var{val}.
  210. @end deffn
  211. @deffn {Scheme Procedure} primitive-property-del! prop obj
  212. @deffnx {C Function} scm_primitive_property_del_x (prop, obj)
  213. Remove any value associated with @var{prop} and @var{obj}.
  214. @end deffn
  215. @node Old-fashioned Properties
  216. @subsubsection An Older Approach to Properties
  217. Traditionally, Lisp systems provide a different object property
  218. interface to that provided by @code{make-object-property}, in which the
  219. object property that is being set or retrieved is indicated by a symbol.
  220. Guile includes this older kind of interface as well, but it may well be
  221. removed in a future release, as it is less powerful than
  222. @code{make-object-property} and so increases the size of the Guile
  223. library for no benefit. (And it is trivial to write a compatibility
  224. layer in Scheme.)
  225. @deffn {Scheme Procedure} object-properties obj
  226. @deffnx {C Function} scm_object_properties (obj)
  227. Return @var{obj}'s property list.
  228. @end deffn
  229. @deffn {Scheme Procedure} set-object-properties! obj alist
  230. @deffnx {C Function} scm_set_object_properties_x (obj, alist)
  231. Set @var{obj}'s property list to @var{alist}.
  232. @end deffn
  233. @deffn {Scheme Procedure} object-property obj key
  234. @deffnx {C Function} scm_object_property (obj, key)
  235. Return the property of @var{obj} with name @var{key}.
  236. @end deffn
  237. @deffn {Scheme Procedure} set-object-property! obj key value
  238. @deffnx {C Function} scm_set_object_property_x (obj, key, value)
  239. In @var{obj}'s property list, set the property named @var{key}
  240. to @var{value}.
  241. @end deffn
  242. @node Sorting
  243. @subsection Sorting
  244. @c FIXME::martin: Review me!
  245. @cindex sorting
  246. @cindex sorting lists
  247. @cindex sorting vectors
  248. Sorting is very important in computer programs. Therefore, Guile comes
  249. with several sorting procedures built-in. As always, procedures with
  250. names ending in @code{!} are side-effecting, that means that they may
  251. modify their parameters in order to produce their results.
  252. The first group of procedures can be used to merge two lists (which must
  253. be already sorted on their own) and produce sorted lists containing
  254. all elements of the input lists.
  255. @deffn {Scheme Procedure} merge alist blist less
  256. @deffnx {C Function} scm_merge (alist, blist, less)
  257. Merge two already sorted lists into one.
  258. Given two lists @var{alist} and @var{blist}, such that
  259. @code{(sorted? alist less?)} and @code{(sorted? blist less?)},
  260. return a new list in which the elements of @var{alist} and
  261. @var{blist} have been stably interleaved so that
  262. @code{(sorted? (merge alist blist less?) less?)}.
  263. Note: this does _not_ accept vectors.
  264. @end deffn
  265. @deffn {Scheme Procedure} merge! alist blist less
  266. @deffnx {C Function} scm_merge_x (alist, blist, less)
  267. Takes two lists @var{alist} and @var{blist} such that
  268. @code{(sorted? alist less?)} and @code{(sorted? blist less?)} and
  269. returns a new list in which the elements of @var{alist} and
  270. @var{blist} have been stably interleaved so that
  271. @code{(sorted? (merge alist blist less?) less?)}.
  272. This is the destructive variant of @code{merge}
  273. Note: this does _not_ accept vectors.
  274. @end deffn
  275. The following procedures can operate on sequences which are either
  276. vectors or list. According to the given arguments, they return sorted
  277. vectors or lists, respectively. The first of the following procedures
  278. determines whether a sequence is already sorted, the other sort a given
  279. sequence. The variants with names starting with @code{stable-} are
  280. special in that they maintain a special property of the input sequences:
  281. If two or more elements are the same according to the comparison
  282. predicate, they are left in the same order as they appeared in the
  283. input.
  284. @deffn {Scheme Procedure} sorted? items less
  285. @deffnx {C Function} scm_sorted_p (items, less)
  286. Return @code{#t} iff @var{items} is a list or a vector such that
  287. for all 1 <= i <= m, the predicate @var{less} returns true when
  288. applied to all elements i - 1 and i
  289. @end deffn
  290. @deffn {Scheme Procedure} sort items less
  291. @deffnx {C Function} scm_sort (items, less)
  292. Sort the sequence @var{items}, which may be a list or a
  293. vector. @var{less} is used for comparing the sequence
  294. elements. This is not a stable sort.
  295. @end deffn
  296. @deffn {Scheme Procedure} sort! items less
  297. @deffnx {C Function} scm_sort_x (items, less)
  298. Sort the sequence @var{items}, which may be a list or a
  299. vector. @var{less} is used for comparing the sequence
  300. elements. The sorting is destructive, that means that the
  301. input sequence is modified to produce the sorted result.
  302. This is not a stable sort.
  303. @end deffn
  304. @deffn {Scheme Procedure} stable-sort items less
  305. @deffnx {C Function} scm_stable_sort (items, less)
  306. Sort the sequence @var{items}, which may be a list or a
  307. vector. @var{less} is used for comparing the sequence elements.
  308. This is a stable sort.
  309. @end deffn
  310. @deffn {Scheme Procedure} stable-sort! items less
  311. @deffnx {C Function} scm_stable_sort_x (items, less)
  312. Sort the sequence @var{items}, which may be a list or a
  313. vector. @var{less} is used for comparing the sequence elements.
  314. The sorting is destructive, that means that the input sequence
  315. is modified to produce the sorted result.
  316. This is a stable sort.
  317. @end deffn
  318. The procedures in the last group only accept lists or vectors as input,
  319. as their names indicate.
  320. @deffn {Scheme Procedure} sort-list items less
  321. @deffnx {C Function} scm_sort_list (items, less)
  322. Sort the list @var{items}, using @var{less} for comparing the
  323. list elements. This is a stable sort.
  324. @end deffn
  325. @deffn {Scheme Procedure} sort-list! items less
  326. @deffnx {C Function} scm_sort_list_x (items, less)
  327. Sort the list @var{items}, using @var{less} for comparing the
  328. list elements. The sorting is destructive, that means that the
  329. input list is modified to produce the sorted result.
  330. This is a stable sort.
  331. @end deffn
  332. @deffn {Scheme Procedure} restricted-vector-sort! vec less startpos endpos
  333. @deffnx {C Function} scm_restricted_vector_sort_x (vec, less, startpos, endpos)
  334. Sort the vector @var{vec}, using @var{less} for comparing
  335. the vector elements. @var{startpos} (inclusively) and
  336. @var{endpos} (exclusively) delimit
  337. the range of the vector which gets sorted. The return value
  338. is not specified.
  339. @end deffn
  340. @node Copying
  341. @subsection Copying Deep Structures
  342. @c FIXME::martin: Review me!
  343. The procedures for copying lists (@pxref{Lists}) only produce a flat
  344. copy of the input list, and currently Guile does not even contain
  345. procedures for copying vectors. @code{copy-tree} can be used for these
  346. application, as it does not only copy the spine of a list, but also
  347. copies any pairs in the cars of the input lists.
  348. @deffn {Scheme Procedure} copy-tree obj
  349. @deffnx {C Function} scm_copy_tree (obj)
  350. Recursively copy the data tree that is bound to @var{obj}, and return a
  351. the new data structure. @code{copy-tree} recurses down the
  352. contents of both pairs and vectors (since both cons cells and vector
  353. cells may point to arbitrary objects), and stops recursing when it hits
  354. any other object.
  355. @end deffn
  356. @node General Conversion
  357. @subsection General String Conversion
  358. @c FIXME::martin: Review me!
  359. When debugging Scheme programs, but also for providing a human-friendly
  360. interface, a procedure for converting any Scheme object into string
  361. format is very useful. Conversion from/to strings can of course be done
  362. with specialized procedures when the data type of the object to convert
  363. is known, but with this procedure, it is often more comfortable.
  364. @code{object->string} converts an object by using a print procedure for
  365. writing to a string port, and then returning the resulting string.
  366. Converting an object back from the string is only possible if the object
  367. type has a read syntax and the read syntax is preserved by the printing
  368. procedure.
  369. @deffn {Scheme Procedure} object->string obj [printer]
  370. @deffnx {C Function} scm_object_to_string (obj, printer)
  371. Return a Scheme string obtained by printing @var{obj}.
  372. Printing function can be specified by the optional second
  373. argument @var{printer} (default: @code{write}).
  374. @end deffn
  375. @node Hooks
  376. @subsection Hooks
  377. @tpindex Hooks
  378. A hook is a list of procedures to be called at well defined points in
  379. time. Typically, an application provides a hook @var{h} and promises
  380. its users that it will call all of the procedures in @var{h} at a
  381. defined point in the application's processing. By adding its own
  382. procedure to @var{h}, an application user can tap into or even influence
  383. the progress of the application.
  384. Guile itself provides several such hooks for debugging and customization
  385. purposes: these are listed in a subsection below.
  386. When an application first creates a hook, it needs to know how many
  387. arguments will be passed to the hook's procedures when the hook is run.
  388. The chosen number of arguments (which may be none) is declared when the
  389. hook is created, and all the procedures that are added to that hook must
  390. be capable of accepting that number of arguments.
  391. A hook is created using @code{make-hook}. A procedure can be added to
  392. or removed from a hook using @code{add-hook!} or @code{remove-hook!},
  393. and all of a hook's procedures can be removed together using
  394. @code{reset-hook!}. When an application wants to run a hook, it does so
  395. using @code{run-hook}.
  396. @menu
  397. * Hook Example:: Hook usage by example.
  398. * Hook Reference:: Reference of all hook procedures.
  399. * C Hooks:: Hooks for use from C code.
  400. * GC Hooks:: Garbage collection hooks.
  401. * REPL Hooks:: Hooks into the Guile REPL.
  402. @end menu
  403. @node Hook Example
  404. @subsubsection Hook Usage by Example
  405. Hook usage is shown by some examples in this section. First, we will
  406. define a hook of arity 2 --- that is, the procedures stored in the hook
  407. will have to accept two arguments.
  408. @lisp
  409. (define hook (make-hook 2))
  410. hook
  411. @result{} #<hook 2 40286c90>
  412. @end lisp
  413. Now we are ready to add some procedures to the newly created hook with
  414. @code{add-hook!}. In the following example, two procedures are added,
  415. which print different messages and do different things with their
  416. arguments.
  417. @lisp
  418. (add-hook! hook (lambda (x y)
  419. (display "Foo: ")
  420. (display (+ x y))
  421. (newline)))
  422. (add-hook! hook (lambda (x y)
  423. (display "Bar: ")
  424. (display (* x y))
  425. (newline)))
  426. @end lisp
  427. Once the procedures have been added, we can invoke the hook using
  428. @code{run-hook}.
  429. @lisp
  430. (run-hook hook 3 4)
  431. @print{} Bar: 12
  432. @print{} Foo: 7
  433. @end lisp
  434. Note that the procedures are called in the reverse of the order with
  435. which they were added. This is because the default behaviour of
  436. @code{add-hook!} is to add its procedure to the @emph{front} of the
  437. hook's procedure list. You can force @code{add-hook!} to add its
  438. procedure to the @emph{end} of the list instead by providing a third
  439. @code{#t} argument on the second call to @code{add-hook!}.
  440. @lisp
  441. (add-hook! hook (lambda (x y)
  442. (display "Foo: ")
  443. (display (+ x y))
  444. (newline)))
  445. (add-hook! hook (lambda (x y)
  446. (display "Bar: ")
  447. (display (* x y))
  448. (newline))
  449. #t) ; @r{<- Change here!}
  450. (run-hook hook 3 4)
  451. @print{} Foo: 7
  452. @print{} Bar: 12
  453. @end lisp
  454. @node Hook Reference
  455. @subsubsection Hook Reference
  456. When you create a hook with @code{make-hook}, you must specify the arity
  457. of the procedures which can be added to the hook. If the arity is not
  458. given explicitly as an argument to @code{make-hook}, it defaults to
  459. zero. All procedures of a given hook must have the same arity, and when
  460. the procedures are invoked using @code{run-hook}, the number of
  461. arguments passed must match the arity specified at hook creation time.
  462. The order in which procedures are added to a hook matters. If the third
  463. parameter to @code{add-hook!} is omitted or is equal to @code{#f}, the
  464. procedure is added in front of the procedures which might already be on
  465. that hook, otherwise the procedure is added at the end. The procedures
  466. are always called from the front to the end of the list when they are
  467. invoked via @code{run-hook}.
  468. The ordering of the list of procedures returned by @code{hook->list}
  469. matches the order in which those procedures would be called if the hook
  470. was run using @code{run-hook}.
  471. Note that the C functions in the following entries are for handling
  472. @dfn{Scheme-level} hooks in C. There are also @dfn{C-level} hooks which
  473. have their own interface (@pxref{C Hooks}).
  474. @deffn {Scheme Procedure} make-hook [n_args]
  475. @deffnx {C Function} scm_make_hook (n_args)
  476. Create a hook for storing procedure of arity @var{n_args}.
  477. @var{n_args} defaults to zero. The returned value is a hook
  478. object to be used with the other hook procedures.
  479. @end deffn
  480. @deffn {Scheme Procedure} hook? x
  481. @deffnx {C Function} scm_hook_p (x)
  482. Return @code{#t} if @var{x} is a hook, @code{#f} otherwise.
  483. @end deffn
  484. @deffn {Scheme Procedure} hook-empty? hook
  485. @deffnx {C Function} scm_hook_empty_p (hook)
  486. Return @code{#t} if @var{hook} is an empty hook, @code{#f}
  487. otherwise.
  488. @end deffn
  489. @deffn {Scheme Procedure} add-hook! hook proc [append_p]
  490. @deffnx {C Function} scm_add_hook_x (hook, proc, append_p)
  491. Add the procedure @var{proc} to the hook @var{hook}. The
  492. procedure is added to the end if @var{append_p} is true,
  493. otherwise it is added to the front. The return value of this
  494. procedure is not specified.
  495. @end deffn
  496. @deffn {Scheme Procedure} remove-hook! hook proc
  497. @deffnx {C Function} scm_remove_hook_x (hook, proc)
  498. Remove the procedure @var{proc} from the hook @var{hook}. The
  499. return value of this procedure is not specified.
  500. @end deffn
  501. @deffn {Scheme Procedure} reset-hook! hook
  502. @deffnx {C Function} scm_reset_hook_x (hook)
  503. Remove all procedures from the hook @var{hook}. The return
  504. value of this procedure is not specified.
  505. @end deffn
  506. @deffn {Scheme Procedure} hook->list hook
  507. @deffnx {C Function} scm_hook_to_list (hook)
  508. Convert the procedure list of @var{hook} to a list.
  509. @end deffn
  510. @deffn {Scheme Procedure} run-hook hook . args
  511. @deffnx {C Function} scm_run_hook (hook, args)
  512. Apply all procedures from the hook @var{hook} to the arguments
  513. @var{args}. The order of the procedure application is first to
  514. last. The return value of this procedure is not specified.
  515. @end deffn
  516. If, in C code, you are certain that you have a hook object and well
  517. formed argument list for that hook, you can also use
  518. @code{scm_c_run_hook}, which is identical to @code{scm_run_hook} but
  519. does no type checking.
  520. @deftypefn {C Function} void scm_c_run_hook (SCM hook, SCM args)
  521. The same as @code{scm_run_hook} but without any type checking to confirm
  522. that @var{hook} is actually a hook object and that @var{args} is a
  523. well-formed list matching the arity of the hook.
  524. @end deftypefn
  525. For C code, @code{SCM_HOOKP} is a faster alternative to
  526. @code{scm_hook_p}:
  527. @deftypefn {C Macro} int SCM_HOOKP (x)
  528. Return 1 if @var{x} is a Scheme-level hook, 0 otherwise.
  529. @end deftypefn
  530. @subsubheading Handling Scheme-level hooks from C code
  531. Here is an example of how to handle Scheme-level hooks from C code using
  532. the above functions.
  533. @example
  534. if (scm_is_true (scm_hook_p (obj)))
  535. /* handle Scheme-level hook using C functions */
  536. scm_reset_hook_x (obj);
  537. else
  538. /* do something else (obj is not a hook) */
  539. @end example
  540. @node C Hooks
  541. @subsubsection Hooks For C Code.
  542. The hooks already described are intended to be populated by Scheme-level
  543. procedures. In addition to this, the Guile library provides an
  544. independent set of interfaces for the creation and manipulation of hooks
  545. that are designed to be populated by functions implemented in C.
  546. The original motivation here was to provide a kind of hook that could
  547. safely be invoked at various points during garbage collection.
  548. Scheme-level hooks are unsuitable for this purpose as running them could
  549. itself require memory allocation, which would then invoke garbage
  550. collection recursively @dots{} However, it is also the case that these
  551. hooks are easier to work with than the Scheme-level ones if you only
  552. want to register C functions with them. So if that is mainly what your
  553. code needs to do, you may prefer to use this interface.
  554. To create a C hook, you should allocate storage for a structure of type
  555. @code{scm_t_c_hook} and then initialize it using @code{scm_c_hook_init}.
  556. @deftp {C Type} scm_t_c_hook
  557. Data type for a C hook. The internals of this type should be treated as
  558. opaque.
  559. @end deftp
  560. @deftp {C Enum} scm_t_c_hook_type
  561. Enumeration of possible hook types, which are:
  562. @table @code
  563. @item SCM_C_HOOK_NORMAL
  564. @vindex SCM_C_HOOK_NORMAL
  565. Type of hook for which all the registered functions will always be called.
  566. @item SCM_C_HOOK_OR
  567. @vindex SCM_C_HOOK_OR
  568. Type of hook for which the sequence of registered functions will be
  569. called only until one of them returns C true (a non-NULL pointer).
  570. @item SCM_C_HOOK_AND
  571. @vindex SCM_C_HOOK_AND
  572. Type of hook for which the sequence of registered functions will be
  573. called only until one of them returns C false (a NULL pointer).
  574. @end table
  575. @end deftp
  576. @deftypefn {C Function} void scm_c_hook_init (scm_t_c_hook *hook, void *hook_data, scm_t_c_hook_type type)
  577. Initialize the C hook at memory pointed to by @var{hook}. @var{type}
  578. should be one of the values of the @code{scm_t_c_hook_type} enumeration,
  579. and controls how the hook functions will be called. @var{hook_data} is
  580. a closure parameter that will be passed to all registered hook functions
  581. when they are called.
  582. @end deftypefn
  583. To add or remove a C function from a C hook, use @code{scm_c_hook_add}
  584. or @code{scm_c_hook_remove}. A hook function must expect three
  585. @code{void *} parameters which are, respectively:
  586. @table @var
  587. @item hook_data
  588. The hook closure data that was specified at the time the hook was
  589. initialized by @code{scm_c_hook_init}.
  590. @item func_data
  591. The function closure data that was specified at the time that that
  592. function was registered with the hook by @code{scm_c_hook_add}.
  593. @item data
  594. The call closure data specified by the @code{scm_c_hook_run} call that
  595. runs the hook.
  596. @end table
  597. @deftp {C Type} scm_t_c_hook_function
  598. Function type for a C hook function: takes three @code{void *}
  599. parameters and returns a @code{void *} result.
  600. @end deftp
  601. @deftypefn {C Function} void scm_c_hook_add (scm_t_c_hook *hook, scm_t_c_hook_function func, void *func_data, int appendp)
  602. Add function @var{func}, with function closure data @var{func_data}, to
  603. the C hook @var{hook}. The new function is appended to the hook's list
  604. of functions if @var{appendp} is non-zero, otherwise prepended.
  605. @end deftypefn
  606. @deftypefn {C Function} void scm_c_hook_remove (scm_t_c_hook *hook, scm_t_c_hook_function func, void *func_data)
  607. Remove function @var{func}, with function closure data @var{func_data},
  608. from the C hook @var{hook}. @code{scm_c_hook_remove} checks both
  609. @var{func} and @var{func_data} so as to allow for the same @var{func}
  610. being registered multiple times with different closure data.
  611. @end deftypefn
  612. Finally, to invoke a C hook, call the @code{scm_c_hook_run} function
  613. specifying the hook and the call closure data for this run:
  614. @deftypefn {C Function} {void *} scm_c_hook_run (scm_t_c_hook *hook, void *data)
  615. Run the C hook @var{hook} will call closure data @var{data}. Subject to
  616. the variations for hook types @code{SCM_C_HOOK_OR} and
  617. @code{SCM_C_HOOK_AND}, @code{scm_c_hook_run} calls @var{hook}'s
  618. registered functions in turn, passing them the hook's closure data, each
  619. function's closure data, and the call closure data.
  620. @code{scm_c_hook_run}'s return value is the return value of the last
  621. function to be called.
  622. @end deftypefn
  623. @node GC Hooks
  624. @subsubsection Hooks for Garbage Collection
  625. Whenever Guile performs a garbage collection, it calls the following
  626. hooks in the order shown.
  627. @defvr {C Hook} scm_before_gc_c_hook
  628. C hook called at the very start of a garbage collection, after setting
  629. @code{scm_gc_running_p} to 1, but before entering the GC critical
  630. section.
  631. If garbage collection is blocked because @code{scm_block_gc} is
  632. non-zero, GC exits early soon after calling this hook, and no further
  633. hooks will be called.
  634. @end defvr
  635. @defvr {C Hook} scm_before_mark_c_hook
  636. C hook called before beginning the mark phase of garbage collection,
  637. after the GC thread has entered a critical section.
  638. @end defvr
  639. @defvr {C Hook} scm_before_sweep_c_hook
  640. C hook called before beginning the sweep phase of garbage collection.
  641. This is the same as at the end of the mark phase, since nothing else
  642. happens between marking and sweeping.
  643. @end defvr
  644. @defvr {C Hook} scm_after_sweep_c_hook
  645. C hook called after the end of the sweep phase of garbage collection,
  646. but while the GC thread is still inside its critical section.
  647. @end defvr
  648. @defvr {C Hook} scm_after_gc_c_hook
  649. C hook called at the very end of a garbage collection, after the GC
  650. thread has left its critical section.
  651. @end defvr
  652. @defvr {Scheme Hook} after-gc-hook
  653. @vindex scm_after_gc_hook
  654. Scheme hook with arity 0. This hook is run asynchronously
  655. (@pxref{Asyncs}) soon after the GC has completed and any other events
  656. that were deferred during garbage collection have been processed. (Also
  657. accessible from C with the name @code{scm_after_gc_hook}.)
  658. @end defvr
  659. All the C hooks listed here have type @code{SCM_C_HOOK_NORMAL}, are
  660. initialized with hook closure data NULL, are are invoked by
  661. @code{scm_c_hook_run} with call closure data NULL.
  662. @cindex guardians, testing for GC'd objects
  663. The Scheme hook @code{after-gc-hook} is particularly useful in
  664. conjunction with guardians (@pxref{Guardians}). Typically, if you are
  665. using a guardian, you want to call the guardian after garbage collection
  666. to see if any of the objects added to the guardian have been collected.
  667. By adding a thunk that performs this call to @code{after-gc-hook}, you
  668. can ensure that your guardian is tested after every garbage collection
  669. cycle.
  670. @node REPL Hooks
  671. @subsubsection Hooks into the Guile REPL
  672. @c Local Variables:
  673. @c TeX-master: "guile.texi"
  674. @c End: