ctrl-/ to comment/uncomment line

This commit is contained in:
Kartik K. Agaram 2021-11-28 21:08:12 -08:00
parent eec4845c31
commit e80af9206c
2 changed files with 28 additions and 0 deletions

View File

@ -572,6 +572,26 @@ static void editorDelChar() {
E.dirty++;
}
static void editorUncommentCursorRow() {
erow *row = &E.row[E.rowoff+E.cy];
editorRowDelChar(row, 0);
editorRowDelChar(row, 0);
editorRowDelChar(row, 0);
editorRowDelChar(row, 0);
E.coloff = 0;
E.cx = 0;
}
static void editorCommentCursorRow() {
erow *row = &E.row[E.rowoff+E.cy];
editorRowInsertChar(row, 0, ' ');
editorRowInsertChar(row, 0, '?');
editorRowInsertChar(row, 0, '-');
editorRowInsertChar(row, 0, '-');
E.coloff = 0;
E.cx = 0;
}
/* Load the specified program in the editor memory and returns 0 on success
* or 1 on error. */
int editorOpen(char *filename) {
@ -656,6 +676,7 @@ static void editorMenu(void) {
draw_menu_item("^l", "end of line");
draw_menu_item("^u", "delete to start of line");
draw_menu_item("^k", "delete to end of line");
draw_menu_item("^/", "(un)comment line");
attrset(A_NORMAL);
}
@ -1113,6 +1134,12 @@ static void editorProcessKeypress(lua_State* L) {
editorDelChar();
}
break;
case CTRL_SLASH:
if (starts_with(E.row[E.rowoff+E.cy].chars, "--? "))
editorUncommentCursorRow();
else
editorCommentCursorRow();
break;
case KEY_UP:
case KEY_DOWN:
case KEY_LEFT:

View File

@ -26,6 +26,7 @@ enum KEY_ACTION {
CTRL_U = 21,
CTRL_X = 24,
ESC = 27,
CTRL_SLASH = 31,
};
#endif