⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mark.c

📁 VIM文本编辑器
💻 C
📖 第 1 页 / 共 2 页
字号:
/* 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.
 */

/*
 * 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_names[n] 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 struct filemark namedfm[NMARKS + EXTRA_MARKS];	/* marks with file nr */
static char_u *namedfm_names[NMARKS + EXTRA_MARKS];	/* name for namedfm[] */

static void show_one_mark __ARGS((int, char_u *, FPOS *, char_u *));
static void cleanup_jumplist __ARGS((void));

/*
 * setmark(c) - set named mark 'c' at current cursor position
 *
 * Returns OK on success, FAIL if no room for mark or bad name given.
 */
    int
setmark(c)
    int		c;
{
    int		i;

    if (c == '\'' || c == '`')
    {
	setpcmark();
	/* keep it even when the cursor doesn't move */
	curwin->w_prev_pcmark = curwin->w_pcmark;
	return OK;
    }

    if (c > 'z')	    /* some islower() and isupper() cannot handle
				characters above 127 */
	return FAIL;
    if (islower(c))
    {
	i = c - 'a';
	curbuf->b_namedm[i] = curwin->w_cursor;
	return OK;
    }
    if (isupper(c))
    {
	i = c - 'A';
	namedfm[i].mark = curwin->w_cursor;
	namedfm[i].fnum = curbuf->b_fnum;
	return OK;
    }
    return FAIL;
}

/*
 * setpcmark() - set the previous context mark to the current position
 *		 and add it to the jump list
 */
    void
setpcmark()
{
    int i;
#ifdef ROTATE
    struct filemark tempmark;
#endif

    /* for :global the mark is set only once */
    if (global_busy)
	return;

    curwin->w_prev_pcmark = curwin->w_pcmark;
    curwin->w_pcmark = curwin->w_cursor;

#ifdef 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;
	for (i = 1; i < JUMPLISTSIZE; ++i)
	    curwin->w_jumplist[i - 1] = curwin->w_jumplist[i];
    }
    curwin->w_jumplistidx = curwin->w_jumplistlen - 1;

#ifdef ARCHIE
    /* Workaround for a bug in gcc 2.4.5 R2 on the Archimedes
     * Should be fixed in 2.5.x.
     */
    curwin->w_jumplist[curwin->w_jumplistidx].mark.ptr = curwin->w_pcmark.ptr;
    curwin->w_jumplist[curwin->w_jumplistidx].mark.col = curwin->w_pcmark.col;
#else
    curwin->w_jumplist[curwin->w_jumplistidx].mark = curwin->w_pcmark;
#endif
    curwin->w_jumplist[curwin->w_jumplistidx].fnum = curbuf->b_fnum;
    ++curwin->w_jumplistidx;
}

/*
 * checkpcmark() - 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. -- webb.
 *		   If pcmark was deleted (with "dG") the previous mark is
 *		   restored.
 */
    void
checkpcmark()
{
    if (curwin->w_prev_pcmark.lnum != 0
	    && (equal(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 */
    }
}

/*
 * move "count" positions in the jump list (count may be negative)
 */
    FPOS *
movemark(count)
    int count;
{
    FPOS	*pos;

    cleanup_jumplist();

    if (curwin->w_jumplistlen == 0)	    /* nothing to jump to */
	return (FPOS *)NULL;

    if (curwin->w_jumplistidx + count < 0 ||
			curwin->w_jumplistidx + count >= curwin->w_jumplistlen)
	return (FPOS *)NULL;

    /*
     * if first CTRL-O or CTRL-I command after a jump, add cursor position to
     * list.  Careful: If there are duplicates (CTRL-O immidiately 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 (FPOS *)NULL;
    }

    curwin->w_jumplistidx += count;
						/* jump to other file */
    if (curwin->w_jumplist[curwin->w_jumplistidx].fnum != curbuf->b_fnum)
    {
	if (buflist_getfile(curwin->w_jumplist[curwin->w_jumplistidx].fnum,
			  curwin->w_jumplist[curwin->w_jumplistidx].mark.lnum,
							    0, FALSE) == FAIL)
	    return (FPOS *)NULL;
	curwin->w_cursor.col =
			   curwin->w_jumplist[curwin->w_jumplistidx].mark.col;
	pos = (FPOS *)-1;
    }
    else
	pos = &(curwin->w_jumplist[curwin->w_jumplistidx].mark);
    return pos;
}

/*
 * getmark(c) - find mark for char 'c'
 *
 * Return pointer to FPOS if found (caller needs to check lnum!)
 *	  NULL if there is no mark called 'c'.
 *	  -1 if mark is in other file (only if changefile is TRUE)
 */
    FPOS *
getmark(c, changefile)
    int		c;
    int		changefile;		/* allowed to edit another file */
{
    FPOS	    *posp;
    FPOS	    *startp, *endp;
    static  FPOS    pos_copy;
    char_u	    *p;

    posp = NULL;
    if (c > '~')			/* check for islower()/isupper() */
	;
    else 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 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 == '>')	/* start/end of visual area */
    {
	startp = &curbuf->b_visual_start;
	endp = &curbuf->b_visual_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_mode == 'V')
	{
	    pos_copy = *posp;
	    posp = &pos_copy;
	    if (c == '<')
		pos_copy.col = 0;
	    else
		pos_copy.col = MAXCOL;
	}
    }
    else if (islower(c))		/* normal named mark */
	posp = &(curbuf->b_namedm[c - 'a']);
    else if (isupper(c) || vim_isdigit(c))	/* named file mark */
    {
	if (vim_isdigit(c))
	    c = c - '0' + NMARKS;
	else
	    c -= 'A';
	posp = &(namedfm[c].mark);

	if (namedfm[c].fnum == 0 && namedfm_names[c] != NULL)
	{
	    /*
	     * First expand "~/" in the file name to the home directory.
	     * Try to shorten the file name.
	     */
	    expand_env(namedfm_names[c], NameBuff, MAXPATHL);
	    mch_dirname(IObuff, IOSIZE);
	    p = shorten_fname(NameBuff, IObuff);

	    /* buflist_new will call fmarks_check_names() */
	    (void)buflist_new(NameBuff, p, (linenr_t)1, FALSE);
	}

	if (namedfm[c].fnum != curbuf->b_fnum)	    /* mark is in other file */
	{
	    if (namedfm[c].mark.lnum != 0 && changefile && namedfm[c].fnum)
	    {
		if (buflist_getfile(namedfm[c].fnum,
			     namedfm[c].mark.lnum, GETF_SETMARK, FALSE) == OK)
		{
		    curwin->w_cursor.col = namedfm[c].mark.col;
		    return (FPOS *)-1;
		}
	    }
	    posp = &pos_copy;		/* mark exists, but is not valid in
					    current buffer */
	    pos_copy.lnum = 0;
	}
    }
    return posp;
}

/*
 * Check all file marks for a name that matches the file name in buf.
 * May replace the name with an fnum.
 */
    void
fmarks_check_names(buf)
    BUF	    *buf;
{
    char_u	*name;
    int		i;

    if (buf->b_ffname == NULL)
	return;

    name = home_replace_save(buf, buf->b_ffname);
    if (name == NULL)
	return;

    for (i = 0; i < NMARKS + EXTRA_MARKS; ++i)
    {
	if (namedfm[i].fnum == 0 && namedfm_names[i] != NULL &&
					fnamecmp(name, namedfm_names[i]) == 0)
	{
	    namedfm[i].fnum = buf->b_fnum;
	    vim_free(namedfm_names[i]);
	    namedfm_names[i] = NULL;
	}
    }
    vim_free(name);
}

/*
 * Check a if a position from a mark is valid.
 * Give and error message and return FAIL if not.
 */
    int
check_mark(pos)
    FPOS    *pos;
{
    if (pos == NULL)
    {
	emsg(e_umark);
	return FAIL;
    }
    if (pos->lnum == 0)
    {
	emsg(e_marknotset);
	return FAIL;
    }
    if (pos->lnum > curbuf->b_ml.ml_line_count)
    {
	emsg(e_markinval);
	return FAIL;
    }
    return OK;
}

/*
 * clrallmarks() - clear all marks in the buffer 'buf'
 *
 * Used mainly when trashing the entire buffer during ":e" type commands
 */
    void
