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, 675 Mass Ave, Cambridge, MA 02139, USA.
22 #if defined (HAVE_RL_GETC_FUNCTION)
26 #include <readline/readline.h>
27 #include <readline/history.h>
30 #include <libguile/iselect.h>
33 scm_option scm_readline_opts
[] = {
34 { SCM_OPTION_BOOLEAN
, "history-file", 1,
35 "Use history file." },
36 { SCM_OPTION_INTEGER
, "history-length", 200,
38 { SCM_OPTION_INTEGER
, "bounce-parens", 500,
39 "Time (ms) to show matching opening parenthesis (0 = off)."}
42 extern void stifle_history (int max
);
44 SCM_PROC (s_readline_options
, "readline-options-interface", 0, 1, 0, scm_readline_options
);
47 scm_readline_options (setting
)
50 SCM ans
= scm_options (setting
,
52 SCM_N_READLINE_OPTIONS
,
54 stifle_history (SCM_HISTORY_LENGTH
);
63 char *new = malloc (len
+ 1);
67 #endif /* HAVE_STRDUP */
69 #ifndef HAVE_RL_CLEANUP_AFTER_SIGNAL
71 /* These are readline functions added in release 2.3. They will work
72 * together with readline-2.1 and 2.2. (The readline interface is
73 * disabled for earlier releases.)
74 * They are declared static; if we want to use them elsewhere, then
75 * we need external declarations for them, but at the moment, I don't
76 * think anything else in Guile ought to use these.
79 extern void _rl_clean_up_for_exit ();
80 extern void _rl_kill_kbd_macro ();
81 extern int _rl_init_argument ();
84 rl_cleanup_after_signal ()
86 #ifdef HAVE_RL_CLEAR_SIGNALS
87 _rl_clean_up_for_exit ();
89 (*rl_deprep_term_function
) ();
90 #ifdef HAVE_RL_CLEAR_SIGNALS
99 register HIST_ENTRY
*entry
;
103 entry
= current_history ();
105 entry
->data
= (char *)NULL
;
107 _rl_kill_kbd_macro ();
109 _rl_init_argument ();
112 #endif /* !HAVE_RL_CLEANUP_AFTER_SIGNAL */
115 static SCM input_port
;
116 static SCM before_read
;
119 current_input_getc (FILE *in
)
122 if (promptp
&& SCM_NIMP (before_read
))
124 scm_apply (before_read
, SCM_EOL
, SCM_EOL
);
127 ans
= scm_getc (input_port
);
138 SCM_PROC (s_readline
, "readline", 0, 4, 0, scm_readline
);
140 static int in_readline
= 0;
142 static scm_mutex_t reentry_barrier_mutex
;
150 /* We should rather use scm_mutex_try_lock when it becomes available */
151 scm_mutex_lock (&reentry_barrier_mutex
);
158 scm_mutex_unlock (&reentry_barrier_mutex
);
161 scm_misc_error (s_readline
, "readline is not reentrant", SCM_EOL
);
165 handle_error (void *data
, SCM tag
, SCM args
)
167 rl_free_line_state ();
168 rl_cleanup_after_signal ();
169 fclose (rl_instream
);
170 fclose (rl_outstream
);
172 scm_handle_by_throw (data
, tag
, args
);
173 return SCM_UNSPECIFIED
; /* never reached */
177 internal_readline (SCM text
)
181 char *prompt
= SCM_UNBNDP (text
) ? "" : SCM_CHARS (text
);
184 s
= readline (prompt
);
186 ret
= scm_makfrom0str (s
);
196 stream_from_fport (SCM port
, char *mode
, const char *subr
)
201 fd
= dup (((struct scm_fport
*) SCM_STREAM (port
))->fdes
);
208 f
= fdopen (fd
, mode
);
219 scm_readline (SCM text
, SCM inp
, SCM outp
, SCM read_hook
)
225 before_read
= SCM_BOOL_F
;
227 if (!SCM_UNBNDP (text
))
229 if (!(SCM_NIMP (text
) && SCM_STRINGP (text
)))
232 scm_wrong_type_arg (s_readline
, SCM_ARG1
, text
);
234 SCM_COERCE_SUBSTR (text
);
237 if (SCM_UNBNDP (inp
))
240 if (SCM_UNBNDP (outp
))
243 if (!(SCM_UNBNDP (read_hook
) || SCM_FALSEP (read_hook
)))
245 if (!(SCM_NFALSEP (scm_thunk_p (read_hook
))))
248 scm_wrong_type_arg (s_readline
, SCM_ARG4
, read_hook
);
250 before_read
= read_hook
;
253 if (!(SCM_NIMP (inp
) && SCM_OPINFPORTP (inp
)))
256 scm_misc_error (s_readline
,
257 "Input port is not open or not a file port",
260 if (!(SCM_NIMP (outp
) && SCM_OPOUTFPORTP (outp
)))
263 scm_misc_error (s_readline
,
264 "Output port is not open or not a file port",
269 rl_instream
= stream_from_fport (inp
, "r", s_readline
);
270 rl_outstream
= stream_from_fport (outp
, "w", s_readline
);
272 ans
= scm_internal_catch (SCM_BOOL_T
,
273 (scm_catch_body_t
) internal_readline
,
277 fclose (rl_instream
);
278 fclose (rl_outstream
);
284 SCM_PROC (s_add_history
, "add-history", 1, 0, 0, scm_add_history
);
287 scm_add_history (SCM text
)
290 SCM_ASSERT ((SCM_NIMP(text
) && SCM_STRINGP(text
)), text
, SCM_ARG1
,
292 SCM_COERCE_SUBSTR (text
);
294 s
= SCM_CHARS (text
);
295 add_history (strdup (s
));
297 return SCM_UNSPECIFIED
;
301 SCM_PROC (s_read_history
, "read-history", 1, 0, 0, scm_read_history
);
304 scm_read_history (SCM file
)
306 SCM_ASSERT (SCM_NIMP (file
) && SCM_STRINGP (file
),
307 file
, SCM_ARG1
, s_read_history
);
308 return read_history (SCM_ROCHARS (file
)) ? SCM_BOOL_F
: SCM_BOOL_T
;
312 SCM_PROC (s_write_history
, "write-history", 1, 0, 0, scm_write_history
);
315 scm_write_history (SCM file
)
317 SCM_ASSERT (SCM_NIMP (file
) && SCM_STRINGP (file
),
318 file
, SCM_ARG1
, s_write_history
);
319 return write_history (SCM_ROCHARS (file
)) ? SCM_BOOL_F
: SCM_BOOL_T
;
323 SCM_PROC (s_filename_completion_function
, "filename-completion-function", 2, 0, 0, scm_filename_completion_function
);
326 scm_filename_completion_function (SCM text
, SCM continuep
)
330 SCM_ASSERT (SCM_NIMP (text
) && SCM_STRINGP (text
),
333 s_filename_completion_function
);
334 SCM_COERCE_SUBSTR (text
);
335 s
= filename_completion_function (SCM_CHARS (text
), SCM_NFALSEP (continuep
));
336 ans
= scm_makfrom0str (s
);
342 * The following has been modified from code contributed by
343 * Andrew Archibald <aarchiba@undergrad.math.uwaterloo.ca>
346 SCM scm_readline_completion_function_var
;
349 completion_function (char *text
, int continuep
)
351 SCM compfunc
= SCM_CDR (scm_readline_completion_function_var
);
354 if (SCM_FALSEP (compfunc
))
355 return NULL
; /* #f => completion disabled */
358 SCM t
= scm_makfrom0str (text
);
359 SCM c
= continuep
? SCM_BOOL_T
: SCM_BOOL_F
;
360 res
= scm_apply (compfunc
, SCM_LIST2 (t
, c
), SCM_EOL
);
362 if (SCM_FALSEP (res
))
365 if (!(SCM_NIMP (res
) && SCM_STRINGP (res
)))
366 scm_misc_error (s_readline
,
367 "Completion function returned bogus value: %S",
369 SCM_COERCE_SUBSTR (res
);
370 return strdup (SCM_CHARS (res
));
374 /*Bouncing parenthesis (reimplemented by GH, 11/23/98, since readline is strict gpl)*/
376 static void match_paren(int x
, int k
);
377 static int find_matching_paren(int k
);
378 static void init_bouncing_parens();
381 init_bouncing_parens()
383 if(strncmp(rl_get_keymap_name(rl_get_keymap()), "vi", 2)) {
384 rl_bind_key(')', match_paren
);
385 rl_bind_key(']', match_paren
);
386 rl_bind_key('}', match_paren
);
391 find_matching_paren(int k
)
395 int end_parens_found
= 0;
397 /* Choose the corresponding opening bracket. */
398 if (k
== ')') c
= '(';
399 else if (k
== ']') c
= '[';
400 else if (k
== '}') c
= '{';
402 for (i
=rl_point
-2; i
>=0; i
--)
404 /* Is the current character part of a character literal? */
406 && rl_line_buffer
[i
- 1] == '\\'
407 && rl_line_buffer
[i
- 2] == '#')
409 else if (rl_line_buffer
[i
] == k
)
411 else if (rl_line_buffer
[i
] == '"')
413 /* Skip over a string literal. */
414 for (i
--; i
>= 0; i
--)
415 if (rl_line_buffer
[i
] == '"'
417 && rl_line_buffer
[i
- 1] == '\\'))
420 else if (rl_line_buffer
[i
] == c
)
422 if (end_parens_found
==0) return i
;
423 else --end_parens_found
;
430 match_paren(int x
, int k
)
434 struct timeval timeout
;
437 if (!SCM_READLINE_BOUNCE_PARENS
)
440 /* Did we just insert a quoted paren? If so, then don't bounce. */
441 if (rl_point
- 1 >= 1
442 && rl_line_buffer
[rl_point
- 2] == '\\')
445 tmp
= 1000 * SCM_READLINE_BOUNCE_PARENS
;
446 timeout
.tv_sec
= tmp
/ 1000000;
447 timeout
.tv_usec
= tmp
% 1000000;
449 FD_SET(fileno(rl_instream
), &readset
);
453 rl_point
= find_matching_paren(k
);
456 scm_internal_select(1, &readset
, NULL
, NULL
, &timeout
);
466 #include "readline.x"
467 scm_readline_completion_function_var
468 = scm_sysintern ("*readline-completion-function*", SCM_BOOL_F
);
469 rl_getc_function
= current_input_getc
;
470 rl_redisplay_function
= redisplay
;
471 rl_completion_entry_function
= (Function
*) completion_function
;
472 rl_basic_word_break_characters
= "\t\n\"'`;()";
474 scm_mutex_init (&reentry_barrier_mutex
);
476 scm_init_opts (scm_readline_options
,
478 SCM_N_READLINE_OPTIONS
);
479 init_bouncing_parens();
480 scm_add_feature ("readline");