-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqe.h
2403 lines (2079 loc) · 82.4 KB
/
qe.h
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 Fabrice Bellard.
* 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.
*/
#ifndef QE_H
#define QE_H
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <errno.h>
#include <limits.h>
#include <inttypes.h>
#include <time.h>
#ifdef HAVE_QE_CONFIG_H
#include "config.h"
#endif
#ifndef DEFAULT_TAB_WIDTH
#define DEFAULT_TAB_WIDTH 8 /* used to be 4 */
#endif
#ifndef DEFAULT_INDENT_WIDTH
#define DEFAULT_INDENT_WIDTH 8 /* used to be 4 */
#endif
#ifndef DEFAULT_FILL_COLUMN
#define DEFAULT_FILL_COLUMN 80
#endif
/* OS specific defines */
#ifdef CONFIG_WIN32
#define snprintf _snprintf
#define vsnprintf _vsnprintf
#endif
#if (defined(__GNUC__) || defined(__TINYC__))
/* make sure that the keyword is not disabled by glibc (TINYC case) */
#define qe__attr_printf(a, b) __attribute__((format(printf, a, b)))
#else
#define qe__attr_printf(a, b)
#endif
#if defined(__GNUC__) && __GNUC__ > 2
#define qe__attr_nonnull(l) __attribute__((nonnull l))
#define qe__unused__ __attribute__((unused))
#else
#define qe__attr_nonnull(l)
#define qe__unused__
#endif
#ifndef offsetof
#define offsetof(s,m) ((size_t)(&((s *)0)->m))
#endif
#ifndef countof
#define countof(a) ((int)(sizeof(a) / sizeof((a)[0])))
#endif
#ifndef ssizeof
#define ssizeof(a) ((int)(sizeof(a)))
#endif
#define OWNED /* ptr attribute for allocated data owned by a structure */
/* prevent gcc warning about shadowing a global declaration */
#define index index__
/************************/
#include "cutils.h"
/************************/
/* allocation wrappers and utilities */
void *qe_malloc_bytes(size_t size);
void *qe_mallocz_bytes(size_t size);
void *qe_malloc_dup(const void *src, size_t size);
char *qe_strdup(const char *str);
void *qe_realloc(void *pp, size_t size);
#define qe_malloc(t) ((t *)qe_malloc_bytes(sizeof(t)))
#define qe_mallocz(t) ((t *)qe_mallocz_bytes(sizeof(t)))
#define qe_malloc_array(t, n) ((t *)qe_malloc_bytes((n) * sizeof(t)))
#define qe_mallocz_array(t, n) ((t *)qe_mallocz_bytes((n) * sizeof(t)))
#define qe_malloc_hack(t, n) ((t *)qe_malloc_bytes(sizeof(t) + (n)))
#define qe_mallocz_hack(t, n) ((t *)qe_mallocz_bytes(sizeof(t) + (n)))
#ifdef CONFIG_HAS_TYPEOF
#define qe_free(pp) do { typeof(**(pp)) **__ = (pp); (free)(*__); *__ = NULL; } while (0)
#else
#define qe_free(pp) \
do if (sizeof(**(pp)) >= 0) { void *_ = (pp); (free)(*(void **)_); *(void **)_ = NULL; } while (0)
#endif
#ifndef free
#define free(p) do_not_use_free!!(p)
#endif
#ifndef malloc
#define malloc(s) do_not_use_malloc!!(s)
#endif
#ifndef realloc
#define realloc(p,s) do_not_use_realloc!!(p,s)
#endif
/************************/
typedef unsigned char u8;
typedef struct EditState EditState;
typedef struct EditBuffer EditBuffer;
typedef struct QEmacsState QEmacsState;
typedef struct DisplayState DisplayState;
typedef struct ModeProbeData ModeProbeData;
typedef struct QEModeData QEModeData;
typedef struct ModeDef ModeDef;
typedef struct QETimer QETimer;
typedef struct QEColorizeContext QEColorizeContext;
typedef struct KeyDef KeyDef;
typedef struct InputMethod InputMethod;
typedef struct ISearchState ISearchState;
typedef struct QEProperty QEProperty;
static inline char *s8(u8 *p) { return (char*)p; }
static inline const char *cs8(const u8 *p) { return (const char*)p; }
#ifndef INT_MAX
#define INT_MAX 0x7fffffff
#endif
#ifndef INT_MIN
#define INT_MIN (-0x7fffffff-1)
#endif
#define NO_ARG INT_MIN
#define MAX_FILENAME_SIZE 1024 /* Size for a filename buffer */
#define MAX_BUFFERNAME_SIZE 256 /* Size for a buffer name buffer */
#define MAX_CMDNAME_SIZE 32 /* Size for a command name buffer */
extern const char str_version[];
extern const char str_credits[];
/* low level I/O events */
void set_read_handler(int fd, void (*cb)(void *opaque), void *opaque);
void set_write_handler(int fd, void (*cb)(void *opaque), void *opaque);
int set_pid_handler(int pid,
void (*cb)(void *opaque, int status), void *opaque);
void url_exit(void);
void url_redisplay(void);
void register_bottom_half(void (*cb)(void *opaque), void *opaque);
void unregister_bottom_half(void (*cb)(void *opaque), void *opaque);
QETimer *qe_add_timer(int delay, void *opaque, void (*cb)(void *opaque));
void qe_kill_timer(QETimer **tip);
/* main loop for Unix programs using liburlio */
void url_main_loop(void (*init)(void *opaque), void *opaque);
/* util.c */
/* string arrays */
typedef struct StringItem {
void *opaque; /* opaque data that the user can use */
char selected; /* true if selected */
char group; /* used to group sorted items */
char str[1];
} StringItem;
typedef struct StringArray {
int nb_allocated;
int nb_items;
StringItem **items;
} StringArray;
#define NULL_STRINGARRAY { 0, 0, NULL }
typedef struct CompleteState {
StringArray cs;
struct EditState *s;
struct EditState *target;
int start, end, len, fuzzy;
char current[MAX_FILENAME_SIZE];
} CompleteState;
/* media definitions */
#define CSS_MEDIA_TTY 0x0001
#define CSS_MEDIA_SCREEN 0x0002
#define CSS_MEDIA_PRINT 0x0004
#define CSS_MEDIA_TV 0x0008
#define CSS_MEDIA_SPEECH 0x0010
#define CSS_MEDIA_ALL 0xffff
typedef struct CSSRect {
int x1, y1, x2, y2;
} CSSRect;
typedef unsigned int QEColor;
#define QEARGB(a,r,g,b) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
#define QERGB(r,g,b) QEARGB(0xff, r, g, b)
#define QERGB25(r,g,b) QEARGB(1, r, g, b)
#define COLOR_TRANSPARENT 0
#define QERGB_ALPHA(c) (((c) >> 24) & 255)
#define QERGB_RED(c) (((c) >> 16) & 255)
#define QERGB_GREEN(c) (((c) >> 8) & 255)
#define QERGB_BLUE(c) (((c) >> 0) & 255)
/* A qemacs style is a named set of attributes including:
* - colors for foreground and background
* - font style bits
* - font style size
*
* Styles are applied to text in successive phases:
* - the current syntax mode computes style numbers for each character
* on the line. These nubers are stored into the high bits of the
* 32-bit code point.
* - these style numbers are then extracted into an array of 32-bit composite
* style values.
* - the styles from the optional style buffer are combined into these style
* values.
* - search matches and selection styles are applied if relevant.
* - the bidirectional algorithm is applied to compute the display order
* - sequences of code-point with the same compsite style are formed into
* display units, ligatures are applied.
* - the composite style is expanded and converted into display attributes
* to trace the display unit on the device surface
*/
/* Style numbers are limited to 8 bits, the default set has 27 entries */
/* Composite styles are 32-bit values that specify
* - a style number
* - display attributes for underline, bold, blink
* - text and background colors as either palette numbers or 4096 rgb values
*/
#if 0 /* 25-bit color for FG and BG */
#define QE_TERM_STYLE_BITS 64
typedef uint64_t QETermStyle;
#define QE_STYLE_NUM 0x00FF
#define QE_STYLE_SEL 0x02000000 /* special selection style (cumulative with another style) */
#define QE_TERM_COMPOSITE 0x04000000 /* special bit to indicate qe-term composite style */
/* XXX: reversed as attribute? */
/* XXX: faint? */
#define QE_TERM_UNDERLINE 0x08000000
#define QE_TERM_BOLD 0x10000000
#define QE_TERM_ITALIC 0x20000000
#define QE_TERM_BLINK 0x40000000
#define QE_TERM_BG_BITS 25
#define QE_TERM_BG_SHIFT 32
#define QE_TERM_FG_BITS 25
#define QE_TERM_FG_SHIFT 0
#elif 1 /* 8K colors for FG and BG */
#define QE_TERM_STYLE_BITS 32
typedef uint32_t QETermStyle;
#define QE_STYLE_NUM 0x00FF
#define QE_STYLE_SEL 0x02000 /* special selection style (cumulative with another style) */
#define QE_TERM_COMPOSITE 0x04000 /* special bit to indicate qe-term composite style */
#define QE_TERM_UNDERLINE 0x08000
#define QE_TERM_BOLD 0x10000
#define QE_TERM_ITALIC 0x20000
#define QE_TERM_BLINK 0x40000
#define QE_TERM_BG_BITS 13
#define QE_TERM_BG_SHIFT 19
#define QE_TERM_FG_BITS 13
#define QE_TERM_FG_SHIFT 0
#elif 1 /* 256 colors for FG and BG */
#define QE_TERM_STYLE_BITS 32
typedef uint32_t QETermStyle;
#define QE_STYLE_NUM 0x00FF
#define QE_STYLE_SEL 0x0100 /* special selection style (cumulative with another style) */
#define QE_TERM_COMPOSITE 0x0200 /* special bit to indicate qe-term composite style */
#define QE_TERM_UNDERLINE 0x0400
#define QE_TERM_BOLD 0x0800
#define QE_TERM_ITALIC 0x1000
#define QE_TERM_BLINK 0x2000
#define QE_TERM_BG_BITS 8
#define QE_TERM_BG_SHIFT 16
#define QE_TERM_FG_BITS 8
#define QE_TERM_FG_SHIFT 0
#else /* 16 colors for FG and 16 color BG */
#define QE_TERM_STYLE_BITS 16
typedef uint16_t QETermStyle;
#define QE_STYLE_NUM 0x00FF
#define QE_STYLE_SEL 0x0100 /* special selection style (cumulative with another style) */
#define QE_TERM_COMPOSITE 0x0200 /* special bit to indicate qe-term composite style */
#define QE_TERM_UNDERLINE 0x0400
#define QE_TERM_BOLD 0x0800
#define QE_TERM_ITALIC 0x1000
#define QE_TERM_BLINK 0x2000
#define QE_TERM_BG_BITS 4
#define QE_TERM_BG_SHIFT 0
#define QE_TERM_FG_BITS 4
#define QE_TERM_FG_SHIFT 4
#endif
#define QE_TERM_DEF_FG 7
#define QE_TERM_DEF_BG 0
#define QE_TERM_BG_COLORS (1 << QE_TERM_BG_BITS)
#define QE_TERM_FG_COLORS (1 << QE_TERM_FG_BITS)
#define QE_TERM_BG_MASK ((QETermStyle)(QE_TERM_BG_COLORS - 1) << QE_TERM_BG_SHIFT)
#define QE_TERM_FG_MASK ((QETermStyle)(QE_TERM_FG_COLORS - 1) << QE_TERM_FG_SHIFT)
#define QE_TERM_MAKE_COLOR(fg, bg) (((QETermStyle)(fg) << QE_TERM_FG_SHIFT) | ((QETermStyle)(bg) << QE_TERM_BG_SHIFT))
#define QE_TERM_SET_FG(col, fg) ((col) = ((col) & ~QE_TERM_FG_MASK) | ((QETermStyle)(fg) << QE_TERM_FG_SHIFT))
#define QE_TERM_SET_BG(col, bg) ((col) = ((col) & ~QE_TERM_BG_MASK) | ((QETermStyle)(bg) << QE_TERM_BG_SHIFT))
#define QE_TERM_GET_FG(color) (((color) & QE_TERM_FG_MASK) >> QE_TERM_FG_SHIFT)
#define QE_TERM_GET_BG(color) (((color) & QE_TERM_BG_MASK) >> QE_TERM_BG_SHIFT)
typedef struct FindFileState FindFileState;
FindFileState *find_file_open(const char *path, const char *pattern);
int find_file_next(FindFileState *s, char *filename, int filename_size_max);
void find_file_close(FindFileState **sp);
int is_directory(const char *path);
int is_filepattern(const char *filespec);
void canonicalize_path(char *buf, int buf_size, const char *path);
void canonicalize_absolute_path(EditState *s, char *buf, int buf_size, const char *path1);
void canonicalize_absolute_buffer_path(EditBuffer *b, int offset,
char *buf, int buf_size,
const char *path1);
char *make_user_path(char *buf, int buf_size, const char *path);
char *reduce_filename(char *dest, int size, const char *filename);
int match_extension(const char *filename, const char *extlist);
int match_shell_handler(const char *p, const char *list);
int remove_slash(char *buf);
int append_slash(char *buf, int buf_size);
char *makepath(char *buf, int buf_size, const char *path, const char *filename);
void splitpath(char *dirname, int dirname_size,
char *filename, int filename_size, const char *pathname);
extern unsigned char const qe_digit_value__[128];
static inline int qe_digit_value(int c) {
return (unsigned int)c < 128 ? qe_digit_value__[c] : 255;
}
static inline int qe_inrange(int c, int a, int b) {
return (unsigned int)(c - a) <= (unsigned int)(b - a);
}
/* character classification tests assume ASCII superset */
static inline int qe_isspace(int c) {
/* CG: what about \v and \f */
return (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == 160);
}
static inline int qe_isblank(int c) {
return (c == ' ' || c == '\t' || c == 160);
}
static inline int qe_isdigit(int c) {
return qe_inrange(c, '0', '9');
}
static inline int qe_isdigit_(int c) {
return (qe_inrange(c, '0', '9') || c == '_');
}
static inline int qe_isupper(int c) {
return qe_inrange(c, 'A', 'Z');
}
static inline int qe_isupper_(int c) {
return (qe_inrange(c, 'A', 'Z') || c == '_');
}
static inline int qe_islower(int c) {
return qe_inrange(c, 'a', 'z');
}
static inline int qe_islower_(int c) {
return (qe_inrange(c, 'a', 'z') || (c == '_'));
}
static inline int qe_isalpha(int c) {
return qe_inrange(c | ('a' - 'A'), 'a', 'z');
}
static inline int qe_isalpha_(int c) {
return (qe_inrange(c | ('a' - 'A'), 'a', 'z') || c == '_');
}
static inline int qe_isoctdigit(int c) {
return qe_inrange(c, '0', '7');
}
static inline int qe_isoctdigit_(int c) {
return qe_inrange(c, '0', '7') || (c == '_');
}
static inline int qe_isbindigit(int c) {
return qe_inrange(c, '0', '1');
}
static inline int qe_isbindigit_(int c) {
return qe_inrange(c, '0', '1') || (c == '_');
}
static inline int qe_isxdigit(int c) {
return qe_digit_value(c) < 16;
}
static inline int qe_isxdigit_(int c) {
return (qe_digit_value(c) < 16) || (c == '_');
}
static inline int qe_isalnum(int c) {
return qe_digit_value(c) < 36;
}
static inline int qe_isalnum_(int c) {
return (qe_digit_value(c) < 36) || (c == '_');
}
static inline int qe_isword(int c) {
/* XXX: any unicode char >= 128 is considered as word. */
return qe_isalnum_(c) || (c >= 128);
}
static inline int qe_toupper(int c) {
return (qe_inrange(c, 'a', 'z') ? c + 'A' - 'a' : c);
}
static inline int qe_tolower(int c) {
return (qe_inrange(c, 'A', 'Z') ? c + 'a' - 'A' : c);
}
static inline int qe_findchar(const char *str, int c) {
return qe_inrange(c, 1, 255) && strchr(str, c) != NULL;
}
static inline int qe_indexof(const char *str, int c) {
if (qe_inrange(c, 1, 255)) {
const char *p = strchr(str, c);
if (p) return p - str;
}
return -1;
}
static inline int qe_match2(int c, int c1, int c2) {
return c == c1 || c == c2;
}
static inline int check_fcall(const unsigned int *str, int i) {
while (str[i] == ' ')
i++;
return str[i] == '(';
}
int ustr_get_identifier(char *buf, int buf_size, int c,
const unsigned int *str, int i, int n);
int ustr_get_identifier_lc(char *buf, int buf_size, int c,
const unsigned int *str, int i, int n);
int ustr_get_word(char *buf, int buf_size, int c,
const unsigned int *str, int i, int n);
int qe_strcollate(const char *s1, const char *s2);
int qe_strtobool(const char *s, int def);
void qe_strtolower(char *buf, int buf_size, const char *str);
int skip_spaces(const char **pp);
static inline int strequal(const char *s1, const char *s2) {
return !strcmp(s1, s2);
}
int memfind(const char *list, const char *p, int len);
int strfind(const char *list, const char *s);
int strxfind(const char *list, const char *s);
const char *strmem(const char *str, const void *mem, int size);
const void *memstr(const void *buf, int size, const char *str);
#define stristart(str, val, ptr) qe_stristart(str, val, ptr)
int stristart(const char *str, const char *val, const char **ptr);
int strxstart(const char *str, const char *val, const char **ptr);
int strxcmp(const char *str1, const char *str2);
int ustrstart(const unsigned int *str, const char *val, int *lenp);
int ustristart(const unsigned int *str, const char *val, int *lenp);
const unsigned int *ustrstr(const unsigned int *str, const char *val);
const unsigned int *ustristr(const unsigned int *str, const char *val);
static inline unsigned int *umemmove(unsigned int *dest,
const unsigned int *src, int count) {
return (unsigned int *)memmove(dest, src, count * sizeof(unsigned int));
}
static inline unsigned int *umemcpy(unsigned int *dest,
const unsigned int *src, int count) {
return (unsigned int *)memcpy(dest, src, count * sizeof(unsigned int));
}
int umemcmp(const unsigned int *s1, const unsigned int *s2, int count);
int qe_memicmp(const void *p1, const void *p2, int count);
const char *qe_stristr(const char *s1, const char *s2);
int strsubst(char *buf, int buf_size, const char *from,
const char *s1, const char *s2);
int strquote(char *dest, int size, const char *str, int len);
int strunquote(char *dest, int size, const char *str, int len);
void get_str(const char **pp, char *buf, int buf_size, const char *stop);
int css_get_enum(const char *str, const char *enum_str);
extern QEColor const xterm_colors[];
/* XXX: should have a more generic API with precomputed mapping scales */
/* Convert RGB triplet to a composite color */
unsigned int qe_map_color(QEColor color, QEColor const *colors, int count, int *dist);
/* Convert a composite color to an RGB triplet */
QEColor qe_unmap_color(int color, int count);
void color_complete(CompleteState *cp);
int css_define_color(const char *name, const char *value);
int css_get_color(QEColor *color_ptr, const char *p);
void css_free_colors(void);
int css_get_font_family(const char *str);
void css_union_rect(CSSRect *a, const CSSRect *b);
static inline int css_is_null_rect(const CSSRect *a) {
return (a->x2 <= a->x1 || a->y2 <= a->y1);
}
static inline void css_set_rect(CSSRect *a, int x1, int y1, int x2, int y2) {
a->x1 = x1;
a->y1 = y1;
a->x2 = x2;
a->y2 = y2;
}
/* return true if a and b intersect */
static inline int css_is_inter_rect(const CSSRect *a, const CSSRect *b) {
return (!(a->x2 <= b->x1 ||
a->x1 >= b->x2 ||
a->y2 <= b->y1 ||
a->y1 >= b->y2));
}
int get_clock_ms(void);
int get_clock_usec(void);
/* Various string packages: should unify these but keep API simple */
StringItem *set_string(StringArray *cs, int index, const char *str, int group);
StringItem *add_string(StringArray *cs, const char *str, int group);
void free_strings(StringArray *cs);
/* simple dynamic strings wrappers. The strings are always terminated
by zero except if they are empty. */
typedef struct QString {
u8 *data;
int len; /* string length excluding trailing '\0' */
} QString;
static inline void qstrinit(QString *q) {
q->data = NULL;
q->len = 0;
}
static inline void qstrfree(QString *q) {
qe_free(&q->data);
}
int qmemcat(QString *q, const u8 *data1, int len1);
int qstrcat(QString *q, const char *str);
int qprintf(QString *q, const char *fmt, ...) qe__attr_printf(2,3);
/* Dynamic buffers with static allocation */
typedef struct buf_t buf_t;
struct buf_t {
char *buf;
int size, len, pos;
};
static inline buf_t *buf_init(buf_t *bp, char *buf, int size) {
if (size > 0) {
bp->buf = buf;
bp->size = size;
*buf = '\0';
} else {
bp->buf = NULL;
bp->size = 0;
}
bp->len = bp->pos = 0;
return bp;
}
static inline buf_t *buf_attach(buf_t *bp, char *buf, int size, int pos) {
/* assuming 0 <= pos < size */
bp->buf = buf;
bp->size = size;
bp->len = bp->pos = pos;
return bp;
}
static inline int buf_avail(buf_t *bp) {
return bp->size - bp->pos - 1;
}
static inline int buf_put_byte(buf_t *bp, int c) {
if (bp->len < bp->size - 1) {
bp->buf[bp->len++] = c;
bp->buf[bp->len] = '\0';
}
return bp->pos++;
}
int buf_write(buf_t *bp, const void *src, int size);
static inline int buf_puts(buf_t *bp, const char *str) {
return buf_write(bp, str, strlen(str));
}
int buf_printf(buf_t *bp, const char *fmt, ...) qe__attr_printf(2,3);
int buf_putc_utf8(buf_t *bp, int c);
/* Bounded constant strings used in various parse functions */
typedef struct bstr_t {
const char *s;
int len;
} bstr_t;
static inline bstr_t bstr_make(const char *s) {
bstr_t bs = { s, s ? strlen(s) : 0 };
return bs;
}
bstr_t bstr_token(const char *s, int sep, const char **pp);
bstr_t bstr_get_nth(const char *s, int n);
static inline int bstr_equal(bstr_t s1, bstr_t s2) {
/* NULL and empty strings are equivalent */
return s1.len == s2.len && !memcmp(s1.s, s2.s, s1.len);
}
/* our own implementation of qsort_r() */
void qe_qsort_r(void *base, size_t nmemb, size_t size, void *thunk,
int (*compar)(void *, const void *, const void *));
/* Command line options */
enum CmdLineOptionType {
CMD_LINE_TYPE_NONE = 0, /* nothing */
CMD_LINE_TYPE_BOOL = 1, /* boolean ptr */
CMD_LINE_TYPE_INT = 2, /* int ptr */
CMD_LINE_TYPE_STRING = 3, /* string ptr */
CMD_LINE_TYPE_FVOID = 4, /* function() */
CMD_LINE_TYPE_FARG = 5, /* function(string) */
CMD_LINE_TYPE_NEXT = 6, /* next pointer */
};
typedef struct CmdLineOptionDef {
const char *desc;
enum CmdLineOptionType type;
union {
int *int_ptr;
const char **string_ptr;
void (*func_noarg)(void);
void (*func_arg)(const char *);
struct CmdLineOptionDef *next;
} u;
} CmdLineOptionDef;
#define CMD_LINE_NONE() { NULL, CMD_LINE_TYPE_NONE, { NULL }}
#define CMD_LINE_BOOL(s,n,p,h) { s "|" n "||" h, CMD_LINE_TYPE_BOOL, { .int_ptr = p }}
#define CMD_LINE_INT(s,n,a,p,h) { s "|" n "|" a "|" h, CMD_LINE_TYPE_INT, { .int_ptr = p }}
#define CMD_LINE_STRING(s,n,a,p,h) { s "|" n "|" a "|" h, CMD_LINE_TYPE_STRING, { .string_ptr = p }}
#define CMD_LINE_FVOID(s,n,p,h) { s "|" n "||" h, CMD_LINE_TYPE_FVOID, { .func_noarg = p }}
#define CMD_LINE_FARG(s,n,a,p,h) { s "|" n "|" a "|" h, CMD_LINE_TYPE_FARG, { .func_arg = p }}
#define CMD_LINE_LINK() { NULL, CMD_LINE_TYPE_NEXT, { NULL }}
void qe_register_cmd_line_options(CmdLineOptionDef *table);
int find_resource_file(char *path, int path_size, const char *pattern);
typedef int (CSSAbortFunc)(void *);
static inline int max(int a, int b) {
if (a > b)
return a;
else
return b;
}
static inline int maxp(int *pa, int b) {
int a = *pa;
if (a > b)
return a;
else
return *pa = b;
}
static inline int max3(int a, int b, int c) {
return max(max(a, b), c);
}
static inline int min(int a, int b) {
if (a < b)
return a;
else
return b;
}
static inline int minp(int *pa, int b) {
int a = *pa;
if (a < b)
return a;
else
return *pa = b;
}
static inline int min3(int a, int b, int c) {
return min(min(a, b), c);
}
static inline int clamp(int a, int b, int c) {
if (a < b)
return b;
else
if (a > c)
return c;
else
return a;
}
static inline int clampp(int *pa, int b, int c) {
int a = *pa;
if (a < b)
return *pa = b;
else
if (a > c)
return *pa = c;
else
return a;
}
static inline int compute_percent(int a, int b) {
return b <= 0 ? 0 : (int)((long long)a * 100 / b);
}
int compose_keys(unsigned int *keys, int *nb_keys);
int strtokey(const char **pp);
int strtokeys(const char *keystr, unsigned int *keys, int max_keys);
int buf_put_key(buf_t *out, int key);
int buf_put_keys(buf_t *out, unsigned int *keys, int nb_keys);
/* charset.c */
/* maximum number of bytes for a character in all the supported charsets */
#define MAX_CHAR_BYTES 6
typedef struct CharsetDecodeState CharsetDecodeState;
#if defined(__cplusplus)
typedef struct QECharset QECharset;
#else
typedef const struct QECharset QECharset;
#endif
struct QECharset {
const char *name;
const char *aliases;
int (*probe_func)(QECharset *charset, const u8 *buf, int size);
void (*decode_init)(CharsetDecodeState *s);
int (*decode_func)(CharsetDecodeState *s);
/* return NULL if cannot encode. Currently no state since speed is
not critical yet */
u8 *(*encode_func)(QECharset *charset, u8 *buf, int size);
void (*get_pos_func)(CharsetDecodeState *s, const u8 *buf, int size,
int *line_ptr, int *col_ptr);
int (*get_chars_func)(CharsetDecodeState *s, const u8 *buf, int size);
int (*goto_char_func)(CharsetDecodeState *s, const u8 *buf, int size, int pos);
int (*goto_line_func)(CharsetDecodeState *s, const u8 *buf, int size, int lines);
unsigned int char_size : 3;
unsigned int variable_size : 1;
unsigned int table_alloc : 1; /* true if CharsetDecodeState.table must be malloced */
/* private data for some charsets */
u8 eol_char; /* 0x0A for ASCII, 0x25 for EBCDIC */
u8 min_char, max_char;
const unsigned short *encode_table;
const unsigned short *private_table;
struct QECharset *next;
};
extern struct QECharset *first_charset;
/* predefined charsets */
extern struct QECharset charset_raw;
extern struct QECharset charset_8859_1;
extern struct QECharset charset_utf8;
extern struct QECharset charset_vt100; /* used for the tty output */
extern struct QECharset charset_mac_roman;
extern struct QECharset charset_ucs2le, charset_ucs2be;
extern struct QECharset charset_ucs4le, charset_ucs4be;
typedef enum EOLType {
EOL_UNIX = 0,
EOL_DOS,
EOL_MAC,
} EOLType;
struct CharsetDecodeState {
/* 256 ushort table for hyper fast decoding */
const unsigned short *table;
int char_size;
EOLType eol_type;
int eol_char;
const u8 *p;
/* slower decode function for complicated cases */
int (*decode_func)(CharsetDecodeState *s);
void (*get_pos_func)(CharsetDecodeState *s, const u8 *buf, int size,
int *line_ptr, int *col_ptr);
QECharset *charset;
};
#define INVALID_CHAR 0xfffd
#define ESCAPE_CHAR 0xffff
void charset_init(void);
int charset_more_init(void);
int charset_jis_init(void);
void qe_register_charset(struct QECharset *charset);
extern unsigned char const utf8_length[256];
static inline int utf8_is_trailing_byte(int c) { return (c & 0xC0) == 0x80; }
int utf8_encode(char *q, int c);
int utf8_decode(const char **pp);
char *utf8_char_to_string(char *buf, int c);
int utf8_to_unicode(unsigned int *dest, int dest_length, const char *str);
void charset_complete(CompleteState *cp);
QECharset *find_charset(const char *str);
void charset_decode_init(CharsetDecodeState *s, QECharset *charset,
EOLType eol_type);
void charset_decode_close(CharsetDecodeState *s);
void charset_get_pos_8bit(CharsetDecodeState *s, const u8 *buf, int size,
int *line_ptr, int *col_ptr);
int charset_get_chars_8bit(CharsetDecodeState *s, const u8 *buf, int size);
int charset_goto_char_8bit(CharsetDecodeState *s, const u8 *buf, int size, int pos);
int charset_goto_line_8bit(CharsetDecodeState *s, const u8 *buf, int size, int nlines);
QECharset *detect_charset(const u8 *buf, int size, EOLType *eol_typep);
void decode_8bit_init(CharsetDecodeState *s);
int decode_8bit(CharsetDecodeState *s);
u8 *encode_8bit(QECharset *charset, u8 *q, int c);
int unicode_tty_glyph_width(unsigned int ucs);
static inline int qe_isaccent(int c) {
return c >= 0x300 && unicode_tty_glyph_width(c) == 0;
}
/* arabic.c */
int arab_join(unsigned int *line, unsigned int *ctog, int len);
/* indic.c */
int devanagari_log2vis(unsigned int *str, unsigned int *ctog, int len);
/* unicode_join.c */
int unicode_to_glyphs(unsigned int *dst, unsigned int *char_to_glyph_pos,
int dst_size, unsigned int *src, int src_size,
int reverse);
int combine_accent(unsigned int *buf, int c, int accent);
int expand_ligature(unsigned int *buf, int c);
int load_ligatures(void);
void unload_ligatures(void);
/* qe event handling */
enum QEEventType {
QE_KEY_EVENT,
QE_EXPOSE_EVENT, /* full redraw */
QE_UPDATE_EVENT, /* update content */
QE_BUTTON_PRESS_EVENT, /* mouse button press event */
QE_BUTTON_RELEASE_EVENT, /* mouse button release event */
QE_MOTION_EVENT, /* mouse motion event */
QE_SELECTION_CLEAR_EVENT, /* request selection clear (X11 type selection) */
};
#define KEY_CTRL(c) ((c) & 0x001f)
#define KEY_META(c) ((c) | 0xe000)
#define KEY_ESC1(c) ((c) | 0xe100)
#define KEY_CTRLX(c) ((c) | 0xe200)
#define KEY_CTRLXRET(c) ((c) | 0xe300)
#define KEY_CTRLH(c) ((c) | 0xe500)
#define KEY_CTRLC(c) ((c) | 0xe600)
#define KEY_IS_SPECIAL(c) ((c) >= 0xe000 && (c) < 0xf000)
#define KEY_IS_CONTROL(c) (((c) >= 0 && (c) < 32) || (c) == 127)
#define KEY_NONE 0xffff
#define KEY_DEFAULT 0xe401 /* to handle all non special keys */
#define KEY_TAB KEY_CTRL('i')
#define KEY_RET KEY_CTRL('m')
#define KEY_ESC KEY_CTRL('[')
#define KEY_SPC 0x0020
#define KEY_DEL 127 // kbs
#define KEY_BS KEY_CTRL('h') // kbs
#define KEY_UP KEY_ESC1('A') // kcuu1
#define KEY_DOWN KEY_ESC1('B') // kcud1
#define KEY_RIGHT KEY_ESC1('C') // kcuf1
#define KEY_LEFT KEY_ESC1('D') // kcub1
#define KEY_CTRL_UP KEY_ESC1('a')
#define KEY_CTRL_DOWN KEY_ESC1('b')
#define KEY_CTRL_RIGHT KEY_ESC1('c')
#define KEY_CTRL_LEFT KEY_ESC1('d')
#define KEY_CTRL_END KEY_ESC1('f')
#define KEY_CTRL_HOME KEY_ESC1('h')
#define KEY_CTRL_PAGEUP KEY_ESC1('i')
#define KEY_CTRL_PAGEDOWN KEY_ESC1('j')
#define KEY_SHIFT_TAB KEY_ESC1('Z') // kcbt
#define KEY_HOME KEY_ESC1(1) // khome
#define KEY_INSERT KEY_ESC1(2) // kich1
#define KEY_DELETE KEY_ESC1(3) // kdch1
#define KEY_END KEY_ESC1(4) // kend
#define KEY_PAGEUP KEY_ESC1(5) // kpp
#define KEY_PAGEDOWN KEY_ESC1(6) // knp
#define KEY_F1 KEY_ESC1(11)
#define KEY_F2 KEY_ESC1(12)
#define KEY_F3 KEY_ESC1(13)
#define KEY_F4 KEY_ESC1(14)
#define KEY_F5 KEY_ESC1(15)
#define KEY_F6 KEY_ESC1(17)
#define KEY_F7 KEY_ESC1(18)
#define KEY_F8 KEY_ESC1(19)
#define KEY_F9 KEY_ESC1(20)
#define KEY_F10 KEY_ESC1(21)
#define KEY_F11 KEY_ESC1(23)
#define KEY_F12 KEY_ESC1(24)
#define KEY_F13 KEY_ESC1(25)
#define KEY_F14 KEY_ESC1(26)
#define KEY_F15 KEY_ESC1(28)
#define KEY_F16 KEY_ESC1(29)
#define KEY_F17 KEY_ESC1(31)
#define KEY_F18 KEY_ESC1(32)
#define KEY_F19 KEY_ESC1(33)
#define KEY_F20 KEY_ESC1(34)
#define KEY_MOUSE_MOVE KEY_NONE -1
#define KEY_MOUSE_BUTTON_1 KEY_NONE -2
#define KEY_MOUSE_BUTTON_2 KEY_NONE -3
#define KEY_MOUSE_BUTTON_3 KEY_NONE -4
#define KEY_MOUSE_WHEEL_UP KEY_NONE -5
#define KEY_MOUSE_WHEEL_DOWN KEY_NONE -6
typedef struct QEKeyEvent {
enum QEEventType type;
int key;
} QEKeyEvent;
typedef struct QEExposeEvent {
enum QEEventType type;
/* currently, no more info */
} QEExposeEvent;
#define QE_BUTTON_LEFT 0x0001
#define QE_BUTTON_MIDDLE 0x0002
#define QE_BUTTON_RIGHT 0x0004
#define QE_WHEEL_UP 0x0008
#define QE_WHEEL_DOWN 0x0010
/* should probably go somewhere else, or in the config file */
/* how many text lines to scroll when mouse wheel is used */
#define WHEEL_SCROLL_STEP 4
typedef struct QEButtonEvent {
enum QEEventType type;
int x;
int y;
int button;
} QEButtonEvent;
typedef struct QEMotionEvent {
enum QEEventType type;
int x;
int y;
} QEMotionEvent;
typedef union QEEvent {
enum QEEventType type;
QEKeyEvent key_event;
QEExposeEvent expose_event;
QEButtonEvent button_event;
QEMotionEvent motion_event;
} QEEvent;
void qe_handle_event(QEEvent *ev);
/* CG: Should optionally attach grab to a window */
/* CG: Should deal with opaque object life cycle */
void qe_grab_keys(void (*cb)(void *opaque, int key), void *opaque);
void qe_ungrab_keys(void);
KeyDef *qe_find_binding(unsigned int *keys, int nb_keys, KeyDef *kd);
KeyDef *qe_find_current_binding(unsigned int *keys, int nb_keys, ModeDef *m);
#define COLORED_MAX_LINE_SIZE 4096
/* colorize & transform a line, lower level then ColorizeFunc */
/* XXX: should return `len`, the number of valid codepoints copied to
* destination excluding the null terminator and newline if present.
* Truncation can be detected by testing if a newline is present
* at this offset.
*/
typedef int (*GetColorizedLineFunc)(EditState *s,
unsigned int *buf, int buf_size,
QETermStyle *sbuf,
int offset, int *offsetp, int line_num);
struct QEColorizeContext {
EditState *s;
EditBuffer *b;
int offset;
int colorize_state;
int state_only;
int combine_start, combine_stop; /* region for combine_static_colorized_line() */
int cur_pos; /* position of cursor in line or -1 if outside line */
};