clrallmarks(buf)
    BUF	    *buf;
{
    static int		i = -1;

    if (i == -1)	/* first call ever: initialize */
	for (i = 0; i < NMARKS + 1; i++)
	{
	    namedfm[i].mark.lnum = 0;
	    namedfm_names[i] = NULL;
	}

    for (i = 0; i < NMARKS; i++)
	buf->b_namedm[i].lnum = 0;
    buf->b_op_start.lnum = 0;		/* start/end op mark cleared */
    buf->b_op_end.lnum = 0;
    buf->b_last_cursor.lnum = 1;	/* '" mark cleared */
    buf->b_last_cursor.col = 0;
}

/*
 * Get name of file from a filemark.
 * Returns an allocated string.
 */
    char_u *
fm_getname(fmark)
    struct filemark *fmark;
{
    if (fmark->fnum != curbuf->b_fnum)		    /* not current file */
	return buflist_nr2name(fmark->fnum, FALSE, TRUE);
    return vim_strsave((char_u *)"-current-");
}

/*
 * print the marks
 */
    void
do_marks(arg)
    char_u	*arg;
{
    int		i;
    char_u	*name;

    if (arg != NULL && *arg == NUL)
	arg = NULL;

    show_one_mark('\'', arg, &curwin->w_pcmark, NULL);
    for (i = 0; i < NMARKS; ++i)
	show_one_mark(i + 'a', arg, &curbuf->b_namedm[i], NULL);
    for (i = 0; i < NMARKS + EXTRA_MARKS; ++i)
    {
	name = namedfm[i].fnum ? fm_getname(&namedfm[i]) : namedfm_names[i];
	if (name != NULL)
	{
	    show_one_mark(i >= NMARKS ? i - NMARKS + '0' : i + 'A',
						 arg, &namedfm[i].mark, name);
	    if (namedfm[i].fnum)
		vim_free(name);
	}
    }
    show_one_mark('"', arg, &curbuf->b_last_cursor, NULL);
    show_one_mark('[', arg, &curbuf->b_op_start, NULL);
    show_one_mark(']', arg, &curbuf->b_op_end, NULL);
    show_one_mark('<', arg, &curbuf->b_visual_start, NULL);
    show_one_mark('>', arg, &curbuf->b_visual_end, NULL);
    show_one_mark(-1, arg, NULL, NULL);
}

    static void
show_one_mark(c, arg, p, name)
    int	    c;
    char_u  *arg;
    FPOS    *p;
    char_u  *name;
{
    static int	    did_title = FALSE;

    if (c == -1)			    /* finish up */
    {
	if (did_title)
	    did_title = FALSE;
	else
	{
	    if (arg == NULL)
		MSG("No marks set");
	    else
		EMSG2("No marks matching \"%s\"", arg);
	}
    }
    /* don't output anything if 'q' typed at --more-- prompt */
    else if (!got_int && (arg == NULL || vim_strchr(arg, c) != NULL) &&
								 p->lnum != 0)
    {
	if (!did_title)
	{
	    /* Highlight title */
	    MSG_PUTS_TITLE("\nmark line  col file");
	    did_title = TRUE;
	}
	msg_putchar('\n');
	if (!got_int)
	{
	    sprintf((char *)IObuff, " %c %5ld  %3d  ", c, p->lnum, p->col);
	    if (name != NULL)
		STRCAT(IObuff, name);
	    msg_outtrans(IObuff);
	}
	out_flush();		    /* show one line at a time */
    }
}

/*
 * print the jumplist
 */
    void
do_jumps()
{
    int		i;
    char_u	*name;

    cleanup_jumplist();
    /* Highlight title */
    MSG_PUTS_TITLE("\n jump line  file");
    for (i = 0; i < curwin->w_jumplistlen; ++i)
    {
	if (curwin->w_jumplist[i].mark.lnum != 0)
	{
	    name = fm_getname(&curwin->w_jumplist[i]);
	    if (name == NULL)	    /* file name not available */
		continue;

	    msg_putchar('\n');
	    sprintf((char *)IObuff, "%c %2d %5ld  %s",
		i == curwin->w_jumplistidx ? '>' : ' ',
		i > curwin->w_jumplistidx ? i - curwin->w_jumplistidx

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -