-
Notifications
You must be signed in to change notification settings - Fork 0
/
old_comment.bsh
75 lines (64 loc) · 1.86 KB
/
old_comment.bsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
* Author: Simon Wimmer
*
* Hides the current selection from latex output,
* or attempts to unhide a the current cursor position if no text is selected.
*/
int findMatchingBrace(int start, String left, String right)
{
int count = 0;
right_len = right.length();
left_len = left.length();
for (int i = start; i < buffer.getLength() - right_len; i++) {
if (buffer.getText(i, left_len).equals(left)) {
count++;
} else if (buffer.getText(i, right_len).equals(right)) {
if(count > 0) {
count--;
} else {
return i;
}
}
}
return -1;
}
void toggleComment(View view, String marker, String left, String right)
{
this.view = view;
startString = marker + left;
endString = right;
Selection[] selections = textArea.selection;
if (selections.length != 0){
for(Selection selection: selections)
{
newSelection = textArea.getSelectionAtOffset(textArea.caretPosition);
start = newSelection.start;
buffer.insert(start, startString);
textArea.extendSelection(start, start + startString.length());
buffer.insert(newSelection.end, endString);
}
} else {
// No selection -- attempt to remove comment marker and cartouches
start = textArea.getCaretPosition();
// Remove marker
if (!buffer.getText(start, marker.length()).equals(marker)) return;
buffer.remove(start, marker.length());
// Remove whitespace
while (buffer.getText(start, 1).equals(" ")) {
buffer.remove(start, 1);
}
// Remove left cartouche
if (!buffer.getText(start, left.length()).equals(left)) return;
buffer.remove(start, left.length());
// Find matching cartouche
end = findMatchingBrace(textArea.getCaretPosition(), left, right);
if (end >= 0)
{
buffer.remove(end, right.length());
}
}
}
if(buffer.isReadOnly())
Macros.error(view, "Buffer is read-only.");
else
toggleComment(view, "", "(*", "*)");