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