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 23:38:16 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013, 2014, 2015, 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-05 00:09:48 +02: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 23:38:16 +01: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.
|
|
|
|
*
|
|
|
|
* modal.c
|
|
|
|
* Modal commands.
|
|
|
|
*/
|
2014-08-23 21:26:38 +02:00
|
|
|
|
2021-10-31 02:55:26 +03:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2014-08-23 21:26:38 +02:00
|
|
|
#include <ctype.h>
|
2021-10-31 02:55:26 +03:00
|
|
|
#include <regex.h>
|
2016-02-28 17:36:45 +01:00
|
|
|
#include <stdbool.h>
|
2014-08-23 21:26:38 +02:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2021-10-31 02:55:26 +03:00
|
|
|
#include <wchar.h>
|
2014-08-23 21:26:38 +02:00
|
|
|
#include <wctype.h>
|
|
|
|
|
2016-02-28 17:36:45 +01:00
|
|
|
#include "command.h"
|
|
|
|
#include "cursor.h"
|
|
|
|
#include "editor.h"
|
|
|
|
#include "highlight.h"
|
|
|
|
#include "modal.h"
|
|
|
|
#include "multibyte.h"
|
2014-08-23 21:26:38 +02:00
|
|
|
|
2016-02-24 23:29:23 +01:00
|
|
|
static void editor_reset_modal(struct editor* editor)
|
|
|
|
{
|
|
|
|
editor->modal_used = 0;
|
|
|
|
editor->modal_cursor = 0;
|
|
|
|
}
|
|
|
|
|
2014-08-01 12:12:54 +02:00
|
|
|
bool is_truth_string(const char* truth)
|
|
|
|
{
|
|
|
|
return !strcmp(truth, "on") || !strcmp(truth, "off");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_truth_true(const char* truth)
|
|
|
|
{
|
|
|
|
return strcmp(truth, "off") != 0;
|
|
|
|
}
|
|
|
|
|
2014-08-23 21:26:38 +02:00
|
|
|
void editor_modal_left(struct editor* editor)
|
|
|
|
{
|
|
|
|
if ( editor->modal_cursor )
|
|
|
|
editor->modal_cursor--;
|
|
|
|
}
|
|
|
|
|
|
|
|
void editor_modal_right(struct editor* editor)
|
|
|
|
{
|
|
|
|
if ( editor->modal_cursor != editor->modal_used )
|
|
|
|
editor->modal_cursor++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void editor_modal_home(struct editor* editor)
|
|
|
|
{
|
|
|
|
editor->modal_cursor = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void editor_modal_end(struct editor* editor)
|
|
|
|
{
|
|
|
|
editor->modal_cursor = editor->modal_used;
|
|
|
|
}
|
|
|
|
|
|
|
|
void editor_modal_backspace(struct editor* editor)
|
|
|
|
{
|
|
|
|
if ( !editor->modal_cursor )
|
|
|
|
return;
|
|
|
|
|
|
|
|
editor->modal_error = false;
|
|
|
|
|
|
|
|
editor->modal_used--;
|
|
|
|
for ( size_t i = --editor->modal_cursor; i < editor->modal_used; i++ )
|
|
|
|
editor->modal[i] = editor->modal[i+1];
|
|
|
|
}
|
|
|
|
|
|
|
|
void editor_modal_delete(struct editor* editor)
|
|
|
|
{
|
|
|
|
if ( editor->modal_cursor == editor->modal_used )
|
|
|
|
return;
|
|
|
|
|
|
|
|
editor->modal_error = false;
|
|
|
|
|
|
|
|
editor->modal_used--;
|
|
|
|
for ( size_t i = editor->modal_cursor; i < editor->modal_used; i++ )
|
|
|
|
editor->modal[i] = editor->modal[i+1];
|
|
|
|
}
|
|
|
|
|
|
|
|
void editor_modal_load(struct editor* editor, const char* path)
|
|
|
|
{
|
|
|
|
if ( editor_load_file(editor, path) )
|
|
|
|
editor_type_edit(editor);
|
|
|
|
else
|
|
|
|
editor->modal_error = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void editor_modal_save(struct editor* editor, const char* path)
|
|
|
|
{
|
|
|
|
if ( editor_save_file(editor, path) )
|
|
|
|
editor_type_edit(editor);
|
|
|
|
else
|
|
|
|
editor->modal_error = true;
|
|
|
|
}
|
|
|
|
|
2016-02-24 23:29:23 +01:00
|
|
|
void editor_modal_save_load(struct editor* editor, const char* path)
|
|
|
|
{
|
|
|
|
if ( editor_save_file(editor, path) )
|
|
|
|
{
|
|
|
|
editor_reset_modal(editor);
|
|
|
|
editor->mode = MODE_LOAD;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
editor->modal_error = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void editor_modal_save_quit(struct editor* editor, const char* path)
|
|
|
|
{
|
|
|
|
if ( editor_save_file(editor, path) )
|
|
|
|
editor->mode = MODE_QUIT;
|
|
|
|
else
|
|
|
|
editor->modal_error = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void editor_modal_ask_load(struct editor* editor, const char* answer)
|
|
|
|
{
|
|
|
|
if ( tolower((unsigned char) answer[0]) == 'y' )
|
|
|
|
{
|
|
|
|
editor_reset_modal(editor);
|
|
|
|
if ( editor->current_file_name )
|
|
|
|
{
|
|
|
|
if ( editor_save_file(editor, editor->current_file_name) )
|
|
|
|
{
|
|
|
|
editor->mode = MODE_LOAD;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
editor->modal_error = true;
|
|
|
|
}
|
|
|
|
editor->mode = MODE_SAVE_LOAD;
|
|
|
|
}
|
|
|
|
else if ( tolower((unsigned char) answer[0]) == 'n' )
|
|
|
|
{
|
|
|
|
editor_reset_modal(editor);
|
|
|
|
editor->mode = MODE_LOAD;
|
|
|
|
}
|
|
|
|
else if ( !answer[0] )
|
|
|
|
editor_type_edit(editor);
|
|
|
|
else
|
|
|
|
editor->modal_error = true;
|
|
|
|
}
|
|
|
|
|
2014-08-23 21:26:38 +02:00
|
|
|
void editor_modal_ask_quit(struct editor* editor, const char* answer)
|
|
|
|
{
|
2015-08-11 15:37:31 +02:00
|
|
|
if ( tolower((unsigned char) answer[0]) == 'y' )
|
2016-02-24 23:29:23 +01:00
|
|
|
{
|
|
|
|
editor_reset_modal(editor);
|
|
|
|
if ( editor->current_file_name )
|
|
|
|
{
|
|
|
|
if ( editor_save_file(editor, editor->current_file_name) )
|
|
|
|
{
|
|
|
|
editor->mode = MODE_QUIT;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
editor->modal_error = true;
|
|
|
|
}
|
|
|
|
editor->mode = MODE_SAVE_QUIT;
|
|
|
|
}
|
|
|
|
else if ( tolower((unsigned char) answer[0]) == 'n' )
|
2014-08-23 21:26:38 +02:00
|
|
|
editor->mode = MODE_QUIT;
|
2016-02-24 23:29:23 +01:00
|
|
|
else if ( !answer[0] )
|
2014-08-23 21:26:38 +02:00
|
|
|
editor_type_edit(editor);
|
|
|
|
else
|
|
|
|
editor->modal_error = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void editor_modal_goto_line(struct editor* editor, const char* linestr)
|
|
|
|
{
|
|
|
|
if ( linestr[0] )
|
|
|
|
{
|
|
|
|
bool go_back = false, go_forward = false;
|
|
|
|
if ( linestr[0] == '+' )
|
|
|
|
linestr++, go_forward = true;
|
|
|
|
else if ( linestr[0] == '-' )
|
|
|
|
linestr++, go_back = true;
|
|
|
|
if ( !linestr[0] ) { editor->modal_error = true; return; }
|
|
|
|
const char* linestr_end;
|
|
|
|
unsigned long line = strtoul(linestr, (char**) &linestr_end, 0);
|
|
|
|
if ( *linestr_end ) { editor->modal_error = true; return; }
|
|
|
|
if ( go_back )
|
|
|
|
{
|
|
|
|
if ( editor->cursor_row < line )
|
|
|
|
{
|
|
|
|
editor->modal_error = true;
|
|
|
|
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-05 00:09:48 +02:00
|
|
|
editor_cursor_set(editor, editor->cursor_row - line, 0);
|
2014-08-23 21:26:38 +02:00
|
|
|
}
|
|
|
|
else if ( go_forward )
|
|
|
|
{
|
|
|
|
if ( editor->lines_used - (editor->cursor_row+1) < line )
|
|
|
|
{
|
|
|
|
editor->modal_error = true;
|
|
|
|
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-05 00:09:48 +02:00
|
|
|
editor_cursor_set(editor, editor->cursor_row + line, 0);
|
2014-08-23 21:26:38 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( editor->lines_used+1 <= line )
|
|
|
|
{
|
|
|
|
editor->modal_error = true;
|
|
|
|
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-05 00:09:48 +02:00
|
|
|
editor_cursor_set(editor, line ? line - 1 : 0, 0);
|
2014-08-23 21:26:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
editor_type_edit(editor);
|
|
|
|
}
|
|
|
|
|
|
|
|
void editor_modal_margin(struct editor* editor, const char* marginstr)
|
|
|
|
{
|
|
|
|
if ( !marginstr[0] )
|
|
|
|
editor->margin = SIZE_MAX;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char* end_ptr;
|
|
|
|
unsigned long margin = strtoul(marginstr, &end_ptr, 0);
|
|
|
|
if ( *end_ptr ) { editor->modal_error = true; return; }
|
|
|
|
editor->margin = margin;
|
|
|
|
}
|
|
|
|
editor_type_edit(editor);
|
|
|
|
}
|
|
|
|
|
|
|
|
void editor_modal_popen(struct editor* editor, const char* cmd)
|
|
|
|
{
|
|
|
|
if ( cmd[0] && editor_load_popen(editor, cmd) )
|
|
|
|
editor_type_edit(editor);
|
|
|
|
else
|
|
|
|
editor->modal_error = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void editor_modal_tabsize(struct editor* editor, const char* tabsizestr)
|
|
|
|
{
|
|
|
|
if ( !tabsizestr[0] )
|
|
|
|
editor->tabsize = 8;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char* end_ptr;
|
|
|
|
unsigned long tabsize = strtoul(tabsizestr, &end_ptr, 0);
|
|
|
|
if ( !tabsize || *end_ptr || 256 < tabsize )
|
|
|
|
{
|
|
|
|
editor->modal_error = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
editor->tabsize = tabsize;
|
|
|
|
}
|
|
|
|
editor_type_edit(editor);
|
|
|
|
}
|
|
|
|
|
|
|
|
void editor_modal_language(struct editor* editor, const char* language)
|
|
|
|
{
|
|
|
|
if ( !language[0] || !strcmp(language, "none") )
|
2015-07-09 23:00:16 +02:00
|
|
|
editor->highlight_source = LANGUAGE_NONE;
|
2016-03-06 00:14:35 +01:00
|
|
|
else if ( !strcmp(language, "c") || !strcmp(language, "c++") )
|
2015-07-09 23:00:16 +02:00
|
|
|
editor->highlight_source = LANGUAGE_C_CXX;
|
2016-03-06 00:14:35 +01:00
|
|
|
else if ( !strcmp(language, "diff") || !strcmp(language, "patch") )
|
2015-07-09 23:00:16 +02:00
|
|
|
editor->highlight_source = LANGUAGE_DIFF;
|
2016-03-06 00:14:35 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
editor->modal_error = true;
|
2014-08-23 21:26:38 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
editor_type_edit(editor);
|
|
|
|
}
|
|
|
|
|
2014-08-01 12:12:54 +02:00
|
|
|
void editor_modal_line_numbering(struct editor* editor, const char* truth)
|
|
|
|
{
|
|
|
|
if ( !is_truth_string(truth) )
|
|
|
|
{
|
|
|
|
editor->modal_error = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
editor->line_numbering = is_truth_true(truth);
|
|
|
|
editor_type_edit(editor);
|
|
|
|
}
|
|
|
|
|
2014-08-23 21:26:38 +02:00
|
|
|
bool is_modal_command(const char* cmd, const char* candidate, const char** rest)
|
|
|
|
{
|
|
|
|
size_t candidate_len = strlen(candidate);
|
|
|
|
if ( strncmp(cmd, candidate, candidate_len) == 0 &&
|
2015-08-11 15:37:31 +02:00
|
|
|
(!cmd[candidate_len] || isspace((unsigned char) cmd[candidate_len])) )
|
2014-08-23 21:26:38 +02:00
|
|
|
{
|
|
|
|
*rest = cmd + candidate_len;
|
2015-08-11 15:37:31 +02:00
|
|
|
while ( **rest && isspace((unsigned char) **rest) )
|
2014-08-23 21:26:38 +02:00
|
|
|
(*rest)++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void editor_modal_command(struct editor* editor, const char* cmd)
|
|
|
|
{
|
2015-08-11 15:37:31 +02:00
|
|
|
while ( *cmd && isspace((unsigned char) *cmd) )
|
2014-08-23 21:26:38 +02:00
|
|
|
cmd++;
|
|
|
|
if ( cmd[0] == ':' )
|
|
|
|
cmd++;
|
|
|
|
if ( !cmd[0] ) { editor_type_edit(editor); return; }
|
|
|
|
|
|
|
|
if ( !strcmp(cmd, "q") || !strcmp(cmd, "exit") || !strcmp(cmd, "quit") )
|
|
|
|
editor_type_quit(editor);
|
|
|
|
else if ( !strcmp(cmd, "q!") )
|
|
|
|
editor->dirty = false, editor_type_quit(editor);
|
|
|
|
else if ( !strcmp(cmd, "w") )
|
|
|
|
editor_type_save(editor);
|
|
|
|
else if ( !strcmp(cmd, "wq") || !strcmp(cmd, "wq!") )
|
|
|
|
editor->dirty ? editor_type_save(editor)
|
|
|
|
: editor_type_quit(editor);
|
|
|
|
else if ( is_modal_command(cmd, "margin", &cmd) )
|
|
|
|
editor_modal_margin(editor, cmd);
|
|
|
|
else if ( is_modal_command(cmd, "popen", &cmd) )
|
|
|
|
editor_modal_popen(editor, cmd);
|
|
|
|
else if ( is_modal_command(cmd, "tabsize", &cmd) )
|
|
|
|
editor_modal_tabsize(editor, cmd);
|
|
|
|
else if ( is_modal_command(cmd, "language", &cmd) )
|
|
|
|
editor_modal_language(editor, cmd);
|
2014-08-01 12:12:54 +02:00
|
|
|
else if ( is_modal_command(cmd, "line-numbering", &cmd) )
|
|
|
|
editor_modal_line_numbering(editor, cmd);
|
2014-08-23 21:26:38 +02:00
|
|
|
else
|
|
|
|
editor->modal_error = true;
|
|
|
|
}
|
|
|
|
|
2016-01-01 15:01:39 +00:00
|
|
|
void editor_modal_command_config(struct editor* editor, const char* cmd)
|
|
|
|
{
|
|
|
|
while ( *cmd && isspace((unsigned char) *cmd) )
|
|
|
|
cmd++;
|
|
|
|
if ( is_modal_command(cmd, "margin", &cmd) )
|
|
|
|
editor_modal_margin(editor, cmd);
|
|
|
|
else if ( is_modal_command(cmd, "tabsize", &cmd) )
|
|
|
|
editor_modal_tabsize(editor, cmd);
|
|
|
|
else if ( is_modal_command(cmd, "language", &cmd) )
|
|
|
|
editor_modal_language(editor, cmd);
|
|
|
|
else if ( is_modal_command(cmd, "line-numbering", &cmd) )
|
|
|
|
editor_modal_line_numbering(editor, cmd);
|
|
|
|
}
|
|
|
|
|
2021-10-31 02:55:26 +03:00
|
|
|
bool match_line(regex_t* regex, const wchar_t* line, size_t used,
|
|
|
|
bool start_of_line, size_t* start, size_t *end)
|
|
|
|
{
|
|
|
|
if ( !used )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
char* buffer = calloc(used, MB_CUR_MAX);
|
|
|
|
if ( !buffer )
|
|
|
|
return false;
|
|
|
|
size_t buffer_used = 0;
|
|
|
|
mbstate_t ps = {0};
|
|
|
|
for ( size_t i = 0; i < used; i++ )
|
|
|
|
buffer_used += wcrtomb(&buffer[buffer_used], line[i], &ps);
|
|
|
|
|
|
|
|
regmatch_t start_end[] = {{.rm_so = 0, .rm_eo = buffer_used}};
|
|
|
|
int flags = start_of_line ? REG_STARTEND : REG_STARTEND | REG_NOTBOL;
|
|
|
|
int no_match = regexec(regex, buffer, 1, start_end, flags);
|
|
|
|
free(buffer);
|
|
|
|
if ( no_match )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
char mb[MB_CUR_MAX];
|
|
|
|
memset(&ps, 0, sizeof(ps));
|
|
|
|
size_t wc_offset = 0;
|
|
|
|
regoff_t mb_offset = 0;
|
|
|
|
for ( ; mb_offset < start_end[0].rm_so; wc_offset++ )
|
|
|
|
mb_offset += wcrtomb(mb, line[wc_offset], &ps);
|
|
|
|
*start = wc_offset;
|
|
|
|
for ( ; mb_offset < start_end[0].rm_eo; wc_offset++ )
|
|
|
|
mb_offset += wcrtomb(mb, line[wc_offset], &ps);
|
|
|
|
*end = wc_offset;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void editor_modal_search(struct editor* editor, const char* search)
|
|
|
|
{
|
|
|
|
if ( !search[0] )
|
|
|
|
{
|
|
|
|
editor_type_edit(editor);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
regex_t regex;
|
|
|
|
if ( regcomp(®ex, search, REG_EXTENDED) )
|
|
|
|
{
|
|
|
|
editor->modal_error = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t column = editor->cursor_column + 1;
|
|
|
|
if ( column < editor->lines[editor->cursor_row].used )
|
|
|
|
{
|
|
|
|
const wchar_t* line = &editor->lines[editor->cursor_row].data[column];
|
|
|
|
size_t length = editor->lines[editor->cursor_row].used - column;
|
|
|
|
size_t match, match_end;
|
|
|
|
if ( match_line(®ex, line, length, false, &match, &match_end) )
|
|
|
|
{
|
|
|
|
editor_cursor_set(editor, editor->cursor_row, match + column);
|
|
|
|
editor_select_set(editor, editor->cursor_row, match_end + column);
|
|
|
|
regfree(®ex);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t line = editor->cursor_row + 1;
|
|
|
|
size_t remaining = editor->lines_used;
|
|
|
|
while ( remaining-- )
|
|
|
|
{
|
|
|
|
if ( editor->lines_used <= line )
|
|
|
|
line = 0;
|
|
|
|
|
|
|
|
size_t match, match_end;
|
|
|
|
if ( match_line(®ex, editor->lines[line].data,
|
|
|
|
editor->lines[line].used, true, &match, &match_end) )
|
|
|
|
{
|
|
|
|
editor_cursor_set(editor, line, match);
|
|
|
|
editor_select_set(editor, line, match_end);
|
|
|
|
regfree(®ex);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
line++;
|
|
|
|
}
|
|
|
|
|
|
|
|
regfree(®ex);
|
|
|
|
|
|
|
|
editor->modal_error = true;
|
|
|
|
}
|
|
|
|
|
2016-01-01 15:01:39 +00:00
|
|
|
|
2014-08-23 21:26:38 +02:00
|
|
|
void editor_modal_character(struct editor* editor, wchar_t c)
|
|
|
|
{
|
|
|
|
if ( editor->control )
|
|
|
|
{
|
|
|
|
switch ( towlower(c) )
|
|
|
|
{
|
|
|
|
case L'c': editor_type_edit(editor); break;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
editor->modal_error = false;
|
|
|
|
|
|
|
|
if ( c == L'\n' )
|
|
|
|
{
|
|
|
|
if ( !editor->modal )
|
|
|
|
editor->modal = (wchar_t*) malloc(sizeof(wchar_t) * 1);
|
|
|
|
|
|
|
|
editor->modal[editor->modal_used] = L'\0';
|
|
|
|
char* param = convert_wcs_to_mbs(editor->modal);
|
|
|
|
switch ( editor->mode )
|
|
|
|
{
|
|
|
|
case MODE_LOAD: editor_modal_load(editor, param); break;
|
|
|
|
case MODE_SAVE: editor_modal_save(editor, param); break;
|
2016-02-24 23:29:23 +01:00
|
|
|
case MODE_SAVE_LOAD: editor_modal_save_load(editor, param); break;
|
|
|
|
case MODE_SAVE_QUIT: editor_modal_save_quit(editor, param); break;
|
|
|
|
case MODE_ASK_LOAD: editor_modal_ask_load(editor, param); break;
|
2014-08-23 21:26:38 +02:00
|
|
|
case MODE_ASK_QUIT: editor_modal_ask_quit(editor, param); break;
|
|
|
|
case MODE_GOTO_LINE: editor_modal_goto_line(editor, param); break;
|
|
|
|
case MODE_COMMAND: editor_modal_command(editor, param); break;
|
2021-10-31 02:55:26 +03:00
|
|
|
case MODE_SEARCH: editor_modal_search(editor, param); break;
|
2014-08-23 21:26:38 +02:00
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
free(param);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( editor->modal_used == editor->modal_length )
|
|
|
|
{
|
|
|
|
size_t new_length = editor->modal_length ? 2 * editor->modal_length : 8;
|
|
|
|
wchar_t* new_data = (wchar_t*) malloc(sizeof(wchar_t) * (new_length + 1));
|
|
|
|
for ( size_t i = 0; i < editor->modal_used; i++ )
|
|
|
|
new_data[i] = editor->modal[i];
|
|
|
|
free(editor->modal);
|
|
|
|
editor->modal = new_data;
|
|
|
|
editor->modal_length = new_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( size_t i = editor->modal_used; editor->modal_cursor < i; i-- )
|
|
|
|
editor->modal[i] = editor->modal[i-1];
|
|
|
|
editor->modal_used++;
|
|
|
|
editor->modal[editor->modal_cursor++] = c;
|
|
|
|
}
|