xref: /freebsd/contrib/nvi/vi/v_yank.c (revision 1669d8afc64812c8d2d1d147ae1fd42ff441e1b1)
1 /*-
2  * Copyright (c) 1992, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  * Copyright (c) 1992, 1993, 1994, 1995, 1996
5  *	Keith Bostic.  All rights reserved.
6  *
7  * See the LICENSE file for redistribution information.
8  */
9 
10 #include "config.h"
11 
12 #ifndef lint
13 static const char sccsid[] = "@(#)v_yank.c	10.9 (Berkeley) 5/19/96";
14 #endif /* not lint */
15 
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/time.h>
19 
20 #include <bitstring.h>
21 #include <limits.h>
22 #include <stdio.h>
23 
24 #include "../common/common.h"
25 #include "vi.h"
26 
27 /*
28  * v_yank -- [buffer][count]y[count][motion]
29  *	     [buffer][count]Y
30  *	Yank text (or lines of text) into a cut buffer.
31  *
32  * !!!
33  * Historic vi moved the cursor to the from MARK if it was before the current
34  * cursor and on a different line, e.g., "yk" moves the cursor but "yj" and
35  * "yl" do not.  Unfortunately, it's too late to change this now.  Matching
36  * the historic semantics isn't easy.  The line number was always changed and
37  * column movement was usually relative.  However, "y'a" moved the cursor to
38  * the first non-blank of the line marked by a, while "y`a" moved the cursor
39  * to the line and column marked by a.  Hopefully, the motion component code
40  * got it right...   Unlike delete, we make no adjustments here.
41  *
42  * PUBLIC: int v_yank __P((SCR *, VICMD *));
43  */
44 int
45 v_yank(sp, vp)
46 	SCR *sp;
47 	VICMD *vp;
48 {
49 	size_t len;
50 
51 	if (cut(sp,
52 	    F_ISSET(vp, VC_BUFFER) ? &vp->buffer : NULL, &vp->m_start,
53 	    &vp->m_stop, F_ISSET(vp, VM_LMODE) ? CUT_LINEMODE : 0))
54 		return (1);
55 	sp->rptlines[L_YANKED] += (vp->m_stop.lno - vp->m_start.lno) + 1;
56 
57 	/*
58 	 * One special correction, in case we've deleted the current line or
59 	 * character.  We check it here instead of checking in every command
60 	 * that can be a motion component.
61 	 */
62 	if (db_get(sp, vp->m_final.lno, DBG_FATAL, NULL, &len))
63 		return (1);
64 
65 	/*
66 	 * !!!
67 	 * Cursor movements, other than those caused by a line mode command
68 	 * moving to another line, historically reset the relative position.
69 	 *
70 	 * This currently matches the check made in v_delete(), I'm hoping
71 	 * that they should be consistent...
72 	 */
73 	if (!F_ISSET(vp, VM_LMODE)) {
74 		F_CLR(vp, VM_RCM_MASK);
75 		F_SET(vp, VM_RCM_SET);
76 
77 		/* Make sure the set cursor position exists. */
78 		if (vp->m_final.cno >= len)
79 			vp->m_final.cno = len ? len - 1 : 0;
80 	}
81 	return (0);
82 }
83