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