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