| 1 | /* parens.c -- Implementation of matching parentheses feature. */ |
| 2 | |
| 3 | /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc. |
| 4 | |
| 5 | This file is part of the GNU Readline Library, a library for |
| 6 | reading lines of text with interactive input and history editing. |
| 7 | |
| 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. |
| 12 | |
| 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. |
| 17 | |
| 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 |
| 23 | |
| 24 | #include "rlconf.h" |
| 25 | |
| 26 | #if defined (HAVE_CONFIG_H) |
| 27 | # include <config.h> |
| 28 | #endif |
| 29 | |
| 30 | #include <stdio.h> |
| 31 | #include <sys/types.h> |
| 32 | |
| 33 | #if defined (FD_SET) && !defined (HAVE_SELECT) |
| 34 | # define HAVE_SELECT |
| 35 | #endif |
| 36 | |
| 37 | #if defined (HAVE_SELECT) |
| 38 | # include <sys/time.h> |
| 39 | #endif /* HAVE_SELECT */ |
| 40 | #if defined (HAVE_SYS_SELECT_H) |
| 41 | # include <sys/select.h> |
| 42 | #endif |
| 43 | |
| 44 | #if defined (HAVE_STRING_H) |
| 45 | # include <string.h> |
| 46 | #else /* !HAVE_STRING_H */ |
| 47 | # include <strings.h> |
| 48 | #endif /* !HAVE_STRING_H */ |
| 49 | |
| 50 | #if !defined (strchr) && !defined (__STDC__) |
| 51 | extern char *strchr (), *strrchr (); |
| 52 | #endif /* !strchr && !__STDC__ */ |
| 53 | |
| 54 | #include "readline.h" |
| 55 | #include "rlprivate.h" |
| 56 | |
| 57 | static int find_matching_open __P((char *, int, int)); |
| 58 | |
| 59 | /* Non-zero means try to blink the matching open parenthesis when the |
| 60 | close parenthesis is inserted. */ |
| 61 | #if defined (HAVE_SELECT) |
| 62 | int rl_blink_matching_paren = 1; |
| 63 | #else /* !HAVE_SELECT */ |
| 64 | int rl_blink_matching_paren = 0; |
| 65 | #endif /* !HAVE_SELECT */ |
| 66 | |
| 67 | /* Change emacs_standard_keymap to have bindings for paren matching when |
| 68 | ON_OR_OFF is 1, change them back to self_insert when ON_OR_OFF == 0. */ |
| 69 | void |
| 70 | _rl_enable_paren_matching (on_or_off) |
| 71 | int on_or_off; |
| 72 | { |
| 73 | if (on_or_off) |
| 74 | { /* ([{ */ |
| 75 | rl_bind_key_in_map (')', rl_insert_close, emacs_standard_keymap); |
| 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 | } |
| 79 | else |
| 80 | { /* ([{ */ |
| 81 | rl_bind_key_in_map (')', rl_insert, 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 | } |
| 85 | } |
| 86 | |
| 87 | int |
| 88 | rl_insert_close (count, invoking_key) |
| 89 | int count, invoking_key; |
| 90 | { |
| 91 | if (rl_explicit_arg || !rl_blink_matching_paren) |
| 92 | rl_insert (count, invoking_key); |
| 93 | else |
| 94 | { |
| 95 | #if defined (HAVE_SELECT) |
| 96 | int orig_point, match_point, ready; |
| 97 | struct timeval timer; |
| 98 | fd_set readfds; |
| 99 | |
| 100 | rl_insert (1, invoking_key); |
| 101 | (*rl_redisplay_function) (); |
| 102 | match_point = |
| 103 | find_matching_open (rl_line_buffer, rl_point - 2, invoking_key); |
| 104 | |
| 105 | /* Emacs might message or ring the bell here, but I don't. */ |
| 106 | if (match_point < 0) |
| 107 | return -1; |
| 108 | |
| 109 | FD_ZERO (&readfds); |
| 110 | FD_SET (fileno (rl_instream), &readfds); |
| 111 | timer.tv_sec = 0; |
| 112 | timer.tv_usec = 500000; |
| 113 | |
| 114 | orig_point = rl_point; |
| 115 | rl_point = match_point; |
| 116 | (*rl_redisplay_function) (); |
| 117 | ready = select (1, &readfds, (fd_set *)NULL, (fd_set *)NULL, &timer); |
| 118 | rl_point = orig_point; |
| 119 | #else /* !HAVE_SELECT */ |
| 120 | rl_insert (count, invoking_key); |
| 121 | #endif /* !HAVE_SELECT */ |
| 122 | } |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | static int |
| 127 | find_matching_open (string, from, closer) |
| 128 | char *string; |
| 129 | int from, closer; |
| 130 | { |
| 131 | register int i; |
| 132 | int opener, level, delimiter; |
| 133 | |
| 134 | switch (closer) |
| 135 | { |
| 136 | case ']': opener = '['; break; |
| 137 | case '}': opener = '{'; break; |
| 138 | case ')': opener = '('; break; |
| 139 | default: |
| 140 | return (-1); |
| 141 | } |
| 142 | |
| 143 | level = 1; /* The closer passed in counts as 1. */ |
| 144 | delimiter = 0; /* Delimited state unknown. */ |
| 145 | |
| 146 | for (i = from; i > -1; i--) |
| 147 | { |
| 148 | if (delimiter && (string[i] == delimiter)) |
| 149 | delimiter = 0; |
| 150 | else if (rl_basic_quote_characters && strchr (rl_basic_quote_characters, string[i])) |
| 151 | delimiter = string[i]; |
| 152 | else if (!delimiter && (string[i] == closer)) |
| 153 | level++; |
| 154 | else if (!delimiter && (string[i] == opener)) |
| 155 | level--; |
| 156 | |
| 157 | if (!level) |
| 158 | break; |
| 159 | } |
| 160 | return (i); |
| 161 | } |