vi: clear undo buffer when we change to another file

function                                             old     new   delta
init_text_buffer                                     156     190     +34
undo_push                                            360     382     +22
count_lines                                           74      72      -2
undo_pop                                             246     222     -24
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/2 up/down: 56/-26)             Total: 30 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2014-04-03 12:47:48 +02:00
parent 2c51202aec
commit e7430867a8

View File

@ -306,8 +306,8 @@ struct globals {
smallint editing; // >0 while we are editing a file smallint editing; // >0 while we are editing a file
// [code audit says "can be 0, 1 or 2 only"] // [code audit says "can be 0, 1 or 2 only"]
smallint cmd_mode; // 0=command 1=insert 2=replace smallint cmd_mode; // 0=command 1=insert 2=replace
int file_modified; // buffer contents changed (counter, not flag!) int modified_count; // buffer contents changed if !0
int last_file_modified; // = -1; int last_modified_count; // = -1;
int save_argc; // how many file names on cmd line int save_argc; // how many file names on cmd line
int cmdcnt; // repetition count int cmdcnt; // repetition count
unsigned rows, columns; // the terminal screen is this size unsigned rows, columns; // the terminal screen is this size
@ -378,37 +378,37 @@ struct globals {
char scr_out_buf[MAX_SCR_COLS + MAX_TABSTOP * 2]; char scr_out_buf[MAX_SCR_COLS + MAX_TABSTOP * 2];
#if ENABLE_FEATURE_VI_UNDO #if ENABLE_FEATURE_VI_UNDO
// undo_push() operations // undo_push() operations
#define UNDO_INS 0 #define UNDO_INS 0
#define UNDO_DEL 1 #define UNDO_DEL 1
#define UNDO_INS_CHAIN 2 #define UNDO_INS_CHAIN 2
#define UNDO_DEL_CHAIN 3 #define UNDO_DEL_CHAIN 3
// UNDO_*_QUEUED must be equal to UNDO_xxx ORed with UNDO_QUEUED_FLAG // UNDO_*_QUEUED must be equal to UNDO_xxx ORed with UNDO_QUEUED_FLAG
#define UNDO_QUEUED_FLAG 4 #define UNDO_QUEUED_FLAG 4
#define UNDO_INS_QUEUED 4 #define UNDO_INS_QUEUED 4
#define UNDO_DEL_QUEUED 5 #define UNDO_DEL_QUEUED 5
#define UNDO_USE_SPOS 32 #define UNDO_USE_SPOS 32
#define UNDO_EMPTY 64 #define UNDO_EMPTY 64
// Pass-through flags for functions that can be undone // Pass-through flags for functions that can be undone
#define NO_UNDO 0 #define NO_UNDO 0
#define ALLOW_UNDO 1 #define ALLOW_UNDO 1
#define ALLOW_UNDO_CHAIN 2 #define ALLOW_UNDO_CHAIN 2
#if ENABLE_FEATURE_VI_UNDO_QUEUE # if ENABLE_FEATURE_VI_UNDO_QUEUE
#define ALLOW_UNDO_QUEUED 3 #define ALLOW_UNDO_QUEUED 3
char undo_queue_state; char undo_queue_state;
int undo_q; int undo_q;
char *undo_queue_spos; // Start position of queued operation char *undo_queue_spos; // Start position of queued operation
char undo_queue[CONFIG_FEATURE_VI_UNDO_QUEUE_MAX]; char undo_queue[CONFIG_FEATURE_VI_UNDO_QUEUE_MAX];
#else # else
// If undo queuing disabled, don't invoke the missing queue logic // If undo queuing disabled, don't invoke the missing queue logic
#define ALLOW_UNDO_QUEUED 1 #define ALLOW_UNDO_QUEUED 1
#endif /* ENABLE_FEATURE_VI_UNDO_QUEUE */ # endif
struct undo_object { struct undo_object {
struct undo_object *prev; // Linking back avoids list traversal (LIFO) struct undo_object *prev; // Linking back avoids list traversal (LIFO)
int u_type; // 0=deleted, 1=inserted, 2=swapped
int start; // Offset where the data should be restored/deleted int start; // Offset where the data should be restored/deleted
int length; // total data size int length; // total data size
char *undo_text; // ptr to text that will be inserted uint8_t u_type; // 0=deleted, 1=inserted, 2=swapped
char undo_text[1]; // text that was deleted (if deletion)
} *undo_stack_tail; } *undo_stack_tail;
#endif /* ENABLE_FEATURE_VI_UNDO */ #endif /* ENABLE_FEATURE_VI_UNDO */
@ -423,8 +423,8 @@ struct globals {
#define vi_setops (G.vi_setops ) #define vi_setops (G.vi_setops )
#define editing (G.editing ) #define editing (G.editing )
#define cmd_mode (G.cmd_mode ) #define cmd_mode (G.cmd_mode )
#define file_modified (G.file_modified ) #define modified_count (G.modified_count )
#define last_file_modified (G.last_file_modified ) #define last_modified_count (G.last_modified_count)
#define save_argc (G.save_argc ) #define save_argc (G.save_argc )
#define cmdcnt (G.cmdcnt ) #define cmdcnt (G.cmdcnt )
#define rows (G.rows ) #define rows (G.rows )
@ -485,7 +485,7 @@ struct globals {
#define INIT_G() do { \ #define INIT_G() do { \
SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
last_file_modified = -1; \ last_modified_count = -1; \
/* "" but has space for 2 chars: */ \ /* "" but has space for 2 chars: */ \
IF_FEATURE_VI_SEARCH(last_search_pattern = xzalloc(2);) \ IF_FEATURE_VI_SEARCH(last_search_pattern = xzalloc(2);) \
} while (0) } while (0)
@ -608,6 +608,7 @@ static char what_reg(void); // what is letter of current YDreg
static void check_context(char); // remember context for '' command static void check_context(char); // remember context for '' command
#endif #endif
#if ENABLE_FEATURE_VI_UNDO #if ENABLE_FEATURE_VI_UNDO
static void flush_undo_data(void);
static void undo_push(char *, unsigned int, unsigned char); // Push an operation on the undo stack static void undo_push(char *, unsigned int, unsigned char); // Push an operation on the undo stack
static void undo_pop(void); // Undo the last operation static void undo_pop(void); // Undo the last operation
# if ENABLE_FEATURE_VI_UNDO_QUEUE # if ENABLE_FEATURE_VI_UNDO_QUEUE
@ -616,6 +617,7 @@ static void undo_queue_commit(void); // Flush any queued objects to the undo sta
# define undo_queue_commit() ((void)0) # define undo_queue_commit() ((void)0)
# endif # endif
#else #else
#define flush_undo_data() ((void)0)
#define undo_queue_commit() ((void)0) #define undo_queue_commit() ((void)0)
#endif #endif
@ -725,6 +727,8 @@ static int init_text_buffer(char *fn)
int rc; int rc;
int size = file_size(fn); // file size. -1 means does not exist. int size = file_size(fn); // file size. -1 means does not exist.
flush_undo_data();
/* allocate/reallocate text buffer */ /* allocate/reallocate text buffer */
free(text); free(text);
text_size = size + 10240; text_size = size + 10240;
@ -735,14 +739,14 @@ static int init_text_buffer(char *fn)
current_filename = xstrdup(fn); current_filename = xstrdup(fn);
} }
if (size < 0) { if (size < 0) {
// file dont exist. Start empty buf with dummy line // file doesnt exist. Start empty buf with dummy line
char_insert(text, '\n', NO_UNDO); char_insert(text, '\n', NO_UNDO);
rc = 0; rc = 0;
} else { } else {
rc = file_insert(fn, text, 1); rc = file_insert(fn, text, 1);
} }
file_modified = 0; modified_count = 0;
last_file_modified = -1; last_modified_count = -1;
#if ENABLE_FEATURE_VI_YANKMARK #if ENABLE_FEATURE_VI_YANKMARK
/* init the marks. */ /* init the marks. */
memset(mark, 0, sizeof(mark)); memset(mark, 0, sizeof(mark));
@ -1124,7 +1128,7 @@ static void colon(char *buf)
dot_skip_over_ws(); dot_skip_over_ws();
} else if (strncmp(cmd, "edit", i) == 0) { // Edit a file } else if (strncmp(cmd, "edit", i) == 0) { // Edit a file
// don't edit, if the current file has been modified // don't edit, if the current file has been modified
if (file_modified && !useforce) { if (modified_count && !useforce) {
status_line_bold("No write since last change (:%s! overrides)", cmd); status_line_bold("No write since last change (:%s! overrides)", cmd);
goto ret; goto ret;
} }
@ -1227,7 +1231,7 @@ static void colon(char *buf)
goto ret; goto ret;
} }
// don't exit if the file been modified // don't exit if the file been modified
if (file_modified) { if (modified_count) {
status_line_bold("No write since last change (:%s! overrides)", cmd); status_line_bold("No write since last change (:%s! overrides)", cmd);
goto ret; goto ret;
} }
@ -1280,10 +1284,10 @@ static void colon(char *buf)
// if the insert is before "dot" then we need to update // if the insert is before "dot" then we need to update
if (q <= dot) if (q <= dot)
dot += ch; dot += ch;
// file_modified++; // modified_count++;
} }
} else if (strncmp(cmd, "rewind", i) == 0) { // rewind cmd line args } else if (strncmp(cmd, "rewind", i) == 0) { // rewind cmd line args
if (file_modified && !useforce) { if (modified_count && !useforce) {
status_line_bold("No write since last change (:%s! overrides)", cmd); status_line_bold("No write since last change (:%s! overrides)", cmd);
} else { } else {
// reset the filenames to edit // reset the filenames to edit
@ -1438,8 +1442,8 @@ static void colon(char *buf)
} else { } else {
status_line("'%s' %dL, %dC", fn, li, l); status_line("'%s' %dL, %dC", fn, li, l);
if (q == text && r == end - 1 && l == ch) { if (q == text && r == end - 1 && l == ch) {
file_modified = 0; modified_count = 0;
last_file_modified = -1; last_modified_count = -1;
} }
if ((cmd[0] == 'x' || cmd[1] == 'q' || cmd[1] == 'n' if ((cmd[0] == 'x' || cmd[1] == 'q' || cmd[1] == 'n'
|| cmd[0] == 'X' || cmd[1] == 'Q' || cmd[1] == 'N' || cmd[0] == 'X' || cmd[1] == 'Q' || cmd[1] == 'N'
@ -1940,7 +1944,7 @@ static char *char_insert(char *p, char c, int undo) // insert the char c at 'p'
# endif # endif
} }
#else #else
file_modified++; modified_count++;
#endif /* ENABLE_FEATURE_VI_UNDO */ #endif /* ENABLE_FEATURE_VI_UNDO */
p++; p++;
} else if (c == 27) { // Is this an ESC? } else if (c == 27) { // Is this an ESC?
@ -1988,7 +1992,7 @@ static char *char_insert(char *p, char c, int undo) // insert the char c at 'p'
# endif # endif
} }
#else #else
file_modified++; modified_count++;
#endif /* ENABLE_FEATURE_VI_UNDO */ #endif /* ENABLE_FEATURE_VI_UNDO */
p += 1 + stupid_insert(p, c); // insert the char p += 1 + stupid_insert(p, c); // insert the char
#if ENABLE_FEATURE_VI_SETOPTS #if ENABLE_FEATURE_VI_SETOPTS
@ -2206,8 +2210,19 @@ static void showmatching(char *p)
#endif /* FEATURE_VI_SETOPTS */ #endif /* FEATURE_VI_SETOPTS */
#if ENABLE_FEATURE_VI_UNDO #if ENABLE_FEATURE_VI_UNDO
static void flush_undo_data(void)
{
struct undo_object *undo_entry;
while (undo_stack_tail) {
undo_entry = undo_stack_tail;
undo_stack_tail = undo_entry->prev;
free(undo_entry);
}
}
// Undo functions and hooks added by Jody Bruchon (jody@jodybruchon.com) // Undo functions and hooks added by Jody Bruchon (jody@jodybruchon.com)
static void undo_push(char *src, unsigned int length, unsigned char u_type) // Add to the undo stack static void undo_push(char *src, unsigned int length, uint8_t u_type) // Add to the undo stack
{ {
struct undo_object *undo_entry; struct undo_object *undo_entry;
@ -2275,10 +2290,19 @@ static void undo_push(char *src, unsigned int length, unsigned char u_type) // A
u_type = u_type & ~UNDO_QUEUED_FLAG; u_type = u_type & ~UNDO_QUEUED_FLAG;
#endif #endif
// Allocate a new undo object and use it as the stack tail // Allocate a new undo object
undo_entry = xzalloc(sizeof(*undo_entry)); if (u_type == UNDO_DEL || u_type == UNDO_DEL_CHAIN) {
undo_entry->prev = undo_stack_tail; // For UNDO_DEL objects, save deleted text
undo_stack_tail = undo_entry; if ((src + length) == end)
length--;
// If this deletion empties text[], strip the newline. When the buffer becomes
// zero-length, a newline is added back, which requires this to compensate.
undo_entry = xzalloc(offsetof(struct undo_object, undo_text) + length);
memcpy(undo_entry->undo_text, src, length);
} else {
undo_entry = xzalloc(sizeof(*undo_entry));
}
undo_entry->length = length;
#if ENABLE_FEATURE_VI_UNDO_QUEUE #if ENABLE_FEATURE_VI_UNDO_QUEUE
if ((u_type & UNDO_USE_SPOS) != 0) { if ((u_type & UNDO_USE_SPOS) != 0) {
undo_entry->start = undo_queue_spos - text; // use start position from queue undo_entry->start = undo_queue_spos - text; // use start position from queue
@ -2289,18 +2313,12 @@ static void undo_push(char *src, unsigned int length, unsigned char u_type) // A
#else #else
undo_entry->start = src - text; undo_entry->start = src - text;
#endif #endif
// For UNDO_DEL objects, copy the deleted text somewhere
undo_entry->u_type = u_type; undo_entry->u_type = u_type;
if (u_type == UNDO_DEL || u_type == UNDO_DEL_CHAIN) {
if ((src + length) == end) // Push it on undo stack
length--; undo_entry->prev = undo_stack_tail;
// If this deletion empties text[], strip the newline. When the buffer becomes undo_stack_tail = undo_entry;
// zero-length, a newline is added back, which requires this to compensate. modified_count++;
undo_entry->undo_text = xmalloc(length);
memcpy(undo_entry->undo_text, src, length);
}
undo_entry->length = length;
file_modified++;
} }
static void undo_pop(void) // Undo the last operation static void undo_pop(void) // Undo the last operation
@ -2326,9 +2344,8 @@ static void undo_pop(void) // Undo the last operation
u_start = text + undo_entry->start; u_start = text + undo_entry->start;
text_hole_make(u_start, undo_entry->length); text_hole_make(u_start, undo_entry->length);
memcpy(u_start, undo_entry->undo_text, undo_entry->length); memcpy(u_start, undo_entry->undo_text, undo_entry->length);
free(undo_entry->undo_text);
status_line("Undo [%d] %s %d chars at position %d", status_line("Undo [%d] %s %d chars at position %d",
file_modified, "restored", modified_count, "restored",
undo_entry->length, undo_entry->start undo_entry->length, undo_entry->start
); );
break; break;
@ -2339,7 +2356,7 @@ static void undo_pop(void) // Undo the last operation
u_end = u_start - 1 + undo_entry->length; u_end = u_start - 1 + undo_entry->length;
text_hole_delete(u_start, u_end, NO_UNDO); text_hole_delete(u_start, u_end, NO_UNDO);
status_line("Undo [%d] %s %d chars at position %d", status_line("Undo [%d] %s %d chars at position %d",
file_modified, "deleted", modified_count, "deleted",
undo_entry->length, undo_entry->start undo_entry->length, undo_entry->start
); );
break; break;
@ -2360,7 +2377,7 @@ static void undo_pop(void) // Undo the last operation
// Deallocate the undo object we just processed // Deallocate the undo object we just processed
undo_stack_tail = undo_entry->prev; undo_stack_tail = undo_entry->prev;
free(undo_entry); free(undo_entry);
file_modified--; modified_count--;
// For chained operations, continue popping all the way down the chain. // For chained operations, continue popping all the way down the chain.
if (repeat) { if (repeat) {
undo_pop(); // Follow the undo chain if one exists undo_pop(); // Follow the undo chain if one exists
@ -2452,13 +2469,13 @@ static char *text_hole_delete(char *p, char *q, int undo) // delete "p" through
break; break;
# endif # endif
} }
file_modified--; modified_count--;
#endif #endif
if (src < text || src > end) if (src < text || src > end)
goto thd0; goto thd0;
if (dest < text || dest >= end) if (dest < text || dest >= end)
goto thd0; goto thd0;
file_modified++; modified_count++;
if (src >= end) if (src >= end)
goto thd_atend; // just delete the end of the buffer goto thd_atend; // just delete the end of the buffer
memmove(dest, src, cnt); memmove(dest, src, cnt);
@ -2885,7 +2902,7 @@ static int file_insert(const char *fn, char *p, int update_ro_status)
status_line_bold("can't read '%s'", fn); status_line_bold("can't read '%s'", fn);
} }
// if (cnt >= size) // if (cnt >= size)
// file_modified++; // modified_count++;
close(fd); close(fd);
fi0: fi0:
#if ENABLE_FEATURE_VI_READONLY #if ENABLE_FEATURE_VI_READONLY
@ -2922,7 +2939,7 @@ static int file_write(char *fn, char *first, char *last)
ftruncate(fd, charcnt); ftruncate(fd, charcnt);
if (charcnt == cnt) { if (charcnt == cnt) {
// good write // good write
//file_modified = FALSE; //modified_count = FALSE;
} else { } else {
charcnt = 0; charcnt = 0;
} }
@ -3144,7 +3161,7 @@ static int format_edit_status(void)
int cur, percent, ret, trunc_at; int cur, percent, ret, trunc_at;
// file_modified is now a counter rather than a flag. this // modified_count is now a counter rather than a flag. this
// helps reduce the amount of line counting we need to do. // helps reduce the amount of line counting we need to do.
// (this will cause a mis-reporting of modified status // (this will cause a mis-reporting of modified status
// once every MAXINT editing operations.) // once every MAXINT editing operations.)
@ -3154,11 +3171,12 @@ static int format_edit_status(void)
// we're on, then we shouldn't have to do this count_lines() // we're on, then we shouldn't have to do this count_lines()
cur = count_lines(text, dot); cur = count_lines(text, dot);
// reduce counting -- the total lines can't have // count_lines() is expensive.
// changed if we haven't done any edits. // Call it only if something was changed since last time
if (file_modified != last_file_modified) { // we were here:
if (modified_count != last_modified_count) {
tot = cur + count_lines(dot, end - 1) - 1; tot = cur + count_lines(dot, end - 1) - 1;
last_file_modified = file_modified; last_modified_count = modified_count;
} }
// current line percent // current line percent
@ -3185,7 +3203,7 @@ static int format_edit_status(void)
#if ENABLE_FEATURE_VI_READONLY #if ENABLE_FEATURE_VI_READONLY
(readonly_mode ? " [Readonly]" : ""), (readonly_mode ? " [Readonly]" : ""),
#endif #endif
(file_modified ? " [Modified]" : ""), (modified_count ? " [Modified]" : ""),
cur, tot, percent); cur, tot, percent);
if (ret >= 0 && ret < trunc_at) if (ret >= 0 && ret < trunc_at)
@ -3814,7 +3832,7 @@ static void do_cmd(int c)
if (strncmp(p, "quit", cnt) == 0 if (strncmp(p, "quit", cnt) == 0
|| strncmp(p, "q!", cnt) == 0 // delete lines || strncmp(p, "q!", cnt) == 0 // delete lines
) { ) {
if (file_modified && p[1] != '!') { if (modified_count && p[1] != '!') {
status_line_bold("No write since last change (:%s! overrides)", p); status_line_bold("No write since last change (:%s! overrides)", p);
} else { } else {
editing = 0; editing = 0;
@ -3829,8 +3847,8 @@ static void do_cmd(int c)
if (cnt == -1) if (cnt == -1)
status_line_bold("Write error: %s", strerror(errno)); status_line_bold("Write error: %s", strerror(errno));
} else { } else {
file_modified = 0; modified_count = 0;
last_file_modified = -1; last_modified_count = -1;
status_line("'%s' %dL, %dC", current_filename, count_lines(text, end - 1), cnt); status_line("'%s' %dL, %dC", current_filename, count_lines(text, end - 1), cnt);
if (p[0] == 'x' || p[1] == 'q' || p[1] == 'n' if (p[0] == 'x' || p[1] == 'q' || p[1] == 'n'
|| p[0] == 'X' || p[1] == 'Q' || p[1] == 'N' || p[0] == 'X' || p[1] == 'Q' || p[1] == 'N'
@ -3963,7 +3981,7 @@ static void do_cmd(int c)
undo_push((dot - 1), 1, UNDO_INS_CHAIN); undo_push((dot - 1), 1, UNDO_INS_CHAIN);
#else #else
*dot++ = ' '; *dot++ = ' ';
file_modified++; modified_count++;
#endif #endif
while (isblank(*dot)) { // delete leading WS while (isblank(*dot)) { // delete leading WS
text_hole_delete(dot, dot, ALLOW_UNDO_CHAIN); text_hole_delete(dot, dot, ALLOW_UNDO_CHAIN);
@ -4035,7 +4053,7 @@ static void do_cmd(int c)
indicate_error(c); indicate_error(c);
break; break;
} }
if (file_modified) { if (modified_count) {
if (ENABLE_FEATURE_VI_READONLY && readonly_mode) { if (ENABLE_FEATURE_VI_READONLY && readonly_mode) {
status_line_bold("'%s' is read only", current_filename); status_line_bold("'%s' is read only", current_filename);
break; break;
@ -4172,7 +4190,7 @@ static void do_cmd(int c)
undo_push(dot, 1, UNDO_INS_CHAIN); undo_push(dot, 1, UNDO_INS_CHAIN);
#else #else
*dot = c1; *dot = c1;
file_modified++; modified_count++;
#endif #endif
} }
end_cmd_q(); // stop adding to q end_cmd_q(); // stop adding to q
@ -4226,10 +4244,10 @@ static void do_cmd(int c)
#else #else
if (islower(*dot)) { if (islower(*dot)) {
*dot = toupper(*dot); *dot = toupper(*dot);
file_modified++; modified_count++;
} else if (isupper(*dot)) { } else if (isupper(*dot)) {
*dot = tolower(*dot); *dot = tolower(*dot);
file_modified++; modified_count++;
} }
#endif #endif
dot_right(); dot_right();