1
1
little optimization, renamed:
        * variables stack* to undo_stack*
        * pop_action to edit_pop_undo_action
        * edit_push_action to edit_push_undo_action

Signed-off-by: Ilia Maslakov <il.smind@gmail.com>
Этот коммит содержится в:
Ilia Maslakov 2010-12-19 17:36:06 +00:00
родитель 8c1fba1ffa
Коммит b5fc178027
4 изменённых файлов: 71 добавлений и 100 удалений

Просмотреть файл

@ -259,7 +259,7 @@ void edit_insert (WEdit * edit, int c);
void edit_cursor_move (WEdit * edit, long increment); void edit_cursor_move (WEdit * edit, long increment);
void edit_move_block_to_right (WEdit * edit); void edit_move_block_to_right (WEdit * edit);
void edit_move_block_to_left (WEdit * edit); void edit_move_block_to_left (WEdit * edit);
void edit_push_action (WEdit * edit, long c, ...); void edit_push_undo_action (WEdit * edit, long c, ...);
void edit_push_key_press (WEdit * edit); void edit_push_key_press (WEdit * edit);
void edit_insert_ahead (WEdit * edit, int c); void edit_insert_ahead (WEdit * edit, int c);
long edit_write_stream (WEdit * edit, FILE * f); long edit_write_stream (WEdit * edit, FILE * f);

Просмотреть файл

@ -106,12 +106,12 @@ struct WEdit
GArray *serialized_bookmarks; GArray *serialized_bookmarks;
/* undo stack and pointers */ /* undo stack and pointers */
unsigned long stack_pointer; unsigned long undo_stack_pointer;
long *undo_stack; long *undo_stack;
unsigned long stack_size; unsigned long undo_stack_size;
unsigned long stack_size_mask; unsigned long undo_stack_size_mask;
unsigned long stack_bottom; unsigned long undo_stack_bottom;
unsigned int stack_disable:1; /* If not 0, don't save events in the undo stack */ unsigned int undo_stack_disable:1; /* If not 0, don't save events in the undo stack */
struct stat stat1; /* Result of mc_fstat() on the file */ struct stat stat1; /* Result of mc_fstat() on the file */
unsigned int skip_detach_prompt:1; /* Do not prompt whether to detach a file anymore */ unsigned int skip_detach_prompt:1; /* Do not prompt whether to detach a file anymore */

Просмотреть файл

