1 /* parens.c -- Implementation of matching parentheses feature. */
3 /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
5 This file is part of the GNU Readline Library, a library for
6 reading lines of text with interactive input and history editing.
8 The GNU Readline Library is free software; you can redistribute it
9 and/or modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2, or
11 (at your option) any later version.
13 The GNU Readline Library is distributed in the hope that it will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 The GNU General Public License is often shipped with GNU software, and
19 is generally kept in a file called COPYING or LICENSE. If you do not
20 have a copy of the license, write to the Free Software Foundation,
21 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
22 #define READLINE_LIBRARY
24 #if defined (__TANDEM)
30 #if defined (HAVE_CONFIG_H)
35 #include <sys/types.h>
37 #if defined (HAVE_UNISTD_H)
41 #if defined (FD_SET) && !defined (HAVE_SELECT)
45 #if defined (HAVE_SELECT)
46 # include <sys/time.h>
47 #endif /* HAVE_SELECT */
48 #if defined (HAVE_SYS_SELECT_H)
49 # include <sys/select.h>
52 #if defined (HAVE_STRING_H)
54 #else /* !HAVE_STRING_H */
56 #endif /* !HAVE_STRING_H */
58 #if !defined (strchr) && !defined (__STDC__)
59 extern char *strchr (), *strrchr ();
60 #endif /* !strchr && !__STDC__ */
63 #include "rlprivate.h"
65 static int find_matching_open
PARAMS((char *, int, int));
67 /* Non-zero means try to blink the matching open parenthesis when the
68 close parenthesis is inserted. */
69 #if defined (HAVE_SELECT)
70 int rl_blink_matching_paren
= 1;
71 #else /* !HAVE_SELECT */
72 int rl_blink_matching_paren
= 0;
73 #endif /* !HAVE_SELECT */
75 static int _paren_blink_usec
= 500000;
77 /* Change emacs_standard_keymap to have bindings for paren matching when
78 ON_OR_OFF is 1, change them back to self_insert when ON_OR_OFF == 0. */
80 _rl_enable_paren_matching (on_or_off
)
85 rl_bind_key_in_map (')', rl_insert_close
, emacs_standard_keymap
);
86 rl_bind_key_in_map (']', rl_insert_close
, emacs_standard_keymap
);
87 rl_bind_key_in_map ('}', rl_insert_close
, emacs_standard_keymap
);
91 rl_bind_key_in_map (')', rl_insert
, emacs_standard_keymap
);
92 rl_bind_key_in_map (']', rl_insert
, emacs_standard_keymap
);
93 rl_bind_key_in_map ('}', rl_insert
, emacs_standard_keymap
);
98 rl_set_paren_blink_timeout (u
)
103 o
= _paren_blink_usec
;
105 _paren_blink_usec
= u
;
110 rl_insert_close (count
, invoking_key
)
111 int count
, invoking_key
;
113 if (rl_explicit_arg
|| !rl_blink_matching_paren
)
114 _rl_insert_char (count
, invoking_key
);
117 #if defined (HAVE_SELECT)
118 int orig_point
, match_point
, ready
;
119 struct timeval timer
;
122 _rl_insert_char (1, invoking_key
);
123 (*rl_redisplay_function
) ();
125 find_matching_open (rl_line_buffer
, rl_point
- 2, invoking_key
);
127 /* Emacs might message or ring the bell here, but I don't. */
132 FD_SET (fileno (rl_instream
), &readfds
);
134 timer
.tv_usec
= _paren_blink_usec
;
136 orig_point
= rl_point
;
137 rl_point
= match_point
;
138 (*rl_redisplay_function
) ();
139 ready
= select (1, &readfds
, (fd_set
*)NULL
, (fd_set
*)NULL
, &timer
);
140 rl_point
= orig_point
;
141 #else /* !HAVE_SELECT */
142 _rl_insert_char (count
, invoking_key
);
143 #endif /* !HAVE_SELECT */
149 find_matching_open (string
, from
, closer
)
154 int opener
, level
, delimiter
;
158 case ']': opener
= '['; break;
159 case '}': opener
= '{'; break;
160 case ')': opener
= '('; break;
165 level
= 1; /* The closer passed in counts as 1. */
166 delimiter
= 0; /* Delimited state unknown. */
168 for (i
= from
; i
> -1; i
--)
170 if (delimiter
&& (string
[i
] == delimiter
))
172 else if (rl_basic_quote_characters
&& strchr (rl_basic_quote_characters
, string
[i
]))
173 delimiter
= string
[i
];
174 else if (!delimiter
&& (string
[i
] == closer
))
176 else if (!delimiter
&& (string
[i
] == opener
))