* configure.in: Added test if readline clears SA_RESTART flag for
[bpt/guile.git] / guile-readline / readline.c
CommitLineData
c374ab69
MV
1/* readline.c --- line editing support for Guile */
2
576cdec4 3/* Copyright (C) 1997,1999, 2000 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
GB
21
22/* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
23 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
24
c374ab69
MV
25\f
26
739b3bf1 27#include "libguile/_scm.h"
62947883 28#ifdef HAVE_RL_GETC_FUNCTION
fbf68f8b 29#include "libguile.h"
739b3bf1
MD
30#include "libguile/gh.h"
31#include "libguile/iselect.h"
32
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>
c374ab69 38#include <sys/time.h>
c374ab69 39
1c537018 40#include "libguile/validate.h"
a0599745 41#include "guile-readline/readline.h"
c374ab69
MV
42
43scm_option scm_readline_opts[] = {
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{
72 int len = strlen (s);
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
129current_input_getc (FILE *in)
130{
c374ab69
MV
131 if (promptp && SCM_NIMP (before_read))
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
148static scm_mutex_t reentry_barrier_mutex;
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 {
169 if (!(SCM_NIMP (text) && SCM_STRINGP (text)))
170 {
171 --in_readline;
172 scm_wrong_type_arg (s_scm_readline, SCM_ARG1, text);
173 }
174 SCM_COERCE_SUBSTR (text);
175 }
176
177 if (!((SCM_UNBNDP (inp) && SCM_NIMP (scm_cur_inp) && SCM_OPINFPORTP (inp))
48552b1d 178 || (SCM_NIMP (inp) && SCM_OPINFPORTP (inp))))
f48e47b9
GB
179 {
180 --in_readline;
181 scm_misc_error (s_scm_readline,
182 "Input port is not open or not a file port",
183 SCM_EOL);
184 }
185
186 if (!((SCM_UNBNDP (outp) && SCM_NIMP (scm_cur_outp) && SCM_OPINFPORTP (outp))
187 || (SCM_NIMP (outp) && SCM_OPOUTFPORTP (outp))))
188 {
189 --in_readline;
190 scm_misc_error (s_scm_readline,
191 "Output port is not open or not a file port",
192 SCM_EOL);
193 }
194
195 if (!(SCM_UNBNDP (read_hook) || SCM_FALSEP (read_hook)))
196 {
197 if (!(SCM_NFALSEP (scm_thunk_p (read_hook))))
198 {
199 --in_readline;
200 scm_wrong_type_arg (s_scm_readline, SCM_ARG4, read_hook);
201 }
202 before_read = read_hook;
203 }
204
205 scm_readline_init_ports (inp, outp);
206
207 ans = scm_internal_catch (SCM_BOOL_T,
208 (scm_catch_body_t) internal_readline,
f60d011a 209 (void *) SCM_UNPACK (text),
f48e47b9
GB
210 handle_error, 0);
211
212 fclose (rl_instream);
213 fclose (rl_outstream);
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;
225#ifdef USE_THREADS
226 /* We should rather use scm_mutex_try_lock when it becomes available */
227 scm_mutex_lock (&reentry_barrier_mutex);
228#endif
229 if (in_readline)
230 reentryp = 1;
231 else
232 ++in_readline;
233#ifdef USE_THREADS
234 scm_mutex_unlock (&reentry_barrier_mutex);
235#endif
236 if (reentryp)
f48e47b9 237 scm_misc_error (s_scm_readline, "readline is not reentrant", SCM_EOL);
c374ab69
MV
238}
239
240static SCM
241handle_error (void *data, SCM tag, SCM args)
242{
243 rl_free_line_state ();
244 rl_cleanup_after_signal ();
739b3bf1 245 fputc ('\n', rl_outstream); /* We don't want next output on this line */
c374ab69
MV
246 fclose (rl_instream);
247 fclose (rl_outstream);
248 --in_readline;
249 scm_handle_by_throw (data, tag, args);
250 return SCM_UNSPECIFIED; /* never reached */
251}
252
253static SCM
254internal_readline (SCM text)
255{
256 SCM ret;
257 char *s;
258 char *prompt = SCM_UNBNDP (text) ? "" : SCM_CHARS (text);
259
260 promptp = 1;
261 s = readline (prompt);
262 if (s)
263 ret = scm_makfrom0str (s);
264 else
265 ret = SCM_EOF_VAL;
266
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
278 fd = dup (((struct scm_fport *) SCM_STREAM (port))->fdes);
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))
299 inp = scm_cur_inp;
300
301 if (SCM_UNBNDP (outp))
302 outp = scm_cur_outp;
303
304 if (!(SCM_NIMP (inp) && SCM_OPINFPORTP (inp))) {
305 scm_misc_error (0,
306 "Input port is not open or not a file port",
307 SCM_EOL);
308 }
309
310 if (!(SCM_NIMP (outp) && SCM_OPOUTFPORTP (outp))) {
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;
f48e47b9
GB
317 rl_instream = stream_from_fport (inp, "r", s_scm_readline);
318 rl_outstream = stream_from_fport (outp, "w", s_scm_readline);
2e3d5987
MD
319}
320
c374ab69 321
c374ab69 322
b916d813 323SCM_DEFINE (scm_add_history, "add-history", 1, 0, 0,
f48e47b9
GB
324 (SCM text),
325"")
326#define FUNC_NAME s_scm_add_history
c374ab69
MV
327{
328 char* s;
cbaee92a 329 SCM_VALIDATE_STRING (1,text);
c374ab69
MV
330 SCM_COERCE_SUBSTR (text);
331
332 s = SCM_CHARS (text);
333 add_history (strdup (s));
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{
cbaee92a 345 SCM_VALIDATE_STRING (1,file);
f48e47b9 346 return SCM_NEGATE_BOOL(read_history (SCM_ROCHARS (file)));
c374ab69 347}
f48e47b9 348#undef FUNC_NAME
c374ab69
MV
349
350
b916d813 351SCM_DEFINE (scm_write_history, "write-history", 1, 0, 0,
f48e47b9
GB
352 (SCM file),
353"")
354#define FUNC_NAME s_scm_write_history
c374ab69 355{
cbaee92a 356 SCM_VALIDATE_STRING (1,file);
f48e47b9 357 return SCM_NEGATE_BOOL(write_history (SCM_ROCHARS (file)));
c374ab69 358}
f48e47b9 359#undef FUNC_NAME
c374ab69
MV
360
361
b916d813 362SCM_DEFINE (scm_filename_completion_function, "filename-completion-function", 2, 0, 0,
f48e47b9
GB
363 (SCM text, SCM continuep),
364"")
365#define FUNC_NAME s_scm_filename_completion_function
c374ab69
MV
366{
367 char *s;
368 SCM ans;
cbaee92a 369 SCM_VALIDATE_STRING (1,text);
c374ab69
MV
370 SCM_COERCE_SUBSTR (text);
371 s = filename_completion_function (SCM_CHARS (text), SCM_NFALSEP (continuep));
372 ans = scm_makfrom0str (s);
373 free (s);
374 return ans;
375}
f48e47b9 376#undef FUNC_NAME
c374ab69
MV
377
378/*
379 * The following has been modified from code contributed by
380 * Andrew Archibald <aarchiba@undergrad.math.uwaterloo.ca>
381 */
382
383SCM scm_readline_completion_function_var;
384
385static char *
386completion_function (char *text, int continuep)
387{
388 SCM compfunc = SCM_CDR (scm_readline_completion_function_var);
389 SCM res;
390
391 if (SCM_FALSEP (compfunc))
392 return NULL; /* #f => completion disabled */
393 else
394 {
395 SCM t = scm_makfrom0str (text);
396 SCM c = continuep ? SCM_BOOL_T : SCM_BOOL_F;
397 res = scm_apply (compfunc, SCM_LIST2 (t, c), SCM_EOL);
398
399 if (SCM_FALSEP (res))
400 return NULL;
401
402 if (!(SCM_NIMP (res) && SCM_STRINGP (res)))
f48e47b9 403 scm_misc_error (s_scm_readline,
c374ab69
MV
404 "Completion function returned bogus value: %S",
405 SCM_LIST1 (res));
406 SCM_COERCE_SUBSTR (res);
407 return strdup (SCM_CHARS (res));
408 }
409}
410
411/*Bouncing parenthesis (reimplemented by GH, 11/23/98, since readline is strict gpl)*/
412
576cdec4
MD
413static int match_paren (int x, int k);
414static int find_matching_paren (int k);
415static void init_bouncing_parens ();
c374ab69
MV
416
417static void
576cdec4 418init_bouncing_parens ()
c374ab69 419{
576cdec4
MD
420 if (strncmp (rl_get_keymap_name (rl_get_keymap ()), "vi", 2))
421 {
422 rl_bind_key (')', match_paren);
423 rl_bind_key (']', match_paren);
424 rl_bind_key ('}', match_paren);
425 }
c374ab69
MV
426}
427
428static int
429find_matching_paren(int k)
430{
431 register int i;
432 register char c = 0;
433 int end_parens_found = 0;
434
435 /* Choose the corresponding opening bracket. */
436 if (k == ')') c = '(';
437 else if (k == ']') c = '[';
438 else if (k == '}') c = '{';
439
440 for (i=rl_point-2; i>=0; i--)
441 {
442 /* Is the current character part of a character literal? */
443 if (i - 2 >= 0
444 && rl_line_buffer[i - 1] == '\\'
445 && rl_line_buffer[i - 2] == '#')
446 ;
447 else if (rl_line_buffer[i] == k)
448 end_parens_found++;
449 else if (rl_line_buffer[i] == '"')
450 {
451 /* Skip over a string literal. */
452 for (i--; i >= 0; i--)
453 if (rl_line_buffer[i] == '"'
454 && ! (i - 1 >= 0
455 && rl_line_buffer[i - 1] == '\\'))
456 break;
457 }
458 else if (rl_line_buffer[i] == c)
459 {
576cdec4
MD
460 if (end_parens_found==0)
461 return i;
c374ab69
MV
462 else --end_parens_found;
463 }
464 }
465 return -1;
466}
467
576cdec4
MD
468static int
469match_paren (int x, int k)
c374ab69 470{
bc858b80 471 int tmp, fno;
aba2031a 472 SELECT_TYPE readset;
c374ab69
MV
473 struct timeval timeout;
474
576cdec4 475 rl_insert (x, k);
c374ab69 476 if (!SCM_READLINE_BOUNCE_PARENS)
576cdec4 477 return 0;
c374ab69
MV
478
479 /* Did we just insert a quoted paren? If so, then don't bounce. */
480 if (rl_point - 1 >= 1
481 && rl_line_buffer[rl_point - 2] == '\\')
576cdec4 482 return 0;
c374ab69
MV
483
484 tmp = 1000 * SCM_READLINE_BOUNCE_PARENS;
485 timeout.tv_sec = tmp / 1000000;
486 timeout.tv_usec = tmp % 1000000;
576cdec4 487 FD_ZERO (&readset);
bc858b80
MD
488 fno = fileno (rl_instream);
489 FD_SET (fno, &readset);
c374ab69 490
576cdec4
MD
491 if (rl_point > 1)
492 {
493 tmp = rl_point;
494 rl_point = find_matching_paren (k);
495 if (rl_point > -1)
496 {
497 rl_redisplay ();
bc858b80 498 scm_internal_select (fno + 1, &readset, NULL, NULL, &timeout);
576cdec4
MD
499 }
500 rl_point = tmp;
c374ab69 501 }
576cdec4 502 return 0;
c374ab69
MV
503}
504
62947883 505#endif /* HAVE_RL_GETC_FUNCTION */
c374ab69
MV
506
507void
508scm_init_readline ()
509{
62947883 510#ifdef HAVE_RL_GETC_FUNCTION
a0599745 511#include "guile-readline/readline.x"
c374ab69
MV
512 scm_readline_completion_function_var
513 = scm_sysintern ("*readline-completion-function*", SCM_BOOL_F);
514 rl_getc_function = current_input_getc;
515 rl_redisplay_function = redisplay;
516 rl_completion_entry_function = (Function*) completion_function;
517 rl_basic_word_break_characters = "\t\n\"'`;()";
5c11cc9d
GH
518 rl_readline_name = "Guile";
519
c374ab69 520#ifdef USE_THREADS
b74b1a63 521 scm_mutex_init (&reentry_barrier_mutex);
c374ab69
MV
522#endif
523 scm_init_opts (scm_readline_options,
524 scm_readline_opts,
525 SCM_N_READLINE_OPTIONS);
526 init_bouncing_parens();
527 scm_add_feature ("readline");
62947883 528#endif /* HAVE_RL_GETC_FUNCTION */
c374ab69
MV
529}
530
89e00824
ML
531/*
532 Local Variables:
533 c-file-style: "gnu"
534 End:
535*/