parens.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* parens.c -- implementation of matching parentheses feature. */
  2. /* Copyright (C) 1987, 1989, 1992-2009 Free Software Foundation, Inc.
  3. This file is part of the GNU Readline Library (Readline), a library
  4. for reading lines of text with interactive input and history editing.
  5. Readline 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 3 of the License, or
  8. (at your option) any later version.
  9. Readline is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Readline. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #define READLINE_LIBRARY
  17. #if defined (__TANDEM)
  18. # include <floss.h>
  19. #endif
  20. #include "rlconf.h"
  21. #if defined (HAVE_CONFIG_H)
  22. # include <config.h>
  23. #endif
  24. #include <stdio.h>
  25. #include <sys/types.h>
  26. #if defined (HAVE_UNISTD_H)
  27. # include <unistd.h>
  28. #endif
  29. #include "posixselect.h"
  30. #if defined (HAVE_STRING_H)
  31. # include <string.h>
  32. #else /* !HAVE_STRING_H */
  33. # include <strings.h>
  34. #endif /* !HAVE_STRING_H */
  35. #if !defined (strchr) && !defined (__STDC__)
  36. extern char *strchr (), *strrchr ();
  37. #endif /* !strchr && !__STDC__ */
  38. #include "readline.h"
  39. #include "rlprivate.h"
  40. static int find_matching_open PARAMS((char *, int, int));
  41. /* Non-zero means try to blink the matching open parenthesis when the
  42. close parenthesis is inserted. */
  43. #if defined (HAVE_SELECT)
  44. int rl_blink_matching_paren = 1;
  45. #else /* !HAVE_SELECT */
  46. int rl_blink_matching_paren = 0;
  47. #endif /* !HAVE_SELECT */
  48. static int _paren_blink_usec = 500000;
  49. /* Change emacs_standard_keymap to have bindings for paren matching when
  50. ON_OR_OFF is 1, change them back to self_insert when ON_OR_OFF == 0. */
  51. void
  52. _rl_enable_paren_matching (on_or_off)
  53. int on_or_off;
  54. {
  55. if (on_or_off)
  56. { /* ([{ */
  57. rl_bind_key_in_map (')', rl_insert_close, emacs_standard_keymap);
  58. rl_bind_key_in_map (']', rl_insert_close, emacs_standard_keymap);
  59. rl_bind_key_in_map ('}', rl_insert_close, emacs_standard_keymap);
  60. }
  61. else
  62. { /* ([{ */
  63. rl_bind_key_in_map (')', rl_insert, emacs_standard_keymap);
  64. rl_bind_key_in_map (']', rl_insert, emacs_standard_keymap);
  65. rl_bind_key_in_map ('}', rl_insert, emacs_standard_keymap);
  66. }
  67. }
  68. int
  69. rl_set_paren_blink_timeout (u)
  70. int u;
  71. {
  72. int o;
  73. o = _paren_blink_usec;
  74. if (u > 0)
  75. _paren_blink_usec = u;
  76. return (o);
  77. }
  78. int
  79. rl_insert_close (count, invoking_key)
  80. int count, invoking_key;
  81. {
  82. if (rl_explicit_arg || !rl_blink_matching_paren)
  83. _rl_insert_char (count, invoking_key);
  84. else
  85. {
  86. #if defined (HAVE_SELECT)
  87. int orig_point, match_point, ready;
  88. struct timeval timer;
  89. fd_set readfds;
  90. _rl_insert_char (1, invoking_key);
  91. (*rl_redisplay_function) ();
  92. match_point =
  93. find_matching_open (rl_line_buffer, rl_point - 2, invoking_key);
  94. /* Emacs might message or ring the bell here, but I don't. */
  95. if (match_point < 0)
  96. return -1;
  97. FD_ZERO (&readfds);
  98. FD_SET (fileno (rl_instream), &readfds);
  99. USEC_TO_TIMEVAL (_paren_blink_usec, timer);
  100. orig_point = rl_point;
  101. rl_point = match_point;
  102. (*rl_redisplay_function) ();
  103. ready = select (1, &readfds, (fd_set *)NULL, (fd_set *)NULL, &timer);
  104. rl_point = orig_point;
  105. #else /* !HAVE_SELECT */
  106. _rl_insert_char (count, invoking_key);
  107. #endif /* !HAVE_SELECT */
  108. }
  109. return 0;
  110. }
  111. static int
  112. find_matching_open (string, from, closer)
  113. char *string;
  114. int from, closer;
  115. {
  116. register int i;
  117. int opener, level, delimiter;
  118. switch (closer)
  119. {
  120. case ']': opener = '['; break;
  121. case '}': opener = '{'; break;
  122. case ')': opener = '('; break;
  123. default:
  124. return (-1);
  125. }
  126. level = 1; /* The closer passed in counts as 1. */
  127. delimiter = 0; /* Delimited state unknown. */
  128. for (i = from; i > -1; i--)
  129. {
  130. if (delimiter && (string[i] == delimiter))
  131. delimiter = 0;
  132. else if (rl_basic_quote_characters && strchr (rl_basic_quote_characters, string[i]))
  133. delimiter = string[i];
  134. else if (!delimiter && (string[i] == closer))
  135. level++;
  136. else if (!delimiter && (string[i] == opener))
  137. level--;
  138. if (!level)
  139. break;
  140. }
  141. return (i);
  142. }