Skip to content

Commit

Permalink
Initial support for multiple cursors. Still fragile!
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamhm committed Feb 4, 2017
1 parent 1e4ad8f commit aac2d27
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 46 deletions.
13 changes: 13 additions & 0 deletions Buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ struct Buffer_ {
double lastTime;
// size of Tab (\t) in cells
int tabSize;
int nCursors;
struct {
chars x;
int y;
chars savedX;
int savedY;
chars selectXfrom;
int selectYfrom;
chars selectXto;
int selectYto;
bool selecting;
} cursors[100];
// Lua state
ScriptState script;
bool skipOnChange;
Expand Down Expand Up @@ -241,6 +253,7 @@ Buffer* Buffer_new(int x, int y, int w, int h, char* fileName, bool command, Tab
this->tabulation = 3;
this->dosLineBreaks = false;
this->tabSize = 8;
this->nCursors = 0;

Script_initState(&this->script, tabs, this);

Expand Down
4 changes: 2 additions & 2 deletions Clipboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ struct Clipboard_ {
}*/

Clipboard* Clipboard_new() {
Clipboard* Clipboard_new(bool disk) {
Clipboard* this = (Clipboard*) malloc(sizeof(Clipboard));
sprintf(this->clipFileName, "%s/.clipboard", getenv("HOME"));
this->text = NULL;
this->disk = true;
this->disk = disk;
return this;
}

Expand Down
78 changes: 47 additions & 31 deletions Line.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,36 @@ void Line_updateContext(Line* this) {
buffer->selecting = selecting;
}

static void paintSelection(Line* this, int y, char* out, int* outIdx, int* attrs, int tabSize, int xFrom, int yFrom, int xTo, int yTo) {
if ((y >= yFrom && y <= yTo) || (y >= yTo && y <= yFrom)) {
int from, to;
if (yFrom > yTo || (yFrom == yTo && xFrom > xTo)) {
int tmp = yFrom; yFrom = yTo; yTo = tmp;
tmp = xFrom; xFrom = xTo; xTo = tmp;
}
if (y == yFrom && y == yTo) {
from = Text_cellsUntil(this->text, xFrom, tabSize);
to = Text_cellsUntil(this->text, xTo, tabSize);
} else if (y == yFrom && y < yTo) {
from = Text_cellsUntil(this->text, xFrom, tabSize);
out[(*outIdx)++] = ' ';
out[(*outIdx)] = '\0';
to = *outIdx;
} else if (y > yFrom && y == yTo) {
from = 0;
to = Text_cellsUntil(this->text, xTo, tabSize);
} else { // if (y > yFrom && y < yTo) {
from = 0;
out[(*outIdx)++] = ' ';
out[(*outIdx)] = '\0';
to = *outIdx;
}
for (int i = from; i < to; i++) {
attrs[i] = CRT_colors[SelectionColor];
}
}
}

void Line_display(Object* cast, RichString* str) {
Line* this = (Line*) cast;
Buffer* buffer = (Buffer*)(this->super.list->data);
Expand All @@ -83,7 +113,7 @@ void Line_display(Object* cast, RichString* str) {
memset(attrs, 0, sizeAttrs);

int outIdx = 0;
char out[this->text.bytes * tabSize + 1];
char out[this->text.bytes * tabSize + 2];

HighlightContext* context = this->super.prev
? ((Line*)this->super.prev)->context
Expand Down Expand Up @@ -121,38 +151,24 @@ void Line_display(Object* cast, RichString* str) {
attrs[buffer->bracketX] = CRT_colors[BracketColor];
}

if (buffer->selecting) {
int yFrom = buffer->selectYfrom;
int yTo = buffer->selectYto;
if ((y >= yFrom && y <= yTo) || (y >= yTo && y <= yFrom)) {
int from, to;
int xFrom = buffer->selectXfrom;
int xTo = buffer->selectXto;
if (yFrom > yTo || (yFrom == yTo && xFrom > xTo)) {
int tmp = yFrom; yFrom = yTo; yTo = tmp;
tmp = xFrom; xFrom = xTo; xTo = tmp;
if (buffer->nCursors > 0) {
for (int i = 0; i < buffer->nCursors; i++) {
if (buffer->selecting) {
paintSelection(this, y, out, &outIdx, attrs, tabSize, buffer->cursors[i].selectXfrom, buffer->cursors[i].selectYfrom, buffer->cursors[i].selectXto, buffer->cursors[i].selectYto);
}
if (y == yFrom && y == yTo) {
from = Text_cellsUntil(this->text, xFrom, tabSize);
to = Text_cellsUntil(this->text, xTo, tabSize);
} else if (y == yFrom && y < yTo) {
from = Text_cellsUntil(this->text, xFrom, tabSize);
out[outIdx++] = ' ';
out[outIdx] = '\0';
to = outIdx;
} else if (y > yFrom && y == yTo) {
from = 0;
to = Text_cellsUntil(this->text, xTo, tabSize);
} else { // if (y > yFrom && y < yTo) {
from = 0;
out[outIdx++] = ' ';
out[outIdx] = '\0';
to = outIdx;
}
for (int i = from; i < to; i++) {
attrs[i] = CRT_colors[SelectionColor];
if (buffer->cursors[i].y == y) {
int cx = buffer->cursors[i].x;
attrs[cx] = CRT_colors[AlertColor];
if (cx == UTF8_chars(out)) {
out[outIdx++] = ' ';
out[outIdx] = '\0';
}
}
}
} else {
if (buffer->selecting) {
paintSelection(this, y, out, &outIdx, attrs, tabSize, buffer->selectXfrom, buffer->selectYfrom, buffer->selectXto, buffer->selectYto);
}
}

Text outText = Text_new(out);
Expand Down Expand Up @@ -252,7 +268,7 @@ static StringBuffer* getBlock(Line* this, int lines, int xFrom, int xTo, bool de
lineToBufferFrom(str, l, xFrom);
StringBuffer_addChar(str, '\n');
if (delete) Text_deleteChars(&(l->text), xFrom, Text_chars(l->text) - xFrom);
} else {
} else {
lineToBuffer(str, l);
StringBuffer_addChar(str, '\n');
if (delete) Text_prune(&(l->text));
Expand Down
3 changes: 2 additions & 1 deletion Prototypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ void ListItem_addAfter(ListItem* this, ListItem* item);
void ListItem_remove(ListItem* this);
bool UTF8_isValid(const char* text);
int UTF8_bytes(const char sc);
int UTF8_chars(const char* s);
int UTF8_copyChar(char* sdest, const char* ssrc);
wchar_t UTF8_stringToCodePoint(const char* ss);
const char* UTF8_forward(const char* ss, int n);
Expand Down Expand Up @@ -189,7 +190,7 @@ void Buffer_refresh(Buffer* this);
void Buffer_toggleMarking(Buffer* this);
void Buffer_toggleTabCharacters(Buffer* this);
void Buffer_toggleDosLineBreaks(Buffer* this);
Clipboard* Clipboard_new();
Clipboard* Clipboard_new(bool disk);
void Clipboard_delete(Clipboard* this);
Text Clipboard_get(Clipboard* this);
void Clipboard_set(Clipboard* this, char* text, int len);
Expand Down
12 changes: 12 additions & 0 deletions Structures.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,18 @@ struct Buffer_ {
double lastTime;
// size of Tab (\t) in cells
int tabSize;
int nCursors;
struct {
chars x;
int y;
chars savedX;
int savedY;
chars selectXfrom;
int selectYfrom;
chars selectXto;
int selectYto;
bool selecting;
} cursors[100];
// Lua state
ScriptState script;
bool skipOnChange;
Expand Down
2 changes: 1 addition & 1 deletion Text.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ int UTF8_bytes(const char sc) {
return 1;
}

static int UTF8_chars(const char* s) {
int UTF8_chars(const char* s) {
int i = 0;
unsigned char* c = (unsigned char*) s;
while (*c) {
Expand Down
1 change: 1 addition & 0 deletions bindings/default
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ CTRL_Z Dit_undo
CTRL_INSERT Dit_copy
F3 Dit_find
F4 Buffer_selectAll
F5 Dit_multipleCursors
SHIFT_IC Dit_paste
SHIFT_INSERT Dit_paste
CTRL_SHIFT_INSERT Dit_paste
Expand Down
Loading

0 comments on commit aac2d27

Please sign in to comment.