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