diff options
| -rw-r--r-- | editor.c | 43 | ||||
| -rw-r--r-- | editor.h | 6 | ||||
| -rw-r--r-- | input.c | 54 | ||||
| -rw-r--r-- | input.h | 4 | 
4 files changed, 62 insertions, 45 deletions
| @@ -51,6 +51,49 @@ char* editor_prompt(struct editor_state* editor, char* prompt, void (*callback)(  	return NULL;  } +void editor_move_left(struct editor_state *editor) +{ +	if (editor->cursor_x != 0) { +		editor->cursor_x--; +	} else if (editor->cursor_y > 0) { +		editor->cursor_y--; +		editor->cursor_x = editor->rows[editor->cursor_y].size; +	} +} + +void editor_move_right(struct editor_state *editor) +{ +	struct editor_row* row = (editor->cursor_y >= editor->row_count) ? NULL : &editor->rows[editor->cursor_y]; +	if (row && editor->cursor_x < row->size) { +		editor->cursor_x++; +	} else if (row && editor->cursor_x == row->size) { +		editor->cursor_y++; +		editor->cursor_x = 0; +	} +} + +void editor_move_up(struct editor_state *editor) +{ +	if (editor->cursor_y != 0) +		editor->cursor_y--; + +	struct editor_row *row = (editor->cursor_y >= editor->row_count) ? NULL : &editor->rows[editor->cursor_y]; +	int row_length = row ? row->size : 0; +	if (editor->cursor_x > row_length) +		editor->cursor_x = row_length; +} + +void editor_move_down(struct editor_state *editor) +{ +	if (editor->cursor_y != editor->row_count - 1) +		editor->cursor_y++; + +	struct editor_row *row = (editor->cursor_y >= editor->row_count) ? NULL : &editor->rows[editor->cursor_y]; +	int row_length = row ? row->size : 0; +	if (editor->cursor_x > row_length) +		editor->cursor_x = row_length; +} +  void editor_insert_char(struct editor_state* editor, int c)  {  	if (editor->cursor_y == editor->row_count) @@ -23,9 +23,13 @@ struct editor_state {  void init_editor(struct editor_state* editor);  void editor_set_status_message(struct editor_state* editor, const char* format, ...); -void editor_refresh_screen(struct editor_state* editor);  char* editor_prompt(struct editor_state* editor, char* prompt, void (*callback)(struct editor_state*, char*, int)); +void editor_move_left(struct editor_state *); +void editor_move_right(struct editor_state *); +void editor_move_up(struct editor_state *); +void editor_move_down(struct editor_state *); +  void editor_insert_char(struct editor_state* editor, int c);  void editor_insert_newline(struct editor_state* editor);  void editor_delete_char(struct editor_state* editor); @@ -7,41 +7,6 @@  #include "file.h"  #include "row.h" -void editor_move_cursor(struct editor_state* editor, int key) -{ -	struct editor_row* row = (editor->cursor_y >= editor->row_count) ? NULL : &editor->rows[editor->cursor_y]; -	 -	switch (key) { -		case ARROW_LEFT: -			if (editor->cursor_x != 0) { -				editor->cursor_x--; -			} else if (editor->cursor_y > 0) { -				editor->cursor_y--; -				editor->cursor_x = editor->rows[editor->cursor_y].size; -			} -			break; -		case ARROW_RIGHT: -			if (row && editor->cursor_x < row->size) { -				editor->cursor_x++; -			} else if (row && editor->cursor_x == row->size) { -				editor->cursor_y++; -				editor->cursor_x = 0; -			} -			break; -		case ARROW_UP: -			if (editor->cursor_y != 0) editor->cursor_y--; -			break; -		case ARROW_DOWN: -			if (editor->cursor_y != editor->row_count - 1) editor->cursor_y++; -			break; -	} - -	row = (editor->cursor_y >= editor->row_count) ? NULL : &editor->rows[editor->cursor_y]; -	int row_length = row ? row->size : 0; -	if (editor->cursor_x > row_length) -		editor->cursor_x = row_length; -} -  void editor_process_keypress(struct editor_state* editor, int c)  {  	static int quit_message = 1; @@ -74,7 +39,7 @@ void editor_process_keypress(struct editor_state* editor, int c)  		case BACKSPACE:  		case DELETE_KEY:  			if (c == DELETE_KEY)  -				editor_move_cursor(editor, ARROW_RIGHT); +				editor_move_right(editor);  			editor_delete_char(editor);  			break;  		case PAGE_UP: @@ -87,17 +52,24 @@ void editor_process_keypress(struct editor_state* editor, int c)  					if (editor->cursor_y > editor->row_count)  						editor->cursor_y = editor->row_count;  				} - -				int times = editor->screen_rows; -				while (times--) -					editor_move_cursor(editor, c == PAGE_UP ? ARROW_UP : ARROW_DOWN); +				/* +				 * TODO: Reimplement pageup/pagedown, this time by scrolling the +				 * screen without necessarily changing the position of the +				 * cursor relative to the screen +				 */  			}  			break;  		case ARROW_UP: +			editor_move_up(editor); +			break;  		case ARROW_DOWN: +			editor_move_down(editor); +			break;  		case ARROW_LEFT: +			editor_move_left(editor); +			break;  		case ARROW_RIGHT: -			editor_move_cursor(editor, c); +			editor_move_right(editor);  			break;  		case CTRL_KEY('l'):  		case '\x1b': @@ -18,8 +18,6 @@ enum editor_key {  #define CTRL_KEY(k) ((k) & 0x1f) -int editor_read_key(); -void editor_move_cursor(struct editor_state* editor, int key); -void editor_process_keypress(struct editor_state* editor, int keycode); +void editor_process_keypress(struct editor_state *editor, int keycode);  #endif | 
