added indent and unindent commmands

master
bx 2022-04-02 16:12:22 +00:00
parent 5ed3ab8aa6
commit 054e0b02e8
5 changed files with 36 additions and 2 deletions

23
c_indent.c 100644
View File

@ -0,0 +1,23 @@
void
c_indent(char *c) {
Row *row = b_getline(buffer, atoi(c + 2));
if (row == NULL) { puts ("invalid line"); return; }
for(int i = ROW_SIZE - 1; i >= 1; i--) {
row->text[i] = row->text[i - 1];
}
row->text[0] = '\t';
}
void
c_unindent(char *c) {
Row *row = b_getline(buffer, atoi(c + 2));
if (row == NULL) { puts ("invalid line"); return; }
for(int i = 0; i < ROW_SIZE - 1; i++) {
row->text[i] = row->text[i + 1];
}
row->text[ROW_SIZE] = 0;
}

View File

@ -11,3 +11,7 @@ NULL,
['p'] = NULL, ['p'] = NULL,
['p'] = c_print, ['p'] = c_print,
['P'] = NULL, ['P'] = NULL,
[']'] = c_indent,
['['] = c_unindent,

2
efn.c
View File

@ -22,3 +22,5 @@
#include "c_save.c" #include "c_save.c"
#include "c_insertline.c" #include "c_insertline.c"
#include "c_indent.c"

4
efn.h
View File

@ -14,3 +14,7 @@ Row *b_getline(Row *, int);
void c_writeline(char *); void c_writeline(char *);
void c_save(char*); void c_save(char*);
void c_insertline(char *); void c_insertline(char *);
void c_indent(char *);
void c_unindent(char *);

5
main.c
View File

@ -3,8 +3,8 @@ main(int argc, char **argv) {
static char buf[COM_BUF_SIZE]; static char buf[COM_BUF_SIZE];
while(1) { while(1) {
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
printf(" > "); printf(" > ");
char *com = fgets(buf, sizeof(buf), stdin); char *com = fgets(buf, sizeof(buf), stdin);
for (int i = 0; i < sizeof(buf); i++) for (int i = 0; i < sizeof(buf); i++)
if (buf[i] == '\n') if (buf[i] == '\n')
buf[i] = '\0'; buf[i] = '\0';
@ -18,3 +18,4 @@ char *com = fgets(buf, sizeof(buf), stdin);