1; CUA-feel macros for MicroEMACS 4.0
2;
3; Pierre Perret - April 1993
4
5; Uses mark 12 as CUA anchor and mark14 as a temporary (for mark swapping).
6; Uses mark 13 as the other end of the CUA selection (usually shadows the point).
7; Uses the Clipboard for CUA copy/cut/paste in MS-Windows version,
8; uses the kill buffer for same in other implementations.
9
10; NOTE:
11; - some key sequences are not supported by MicroEMACS-DOS:
12;     Shift+Arrow, Ctrl+Ins/Del and Shift+Ins/Del/Home/End/PageUp/PageDown.
13; - Shift+Ctrl+Arrow/Home/End not supported by MicroEMACS-DOS
14
15
16store-procedure CUA-cmdhook
17; NOTE: unfortunately, %keepanchor is always set to FALSE for
18;       M- and ^X prefixed keystrokes, regardless of the binding
19    set %discmd $discmd
20    set $discmd FALSE
21    !if %keepanchor
22        set %keepanchor FALSE
23    !else
24        ; previous command was not an anchor-preserving one
25        !force 12 remove-mark
26        set $hilight 255
27    !endif
28    set $discmd %discmd
29!endm
30set $cmdhook CUA-cmdhook
31
32store-procedure CUA-load
33; Loads the anchor (mark12) into the mark0, saving
34; the previous mark0 into mark14.
35; Loads the selection end (mark13) into the point
36; (not saving the previous point is intentionnal, to avoid
37;  user confusion when the selection has moved out of the
38;  window, due to abuse of the scroll bars)
39; The flag %anchor is set if there is a non-empty selection
40; Used by most deletion/copying actions
41    set %discmd $discmd
42    set $discmd FALSE
43    !force 0 exchange-point-and-mark
44    !if $status
45        14 set-mark
46        0 exchange-point-and-mark
47    !else
48        !force 14 remove-mark
49    !endif
50    !force 12 exchange-point-and-mark
51    !if $status
52        0 set-mark
53        12 exchange-point-and-mark
54        !if &seq $region ""
55            set %anchor FALSE
56        !else
57            set %anchor TRUE
58            13 goto-mark
59        !endif
60    !else
61        !force 0 remove-mark
62        set %anchor FALSE
63    !endif
64    set $discmd %discmd
65!endm
66
67store-procedure CUA-restore
68; restores mark0 from mark14 after a CUA-load call
69    set %discmd $discmd
70    set $discmd FALSE
71    !force 14 exchange-point-and-mark
72    !if $status
73        0 set-mark
74        14 exchange-point-and-mark
75    !else
76        !force 0 remove-mark
77    !endif
78    set $discmd %discmd
79!endm
80
81store-procedure CUA-Del
82    run CUA-load
83    !if %anchor
84        kill-region
85    !else
86        !force 1 delete-next-character
87    !endif
88    run CUA-restore
89!endm
90macro-to-key    CUA-Del     FND
91
92store-procedure CUA-C-Ins
93    run CUA-load
94    !if %anchor
95        set %keepanchor TRUE
96        !if &seq $os MSWIN
97            clip-region
98        !else
99            copy-region
100        !endif
101    !endif
102    run CUA-restore
103!endm
104macro-to-key    CUA-C-Ins	FN^C
105!if &seq $os MSWIN
106    unbind-menu     ">&Edit>&Clipboard>&Copy region"
107    macro-to-menu   CUA-C-Ins	"&Copy@1"
108!endif
109
110store-procedure CUA-S-Del
111    run CUA-load
112    !if %anchor
113        !if &seq $os MSWIN
114            cut-region
115        !else
116            kill-region
117        !endif
118    !endif
119    run CUA-restore
120!endm
121macro-to-key    CUA-S-Del	S-FND
122!if &seq $os MSWIN
123    unbind-menu     ">&Edit>&Clipboard>Cu&t region"
124    macro-to-menu   CUA-S-Del   "Cu&t@0"
125!else
126bind-to-key     yank            S-FNC   ; Shift+Ins
127!endif
128
129
130store-procedure CUA-case-upper
131    run CUA-load
132    !if %anchor
133        set %keepanchor TRUE
134        case-region-upper
135    !endif
136    run CUA-restore
137!endm
138macro-to-key    CUA-case-upper  A-U ; Alt+U
139!if &seq $os MSWIN
140    macro-to-menu   CUA-case-upper  ">&Edit>&Selection@4>&Upper case"
141!endif
142
143store-procedure CUA-case-lower
144    run CUA-load
145    !if %anchor
146        set %keepanchor TRUE
147        case-region-lower
148    !endif
149    run CUA-restore
150!endm
151macro-to-key    CUA-case-lower  A-L ; Alt+L
152!if &seq $os MSWIN
153    macro-to-menu   CUA-case-lower  "&Lower case"
154!endif
155
156store-procedure CUA-count-words
157; this procedure is not really necessary but it demonstrates
158; how to add CUA-based functionality
159    run CUA-load
160    !if %anchor
161        set %keepanchor TRUE
162        count-words
163    !endif
164    run CUA-restore
165!endm
166macro-to-key    CUA-count-words A-W ; Alt+W
167!if &seq $os MSWIN
168    macro-to-menu   CUA-count-words "Count &words"
169!endif
170
171store-procedure CUA-flip-selection
172; a sort of exchange-point-and-anchor, could be used to
173; visualize the selection by going back and forth to its
174; boundaries
175    set %discmd $discmd
176    set $discmd FALSE
177    !force 13 goto-mark
178    !force 12 exchange-point-and-mark
179    !if $status
180        13 set-mark
181        set %keepanchor TRUE
182    !endif
183    set $discmd %discmd
184!endm
185macro-to-key    CUA-flip-selection A-=
186!if &seq $os MSWIN
187    bind-to-menu    nop "-"
188    macro-to-menu   CUA-flip-selection "&Flip"
189!endif
190
191store-procedure CUA-select-region
192; makes the anchor equal to the mark
193; (useful to build very large selections)
194    set %discmd $discmd
195    set $discmd FALSE
196    13 set-mark
197    !force 0 exchange-point-and-mark
198    !if $status
199        12 set-mark
200        0 exchange-point-and-mark
201        set $hilight 12
202        set %keepanchor TRUE
203    !else
204        12 remove-mark
205        set $hilight 255
206    !endif
207    update-screen
208    set $discmd %discmd
209!endm
210macro-to-key    CUA-select-region A-^M  ; Alt+Enter
211!if &seq $os MSWIN
212    macro-to-menu   CUA-select-region   "Select &region"
213!endif
214
215store-procedure CUA-anchor
216; makes sure we have a CUA anchor point (i.e. a mark12)
217; used internally by extended selection keys before command processing
218    set %discmd $discmd
219    set $discmd FALSE
220    !force 12 exchange-point-and-mark
221    !if $status
222        12 exchange-point-and-mark
223    !else
224        12 set-mark
225        13 set-mark
226    !endif
227    set $discmd %discmd
228!endm
229
230store-procedure CUA-select
231; sets hilighting of the selection
232; used internally by extended selection keys after command processing
233; always sets %keepanchor to TRUE and makes sure mark13 shadows the point
234    set %discmd $discmd
235    set $discmd FALSE
236    set %keepanchor TRUE
237    13 set-mark
238    set $hilight 12
239    update-screen
240    set $discmd %discmd
241!endm
242
243store-procedure CUA-S-home
244    run CUA-anchor
245    beginning-of-line
246    run CUA-select
247!endm
248macro-to-key    CUA-S-home          S-FN<
249
250store-procedure CUA-S-end
251    run CUA-anchor
252    end-of-line
253    run CUA-select
254!endm
255macro-to-key    CUA-S-end           S-FN>
256
257store-procedure CUA-SC-home
258    run CUA-anchor
259    beginning-of-file
260    run CUA-select
261!endm
262macro-to-key    CUA-SC-home         S-FN^<
263
264store-procedure CUA-SC-end
265    run CUA-anchor
266    end-of-file
267    run CUA-select
268!endm
269macro-to-key    CUA-SC-end          S-FN^>
270
271store-procedure CUA-S-pageup
272    run CUA-anchor
273    previous-page
274    run CUA-select
275!endm
276macro-to-key    CUA-S-pageup        S-FNZ
277
278store-procedure CUA-S-pagedown
279    run CUA-anchor
280    next-page
281    run CUA-select
282!endm
283macro-to-key    CUA-S-pagedown      S-FNV
284
285store-procedure CUA-S-up
286    run CUA-anchor
287    !force previous-line
288    run CUA-select
289!endm
290macro-to-key    CUA-S-up            S-FNP
291
292store-procedure CUA-S-down
293    run CUA-anchor
294    !force next-line
295    run CUA-select
296!endm
297macro-to-key    CUA-S-down          S-FNN
298
299store-procedure CUA-S-left
300    run CUA-anchor
301    !force backward-character
302    run CUA-select
303!endm
304macro-to-key    CUA-S-left          S-FNB
305
306store-procedure CUA-S-right
307    run CUA-anchor
308    !force forward-character
309    run CUA-select
310!endm
311macro-to-key    CUA-S-right         S-FNF
312
313store-procedure CUA-SC-left
314    run CUA-anchor
315    !force previous-word
316    run CUA-select
317!endm
318macro-to-key    CUA-SC-left         S-FN^B
319
320store-procedure CUA-SC-right
321    run CUA-anchor
322    !force next-word
323    run CUA-select
324!endm
325macro-to-key    CUA-SC-right        S-FN^F
326
327bind-to-key     beginning-of-file   FN^<
328bind-to-key     end-of-file         FN^>
329bind-to-key     beginning-of-line   FN<
330bind-to-key     end-of-line         FN>
331
332; Transfer the standard MicroEMACS left mouse button commands onto the
333; right button (text scrolling, window resizing, screen move/resize...)
334; and the old right button commands to Shift + right button
335!force unbind-key  MS1  ; should have been MS^a anyway!
336bind-to-key mouse-move-down     MSe     ; right_button_down
337bind-to-key mouse-move-up       MSf     ; right_button_up
338bind-to-key mouse-resize-screen MS^e    ; Ctrl + right_button_down
339bind-to-key mouse-region-down   S-MSe     ; Shift + right_button_down
340bind-to-key mouse-region-up     S-MSf     ; Shift + right_button_up
341
342store-procedure MSleft-down
343    set %discmd $discmd
344    set $discmd FALSE
345    !force mouse-move-down
346    !if $status
347        set %keepanchor TRUE
348        12 set-mark
349        13 set-mark
350        set $hilight 12
351        set %msbuf $cbufname
352    !else
353        set %msbuf ""
354        !force 12 remove-mark
355    !endif
356    set $discmd %discmd
357!endm
358macro-to-key MSleft-down    MSa
359
360store-procedure MSdrag
361    !if &equ $hilight 12
362        ; the LEFT button is down
363        run CUA-anchor
364        !force mouse-move-down
365        run CUA-select
366    !else
367        !force mouse-move
368    !endif
369!endm
370macro-to-key MSdrag    MSm
371
372store-procedure MSleft-up
373    set %discmd $discmd
374    set $discmd FALSE
375    !force mouse-move-down
376    !if &and $status &seq %msbuf $cbufname
377        set %keepanchor TRUE
378    !else
379        !force 12 remove-mark
380    !endif
381    set $discmd %discmd
382!endm
383macro-to-key MSleft-up      MSb
384
385store-procedure S-MSleft
386    run CUA-anchor
387    !force mouse-move-down
388    run CUA-select
389!endm
390macro-to-key S-MSleft       S-MSa
391macro-to-key S-MSleft       S-MSb
392