File: case.c

package info (click to toggle)
jove 4.16-3
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 1,804 kB
  • ctags: 2,864
  • sloc: ansic: 27,140; makefile: 399
file content (194 lines) | stat: -rw-r--r-- 3,133 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/************************************************************************
 * This program is Copyright (C) 1986-1996 by Jonathan Payne.  JOVE is  *
 * provided to you without charge, and with no warranty.  You may give  *
 * away copies of JOVE, including sources, provided that this notice is *
 * included in all the files.                                           *
 ************************************************************************/

#include "jove.h"
#include "disp.h"
#include "case.h"
#include "jctype.h"
#include "marks.h"
#include "move.h"

/* Commands:
 *	CapChar
 *	CapWord
 *	CasRegLower
 *	CasRegUpper
 *	LowWord
 *	UppWord
 */

private	bool
	lower proto((char *)),
	upper proto((char *));

private void
	CaseReg proto((bool up)),
	case_reg proto((LinePtr line1,int char1,LinePtr line2,int char2,bool up));

void
CapChar()
{
	register int	num;
	bool	restore = NO;
	Bufpos	b;

	DOTsave(&b);

	num = arg_value();
	if (num < 0) {
		restore = YES;
		num = -num;
		b_char(num);	/* Cap previous EXP chars */
	}
	while (num--) {
		if (upper(&linebuf[curchar])) {
			modify();
			makedirty(curline);
		}
		if (eolp()) {
			if (curline->l_next == NULL)
				break;
			SetLine(curline->l_next);
		} else
			curchar += 1;
	}
	if (restore)
		SetDot(&b);
}

void
CapWord()
{
	register int	num,
			restore = NO;
	Bufpos	b;

	DOTsave(&b);
	num = arg_value();
	if (num < 0) {
		restore = YES;
		f_word(num);		/* Cap previous EXP words */
		num = -num;
	}
	while (num--) {
		to_word(FORWARD);	/* Go to the beginning of the next word. */
		if (eobp())
			break;
		if (upper(&linebuf[curchar])) {
			modify();
			makedirty(curline);
		}
		curchar += 1;
		while (!eolp() && jisword(linebuf[curchar])) {
			if (lower(&linebuf[curchar])) {
				modify();
				makedirty(curline);
			}
			curchar += 1;
		}
	}
	if (restore)
		SetDot(&b);
}

private void
case_word(up)
bool	up;
{
	Bufpos	before;

	DOTsave(&before);
	ForWord();	/* this'll go backward if negative argument */
	case_reg(before.p_line, before.p_char, curline, curchar, up);
}

/* Convert *p to upper case.  Return YES iff it was changed. */

private bool
upper(p)
register char	*p;
{
	if (jislower(*p)) {
		*p = CharUpcase(*p);
		return YES;
	}
	return NO;
}

/* Convert *p to lower case.  Return YES iff it was changed. */

private bool
lower(p)
char	*p;
{
	char c = *p;

	if (jisupper(c)) {
		*p = CharDowncase(c);
		return YES;
	}
	return NO;
}

private void
case_reg(line1, char1, line2, char2, up)
LinePtr	line1,
	line2;
int	char1,
	char2;
bool	up;
{
	(void) fixorder(&line1, &char1, &line2, &char2);
	DotTo(line1, char1);

	for (;;) {
		if (curline == line2 && curchar == char2)
			break;
		if (!eolp())
			if ((up) ? upper(&linebuf[curchar]) : lower(&linebuf[curchar])) {
				makedirty(curline);
				modify();
			}
		f_char(1);
	}
}

void
CasRegLower()
{
	CaseReg(NO);
}

void
CasRegUpper()
{
	CaseReg(YES);
}

private void
CaseReg(up)
bool	up;
{
	register Mark	*mp = CurMark();
	Bufpos	savedot;

	DOTsave(&savedot);
	case_reg(curline, curchar, mp->m_line, mp->m_char, up);
	SetDot(&savedot);
}

void
UppWord()
{
	case_word(YES);
}

void
LowWord()
{
	case_word(NO);
}