1 /* readline.c --- line editing support for Guile */
3 /* Copyright (C) 1997,1999,2000,2001 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
22 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
23 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
27 #include "libguile/_scm.h"
28 #ifdef HAVE_RL_GETC_FUNCTION
30 #include "libguile/gh.h"
31 #include "libguile/iselect.h"
37 #include <readline/readline.h>
38 #include <readline/history.h>
42 #include "libguile/validate.h"
43 #include "guile-readline/readline.h"
45 scm_t_option scm_readline_opts
[] = {
46 { SCM_OPTION_BOOLEAN
, "history-file", 1,
47 "Use history file." },
48 { SCM_OPTION_INTEGER
, "history-length", 200,
50 { SCM_OPTION_INTEGER
, "bounce-parens", 500,
51 "Time (ms) to show matching opening parenthesis (0 = off)."}
54 extern void stifle_history (int max
);
56 SCM_DEFINE (scm_readline_options
, "readline-options-interface", 0, 1, 0,
59 #define FUNC_NAME s_scm_readline_options
61 SCM ans
= scm_options (setting
,
63 SCM_N_READLINE_OPTIONS
,
65 stifle_history (SCM_HISTORY_LENGTH
);
74 size_t len
= strlen (s
);
75 char *new = malloc (len
+ 1);
79 #endif /* HAVE_STRDUP */
81 #ifndef HAVE_RL_CLEANUP_AFTER_SIGNAL
83 /* These are readline functions added in release 2.3. They will work
84 * together with readline-2.1 and 2.2. (The readline interface is
85 * disabled for earlier releases.)
86 * They are declared static; if we want to use them elsewhere, then
87 * we need external declarations for them, but at the moment, I don't
88 * think anything else in Guile ought to use these.
91 extern void _rl_clean_up_for_exit ();
92 extern void _rl_kill_kbd_macro ();
93 extern int _rl_init_argument ();
96 rl_cleanup_after_signal ()
98 #ifdef HAVE_RL_CLEAR_SIGNALS
99 _rl_clean_up_for_exit ();
101 (*rl_deprep_term_function
) ();
102 #ifdef HAVE_RL_CLEAR_SIGNALS
105 rl_pending_input
= 0;
109 rl_free_line_state ()
111 register HIST_ENTRY
*entry
;
115 entry
= current_history ();
117 entry
->data
= (char *)NULL
;
119 _rl_kill_kbd_macro ();
121 _rl_init_argument ();
124 #endif /* !HAVE_RL_CLEANUP_AFTER_SIGNAL */
127 static SCM input_port
;
128 static SCM before_read
;
131 current_input_getc (FILE *in SCM_UNUSED
)
133 if (promptp
&& !SCM_FALSEP (before_read
))
135 scm_apply (before_read
, SCM_EOL
, SCM_EOL
);
138 return scm_getc (input_port
);
148 static int in_readline
= 0;
150 static scm_t_mutex reentry_barrier_mutex
;
153 static SCM
internal_readline (SCM text
);
154 static SCM
handle_error (void *data
, SCM tag
, SCM args
);
155 static void reentry_barrier (void);
158 SCM_DEFINE (scm_readline
, "%readline", 0, 4, 0,
159 (SCM text
, SCM inp
, SCM outp
, SCM read_hook
),
161 #define FUNC_NAME s_scm_readline
167 before_read
= SCM_BOOL_F
;
169 if (!SCM_UNBNDP (text
))
171 if (!SCM_STRINGP (text
))
174 scm_wrong_type_arg (s_scm_readline
, SCM_ARG1
, text
);
176 SCM_STRING_COERCE_0TERMINATION_X (text
);
179 if (!((SCM_UNBNDP (inp
) && SCM_OPINFPORTP (scm_cur_inp
))
180 || SCM_OPINFPORTP (inp
)))
183 scm_misc_error (s_scm_readline
,
184 "Input port is not open or not a file port",
188 if (!((SCM_UNBNDP (outp
) && SCM_OPINFPORTP (scm_cur_outp
))
189 || SCM_OPOUTFPORTP (outp
)))
192 scm_misc_error (s_scm_readline
,
193 "Output port is not open or not a file port",
197 if (!(SCM_UNBNDP (read_hook
) || SCM_FALSEP (read_hook
)))
199 if (!(SCM_NFALSEP (scm_thunk_p (read_hook
))))
202 scm_wrong_type_arg (s_scm_readline
, SCM_ARG4
, read_hook
);
204 before_read
= read_hook
;
207 scm_readline_init_ports (inp
, outp
);
209 ans
= scm_internal_catch (SCM_BOOL_T
,
210 (scm_t_catch_body
) internal_readline
,
211 (void *) SCM_UNPACK (text
),
214 fclose (rl_instream
);
215 fclose (rl_outstream
);
228 /* We should rather use scm_t_mutexry_lock when it becomes available */
229 scm_mutex_lock (&reentry_barrier_mutex
);
236 scm_mutex_unlock (&reentry_barrier_mutex
);
239 scm_misc_error (s_scm_readline
, "readline is not reentrant", SCM_EOL
);
243 handle_error (void *data
, SCM tag
, SCM args
)
245 rl_free_line_state ();
246 rl_cleanup_after_signal ();
247 fputc ('\n', rl_outstream
); /* We don't want next output on this line */
248 fclose (rl_instream
);
249 fclose (rl_outstream
);
251 scm_handle_by_throw (data
, tag
, args
);
252 return SCM_UNSPECIFIED
; /* never reached */
256 internal_readline (SCM text
)
260 char *prompt
= SCM_UNBNDP (text
) ? "" : SCM_STRING_CHARS (text
);
263 s
= readline (prompt
);
265 ret
= scm_makfrom0str (s
);
275 stream_from_fport (SCM port
, char *mode
, const char *subr
)
280 fd
= dup (((struct scm_t_fport
*) SCM_STREAM (port
))->fdes
);
287 f
= fdopen (fd
, mode
);
298 scm_readline_init_ports (SCM inp
, SCM outp
)
300 if (SCM_UNBNDP (inp
))
303 if (SCM_UNBNDP (outp
))
306 if (!SCM_OPINFPORTP (inp
)) {
308 "Input port is not open or not a file port",
312 if (!SCM_OPOUTFPORTP (outp
)) {
314 "Output port is not open or not a file port",
319 rl_instream
= stream_from_fport (inp
, "r", s_scm_readline
);
320 rl_outstream
= stream_from_fport (outp
, "w", s_scm_readline
);
325 SCM_DEFINE (scm_add_history
, "add-history", 1, 0, 0,
328 #define FUNC_NAME s_scm_add_history
331 SCM_VALIDATE_STRING (1,text
);
332 SCM_STRING_COERCE_0TERMINATION_X (text
);
334 s
= SCM_STRING_CHARS (text
);
335 add_history (strdup (s
));
337 return SCM_UNSPECIFIED
;
342 SCM_DEFINE (scm_read_history
, "read-history", 1, 0, 0,
345 #define FUNC_NAME s_scm_read_history
347 SCM_VALIDATE_STRING (1,file
);
348 return SCM_NEGATE_BOOL (read_history (SCM_STRING_CHARS (file
)));
353 SCM_DEFINE (scm_write_history
, "write-history", 1, 0, 0,
356 #define FUNC_NAME s_scm_write_history
358 SCM_VALIDATE_STRING (1,file
);
359 return SCM_NEGATE_BOOL (write_history (SCM_STRING_CHARS (file
)));
363 SCM_DEFINE (scm_clear_history
, "clear-history", 0, 0, 0,
365 "Clear the history buffer of the readline machinery.")
366 #define FUNC_NAME s_scm_clear_history
369 return SCM_UNSPECIFIED
;
374 SCM_DEFINE (scm_filename_completion_function
, "filename-completion-function", 2, 0, 0,
375 (SCM text
, SCM continuep
),
377 #define FUNC_NAME s_scm_filename_completion_function
381 SCM_VALIDATE_STRING (1,text
);
382 SCM_STRING_COERCE_0TERMINATION_X (text
);
383 #ifdef HAVE_RL_FILENAME_COMPLETION_FUNCTION
384 s
= rl_filename_completion_function (SCM_STRING_CHARS (text
), SCM_NFALSEP (continuep
));
386 s
= filename_completion_function (SCM_STRING_CHARS (text
), SCM_NFALSEP (continuep
));
388 ans
= scm_makfrom0str (s
);
395 * The following has been modified from code contributed by
396 * Andrew Archibald <aarchiba@undergrad.math.uwaterloo.ca>
399 SCM scm_readline_completion_function_var
;
402 completion_function (char *text
, int continuep
)
404 SCM compfunc
= SCM_VARIABLE_REF (scm_readline_completion_function_var
);
407 if (SCM_FALSEP (compfunc
))
408 return NULL
; /* #f => completion disabled */
411 SCM t
= scm_makfrom0str (text
);
412 SCM c
= continuep
? SCM_BOOL_T
: SCM_BOOL_F
;
413 res
= scm_apply (compfunc
, scm_list_2 (t
, c
), SCM_EOL
);
415 if (SCM_FALSEP (res
))
418 if (!SCM_STRINGP (res
))
419 scm_misc_error (s_scm_readline
,
420 "Completion function returned bogus value: %S",
422 SCM_STRING_COERCE_0TERMINATION_X (res
);
423 return strdup (SCM_STRING_CHARS (res
));
427 /*Bouncing parenthesis (reimplemented by GH, 11/23/98, since readline is strict gpl)*/
429 static int match_paren (int x
, int k
);
430 static int find_matching_paren (int k
);
431 static void init_bouncing_parens ();
434 init_bouncing_parens ()
436 if (strncmp (rl_get_keymap_name (rl_get_keymap ()), "vi", 2))
438 rl_bind_key (')', match_paren
);
439 rl_bind_key (']', match_paren
);
440 rl_bind_key ('}', match_paren
);
445 find_matching_paren(int k
)
449 int end_parens_found
= 0;
451 /* Choose the corresponding opening bracket. */
452 if (k
== ')') c
= '(';
453 else if (k
== ']') c
= '[';
454 else if (k
== '}') c
= '{';
456 for (i
=rl_point
-2; i
>=0; i
--)
458 /* Is the current character part of a character literal? */
460 && rl_line_buffer
[i
- 1] == '\\'
461 && rl_line_buffer
[i
- 2] == '#')
463 else if (rl_line_buffer
[i
] == k
)
465 else if (rl_line_buffer
[i
] == '"')
467 /* Skip over a string literal. */
468 for (i
--; i
>= 0; i
--)
469 if (rl_line_buffer
[i
] == '"'
471 && rl_line_buffer
[i
- 1] == '\\'))
474 else if (rl_line_buffer
[i
] == c
)
476 if (end_parens_found
==0)
478 else --end_parens_found
;
485 match_paren (int x
, int k
)
489 struct timeval timeout
;
492 if (!SCM_READLINE_BOUNCE_PARENS
)
495 /* Did we just insert a quoted paren? If so, then don't bounce. */
496 if (rl_point
- 1 >= 1
497 && rl_line_buffer
[rl_point
- 2] == '\\')
500 tmp
= 1000 * SCM_READLINE_BOUNCE_PARENS
;
501 timeout
.tv_sec
= tmp
/ 1000000;
502 timeout
.tv_usec
= tmp
% 1000000;
504 fno
= fileno (rl_instream
);
505 FD_SET (fno
, &readset
);
510 rl_point
= find_matching_paren (k
);
514 scm_internal_select (fno
+ 1, &readset
, NULL
, NULL
, &timeout
);
521 #if defined (HAVE_RL_PRE_INPUT_HOOK) && defined (GUILE_SIGWINCH_SA_RESTART_CLEARED)
522 /* Readline disables SA_RESTART on SIGWINCH.
523 * This code turns it back on.
526 sigwinch_enable_restart (void)
528 #ifdef HAVE_SIGINTERRUPT
529 siginterrupt (SIGWINCH
, 0);
531 struct sigaction action
;
533 sigaction (SIGWINCH
, NULL
, &action
);
534 action
.sa_flags
|= SA_RESTART
;
535 sigaction (SIGWINCH
, &action
, NULL
);
541 #endif /* HAVE_RL_GETC_FUNCTION */
546 #ifdef HAVE_RL_GETC_FUNCTION
547 #include "guile-readline/readline.x"
548 scm_readline_completion_function_var
549 = scm_c_define ("*readline-completion-function*", SCM_BOOL_F
);
550 rl_getc_function
= current_input_getc
;
551 rl_redisplay_function
= redisplay
;
552 #if defined (_RL_FUNCTION_TYPEDEF)
553 rl_completion_entry_function
= (rl_compentry_func_t
*) completion_function
;
555 rl_completion_entry_function
= (Function
*) completion_function
;
557 rl_basic_word_break_characters
= "\t\n\"'`;()";
558 rl_readline_name
= "Guile";
559 #if defined (HAVE_RL_PRE_INPUT_HOOK) && defined (GUILE_SIGWINCH_SA_RESTART_CLEARED)
560 rl_pre_input_hook
= sigwinch_enable_restart
;
564 scm_mutex_init (&reentry_barrier_mutex
);
566 scm_init_opts (scm_readline_options
,
568 SCM_N_READLINE_OPTIONS
);
569 init_bouncing_parens();
570 scm_add_feature ("readline");
571 #endif /* HAVE_RL_GETC_FUNCTION */