1 /*
2  *	Device independent tty interface for JOE
3  *	Copyright
4  *		(C) 1992 Joseph H. Allen
5  *
6  *	This file is part of JOE (Joe's Own Editor)
7  */
8 
9 struct hentry {
10 	ptrdiff_t	next;
11 	ptrdiff_t	loc;
12 };
13 
14 /* Each terminal has one of these: terminal capability database */
15 
16 #ifdef __MSDOS__
17 
18 struct scrn {
19 	ptrdiff_t	li;		/* Height of screen */
20 	ptrdiff_t	co;		/* Width of screen */
21 	short	*scrn;		/* Buffer */
22 	int	scroll;
23 	int	insdel;
24 	int	*updtab;	/* Lines which need to be updated */
25 	HIGHLIGHT_STATE *syntax;
26 	int	*compose;
27 	ptrdiff_t	*sary;
28 };
29 
30 #else
31 
32 #define COMPOSE 4 /* Maximum number of characters per cell (one start plus some number of combining) */
33 /* If there are more than (COMPOSE - 1) combining characters, JOE will emit them, but during a line update
34  * it keeps re-emitting them even if not necessary (because the screen buffer doesn't have enough
35  * to record them all, so it doesn't know if the cell is already correct during an update). */
36 
37 struct scrn {
38 	CAP	*cap;		/* Termcap/Terminfo data */
39 
40 	ptrdiff_t	li;		/* Screen height */
41         ptrdiff_t	co;		/* Screen width */
42 
43 	const char	*ti;		/* Initialization string */
44 	const char	*cl;		/* Home and clear screen... really an
45 				   init. string */
46 	const char	*cd;		/* Clear to end of screen */
47 	const char	*te;		/* Restoration string */
48 	const char	*brp;		/* Bracketed paste mode */
49 	const char	*bre;		/* Stop bracketed paste */
50 
51 	int	haz;		/* Terminal can't print ~s */
52 	int	os;		/* Terminal overstrikes */
53 	int	eo;		/* Can use blank to erase even if os */
54 	int	ul;		/* _ overstrikes */
55 	int	am;		/* Terminal has autowrap, but not magicwrap */
56 	int	xn;		/* Terminal has magicwrap */
57 
58 	const char	*so;		/* Enter standout (inverse) mode */
59 	const char	*se;		/* Exit standout mode */
60 
61 	const char	*us;		/* Enter underline mode */
62 	const char	*ue;		/* Exit underline mode */
63 	const char	*uc;		/* Single time underline character */
64 
65 	int	ms;		/* Ok to move when in standout/underline mode */
66 
67 	const char	*mb;		/* Enter blinking mode */
68 	const char	*md;		/* Enter bold mode */
69 	const char	*mh;		/* Enter dim mode */
70 	const char	*mr;		/* Enter inverse mode */
71 	const char      *stricken;      /* Enter strikeout mode */
72 	const char      *dunderline;    /* Enter double-underline mode */
73 	const char	*me;		/* Exit above modes */
74 
75 	const char	*ZH;		/* Enter italic mode */
76 	const char	*ZR;		/* Exit italic mode */
77 
78 	const char	*Sb;		/* Set background color */
79 	const char	*Sf;		/* Set foreground color */
80 	int	Co;			/* No. of colors */
81 	int	ut;		/* Screen erases with background color */
82 
83 	int	da, db;		/* Extra lines exist above, below */
84 	const char	*al, *dl, *AL, *DL;	/* Insert/delete lines */
85 	const char	*cs;		/* Set scrolling region */
86 	int	rr;		/* Set for scrolling region relative addressing */
87 	const char	*sf, *SF, *sr, *SR;	/* Scroll */
88 
89 	const char	*dm, *dc, *DC, *ed;	/* Delete characters */
90 	const char	*im, *ic, *IC, *ip, *ei;	/* Insert characters */
91 	int	mi;		/* Set if ok to move while in insert mode */
92 
93 	const char	*bs;		/* Move cursor left 1 */
94 	ptrdiff_t	cbs;
95 	const char	*lf;		/* Move cursor down 1 */
96 	ptrdiff_t	clf;
97 	const char	*up;		/* Move cursor up 1 */
98 	ptrdiff_t	cup;
99 	const char	*nd;		/* Move cursor right 1 */
100 
101 	const char	*ta;		/* Move cursor to next tab stop */
102 	ptrdiff_t	cta;
103 	const char	*bt;		/* Move cursor to previous tab stop */
104 	ptrdiff_t	cbt;
105 	ptrdiff_t	tw;		/* Tab width */
106 
107 	const char	*ho;		/* Home cursor to upper left */
108 	ptrdiff_t	cho;
109 	const char	*ll;		/* Home cursor to lower left */
110 	ptrdiff_t	cll;
111 	const char	*cr;		/* Move cursor to left edge */
112 	ptrdiff_t	ccr;
113 	const char	*RI;		/* Move cursor right n */
114 	ptrdiff_t	cRI;
115 	const char	*LE;		/* Move cursor left n */
116         ptrdiff_t	cLE;
117 	const char	*UP;		/* Move cursor up n */
118 	ptrdiff_t	cUP;
119 	const char	*DO;		/* Move cursor down n */
120 	ptrdiff_t	cDO;
121 	const char	*ch;		/* Set cursor column */
122 	ptrdiff_t	cch;
123 	const char	*cv;		/* Set cursor row */
124 	ptrdiff_t	ccv;
125 	const char	*cV;		/* Goto beginning of specified line */
126 	ptrdiff_t	ccV;
127 	const char	*cm;		/* Set cursor row and column */
128 	ptrdiff_t	ccm;
129 
130 	const char	*ce;		/* Clear to end of line */
131 	ptrdiff_t	cce;
132 
133 	int 	assume_256;	/* Assume terminal has 256 color mode, but use
134 	                           regular mode for standard colors just in case */
135 
136 	int	truecolor;	/* Terminal supports true color */
137 	int	*palette;	/* Truecolor RGB palette */
138 
139 	/* Basic abilities */
140 	int	scroll;		/* Set to use scrolling */
141 	int	insdel;		/* Set to use insert/delete within line */
142 
143 	/* Current state of terminal */
144 	int	(*scrn)[COMPOSE];		/* Characters on screen */
145 	int	*attr;		/* Attributes on screen */
146 	ptrdiff_t	x, y;		/* Current cursor position (-1 for unknown) */
147 	ptrdiff_t	top, bot;	/* Current scrolling region */
148 	int	attrib;		/* Current character attributes */
149 	int	ins;		/* Set if we're in insert mode */
150 
151 	int	*updtab;	/* Dirty lines table */
152 	int	avattr;		/* Bits set for available attributes */
153 	ptrdiff_t	*sary;		/* Scroll buffer array */
154 
155 	int	*compose;	/* Line compose buffer */
156 	ptrdiff_t	*ofst;		/* stuff for magic */
157 	struct hentry	*htab;
158 	struct hentry	*ary;
159 };
160 
161 #endif
162 
163 extern int skiptop;
164 
165 /* SCRN *nopen(void);
166  *
167  * Open the screen (sets TTY mode so that screen may be used immediately after
168  * the 'nopen').
169  */
170 SCRN *nopen(CAP *cap);
171 
172 /* int nresize(SCRN *t,int w,int h);
173  *
174  * Change size of screen.  For example, call this when you find out that
175  * the Xterm changed size.
176  */
177 int nresize(SCRN *t, ptrdiff_t w, ptrdiff_t h);
178 
179 /* void nredraw(SCRN *t);
180  *
181  * Invalidate all state variables for the terminal.  This way, everything gets
182  * redrawn.
183  */
184 void nredraw(SCRN *t);
185 
186 void npartial(SCRN *t);
187 void nescape(SCRN *t);
188 void nreturn(SCRN *t);
189 
190 /* void nclose(SCRN *t);
191  *
192  * Close the screen and restore TTY to initial state.
193  *
194  * if 'flg' is set, tclose doesn't mess with the signals.
195  */
196 void nclose(SCRN *t);
197 
198 /* int cpos(SCRN *t,int x,int y);
199  *
200  * Set cursor position
201  */
202 int cpos(register SCRN *t, register ptrdiff_t x, register ptrdiff_t y);
203 
204 /* int attr(SCRN *t,int a);
205  *
206  * Set attributes
207  */
208 int set_attr(SCRN *t, int c);
209 
210 /* Encode character as utf8 */
211 void utf8_putc(int c);
212 
213 /* void outatr(SCRN *t,int *scrn,int *attr,int x,int y,int c,int a);
214  *
215  * Output a character at the given screen coordinate.  The cursor position
216  * after this function is executed is indeterminate.
217  */
218 
219 /* Character attribute bits */
220 
221 #ifdef __MSDOS__
222 
223 #define INVERSE 1
224 #define UNDERLINE 2
225 #define BOLD 4
226 #define BLINK 8
227 #define DIM 16
228 #define ITALIC 32
229 
230 extern unsigned atab[];
231 
232 #define outatr(t,scrn,attr,x,y,c,a) do { \
233 	(t); \
234 	(x); \
235 	(y); \
236 	*(scrn) = ((unsigned)(c) | atab[a]); \
237 } while(0)
238 
239 #else
240 
241 #define CONTEXT_COMMENT	1
242 #define CONTEXT_STRING	2
243 #define CONTEXT_MASK	(CONTEXT_COMMENT+CONTEXT_STRING)
244 
245 #define DOUBLE_UNDERLINE   8
246 #define CROSSED_OUT       16
247 #define ITALIC		  32
248 #define INVERSE		  64
249 #define UNDERLINE	 128
250 #define BOLD		 256
251 #define BLINK		 512
252 #define DIM		1024
253 #define AT_MASK		(INVERSE+UNDERLINE+BOLD+BLINK+DIM+ITALIC+DOUBLE_UNDERLINE+CROSSED_OUT)
254 
255 #define BG_SHIFT	11
256 #define BG_VALUE	(255<<BG_SHIFT)
257 #define BG_NOT_DEFAULT	(256<<BG_SHIFT)
258 #define BG_TRUECOLOR	(512<<BG_SHIFT)
259 #define BG_MASK		(1023<<BG_SHIFT)
260 
261 #define BG_DEFAULT	(0<<BG_SHIFT)
262 
263 /* #define BG_COLOR(color)	(BG_NOT_DEFAULT^(color)<<BG_SHIFT) */
264 #define BG_COLOR(color)	(color)
265 
266 #define BG_BLACK	(BG_NOT_DEFAULT|(0<<BG_SHIFT))
267 #define BG_RED		(BG_NOT_DEFAULT|(1<<BG_SHIFT))
268 #define BG_GREEN	(BG_NOT_DEFAULT|(2<<BG_SHIFT))
269 #define BG_YELLOW	(BG_NOT_DEFAULT|(3<<BG_SHIFT))
270 #define BG_BLUE		(BG_NOT_DEFAULT|(4<<BG_SHIFT))
271 #define BG_MAGENTA	(BG_NOT_DEFAULT|(5<<BG_SHIFT))
272 #define BG_CYAN		(BG_NOT_DEFAULT|(6<<BG_SHIFT))
273 #define BG_WHITE	(BG_NOT_DEFAULT|(7<<BG_SHIFT))
274 #define BG_BBLACK	(BG_NOT_DEFAULT|(8<<BG_SHIFT))
275 #define BG_BRED		(BG_NOT_DEFAULT|(9<<BG_SHIFT))
276 #define BG_BGREEN	(BG_NOT_DEFAULT|(10<<BG_SHIFT))
277 #define BG_BYELLOW	(BG_NOT_DEFAULT|(11<<BG_SHIFT))
278 #define BG_BBLUE	(BG_NOT_DEFAULT|(12<<BG_SHIFT))
279 #define BG_BMAGENTA	(BG_NOT_DEFAULT|(13<<BG_SHIFT))
280 #define BG_BCYAN	(BG_NOT_DEFAULT|(14<<BG_SHIFT))
281 #define BG_BWHITE	(BG_NOT_DEFAULT|(15<<BG_SHIFT))
282 
283 #define FG_SHIFT	21
284 #define FG_VALUE	(255<<FG_SHIFT)
285 #define FG_NOT_DEFAULT	(256<<FG_SHIFT)
286 #define FG_TRUECOLOR	(512<<FG_SHIFT)
287 #define FG_MASK		(1023<<FG_SHIFT)
288 
289 #define FG_DEFAULT	(0<<FG_SHIFT)
290 #define FG_BWHITE	(FG_NOT_DEFAULT|(15<<FG_SHIFT))
291 #define FG_BCYAN	(FG_NOT_DEFAULT|(14<<FG_SHIFT))
292 #define FG_BMAGENTA	(FG_NOT_DEFAULT|(13<<FG_SHIFT))
293 #define FG_BBLUE	(FG_NOT_DEFAULT|(12<<FG_SHIFT))
294 #define FG_BYELLOW	(FG_NOT_DEFAULT|(11<<FG_SHIFT))
295 #define FG_BGREEN	(FG_NOT_DEFAULT|(10<<FG_SHIFT))
296 #define FG_BRED		(FG_NOT_DEFAULT|(9<<FG_SHIFT))
297 #define FG_BBLACK	(FG_NOT_DEFAULT|(8<<FG_SHIFT))
298 #define FG_WHITE	(FG_NOT_DEFAULT|(7<<FG_SHIFT))
299 #define FG_CYAN		(FG_NOT_DEFAULT|(6<<FG_SHIFT))
300 #define FG_MAGENTA	(FG_NOT_DEFAULT|(5<<FG_SHIFT))
301 #define FG_BLUE		(FG_NOT_DEFAULT|(4<<FG_SHIFT))
302 #define FG_YELLOW	(FG_NOT_DEFAULT|(3<<FG_SHIFT))
303 #define FG_GREEN	(FG_NOT_DEFAULT|(2<<FG_SHIFT))
304 #define FG_RED		(FG_NOT_DEFAULT|(1<<FG_SHIFT))
305 #define FG_BLACK	(FG_NOT_DEFAULT|(0<<FG_SHIFT))
306 
307 void outatr_complete(SCRN *t);
308 void outatr(struct charmap *map,SCRN *t,int (*scrn)[COMPOSE],int *attrf,ptrdiff_t xx,ptrdiff_t yy,int c,int a);
309 
310 #endif
311 
312 /* int eraeol(SCRN *t,int x,int y);
313  *
314  * Erase from screen coordinate to end of line.
315  */
316 int eraeol(SCRN *t, ptrdiff_t x, ptrdiff_t y, int atr);
317 
318 /* void nscrlup(SCRN *t,int top,int bot,int amnt);
319  *
320  * Buffered scroll request.  Request that some lines up.  'top' and 'bot'
321  * indicate which lines to scroll.  'bot' is the last line to scroll + 1.
322  * 'amnt' is distance in lines to scroll.
323  */
324 void nscrlup(SCRN *t, ptrdiff_t top, ptrdiff_t bot, ptrdiff_t amnt);
325 
326 /* void nscrldn(SCRN *t,int top,int bot,int amnt);
327  *
328  * Buffered scroll request.  Scroll some lines down.  'top' and 'bot'
329  * indicate which lines to scroll.  'bot' is the last line to scroll + 1.
330  * 'amnt' is distance in lines to scroll.
331  */
332 void nscrldn(SCRN *t, ptrdiff_t top, ptrdiff_t bot, ptrdiff_t amnt);
333 
334 /* void nscroll(SCRN *t);
335  *
336  * Execute buffered scroll requests
337  */
338 void nscroll(SCRN *t, int atr);
339 
340 /* void magic(SCRN *t,int y,int *cur,int *new);
341  *
342  * Figure out and execute line shifting
343  */
344 void magic(SCRN *t, ptrdiff_t y, int *cs, int *ca, int *s, int *a,ptrdiff_t placex);
345 
346 int clrins(SCRN *t);
347 
348 int meta_color(const char *s);
349 
350 /* Generate a field */
351 void genfield(SCRN *t,int (*scrn)[COMPOSE],int *attr,ptrdiff_t x,ptrdiff_t y,ptrdiff_t ofst,const char *s,ptrdiff_t len,int atr,ptrdiff_t width,int flg,int *fmt);
352 
353 /* Column width of a string takes into account utf-8) */
354 ptrdiff_t txtwidth(const char *s,ptrdiff_t len);
355 
356 off_t txtwidth1(struct charmap *map, off_t tabwidth, const char *s, ptrdiff_t len);
357 
358 /* Generate a field: formatted */
359 void genfmt(SCRN *t, ptrdiff_t x, ptrdiff_t y, ptrdiff_t ofst, const char *s, int atr, int iatr, int flg);
360 
361 /* Column width of formatted string */
362 ptrdiff_t fmtlen(const char *s);
363 
364 /* Offset within formatted string of particular column */
365 ptrdiff_t fmtpos(const char *s, ptrdiff_t goal);
366 
367 /* Set extended colors palette */
368 void setextpal(SCRN *t, int *palette);
369 
370 extern int bg_text;
371 extern int env_lines;
372 extern int env_columns;
373 extern int notite;
374 extern int brpaste;
375 extern int nolinefeeds;
376 extern int opt_usetabs;
377 extern int assume_color;
378 extern int assume_256color;
379