1;	CPAGE.CMD:	C language MENU Page
2;			for MicroEMACS 3.9e and above
3;			(C)opyright 1988 by Daniel Lawrence
4;
5
6; set up the "clean" procedure
7store-procedure clean
8	delete-buffer "[c-new-proc]"
9	delete-buffer "[c-new-func]"
10	delete-buffer "[c-new-if]"
11	delete-buffer "[c-new-while]"
12	delete-buffer "[c-new-repeat]"
13	delete-buffer "[c-new-switch]"
14	delete-buffer "[c-value]"
15	delete-buffer "[c-keys-up]"
16	delete-buffer "[c-find-com]
17	delete-buffer "[c-comment-block]"
18	delete-buffer "[drawbox]"
19	delete-buffer "[setpoints]"
20	set $cmode %oldmode
21	set $gmode %oldgmode
22!endm
23
24; make sure the function key window is up
25	set %rcfkeys FALSE
26	toggle-fkeys
27	write-message "Loading..."
28
29; Write out the page instructions
30	save-window
31	1 next-window
32	beginning-of-file
33	set $curcol 25
34	overwrite-string " F1 PROCEDURE             F2 function           "
35	next-line
36	set $curcol 25
37	overwrite-string " F3 if then else          F4 while              "
38	next-line
39	set $curcol 25
40	overwrite-string " F5 REPEAT                F6 switch             "
41	next-line
42	set $curcol 18
43	overwrite-string "C   "
44	set $curcol 25
45	overwrite-string " F7 VALUE                 F8 Reserved words UP  "
46	next-line
47	set $curcol 25
48	overwrite-string " F9 chk comment matching F10 Comment block      "
49	unmark-buffer
50	beginning-of-file
51	!force restore-window
52	update-screen
53
54;
55
56store-procedure c-new-proc
57;	set $debug TRUE
58        set %proc @"Procedure Name: "
59        insert-string &cat &cat "Procedure " %proc "~n"
60        insert-string "   ( VAR ~n"
61        insert-string "   );~n"
62        insert-string "~n"
63        insert-string &cat &cat "{ (* " %proc " *)~n"
64        insert-string &cat &cat "};  (* " %proc " *)~n"
65	previous-line
66        open-line
67        insert-string "   "
68	!force set $debug FALSE
69!endm
70
71;	Function
72store-procedure c-new-func
73        set %func @"Function Name: "
74        set %type @"Return type: "
75	insert-string %type
76	!if &not &sequal &right %type 1 "*"
77		insert-string " "
78	!endif
79        insert-string &cat %func "()~n~n{~n	~n}~n"
80        3 backward-character
81	!force set $debug FALSE
82!endm
83
84;
85
86store-procedure	c-new-if
87        set %cond @"Condition Expr: "
88        insert-string &cat &cat "if (" %cond ") {"
89	newline-and-indent
90	newline-and-indent
91        insert-string "} else {"
92	newline-and-indent
93	newline-and-indent
94       	insert-string &cat &cat "}  /* if (" %cond ") */"
95	1 previous-line
96        end-of-line
97        handle-tab
98	2 previous-line
99        end-of-line
100        handle-tab
101!endm
102
103;
104
105store-procedure	c-new-while
106        set %cond @"Condition Expr: "
107	;set $debug TRUE
108        insert-string &cat &cat "while (" %cond ") {"
109	newline-and-indent
110	newline-and-indent
111        insert-string &cat &cat "}  /* while (" %cond ") */"
112	1 previous-line
113        end-of-line
114	handle-tab
115!endm
116
117store-procedure c-new-repeat
118        set %cond @"Condition Expr: "
119	;set $debug TRUE
120        set %curcol $curcol
121        insert-string &cat &cat "REPEAT~n"
122        %curcol insert-string " "
123        set $curcol %curcol
124        !if &EQUal  %curcol 3
125	        insert-string &cat &cat "UNTIL " %cond ";"
126	!else
127	        insert-string &cat      "UNTIL " %cond
128	!endif
129        beginning-of-line
130        open-line
131        %curcol insert-string " "
132        insert-string "   "
133	!force set $debug FALSE
134!endm
135
136store-procedure c-new-switch
137        set %expr @"Switch Expr: "
138	;set $debug TRUE
139        set %curcol $curcol
140        insert-string &cat &cat "switch (" %expr ") {"
141	newline-and-indent
142	insert-string "}"
143	previous-line
144	end-of-line
145	newline-and-indent
146	handle-tab
147	insert-string "case "
148	!force set $debug FALSE
149!endm
150
151store-procedure c-value
152;	set $debug TRUE
153        set %curcol $curcol
154        set %value @"Value: "
155        insert-string &cat %value " : {~n"
156        %curcol insert-string " "
157        insert-string "    };~n"
158        %curcol insert-string " "
159	previous-line
160        beginning-of-line
161        open-line
162        %curcol insert-string " "
163        insert-string "       "
164	!force set $debug FALSE
165!endm
166
167store-procedure	c-keys-up
168;	set $debug TRUE
169        set %curcol $curcol
170	;save-file
171        set-mark
172	beginning-of-file
173        write-message "[Replacing 'begin']
174	replace-string "begin" "BEGIN"
175
176	beginning-of-file
177        write-message "[Replacing 'end']
178	replace-string "end" "END"
179
180	beginning-of-file
181        write-message "[Replacing ' if']
182	replace-string " if" " IF"
183
184	beginning-of-file
185        write-message "[Replacing ' else']
186	replace-string " else" " ELSE"
187
188	beginning-of-file
189        write-message "[Replacing ' repeat']
190	replace-string " repeat" " REPEAT"
191
192	beginning-of-file
193        write-message "[Replacing ' while']
194	replace-string " while" " WHILE"
195
196        exchange-point-and-mark
197	!force set $debug FALSE
198!endm
199
200store-procedure	c-find-com
201;	This hunts down mismatched comment problems in C
202
203	beginning-of-file
204	!force search-forward "/*"
205	!if &seq $status FALSE
206		!goto nend
207	!endif
208
209*nxtopen
210	;record the position of the open comment
211	update-screen
212	set %oline $curline
213	set %opos $curcol
214
215	;find the first close comment
216	!force search-forward "*/"
217	!if &seq $status FALSE
218		write-message "%%No close to this open comment"
219		!return
220	!endif
221
222	;record the position of the close comment
223	set %cline $curline
224	set %cpos $curcol
225
226	;go back to the open and find the next open
227	set $curline %oline
228	set $curcol %opos
229
230	;and now find the next open
231	!force search-forward "/*"
232	!if &seq $status FALSE
233		write-message "No errors"
234		!return
235	!endif
236	set %nline $curline
237	set %npos $curcol
238
239	;compare the close to the next open
240	!if &less %cline %nline
241		!goto getnext
242	!endif
243	!if &and &equ %cline %nline &less %cpos %npos
244		!goto getnext
245	!endif
246
247	;report a mismatch
248	set $curline %oline
249	set $curcol %opos
250	write-message "%%This comment does not terminate properly"
251	!return
252
253*getnext
254	set $curline %nline
255	set $curcol %npos
256	!goto nxtopen
257!endm
258
259store-procedure c-comment-block
260	set %c1 "/"
261	set %c2 "*"
262	set %c3 "\"
263	set %c4 "\"
264	set %c5 "/"
265	set %c6 "*"
266	run drawbox
267!endm
268
269store-procedure drawbox
270	run setpoints
271	set $curline %mline
272	set $curcol %mcol
273;draw top horizontal line
274	insert-string %c1
275	set %width &add 2 &sub %pcol %mcol
276	%width insert-string %c2
277 	insert-string %c3
278	newline-and-indent
279;draw bottom horizontal line
280	%pline goto-line
281	next-line
282	end-of-line
283   	newline
284	%mcol insert-string " "
285	insert-string %c4
286	%width insert-string %c2
287	insert-string %c5
288;bump pline
289	set %pline &add %pline 1
290;draw verticals -- go to top and work our way down
291	%mline goto-line
292	!while &less $curline %pline
293		next-line
294		end-of-line
295		!if &less $curcol %pcol
296			&sub %pcol $curcol insert-string " "
297		!endif
298		set $curcol %pcol
299		insert-string " "
300		insert-string %c6
301		set $curcol %mcol
302		insert-string %c6
303		insert-string " "
304	!endwhile
305;return to point
306	%pline goto-line
307	next-line
308	beginning-of-line
309	&add 6 %width forward-character
310!endm
311
312store-procedure setpoints
313; procedure will set pcol, pline, mcol and mline. currently at point
314; it will also detab the region
315	set %pcol $curcol
316	set %pline $curline
317	exchange-point-and-mark
318	set %mcol $curcol
319	set %mline $curline
320	exchange-point-and-mark
321	detab-region
322	set $curline %pline
323	set $curcol %pcol
324!endm
325
326macro-to-key c-new-proc		S-FN1
327macro-to-key c-new-func		S-FN2
328macro-to-key c-new-if		S-FN3
329macro-to-key c-new-while	S-FN4
330macro-to-key c-new-repeat	S-FN5
331macro-to-key c-new-switch	S-FN6
332macro-to-key c-value		S-FN7
333macro-to-key c-keys-up		S-FN8
334macro-to-key c-find-com		S-FN9
335macro-to-key c-comment-block	S-FN0
336macro-to-key c-comment-block	A-FN0
337
338; Set up CMODE
339set %oldmode $cmode
340set %oldgmode $gmode
341add-mode CMODE
342add-global-mode CMODE
343
344write-message "[C MENU page loaded]"
345