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