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
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 #if defined (HAVE_RL_GETC_FUNCTION)
29 #include "libguile/libguile.h"
30 #include "libguile/gh.h"
31 #include "libguile/iselect.h"
33 #include <readline/readline.h>
34 #include <readline/history.h>
39 scm_option scm_readline_opts
[] = {
40 { SCM_OPTION_BOOLEAN
, "history-file", 1,
41 "Use history file." },
42 { SCM_OPTION_INTEGER
, "history-length", 200,
44 { SCM_OPTION_INTEGER
, "bounce-parens", 500,
45 "Time (ms) to show matching opening parenthesis (0 = off)."}
48 extern void stifle_history (int max
);
50 GUILE_PROC (scm_readline_options
, "readline-options-interface", 0, 1, 0,
53 #define FUNC_NAME s_scm_readline_options
55 SCM ans
= scm_options (setting
,
57 SCM_N_READLINE_OPTIONS
,
59 stifle_history (SCM_HISTORY_LENGTH
);
69 char *new = malloc (len
+ 1);
73 #endif /* HAVE_STRDUP */
75 #ifndef HAVE_RL_CLEANUP_AFTER_SIGNAL
77 /* These are readline functions added in release 2.3. They will work
78 * together with readline-2.1 and 2.2. (The readline interface is
79 * disabled for earlier releases.)
80 * They are declared static; if we want to use them elsewhere, then
81 * we need external declarations for them, but at the moment, I don't
82 * think anything else in Guile ought to use these.
85 extern void _rl_clean_up_for_exit ();
86 extern void _rl_kill_kbd_macro ();
87 extern int _rl_init_argument ();
90 rl_cleanup_after_signal ()
92 #ifdef HAVE_RL_CLEAR_SIGNALS
93 _rl_clean_up_for_exit ();
95 (*rl_deprep_term_function
) ();
96 #ifdef HAVE_RL_CLEAR_SIGNALS
103 rl_free_line_state ()
105 register HIST_ENTRY
*entry
;
109 entry
= current_history ();
111 entry
->data
= (char *)NULL
;
113 _rl_kill_kbd_macro ();
115 _rl_init_argument ();
118 #endif /* !HAVE_RL_CLEANUP_AFTER_SIGNAL */
121 static SCM input_port
;
122 static SCM before_read
;
125 current_input_getc (FILE *in
)
128 if (promptp
&& SCM_NIMP (before_read
))
130 scm_apply (before_read
, SCM_EOL
, SCM_EOL
);
133 ans
= scm_getc (input_port
);
144 static int in_readline
= 0;
146 static scm_mutex_t reentry_barrier_mutex
;
149 static SCM
internal_readline (SCM text
);
150 static SCM
handle_error (void *data
, SCM tag
, SCM args
);
151 static void reentry_barrier ();
154 GUILE_PROC (scm_readline
, "%readline", 0, 4, 0,
155 (SCM text
, SCM inp
, SCM outp
, SCM read_hook
),
157 #define FUNC_NAME s_scm_readline
163 before_read
= SCM_BOOL_F
;
165 if (!SCM_UNBNDP (text
))
167 if (!(SCM_NIMP (text
) && SCM_STRINGP (text
)))
170 scm_wrong_type_arg (s_scm_readline
, SCM_ARG1
, text
);
172 SCM_COERCE_SUBSTR (text
);
175 if (!((SCM_UNBNDP (inp
) && SCM_NIMP (scm_cur_inp
) && SCM_OPINFPORTP (inp
))
176 || SCM_NIMP (inp
) && SCM_OPINFPORTP (inp
)))
179 scm_misc_error (s_scm_readline
,
180 "Input port is not open or not a file port",
184 if (!((SCM_UNBNDP (outp
) && SCM_NIMP (scm_cur_outp
) && SCM_OPINFPORTP (outp
))
185 || (SCM_NIMP (outp
) && SCM_OPOUTFPORTP (outp
))))
188 scm_misc_error (s_scm_readline
,
189 "Output port is not open or not a file port",
193 if (!(SCM_UNBNDP (read_hook
) || SCM_FALSEP (read_hook
)))
195 if (!(SCM_NFALSEP (scm_thunk_p (read_hook
))))
198 scm_wrong_type_arg (s_scm_readline
, SCM_ARG4
, read_hook
);
200 before_read
= read_hook
;
203 scm_readline_init_ports (inp
, outp
);
205 ans
= scm_internal_catch (SCM_BOOL_T
,
206 (scm_catch_body_t
) internal_readline
,
210 fclose (rl_instream
);
211 fclose (rl_outstream
);
224 /* We should rather use scm_mutex_try_lock when it becomes available */
225 scm_mutex_lock (&reentry_barrier_mutex
);
232 scm_mutex_unlock (&reentry_barrier_mutex
);
235 scm_misc_error (s_scm_readline
, "readline is not reentrant", SCM_EOL
);
239 handle_error (void *data
, SCM tag
, SCM args
)
241 rl_free_line_state ();
242 rl_cleanup_after_signal ();
243 fputc ('\n', rl_outstream
); /* We don't want next output on this line */
244 fclose (rl_instream
);
245 fclose (rl_outstream
);
247 scm_handle_by_throw (data
, tag
, args
);
248 return SCM_UNSPECIFIED
; /* never reached */
252 internal_readline (SCM text
)
256 char *prompt
= SCM_UNBNDP (text
) ? "" : SCM_CHARS (text
);
259 s
= readline (prompt
);
261 ret
= scm_makfrom0str (s
);
271 stream_from_fport (SCM port
, char *mode
, const char *subr
)
276 fd
= dup (((struct scm_fport
*) SCM_STREAM (port
))->fdes
);
283 f
= fdopen (fd
, mode
);
294 scm_readline_init_ports (SCM inp
, SCM outp
)
296 if (SCM_UNBNDP (inp
))
299 if (SCM_UNBNDP (outp
))
302 if (!(SCM_NIMP (inp
) && SCM_OPINFPORTP (inp
))) {
304 "Input port is not open or not a file port",
308 if (!(SCM_NIMP (outp
) && SCM_OPOUTFPORTP (outp
))) {
310 "Output port is not open or not a file port",
315 rl_instream
= stream_from_fport (inp
, "r", s_scm_readline
);
316 rl_outstream
= stream_from_fport (outp
, "w", s_scm_readline
);
321 GUILE_PROC (scm_add_history
, "add-history", 1, 0, 0,
324 #define FUNC_NAME s_scm_add_history
327 SCM_VALIDATE_STRING(1,text
);
328 SCM_COERCE_SUBSTR (text
);
330 s
= SCM_CHARS (text
);
331 add_history (strdup (s
));
333 return SCM_UNSPECIFIED
;
338 GUILE_PROC (scm_read_history
, "read-history", 1, 0, 0,
341 #define FUNC_NAME s_scm_read_history
343 SCM_VALIDATE_STRING(1,file
);
344 return SCM_NEGATE_BOOL(read_history (SCM_ROCHARS (file
)));
349 GUILE_PROC (scm_write_history
, "write-history", 1, 0, 0,
352 #define FUNC_NAME s_scm_write_history
354 SCM_VALIDATE_STRING(1,file
);
355 return SCM_NEGATE_BOOL(write_history (SCM_ROCHARS (file
)));
360 GUILE_PROC (scm_filename_completion_function
, "filename-completion-function", 2, 0, 0,
361 (SCM text
, SCM continuep
),
363 #define FUNC_NAME s_scm_filename_completion_function
367 SCM_VALIDATE_STRING(1,text
);
368 SCM_COERCE_SUBSTR (text
);
369 s
= filename_completion_function (SCM_CHARS (text
), SCM_NFALSEP (continuep
));
370 ans
= scm_makfrom0str (s
);
377 * The following has been modified from code contributed by
378 * Andrew Archibald <aarchiba@undergrad.math.uwaterloo.ca>
381 SCM scm_readline_completion_function_var
;
384 completion_function (char *text
, int continuep
)
386 SCM compfunc
= SCM_CDR (scm_readline_completion_function_var
);
389 if (SCM_FALSEP (compfunc
))
390 return NULL
; /* #f => completion disabled */
393 SCM t
= scm_makfrom0str (text
);
394 SCM c
= continuep
? SCM_BOOL_T
: SCM_BOOL_F
;
395 res
= scm_apply (compfunc
, SCM_LIST2 (t
, c
), SCM_EOL
);
397 if (SCM_FALSEP (res
))
400 if (!(SCM_NIMP (res
) && SCM_STRINGP (res
)))
401 scm_misc_error (s_scm_readline
,
402 "Completion function returned bogus value: %S",
404 SCM_COERCE_SUBSTR (res
);
405 return strdup (SCM_CHARS (res
));
409 /*Bouncing parenthesis (reimplemented by GH, 11/23/98, since readline is strict gpl)*/
411 static void match_paren(int x
, int k
);
412 static int find_matching_paren(int k
);
413 static void init_bouncing_parens();
416 init_bouncing_parens()
418 if(strncmp(rl_get_keymap_name(rl_get_keymap()), "vi", 2)) {
419 rl_bind_key(')', match_paren
);
420 rl_bind_key(']', match_paren
);
421 rl_bind_key('}', match_paren
);
426 find_matching_paren(int k
)
430 int end_parens_found
= 0;
432 /* Choose the corresponding opening bracket. */
433 if (k
== ')') c
= '(';
434 else if (k
== ']') c
= '[';
435 else if (k
== '}') c
= '{';
437 for (i
=rl_point
-2; i
>=0; i
--)
439 /* Is the current character part of a character literal? */
441 && rl_line_buffer
[i
- 1] == '\\'
442 && rl_line_buffer
[i
- 2] == '#')
444 else if (rl_line_buffer
[i
] == k
)
446 else if (rl_line_buffer
[i
] == '"')
448 /* Skip over a string literal. */
449 for (i
--; i
>= 0; i
--)
450 if (rl_line_buffer
[i
] == '"'
452 && rl_line_buffer
[i
- 1] == '\\'))
455 else if (rl_line_buffer
[i
] == c
)
457 if (end_parens_found
==0) return i
;
458 else --end_parens_found
;
465 match_paren(int x
, int k
)
469 struct timeval timeout
;
472 if (!SCM_READLINE_BOUNCE_PARENS
)
475 /* Did we just insert a quoted paren? If so, then don't bounce. */
476 if (rl_point
- 1 >= 1
477 && rl_line_buffer
[rl_point
- 2] == '\\')
480 tmp
= 1000 * SCM_READLINE_BOUNCE_PARENS
;
481 timeout
.tv_sec
= tmp
/ 1000000;
482 timeout
.tv_usec
= tmp
% 1000000;
484 FD_SET(fileno(rl_instream
), &readset
);
488 rl_point
= find_matching_paren(k
);
491 scm_internal_select(1, &readset
, NULL
, NULL
, &timeout
);
501 #include "readline.x"
502 scm_readline_completion_function_var
503 = scm_sysintern ("*readline-completion-function*", SCM_BOOL_F
);
504 rl_getc_function
= current_input_getc
;
505 rl_redisplay_function
= redisplay
;
506 rl_completion_entry_function
= (Function
*) completion_function
;
507 rl_basic_word_break_characters
= "\t\n\"'`;()";
508 rl_readline_name
= "Guile";
511 scm_mutex_init (&reentry_barrier_mutex
);
513 scm_init_opts (scm_readline_options
,
515 SCM_N_READLINE_OPTIONS
);
516 init_bouncing_parens();
517 scm_add_feature ("readline");