Fix for a crash which happened when a document couldn't be opened.
[AROS-Contrib.git] / fish / microemacs / word.c
blob123d9788070ff9428aa7c8c5de57e698528f4b98
1 /*
2 * The routines in this file implement commands that work word at a time.
3 * There are all sorts of word mode commands. If I do any sentence and/or
4 * paragraph mode commands, they are likely to be put in this file.
5 */
7 #include <stdio.h>
8 #include "ed.h"
11 /* Word wrap on n-spaces. Back-over whatever precedes the point on the current
12 * line and stop on the first word-break or the beginning of the line. If we
13 * reach the beginning of the line, jump back to the end of the word and start
14 * a new line. Otherwise, break the line at the word-break, eat it, and jump
15 * back to the end of the word.
16 * NOTE: This function may leaving trailing blanks.
17 * Returns TRUE on success, FALSE on errors.
19 int wrapword()
21 register int cnt;
22 struct LINE *oldp;
23 oldp = curwp->w_dotp;
24 cnt = -1;
25 do {
26 cnt++;
27 if (! backchar(0, 1))
28 return(FALSE);
30 while (! inword());
31 if (! backword(0, 1))
32 return(FALSE);
33 if ((oldp == curwp->w_dotp) && curwp->w_doto) {
34 if (! backdel(0, 1))
35 return(FALSE);
36 if (! newline(0, 1))
37 return(FALSE);
39 return(forwword(0, 1) && forwchar(0, cnt));
43 * Move the cursor backward by "n" words. All of the details of motion are
44 * performed by the "backchar" and "forwchar" routines. Error if you try to
45 * move beyond the buffers.
47 int backword(f, n)
48 int f;
49 int n;
51 if (n < 0)
52 return (forwword(f, -n));
53 if (backchar(FALSE, 1) == FALSE)
54 return (FALSE);
55 while (n--) {
56 while (inword() == FALSE) {
57 if (backchar(FALSE, 1) == FALSE)
58 return (FALSE);
60 while (inword() != FALSE) {
61 if (backchar(FALSE, 1) == FALSE)
62 return (FALSE);
65 return (forwchar(FALSE, 1));
69 * Move the cursor forward by the specified number of words. All of the motion
70 * is done by "forwchar". Error if you try and move beyond the buffer's end.
72 int forwword(f, n)
73 int f;
74 int n;
76 if (n < 0)
77 return (backword(f, -n));
78 while (n--) {
79 while (inword() == FALSE) {
80 if (forwchar(FALSE, 1) == FALSE)
81 return (FALSE);
83 while (inword() != FALSE) {
84 if (forwchar(FALSE, 1) == FALSE)
85 return (FALSE);
88 return (TRUE);
92 * Move the cursor forward by the specified number of words. As you move,
93 * convert any characters to upper case. Error if you try and move beyond the
94 * end of the buffer. Bound to "M-U".
96 int upperword(f, n)
97 int f;
98 int n;
100 register int c;
102 if (n < 0)
103 return (FALSE);
104 while (n--) {
105 while (inword() == FALSE) {
106 if (forwchar(FALSE, 1) == FALSE)
107 return (FALSE);
109 while (inword() != FALSE) {
110 c = lgetc(curwp->w_dotp, curwp->w_doto);
111 if (c>='a' && c<='z') {
112 c -= 'a'-'A';
113 lputc(curwp->w_dotp, curwp->w_doto, c);
114 lchange(WFHARD);
116 if (forwchar(FALSE, 1) == FALSE)
117 return (FALSE);
120 return (TRUE);
124 * Move the cursor forward by the specified number of words. As you move
125 * convert characters to lower case. Error if you try and move over the end of
126 * the buffer. Bound to "M-L".
128 int lowerword(f, n)
129 int f;
130 int n;
132 register int c;
134 if (n < 0)
135 return (FALSE);
136 while (n--) {
137 while (inword() == FALSE) {
138 if (forwchar(FALSE, 1) == FALSE)
139 return (FALSE);
141 while (inword() != FALSE) {
142 c = lgetc(curwp->w_dotp, curwp->w_doto);
143 if (c>='A' && c<='Z') {
144 c += 'a'-'A';
145 lputc(curwp->w_dotp, curwp->w_doto, c);
146 lchange(WFHARD);
148 if (forwchar(FALSE, 1) == FALSE)
149 return (FALSE);
152 return (TRUE);
156 * Move the cursor forward by the specified number of words. As you move
157 * convert the first character of the word to upper case, and subsequent
158 * characters to lower case. Error if you try and move past the end of the
159 * buffer. Bound to "M-C".
161 int capword(f, n)
162 int f;
163 int n;
165 register int c;
167 if (n < 0)
168 return (FALSE);
169 while (n--) {
170 while (inword() == FALSE) {
171 if (forwchar(FALSE, 1) == FALSE)
172 return (FALSE);
174 if (inword() != FALSE) {
175 c = lgetc(curwp->w_dotp, curwp->w_doto);
176 if (c>='a' && c<='z') {
177 c -= 'a'-'A';
178 lputc(curwp->w_dotp, curwp->w_doto, c);
179 lchange(WFHARD);
181 if (forwchar(FALSE, 1) == FALSE)
182 return (FALSE);
183 while (inword() != FALSE) {
184 c = lgetc(curwp->w_dotp, curwp->w_doto);
185 if (c>='A' && c<='Z') {
186 c += 'a'-'A';
187 lputc(curwp->w_dotp, curwp->w_doto, c);
188 lchange(WFHARD);
190 if (forwchar(FALSE, 1) == FALSE)
191 return (FALSE);
195 return (TRUE);
199 * Kill forward by "n" words. Remember the location of dot. Move forward by
200 * the right number of words. Put dot back where it was and issue the kill
201 * command for the right number of characters. Bound to "M-D".
203 int delfword(f, n)
204 int f;
205 int n;
207 register int size;
208 register LINE *dotp;
209 register int doto;
211 if (n < 0)
212 return (FALSE);
213 dotp = curwp->w_dotp;
214 doto = curwp->w_doto;
215 size = 0;
216 while (n--) {
217 while (inword() == FALSE) {
218 if (forwchar(FALSE, 1) == FALSE)
219 return (FALSE);
220 ++size;
222 while (inword() != FALSE) {
223 if (forwchar(FALSE, 1) == FALSE)
224 return (FALSE);
225 ++size;
228 curwp->w_dotp = dotp;
229 curwp->w_doto = doto;
230 return (ldelete(size, TRUE));
234 * Kill backwards by "n" words. Move backwards by the desired number of words,
235 * counting the characters. When dot is finally moved to its resting place,
236 * fire off the kill command. Bound to "M-Rubout" and to "M-Backspace".
238 int delbword(f, n)
239 int f;
240 int n;
242 register int size;
244 if (n < 0)
245 return (FALSE);
246 if (backchar(FALSE, 1) == FALSE)
247 return (FALSE);
248 size = 0;
249 while (n--) {
250 while (inword() == FALSE) {
251 if (backchar(FALSE, 1) == FALSE)
252 return (FALSE);
253 ++size;
255 while (inword() != FALSE) {
256 if (backchar(FALSE, 1) == FALSE)
257 return (FALSE);
258 ++size;
261 if (forwchar(FALSE, 1) == FALSE)
262 return (FALSE);
263 return (ldelete(size, TRUE));
267 * Return TRUE if the character at dot is a character that is considered to be
268 * part of a word. The word character list is hard coded. Should be setable.
270 int inword()
272 register int c;
274 if (curwp->w_doto == llength(curwp->w_dotp))
275 return (FALSE);
276 c = lgetc(curwp->w_dotp, curwp->w_doto);
277 if (c>='a' && c<='z')
278 return (TRUE);
279 if (c>='A' && c<='Z')
280 return (TRUE);
281 if (c>='0' && c<='9')
282 return (TRUE);
283 if (c=='$' || c=='_') /* For identifiers */
284 return (TRUE);
285 return (FALSE);