-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathte_macro.c
342 lines (286 loc) · 6.34 KB
/
te_macro.c
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* te_macro.c
Text editor.
Macros.
Copyright (c) 2015-2021 Miguel Garcia / FloppySoftware
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
Changes:
30 Jan 2018 : Extracted from te.c.
20 Feb 2018 : Disable code for macros from strings, for now.
26 Dec 2018 : Allow # of repeats in macros - ie {up:12}. Rename MacroGetCh() to MacroGet().
29 Dec 2018 : Added MacroRunning().
26 Dec 2019 : Now K_INTRO is K_CR.
01 Jul 2021 : Change macro symbol names to match key bindings names.
06 Jul 2021 : Optimize MacroGet() a bit.
25 Sep 2021 : Added MacroIsCmdChar(). Allow comments as {# comment}. Send '\0' from MacroStop().
01 Nov 2021 : Modified MacroRunFile() to set raw mode and return success / error flag.
Modified MacroGetRaw() and MacroGet() to support raw mode.
Check in MacroGetRaw() for illegal characters.
18 Nov 2021 : Added {AutoIndent}, {AutoList}.
20 Nov 2021 : Added MatchSym().
*/
/* Run a macro from file
---------------------
Returns NZ on error.
*/
MacroRunFile(fname, raw)
char *fname;
int raw;
{
if(!(mac_fp = fopen(fname, "r")))
{
ErrLineOpen();
return -1;
}
mac_raw = raw;
/* Reset auto-indentation and auto-list */
mac_indent = cf_indent;
mac_list = cf_list;
cf_indent = cf_list = 0;
return 0;
}
/* Run a macro from string
-----------------------
*/
/*
MacroRunStr(s)
char *s;
{
mac_str = s;
}
*/
/* Tell if a macro is running
--------------------------
*/
MacroRunning()
{
return mac_fp != NULL /* || mac_str != NULL */;
}
/* Stop a macro
------------
*/
MacroStop()
{
if(mac_fp)
{
fclose(mac_fp);
}
mac_fp = /*mac_str =*/ NULL;
/* Restore auto-indentation and auto-list */
cf_indent = mac_indent;
cf_list = mac_list;
/* Flag end of input */
ForceCh('\0');
}
/* Read raw character from macro input
-----------------------------------
*/
MacroGetRaw()
{
int ch;
if(mac_fp)
{
if(mac_raw)
{
/* Raw mode - translate some control chars. */
switch(ch = fgetc(mac_fp))
{
case '\n' : ch = K_CR; break;
case '\t' : ch = ' '; break;
}
}
else
{
/* Normal mode: ignore new-lines */
while((ch = fgetc(mac_fp)) == '\n')
;
}
if(ch != EOF)
{
/* Translate control chars. */
if(ch < 32 || ch == 127)
{
ch = '?';
}
return ch;
}
MacroStop();
}
/*
else if(mac_str)
{
if(*mac_str)
{
return *mac_str++;
}
MacroStop();
}
*/
/* No character available */
return '\0';
}
/* Check if a character is legal for symbol name
---------------------------------------------
*/
MacroIsCmdChar(ch)
char ch;
{
return isalpha(ch) || ch == '#' || ch == '+' || ch == '-';
}
/* Check if a string is a symbol
-----------------------------
*/
MatchSym(s)
char *s;
{
return MatchStr(mac_sym, s);
}
/* Process a macro input unit
--------------------------
*/
MacroGet()
{
int i, n, ch;
/* Continue if there is a character available */
if((ch = MacroGetRaw()))
{
/* Return character if raw mode */
if(mac_raw)
{
ForceCh(ch);
return;
}
/* Return character if it's not the start of a symbol */
if(ch != MAC_START)
{
/* Check for escaped characters */
if(ch != MAC_ESCAPE)
{
ForceCh(ch);
}
else
{
if((ch = MacroGetRaw()))
{
ForceCh(ch);
}
else
{
/* Error: missing escaped character */
ErrLine("Bad escape sequence");
MacroStop();
}
}
return;
}
/* Get symbol name like {up} or {up:12} --> "up" */
for(i = 0; MacroIsCmdChar(ch = MacroGetRaw()) && i < MAC_SYM_MAX; ++i)
{
mac_sym[i] = tolower(ch);
}
if(i)
{
/* End of symbol name */
mac_sym[i] = '\0';
/* Get # of repeats if any - ie: {up:12} --> 12 */
if(ch == MAC_SEP)
{
n = 0;
while(isdigit(ch = MacroGetRaw()))
n = n * 10 + ch - '0';
if(n < 0 || n > FORCED_MAX)
{
n = -1;
}
}
else
{
n = 1;
}
if(n >= 0)
{
/* Check for comments */
if(ch == ' ')
{
if((MatchSym("#")))
{
while((ch = MacroGetRaw()))
{
if(ch == MAC_END)
{
ForceCh('\0');
return;
}
}
}
}
/* Check for commands */
if(ch == MAC_END)
{
/* Do command action */
ch = 0;
if (MatchSym("up")) ch = K_UP;
else if(MatchSym("down")) ch = K_DOWN;
else if(MatchSym("left")) ch = K_LEFT;
else if(MatchSym("right")) ch = K_RIGHT;
else if(MatchSym("begin")) ch = K_BEGIN;
else if(MatchSym("end")) ch = K_END;
else if(MatchSym("top")) ch = K_TOP;
else if(MatchSym("bottom")) ch = K_BOTTOM;
else if(MatchSym("newline")) ch = K_CR;
else if(MatchSym("indent")) ch = K_TAB;
else if(MatchSym("delright")) ch = K_RDEL;
else if(MatchSym("delleft")) ch = K_LDEL;
else if(MatchSym("cut")) ch = K_CUT;
else if(MatchSym("copy")) ch = K_COPY;
else if(MatchSym("paste")) ch = K_PASTE;
else if(MatchSym("delete")) ch = K_DELETE;
else if(MatchSym("clearclip")) ch = K_CLRCLP;
#if OPT_BLOCK
else if(MatchSym("blockstart")) ch = K_BLK_START;
else if(MatchSym("blockend")) ch = K_BLK_END;
#endif
if(ch)
{
while(n--)
{
if(ForceCh(ch))
break;
}
return;
}
/* Special commands */
if(MatchSym("filename"))
{
while(n--)
ForceStr(CurrentFile());
return;
}
else if(MatchSym("autoindent")) {
cf_indent = (n ? 1 : 0);
ForceCh('\0');
return;
}
else if(MatchSym("autolist")) {
cf_list = (n ? 1 : 0);
ForceCh('\0');
return;
}
}
}
}
/* Error: symbol name not found, bad formed, too large, bad # of repeats */
ErrLine("Bad symbol");
MacroStop();
}
}