35
36:- module(pce_keybinding, []). 37:- use_module(pce_boot(pce_principal)). 38:- use_module(pce_boot(pce_realise)). 39
40:- multifile
41 binding/3. 42
43message_level(silent).
45
46
47
59binding(cua, editor,
60 [ '\\C-v' = paste,
61 '\\C-s' = save_buffer
62 ]).
63binding(cua, 'emacs$fundamental',
64 [ '\\C-f' = isearch_forward,
65 '\\C-o' = open,
66 '\\C-n' = new,
67 '\\C-p' = print
68 ]).
69binding(apple, editor,
70 [ '\\es' = save_buffer,
71 '\\ez' = undo
72 ]).
73binding(apple, 'emacs$fundamental',
74 [ '\\ec' = copy_or_capitalize_word,
75 '\\ex' = cut_or_execute_extended_command
76 ]).
77binding(apple, emacs_page,
78 [ '\\ev' = paste_or_scroll_down
79 ]).
80
81
82
90set_keybinding_style(Mode) :-
91 current_style(Mode),
92 !.
93set_keybinding_style(emacs) :-
94 !,
95 send(@key_bindings, for_all,
96 message(@arg2, unmodify)),
97 set_style(emacs).
98set_keybinding_style(Style) :-
99 set_keybinding_style(emacs),
100 ( binding(Style, Table, Modifications),
101 get(@key_bindings, member, Table, KB),
102 modify(Modifications, KB),
103 fail
104 ; true
105 ),
106 set_style(Style).
107
108
109modify([], _).
110modify([Mod|T], KB) :-
111 modify1(Mod, KB),
112 modify(T, KB).
113
114modify1(Key = Command, KB) :-
115 get(KB?bindings, value, Key, Command),
116 !.
117modify1(Key = Command, KB) :-
118 send(KB, save_default, Key),
119 send(KB, function, Key, Command),
120 get(KB, name, Table),
121 message_level(Level),
122 print_message(Level, format('~w (~p): ~w --> ~w',
123 [Table, KB, Key, Command])).
124modify1(delete(Key), KB) :-
125 \+ get(KB?bindings, value, Key, _),
126 !.
127modify1(delete(Key), KB) :-
128 send(KB, save_default, Key),
129 get(KB, bindings, Bindings),
130 send(Bindings, delete, Key),
131 get(KB, name, Table),
132 message_level(Level),
133 print_message(Level, format('~w: deleted ~w', [Table, Key])).
134
135
136 139
140:- pce_extend_class(key_binding).
141
142class_variable(style, name,
143 [ 'X'(emacs),
144 windows(cua),
145 apple(apple)
146 ],
147 ).
156current_style(Style) :-
157 get(@pce, convert, key_binding, class, Class),
158 get(Class, class_variable, style, Var),
159 get(Var, value, Style).
160
161set_style(Style) :-
162 get(@pce, convert, key_binding, class, Class),
163 get(Class, class_variable, style, Var),
164 send(Var, value, Style).
165
166
167apply_preferences(KB) :->
168 ::
169 send(KB, apply_cua),
170 send(KB, bind_resources). 171
172apply_cua(KB) :->
173 ::
174 current_style(Mode),
175 ( Mode == emacs
176 -> true
177 ; get(KB, name, Name),
178 binding(Mode, Name, Modifications)
179 -> modify(Modifications, KB)
180 ; true
181 ).
182
183save_default(KB, Key:name) :->
184 ::
185 ( get(KB, attribute, modified, Undo)
186 -> true
187 ; send(KB, attribute, modified, new(Undo, sheet))
188 ),
189 ( get(Undo, value, Key, _)
190 -> true 191 ; get(KB, bindings, Bindings),
192 ( get(Bindings, value, Key, Command)
193 -> send(Undo, value, Key, Command)
194 ; send(Undo, value, Key, @nil)
195 )
196 ).
197
198unmodify(KB) :->
199 ::
200 ( get(KB, attribute, modified, Undo)
201 -> send(Undo, for_all,
202 message(KB, unbind, @arg1?name, @arg1?value)),
203 send(KB, delete_attribute, modified)
204 ; true
205 ).
206
207unbind(KB, Key:name, Command:[name|code]*) :->
208 ::
209 get(KB, name, Table),
210 message_level(Level),
211 ( Command == @nil
212 -> get(KB, bindings, Sheet),
213 send(Sheet, delete, Key),
214 print_message(Level,
215 format('~w: deleted ~w', [Table, Key]))
216 ; send(KB, function, Key, Command),
217 print_message(Level,
218 format('~w (~p): ~w --> ~w',
219 [Table, KB, Key, Command]))
220 ).
221
222:- pce_end_class(key_binding).
223
224
229
230:- pce_extend_class(pce).
231
232:- pce_group(preferences).
233
234key_binding_style(_Pce, Style:name) :->
235 ::
236 set_keybinding_style(Style).
237
238key_binding_style(_Pce, Style:name) :<-
239 ::
240 current_style(Style).
241
242key_binding_styles(_Pce, Styles:chain) :<-
243 ::
244 findall(Style, binding(Style, _Class, _Mod), StyleList),
245 sort([emacs|StyleList], Sorted),
246 new(Styles, chain),
247 add_styles(Sorted, Styles).
248
249add_styles([], _).
250add_styles([H|T], Chain) :-
251 send(Chain, append, H),
252 add_styles(T, Chain).
253
254:- pce_end_class(pce).
255
256
259
260make_key_binding_style_type :-
261 get(@pce, convert, key_binding_style, type, Type),
262 send(Type, name_reference, key_binding_style_type),
263 send(Type, kind, name_of),
264 get(@pce, key_binding_styles, Styles),
265 send(Type, slot, context, Styles).
266
267:- initialization make_key_binding_style_type. 268
269
270 273
274:- pce_extend_class(editor).
275
276copy_or_capitalize_word(E, Arg:[int]) :->
277 ::
278 ( Arg == @default,
279 send(@event, has_modifier, m)
280 -> send(E, copy)
281 ; send(E, capitalize_word, Arg)
282 ).
283
284cut_or_execute_extended_command(E, Arg:[int]) :->
285 ::
286 ( Arg == @default,
287 send(@event, has_modifier, m)
288 -> send(E, cut)
289 ; send(E, noarg_call, execute_extended_command, Arg)
290 ).
291
292
293paste_or_scroll_down(E, Arg:[int]) :->
294 ::
295 ( Arg == @default,
296 send(@event, has_modifier, m)
297 -> send(E, paste)
298 ; send(E, scroll_down, Arg)
299 ).
300
301:- pce_end_class(editor).
302
303:- pce_extend_class(list_browser).
304
305paste_or_scroll_down(LB, Arg:[int]) :->
306 ::
307 send(LB, scroll_down, Arg).
308
309:- pce_end_class(list_browser)