| 4020 | // have identical opening and closing symbols |
| 4021 | // TODO support across multiple lines |
| 4022 | function findBeginningAndEnd(cm, head, symb, inclusive) { |
| 4023 | var cur = copyCursor(head); |
| 4024 | var line = cm.getLine(cur.line); |
| 4025 | var chars = line.split(''); |
| 4026 | var start, end, i, len; |
| 4027 | var firstIndex = chars.indexOf(symb); |
| 4028 | |
| 4029 | // the decision tree is to always look backwards for the beginning first, |
| 4030 | // but if the cursor is in front of the first instance of the symb, |
| 4031 | // then move the cursor forward |
| 4032 | if (cur.ch < firstIndex) { |
| 4033 | cur.ch = firstIndex; |
| 4034 | // Why is this line even here??? |
| 4035 | // cm.setCursor(cur.line, firstIndex+1); |
| 4036 | } |
| 4037 | // otherwise if the cursor is currently on the closing symbol |
| 4038 | else if (firstIndex < cur.ch && chars[cur.ch] == symb) { |
| 4039 | end = cur.ch; // assign end to the current cursor |
| 4040 | --cur.ch; // make sure to look backwards |
| 4041 | } |
| 4042 | |
| 4043 | // if we're currently on the symbol, we've got a start |
| 4044 | if (chars[cur.ch] == symb && !end) { |
| 4045 | start = cur.ch + 1; // assign start to ahead of the cursor |
| 4046 | } else { |
| 4047 | // go backwards to find the start |
| 4048 | for (i = cur.ch; i > -1 && !start; i--) { |
| 4049 | if (chars[i] == symb) { |
| 4050 | start = i + 1; |
| 4051 | } |
| 4052 | } |
| 4053 | } |
| 4054 | |
| 4055 | // look forwards for the end symbol |
| 4056 | if (start && !end) { |
| 4057 | for (i = start, len = chars.length; i < len && !end; i++) { |
| 4058 | if (chars[i] == symb) { |
| 4059 | end = i; |
| 4060 | } |
| 4061 | } |
| 4062 | } |
| 4063 | |
| 4064 | // nothing found |
| 4065 | if (!start || !end) { |
| 4066 | return { start: cur, end: cur }; |
| 4067 | } |
| 4068 | |
| 4069 | // include the symbols |
| 4070 | if (inclusive) { |
| 4071 | --start; ++end; |
| 4072 | } |
| 4073 | |
| 4074 | return { |
| 4075 | start: Pos(cur.line, start), |
| 4076 | end: Pos(cur.line, end) |
| 4077 | }; |
| 4078 | } |
| 4079 | |