forked from barnowl/barnowl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
editwin.c
1427 lines (1189 loc) · 32.8 KB
/
editwin.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
#include "owl.h"
#define VALID_EXCURSION (0x9a2b4729)
typedef struct _owl_editwin_excursion { /*noproto*/
int valid;
int index;
int mark;
int goal_column;
int lock;
struct _owl_editwin_excursion *next;
} oe_excursion;
struct _owl_editwin { /*noproto*/
int refcount;
char *buff;
owl_history *hist;
int bufflen;
int allocated;
int index;
int mark;
int goal_column;
int topindex;
int column;
int winlines, wincols, fillcol, wrapcol;
owl_window *win;
gulong repaint_id;
gulong resized_id;
int style;
int lock;
int dotsend;
int echochar;
oe_excursion *excursions;
void (*callback)(struct _owl_editwin *e, bool success);
void (*destroy_cbdata)(void *);
void *cbdata;
};
static void oe_set_window(owl_editwin *e, owl_window *w, int winlines, int wincols);
static void oe_redraw(owl_window *win, WINDOW *curswin, void *user_data);
static void oe_reframe(owl_editwin *e);
static void oe_save_excursion(owl_editwin *e, oe_excursion *x);
static void oe_release_excursion(owl_editwin *e, oe_excursion *x);
static void oe_restore_excursion(owl_editwin *e, oe_excursion *x);
static void oe_restore_mark_only(owl_editwin *e, oe_excursion *x);
static int oe_char_width(gunichar c, int column);
static int oe_region_column(owl_editwin *e, int start, int end, int offset);
static int oe_region_width(owl_editwin *e, int start, int end, int offset);
static int oe_find_display_line(owl_editwin *e, int *x, int index, int *hard);
static void oe_insert_char(owl_editwin *e, gunichar c);
static int owl_editwin_limit_maxcols(int width, int cols);
static int owl_editwin_check_dotsend(owl_editwin *e);
static int owl_editwin_is_char_in(owl_editwin *e, const char *set);
static gunichar owl_editwin_get_char_at_point(owl_editwin *e);
static int owl_editwin_replace_internal(owl_editwin *e, int replace, const char *s);
static const char *oe_copy_buf(owl_editwin *e, const char *buf, int len);
static int oe_copy_region(owl_editwin *e);
static CALLER_OWN char *oe_chunk(owl_editwin *e, int start, int end);
static void oe_destroy_cbdata(owl_editwin *e);
static void oe_dirty(owl_editwin *e);
static void oe_window_resized(owl_window *w, owl_editwin *e);
#define INCR 4096
#define WHITESPACE " \n\t"
static CALLER_OWN owl_editwin *owl_editwin_allocate(void)
{
owl_editwin *e = g_new0(owl_editwin, 1);
e->refcount = 1;
return e;
}
static void _owl_editwin_delete(owl_editwin *e)
{
if (e->win) {
g_signal_handler_disconnect(e->win, e->repaint_id);
g_signal_handler_disconnect(e->win, e->resized_id);
g_object_unref(e->win);
}
g_free(e->buff);
/* just in case someone forgot to clean up */
while (e->excursions) {
oe_release_excursion(e, e->excursions);
}
oe_destroy_cbdata(e);
g_free(e);
}
static inline void oe_set_index(owl_editwin *e, int index)
{
if (index != e->index) {
e->goal_column = -1;
e->column = -1;
}
e->index = index;
oe_dirty(e);
}
static inline void oe_set_mark(owl_editwin *e, int mark)
{
e->mark = mark;
}
void owl_editwin_set_mark(owl_editwin *e)
{
oe_set_mark(e, e->index);
/* owl_function_makemsg("Mark set."); */
}
static void _owl_editwin_init(owl_editwin *e,
int winlines,
int wincols,
int style,
owl_history *hist)
{
e->buff=g_new(char, INCR);
e->buff[0]='\0';
e->bufflen=0;
e->hist=hist;
e->allocated=INCR;
oe_set_index(e, 0);
oe_set_mark(e, -1);
e->goal_column = -1;
e->column = -1;
e->topindex = 0;
e->excursions = NULL;
e->style=style;
if ((style!=OWL_EDITWIN_STYLE_MULTILINE) &&
(style!=OWL_EDITWIN_STYLE_ONELINE)) {
e->style=OWL_EDITWIN_STYLE_MULTILINE;
}
e->lock=0;
e->dotsend=0;
e->echochar='\0';
}
CALLER_OWN owl_editwin *owl_editwin_new(owl_window *win, int winlines, int wincols, int style, owl_history *hist)
{
owl_editwin *e = owl_editwin_allocate();
_owl_editwin_init(e, winlines, wincols, style, hist);
oe_set_window(e, win, winlines, wincols);
return e;
}
owl_editwin *owl_editwin_ref(owl_editwin *e)
{
e->refcount++;
return e;
}
void owl_editwin_unref(owl_editwin *e)
{
e->refcount--;
if (e->refcount <= 0)
_owl_editwin_delete(e);
}
/* TODO: collapse this window into oe_window_resized. Need to stop
* passing winlines and wincols to oe_set_window and get them out of
* the owl_window. The tester code will first need to pass in an
* owl_window headless or so. */
static void oe_set_window_size(owl_editwin *e, int winlines, int wincols)
{
e->winlines=winlines;
e->wincols=wincols;
/* fillcol and wrapcol may have changed. */
e->fillcol=owl_editwin_limit_maxcols(wincols-7, owl_global_get_edit_maxfillcols(&g));
if (e->style == OWL_EDITWIN_STYLE_MULTILINE)
e->wrapcol=owl_editwin_limit_maxcols(wincols-7, owl_global_get_edit_maxwrapcols(&g));
else
e->wrapcol = 0;
}
static void oe_window_resized(owl_window *w, owl_editwin *e)
{
int winlines, wincols;
owl_window_get_position(w, &winlines, &wincols, NULL, NULL);
oe_set_window_size(e, winlines, wincols);
}
static void oe_set_window(owl_editwin *e, owl_window *w, int winlines, int wincols)
{
e->win=w;
oe_set_window_size(e, winlines, wincols);
if (e->win) {
g_object_ref(e->win);
e->repaint_id = g_signal_connect(w, "redraw", G_CALLBACK(oe_redraw), e);
e->resized_id = g_signal_connect(w, "resized", G_CALLBACK(oe_window_resized), e);
owl_window_dirty(e->win);
}
}
owl_window *owl_editwin_get_window(owl_editwin *e)
{
return e->win;
}
/* echo the character 'ch' for each normal character keystroke,
* excepting locktext. This is useful for entering passwords etc. If
* ch=='\0' characters are echo'd normally
*/
void owl_editwin_set_echochar(owl_editwin *e, int ch)
{
e->echochar=ch;
oe_dirty(e);
}
owl_history *owl_editwin_get_history(owl_editwin *e)
{
return(e->hist);
}
void owl_editwin_set_dotsend(owl_editwin *e)
{
e->dotsend=1;
}
void owl_editwin_set_callback(owl_editwin *e, void (*cb)(owl_editwin *, bool))
{
e->callback = cb;
}
void (*owl_editwin_get_callback(owl_editwin *e))(owl_editwin *, bool)
{
return e->callback;
}
static void oe_destroy_cbdata(owl_editwin *e) {
if (e->destroy_cbdata)
e->destroy_cbdata(e->cbdata);
e->cbdata = NULL;
e->destroy_cbdata = NULL;
}
void owl_editwin_set_cbdata(owl_editwin *e, void *data, void (*destroy)(void *))
{
oe_destroy_cbdata(e);
e->cbdata = data;
e->destroy_cbdata = destroy;
}
void *owl_editwin_get_cbdata(owl_editwin *e) {
return e->cbdata;
}
void owl_editwin_do_callback(owl_editwin *e, bool success)
{
void (*cb)(owl_editwin *, bool);
cb = owl_editwin_get_callback(e);
if (!cb) {
owl_function_error("Internal error: No editwin callback!");
} else {
cb(e, success);
}
}
static int owl_editwin_limit_maxcols(int width, int cols)
{
if (cols == 0)
return width;
return cols;
}
/* set text to be 'locked in' at the beginning of the buffer, any
* previous text (including locked text) will be overwritten
*/
void owl_editwin_set_locktext(owl_editwin *e, const char *text)
{
oe_set_index(e, 0);
e->lock = 0;
owl_editwin_replace(e, e->bufflen, text);
e->buff[e->bufflen] = 0;
e->lock=e->bufflen;
oe_set_index(e, e->lock);
oe_dirty(e);
}
int owl_editwin_get_style(owl_editwin *e)
{
return(e->style);
}
/* clear all text except for locktext and put the cursor at the
* beginning
*/
void owl_editwin_clear(owl_editwin *e)
{
int lock = e->lock;
int dotsend=e->dotsend;
char *locktext=NULL;
char echochar=e->echochar;
if (lock > 0) {
locktext = g_strndup(e->buff, lock);
}
g_free(e->buff);
_owl_editwin_init(e, e->winlines, e->wincols, e->style, e->hist);
if (lock > 0) {
owl_editwin_set_locktext(e, locktext);
}
if (dotsend) {
owl_editwin_set_dotsend(e);
}
if (echochar) {
owl_editwin_set_echochar(e, echochar);
}
g_free(locktext);
oe_set_index(e, lock);
}
void owl_editwin_recenter(owl_editwin *e)
{
e->topindex = -1;
oe_dirty(e);
}
static void oe_save_excursion(owl_editwin *e, oe_excursion *x)
{
x->index = e->index;
x->mark = e->mark;
x->goal_column = e->goal_column;
x->lock = e->lock;
x->valid = VALID_EXCURSION;
x->next = e->excursions;
e->excursions = x;
}
static void oe_release_excursion(owl_editwin *e, oe_excursion *x)
{
oe_excursion **px;
x->valid = 0;
for (px = &e->excursions; *px != NULL; px = &(*px)->next)
if (*px == x) {
*px = x->next;
return;
}
abort();
}
static void oe_restore_excursion(owl_editwin *e, oe_excursion *x)
{
if (x->valid == VALID_EXCURSION) {
oe_set_index(e, x->index);
e->goal_column = x->goal_column;
e->mark = x->mark;
e->lock = x->lock;
oe_release_excursion(e, x);
}
}
static void oe_restore_mark_only(owl_editwin *e, oe_excursion *x)
{
if (x->valid == VALID_EXCURSION) {
e->mark = x->mark;
oe_release_excursion(e, x);
}
}
/* External interface to oe_save_excursion */
owl_editwin_excursion *owl_editwin_begin_excursion(owl_editwin *e)
{
owl_editwin_excursion *x = g_new(owl_editwin_excursion, 1);
oe_save_excursion(e, x);
return x;
}
void owl_editwin_end_excursion(owl_editwin *e, owl_editwin_excursion *x)
{
oe_restore_excursion(e, x);
g_free(x);
}
static inline const char *oe_next_point(owl_editwin *e, const char *p)
{
const char *boundary = e->buff + e->bufflen + 1;
const char *q;
q = g_utf8_find_next_char(p, boundary);
while (q && g_unichar_ismark(g_utf8_get_char(q)))
q = g_utf8_find_next_char(q, boundary);
if (q == p)
return NULL;
return q;
}
static inline const char *oe_prev_point(owl_editwin *e, const char *p)
{
const char *boundary = e->buff + e->lock;
p = g_utf8_find_prev_char(boundary, p);
while (p && g_unichar_ismark(g_utf8_get_char(p)))
p = g_utf8_find_prev_char(boundary, p);
return p;
}
static int oe_char_width(gunichar c, int column)
{
int cw;
if (c == 9) /* TAB */
return TABSIZE - column % TABSIZE;
cw = mk_wcwidth(c);
if (cw < 0) /* control characters */
cw = 0;
return cw;
}
/* Finds the display line of 'e' starting at the character
* 'index'. The index just after the line is returned. Whether the
* line ends at a hard break (newline) or soft break (wrapping) is
* returned in 'hard'.
*
* If the point (e->index) is contained in the line, its position is
* returned in 'x'.
*/
static int oe_find_display_line(owl_editwin *e, int *x, int index, int *hard)
{
int width = 0, cw;
gunichar c;
const char *p;
while(1) {
/* note the position of the dot */
if (x != NULL && index == e->index && width < e->wincols)
*x = width;
/* get the current character */
c = g_utf8_get_char(e->buff + index);
/* figure out how wide it is */
cw = oe_char_width(c, width);
if (width + cw > e->wincols - 1) {
if (x != NULL && *x == width)
*x = -1;
if (hard != NULL) *hard = 0;
break;
}
width += cw;
if (c == '\n') {
if (width < e->wincols)
++index; /* skip the newline */
if (hard != NULL) *hard = 1;
break;
}
/* find the next character */
p = oe_next_point(e, e->buff + index);
if (p == NULL) { /* we ran off the end */
if (x != NULL && e->index > index)
*x = width + 1;
if (hard != NULL) *hard = 1;
break;
}
index = p - e->buff;
}
return index;
}
static void oe_reframe(owl_editwin *e) {
oe_excursion x;
int goal = 1 + e->winlines / 2;
int index;
int count = 0;
int n, i;
int last;
oe_save_excursion(e, &x);
/* step back line-by-line through the buffer until we have >= goal lines of
display text */
e->lock = 0; /* we can (must) tread on the locktext */
last = -1;
while (count < goal) {
index = e->index;
owl_editwin_move_to_beginning_of_line(e);
if (last == e->index)
break;
last = e->index;
for (n = 0, i = e->index; i < index; n++)
i = oe_find_display_line(e, NULL, i, NULL);
count += n == 0 ? 1 : n;
if (count < goal)
owl_editwin_point_move(e, -1);
}
e->topindex = e->index;
/* if we overshot, backtrack */
for (n = 0; n < (count - goal); n++)
e->topindex = oe_find_display_line(e, NULL, e->topindex, NULL);
oe_restore_excursion(e, &x);
oe_dirty(e);
}
static void oe_addnec(owl_editwin *e, WINDOW *curswin, int count)
{
int i;
for (i = 0; i < count; i++)
waddch(curswin, e->echochar);
}
static void oe_mvaddnec(owl_editwin *e, WINDOW *curswin, int y, int x, int count)
{
wmove(curswin, y, x);
oe_addnec(e, curswin, count);
}
/* regenerate the text on the curses window */
static void oe_redraw(owl_window *win, WINDOW *curswin, void *user_data)
{
int x = -1, y = -1, t, hard;
int line, index, lineindex, times = 0;
owl_editwin *e = user_data;
do {
werase(curswin);
if (e->topindex == -1 || e->index < e->topindex)
oe_reframe(e);
line = 0;
index = e->topindex;
while(line < e->winlines) {
lineindex = index;
t = -1;
index = oe_find_display_line(e, &t, lineindex, &hard);
if (x == -1 && t != -1)
x = t, y = line;
if (index - lineindex) {
if (!e->echochar)
mvwaddnstr(curswin, line, 0,
e->buff + lineindex,
index - lineindex);
else {
if(lineindex < e->lock) {
mvwaddnstr(curswin, line, 0,
e->buff + lineindex,
MIN(index - lineindex,
e->lock - lineindex));
if (e->lock < index)
oe_addnec(e, curswin,
oe_region_width(e, e->lock, index,
oe_region_width(e, lineindex, e->lock, 0)));
} else
oe_mvaddnec(e, curswin, line, 0, oe_region_width(e, lineindex, index, 0));
}
if (!hard)
waddch(curswin, '\\');
}
line++;
}
if (x == -1)
e->topindex = -1; /* force a reframe */
times++;
} while(x == -1 && times < 3);
wmove(curswin, y, x);
}
static inline void oe_fixup(int *target, int start, int end, int change) {
if (*target > start) {
if (*target <= end)
*target = end + change;
else
*target += change;
}
}
int owl_editwin_replace_region(owl_editwin *e, const char *s)
{
oe_excursion x;
int ret;
if (e->mark == -1) {
owl_function_error("The mark is unset, there is no region to replace.");
return 0;
}
oe_save_excursion(e, &x);
if(e->index > e->mark) {
owl_editwin_exchange_point_and_mark(e);
}
ret = owl_editwin_replace_internal(e, e->mark - e->index, s);
oe_restore_excursion(e, &x);
return ret;
}
/* replace 'replace' characters at the point with s, returning the change in size */
int owl_editwin_replace(owl_editwin *e, int replace, const char *s)
{
int start, end, i;
const char *p;
if (!g_utf8_validate(s, -1, NULL)) {
owl_function_debugmsg("owl_editwin_insert_string: received non-UTF-8 string.");
return 0;
}
start = e->index;
for (i = 0, p = e->buff + start; i < replace && p != NULL; i++)
p = oe_next_point(e, p);
if (p != NULL)
end = p - e->buff;
else
end = e->bufflen;
return owl_editwin_replace_internal(e, end - start, s);
}
static int owl_editwin_replace_internal(owl_editwin *e, int replace, const char *s)
{
int start, end, free, need, size, change, oldindex;
oe_excursion *x;
start = e->index;
end = start + replace;
free = e->allocated - e->bufflen + end - start;
need = strlen(s) - free;
if (need > 0) {
size = e->allocated + need + INCR - (need % INCR);
e->buff = g_renew(char, e->buff, size);
e->allocated = size;
}
memmove(e->buff + start + strlen(s), e->buff + end, e->bufflen + 1 - end);
memcpy(e->buff + start, s, strlen(s));
change = start - end + strlen(s);
e->bufflen += change;
oldindex = e->index;
e->index += strlen(s);
if (e->column != -1)
e->column = oe_region_column(e, oldindex, e->index, e->column);
/* fix up the mark */
oe_fixup(&e->mark, start, end, change);
oe_fixup(&e->topindex, start, end, change);
/* fix up any saved points after the replaced area */
for (x = e->excursions; x != NULL; x = x->next) {
oe_fixup(&x->index, start, end, change);
oe_fixup(&x->mark, start, end, change);
}
/* recenter if needed */
if (start <= e->topindex)
owl_editwin_recenter(e);
oe_dirty(e);
return change;
}
/* linewrap the word just before the cursor.
* returns 0 on success
* returns -1 if we could not wrap.
*/
static void _owl_editwin_linewrap_word(owl_editwin *e)
{
oe_excursion x;
gunichar c;
oe_save_excursion(e, &x);
while (owl_editwin_point_move(e, -1)) {
c = owl_editwin_get_char_at_point(e);
if (owl_util_can_break_after(c) || c == '\n') {
if (c != '\n')
owl_editwin_replace(e, c != ' ' ? 0 : 1, "\n");
break;
}
}
oe_restore_excursion(e, &x);
}
/* delete the character at the current point, following chars
* shift left.
*/
void owl_editwin_delete_char(owl_editwin *e)
{
owl_editwin_replace(e, 1, "");
}
/* Swap the character at point with the character at point-1 and
* advance the pointer. If point is at beginning of buffer do
* nothing. If point is after the last character swap point-1 with
* point-2. (Behaves as observed in tcsh and emacs).
*/
void owl_editwin_transpose_chars(owl_editwin *e)
{
const char *middle, *end, *start;
char *tmp;
if (e->bufflen == 0) return;
if (e->index == e->bufflen)
owl_editwin_point_move(e, -1); /* point is after last character */
if (owl_editwin_at_beginning_of_buffer(e))
return; /* point is at beginning of buffer, do nothing */
/* Transpose two utf-8 unicode glyphs. */
middle = e->buff + e->index;
end = oe_next_point(e, middle);
if (end == NULL)
return;
start = oe_prev_point(e, middle);
if (start == NULL)
return;
tmp = g_new(char, (end - start) + 1);
tmp[(end - start)] = 0;
memcpy(tmp, middle, end - middle);
memcpy(tmp + (end - middle), start, middle - start);
owl_editwin_point_move(e, -1);
owl_editwin_replace(e, 2, tmp);
}
/* insert 'string' at the current point, later text is shifted
* right
*/
void owl_editwin_insert_string(owl_editwin *e, const char *s)
{
owl_editwin_replace(e, 0, s);
}
/* We assume index is not set to point to a mid-char */
static gunichar owl_editwin_get_char_at_point(owl_editwin *e)
{
return g_utf8_get_char(e->buff + e->index);
}
void owl_editwin_exchange_point_and_mark(owl_editwin *e) {
int tmp;
if (e->mark != -1) {
tmp = e->mark;
owl_editwin_set_mark(e);
oe_set_index(e, tmp);
}
}
int owl_editwin_point_move(owl_editwin *e, int delta)
{
const char *p;
int change, d = 0;
change = MAX(delta, - delta);
p = e->buff + e->index;
while (d < change && p != NULL) {
if (delta > 0)
p = oe_next_point(e, p);
else
p = oe_prev_point(e, p);
if (p != NULL) {
oe_set_index(e, p - e->buff);
d++;
}
}
return delta > 0 ? d : -d;
}
int owl_editwin_at_beginning_of_buffer(owl_editwin *e) {
if (e->index == e->lock)
return 1;
return 0;
}
int owl_at_end_of_buffer(owl_editwin *e) {
if (e->index == e->bufflen)
return 1;
return 0;
}
static int owl_editwin_at_beginning_of_line(owl_editwin *e)
{
oe_excursion x;
int ret;
if (owl_editwin_at_beginning_of_buffer(e))
return 1;
oe_save_excursion(e, &x);
owl_editwin_point_move(e, -1);
ret = (owl_editwin_get_char_at_point(e) == '\n');
oe_restore_excursion(e, &x);
return ret;
}
static int owl_editwin_is_char_in(owl_editwin *e, const char *set)
{
const char *p;
for (p = set; *p != 0; p = g_utf8_find_next_char(p, NULL))
if (owl_editwin_get_char_at_point(e) == g_utf8_get_char(p))
return 1;
return 0;
}
int owl_editwin_move_if_in(owl_editwin *e, int delta, const char *set)
{
int change, distance = 0;
while (owl_editwin_is_char_in(e, set)) {
change = owl_editwin_point_move(e, delta);
distance += change;
if (change == 0)
break;
}
return distance;
}
int owl_editwin_move_if_not_in(owl_editwin *e, int delta, const char *set)
{
int change, distance = 0;
while (!owl_editwin_is_char_in(e, set)) {
change = owl_editwin_point_move(e, delta);
distance += change;
if (change == 0)
break;
}
return distance;
}
int owl_editwin_move_to_beginning_of_line(owl_editwin *e)
{
int distance = 0;
if (!owl_editwin_at_beginning_of_line(e)) {
/* move off the \n if were at the end of a line */
distance += owl_editwin_point_move(e, -1);
distance += owl_editwin_move_if_not_in(e, -1, "\n");
/* If we stopped because we reached a '\n', rather than because we
* hit the top of the buffer, move forward from the end of the
* previous line to the start of the current. */
if (owl_editwin_get_char_at_point(e) == '\n')
distance += owl_editwin_point_move(e, 1);
}
e->goal_column = 0; /* subtleties */
return distance;
}
int owl_editwin_move_to_end_of_line(owl_editwin *e)
{
return owl_editwin_move_if_not_in(e, 1, "\n");
}
int owl_editwin_line_move(owl_editwin *e, int delta)
{
int goal_column, change, ll, distance;
int count = 0;
change = MAX(delta, -delta);
goal_column = e->goal_column;
distance = owl_editwin_move_to_beginning_of_line(e);
goal_column = goal_column == -1 ? -distance : goal_column;
while(count < change) {
if (delta > 0) {
distance += owl_editwin_move_if_not_in(e, 1, "\n");
distance += owl_editwin_point_move(e, 1);
} else {
/* I really want to assert delta < 0 here */
distance += owl_editwin_point_move(e, -1); /* to the newline on
the previous line */
distance += owl_editwin_move_to_beginning_of_line(e);
}
count++;
}
distance += (ll = owl_editwin_move_to_end_of_line(e));
if (ll > goal_column)
distance += owl_editwin_point_move(e, goal_column - ll);
e->goal_column = goal_column;
oe_dirty(e);
return distance;
}
void owl_editwin_backspace(owl_editwin *e)
{
/* delete the char before the current one
* and shift later chars left
*/
if(owl_editwin_point_move(e, -1))
owl_editwin_delete_char(e);
}
void owl_editwin_key_up(owl_editwin *e)
{
owl_editwin_line_move(e, -1);
}
void owl_editwin_key_down(owl_editwin *e)
{
owl_editwin_line_move(e, 1);
}
void owl_editwin_key_left(owl_editwin *e)
{
owl_editwin_point_move(e, -1);
}
void owl_editwin_key_right(owl_editwin *e)
{
owl_editwin_point_move(e, 1);
}
int owl_editwin_forward_word(owl_editwin *e)
{
int distance;
/* if we're starting on a space, find the first non-space */
distance = owl_editwin_move_if_in(e, 1, WHITESPACE);
/* now find the end of this word */
distance += owl_editwin_move_if_not_in(e, 1, WHITESPACE);
return distance;
}
void owl_editwin_move_to_nextword(owl_editwin *e)
{
owl_editwin_forward_word(e);
}
/* go backwards to the last non-space character
*/
int owl_editwin_backward_word(owl_editwin *e)
{
oe_excursion x;
int distance = 0;
int further = 0;
int beginning;
/* if in middle of word, beginning of word */
/* if at beginning of a word, find beginning of previous word */
if (owl_editwin_is_char_in(e, WHITESPACE)) {
/* if in whitespace past end of word, find a word , the find the beginning*/
distance += owl_editwin_move_if_in(e, -1, WHITESPACE); /* leaves us on the last
character of the word */
oe_save_excursion(e, &x);
/* are we at the beginning of a word? */
owl_editwin_point_move(e, -1);
beginning = owl_editwin_is_char_in(e, WHITESPACE);
oe_restore_excursion(e, &x);
if (beginning)
return distance;
} else {
/* in the middle of the word; */
oe_save_excursion(e, &x);
further += owl_editwin_point_move(e, -1);
if (owl_editwin_is_char_in(e, WHITESPACE)) { /* we were at the beginning */
distance += owl_editwin_backward_word(e); /* previous case */
oe_release_excursion(e, &x);
return distance + further;
} else {
oe_restore_excursion(e, &x);
}
}
distance += owl_editwin_move_if_not_in(e, -1, WHITESPACE);
/* will go past */
if (e->index > e->lock)