1753 lines · 41.2 KB · C
RawDownload
/* vi:set ts=8 sts=4 sw=4: * * VIM - Vi IMproved	by Bram Moolenaar * * Do ":help uganda"  in Vim to read copying and usage conditions. * Do ":help credits" in Vim to see a list of people who contributed. * See README.txt for an overview of the Vim source code. */ /* * mark.c: functions for setting marks and jumping to them */ #include "vim.h" /* * This file contains routines to maintain and manipulate marks. */ /* * If a named file mark's lnum is non-zero, it is valid. * If a named file mark's fnum is non-zero, it is for an existing buffer, * otherwise it is from .viminfo and namedfm[n].fname is the file name. * There are marks 'A - 'Z (set by user) and '0 to '9 (set when writing * viminfo). */#define EXTRA_MARKS 10					/* marks 0-9 */static xfmark_T namedfm[NMARKS + EXTRA_MARKS];		/* marks with file nr */ static void fname2fnum __ARGS((xfmark_T *fm));static void fmarks_check_one __ARGS((xfmark_T *fm, char_u *name, buf_T *buf));static char_u *mark_line __ARGS((pos_T *mp, int lead_len));static void show_one_mark __ARGS((int, char_u *, pos_T *, char_u *, int current));#ifdef FEAT_JUMPLISTstatic void cleanup_jumplist __ARGS((void));#endif#ifdef FEAT_VIMINFOstatic void write_one_filemark __ARGS((FILE *fp, xfmark_T *fm, int c1, int c2));#endif /* * Set named mark "c" at current cursor position. * Returns OK on success, FAIL if bad name given. */    intsetmark(c)    int		c;{    return setmark_pos(c, &curwin->w_cursor, curbuf->b_fnum);} /* * Set named mark "c" to position "pos". * When "c" is upper case use file "fnum". * Returns OK on success, FAIL if bad name given. */    intsetmark_pos(c, pos, fnum)    int		c;    pos_T	*pos;    int		fnum;{    int		i;     /* Check for a special key (may cause islower() to crash). */    if (c < 0)	return FAIL;     if (c == '\'' || c == '`')    {	if (pos == &curwin->w_cursor)	{	    setpcmark();	    /* keep it even when the cursor doesn't move */	    curwin->w_prev_pcmark = curwin->w_pcmark;	}	else	    curwin->w_pcmark = *pos;	return OK;    }     /* Allow setting '[ and '] for an autocommand that simulates reading a     * file. */    if (c == '[')    {	curbuf->b_op_start = *pos;	return OK;    }    if (c == ']')    {	curbuf->b_op_end = *pos;	return OK;    } #ifndef EBCDIC    if (c > 'z')	    /* some islower() and isupper() cannot handle				characters above 127 */	return FAIL;#endif    if (islower(c))    {	i = c - 'a';	curbuf->b_namedm[i] = *pos;	return OK;    }    if (isupper(c))    {	i = c - 'A';	namedfm[i].fmark.mark = *pos;	namedfm[i].fmark.fnum = fnum;	vim_free(namedfm[i].fname);	namedfm[i].fname = NULL;	return OK;    }    return FAIL;} /* * Set the previous context mark to the current position and add it to the * jump list. */    voidsetpcmark(){#ifdef FEAT_JUMPLIST    int		i;    xfmark_T	*fm;#endif#ifdef JUMPLIST_ROTATE    xfmark_T	tempmark;#endif     /* for :global the mark is set only once */    if (global_busy || listcmd_busy || cmdmod.keepjumps)	return;     curwin->w_prev_pcmark = curwin->w_pcmark;    curwin->w_pcmark = curwin->w_cursor; #ifdef FEAT_JUMPLIST# ifdef JUMPLIST_ROTATE    /*     * If last used entry is not at the top, put it at the top by rotating     * the stack until it is (the newer entries will be at the bottom).     * Keep one entry (the last used one) at the top.     */    if (curwin->w_jumplistidx < curwin->w_jumplistlen)	++curwin->w_jumplistidx;    while (curwin->w_jumplistidx < curwin->w_jumplistlen)    {	tempmark = curwin->w_jumplist[curwin->w_jumplistlen - 1];	for (i = curwin->w_jumplistlen - 1; i > 0; --i)	    curwin->w_jumplist[i] = curwin->w_jumplist[i - 1];	curwin->w_jumplist[0] = tempmark;	++curwin->w_jumplistidx;    }# endif     /* If jumplist is full: remove oldest entry */    if (++curwin->w_jumplistlen > JUMPLISTSIZE)    {	curwin->w_jumplistlen = JUMPLISTSIZE;	vim_free(curwin->w_jumplist[0].fname);	for (i = 1; i < JUMPLISTSIZE; ++i)	    curwin->w_jumplist[i - 1] = curwin->w_jumplist[i];    }    curwin->w_jumplistidx = curwin->w_jumplistlen;    fm = &curwin->w_jumplist[curwin->w_jumplistlen - 1];     fm->fmark.mark = curwin->w_pcmark;    fm->fmark.fnum = curbuf->b_fnum;    fm->fname = NULL;#endif} /* * To change context, call setpcmark(), then move the current position to * where ever, then call checkpcmark().  This ensures that the previous * context will only be changed if the cursor moved to a different line. * If pcmark was deleted (with "dG") the previous mark is restored. */    voidcheckpcmark(){    if (curwin->w_prev_pcmark.lnum != 0	    && (equalpos(curwin->w_pcmark, curwin->w_cursor)		|| curwin->w_pcmark.lnum == 0))    {	curwin->w_pcmark = curwin->w_prev_pcmark;	curwin->w_prev_pcmark.lnum = 0;		/* Show it has been checked */    }} #if defined(FEAT_JUMPLIST) || defined(PROTO)/* * move "count" positions in the jump list (count may be negative) */    pos_T *movemark(count)    int count;{    pos_T	*pos;    xfmark_T	*jmp;     cleanup_jumplist();     if (curwin->w_jumplistlen == 0)	    /* nothing to jump to */	return (pos_T *)NULL;     for (;;)    {	if (curwin->w_jumplistidx + count < 0		|| curwin->w_jumplistidx + count >= curwin->w_jumplistlen)	    return (pos_T *)NULL; 	/*	 * if first CTRL-O or CTRL-I command after a jump, add cursor position	 * to list.  Careful: If there are duplicates (CTRL-O immediately after	 * starting Vim on a file), another entry may have been removed.	 */	if (curwin->w_jumplistidx == curwin->w_jumplistlen)	{	    setpcmark();	    --curwin->w_jumplistidx;	/* skip the new entry */	    if (curwin->w_jumplistidx + count < 0)		return (pos_T *)NULL;	} 	curwin->w_jumplistidx += count; 	jmp = curwin->w_jumplist + curwin->w_jumplistidx;	if (jmp->fmark.fnum == 0)	    fname2fnum(jmp);	if (jmp->fmark.fnum != curbuf->b_fnum)	{	    /* jump to other file */	    if (buflist_findnr(jmp->fmark.fnum) == NULL)	    {					     /* Skip this one .. */		count += count < 0 ? -1 : 1;		continue;	    }	    if (buflist_getfile(jmp->fmark.fnum, jmp->fmark.mark.lnum,							    0, FALSE) == FAIL)		return (pos_T *)NULL;	    /* Set lnum again, autocommands my have changed it */	    curwin->w_cursor = jmp->fmark.mark;	    pos = (pos_T *)-1;	}	else	    pos = &(jmp->fmark.mark);	return pos;    }} /* * Move "count" positions in the changelist (count may be negative). */    pos_T *movechangelist(count)    int		count;{    int		n;     if (curbuf->b_changelistlen == 0)	    /* nothing to jump to */	return (pos_T *)NULL;     n = curwin->w_changelistidx;    if (n + count < 0)    {	if (n == 0)	    return (pos_T *)NULL;	n = 0;    }    else if (n + count >= curbuf->b_changelistlen)    {	if (n == curbuf->b_changelistlen - 1)	    return (pos_T *)NULL;	n = curbuf->b_changelistlen - 1;    }    else	n += count;    curwin->w_changelistidx = n;    return curbuf->b_changelist + n;}#endif /* * Find mark "c". * If "changefile" is TRUE it's allowed to edit another file for '0, 'A, etc. * If "fnum" is not NULL store the fnum there for '0, 'A etc., don't edit * another file. * Returns: * - pointer to pos_T if found.  lnum is 0 when mark not set, -1 when mark is *   in another file which can't be gotten. (caller needs to check lnum!) * - NULL if there is no mark called 'c'. * - -1 if mark is in other file and jumped there (only if changefile is TRUE) */    pos_T *getmark(c, changefile)    int		c;    int		changefile;{    return getmark_fnum(c, changefile, NULL);}     pos_T *getmark_fnum(c, changefile, fnum)    int		c;    int		changefile;    int		*fnum;{    pos_T		*posp;#ifdef FEAT_VISUAL    pos_T		*startp, *endp;#endif    static pos_T	pos_copy;     posp = NULL;     /* Check for special key, can't be a mark name and might cause islower()     * to crash. */    if (c < 0)	return posp;#ifndef EBCDIC    if (c > '~')			/* check for islower()/isupper() */	;    else#endif	if (c == '\'' || c == '`')	/* previous context mark */    {	pos_copy = curwin->w_pcmark;	/* need to make a copy because */	posp = &pos_copy;		/*   w_pcmark may be changed soon */    }    else if (c == '"')			/* to pos when leaving buffer */	posp = &(curbuf->b_last_cursor);    else if (c == '^')			/* to where Insert mode stopped */	posp = &(curbuf->b_last_insert);    else if (c == '.')			/* to where last change was made */	posp = &(curbuf->b_last_change);    else if (c == '[')			/* to start of previous operator */	posp = &(curbuf->b_op_start);    else if (c == ']')			/* to end of previous operator */	posp = &(curbuf->b_op_end);    else if (c == '{' || c == '}')	/* to previous/next paragraph */    {	pos_T	pos;	oparg_T	oa;	int	slcb = listcmd_busy; 	pos = curwin->w_cursor;	listcmd_busy = TRUE;	    /* avoid that '' is changed */	if (findpar(&oa.inclusive,			       c == '}' ? FORWARD : BACKWARD, 1L, NUL, FALSE))	{	    pos_copy = curwin->w_cursor;	    posp = &pos_copy;	}	curwin->w_cursor = pos;	listcmd_busy = slcb;    }    else if (c == '(' || c == ')')	/* to previous/next sentence */    {	pos_T	pos;	int	slcb = listcmd_busy; 	pos = curwin->w_cursor;	listcmd_busy = TRUE;	    /* avoid that '' is changed */	if (findsent(c == ')' ? FORWARD : BACKWARD, 1L))	{	    pos_copy = curwin->w_cursor;	    posp = &pos_copy;	}	curwin->w_cursor = pos;	listcmd_busy = slcb;    }#ifdef FEAT_VISUAL    else if (c == '<' || c == '>')	/* start/end of visual area */    {	startp = &curbuf->b_visual.vi_start;	endp = &curbuf->b_visual.vi_end;	if ((c == '<') == lt(*startp, *endp))	    posp = startp;	else	    posp = endp;	/*	 * For Visual line mode, set mark at begin or end of line	 */	if (curbuf->b_visual.vi_mode == 'V')	{	    pos_copy = *posp;	    posp = &pos_copy;	    if (c == '<')		pos_copy.col = 0;	    else		pos_copy.col = MAXCOL;#ifdef FEAT_VIRTUALEDIT	    pos_copy.coladd = 0;#endif	}    }#endif    else if (ASCII_ISLOWER(c))		/* normal named mark */    {	posp = &(curbuf->b_namedm[c - 'a']);    }    else if (ASCII_ISUPPER(c) || VIM_ISDIGIT(c))	/* named file mark */    {	if (VIM_ISDIGIT(c))	    c = c - '0' + NMARKS;	else	    c -= 'A';	posp = &(namedfm[c].fmark.mark); 	if (namedfm[c].fmark.fnum == 0)	    fname2fnum(&namedfm[c]); 	if (fnum != NULL)	    *fnum = namedfm[c].fmark.fnum;	else if (namedfm[c].fmark.fnum != curbuf->b_fnum)	{	    /* mark is in another file */	    posp = &pos_copy; 	    if (namedfm[c].fmark.mark.lnum != 0				       && changefile && namedfm[c].fmark.fnum)	    {		if (buflist_getfile(namedfm[c].fmark.fnum,				      (linenr_T)1, GETF_SETMARK, FALSE) == OK)		{		    /* Set the lnum now, autocommands could have changed it */		    curwin->w_cursor = namedfm[c].fmark.mark;		    return (pos_T *)-1;		}		pos_copy.lnum = -1;	/* can't get file */	    }	    else		pos_copy.lnum = 0;	/* mark exists, but is not valid in					   current buffer */	}    }     return posp;} /* * Search for the next named mark in the current file. * * Returns pointer to pos_T of the next mark or NULL if no mark is found. */    pos_T *getnextmark(startpos, dir, begin_line)    pos_T	*startpos;	/* where to start */    int		dir;	/* direction for search */    int		begin_line;{    int		i;    pos_T	*result = NULL;    pos_T	pos;     pos = *startpos;     /* When searching backward and leaving the cursor on the first non-blank,     * position must be in a previous line.     * When searching forward and leaving the cursor on the first non-blank,     * position must be in a next line. */    if (dir == BACKWARD && begin_line)	pos.col = 0;    else if (dir == FORWARD && begin_line)	pos.col = MAXCOL;     for (i = 0; i < NMARKS; i++)    {	if (curbuf->b_namedm[i].lnum > 0)	{	    if (dir == FORWARD)	    {		if ((result == NULL || lt(curbuf->b_namedm[i], *result))			&& lt(pos, curbuf->b_namedm[i]))		    result = &curbuf->b_namedm[i];	    }	    else	    {		if ((result == NULL || lt(*result, curbuf->b_namedm[i]))			&& lt(curbuf->b_namedm[i], pos))		    result = &curbuf->b_namedm[i];	    }	}    }     return result;} /* * For an xtended filemark: set the fnum from the fname. * This is used for marks obtained from the .viminfo file.  It's postponed * until the mark is used to avoid a long startup delay. */    static voidfname2fnum(fm)    xfmark_T	*fm;

Lines 1–500 of 1753.