Line terminators in text buffer windows
[projects/chimara/chimara.git] / libchimara / input.c
1 #include "charset.h"
2 #include "magic.h"
3 #include "input.h"
4 #include "pager.h"
5 #include "chimara-glk-private.h"
6 #include "garglk.h"
7
8 extern GPrivate *glk_data_key;
9
10 /* Forward declarations */
11 static int finish_text_buffer_line_input(winid_t win, gboolean emit_signal);
12 static int finish_text_grid_line_input(winid_t win, gboolean emit_signal);
13 static void cancel_old_input_request(winid_t win);
14
15 /* Internal function: code common to both flavors of char event request */
16 void
17 request_char_event_common(winid_t win, gboolean unicode)
18 {
19         VALID_WINDOW(win, return);
20         g_return_if_fail(win->type != wintype_TextBuffer || win->type != wintype_TextGrid);
21
22         cancel_old_input_request(win);
23
24         flush_window_buffer(win);
25
26         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
27
28         win->input_request_type = unicode? INPUT_REQUEST_CHARACTER_UNICODE : INPUT_REQUEST_CHARACTER;
29         g_signal_handler_unblock( win->widget, win->char_input_keypress_handler );
30
31         gdk_threads_enter();
32         gtk_widget_grab_focus( GTK_WIDGET(win->widget) );
33         gdk_threads_leave();
34
35         /* Emit the "waiting" signal to let listeners know we are ready for input */
36         g_signal_emit_by_name(glk_data->self, "waiting");
37 }
38
39 /**
40  * glk_request_char_event:
41  * @win: A window to request char events from.
42  *
43  * Request input of a Latin-1 character or special key. A window cannot have
44  * requests for both character and line input at the same time. Nor can it have
45  * requests for character input of both types (Latin-1 and Unicode). It is
46  * illegal to call glk_request_char_event() if the window already has a pending
47  * request for either character or line input.
48  */
49 void
50 glk_request_char_event(winid_t win)
51 {
52         request_char_event_common(win, FALSE);
53 }
54
55 /**
56  * glk_request_char_event_uni:
57  * @win: A window to request char events from.
58  *
59  * Request input of a Unicode character or special key. See
60  * glk_request_char_event().
61  */
62 void
63 glk_request_char_event_uni(winid_t win)
64 {
65         request_char_event_common(win, TRUE);
66 }
67
68 /**
69  * glk_cancel_char_event:
70  * @win: A window to cancel the latest char event request on.
71  *
72  * This cancels a pending request for character input. (Either Latin-1 or
73  * Unicode.) For convenience, it is legal to call glk_cancel_char_event() even
74  * if there is no charcter input request on that window. Glk will ignore the
75  * call in this case.
76  */
77 void
78 glk_cancel_char_event(winid_t win)
79 {
80         VALID_WINDOW(win, return);
81         g_return_if_fail(win->type != wintype_TextBuffer || win->type != wintype_TextGrid);
82
83         if(win->input_request_type == INPUT_REQUEST_CHARACTER || win->input_request_type == INPUT_REQUEST_CHARACTER_UNICODE)
84         {
85                 win->input_request_type = INPUT_REQUEST_NONE;
86                 g_signal_handler_block( win->widget, win->char_input_keypress_handler );
87         }
88 }
89
90 /* Internal function: Request either latin-1 or unicode line input, in a text grid window. */
91 static void
92 text_grid_request_line_event_common(winid_t win, glui32 maxlen, gboolean insert, gchar *inserttext)
93 {
94         gdk_threads_enter();
95
96         GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
97
98     GtkTextMark *cursor = gtk_text_buffer_get_mark(buffer, "cursor_position");
99     GtkTextIter start_iter, end_iter;
100     gtk_text_buffer_get_iter_at_mark(buffer, &start_iter, cursor);
101
102     /* Determine the maximum length of the line input */
103     gint cursorpos = gtk_text_iter_get_line_offset(&start_iter);
104     /* Odd; the Glk spec says the maximum input length is
105     windowwidth - 1 - cursorposition. I say no, because if cursorposition is
106     zero, then the input should fill the whole line. FIXME??? */
107     win->input_length = MIN(win->width - cursorpos, win->line_input_buffer_max_len);
108     end_iter = start_iter;
109     gtk_text_iter_set_line_offset(&end_iter, cursorpos + win->input_length);
110
111         /* If the buffer currently has a selection with one bound in the middle of
112         the input field, then deselect it. Otherwise the input field gets trashed */
113         GtkTextIter start_sel, end_sel;
114         if( gtk_text_buffer_get_selection_bounds(buffer, &start_sel, &end_sel) )
115         {
116                 if( gtk_text_iter_in_range(&start_sel, &start_iter, &end_iter) )
117                         gtk_text_buffer_place_cursor(buffer, &end_sel);
118                 if( gtk_text_iter_in_range(&end_sel, &start_iter, &end_iter) )
119                         gtk_text_buffer_place_cursor(buffer, &start_sel);
120         }
121
122     /* Erase the text currently in the input field and replace it with a GtkEntry */
123     gtk_text_buffer_delete(buffer, &start_iter, &end_iter);
124     win->input_anchor = gtk_text_buffer_create_child_anchor(buffer, &start_iter);
125     win->input_entry = gtk_entry_new();
126         /* Set the entry's font to match that of the window */
127     GtkRcStyle *style = gtk_widget_get_modifier_style(win->widget);     /* Don't free */
128         gtk_widget_modify_font(win->input_entry, style->font_desc);
129         /* Make the entry as small as possible to fit with the text */
130         gtk_entry_set_has_frame(GTK_ENTRY(win->input_entry), FALSE);
131         GtkBorder border = { 0, 0, 0, 0 };
132
133         /* COMPAT: */
134 #if GTK_CHECK_VERSION(2,10,0)
135         gtk_entry_set_inner_border(GTK_ENTRY(win->input_entry), &border);
136 #endif
137     gtk_entry_set_max_length(GTK_ENTRY(win->input_entry), win->input_length);
138     gtk_entry_set_width_chars(GTK_ENTRY(win->input_entry), win->input_length);
139
140     /* Insert pre-entered text if needed */
141     if(insert)
142         gtk_entry_set_text(GTK_ENTRY(win->input_entry), inserttext);
143
144     /* Set background color of entry (TODO: implement as property) */
145     GdkColor background;
146         gdk_color_parse("grey", &background);
147     gtk_widget_modify_base(win->input_entry, GTK_STATE_NORMAL, &background);
148
149     g_signal_connect(win->input_entry, "activate", G_CALLBACK(on_input_entry_activate), win);
150     g_signal_connect(win->input_entry, "key-press-event", G_CALLBACK(on_input_entry_key_press_event), win);
151     win->line_input_entry_changed = g_signal_connect(win->input_entry, "changed", G_CALLBACK(on_input_entry_changed), win);
152
153     gtk_widget_show(win->input_entry);
154     gtk_text_view_add_child_at_anchor(GTK_TEXT_VIEW(win->widget), win->input_entry, win->input_anchor);
155
156         gtk_widget_grab_focus(win->input_entry);
157
158         gdk_threads_leave();
159 }
160
161 /* Internal function: Request either latin-1 or unicode line input, in a text buffer window. */
162 static void
163 text_buffer_request_line_event_common(winid_t win, glui32 maxlen, gboolean insert, gchar *inserttext)
164 {
165         flush_window_buffer(win);
166
167         gdk_threads_enter();
168
169         GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
170
171     /* Move the input_position mark to the end of the window_buffer */
172     GtkTextMark *input_position = gtk_text_buffer_get_mark(buffer, "input_position");
173     GtkTextIter end_iter;
174     gtk_text_buffer_get_end_iter(buffer, &end_iter);
175     gtk_text_buffer_move_mark(buffer, input_position, &end_iter);
176
177     /* Set the entire contents of the window_buffer as uneditable
178      * (so input can only be entered at the end) */
179     GtkTextIter start_iter;
180     gtk_text_buffer_get_start_iter(buffer, &start_iter);
181     gtk_text_buffer_remove_tag_by_name(buffer, "uneditable", &start_iter, &end_iter);
182     gtk_text_buffer_apply_tag_by_name(buffer, "uneditable", &start_iter, &end_iter);
183
184     /* Insert pre-entered text if needed */
185     if(insert) {
186         gtk_text_buffer_insert(buffer, &end_iter, inserttext, -1);
187                 gtk_text_buffer_get_end_iter(buffer, &end_iter); /* update after text insertion */
188         }
189
190         /* Apply the correct style to the input prompt */
191         GtkTextIter input_iter;
192     gtk_text_buffer_get_iter_at_mark(buffer, &input_iter, input_position);
193     gtk_text_buffer_apply_tag_by_name(buffer, "input", &input_iter, &end_iter);
194
195     gtk_text_view_set_editable(GTK_TEXT_VIEW(win->widget), TRUE);
196
197         g_signal_handler_unblock(buffer, win->insert_text_handler);
198         gtk_widget_grab_focus(win->widget);
199
200         gdk_threads_leave();
201 }
202
203 /**
204  * glk_request_line_event:
205  * @win: A text buffer or text grid window to request line input on.
206  * @buf: A buffer of at least @maxlen bytes.
207  * @maxlen: Length of the buffer.
208  * @initlen: The number of characters in @buf to pre-enter.
209  *
210  * Requests input of a line of Latin-1 characters. A window cannot have requests
211  * for both character and line input at the same time. Nor can it have requests
212  * for line input of both types (Latin-1 and Unicode). It is illegal to call
213  * glk_request_line_event() if the window already has a pending request for
214  * either character or line input.
215  *
216  * The @buf argument is a pointer to space where the line input will be stored.
217  * (This may not be %NULL.) @maxlen is the length of this space, in bytes; the
218  * library will not accept more characters than this. If @initlen is nonzero,
219  * then the first @initlen bytes of @buf will be entered as pre-existing input
220  * — just as if the player had typed them himself. (The player can continue
221  * composing after this pre-entered input, or delete it or edit as usual.)
222  *
223  * The contents of the buffer are undefined until the input is completed (either
224  * by a line input event, or glk_cancel_line_event(). The library may or may not
225  * fill in the buffer as the player composes, while the input is still pending;
226  * it is illegal to change the contents of the buffer yourself.
227  */
228 void
229 glk_request_line_event(winid_t win, char *buf, glui32 maxlen, glui32 initlen)
230 {
231         VALID_WINDOW(win, return);
232         g_return_if_fail(buf);
233         g_return_if_fail(win->type != wintype_TextBuffer || win->type != wintype_TextGrid);
234         g_return_if_fail(initlen <= maxlen);
235
236         cancel_old_input_request(win);
237
238         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
239
240         /* Register the buffer */
241         if(glk_data->register_arr)
242         win->buffer_rock = (*glk_data->register_arr)(buf, maxlen, "&+#!Cn");
243
244         win->input_request_type = INPUT_REQUEST_LINE;
245         win->line_input_buffer = buf;
246         win->line_input_buffer_max_len = maxlen;
247         win->echo_current_line_input = win->echo_line_input;
248         win->current_extra_line_terminators = g_slist_copy(win->extra_line_terminators);
249
250         gchar *inserttext = (initlen > 0)? g_strndup(buf, initlen) : g_strdup("");
251         switch(win->type)
252         {
253             case wintype_TextBuffer:
254                 text_buffer_request_line_event_common(win, maxlen, (initlen > 0), inserttext);
255                 break;
256             case wintype_TextGrid:
257                 text_grid_request_line_event_common(win, maxlen, (initlen > 0), inserttext);
258                 break;
259     }
260         g_free(inserttext);
261         g_signal_handler_unblock(win->widget, win->line_input_keypress_handler);
262
263         /* Emit the "waiting" signal to let listeners know we are ready for input */
264         g_signal_emit_by_name(glk_data->self, "waiting");
265 }
266
267 /**
268  * glk_request_line_event_uni:
269  * @win: A text buffer or text grid window to request line input on.
270  * @buf: A buffer of at least @maxlen characters.
271  * @maxlen: Length of the buffer.
272  * @initlen: The number of characters in @buf to pre-enter.
273  *
274  * Request input of a line of Unicode characters. This works the same as
275  * glk_request_line_event(), except the result is stored in an array of
276  * <type>glui32</type> values instead of an array of characters, and the values
277  * may be any valid Unicode code points.
278  *
279  * If possible, the library should return fully composed Unicode characters,
280  * rather than strings of base and composition characters.
281  *
282  * <note><para>
283  *   Fully-composed characters are the norm for Unicode text, so an
284  *   implementation that ignores this issue will probably produce the right
285  *   result. However, the game may not want to rely on that. Another factor is
286  *   that case-folding can (occasionally) produce non-normalized text.
287  *   Therefore, to cover all its bases, a game should call
288  *   glk_buffer_to_lower_case_uni(), followed by
289  *   glk_buffer_canon_normalize_uni(), before parsing.
290  * </para></note>
291  *
292  * <note><para>
293  *   Earlier versions of this spec said that line input must always be in
294  *   Unicode Normalization Form C. However, this has not been universally
295  *   implemented. It is also somewhat redundant, for the results noted above.
296  *   Therefore, we now merely recommend that line input be fully composed. The
297  *   game is ultimately responsible for all case-folding and normalization. See
298  *   <link linkend="chimara-Unicode-String-Normalization">Unicode String
299  *   Normalization</link>.
300  * </para></note>
301  */
302 void
303 glk_request_line_event_uni(winid_t win, glui32 *buf, glui32 maxlen, glui32 initlen)
304 {
305         VALID_WINDOW(win, return);
306         g_return_if_fail(buf);
307         g_return_if_fail(win->type != wintype_TextBuffer || win->type != wintype_TextGrid);
308         g_return_if_fail(initlen <= maxlen);
309
310         cancel_old_input_request(win);
311         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
312
313         /* Register the buffer */
314         if(glk_data->register_arr)
315         win->buffer_rock = (*glk_data->register_arr)(buf, maxlen, "&+#!Iu");
316
317         win->input_request_type = INPUT_REQUEST_LINE_UNICODE;
318         win->line_input_buffer_unicode = buf;
319         win->line_input_buffer_max_len = maxlen;
320         win->echo_current_line_input = win->echo_line_input;
321         win->current_extra_line_terminators = g_slist_copy(win->extra_line_terminators);
322
323         gchar *utf8;
324         if(initlen > 0) {
325                 utf8 = convert_ucs4_to_utf8(buf, initlen);
326                 if(utf8 == NULL)
327                         return;
328         }
329         else
330                 utf8 = g_strdup("");
331
332     switch(win->type)
333         {
334             case wintype_TextBuffer:
335                 text_buffer_request_line_event_common(win, maxlen, (initlen > 0), utf8);
336                 break;
337             case wintype_TextGrid:
338                 text_grid_request_line_event_common(win, maxlen, (initlen > 0), utf8);
339                 break;
340     }
341     g_signal_handler_unblock(win->widget, win->line_input_keypress_handler);
342         g_free(utf8);
343
344         /* Emit the "waiting" signal to let listeners know we are ready for input */
345         g_signal_emit_by_name(glk_data->self, "waiting");
346 }
347
348 /**
349  * glk_cancel_line_event:
350  * @win: A text buffer or text grid window to cancel line input on.
351  * @event: Will be filled in if the user had already input something.
352  *
353  * This cancels a pending request for line input. (Either Latin-1 or Unicode.)
354  *
355  * The event pointed to by the event argument will be filled in as if the
356  * player had hit <keycap>enter</keycap>, and the input composed so far will be
357  * stored in the buffer; see below. If you do not care about this information,
358  * pass %NULL as the @event argument. (The buffer will still be filled.)
359  *
360  * For convenience, it is legal to call glk_cancel_line_event() even if there
361  * is no line input request on that window. The event type will be set to
362  * %evtype_None in this case.
363  */
364 void
365 glk_cancel_line_event(winid_t win, event_t *event)
366 {
367         VALID_WINDOW(win, return);
368         g_return_if_fail(win->type != wintype_TextBuffer || win->type != wintype_TextGrid);
369
370         if(event != NULL) {
371                 event->type = evtype_None;
372                 event->win = win;
373                 event->val1 = 0;
374                 event->val2 = 0;
375         }
376
377         if(win->input_request_type != INPUT_REQUEST_LINE && win->input_request_type != INPUT_REQUEST_LINE_UNICODE)
378                 return;
379
380         g_signal_handler_block( win->widget, win->line_input_keypress_handler );
381
382         int chars_written = 0;
383
384         gdk_threads_enter();
385         if(win->type == wintype_TextGrid) {
386                 chars_written = finish_text_grid_line_input(win, FALSE);
387         } else if(win->type == wintype_TextBuffer) {
388                 gtk_text_view_set_editable( GTK_TEXT_VIEW(win->widget), FALSE );
389                 GtkTextBuffer *window_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
390                 g_signal_handler_block(window_buffer, win->insert_text_handler);
391                 chars_written = finish_text_buffer_line_input(win, FALSE);
392         }
393         gdk_threads_leave();
394
395         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
396         if(glk_data->unregister_arr)
397         {
398                 if(win->input_request_type == INPUT_REQUEST_LINE_UNICODE)
399                         (*glk_data->unregister_arr)(win->line_input_buffer_unicode, win->line_input_buffer_max_len, "&+#!Iu", win->buffer_rock);
400                 else
401                 (*glk_data->unregister_arr)(win->line_input_buffer, win->line_input_buffer_max_len, "&+#!Cn", win->buffer_rock);
402     }
403
404         if(event != NULL && chars_written > 0) {
405                 event->type = evtype_LineInput;
406                 event->val1 = chars_written;
407         }
408 }
409
410 /* Helper function: Turn off shutdown key-press-event signal handler */
411 static gboolean
412 turn_off_handler(GNode *node)
413 {
414         winid_t win = node->data;
415         g_signal_handler_block(win->widget, win->shutdown_keypress_handler);
416         return FALSE; /* don't stop */
417 }
418
419 /* Internal function: Callback for signal key-press-event while waiting for shutdown. */
420 gboolean
421 on_shutdown_key_press_event(GtkWidget *widget, GdkEventKey *event, winid_t win)
422 {
423         ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(widget, CHIMARA_TYPE_GLK));
424         g_assert(glk);
425         CHIMARA_GLK_USE_PRIVATE(glk, priv);
426         
427         /* Turn off all the signal handlers */
428         if(priv->root_window)
429                 g_node_traverse(priv->root_window, G_IN_ORDER, G_TRAVERSE_LEAVES, -1, (GNodeTraverseFunc)turn_off_handler, NULL);
430         
431         /* Signal the Glk library that it can shut everything down now */
432         g_mutex_lock(priv->shutdown_lock);
433         g_cond_signal(priv->shutdown_key_pressed);
434         g_mutex_unlock(priv->shutdown_lock);
435         
436         return TRUE; /* block the event */
437 }
438
439 /* Internal function: General callback for signal key-press-event on a text buffer or text grid window. Used in character input on both text buffers and grids. Blocked when not in use. */
440 gboolean
441 on_char_input_key_press_event(GtkWidget *widget, GdkEventKey *event, winid_t win)
442 {
443         /* Ignore modifier keys, otherwise the char input will already trigger on 
444         the shift key when the user tries to type a capital letter */
445         if(event->is_modifier)
446                 return FALSE; /* don't stop the event */
447
448         /* All text up to the input position is now regarded as being read by the user */
449         if(win->type == wintype_TextBuffer)
450                 pager_update(win);
451         
452         glui32 keycode = keyval_to_glk_keycode(event->keyval, win->input_request_type == INPUT_REQUEST_CHARACTER_UNICODE);
453
454         ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(widget, CHIMARA_TYPE_GLK));
455         g_assert(glk);
456         event_throw(glk, evtype_CharInput, win, keycode, 0);
457         g_signal_emit_by_name(glk, "char-input", win->rock, event->keyval);
458
459         /* Only one keypress will be handled */
460         win->input_request_type = INPUT_REQUEST_NONE;
461         g_signal_handler_block(win->widget, win->char_input_keypress_handler);
462
463         return TRUE;
464 }
465
466 gboolean
467 on_line_input_key_press_event(GtkWidget *widget, GdkEventKey *event, winid_t win)
468 {
469         switch(win->type)
470         {
471                 case wintype_TextBuffer:
472                 {
473                         /* All text up to the input position is now regarded as being read by the user */
474                         pager_update(win);
475
476                         GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(win->widget));
477
478                         /* History up/down */
479                         if(event->keyval == GDK_Up || event->keyval == GDK_KP_Up
480                                 || event->keyval == GDK_Down || event->keyval == GDK_KP_Down)
481                         {
482                                 /* Prevent falling off the end of the history list */
483                                 if(win->history == NULL)
484                                         return TRUE;
485                                 if( (event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
486                                         && win->history_pos && win->history_pos->next == NULL)
487                                         return TRUE;
488                                 if( (event->keyval == GDK_Down || event->keyval == GDK_KP_Down)
489                                         && (win->history_pos == NULL || win->history_pos->prev == NULL) )
490                                         return TRUE;
491
492                                 GtkTextIter start, end;
493                                 /* Erase any text that was already typed */
494                                 GtkTextMark *input_position = gtk_text_buffer_get_mark(buffer, "input_position");
495                                 gtk_text_buffer_get_iter_at_mark(buffer, &start, input_position);
496                                 gtk_text_buffer_get_end_iter(buffer, &end);
497
498                                 if(win->history_pos == NULL) {
499                                         gchar *current_input = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
500                                         win->history = g_list_prepend(win->history, current_input);
501                                         win->history_pos = win->history;
502                                 }
503
504                                 gtk_text_buffer_delete(buffer, &start, &end);
505
506                                 if(event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
507                                 {
508                                         if(win->history_pos)
509                                                 win->history_pos = g_list_next(win->history_pos);
510                                         else
511                                                 win->history_pos = win->history;
512                                 }
513                                 else /* down */
514                                         win->history_pos = g_list_previous(win->history_pos);
515
516                                 /* Insert the history item into the window */
517                                 gtk_text_buffer_get_end_iter(buffer, &end);
518
519                                 g_signal_handler_block(buffer, win->insert_text_handler);
520                                 gtk_text_buffer_insert_with_tags_by_name(buffer, &end, win->history_pos->data, -1, "default", "input", NULL);
521                                 g_signal_handler_unblock(buffer, win->insert_text_handler);
522                                 return TRUE;
523                         }
524
525                         /* Move to beginning/end of input field */
526                         else if(event->keyval == GDK_Home) {
527                                 GtkTextIter input_iter;
528                                 GtkTextMark *input_position = gtk_text_buffer_get_mark(buffer, "input_position");
529                                 gtk_text_buffer_get_iter_at_mark(buffer, &input_iter, input_position);
530                                 gtk_text_buffer_place_cursor(buffer, &input_iter);
531                                 return TRUE;
532                         }
533                         else if(event->keyval == GDK_End) {
534                                 GtkTextIter end_iter;
535                                 gtk_text_buffer_get_end_iter(buffer, &end_iter);
536                                 gtk_text_buffer_place_cursor(buffer, &end_iter);
537                                 return TRUE;
538                         }
539
540                         /* Handle the line terminators */
541                         else if(event->keyval == GDK_Return || event->keyval == GDK_KP_Enter
542                            || g_slist_find(win->current_extra_line_terminators, GUINT_TO_POINTER(event->keyval)))
543                         {
544                                 /* Remove signal handlers */
545                                 g_signal_handler_block(buffer, win->insert_text_handler);
546                                 g_signal_handler_block(win->widget, win->line_input_keypress_handler);
547
548                                 /* Insert a newline (even if line input was terminated with a different key */
549                                 GtkTextIter end;
550                                 gtk_text_buffer_get_end_iter(buffer, &end);
551                                 gtk_text_buffer_insert(buffer, &end, "\n", 1);
552
553                                 /* Make the window uneditable again and retrieve the text that was input */
554                                 gtk_text_view_set_editable(GTK_TEXT_VIEW(win->widget), FALSE);
555
556                                 int chars_written = finish_text_buffer_line_input(win, TRUE);
557                                 ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK));
558                                 event_throw(glk, evtype_LineInput, win, chars_written, 0);
559                                 return TRUE;
560                         }
561                 }
562                         return FALSE;
563
564                 /* If this is a text grid window, then redirect the key press to the line input GtkEntry */
565                 case wintype_TextGrid:
566                 {
567                         if(event->keyval == GDK_Up || event->keyval == GDK_KP_Up
568                                 || event->keyval == GDK_Down || event->keyval == GDK_KP_Down
569                                 || event->keyval == GDK_Left || event->keyval == GDK_KP_Left
570                                 || event->keyval == GDK_Right || event->keyval == GDK_KP_Right
571                                 || event->keyval == GDK_Tab || event->keyval == GDK_KP_Tab
572                                 || event->keyval == GDK_Page_Up || event->keyval == GDK_KP_Page_Up
573                                 || event->keyval == GDK_Page_Down || event->keyval == GDK_KP_Page_Down
574                                 || event->keyval == GDK_Home || event->keyval == GDK_KP_Home
575                                 || event->keyval == GDK_End || event->keyval == GDK_KP_End)
576                                 return FALSE; /* Don't redirect these keys */
577                         gtk_widget_grab_focus(win->input_entry);
578                         gtk_editable_set_position(GTK_EDITABLE(win->input_entry), -1);
579                         gboolean retval = TRUE;
580                         g_signal_emit_by_name(win->input_entry, "key-press-event", event, &retval);
581                         return retval; /* Block this key event if the entry handled it */
582                 }
583         }
584         return FALSE;
585 }
586
587 /* Internal function: finish handling a line input request, for both text grid and text buffer windows. */
588 static int
589 write_to_window_buffer(winid_t win, const gchar *inserted_text)
590 {
591         int copycount = 0;
592
593     /* Convert the string from UTF-8 to Latin-1 or Unicode */
594     if(win->input_request_type == INPUT_REQUEST_LINE)
595     {
596         gsize bytes_written;
597         gchar *latin1 = convert_utf8_to_latin1(inserted_text, &bytes_written);
598
599         if(latin1 == NULL)
600             return 0;
601
602         /* Place input in the echo stream */
603         if(win->echo_stream != NULL)
604             glk_put_string_stream(win->echo_stream, latin1);
605
606         /* Copy the string (bytes_written does not include the NULL at the end) */
607         copycount = MIN(win->line_input_buffer_max_len, bytes_written);
608         memcpy(win->line_input_buffer, latin1, copycount);
609         g_free(latin1);
610     }
611     else if(win->input_request_type == INPUT_REQUEST_LINE_UNICODE)
612     {
613         glong items_written;
614         gunichar *unicode = convert_utf8_to_ucs4(inserted_text, &items_written);
615
616         if(unicode == NULL)
617             return 0;
618
619         /* Place input in the echo stream */
620         if(win->echo_stream != NULL)
621             glk_put_string_stream_uni(win->echo_stream, unicode);
622
623         /* Copy the string (but not the NULL at the end) */
624         copycount = MIN(win->line_input_buffer_max_len, items_written);
625         memcpy(win->line_input_buffer_unicode, unicode, copycount * sizeof(gunichar));
626         g_free(unicode);
627     }
628     else
629         WARNING("Wrong input request type");
630
631     win->input_request_type = INPUT_REQUEST_NONE;
632         return copycount;
633 }
634
635 /* Internal function: Retrieves the input of a TextBuffer window and stores it in the window buffer.
636  * Returns the number of characters written, suitable for inclusion in a line input event. */
637 static int
638 finish_text_buffer_line_input(winid_t win, gboolean emit_signal)
639 {
640         VALID_WINDOW(win, return 0);
641         g_return_val_if_fail(win->type == wintype_TextBuffer, 0);
642
643         GtkTextIter start_iter, end_iter;
644
645         GtkTextBuffer *window_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
646         GtkTextMark *input_position = gtk_text_buffer_get_mark(window_buffer, "input_position");
647         gtk_text_buffer_get_iter_at_mark(window_buffer, &start_iter, input_position);
648         gtk_text_buffer_get_end_iter(window_buffer, &end_iter);
649
650         gchar *inserted_text = gtk_text_buffer_get_text(window_buffer, &start_iter, &end_iter, FALSE);
651
652         /* If echoing is turned off, remove the text from the window */
653         if(!win->echo_current_line_input)
654                 gtk_text_buffer_delete(window_buffer, &start_iter, &end_iter);
655
656         /* Don't include the newline in the input */
657         char *last_char = inserted_text + strlen(inserted_text) - 1;
658         if(*last_char == '\n')
659                 *last_char = '\0';
660
661         int chars_written = write_to_window_buffer(win, inserted_text);
662         if(emit_signal)
663         {
664                 ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK));
665                 g_assert(glk);
666                 g_signal_emit_by_name(glk, "line-input", win->rock, inserted_text);
667         }
668
669         /* Add the text to the window input history */
670         if(win->history_pos != NULL)
671         {
672                 g_free(win->history->data);
673                 win->history = g_list_delete_link(win->history, win->history);
674         }
675         if(*inserted_text != 0)
676                 win->history = g_list_prepend(win->history, g_strdup(inserted_text));
677
678         win->history_pos = NULL;
679
680         g_free(inserted_text);
681
682         return chars_written;
683 }
684
685 /* Internal function: Retrieves the input of a TextGrid window and stores it in the window buffer.
686  * Returns the number of characters written, suitable for inclusion in a line input event. */
687 static int
688 finish_text_grid_line_input(winid_t win, gboolean emit_signal)
689 {
690         VALID_WINDOW(win, return 0);
691         g_return_val_if_fail(win->type == wintype_TextGrid, 0);
692
693         GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
694
695         gchar *text = g_strdup( gtk_entry_get_text(GTK_ENTRY(win->input_entry)) );
696         /* Move the focus back into the text view */
697         gtk_widget_grab_focus(win->widget);
698         /* Remove entry widget from text view */
699         /* Should be ok even though this is the widget's own signal handler */
700         gtk_container_remove( GTK_CONTAINER(win->widget), GTK_WIDGET(win->input_entry) );
701         win->input_entry = NULL;
702         /* Delete the child anchor */
703         GtkTextIter start, end;
704         gtk_text_buffer_get_iter_at_child_anchor(buffer, &start, win->input_anchor);
705         end = start;
706         gtk_text_iter_forward_char(&end); /* Point after the child anchor */
707         gtk_text_buffer_delete(buffer, &start, &end);
708         win->input_anchor = NULL;
709
710     gchar *spaces = g_strnfill(win->input_length - g_utf8_strlen(text, -1), ' ');
711     gchar *text_to_insert = g_strconcat(text, spaces, NULL);
712         g_free(spaces);
713     gtk_text_buffer_insert(buffer, &start, text_to_insert, -1);
714     g_free(text_to_insert);
715
716     int chars_written = write_to_window_buffer(win, text);
717     if(emit_signal)
718     {
719                 ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK));
720                 g_assert(glk);
721                 g_signal_emit_by_name(glk, "line-input", win->rock, text);
722     }
723
724         /* Add the text to the window input history */
725         if(win->history_pos != NULL)
726         {
727                 g_free(win->history->data);
728                 win->history = g_list_delete_link(win->history, win->history);
729         }
730         if(*text != 0)
731                 win->history = g_list_prepend(win->history, g_strdup(text));
732         win->history_pos = NULL;
733
734         g_free(text);
735         return chars_written;
736 }
737
738 /* Internal function: Callback for signal insert-text on a text buffer window.
739 Runs after the default handler has already inserted the text. */
740 void
741 after_window_insert_text(GtkTextBuffer *textbuffer, GtkTextIter *location, gchar *text, gint len, winid_t win)
742 {
743         GtkTextBuffer *window_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
744
745         /* Set the history position to NULL and erase the text we were already editing */
746         if(win->history_pos != NULL)
747         {
748                 g_free(win->history->data);
749                 win->history = g_list_delete_link(win->history, win->history);
750                 win->history_pos = NULL;
751         }
752
753         /* Apply the 'input' style to the text that was entered */
754         GtkTextIter end_iter;
755         gtk_text_buffer_get_end_iter(window_buffer, &end_iter);
756         GtkTextIter input_iter;
757         GtkTextMark *input_position = gtk_text_buffer_get_mark(window_buffer, "input_position");
758         gtk_text_buffer_get_iter_at_mark(window_buffer, &input_iter, input_position);
759         gtk_text_buffer_apply_tag_by_name(window_buffer, "input", &input_iter, &end_iter);
760 }
761
762 /* Internal function: Callback for signal activate on the line input GtkEntry
763 in a text grid window. */
764 void
765 on_input_entry_activate(GtkEntry *input_entry, winid_t win)
766 {
767         g_signal_handler_block(win->widget, win->line_input_keypress_handler);
768
769         int chars_written = finish_text_grid_line_input(win, TRUE);
770         ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK));
771         event_throw(glk, evtype_LineInput, win, chars_written, 0);
772 }
773
774 /* Internal function: Callback for signal key-press-event on the line input
775 GtkEntry in a text grid window. */
776 gboolean
777 on_input_entry_key_press_event(GtkEntry *input_entry, GdkEventKey *event, winid_t win)
778 {
779         if(event->keyval == GDK_Up || event->keyval == GDK_KP_Up
780                 || event->keyval == GDK_Down || event->keyval == GDK_KP_Down)
781         {
782                 /* Prevent falling off the end of the history list */
783                 if( (event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
784                         && win->history_pos && win->history_pos->next == NULL)
785                         return TRUE;
786                 if( (event->keyval == GDK_Down || event->keyval == GDK_KP_Down)
787                         && (win->history_pos == NULL || win->history_pos->prev == NULL) )
788                         return TRUE;
789
790                 if(win->history_pos == NULL)
791                 {
792                         const gchar *current_input = gtk_entry_get_text(input_entry);
793                         win->history = g_list_prepend(win->history, g_strdup(current_input));
794                         win->history_pos = win->history;
795                 }
796
797                 if(event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
798                 {
799                         if(win->history_pos)
800                                 win->history_pos = g_list_next(win->history_pos);
801                         else
802                                 win->history_pos = win->history;
803                 }
804                 else /* down */
805                         win->history_pos = g_list_previous(win->history_pos);
806
807                 /* Insert the history item into the window */
808                 g_signal_handler_block(input_entry, win->line_input_entry_changed);
809                 gtk_entry_set_text(input_entry, win->history_pos->data);
810                 g_signal_handler_unblock(input_entry, win->line_input_entry_changed);
811                 return TRUE;
812         }
813         return FALSE;
814 }
815
816 void
817 on_input_entry_changed(GtkEditable *editable, winid_t win)
818 {
819         /* Set the history position to NULL and erase the text we were already editing */
820         if(win->history_pos != NULL)
821         {
822                 g_free(win->history->data);
823                 win->history = g_list_delete_link(win->history, win->history);
824                 win->history_pos = NULL;
825         }
826 }
827
828 glui32
829 keyval_to_glk_keycode(guint keyval, gboolean unicode)
830 {
831         glui32 keycode;
832         switch(keyval) {
833                 case GDK_Up:
834                 case GDK_KP_Up: return keycode_Up;
835                 case GDK_Down:
836                 case GDK_KP_Down: return keycode_Down;
837                 case GDK_Left:
838                 case GDK_KP_Left: return keycode_Left;
839                 case GDK_Right:
840                 case GDK_KP_Right: return keycode_Right;
841                 case GDK_Linefeed:
842                 case GDK_Return:
843                 case GDK_KP_Enter: return keycode_Return;
844                 case GDK_Delete:
845                 case GDK_BackSpace:
846                 case GDK_KP_Delete: return keycode_Delete;
847                 case GDK_Escape: return keycode_Escape;
848                 case GDK_Tab:
849                 case GDK_KP_Tab: return keycode_Tab;
850                 case GDK_Page_Up:
851                 case GDK_KP_Page_Up: return keycode_PageUp;
852                 case GDK_Page_Down:
853                 case GDK_KP_Page_Down: return keycode_PageDown;
854                 case GDK_Home:
855                 case GDK_KP_Home: return keycode_Home;
856                 case GDK_End:
857                 case GDK_KP_End: return keycode_End;
858                 case GDK_F1:
859                 case GDK_KP_F1: return keycode_Func1;
860                 case GDK_F2:
861                 case GDK_KP_F2: return keycode_Func2;
862                 case GDK_F3:
863                 case GDK_KP_F3: return keycode_Func3;
864                 case GDK_F4:
865                 case GDK_KP_F4: return keycode_Func4;
866                 case GDK_F5: return keycode_Func5;
867                 case GDK_F6: return keycode_Func6;
868                 case GDK_F7: return keycode_Func7;
869                 case GDK_F8: return keycode_Func8;
870                 case GDK_F9: return keycode_Func9;
871                 case GDK_F10: return keycode_Func10;
872                 case GDK_F11: return keycode_Func11;
873                 case GDK_F12: return keycode_Func12;
874                 default:
875                         keycode = gdk_keyval_to_unicode(keyval);
876                         /* If keycode is 0, then keyval was not recognized; also return
877                         unknown if Latin-1 input was requested and the character is not in
878                         Latin-1 */
879                         if(keycode == 0 || (!unicode && keycode > 255))
880                                 return keycode_Unknown;
881                         return keycode;
882         }
883 }
884
885 void
886 force_char_input_from_queue(winid_t win, event_t *event)
887 {
888         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
889         guint keyval = GPOINTER_TO_UINT(g_async_queue_pop(glk_data->char_input_queue));
890
891         glk_cancel_char_event(win);
892
893         gdk_threads_enter();
894         ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK));
895         g_assert(glk);
896         g_signal_emit_by_name(glk, "char-input", win->rock, keyval);
897         gdk_threads_leave();
898
899         event->type = evtype_CharInput;
900         event->win = win;
901         event->val1 = keyval_to_glk_keycode(keyval, win->input_request_type == INPUT_REQUEST_CHARACTER_UNICODE);
902         event->val2 = 0;
903 }
904
905 void
906 force_line_input_from_queue(winid_t win, event_t *event)
907 {
908         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
909         const gchar *text = g_async_queue_pop(glk_data->line_input_queue);
910         glui32 chars_written = 0;
911
912         gdk_threads_enter();
913         if(win->type == wintype_TextBuffer)
914         {
915                 GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
916                 GtkTextIter start, end;
917
918                 /* Remove signal handlers so the line input doesn't get picked up again */
919                 g_signal_handler_block(buffer, win->insert_text_handler);
920                 g_signal_handler_block(win->widget, win->line_input_keypress_handler);
921
922                 /* Erase any text that was already typed */
923                 GtkTextMark *input_position = gtk_text_buffer_get_mark(buffer, "input_position");
924                 gtk_text_buffer_get_iter_at_mark(buffer, &start, input_position);
925                 gtk_text_buffer_get_end_iter(buffer, &end);
926                 gtk_text_buffer_delete(buffer, &start, &end);
927
928                 /* Make the window uneditable again */
929                 gtk_text_view_set_editable(GTK_TEXT_VIEW(win->widget), FALSE);
930
931                 /* Insert the forced input into the window */
932                 if(win->echo_current_line_input)
933                 {
934                         gtk_text_buffer_get_end_iter(buffer, &end);
935                         gchar *text_to_insert = g_strconcat(text, "\n", NULL);
936                         gtk_text_buffer_insert_with_tags_by_name(buffer, &end, text_to_insert, -1, "default", "input", NULL);
937                 }
938
939                 chars_written = finish_text_buffer_line_input(win, TRUE);
940         }
941         else if(win->type == wintype_TextGrid)
942         {
943                 /* Remove signal handlers so the line input doesn't get picked up again */
944                 g_signal_handler_block(win->widget, win->char_input_keypress_handler);
945
946                 /* Insert the forced input into the window */
947                 gtk_entry_set_text(GTK_ENTRY(win->input_entry), text);
948                 chars_written = finish_text_grid_line_input(win, TRUE);
949         }
950         gdk_threads_leave();
951
952         event->type = evtype_LineInput;
953         event->win = win;
954         event->val1 = chars_written;
955         event->val2 = 0;
956 }
957
958 /*** Internal function: cancels any pending input requests on the window and presents a warning if not INPUT_REQUEST_NONE ***/
959 void
960 cancel_old_input_request(winid_t win)
961 {
962         switch(win->input_request_type) {
963         case INPUT_REQUEST_NONE:
964                 break; /* All is well */
965         case INPUT_REQUEST_CHARACTER:
966         case INPUT_REQUEST_CHARACTER_UNICODE:
967                 glk_cancel_char_event(win);
968                 WARNING("Cancelling pending char event");
969                 break;
970         case INPUT_REQUEST_LINE:
971         case INPUT_REQUEST_LINE_UNICODE:
972                 glk_cancel_line_event(win, NULL);
973                 WARNING("Cancelling pending line event");
974                 break;
975         default:
976                 WARNING("Could not cancel pending input request: unknown input request");
977         }
978 }
979
980 /**
981  * glk_set_echo_line_event:
982  * @win: The window in which to change the echoing behavior.
983  * @val: Zero to turn off echoing, nonzero for normal echoing.
984  *
985  * Normally, after line input is completed or cancelled in a buffer window, the
986  * library ensures that the complete input line (or its latest state, after
987  * cancelling) is displayed at the end of the buffer, followed by a newline.
988  * This call allows you to suppress this behavior. If the @val argument is zero,
989  * all <emphasis>subsequent</emphasis> line input requests in the given window
990  * will leave the buffer unchanged after the input is completed or cancelled;
991  * the player's input will not be printed. If @val is nonzero, subsequent input
992  * requests will have the normal printing behavior.
993  *
994  * <note><para>
995  *   Note that this feature is unrelated to the window's echo stream.
996  * </para></note>
997  *
998  * If you turn off line input echoing, you can reproduce the standard input
999  * behavior by following each line input event (or line input cancellation) by
1000  * printing the input line, in the Input style, followed by a newline in the
1001  * original style.
1002  *
1003  * The glk_set_echo_line_event() does not affect a pending line input request.
1004  * It also has no effect in non-buffer windows.
1005  * <note><para>
1006  *   In a grid window, the game can overwrite the input area at will, so there
1007  *   is no need for this distinction.
1008  * </para></note>
1009  *
1010  * Not all libraries support this feature. You can test for it with
1011  * %gestalt_LineInputEcho.
1012  */
1013 void
1014 glk_set_echo_line_event(winid_t win, glui32 val)
1015 {
1016         VALID_WINDOW(win, return);
1017
1018         if(win->type != wintype_TextBuffer)
1019                 return; /* Quietly do nothing */
1020
1021         win->echo_line_input = val? TRUE : FALSE;
1022 }
1023
1024 /* Internal function to convert from a Glk keycode to a GDK keyval. */
1025 static unsigned
1026 keycode_to_gdk_keyval(glui32 keycode)
1027 {
1028         switch (keycode)
1029         {
1030                 case keycode_Left:
1031                         return GDK_Left;
1032                 case keycode_Right:
1033                         return GDK_Right;
1034                 case keycode_Up:
1035                         return GDK_Up;
1036                 case keycode_Down:
1037                         return GDK_Down;
1038                 case keycode_Return:
1039                         return GDK_Return;
1040                 case keycode_Delete:
1041                         return GDK_Delete;
1042                 case keycode_Escape:
1043                         return GDK_Escape;
1044                 case keycode_Tab:
1045                         return GDK_Tab;
1046                 case keycode_PageUp:
1047                         return GDK_Page_Up;
1048                 case keycode_PageDown:
1049                         return GDK_Page_Down;
1050                 case keycode_Home:
1051                         return GDK_Home;
1052                 case keycode_End:
1053                         return GDK_End;
1054                 case keycode_Func1:
1055                         return GDK_F1;
1056                 case keycode_Func2:
1057                         return GDK_F2;
1058                 case keycode_Func3:
1059                         return GDK_F3;
1060                 case keycode_Func4:
1061                         return GDK_F4;
1062                 case keycode_Func5:
1063                         return GDK_F5;
1064                 case keycode_Func6:
1065                         return GDK_F6;
1066                 case keycode_Func7:
1067                         return GDK_F7;
1068                 case keycode_Func8:
1069                         return GDK_F8;
1070                 case keycode_Func9:
1071                         return GDK_F9;
1072                 case keycode_Func10:
1073                         return GDK_F10;
1074                 case keycode_Func11:
1075                         return GDK_F11;
1076                 case keycode_Func12:
1077                         return GDK_F12;
1078                 case keycode_Erase:
1079                         return GDK_BackSpace;
1080         }
1081         unsigned keyval = gdk_unicode_to_keyval(keycode);
1082         if(keyval < 0x01000000) /* magic number meaning illegal unicode point */
1083                 return keyval;
1084         return 0;
1085 }
1086
1087 /* Internal function to decide whether @keycode is a valid line terminator. */
1088 gboolean
1089 is_valid_line_terminator(glui32 keycode)
1090 {
1091         switch(keycode) {
1092                 case keycode_Escape:
1093                 case keycode_Func1:
1094                 case keycode_Func2:
1095                 case keycode_Func3:
1096                 case keycode_Func4:
1097                 case keycode_Func5:
1098                 case keycode_Func6:
1099                 case keycode_Func7:
1100                 case keycode_Func8:
1101                 case keycode_Func9:
1102                 case keycode_Func10:
1103                 case keycode_Func11:
1104                 case keycode_Func12:
1105                         return TRUE;
1106         }
1107         return FALSE;
1108 }
1109
1110 /**
1111  * glk_set_terminators_line_event:
1112  * @win: The window for which to set the line input terminator keys.
1113  * @keycodes: An array of <code>keycode_</code> constants, of length @count.
1114  * @count: The array length of @keycodes.
1115  *
1116  * It is possible to request that other keystrokes complete line input as well.
1117  * (This allows a game to intercept function keys or other special keys during
1118  * line input.) To do this, call glk_set_terminators_line_event(), and pass an
1119  * array of @count keycodes. These must all be special keycodes (see <link
1120  * linkend="chimara-Character-Input">Character Input</link>). Do not include
1121  * regular printable characters in the array, nor %keycode_Return (which
1122  * represents the default <keycap>enter</keycap> key and will always be
1123  * recognized). To return to the default behavior, pass a %NULL or empty array.
1124  *
1125  * The glk_set_terminators_line_event() affects <emphasis>subsequent</emphasis>
1126  * line input requests in the given window. It does not affect a pending line
1127  * input request.
1128  *
1129  * <note><para>
1130  *   This distinction makes life easier for interpreters that set up UI
1131  *   callbacks only at the start of input.
1132  * </para></note>
1133  *
1134  * A library may not support this feature; if it does, it may not support all
1135  * special keys as terminators. (Some keystrokes are reserved for OS or
1136  * interpreter control.) You can test for this with %gestalt_LineTerminators and
1137  * %gestalt_LineTerminatorKey.
1138  */
1139 void
1140 glk_set_terminators_line_event(winid_t win, glui32 *keycodes, glui32 count)
1141 {
1142         VALID_WINDOW(win, return);
1143
1144         g_slist_free(win->extra_line_terminators);
1145         win->extra_line_terminators = NULL;
1146
1147         if(keycodes == NULL || count == 0)
1148                 return;
1149
1150         int i;
1151         for(i = 0; i < count; i++)
1152         {
1153                 unsigned key = keycode_to_gdk_keyval(keycodes[i]);
1154                 if(is_valid_line_terminator(keycodes[i]))
1155                         win->extra_line_terminators = g_slist_prepend(win->extra_line_terminators, GUINT_TO_POINTER(key));
1156                 else
1157                    WARNING_S("Ignoring invalid line terminator", gdk_keyval_name(key));
1158         }
1159 }