1 /* readline.c --- line editing support for Guile */
3 /* Copyright (C) 1997,1999 Free Software Foundation, Inc.
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)
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.
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
17 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307 USA
23 #include "libguile/_scm.h"
24 #if defined (HAVE_RL_GETC_FUNCTION)
25 #include "libguile/libguile.h"
26 #include "libguile/gh.h"
27 #include "libguile/iselect.h"
29 #include <readline/readline.h>
30 #include <readline/history.h>
35 scm_option scm_readline_opts
[] = {
36 { SCM_OPTION_BOOLEAN
, "history-file", 1,
37 "Use history file." },
38 { SCM_OPTION_INTEGER
, "history-length", 200,
40 { SCM_OPTION_INTEGER
, "bounce-parens", 500,
41 "Time (ms) to show matching opening parenthesis (0 = off)."}
44 extern void stifle_history (int max
);
46 SCM_PROC (s_readline_options
, "readline-options-interface", 0, 1, 0, scm_readline_options
);
49 scm_readline_options (setting
)
52 SCM ans
= scm_options (setting
,
54 SCM_N_READLINE_OPTIONS
,
56 stifle_history (SCM_HISTORY_LENGTH
);
65 char *new = malloc (len
+ 1);
69 #endif /* HAVE_STRDUP */
71 #ifndef HAVE_RL_CLEANUP_AFTER_SIGNAL
73 /* These are readline functions added in release 2.3. They will work
74 * together with readline-2.1 and 2.2. (The readline interface is
75 * disabled for earlier releases.)
76 * They are declared static; if we want to use them elsewhere, then
77 * we need external declarations for them, but at the moment, I don't
78 * think anything else in Guile ought to use these.
81 extern void _rl_clean_up_for_exit ();
82 extern void _rl_kill_kbd_macro ();
83 extern int _rl_init_argument ();
86 rl_cleanup_after_signal ()
88 #ifdef HAVE_RL_CLEAR_SIGNALS
89 _rl_clean_up_for_exit ();
91 (*rl_deprep_term_function
) ();
92 #ifdef HAVE_RL_CLEAR_SIGNALS
101 register HIST_ENTRY
*entry
;
105 entry
= current_history ();
107 entry
->data
= (char *)NULL
;
109 _rl_kill_kbd_macro ();
111 _rl_init_argument ();
114 #endif /* !HAVE_RL_CLEANUP_AFTER_SIGNAL */
117 static SCM input_port
;
118 static SCM before_read
;
121 current_input_getc (FILE *in
)
124 if (promptp
&& SCM_NIMP (before_read
))
126 scm_apply (before_read
, SCM_EOL
, SCM_EOL
);
129 ans
= scm_getc (input_port
);
140 SCM_PROC (s_readline
, "%readline", 0, 4, 0, scm_readline
);
142 static int in_readline
= 0;
144 static scm_mutex_t reentry_barrier_mutex
;
152 /* We should rather use scm_mutex_try_lock when it becomes available */
153 scm_mutex_lock (&reentry_barrier_mutex
);
160 scm_mutex_unlock (&reentry_barrier_mutex
);
163 scm_misc_error (s_readline
, "readline is not reentrant", SCM_EOL
);
167 handle_error (void *data
, SCM tag
, SCM args
)
169 rl_free_line_state ();
170 rl_cleanup_after_signal ();
171 fputc ('\n', rl_outstream
); /* We don't want next output on this line */
172 fclose (rl_instream
);
173 fclose (rl_outstream
);
175 scm_handle_by_throw (data
, tag
, args
);
176 return SCM_UNSPECIFIED
; /* never reached */
180 internal_readline (SCM text
)
184 char *prompt
= SCM_UNBNDP (text
) ? "" : SCM_CHARS (text
);
187 s
= readline (prompt
);
189 ret
= scm_makfrom0str (s
);
199 stream_from_fport (SCM port
, char *mode
, const char *subr
)
204 fd
= dup (((struct scm_fport
*) SCM_STREAM (port
))->fdes
);
211 f
= fdopen (fd
, mode
);
222 scm_readline_init_ports (SCM inp
, SCM outp
)
224 if (SCM_UNBNDP (inp
))
227 if (SCM_UNBNDP (outp
))
230 if (!(SCM_NIMP (inp
) && SCM_OPINFPORTP (inp
))) {
232 "Input port is not open or not a file port",
236 if (!(SCM_NIMP (outp
) && SCM_OPOUTFPORTP (outp
))) {
238 "Output port is not open or not a file port",
243 rl_instream
= stream_from_fport (inp
, "r", s_readline
);
244 rl_outstream
= stream_from_fport (outp
, "w", s_readline
);
248 scm_readline (SCM text
, SCM inp
, SCM outp
, SCM read_hook
)
254 before_read
= SCM_BOOL_F
;
256 if (!SCM_UNBNDP (text
))
258 if (!(SCM_NIMP (text
) && SCM_STRINGP (text
)))
261 scm_wrong_type_arg (s_readline
, SCM_ARG1
, text
);
263 SCM_COERCE_SUBSTR (text
);
266 if (!((SCM_UNBNDP (inp
) && SCM_NIMP (scm_cur_inp
) && SCM_OPINFPORTP (inp
))
267 || SCM_NIMP (inp
) && SCM_OPINFPORTP (inp
)))
270 scm_misc_error (s_readline
,
271 "Input port is not open or not a file port",
275 if (!((SCM_UNBNDP (outp
) && SCM_NIMP (scm_cur_outp
) && SCM_OPINFPORTP (outp
))
276 || (SCM_NIMP (outp
) && SCM_OPOUTFPORTP (outp
))))
279 scm_misc_error (s_readline
,
280 "Output port is not open or not a file port",
284 if (!(SCM_UNBNDP (read_hook
) || SCM_FALSEP (read_hook
)))
286 if (!(SCM_NFALSEP (scm_thunk_p (read_hook
))))
289 scm_wrong_type_arg (s_readline
, SCM_ARG4
, read_hook
);
291 before_read
= read_hook
;
294 scm_readline_init_ports (inp
, outp
);
296 ans
= scm_internal_catch (SCM_BOOL_T
,
297 (scm_catch_body_t
) internal_readline
,
301 fclose (rl_instream
);
302 fclose (rl_outstream
);
308 SCM_PROC (s_add_history
, "add-history", 1, 0, 0, scm_add_history
);
311 scm_add_history (SCM text
)
314 SCM_ASSERT ((SCM_NIMP(text
) && SCM_STRINGP(text
)), text
, SCM_ARG1
,
316 SCM_COERCE_SUBSTR (text
);
318 s
= SCM_CHARS (text
);
319 add_history (strdup (s
));
321 return SCM_UNSPECIFIED
;
325 SCM_PROC (s_read_history
, "read-history", 1, 0, 0, scm_read_history
);
328 scm_read_history (SCM file
)
330 SCM_ASSERT (SCM_NIMP (file
) && SCM_STRINGP (file
),
331 file
, SCM_ARG1
, s_read_history
);
332 return read_history (SCM_ROCHARS (file
)) ? SCM_BOOL_F
: SCM_BOOL_T
;
336 SCM_PROC (s_write_history
, "write-history", 1, 0, 0, scm_write_history
);
339 scm_write_history (SCM file
)
341 SCM_ASSERT (SCM_NIMP (file
) && SCM_STRINGP (file
),
342 file
, SCM_ARG1
, s_write_history
);
343 return write_history (SCM_ROCHARS (file
)) ? SCM_BOOL_F
: SCM_BOOL_T
;
347 SCM_PROC (s_filename_completion_function
, "filename-completion-function", 2, 0, 0, scm_filename_completion_function
);
350 scm_filename_completion_function (SCM text
, SCM continuep
)
354 SCM_ASSERT (SCM_NIMP (text
) && SCM_STRINGP (text
),
357 s_filename_completion_function
);
358 SCM_COERCE_SUBSTR (text
);
359 s
= filename_completion_function (SCM_CHARS (text
), SCM_NFALSEP (continuep
));
360 ans
= scm_makfrom0str (s
);
366 * The following has been modified from code contributed by
367 * Andrew Archibald <aarchiba@undergrad.math.uwaterloo.ca>
370 SCM scm_readline_completion_function_var
;
373 completion_function (char *text
, int continuep
)
375 SCM compfunc
= SCM_CDR (scm_readline_completion_function_var
);
378 if (SCM_FALSEP (compfunc
))
379 return NULL
; /* #f => completion disabled */
382 SCM t
= scm_makfrom0str (text
);
383 SCM c
= continuep
? SCM_BOOL_T
: SCM_BOOL_F
;
384 res
= scm_apply (compfunc
, SCM_LIST2 (t
, c
), SCM_EOL
);
386 if (SCM_FALSEP (res
))
389 if (!(SCM_NIMP (res
) && SCM_STRINGP (res
)))
390 scm_misc_error (s_readline
,
391 "Completion function returned bogus value: %S",
393 SCM_COERCE_SUBSTR (res
);
394 return strdup (SCM_CHARS (res
));
398 /*Bouncing parenthesis (reimplemented by GH, 11/23/98, since readline is strict gpl)*/
400 static void match_paren(int x
, int k
);
401 static int find_matching_paren(int k
);
402 static void init_bouncing_parens();
405 init_bouncing_parens()
407 if(strncmp(rl_get_keymap_name(rl_get_keymap()), "vi", 2)) {
408 rl_bind_key(')', match_paren
);
409 rl_bind_key(']', match_paren
);
410 rl_bind_key('}', match_paren
);
415 find_matching_paren(int k
)
419 int end_parens_found
= 0;
421 /* Choose the corresponding opening bracket. */
422 if (k
== ')') c
= '(';
423 else if (k
== ']') c
= '[';
424 else if (k
== '}') c
= '{';
426 for (i
=rl_point
-2; i
>=0; i
--)
428 /* Is the current character part of a character literal? */
430 && rl_line_buffer
[i
- 1] == '\\'
431 && rl_line_buffer
[i
- 2] == '#')
433 else if (rl_line_buffer
[i
] == k
)
435 else if (rl_line_buffer
[i
] == '"')
437 /* Skip over a string literal. */
438 for (i
--; i
>= 0; i
--)
439 if (rl_line_buffer
[i
] == '"'
441 && rl_line_buffer
[i
- 1] == '\\'))
444 else if (rl_line_buffer
[i
] == c
)
446 if (end_parens_found
==0) return i
;
447 else --end_parens_found
;
454 match_paren(int x
, int k
)
458 struct timeval timeout
;
461 if (!SCM_READLINE_BOUNCE_PARENS
)
464 /* Did we just insert a quoted paren? If so, then don't bounce. */
465 if (rl_point
- 1 >= 1
466 && rl_line_buffer
[rl_point
- 2] == '\\')
469 tmp
= 1000 * SCM_READLINE_BOUNCE_PARENS
;
470 timeout
.tv_sec
= tmp
/ 1000000;
471 timeout
.tv_usec
= tmp
% 1000000;
473 FD_SET(fileno(rl_instream
), &readset
);
477 rl_point
= find_matching_paren(k
);
480 scm_internal_select(1, &readset
, NULL
, NULL
, &timeout
);
490 #include "readline.x"
491 scm_readline_completion_function_var
492 = scm_sysintern ("*readline-completion-function*", SCM_BOOL_F
);
493 rl_getc_function
= current_input_getc
;
494 rl_redisplay_function
= redisplay
;
495 rl_completion_entry_function
= (Function
*) completion_function
;
496 rl_basic_word_break_characters
= "\t\n\"'`;()";
497 rl_readline_name
= "Guile";
500 scm_mutex_init (&reentry_barrier_mutex
);
502 scm_init_opts (scm_readline_options
,
504 SCM_N_READLINE_OPTIONS
);
505 init_bouncing_parens();
506 scm_add_feature ("readline");