Skip to content

Commit

Permalink
F6: "find all" for cursors
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamhm committed Dec 8, 2023
1 parent a1935d9 commit 8dc00e8
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ struct Buffer_ {
bool selecting;
int xLen;
int yLen;
} cursors[100];
} cursors[999];
// Lua state
ScriptState script;
bool skipOnChange;
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ Quick reference
* ...tab - Type text to go to the open tab that matches that substring.
* Ctrl+B - back (to previous location, before last find, go-to-line, tab-switch, etc.)
* You can press Ctrl+B multiple times to go back various levels.
* F5 - multiple cursors
* if you press F5 with a selection, the next cursor will appear at the next occurrence of the selection.
* F6 - find all occurrences of a selection and apply multiple cursors
* Tabs of open files:
* Ctrl+J - previous tab
* Ctrl+K - next tab
Expand Down
2 changes: 1 addition & 1 deletion Structures.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ struct Buffer_ {
bool selecting;
int xLen;
int yLen;
} cursors[100];
} cursors[999];
// Lua state
ScriptState script;
bool skipOnChange;
Expand Down
1 change: 1 addition & 0 deletions bindings/default
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ F3 Dit_find
F12 Buffer_selectAll
F5 Dit_multipleCursors
SHIFT_F5 Dit_decreaseMultipleCursors
F6 Dit_findAllCursors
SHIFT_IC Dit_paste
SHIFT_INSERT Dit_paste
CTRL_SHIFT_INSERT Dit_paste
Expand Down
50 changes: 48 additions & 2 deletions dit.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ static bool confirmClose(Buffer* buffer, TabManager* tabs, char* question) {

static Clipboard* Dit_clipboard = NULL;

static Clipboard* Dit_multipleClipboards[100] = { NULL };
static Clipboard* Dit_multipleClipboards[999] = { NULL };

static int xclipOk = 1;

Expand Down Expand Up @@ -588,7 +588,7 @@ static void Dit_decreaseMultipleCursors(Buffer* buffer) {
}

static void Dit_multipleCursors(Buffer* buffer) {
if (buffer->nCursors == 100) {
if (buffer->nCursors == 999) {
return;
}
int c = buffer->nCursors;
Expand Down Expand Up @@ -664,6 +664,50 @@ static void Dit_multipleCursors(Buffer* buffer) {
Buffer_goto(buffer, newX, newY, true);
}

static void Dit_findAllCursors(Buffer* buffer) {
if (buffer->nCursors == 999) {
return;
}
if (!buffer->selecting) {
return;
}
if (buffer->selectYfrom != buffer->selectYto) {
return;
}
if (buffer->nCursors == 0) {
Dit_multipleCursors(buffer);
}

Coords found = { .x = NOT_A_COORD, .y = NOT_A_COORD };

int blockLen;
char* block = Buffer_copyBlock(buffer, &blockLen);
if (!block) {
return;
}
Text text = Text_new(block);

int firstX = buffer->cursors[buffer->nCursors - 1].selectXfrom;
int firstY = buffer->cursors[buffer->nCursors - 1].selectYfrom;

for (;;) {
if (buffer->nCursors == 999) {
break;
}
found = Buffer_find(buffer, text, true, true, false, true);
if (found.x == NOT_A_COORD) {
break;
}
if (found.x == firstX && found.y == firstY) {
break;
}

Dit_multipleCursors(buffer);
}

free(block);
}

static bool canKeyDoMultiple(int ch, int limit, Dit_Action* keys) {
if (ch > limit)
return true;
Expand Down Expand Up @@ -1219,6 +1263,7 @@ static void Dit_registerActions() {
Hashtable_putString(Dit_actions, "Dit_wordWrap", (void*)(long) Dit_wordWrap);
Hashtable_putString(Dit_actions, "Dit_multipleCursors", (void*)(long) Dit_multipleCursors);
Hashtable_putString(Dit_actions, "Dit_decreaseMultipleCursors", (void*)(long) Dit_decreaseMultipleCursors);
Hashtable_putString(Dit_actions, "Dit_findAllCursors", (void*)(long) Dit_findAllCursors);
}

static void Dit_loadHardcodedBindings(Dit_Action* keys) {
Expand Down Expand Up @@ -1254,6 +1299,7 @@ static void Dit_loadHardcodedBindings(Dit_Action* keys) {
keys[KEY_CS_INSERT] = (Dit_Action) Dit_paste;
keys[KEY_SDC] = (Dit_Action) Dit_cut;
keys[KEY_F(5)] = (Dit_Action) Dit_multipleCursors;
keys[KEY_F(6)] = (Dit_Action) Dit_findAllCursors;
keys[KEY_F(8)] = (Dit_Action) Dit_deleteLine;
keys[KEY_F(10)] = (Dit_Action) Dit_quit;
keys[0x0d] = (Dit_Action) Dit_breakLine;
Expand Down

0 comments on commit 8dc00e8

Please sign in to comment.