125068Sjaap #ifndef lint
2*64045Sbostic static char sccsid[] = "@(#)maktab.c	1.4 (Berkeley) 07/27/93";
325068Sjaap #endif lint
425068Sjaap 
525068Sjaap 
625090Sjaap /*
725090Sjaap  * compute tab stops
825090Sjaap  */
925068Sjaap 
1025068Sjaap #define tx(a)		(a>0 && a<128)
1125068Sjaap 
1225068Sjaap #define FN(i,c)		font[stynum[i]][c]
1325068Sjaap #define SZ(i,c)		csize[stynum[i]][c]
1425068Sjaap 
1525068Sjaap #include "defs.h"
1625068Sjaap #include "ext.h"
1725068Sjaap 
1825068Sjaap /*
1925068Sjaap  * define the tab stops of the table
2025068Sjaap  */
maktab()2125068Sjaap maktab()
2225068Sjaap {
2325068Sjaap 	int icol, ilin, tsep, k, ik, vforml, il, text;
2425068Sjaap 	int doubled[MAXCOL], acase[MAXCOL];
2525068Sjaap 	char *s;
2625068Sjaap 
2725068Sjaap 	for(icol = 0; icol < ncol; icol++){
2825068Sjaap 		doubled[icol] = acase[icol] = 0;
2925068Sjaap 		printf(".nr %2s 0\n", reg(icol, CRIGHT));
3025068Sjaap 		for(text = 0; text < 2; text++){
3125068Sjaap 			if(text)
3225068Sjaap 				printf(".%2s\n.rm %2s\n", reg(icol, CRIGHT),
3325068Sjaap 							reg(icol, CRIGHT));
3425068Sjaap 			for(ilin = 0; ilin < nlin; ilin++){
3525068Sjaap 				if(instead[ilin] || fullbot[ilin]){
3625068Sjaap 					continue;
3725090Sjaap 				}
3825068Sjaap 				vforml = ilin;
3925068Sjaap 				for(il = prev(ilin);
4025068Sjaap 				    il >= 0 && vspen(table[il][icol].col);
4125068Sjaap 				    il = prev(il))
4225068Sjaap 					vforml = il;
4325068Sjaap 				if(fspan(vforml, icol)){
4425068Sjaap 					continue;
4525090Sjaap 				}
4625068Sjaap 				if(filler(table[ilin][icol].col)){
4725068Sjaap 					continue;
4825090Sjaap 				}
4925068Sjaap 				if((ctop[stynum[ilin]][icol] & ZEROW) != 0){
5025068Sjaap 					continue;
5125090Sjaap 				}
5225068Sjaap 				switch(ctype(vforml, icol)){
5325068Sjaap 
5425068Sjaap 				case 'a':
5525068Sjaap 					acase[icol] = 1;
5625068Sjaap 					s = table[ilin][icol].col;
5725068Sjaap 					if(s > 0 && s < (char *)128 && text){
5825068Sjaap 						if(doubled[icol] == 0)
5925068Sjaap 							printf(
6025068Sjaap 							 ".nr %d 0\n.nr %d 0\n",
6125068Sjaap 							 S1, S2);
6225068Sjaap 						doubled[icol] = 1;
6325068Sjaap 						printf(
6425068Sjaap 					  ".if \\n(%c->\\n(%d .nr %d \\n(%c-\n",
6525068Sjaap 								s, S2, S2, s);
6625068Sjaap 					}
6725068Sjaap 				case 'n':
6825068Sjaap 					if(table[ilin][icol].rcol != 0){
6925068Sjaap 						if(doubled[icol] == 0
7025068Sjaap 							 && text == 0)
7125068Sjaap 							printf(
7225068Sjaap 							 ".nr %d 0\n.nr %d 0\n",
7325068Sjaap 							 S1, S2);
7425068Sjaap 						doubled[icol] = 1;
7525068Sjaap 						if(real(s=table[ilin][icol].col)
7625068Sjaap 						    && !vspen(s)){
7725068Sjaap 							if(tx((int)s) != text)
7825068Sjaap 								continue;
7925068Sjaap 							printf(".nr %d ", TMP);
8025068Sjaap 							wide(s,
8125068Sjaap 							      FN(vforml, icol),
8225068Sjaap 							      SZ(vforml, icol));
8325068Sjaap 							printf("\n");
8425068Sjaap 							printf(
8525068Sjaap 					   ".if \\n(%d<\\n(%d .nr %d \\n(%d\n",
8625068Sjaap 							      S1, TMP, S1, TMP);
8725068Sjaap 						}
8825068Sjaap 						if(text == 0
8925068Sjaap 						   && real(s=table[ilin][icol].rcol)
9025068Sjaap 						   && !vspen(s) && !barent(s)){
9125068Sjaap 							printf(
9225068Sjaap 							   ".nr %d \\w%c%s%c\n",
9325068Sjaap 							  	TMP, F1, s, F1);
9425068Sjaap 							printf(
9525068Sjaap 					    ".if \\n(%d<\\n(%d .nr %d \\n(%d\n",
9625068Sjaap 							      S2, TMP, S2, TMP);
9725068Sjaap 						}
9825068Sjaap 						continue;
9925068Sjaap 					}
10025068Sjaap 				case 'r':
10125068Sjaap 				case 'c':
10225068Sjaap 				case 'l':
10325068Sjaap 					if(real(s = table[ilin][icol].col)
10425068Sjaap 					   && !vspen(s)){
10525068Sjaap 						if(tx((int)s) != text)
10625068Sjaap 							continue;
10725068Sjaap 						printf(".nr %d ", TMP);
10825068Sjaap 						wide(s,
10925068Sjaap 							FN(vforml, icol),
11025068Sjaap 							SZ(vforml, icol));
11125068Sjaap 						printf("\n");
11225068Sjaap 						printf(
11325068Sjaap 	   ".if \\n(%2s<\\n(%d .nr %2s \\n(%d\n", reg(icol, CRIGHT), TMP,
11425068Sjaap 							reg(icol, CRIGHT), TMP);
11525068Sjaap 					}
11625068Sjaap 				}
11725068Sjaap 			}
11825068Sjaap 		}
11925068Sjaap 		if(acase[icol]){
12025068Sjaap 			printf(".if \\n(%d>=\\n(%2s .nr %2s \\n(%du+2n\n",
12125068Sjaap 				S2, reg(icol, CRIGHT), reg(icol, CRIGHT), S2);
12225068Sjaap 		}
12325068Sjaap 		if(doubled[icol]){
12425068Sjaap 			printf(".nr %2s \\n(%d\n", reg(icol, CMID), S1);
12525068Sjaap 			printf(".nr %d \\n(%2s+\\n(%d\n", TMP, reg(icol, CMID),
12625068Sjaap 									S2);
12725068Sjaap 			printf(".if \\n(%d>\\n(%2s .nr %2s \\n(%d\n", TMP,
12825068Sjaap 				    reg(icol, CRIGHT), reg(icol, CRIGHT), TMP);
12925068Sjaap 			printf(
13025068Sjaap 			     ".if \\n(%d<\\n(%2s .nr %2s +(\\n(%2s-\\n(%d)/2\n",
13125068Sjaap 			     TMP, reg(icol, CRIGHT), reg(icol, CMID),
13225068Sjaap 			     				reg(icol, CRIGHT), TMP);
13325068Sjaap 		}
13425068Sjaap 		if(cll[icol][0]){
13525068Sjaap 			printf(".nr %d %sn\n", TMP, cll[icol]);
13625068Sjaap 			printf(".if \\n(%2s<\\n(%d .nr %2s \\n(%d\n",
13725068Sjaap 				reg(icol, CRIGHT), TMP, reg(icol, CRIGHT), TMP);
13825068Sjaap 		}
13925068Sjaap 		for(ilin = 0; ilin < nlin; ilin++)
14025068Sjaap 			if(k = lspan(ilin, icol)){
14125068Sjaap 				s = table[ilin][icol - k].col;
14225068Sjaap 				if(!real(s) || barent(s) || vspen(s))
14325068Sjaap 					continue;
14425068Sjaap 				printf(".nr %d ", TMP);
14525068Sjaap 				wide(table[ilin][icol - k].col,
14625068Sjaap 				      FN(ilin, icol - k), SZ(ilin, icol - k));
14725068Sjaap 				for(ik = k; ik >= 0; ik--){
14825068Sjaap 					printf("-\\n(%2s", reg(icol-ik,CRIGHT));
14925068Sjaap 					if(!expflg && ik > 0)
15025068Sjaap 						printf( "-%dn", sep[icol - ik]);
15125068Sjaap 				}
15225068Sjaap 				printf("\n");
15325068Sjaap 				printf(".if \\n(%d>0 .nr %d \\n(%d/%d\n",
15425068Sjaap 							TMP, TMP, TMP, k);
15525068Sjaap 				printf(".if \\n(%d<0 .nr %d 0\n",
15625068Sjaap 							TMP, TMP);
15725068Sjaap 				for(ik = 1; ik <= k; ik++){
15825068Sjaap 					if(doubled[icol - k + ik]){
15925068Sjaap 						printf(".nr %2s +\\n(%d/2\n",
16025068Sjaap 							reg(icol - k + ik,CMID),
16125068Sjaap 								TMP);
16225068Sjaap 					}
16325068Sjaap 					printf(".nr %2s +\\n(%d\n",
16425068Sjaap 						reg(icol - k + ik, CRIGHT),TMP);
16525068Sjaap 				}
16625068Sjaap 			}
16725068Sjaap 	}
16825068Sjaap 	if(textflg)
16925068Sjaap 		untext();
17025068Sjaap 	/*
17125068Sjaap 	 * if even requested, make all columns widest width
17225068Sjaap 	 */
17325068Sjaap 
17425068Sjaap #define TMP1 S1
17525068Sjaap #define TMP2 S2
17625068Sjaap 
17725068Sjaap 	if(evenflg){
17825068Sjaap 		printf(".nr %d 0\n", TMP);
17925068Sjaap 		for(icol = 0; icol < ncol; icol++){
18025068Sjaap 			if(evenup[icol] == 0)
18125068Sjaap 				continue;
18225068Sjaap 			printf(".if \\n(%2s>\\n(%d .nr %d \\n(%2s\n",
18325068Sjaap 				reg(icol, CRIGHT), TMP, TMP, reg(icol, CRIGHT));
18425068Sjaap 		}
18525068Sjaap 		for(icol = 0; icol < ncol; icol++){
18625068Sjaap  			/*
18725068Sjaap 			 * if column not evened just retain old interval
18825068Sjaap 			 */
18925068Sjaap 			if(evenup[icol] == 0)
19025068Sjaap 				continue;
19125068Sjaap 			if(doubled[icol])
19225068Sjaap 				printf(
19325068Sjaap 				   ".nr %2s (100*\\n(%2s/\\n(%2s)*\\n(%d/100\n",
19425068Sjaap 				   	reg(icol, CMID), reg(icol, CMID),
19525068Sjaap 				   		reg(icol, CRIGHT), TMP);
19625068Sjaap 			/*
19725068Sjaap 		 	* that nonsense with the 100's and parens tries to avoid
19825068Sjaap 		 	* overflow while proportionally shifting the middle of
19925068Sjaap 		 	* the number
20025068Sjaap 		 	*/
20125068Sjaap 			printf(".nr %2s \\n(%d\n", reg(icol, CRIGHT), TMP);
20225068Sjaap 		}
20325068Sjaap 	}
20425068Sjaap 	/*
20525068Sjaap 	 * now adjust for total table width
20625068Sjaap 	 */
20725068Sjaap 	for(tsep = icol = 0; icol < ncol; icol++)
20825068Sjaap 		tsep += sep[icol];
20925068Sjaap 	if(expflg){
21025068Sjaap 		printf(".nr %d 0", TMP);
21125068Sjaap 		for(icol = 0; icol < ncol; icol++)
21225068Sjaap 			printf("+\\n(%2s", reg(icol, CRIGHT));
21325068Sjaap 		printf("\n");
21425068Sjaap 		/*
21525068Sjaap 		 * Bug fix: Most users expect the expand to take place
21625068Sjaap 		 * over the line length minus the current indentation
21725068Sjaap 		 * (I do as well, a bit ugly to see the table creeping
21825068Sjaap 		 * in the right margin (jna))
21925068Sjaap 		 */
22025068Sjaap 		printf(".nr %d \\n(.l-\\n(.i-\\n(%d\n", TMP, TMP);
22125068Sjaap 		if(boxflg || dboxflg || allflg)
22225068Sjaap 			tsep += 1;
22325068Sjaap 		else
22425068Sjaap 			tsep -= sep[ncol - 1];
22525068Sjaap 		printf(".nr %d \\n(%d/%d\n", TMP, TMP, tsep);
22625068Sjaap 		printf(".if \\n(%d<0 .nr %d 0\n", TMP, TMP);
22725068Sjaap 	} else
22825068Sjaap 		printf(".nr %d 1n\n", TMP);
22925068Sjaap 	printf(".nr %2s 0\n", reg(-1,CRIGHT));
23025068Sjaap 	tsep = (boxflg || allflg || dboxflg || left1flg) ? 1 : 0;
23125068Sjaap 	for(icol = 0; icol < ncol; icol++){
23225068Sjaap 		printf(".nr %2s \\n(%2s+(%d*\\n(%d)\n", reg(icol, CLEFT),
23325068Sjaap 					reg(icol -1, CRIGHT), tsep, TMP);
23425068Sjaap 		printf(".nr %2s +\\n(%2s\n",reg(icol, CRIGHT),reg(icol, CLEFT));
23525068Sjaap 		if(doubled[icol]){
23625068Sjaap 			/*
23725068Sjaap 			 * the next line is last-ditch effort to avoid
23825068Sjaap 			 * zero field width
23925068Sjaap 			 */
24025068Sjaap 			/*
24125068Sjaap 			printf(".if \\n(%2s=0 .nr %2s 1\n", reg(icol,CMID),
24225068Sjaap 		   				reg(icol,CMID));
24325068Sjaap 			 */
24425068Sjaap 			printf(".nr %2s +\\n(%2s\n", reg(icol, CMID),
24525068Sjaap 							reg(icol, CLEFT));
24625068Sjaap 			/*
24725068Sjaap 			printf(".if n .if \\n(%2s%%24>0 .nr %2s +12u\n",
24825068Sjaap 					reg(icol, CMID), reg(icol, CMID));
24925068Sjaap 			 */
25025068Sjaap 		}
25125068Sjaap 		tsep = sep[icol];
25225068Sjaap 	}
25325068Sjaap 	if(rightl)
25425068Sjaap 		printf(".nr %2s (\\n(%2s+\\n(%2s)/2\n", reg(ncol-1, CRIGHT),
25525068Sjaap 							reg(ncol-1, CLEFT),
25625068Sjaap 							reg(ncol-2, CRIGHT));
25725068Sjaap 	printf(".nr TW \\n(%2s\n", reg(ncol-1, CRIGHT));
25825068Sjaap 	if(boxflg || allflg || dboxflg)
25925068Sjaap 		printf(".nr TW +%d*\\n(%d\n", sep[ncol - 1], TMP);
26025068Sjaap 	printf(
261*64045Sbostic ".if t .if\\n(TW>\\n(.lu .tm Table at line %d file %s is too wide - \\n(TW units\n",
26230059Sjaap 		iline - 1, strlen(oldname) ? oldname : ifile);
26325068Sjaap 	return;
26425068Sjaap }
26525068Sjaap 
26625068Sjaap /*
26725068Sjaap  * tell troff to calculate width of an entry
26825068Sjaap  */
wide(s,fn,size)26925068Sjaap wide(s, fn, size)
27025068Sjaap char *s, *size, *fn;
27125068Sjaap {
27225068Sjaap 	/*
27325068Sjaap 	 * if s is a pointer to a string, calculate the with of that string
27425068Sjaap 	 */
27525068Sjaap 	if(point(s)){
27625068Sjaap 		printf("\\w%c", F1);
27725068Sjaap 		if(*fn > 0)
27825068Sjaap 			putfont(fn);
27925068Sjaap 		if(*size)
28025068Sjaap 			putsize(size);
28125068Sjaap 		printf("%s", s);
28225068Sjaap 		if(*fn > 0)
28325068Sjaap 			putfont("P");
28425068Sjaap 		if(*size)
28525068Sjaap 			putsize("0");
28625068Sjaap 		printf("%c", F1);
28725068Sjaap 	} else
28825068Sjaap 		/*
28925068Sjaap 		 * it is the name of a diversion, so we know the witdh
29025068Sjaap 		 * is in <diversionname>-
29125068Sjaap 		 */
29225068Sjaap 		printf("\\n(%c-", s);
29325068Sjaap }
29425068Sjaap 
filler(s)29525068Sjaap filler(s)
29625068Sjaap char *s;
29725068Sjaap {
29825068Sjaap 	return(point(s) && s[0] == '\\' && s[1] == 'R');
29925068Sjaap }
300