Skip to content

Commit 6b1795d

Browse files
committed
auto close & custom_complete routine
1 parent ce39283 commit 6b1795d

File tree

4 files changed

+56
-21
lines changed

4 files changed

+56
-21
lines changed

TeXmacs/progs/dynamic/program-edit.scm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,12 @@
654654
(clipboard-cut "nowhere")
655655
(clipboard-clear "nowhere"))
656656

657+
(tm-define (kbd-remove t forwards?)
658+
(:mode complete-mode?)
659+
(remove-text forwards?)
660+
(exec-delayed (lambda ()
661+
(kbd-variant (focus-tree) #t))))
662+
657663
(tm-define (kbd-variant t forwards?)
658664
(:require (and (prog-field-context? t) (program-supports-completions?)))
659665
(let* ((lan (get-env "prog-language"))

TeXmacs/progs/generic/generic-edit.scm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@
142142
(clipboard-cut "nowhere")
143143
(clipboard-clear "nowhere"))
144144

145+
(tm-define (kbd-remove t forwards?)
146+
(:mode complete-mode?)
147+
(remove-text forwards?)
148+
(exec-delayed (lambda ()
149+
(kbd-variant (focus-tree) #t))))
150+
145151
(tm-define (kbd-variant t forwards?)
146152
(:require (tree-is-buffer? t))
147153
(if (and (not (complete-try?)) forwards?)

src/Edit/Interface/edit_complete.cpp

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,22 @@ edit_interface_rep::complete_try () {
8787
}
8888
else {
8989
if ((end == 0) || (!is_iso_alpha (s[end - 1])) ||
90-
((end != N (s)) && is_iso_alpha (s[end])))
90+
((end != N (s)) && is_iso_alpha (s[end]))) {
91+
set_input_normal ();
92+
SERVER (set_completion_listbox_visible (false));
9193
return false;
94+
}
9295
int start= end - 1;
9396
while ((start > 0) && is_iso_alpha (s[start - 1]))
9497
start--;
9598
ss= s (start, end);
9699
a = find_completions (drd, et, ss);
97100
}
98-
if (N (a) == 0) return false;
101+
if (N (a) <= 1) {
102+
set_input_normal ();
103+
SERVER (set_completion_listbox_visible (false));
104+
return false;
105+
}
99106
complete_start (ss, a);
100107
return true;
101108
}
@@ -123,25 +130,18 @@ edit_interface_rep::complete_start (string prefix, array<string> compls) {
123130
if ((end < N (prefix)) || (s (end - N (prefix), end) != prefix)) return;
124131

125132
// perform first completion and switch to completion mode if necessary
126-
if (N (compls) == 1) {
127-
string s= compls[0];
128-
if (ends (s, "()")) // temporary fix for Pari
129-
insert_tree (s, path (N (s) - 1));
130-
else insert_tree (s);
131-
completions= array<string> ();
132-
}
133-
else {
133+
{
134134
completion_prefix= prefix;
135135
completions = close_completions (compls);
136136
completion_pos = 0;
137137
{
138138
// TODO: ?refactor this part to edit_interface_rep::show_completion_listbox
139139
cout << "complete_start: "
140-
<< "completions=" << completions
141-
<< ", prefix=" << completion_prefix
142-
<< ", tp=" << tp
143-
<< ", et=" << et
144-
<< ", eb=" << eb
140+
// << "completions=" << completions
141+
<< ", prefix=" << completion_prefix
142+
// << ", tp=" << tp
143+
// << ", et=" << et
144+
// << ", eb=" << eb
145145
<< LF;
146146
array<string> full_completions;
147147
for (int i= 0; i < N (completions); ++i) {
@@ -166,7 +166,7 @@ edit_interface_rep::complete_start (string prefix, array<string> compls) {
166166
}
167167
insert_tree (completions[0]);
168168
complete_message ();
169-
beep ();
169+
// beep ();
170170
set_input_mode (INPUT_COMPLETE);
171171
}
172172
}
@@ -182,11 +182,16 @@ edit_interface_rep::complete_keypress (string key) {
182182
SERVER (set_completion_listbox_visible (false));
183183
return true;
184184
}
185-
if ((key != "tab") && (key != "S-tab") && (key != "up") && (key != "down")) {
185+
else if (key == "escape" || key == " ") {
186186
set_input_normal ();
187187
SERVER (set_completion_listbox_visible (false));
188188
return false;
189189
}
190+
else if ((key != "tab") && (key != "S-tab") && (key != "up") && (key != "down")) {
191+
//set_input_normal ();
192+
//SERVER (set_completion_listbox_visible (false));
193+
return false;
194+
}
190195
tree st= subtree (et, path_up (tp));
191196
if (is_compound (st)) {
192197
set_input_normal ();
@@ -272,7 +277,7 @@ edit_interface_rep::custom_complete (tree r) {
272277
if (!is_tuple (r)) return;
273278
int i, n= N (r);
274279
string prefix;
275-
array<string> compls;
280+
hashset<string> compls;
276281
compls << string ("");
277282
for (i= 0; i < n; i++)
278283
if (is_atomic (r[i])) {
@@ -283,8 +288,12 @@ edit_interface_rep::custom_complete (tree r) {
283288
}
284289
// cout << prefix << ", " << compls << LF;
285290

286-
if ((prefix == "") || (N (compls) == 0)) return;
287-
complete_start (prefix, compls);
291+
if ((prefix == "") || (N (compls) <= 1)) {
292+
set_input_normal ();
293+
SERVER (set_completion_listbox_visible (false));
294+
return;
295+
}
296+
complete_start (prefix, as_completions (compls));
288297
}
289298

290299
/******************************************************************************

src/Edit/Interface/edit_keyboard.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,11 @@ edit_interface_rep::try_shortcut (string comb) {
152152
verbatim (rhs), shorth == "" ? 1 : 3000);
153153
}
154154
if ((status & 1) == 1) cmd ();
155-
else if (N (shorth) > 0 && comb != "S-F5") call ("kbd-insert", shorth);
155+
else if (N (shorth) > 0 && comb != "S-F5") {
156+
call ("kbd-insert", shorth);
157+
if (get_input_mode() == INPUT_COMPLETE)
158+
exec_delayed (scheme_cmd("(kbd-variant (focus-tree) #t)"));
159+
}
156160
// cout << "Mark= " << sh_mark << "\n";
157161
return true;
158162
}
@@ -223,6 +227,7 @@ is_combo_shortcuts (string key) {
223227

224228
void
225229
edit_interface_rep::key_press (string gkey) {
230+
cout << "Key press: " << gkey << "\n";
226231
string zero= "a";
227232
zero[0] = '\0';
228233
string key = replace (gkey, "<#0>", zero);
@@ -303,12 +308,19 @@ edit_interface_rep::key_press (string gkey) {
303308
if (!inside_active_graphics ()) {
304309
archive_state ();
305310
call ("kbd-insert", rew);
311+
cout << "Inserted single character1: " << rew << "\n";
312+
if (get_input_mode() == INPUT_COMPLETE){
313+
cout << "Exec delayed complete-try\n";
314+
//exec_delayed (scheme_cmd("(if (complete-try?) (noop))"));
315+
exec_delayed (scheme_cmd("(kbd-variant (focus-tree) #t)"));
316+
}
306317
}
307318
interrupt_shortcut ();
308319
}
309320
else if (contains_unicode_char (rew)) {
310321
archive_state ();
311322
call ("kbd-insert", key);
323+
cout << "Inserted single character2: " << rew << "\n";
312324
interrupt_shortcut ();
313325
}
314326
#ifdef Q_OS_MAC
@@ -323,11 +335,13 @@ edit_interface_rep::key_press (string gkey) {
323335
!inside_active_graphics ()) {
324336
archive_state ();
325337
call ("kbd-insert", "<" * key * ">");
338+
cout << "Inserted single character3: " << rew << "\n";
326339
interrupt_shortcut ();
327340
}
328341
else if (N (key) > 1 && !is_combo_shortcuts (key) && key != "escape") {
329342
archive_state ();
330343
call ("insert", key);
344+
cout << "Inserted single character4: " << rew << "\n";
331345
interrupt_shortcut ();
332346
}
333347
else {

0 commit comments

Comments
 (0)