hooks.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* Copyright 1995-1996,1998-2001,2003,2006,2008-2009,2011,2018
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <stdio.h>
  19. #include "boolean.h"
  20. #include "eval.h"
  21. #include "gsubr.h"
  22. #include "list.h"
  23. #include "numbers.h"
  24. #include "pairs.h"
  25. #include "ports.h"
  26. #include "procprop.h"
  27. #include "smob.h"
  28. #include "strings.h"
  29. #include "hooks.h"
  30. /* Scheme level hooks
  31. *
  32. * A hook is basically a list of procedures to be called at well defined
  33. * points in time.
  34. *
  35. * Hook arity is not a full member of this type and therefore lacks an
  36. * accessor. It exists to aid debugging and is not intended to be used in
  37. * programs.
  38. */
  39. scm_t_bits scm_tc16_hook;
  40. static int
  41. hook_print (SCM hook, SCM port, scm_print_state *pstate)
  42. {
  43. SCM ls, name;
  44. scm_puts ("#<hook ", port);
  45. scm_intprint (SCM_HOOK_ARITY (hook), 10, port);
  46. scm_putc (' ', port);
  47. scm_uintprint (SCM_UNPACK (hook), 16, port);
  48. ls = SCM_HOOK_PROCEDURES (hook);
  49. while (scm_is_pair (ls))
  50. {
  51. scm_putc (' ', port);
  52. name = scm_procedure_name (SCM_CAR (ls));
  53. if (scm_is_true (name))
  54. scm_iprin1 (name, port, pstate);
  55. else
  56. scm_putc ('?', port);
  57. ls = SCM_CDR (ls);
  58. }
  59. scm_putc ('>', port);
  60. return 1;
  61. }
  62. SCM_DEFINE (scm_make_hook, "make-hook", 0, 1, 0,
  63. (SCM n_args),
  64. "Create a hook for storing procedure of arity @var{n_args}.\n"
  65. "@var{n_args} defaults to zero. The returned value is a hook\n"
  66. "object to be used with the other hook procedures.")
  67. #define FUNC_NAME s_scm_make_hook
  68. {
  69. unsigned int n;
  70. if (SCM_UNBNDP (n_args))
  71. n = 0;
  72. else
  73. n = scm_to_unsigned_integer (n_args, 0, 16);
  74. SCM_RETURN_NEWSMOB (scm_tc16_hook + (n << 16), SCM_UNPACK (SCM_EOL));
  75. }
  76. #undef FUNC_NAME
  77. SCM_DEFINE (scm_hook_p, "hook?", 1, 0, 0,
  78. (SCM x),
  79. "Return @code{#t} if @var{x} is a hook, @code{#f} otherwise.")
  80. #define FUNC_NAME s_scm_hook_p
  81. {
  82. return scm_from_bool (SCM_HOOKP (x));
  83. }
  84. #undef FUNC_NAME
  85. SCM_DEFINE (scm_hook_empty_p, "hook-empty?", 1, 0, 0,
  86. (SCM hook),
  87. "Return @code{#t} if @var{hook} is an empty hook, @code{#f}\n"
  88. "otherwise.")
  89. #define FUNC_NAME s_scm_hook_empty_p
  90. {
  91. SCM_VALIDATE_HOOK (1, hook);
  92. return scm_from_bool (scm_is_null (SCM_HOOK_PROCEDURES (hook)));
  93. }
  94. #undef FUNC_NAME
  95. SCM_DEFINE (scm_add_hook_x, "add-hook!", 2, 1, 0,
  96. (SCM hook, SCM proc, SCM append_p),
  97. "Add the procedure @var{proc} to the hook @var{hook}. The\n"
  98. "procedure is added to the end if @var{append_p} is true,\n"
  99. "otherwise it is added to the front. The return value of this\n"
  100. "procedure is not specified.")
  101. #define FUNC_NAME s_scm_add_hook_x
  102. {
  103. SCM rest;
  104. int n_args, p_req, p_opt, p_rest;
  105. SCM_VALIDATE_HOOK (1, hook);
  106. SCM_ASSERT (scm_i_procedure_arity (proc, &p_req, &p_opt, &p_rest),
  107. proc, SCM_ARG2, FUNC_NAME);
  108. n_args = SCM_HOOK_ARITY (hook);
  109. if (p_req > n_args || (!p_rest && p_req + p_opt < n_args))
  110. scm_wrong_type_arg (FUNC_NAME, 2, proc);
  111. rest = scm_delq_x (proc, SCM_HOOK_PROCEDURES (hook));
  112. SCM_SET_HOOK_PROCEDURES (hook,
  113. (!SCM_UNBNDP (append_p) && scm_is_true (append_p)
  114. ? scm_append_x (scm_list_2 (rest, scm_list_1 (proc)))
  115. : scm_cons (proc, rest)));
  116. return SCM_UNSPECIFIED;
  117. }
  118. #undef FUNC_NAME
  119. SCM_DEFINE (scm_remove_hook_x, "remove-hook!", 2, 0, 0,
  120. (SCM hook, SCM proc),
  121. "Remove the procedure @var{proc} from the hook @var{hook}. The\n"
  122. "return value of this procedure is not specified.")
  123. #define FUNC_NAME s_scm_remove_hook_x
  124. {
  125. SCM_VALIDATE_HOOK (1, hook);
  126. SCM_SET_HOOK_PROCEDURES (hook,
  127. scm_delq_x (proc, SCM_HOOK_PROCEDURES (hook)));
  128. return SCM_UNSPECIFIED;
  129. }
  130. #undef FUNC_NAME
  131. SCM_DEFINE (scm_reset_hook_x, "reset-hook!", 1, 0, 0,
  132. (SCM hook),
  133. "Remove all procedures from the hook @var{hook}. The return\n"
  134. "value of this procedure is not specified.")
  135. #define FUNC_NAME s_scm_reset_hook_x
  136. {
  137. SCM_VALIDATE_HOOK (1, hook);
  138. SCM_SET_HOOK_PROCEDURES (hook, SCM_EOL);
  139. return SCM_UNSPECIFIED;
  140. }
  141. #undef FUNC_NAME
  142. SCM_DEFINE (scm_run_hook, "run-hook", 1, 0, 1,
  143. (SCM hook, SCM args),
  144. "Apply all procedures from the hook @var{hook} to the arguments\n"
  145. "@var{args}. The order of the procedure application is first to\n"
  146. "last. The return value of this procedure is not specified.")
  147. #define FUNC_NAME s_scm_run_hook
  148. {
  149. SCM_VALIDATE_HOOK (1, hook);
  150. if (scm_ilength (args) != SCM_HOOK_ARITY (hook))
  151. SCM_MISC_ERROR ("Hook ~S requires ~A arguments",
  152. scm_list_2 (hook, scm_from_int (SCM_HOOK_ARITY (hook))));
  153. scm_c_run_hook (hook, args);
  154. return SCM_UNSPECIFIED;
  155. }
  156. #undef FUNC_NAME
  157. void
  158. scm_c_run_hook (SCM hook, SCM args)
  159. {
  160. SCM procs = SCM_HOOK_PROCEDURES (hook);
  161. while (scm_is_pair (procs))
  162. {
  163. scm_apply_0 (SCM_CAR (procs), args);
  164. procs = SCM_CDR (procs);
  165. }
  166. }
  167. void
  168. scm_c_run_hookn (SCM hook, SCM *argv, size_t nargs)
  169. {
  170. SCM procs = SCM_HOOK_PROCEDURES (hook);
  171. while (scm_is_pair (procs))
  172. {
  173. scm_call_n (SCM_CAR (procs), argv, nargs);
  174. procs = SCM_CDR (procs);
  175. }
  176. }
  177. SCM_DEFINE (scm_hook_to_list, "hook->list", 1, 0, 0,
  178. (SCM hook),
  179. "Convert the procedure list of @var{hook} to a list.")
  180. #define FUNC_NAME s_scm_hook_to_list
  181. {
  182. SCM_VALIDATE_HOOK (1, hook);
  183. return scm_list_copy (SCM_HOOK_PROCEDURES (hook));
  184. }
  185. #undef FUNC_NAME
  186. void
  187. scm_init_hooks ()
  188. {
  189. scm_tc16_hook = scm_make_smob_type ("hook", 0);
  190. scm_set_smob_print (scm_tc16_hook, hook_print);
  191. #include "hooks.x"
  192. }