Got most keys added to basic key handling
This commit is contained in:
parent
c88b19a4da
commit
7a8982cf19
85
csedit.c
85
csedit.c
|
@ -18,14 +18,16 @@
|
|||
|
||||
enum editorMode {CommandMode, InputMode, VisualMode};
|
||||
enum editorKey {
|
||||
BACKSPACE = 127,
|
||||
ARROW_LEFT = 1000,
|
||||
ARROW_RIGHT,
|
||||
ARROW_UP,
|
||||
ARROW_DOWN,
|
||||
PAGE_UP,
|
||||
PAGE_DOWN,
|
||||
DELETE,
|
||||
BACKSPACE
|
||||
DEL_KEY,
|
||||
HOME_KEY,
|
||||
END_KEY
|
||||
};
|
||||
|
||||
//
|
||||
|
@ -92,11 +94,33 @@ int editorReadKey() {
|
|||
if (read(STDIN_FILENO, &seq[0], 1) != 1) return '\033';
|
||||
if (read(STDIN_FILENO, &seq[1], 1) != 1) return '\033';
|
||||
if (seq[0] == '[') {
|
||||
if (seq[1] >= '0' && seq[1] <= '9') {
|
||||
if (read(STDIN_FILENO, &seq[2], 1) != 1) return '\033';
|
||||
if (seq[2] == '~') {
|
||||
switch (seq[1]) {
|
||||
case '1': return HOME_KEY;
|
||||
case '3': return DEL_KEY;
|
||||
case '4': return END_KEY;
|
||||
case '5': return PAGE_UP;
|
||||
case '6': return PAGE_DOWN;
|
||||
case '7': return HOME_KEY;
|
||||
case '8': return END_KEY;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
switch (seq[1]) {
|
||||
case 'A': return ARROW_UP;
|
||||
case 'B': return ARROW_DOWN;
|
||||
case 'C': return ARROW_RIGHT;
|
||||
case 'D': return ARROW_LEFT;
|
||||
case 'H': return HOME_KEY;
|
||||
case 'F': return END_KEY;
|
||||
}
|
||||
}
|
||||
} else if (seq[0] == 'O') {
|
||||
switch (seq[1]) {
|
||||
case 'H': return HOME_KEY;
|
||||
case 'F': return END_KEY;
|
||||
}
|
||||
}
|
||||
return '\033';
|
||||
|
@ -236,14 +260,16 @@ void editorMoveCursor(int key) {
|
|||
}
|
||||
}
|
||||
|
||||
void editorPageCursor(int key) {
|
||||
if (key == PAGE_UP || key == PAGE_DOWN) {
|
||||
int times = E.screenRows;
|
||||
while (times--)
|
||||
editorMoveCursor(key == PAGE_UP ? ARROW_UP : ARROW_DOWN);
|
||||
}
|
||||
}
|
||||
|
||||
void editorInputKp(int c) {
|
||||
switch (c) {
|
||||
case ARROW_DOWN:
|
||||
case ARROW_UP:
|
||||
case ARROW_LEFT:
|
||||
case ARROW_RIGHT:
|
||||
editorMoveCursor(c);
|
||||
break;
|
||||
case '\033':
|
||||
E.mode = CommandMode;
|
||||
default:
|
||||
|
@ -253,23 +279,29 @@ void editorInputKp(int c) {
|
|||
|
||||
void editorCommandKp(int c) {
|
||||
switch (c) {
|
||||
case 'q':
|
||||
quit();
|
||||
break;
|
||||
case 'j':
|
||||
case 'k':
|
||||
case 'h':
|
||||
case 'l':
|
||||
case ARROW_DOWN:
|
||||
case ARROW_UP:
|
||||
case ARROW_LEFT:
|
||||
case ARROW_RIGHT:
|
||||
editorMoveCursor(c);
|
||||
break;
|
||||
case 'i':
|
||||
E.mode = InputMode;
|
||||
break;
|
||||
case 'u':
|
||||
editorPageCursor(PAGE_UP);
|
||||
break;
|
||||
case 'd':
|
||||
editorPageCursor(PAGE_DOWN);
|
||||
break;
|
||||
case '$':
|
||||
E.cx = E.screenCols - 1;
|
||||
break;
|
||||
case '0':
|
||||
case '^':
|
||||
E.cx = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void editorProcessKeypress() {
|
||||
|
@ -278,6 +310,27 @@ void editorProcessKeypress() {
|
|||
case CTRL_KEY('q'):
|
||||
quit();
|
||||
break;
|
||||
case ARROW_DOWN:
|
||||
case ARROW_UP:
|
||||
case ARROW_LEFT:
|
||||
case ARROW_RIGHT:
|
||||
editorMoveCursor(c);
|
||||
return;
|
||||
case PAGE_UP:
|
||||
case PAGE_DOWN:
|
||||
editorPageCursor(c);
|
||||
return;
|
||||
case CTRL_KEY('u'):
|
||||
editorPageCursor(PAGE_UP);
|
||||
return;
|
||||
case CTRL_KEY('d'):
|
||||
editorPageCursor(PAGE_DOWN);
|
||||
return;
|
||||
case HOME_KEY:
|
||||
E.cx = 0;
|
||||
break;
|
||||
case END_KEY:
|
||||
E.cx = E.screenCols - 1;
|
||||
}
|
||||
|
||||
if (E.mode == CommandMode) {
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
## simple
|
||||
|
||||
g - top of document
|
||||
G - bottom of document
|
||||
^ - beginning of text in row
|
||||
0 - beginning of row
|
||||
$ - end of row
|
||||
hjkl - movement
|
||||
o - open row below in insert
|
||||
O - open row above in insert
|
||||
w - next word
|
||||
e - end of word
|
||||
a - enter insert mode after current position
|
||||
i - enter insert mode
|
||||
u - page up
|
||||
d - page down
|
||||
<esc> - enter command mode
|
||||
^s - save
|
||||
^S - save as
|
||||
^o - open file
|
||||
|
||||
<PAGE_UP>
|
||||
<PAGE_DOWN>
|
||||
<HOME>
|
||||
<END>
|
||||
<ARROWS>
|
||||
|
||||
|
||||
## complex
|
||||
|
||||
- d (<digit> delete __ )
|
||||
- w, j, k, h, l, e, $, ^, d, g, G, 0
|
||||
- <movement> (<digit> <movement>)
|
||||
- w, e, h, j, k, l
|
||||
|
||||
### how it will work
|
||||
Digits get added to a buffer
|
||||
Once an alpha char gets entered the buffer gets converted to an int and stored
|
||||
if the alpha char is a d it gets stored as the action, pending a movement key for direction
|
||||
if the alpha char is a movement key then the int, action (if present) and movement combine to
|
||||
create a loop.
|
||||
|
Loading…
Reference in New Issue