Relicense Sortix to the ISC license.
I hereby relicense all my work on Sortix under the ISC license as below.
All Sortix contributions by other people are already under this license,
are not substantial enough to be copyrightable, or have been removed.
All imported code from other projects is compatible with this license.
All GPL licensed code from other projects had previously been removed.
Copyright 2011-2016 Jonas 'Sortie' Termansen and contributors.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2016-03-02 22:38:16 +00:00
|
|
|
/*
|
2016-03-05 23:49:55 +00:00
|
|
|
* Copyright (c) 2013, 2014, 2016 Jonas 'Sortie' Termansen.
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
* Copyright (c) 2021 Juhani 'nortti' Krekelä.
|
Relicense Sortix to the ISC license.
I hereby relicense all my work on Sortix under the ISC license as below.
All Sortix contributions by other people are already under this license,
are not substantial enough to be copyrightable, or have been removed.
All imported code from other projects is compatible with this license.
All GPL licensed code from other projects had previously been removed.
Copyright 2011-2016 Jonas 'Sortie' Termansen and contributors.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2016-03-02 22:38:16 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
* command.c
|
|
|
|
* Editor commands.
|
|
|
|
*/
|
2014-08-23 19:26:38 +00:00
|
|
|
|
|
|
|
#include <assert.h>
|
2016-02-28 16:36:45 +00:00
|
|
|
#include <stdbool.h>
|
2014-08-23 19:26:38 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <wchar.h>
|
|
|
|
#include <wctype.h>
|
|
|
|
|
2016-02-28 16:36:45 +00:00
|
|
|
#include "command.h"
|
|
|
|
#include "cursor.h"
|
|
|
|
#include "display.h"
|
|
|
|
#include "editor.h"
|
|
|
|
#include "multibyte.h"
|
|
|
|
#include "terminal.h"
|
2014-08-23 19:26:38 +00:00
|
|
|
|
|
|
|
void editor_type_newline(struct editor* editor)
|
|
|
|
{
|
|
|
|
editor->dirty = true;
|
|
|
|
|
|
|
|
if ( editor->lines_used == editor->lines_length )
|
|
|
|
{
|
|
|
|
size_t new_length = editor->lines_length ? 2 * editor->lines_length : 8;
|
2016-02-28 16:36:45 +00:00
|
|
|
struct line* new_lines = (struct line*)
|
|
|
|
malloc(sizeof(struct line) * new_length);
|
2014-08-23 19:26:38 +00:00
|
|
|
for ( size_t i = 0; i < editor->lines_used; i++ )
|
|
|
|
new_lines[i] = editor->lines[i];
|
2016-02-28 16:36:45 +00:00
|
|
|
free(editor->lines);
|
2014-08-23 19:26:38 +00:00
|
|
|
editor->lines = new_lines;
|
|
|
|
editor->lines_length = new_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( size_t i = editor->lines_used-1; editor->cursor_row < i; i-- )
|
|
|
|
editor->lines[i+1] = editor->lines[i];
|
|
|
|
editor->lines_used++;
|
|
|
|
|
|
|
|
struct line old_line = editor->lines[editor->cursor_row];
|
|
|
|
|
|
|
|
size_t keep_length = editor->cursor_column;
|
|
|
|
size_t move_length = old_line.used - keep_length;
|
|
|
|
|
|
|
|
struct line* keep_line = &editor->lines[editor->cursor_row];
|
|
|
|
struct line* move_line = &editor->lines[editor->cursor_row+1];
|
|
|
|
|
2016-02-28 16:36:45 +00:00
|
|
|
keep_line->data = (wchar_t*) malloc(sizeof(wchar_t) * keep_length);
|
2014-08-23 19:26:38 +00:00
|
|
|
keep_line->used = keep_length;
|
|
|
|
keep_line->length = keep_length;
|
2021-04-27 20:52:04 +00:00
|
|
|
if ( keep_length )
|
|
|
|
memcpy(keep_line->data, old_line.data + 0,
|
|
|
|
sizeof(wchar_t) * keep_length);
|
2014-08-23 19:26:38 +00:00
|
|
|
|
2016-02-28 16:36:45 +00:00
|
|
|
move_line->data = (wchar_t*) malloc(sizeof(wchar_t) * move_length);
|
2014-08-23 19:26:38 +00:00
|
|
|
move_line->used = move_length;
|
|
|
|
move_line->length = move_length;
|
2021-04-27 20:52:04 +00:00
|
|
|
if ( move_length )
|
|
|
|
memcpy(move_line->data, old_line.data + keep_length,
|
|
|
|
sizeof(wchar_t) * move_length);
|
2014-08-23 19:26:38 +00:00
|
|
|
|
|
|
|
editor_cursor_set(editor, editor->cursor_row+1, 0);
|
|
|
|
|
2016-02-28 16:36:45 +00:00
|
|
|
free(old_line.data);
|
2014-08-23 19:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void editor_type_delete_selection(struct editor* editor);
|
|
|
|
|
|
|
|
void editor_type_combine_with_last(struct editor* editor)
|
|
|
|
{
|
|
|
|
if ( !editor->cursor_row )
|
|
|
|
return;
|
|
|
|
|
|
|
|
editor->dirty = true;
|
|
|
|
|
|
|
|
struct line* keep_line = &editor->lines[editor->cursor_row-1];
|
|
|
|
struct line* gone_line = &editor->lines[editor->cursor_row];
|
|
|
|
|
|
|
|
wchar_t* keep_line_data = keep_line->data;
|
|
|
|
wchar_t* gone_line_data = gone_line->data;
|
|
|
|
|
|
|
|
size_t new_length = keep_line->used + gone_line->used;
|
2016-02-28 16:36:45 +00:00
|
|
|
wchar_t* new_data = (wchar_t*) malloc(sizeof(wchar_t) * new_length);
|
2014-08-23 19:26:38 +00:00
|
|
|
|
|
|
|
memcpy(new_data, keep_line_data, sizeof(wchar_t) * keep_line->used);
|
|
|
|
memcpy(new_data + keep_line->used, gone_line_data, sizeof(wchar_t) * gone_line->used);
|
|
|
|
|
|
|
|
editor_cursor_set(editor, editor->cursor_row-1, keep_line->used);
|
|
|
|
|
|
|
|
keep_line->data = new_data;
|
|
|
|
keep_line->used = new_length;
|
|
|
|
keep_line->length = new_length;
|
|
|
|
|
|
|
|
editor->lines_used--;
|
|
|
|
for ( size_t i = editor->cursor_row + 1; i < editor->lines_used; i++ )
|
|
|
|
editor->lines[i] = editor->lines[i+1];
|
|
|
|
|
|
|
|
free(keep_line_data);
|
|
|
|
free(gone_line_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void editor_type_backspace(struct editor* editor)
|
|
|
|
{
|
|
|
|
if ( !(editor->select_row == editor->cursor_row &&
|
|
|
|
editor->select_column == editor->cursor_column) )
|
|
|
|
{
|
|
|
|
editor_type_delete_selection(editor);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct line* current_line = &editor->lines[editor->cursor_row];
|
|
|
|
|
|
|
|
if ( !editor->cursor_column )
|
|
|
|
{
|
|
|
|
editor_type_combine_with_last(editor);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
editor->dirty = true;
|
|
|
|
|
|
|
|
current_line->used--;
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
for ( size_t i = editor->cursor_column - 1; i < current_line->used; i++ )
|
2014-08-23 19:26:38 +00:00
|
|
|
current_line->data[i] = current_line->data[i+1];
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
editor_cursor_set(editor, editor->cursor_row, editor->cursor_column - 1);
|
2014-08-23 19:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void editor_type_combine_with_next(struct editor* editor)
|
|
|
|
{
|
|
|
|
if ( editor->cursor_row + 1 == editor->lines_used )
|
|
|
|
return;
|
|
|
|
|
|
|
|
editor->dirty = true;
|
|
|
|
|
|
|
|
struct line* keep_line = &editor->lines[editor->cursor_row];
|
|
|
|
struct line* gone_line = &editor->lines[editor->cursor_row+1];
|
|
|
|
|
|
|
|
wchar_t* keep_line_data = keep_line->data;
|
|
|
|
wchar_t* gone_line_data = gone_line->data;
|
|
|
|
|
|
|
|
size_t new_length = keep_line->used + gone_line->used;
|
2016-02-28 16:36:45 +00:00
|
|
|
wchar_t* new_data = (wchar_t*) malloc(sizeof(wchar_t) * new_length);
|
2014-08-23 19:26:38 +00:00
|
|
|
|
|
|
|
memcpy(new_data, keep_line_data, sizeof(wchar_t) * keep_line->used);
|
|
|
|
memcpy(new_data + keep_line->used, gone_line_data, sizeof(wchar_t) * gone_line->used);
|
|
|
|
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
editor_cursor_set(editor, editor->cursor_row, keep_line->used);
|
2014-08-23 19:26:38 +00:00
|
|
|
|
|
|
|
keep_line->data = new_data;
|
|
|
|
keep_line->used = new_length;
|
|
|
|
keep_line->length = new_length;
|
|
|
|
|
|
|
|
editor->lines_used--;
|
|
|
|
for ( size_t i = editor->cursor_row + 1; i < editor->lines_used; i++ )
|
|
|
|
editor->lines[i] = editor->lines[i+1];
|
|
|
|
|
|
|
|
free(keep_line_data);
|
|
|
|
free(gone_line_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void editor_type_delete(struct editor* editor)
|
|
|
|
{
|
|
|
|
if ( !(editor->select_row == editor->cursor_row &&
|
|
|
|
editor->select_column == editor->cursor_column) )
|
|
|
|
{
|
|
|
|
editor_type_delete_selection(editor);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct line* current_line = &editor->lines[editor->cursor_row];
|
|
|
|
|
|
|
|
if ( editor->cursor_column == current_line->used )
|
|
|
|
{
|
|
|
|
editor_type_combine_with_next(editor);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
editor->dirty = true;
|
|
|
|
|
|
|
|
current_line->used--;
|
|
|
|
for ( size_t i = editor->cursor_column; i < current_line->used; i++ )
|
|
|
|
current_line->data[i] = current_line->data[i+1];
|
|
|
|
}
|
|
|
|
|
|
|
|
void editor_type_delete_selection(struct editor* editor)
|
|
|
|
{
|
|
|
|
if ( is_row_column_lt(editor->select_row, editor->select_column,
|
|
|
|
editor->cursor_row, editor->cursor_column) )
|
|
|
|
{
|
|
|
|
size_t tmp;
|
|
|
|
tmp = editor->select_row;
|
|
|
|
editor->select_row = editor->cursor_row;
|
|
|
|
editor->cursor_row = tmp;
|
|
|
|
tmp = editor->select_column;
|
|
|
|
editor->select_column = editor->cursor_column;
|
|
|
|
editor->cursor_column = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t desired_row = editor->cursor_row;
|
|
|
|
size_t desired_column = editor->cursor_column;
|
|
|
|
|
|
|
|
editor->cursor_row = editor->select_row;
|
|
|
|
editor->cursor_column = editor->select_column;
|
|
|
|
|
|
|
|
while ( !(editor->cursor_row == desired_row &&
|
|
|
|
editor->cursor_column == desired_column) )
|
|
|
|
editor_type_backspace(editor);
|
|
|
|
}
|
|
|
|
|
2014-07-31 17:56:36 +00:00
|
|
|
void editor_type_exit_select_left(struct editor* editor)
|
2014-08-23 19:26:38 +00:00
|
|
|
{
|
|
|
|
if ( editor_has_selection(editor) )
|
|
|
|
{
|
|
|
|
size_t column, row;
|
|
|
|
row_column_smallest(editor->cursor_row, editor->cursor_column,
|
|
|
|
editor->select_row, editor->select_column,
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
&row, &column);
|
|
|
|
editor_cursor_set(editor, row, column);
|
2014-08-23 19:26:38 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-07-31 17:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void editor_type_exit_select_right(struct editor* editor)
|
|
|
|
{
|
|
|
|
if ( editor_has_selection(editor) )
|
|
|
|
{
|
|
|
|
size_t column, row;
|
|
|
|
row_column_biggest(editor->cursor_row, editor->cursor_column,
|
2018-03-11 16:10:09 +00:00
|
|
|
editor->select_row, editor->select_column,
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
&row, &column);
|
|
|
|
editor_cursor_set(editor, row, column);
|
2014-07-31 17:56:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void editor_type_left(struct editor* editor)
|
|
|
|
{
|
|
|
|
editor_type_exit_select_left(editor);
|
2014-08-23 19:26:38 +00:00
|
|
|
if ( editor->cursor_column )
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
editor_cursor_set(editor, editor->cursor_row,
|
|
|
|
editor->cursor_column - 1);
|
2014-08-23 19:26:38 +00:00
|
|
|
else if ( editor->cursor_row )
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
editor_cursor_set(editor, editor->cursor_row - 1,
|
|
|
|
editor->lines[editor->cursor_row - 1].used);
|
2014-08-23 19:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void editor_type_select_left(struct editor* editor)
|
|
|
|
{
|
|
|
|
if ( editor->select_column )
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
editor_select_set(editor, editor->select_row,
|
|
|
|
editor->select_column - 1);
|
2014-08-23 19:26:38 +00:00
|
|
|
else if ( editor->select_row )
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
editor_select_set(editor, editor->select_row - 1,
|
|
|
|
editor->lines[editor->select_row - 1].used);
|
2014-08-23 19:26:38 +00:00
|
|
|
}
|
|
|
|
|
2014-07-31 17:56:36 +00:00
|
|
|
void editor_type_control_left(struct editor* editor)
|
2014-08-23 19:26:38 +00:00
|
|
|
{
|
2014-07-31 17:56:36 +00:00
|
|
|
editor_type_exit_select_left(editor);
|
|
|
|
if ( editor->cursor_column || editor->cursor_row )
|
|
|
|
editor_type_left(editor);
|
|
|
|
int state = 0;
|
|
|
|
while ( editor->cursor_column || editor->cursor_row )
|
2014-08-23 19:26:38 +00:00
|
|
|
{
|
2014-07-31 17:56:36 +00:00
|
|
|
editor_type_left(editor);
|
|
|
|
struct line* line = &editor->lines[editor->cursor_row];
|
|
|
|
wchar_t wc = line->data[editor->cursor_column];
|
|
|
|
if ( (state == 0 && !iswspace(wc)) || (state == 1 && iswspace(wc)) )
|
|
|
|
{
|
|
|
|
editor_type_right(editor);
|
|
|
|
if ( ++state == 2 )
|
|
|
|
break;
|
|
|
|
}
|
2014-08-23 19:26:38 +00:00
|
|
|
}
|
2014-07-31 17:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void editor_type_control_select_left(struct editor* editor)
|
|
|
|
{
|
|
|
|
if ( editor->select_column || editor->select_row )
|
|
|
|
editor_type_select_left(editor);
|
|
|
|
int state = 0;
|
|
|
|
while ( editor->select_column || editor->select_row )
|
|
|
|
{
|
|
|
|
editor_type_select_left(editor);
|
|
|
|
struct line* line = &editor->lines[editor->select_row];
|
|
|
|
wchar_t wc = line->data[editor->select_column];
|
|
|
|
if ( (state == 0 && !iswspace(wc)) || (state == 1 && iswspace(wc)) )
|
|
|
|
{
|
|
|
|
editor_type_select_right(editor);
|
|
|
|
if ( ++state == 2 )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void editor_type_right(struct editor* editor)
|
|
|
|
{
|
|
|
|
editor_type_exit_select_right(editor);
|
2014-08-23 19:26:38 +00:00
|
|
|
struct line* current_line = &editor->lines[editor->cursor_row];
|
|
|
|
if ( editor->cursor_column != current_line->used )
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
editor_cursor_set(editor, editor->cursor_row,
|
|
|
|
editor->cursor_column + 1);
|
|
|
|
else if ( editor->cursor_row + 1 != editor->lines_used )
|
|
|
|
editor_cursor_set(editor, editor->cursor_row + 1, 0);
|
2014-08-23 19:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void editor_type_select_right(struct editor* editor)
|
|
|
|
{
|
|
|
|
struct line* current_line = &editor->lines[editor->select_row];
|
|
|
|
if ( editor->select_column != current_line->used )
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
editor_select_set(editor, editor->select_row,
|
|
|
|
editor->select_column + 1);
|
|
|
|
else if ( editor->select_row + 1 != editor->lines_used )
|
|
|
|
editor_select_set(editor, editor->select_row + 1, 0);
|
2014-08-23 19:26:38 +00:00
|
|
|
}
|
|
|
|
|
2014-07-31 17:56:36 +00:00
|
|
|
void editor_type_control_right(struct editor* editor)
|
2014-08-23 19:26:38 +00:00
|
|
|
{
|
2014-07-31 17:56:36 +00:00
|
|
|
editor_type_exit_select_right(editor);
|
|
|
|
int state = 0;
|
|
|
|
while ( editor->cursor_column != editor->lines[editor->cursor_row].used ||
|
|
|
|
editor->cursor_row != editor->lines_used )
|
2014-08-23 19:26:38 +00:00
|
|
|
{
|
2014-07-31 17:56:36 +00:00
|
|
|
struct line* line = &editor->lines[editor->cursor_row];
|
|
|
|
wchar_t wc = editor->cursor_column != line->used ?
|
2014-08-01 08:47:10 +00:00
|
|
|
line->data[editor->cursor_column] : L' ';
|
2014-07-31 17:56:36 +00:00
|
|
|
if ( (state == 0 && !iswspace(wc)) || (state == 1 && iswspace(wc)) )
|
|
|
|
{
|
|
|
|
if ( ++state == 2 )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
editor_type_right(editor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void editor_type_control_select_right(struct editor* editor)
|
|
|
|
{
|
|
|
|
int state = 0;
|
|
|
|
while ( editor->select_column != editor->lines[editor->select_row].used ||
|
|
|
|
editor->select_row != editor->lines_used )
|
|
|
|
{
|
|
|
|
struct line* line = &editor->lines[editor->select_row];
|
|
|
|
wchar_t wc = editor->select_column != line->used ?
|
2014-08-01 08:47:10 +00:00
|
|
|
line->data[editor->select_column] : L' ';
|
2014-07-31 17:56:36 +00:00
|
|
|
if ( (state == 0 && !iswspace(wc)) || (state == 1 && iswspace(wc)) )
|
|
|
|
{
|
|
|
|
if ( ++state == 2 )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
editor_type_select_right(editor);
|
2014-08-23 19:26:38 +00:00
|
|
|
}
|
2014-07-31 17:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void editor_type_up(struct editor* editor)
|
|
|
|
{
|
|
|
|
editor_type_exit_select_left(editor);
|
2014-08-23 19:26:38 +00:00
|
|
|
if ( !editor->cursor_row )
|
|
|
|
{
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
editor_cursor_set(editor, 0, 0);
|
2014-08-23 19:26:38 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-08-01 08:47:10 +00:00
|
|
|
struct line* old_line = &editor->lines[editor->cursor_row];
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
struct line* new_line = &editor->lines[editor->cursor_row - 1];
|
2014-08-01 08:47:10 +00:00
|
|
|
size_t old_column = editor_display_column_of_line_offset(editor, old_line, editor->cursor_column);
|
|
|
|
size_t new_offset = editor_line_offset_of_display_column(editor, new_line, old_column);
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
editor_cursor_set(editor, editor->cursor_row - 1, new_offset);
|
2014-08-23 19:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void editor_type_select_up(struct editor* editor)
|
|
|
|
{
|
|
|
|
if ( !editor->select_row )
|
|
|
|
{
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
editor_select_set(editor, 0, 0);
|
2014-08-23 19:26:38 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-08-01 08:47:10 +00:00
|
|
|
struct line* old_line = &editor->lines[editor->select_row];
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
struct line* new_line = &editor->lines[editor->select_row - 1];
|
2014-08-01 08:47:10 +00:00
|
|
|
size_t old_column = editor_display_column_of_line_offset(editor, old_line, editor->select_column);
|
|
|
|
size_t new_offset = editor_line_offset_of_display_column(editor, new_line, old_column);
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
editor_select_set(editor, editor->select_row - 1, new_offset);
|
2014-08-23 19:26:38 +00:00
|
|
|
}
|
|
|
|
|
2014-07-31 17:56:36 +00:00
|
|
|
void editor_type_control_up(struct editor* editor)
|
|
|
|
{
|
|
|
|
editor_type_exit_select_left(editor);
|
|
|
|
if ( editor->cursor_row )
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
editor_cursor_set(editor, editor->cursor_row - 1, 0);
|
|
|
|
else
|
|
|
|
editor_cursor_set(editor, editor->cursor_row, 0);
|
2014-07-31 17:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void editor_type_control_select_up(struct editor* editor)
|
|
|
|
{
|
|
|
|
if ( editor->select_row )
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
editor_select_set(editor, editor->select_row - 1, 0);
|
|
|
|
else
|
|
|
|
editor_select_set(editor, editor->select_row, 0);
|
2014-07-31 17:56:36 +00:00
|
|
|
}
|
|
|
|
|
2014-08-23 19:26:38 +00:00
|
|
|
void editor_type_down(struct editor* editor)
|
|
|
|
{
|
2014-07-31 17:56:36 +00:00
|
|
|
editor_type_exit_select_right(editor);
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
if ( editor->cursor_row + 1 == editor->lines_used )
|
2014-08-23 19:26:38 +00:00
|
|
|
{
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
editor_cursor_set(editor, editor->cursor_row,
|
|
|
|
editor->lines[editor->cursor_row].used);
|
2014-08-23 19:26:38 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-08-01 08:47:10 +00:00
|
|
|
struct line* old_line = &editor->lines[editor->cursor_row];
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
struct line* new_line = &editor->lines[editor->cursor_row + 1];
|
2014-08-01 08:47:10 +00:00
|
|
|
size_t old_column = editor_display_column_of_line_offset(editor, old_line, editor->cursor_column);
|
|
|
|
size_t new_offset = editor_line_offset_of_display_column(editor, new_line, old_column);
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
editor_cursor_set(editor, editor->cursor_row + 1, new_offset);
|
2014-08-23 19:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void editor_type_select_down(struct editor* editor)
|
|
|
|
{
|
2018-03-11 16:10:09 +00:00
|
|
|
if ( editor->select_row+1 == editor->lines_used )
|
2014-08-23 19:26:38 +00:00
|
|
|
{
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
editor_select_set(editor, editor->select_row,
|
|
|
|
editor->lines[editor->select_row].used);
|
2014-08-23 19:26:38 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-08-01 08:47:10 +00:00
|
|
|
struct line* old_line = &editor->lines[editor->select_row];
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
struct line* new_line = &editor->lines[editor->select_row + 1];
|
2014-08-01 08:47:10 +00:00
|
|
|
size_t old_column = editor_display_column_of_line_offset(editor, old_line, editor->select_column);
|
|
|
|
size_t new_offset = editor_line_offset_of_display_column(editor, new_line, old_column);
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
editor_select_set(editor, editor->select_row + 1, new_offset);
|
2014-08-23 19:26:38 +00:00
|
|
|
}
|
|
|
|
|
2014-07-31 17:56:36 +00:00
|
|
|
void editor_type_control_down(struct editor* editor)
|
|
|
|
{
|
|
|
|
editor_type_exit_select_right(editor);
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
if ( editor->cursor_row + 1 < editor->lines_used )
|
|
|
|
editor_cursor_set(editor, editor->cursor_row + 1,
|
|
|
|
editor->lines[editor->cursor_row + 1].used);
|
|
|
|
else
|
|
|
|
editor_cursor_set(editor, editor->cursor_row,
|
|
|
|
editor->lines[editor->cursor_row].used);
|
2014-07-31 17:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void editor_type_control_select_down(struct editor* editor)
|
|
|
|
{
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
if ( editor->select_row + 1 < editor->lines_used )
|
|
|
|
editor_select_set(editor, editor->select_row + 1,
|
|
|
|
editor->lines[editor->select_row + 1].used);
|
|
|
|
else
|
|
|
|
editor_select_set(editor, editor->select_row,
|
|
|
|
editor->lines[editor->select_row].used);
|
2014-07-31 17:56:36 +00:00
|
|
|
}
|
|
|
|
|
2014-08-23 19:26:38 +00:00
|
|
|
void editor_skip_leading(struct editor* editor)
|
|
|
|
{
|
|
|
|
struct line* current_line = &editor->lines[editor->cursor_row];
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
for ( editor_cursor_set(editor, editor->cursor_row, 0);
|
2014-08-23 19:26:38 +00:00
|
|
|
editor->cursor_column < current_line->used;
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
editor_cursor_set(editor, editor->cursor_row,
|
|
|
|
editor->cursor_column + 1) )
|
2014-08-23 19:26:38 +00:00
|
|
|
if ( !iswspace(current_line->data[editor->cursor_column]) )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
void editor_select_skip_leading(struct editor* editor)
|
|
|
|
{
|
|
|
|
struct line* current_line = &editor->lines[editor->select_row];
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
for ( editor_select_set(editor, editor->select_row, 0);
|
2014-08-23 19:26:38 +00:00
|
|
|
editor->select_column < current_line->used;
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
editor_select_set(editor, editor->select_row,
|
|
|
|
editor->select_column + 1) )
|
2014-08-23 19:26:38 +00:00
|
|
|
if ( !iswspace(current_line->data[editor->select_column]) )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
void editor_type_home(struct editor* editor)
|
|
|
|
{
|
|
|
|
if ( editor_has_selection(editor) )
|
|
|
|
{
|
|
|
|
size_t column, row;
|
|
|
|
row_column_smallest(editor->cursor_row, editor->cursor_column,
|
|
|
|
editor->select_row, editor->select_column,
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
&row, &column);
|
|
|
|
editor_cursor_set(editor, row, column);
|
2014-08-23 19:26:38 +00:00
|
|
|
}
|
|
|
|
if ( !editor->cursor_column )
|
|
|
|
{
|
|
|
|
editor_skip_leading(editor);
|
|
|
|
return;
|
|
|
|
}
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
editor_cursor_set(editor, editor->cursor_row, 0);
|
2014-08-23 19:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void editor_type_select_home(struct editor* editor)
|
|
|
|
{
|
|
|
|
if ( !editor->select_column )
|
|
|
|
{
|
|
|
|
editor_select_skip_leading(editor);
|
|
|
|
return;
|
|
|
|
}
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
editor_select_set(editor, editor->select_row, 0);
|
2014-08-23 19:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void editor_skip_ending(struct editor* editor)
|
|
|
|
{
|
|
|
|
struct line* current_line = &editor->lines[editor->cursor_row];
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
for ( editor_cursor_set(editor, editor->cursor_row, current_line->used);
|
2014-08-23 19:26:38 +00:00
|
|
|
editor->cursor_column;
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
editor_cursor_set(editor, editor->cursor_row,
|
|
|
|
editor->cursor_column - 1) )
|
2014-08-23 19:26:38 +00:00
|
|
|
if ( !iswspace(current_line->data[editor->cursor_column-1]) )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
void editor_select_skip_ending(struct editor* editor)
|
|
|
|
{
|
|
|
|
struct line* current_line = &editor->lines[editor->select_row];
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
for ( editor_select_set(editor, editor->select_row, current_line->used);
|
2014-08-23 19:26:38 +00:00
|
|
|
editor->select_column;
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
editor_select_set(editor, editor->select_row,
|
|
|
|
editor->select_column - 1) )
|
2014-08-23 19:26:38 +00:00
|
|
|
if ( !iswspace(current_line->data[editor->select_column-1]) )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
void editor_type_end(struct editor* editor)
|
|
|
|
{
|
|
|
|
if ( editor_has_selection(editor) )
|
|
|
|
{
|
|
|
|
size_t column, row;
|
|
|
|
row_column_biggest(editor->cursor_row, editor->cursor_column,
|
|
|
|
editor->select_row, editor->select_column,
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
&row, &column);
|
|
|
|
editor_cursor_set(editor, row, column);
|
2014-08-23 19:26:38 +00:00
|
|
|
}
|
|
|
|
struct line* current_line = &editor->lines[editor->cursor_row];
|
|
|
|
if ( editor->cursor_column == current_line->used )
|
|
|
|
{
|
|
|
|
editor_skip_ending(editor);
|
|
|
|
return;
|
|
|
|
}
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
editor_cursor_set(editor, editor->cursor_row, current_line->used);
|
2014-08-23 19:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void editor_type_select_end(struct editor* editor)
|
|
|
|
{
|
|
|
|
struct line* current_line = &editor->lines[editor->select_row];
|
|
|
|
if ( editor->select_column == current_line->used )
|
|
|
|
{
|
|
|
|
editor_select_skip_ending(editor);
|
|
|
|
return;
|
|
|
|
}
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
editor_select_set(editor, editor->select_row, current_line->used);
|
2014-08-23 19:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void editor_type_page_up(struct editor* editor)
|
|
|
|
{
|
|
|
|
if ( editor_has_selection(editor) )
|
|
|
|
{
|
|
|
|
size_t column, row;
|
|
|
|
row_column_smallest(editor->cursor_row, editor->cursor_column,
|
|
|
|
editor->select_row, editor->select_column,
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
&row, &column);
|
|
|
|
editor_cursor_set(editor, row, column);
|
2014-08-23 19:26:38 +00:00
|
|
|
}
|
|
|
|
if ( editor->cursor_row < editor->viewport_height )
|
|
|
|
{
|
|
|
|
editor_cursor_set(editor, 0, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
size_t new_line = editor->cursor_row - editor->viewport_height;
|
|
|
|
size_t new_line_len = editor->lines[new_line].used;
|
|
|
|
if ( new_line_len < editor->cursor_column )
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
editor_cursor_set(editor, new_line, new_line_len);
|
|
|
|
else
|
|
|
|
editor_cursor_set(editor, new_line, editor->cursor_column);
|
2014-08-23 19:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void editor_type_select_page_up(struct editor* editor)
|
|
|
|
{
|
|
|
|
if ( editor->select_row < editor->viewport_height )
|
|
|
|
{
|
|
|
|
editor_select_set(editor, 0, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
size_t new_line = editor->select_row - editor->viewport_height;
|
|
|
|
size_t new_line_len = editor->lines[new_line].used;
|
|
|
|
if ( new_line_len < editor->select_column )
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
editor_select_set(editor, new_line, new_line_len);
|
|
|
|
else
|
|
|
|
editor_select_set(editor, new_line, editor->select_column);
|
2014-08-23 19:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void editor_type_page_down(struct editor* editor)
|
|
|
|
{
|
|
|
|
if ( editor_has_selection(editor) )
|
|
|
|
{
|
|
|
|
size_t column, row;
|
|
|
|
row_column_biggest(editor->cursor_row, editor->cursor_column,
|
|
|
|
editor->select_row, editor->select_column,
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
&row, &column);
|
|
|
|
editor_cursor_set(editor, row, column);
|
2014-08-23 19:26:38 +00:00
|
|
|
}
|
|
|
|
size_t new_line = editor->cursor_row + editor->viewport_height;
|
|
|
|
if ( editor->lines_used <= new_line )
|
|
|
|
{
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
editor_cursor_set(editor, editor->lines_used - 1,
|
|
|
|
editor->lines[editor->lines_used - 1].used);
|
2014-08-23 19:26:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
size_t new_line_len = editor->lines[new_line].used;
|
|
|
|
if ( new_line_len < editor->cursor_column )
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
editor_cursor_set(editor, new_line, new_line_len);
|
|
|
|
else
|
|
|
|
editor_cursor_set(editor, new_line, editor->cursor_column);
|
2014-08-23 19:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void editor_type_select_page_down(struct editor* editor)
|
|
|
|
{
|
|
|
|
size_t new_line = editor->select_row + editor->viewport_height;
|
|
|
|
if ( editor->lines_used <= new_line )
|
|
|
|
{
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
editor_select_set(editor, editor->lines_used - 1,
|
|
|
|
editor->lines[editor->lines_used - 1].used);
|
2014-08-23 19:26:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
size_t new_line_len = editor->lines[new_line].used;
|
|
|
|
if ( new_line_len < editor->select_column )
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
editor_select_set(editor, new_line, new_line_len);
|
|
|
|
else
|
|
|
|
editor_select_set(editor, new_line, editor->select_column);
|
2014-08-23 19:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void editor_type_edit(struct editor* editor)
|
|
|
|
{
|
|
|
|
editor->mode = MODE_EDIT;
|
|
|
|
}
|
|
|
|
|
2021-10-30 23:55:26 +00:00
|
|
|
void editor_type_search(struct editor* editor)
|
|
|
|
{
|
|
|
|
editor->mode = MODE_SEARCH;
|
|
|
|
editor->modal_used = 0;
|
|
|
|
editor->modal_cursor = 0;
|
|
|
|
editor->modal_error = false;
|
|
|
|
}
|
|
|
|
|
2014-08-23 19:26:38 +00:00
|
|
|
void editor_type_goto_line(struct editor* editor)
|
|
|
|
{
|
|
|
|
editor->mode = MODE_GOTO_LINE;
|
|
|
|
editor->modal_used = 0;
|
|
|
|
editor->modal_cursor = 0;
|
|
|
|
editor->modal_error = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void editor_type_save(struct editor* editor)
|
|
|
|
{
|
|
|
|
editor->mode = MODE_SAVE;
|
|
|
|
|
|
|
|
free(editor->modal);
|
|
|
|
editor->modal = convert_mbs_to_wcs(editor->current_file_name);
|
|
|
|
editor->modal_used = wcslen(editor->modal);
|
|
|
|
editor->modal_length = editor->modal_used+1;
|
|
|
|
editor->modal_cursor = editor->modal_used;
|
|
|
|
editor->modal_error = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void editor_type_save_as(struct editor* editor)
|
|
|
|
{
|
|
|
|
editor->mode = MODE_SAVE;
|
|
|
|
editor->modal_used = 0;
|
|
|
|
editor->modal_cursor = 0;
|
|
|
|
editor->modal_error = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void editor_type_open(struct editor* editor)
|
|
|
|
{
|
2016-02-24 22:29:23 +00:00
|
|
|
editor->mode = editor->dirty ? MODE_ASK_LOAD : MODE_LOAD;
|
2014-08-23 19:26:38 +00:00
|
|
|
editor->modal_used = 0;
|
|
|
|
editor->modal_cursor = 0;
|
|
|
|
editor->modal_error = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void editor_type_open_as(struct editor* editor)
|
|
|
|
{
|
2016-02-24 22:29:23 +00:00
|
|
|
if ( editor->dirty )
|
|
|
|
return editor_type_open(editor);
|
|
|
|
|
2014-08-23 19:26:38 +00:00
|
|
|
editor->mode = MODE_LOAD;
|
|
|
|
|
|
|
|
free(editor->modal);
|
|
|
|
editor->modal = convert_mbs_to_wcs(editor->current_file_name);
|
|
|
|
editor->modal_used = wcslen(editor->modal);
|
|
|
|
editor->modal_length = editor->modal_used+1;
|
|
|
|
editor->modal_cursor = editor->modal_used;
|
|
|
|
editor->modal_error = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void editor_type_quit(struct editor* editor)
|
|
|
|
{
|
|
|
|
editor->mode = editor->dirty ? MODE_ASK_QUIT : MODE_QUIT;
|
|
|
|
editor->modal_cursor = 0;
|
|
|
|
editor->modal_used = 0;
|
|
|
|
editor->modal_error = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void editor_type_command(struct editor* editor)
|
|
|
|
{
|
|
|
|
editor->mode = MODE_COMMAND;
|
|
|
|
editor->modal_cursor = 0;
|
|
|
|
editor->modal_used = 0;
|
|
|
|
editor->modal_error = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void editor_type_raw_character(struct editor* editor, wchar_t c)
|
|
|
|
{
|
|
|
|
struct line* current_line = &editor->lines[editor->cursor_row];
|
|
|
|
|
|
|
|
if ( current_line->used == current_line->length )
|
|
|
|
{
|
|
|
|
size_t new_length = current_line->length ? 2 * current_line->length : 8;
|
2016-02-28 16:36:45 +00:00
|
|
|
wchar_t* new_data = (wchar_t*) malloc(sizeof(wchar_t) * new_length);
|
2014-08-23 19:26:38 +00:00
|
|
|
for ( size_t i = 0; i < current_line->used; i++ )
|
|
|
|
new_data[i] = current_line->data[i];
|
2016-02-28 16:36:45 +00:00
|
|
|
free(current_line->data);
|
2014-08-23 19:26:38 +00:00
|
|
|
current_line->data = new_data;
|
|
|
|
current_line->length = new_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
editor->dirty = true;
|
|
|
|
|
|
|
|
for ( size_t i = current_line->used; editor->cursor_column < i; i-- )
|
|
|
|
current_line->data[i] = current_line->data[i-1];
|
|
|
|
current_line->used++;
|
Refactor movement code in editor(1).
Firstly, lots of places in editor(1) would do a move from (x₀,y₀) to
(x,y) by first moving to (x,y₀) or (x₀,y) and only then (x,y). If the
intermediate positions were not valid cursor positions, this might cause
out of bounds access. Categorically fixed this by removing functions for
moving only horizontally or only vertically.
Secondly, editor_select_set() would set the column before setting the
line. Since the code for setting the column accesses the currently set
line at the new cursor position, it might cause out of bounds access
even on valid cursor positions. Fixed this by swapping the order in
which column and row are set.
Thirdly, the order of arguments passed to row_column_smallest() and
row_column_biggest() was wrong, with column being passed before the row,
even though they were defined the other way. However, this did not
result in out of bounds memory accesses due to the parameters to
editor_cursor_set() also being swapped at relevant callsites.
Finally, the boundary condition for control-down was off by one.
2021-11-04 22:09:48 +00:00
|
|
|
current_line->data[editor->cursor_column] = c;
|
|
|
|
editor_cursor_set(editor, editor->cursor_row, editor->cursor_column + 1);
|
2014-08-23 19:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void editor_type_copy(struct editor* editor)
|
|
|
|
{
|
|
|
|
if ( editor->cursor_row == editor->select_row &&
|
|
|
|
editor->cursor_column == editor->select_column )
|
|
|
|
return;
|
|
|
|
|
2016-02-28 16:36:45 +00:00
|
|
|
free(editor->clipboard);
|
2014-08-23 19:26:38 +00:00
|
|
|
|
|
|
|
size_t start_row;
|
|
|
|
size_t start_column;
|
|
|
|
size_t end_row;
|
|
|
|
size_t end_column;
|
|
|
|
if ( is_row_column_lt(editor->select_row, editor->select_column,
|
|
|
|
editor->cursor_row, editor->cursor_column) )
|
|
|
|
{
|
|
|
|
start_row = editor->select_row;
|
|
|
|
start_column = editor->select_column;
|
|
|
|
end_row = editor->cursor_row;
|
|
|
|
end_column = editor->cursor_column;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
start_row = editor->cursor_row;
|
|
|
|
start_column = editor->cursor_column;
|
|
|
|
end_row = editor->select_row;
|
|
|
|
end_column = editor->select_column;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t length = 0;
|
|
|
|
for ( size_t row = start_row, column = start_column;
|
|
|
|
is_row_column_lt(row, column, end_row, end_column); )
|
|
|
|
{
|
2016-03-05 23:49:55 +00:00
|
|
|
struct line* line = &editor->lines[row];
|
2014-08-23 19:26:38 +00:00
|
|
|
if ( row == end_row )
|
|
|
|
{
|
|
|
|
length += end_column - column;
|
|
|
|
column = end_column;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-03-05 23:49:55 +00:00
|
|
|
length += (line->used - column) + 1 /*newline*/;
|
2014-08-23 19:26:38 +00:00
|
|
|
column = 0;
|
|
|
|
row++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-28 16:36:45 +00:00
|
|
|
editor->clipboard = (wchar_t*) malloc(sizeof(wchar_t) * (length + 1));
|
2014-08-23 19:26:38 +00:00
|
|
|
size_t offset = 0;
|
|
|
|
for ( size_t row = start_row, column = start_column;
|
|
|
|
is_row_column_lt(row, column, end_row, end_column); )
|
|
|
|
{
|
|
|
|
struct line* line = &editor->lines[row];
|
|
|
|
if ( row == end_row )
|
|
|
|
{
|
2016-03-05 23:49:55 +00:00
|
|
|
memcpy(editor->clipboard + offset, line->data + column,
|
|
|
|
sizeof(wchar_t) * (end_column - column));
|
2014-08-23 19:26:38 +00:00
|
|
|
offset += end_column - column;
|
|
|
|
column = end_column;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-03-05 23:49:55 +00:00
|
|
|
memcpy(editor->clipboard + offset, line->data + column,
|
|
|
|
sizeof(wchar_t) * (line->used - column));
|
|
|
|
editor->clipboard[offset + (line->used - column)] = L'\n';
|
|
|
|
offset += (line->used - column) + 1 /*newline*/;
|
2014-08-23 19:26:38 +00:00
|
|
|
column = 0;
|
|
|
|
row++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
editor->clipboard[length] = L'\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
void editor_type_cut(struct editor* editor)
|
|
|
|
{
|
|
|
|
if ( editor->cursor_row == editor->select_row &&
|
|
|
|
editor->cursor_column == editor->select_column )
|
|
|
|
return;
|
|
|
|
|
|
|
|
editor_type_copy(editor);
|
|
|
|
editor_type_delete_selection(editor);
|
|
|
|
}
|
|
|
|
|
|
|
|
void editor_type_paste(struct editor* editor)
|
|
|
|
{
|
|
|
|
if ( !(editor->cursor_row == editor->select_row &&
|
|
|
|
editor->cursor_column == editor->select_column) )
|
|
|
|
editor_type_delete_selection(editor);
|
|
|
|
|
|
|
|
for ( size_t i = 0; editor->clipboard && editor->clipboard[i]; i++ )
|
|
|
|
{
|
|
|
|
if ( editor->clipboard[i] == L'\n' )
|
|
|
|
editor_type_newline(editor);
|
|
|
|
else
|
|
|
|
editor_type_raw_character(editor, editor->clipboard[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-27 23:30:50 +00:00
|
|
|
void editor_type_suspend(struct editor* editor)
|
|
|
|
{
|
|
|
|
editor->suspend_requested = true;
|
|
|
|
}
|
|
|
|
|
2014-08-23 19:26:38 +00:00
|
|
|
void editor_type_character(struct editor* editor, wchar_t c)
|
|
|
|
{
|
|
|
|
if ( editor->control )
|
|
|
|
{
|
|
|
|
switch ( towlower(c) )
|
|
|
|
{
|
|
|
|
case L'c': editor_type_copy(editor); break;
|
2021-10-30 23:55:26 +00:00
|
|
|
case L'f': editor_type_search(editor); break;
|
2021-04-27 20:40:14 +00:00
|
|
|
case L'g': editor_type_goto_line(editor); break;
|
2014-08-23 19:26:38 +00:00
|
|
|
case L'k': editor_type_cut(editor); break;
|
|
|
|
case L'o': editor->shift ?
|
|
|
|
editor_type_open_as(editor) :
|
|
|
|
editor_type_open(editor); break;
|
|
|
|
case L'q': editor_type_quit(editor); break;
|
|
|
|
case L's': editor->shift ?
|
|
|
|
editor_type_save_as(editor) :
|
|
|
|
editor_type_save(editor); break;
|
|
|
|
case L'v': editor_type_paste(editor); break;
|
|
|
|
case L'x': editor_type_cut(editor); break;
|
2014-07-27 23:30:50 +00:00
|
|
|
case L'z': editor_type_suspend(editor); break;
|
2014-08-23 19:26:38 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( editor_has_selection(editor) )
|
|
|
|
editor_type_delete_selection(editor);
|
|
|
|
|
|
|
|
if ( c == L'\n' ) { editor_type_newline(editor); return; }
|
|
|
|
|
|
|
|
editor_type_raw_character(editor, c);
|
|
|
|
}
|