1 /* mnemon - A memory training library
3 * Copyright © 2006,2011 Carl Worth
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA."
30 #include <sys/types.h>
39 #include <readline/readline.h>
40 #include <readline/history.h>
42 #define ASSERT_NOT_REACHED \
44 static const int NOT_REACHED = 0; \
45 assert (NOT_REACHED); \
48 #define unused(foo) foo __attribute__((unused))
52 typedef struct _item {
66 CATEGORY_ORDER_RANDOM,
67 CATEGORY_ORDER_SEQUENTIAL
75 CHALLENGE_TYPE_TEXT_TO_SPEECH
84 /* Support sequential introduction of items from bin 0 */
85 category_order_t order;
86 /* Support categories where responses are timed (0.0 == disable). */
89 /* Support challenges of non-text types (image, audio, etc.) */
90 challenge_type_t challenge_type;
91 /* Whether to repeat afterwards (for a little extra reinforcement) */
102 fprintf (stderr, "Error: out of memory\n");
110 xrealloc (void *ptr, size_t size)
114 ret = realloc (ptr, size);
116 fprintf (stderr, "Error: out of memory\n");
124 xstrdup (const char *s)
130 fprintf (stderr, "Error: out of memory\n");
138 xstrndup (const char *s, size_t n)
142 ret = strndup (s, n);
144 fprintf (stderr, "Error: out of memory\n");
152 xasprintf (char **strp, const char *fmt, ...)
158 ret = vasprintf (strp, fmt, ap);
162 fprintf (stderr, "Error: out of memory\n");
168 item_init (item_t *item,
170 const char *challenge,
171 const char *response)
175 item->challenge = xmalloc (strlen (challenge) + 1 +
176 strlen (response) + 1);
177 item->response = item->challenge + strlen (challenge) + 1;
179 strcpy (item->challenge, challenge);
180 strcpy (item->response, response);
184 item_fini (item_t *item)
186 /* item->response shares allocation with item->challenge, so
187 * doesn't require a separate call to free */
188 free (item->challenge);
192 category_init (category_t *category,
195 category->name = xstrdup (name);
197 category->items_size = 0;
198 category->num_items = 0;
199 category->items = NULL;
200 category->order = CATEGORY_ORDER_RANDOM;
201 category->time_limit = 0.0;
202 category->bin_zero_head = 0;
203 category->challenge_type = CHALLENGE_TYPE_TEXT;
204 category->repeat = 0;
208 category_fini (category_t *category)
212 for (i = 0; i < category->num_items; i++)
213 item_fini (&category->items[i]);
215 free (category->items);
217 free (category->name);
221 category_grow (category_t *category)
223 if (category->items_size)
224 category->items_size *= 2;
226 category->items_size = 1;
228 category->items = xrealloc (category->items,
229 category->items_size * sizeof (item_t));
233 category_add_item (category_t *category,
235 const char *challenge,
236 const char *response)
240 if (category->num_items == category->items_size)
241 category_grow (category);
243 item = &category->items[category->num_items++];
245 item_init (item, score, challenge, response);
251 category_next_bin_zero_item (category_t *category)
253 int *i = &category->bin_zero_head;
255 for ( ; *i < category->num_items; *i = *i + 1)
256 if (category->items[*i].score == 0)
257 return &category->items[*i];
263 category_print (category_t *category,
269 fprintf (file, "order = %s\n\n",
270 category->order == CATEGORY_ORDER_RANDOM ? "random" : "sequential");
271 fprintf (file, "time = %f\n\n",
272 category->time_limit);
274 fprintf (file, "challenge = ");
275 switch (category->challenge_type) {
276 case CHALLENGE_TYPE_TEXT:
277 fprintf (file, "text");
279 case CHALLENGE_TYPE_IMAGE:
280 fprintf (file, "image");
282 case CHALLENGE_TYPE_AUDIO:
283 fprintf (file, "audio");
285 case CHALLENGE_TYPE_MIDI:
286 fprintf (file, "midi");
288 case CHALLENGE_TYPE_TEXT_TO_SPEECH:
289 fprintf (file, "text-to-speech");
292 fprintf (file, "\n\n");
294 fprintf (file, "repeat = %d\n\n", category->repeat);
296 for (i = 0; i < category->num_items; i++) {
297 item = &category->items[i];
299 fprintf (file, "\n");
300 fprintf (file, "%d\n%s\n%s\n",
308 bin_init (bin_t *bin,
319 bin_fini (bin_t *bin)
325 bin_grow (bin_t *bin)
328 bin->items_size *= 2;
332 bin->items = xrealloc (bin->items,
333 bin->items_size * sizeof (item_t*));
337 bin_add_item (bin_t *bin,
340 assert (item->score == bin->score);
342 if (bin->num_items == bin->items_size)
345 bin->items[bin->num_items++] = item;
349 bin_remove_item (bin_t *bin,
352 /* Replace the current item with the last item, (no need to shift
353 * any more than that since we don't care about the order of the
354 * items within a bin). */
357 bin->items[item_index] = bin->items[bin->num_items];
360 /* Find the index for an item within a bin.
362 * XXX: This is currently a linear search, so is a potential
363 * performance problem.
366 bin_item_index (bin_t *bin,
371 for (i = 0; i < bin->num_items; i++)
372 if (bin->items[i] == item)
378 typedef int (item_match_predicate_t) (void *closure, item_t *item);
380 /* Return the number of items in the bin from the given category (or
381 * from all categories if category == NULL) */
383 bin_num_items_matching (bin_t *bin,
384 item_match_predicate_t *predicate,
387 int i, num_items = 0;
389 if (predicate == NULL)
390 return bin->num_items;
392 for (i = 0; i < bin->num_items; i++)
393 if ((predicate) (closure, bin->items[i]))
400 mnemon_init (mnemon_t *mnemon)
404 home = getenv ("HOME");
408 xasprintf (&mnemon->dir_name, "%s/.mnemon", getenv ("HOME"));
410 mnemon->categories_size = 0;
411 mnemon->num_categories = 0;
412 mnemon->categories = NULL;
414 mnemon->bins_size = 0;
415 mnemon->num_bins = 0;
418 mnemon->to_introduce = 10;
419 mnemon->to_master = 10;
420 mnemon->unlearned = 0;
421 mnemon->mastered = -1;
425 mnemon_fini (mnemon_t *mnemon)
429 for (i = 0; i < mnemon->num_bins; i++)
430 bin_fini (&mnemon->bins[i]);
433 for (i = 0; i < mnemon->num_categories; i++)
434 category_fini (&mnemon->categories[i]);
435 free (mnemon->categories);
437 free (mnemon->dir_name);
441 mnemon_categories_grow (mnemon_t *mnemon)
443 if (mnemon->categories_size)
444 mnemon->categories_size *= 2;
446 mnemon->categories_size = 1;
448 mnemon->categories = xrealloc (mnemon->categories,
449 mnemon->categories_size * sizeof (category_t));
452 /* Get a category by name if it exists */
454 mnemon_get_category_if_exists (mnemon_t *mnemon,
459 for (i = 0; i < mnemon->num_categories; i++)
460 if (strcmp (mnemon->categories[i].name, name) == 0)
461 return &mnemon->categories[i];
466 /* Get a category by name, creating new one if necessary. */
468 mnemon_get_category (mnemon_t *mnemon,
471 category_t *category;
473 category = mnemon_get_category_if_exists (mnemon, name);
477 mnemon_categories_grow (mnemon);
479 category = &mnemon->categories[mnemon->num_categories++];
481 category_init (category, name);
487 mnemon_bins_grow (mnemon_t *mnemon)
489 if (mnemon->bins_size)
490 mnemon->bins_size *= 2;
492 mnemon->bins_size = 1;
494 mnemon->bins = xrealloc (mnemon->bins,
495 mnemon->bins_size * sizeof (bin_t));
499 mnemon_get_bin (mnemon_t *mnemon,
505 for (i = 0; i < mnemon->num_bins; i++)
506 if (mnemon->bins[i].score == score)
507 return &mnemon->bins[i];
508 else if (mnemon->bins[i].score > score)
511 if (mnemon->num_bins == mnemon->bins_size)
512 mnemon_bins_grow (mnemon);
514 bin = &mnemon->bins[i];
516 /* Make room to insert new bin at its sorted location. */
517 if (i < mnemon->num_bins)
518 memmove (bin + 1, bin, (mnemon->num_bins - i) * sizeof (bin_t));
521 bin_init (bin, score);
527 mnemon_remove_bin (mnemon_t *mnemon,
530 int i = bin - mnemon->bins;
534 memmove (bin, bin + 1, (mnemon->num_bins - i) * sizeof (bin_t));
541 int len = strlen (s);
544 if (s[len - 1] == '\n')
549 trim_space (char *string)
554 while (*s && isspace (*s))
559 s = string + strlen (string) - 1;
560 while (s > string && isspace (*s)) {
569 mnemon_load_category (mnemon_t *mnemon,
573 char *line = NULL, *end;
574 size_t line_size = 0;
578 category_t *category;
582 path = xmalloc (strlen (mnemon->dir_name) + 1 + strlen (name) + 1);
583 sprintf (path, "%s/%s", mnemon->dir_name, name);
585 file = fopen (path, "r");
587 fprintf (stderr, "Error: Failed to open %s: %s\n",
588 path, strerror (errno));
592 fstat (fileno(file), &st);
593 if (! S_ISREG(st.st_mode)) {
594 fprintf (stderr, "Error: File %s is not a regular file.\n", path);
598 category = mnemon_get_category (mnemon, name);
600 #define READ_LINE do { \
601 bytes_read = getline (&line, &line_size, file); \
602 if (bytes_read == -1) \
610 char *name, *equal, *value;
612 /* Ignore blank lines */
617 /* An initial digit means we hit an item. Trigger the
618 * spaghetti machine. */
619 if ((*line >= '0' && *line <= '9') || *line == '-')
622 equal = strchr (line, '=');
624 fprintf (stderr, "Malformed option, (expected name=value): \"%s\" at %s:%d\n",
625 line, path, line_count);
633 name = trim_space (name);
634 value = trim_space (value);
636 if (strcmp (name, "order") == 0) {
637 if (strcmp (value, "sequential") == 0) {
638 category->order = CATEGORY_ORDER_SEQUENTIAL;
639 } else if (strcmp (value, "random") == 0) {
640 category->order = CATEGORY_ORDER_RANDOM;
642 fprintf (stderr, "Unknown value for \"order\" option \"%s\" at %s:%d\n",
643 value, path, line_count);
646 } else if (strcmp (name, "time") == 0) {
649 limit = strtod (value, &end);
650 while (isspace (*end))
653 category->time_limit = limit;
655 fprintf (stderr, "Failed to parse time value: %s at %s:%d\n",
656 value, path, line_count);
659 } else if (strcmp (name, "challenge") == 0) {
660 if (strcmp (value, "text") == 0) {
661 category->challenge_type = CHALLENGE_TYPE_TEXT;
662 } else if (strcmp (value, "image") == 0) {
663 category->challenge_type = CHALLENGE_TYPE_IMAGE;
664 } else if (strcmp (value, "audio") == 0) {
665 category->challenge_type = CHALLENGE_TYPE_AUDIO;
666 } else if (strcmp (value, "midi") == 0) {
667 category->challenge_type = CHALLENGE_TYPE_MIDI;
668 } else if (strcmp (value, "text-to-speech") == 0) {
669 category->challenge_type = CHALLENGE_TYPE_TEXT_TO_SPEECH;
671 fprintf (stderr, "Unknown value for \"challenge\" option \"%s\" at %s:%d\n",
672 value, path, line_count);
675 } else if (strcmp (name, "repeat") == 0) {
676 if (strcmp (value, "0") == 0)
677 category->repeat = 0;
679 category->repeat = 1;
681 fprintf (stderr, "Unknown option %s at %s:%d\n",
682 name, path, line_count);
690 char *challenge, *response;
692 /* Ignore blank lines */
697 /* Read bin number */
699 score = strtol (line, &end, 10);
701 fprintf (stderr, "Failed to parse bin number from \"%s\" at %s:%d\n",
702 line, path, line_count);
708 challenge = strdup (line);
714 category_add_item (category, score, challenge, response);
724 /* Resize category items to fit exactly. */
725 category->items_size = category->num_items;
726 category->items = xrealloc (category->items, category->items_size * sizeof (item_t));
728 /* Now that the category is completely loaded, with stable
729 * pointers to every item, we can add each item to its appropriate
731 for (i = 0; i < category->num_items; i++) {
732 item_t *item = &category->items[i];
733 bin_t *bin = mnemon_get_bin (mnemon, item->score);
735 bin_add_item (bin, item);
740 mnemon_load (mnemon_t *mnemon)
743 struct dirent *dirent;
745 dir = opendir (mnemon->dir_name);
747 fprintf (stderr, "Error: Failed to open directory %s: %s\n",
748 mnemon->dir_name, strerror (errno));
753 dirent = readdir (dir);
757 if (dirent->d_type == DT_REG) {
758 /* Ignore files matching *~, (yes, this shouldn't be
759 * hard-coded in such an ad-hoc way, but there you go. */
760 if (dirent->d_name[strlen(dirent->d_name)-1] != '~')
761 mnemon_load_category (mnemon, dirent->d_name);
769 mnemon_save (mnemon_t *mnemon)
772 char *filename, *lock_filename;
774 category_t *category;
776 for (i = 0; i < mnemon->num_categories; i++) {
777 category = &mnemon->categories[i];
779 xasprintf (&filename, "%s/%s",
780 mnemon->dir_name, category->name);
781 xasprintf (&lock_filename, "%s/.#%s",
782 mnemon->dir_name, category->name);
784 file = fopen (lock_filename, "w");
786 fprintf (stderr, "Error: Failed to open %s for writing: %s\n",
787 lock_filename, strerror (errno));
791 category_print (category, file);
793 fsync (fileno (file));
796 err = rename (lock_filename, filename);
798 fprintf (stderr, "Error: Failed to rename %s to %s: %s\n",
799 lock_filename, filename, strerror (errno));
804 free (lock_filename);
808 /* Return a uniformly-distributed pseudo-random integer within the
811 * 0 <= result < num_values
814 rand_within (int num_values)
816 return (int) (num_values * (rand() / (RAND_MAX + 1.0)));
819 /* Return an exponentially-distributed pseudo-random integer within
822 * 0 <= result < num_values
824 * The distribution is such that each successively larger value will
825 * occur with a probability of half of the previous value.
828 rand_within_exponential (int num_values)
831 static uint32_t mask = 0;
835 /* Optimize the constant case. */
845 while (mask > RAND_MAX)
852 if (ones == num_values)
860 /* Find the category to which an item belongs. */
862 mnemon_item_category (mnemon_t *mnemon,
865 category_t *category;
868 for (i = 0; i < mnemon->num_categories; i++) {
869 category = &mnemon->categories[i];
870 item_index = item - category->items;
871 if (item_index >= 0 && item_index < category->num_items)
878 typedef struct _item_in_category_closure
881 category_t *category;
882 } item_in_category_closure_t;
885 mnemon_item_in_category (void *closure, item_t *item)
887 item_in_category_closure_t *iicc = closure;
888 mnemon_t *mnemon = iicc->mnemon;
889 category_t *category = iicc->category;
891 return (mnemon_item_category (mnemon, item) == category);
894 typedef struct _item_in_category_of_length_closure
897 category_t *category;
899 } item_in_category_of_length_closure_t;
902 mnemon_item_in_category_of_length (void *closure, item_t *item)
904 item_in_category_of_length_closure_t *iicolc = closure;
905 mnemon_t *mnemon = iicolc->mnemon;
906 category_t *category = iicolc->category;
907 unsigned int length = iicolc->length;
909 if (mnemon_item_category (mnemon, item) != category)
912 return strlen (item->challenge) == length;
916 mnemon_select_item (mnemon_t *mnemon,
919 category_t **category_ret)
921 int bin_index, item_index;
924 category_t *category;
926 bin_index = rand_within_exponential (mnemon->num_bins);
927 bin = &mnemon->bins[bin_index];
929 /* The most intuitive understanding of the to_introduce counter is
930 * that it's tracking never-before-learned items as they are
931 * pulled from the bin with score 0. But that bin can become
932 * empty. So the refined rule is that we decrement to_introduce
933 * whenever we pull from the lowest-indexed bin with a
934 * non-negative score. */
935 if (mnemon->to_introduce && bin->score >=0 &&
936 (bin_index == 0 || mnemon->bins[bin_index-1].score < 0))
938 mnemon->to_introduce--;
941 item_index = rand_within (bin->num_items);
943 item = bin->items[item_index];
944 category = mnemon_item_category (mnemon, item);
946 if (bin->score == 0) {
947 if (category->order == CATEGORY_ORDER_SEQUENTIAL) {
948 item = category_next_bin_zero_item (category);
950 item_index = bin_item_index (bin, item);
955 *item_index_ret = item_index;
956 *category_ret = category;
960 #define HISTOGRAM_ROW_FORMAT "%3d: %3d"
961 #define HISTOGRAM_BAR_WIDTH 63
964 print_histogram_bar (double size,
967 int units_per_cell = (int) ceil (max / HISTOGRAM_BAR_WIDTH);
968 static char const *boxes[8] = {
973 while (size > units_per_cell) {
975 size -= units_per_cell;
978 size /= units_per_cell;
982 else if (size > 6.5/8.0)
984 else if (size > 5.5/8.0)
986 else if (size > 4.5/8.0)
988 else if (size > 3.5/8.0)
990 else if (size > 2.5/8.0)
992 else if (size > 1.5/8.0)
994 else if (size > 0.5/8.0)
1001 mnemon_print_histogram (mnemon_t *mnemon,
1002 const char *category_name,
1005 int i, last_score, max;
1006 category_t *category = NULL;
1009 item_match_predicate_t *predicate = NULL;
1010 void *closure = NULL;
1011 item_in_category_closure_t item_in_category;
1012 item_in_category_of_length_closure_t item_in_category_of_length;
1014 if (mnemon->num_bins == 0)
1017 if (category_name) {
1018 category = mnemon_get_category_if_exists (mnemon, category_name);
1021 predicate = mnemon_item_in_category_of_length;
1022 item_in_category_of_length.mnemon = mnemon;
1023 item_in_category_of_length.category = category;
1024 item_in_category_of_length.length = length;
1025 closure = &item_in_category_of_length;
1027 predicate = mnemon_item_in_category;
1028 item_in_category.mnemon = mnemon;
1029 item_in_category.category = category;
1030 closure = &item_in_category;
1035 for (i = 0; i < mnemon->num_bins; i++) {
1036 num_items = bin_num_items_matching (&mnemon->bins[i],
1037 predicate, closure);
1038 if (i == 0 || num_items > max)
1042 for (i = 0; i < mnemon->num_bins; i++) {
1043 bin = &mnemon->bins[i];
1045 while (bin->score - last_score > 1)
1046 printf (HISTOGRAM_ROW_FORMAT "\n", ++last_score, 0);
1047 num_items = bin_num_items_matching (bin,
1048 predicate, closure);
1049 printf (HISTOGRAM_ROW_FORMAT " ", bin->score, num_items);
1050 print_histogram_bar (num_items, max);
1051 last_score = bin->score;
1056 mnemon_handle_command (mnemon_t *mnemon,
1057 const char *command)
1061 switch (command[0]) {
1062 /* 'h' for histogram */
1065 char *category = NULL;
1069 arg += strspn (arg, " \t");
1070 len = strcspn (arg, " \t");
1072 category = xstrndup (arg, len);
1074 arg += strspn (arg, " \t");
1076 length = atoi (arg);
1078 mnemon_print_histogram (mnemon, category, length);
1081 /* 'r' for repeat */
1084 /* Nothing necessary for repeating. */
1088 printf ("Unknown command: %s\n", command);
1094 mnemon_handle_response (mnemon_t *mnemon,
1098 const char *response,
1099 double response_time,
1104 correct = (strcmp (response, item->response) == 0);
1106 bin_remove_item (bin, item_index);
1108 /* If the bin is now empty, we must remove it. Also if we just
1109 * picked the last word we'll ever pick from the bin with
1110 * score 0, then we can remove that as well. */
1111 if (bin->num_items == 0 ||
1112 (bin->score == 0 && mnemon->to_introduce == 0))
1114 mnemon_remove_bin (mnemon, bin);
1118 (time_limit == 0.0 || response_time < time_limit))
1121 /* We reserve an item score of 0 for an item that has
1122 * never been asked. */
1123 if (item->score == 0) {
1125 mnemon->unlearned--;
1126 printf ("You got it!");
1127 } else if (item->score < 0) {
1128 printf ("Yes---just give me %d more.",
1130 } else if (item->score == 1) {
1131 printf ("On your first try, no less!");
1133 printf ("Masterful (%dx).", item->score);
1134 if (mnemon->to_master)
1135 mnemon->to_master--;
1139 printf (" %s is the correct answer.",
1142 printf ("Correct, but not quite quick enough (%0.2f seconds---needed %0.2f seconds)\n",
1143 response_time, time_limit);
1144 /* Penalize an incorrect response by forcing the score
1146 if (item->score >= 0) {
1147 if (item->score > 0)
1148 printf (" Oops, you knew that, right? (%dx)\n ",
1150 mnemon->unlearned++;
1151 /* We increase to_master here as an extra penalty. If the
1152 * user is forgetting stuff learned previously, then more
1153 * time should be spent on mastering than learning new
1154 * items. Note that we only do this during the initial
1155 * phase while new items are still being introduced. */
1156 if (mnemon->to_introduce)
1157 mnemon->to_master++;
1158 /* We go to -2 to force a little extra reinforcement
1159 * when re-learning an item, (otherwise, it will often
1160 * get asked again immediately where it is easy to get
1161 * a correct response without any learning). */
1169 if (mnemon->to_introduce)
1170 printf ("%d to come. ", mnemon->to_introduce);
1171 if (mnemon->unlearned)
1172 printf ("%d still unlearned. ", mnemon->unlearned);
1173 if (mnemon->to_introduce == 0 && mnemon->to_master > 0)
1174 printf ("%d items to master", mnemon->to_master);
1177 bin = mnemon_get_bin (mnemon, item->score);
1179 bin_add_item (bin, item);
1183 mnemon_show_challenge (mnemon_t *mnemon,
1184 challenge_type_t challenge_type,
1185 const char *challenge)
1187 const char *program;
1190 if (challenge_type == CHALLENGE_TYPE_TEXT) {
1191 printf ("%s\n", challenge);
1195 /* XXX: Yes, shelling out to system is total cheese. The planned
1196 * fix here is to bring graphical display in process, (or at least
1197 * have a custom external program that accepts image filenames on
1200 switch (challenge_type) {
1201 case CHALLENGE_TYPE_TEXT:
1204 case CHALLENGE_TYPE_IMAGE:
1205 program = "xli -gamma 2.2";
1207 case CHALLENGE_TYPE_AUDIO:
1210 case CHALLENGE_TYPE_MIDI:
1211 program = "timidity -Os";
1213 case CHALLENGE_TYPE_TEXT_TO_SPEECH:
1214 program = "mnemon-tts";
1218 xasprintf (&command, "%s %s/%s >/dev/null 2>&1 &",
1227 mnemon_hide_challenge (unused (mnemon_t *mnemon),
1228 challenge_type_t challenge_type)
1232 if (challenge_type != CHALLENGE_TYPE_IMAGE)
1235 /* XXX: And this is just embarrassing (obviously wrong in several
1236 * ways). Hopefully I'll amend away any commit that includes this.
1238 xasprintf (&command, "killall xli");
1244 mnemon_do_challenges (mnemon_t *mnemon)
1249 category_t *category;
1253 /* Count the number of items with negative scores. */
1254 mnemon->unlearned = 0;
1255 for (i = 0; i < mnemon->num_bins; i++) {
1256 bin = &mnemon->bins[i];
1257 if (bin->score >= 0)
1259 mnemon->unlearned += bin->num_items;
1262 mnemon->to_introduce -= mnemon->unlearned;
1263 if (mnemon->to_introduce < 0)
1264 mnemon->to_introduce = 0;
1266 /* Get rid of bin with score of 0 if we aren't going to be
1267 * introducing anything from it. */
1268 if (mnemon->to_introduce == 0) {
1269 bin = mnemon_get_bin (mnemon, 0);
1270 mnemon_remove_bin (mnemon, bin);
1273 if (mnemon->unlearned) {
1274 printf ("You've got %d items to learn already. ", mnemon->unlearned);
1275 if (mnemon->to_introduce)
1276 printf ("I'll introduce %d more as we go.", mnemon->to_introduce);
1279 printf ("Introducing %d new items.\n", mnemon->to_introduce);
1284 struct timeval start, end;
1286 mnemon_select_item (mnemon, &bin, &item_index, &category);
1287 item = bin->items[item_index];
1290 if (category->time_limit > 0.0) {
1291 response = readline ("The next one is timed. Press enter when ready:");
1295 mnemon_show_challenge (mnemon, category->challenge_type,
1298 gettimeofday (&start, NULL);
1299 response = readline ("> ");
1300 gettimeofday (&end, NULL);
1302 mnemon_hide_challenge (mnemon, category->challenge_type);
1304 /* Terminate on EOF */
1305 if (response == NULL) {
1310 if (response[0] == '/') {
1311 mnemon_handle_command (mnemon, response + 1);
1318 mnemon_handle_response (mnemon, bin, item_index,
1320 (end.tv_sec + end.tv_usec / 1e6) -
1321 (start.tv_sec + start.tv_usec / 1e6),
1322 category->time_limit);
1325 /* Replay audio challenges for reinforcement. */
1326 if (category->repeat)
1328 mnemon_show_challenge (mnemon, category->challenge_type,
1330 printf ("%s\n", item->challenge);
1333 } while (mnemon->to_introduce ||
1334 mnemon->unlearned ||
1335 mnemon->to_master > 0);