-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqe.c
6782 lines (6020 loc) · 179 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, 2001, 2002 Fabrice Bellard.
*
* 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"
#include "qfribidi.h"
#ifdef CONFIG_DLL
#define _GNU_SOURCE
#include <dlfcn.h>
#endif
/* each history list */
typedef struct HistoryEntry {
struct HistoryEntry *next;
StringArray history;
char name[32];
} HistoryEntry;
int (*__initcall_first)(void) __init_call = NULL;
void (*__exitcall_first)(void) __exit_call = NULL;
extern int (*__initcall_modules_end)(void);
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 do_cmd_set_mode(EditState *s, const char *name);
void do_delete_window(EditState *s, int force);
void do_end_macro(EditState *s);
void center_cursor(EditState *s);
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);
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;
static ModeDef *first_mode = NULL;
static KeyDef *first_key = NULL;
static CmdDef *first_cmd = NULL;
static CompletionEntry *first_completion = NULL;
static HistoryEntry *first_history = NULL;
static QEditScreen global_screen;
static int screen_width = 0;
static int screen_height = 0;
#define DEFAULT_INDENT_SIZE 8
#define DEFAULT_TAB_SIZE 8
#define DEFAULT_INDENT_TABS_MODE 1
#define DEFAULT_LINE_NUM_MODE 0
#define DEFAULT_HIGHLIGHT_OVER_MARGIN 0
#define DEFAULT_MARGIN_SIZE 80
int g_indent_size = DEFAULT_INDENT_SIZE;
int g_tab_size = DEFAULT_TAB_SIZE;
int g_indent_tabs_mode = DEFAULT_INDENT_TABS_MODE;
int g_line_num_mode = DEFAULT_LINE_NUM_MODE;
int g_highlight_over_margin = DEFAULT_HIGHLIGHT_OVER_MARGIN;
int g_margin_size = DEFAULT_MARGIN_SIZE;
char g_backup_dir[PATH_MAX];
/* mode handling */
void qe_register_mode(ModeDef *m)
{
ModeDef **p;
CmdDef *table;
/* record mode in mode list */
p = &first_mode;
while (*p != NULL) p = &(*p)->next;
m->next = NULL;
*p = m;
/* add missing functions */
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->mode_line)
m->mode_line = text_mode_line;
/* add a new command to switch to that mode */
if (!(m->mode_flags & MODEF_NOCMD)) {
char buf[64], *args, *name;
int size;
table = malloc(sizeof(CmdDef) * 2);
memset(table, 0, sizeof(CmdDef) * 2);
table->key = KEY_NONE;
table->alt_key = KEY_NONE;
/* lower case convert for C mode */
pstrcpy(buf, sizeof(buf) - 10, m->name);
css_strtolower(buf, sizeof(buf));
pstrcat(buf, sizeof(buf) - 10, "-mode");
args = buf + strlen(buf) + 1;
strcpy(args, "S"); /* constant string parameter */
size = strlen(args) + strlen(buf) + 2;
name = malloc(size);
memcpy(name, buf, size);
table->name = name;
table->action.func = (void*)do_cmd_set_mode;
table->val = strdup(m->name);
qe_register_cmd_table(table, NULL);
}
}
static ModeDef *find_mode(const char *mode_name)
{
ModeDef *p;
p = first_mode;
while (p != NULL) {
if (!strcmp(mode_name, p->name))
return p;
p = p->next;
}
return NULL;
}
/* commands handling */
CmdDef *qe_find_cmd(const char *cmd_name)
{
CmdDef *d;
d = first_cmd;
while (d != NULL) {
while (d->name != NULL) {
if (!strcmp(cmd_name, d->name))
return d;
d++;
}
d = d->action.next;
}
return NULL;
}
static int qe_register_binding1(unsigned int *keys, int nb_keys,
CmdDef *d, ModeDef *m)
{
KeyDef **lp, *p;
/* add key */
p = malloc(sizeof(KeyDef) + (nb_keys - 1) * sizeof(unsigned int));
if (!p)
return -1;
p->nb_keys = nb_keys;
p->cmd = d;
p->mode = m;
memcpy(p->keys, keys, nb_keys * sizeof(unsigned int));
/* find position : mode keys should be before generic keys */
if (m == NULL) {
lp = &first_key;
while (*lp != NULL) lp = &(*lp)->next;
*lp = p;
p->next = NULL;
} else {
p->next = first_key;
first_key = p;
}
return 0;
}
/* convert compressed mappings to real ones */
static void 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 {
keys[nb_keys++] = key;
}
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, const char *mode)
{
CmdDef **ld, *d;
ModeDef *m;
m = NULL;
if (mode)
m = find_mode(mode);
/* find last command table */
ld = &first_cmd;
while (*ld != NULL) {
d = *ld;
while (d->name != NULL) {
d++;
}
ld = &d->action.next;
}
/* add new command table */
*ld = cmds;
/* add default bindings */
d = cmds;
while (d->name != NULL) {
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);
d++;
}
}
/* key binding handling */
void qe_register_binding(int key, const char *cmd_name, const char *mode_names)
{
CmdDef *d;
ModeDef *m;
const char *p, *r;
char mode_name[64];
d = qe_find_cmd(cmd_name);
if (!d)
return;
if (!mode_names || mode_names[0] == '\0') {
qe_register_binding2(key, d, NULL);
} else {
p = mode_names;
for(;;) {
r = strchr(p, '|');
/* XXX: overflows */
if (!r) {
strcpy(mode_name, p);
} else {
memcpy(mode_name, p, r - p);
mode_name[r - p] = '\0';
}
m = find_mode(mode_name);
if (m) {
qe_register_binding2(key, d, m);
}
if (!r)
break;
p = r + 1;
}
}
}
void command_completion(StringArray *cs, const char *input)
{
int count, len;
CmdDef *d;
count = 0;
(void)count;
len = strlen(input);
d = first_cmd;
while (d != NULL) {
while (d->name != NULL) {
if (!strncmp(d->name, input, len))
add_string(cs, d->name);
d++;
}
d = d->action.next;
}
}
#define MAX_KEYS 10
void do_global_set_key(EditState *s, const char *keystr, const char *cmd_name)
{
int key, nb_keys;
unsigned int keys[MAX_KEYS];
const char *p;
CmdDef *d;
p = keystr;
nb_keys = 0;
for(;;) {
skip_spaces(&p);
if (*p == '\0')
break;
key = strtokey(&p);
keys[nb_keys++] = key;
if (nb_keys >= MAX_KEYS)
break;
}
d = qe_find_cmd(cmd_name);
if (!d)
return;
qe_register_binding1(keys, nb_keys, d, NULL);
}
/* basic editing functions */
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)
{
int c, offset1;
for(;;) {
if (s->offset <= 0)
break;
c = eb_prevc(s->b, s->offset, &offset1);
if (c == '\n')
break;
s->offset = offset1;
}
}
void text_move_eol(EditState *s)
{
int c, offset1;
for(;;) {
c = eb_nextc(s->b, s->offset, &offset1);
if (c == '\n')
break;
s->offset = offset1;
}
}
int isword(int c)
{
/* XXX: any unicode char >= 128 is considered as word. */
return (c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') ||
(c == '_') || (c >= 128);
}
static 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 (isword(c) == w)
break;
s->offset = offset1;
}
}
static 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 (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);
}
}
/* 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_empty_line(b, offset)) {
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_empty_line(b, offset)) {
offset = eb_next_line(b, offset);
break;
}
eb_prevc(b, offset, &offset);
}
return offset;
}
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_empty_line(s->b, offset))
break;
/* line just before */
eb_prevc(s->b, offset, &offset);
}
offset = eb_start_paragraph(s->b, offset);
/* line just before */
eb_prevc(s->b, offset, &offset);
offset = eb_goto_bol(s->b, offset);
s->offset = offset;
}
void do_forward_paragraph(EditState *s)
{
s->offset = eb_next_paragraph(s->b, s->offset);
}
#define PARAGRAPH_WIDTH 76
void do_fill_paragraph(EditState *s)
{
int par_start, par_end, col;
int offset, offset1, n, c, line_count, indent_size;
int chunk_start, word_start, word_size, word_count, space_size;
unsigned char buf[1];
(void)line_count;
/* 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_empty_line(s->b, offset)) {
while (offset < par_end) {
c = eb_nextc(s->b, offset, &offset);
if (!isspace(c))
break;
indent_size++;
}
}
/* suppress any spaces in between */
col = 0;
offset = par_start;
word_count = 0;
line_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 (!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 (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 then word */
if (offset == par_end ||
(col + 1 + word_size > PARAGRAPH_WIDTH)) {
buf[0] = '\n';
eb_write(s->b, chunk_start, buf, 1);
chunk_start++;
if (offset < par_end) {
/* indent */
buf[0] = ' ';
for(n = indent_size; n > 0; n--)
eb_insert(s->b, chunk_start, buf, 1);
chunk_start += indent_size;
word_start += indent_size;
offset += indent_size;
par_end += indent_size;
}
col = word_size + indent_size;
} else {
buf[0] = ' ';
eb_write(s->b, chunk_start, buf, 1);
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 case functions (XXX: use generic unicode
function). Return next offset */
static int eb_changecase(EditBuffer *b, int offset, int up)
{
int offset1, ch, len;
unsigned char buf[MAX_CHAR_BYTES];
ch = eb_nextc(b, offset, &offset1);
if (ch < 128) {
if (up)
ch = toupper(ch);
else
ch = tolower(ch);
}
len = unicode_to_charset(buf, ch, b->charset);
if (len == (offset1 - offset)) {
eb_write(b, offset, buf, len);
} else {
eb_delete(b, offset, offset1 - offset);
eb_insert(b, offset, buf, len);
}
return offset + len;
}
/* XXX: only ascii */
char *do_read_word_at_offset(EditState *s)
{
int c, offset1, o_offs, lw;
char word[256];
o_offs = s->offset;
lw = 0;
memset(word, 0, sizeof(word));
/* move to the beginning of the word first */
for(;;) {
if (s->offset == 0) {
s->offset = o_offs;
break;
}
c = eb_prevc(s->b, s->offset, &offset1);
if (!isword(c))
break;
s->offset = offset1;
}
for (;;) {
if (s->offset >= s->b->total_size)
break;
c = eb_nextc(s->b, s->offset, &offset1);
if (!isword(c)) {
s->offset = o_offs;
break;
}
s->offset = offset1;
if (lw < sizeof(word))
word[lw] = c;
lw++;
}
if (lw < sizeof(word))
word[lw] = '\0';
s->offset = o_offs;
return strdup(word);
}
void do_changecase_word(EditState *s, int up)
{
int c;
word_right(s, 1);
for(;;) {
if (s->offset >= s->b->total_size)
break;
c = eb_nextc(s->b, s->offset, NULL);
if (!isword(c))
break;
s->offset = eb_changecase(s->b, s->offset, up);
}
}
void do_changecase_region(EditState *s, int up)
{
int offset;
/* WARNING: during case change, the region offsets can change, so
it is not so simple ! */
if (s->offset > s->b->mark)
offset = s->b->mark;
else
offset = s->offset;
for(;;) {
if (s->offset > s->b->mark) {
if (offset >= s->offset)
break;
} else {
if (offset >= s->b->mark)
break;
}
offset = eb_changecase(s->b, offset, up);
}
}
void do_delete_word(EditState *s, int dir)
{
int start = s->offset;
int end;
if (s->b->flags & BF_READONLY)
return;
if (s->mode->move_word_left_right)
s->mode->move_word_left_right(s, dir);
end = s->offset;
if (start < end)
eb_delete(s->b, start, end-start);
else
eb_delete(s->b, end, start-end);
}
void do_delete_char(EditState *s)
{
int offset1;
eb_nextc(s->b, s->offset, &offset1);
eb_delete(s->b, s->offset, offset1 - s->offset);
}
void do_delete_trailing_whitespace(EditState *s)
{
int c, offset1, i, nr_spaces, twl = 0, tline_num, tcol_num;
char status[128];
/* move to begining of file */
do_eof(s);
eb_get_pos(s->b, &tline_num, &tcol_num, s->offset);
do_bof(s);
for (i = 0; i < tline_num; i++) {
do_goto_line(s, i);
nr_spaces = 0;
/* goto end of line and start backtrack */
do_eol(s);
for (;;) {
c = eb_prevc(s->b, s->offset, &offset1);
if (!isspace(c) || c == '\n')
break;
s->offset = offset1;
nr_spaces++;
}
if (nr_spaces > 0) {
eb_delete(s->b, s->offset, nr_spaces);
twl++;
}
}
if (twl > 0)
snprintf(status, sizeof(status), "Deleted trailing whitespaces from %d line(s)", twl);
else
snprintf(status, sizeof(status), "No trailing whitespaces");
put_status(s, status);
}
/* XXX: can do a little better */
void do_match_parenthesis(EditState *s)
{
int offset1, initial_offset, c;
int this_paren, other_paren;
unsigned char r;
typedef int (*_eb_getchar)(EditBuffer *s, int offset, int *ptr);
_eb_getchar get_char;
int nest = 1, fw = 0;
initial_offset = s->offset;
eb_read(s->b, s->offset, &r, sizeof(r));
this_paren = (int)r;
if (this_paren == '{' || this_paren == '(' || this_paren == '[') {
if (this_paren == '(') {
other_paren = this_paren + 1;
} else {
other_paren = this_paren + 2;
}
get_char = eb_nextc;
s->offset++;
fw = 1;
} else if (this_paren == '}' || this_paren == ')' || this_paren == ']') {
if (this_paren == ')') {
other_paren = this_paren - 1;
} else {
other_paren = this_paren - 2;
}
get_char = eb_prevc;
} else
/* if its not a parenthesis nothing to do */
return;
for (;;) {
c = get_char(s->b, s->offset, &offset1);
if (c == this_paren) {
nest++;
} else if (c == other_paren) {
nest--;
if (nest <= 0) {
if (nest == 0 && !fw)
s->offset = offset1;
else if (nest < 0)
s->offset = initial_offset;
break;
}
}
if (offset1 <= 0 || offset1 >= s->b->total_size) {
s->offset = initial_offset;
nest = -1;
break;
}
s->offset = offset1;
}
if (nest == 0) {
do_refresh(s);
} else {
put_status(s, "No matching parenthesis!");
}
}
void do_backspace(EditState *s)
{
int offset1;
eb_prevc(s->b, s->offset, &offset1);
if (offset1 < s->offset) {
eb_delete(s->b, offset1, s->offset - offset1);
s->offset = offset1;
/* special case for composing */
if (s->compose_len > 0)
s->compose_len--;
}
}
void do_transpose_char(EditState *s)
{
int offset1;
u8 ch[2], t;
int o_offs = s->offset;
eb_prevc(s->b, s->offset, &offset1);
eb_read(s->b, offset1, &ch[0], sizeof(ch));
if (offset1 < s->offset) {
t = ch[0];
ch[0] = ch[1];
ch[1] = t;
eb_delete(s->b, offset1, (s->offset - offset1+1));
eb_insert(s->b, s->offset, ch, sizeof(ch));
s->offset = o_offs;
do_refresh(s);
}
}
/* 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, 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;
}
}
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;
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, int offset2, int line_num,
int x, int y, int w, int h, 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);
}
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 != 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) {
if (s->offset_top <= 0)
return;
s->offset_top = s->mode->text_backward_offset(s, s->offset_top - 1);
/* 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;