Debian Codegraph @

case.c

raw
1 /**************************************************************************
2 * This program is Copyright (C) 1986-2002 by Jonathan Payne. JOVE is *
3 * provided by Jonathan and Jovehacks without charge and without *
4 * warranty. You may copy, modify, and/or distribute JOVE, provided that *
5 * this notice is included in all the source files and documentation. *
6 **************************************************************************/
7
8 #include "jove.h"
9 #include "disp.h"
10 #include "case.h"
11 #include "jctype.h"
12 #include "marks.h"
13 #include "move.h"
14
15 /* Commands:
16 * CapChar
17 * CapWord
18 * CasRegLower
19 * CasRegUpper
20 * LowWord
21 * UppWord
22 */
23
24 private jbool
25 lower(char *),
26 upper(char *);
27
28 private void
29 CaseReg(jbool up),
30 case_reg(LinePtr line1, int char1, LinePtr line2, int char2, jbool up);
31
32 void
33 CapChar(void)
34 {
35 int num;
36 jbool restore = NO;
37 Bufpos b;
38
39 DOTsave(&b);
40
41 num = arg_value();
42 if (num < 0) {
43 restore = YES;
44 num = -num;
45 b_char(num); /* Cap previous EXP chars */
46 }
47 while (num--) {
48 if (upper(&linebuf[curchar])) {
49 modify();
50 makedirty(curline);
51 }
52 if (eolp()) {
53 if (curline->l_next == NULL)
54 break;
55
56 SetLine(curline->l_next);
57 } else
58 curchar += 1;
59 }
60 if (restore)
61 SetDot(&b);
62 }
63
64 void
65 CapWord(void)
66 {
67 int num,
68 restore = NO;
69 Bufpos b;
70
71 DOTsave(&b);
72 num = arg_value();
73 if (num < 0) {
74 restore = YES;
75 f_word(num); /* Cap previous EXP words */
76 num = -num;
77 }
78 while (num--) {
79 to_word(FORWARD); /* Go to the beginning of the next word. */
80 if (eobp())
81 break;
82
83 if (upper(&linebuf[curchar])) {
84 modify();
85 makedirty(curline);
86 }
87 curchar += 1;
88 while (!eolp() && jisword(linebuf[curchar])) {
89 if (lower(&linebuf[curchar])) {
90 modify();
91 makedirty(curline);
92 }
93 curchar += 1;
94 }
95 }
96 if (restore)
97 SetDot(&b);
98 }
99
100 private void
101 case_word(jbool up)
102 {
103 Bufpos before;
104
105 DOTsave(&before);
106 ForWord(); /* this'll go backward if negative argument */
107 case_reg(before.p_line, before.p_char, curline, curchar, up);
108 }
109
110 /* Convert *p to upper case. Return YES iff it was changed. */
111
112 private jbool
113 upper(char *p)
114 {
115 char c = *p;
116
117 if (jislower(c)) {
118 *p = CharUpcase(c);
119 return YES;
120 }
121 return NO;
122 }
123
124 /* Convert *p to lower case. Return YES iff it was changed. */
125
126 private jbool
127 lower(char *p)
128 {
129 char c = *p;
130
131 if (jisupper(c)) {
132 *p = CharDowncase(c);
133 return YES;
134 }
135 return NO;
136 }
137
138 private void
139 case_reg(LinePtr line1, int char1, LinePtr line2, int char2, jbool up)
140 {
141 (void) fixorder(&line1, &char1, &line2, &char2);
142 DotTo(line1, char1);
143
144 for (;;) {
145 if (curline == line2 && curchar == char2)
146 break;
147
148 if (!eolp())
149 if ((up) ? upper(&linebuf[curchar]) : lower(&linebuf[curchar])) {
150 makedirty(curline);
151 modify();
152 }
153 f_char(1);
154 }
155 }
156
157 void
158 CasRegLower(void)
159 {
160 CaseReg(NO);
161 }
162
163 void
164 CasRegUpper(void)
165 {
166 CaseReg(YES);
167 }
168
169 private void
170 CaseReg(jbool up)
171 {
172 Mark *mp = CurMark();
173 Bufpos savedot;
174
175 DOTsave(&savedot);
176 case_reg(curline, curchar, mp->m_line, mp->m_char, up);
177 SetDot(&savedot);
178 }
179
180 void
181 UppWord(void)
182 {
183 case_word(YES);
184 }
185
186 void
187 LowWord(void)
188 {
189 case_word(NO);
190 }