@ -478,13 +478,13 @@ edit_load_file (WEdit * edit)
edit->last_byte = 0; edit->last_byte = 0;
if (*edit->filename) if (*edit->filename)
{ {
edit->stack_disable = 1; edit->undo_stack_disable = 1;
if (!edit_insert_file (edit, edit->filename)) if (!edit_insert_file (edit, edit->filename))
{ {
edit_clean (edit); edit_clean (edit);
return 1; return 1;
} }
edit->stack_disable = 0; edit->undo_stack_disable = 0;
} }
} }
edit->lb = LB_ASIS; edit->lb = LB_ASIS;
@ -580,31 +580,31 @@ edit_set_keymap (void)
*/ */
static long static long
pop_action (WEdit * edit) edit_pop_undo_action (WEdit * edit)
{ {
long c; long c;
unsigned long sp = edit->stack_pointer; unsigned long sp = edit->undo_stack_pointer;
if (sp == edit->stack_bottom) if (sp == edit->undo_stack_bottom)
return STACK_BOTTOM; return STACK_BOTTOM;
sp = (sp - 1) & edit->stack_size_mask; sp = (sp - 1) & edit->undo_stack_size_mask;
c = edit->undo_stack[sp]; c = edit->undo_stack[sp];
if (c >= 0) if (c >= 0)
{ {
/* edit->undo_stack[sp] = '@'; */ /* edit->undo_stack[sp] = '@'; */
edit->stack_pointer = (edit->stack_pointer - 1) & edit->stack_size_mask; edit->undo_stack_pointer = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
return c; return c;
} }
if (sp == edit->stack_bottom) if (sp == edit->undo_stack_bottom)
return STACK_BOTTOM; return STACK_BOTTOM;
c = edit->undo_stack[(sp - 1) & edit->stack_size_mask]; c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
if (edit->undo_stack[sp] == -2) if (edit->undo_stack[sp] == -2)
{ {
/* edit->undo_stack[sp] = '@'; */ /* edit->undo_stack[sp] = '@'; */
edit->stack_pointer = sp; edit->undo_stack_pointer = sp;
} }
else else
edit->undo_stack[sp]++; edit->undo_stack[sp]++;
@ -682,7 +682,7 @@ edit_backspace (WEdit * edit, const int byte_delete)
} }
edit->last_byte--; edit->last_byte--;
edit->curs1--; edit->curs1--;
edit_push_action (edit, p); edit_push_undo_action (edit, p);
} }
edit_modification (edit); edit_modification (edit);
if (p == '\n') if (p == '\n')
@ -1226,9 +1226,9 @@ edit_do_undo (WEdit * edit)
long ac; long ac;
long count = 0; long count = 0;
edit->stack_disable = 1; /* don't record undo's onto undo stack! */ edit->undo_stack_disable = 1; /* don't record undo's onto undo stack! */
edit->over_col = 0; edit->over_col = 0;
while ((ac = pop_action (edit)) < KEY_PRESS) while ((ac = edit_pop_undo_action (edit)) < KEY_PRESS)
{ {
switch ((int) ac) switch ((int) ac)
{ {
@ -1286,7 +1286,7 @@ edit_do_undo (WEdit * edit)
edit_update_curs_row (edit); edit_update_curs_row (edit);
done_undo:; done_undo:;
edit->stack_disable = 0; edit->undo_stack_disable = 0;
} }
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
@ -2027,9 +2027,9 @@ edit_init (WEdit * edit, int lines, int columns, const char *filename, long line
edit->bracket = -1; edit->bracket = -1;
edit->force |= REDRAW_PAGE; edit->force |= REDRAW_PAGE;
edit_set_filename (edit, filename); edit_set_filename (edit, filename);
edit->stack_size = START_STACK_SIZE; edit->undo_stack_size = START_STACK_SIZE;
edit->stack_size_mask = START_STACK_SIZE - 1; edit->undo_stack_size_mask = START_STACK_SIZE - 1;
edit->undo_stack = g_malloc0 ((edit->stack_size + 10) * sizeof (long)); edit->undo_stack = g_malloc0 ((edit->undo_stack_size + 10) * sizeof (long));
edit->utf8 = 0; edit->utf8 = 0;
edit->converter = str_cnv_from_term; edit->converter = str_cnv_from_term;
@ -2259,52 +2259,40 @@ edit_set_codeset (WEdit * edit)
*/ */
void void
edit_push_action (WEdit * edit, long c, ...) edit_push_undo_action (WEdit * edit, long c, ...)
{ {
unsigned long sp = edit->stack_pointer; unsigned long sp = edit->undo_stack_pointer;
unsigned long spm1; unsigned long spm1;
long *t; long *t;
/* first enlarge the stack if necessary */ /* first enlarge the stack if necessary */
if (sp > edit->stack_size - 10) if (sp > edit->undo_stack_size - 10)
{ /* say */ { /* say */
if (option_max_undo < 256) if (option_max_undo < 256)
option_max_undo = 256; option_max_undo = 256;
if (edit->stack_size < (unsigned long) option_max_undo) if (edit->undo_stack_size < (unsigned long) option_max_undo)
{ {
t = g_realloc (edit->undo_stack, (edit->stack_size * 2 + 10) * sizeof (long)); t = g_realloc (edit->undo_stack, (edit->undo_stack_size * 2 + 10) * sizeof (long));
if (t) if (t)
{ {
edit->undo_stack = t; edit->undo_stack = t;
edit->stack_size <<= 1; edit->undo_stack_size <<= 1;
edit->stack_size_mask = edit->stack_size - 1; edit->undo_stack_size_mask = edit->undo_stack_size - 1;
} }
} }
} }
spm1 = (edit->stack_pointer - 1) & edit->stack_size_mask; spm1 = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
if (edit->stack_disable) if (edit->undo_stack_disable)
return; return;
#ifdef FAST_MOVE_CURSOR if (edit->undo_stack_bottom != sp
if (c == CURS_LEFT_LOTS || c == CURS_RIGHT_LOTS) && spm1 != edit->undo_stack_bottom
{ && ((sp - 2) & edit->undo_stack_size_mask) != edit->undo_stack_bottom)
va_list ap;
edit->undo_stack[sp] = (c == CURS_LEFT_LOTS) ? CURS_LEFT : CURS_RIGHT;
edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask;
va_start (ap, c);
c = -(va_arg (ap, int));
va_end (ap);
}
else
#endif /* ! FAST_MOVE_CURSOR */
if (edit->stack_bottom != sp
&& spm1 != edit->stack_bottom
&& ((sp - 2) & edit->stack_size_mask) != edit->stack_bottom)
{ {
int d; int d;
if (edit->undo_stack[spm1] < 0) if (edit->undo_stack[spm1] < 0)
{ {
d = edit->undo_stack[(sp - 2) & edit->stack_size_mask]; d = edit->undo_stack[(sp - 2) & edit->undo_stack_size_mask];
if (d == c) if (d == c)
{ {
if (edit->undo_stack[spm1] > -1000000000) if (edit->undo_stack[spm1] > -1000000000)
@ -2315,16 +2303,6 @@ edit_push_action (WEdit * edit, long c, ...)
} }
} }
/* #define NO_STACK_CURSMOVE_ANIHILATION */ /* #define NO_STACK_CURSMOVE_ANIHILATION */
#ifndef NO_STACK_CURSMOVE_ANIHILATION
else if ((c == CURS_LEFT && d == CURS_RIGHT) || (c == CURS_RIGHT && d == CURS_LEFT))
{ /* a left then a right anihilate each other */
if (edit->undo_stack[spm1] == -2)
edit->stack_pointer = spm1;
else
edit->undo_stack[spm1]++;
return;
}
#endif
} }
else else
{ {
@ -2336,37 +2314,30 @@ edit_push_action (WEdit * edit, long c, ...)
edit->undo_stack[sp] = -2; edit->undo_stack[sp] = -2;
goto check_bottom; goto check_bottom;
} }
#ifndef NO_STACK_CURSMOVE_ANIHILATION
else if ((c == CURS_LEFT && d == CURS_RIGHT) || (c == CURS_RIGHT && d == CURS_LEFT))
{ /* a left then a right anihilate each other */
edit->stack_pointer = spm1;
return;
}
#endif
} }
} }
edit->undo_stack[sp] = c; edit->undo_stack[sp] = c;
check_bottom: check_bottom:
edit->stack_pointer = (edit->stack_pointer + 1) & edit->stack_size_mask; edit->undo_stack_pointer = (edit->undo_stack_pointer + 1) & edit->undo_stack_size_mask;
/* if the sp wraps round and catches the stack_bottom then erase /* if the sp wraps round and catches the undo_stack_bottom then erase
* the first set of actions on the stack to make space - by moving * the first set of actions on the stack to make space - by moving
* stack_bottom forward one "key press" */ * undo_stack_bottom forward one "key press" */
c = (edit->stack_pointer + 2) & edit->stack_size_mask; c = (edit->undo_stack_pointer + 2) & edit->undo_stack_size_mask;
if ((unsigned long) c == edit->stack_bottom || if ((unsigned long) c == edit->undo_stack_bottom ||
(((unsigned long) c + 1) & edit->stack_size_mask) == edit->stack_bottom) (((unsigned long) c + 1) & edit->undo_stack_size_mask) == edit->undo_stack_bottom)
do do
{ {
edit->stack_bottom = (edit->stack_bottom + 1) & edit->stack_size_mask; edit->undo_stack_bottom = (edit->undo_stack_bottom + 1) & edit->undo_stack_size_mask;
} }
while (edit->undo_stack[edit->stack_bottom] < KEY_PRESS while (edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS
&& edit->stack_bottom != edit->stack_pointer); && edit->undo_stack_bottom != edit->undo_stack_pointer);
/*If a single key produced enough pushes to wrap all the way round then we would notice that the [stack_bottom] does not contain KEY_PRESS. The stack is then initialised: */ /*If a single key produced enough pushes to wrap all the way round then we would notice that the [undo_stack_bottom] does not contain KEY_PRESS. The stack is then initialised: */
if (edit->stack_pointer != edit->stack_bottom if (edit->undo_stack_pointer != edit->undo_stack_bottom
&& edit->undo_stack[edit->stack_bottom] < KEY_PRESS) && edit->undo_stack[edit->undo_stack_bottom] < KEY_PRESS)
edit->stack_bottom = edit->stack_pointer = 0; edit->undo_stack_bottom = edit->undo_stack_pointer = 0;
} }
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
@ -2407,7 +2378,7 @@ edit_insert (WEdit * edit, int c)
} }
/* save the reverse command onto the undo stack */ /* save the reverse command onto the undo stack */
edit_push_action (edit, BACKSPACE); edit_push_undo_action (edit, BACKSPACE);
/* update markers */ /* update markers */
edit->mark1 += (edit->mark1 > edit->curs1); edit->mark1 += (edit->mark1 > edit->curs1);
@ -2452,7 +2423,7 @@ edit_insert_ahead (WEdit * edit, int c)
edit->total_lines++; edit->total_lines++;
edit->force |= REDRAW_AFTER_CURSOR; edit->force |= REDRAW_AFTER_CURSOR;
} }
edit_push_action (edit, DELCHAR); edit_push_undo_action (edit, DELCHAR);
edit->mark1 += (edit->mark1 >= edit->curs1); edit->mark1 += (edit->mark1 >= edit->curs1);
edit->mark2 += (edit->mark2 >= edit->curs1); edit->mark2 += (edit->mark2 >= edit->curs1);
@ -2511,7 +2482,7 @@ edit_delete (WEdit * edit, const int byte_delete)
} }
edit->last_byte--; edit->last_byte--;
edit->curs2--; edit->curs2--;
edit_push_action (edit, p + 256); edit_push_undo_action (edit, p + 256);
} }
edit_modification (edit); edit_modification (edit);
@ -2545,7 +2516,7 @@ edit_move_backward_lots (WEdit * edit, long increment)
increment = edit->curs1; increment = edit->curs1;
if (increment <= 0) if (increment <= 0)
return -1; return -1;
edit_push_action (edit, CURS_RIGHT_LOTS, increment); edit_push_undo_action (edit, CURS_RIGHT_LOTS, increment);
t = r = EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE); t = r = EDIT_BUF_SIZE - (edit->curs2 & M_EDIT_BUF_SIZE);
if (r > increment) if (r > increment)
@ -2655,7 +2626,7 @@ edit_cursor_move (WEdit * edit, long increment)
if (!edit->curs1) if (!edit->curs1)
return; return;
edit_push_action (edit, CURS_RIGHT); edit_push_undo_action (edit, CURS_RIGHT);
c = edit_get_byte (edit, edit->curs1 - 1); c = edit_get_byte (edit, edit->curs1 - 1);
if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE)) if (!((edit->curs2 + 1) & M_EDIT_BUF_SIZE))
@ -2686,7 +2657,7 @@ edit_cursor_move (WEdit * edit, long increment)
if (!edit->curs2) if (!edit->curs2)
return; return;
edit_push_action (edit, CURS_LEFT); edit_push_undo_action (edit, CURS_LEFT);
c = edit_get_byte (edit, edit->curs1); c = edit_get_byte (edit, edit->curs1);
if (!(edit->curs1 & M_EDIT_BUF_SIZE)) if (!(edit->curs1 & M_EDIT_BUF_SIZE))
@ -3059,8 +3030,8 @@ edit_move_display (WEdit * e, long line)
void void
edit_push_markers (WEdit * edit) edit_push_markers (WEdit * edit)
{ {
edit_push_action (edit, MARK_1 + edit->mark1); edit_push_undo_action (edit, MARK_1 + edit->mark1);
edit_push_action (edit, MARK_2 + edit->mark2); edit_push_undo_action (edit, MARK_2 + edit->mark2);
} }
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
@ -3186,9 +3157,9 @@ edit_insert_indent (WEdit * edit, int indent)
void void
edit_push_key_press (WEdit * edit) edit_push_key_press (WEdit * edit)
{ {
edit_push_action (edit, KEY_PRESS + edit->start_display); edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
if (edit->mark2 == -1) if (edit->mark2 == -1)
edit_push_action (edit, MARK_1 + edit->mark1); edit_push_undo_action (edit, MARK_1 + edit->mark1);
} }
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
@ -3362,7 +3333,7 @@ edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
if (!option_persistent_selections) if (!option_persistent_selections)
{ {
if (edit->column_highlight) if (edit->column_highlight)
edit_push_action (edit, COLUMN_ON); edit_push_undo_action (edit, COLUMN_ON);
edit->column_highlight = 0; edit->column_highlight = 0;
edit_mark_cmd (edit, 1); edit_mark_cmd (edit, 1);
} }
@ -3640,14 +3611,14 @@ edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
if (edit->mark2 >= 0) if (edit->mark2 >= 0)
{ {
if (edit->column_highlight) if (edit->column_highlight)
edit_push_action (edit, COLUMN_ON); edit_push_undo_action (edit, COLUMN_ON);
edit->column_highlight = 0; edit->column_highlight = 0;
} }
edit_mark_cmd (edit, 0); edit_mark_cmd (edit, 0);
break; break;
case CK_Column_Mark: case CK_Column_Mark:
if (!edit->column_highlight) if (!edit->column_highlight)
edit_push_action (edit, COLUMN_OFF); edit_push_undo_action (edit, COLUMN_OFF);
edit->column_highlight = 1; edit->column_highlight = 1;
edit_mark_cmd (edit, 0); edit_mark_cmd (edit, 0);
break; break;
@ -3657,7 +3628,7 @@ edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
break; break;
case CK_Unmark: case CK_Unmark:
if (edit->column_highlight) if (edit->column_highlight)
edit_push_action (edit, COLUMN_ON); edit_push_undo_action (edit, COLUMN_ON);
edit->column_highlight = 0; edit->column_highlight = 0;
edit_mark_cmd (edit, 1); edit_mark_cmd (edit, 1);
break; break;

