1 /* readline.c --- line editing support for Guile */
3 /* Copyright (C) 1997 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.
19 * As a special exception, the Free Software Foundation gives permission
20 * for additional uses of the text contained in its release of GUILE.
22 * The exception is that, if you link the GUILE library with other files
23 * to produce an executable, this does not by itself cause the
24 * resulting executable to be covered by the GNU General Public License.
25 * Your use of that executable is in no way restricted on account of
26 * linking the GUILE library code into it.
28 * This exception does not however invalidate any other reasons why
29 * the executable file might be covered by the GNU General Public License.
31 * This exception applies only to the code released by the
32 * Free Software Foundation under the name GUILE. If you copy
33 * code from other Free Software Foundation releases into a copy of
34 * GUILE, as the General Public License permits, the exception does
35 * not apply to the code that you add in this way. To avoid misleading
36 * anyone as to the status of such modified files, you must delete
37 * this exception notice from them.
39 * If you write modifications of your own for GUILE, it is your choice
40 * whether to permit this exception to apply to your modifications.
41 * If you do not wish that, delete this exception notice.
46 #if defined (HAVE_RL_GETC_FUNCTION)
50 #include <readline/readline.h>
51 #include <readline/history.h>
57 scm_option scm_readline_opts
[] = {
58 { SCM_OPTION_BOOLEAN
, "history-file", 1,
59 "Use history file." },
60 { SCM_OPTION_INTEGER
, "history-length", 200,
62 { SCM_OPTION_INTEGER
, "bounce-parens", 500,
63 "Time (ms) to show matching opening parenthesis (0 = off)."}
66 extern void stifle_history (int max
);
68 SCM_PROC (s_readline_options
, "readline-options-interface", 0, 1, 0, scm_readline_options
);
71 scm_readline_options (setting
)
74 SCM ans
= scm_options (setting
,
76 SCM_N_READLINE_OPTIONS
,
78 stifle_history (SCM_HISTORY_LENGTH
);
87 char *new = malloc (len
+ 1);
91 #endif /* HAVE_STRDUP */
93 #ifndef HAVE_RL_CLEANUP_AFTER_SIGNAL
95 /* These are readline functions added in release 2.3. They will work
96 * together with readline-2.1 and 2.2. (The readline interface is
97 * disabled for earlier releases.)
98 * They are declared static; if we want to use them elsewhere, then
99 * we need external declarations for them, but at the moment, I don't
100 * think anything else in Guile ought to use these.
103 extern void _rl_clean_up_for_exit ();
104 extern void _rl_kill_kbd_macro ();
105 extern int _rl_init_argument ();
108 rl_cleanup_after_signal ()
110 #ifdef HAVE_RL_CLEAR_SIGNALS
111 _rl_clean_up_for_exit ();
113 (*rl_deprep_term_function
) ();
114 #ifdef HAVE_RL_CLEAR_SIGNALS
117 rl_pending_input
= 0;
121 rl_free_line_state ()
123 register HIST_ENTRY
*entry
;
127 entry
= current_history ();
129 entry
->data
= (char *)NULL
;
131 _rl_kill_kbd_macro ();
133 _rl_init_argument ();
136 #endif /* !HAVE_RL_CLEANUP_AFTER_SIGNAL */
139 static SCM input_port
;
140 static SCM before_read
;
143 current_input_getc (FILE *in
)
146 if (promptp
&& SCM_NIMP (before_read
))
148 scm_apply (before_read
, SCM_EOL
, SCM_EOL
);
151 ans
= scm_getc (input_port
);
162 SCM_PROC (s_readline
, "readline", 0, 4, 0, scm_readline
);
164 static int in_readline
= 0;
166 static scm_mutex_t reentry_barrier_mutex
;
174 /* We should rather use scm_mutex_try_lock when it becomes available */
175 scm_mutex_lock (&reentry_barrier_mutex
);
182 scm_mutex_unlock (&reentry_barrier_mutex
);
185 scm_misc_error (s_readline
, "readline is not reentrant", SCM_EOL
);
189 handle_error (void *data
, SCM tag
, SCM args
)
191 rl_free_line_state ();
192 rl_cleanup_after_signal ();
194 scm_handle_by_throw (data
, tag
, args
);
195 return SCM_UNSPECIFIED
; /* never reached */
199 internal_readline (SCM text
)
203 char *prompt
= SCM_UNBNDP (text
) ? "" : SCM_CHARS (text
);
206 s
= readline (prompt
);
208 ret
= scm_makfrom0str (s
);
218 scm_readline (SCM text
, SCM inp
, SCM outp
, SCM read_hook
)
224 before_read
= SCM_BOOL_F
;
226 if (!SCM_UNBNDP (text
))
228 if (!(SCM_NIMP (text
) && SCM_STRINGP (text
)))
231 scm_wrong_type_arg (s_readline
, SCM_ARG1
, text
);
233 SCM_COERCE_SUBSTR (text
);
236 if (SCM_UNBNDP (inp
))
239 if (SCM_UNBNDP (outp
))
242 if (!(SCM_UNBNDP (read_hook
) || SCM_FALSEP (read_hook
)))
244 if (!(SCM_NFALSEP (scm_thunk_p (read_hook
))))
247 scm_wrong_type_arg (s_readline
, SCM_ARG4
, read_hook
);
249 before_read
= read_hook
;
252 if (!(SCM_NIMP (inp
) && SCM_OPINFPORTP (inp
)))
255 scm_misc_error (s_readline
,
256 "Input port is not open or not a file port",
259 if (!(SCM_NIMP (outp
) && SCM_OPOUTFPORTP (outp
)))
262 scm_misc_error (s_readline
,
263 "Output port is not open or not a file port",
268 rl_instream
= (FILE *) SCM_STREAM (inp
);
269 rl_outstream
= (FILE *) SCM_STREAM (outp
);
271 ans
= scm_internal_catch (SCM_BOOL_T
,
272 (scm_catch_body_t
) internal_readline
,
279 SCM_PROC (s_add_history
, "add-history", 1, 0, 0, scm_add_history
);
282 scm_add_history (SCM text
)
285 SCM_ASSERT ((SCM_NIMP(text
) && SCM_STRINGP(text
)), text
, SCM_ARG1
,
287 SCM_COERCE_SUBSTR (text
);
289 s
= SCM_CHARS (text
);
290 add_history (strdup (s
));
292 return SCM_UNSPECIFIED
;
296 SCM_PROC (s_read_history
, "read-history", 1, 0, 0, scm_read_history
);
299 scm_read_history (SCM file
)
301 SCM_ASSERT (SCM_NIMP (file
) && SCM_STRINGP (file
),
302 file
, SCM_ARG1
, s_read_history
);
303 return read_history (SCM_ROCHARS (file
)) ? SCM_BOOL_F
: SCM_BOOL_T
;
307 SCM_PROC (s_write_history
, "write-history", 1, 0, 0, scm_write_history
);
310 scm_write_history (SCM file
)
312 SCM_ASSERT (SCM_NIMP (file
) && SCM_STRINGP (file
),
313 file
, SCM_ARG1
, s_write_history
);
314 return write_history (SCM_ROCHARS (file
)) ? SCM_BOOL_F
: SCM_BOOL_T
;
318 SCM_PROC (s_filename_completion_function
, "filename-completion-function", 2, 0, 0, scm_filename_completion_function
);
321 scm_filename_completion_function (SCM text
, SCM continuep
)
325 SCM_ASSERT (SCM_NIMP (text
) && SCM_STRINGP (text
),
328 s_filename_completion_function
);
329 SCM_COERCE_SUBSTR (text
);
330 s
= filename_completion_function (SCM_CHARS (text
), SCM_NFALSEP (continuep
));
331 ans
= scm_makfrom0str (s
);
337 * The following has been modified from code contributed by
338 * Andrew Archibald <aarchiba@undergrad.math.uwaterloo.ca>
341 SCM scm_readline_completion_function_var
;
344 completion_function (char *text
, int continuep
)
346 SCM compfunc
= SCM_CDR (scm_readline_completion_function_var
);
349 if (SCM_FALSEP (compfunc
))
350 return NULL
; /* #f => completion disabled */
353 SCM t
= scm_makfrom0str (text
);
354 SCM c
= continuep
? SCM_BOOL_T
: SCM_BOOL_F
;
355 res
= scm_apply (compfunc
, SCM_LIST2 (t
, c
), SCM_EOL
);
357 if (SCM_FALSEP (res
))
360 if (!(SCM_NIMP (res
) && SCM_STRINGP (res
)))
361 scm_misc_error (s_readline
,
362 "Completion function returned bogus value: %S",
364 SCM_COERCE_SUBSTR (res
);
365 return strdup (SCM_CHARS (res
));
369 /*Bouncing parenthesis (reimplemented by GH, 11/23/98, since readline is strict gpl)*/
371 static void match_paren(int x
, int k
);
372 static int find_matching_paren(int k
);
373 static void init_bouncing_parens();
376 init_bouncing_parens()
378 if(strncmp(rl_get_keymap_name(rl_get_keymap()), "vi", 2)) {
379 rl_bind_key(')', match_paren
);
380 rl_bind_key(']', match_paren
);
381 rl_bind_key('}', match_paren
);
386 find_matching_paren(int k
)
390 int end_parens_found
= 0;
392 /* Choose the corresponding opening bracket. */
393 if (k
== ')') c
= '(';
394 else if (k
== ']') c
= '[';
395 else if (k
== '}') c
= '{';
397 for (i
=rl_point
-2; i
>=0; i
--)
399 /* Is the current character part of a character literal? */
401 && rl_line_buffer
[i
- 1] == '\\'
402 && rl_line_buffer
[i
- 2] == '#')
404 else if (rl_line_buffer
[i
] == k
)
406 else if (rl_line_buffer
[i
] == '"')
408 /* Skip over a string literal. */
409 for (i
--; i
>= 0; i
--)
410 if (rl_line_buffer
[i
] == '"'
412 && rl_line_buffer
[i
- 1] == '\\'))
415 else if (rl_line_buffer
[i
] == c
)
417 if (end_parens_found
==0) return i
;
418 else --end_parens_found
;
425 match_paren(int x
, int k
)
429 struct timeval timeout
;
432 if (!SCM_READLINE_BOUNCE_PARENS
)
435 /* Did we just insert a quoted paren? If so, then don't bounce. */
436 if (rl_point
- 1 >= 1
437 && rl_line_buffer
[rl_point
- 2] == '\\')
440 tmp
= 1000 * SCM_READLINE_BOUNCE_PARENS
;
441 timeout
.tv_sec
= tmp
/ 1000000;
442 timeout
.tv_usec
= tmp
% 1000000;
444 FD_SET(fileno(rl_instream
), &readset
);
448 rl_point
= find_matching_paren(k
);
451 scm_internal_select(1, &readset
, NULL
, NULL
, &timeout
);
461 #include "readline.x"
462 scm_readline_completion_function_var
463 = scm_sysintern ("*readline-completion-function*", SCM_BOOL_F
);
464 rl_getc_function
= current_input_getc
;
465 rl_redisplay_function
= redisplay
;
466 rl_completion_entry_function
= (Function
*) completion_function
;
467 rl_basic_word_break_characters
= "\t\n\"'`;()";
469 scm_mutex_init (&reentry_barrier_mutex
);
471 scm_init_opts (scm_readline_options
,
473 SCM_N_READLINE_OPTIONS
);
474 init_bouncing_parens();
475 scm_add_feature ("readline");