119304Speter/*-
219304Speter * Copyright (c) 1992, 1993, 1994
319304Speter *	The Regents of the University of California.  All rights reserved.
419304Speter * Copyright (c) 1992, 1993, 1994, 1995, 1996
519304Speter *	Keith Bostic.  All rights reserved.
619304Speter *
719304Speter * See the LICENSE file for redistribution information.
819304Speter */
919304Speter
1019304Speter#include "config.h"
1119304Speter
1219304Speter#ifndef lint
13254225Speterstatic const char sccsid[] = "$Id: v_yank.c,v 10.10 2001/06/25 15:19:36 skimo Exp $";
1419304Speter#endif /* not lint */
1519304Speter
1619304Speter#include <sys/types.h>
1719304Speter#include <sys/queue.h>
1819304Speter#include <sys/time.h>
1919304Speter
2019304Speter#include <bitstring.h>
2119304Speter#include <limits.h>
2219304Speter#include <stdio.h>
2319304Speter
2419304Speter#include "../common/common.h"
2519304Speter#include "vi.h"
2619304Speter
2719304Speter/*
2819304Speter * v_yank -- [buffer][count]y[count][motion]
2919304Speter *	     [buffer][count]Y
3019304Speter *	Yank text (or lines of text) into a cut buffer.
3119304Speter *
3219304Speter * !!!
3319304Speter * Historic vi moved the cursor to the from MARK if it was before the current
3419304Speter * cursor and on a different line, e.g., "yk" moves the cursor but "yj" and
3519304Speter * "yl" do not.  Unfortunately, it's too late to change this now.  Matching
3619304Speter * the historic semantics isn't easy.  The line number was always changed and
3719304Speter * column movement was usually relative.  However, "y'a" moved the cursor to
3819304Speter * the first non-blank of the line marked by a, while "y`a" moved the cursor
3919304Speter * to the line and column marked by a.  Hopefully, the motion component code
4019304Speter * got it right...   Unlike delete, we make no adjustments here.
4119304Speter *
4219304Speter * PUBLIC: int v_yank __P((SCR *, VICMD *));
4319304Speter */
4419304Speterint
45254225Speterv_yank(SCR *sp, VICMD *vp)
4619304Speter{
4719304Speter	size_t len;
4819304Speter
4919304Speter	if (cut(sp,
5019304Speter	    F_ISSET(vp, VC_BUFFER) ? &vp->buffer : NULL, &vp->m_start,
5119304Speter	    &vp->m_stop, F_ISSET(vp, VM_LMODE) ? CUT_LINEMODE : 0))
5219304Speter		return (1);
5319304Speter	sp->rptlines[L_YANKED] += (vp->m_stop.lno - vp->m_start.lno) + 1;
5419304Speter
5519304Speter	/*
5619304Speter	 * One special correction, in case we've deleted the current line or
5719304Speter	 * character.  We check it here instead of checking in every command
5819304Speter	 * that can be a motion component.
5919304Speter	 */
6019304Speter	if (db_get(sp, vp->m_final.lno, DBG_FATAL, NULL, &len))
6119304Speter		return (1);
6219304Speter
6319304Speter	/*
6419304Speter	 * !!!
6519304Speter	 * Cursor movements, other than those caused by a line mode command
6619304Speter	 * moving to another line, historically reset the relative position.
6719304Speter	 *
6819304Speter	 * This currently matches the check made in v_delete(), I'm hoping
6919304Speter	 * that they should be consistent...
7019304Speter	 */
7119304Speter	if (!F_ISSET(vp, VM_LMODE)) {
7219304Speter		F_CLR(vp, VM_RCM_MASK);
7319304Speter		F_SET(vp, VM_RCM_SET);
7419304Speter
7519304Speter		/* Make sure the set cursor position exists. */
7619304Speter		if (vp->m_final.cno >= len)
7719304Speter			vp->m_final.cno = len ? len - 1 : 0;
7819304Speter	}
7919304Speter	return (0);
8019304Speter}
81