Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / nvi / vi / v_ulcase.c
blobc6d91515e04142478224dbac6c5543a9a720ac5a
1 /* $NetBSD: v_ulcase.c,v 1.1.1.2 2008/05/18 14:31:47 aymeric Exp $ */
3 /*-
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1992, 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
12 #include "config.h"
14 #ifndef lint
15 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";
16 #endif /* not lint */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/time.h>
22 #include <bitstring.h>
23 #include <ctype.h>
24 #include <errno.h>
25 #include <limits.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
30 #include "../common/common.h"
31 #include "vi.h"
33 static int ulcase __P((SCR *, db_recno_t, CHAR_T *, size_t, size_t, size_t));
36 * v_ulcase -- [count]~
37 * Toggle upper & lower case letters.
39 * !!!
40 * Historic vi didn't permit ~ to cross newline boundaries. I can
41 * think of no reason why it shouldn't, which at least lets the user
42 * auto-repeat through a paragraph.
44 * !!!
45 * In historic vi, the count was ignored. It would have been better
46 * if there had been an associated motion, but it's too late to make
47 * that the default now.
49 * PUBLIC: int v_ulcase __P((SCR *, VICMD *));
51 int
52 v_ulcase(SCR *sp, VICMD *vp)
54 db_recno_t lno;
55 size_t cno, lcnt, len;
56 u_long cnt;
57 CHAR_T *p;
59 lno = vp->m_start.lno;
60 cno = vp->m_start.cno;
62 for (cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1; cnt > 0; cno = 0) {
63 /* SOF is an error, EOF is an infinite count sink. */
64 if (db_get(sp, lno, 0, &p, &len)) {
65 if (lno == 1) {
66 v_emsg(sp, NULL, VIM_EMPTY);
67 return (1);
69 --lno;
70 break;
73 /* Empty lines decrement the count by one. */
74 if (len == 0) {
75 --cnt;
76 vp->m_final.cno = 0;
77 continue;
80 if (cno + cnt >= len) {
81 lcnt = len - 1;
82 cnt -= len - cno;
84 vp->m_final.cno = len - 1;
85 } else {
86 lcnt = cno + cnt - 1;
87 cnt = 0;
89 vp->m_final.cno = lcnt + 1;
92 if (ulcase(sp, lno, p, len, cno, lcnt))
93 return (1);
95 if (cnt > 0)
96 ++lno;
99 vp->m_final.lno = lno;
100 return (0);
104 * v_mulcase -- [count]~[count]motion
105 * Toggle upper & lower case letters over a range.
107 * PUBLIC: int v_mulcase __P((SCR *, VICMD *));
110 v_mulcase(SCR *sp, VICMD *vp)
112 CHAR_T *p;
113 size_t len;
114 db_recno_t lno;
116 for (lno = vp->m_start.lno;;) {
117 if (db_get(sp, lno, DBG_FATAL, &p, &len))
118 return (1);
119 if (len != 0 && ulcase(sp, lno, p, len,
120 lno == vp->m_start.lno ? vp->m_start.cno : 0,
121 !F_ISSET(vp, VM_LMODE) &&
122 lno == vp->m_stop.lno ? vp->m_stop.cno : len))
123 return (1);
125 if (++lno > vp->m_stop.lno)
126 break;
130 * XXX
131 * I didn't create a new motion command when I added motion semantics
132 * for ~. While that's the correct way to do it, that choice would
133 * have required changes all over the vi directory for little gain.
134 * Instead, we pretend it's a yank command. Note, this means that we
135 * follow the cursor motion rules for yank commands, but that seems
136 * reasonable to me.
138 return (0);
142 * ulcase --
143 * Change part of a line's case.
145 static int
146 ulcase(SCR *sp, db_recno_t lno, CHAR_T *lp, size_t len, size_t scno, size_t ecno)
148 size_t blen;
149 int change, rval;
150 CHAR_T ch, *p, *t;
151 CHAR_T *bp;
153 GET_SPACE_RETW(sp, bp, blen, len);
154 MEMMOVEW(bp, lp, len);
156 change = rval = 0;
157 for (p = bp + scno, t = bp + ecno + 1; p < t; ++p) {
158 ch = *p;
159 if (ISLOWER(ch)) {
160 *p = TOUPPER(ch);
161 change = 1;
162 } else if (ISUPPER(ch)) {
163 *p = TOLOWER(ch);
164 change = 1;
168 if (change && db_set(sp, lno, bp, len))
169 rval = 1;
171 FREE_SPACEW(sp, bp, blen);
172 return (rval);