1*e0b8e63eSJohn Marino /*-
2*e0b8e63eSJohn Marino * Copyright (c) 1992, 1993, 1994
3*e0b8e63eSJohn Marino * The Regents of the University of California. All rights reserved.
4*e0b8e63eSJohn Marino * Copyright (c) 1992, 1993, 1994, 1995, 1996
5*e0b8e63eSJohn Marino * Keith Bostic. All rights reserved.
6*e0b8e63eSJohn Marino *
7*e0b8e63eSJohn Marino * See the LICENSE file for redistribution information.
8*e0b8e63eSJohn Marino */
9*e0b8e63eSJohn Marino
10*e0b8e63eSJohn Marino #include "config.h"
11*e0b8e63eSJohn Marino
12*e0b8e63eSJohn Marino #include <sys/types.h>
13*e0b8e63eSJohn Marino #include <sys/queue.h>
14*e0b8e63eSJohn Marino #include <sys/time.h>
15*e0b8e63eSJohn Marino
16*e0b8e63eSJohn Marino #include <bitstring.h>
17*e0b8e63eSJohn Marino #include <limits.h>
18*e0b8e63eSJohn Marino #include <stdio.h>
19*e0b8e63eSJohn Marino
20*e0b8e63eSJohn Marino #include "../common/common.h"
21*e0b8e63eSJohn Marino #include "vi.h"
22*e0b8e63eSJohn Marino
23*e0b8e63eSJohn Marino /*
24*e0b8e63eSJohn Marino * v_right -- [count]' ', [count]l
25*e0b8e63eSJohn Marino * Move right by columns.
26*e0b8e63eSJohn Marino *
27*e0b8e63eSJohn Marino * PUBLIC: int v_right(SCR *, VICMD *);
28*e0b8e63eSJohn Marino */
29*e0b8e63eSJohn Marino int
v_right(SCR * sp,VICMD * vp)30*e0b8e63eSJohn Marino v_right(SCR *sp, VICMD *vp)
31*e0b8e63eSJohn Marino {
32*e0b8e63eSJohn Marino size_t len;
33*e0b8e63eSJohn Marino int isempty;
34*e0b8e63eSJohn Marino
35*e0b8e63eSJohn Marino if (db_eget(sp, vp->m_start.lno, NULL, &len, &isempty)) {
36*e0b8e63eSJohn Marino if (isempty)
37*e0b8e63eSJohn Marino goto eol;
38*e0b8e63eSJohn Marino return (1);
39*e0b8e63eSJohn Marino }
40*e0b8e63eSJohn Marino
41*e0b8e63eSJohn Marino /* It's always illegal to move right on empty lines. */
42*e0b8e63eSJohn Marino if (len == 0) {
43*e0b8e63eSJohn Marino eol: v_eol(sp, NULL);
44*e0b8e63eSJohn Marino return (1);
45*e0b8e63eSJohn Marino }
46*e0b8e63eSJohn Marino
47*e0b8e63eSJohn Marino /*
48*e0b8e63eSJohn Marino * Non-motion commands move to the end of the range. Delete and
49*e0b8e63eSJohn Marino * yank stay at the start. Ignore others. Adjust the end of the
50*e0b8e63eSJohn Marino * range for motion commands.
51*e0b8e63eSJohn Marino *
52*e0b8e63eSJohn Marino * !!!
53*e0b8e63eSJohn Marino * Historically, "[cdsy]l" worked at the end of a line. Also,
54*e0b8e63eSJohn Marino * EOL is a count sink.
55*e0b8e63eSJohn Marino */
56*e0b8e63eSJohn Marino vp->m_stop.cno = vp->m_start.cno +
57*e0b8e63eSJohn Marino (F_ISSET(vp, VC_C1SET) ? vp->count : 1);
58*e0b8e63eSJohn Marino if (vp->m_start.cno == len - 1 && !ISMOTION(vp)) {
59*e0b8e63eSJohn Marino v_eol(sp, NULL);
60*e0b8e63eSJohn Marino return (1);
61*e0b8e63eSJohn Marino }
62*e0b8e63eSJohn Marino if (vp->m_stop.cno >= len) {
63*e0b8e63eSJohn Marino vp->m_stop.cno = len - 1;
64*e0b8e63eSJohn Marino vp->m_final = ISMOTION(vp) ? vp->m_start : vp->m_stop;
65*e0b8e63eSJohn Marino } else if (ISMOTION(vp)) {
66*e0b8e63eSJohn Marino --vp->m_stop.cno;
67*e0b8e63eSJohn Marino vp->m_final = vp->m_start;
68*e0b8e63eSJohn Marino } else
69*e0b8e63eSJohn Marino vp->m_final = vp->m_stop;
70*e0b8e63eSJohn Marino return (0);
71*e0b8e63eSJohn Marino }
72*e0b8e63eSJohn Marino
73*e0b8e63eSJohn Marino /*
74*e0b8e63eSJohn Marino * v_dollar -- [count]$
75*e0b8e63eSJohn Marino * Move to the last column.
76*e0b8e63eSJohn Marino *
77*e0b8e63eSJohn Marino * PUBLIC: int v_dollar(SCR *, VICMD *);
78*e0b8e63eSJohn Marino */
79*e0b8e63eSJohn Marino int
v_dollar(SCR * sp,VICMD * vp)80*e0b8e63eSJohn Marino v_dollar(SCR *sp, VICMD *vp)
81*e0b8e63eSJohn Marino {
82*e0b8e63eSJohn Marino size_t len;
83*e0b8e63eSJohn Marino int isempty;
84*e0b8e63eSJohn Marino
85*e0b8e63eSJohn Marino /*
86*e0b8e63eSJohn Marino * !!!
87*e0b8e63eSJohn Marino * A count moves down count - 1 rows, so, "3$" is the same as "2j$".
88*e0b8e63eSJohn Marino */
89*e0b8e63eSJohn Marino if ((F_ISSET(vp, VC_C1SET) ? vp->count : 1) != 1) {
90*e0b8e63eSJohn Marino /*
91*e0b8e63eSJohn Marino * !!!
92*e0b8e63eSJohn Marino * Historically, if the $ is a motion, and deleting from
93*e0b8e63eSJohn Marino * at or before the first non-blank of the line, it's a
94*e0b8e63eSJohn Marino * line motion, and the line motion flag is set.
95*e0b8e63eSJohn Marino */
96*e0b8e63eSJohn Marino vp->m_stop.cno = 0;
97*e0b8e63eSJohn Marino if (nonblank(sp, vp->m_start.lno, &vp->m_stop.cno))
98*e0b8e63eSJohn Marino return (1);
99*e0b8e63eSJohn Marino if (ISMOTION(vp) && vp->m_start.cno <= vp->m_stop.cno)
100*e0b8e63eSJohn Marino F_SET(vp, VM_LMODE);
101*e0b8e63eSJohn Marino
102*e0b8e63eSJohn Marino --vp->count;
103*e0b8e63eSJohn Marino if (v_down(sp, vp))
104*e0b8e63eSJohn Marino return (1);
105*e0b8e63eSJohn Marino }
106*e0b8e63eSJohn Marino
107*e0b8e63eSJohn Marino /*
108*e0b8e63eSJohn Marino * !!!
109*e0b8e63eSJohn Marino * Historically, it was illegal to use $ as a motion command on
110*e0b8e63eSJohn Marino * an empty line. Unfortunately, even though C was historically
111*e0b8e63eSJohn Marino * aliased to c$, it (and not c$) was special cased to work on
112*e0b8e63eSJohn Marino * empty lines. Since we alias C to c$ too, we have a problem.
113*e0b8e63eSJohn Marino * To fix it, we let c$ go through, on the assumption that it's
114*e0b8e63eSJohn Marino * not a problem for it to work.
115*e0b8e63eSJohn Marino */
116*e0b8e63eSJohn Marino if (db_eget(sp, vp->m_stop.lno, NULL, &len, &isempty)) {
117*e0b8e63eSJohn Marino if (!isempty)
118*e0b8e63eSJohn Marino return (1);
119*e0b8e63eSJohn Marino len = 0;
120*e0b8e63eSJohn Marino }
121*e0b8e63eSJohn Marino
122*e0b8e63eSJohn Marino if (len == 0) {
123*e0b8e63eSJohn Marino if (ISMOTION(vp) && !ISCMD(vp->rkp, 'c')) {
124*e0b8e63eSJohn Marino v_eol(sp, NULL);
125*e0b8e63eSJohn Marino return (1);
126*e0b8e63eSJohn Marino }
127*e0b8e63eSJohn Marino return (0);
128*e0b8e63eSJohn Marino }
129*e0b8e63eSJohn Marino
130*e0b8e63eSJohn Marino /*
131*e0b8e63eSJohn Marino * Non-motion commands move to the end of the range. Delete
132*e0b8e63eSJohn Marino * and yank stay at the start. Ignore others.
133*e0b8e63eSJohn Marino */
134*e0b8e63eSJohn Marino vp->m_stop.cno = len ? len - 1 : 0;
135*e0b8e63eSJohn Marino vp->m_final = ISMOTION(vp) ? vp->m_start : vp->m_stop;
136*e0b8e63eSJohn Marino return (0);
137*e0b8e63eSJohn Marino }
138