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