Просмотреть файл

@ -937,7 +937,7 @@ edit_do_search (WEdit * edit)
if (edit->search == NULL) if (edit->search == NULL)
edit->search_start = edit->curs1; edit->search_start = edit->curs1;
edit_push_action (edit, KEY_PRESS + edit->start_display); edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
if (search_create_bookmark) if (search_create_bookmark)
{ {
@ -1397,7 +1397,7 @@ edit_save_as_cmd (WEdit * edit)
return 0; return 0;
exp = edit_get_save_file_as (edit); exp = edit_get_save_file_as (edit);
edit_push_action (edit, KEY_PRESS + edit->start_display); edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
if (exp) if (exp)
{ {
@ -1508,7 +1508,7 @@ edit_save_macro_cmd (WEdit * edit, struct macro macro[], int n)
FILE *f; FILE *f;
int s, i; int s, i;
edit_push_action (edit, KEY_PRESS + edit->start_display); edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
s = editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), 1); s = editcmd_dialog_raw_key_query (_("Save macro"), _("Press the macro's new hotkey:"), 1);
edit->force |= REDRAW_COMPLETELY; edit->force |= REDRAW_COMPLETELY;
if (s) if (s)
@ -1928,7 +1928,7 @@ edit_block_copy_cmd (WEdit * edit)
if (edit->column_highlight) if (edit->column_highlight)
{ {
edit_set_markers (edit, 0, 0, 0, 0); edit_set_markers (edit, 0, 0, 0, 0);
edit_push_action (edit, COLUMN_ON); edit_push_undo_action (edit, COLUMN_ON);
edit->column_highlight = 0; edit->column_highlight = 0;
} }
else if (start_mark < current && end_mark > current) else if (start_mark < current && end_mark > current)
@ -2007,7 +2007,7 @@ edit_block_move_cmd (WEdit * edit)
edit->curs1), x, 0) - edit->curs1); edit->curs1), x, 0) - edit->curs1);
} }
edit_set_markers (edit, 0, 0, 0, 0); edit_set_markers (edit, 0, 0, 0, 0);
edit_push_action (edit, COLUMN_ON); edit_push_undo_action (edit, COLUMN_ON);
edit->column_highlight = 0; edit->column_highlight = 0;
} }
else else
@ -2089,7 +2089,7 @@ edit_replace_cmd (WEdit * edit, int again)
disp1 = edit_replace_cmd__conv_to_display (saved1 ? saved1 : (char *) ""); disp1 = edit_replace_cmd__conv_to_display (saved1 ? saved1 : (char *) "");
disp2 = edit_replace_cmd__conv_to_display (saved2 ? saved2 : (char *) ""); disp2 = edit_replace_cmd__conv_to_display (saved2 ? saved2 : (char *) "");
edit_push_action (edit, KEY_PRESS + edit->start_display); edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
editcmd_dialog_replace_show (edit, disp1, disp2, &input1, &input2); editcmd_dialog_replace_show (edit, disp1, disp2, &input1, &input2);
@ -2566,7 +2566,7 @@ edit_save_block_cmd (WEdit * edit)
input_expand_dialog (_("Save block"), _("Enter file name:"), input_expand_dialog (_("Save block"), _("Enter file name:"),
MC_HISTORY_EDIT_SAVE_BLOCK, tmp); MC_HISTORY_EDIT_SAVE_BLOCK, tmp);
g_free (tmp); g_free (tmp);
edit_push_action (edit, KEY_PRESS + edit->start_display); edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
if (exp) if (exp)
{ {
if (!*exp) if (!*exp)
@ -2607,7 +2607,7 @@ edit_insert_file_cmd (WEdit * edit)
exp = input_expand_dialog (_("Insert file"), _("Enter file name:"), exp = input_expand_dialog (_("Insert file"), _("Enter file name:"),
MC_HISTORY_EDIT_INSERT_FILE, tmp); MC_HISTORY_EDIT_INSERT_FILE, tmp);
g_free (tmp); g_free (tmp);
edit_push_action (edit, KEY_PRESS + edit->start_display); edit_push_undo_action (edit, KEY_PRESS + edit->start_display);
if (exp) if (exp)
{ {
if (!*exp) if (!*exp)