-
Notifications
You must be signed in to change notification settings - Fork 0
/
extras.c
1986 lines (1781 loc) · 59.8 KB
/
extras.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, extra commands non full version
*
* Copyright (c) 2000-2020 Charlie Gordon.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <time.h>
#include "qe.h"
#include "qfribidi.h"
#include "variables.h"
static int qe_skip_comments(EditState *s, int offset, int *offsetp)
{
unsigned int buf[COLORED_MAX_LINE_SIZE];
QETermStyle sbuf[COLORED_MAX_LINE_SIZE];
int line_num, col_num, len, pos;
int offset0, offset1;
if (!s->colorize_func && !s->b->b_styles)
return 0;
eb_get_pos(s->b, &line_num, &col_num, offset);
offset0 = eb_goto_bol2(s->b, offset, &pos);
len = get_colorized_line(s, buf, countof(buf), sbuf,
offset0, &offset1, line_num);
if (len > countof(buf))
len = countof(buf);
if (pos >= len)
return 0;
if (sbuf[pos] != QE_STYLE_COMMENT)
return 0;
while (pos < len && sbuf[pos] == QE_STYLE_COMMENT) {
offset = eb_next(s->b, offset);
pos++;
}
*offsetp = offset;
return 1;
}
static int qe_skip_spaces(EditState *s, int offset, int *offsetp)
{
int offset0 = offset, offset1;
while (offset < s->b->total_size
&& qe_isspace(eb_nextc(s->b, offset, &offset1))) {
offset = offset1;
}
if (offset != offset0) {
*offsetp = offset;
return 1;
}
return 0;
}
static void compare_resync(EditState *s1, EditState *s2,
int save1, int save2,
int *offset1_ptr, int *offset2_ptr)
{
int pos1, off1, pos2, off2;
int ch1, ch2;
off1 = save1;
off2 = save2;
/* try skipping blanks */
while (qe_isblank(ch1 = eb_nextc(s1->b, pos1 = off1, &off1)))
continue;
while (qe_isblank(ch2 = eb_nextc(s2->b, pos2 = off2, &off2)))
continue;
/* XXX: should try and detect a simple insertion first
by comparing from the end of both lines */
if (ch1 != ch2) {
/* try skipping current words and subsequent blanks */
off1 = pos1;
off2 = pos2;
while (!qe_isspace(ch1 = eb_nextc(s1->b, pos1 = off1, &off1)))
continue;
while (!qe_isspace(ch2 = eb_nextc(s2->b, pos2 = off2, &off2)))
continue;
while (qe_isblank(ch1 = eb_nextc(s1->b, pos1 = off1, &off1)))
continue;
while (qe_isblank(ch2 = eb_nextc(s2->b, pos2 = off2, &off2)))
continue;
if (ch1 != ch2) {
/* Try to resync from end of line */
pos1 = eb_goto_eol(s1->b, save1);
pos2 = eb_goto_eol(s2->b, save2);
}
}
while (pos1 > save1 && pos2 > save2
&& eb_prevc(s1->b, pos1, &off1) == eb_prevc(s2->b, pos2, &off2)) {
pos1 = off1;
pos2 = off2;
}
*offset1_ptr = pos1;
*offset2_ptr = pos2;
}
void do_compare_windows(EditState *s, int argval)
{
QEmacsState *qs = s->qe_state;
EditState *s1;
EditState *s2;
int offset1, offset2, size1, size2, ch1, ch2;
int tries, resync = 0;
char buf1[MAX_CHAR_BYTES + 2], buf2[MAX_CHAR_BYTES + 2];
const char *comment = "";
s1 = s;
/* Should use same internal function as for next_window */
for (s2 = s1;;) {
s2 = s2->next_window;
if (s2 == NULL)
s2 = qs->first_window;
if (s2 == s1) {
/* single window: bail out */
return;
}
if (s2->b->flags & BF_DIRED)
continue;
break;
}
if (argval != NO_ARG) {
if (argval & 4)
qs->ignore_spaces ^= 1;
if (argval & 16)
qs->ignore_comments ^= 1;
}
size1 = s1->b->total_size;
size2 = s2->b->total_size;
if (qs->last_cmd_func == (CmdFunc)do_compare_windows) {
resync = 1;
}
for (tries = 0;; resync = 0) {
if (++tries >= 100000) {
tries = 0;
//if (check_input())
// return;
}
if (s1->offset >= size1) {
offset1 = s1->offset;
ch1 = EOF;
} else {
ch1 = eb_nextc(s1->b, s1->offset, &offset1);
}
if (s2->offset >= size2) {
offset2 = s2->offset;
ch2 = EOF;
} else {
ch2 = eb_nextc(s2->b, s2->offset, &offset2);
}
if (ch1 != ch2) {
if (qs->ignore_spaces) {
/* UTF-8 issue */
if (qe_isspace(ch1) || qe_isspace(ch2)) {
qe_skip_spaces(s1, s1->offset, &s1->offset);
qe_skip_spaces(s2, s2->offset, &s2->offset);
if (!*comment)
comment = "Skipped spaces, ";
continue;
}
}
if (qs->ignore_comments) {
if (qe_skip_comments(s1, s1->offset, &s1->offset) |
qe_skip_comments(s2, s2->offset, &s2->offset)) {
comment = "Skipped comments, ";
continue;
}
}
if (ch1 == EOF || ch2 == EOF) {
put_status(s, "%sExtra characters", comment);
break;
}
if (resync) {
int save1 = s1->offset, save2 = s2->offset;
compare_resync(s1, s2, save1, save2, &s1->offset, &s2->offset);
put_status(s, "Skipped %d and %d bytes",
s1->offset - save1, s2->offset - save2);
break;
}
put_status(s, "%sDifference: '%s' [0x%02X] <-> '%s' [0x%02X]", comment,
utf8_char_to_string(buf1, ch1), ch1,
utf8_char_to_string(buf2, ch2), ch2);
break;
}
if (ch1 != EOF) {
s1->offset = offset1;
s2->offset = offset2;
continue;
}
put_status(s, "%sNo difference", comment);
break;
}
}
void do_compare_files(EditState *s, const char *filename, int bflags)
{
char buf[MAX_FILENAME_SIZE];
char dir[MAX_FILENAME_SIZE];
int pathlen, parent_pathlen;
const char *tail;
EditState *e;
pathlen = get_basename_offset(filename);
get_default_path(s->b, s->offset, dir, sizeof(dir));
if (strstart(filename, dir, &tail)) {
snprintf(buf, sizeof(buf), "%s../%s", dir, tail);
} else
if (pathlen == 0) {
snprintf(buf, sizeof(buf), "../%s", filename);
} else
if (pathlen == 1) {
put_status(s, "Reference file is in root directory: %s", filename);
return;
} else
if (pathlen >= MAX_FILENAME_SIZE) {
put_status(s, "Filename too long: %s", filename);
return;
} else {
pstrcpy(buf, sizeof(buf), filename);
buf[pathlen - 1] = '\0'; /* overwite the path separator */
parent_pathlen = get_basename_offset(buf);
pstrcpy(buf + parent_pathlen, sizeof(buf) - parent_pathlen, filename + pathlen);
}
// XXX: should check for regular file
if (access(filename, R_OK)) {
put_status(s, "Cannot access file %s: %s",
filename, strerror(errno));
return;
}
if (access(buf, R_OK)) {
put_status(s, "Cannot access file %s: %s",
buf, strerror(errno));
return;
}
do_find_file(s, filename, bflags);
do_delete_other_windows(s, 0);
e = qe_split_window(s, 50, SW_STACKED);
if (e) {
s->qe_state->active_window = e;
do_find_file(e, buf, bflags);
}
}
void do_delete_horizontal_space(EditState *s)
{
int from, to, offset;
/* boundary check unnecessary because eb_prevc returns '\n'
* at bof and eof and qe_isblank return true only on SPC and TAB.
*/
from = to = s->offset;
while (qe_isblank(eb_prevc(s->b, from, &offset)))
from = offset;
while (qe_isblank(eb_nextc(s->b, to, &offset)))
to = offset;
eb_delete_range(s->b, from, to);
}
static void do_delete_blank_lines(EditState *s)
{
/* Delete blank lines:
* On blank line, delete all surrounding blank lines, leaving just one.
* On isolated blank line, delete that one.
* On nonblank line, delete any immediately following blank lines.
*/
/* XXX: should simplify */
int from, offset, offset0, offset1, all = 0;
EditBuffer *b = s->b;
offset = eb_goto_bol(b, s->offset);
if (eb_is_blank_line(b, offset, &offset1)) {
if ((offset == 0 || !eb_is_blank_line(b,
eb_prev_line(b, offset), NULL))
&& (offset1 >= b->total_size || !eb_is_blank_line(b,
offset1, NULL))) {
all = 1;
}
} else {
offset = eb_next_paragraph(b, offset);
all = 1;
}
from = offset;
while (from > 0) {
offset0 = eb_prev_line(b, from);
if (!eb_is_blank_line(b, offset0, NULL))
break;
from = offset0;
}
if (!all) {
eb_delete_range(b, from, offset);
/* Keep current blank line */
from = offset = eb_next_line(b, from);
}
while (offset < s->b->total_size) {
if (!eb_is_blank_line(b, offset, &offset))
break;
}
eb_delete_range(b, from, offset);
}
static void eb_tabify(EditBuffer *b, int p1, int p2)
{
/* We implement a complete analysis of the region instead of
* scanning for certain space patterns (such as / [ \t]/). It is
* fast enough, sometimes even faster, more concise and more
* correct for pathological case where initial space falls on tab
* column.
*/
/* XXX: should extend for language modes to not replace spaces
* inside character constants, strings, regex, comments,
* preprocessor, etc. Implementation is not too difficult with a
* new buffer reader eb_nextc_style() using colorizer and a
* one line cache.
*/
int tw = b->tab_width > 0 ? b->tab_width : 8;
int start = max(0, min(p1, p2));
int stop = min(b->total_size, max(p1, p2));
int col;
int offset, offset1, offset2, delta;
col = 0;
offset = eb_goto_bol(b, start);
for (; offset < stop; offset = offset1) {
int c = eb_nextc(b, offset, &offset1);
if (c == '\r' || c == '\n') {
col = 0;
continue;
}
if (c == '\t') {
col += tw - col % tw;
continue;
}
col += unicode_tty_glyph_width(c);
if (c != ' ' || offset < start || col % tw == 0)
continue;
while (offset1 < stop) {
c = eb_nextc(b, offset1, &offset2);
if (c == ' ') {
col += unicode_tty_glyph_width(c);
offset1 = offset2;
if (col % tw == 0) {
delta = eb_delete_range(b, offset, offset1);
delta = eb_insert_uchar(b, offset, '\t') - delta;
offset1 += delta;
stop += delta;
break;
}
continue;
} else
if (c == '\t') {
col += tw - col % tw;
delta = -eb_delete_range(b, offset, offset1);
offset1 = offset2;
offset1 += delta;
stop += delta;
}
break;
}
}
}
static void do_tabify_buffer(EditState *s)
{
/* deactivate region hilite */
s->region_style = 0;
eb_tabify(s->b, 0, s->b->total_size);
}
static void do_tabify_region(EditState *s)
{
/* deactivate region hilite */
s->region_style = 0;
eb_tabify(s->b, s->b->mark, s->offset);
}
static void eb_untabify(EditBuffer *b, int p1, int p2)
{
/* We implement a complete analysis of the region instead of
* potentially faster scan for '\t'. It is fast enough and even
* faster if there are lots of tabs.
*/
int tw = b->tab_width > 0 ? b->tab_width : 8;
int start = max(0, min(p1, p2));
int stop = min(b->total_size, max(p1, p2));
int col, col0;
int offset, offset1, offset2, delta;
col = 0;
offset = eb_goto_bol(b, start);
for (; offset < stop; offset = offset1) {
int c = eb_nextc(b, offset, &offset1);
if (c == '\r' || c == '\n') {
col = 0;
continue;
}
if (c != '\t') {
col += unicode_tty_glyph_width(c);
continue;
}
col0 = col;
col += tw - col % tw;
if (offset < start)
continue;
while (eb_nextc(b, offset1, &offset2) == '\t') {
col += tw;
offset1 = offset2;
}
delta = eb_delete_range(b, offset, offset1);
delta = eb_insert_spaces(b, offset, col - col0) - delta;
offset1 += delta;
stop += delta;
}
}
static void do_untabify_buffer(EditState *s)
{
/* deactivate region hilite */
s->region_style = 0;
eb_untabify(s->b, 0, s->b->total_size);
}
static void do_untabify_region(EditState *s)
{
/* deactivate region hilite */
s->region_style = 0;
eb_untabify(s->b, s->b->mark, s->offset);
}
static void do_indent_region(EditState *s)
{
int col_num, line1, line2;
/* deactivate region hilite */
s->region_style = 0;
/* Swap point and mark so mark <= point */
if (s->offset < s->b->mark) {
int tmp = s->b->mark;
s->b->mark = s->offset;
s->offset = tmp;
}
/* We do it with lines to avoid offset variations during indenting */
eb_get_pos(s->b, &line1, &col_num, s->b->mark);
eb_get_pos(s->b, &line2, &col_num, s->offset);
if (col_num == 0)
line2--;
/* Iterate over all lines inside block */
for (; line1 <= line2; line1++) {
if (s->mode->indent_func) {
(s->mode->indent_func)(s, eb_goto_pos(s->b, line1, 0));
} else {
s->offset = eb_goto_pos(s->b, line1, 0);
do_tab(s, 1);
}
}
}
void do_show_date_and_time(EditState *s, int argval)
{
time_t t = argval;
if (argval == NO_ARG)
time(&t);
put_status(s, "%.24s", ctime(&t));
}
/* forward / backward block */
#define MAX_LEVEL 32
/* Return the matching delimiter for all pairs */
static int matching_delimiter(int c) {
const char *pairs = "(){}[]<>";
int i;
for (i = 0; pairs[i]; i++) {
if (pairs[i] == c)
return pairs[i ^ 1];
}
return c;
}
static void do_forward_block(EditState *s, int dir)
{
unsigned int buf[COLORED_MAX_LINE_SIZE];
QETermStyle sbuf[COLORED_MAX_LINE_SIZE];
char balance[MAX_LEVEL];
int use_colors;
int line_num, col_num, style, style0, c, level;
int pos; /* position of the current character on line */
int len; /* number of colorized positions */
int offset; /* offset of the current character */
int offset0; /* offset of the beginning of line */
int offset1; /* offset of the beginning of the next line */
offset = s->offset;
eb_get_pos(s->b, &line_num, &col_num, offset);
offset1 = offset0 = eb_goto_bol2(s->b, offset, &pos);
use_colors = s->colorize_func || s->b->b_styles;
style0 = 0;
len = 0;
if (use_colors) {
len = get_colorized_line(s, buf, countof(buf), sbuf,
offset1, &offset1, line_num);
if (len < countof(buf) - 2) {
if (pos > 0
&& ((c = buf[pos - 1]) == ']' || c == '}' || c == ')')) {
style0 = sbuf[pos - 1];
} else
if (pos < len) {
style0 = sbuf[pos];
}
} else {
/* very long line detected, use fallback version */
use_colors = 0;
len = 0;
}
}
level = 0;
if (dir < 0) {
for (;;) {
c = eb_prevc(s->b, offset, &offset);
if (c == '\n') {
if (offset <= 0)
break;
line_num--;
offset1 = offset0 = eb_goto_bol2(s->b, offset, &pos);
len = 0;
if (use_colors) {
len = get_colorized_line(s, buf, countof(buf), sbuf,
offset1, &offset1, line_num);
if (len >= countof(buf) - 2) {
/* very long line detected, use fallback version */
use_colors = 0;
len = 0;
style0 = 0;
}
}
continue;
}
style = 0;
--pos;
if (pos >= 0 && pos < len) {
style = sbuf[pos];
}
if (style != style0 && style != QE_STYLE_KEYWORD && style != QE_STYLE_FUNCTION) {
if (style0 == 0)
continue;
style0 = 0;
if (style != 0)
continue;
}
switch (c) {
case '\"':
case '\'':
if (pos >= len) {
/* simplistic string skip with escape char */
int c1, off;
while ((c1 = eb_prevc(s->b, offset, &off)) != '\n') {
offset = off;
pos--;
if (c1 == c && eb_prevc(s->b, offset, &off) != '\\')
break;
}
}
break;
case ')':
case ']':
case '}':
if (level < MAX_LEVEL) {
balance[level] = matching_delimiter(c);
}
level++;
break;
case '(':
case '[':
case '{':
if (level > 0) {
--level;
if (level < MAX_LEVEL && balance[level] != c) {
/* XXX: should set mark and offset */
put_status(s, "Unmatched delimiter %c <> %c",
c, balance[level]);
return;
}
if (level == 0) {
s->offset = offset;
return;
}
} else {
/* silently move up one level */
}
break;
}
}
} else {
for (;;) {
c = eb_nextc(s->b, offset, &offset);
if (c == '\n') {
line_num++;
if (offset >= s->b->total_size)
break;
offset1 = offset0 = offset;
len = 0;
if (use_colors) {
len = get_colorized_line(s, buf, countof(buf), sbuf,
offset1, &offset1, line_num);
if (len >= countof(buf) - 2) {
/* very long line detected, use fallback version */
use_colors = 0;
len = 0;
style0 = 0;
}
}
pos = 0;
continue;
}
style = 0;
if (pos < len) {
style = sbuf[pos];
}
pos++;
if (style0 != style && style != QE_STYLE_KEYWORD && style != QE_STYLE_FUNCTION) {
if (style0 == 0)
continue;
style0 = 0;
if (style != 0)
continue;
}
switch (c) {
case '\"':
case '\'':
if (pos >= len) {
/* simplistic string skip with escape char */
int c1, off;
while ((c1 = eb_nextc(s->b, offset, &off)) != '\n') {
offset = off;
pos++;
if (c1 == '\\') {
if (eb_nextc(s->b, offset, &off) == '\n')
break;
offset = off;
pos++;
} else
if (c1 == c) {
break;
}
}
}
break;
case '(':
case '[':
case '{':
if (level < MAX_LEVEL) {
balance[level] = matching_delimiter(c);
}
level++;
break;
case ')':
case ']':
case '}':
if (level > 0) {
--level;
if (level < MAX_LEVEL && balance[level] != c) {
/* XXX: should set mark and offset */
put_status(s, "Unmatched delimiter %c <> %c",
c, balance[level]);
return;
}
if (level == 0) {
s->offset = offset;
return;
}
} else {
/* silently move up one level */
}
break;
}
}
}
if (level != 0) {
/* XXX: should set mark and offset */
put_status(s, "Unmatched delimiter");
} else {
s->offset = offset;
}
}
static void do_kill_block(EditState *s, int dir)
{
int start = s->offset;
do_forward_block(s, dir);
do_kill(s, start, s->offset, dir, 0);
}
void do_transpose(EditState *s, int cmd)
{
QEmacsState *qs = s->qe_state;
int offset0, offset1, offset2, offset3, end_offset;
int size0, size1, size2;
EditBuffer *b = s->b;
if (check_read_only(s))
return;
/* compute positions of ranges to swap:
offset0..offset1 and offset2..offset3
end_offset is the ending position after the swap
*/
switch (cmd) {
case CMD_TRANSPOSE_CHARS:
/* at end of line, transpose previous 2 characters,
* otherwise transpose characters before and after point.
*/
offset1 = offset2 = s->offset;
offset0 = eb_prev(b, offset1);
if (eb_nextc(b, offset2, &offset3) == '\n') {
end_offset = offset3 = offset2;
offset1 = offset2 = offset0;
offset0 = eb_prev(b, offset1);
} else {
/* XXX: should have specific flag */
if (qs->emulation_flags == 1) {
/* keep current position between characters */
end_offset = offset0 + offset3 - offset2;
} else {
/* set position past second character (emacs behaviour) */
end_offset = offset3;
}
}
break;
case CMD_TRANSPOSE_WORDS:
word_right(s, 1);
word_right(s, 0);
offset3 = s->offset;
word_left(s, 0);
offset2 = s->offset;
word_left(s, 1);
offset1 = s->offset;
word_left(s, 0);
offset0 = s->offset;
if (qs->emulation_flags == 1) {
/* set position to end of first word */
end_offset = offset0 + offset3 - offset2;
} else {
/* set position past last word (emacs behaviour) */
end_offset = offset3;
}
break;
case CMD_TRANSPOSE_LINES:
do_eol(s);
offset3 = s->offset;
do_bol(s);
offset2 = s->offset;
offset1 = eb_prev(b, offset2); /* skip line feed */
s->offset = offset1;
do_bol(s);
offset0 = s->offset;
if (qs->emulation_flags == 1) {
/* set position to start of second line */
end_offset = offset0 + offset3 - offset1;
} else {
/* set position past second line (emacs behaviour) */
end_offset = offset3;
}
break;
default:
return;
}
size0 = offset1 - offset0;
size1 = offset2 - offset1;
size2 = offset3 - offset2;
if (!b->b_styles && size0 + size1 + size2 <= 1024) {
u8 buf[1024];
/* Use fast method and generate single undo record */
eb_read(b, offset2, buf, size2);
eb_read(b, offset1, buf + size2, size1);
eb_read(b, offset0, buf + size2 + size1, size0);
eb_write(b, offset0, buf, size0 + size1 + size2);
} else {
EditBuffer *b1 = eb_new("*tmp*", BF_SYSTEM | (b->flags & BF_STYLES));
eb_set_charset(b1, b->charset, b->eol_type);
/* Use eb_insert_buffer_convert to copy styles.
* This conversion should not change sizes */
eb_insert_buffer_convert(b1, 0, b, offset2, size2);
eb_insert_buffer_convert(b1, size2, b, offset1, size1);
eb_insert_buffer_convert(b1, size2 + size1, b, offset0, size0);
/* XXX: This will create 2 undo records */
eb_delete(b, offset0, size0 + size1 + size2);
eb_insert_buffer_convert(b, offset0, b1, 0, b1->total_size);
eb_free(&b1);
}
s->offset = end_offset;
}
/* remove a key binding from mode or globally */
static int qe_unregister_binding1(unsigned int *keys, int nb_keys, ModeDef *m)
{
QEmacsState *qs = &qe_state;
KeyDef **lp, *p;
lp = m ? &m->first_key : &qs->first_key;
while (*lp) {
if ((*lp)->nb_keys == nb_keys
&& !memcmp((*lp)->keys, keys, nb_keys * sizeof(*keys)))
{
p = *lp;
*lp = (*lp)->next;
qe_free(&p);
return 1;
}
lp = &(*lp)->next;
}
return 0;
}
static void do_unset_key(EditState *s, const char *keystr, int local)
{
unsigned int keys[MAX_KEYS];
int nb_keys;
nb_keys = strtokeys(keystr, keys, MAX_KEYS);
if (!nb_keys)
return;
qe_unregister_binding1(keys, nb_keys, local ? s->mode : NULL);
}
/*---------------- help ----------------*/
int qe_list_bindings(CmdDef *d, ModeDef *mode, int inherit, char *buf, int size)
{
int pos;
buf_t outbuf, *out;
out = buf_init(&outbuf, buf, size);
pos = 0;
for (;;) {
KeyDef *kd = mode ? mode->first_key : qe_state.first_key;
for (; kd != NULL; kd = kd->next) {
if (kd->cmd == d
&& qe_find_current_binding(kd->keys, kd->nb_keys, mode) == kd) {
if (out->len > pos)
buf_puts(out, ", ");
buf_put_keys(out, kd->keys, kd->nb_keys);
}
}
if (!inherit || !mode)
break;
/* Move up to base mode */
mode = mode->fallback;
}
return out->len;
}
void do_show_bindings(EditState *s, const char *cmd_name)
{
char buf[256];
CmdDef *d;
if ((d = qe_find_cmd(cmd_name)) == NULL) {
put_status(s, "No command %s", cmd_name);
return;
}
if (qe_list_bindings(d, s->mode, 1, buf, sizeof(buf))) {
put_status(s, "%s is bound to %s", cmd_name, buf);
} else {
put_status(s, "%s is not bound to any key", cmd_name);
}
}
#define SF_REVERSE 1
#define SF_FOLD 2
#define SF_DICT 4
#define SF_NUMBER 8
#define SF_COLUMN 16
#define SF_BASENAME 32
#define SF_PARAGRAPH 64
static int eb_sort_span(EditBuffer *b, int *pp1, int *pp2, int cur_offset, int flags);
static void print_bindings(EditBuffer *b, ModeDef *mode)
{
char buf[256];
CmdDef *d;
int gfound;
int start = b->total_size;
int stop;
gfound = 0;
d = qe_state.first_cmd;
while (d != NULL) {
while (d->name != NULL) {
if (qe_list_bindings(d, mode, 0, buf, sizeof(buf))) {
if (!gfound) {
if (mode) {
eb_printf(b, "\n%s mode bindings:\n\n", mode->name);
} else {
eb_printf(b, "\nGlobal bindings:\n\n");
}
start = b->total_size;
gfound = 1;
}
eb_printf(b, "%24s : %s\n", d->name, buf);
}
d++;
}
d = d->action.next;
}
if (gfound) {
stop = b->total_size;
eb_sort_span(b, &start, &stop, stop, SF_DICT);
}
}
void do_describe_bindings(EditState *s)
{
EditBuffer *b;
b = new_help_buffer();
if (!b)
return;
print_bindings(b, s->mode);
print_bindings(b, NULL);
b->flags |= BF_READONLY;
show_popup(s, b, "Bindings");
}
void do_apropos(EditState *s, const char *str)
{
QEmacsState *qs = s->qe_state;
char buf[256];
EditBuffer *b;
CmdDef *d;
VarDef *vp;
int found, start, stop;
b = new_help_buffer();
if (!b)
return;
eb_putc(b, '\n');
start = b->total_size;
found = 0;
d = qs->first_cmd;
while (d != NULL) {
while (d->name != NULL) {
if (strstr(d->name, str)) {
/* print name and prototype */
qe_get_prototype(d, buf, sizeof(buf));
eb_printf(b, "command: %s%s", d->name, buf);
if (qe_list_bindings(d, s->mode, 1, buf, sizeof(buf)))
eb_printf(b, " bound to %s", buf);
eb_putc(b, '\n');
if (d->desc && *d->desc) {
/* print short description */
eb_printf(b, " %s", d->desc);
}