#include "vim.h"
#define U_USE_MALLOC 1
static void u_unch_branch __ARGS((u_header_T *uhp));
static u_entry_T *u_get_headentry __ARGS((void));
static void u_getbot __ARGS((void));
static int undo_allowed __ARGS((void));
static int u_savecommon __ARGS((linenr_T, linenr_T, linenr_T));
static void u_doit __ARGS((int count));
static void u_undoredo __ARGS((int undo));
static void u_undo_end __ARGS((int did_undo, int absolute));
static void u_add_time __ARGS((char_u *buf, size_t buflen, time_t tt));
static void u_freeheader __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp));
static void u_freebranch __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp));
static void u_freeentries __ARGS((buf_T *buf, u_header_T *uhp, u_header_T **uhpp));
static void u_freeentry __ARGS((u_entry_T *, long));
#ifdef U_USE_MALLOC
# define U_FREE_LINE(ptr) vim_free(ptr)
# define U_ALLOC_LINE(size) lalloc((long_u)((size) + 1), FALSE)
#else
static void u_free_line __ARGS((char_u *ptr, int keep));
static char_u *u_alloc_line __ARGS((unsigned size));
# define U_FREE_LINE(ptr) u_free_line((ptr), FALSE)
# define U_ALLOC_LINE(size) u_alloc_line(size)
#endif
static char_u *u_save_line __ARGS((linenr_T));
static long u_newcount, u_oldcount;
static int undo_undoes = FALSE;
int
u_save_cursor()
{
return (u_save((linenr_T)(curwin->w_cursor.lnum - 1),
(linenr_T)(curwin->w_cursor.lnum + 1)));
}
int
u_save(top, bot)
linenr_T top, bot;
{
if (undo_off)
return OK;
if (top > curbuf->b_ml.ml_line_count ||
top >= bot || bot > curbuf->b_ml.ml_line_count + 1)
return FALSE;
if (top + 2 == bot)
u_saveline((linenr_T)(top + 1));
return (u_savecommon(top, bot, (linenr_T)0));
}
int
u_savesub(lnum)
linenr_T lnum;
{
if (undo_off)
return OK;
return (u_savecommon(lnum - 1, lnum + 1, lnum + 1));
}
int
u_inssub(lnum)
linenr_T lnum;
{
if (undo_off)
return OK;
return (u_savecommon(lnum - 1, lnum, lnum + 1));
}
int
u_savedel(lnum, nlines)
linenr_T lnum;
long nlines;
{
if (undo_off)
return OK;
return (u_savecommon(lnum - 1, lnum + nlines,
nlines == curbuf->b_ml.ml_line_count ? 2 : lnum));
}
static int
undo_allowed()
{
if (!curbuf->b_p_ma)
{
EMSG(_(e_modifiable));
return FALSE;
}
#ifdef HAVE_SANDBOX
if (sandbox != 0)
{
EMSG(_(e_sandbox));
return FALSE;
}
#endif
if (textlock != 0)
{
EMSG(_(e_secure));
return FALSE;
}
return TRUE;
}
static int
u_savecommon(top, bot, newbot)
linenr_T top, bot;
linenr_T newbot;
{
linenr_T lnum;
long i;
u_header_T *uhp;
u_header_T *old_curhead;
u_entry_T *uep;
u_entry_T *prev_uep;
long size;
if (!undo_allowed())
return FAIL;
#ifdef FEAT_NETBEANS_INTG
if (usingNetbeans)
{
if (netbeans_is_guarded(top, bot))
{
EMSG(_(e_guarded));
return FAIL;
}
if (curbuf->b_p_ro)
{
EMSG(_(e_nbreadonly));
return FAIL;
}
}
#endif
#ifdef FEAT_AUTOCMD
change_warning(0);
#endif
size = bot - top - 1;
if (curbuf->b_u_synced)
{
#ifdef FEAT_JUMPLIST
curbuf->b_new_change = TRUE;
#endif
if (p_ul >= 0)
{
uhp = (u_header_T *)U_ALLOC_LINE((unsigned)sizeof(u_header_T));
if (uhp == NULL)
goto nomem;
}
else
uhp = NULL;
old_curhead = curbuf->b_u_curhead;
if (old_curhead != NULL)
{
curbuf->b_u_newhead = old_curhead->uh_next;
curbuf->b_u_curhead = NULL;
}
while (curbuf->b_u_numhead > p_ul && curbuf->b_u_oldhead != NULL)
{
u_header_T *uhfree = curbuf->b_u_oldhead;
if (uhfree->uh_alt_next == NULL)
u_freeheader(curbuf, uhfree, &old_curhead);
else
{
while (uhfree->uh_alt_next != NULL)
uhfree = uhfree->uh_alt_next;
u_freebranch(curbuf, uhfree, &old_curhead);
}
}
if (uhp == NULL)
{
if (old_curhead != NULL)
u_freebranch(curbuf, old_curhead, NULL);
curbuf->b_u_synced = FALSE;
return OK;
}
uhp->uh_prev = NULL;
uhp->uh_next = curbuf->b_u_newhead;
uhp->uh_alt_next = old_curhead;
if (old_curhead != NULL)
{
old_curhead->uh_alt_prev = uhp;
if (curbuf->b_u_oldhead == old_curhead)
curbuf->b_u_oldhead = uhp;
}
uhp->uh_alt_prev = NULL;
if (curbuf->b_u_newhead != NULL)
curbuf->b_u_newhead->uh_prev = uhp;
uhp->uh_seq = ++curbuf->b_u_seq_last;
curbuf->b_u_seq_cur = uhp->uh_seq;
uhp->uh_time = time(NULL);
curbuf->b_u_seq_time = uhp->uh_time + 1;
uhp->uh_walk = 0;
uhp->uh_entry = NULL;
uhp->uh_getbot_entry = NULL;
uhp->uh_cursor = curwin->w_cursor;
#ifdef FEAT_VIRTUALEDIT
if (virtual_active() && curwin->w_cursor.coladd > 0)
uhp->uh_cursor_vcol = getviscol();
else
uhp->uh_cursor_vcol = -1;
#endif
uhp->uh_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
mch_memmove(uhp->uh_namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
#ifdef FEAT_VISUAL
uhp->uh_visual = curbuf->b_visual;
#endif
curbuf->b_u_newhead = uhp;
if (curbuf->b_u_oldhead == NULL)
curbuf->b_u_oldhead = uhp;
++curbuf->b_u_numhead;
}
else
{
if (p_ul < 0)
return OK;
if (size == 1)
{
uep = u_get_headentry();
prev_uep = NULL;
for (i = 0; i < 10; ++i)
{
if (uep == NULL)
break;
if ((curbuf->b_u_newhead->uh_getbot_entry != uep
? (uep->ue_top + uep->ue_size + 1
!= (uep->ue_bot == 0
? curbuf->b_ml.ml_line_count + 1
: uep->ue_bot))
: uep->ue_lcount != curbuf->b_ml.ml_line_count)
|| (uep->ue_size > 1
&& top >= uep->ue_top
&& top + 2 <= uep->ue_top + uep->ue_size + 1))
break;
if (uep->ue_size == 1 && uep->ue_top == top)
{
if (i > 0)
{
u_getbot();
curbuf->b_u_synced = FALSE;
prev_uep->ue_next = uep->ue_next;
uep->ue_next = curbuf->b_u_newhead->uh_entry;
curbuf->b_u_newhead->uh_entry = uep;
}
if (newbot != 0)
uep->ue_bot = newbot;
else if (bot > curbuf->b_ml.ml_line_count)
uep->ue_bot = 0;
else
{
uep->ue_lcount = curbuf->b_ml.ml_line_count;
curbuf->b_u_newhead->uh_getbot_entry = uep;
}
return OK;
}
prev_uep = uep;
uep = uep->ue_next;
}
}
u_getbot();
}
#if !defined(UNIX) && !defined(DJGPP) && !defined(WIN32) && !defined(__EMX__)
if (size >= 8000)
goto nomem;
#endif
uep = (u_entry_T *)U_ALLOC_LINE((unsigned)sizeof(u_entry_T));
if (uep == NULL)
goto nomem;
uep->ue_size = size;
uep->ue_top = top;
if (newbot != 0)
uep->ue_bot = newbot;
else if (bot > curbuf->b_ml.ml_line_count)
uep->ue_bot = 0;
else
{
uep->ue_lcount = curbuf->b_ml.ml_line_count;
curbuf->b_u_newhead->uh_getbot_entry = uep;
}
if (size > 0)
{
if ((uep->ue_array = (char_u **)U_ALLOC_LINE(
(unsigned)(sizeof(char_u *) * size))) == NULL)
{
u_freeentry(uep, 0L);
goto nomem;
}
for (i = 0, lnum = top + 1; i < size; ++i)
{
fast_breakcheck();
if (got_int)
{
u_freeentry(uep, i);
return FAIL;
}
if ((uep->ue_array[i] = u_save_line(lnum++)) == NULL)
{
u_freeentry(uep, i);
goto nomem;
}
}
}
else
uep->ue_array = NULL;
uep->ue_next = curbuf->b_u_newhead->uh_entry;
curbuf->b_u_newhead->uh_entry = uep;
curbuf->b_u_synced = FALSE;
undo_undoes = FALSE;
return OK;
nomem:
msg_silent = 0;
if (ask_yesno((char_u *)_("No undo possible; continue anyway"), TRUE)
== 'y')
{
undo_off = TRUE;
return OK;
}
do_outofmem_msg((long_u)0);
return FAIL;
}
void
u_undo(count)
int count;
{
if (curbuf->b_u_synced == FALSE)
{
u_sync(TRUE);
count = 1;
}
if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
undo_undoes = TRUE;
else
undo_undoes = !undo_undoes;
u_doit(count);
}
void
u_redo(count)
int count;
{
if (vim_strchr(p_cpo, CPO_UNDO) == NULL)
undo_undoes = FALSE;
u_doit(count);
}
static void
u_doit(startcount)
int startcount;
{
int count = startcount;
if (!undo_allowed())
return;
u_newcount = 0;
u_oldcount = 0;
if (curbuf->b_ml.ml_flags & ML_EMPTY)
u_oldcount = -1;
while (count--)
{
if (undo_undoes)
{
if (curbuf->b_u_curhead == NULL)
curbuf->b_u_curhead = curbuf->b_u_newhead;
else if (p_ul > 0)
curbuf->b_u_curhead = curbuf->b_u_curhead->uh_next;
if (curbuf->b_u_numhead == 0 || curbuf->b_u_curhead == NULL)
{
curbuf->b_u_curhead = curbuf->b_u_oldhead;
beep_flush();
if (count == startcount - 1)
{
MSG(_("Already at oldest change"));
return;
}
break;
}
u_undoredo(TRUE);
}
else
{
if (curbuf->b_u_curhead == NULL || p_ul <= 0)
{
beep_flush();
if (count == startcount - 1)
{
MSG(_("Already at newest change"));
return;
}
break;
}
u_undoredo(FALSE);
if (curbuf->b_u_curhead->uh_prev == NULL)
curbuf->b_u_newhead = curbuf->b_u_curhead;
curbuf->b_u_curhead = curbuf->b_u_curhead->uh_prev;
}
}
u_undo_end(undo_undoes, FALSE);
}
static int lastmark = 0;
void
undo_time(step, sec, absolute)
long step;
int sec;
int absolute;
{
long target;
long closest;
long closest_start;
long closest_seq = 0;
long val;
u_header_T *uhp;
u_header_T *last;
int mark;
int nomark;
int round;
int dosec = sec;
int above = FALSE;
int did_undo = TRUE;
if (curbuf->b_u_synced == FALSE)
u_sync(TRUE);
u_newcount = 0;
u_oldcount = 0;
if (curbuf->b_ml.ml_flags & ML_EMPTY)
u_oldcount = -1;
if (absolute)
{
target = step;
closest = -1;
}
else
{
if (sec)
target = (long)(curbuf->b_u_seq_time - starttime) + step;
else
target = curbuf->b_u_seq_cur + step;
if (step < 0)
{
if (target < 0)
target = 0;
closest = -1;
}
else
{
if (sec)
closest = (long)(time(NULL) - starttime + 1);
else
closest = curbuf->b_u_seq_last + 2;
if (target >= closest)
target = closest - 1;
}
}
closest_start = closest;
closest_seq = curbuf->b_u_seq_cur;
for (round = 1; round <= 2; ++round)
{
mark = ++lastmark;
nomark = ++lastmark;
if (curbuf->b_u_curhead == NULL)
uhp = curbuf->b_u_newhead;
else
uhp = curbuf->b_u_curhead;
while (uhp != NULL)
{
uhp->uh_walk = mark;
val = (long)(dosec ? (uhp->uh_time - starttime) : uhp->uh_seq);
if (round == 1)
{
if ((step < 0 ? uhp->uh_seq <= curbuf->b_u_seq_cur
: uhp->uh_seq > curbuf->b_u_seq_cur)
&& ((dosec && val == closest)
? (step < 0
? uhp->uh_seq < closest_seq
: uhp->uh_seq > closest_seq)
: closest == closest_start
|| (val > target
? (closest > target
? val - target <= closest - target
: val - target <= target - closest)
: (closest > target
? target - val <= closest - target
: target - val <= target - closest))))
{
closest = val;
closest_seq = uhp->uh_seq;
}
}
if (target == val && !dosec)
break;
if (uhp->uh_prev != NULL && uhp->uh_prev->uh_walk != nomark
&& uhp->uh_prev->uh_walk != mark)
uhp = uhp->uh_prev;
else if (uhp->uh_alt_next != NULL
&& uhp->uh_alt_next->uh_walk != nomark
&& uhp->uh_alt_next->uh_walk != mark)
uhp = uhp->uh_alt_next;
else if (uhp->uh_next != NULL && uhp->uh_alt_prev == NULL
&& uhp->uh_next->uh_walk != nomark
&& uhp->uh_next->uh_walk != mark)
{
if (uhp == curbuf->b_u_curhead)
uhp->uh_walk = nomark;
uhp = uhp->uh_next;
}
else
{
uhp->uh_walk = nomark;
if (uhp->uh_alt_prev != NULL)
uhp = uhp->uh_alt_prev;
else
uhp = uhp->uh_next;
}
}
if (uhp != NULL)
break;
if (absolute)
{
EMSGN(_("Undo number %ld not found"), step);
return;
}
if (closest == closest_start)
{
if (step < 0)
MSG(_("Already at oldest change"));
else
MSG(_("Already at newest change"));
return;
}
target = closest_seq;
dosec = FALSE;
if (step < 0)
above = TRUE;
}
if (uhp != NULL)
{
for (;;)
{
uhp = curbuf->b_u_curhead;
if (uhp == NULL)
uhp = curbuf->b_u_newhead;
else
uhp = uhp->uh_next;
if (uhp == NULL || uhp->uh_walk != mark
|| (uhp->uh_seq == target && !above))
break;
curbuf->b_u_curhead = uhp;
u_undoredo(TRUE);
uhp->uh_walk = nomark;
}
uhp = curbuf->b_u_curhead;
while (uhp != NULL)
{
last = uhp;
while (last->uh_alt_next != NULL
&& last->uh_alt_next->uh_walk == mark)
last = last->uh_alt_next;
if (last != uhp)
{
if (last->uh_alt_next != NULL)
last->uh_alt_next->uh_alt_prev = last->uh_alt_prev;
last->uh_alt_prev->uh_alt_next = last->uh_alt_next;
last->uh_alt_prev = NULL;
last->uh_alt_next = uhp;
uhp->uh_alt_prev = last;
uhp = last;
if (uhp->uh_next != NULL)
uhp->uh_next->uh_prev = uhp;
}
curbuf->b_u_curhead = uhp;
if (uhp->uh_walk != mark)
break;
if (uhp->uh_seq == target && above)
{
curbuf->b_u_seq_cur = target - 1;
break;
}
u_undoredo(FALSE);
if (uhp->uh_prev == NULL)
curbuf->b_u_newhead = uhp;
curbuf->b_u_curhead = uhp->uh_prev;
did_undo = FALSE;
if (uhp->uh_seq == target)
break;
uhp = uhp->uh_prev;
if (uhp == NULL || uhp->uh_walk != mark)
{
EMSG2(_(e_intern2), "undo_time()");
break;
}
}
}
u_undo_end(did_undo, absolute);
}
static void
u_undoredo(undo)
int undo;
{
char_u **newarray = NULL;
linenr_T oldsize;
linenr_T newsize;
linenr_T top, bot;
linenr_T lnum;
linenr_T newlnum = MAXLNUM;
long i;
u_entry_T *uep, *nuep;
u_entry_T *newlist = NULL;
int old_flags;
int new_flags;
pos_T namedm[NMARKS];
#ifdef FEAT_VISUAL
visualinfo_T visualinfo;
#endif
int empty_buffer;
u_header_T *curhead = curbuf->b_u_curhead;
old_flags = curhead->uh_flags;
new_flags = (curbuf->b_changed ? UH_CHANGED : 0) +
((curbuf->b_ml.ml_flags & ML_EMPTY) ? UH_EMPTYBUF : 0);
setpcmark();
mch_memmove(namedm, curbuf->b_namedm, sizeof(pos_T) * NMARKS);
#ifdef FEAT_VISUAL
visualinfo = curbuf->b_visual;
#endif
curbuf->b_op_start.lnum = curbuf->b_ml.ml_line_count;
curbuf->b_op_start.col = 0;
curbuf->b_op_end.lnum = 0;
curbuf->b_op_end.col = 0;
for (uep = curhead->uh_entry; uep != NULL; uep = nuep)
{
top = uep->ue_top;
bot = uep->ue_bot;
if (bot == 0)
bot = curbuf->b_ml.ml_line_count + 1;
if (top > curbuf->b_ml.ml_line_count || top >= bot
|| bot > curbuf->b_ml.ml_line_count + 1)
{
EMSG(_("E438: u_undo: line numbers wrong"));
changed();
return;
}
oldsize = bot - top - 1;
newsize = uep->ue_size;
if (top < newlnum)
{
lnum = curhead->uh_cursor.lnum;
if (lnum >= top && lnum <= top + newsize + 1)
{
curwin->w_cursor = curhead->uh_cursor;
newlnum = curwin->w_cursor.lnum - 1;
}
else
{
for (i = 0; i < newsize && i < oldsize; ++i)
if (STRCMP(uep->ue_array[i], ml_get(top + 1 + i)) != 0)
break;
if (i == newsize && newlnum == MAXLNUM && uep->ue_next == NULL)
{
newlnum = top;
curwin->w_cursor.lnum = newlnum + 1;
}
else if (i < newsize)
{
newlnum = top + i;
curwin->w_cursor.lnum = newlnum + 1;
}
}
}
empty_buffer = FALSE;
if (oldsize > 0)
{
if ((newarray = (char_u **)U_ALLOC_LINE(
(unsigned)(sizeof(char_u *) * oldsize))) == NULL)
{
do_outofmem_msg((long_u)(sizeof(char_u *) * oldsize));
while (uep != NULL)
{
nuep = uep->ue_next;
u_freeentry(uep, uep->ue_size);
uep = nuep;
}
break;
}
for (lnum = bot - 1, i = oldsize; --i >= 0; --lnum)
{
if ((newarray[i] = u_save_line(lnum)) == NULL)
do_outofmem_msg((long_u)0);
if (curbuf->b_ml.ml_line_count == 1)
empty_buffer = TRUE;
ml_delete(lnum, FALSE);
}
}
else
newarray = NULL;
if (newsize)
{
for (lnum = top, i = 0; i < newsize; ++i, ++lnum)
{
if (empty_buffer && lnum == 0)
ml_replace((linenr_T)1, uep->ue_array[i], TRUE);
else
ml_append(lnum, uep->ue_array[i], (colnr_T)0, FALSE);
U_FREE_LINE(uep->ue_array[i]);
}
U_FREE_LINE((char_u *)uep->ue_array);
}
if (oldsize != newsize)
{
mark_adjust(top + 1, top + oldsize, (long)MAXLNUM,
(long)newsize - (long)oldsize);
if (curbuf->b_op_start.lnum > top + oldsize)
curbuf->b_op_start.lnum += newsize - oldsize;
if (curbuf->b_op_end.lnum > top + oldsize)
curbuf->b_op_end.lnum += newsize - oldsize;
}
changed_lines(top + 1, 0, bot, newsize - oldsize);
if (top + 1 < curbuf->b_op_start.lnum)
curbuf->b_op_start.lnum = top + 1;
if (newsize == 0 && top + 1 > curbuf->b_op_end.lnum)
curbuf->b_op_end.lnum = top + 1;
else if (top + newsize > curbuf->b_op_end.lnum)
curbuf->b_op_end.lnum = top + newsize;
u_newcount += newsize;
u_oldcount += oldsize;
uep->ue_size = oldsize;
uep->ue_array = newarray;
uep->ue_bot = top + newsize + 1;
nuep = uep->ue_next;
uep->ue_next = newlist;
newlist = uep;
}
curhead->uh_entry = newlist;
curhead->uh_flags = new_flags;
if ((old_flags & UH_EMPTYBUF) && bufempty())
curbuf->b_ml.ml_flags |= ML_EMPTY;
if (old_flags & UH_CHANGED)
changed();
else
#ifdef FEAT_NETBEANS_INTG
if (!isNetbeansModified(curbuf))
#endif
unchanged(curbuf, FALSE);
for (i = 0; i < NMARKS; ++i)
if (curhead->uh_namedm[i].lnum != 0)
{
curbuf->b_namedm[i] = curhead->uh_namedm[i];
curhead->uh_namedm[i] = namedm[i];
}
#ifdef FEAT_VISUAL
if (curhead->uh_visual.vi_start.lnum != 0)
{
curbuf->b_visual = curhead->uh_visual;
curhead->uh_visual = visualinfo;
}
#endif
if (curhead->uh_cursor.lnum + 1 == curwin->w_cursor.lnum
&& curwin->w_cursor.lnum > 1)
--curwin->w_cursor.lnum;
if (curhead->uh_cursor.lnum == curwin->w_cursor.lnum)
{
curwin->w_cursor.col = curhead->uh_cursor.col;
#ifdef FEAT_VIRTUALEDIT
if (virtual_active() && curhead->uh_cursor_vcol >= 0)
coladvance((colnr_T)curhead->uh_cursor_vcol);
else
curwin->w_cursor.coladd = 0;
#endif
}
else if (curwin->w_cursor.lnum <= curbuf->b_ml.ml_line_count)
beginline(BL_SOL | BL_FIX);
else
{
curwin->w_cursor.col = 0;
#ifdef FEAT_VIRTUALEDIT
curwin->w_cursor.coladd = 0;
#endif
}
check_cursor();
curbuf->b_u_seq_cur = curhead->uh_seq;
if (undo)
--curbuf->b_u_seq_cur;
curbuf->b_u_seq_time = curhead->uh_time;
}
static void
u_undo_end(did_undo, absolute)
int did_undo;
int absolute;
{
char *msg;
u_header_T *uhp;
char_u msgbuf[80];
#ifdef FEAT_FOLDING
if ((fdo_flags & FDO_UNDO) && KeyTyped)
foldOpenCursor();
#endif
if (global_busy
|| !messaging())
return;
if (curbuf->b_ml.ml_flags & ML_EMPTY)
--u_newcount;
u_oldcount -= u_newcount;
if (u_oldcount == -1)
msg = N_("more line");
else if (u_oldcount < 0)
msg = N_("more lines");
else if (u_oldcount == 1)
msg = N_("line less");
else if (u_oldcount > 1)
msg = N_("fewer lines");
else
{
u_oldcount = u_newcount;
if (u_newcount == 1)
msg = N_("change");
else
msg = N_("changes");
}
if (curbuf->b_u_curhead != NULL)
{
if (absolute && curbuf->b_u_curhead->uh_next != NULL)
{
uhp = curbuf->b_u_curhead->uh_next;
did_undo = FALSE;
}
else if (did_undo)
uhp = curbuf->b_u_curhead;
else
uhp = curbuf->b_u_curhead->uh_next;
}
else
uhp = curbuf->b_u_newhead;
if (uhp == NULL)
*msgbuf = NUL;
else
u_add_time(msgbuf, sizeof(msgbuf), uhp->uh_time);
smsg((char_u *)_("%ld %s; %s #%ld %s"),
u_oldcount < 0 ? -u_oldcount : u_oldcount,
_(msg),
did_undo ? _("before") : _("after"),
uhp == NULL ? 0L : uhp->uh_seq,
msgbuf);
}
void
u_sync(force)
int force;
{
if (curbuf->b_u_synced || (!force && no_u_sync > 0))
return;
#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
if (im_is_preediting())
return;
#endif
if (p_ul < 0)
curbuf->b_u_synced = TRUE;
else
{
u_getbot();
curbuf->b_u_curhead = NULL;
}
}
void
ex_undolist(eap)
exarg_T *eap;
{
garray_T ga;
u_header_T *uhp;
int mark;
int nomark;
int changes = 1;
int i;
mark = ++lastmark;
nomark = ++lastmark;
ga_init2(&ga, (int)sizeof(char *), 20);
uhp = curbuf->b_u_oldhead;
while (uhp != NULL)
{
if (uhp->uh_prev == NULL && uhp->uh_walk != nomark
&& uhp->uh_walk != mark)
{
if (ga_grow(&ga, 1) == FAIL)
break;
vim_snprintf((char *)IObuff, IOSIZE, "%6ld %7ld ",
uhp->uh_seq, changes);
u_add_time(IObuff + STRLEN(IObuff), IOSIZE - STRLEN(IObuff),
uhp->uh_time);
((char_u **)(ga.ga_data))[ga.ga_len++] = vim_strsave(IObuff);
}
uhp->uh_walk = mark;
if (uhp->uh_prev != NULL && uhp->uh_prev->uh_walk != nomark
&& uhp->uh_prev->uh_walk != mark)
{
uhp = uhp->uh_prev;
++changes;
}
else if (uhp->uh_alt_next != NULL
&& uhp->uh_alt_next->uh_walk != nomark
&& uhp->uh_alt_next->uh_walk != mark)
uhp = uhp->uh_alt_next;
else if (uhp->uh_next != NULL && uhp->uh_alt_prev == NULL
&& uhp->uh_next->uh_walk != nomark
&& uhp->uh_next->uh_walk != mark)
{
uhp = uhp->uh_next;
--changes;
}
else
{
uhp->uh_walk = nomark;
if (uhp->uh_alt_prev != NULL)
uhp = uhp->uh_alt_prev;
else
{
uhp = uhp->uh_next;
--changes;
}
}
}
if (ga.ga_len == 0)
MSG(_("Nothing to undo"));
else
{
sort_strings((char_u **)ga.ga_data, ga.ga_len);
msg_start();
msg_puts_attr((char_u *)_("number changes time"), hl_attr(HLF_T));
for (i = 0; i < ga.ga_len && !got_int; ++i)
{
msg_putchar('\n');
if (got_int)
break;
msg_puts(((char_u **)ga.ga_data)[i]);
}
msg_end();
ga_clear_strings(&ga);
}
}
static void
u_add_time(buf, buflen, tt)
char_u *buf;
size_t buflen;
time_t tt;
{
#ifdef HAVE_STRFTIME
struct tm *curtime;
if (time(NULL) - tt >= 100)
{
curtime = localtime(&tt);
(void)strftime((char *)buf, buflen, "%H:%M:%S", curtime);
}
else
#endif
vim_snprintf((char *)buf, buflen, _("%ld seconds ago"),
(long)(time(NULL) - tt));
}
void
ex_undojoin(eap)
exarg_T *eap;
{
if (curbuf->b_u_newhead == NULL)
return;
if (curbuf->b_u_curhead != NULL)
{
EMSG(_("E790: undojoin is not allowed after undo"));
return;
}
if (!curbuf->b_u_synced)
return;
if (p_ul < 0)
return;
else
{
curbuf->b_u_curhead = curbuf->b_u_newhead;
curbuf->b_u_synced = FALSE;
}
}
void
u_unchanged(buf)
buf_T *buf;
{
u_unch_branch(buf->b_u_oldhead);
buf->b_did_warn = FALSE;
}
static void
u_unch_branch(uhp)
u_header_T *uhp;
{
u_header_T *uh;
for (uh = uhp; uh != NULL; uh = uh->uh_prev)
{
uh->uh_flags |= UH_CHANGED;
if (uh->uh_alt_next != NULL)
u_unch_branch(uh->uh_alt_next);
}
}
static u_entry_T *
u_get_headentry()
{
if (curbuf->b_u_newhead == NULL || curbuf->b_u_newhead->uh_entry == NULL)
{
EMSG(_("E439: undo list corrupt"));
return NULL;
}
return curbuf->b_u_newhead->uh_entry;
}
static void
u_getbot()
{
u_entry_T *uep;
linenr_T extra;
uep = u_get_headentry();
if (uep == NULL)
return;
uep = curbuf->b_u_newhead->uh_getbot_entry;
if (uep != NULL)
{
extra = curbuf->b_ml.ml_line_count - uep->ue_lcount;
uep->ue_bot = uep->ue_top + uep->ue_size + 1 + extra;
if (uep->ue_bot < 1 || uep->ue_bot > curbuf->b_ml.ml_line_count)
{
EMSG(_("E440: undo line missing"));
uep->ue_bot = uep->ue_top + 1;
}
curbuf->b_u_newhead->uh_getbot_entry = NULL;
}
curbuf->b_u_synced = TRUE;
}
static void
u_freeheader(buf, uhp, uhpp)
buf_T *buf;
u_header_T *uhp;
u_header_T **uhpp;
{
if (uhp->uh_alt_next != NULL)
u_freebranch(buf, uhp->uh_alt_next, uhpp);
if (uhp->uh_alt_prev != NULL)
uhp->uh_alt_prev->uh_alt_next = NULL;
if (uhp->uh_next == NULL)
buf->b_u_oldhead = uhp->uh_prev;
else
uhp->uh_next->uh_prev = uhp->uh_prev;
if (uhp->uh_prev == NULL)
buf->b_u_newhead = uhp->uh_next;
else
uhp->uh_prev->uh_next = uhp->uh_next;
u_freeentries(buf, uhp, uhpp);
}
static void
u_freebranch(buf, uhp, uhpp)
buf_T *buf;
u_header_T *uhp;
u_header_T **uhpp;
{
u_header_T *tofree, *next;
if (uhp->uh_alt_prev != NULL)
uhp->uh_alt_prev->uh_alt_next = NULL;
next = uhp;
while (next != NULL)
{
tofree = next;
if (tofree->uh_alt_next != NULL)
u_freebranch(buf, tofree->uh_alt_next, uhpp);
next = tofree->uh_prev;
u_freeentries(buf, tofree, uhpp);
}
}
static void
u_freeentries(buf, uhp, uhpp)
buf_T *buf;
u_header_T *uhp;
u_header_T **uhpp;
{
u_entry_T *uep, *nuep;
if (buf->b_u_curhead == uhp)
buf->b_u_curhead = NULL;
if (uhpp != NULL && uhp == *uhpp)
*uhpp = NULL;
for (uep = uhp->uh_entry; uep != NULL; uep = nuep)
{
nuep = uep->ue_next;
u_freeentry(uep, uep->ue_size);
}
U_FREE_LINE((char_u *)uhp);
--buf->b_u_numhead;
}
static void
u_freeentry(uep, n)
u_entry_T *uep;
long n;
{
while (n > 0)
U_FREE_LINE(uep->ue_array[--n]);
U_FREE_LINE((char_u *)uep->ue_array);
U_FREE_LINE((char_u *)uep);
}
void
u_clearall(buf)
buf_T *buf;
{
buf->b_u_newhead = buf->b_u_oldhead = buf->b_u_curhead = NULL;
buf->b_u_synced = TRUE;
buf->b_u_numhead = 0;
buf->b_u_line_ptr = NULL;
buf->b_u_line_lnum = 0;
}
void
u_saveline(lnum)
linenr_T lnum;
{
if (lnum == curbuf->b_u_line_lnum)
return;
if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
return;
u_clearline();
curbuf->b_u_line_lnum = lnum;
if (curwin->w_cursor.lnum == lnum)
curbuf->b_u_line_colnr = curwin->w_cursor.col;
else
curbuf->b_u_line_colnr = 0;
if ((curbuf->b_u_line_ptr = u_save_line(lnum)) == NULL)
do_outofmem_msg((long_u)0);
}
void
u_clearline()
{
if (curbuf->b_u_line_ptr != NULL)
{
U_FREE_LINE(curbuf->b_u_line_ptr);
curbuf->b_u_line_ptr = NULL;
curbuf->b_u_line_lnum = 0;
}
}
void
u_undoline()
{
colnr_T t;
char_u *oldp;
if (undo_off)
return;
if (curbuf->b_u_line_ptr == NULL ||
curbuf->b_u_line_lnum > curbuf->b_ml.ml_line_count)
{
beep_flush();
return;
}
if (u_savecommon(curbuf->b_u_line_lnum - 1,
curbuf->b_u_line_lnum + 1, (linenr_T)0) == FAIL)
return;
oldp = u_save_line(curbuf->b_u_line_lnum);
if (oldp == NULL)
{
do_outofmem_msg((long_u)0);
return;
}
ml_replace(curbuf->b_u_line_lnum, curbuf->b_u_line_ptr, TRUE);
changed_bytes(curbuf->b_u_line_lnum, 0);
U_FREE_LINE(curbuf->b_u_line_ptr);
curbuf->b_u_line_ptr = oldp;
t = curbuf->b_u_line_colnr;
if (curwin->w_cursor.lnum == curbuf->b_u_line_lnum)
curbuf->b_u_line_colnr = curwin->w_cursor.col;
if (Unix2003_compat) {
t = 0;
}
curwin->w_cursor.col = t;
curwin->w_cursor.lnum = curbuf->b_u_line_lnum;
}
#ifdef U_USE_MALLOC
void
u_blockfree(buf)
buf_T *buf;
{
while (buf->b_u_oldhead != NULL)
u_freeheader(buf, buf->b_u_oldhead, NULL);
U_FREE_LINE(buf->b_u_line_ptr);
}
#else
#define MEMBLOCKSIZE 2044
#ifdef ALIGN_LONG
# define M_OFFSET (sizeof(long_u))
#else
# define M_OFFSET (sizeof(short_u))
#endif
static char_u *u_blockalloc __ARGS((long_u));
static char_u *
u_blockalloc(size)
long_u size;
{
mblock_T *p;
mblock_T *mp, *next;
p = (mblock_T *)lalloc(size + sizeof(mblock_T), FALSE);
if (p != NULL)
{
for (mp = &curbuf->b_block_head;
(next = mp->mb_next) != NULL && next < p;
mp = next)
;
p->mb_next = next;
p->mb_size = size;
p->mb_maxsize = 0;
mp->mb_next = p;
p->mb_info.m_next = NULL;
p->mb_info.m_size = 0;
curbuf->b_mb_current = p;
curbuf->b_m_search = NULL;
p++;
}
return (char_u *)p;
}
void
u_blockfree(buf)
buf_T *buf;
{
mblock_T *p, *np;
for (p = buf->b_block_head.mb_next; p != NULL; p = np)
{
np = p->mb_next;
vim_free(p);
}
buf->b_block_head.mb_next = NULL;
buf->b_m_search = NULL;
buf->b_mb_current = NULL;
}
static void
u_free_line(ptr, keep)
char_u *ptr;
int keep;
{
minfo_T *next;
minfo_T *prev, *curr;
minfo_T *mp;
mblock_T *nextb;
mblock_T *prevb;
long_u maxsize;
if (ptr == NULL || ptr == IObuff)
return;
mp = (minfo_T *)(ptr - M_OFFSET);
if (curbuf->b_mb_current == NULL || mp < (minfo_T *)curbuf->b_mb_current)
{
curbuf->b_mb_current = curbuf->b_block_head.mb_next;
curbuf->b_m_search = NULL;
}
if ((nextb = curbuf->b_mb_current->mb_next) != NULL
&& (minfo_T *)nextb < mp)
{
curbuf->b_mb_current = nextb;
curbuf->b_m_search = NULL;
}
while ((nextb = curbuf->b_mb_current->mb_next) != NULL
&& (minfo_T *)nextb < mp)
curbuf->b_mb_current = nextb;
curr = NULL;
if (curbuf->b_m_search == NULL || mp < (curbuf->b_m_search->m_next))
next = &(curbuf->b_mb_current->mb_info);
else
next = curbuf->b_m_search;
#ifdef SLOW_BUT_EASY_TO_READ
do
{
prev = curr;
curr = next;
next = next->m_next;
}
while (mp > next && next != NULL);
#else
do
{
prev = next->m_next;
if (prev == NULL || mp <= prev)
{
prev = curr;
curr = next;
next = next->m_next;
break;
}
curr = prev->m_next;
if (curr == NULL || mp <= curr)
{
prev = next;
curr = prev->m_next;
next = curr->m_next;
break;
}
next = curr->m_next;
}
while (mp > next && next != NULL);
#endif
if ((char_u *)mp + mp->m_size == (char_u *)next)
{
mp->m_size += next->m_size;
mp->m_next = next->m_next;
}
else
mp->m_next = next;
maxsize = mp->m_size;
if (prev != NULL && (char_u *)curr + curr->m_size == (char_u *)mp)
{
curr->m_size += mp->m_size;
maxsize = curr->m_size;
curr->m_next = mp->m_next;
curbuf->b_m_search = prev;
}
else
{
curr->m_next = mp;
curbuf->b_m_search = curr;
}
if (!keep && curbuf->b_mb_current->mb_size
== curbuf->b_mb_current->mb_info.m_next->m_size)
{
prevb = &curbuf->b_block_head;
for (nextb = prevb->mb_next; nextb != curbuf->b_mb_current;
nextb = nextb->mb_next)
prevb = nextb;
prevb->mb_next = nextb->mb_next;
vim_free(nextb);
curbuf->b_mb_current = NULL;
curbuf->b_m_search = NULL;
}
else if (curbuf->b_mb_current->mb_maxsize < maxsize)
curbuf->b_mb_current->mb_maxsize = maxsize;
}
static char_u *
u_alloc_line(size)
unsigned size;
{
minfo_T *mp, *mprev, *mp2;
mblock_T *mbp;
int size_align;
size += M_OFFSET + 1;
if (size < sizeof(minfo_T) + 1)
size = sizeof(minfo_T) + 1;
size_align = (size + ALIGN_MASK) & ~ALIGN_MASK;
if (curbuf->b_mb_current == NULL || curbuf->b_m_search == NULL)
{
curbuf->b_mb_current = &curbuf->b_block_head;
curbuf->b_m_search = &(curbuf->b_block_head.mb_info);
}
mbp = curbuf->b_mb_current;
while (mbp->mb_maxsize < size_align)
{
if (mbp->mb_next != NULL)
mbp = mbp->mb_next;
else
mbp = &curbuf->b_block_head;
if (mbp == curbuf->b_mb_current)
{
int n = (size_align > (MEMBLOCKSIZE / 4)
? size_align : MEMBLOCKSIZE);
mp = (minfo_T *)u_blockalloc((long_u)n);
if (mp == NULL)
return (NULL);
mp->m_size = n;
u_free_line((char_u *)mp + M_OFFSET, TRUE);
mbp = curbuf->b_mb_current;
break;
}
}
if (mbp != curbuf->b_mb_current)
curbuf->b_m_search = &(mbp->mb_info);
mprev = curbuf->b_m_search;
mp = curbuf->b_m_search->m_next;
for (;;)
{
if (mp == NULL)
mp = &(mbp->mb_info);
if (mp->m_size >= size)
break;
if (mp == curbuf->b_m_search)
{
EMSG2(_(e_intern2), "u_alloc_line()");
return NULL;
}
mprev = mp;
mp = mp->m_next;
}
if (mp->m_size >= mbp->mb_maxsize)
mbp->mb_maxsize = 0;
if ((long)mp->m_size - size_align >= (long)(sizeof(minfo_T) + 1))
{
mp2 = (minfo_T *)((char_u *)mp + size_align);
mp2->m_size = mp->m_size - size_align;
mp2->m_next = mp->m_next;
mprev->m_next = mp2;
mp->m_size = size_align;
}
else
{
mprev->m_next = mp->m_next;
}
curbuf->b_m_search = mprev;
curbuf->b_mb_current = mbp;
if (mbp->mb_maxsize == 0)
for (mp2 = &(mbp->mb_info); mp2 != NULL; mp2 = mp2->m_next)
if (mbp->mb_maxsize < mp2->m_size)
mbp->mb_maxsize = mp2->m_size;
mp = (minfo_T *)((char_u *)mp + M_OFFSET);
*(char_u *)mp = NUL;
return ((char_u *)mp);
}
#endif
static char_u *
u_save_line(lnum)
linenr_T lnum;
{
char_u *src;
char_u *dst;
unsigned len;
src = ml_get(lnum);
len = (unsigned)STRLEN(src);
if ((dst = U_ALLOC_LINE(len)) != NULL)
mch_memmove(dst, src, (size_t)(len + 1));
return (dst);
}
int
bufIsChanged(buf)
buf_T *buf;
{
return
#ifdef FEAT_QUICKFIX
!bt_dontwrite(buf) &&
#endif
(buf->b_changed || file_ff_differs(buf));
}
int
curbufIsChanged()
{
return
#ifdef FEAT_QUICKFIX
!bt_dontwrite(curbuf) &&
#endif
(curbuf->b_changed || file_ff_differs(curbuf));
}