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