merge from 1.8 branch
[bpt/guile.git] / guile-readline / readline.c
CommitLineData
c374ab69
MV
1/* readline.c --- line editing support for Guile */
2
1b09b607 3/* Copyright (C) 1997,1999,2000,2001, 2002, 2003, 2006 Free Software Foundation, Inc.
c374ab69
MV
4 *
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 2, or (at your option)
8 * any later version.
9 *
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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this software; see the file COPYING. If not, write to
92205699
MV
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 USA
c374ab69
MV
19 *
20 */
f48e47b9 21
f48e47b9 22
c374ab69
MV
23\f
24
26aff4f9
RB
25#if HAVE_CONFIG_H
26# include <config.h>
27#endif
28
739b3bf1 29#include "libguile/_scm.h"
62947883 30#ifdef HAVE_RL_GETC_FUNCTION
fbf68f8b 31#include "libguile.h"
739b3bf1
MD
32#include "libguile/gh.h"
33#include "libguile/iselect.h"
34
ffdeebc3 35#include <stdio.h>
48552b1d
MD
36#ifdef HAVE_UNISTD_H
37#include <unistd.h>
38#endif
c374ab69
MV
39#include <readline/readline.h>
40#include <readline/history.h>
5e90b6ac 41#ifndef __MINGW32__
c374ab69 42#include <sys/time.h>
5e90b6ac
MV
43#else
44#include <io.h>
45#endif
b71099ba 46#include <signal.h>
c374ab69 47
1c537018 48#include "libguile/validate.h"
a0599745 49#include "guile-readline/readline.h"
c374ab69 50
593be5d2 51scm_t_option scm_readline_opts[] = {
c374ab69
MV
52 { SCM_OPTION_BOOLEAN, "history-file", 1,
53 "Use history file." },
54 { SCM_OPTION_INTEGER, "history-length", 200,
55 "History length." },
56 { SCM_OPTION_INTEGER, "bounce-parens", 500,
57 "Time (ms) to show matching opening parenthesis (0 = off)."}
58};
59
60extern void stifle_history (int max);
61
b916d813 62SCM_DEFINE (scm_readline_options, "readline-options-interface", 0, 1, 0,
f48e47b9
GB
63 (SCM setting),
64"")
65#define FUNC_NAME s_scm_readline_options
c374ab69
MV
66{
67 SCM ans = scm_options (setting,
68 scm_readline_opts,
69 SCM_N_READLINE_OPTIONS,
f48e47b9 70 FUNC_NAME);
c374ab69
MV
71 stifle_history (SCM_HISTORY_LENGTH);
72 return ans;
73}
f48e47b9 74#undef FUNC_NAME
c374ab69
MV
75
76#ifndef HAVE_STRDUP
77static char *
78strdup (char *s)
79{
1be6b49c 80 size_t len = strlen (s);
c374ab69
MV
81 char *new = malloc (len + 1);
82 strcpy (new, s);
83 return new;
84}
85#endif /* HAVE_STRDUP */
86
87#ifndef HAVE_RL_CLEANUP_AFTER_SIGNAL
88
89/* These are readline functions added in release 2.3. They will work
90 * together with readline-2.1 and 2.2. (The readline interface is
91 * disabled for earlier releases.)
92 * They are declared static; if we want to use them elsewhere, then
93 * we need external declarations for them, but at the moment, I don't
94 * think anything else in Guile ought to use these.
95 */
96
97extern void _rl_clean_up_for_exit ();
98extern void _rl_kill_kbd_macro ();
99extern int _rl_init_argument ();
100
2e3d5987 101void
c374ab69
MV
102rl_cleanup_after_signal ()
103{
104#ifdef HAVE_RL_CLEAR_SIGNALS
105 _rl_clean_up_for_exit ();
106#endif
107 (*rl_deprep_term_function) ();
108#ifdef HAVE_RL_CLEAR_SIGNALS
109 rl_clear_signals ();
110#endif
111 rl_pending_input = 0;
112}
113
2e3d5987 114void
c374ab69
MV
115rl_free_line_state ()
116{
117 register HIST_ENTRY *entry;
118
119 free_undo_list ();
120
121 entry = current_history ();
122 if (entry)
123 entry->data = (char *)NULL;
124
125 _rl_kill_kbd_macro ();
126 rl_clear_message ();
127 _rl_init_argument ();
128}
129
130#endif /* !HAVE_RL_CLEANUP_AFTER_SIGNAL */
131
132static int promptp;
133static SCM input_port;
134static SCM before_read;
135
136static int
e81d98ec 137current_input_getc (FILE *in SCM_UNUSED)
c374ab69 138{
be49d1df 139 if (promptp && scm_is_true (before_read))
c374ab69
MV
140 {
141 scm_apply (before_read, SCM_EOL, SCM_EOL);
142 promptp = 0;
143 }
bc858b80 144 return scm_getc (input_port);
c374ab69
MV
145}
146
c374ab69 147static int in_readline = 0;
bb0f37e7 148static SCM reentry_barrier_mutex;
c374ab69 149
f48e47b9
GB
150static SCM internal_readline (SCM text);
151static SCM handle_error (void *data, SCM tag, SCM args);
48552b1d 152static void reentry_barrier (void);
f48e47b9
GB
153
154
b916d813 155SCM_DEFINE (scm_readline, "%readline", 0, 4, 0,
f48e47b9
GB
156 (SCM text, SCM inp, SCM outp, SCM read_hook),
157"")
158#define FUNC_NAME s_scm_readline
159{
160 SCM ans;
161
162 reentry_barrier ();
163
164 before_read = SCM_BOOL_F;
165
166 if (!SCM_UNBNDP (text))
167 {
ad6dec05 168 if (!scm_is_string (text))
f48e47b9
GB
169 {
170 --in_readline;
171 scm_wrong_type_arg (s_scm_readline, SCM_ARG1, text);
172 }
f48e47b9
GB
173 }
174
0ddf47fc 175 if (!((SCM_UNBNDP (inp) && SCM_OPINFPORTP (scm_current_input_port ()))
379b35da 176 || SCM_OPINFPORTP (inp)))
f48e47b9
GB
177 {
178 --in_readline;
179 scm_misc_error (s_scm_readline,
180 "Input port is not open or not a file port",
181 SCM_EOL);
182 }
183
0ddf47fc 184 if (!((SCM_UNBNDP (outp) && SCM_OPOUTFPORTP (scm_current_output_port ()))
379b35da 185 || SCM_OPOUTFPORTP (outp)))
f48e47b9
GB
186 {
187 --in_readline;
188 scm_misc_error (s_scm_readline,
189 "Output port is not open or not a file port",
190 SCM_EOL);
191 }
192
be49d1df 193 if (!(SCM_UNBNDP (read_hook) || scm_is_false (read_hook)))
f48e47b9 194 {
be49d1df 195 if (scm_is_false (scm_thunk_p (read_hook)))
f48e47b9
GB
196 {
197 --in_readline;
198 scm_wrong_type_arg (s_scm_readline, SCM_ARG4, read_hook);
199 }
200 before_read = read_hook;
201 }
202
203 scm_readline_init_ports (inp, outp);
204
205 ans = scm_internal_catch (SCM_BOOL_T,
593be5d2 206 (scm_t_catch_body) internal_readline,
f60d011a 207 (void *) SCM_UNPACK (text),
f48e47b9
GB
208 handle_error, 0);
209
8f99e3f3 210#ifndef __MINGW32__
f48e47b9
GB
211 fclose (rl_instream);
212 fclose (rl_outstream);
8f99e3f3 213#endif
f48e47b9
GB
214
215 --in_readline;
216 return ans;
217}
218#undef FUNC_NAME
219
220
c374ab69
MV
221static void
222reentry_barrier ()
223{
224 int reentryp = 0;
246c563b 225 /* We should rather use scm_try_mutex when it becomes available */
bb0f37e7 226 scm_lock_mutex (reentry_barrier_mutex);
c374ab69
MV
227 if (in_readline)
228 reentryp = 1;
229 else
230 ++in_readline;
bb0f37e7 231 scm_unlock_mutex (reentry_barrier_mutex);
c374ab69 232 if (reentryp)
f48e47b9 233 scm_misc_error (s_scm_readline, "readline is not reentrant", SCM_EOL);
c374ab69
MV
234}
235
236static SCM
237handle_error (void *data, SCM tag, SCM args)
238{
239 rl_free_line_state ();
240 rl_cleanup_after_signal ();
739b3bf1 241 fputc ('\n', rl_outstream); /* We don't want next output on this line */
8f99e3f3 242#ifndef __MINGW32__
c374ab69
MV
243 fclose (rl_instream);
244 fclose (rl_outstream);
8f99e3f3 245#endif
c374ab69
MV
246 --in_readline;
247 scm_handle_by_throw (data, tag, args);
248 return SCM_UNSPECIFIED; /* never reached */
249}
250
251static SCM
252internal_readline (SCM text)
253{
254 SCM ret;
255 char *s;
ad6dec05 256 char *prompt = SCM_UNBNDP (text) ? "" : scm_to_locale_string (text);
c374ab69
MV
257
258 promptp = 1;
259 s = readline (prompt);
260 if (s)
ad6dec05 261 ret = scm_from_locale_string (s);
c374ab69
MV
262 else
263 ret = SCM_EOF_VAL;
264
ad6dec05
MV
265 if (!SCM_UNBNDP (text))
266 free (prompt);
c374ab69
MV
267 free (s);
268
269 return ret;
270}
271
272static FILE *
273stream_from_fport (SCM port, char *mode, const char *subr)
274{
275 int fd;
276 FILE *f;
277
593be5d2 278 fd = dup (((struct scm_t_fport *) SCM_STREAM (port))->fdes);
c374ab69
MV
279 if (fd == -1)
280 {
281 --in_readline;
282 scm_syserror (subr);
283 }
284
285 f = fdopen (fd, mode);
286 if (f == NULL)
287 {
288 --in_readline;
289 scm_syserror (subr);
290 }
291
292 return f;
293}
294
2e3d5987
MD
295void
296scm_readline_init_ports (SCM inp, SCM outp)
297{
298 if (SCM_UNBNDP (inp))
0ddf47fc 299 inp = scm_current_input_port ();
2e3d5987
MD
300
301 if (SCM_UNBNDP (outp))
0ddf47fc 302 outp = scm_current_output_port ();
2e3d5987 303
379b35da 304 if (!SCM_OPINFPORTP (inp)) {
2e3d5987
MD
305 scm_misc_error (0,
306 "Input port is not open or not a file port",
307 SCM_EOL);
308 }
309
379b35da 310 if (!SCM_OPOUTFPORTP (outp)) {
2e3d5987
MD
311 scm_misc_error (0,
312 "Output port is not open or not a file port",
313 SCM_EOL);
314 }
315
316 input_port = inp;
8f99e3f3 317#ifndef __MINGW32__
f48e47b9
GB
318 rl_instream = stream_from_fport (inp, "r", s_scm_readline);
319 rl_outstream = stream_from_fport (outp, "w", s_scm_readline);
8f99e3f3 320#endif
2e3d5987
MD
321}
322
c374ab69 323
c374ab69 324
b916d813 325SCM_DEFINE (scm_add_history, "add-history", 1, 0, 0,
f48e47b9
GB
326 (SCM text),
327"")
328#define FUNC_NAME s_scm_add_history
c374ab69
MV
329{
330 char* s;
c374ab69 331
ad6dec05
MV
332 s = scm_to_locale_string (text);
333 add_history (s);
c374ab69
MV
334
335 return SCM_UNSPECIFIED;
336}
f48e47b9 337#undef FUNC_NAME
c374ab69
MV
338
339
b916d813 340SCM_DEFINE (scm_read_history, "read-history", 1, 0, 0,
f48e47b9
GB
341 (SCM file),
342"")
343#define FUNC_NAME s_scm_read_history
c374ab69 344{
ad6dec05
MV
345 char *filename;
346 SCM ret;
347
348 filename = scm_to_locale_string (file);
349 ret = scm_from_bool (!read_history (filename));
350 free (filename);
351 return ret;
c374ab69 352}
f48e47b9 353#undef FUNC_NAME
c374ab69
MV
354
355
b916d813 356SCM_DEFINE (scm_write_history, "write-history", 1, 0, 0,
f48e47b9
GB
357 (SCM file),
358"")
359#define FUNC_NAME s_scm_write_history
c374ab69 360{
ad6dec05
MV
361 char *filename;
362 SCM ret;
363
364 filename = scm_to_locale_string (file);
365 ret = scm_from_bool (!write_history (filename));
366 free (filename);
367 return ret;
c374ab69 368}
f48e47b9 369#undef FUNC_NAME
c374ab69 370
8ed35a15
MV
371SCM_DEFINE (scm_clear_history, "clear-history", 0, 0, 0,
372 (),
373 "Clear the history buffer of the readline machinery.")
374#define FUNC_NAME s_scm_clear_history
375{
376 clear_history();
377 return SCM_UNSPECIFIED;
378}
379#undef FUNC_NAME
380
c374ab69 381
b916d813 382SCM_DEFINE (scm_filename_completion_function, "filename-completion-function", 2, 0, 0,
f48e47b9
GB
383 (SCM text, SCM continuep),
384"")
385#define FUNC_NAME s_scm_filename_completion_function
c374ab69
MV
386{
387 char *s;
388 SCM ans;
ad6dec05 389 char *c_text = scm_to_locale_string (text);
dcb17187 390#ifdef HAVE_RL_FILENAME_COMPLETION_FUNCTION
ad6dec05 391 s = rl_filename_completion_function (c_text, scm_is_true (continuep));
dcb17187 392#else
ad6dec05 393 s = filename_completion_function (c_text, scm_is_true (continuep));
dcb17187 394#endif
ad6dec05
MV
395 ans = scm_take_locale_string (s);
396 free (c_text);
c374ab69
MV
397 return ans;
398}
f48e47b9 399#undef FUNC_NAME
c374ab69
MV
400
401/*
402 * The following has been modified from code contributed by
403 * Andrew Archibald <aarchiba@undergrad.math.uwaterloo.ca>
404 */
405
406SCM scm_readline_completion_function_var;
407
408static char *
409completion_function (char *text, int continuep)
410{
296ff5e7 411 SCM compfunc = SCM_VARIABLE_REF (scm_readline_completion_function_var);
c374ab69
MV
412 SCM res;
413
be49d1df 414 if (scm_is_false (compfunc))
c374ab69
MV
415 return NULL; /* #f => completion disabled */
416 else
417 {
ad6dec05 418 SCM t = scm_from_locale_string (text);
be49d1df 419 SCM c = scm_from_bool (continuep);
5b2a7b59 420 res = scm_apply (compfunc, scm_list_2 (t, c), SCM_EOL);
c374ab69 421
be49d1df 422 if (scm_is_false (res))
c374ab69
MV
423 return NULL;
424
ad6dec05 425 return scm_to_locale_string (res);
c374ab69
MV
426 }
427}
428
429/*Bouncing parenthesis (reimplemented by GH, 11/23/98, since readline is strict gpl)*/
430
576cdec4
MD
431static int match_paren (int x, int k);
432static int find_matching_paren (int k);
433static void init_bouncing_parens ();
c374ab69
MV
434
435static void
576cdec4 436init_bouncing_parens ()
c374ab69 437{
576cdec4
MD
438 if (strncmp (rl_get_keymap_name (rl_get_keymap ()), "vi", 2))
439 {
440 rl_bind_key (')', match_paren);
441 rl_bind_key (']', match_paren);
442 rl_bind_key ('}', match_paren);
443 }
c374ab69
MV
444}
445
446static int
447find_matching_paren(int k)
448{
449 register int i;
450 register char c = 0;
451 int end_parens_found = 0;
452
453 /* Choose the corresponding opening bracket. */
454 if (k == ')') c = '(';
455 else if (k == ']') c = '[';
456 else if (k == '}') c = '{';
457
458 for (i=rl_point-2; i>=0; i--)
459 {
460 /* Is the current character part of a character literal? */
461 if (i - 2 >= 0
462 && rl_line_buffer[i - 1] == '\\'
463 && rl_line_buffer[i - 2] == '#')
464 ;
465 else if (rl_line_buffer[i] == k)
466 end_parens_found++;
467 else if (rl_line_buffer[i] == '"')
468 {
469 /* Skip over a string literal. */
470 for (i--; i >= 0; i--)
471 if (rl_line_buffer[i] == '"'
472 && ! (i - 1 >= 0
473 && rl_line_buffer[i - 1] == '\\'))
474 break;
475 }
476 else if (rl_line_buffer[i] == c)
477 {
576cdec4
MD
478 if (end_parens_found==0)
479 return i;
c374ab69
MV
480 else --end_parens_found;
481 }
482 }
483 return -1;
484}
485
576cdec4
MD
486static int
487match_paren (int x, int k)
c374ab69 488{
8f99e3f3
SJ
489 int tmp;
490#ifndef __MINGW32__
491 int fno;
aba2031a 492 SELECT_TYPE readset;
c374ab69 493 struct timeval timeout;
8f99e3f3
SJ
494#endif
495
576cdec4 496 rl_insert (x, k);
c374ab69 497 if (!SCM_READLINE_BOUNCE_PARENS)
576cdec4 498 return 0;
c374ab69
MV
499
500 /* Did we just insert a quoted paren? If so, then don't bounce. */
501 if (rl_point - 1 >= 1
502 && rl_line_buffer[rl_point - 2] == '\\')
576cdec4 503 return 0;
c374ab69 504
8f99e3f3 505#ifndef __MINGW32__
c374ab69
MV
506 tmp = 1000 * SCM_READLINE_BOUNCE_PARENS;
507 timeout.tv_sec = tmp / 1000000;
508 timeout.tv_usec = tmp % 1000000;
576cdec4 509 FD_ZERO (&readset);
bc858b80
MD
510 fno = fileno (rl_instream);
511 FD_SET (fno, &readset);
8f99e3f3
SJ
512#endif
513
576cdec4
MD
514 if (rl_point > 1)
515 {
516 tmp = rl_point;
517 rl_point = find_matching_paren (k);
518 if (rl_point > -1)
519 {
520 rl_redisplay ();
8f99e3f3 521#ifndef __MINGW32__
0ddf47fc 522 scm_std_select (fno + 1, &readset, NULL, NULL, &timeout);
8f99e3f3
SJ
523#else
524 WaitForSingleObject (GetStdHandle(STD_INPUT_HANDLE),
525 SCM_READLINE_BOUNCE_PARENS);
526#endif
576cdec4
MD
527 }
528 rl_point = tmp;
c374ab69 529 }
576cdec4 530 return 0;
c374ab69
MV
531}
532
b71099ba
MD
533#if defined (HAVE_RL_PRE_INPUT_HOOK) && defined (GUILE_SIGWINCH_SA_RESTART_CLEARED)
534/* Readline disables SA_RESTART on SIGWINCH.
535 * This code turns it back on.
536 */
537static int
538sigwinch_enable_restart (void)
539{
540#ifdef HAVE_SIGINTERRUPT
541 siginterrupt (SIGWINCH, 0);
542#else
543 struct sigaction action;
544
545 sigaction (SIGWINCH, NULL, &action);
546 action.sa_flags |= SA_RESTART;
547 sigaction (SIGWINCH, &action, NULL);
548#endif
549 return 0;
550}
551#endif
552
26047451
MD
553#endif /* HAVE_RL_GETC_FUNCTION */
554
c374ab69
MV
555void
556scm_init_readline ()
557{
62947883 558#ifdef HAVE_RL_GETC_FUNCTION
a0599745 559#include "guile-readline/readline.x"
c374ab69 560 scm_readline_completion_function_var
296ff5e7 561 = scm_c_define ("*readline-completion-function*", SCM_BOOL_F);
8f99e3f3 562#ifndef __MINGW32__
c374ab69 563 rl_getc_function = current_input_getc;
8f99e3f3 564#endif
dcb17187
MV
565#if defined (_RL_FUNCTION_TYPEDEF)
566 rl_completion_entry_function = (rl_compentry_func_t*) completion_function;
567#else
c374ab69 568 rl_completion_entry_function = (Function*) completion_function;
dcb17187 569#endif
c374ab69 570 rl_basic_word_break_characters = "\t\n\"'`;()";
5c11cc9d 571 rl_readline_name = "Guile";
b71099ba
MD
572#if defined (HAVE_RL_PRE_INPUT_HOOK) && defined (GUILE_SIGWINCH_SA_RESTART_CLEARED)
573 rl_pre_input_hook = sigwinch_enable_restart;
574#endif
5c11cc9d 575
bb0f37e7 576 reentry_barrier_mutex = scm_permanent_object (scm_make_mutex ());
c374ab69
MV
577 scm_init_opts (scm_readline_options,
578 scm_readline_opts,
579 SCM_N_READLINE_OPTIONS);
580 init_bouncing_parens();
581 scm_add_feature ("readline");
62947883 582#endif /* HAVE_RL_GETC_FUNCTION */
c374ab69
MV
583}
584
89e00824
ML
585/*
586 Local Variables:
587 c-file-style: "gnu"
588 End:
589*/