-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqe.c
8090 lines (7140 loc) · 220 KB
/
qe.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* QEmacs, tiny but powerful multimode editor
*
* Copyright (c) 2000-2002 Fabrice Bellard.
* Copyright (c) 2000-2014 Charlie Gordon.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "qe.h"
#ifdef CONFIG_TINY
#undef CONFIG_DLL
#undef CONFIG_ALL_KMAPS
#undef CONFIG_UNICODE_JOIN
#endif
#include "qfribidi.h"
#include "variables.h"
#ifdef CONFIG_DLL
#include <dlfcn.h>
#endif
/* each history list */
typedef struct HistoryEntry {
struct HistoryEntry *next;
StringArray history;
char name[32];
} HistoryEntry;
int debug_flags;
#ifdef CONFIG_INIT_CALLS
static int (*__initcall_first)(void) __init_call = NULL;
static void (*__exitcall_first)(void) __exit_call = NULL;
#endif
static int get_line_height(QEditScreen *screen, int style_index);
void print_at_byte(QEditScreen *screen,
int x, int y, int width, int height,
const char *str, int style_index);
static void get_default_path(EditState *s, char *buf, int buf_size);
static EditBuffer *predict_switch_to_buffer(EditState *s);
static StringArray *get_history(const char *name);
static void qe_key_process(int key);
ModeSavedData *generic_mode_save_data(EditState *s);
static void generic_text_display(EditState *s);
static void display1(DisplayState *s);
#ifndef CONFIG_TINY
static void save_selection(void);
#endif
static CompletionFunc find_completion(const char *name);
QEmacsState qe_state;
/* should handle multiple screens, and multiple sessions */
static QEditScreen global_screen;
static int screen_width = 0;
static int screen_height = 0;
static int no_init_file;
#ifndef CONFIG_TINY
static int free_everything;
#endif
static const char *user_option;
/* mode handling */
void qe_register_mode(ModeDef *m)
{
QEmacsState *qs = &qe_state;
ModeDef **p;
/* register mode in mode list (at end) */
p = &qs->first_mode;
while (*p != NULL)
p = &(*p)->next;
m->next = NULL;
*p = m;
/* add missing functions */
if (!m->mode_init)
m->mode_init = text_mode_init;
if (!m->mode_close)
m->mode_close = text_mode_close;
if (!m->display)
m->display = generic_text_display;
if (!m->mode_save_data)
m->mode_save_data = generic_mode_save_data;
if (!m->data_type)
m->data_type = &raw_data_type;
if (!m->get_mode_line)
m->get_mode_line = text_mode_line;
/* add a new command to switch to that mode */
if (!(m->mode_flags & MODEF_NOCMD)) {
char buf[64];
int size;
CmdDef *def;
/* lower case convert for C mode, Perl... */
qe_strtolower(buf, sizeof(buf) - 10, m->name);
pstrcat(buf, sizeof(buf), "-mode");
size = strlen(buf) + 1;
/* constant immediate string parameter */
size += snprintf(buf + size, sizeof(buf) - size,
"S{%s}", m->name) + 1;
def = qe_mallocz_array(CmdDef, 2);
def->name = qe_malloc_dup(buf, size);
def->key = def->alt_key = KEY_NONE;
def->sig = CMD_ESs;
def->val = 0;
def->action.ESs = do_set_mode;
def[1].val = 1; /* flag as allocated for free-all */
qe_register_cmd_table(def, NULL);
}
}
void mode_completion(CompleteState *cp)
{
QEmacsState *qs = cp->s->qe_state;
ModeDef *m;
for (m = qs->first_mode; m != NULL; m = m->next) {
complete_test(cp, m->name);
}
}
static ModeDef *find_mode(const char *name)
{
QEmacsState *qs = &qe_state;
ModeDef *m;
for (m = qs->first_mode; m != NULL; m = m->next) {
if (strequal(m->name, name))
return m;
}
return NULL;
}
/* commands handling */
CmdDef *qe_find_cmd(const char *cmd_name)
{
QEmacsState *qs = &qe_state;
CmdDef *d;
d = qs->first_cmd;
while (d != NULL) {
while (d->name != NULL) {
if (strequal(cmd_name, d->name))
return d;
d++;
}
d = d->action.next;
}
return NULL;
}
void command_completion(CompleteState *cp)
{
QEmacsState *qs = cp->s->qe_state;
CmdDef *d;
d = qs->first_cmd;
while (d != NULL) {
while (d->name != NULL) {
complete_test(cp, d->name);
d++;
}
d = d->action.next;
}
}
static int qe_register_binding1(unsigned int *keys, int nb_keys,
CmdDef *d, ModeDef *m)
{
QEmacsState *qs = &qe_state;
KeyDef **lp, *p;
int i;
if (!d)
return -1;
/* add key */
p = qe_malloc_hack(KeyDef, (nb_keys - 1) * sizeof(p->keys[0]));
if (!p)
return -1;
p->cmd = d;
p->nb_keys = nb_keys;
for (i = 0; i < nb_keys; i++) {
p->keys[i] = keys[i];
}
lp = m ? &m->first_key : &qs->first_key;
/* Bindings must be prepended to override previous bindings */
#if 0
while (*lp != NULL && (*lp)->mode != NULL)
lp = &(*lp)->next;
#endif
p->next = *lp;
*lp = p;
return 0;
}
/* convert compressed mappings to real ones */
static int qe_register_binding2(int key, CmdDef *d, ModeDef *m)
{
int nb_keys;
unsigned int keys[3];
nb_keys = 0;
if (key >= KEY_CTRLX(0) && key <= KEY_CTRLX(0xff)) {
keys[nb_keys++] = KEY_CTRL('x');
keys[nb_keys++] = key & 0xff;
} else
if (key >= KEY_CTRLXRET(0) && key <= KEY_CTRLXRET(0xff)) {
keys[nb_keys++] = KEY_CTRL('x');
keys[nb_keys++] = KEY_RET;
keys[nb_keys++] = key & 0xff;
} else
if (key >= KEY_CTRLH(0) && key <= KEY_CTRLH(0xff)) {
keys[nb_keys++] = KEY_CTRL('h');
keys[nb_keys++] = key & 0xff;
} else
if (key >= KEY_CTRLC(0) && key <= KEY_CTRLC(0xff)) {
keys[nb_keys++] = KEY_CTRL('c');
keys[nb_keys++] = key & 0xff;
} else {
keys[nb_keys++] = key;
}
return qe_register_binding1(keys, nb_keys, d, m);
}
/* if mode is non NULL, the defined keys are only active in this mode */
void qe_register_cmd_table(CmdDef *cmds, ModeDef *m)
{
QEmacsState *qs = &qe_state;
CmdDef **ld, *d;
/* find last command table */
for (ld = &qs->first_cmd;;) {
d = *ld;
if (d == NULL) {
/* link new command table */
*ld = cmds;
break;
}
if (d == cmds) {
/* Command table already registered, still do the binding
* phase to allow multiple mode bindings.
*/
break;
}
while (d->name != NULL) {
d++;
}
ld = &d->action.next;
}
/* add default bindings */
for (d = cmds; d->name != NULL; d++) {
if (d->key == KEY_CTRL('x') || d->key == KEY_CTRL('c')
|| d->key == KEY_ESC) {
unsigned int keys[2];
keys[0] = d->key;
keys[1] = d->alt_key;
qe_register_binding1(keys, 2, d, m);
} else {
if (d->key != KEY_NONE)
qe_register_binding2(d->key, d, m);
if (d->alt_key != KEY_NONE)
qe_register_binding2(d->alt_key, d, m);
}
}
}
/* key binding handling */
int qe_register_binding(int key, const char *cmd_name, ModeDef *m)
{
return qe_register_binding2(key, qe_find_cmd(cmd_name), m);
}
int qe_mode_set_key(ModeDef *m, const char *keystr, const char *cmd_name)
{
unsigned int keys[MAX_KEYS];
int nb_keys;
nb_keys = strtokeys(keystr, keys, MAX_KEYS);
if (!nb_keys)
return -2;
return qe_register_binding1(keys, nb_keys, qe_find_cmd(cmd_name), m);
}
void do_set_key(EditState *s, const char *keystr,
const char *cmd_name, int local)
{
int res = qe_mode_set_key(local ? s->mode : NULL, keystr, cmd_name);
if (res == -2)
put_status(s, "Invalid keys: %s", keystr);
if (res == -1)
put_status(s, "Invalid command: %s", cmd_name);
}
void do_toggle_control_h(EditState *s, int set)
{
/* Achtung Minen! do_toggle_control_h can be called from tty_init
* with a NULL EditState.
*/
QEmacsState *qs = s ? s->qe_state : &qe_state;
ModeDef *m;
KeyDef *kd;
int i;
if (set)
set = (set > 0);
else
set = !qs->backspace_is_control_h;
if (qs->backspace_is_control_h == set)
return;
qs->backspace_is_control_h = set;
/* CG: This hack in incompatible with support for multiple
* concurrent input consoles.
*/
for (m = qs->first_mode;; m = m->next) {
for (kd = m ? m->first_key : qs->first_key; kd; kd = kd->next) {
for (i = 0; i < kd->nb_keys; i++) {
switch (kd->keys[i]) {
case KEY_CTRL('h'):
kd->keys[i] = set ? KEY_META('h') : KEY_DEL;
break;
case KEY_DEL:
if (set)
kd->keys[i] = KEY_CTRL('h');
break;
case KEY_META('h'):
if (!set)
kd->keys[i] = KEY_CTRL('h');
break;
}
}
}
if (!m)
break;
}
}
void do_set_emulation(EditState *s, const char *name)
{
QEmacsState *qs = s->qe_state;
if (strequal(name, "epsilon")) {
qs->flag_split_window_change_focus = 1;
} else
if (strequal(name, "emacs") || strequal(name, "xemacs")) {
qs->flag_split_window_change_focus = 0;
} else
if (strequal(name, "vi") || strequal(name, "vim")) {
put_status(s, "Emulation '%s' not available yet", name);
} else {
put_status(s, "Unknown emulation '%s'", name);
}
}
void do_set_trace(EditState *s)
{
do_split_window(s, 0);
do_switch_to_buffer(s, "*trace*");
do_previous_window(s);
}
void do_cd(EditState *s, const char *path)
{
char buf[MAX_FILENAME_SIZE];
canonicalize_absolute_path(buf, sizeof(buf), path);
if (chdir(buf)) {
put_status(s, "Cannot change directory to '%s'", buf);
} else {
getcwd(buf, sizeof(buf));
put_status(s, "Current directory: %s", buf);
}
}
/* basic editing functions */
/* CG: should indirect these through mode ! */
void do_bof(EditState *s)
{
s->offset = 0;
}
void do_eof(EditState *s)
{
s->offset = s->b->total_size;
}
void do_bol(EditState *s)
{
if (s->mode->move_bol)
s->mode->move_bol(s);
}
void do_eol(EditState *s)
{
if (s->mode->move_eol)
s->mode->move_eol(s);
}
void do_word_right(EditState *s, int dir)
{
if (s->mode->move_word_left_right)
s->mode->move_word_left_right(s, dir);
}
void text_move_bol(EditState *s)
{
s->offset = eb_goto_bol(s->b, s->offset);
}
void text_move_eol(EditState *s)
{
s->offset = eb_goto_eol(s->b, s->offset);
}
void word_right(EditState *s, int w)
{
int c, offset1;
for (;;) {
if (s->offset >= s->b->total_size)
break;
c = eb_nextc(s->b, s->offset, &offset1);
if (qe_isword(c) == w)
break;
s->offset = offset1;
}
}
void word_left(EditState *s, int w)
{
int c, offset1;
for (;;) {
if (s->offset == 0)
break;
c = eb_prevc(s->b, s->offset, &offset1);
if (qe_isword(c) == w)
break;
s->offset = offset1;
}
}
void text_move_word_left_right(EditState *s, int dir)
{
if (dir > 0) {
word_right(s, 1);
word_right(s, 0);
} else {
word_left(s, 1);
word_left(s, 0);
}
}
void do_mark_region(EditState *s, int mark, int offset)
{
/* CG: Should have local and global mark rings */
s->b->mark = clamp(mark, 0, s->b->total_size);
s->offset = clamp(offset, 0, s->b->total_size);
/* activate region hilite */
if (s->qe_state->hilite_region)
s->region_style = QE_STYLE_REGION_HILITE;
}
/* paragraph handling */
int eb_next_paragraph(EditBuffer *b, int offset)
{
int text_found;
offset = eb_goto_bol(b, offset);
/* find end of paragraph */
text_found = 0;
for (;;) {
if (offset >= b->total_size)
break;
if (eb_is_blank_line(b, offset, NULL)) {
if (text_found)
break;
} else {
text_found = 1;
}
offset = eb_next_line(b, offset);
}
return offset;
}
int eb_start_paragraph(EditBuffer *b, int offset)
{
for (;;) {
offset = eb_goto_bol(b, offset);
if (offset <= 0)
break;
/* check if only spaces */
if (eb_is_blank_line(b, offset, &offset)) {
break;
}
eb_prevc(b, offset, &offset);
}
return offset;
}
void do_mark_paragraph(EditState *s)
{
int start = eb_start_paragraph(s->b, s->offset);
int end = eb_next_paragraph(s->b, s->offset);
do_mark_region(s, start, end);
}
void do_backward_paragraph(EditState *s)
{
int offset;
offset = s->offset;
/* skip empty lines */
for (;;) {
if (offset <= 0)
break;
offset = eb_goto_bol(s->b, offset);
if (!eb_is_blank_line(s->b, offset, NULL))
break;
/* line just before */
eb_prevc(s->b, offset, &offset);
}
offset = eb_start_paragraph(s->b, offset);
/* line just before */
offset = eb_prev_line(s->b, offset);
s->offset = offset;
}
void do_forward_paragraph(EditState *s)
{
s->offset = eb_next_paragraph(s->b, s->offset);
}
void do_kill_paragraph(EditState *s, int dir)
{
int start = s->offset;
if (s->b->flags & BF_READONLY)
return;
if (dir < 0)
do_backward_paragraph(s);
else
do_forward_paragraph(s);
do_kill(s, start, s->offset, dir);
}
void do_fill_paragraph(EditState *s)
{
/* buffer offsets, byte counts */
int par_start, par_end, offset, offset1, chunk_start, word_start;
/* number of characters */
int col, indent_size, word_size, space_size;
/* other counts */
int n, word_count;
/* character */
int c;
/* find start & end of paragraph */
par_start = eb_start_paragraph(s->b, s->offset);
par_end = eb_next_paragraph(s->b, par_start);
/* compute indent size */
indent_size = 0;
offset = eb_next_line(s->b, par_start);
if (!eb_is_blank_line(s->b, offset, NULL)) {
while (offset < par_end) {
c = eb_nextc(s->b, offset, &offset);
if (!qe_isblank(c))
break;
indent_size++;
}
}
/* suppress any spaces in between */
col = 0;
offset = par_start;
word_count = 0;
while (offset < par_end) {
/* skip spaces */
chunk_start = offset;
space_size = 0;
while (offset < par_end) {
c = eb_nextc(s->b, offset, &offset1);
if (!qe_isspace(c))
break;
offset = offset1;
space_size++;
}
/* skip word */
word_start = offset;
word_size = 0;
while (offset < par_end) {
c = eb_nextc(s->b, offset, &offset1);
if (qe_isspace(c))
break;
offset = offset1;
word_size++;
}
if (word_count == 0) {
/* first word: preserve spaces */
col += space_size + word_size;
} else {
/* insert space single space the word */
if (offset == par_end
|| (col + 1 + word_size > s->b->fill_column)) {
eb_delete_uchar(s->b, chunk_start);
chunk_start += eb_insert_uchar(s->b, chunk_start, '\n');
if (offset < par_end) {
/* indent */
int nb = eb_insert_spaces(s->b, offset, indent_size);
chunk_start += nb;
word_start += nb;
offset += nb;
par_end += nb;
}
col = word_size + indent_size;
} else {
eb_delete_uchar(s->b, chunk_start);
chunk_start += eb_insert_uchar(s->b, chunk_start, ' ');
col += 1 + word_size;
}
/* remove all other spaces if needed */
n = word_start - chunk_start;
if (n > 0) {
eb_delete(s->b, chunk_start, n);
offset -= n;
par_end -= n;
}
}
word_count++;
}
}
/* Upper / lower / capital case functions. Update offset, return isword */
/* arg: -1=lower-case, +1=upper-case, +2=capital-case */
static int eb_changecase(EditBuffer *b, int *offsetp, int arg)
{
int offset0, ch, ch1, len;
char buf[MAX_CHAR_BYTES];
offset0 = *offsetp;
ch = eb_nextc(b, offset0, offsetp);
if (!qe_isword(ch))
return 0;
if (arg > 0)
ch1 = qe_toupper(ch);
else
ch1 = qe_tolower(ch);
if (ch != ch1) {
len = eb_encode_uchar(b, buf, ch1);
eb_replace(b, offset0, *offsetp - offset0, buf, len);
*offsetp = offset0 + len;
}
return 1;
}
void do_changecase_word(EditState *s, int arg)
{
int offset;
word_right(s, 1);
for (offset = s->offset;;) {
if (offset >= s->b->total_size)
break;
if (!eb_changecase(s->b, &offset, arg))
break;
s->offset = offset;
if (arg == 2)
arg = -2;
}
}
void do_changecase_region(EditState *s, int arg)
{
int offset;
/* deactivate region hilite */
s->region_style = 0;
/* WARNING: during case change, the region offsets can change, so
it is not so simple ! */
/* XXX: if last char of region changes width, offset will move */
offset = min(s->offset, s->b->mark);
for (;;) {
if (offset >= max(s->offset, s->b->mark))
break;
if (eb_changecase(s->b, &offset, arg)) {
if (arg == 2)
arg = -arg;
} else {
if (arg == -2)
arg = -arg;
}
}
}
void do_delete_char(EditState *s, int argval)
{
int endpos, i;
if (s->b->flags & BF_READONLY)
return;
if (argval == NO_ARG) {
if (s->qe_state->last_cmd_func != (CmdFunc)do_append_next_kill) {
eb_delete_uchar(s->b, s->offset);
return;
}
argval = 1;
}
/* save kill if numeric argument given */
endpos = s->offset;
for (i = argval; i > 0 && endpos < s->b->total_size; i--) {
eb_nextc(s->b, endpos, &endpos);
}
for (i = argval; i < 0 && endpos > 0; i++) {
eb_prevc(s->b, endpos, &endpos);
}
do_kill(s, s->offset, endpos, argval);
}
void do_backspace(EditState *s, int argval)
{
int offset1;
if (s->b->flags & BF_READONLY) {
/* CG: could scroll down */
return;
}
/* XXX: Should delete hilighted region */
/* deactivate region hilite */
s->region_style = 0;
if (argval == NO_ARG) {
if (s->qe_state->last_cmd_func == (CmdFunc)do_tab
&& !s->indent_tabs_mode) {
/* Delete tab or indentation? */
do_undo(s);
return;
}
if (s->qe_state->last_cmd_func != (CmdFunc)do_append_next_kill) {
eb_prevc(s->b, s->offset, &offset1);
if (offset1 < s->offset) {
eb_delete_range(s->b, offset1, s->offset);
/* special case for composing */
if (s->compose_len > 0)
s->compose_len--;
}
return;
}
argval = 1;
}
/* save kill if numeric argument given */
do_delete_char(s, -argval);
}
/* return the cursor position relative to the screen. Note that xc is
given in pixel coordinates */
typedef struct {
int linec;
int yc;
int xc;
int offsetc;
DirType basec; /* direction of the line */
DirType dirc; /* direction of the char under the cursor */
int cursor_width; /* can be negative depending on char orientation */
int cursor_height;
} CursorContext;
int cursor_func(DisplayState *ds,
int offset1, int offset2, int line_num,
int x, int y, int w, int h, __unused__ int hex_mode)
{
CursorContext *m = ds->cursor_opaque;
if (m->offsetc >= offset1 && m->offsetc < offset2) {
m->xc = x;
m->yc = y;
m->basec = ds->base;
m->dirc = ds->base; /* XXX: do it */
m->cursor_width = w;
m->cursor_height = h;
m->linec = line_num;
#if 0
printf("cursor_func: xc=%d yc=%d linec=%d offset: %d<=%d<%d\n",
m->xc, m->yc, m->linec, offset1, m->offsetc, offset2);
#endif
return -1;
} else {
return 0;
}
}
static void get_cursor_pos(EditState *s, CursorContext *m)
{
DisplayState ds1, *ds = &ds1;
display_init(ds, s, DISP_CURSOR);
ds->cursor_opaque = m;
ds->cursor_func = cursor_func;
memset(m, 0, sizeof(*m));
m->offsetc = s->offset;
m->xc = m->yc = NO_CURSOR;
display1(ds);
}
typedef struct {
int yd;
int xd;
int xdmin;
int offsetd;
} MoveContext;
/* called each time the cursor could be displayed */
static int down_cursor_func(DisplayState *ds,
int offset1, __unused__ int offset2, int line_num,
int x, __unused__ int y,
__unused__ int w, __unused__ int h,
__unused__ int hex_mode)
{
int d;
MoveContext *m = ds->cursor_opaque;
if (line_num == m->yd) {
/* find the closest char */
d = abs(x - m->xd);
if (d < m->xdmin) {
m->xdmin = d;
m->offsetd = offset1;
}
return 0;
} else if (line_num > m->yd) {
/* no need to explore more chars */
return -1;
} else {
return 0;
}
}
void do_up_down(EditState *s, int dir)
{
if (s->mode->move_up_down)
s->mode->move_up_down(s, dir);
}
void do_left_right(EditState *s, int dir)
{
if (s->mode->move_left_right)
s->mode->move_left_right(s, dir);
}
/* CG: Should move this to EditState */
static int up_down_last_x = -1;
void text_move_up_down(EditState *s, int dir)
{
MoveContext m1, *m = &m1;
DisplayState ds1, *ds = &ds1;
CursorContext cm;
if (s->qe_state->last_cmd_func != (CmdFunc)do_up_down)
up_down_last_x = -1;
get_cursor_pos(s, &cm);
if (cm.xc == NO_CURSOR)
return;
if (up_down_last_x == -1)
up_down_last_x = cm.xc;
if (dir < 0) {
/* difficult case: we need to go backward on displayed text */
while (cm.linec <= 0) {
int offset_top = s->offset_top;
if (offset_top <= 0)
return;
eb_prevc(s->b, offset_top, &offset_top);
s->offset_top = s->mode->text_backward_offset(s, offset_top);
/* adjust y_disp so that the cursor is at the same position */
s->y_disp += cm.yc;
get_cursor_pos(s, &cm);
s->y_disp -= cm.yc;
}
}
/* find cursor offset */
m->yd = cm.linec + dir;
m->xd = up_down_last_x;
m->xdmin = 0x7fffffff;
/* if no cursor position is found, we go to bof or eof according
to dir */
if (dir > 0)
m->offsetd = s->b->total_size;
else
m->offsetd = 0;
display_init(ds, s, DISP_CURSOR);
ds->cursor_opaque = m;
ds->cursor_func = down_cursor_func;
display1(ds);
s->offset = m->offsetd;
}
typedef struct {
int y_found;
int offset_found;
int dir;
int offsetc;
} ScrollContext;
/* called each time the cursor could be displayed */
static int scroll_cursor_func(DisplayState *ds,
int offset1, int offset2,
__unused__ int line_num,
__unused__ int x, int y,
__unused__ int w, int h,
__unused__ int hex_mode)
{
ScrollContext *m = ds->cursor_opaque;
int y1;
y1 = y + h;
/* XXX: add bidir handling : position cursor on left / right */
if (m->dir < 0) {
if (y >= 0 && y < m->y_found) {
m->y_found = y;
m->offset_found = offset1;
}
} else {
if (y1 <= ds->height && y1 > m->y_found) {
m->y_found = y1;
m->offset_found = offset1;
}
}
if (m->offsetc >= offset1 && m->offsetc < offset2 &&
y >= 0 && y1 <= ds->height) {
m->offset_found = m->offsetc;
m->y_found = 0x7fffffff * m->dir; /* ensure that no other
position will be found */
return -1;
}
return 0;
}
void do_scroll_left_right(EditState *s, int dir)
{
/* XXX: should change x_disp by space_width increments */
s->x_disp[0] += dir;
}
void do_scroll_up_down(EditState *s, int dir)
{
if (s->mode->scroll_up_down)
s->mode->scroll_up_down(s, dir);
}