1 /* parens.c -- implementation of matching parentheses feature. */
3 /* Copyright (C) 1987, 1989, 1992-2009 Free Software Foundation, Inc.
5 This file is part of the GNU Readline Library (Readline), a library
6 for reading lines of text with interactive input and history editing.
8 Readline is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 Readline is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Readline. If not, see <http://www.gnu.org/licenses/>.
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 #include "posixselect.h"
43 #if defined (HAVE_STRING_H)
45 #else /* !HAVE_STRING_H */
47 #endif /* !HAVE_STRING_H */
49 #if !defined (strchr) && !defined (__STDC__)
50 extern char *strchr (), *strrchr ();
51 #endif /* !strchr && !__STDC__ */
54 #include "rlprivate.h"
56 static int find_matching_open
PARAMS((char *, int, int));
58 /* Non-zero means try to blink the matching open parenthesis when the
59 close parenthesis is inserted. */
60 #if defined (HAVE_SELECT)
61 int rl_blink_matching_paren
= 1;
62 #else /* !HAVE_SELECT */
63 int rl_blink_matching_paren
= 0;
64 #endif /* !HAVE_SELECT */
66 static int _paren_blink_usec
= 500000;
68 /* Change emacs_standard_keymap to have bindings for paren matching when
69 ON_OR_OFF is 1, change them back to self_insert when ON_OR_OFF == 0. */
71 _rl_enable_paren_matching (on_or_off
)
76 rl_bind_key_in_map (')', rl_insert_close
, emacs_standard_keymap
);
77 rl_bind_key_in_map (']', rl_insert_close
, emacs_standard_keymap
);
78 rl_bind_key_in_map ('}', rl_insert_close
, emacs_standard_keymap
);
82 rl_bind_key_in_map (')', rl_insert
, emacs_standard_keymap
);
83 rl_bind_key_in_map (']', rl_insert
, emacs_standard_keymap
);
84 rl_bind_key_in_map ('}', rl_insert
, emacs_standard_keymap
);
89 rl_set_paren_blink_timeout (u
)
94 o
= _paren_blink_usec
;
96 _paren_blink_usec
= u
;
101 rl_insert_close (count
, invoking_key
)
102 int count
, invoking_key
;
104 if (rl_explicit_arg
|| !rl_blink_matching_paren
)
105 _rl_insert_char (count
, invoking_key
);
108 #if defined (HAVE_SELECT)
109 int orig_point
, match_point
, ready
;
110 struct timeval timer
;
113 _rl_insert_char (1, invoking_key
);
114 (*rl_redisplay_function
) ();
116 find_matching_open (rl_line_buffer
, rl_point
- 2, invoking_key
);
118 /* Emacs might message or ring the bell here, but I don't. */
123 FD_SET (fileno (rl_instream
), &readfds
);
124 USEC_TO_TIMEVAL (_paren_blink_usec
, timer
);
126 orig_point
= rl_point
;
127 rl_point
= match_point
;
128 (*rl_redisplay_function
) ();
129 ready
= select (1, &readfds
, (fd_set
*)NULL
, (fd_set
*)NULL
, &timer
);
130 rl_point
= orig_point
;
131 #else /* !HAVE_SELECT */
132 _rl_insert_char (count
, invoking_key
);
133 #endif /* !HAVE_SELECT */
139 find_matching_open (string
, from
, closer
)
144 int opener
, level
, delimiter
;
148 case ']': opener
= '['; break;
149 case '}': opener
= '{'; break;
150 case ')': opener
= '('; break;
155 level
= 1; /* The closer passed in counts as 1. */
156 delimiter
= 0; /* Delimited state unknown. */
158 for (i
= from
; i
> -1; i
--)
160 if (delimiter
&& (string
[i
] == delimiter
))
162 else if (rl_basic_quote_characters
&& strchr (rl_basic_quote_characters
, string
[i
]))
163 delimiter
= string
[i
];
164 else if (!delimiter
&& (string
[i
] == closer
))
166 else if (!delimiter
&& (string
[i
] == opener
))