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.
6 *
7 * See the LICENSE file for redistribution information.
8 */
9
10 #include "config.h"
11
12 #include <sys/types.h>
13 #include <sys/queue.h>
14 #include <sys/time.h>
15
16 #include <bitstring.h>
17 #include <ctype.h>
18 #include <errno.h>
19 #include <limits.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "../common/common.h"
25 #include "vi.h"
26
27 static int ulcase(SCR *, recno_t, CHAR_T *, size_t, size_t, size_t);
28
29 /*
30 * v_ulcase -- [count]~
31 * Toggle upper & lower case letters.
32 *
33 * !!!
34 * Historic vi didn't permit ~ to cross newline boundaries. I can
35 * think of no reason why it shouldn't, which at least lets the user
36 * auto-repeat through a paragraph.
37 *
38 * !!!
39 * In historic vi, the count was ignored. It would have been better
40 * if there had been an associated motion, but it's too late to make
41 * that the default now.
42 *
43 * PUBLIC: int v_ulcase(SCR *, VICMD *);
44 */
45 int
v_ulcase(SCR * sp,VICMD * vp)46 v_ulcase(SCR *sp, VICMD *vp)
47 {
48 recno_t lno;
49 size_t cno, lcnt, len;
50 u_long cnt;
51 CHAR_T *p;
52
53 lno = vp->m_start.lno;
54 cno = vp->m_start.cno;
55
56 for (cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1; cnt > 0; cno = 0) {
57 /* SOF is an error, EOF is an infinite count sink. */
58 if (db_get(sp, lno, 0, &p, &len)) {
59 if (lno == 1) {
60 v_emsg(sp, NULL, VIM_EMPTY);
61 return (1);
62 }
63 --lno;
64 break;
65 }
66
67 /* Empty lines decrement the count by one. */
68 if (len == 0) {
69 --cnt;
70 vp->m_final.cno = 0;
71 continue;
72 }
73
74 if (cno + cnt >= len) {
75 lcnt = len - 1;
76 cnt -= len - cno;
77
78 vp->m_final.cno = len - 1;
79 } else {
80 lcnt = cno + cnt - 1;
81 cnt = 0;
82
83 vp->m_final.cno = lcnt + 1;
84 }
85
86 if (ulcase(sp, lno, p, len, cno, lcnt))
87 return (1);
88
89 if (cnt > 0)
90 ++lno;
91 }
92
93 vp->m_final.lno = lno;
94 return (0);
95 }
96
97 /*
98 * v_mulcase -- [count]~[count]motion
99 * Toggle upper & lower case letters over a range.
100 *
101 * PUBLIC: int v_mulcase(SCR *, VICMD *);
102 */
103 int
v_mulcase(SCR * sp,VICMD * vp)104 v_mulcase(SCR *sp, VICMD *vp)
105 {
106 CHAR_T *p;
107 size_t len;
108 recno_t lno;
109
110 for (lno = vp->m_start.lno;;) {
111 if (db_get(sp, lno, DBG_FATAL, &p, &len))
112 return (1);
113 if (len != 0 && ulcase(sp, lno, p, len,
114 lno == vp->m_start.lno ? vp->m_start.cno : 0,
115 !F_ISSET(vp, VM_LMODE) &&
116 lno == vp->m_stop.lno ? vp->m_stop.cno : len))
117 return (1);
118
119 if (++lno > vp->m_stop.lno)
120 break;
121 }
122
123 /*
124 * XXX
125 * I didn't create a new motion command when I added motion semantics
126 * for ~. While that's the correct way to do it, that choice would
127 * have required changes all over the vi directory for little gain.
128 * Instead, we pretend it's a yank command. Note, this means that we
129 * follow the cursor motion rules for yank commands, but that seems
130 * reasonable to me.
131 */
132 return (0);
133 }
134
135 /*
136 * ulcase --
137 * Change part of a line's case.
138 */
139 static int
ulcase(SCR * sp,recno_t lno,CHAR_T * lp,size_t len,size_t scno,size_t ecno)140 ulcase(SCR *sp, recno_t lno, CHAR_T *lp, size_t len, size_t scno, size_t ecno)
141 {
142 size_t blen;
143 int change, rval;
144 ARG_CHAR_T ch;
145 CHAR_T *p, *t, *bp;
146
147 GET_SPACE_RETW(sp, bp, blen, len);
148 MEMMOVE(bp, lp, len);
149
150 change = rval = 0;
151 for (p = bp + scno, t = bp + ecno + 1; p < t; ++p) {
152 ch = (UCHAR_T)*p;
153 if (ISLOWER(ch)) {
154 *p = TOUPPER(ch);
155 change = 1;
156 } else if (ISUPPER(ch)) {
157 *p = TOLOWER(ch);
158 change = 1;
159 }
160 }
161
162 if (change && db_set(sp, lno, bp, len))
163 rval = 1;
164
165 FREE_SPACEW(sp, bp, blen);
166 return (rval);
167 }
168