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
25 #include "libguile/_scm.h"
26 #ifdef HAVE_RL_GETC_FUNCTION
28 #include "libguile/gh.h"
29 #include "libguile/iselect.h"
35 #include <readline/readline.h>
36 #include <readline/history.h>
44 #include "libguile/validate.h"
45 #include "guile-readline/readline.h"
47 scm_t_option scm_readline_opts
[] = {
48 { SCM_OPTION_BOOLEAN
, "history-file", 1,
49 "Use history file." },
50 { SCM_OPTION_INTEGER
, "history-length", 200,
52 { SCM_OPTION_INTEGER
, "bounce-parens", 500,
53 "Time (ms) to show matching opening parenthesis (0 = off)."}
56 extern void stifle_history (int max
);
58 SCM_DEFINE (scm_readline_options
, "readline-options-interface", 0, 1, 0,
61 #define FUNC_NAME s_scm_readline_options
63 SCM ans
= scm_options (setting
,
65 SCM_N_READLINE_OPTIONS
,
67 stifle_history (SCM_HISTORY_LENGTH
);
76 size_t len
= strlen (s
);
77 char *new = malloc (len
+ 1);
81 #endif /* HAVE_STRDUP */
83 #ifndef HAVE_RL_CLEANUP_AFTER_SIGNAL
85 /* These are readline functions added in release 2.3. They will work
86 * together with readline-2.1 and 2.2. (The readline interface is
87 * disabled for earlier releases.)
88 * They are declared static; if we want to use them elsewhere, then
89 * we need external declarations for them, but at the moment, I don't
90 * think anything else in Guile ought to use these.
93 extern void _rl_clean_up_for_exit ();
94 extern void _rl_kill_kbd_macro ();
95 extern int _rl_init_argument ();
98 rl_cleanup_after_signal ()
100 #ifdef HAVE_RL_CLEAR_SIGNALS
101 _rl_clean_up_for_exit ();
103 (*rl_deprep_term_function
) ();
104 #ifdef HAVE_RL_CLEAR_SIGNALS
107 rl_pending_input
= 0;
111 rl_free_line_state ()
113 register HIST_ENTRY
*entry
;
117 entry
= current_history ();
119 entry
->data
= (char *)NULL
;
121 _rl_kill_kbd_macro ();
123 _rl_init_argument ();
126 #endif /* !HAVE_RL_CLEANUP_AFTER_SIGNAL */
129 static SCM input_port
;
130 static SCM before_read
;
133 current_input_getc (FILE *in SCM_UNUSED
)
135 if (promptp
&& !SCM_FALSEP (before_read
))
137 scm_apply (before_read
, SCM_EOL
, SCM_EOL
);
140 return scm_getc (input_port
);
150 static int in_readline
= 0;
152 static scm_t_mutex reentry_barrier_mutex
;
155 static SCM
internal_readline (SCM text
);
156 static SCM
handle_error (void *data
, SCM tag
, SCM args
);
157 static void reentry_barrier (void);
160 SCM_DEFINE (scm_readline
, "%readline", 0, 4, 0,
161 (SCM text
, SCM inp
, SCM outp
, SCM read_hook
),
163 #define FUNC_NAME s_scm_readline
169 before_read
= SCM_BOOL_F
;
171 if (!SCM_UNBNDP (text
))
173 if (!SCM_STRINGP (text
))
176 scm_wrong_type_arg (s_scm_readline
, SCM_ARG1
, text
);
180 if (!((SCM_UNBNDP (inp
) && SCM_OPINFPORTP (scm_cur_inp
))
181 || SCM_OPINFPORTP (inp
)))
184 scm_misc_error (s_scm_readline
,
185 "Input port is not open or not a file port",
189 if (!((SCM_UNBNDP (outp
) && SCM_OPINFPORTP (scm_cur_outp
))
190 || SCM_OPOUTFPORTP (outp
)))
193 scm_misc_error (s_scm_readline
,
194 "Output port is not open or not a file port",
198 if (!(SCM_UNBNDP (read_hook
) || SCM_FALSEP (read_hook
)))
200 if (!(SCM_NFALSEP (scm_thunk_p (read_hook
))))
203 scm_wrong_type_arg (s_scm_readline
, SCM_ARG4
, read_hook
);
205 before_read
= read_hook
;
208 scm_readline_init_ports (inp
, outp
);
210 ans
= scm_internal_catch (SCM_BOOL_T
,
211 (scm_t_catch_body
) internal_readline
,
212 (void *) SCM_UNPACK (text
),
216 fclose (rl_instream
);
217 fclose (rl_outstream
);
231 /* We should rather use scm_t_mutexry_lock when it becomes available */
232 scm_mutex_lock (&reentry_barrier_mutex
);
239 scm_mutex_unlock (&reentry_barrier_mutex
);
242 scm_misc_error (s_scm_readline
, "readline is not reentrant", SCM_EOL
);
246 handle_error (void *data
, SCM tag
, SCM args
)
248 rl_free_line_state ();
249 rl_cleanup_after_signal ();
250 fputc ('\n', rl_outstream
); /* We don't want next output on this line */
252 fclose (rl_instream
);
253 fclose (rl_outstream
);
256 scm_handle_by_throw (data
, tag
, args
);
257 return SCM_UNSPECIFIED
; /* never reached */
261 internal_readline (SCM text
)
265 char *prompt
= SCM_UNBNDP (text
) ? "" : SCM_STRING_CHARS (text
);
268 s
= readline (prompt
);
270 ret
= scm_makfrom0str (s
);
280 stream_from_fport (SCM port
, char *mode
, const char *subr
)
285 fd
= dup (((struct scm_t_fport
*) SCM_STREAM (port
))->fdes
);
292 f
= fdopen (fd
, mode
);
303 scm_readline_init_ports (SCM inp
, SCM outp
)
305 if (SCM_UNBNDP (inp
))
308 if (SCM_UNBNDP (outp
))
311 if (!SCM_OPINFPORTP (inp
)) {
313 "Input port is not open or not a file port",
317 if (!SCM_OPOUTFPORTP (outp
)) {
319 "Output port is not open or not a file port",
325 rl_instream
= stream_from_fport (inp
, "r", s_scm_readline
);
326 rl_outstream
= stream_from_fport (outp
, "w", s_scm_readline
);
332 SCM_DEFINE (scm_add_history
, "add-history", 1, 0, 0,
335 #define FUNC_NAME s_scm_add_history
338 SCM_VALIDATE_STRING (1,text
);
340 s
= SCM_STRING_CHARS (text
);
341 add_history (strdup (s
));
343 return SCM_UNSPECIFIED
;
348 SCM_DEFINE (scm_read_history
, "read-history", 1, 0, 0,
351 #define FUNC_NAME s_scm_read_history
353 SCM_VALIDATE_STRING (1,file
);
354 return SCM_NEGATE_BOOL (read_history (SCM_STRING_CHARS (file
)));
359 SCM_DEFINE (scm_write_history
, "write-history", 1, 0, 0,
362 #define FUNC_NAME s_scm_write_history
364 SCM_VALIDATE_STRING (1,file
);
365 return SCM_NEGATE_BOOL (write_history (SCM_STRING_CHARS (file
)));
369 SCM_DEFINE (scm_clear_history
, "clear-history", 0, 0, 0,
371 "Clear the history buffer of the readline machinery.")
372 #define FUNC_NAME s_scm_clear_history
375 return SCM_UNSPECIFIED
;
380 SCM_DEFINE (scm_filename_completion_function
, "filename-completion-function", 2, 0, 0,
381 (SCM text
, SCM continuep
),
383 #define FUNC_NAME s_scm_filename_completion_function
387 SCM_VALIDATE_STRING (1,text
);
388 #ifdef HAVE_RL_FILENAME_COMPLETION_FUNCTION
389 s
= rl_filename_completion_function (SCM_STRING_CHARS (text
), SCM_NFALSEP (continuep
));
391 s
= filename_completion_function (SCM_STRING_CHARS (text
), SCM_NFALSEP (continuep
));
393 ans
= scm_makfrom0str (s
);
400 * The following has been modified from code contributed by
401 * Andrew Archibald <aarchiba@undergrad.math.uwaterloo.ca>
404 SCM scm_readline_completion_function_var
;
407 completion_function (char *text
, int continuep
)
409 SCM compfunc
= SCM_VARIABLE_REF (scm_readline_completion_function_var
);
412 if (SCM_FALSEP (compfunc
))
413 return NULL
; /* #f => completion disabled */
416 SCM t
= scm_makfrom0str (text
);
417 SCM c
= continuep
? SCM_BOOL_T
: SCM_BOOL_F
;
418 res
= scm_apply (compfunc
, scm_list_2 (t
, c
), SCM_EOL
);
420 if (SCM_FALSEP (res
))
423 if (!SCM_STRINGP (res
))
424 scm_misc_error (s_scm_readline
,
425 "Completion function returned bogus value: %S",
427 return strdup (SCM_STRING_CHARS (res
));
431 /*Bouncing parenthesis (reimplemented by GH, 11/23/98, since readline is strict gpl)*/
433 static int match_paren (int x
, int k
);
434 static int find_matching_paren (int k
);
435 static void init_bouncing_parens ();
438 init_bouncing_parens ()
440 if (strncmp (rl_get_keymap_name (rl_get_keymap ()), "vi", 2))
442 rl_bind_key (')', match_paren
);
443 rl_bind_key (']', match_paren
);
444 rl_bind_key ('}', match_paren
);
449 find_matching_paren(int k
)
453 int end_parens_found
= 0;
455 /* Choose the corresponding opening bracket. */
456 if (k
== ')') c
= '(';
457 else if (k
== ']') c
= '[';
458 else if (k
== '}') c
= '{';
460 for (i
=rl_point
-2; i
>=0; i
--)
462 /* Is the current character part of a character literal? */
464 && rl_line_buffer
[i
- 1] == '\\'
465 && rl_line_buffer
[i
- 2] == '#')
467 else if (rl_line_buffer
[i
] == k
)
469 else if (rl_line_buffer
[i
] == '"')
471 /* Skip over a string literal. */
472 for (i
--; i
>= 0; i
--)
473 if (rl_line_buffer
[i
] == '"'
475 && rl_line_buffer
[i
- 1] == '\\'))
478 else if (rl_line_buffer
[i
] == c
)
480 if (end_parens_found
==0)
482 else --end_parens_found
;
489 match_paren (int x
, int k
)
495 struct timeval timeout
;
499 if (!SCM_READLINE_BOUNCE_PARENS
)
502 /* Did we just insert a quoted paren? If so, then don't bounce. */
503 if (rl_point
- 1 >= 1
504 && rl_line_buffer
[rl_point
- 2] == '\\')
508 tmp
= 1000 * SCM_READLINE_BOUNCE_PARENS
;
509 timeout
.tv_sec
= tmp
/ 1000000;
510 timeout
.tv_usec
= tmp
% 1000000;
512 fno
= fileno (rl_instream
);
513 FD_SET (fno
, &readset
);
519 rl_point
= find_matching_paren (k
);
524 scm_internal_select (fno
+ 1, &readset
, NULL
, NULL
, &timeout
);
526 WaitForSingleObject (GetStdHandle(STD_INPUT_HANDLE
),
527 SCM_READLINE_BOUNCE_PARENS
);
535 #if defined (HAVE_RL_PRE_INPUT_HOOK) && defined (GUILE_SIGWINCH_SA_RESTART_CLEARED)
536 /* Readline disables SA_RESTART on SIGWINCH.
537 * This code turns it back on.
540 sigwinch_enable_restart (void)
542 #ifdef HAVE_SIGINTERRUPT
543 siginterrupt (SIGWINCH
, 0);
545 struct sigaction action
;
547 sigaction (SIGWINCH
, NULL
, &action
);
548 action
.sa_flags
|= SA_RESTART
;
549 sigaction (SIGWINCH
, &action
, NULL
);
555 #endif /* HAVE_RL_GETC_FUNCTION */
560 #ifdef HAVE_RL_GETC_FUNCTION
561 #include "guile-readline/readline.x"
562 scm_readline_completion_function_var
563 = scm_c_define ("*readline-completion-function*", SCM_BOOL_F
);
565 rl_getc_function
= current_input_getc
;
567 rl_redisplay_function
= redisplay
;
568 #if defined (_RL_FUNCTION_TYPEDEF)
569 rl_completion_entry_function
= (rl_compentry_func_t
*) completion_function
;
571 rl_completion_entry_function
= (Function
*) completion_function
;
573 rl_basic_word_break_characters
= "\t\n\"'`;()";
574 rl_readline_name
= "Guile";
575 #if defined (HAVE_RL_PRE_INPUT_HOOK) && defined (GUILE_SIGWINCH_SA_RESTART_CLEARED)
576 rl_pre_input_hook
= sigwinch_enable_restart
;
580 scm_mutex_init (&reentry_barrier_mutex
);
582 scm_init_opts (scm_readline_options
,
584 SCM_N_READLINE_OPTIONS
);
585 init_bouncing_parens();
586 scm_add_feature ("readline");
587 #endif /* HAVE_RL_GETC_FUNCTION */