Rename modes and utility functions
This commit is contained in:
parent
3962f30539
commit
112e20f4b1
1 changed files with 20 additions and 20 deletions
40
main.cpp
40
main.cpp
|
@ -1,19 +1,17 @@
|
|||
#include <ncurses.h>
|
||||
#include <curses.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <fstream>
|
||||
|
||||
#include "treap.hpp"
|
||||
|
||||
using std::string;
|
||||
using std::vector;
|
||||
|
||||
#define ESC 27
|
||||
#define ENTER 10
|
||||
#define BS 127
|
||||
#define adds(s) addstr(s.c_str())
|
||||
enum mode_type { insert, normal };
|
||||
enum mode_type { INSERT, NORMAL, SELECT };
|
||||
|
||||
|
||||
// Global variables
|
||||
|
@ -31,10 +29,11 @@ string get_line(int r) {
|
|||
}
|
||||
char get(int r, int s) { return get_line(file_offset+r)[s]; }
|
||||
void set(int r, int s, char ch) { file.find(file_offset+r)->text[s] = ch; }
|
||||
void insert_line(int r, string text = "") { file.insert(file_offset+r, text); }
|
||||
void insert_char(int r, int s, char ch) { file.find(file_offset+r)->text.insert(s, string{ch}); }
|
||||
void remove_char(int r, int s, int len=1) { file.find(file_offset+r)->text.erase(s,len); }
|
||||
void remove_line(int r) { file.remove(file_offset+r); }
|
||||
void new_line(int r, string text = "") { file.insert(file_offset+r, text); }
|
||||
void insert(int r, int s, string t) { file.find(file_offset+r)->text.insert(s, t); }
|
||||
void insert(int r, int s, char ch) { insert(r, s, string{ch}); }
|
||||
void remove(int r, int s, int len=1) { file.find(file_offset+r)->text.erase(s,len); }
|
||||
void remove(int r) { file.remove(file_offset+r); }
|
||||
|
||||
int get_number() {
|
||||
char ch = '0';
|
||||
|
@ -54,7 +53,7 @@ bool load(string filename) {
|
|||
|
||||
string line;
|
||||
while (std::getline(infile, line))
|
||||
file.insert(file.size(), line);
|
||||
file.append(line);
|
||||
return 0;
|
||||
}
|
||||
// Save file from buffer
|
||||
|
@ -92,7 +91,7 @@ void print_file(int start = 0) {
|
|||
string split_line(int r, int s) {
|
||||
string line = get_line(r);
|
||||
string ret = line.substr(s, line.size());
|
||||
remove_char(r, s, line.size()-s);
|
||||
remove(r, s, line.size()-s);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -169,7 +168,7 @@ int main(int argc, char* argv[]) {
|
|||
refresh();
|
||||
print_file();
|
||||
|
||||
mode_type mode = normal;
|
||||
mode_type mode = NORMAL;
|
||||
|
||||
// Main loop
|
||||
bool run = true;
|
||||
|
@ -177,7 +176,7 @@ int main(int argc, char* argv[]) {
|
|||
move(row, col);
|
||||
char ch = getch();
|
||||
switch(mode) {
|
||||
case normal:
|
||||
case NORMAL:
|
||||
print_line(row);
|
||||
switch(ch) {
|
||||
case 'h': case 'j': case 'k': case 'l':
|
||||
|
@ -188,15 +187,15 @@ int main(int argc, char* argv[]) {
|
|||
print_file();
|
||||
break;
|
||||
case 'd':
|
||||
remove_line(row);
|
||||
remove(row);
|
||||
print_file(row);
|
||||
break;
|
||||
case 'x':
|
||||
remove_char(row, col);
|
||||
remove(row, col);
|
||||
print_line(row);
|
||||
break;
|
||||
case 'o':
|
||||
insert_line(row);
|
||||
new_line(row);
|
||||
print_file(row);
|
||||
break;
|
||||
case 'q':
|
||||
|
@ -206,33 +205,34 @@ int main(int argc, char* argv[]) {
|
|||
save(filename);
|
||||
break;
|
||||
case 'i':
|
||||
mode = insert;
|
||||
mode = INSERT;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case insert:
|
||||
case INSERT:
|
||||
switch(ch) {
|
||||
case ESC:
|
||||
mode = normal;
|
||||
mode = NORMAL;
|
||||
print_line(row);
|
||||
break;
|
||||
case BS:
|
||||
if(col>0)
|
||||
remove_char(row, --col);
|
||||
remove(row, --col);
|
||||
print_line(row);
|
||||
break;
|
||||
case ENTER:
|
||||
insert_line(row+1, split_line(row, col));
|
||||
new_line(row+1, split_line(row, col));
|
||||
print_file(row++);
|
||||
jump_line_end();
|
||||
break;
|
||||
default:
|
||||
insert_char(row, col++, ch);
|
||||
insert(row, col++, ch);
|
||||
print_line(row);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
refresh();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue