align CHAR_T string in log
[nvi.git] / vi / v_mark.c
blob8d0f470605792bd8c1f97263b30aae3c6228cc30
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.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: v_mark.c,v 10.11 1996/12/18 10:24:46 bostic Exp $ (Berkeley) $Date: 1996/12/18 10:24:46 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/time.h>
20 #include <bitstring.h>
21 #include <limits.h>
22 #include <stdlib.h>
23 #include <stdio.h>
25 #include "../common/common.h"
26 #include "vi.h"
28 enum which {BQMARK, FQMARK};
29 static int mark __P((SCR *, VICMD *, int, enum which));
32 * v_mark -- m[a-z]
33 * Set a mark.
35 * PUBLIC: int v_mark __P((SCR *, VICMD *));
37 int
38 v_mark(sp, vp)
39 SCR *sp;
40 VICMD *vp;
42 return (mark_set(sp, vp->character, &vp->m_start, 1));
46 * v_bmark -- `['`a-z]
47 * Move to a mark.
49 * Moves to a mark, setting both row and column.
51 * !!!
52 * Although not commonly known, the "'`" and "'`" forms are historically
53 * valid. The behavior is determined by the first character, so "`'" is
54 * the same as "``". Remember this fact -- you'll be amazed at how many
55 * people don't know it and will be delighted that you are able to tell
56 * them.
58 * PUBLIC: int v_bmark __P((SCR *, VICMD *));
60 int
61 v_bmark(sp, vp)
62 SCR *sp;
63 VICMD *vp;
65 return (mark(sp, vp, 1, BQMARK));
69 * v_fmark -- '['`a-z]
70 * Move to a mark.
72 * Move to the first nonblank character of the line containing the mark.
74 * PUBLIC: int v_fmark __P((SCR *, VICMD *));
76 int
77 v_fmark(sp, vp)
78 SCR *sp;
79 VICMD *vp;
81 return (mark(sp, vp, 1, FQMARK));
85 * v_emark -- <mouse click>
86 * Mouse mark.
88 * PUBLIC: int v_emark __P((SCR *, VICMD *));
90 int
91 v_emark(sp, vp)
92 SCR *sp;
93 VICMD *vp;
95 SMAP *smp;
97 smp = HMAP + vp->ev.e_lno;
98 if (smp > TMAP) {
99 msgq(sp, M_BERR, "320|Unknown cursor position.");
100 return (1);
102 vp->m_stop.lno = smp->lno;
103 vp->m_stop.cno =
104 vs_colpos(sp, smp->lno, vp->ev.e_cno + (smp->soff - 1) * sp->cols);
105 return (mark(sp, vp, 0, BQMARK));
109 * mark --
110 * Mark commands.
112 static int
113 mark(sp, vp, getmark, cmd)
114 SCR *sp;
115 VICMD *vp;
116 int getmark;
117 enum which cmd;
119 dir_t dir;
120 MARK m;
121 size_t len;
123 if (getmark && mark_get(sp, vp->character, &vp->m_stop, M_BERR))
124 return (1);
127 * !!!
128 * Historically, BQMARKS for character positions that no longer
129 * existed acted as FQMARKS.
131 * FQMARKS move to the first non-blank.
133 switch (cmd) {
134 case BQMARK:
135 if (db_get(sp, vp->m_stop.lno, DBG_FATAL, NULL, &len))
136 return (1);
137 if (vp->m_stop.cno < len ||
138 vp->m_stop.cno == len && len == 0)
139 break;
141 if (ISMOTION(vp))
142 F_SET(vp, VM_LMODE);
143 cmd = FQMARK;
144 /* FALLTHROUGH */
145 case FQMARK:
146 vp->m_stop.cno = 0;
147 if (nonblank(sp, vp->m_stop.lno, &vp->m_stop.cno))
148 return (1);
149 break;
150 default:
151 abort();
154 /* Non-motion commands move to the end of the range. */
155 if (!ISMOTION(vp)) {
156 vp->m_final = vp->m_stop;
157 return (0);
161 * !!!
162 * If a motion component to a BQMARK, the cursor has to move.
164 if (cmd == BQMARK &&
165 vp->m_stop.lno == vp->m_start.lno &&
166 vp->m_stop.cno == vp->m_start.cno) {
167 v_nomove(sp);
168 return (1);
172 * If the motion is in the reverse direction, switch the start and
173 * stop MARK's so that it's in a forward direction. (There's no
174 * reason for this other than to make the tests below easier. The
175 * code in vi.c:vi() would have done the switch.) Both forward
176 * and backward motions can happen for any kind of search command.
178 if (vp->m_start.lno > vp->m_stop.lno ||
179 vp->m_start.lno == vp->m_stop.lno &&
180 vp->m_start.cno > vp->m_stop.cno) {
181 m = vp->m_start;
182 vp->m_start = vp->m_stop;
183 vp->m_stop = m;
184 dir = BACKWARD;
185 } else
186 dir = FORWARD;
189 * Yank cursor motion, when associated with marks as motion commands,
190 * historically behaved as follows:
192 * ` motion ' motion
193 * Line change? Line change?
194 * Y N Y N
195 * -------------- ---------------
196 * FORWARD: | NM NM | NM NM
197 * | |
198 * BACKWARD: | M M | M NM(1)
200 * where NM means the cursor didn't move, and M means the cursor
201 * moved to the mark.
203 * As the cursor was usually moved for yank commands associated
204 * with backward motions, this implementation regularizes it by
205 * changing the NM at position (1) to be an M. This makes mark
206 * motions match search motions, which is probably A Good Thing.
208 * Delete cursor motion was always to the start of the text region,
209 * regardless. Ignore other motion commands.
211 #ifdef HISTORICAL_PRACTICE
212 if (ISCMD(vp->rkp, 'y')) {
213 if ((cmd == BQMARK ||
214 cmd == FQMARK && vp->m_start.lno != vp->m_stop.lno) &&
215 (vp->m_start.lno > vp->m_stop.lno ||
216 vp->m_start.lno == vp->m_stop.lno &&
217 vp->m_start.cno > vp->m_stop.cno))
218 vp->m_final = vp->m_stop;
219 } else if (ISCMD(vp->rkp, 'd'))
220 if (vp->m_start.lno > vp->m_stop.lno ||
221 vp->m_start.lno == vp->m_stop.lno &&
222 vp->m_start.cno > vp->m_stop.cno)
223 vp->m_final = vp->m_stop;
224 #else
225 vp->m_final = vp->m_start;
226 #endif
229 * Forward marks are always line oriented, and it's set in the
230 * vcmd.c table.
232 if (cmd == FQMARK)
233 return (0);
236 * BQMARK'S moving backward and starting at column 0, and ones moving
237 * forward and ending at column 0 are corrected to the last column of
238 * the previous line. Otherwise, adjust the starting/ending point to
239 * the character before the current one (this is safe because we know
240 * the search had to move to succeed).
242 * Mark motions become line mode opertions if they start at the first
243 * nonblank and end at column 0 of another line.
245 if (vp->m_start.lno < vp->m_stop.lno && vp->m_stop.cno == 0) {
246 if (db_get(sp, --vp->m_stop.lno, DBG_FATAL, NULL, &len))
247 return (1);
248 vp->m_stop.cno = len ? len - 1 : 0;
249 len = 0;
250 if (nonblank(sp, vp->m_start.lno, &len))
251 return (1);
252 if (vp->m_start.cno <= len)
253 F_SET(vp, VM_LMODE);
254 } else
255 --vp->m_stop.cno;
257 return (0);