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