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